Skip to content

Commit 39a5890

Browse files
wormeymanclaude
andcommitted
perf(export): replace jszip with client-zip - 302 -> 211 kB bundle
The Download ZIP button bundles three short text files (the two Factorio JSON documents plus the exchange string) for the headless CLI route. It was costing 97 kB of the shipped bundle to do it. `jszip`'s `browser` field resolves `./lib/index` to `dist/jszip.min.js`, a 97.6 kB pre-minified **browserify** bundle. That is an opaque IIFE with its own module registry, so it cannot be tree-shaken, and the pako 1.0.11 baked inside it cannot dedupe against the app's pako 3.0.1. The app was shipping two pako majors plus `readable-stream`, `setimmediate` and eight more Node shims in order to write three text files. Measured, by building both ways: | | raw | gzip | | --- | --- | --- | | before (jszip) | 302.07 kB | 99.81 kB | | after (client-zip) | 211.44 kB | 73.11 kB | | delta | **-90.63 kB (-30%)** | **-26.70 kB (-27%)** | Isolating the libraries: a build with the zip feature stubbed out entirely is 204.95 kB, so `client-zip` costs **6.49 kB** where `jszip` cost 97.1 kB - about 15x smaller for this use. Confirmed gone from the bundle rather than assumed: `JSZip`, `jszip`, `setImmediate`, `readable-stream` and `_tr_stored_block` markers all drop to zero, and pako's `incorrect header check` string goes from 2 occurrences to 1 - the codec's pako 3.0.1 remains, the duplicate is gone. `vp build` still prints no warnings at all, which is the standing invariant. **The output is materially unchanged, and the reason is worth recording: jszip was not compressing either.** Both writers emit `Stored` entries at 0% - `generateAsync` without an explicit `compression` option does not deflate. So the whole pako 1.x payload was dead weight for this path. The downloaded archive goes 7888 -> 7936 bytes (+48 bytes, +0.6%), which is the central-directory layout differing slightly, not a compression regression. `jszip` moves to devDependencies and stays the **reader** in `test/zipExport.spec.ts`. That is deliberate: checking a client-zip archive with client-zip would prove self-consistency, not correctness. Validated against a third implementation too - `unzip -t` (Info-ZIP) reports "No errors detected" and lists all three entries with correct names, sizes and CRCs. Note `pnpm add -D` will not relocate a package whose installed version already satisfies the range - it prints "Already up to date" and leaves it where it was - so the dependencies -> devDependencies move was made by editing `package.json` directly and re-running `pnpm install`. The spec gains two cases, because changing the *writer* means entry presence is no longer a sufficient assertion: one parses both JSON entries back and checks real fields, and asserts the exchange string survives verbatim against the fixture; one covers preset-name-derived entry naming and the exact entry set. 1249 -> 1251 tests. Not adopted for currency: `client-zip`'s last human commit is 2025-03-14 (the newer repo push is a Dependabot branch). This swap buys bundle size and API fit, not fresher maintenance. What it does improve is backlog - 8 open issues against jszip's 412 and 40 unanswered PRs. Gate: `pnpm run verify` exit 0 - 314 files formatted, 301 clean, app 1251 passed / 3 skipped, worker 12, container 3. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GjMSePtN9aTgr8EtZp66k1
1 parent 605a4cc commit 39a5890

4 files changed

Lines changed: 92 additions & 16 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"dependencies": {
2727
"@vue/devtools-api": "^8.2.1",
28-
"jszip": "^3.10.1",
28+
"client-zip": "^2.5.0",
2929
"pako": "^3.0.1",
3030
"pinia": "^4.0.2",
3131
"vue": "^3.5.40"
@@ -35,6 +35,7 @@
3535
"@vue/test-utils": "^2.4.11",
3636
"concurrently": "^10.0.4",
3737
"happy-dom": "^20.11.1",
38+
"jszip": "^3.10.1",
3839
"typescript": "^6.0.3",
3940
"vite-plus": "^0.2.6",
4041
"vue-tsc": "^3.3.8"

pnpm-lock.yaml

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/io/zipExport.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import JSZip from "jszip";
1+
import { downloadZip } from "client-zip";
22
import { encodeExchangeString } from "../codec/mapExchangeString";
33
import { presetToEncodable } from "../model/convert";
44
import type { Preset } from "../model/types";
@@ -7,11 +7,42 @@ import { toMapGenSettingsJson, toMapSettingsJson } from "./jsonExport";
77
/**
88
* Bundle a preset's two Factorio JSON documents plus its map-exchange string
99
* into a single downloadable ZIP `Blob`.
10+
*
11+
* The two JSON files are what the game's own CLI consumes - `factorio --create
12+
* <save> --map-gen-settings <file> --map-settings <file>` - so this is the
13+
* headless/dedicated-server route for a preset. The `.txt` carries the exchange
14+
* string for the in-game map-generator dialog.
15+
*
16+
* Backed by `client-zip` rather than `jszip`, which is a bundle-size decision:
17+
* jszip's `browser` field resolves to a 97.6 kB pre-minified *browserify*
18+
* bundle - an opaque IIFE that cannot be tree-shaken and that carries its own
19+
* copy of pako 1.x, so the app shipped two pako majors plus readable-stream and
20+
* setimmediate shims in order to write three short text files. `client-zip` is
21+
* zero-dependency browser-native ESM.
22+
*
23+
* `jszip` remains a devDependency and is *deliberately* still the reader in
24+
* `test/zipExport.spec.ts`. Checking our own writer with our own reader would
25+
* be self-consistent rather than correct; an independent, battle-tested reader
26+
* is the whole point of that test.
27+
*
28+
* Entry timestamps default to "now", which is what jszip did too, so the
29+
* archive is not byte-reproducible across runs. That is fine and always was:
30+
* byte-exactness is the *exchange string's* invariant, enforced in
31+
* `src/codec/`. A ZIP is read by whatever unzip tool opens it.
1032
*/
1133
export async function buildZip(preset: Preset): Promise<Blob> {
12-
const zip = new JSZip();
13-
zip.file("map-gen-settings.json", JSON.stringify(toMapGenSettingsJson(preset), null, 2));
14-
zip.file("map-settings.json", JSON.stringify(toMapSettingsJson(preset), null, 2));
15-
zip.file(`${preset.name}.txt`, encodeExchangeString(presetToEncodable(preset)));
16-
return zip.generateAsync({ type: "blob" });
34+
return downloadZip([
35+
{
36+
name: "map-gen-settings.json",
37+
input: JSON.stringify(toMapGenSettingsJson(preset), null, 2),
38+
},
39+
{
40+
name: "map-settings.json",
41+
input: JSON.stringify(toMapSettingsJson(preset), null, 2),
42+
},
43+
{
44+
name: `${preset.name}.txt`,
45+
input: encodeExchangeString(presetToEncodable(preset)),
46+
},
47+
]).blob();
1748
}

test/zipExport.spec.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,54 @@ import fixtures from "./fixtures/builtin-presets.json";
77

88
const presets = fixtures.presets as Record<string, string>;
99

10+
function defaultPreset() {
11+
return presetFromDecoded("Default", decodeExchangeString(presets["Default"] as string), true);
12+
}
13+
1014
describe("buildZip", () => {
15+
// `buildZip` writes with `client-zip`; this reads with `jszip`, which is a
16+
// devDependency kept for exactly this purpose. Reading our own archive with
17+
// our own writer's library would only prove self-consistency.
1118
it("bundles the two JSON files and the exchange string", async () => {
12-
const preset = presetFromDecoded(
13-
"Default",
14-
decodeExchangeString(presets["Default"] as string),
15-
true,
16-
);
17-
const blob = await buildZip(preset);
19+
const blob = await buildZip(defaultPreset());
1820
const zip = await JSZip.loadAsync(blob);
1921
expect(zip.file("map-gen-settings.json")).not.toBeNull();
2022
expect(zip.file("map-settings.json")).not.toBeNull();
2123
const txt = await zip.file("Default.txt")?.async("string");
2224
expect(txt?.startsWith(">>>")).toBe(true);
2325
});
26+
27+
it("round-trips each entry's bytes intact, not merely its name", async () => {
28+
const preset = defaultPreset();
29+
const zip = await JSZip.loadAsync(await buildZip(preset));
30+
31+
// Entry presence says nothing about whether the payload survived the
32+
// writer. Parse it back and check real fields.
33+
const gen = JSON.parse((await zip.file("map-gen-settings.json")!.async("string")) as string);
34+
expect(gen.width).toBe(preset.width);
35+
expect(gen.height).toBe(preset.height);
36+
expect(gen.autoplace_controls).toBeTypeOf("object");
37+
expect(Object.keys(gen.autoplace_controls).length).toBeGreaterThan(0);
38+
39+
const map = JSON.parse((await zip.file("map-settings.json")!.async("string")) as string);
40+
expect(map.pollution).toBeTypeOf("object");
41+
expect(map.enemy_evolution).toBeTypeOf("object");
42+
43+
// The exchange string must survive verbatim - it is the one artifact in
44+
// the archive with a byte-exactness invariant behind it.
45+
const txt = (await zip.file("Default.txt")!.async("string")) as string;
46+
expect(txt).toBe(presets["Default"]);
47+
});
48+
49+
it("names the text entry after the preset", async () => {
50+
const preset = defaultPreset();
51+
preset.name = "My Custom Preset";
52+
const zip = await JSZip.loadAsync(await buildZip(preset));
53+
expect(zip.file("My Custom Preset.txt")).not.toBeNull();
54+
expect(Object.keys(zip.files).sort()).toEqual([
55+
"My Custom Preset.txt",
56+
"map-gen-settings.json",
57+
"map-settings.json",
58+
]);
59+
});
2460
});

0 commit comments

Comments
 (0)