Skip to content

Commit ee28849

Browse files
committed
v2.1.1 Refactor mosh
1 parent d4e6c0c commit ee28849

File tree

4 files changed

+68
-91
lines changed

4 files changed

+68
-91
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
language: node_js
22
node_js:
3-
- 10
4-
- 12
53
- 14

lib/mosh.js

Lines changed: 62 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = mosh;
1515

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

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

38+
// return data if we aren't using a CB
3839
if (!write) return Buffer.from(encodedMosh);
39-
4040
write(null, Buffer.from(encodedMosh));
4141
}
4242

4343
async function preflightChecks(...args) {
4444
let [source, mode, write] = args;
45+
let ext;
46+
47+
// source validation
48+
const sourceIsBuffer = Buffer.isBuffer(source);
49+
const sourceIsString = typeof source === "string" || source instanceof String;
4550

46-
// validate source image; may be a buffer or path.
47-
let sourceIsPath, sourcePath, ext;
48-
if (
49-
!source ||
50-
(source &&
51-
!Buffer.isBuffer(source) &&
52-
!(sourceIsPath = source.constructor.name === "String"))
53-
) {
51+
if (!source || (!sourceIsString && !sourceIsBuffer)) {
5452
return [
5553
"Invalid image source -- source must be of type String (path) or Buffer.",
56-
null,
57-
null,
5854
write,
5955
];
6056
}
6157

62-
if (sourceIsPath) {
63-
if (!(sourcePath = await validatePath(source))) {
64-
return [
65-
`Invalid source path: ${source}\n\tFile does not exist.`,
66-
null,
67-
null,
68-
write,
69-
];
58+
if (sourceIsString) {
59+
const sourcePath = await validatePath(source);
60+
61+
if (!sourcePath) {
62+
return [`Invalid source path: ${source}\nFile does not exist.`, write];
7063
}
7164

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

7467
if (!validateExtension(ext)) {
75-
return [`Invalid file type: ${ext}`, null, null, write];
68+
return [`Invalid file type: ${ext}`, write];
7669
}
7770

78-
// load image data
7971
try {
8072
source = await readFile(sourcePath);
81-
} catch (e) {
82-
return [e.message, null, null, write];
73+
} catch (err) {
74+
return [e.message, write];
8375
}
8476
}
8577

86-
// valid mode, if passed; checks against supported modes.
87-
let isString, isArray;
88-
if (
89-
mode &&
90-
!(isString = mode.constructor.name === "String") &&
91-
!(isArray = mode.constructor.name === "Array")
92-
) {
78+
// ext validation
79+
if (!ext) {
80+
const fileType = await FileType.fromBuffer(source);
81+
82+
if (fileType?.ext && validateExtension(fileType?.ext)) {
83+
ext = fileType.ext;
84+
} else {
85+
return [`Invalid file type, requires supported image file.`, write];
86+
}
87+
}
88+
89+
// mode validation
90+
const modeIsString = typeof mode === "string" || mode instanceof String;
91+
const modeIsArray = Array.isArray(mode);
92+
93+
if (mode && !modeIsString && !modeIsArray) {
9394
return [
9495
`Invalid mode: '${mode}'; mode must be of type String or Array.`,
95-
null,
96-
null,
9796
write,
9897
];
99-
} else if (mode) {
100-
if (isArray) {
101-
let e = [];
102-
mode.forEach((m, i) => {
103-
if (!mosh.MODES[m] && m !== null) e.push(m);
104-
105-
// assign random mode for any null values
106-
if (m === null) mode.splice(i, 1, randomMode());
107-
});
108-
109-
if (e.length > 0)
110-
return [`Invalid mosh modes: '${e.join(",")}'`, null, null, write];
111-
}
112-
113-
if (isString) {
114-
if (!mosh.MODES[mode])
115-
return [`Invalid mosh mode: '${mode}'`, null, null, write];
116-
117-
mode = [mode];
118-
}
11998
}
12099

121-
if (!mode) mode = [randomMode()];
100+
if (mode && modeIsString) {
101+
if (!mosh.MODES[mode]) {
102+
return [`Invalid mosh mode: '${mode}'`, write];
103+
}
122104

123-
// ext may be passed instead of write for buffer return
124-
if (
125-
write &&
126-
write.constructor.name === "String" &&
127-
validateExtension(write.replace(".", ""))
128-
) {
129-
ext = write;
130-
write = null;
105+
mode = [mode];
131106
}
132107

133-
if (!ext) {
134-
const fileType = await FileType.fromBuffer(source);
108+
if (mode && modeIsArray) {
109+
let e = [];
110+
mode.forEach((m, i) => {
111+
if (!mosh.MODES[m] && m != null) e.push(m);
135112

136-
if (fileType && fileType.ext && validateExtension(fileType.ext)) {
137-
ext = fileType.ext;
138-
} else {
139-
return [
140-
`Invalid file type, requires supported image file.`,
141-
null,
142-
null,
143-
write,
144-
];
113+
// assign random mode for any null values
114+
if (m === null) mode.splice(i, 1, randomMode());
115+
});
116+
117+
if (e.length > 0) {
118+
return [`Invalid mosh modes: '${e.join(",")}'`, write];
145119
}
146120
}
147121

148-
// validate write, if passed; may be cb function or path.
149-
let writeIsPath, writePath;
150-
if (
151-
write &&
152-
!(writeIsPath = write.constructor.name === "String") &&
153-
write.constructor.name !== "Function"
154-
) {
155-
return [`Invalid callback, or write path.`, null, null, write];
122+
if (!mode) mode = [randomMode()];
123+
124+
// write validation
125+
const writeIsString = typeof write === "string" || write instanceof String;
126+
const writeIsCb = write instanceof Function;
127+
128+
if (write && !writeIsString && !writeIsCb) {
129+
return [`Invalid callback, or write path.`, write];
156130
}
157131

158-
if (writeIsPath) {
132+
if (writeIsString) {
159133
// bubble up path errors
160134
const dir = dirname(write);
161135
const filename = basename(write);
136+
const writePath = await validatePath(dir);
162137

163-
if (!(writePath = await validatePath(dir))) {
164-
return [`Invalid write location: ${dir}`, null, null, write];
138+
if (!writePath) {
139+
return [`Invalid write location: ${dir}`, write];
165140
}
166141

167142
// prepare write function
@@ -171,7 +146,7 @@ async function preflightChecks(...args) {
171146
};
172147
}
173148

174-
return [null, source, mode, write, ext];
149+
return [null, write, source, mode, ext];
175150
}
176151

177152
async function validatePath(fpath) {
@@ -181,6 +156,7 @@ async function validatePath(fpath) {
181156
}
182157

183158
try {
159+
// stat will reject when the path is invalid
184160
await stat(fpath);
185161
return fpath;
186162
} catch (err) {
@@ -205,7 +181,7 @@ function validateExtension(ext) {
205181
}
206182

207183
function handleError(msg, cb) {
208-
const usingCb = cb && cb.constructor.name === "Function";
184+
const usingCb = cb && cb instanceof Function;
209185
const error = new Error(msg);
210186

211187
if (usingCb) cb(error);

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "datamosh",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "Edit images via buffers.",
55
"keywords": [
66
"datamosh",
@@ -15,6 +15,9 @@
1515
"buffer",
1616
"glitch"
1717
],
18+
"engines": {
19+
"node": ">=14.0.0"
20+
},
1821
"author": "Michael Sterpka <michaelsterpka@gmail.com>",
1922
"contributors": [
2023
"Tyler Laskey <tlaskey3@gmail.com> (https://github.com/tlaskey)"

tests/preflight.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("Preflight source checks should", () => {
6060

6161
expect(enoent.constructor).toBe(Error);
6262
expect(enoent.message).toBe(
63-
`Invalid source path: ${path}\n\tFile does not exist.`
63+
`Invalid source path: ${path}\nFile does not exist.`
6464
);
6565
});
6666
});
@@ -119,7 +119,7 @@ describe("Preflight write checks should", () => {
119119

120120
expect(badWrite.constructor).toBe(Error);
121121
expect(badWrite.message).toBe(
122-
`Invalid source path: ${path}\n\tFile does not exist.`
122+
`Invalid source path: ${path}\nFile does not exist.`
123123
);
124124
});
125125
});

0 commit comments

Comments
 (0)