Skip to content

Commit

Permalink
Merge v2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mster committed Jul 13, 2021
2 parents d4e6c0c + ee28849 commit e72a975
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 91 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
language: node_js
node_js:
- 10
- 12
- 14
148 changes: 62 additions & 86 deletions lib/mosh.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = mosh;

async function mosh(...args) {
// it's cool cuz it's nerdy.
const [err, source, mode, write, ext] = await preflightChecks(...args);
const [err, write, source, mode, ext] = await preflightChecks(...args);

// callback with error if using cb, else throw
if (err) {
Expand All @@ -35,133 +35,108 @@ async function mosh(...args) {
// encode image
const encodedMosh = encode(imgBuff, [width, height], ext);

// return data if we aren't using a CB
if (!write) return Buffer.from(encodedMosh);

write(null, Buffer.from(encodedMosh));
}

async function preflightChecks(...args) {
let [source, mode, write] = args;
let ext;

// source validation
const sourceIsBuffer = Buffer.isBuffer(source);
const sourceIsString = typeof source === "string" || source instanceof String;

// validate source image; may be a buffer or path.
let sourceIsPath, sourcePath, ext;
if (
!source ||
(source &&
!Buffer.isBuffer(source) &&
!(sourceIsPath = source.constructor.name === "String"))
) {
if (!source || (!sourceIsString && !sourceIsBuffer)) {
return [
"Invalid image source -- source must be of type String (path) or Buffer.",
null,
null,
write,
];
}

if (sourceIsPath) {
if (!(sourcePath = await validatePath(source))) {
return [
`Invalid source path: ${source}\n\tFile does not exist.`,
null,
null,
write,
];
if (sourceIsString) {
const sourcePath = await validatePath(source);

if (!sourcePath) {
return [`Invalid source path: ${source}\nFile does not exist.`, write];
}

ext = parse(sourcePath).ext.replace(".", "");

if (!validateExtension(ext)) {
return [`Invalid file type: ${ext}`, null, null, write];
return [`Invalid file type: ${ext}`, write];
}

// load image data
try {
source = await readFile(sourcePath);
} catch (e) {
return [e.message, null, null, write];
} catch (err) {
return [e.message, write];
}
}

// valid mode, if passed; checks against supported modes.
let isString, isArray;
if (
mode &&
!(isString = mode.constructor.name === "String") &&
!(isArray = mode.constructor.name === "Array")
) {
// ext validation
if (!ext) {
const fileType = await FileType.fromBuffer(source);

if (fileType?.ext && validateExtension(fileType?.ext)) {
ext = fileType.ext;
} else {
return [`Invalid file type, requires supported image file.`, write];
}
}

// mode validation
const modeIsString = typeof mode === "string" || mode instanceof String;
const modeIsArray = Array.isArray(mode);

if (mode && !modeIsString && !modeIsArray) {
return [
`Invalid mode: '${mode}'; mode must be of type String or Array.`,
null,
null,
write,
];
} else if (mode) {
if (isArray) {
let e = [];
mode.forEach((m, i) => {
if (!mosh.MODES[m] && m !== null) e.push(m);

// assign random mode for any null values
if (m === null) mode.splice(i, 1, randomMode());
});

if (e.length > 0)
return [`Invalid mosh modes: '${e.join(",")}'`, null, null, write];
}

if (isString) {
if (!mosh.MODES[mode])
return [`Invalid mosh mode: '${mode}'`, null, null, write];

mode = [mode];
}
}

if (!mode) mode = [randomMode()];
if (mode && modeIsString) {
if (!mosh.MODES[mode]) {
return [`Invalid mosh mode: '${mode}'`, write];
}

// ext may be passed instead of write for buffer return
if (
write &&
write.constructor.name === "String" &&
validateExtension(write.replace(".", ""))
) {
ext = write;
write = null;
mode = [mode];
}

if (!ext) {
const fileType = await FileType.fromBuffer(source);
if (mode && modeIsArray) {
let e = [];
mode.forEach((m, i) => {
if (!mosh.MODES[m] && m != null) e.push(m);

if (fileType && fileType.ext && validateExtension(fileType.ext)) {
ext = fileType.ext;
} else {
return [
`Invalid file type, requires supported image file.`,
null,
null,
write,
];
// assign random mode for any null values
if (m === null) mode.splice(i, 1, randomMode());
});

if (e.length > 0) {
return [`Invalid mosh modes: '${e.join(",")}'`, write];
}
}

// validate write, if passed; may be cb function or path.
let writeIsPath, writePath;
if (
write &&
!(writeIsPath = write.constructor.name === "String") &&
write.constructor.name !== "Function"
) {
return [`Invalid callback, or write path.`, null, null, write];
if (!mode) mode = [randomMode()];

// write validation
const writeIsString = typeof write === "string" || write instanceof String;
const writeIsCb = write instanceof Function;

if (write && !writeIsString && !writeIsCb) {
return [`Invalid callback, or write path.`, write];
}

if (writeIsPath) {
if (writeIsString) {
// bubble up path errors
const dir = dirname(write);
const filename = basename(write);
const writePath = await validatePath(dir);

if (!(writePath = await validatePath(dir))) {
return [`Invalid write location: ${dir}`, null, null, write];
if (!writePath) {
return [`Invalid write location: ${dir}`, write];
}

// prepare write function
Expand All @@ -171,7 +146,7 @@ async function preflightChecks(...args) {
};
}

return [null, source, mode, write, ext];
return [null, write, source, mode, ext];
}

async function validatePath(fpath) {
Expand All @@ -181,6 +156,7 @@ async function validatePath(fpath) {
}

try {
// stat will reject when the path is invalid
await stat(fpath);
return fpath;
} catch (err) {
Expand All @@ -205,7 +181,7 @@ function validateExtension(ext) {
}

function handleError(msg, cb) {
const usingCb = cb && cb.constructor.name === "Function";
const usingCb = cb && cb instanceof Function;
const error = new Error(msg);

if (usingCb) cb(error);
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datamosh",
"version": "2.1.0",
"version": "2.1.1",
"description": "Edit images via buffers.",
"keywords": [
"datamosh",
Expand All @@ -15,6 +15,9 @@
"buffer",
"glitch"
],
"engines": {
"node": ">=14.0.0"
},
"author": "Michael Sterpka <[email protected]>",
"contributors": [
"Tyler Laskey <[email protected]> (https://github.com/tlaskey)"
Expand Down
4 changes: 2 additions & 2 deletions tests/preflight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("Preflight source checks should", () => {

expect(enoent.constructor).toBe(Error);
expect(enoent.message).toBe(
`Invalid source path: ${path}\n\tFile does not exist.`
`Invalid source path: ${path}\nFile does not exist.`
);
});
});
Expand Down Expand Up @@ -119,7 +119,7 @@ describe("Preflight write checks should", () => {

expect(badWrite.constructor).toBe(Error);
expect(badWrite.message).toBe(
`Invalid source path: ${path}\n\tFile does not exist.`
`Invalid source path: ${path}\nFile does not exist.`
);
});
});

0 comments on commit e72a975

Please sign in to comment.