@afterpack/protection-map 0.1.0-rc.20260925T1347
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 +201 -0
- package/NOTICE +4 -0
- package/README.md +55 -0
- package/codec.mjs +292 -0
- package/index.d.ts +53 -0
- package/index.mjs +1 -0
- package/package.json +63 -0
- package/render-core.mjs +83 -0
- package/render.mjs +66 -0
- package/template.html +4302 -0
package/render-core.mjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { gzipSync } from "node:zlib";
|
|
4
|
+
import { encodeCompact } from "./codec.mjs";
|
|
5
|
+
|
|
6
|
+
export const LINEAGE_EMBED_CAP = 20;
|
|
7
|
+
export const PLACEHOLDER = "__AFTERPACK_DATA__";
|
|
8
|
+
|
|
9
|
+
export function defaultTemplatePath() {
|
|
10
|
+
return createRequire(import.meta.url).resolve("@afterpack/protection-map/template.html");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function readDefaultTemplate() {
|
|
14
|
+
return readFileSync(defaultTemplatePath(), "utf8");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function normalizeToFiles(raw) {
|
|
18
|
+
if (raw && Array.isArray(raw.files)) return raw;
|
|
19
|
+
return { files: [raw] };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function capLineage(data) {
|
|
23
|
+
let cappedRegions = 0;
|
|
24
|
+
let totalLineageStepsRemoved = 0;
|
|
25
|
+
for (const fileDoc of data.files || []) {
|
|
26
|
+
for (const region of fileDoc?.regions || []) {
|
|
27
|
+
if (Array.isArray(region.lineage) && region.lineage.length > LINEAGE_EMBED_CAP) {
|
|
28
|
+
const originalLength = region.lineage.length;
|
|
29
|
+
region.lineage = region.lineage.slice(0, LINEAGE_EMBED_CAP);
|
|
30
|
+
region.lineageCapped = originalLength;
|
|
31
|
+
cappedRegions += 1;
|
|
32
|
+
totalLineageStepsRemoved += originalLength - LINEAGE_EMBED_CAP;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { cappedRegions, totalLineageStepsRemoved };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function embedIntoTemplate(template, data) {
|
|
40
|
+
const occurrences = template.split(PLACEHOLDER).length - 1;
|
|
41
|
+
if (occurrences !== 1) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`expected exactly one "${PLACEHOLDER}" sentinel in the template, found ${occurrences}`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const safeJson = JSON.stringify(data).replace(/</g, "\\u003c");
|
|
47
|
+
return template.replace(PLACEHOLDER, () => safeJson);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function stripLineage(data) {
|
|
51
|
+
for (const fileDoc of data.files || []) {
|
|
52
|
+
for (const region of fileDoc?.regions || []) {
|
|
53
|
+
if (Array.isArray(region.lineage) && region.lineage.length > 0) region.lineage = [];
|
|
54
|
+
if (region.lineageCapped != null) delete region.lineageCapped;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function embedCompact(template, compact) {
|
|
60
|
+
const occurrences = template.split(PLACEHOLDER).length - 1;
|
|
61
|
+
if (occurrences !== 1) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`expected exactly one "${PLACEHOLDER}" sentinel in the template, found ${occurrences}`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
const json = JSON.stringify(compact);
|
|
67
|
+
const base64 = gzipSync(Buffer.from(json, "utf8"), { level: 9 }).toString("base64");
|
|
68
|
+
return template.replace(PLACEHOLDER, () => base64);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function renderProtectionMapHtml(raw, { template, includeLineage = true } = {}) {
|
|
72
|
+
const data = normalizeToFiles(raw);
|
|
73
|
+
let cappedRegions = 0;
|
|
74
|
+
let totalLineageStepsRemoved = 0;
|
|
75
|
+
if (includeLineage) {
|
|
76
|
+
({ cappedRegions, totalLineageStepsRemoved } = capLineage(data));
|
|
77
|
+
} else {
|
|
78
|
+
stripLineage(data);
|
|
79
|
+
}
|
|
80
|
+
const compact = encodeCompact(data, { includeLineage });
|
|
81
|
+
const html = embedCompact(template ?? readDefaultTemplate(), compact);
|
|
82
|
+
return { html, files: data.files.length, cappedRegions, totalLineageStepsRemoved };
|
|
83
|
+
}
|
package/render.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { LINEAGE_EMBED_CAP, renderProtectionMapHtml } from "./render-core.mjs";
|
|
5
|
+
|
|
6
|
+
function usageAndExit() {
|
|
7
|
+
console.error(
|
|
8
|
+
"usage: afterpack-protection-map <protmap.json> <out.html> [--template <file>] [--no-lineage]",
|
|
9
|
+
);
|
|
10
|
+
process.exit(2);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function main() {
|
|
14
|
+
const positional = [];
|
|
15
|
+
let includeLineage = true;
|
|
16
|
+
let templatePath;
|
|
17
|
+
const args = process.argv.slice(2);
|
|
18
|
+
for (let i = 0; i < args.length; i++) {
|
|
19
|
+
if (args[i] === "--no-lineage") includeLineage = false;
|
|
20
|
+
else if (args[i] === "--template") templatePath = args[++i] ?? usageAndExit();
|
|
21
|
+
else positional.push(args[i]);
|
|
22
|
+
}
|
|
23
|
+
const [jsonPath, outPath] = positional;
|
|
24
|
+
if (!jsonPath || !outPath || positional.length > 2) usageAndExit();
|
|
25
|
+
|
|
26
|
+
let raw;
|
|
27
|
+
try {
|
|
28
|
+
raw = JSON.parse(readFileSync(jsonPath, "utf8"));
|
|
29
|
+
} catch (err) {
|
|
30
|
+
console.error(`failed to read/parse ${jsonPath}: ${err.message}`);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let template;
|
|
35
|
+
if (templatePath) {
|
|
36
|
+
try {
|
|
37
|
+
template = readFileSync(templatePath, "utf8");
|
|
38
|
+
} catch (err) {
|
|
39
|
+
console.error(`failed to read template ${templatePath}: ${err.message}`);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let result;
|
|
45
|
+
try {
|
|
46
|
+
result = renderProtectionMapHtml(
|
|
47
|
+
raw,
|
|
48
|
+
template ? { template, includeLineage } : { includeLineage },
|
|
49
|
+
);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
console.error(err.message);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
writeFileSync(outPath, result.html, "utf8");
|
|
55
|
+
|
|
56
|
+
const bytes = Buffer.byteLength(result.html, "utf8");
|
|
57
|
+
console.log(`wrote ${outPath} (${(bytes / 1024).toFixed(1)} KB, ${result.files} file(s))`);
|
|
58
|
+
if (result.cappedRegions > 0) {
|
|
59
|
+
console.log(
|
|
60
|
+
`capped lineage on ${result.cappedRegions} region(s) to ${LINEAGE_EMBED_CAP} entries ` +
|
|
61
|
+
`(${result.totalLineageStepsRemoved} steps trimmed total; region.lineageCapped records the original count)`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
main();
|