Open History to see the last commit
1import JSZip from "jszip";
2import { describe, expect, it } from "vite-plus/test";
3import { boundedZip } from "./boundedZip.ts";
4
5// Writes each name as raw bytes with no UTF-8 flag, as legacy and macOS archivers do.
6async function zipWithRawNames(entries: ReadonlyArray<readonly [number[], string]>) {
7 const placeholders = entries.map(([raw], index) => String(index).padStart(raw.length, "_"));
8 const bytes = new Uint8Array(
9 await entries
10 .reduce((zip, [, content], index) => zip.file(placeholders[index]!, content), new JSZip())
11 .generateAsync({ type: "arraybuffer", compression: "STORE" }),
12 );
13 const text = new TextDecoder("latin1").decode(bytes);
14 entries.forEach(([raw], index) => {
15 for (const match of text.matchAll(new RegExp(placeholders[index]!, "g")))
16 bytes.set(raw, match.index);
17 });
18 return bytes.buffer;
19}
20
21const ascii = (text: string) => Array.from(text, (char) => char.charCodeAt(0));
22
23describe("boundedZip", () => {
24 it("decodes unflagged names as UTF-8, else CP437, without merging entries", async () => {
25 const files = await boundedZip(
26 await zipWithRawNames([
27 [[...ascii("caf"), 0x82, ...ascii(".txt")], "CP437 e-acute"],
28 [[...ascii("caf"), 0x8a, ...ascii(".txt")], "CP437 e-grave"],
29 [[...ascii("na"), 0xc3, 0xaf, ...ascii("ve.txt")], "UTF-8"],
30 [[0xef, 0xbb, 0xbf, ...ascii("a.txt")], "UTF-8 with BOM"],
31 [ascii("a.txt"), "ASCII"],
32 ]),
33 );
34 expect([...files].map(([name, file]) => [name, new TextDecoder().decode(file.bytes)])).toEqual([
35 ["café.txt", "CP437 e-acute"],
36 ["cafè.txt", "CP437 e-grave"],
37 ["naïve.txt", "UTF-8"],
38 ["\uFEFFa.txt", "UTF-8 with BOM"],
39 ["a.txt", "ASCII"],
40 ]);
41 });
42});