memory-extract 0.1.0 → 0.1.2

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.
@@ -0,0 +1,10 @@
1
+ export {
2
+ buildBrowseListingDocument,
3
+ buildInteractiveBrowseDocument,
4
+ buildMemoryBrowseListings,
5
+ collectVirtualDirectoryPaths,
6
+ isHtmlEntry,
7
+ listVirtualEntries,
8
+ parentVirtualPath,
9
+ resolveMemoryPlayMode,
10
+ } from "../src/memoryPlay.js";
package/tools/unpack.mjs CHANGED
@@ -7,21 +7,32 @@ import {
7
7
  listManifestFiles,
8
8
  readMemoryFile,
9
9
  } from "./memoryFormat.mjs";
10
- import { loadHostProject } from "./hostProject.mjs";
10
+ import {
11
+ loadHostProject,
12
+ parseUnpackArgv,
13
+ resolveUnpackDir,
14
+ } from "./hostProject.mjs";
11
15
 
12
16
  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
+ const host = await loadHostProject(process.cwd());
18
+ const { input, out: explicitOut } = parseUnpackArgv(argv);
19
+
20
+ if (!input) {
21
+ throw new Error("Specify a memory PNG path.");
22
+ }
17
23
 
18
- return { input, outDir };
24
+ return { input, explicitOut, host };
19
25
  };
20
26
 
21
27
  const main = async () => {
22
- const { input, outDir } = await parseArgs(process.argv.slice(2));
28
+ const { input, explicitOut, host } = await parseArgs(process.argv.slice(2));
23
29
  const buffer = await readFile(input);
24
30
  const { png, manifest, fileBytes, version } = extractMemory(buffer);
31
+ const outDir = resolveUnpackDir({
32
+ manifestSource: manifest.source ?? "",
33
+ explicitOut,
34
+ host,
35
+ });
25
36
 
26
37
  await mkdir(outDir, { recursive: true });
27
38
  await writeFile(path.join(outDir, "cover.png"), png);
@@ -1,62 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { readFile, stat, writeFile } from "node:fs/promises";
3
- import path from "node:path";
4
- import { MEMORY_VERSION } from "./memoryFormat.mjs";
5
-
6
- export const CACHE_FILE = ".memory-pack-cache.json";
7
-
8
- export const hashBuffer = (buffer) =>
9
- createHash("sha256").update(buffer).digest("hex");
10
-
11
- export const buildPackFingerprint = (options, files, cover) => ({
12
- cacheVersion: 1,
13
- formatVersion: MEMORY_VERSION,
14
- name: options.name,
15
- entry: options.entry,
16
- out: path.resolve(options.out),
17
- exclude: [...options.exclude].sort(),
18
- coverMaxSize: options.coverMaxSize,
19
- coverResize: options.coverResize,
20
- cover: hashBuffer(cover),
21
- files: Object.fromEntries(
22
- Object.keys(files)
23
- .sort()
24
- .map((filePath) => [filePath, hashBuffer(files[filePath])])
25
- ),
26
- });
27
-
28
- export const fingerprintsEqual = (left, right) =>
29
- JSON.stringify(left) === JSON.stringify(right);
30
-
31
- export const cachePathFor = (rootDir) => path.join(rootDir, CACHE_FILE);
32
-
33
- export const loadPackCache = async (rootDir) => {
34
- try {
35
- const raw = await readFile(cachePathFor(rootDir), "utf8");
36
- return JSON.parse(raw);
37
- } catch {
38
- return null;
39
- }
40
- };
41
-
42
- export const savePackCache = async (rootDir, fingerprint) => {
43
- await writeFile(
44
- cachePathFor(rootDir),
45
- `${JSON.stringify({ fingerprint }, null, 2)}\n`
46
- );
47
- };
48
-
49
- export const isPackUpToDate = async (rootDir, fingerprint, outputPath) => {
50
- try {
51
- await stat(outputPath);
52
- } catch {
53
- return false;
54
- }
55
-
56
- const cache = await loadPackCache(rootDir);
57
- if (!cache?.fingerprint) {
58
- return false;
59
- }
60
-
61
- return fingerprintsEqual(cache.fingerprint, fingerprint);
62
- };
@@ -1,55 +0,0 @@
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
- };