memory-extract 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/package.json +35 -0
- package/src/index.js +25 -0
- package/src/loadMemory.js +198 -0
- package/src/memoryFormat.js +213 -0
- package/src/payloadFormat.js +171 -0
- package/tools/blankCover.mjs +36 -0
- package/tools/glob.mjs +12 -0
- package/tools/hostProject.mjs +45 -0
- package/tools/memoryFormat.mjs +277 -0
- package/tools/pack.mjs +239 -0
- package/tools/packCache.mjs +62 -0
- package/tools/pngInspect.mjs +22 -0
- package/tools/pngUtils.mjs +36 -0
- package/tools/resizeCover.mjs +55 -0
- package/tools/unpack.mjs +53 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import sharp from "sharp";
|
|
2
|
+
import { inspectPng } from "./pngInspect.mjs";
|
|
3
|
+
|
|
4
|
+
const DEFAULT_MAX_BYTES = 512 * 1024;
|
|
5
|
+
|
|
6
|
+
export const prepareCover = async (
|
|
7
|
+
input,
|
|
8
|
+
{ maxSize = 512, resize = true, maxBytes = DEFAULT_MAX_BYTES } = {}
|
|
9
|
+
) => {
|
|
10
|
+
if (!resize || maxSize <= 0) {
|
|
11
|
+
return {
|
|
12
|
+
buffer: input,
|
|
13
|
+
info: inspectPng(input),
|
|
14
|
+
changed: false,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const metadata = await sharp(input).metadata();
|
|
19
|
+
const original = {
|
|
20
|
+
width: metadata.width ?? 0,
|
|
21
|
+
height: metadata.height ?? 0,
|
|
22
|
+
bytes: input.length,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const needsResize = original.width > maxSize || original.height > maxSize;
|
|
26
|
+
const needsCompress = original.bytes > maxBytes;
|
|
27
|
+
|
|
28
|
+
if (!needsResize && !needsCompress) {
|
|
29
|
+
return {
|
|
30
|
+
buffer: input,
|
|
31
|
+
info: inspectPng(input),
|
|
32
|
+
changed: false,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let pipeline = sharp(input);
|
|
37
|
+
|
|
38
|
+
if (needsResize) {
|
|
39
|
+
pipeline = pipeline.resize(maxSize, maxSize, {
|
|
40
|
+
fit: "inside",
|
|
41
|
+
withoutEnlargement: true,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const buffer = await pipeline
|
|
46
|
+
.png({ compressionLevel: 9, effort: 10 })
|
|
47
|
+
.toBuffer();
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
buffer,
|
|
51
|
+
info: inspectPng(buffer),
|
|
52
|
+
changed: true,
|
|
53
|
+
from: original,
|
|
54
|
+
};
|
|
55
|
+
};
|
package/tools/unpack.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { resolveSafePath } from "../src/payloadFormat.js";
|
|
5
|
+
import {
|
|
6
|
+
extractMemory,
|
|
7
|
+
listManifestFiles,
|
|
8
|
+
readMemoryFile,
|
|
9
|
+
} from "./memoryFormat.mjs";
|
|
10
|
+
import { loadHostProject } from "./hostProject.mjs";
|
|
11
|
+
|
|
12
|
+
const parseArgs = async (argv) => {
|
|
13
|
+
const rootDir = process.cwd();
|
|
14
|
+
const host = await loadHostProject(rootDir);
|
|
15
|
+
const input = path.resolve(argv[0] ?? host.out);
|
|
16
|
+
const outDir = path.resolve(argv[1] ?? host.unpackDir);
|
|
17
|
+
|
|
18
|
+
return { input, outDir };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const main = async () => {
|
|
22
|
+
const { input, outDir } = await parseArgs(process.argv.slice(2));
|
|
23
|
+
const buffer = await readFile(input);
|
|
24
|
+
const { png, manifest, fileBytes, version } = extractMemory(buffer);
|
|
25
|
+
|
|
26
|
+
await mkdir(outDir, { recursive: true });
|
|
27
|
+
await writeFile(path.join(outDir, "cover.png"), png);
|
|
28
|
+
|
|
29
|
+
for (const filePath of listManifestFiles(manifest)) {
|
|
30
|
+
const target = resolveSafePath(
|
|
31
|
+
outDir,
|
|
32
|
+
filePath,
|
|
33
|
+
path.sep,
|
|
34
|
+
path.resolve.bind(path)
|
|
35
|
+
);
|
|
36
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
37
|
+
await writeFile(target, readMemoryFile(manifest, filePath, fileBytes));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
await writeFile(
|
|
41
|
+
path.join(outDir, "manifest.json"),
|
|
42
|
+
`${JSON.stringify(manifest, null, 2)}\n`
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
console.log(
|
|
46
|
+
`Extracted ${listManifestFiles(manifest).length} files to ${outDir} (format v${version})`
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
main().catch((error) => {
|
|
51
|
+
console.error(error.message ?? error);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
});
|