| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { inflateRawSync } from "node:zlib"; |
| 4 | |
| 5 | import { ZIP_MODES, crc32, zip } from "./zip.ts"; |
| 6 | |
| 7 | test("crc32 matches the standard check value", () => { |
| 8 | assert.equal(crc32(new TextEncoder().encode("123456789")), 0xcbf43926); |
| 9 | }); |
| 10 | |
| 11 | test("a zip holds each file, deflated or stored, and reads back", async () => { |
| 12 | const text = new TextEncoder().encode("hello ".repeat(200)); |
| 13 | const tiny = new TextEncoder().encode("x"); |
| 14 | const out = await zip([ |
| 15 | { path: "repo-main/README.md", data: text }, |
| 16 | { path: "repo-main/a.txt", data: tiny }, |
| 17 | { path: "repo-main/empty", data: new Uint8Array() }, |
| 18 | ]); |
| 19 | const view = new DataView(out.buffer, out.byteOffset, out.byteLength); |
| 20 | // The end record names three entries and where the directory starts. |
| 21 | const end = out.length - 22; |
| 22 | assert.equal(view.getUint32(end, true), 0x06054b50); |
| 23 | assert.equal(view.getUint16(end + 10, true), 3); |
| 24 | // Read each local entry back. |
| 25 | let at = 0; |
| 26 | const read: Record<string, string> = {}; |
| 27 | for (let i = 0; i < 3; i++) { |
| 28 | assert.equal(view.getUint32(at, true), 0x04034b50); |
| 29 | const method = view.getUint16(at + 8, true); |
| 30 | const size = view.getUint32(at + 18, true); |
| 31 | const nameLength = view.getUint16(at + 26, true); |
| 32 | const name = new TextDecoder().decode(out.subarray(at + 30, at + 30 + nameLength)); |
| 33 | const body = out.subarray(at + 30 + nameLength, at + 30 + nameLength + size); |
| 34 | read[name] = new TextDecoder().decode(method === 8 ? inflateRawSync(body) : body); |
| 35 | at += 30 + nameLength + size; |
| 36 | } |
| 37 | assert.equal(read["repo-main/README.md"], "hello ".repeat(200)); |
| 38 | assert.equal(read["repo-main/a.txt"], "x"); |
| 39 | assert.equal(read["repo-main/empty"], ""); |
| 40 | }); |
| 41 | |
| 42 | test("each file keeps its Unix mode: executables and symlinks", async () => { |
| 43 | const out = await zip([ |
| 44 | { path: "repo/run", data: new TextEncoder().encode("#!/bin/sh\n"), mode: ZIP_MODES.exec }, |
| 45 | { path: "repo/link", data: new TextEncoder().encode("run"), mode: ZIP_MODES.symlink }, |
| 46 | { path: "repo/plain", data: new TextEncoder().encode("x") }, |
| 47 | ]); |
| 48 | const view = new DataView(out.buffer, out.byteOffset, out.byteLength); |
| 49 | const end = out.length - 22; |
| 50 | let at = view.getUint32(end + 16, true); |
| 51 | const modes: Record<string, number> = {}; |
| 52 | for (let i = 0; i < 3; i++) { |
| 53 | assert.equal(view.getUint32(at, true), 0x02014b50); |
| 54 | const nameLength = view.getUint16(at + 28, true); |
| 55 | const name = new TextDecoder().decode(out.subarray(at + 46, at + 46 + nameLength)); |
| 56 | modes[name] = view.getUint32(at + 38, true) >>> 16; |
| 57 | at += 46 + nameLength; |
| 58 | } |
| 59 | assert.deepEqual(modes, { "repo/run": 0o100755, "repo/link": 0o120777, "repo/plain": 0o100644 }); |
| 60 | }); |