Skip to content

Commit

Permalink
Merge v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mster committed Jun 18, 2021
2 parents c795415 + f1cee97 commit 3517f80
Show file tree
Hide file tree
Showing 7 changed files with 3,222 additions and 66 deletions.
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
.DS_Store

# images
images/
*.jpeg
*.jpg
*.png

node_modules/
example.js

Expand Down
35 changes: 16 additions & 19 deletions lib/mosh.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const decode = require("image-decode");
const FileType = require("file-type");
const { parse, dirname, basename, join } = require("path");
const { homedir } = require("os");
const { stat, readFile, writeFile } = require("fs/promises");
const { promisify } = require("util");
const { stat: st, readFile: rF, writeFile: wF } = require("fs");
const stat = promisify(st),
readFile = promisify(rF),
writeFile = promisify(wF);

module.exports = mosh;

Expand All @@ -31,14 +35,13 @@ async function mosh(...args) {
// encode image
const encodedMosh = encode(imgBuff, [width, height], ext);

if (!write) return encodedMosh;
if (!write) return Buffer.from(encodedMosh);

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

async function preflightChecks(...args) {
let [source, mode, write] = args;
const modeNames = Object.keys(mosh.MODES);

// validate source image; may be a buffer or path.
let sourceIsPath, sourcePath, ext;
Expand Down Expand Up @@ -76,7 +79,7 @@ async function preflightChecks(...args) {
try {
source = await readFile(sourcePath);
} catch (e) {
return [e.msg, null, null, write];
return [e.message, null, null, write];
}
}

Expand All @@ -95,28 +98,21 @@ async function preflightChecks(...args) {
];
} else if (mode) {
if (isArray) {
let e = [];
mode.forEach((m, i) => {
if (!mosh.MODES[m] && m !== null)
return [
`Invalid mosh mode: '${m}';\n\tModes: ${modeNames.join(", ")}`,
null,
null,
write,
];
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}';\n\tModes: ${modeNames.join(", ")}`,
null,
null,
write,
];
return [`Invalid mosh mode: '${mode}'`, null, null, write];

mode = [mode];
}
Expand Down Expand Up @@ -169,8 +165,9 @@ async function preflightChecks(...args) {
}

// prepare write function
write = async (data) => {
await writeFile(join(writePath, filename), data);
write = async (err, data) => {
if (!err) await writeFile(join(writePath, filename), data);
else throw err;
};
}

Expand Down
Loading

0 comments on commit 3517f80

Please sign in to comment.