TheYuchen commited on
Commit
c4e53ef
·
1 Parent(s): efd5605

Figure export: one <base>.bundle.zip download (STORE zip writer, no deps)

Browse files

Packaging decision resolved: zip, without the dependency. New
frontend/src/lib/zip.ts is a minimal STORE-method ZIP writer (local
file headers + central directory + EOCD, table-driven CRC-32, ~100
lines, pure JS). exportFigure now collects the .svg, the .png (hybrid/
raster path only) and the .provenance.json and downloads exactly ONE
<base>.bundle.zip — a single camera click can no longer trip browser
multi-download blocking, which silently dropped the sidecar and/or
PNG. In-SVG provenance <metadata> unchanged. New node lane
scripts/check_zip.test.ts asserts the CRC-32 check vector
("123456789" -> 0xCBF43926), the archive structure, and interop:
the fixture is listed with `python3 -m zipfile -l` and CRC-tested
with `-t` (Done testing). Camera button/claims-map wording and the
Wave-Q export contract + filename notes updated. Backlog entry
removed.

docs/AUDIT_BACKLOG.md CHANGED
@@ -16,8 +16,3 @@ deferred.
16
 
17
  ## Infra
18
 
19
- - figure export triple-download can be popup-blocked (SVG + PNG +
20
- provenance sidecar are three programmatic downloads from one click)
21
- [deferred — needs a packaging decision: zip the three artifacts
22
- (new dependency), drop to two downloads, or stage sequential
23
- prompts. Each changes the documented export contract.]
 
16
 
17
  ## Infra
18
 
 
 
 
 
 
docs/EVIDENCE_WORKBENCH.md CHANGED
@@ -901,9 +901,10 @@ the run to finish, then export three states —
901
  | 2 | batch 4 of 5 | funnel narrowing, still outside ±2pp | 2,048 shots, point 0.4878, ±2.16pp |
902
  | 3 | final | ⏹ stop + unspent budget | 2,560 of 4,096 shots, point 0.484375, ±1.93pp ≤ target |
903
 
904
- Files land as `evidence-theater_F0_batch2.svg`, `_batch4.svg`,
905
- `evidence-theater_F0.svg` — each regenerable from its own embedded
906
- provenance (scenario key + seed + trace_position).
 
907
 
908
  ### Illustrator-grade true-SVG export
909
 
@@ -960,9 +961,18 @@ is 2.5× on every machine. Default 2.5×; **4× print-resolution via
960
  alt/⌥-click or press-and-hold (≥550 ms) on any camera button**.
961
 
962
  File naming ties every artifact to its regeneration recipe:
963
- `<view>_<scenario|runid>[_batchK]` as the base —
964
- `<base>.svg`, `<base>_<scale>x.png`, `<base>.provenance.json`
965
- (e.g. `multiverse_F2_4x.png`, `evidence-theater_F0_batch2.svg`).
 
 
 
 
 
 
 
 
 
966
  The slug prefers the active scenario key (now recorded by the
967
  scenario loader and embedded as `provenance.scenario`), falling back
968
  to the first visible run_id.
@@ -995,9 +1005,10 @@ Flow HTML/SVG composite (raster-only as a whole).
995
  4. Provenance `<metadata id="provenance">` embedded (app version,
996
  export time, scenario key, run_ids/root_seeds/config_hashes,
997
  SharePayload graph, trace_position when scrubbed) + the
998
- `.provenance.json` sidecar for raster-only use. Note: Illustrator
999
- may drop `<metadata>` on re-save — the sidecar is the durable
1000
- copy; keep it next to the .ai file.
 
1001
 
1002
  ## UX comprehension + polish wave (SHIPPED 2026-07-06)
1003
 
 
901
  | 2 | batch 4 of 5 | funnel narrowing, still outside ±2pp | 2,048 shots, point 0.4878, ±2.16pp |
902
  | 3 | final | ⏹ stop + unspent budget | 2,560 of 4,096 shots, point 0.484375, ±1.93pp ≤ target |
903
 
904
+ Files land as `evidence-theater_F0_batch2.bundle.zip`,
905
+ `_batch4.bundle.zip`, `evidence-theater_F0.bundle.zip` (each bundle
906
+ holds the .svg + .provenance.json) — each regenerable from its own
907
+ embedded provenance (scenario key + seed + trace_position).
908
 
909
  ### Illustrator-grade true-SVG export
910
 
 
961
  alt/⌥-click or press-and-hold (≥550 ms) on any camera button**.
962
 
963
  File naming ties every artifact to its regeneration recipe:
964
+ `<view>_<scenario|runid>[_batchK]` as the base. A camera click
965
+ downloads ONE `<base>.bundle.zip` (STORE-method writer in
966
+ frontend/src/lib/zip.ts — pure JS, no dependency; CRC-32 and
967
+ python-zipfile interop asserted by the check_zip node lane)
968
+ containing `<base>.svg`, `<base>_<scale>x.png` (hybrid/raster path
969
+ only) and `<base>.provenance.json` — e.g. `multiverse_F2.bundle.zip`
970
+ holding `multiverse_F2_4x.png`, or
971
+ `evidence-theater_F0_batch2.bundle.zip` holding
972
+ `evidence-theater_F0_batch2.svg`. One download per click by design
973
+ (audit backlog): three programmatic downloads from one click could
974
+ trip browser multi-download blocking, silently dropping the sidecar
975
+ and/or PNG. The in-SVG provenance `<metadata>` is unchanged.
976
  The slug prefers the active scenario key (now recorded by the
977
  scenario loader and embedded as `provenance.scenario`), falling back
978
  to the first visible run_id.
 
1005
  4. Provenance `<metadata id="provenance">` embedded (app version,
1006
  export time, scenario key, run_ids/root_seeds/config_hashes,
1007
  SharePayload graph, trace_position when scrubbed) + the
1008
+ `.provenance.json` sidecar (inside the export's .bundle.zip) for
1009
+ raster-only use. Note: Illustrator may drop `<metadata>` on
1010
+ re-save — the sidecar is the durable copy; keep it next to the
1011
+ .ai file.
1012
 
1013
  ## UX comprehension + polish wave (SHIPPED 2026-07-06)
1014
 
frontend/scripts/check_zip.test.ts ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Byte-level verification of the STORE-method ZIP writer (lib/zip.ts)
2
+ // that packages figure-export bundles — pure functions, no DOM, so
3
+ // this runs in plain node:
4
+ // node --experimental-strip-types scripts/check_zip.test.ts
5
+ //
6
+ // What is verified:
7
+ // 1. CRC-32 against the canonical check vector ("123456789" →
8
+ // 0xCBF43926), the empty input, and a second published vector;
9
+ // 2. archive structure: local-header / central-directory / EOCD
10
+ // signatures at their computed offsets, STORE method, and the
11
+ // central directory landing exactly after the local records;
12
+ // 3. interop — the real claim: the fixture zip is written to disk
13
+ // and read back with `python3 -m zipfile` (-l listing with every
14
+ // bundled filename + true size, then -t CRC integrity test) —
15
+ // the same reader a reviewer would reach for first.
16
+
17
+ import assert from "node:assert/strict";
18
+ import { execFileSync } from "node:child_process";
19
+ import { writeFileSync } from "node:fs";
20
+ import { tmpdir } from "node:os";
21
+ import { join } from "node:path";
22
+ import { buildZip, crc32 } from "../src/lib/zip.ts";
23
+
24
+ const enc = new TextEncoder();
25
+
26
+ // -- 1. CRC-32 check vectors -------------------------------------------------
27
+ assert.equal(crc32(enc.encode("123456789")), 0xcbf43926);
28
+ assert.equal(crc32(new Uint8Array(0)), 0x00000000);
29
+ assert.equal(
30
+ crc32(enc.encode("The quick brown fox jumps over the lazy dog")),
31
+ 0x414fa339,
32
+ );
33
+
34
+ // -- 2. archive structure ------------------------------------------------
35
+ // Shaped like a real figure bundle: svg + raster png + provenance json.
36
+ const entries = [
37
+ { name: "evidence-theater_F0.svg", data: enc.encode("<svg>fixture</svg>") },
38
+ {
39
+ name: "evidence-theater_F0_2.5x.png",
40
+ data: new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 7]),
41
+ },
42
+ {
43
+ name: "evidence-theater_F0.provenance.json",
44
+ data: enc.encode(JSON.stringify({ app: "QuDA Studio", runs: [] })),
45
+ },
46
+ ];
47
+ const zip = buildZip(entries, new Date(2026, 6, 11, 12, 0, 0));
48
+ const dv = new DataView(zip.buffer, zip.byteOffset, zip.byteLength);
49
+ assert.equal(dv.getUint32(0, true), 0x04034b50); // first local header
50
+ const eocdAt = zip.length - 22;
51
+ assert.equal(dv.getUint32(eocdAt, true), 0x06054b50); // EOCD signature
52
+ assert.equal(dv.getUint16(eocdAt + 10, true), entries.length);
53
+ const cdOffset = dv.getUint32(eocdAt + 16, true);
54
+ assert.equal(dv.getUint32(cdOffset, true), 0x02014b50); // central dir sig
55
+ assert.equal(dv.getUint16(cdOffset + 10, true), 0); // method: STORE
56
+ // STORE local records are exactly header(30) + name + data, back to back.
57
+ const expectedCdOffset = entries.reduce(
58
+ (n, e) => n + 30 + e.name.length + e.data.length,
59
+ 0,
60
+ );
61
+ assert.equal(cdOffset, expectedCdOffset);
62
+
63
+ // -- 3. interop: python3's zipfile must accept the writer's bytes ---------
64
+ const fixture = join(tmpdir(), "quda_check_zip_fixture.zip");
65
+ writeFileSync(fixture, zip);
66
+ const listing = execFileSync("python3", ["-m", "zipfile", "-l", fixture], {
67
+ encoding: "utf8",
68
+ });
69
+ console.log(`$ python3 -m zipfile -l ${fixture}\n${listing}`);
70
+ for (const e of entries) {
71
+ const row = new RegExp(
72
+ `${e.name.replace(/[.]/g, "\\.")}\\s.*\\b${e.data.length}\\b`,
73
+ );
74
+ assert.match(listing, row, `listing row for ${e.name}`);
75
+ }
76
+ // -t decompresses every member and checks its CRC — the integrity claim.
77
+ const integrity = execFileSync("python3", ["-m", "zipfile", "-t", fixture], {
78
+ encoding: "utf8",
79
+ });
80
+ console.log(`$ python3 -m zipfile -t ${fixture}\n${integrity.trim()}`);
81
+ assert.match(integrity, /Done testing/);
82
+
83
+ console.log("check_zip: all assertions passed");
frontend/src/components/ClaimsMap.tsx CHANGED
@@ -98,7 +98,7 @@ const SECTIONS: ClaimSection[] = [
98
  },
99
  {
100
  claim: "Provenance-backed figures — every export regenerates bit-exactly from its own metadata",
101
- where: "Camera buttons on the canvas, Evidence board, evidence pane and theater (SVG + PNG + sidecar)",
102
  scenarios: ["F0"],
103
  },
104
  ],
 
98
  },
99
  {
100
  claim: "Provenance-backed figures — every export regenerates bit-exactly from its own metadata",
101
+ where: "Camera buttons on the canvas, Evidence board, evidence pane and theater (one .bundle.zip: SVG + PNG + sidecar)",
102
  scenarios: ["F0"],
103
  },
104
  ],
frontend/src/components/FigureExportButton.tsx CHANGED
@@ -5,7 +5,8 @@ import { exportFigure } from "../lib/figureExport";
5
 
6
  /**
7
  * Small camera button that exports its host view as a paper figure
8
- * (SVG + PNG + .provenance.json sidecar — see lib/figureExport.ts).
 
9
  * Placed on the canvas toolbar, the Multiverse board header, the
10
  * Evidence pane header and the Evidence theater. The button itself
11
  * never appears in exports (figureExport strips all <button>
@@ -27,7 +28,7 @@ export function FigureExportButton({
27
  }: {
28
  /** Resolve the DOM subtree to export at click time. */
29
  getTarget: () => HTMLElement | SVGSVGElement | null;
30
- /** Download basename (also names the provenance sidecar). */
31
  name: string;
32
  /** Provenance view key: canvas | multiverse | evidence-<tab>. */
33
  view: string;
@@ -115,7 +116,7 @@ export function FigureExportButton({
115
  onPointerCancel={pressCancel}
116
  data-marker="figure-export"
117
  className={`btn ${className}`}
118
- title="Export this view as a paper figure: SVG + PNG with embedded provenance (run ids, seeds, config hashes, graph) + a .provenance.json sidecar. Alt/⌥-click or press-and-hold for the 4× print-resolution PNG (default 2.5×)."
119
  aria-label="Export figure"
120
  >
121
  <Icon
 
5
 
6
  /**
7
  * Small camera button that exports its host view as a paper figure
8
+ * (one .bundle.zip: SVG + PNG + .provenance.json sidecar — see
9
+ * lib/figureExport.ts).
10
  * Placed on the canvas toolbar, the Multiverse board header, the
11
  * Evidence pane header and the Evidence theater. The button itself
12
  * never appears in exports (figureExport strips all <button>
 
28
  }: {
29
  /** Resolve the DOM subtree to export at click time. */
30
  getTarget: () => HTMLElement | SVGSVGElement | null;
31
+ /** Download basename (names the bundle zip and its contents). */
32
  name: string;
33
  /** Provenance view key: canvas | multiverse | evidence-<tab>. */
34
  view: string;
 
116
  onPointerCancel={pressCancel}
117
  data-marker="figure-export"
118
  className={`btn ${className}`}
119
+ title="Export this view as a paper figure: one .bundle.zip holding the SVG + PNG with embedded provenance (run ids, seeds, config hashes, graph) + a .provenance.json sidecar. Alt/⌥-click or press-and-hold for the 4× print-resolution PNG (default 2.5×)."
120
  aria-label="Export figure"
121
  >
122
  <Icon
frontend/src/lib/figureExport.ts CHANGED
@@ -30,8 +30,12 @@
30
  // as the always-works fallback — explicit pixel dimensions
31
  // (css px × scale, never devicePixelRatio), 2.5× by default, 4×
32
  // via alt/⌥-click or long-press on the camera button.
33
- // Both paths also download a `<name>.provenance.json` sidecar with
34
  // the same JSON that is embedded in the SVG <metadata> element.
 
 
 
 
35
  //
36
  // Paper styling transform applied to the clone:
37
  // * forced light/white background (the theme attribute is lifted for
@@ -52,6 +56,7 @@ import {
52
  finalizeSvgMarkup,
53
  inlinePresentation,
54
  } from "./svgPaper";
 
55
 
56
  const SVG_NS = "http://www.w3.org/2000/svg";
57
  const XHTML_NS = "http://www.w3.org/1999/xhtml";
@@ -461,12 +466,16 @@ function rasterize(
461
  // ---------------------------------------------------------------------------
462
 
463
  /**
464
- * Export `target` as a paper figure. Downloads (base name is
465
- * `<view>_<scenario|runid>[_batchK]` so the filename itself names the
466
- * recipe that regenerates the figure):
467
  * <base>.svg — vector (true-SVG or foreignObject hybrid)
468
  * <base>_<scale>x.png — explicit-pixel raster (hybrid targets only)
469
  * <base>.provenance.json — the same provenance embedded in the SVG
 
 
 
 
470
  */
471
  export async function exportFigure(
472
  target: HTMLElement | SVGSVGElement,
@@ -491,32 +500,34 @@ export async function exportFigure(
491
  const base =
492
  (slug ? `${opts.name}_${slug}` : opts.name) +
493
  (opts.tracePosition != null ? `_batch${opts.tracePosition}` : "");
 
 
494
  await withLightTheme(async () => {
495
  if (target instanceof SVGSVGElement) {
496
  const svgString = serializeSvgNative(target, provenance);
497
- download(
498
- new Blob([svgString], { type: "image/svg+xml;charset=utf-8" }),
499
- `${base}.svg`,
500
- );
501
  } else {
502
  const { svg, width, height } = serializeHtmlHybrid(target, provenance);
503
- download(
504
- new Blob([svg], { type: "image/svg+xml;charset=utf-8" }),
505
- `${base}.svg`,
506
- );
507
  try {
508
  const png = await rasterize(svg, width, height, scale);
509
- download(png, `${base}_${scale}x.png`);
 
 
 
510
  } catch {
511
  // foreignObject rasterization can fail on exotic browsers —
512
- // the SVG + sidecar still landed, so don't fail the export.
 
513
  }
514
  }
515
  });
 
 
 
 
516
  download(
517
- new Blob([JSON.stringify(provenance, null, 2)], {
518
- type: "application/json",
519
- }),
520
- `${base}.provenance.json`,
521
  );
522
  }
 
30
  // as the always-works fallback — explicit pixel dimensions
31
  // (css px × scale, never devicePixelRatio), 2.5× by default, 4×
32
  // via alt/⌥-click or long-press on the camera button.
33
+ // Both paths also write a `<base>.provenance.json` sidecar carrying
34
  // the same JSON that is embedded in the SVG <metadata> element.
35
+ // Everything ships as ONE `<base>.bundle.zip` download (lib/zip.ts,
36
+ // STORE method, no deps): three separate programmatic downloads
37
+ // from a single click could trip browser multi-download blocking,
38
+ // which silently dropped the sidecar and/or the PNG.
39
  //
40
  // Paper styling transform applied to the clone:
41
  // * forced light/white background (the theme attribute is lifted for
 
56
  finalizeSvgMarkup,
57
  inlinePresentation,
58
  } from "./svgPaper";
59
+ import { buildZip, type ZipEntry } from "./zip";
60
 
61
  const SVG_NS = "http://www.w3.org/2000/svg";
62
  const XHTML_NS = "http://www.w3.org/1999/xhtml";
 
466
  // ---------------------------------------------------------------------------
467
 
468
  /**
469
+ * Export `target` as a paper figure. Downloads ONE `<base>.bundle.zip`
470
+ * (base name is `<view>_<scenario|runid>[_batchK]` so the filename
471
+ * itself names the recipe that regenerates the figure) containing:
472
  * <base>.svg — vector (true-SVG or foreignObject hybrid)
473
  * <base>_<scale>x.png — explicit-pixel raster (hybrid targets only)
474
  * <base>.provenance.json — the same provenance embedded in the SVG
475
+ * One download per click by design (audit backlog): the old triple
476
+ * download tripped multi-download blocking on strict browsers. The
477
+ * zip is STORE-method (lib/zip.ts); the SVG keeps its embedded
478
+ * provenance <metadata> unchanged, zipped or not.
479
  */
480
  export async function exportFigure(
481
  target: HTMLElement | SVGSVGElement,
 
500
  const base =
501
  (slug ? `${opts.name}_${slug}` : opts.name) +
502
  (opts.tracePosition != null ? `_batch${opts.tracePosition}` : "");
503
+ const enc = new TextEncoder();
504
+ const files: ZipEntry[] = [];
505
  await withLightTheme(async () => {
506
  if (target instanceof SVGSVGElement) {
507
  const svgString = serializeSvgNative(target, provenance);
508
+ files.push({ name: `${base}.svg`, data: enc.encode(svgString) });
 
 
 
509
  } else {
510
  const { svg, width, height } = serializeHtmlHybrid(target, provenance);
511
+ files.push({ name: `${base}.svg`, data: enc.encode(svg) });
 
 
 
512
  try {
513
  const png = await rasterize(svg, width, height, scale);
514
+ files.push({
515
+ name: `${base}_${scale}x.png`,
516
+ data: new Uint8Array(await png.arrayBuffer()),
517
+ });
518
  } catch {
519
  // foreignObject rasterization can fail on exotic browsers —
520
+ // the SVG + sidecar still land in the bundle, so don't fail
521
+ // the export.
522
  }
523
  }
524
  });
525
+ files.push({
526
+ name: `${base}.provenance.json`,
527
+ data: enc.encode(JSON.stringify(provenance, null, 2)),
528
+ });
529
  download(
530
+ new Blob([buildZip(files)], { type: "application/zip" }),
531
+ `${base}.bundle.zip`,
 
 
532
  );
533
  }
frontend/src/lib/zip.ts ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Minimal ZIP writer, STORE method only — pure JS, zero dependencies.
2
+ //
3
+ // Why hand-rolled: the figure exporter bundles up to three small
4
+ // artifacts (SVG, PNG, provenance JSON) into ONE download so a single
5
+ // camera click can never trip a browser's multi-download blocking
6
+ // (audit backlog: the old triple download silently lost the sidecar
7
+ // and/or PNG on strict settings). SVG + JSON are tiny and PNG bytes
8
+ // are already Deflate-compressed, so STORE gives up nothing worth a
9
+ // dependency. Layout per PKWARE APPNOTE: [local file header + data]*,
10
+ // central directory, end-of-central-directory record. CRC-32 is the
11
+ // standard reflected 0xEDB88320 table algorithm — verified against
12
+ // the canonical "123456789" → 0xCBF43926 check value, and the whole
13
+ // writer is read back by python's zipfile, in scripts/check_zip.test.ts.
14
+
15
+ export interface ZipEntry {
16
+ /** Path inside the archive (forward slashes; UTF-8 flag is set). */
17
+ name: string;
18
+ data: Uint8Array;
19
+ }
20
+
21
+ const CRC_TABLE: Uint32Array = (() => {
22
+ const t = new Uint32Array(256);
23
+ for (let n = 0; n < 256; n++) {
24
+ let c = n;
25
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
26
+ t[n] = c >>> 0;
27
+ }
28
+ return t;
29
+ })();
30
+
31
+ export function crc32(data: Uint8Array): number {
32
+ let c = 0xffffffff;
33
+ for (let i = 0; i < data.length; i++)
34
+ c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
35
+ return (c ^ 0xffffffff) >>> 0;
36
+ }
37
+
38
+ /** DOS date/time pair (2-second resolution) — what ZIP headers store. */
39
+ function dosDateTime(d: Date): { time: number; date: number } {
40
+ return {
41
+ time: (d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() >> 1),
42
+ date:
43
+ ((d.getFullYear() - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate(),
44
+ };
45
+ }
46
+
47
+ /** Build a ZIP archive (STORE method) from named byte entries.
48
+ * `now` is injectable so the node lane can build a byte-stable
49
+ * fixture. */
50
+ export function buildZip(
51
+ entries: ZipEntry[],
52
+ now: Date = new Date(),
53
+ ): Uint8Array<ArrayBuffer> {
54
+ const enc = new TextEncoder();
55
+ const { time, date } = dosDateTime(now);
56
+ const locals: Uint8Array[] = [];
57
+ const centrals: Uint8Array[] = [];
58
+ let offset = 0; // running local-header offset = central "offset" field
59
+ for (const { name, data } of entries) {
60
+ const nameB = enc.encode(name);
61
+ const crc = crc32(data);
62
+ // The 24-byte run shared by both record types: version-needed,
63
+ // flags, method, time, date, crc, csize, usize, name length.
64
+ const common = (v: DataView, at: number) => {
65
+ v.setUint16(at, 20, true); // version needed: 2.0
66
+ v.setUint16(at + 2, 0x0800, true); // gp flag: UTF-8 names
67
+ v.setUint16(at + 4, 0, true); // method: STORE
68
+ v.setUint16(at + 6, time, true);
69
+ v.setUint16(at + 8, date, true);
70
+ v.setUint32(at + 10, crc, true);
71
+ v.setUint32(at + 14, data.length, true); // compressed = raw (STORE)
72
+ v.setUint32(at + 18, data.length, true); // uncompressed
73
+ v.setUint16(at + 22, nameB.length, true);
74
+ };
75
+ const local = new Uint8Array(30 + nameB.length + data.length);
76
+ const lv = new DataView(local.buffer);
77
+ lv.setUint32(0, 0x04034b50, true); // local file header signature
78
+ common(lv, 4); // extra-len at 28 stays 0
79
+ local.set(nameB, 30);
80
+ local.set(data, 30 + nameB.length);
81
+ locals.push(local);
82
+
83
+ const central = new Uint8Array(46 + nameB.length);
84
+ const cv = new DataView(central.buffer);
85
+ cv.setUint32(0, 0x02014b50, true); // central directory signature
86
+ cv.setUint16(4, 20, true); // version made by
87
+ common(cv, 6);
88
+ // extra/comment lengths, disk#, internal + external attrs (bytes
89
+ // 30..41) stay zero; offset of the local header:
90
+ cv.setUint32(42, offset, true);
91
+ central.set(nameB, 46);
92
+ centrals.push(central);
93
+ offset += local.length;
94
+ }
95
+ const cdSize = centrals.reduce((n, c) => n + c.length, 0);
96
+ const eocd = new Uint8Array(22);
97
+ const ev = new DataView(eocd.buffer);
98
+ ev.setUint32(0, 0x06054b50, true); // EOCD signature
99
+ ev.setUint16(8, entries.length, true); // entries on this disk
100
+ ev.setUint16(10, entries.length, true); // entries total
101
+ ev.setUint32(12, cdSize, true);
102
+ ev.setUint32(16, offset, true); // central directory offset
103
+ const out = new Uint8Array(offset + cdSize + 22);
104
+ let at = 0;
105
+ for (const part of [...locals, ...centrals, eocd]) {
106
+ out.set(part, at);
107
+ at += part.length;
108
+ }
109
+ return out;
110
+ }