@ac-kit/format-wacz 0.0.0-stage → 0.1.0

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/LICENSE +28 -0
  3. package/README.md +173 -2
  4. package/dist/_datapackage-json.d.ts +36 -0
  5. package/dist/_datapackage-json.d.ts.map +1 -0
  6. package/dist/_datapackage-json.js +247 -0
  7. package/dist/_datapackage-json.js.map +1 -0
  8. package/dist/_hashing.d.ts +23 -0
  9. package/dist/_hashing.d.ts.map +1 -0
  10. package/dist/_hashing.js +39 -0
  11. package/dist/_hashing.js.map +1 -0
  12. package/dist/_lines.d.ts +11 -0
  13. package/dist/_lines.d.ts.map +1 -0
  14. package/dist/_lines.js +74 -0
  15. package/dist/_lines.js.map +1 -0
  16. package/dist/_pages.d.ts +27 -0
  17. package/dist/_pages.d.ts.map +1 -0
  18. package/dist/_pages.js +155 -0
  19. package/dist/_pages.js.map +1 -0
  20. package/dist/_paths.d.ts +8 -0
  21. package/dist/_paths.d.ts.map +1 -0
  22. package/dist/_paths.js +20 -0
  23. package/dist/_paths.js.map +1 -0
  24. package/dist/index.d.ts +6 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +6 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/open-wacz.d.ts +207 -0
  29. package/dist/open-wacz.d.ts.map +1 -0
  30. package/dist/open-wacz.js +339 -0
  31. package/dist/open-wacz.js.map +1 -0
  32. package/dist/wacz-datapackage.d.ts +84 -0
  33. package/dist/wacz-datapackage.d.ts.map +1 -0
  34. package/dist/wacz-datapackage.js +3 -0
  35. package/dist/wacz-datapackage.js.map +1 -0
  36. package/dist/wacz-error.d.ts +13 -0
  37. package/dist/wacz-error.d.ts.map +1 -0
  38. package/dist/wacz-error.js +19 -0
  39. package/dist/wacz-error.js.map +1 -0
  40. package/dist/wacz-page.d.ts +27 -0
  41. package/dist/wacz-page.d.ts.map +1 -0
  42. package/dist/wacz-page.js +2 -0
  43. package/dist/wacz-page.js.map +1 -0
  44. package/dist/write-wacz.d.ts +193 -0
  45. package/dist/write-wacz.d.ts.map +1 -0
  46. package/dist/write-wacz.js +431 -0
  47. package/dist/write-wacz.js.map +1 -0
  48. package/package.json +72 -3
package/CHANGELOG.md ADDED
@@ -0,0 +1,65 @@
1
+ # @ac-kit/format-wacz
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5152230: New package `@ac-kit/format-wacz`: WACZ 1.1.1 web archive collections.
8
+
9
+ - `createWaczWriter(options)` streams a WACZ: `append` writes WARC records
10
+ (one gzip member each, stored in the ZIP with a data descriptor and ZIP64
11
+ sizes unless `zip64: false`) and indexes them; `addPage` and `addFile` add
12
+ pages and files of your own; `close` writes the sorted CDXJ index
13
+ (`indexes/index.cdx`), `pages/pages.jsonl` with its header line,
14
+ `datapackage.json` (resources with SHA-256 and size, `created` and
15
+ `modified` to the second) and `datapackage-digest.json`, and resolves with a
16
+ summary, whose `indexIssues` lists POST captures without a faithful replay
17
+ key (request bodies are read whole up to 1 MiB; a streamed one has its head
18
+ and up to 1 MiB of body captured as it is written, is left unlocked after
19
+ use, and is cancelled if the WARC writer refuses the record). An optional
20
+ `sign({ hash, created })` hook returns a
21
+ wacz-auth 0.1.0 `signedData`, embedded with only the listed members, which
22
+ must make one complete anonymous or domain form; a result that does not, or
23
+ whose `hash` or `created` differ, rejects `close` with a `TypeError`.
24
+ Aborting (`abort` or `signal`) rejects every pending call at once. One path
25
+ rule (relative, no `\`, control characters, drive, empty, `.` or `..`
26
+ segments) applies to the WARC name, added files and verification; added
27
+ files also may not reuse reserved names or repeat a path, ASCII case aside.
28
+ Page `ts` must serialize as RFC 3339, instants included.
29
+ - `openWacz(source, limits)` opens a WACZ from a `Blob`, a ranged
30
+ `ZipSource` or bytes, reading only the ZIP directory and the datapackage
31
+ files, permissively; `pages()`, `index()` and `readRecord(entry)` stream on
32
+ demand (a record decompresses at most `maxRecordSize`), and `verify()`
33
+ hashes every listed file (`match` needs a clean read, CRC-32 included),
34
+ checks the datapackage digest, judges `signedData` (form, consistency;
35
+ the signature is not checked) and lists `conformance` departures. ZipNum
36
+ indexes throw `UnsupportedError`. Every limit is required and must be a
37
+ safe non-negative integer (`TypeError` before any read);
38
+ `WaczLimitError` and `WaczFormatError` report oversized or malformed
39
+ documents.
40
+
41
+ ### Patch Changes
42
+
43
+ - Updated dependencies [2a0fe74]
44
+ - Updated dependencies [5152230]
45
+ - Updated dependencies [2a0fe74]
46
+ - Updated dependencies [5152230]
47
+ - Updated dependencies [5152230]
48
+ - Updated dependencies [2a0fe74]
49
+ - Updated dependencies [2a0fe74]
50
+ - Updated dependencies [5152230]
51
+ - Updated dependencies [5152230]
52
+ - Updated dependencies [5152230]
53
+ - Updated dependencies [5152230]
54
+ - Updated dependencies [5152230]
55
+ - Updated dependencies [5152230]
56
+ - Updated dependencies [5152230]
57
+ - Updated dependencies [5152230]
58
+ - Updated dependencies [5152230]
59
+ - Updated dependencies [5152230]
60
+ - @ac-kit/core@0.10.0
61
+ - @ac-kit/format-core@0.4.0
62
+ - @ac-kit/format-warc@0.1.0
63
+ - @ac-kit/format-zip@0.3.0
64
+ - @ac-kit/crypto-hash@0.1.9
65
+ - @ac-kit/format-ndjson@0.2.3
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,3 +1,174 @@
1
- # Temporary Holding Version
1
+ # @ac-kit/format-wacz
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ WACZ web archive collections (WACZ 1.1.1): write a browsing session as one
4
+ ZIP file that ReplayWeb.page opens, and read or verify one without loading it
5
+ whole.
6
+
7
+ A WACZ is a ZIP holding a WARC file of recorded HTTP traffic, a sorted CDXJ
8
+ index of its records, a list of pages, and a `datapackage.json` manifest with
9
+ the SHA-256 of every file, itself hashed (and optionally signed) in
10
+ `datapackage-digest.json`. Replay tools read it by HTTP ranges: the ZIP
11
+ directory, the index, then only the records a page needs. This package writes
12
+ such archives as a stream and reads them by ranges; it performs no I/O and
13
+ runs alike in Node and in browsers.
14
+
15
+ ```ts
16
+ import { printWarcHttpHead } from "@ac-kit/format-warc";
17
+ import { createWaczWriter, openWacz } from "@ac-kit/format-wacz";
18
+
19
+ const wacz = createWaczWriter({ datapackage: { title: "Example" } });
20
+ // Read the output while writing: nothing settles until someone reads it.
21
+ const file = new Response(wacz.readable).bytes();
22
+
23
+ const body = new TextEncoder().encode("<!doctype html><title>Hello</title>");
24
+ const head = printWarcHttpHead({
25
+ kind: "response",
26
+ status: 200,
27
+ fields: [["content-type", "text/html"]],
28
+ });
29
+ await wacz.append({
30
+ type: "response",
31
+ targetUri: "https://example.test/",
32
+ block: new Uint8Array([...head, ...body]),
33
+ });
34
+ wacz.addPage({ url: "https://example.test/", ts: Temporal.Now.instant() });
35
+ const summary = await wacz.close();
36
+ summary.datapackageHash; // "sha256:…" of datapackage.json
37
+
38
+ const archive = await openWacz(await file, {
39
+ maxEntries: 10_000,
40
+ maxEntrySize: 64 * 1024 * 1024,
41
+ maxCompressionRatio: 1100,
42
+ maxDirectorySize: 16 * 1024 * 1024,
43
+ maxDatapackageSize: 16 * 1024 * 1024,
44
+ maxIndexSize: 256 * 1024 * 1024,
45
+ maxPageLineLength: 16 * 1024 * 1024,
46
+ maxRecordSize: 256 * 1024 * 1024,
47
+ });
48
+ for await (const entry of archive.index()) {
49
+ const record = await archive.readRecord(entry);
50
+ record.header.targetUri; // "https://example.test/"
51
+ await record.finish();
52
+ }
53
+ (await archive.verify()).datapackageDigest; // "match"
54
+ ```
55
+
56
+ **Requires a `Temporal` global** for dates, and Node 24.19+ or 26.5+, whose
57
+ `DecompressionStream` rejects data after a gzip member. Node 26 ships
58
+ `Temporal`; on Node 24 load a polyfill such as
59
+ [`temporal-polyfill`](https://www.npmjs.com/package/temporal-polyfill) first.
60
+ It also uses `CompressionStream`, `DecompressionStream` and
61
+ `crypto.randomUUID`.
62
+
63
+ ## Writing
64
+
65
+ `createWaczWriter(options)` returns a `readable` stream and:
66
+
67
+ - `append(record)`: writes one WARC record (see `@ac-kit/format-warc`'s
68
+ `createWarcWriter`, whose rules and errors apply) and indexes it. A request
69
+ block supplies the body a POST request is looked up by: read whole when in
70
+ memory; when streamed, its HTTP head (within the WARC writer's
71
+ `maxHttpHeadSize`) and up to `maxRequestBodySize` (1 MiB) body bytes are
72
+ copied as it is written. The request stream is left unlocked after use,
73
+ and is cancelled if the WARC writer refuses the record.
74
+ A body that gives no key matching ReplayWeb.page's (absent, or too large
75
+ with a query depending on its end) is listed in the summary's
76
+ `indexIssues`, never keyed as an empty body.
77
+ - `addPage(page)`: lists a page in `pages/pages.jsonl` (`url`, `ts` as a
78
+ `Temporal.Instant` or an RFC 3339 string, and optionally `id`, `title`,
79
+ `text`, `size`, other members).
80
+ - `addFile(path, content)`: adds a file of your own outside `archive/`,
81
+ `indexes/` and `pages/`, listed in the manifest. Paths are relative,
82
+ without empty, `.` or `..` segments, `\`, control characters or a drive
83
+ (`C:`), and are compared without regard to ASCII case against the reserved
84
+ names and each other, so the archive extracts alike everywhere.
85
+ - `close()`: ends the WARC and writes the index, the pages, your files,
86
+ `datapackage.json` and `datapackage-digest.json`; resolves with a summary
87
+ once `readable` has been read to its end.
88
+ - `abort(reason)`, or aborting `signal`: errors `readable` and rejects every
89
+ pending call at once, read or not; what was read is a truncated archive.
90
+
91
+ The layout is what ReplayWeb.page and py-wacz expect:
92
+
93
+ | Entry | How |
94
+ |---|---|
95
+ | `archive/data.warc.gz` | one gzip member per record, **stored** in the ZIP (records are read by range), streamed with a data descriptor and ZIP64 sizes (`zip64: false` for readers that walk local headers; the WARC then fails with `ZipLimitError` past 4 GiB) |
96
+ | `indexes/index.cdx` | plain CDXJ, sorted, deflated; read whole by ReplayWeb.page |
97
+ | `pages/pages.jsonl` | the `json-pages-1.0` header line, then one page per line |
98
+ | your files | in the order added |
99
+ | `datapackage.json` | `profile`, `wacz_version` `1.1.1`, metadata, `created` and `modified` to the second, then `resources` (name, path, `sha256:` hash, bytes) |
100
+ | `datapackage-digest.json` | `path`, `hash` of the manifest bytes as written, and `signedData` |
101
+
102
+ Records stream from `append` to `readable` without being buffered; memory is
103
+ one chunk in flight, about 300 bytes of index per record, the page lines and
104
+ the ZIP directory.
105
+
106
+ ### Signing
107
+
108
+ `sign({ hash, created }, signal)` is called once with the manifest's hash and
109
+ its `created`; return a wacz-auth 0.1.0 `signedData` and it is embedded,
110
+ keeping only the members that specification lists. They must make one
111
+ complete form — anonymous (`signature`, `publicKey`) or domain identity
112
+ (`signature`, `domain`, `domainCert`, `timeSignature`, `timestampCert`,
113
+ optionally `crossSignedCert`) — and its `hash` and `created` must equal the
114
+ ones given, or `close` rejects with a `TypeError`. This package creates and
115
+ checks no signature itself.
116
+
117
+ ## Reading
118
+
119
+ `openWacz(source, limits)` opens a `Blob` (a `File`, Node's `fs.openAsBlob`),
120
+ any ranged `ZipSource` from `@ac-kit/format-zip` (an HTTP server with `Range`),
121
+ or bytes in memory. It reads the ZIP directory and the two datapackage files,
122
+ permissively so slightly-off archives still replay; everything else on
123
+ demand. Written for WACZ 1.0.0–1.1.1; other versions open and are listed in
124
+ `conformance`. A multi-WACZ package throws `UnsupportedError`
125
+ (`@ac-kit/core`); a WARC compressed inside the ZIP makes `readRecord` throw it:
126
+
127
+ - `pages(path?)` streams the pages of `pages/pages.jsonl` (or another list).
128
+ - `index()` streams the entries of every `indexes/*.cdx` and `*.cdxj`. ZipNum
129
+ (`.idx`) and gzip-compressed indexes throw `UnsupportedError`.
130
+ - `readRecord(entry)` reads the record an entry points to from its range of
131
+ the stored WARC, as ReplayWeb.page does, decompressing at most
132
+ `maxRecordSize` bytes (a per-call `maxDecompressedSize`, a safe
133
+ non-negative integer, can only lower it).
134
+ - `verify()` streams every listed file through SHA-256 and compares hash and
135
+ size with the manifest (`match` needs a clean read, CRC-32 included), lists
136
+ files the manifest omits, checks `datapackage-digest.json` against the
137
+ stored manifest bytes, judges `signedData` in three separate verdicts
138
+ (complete form, consistency with the archive, signature — not checked
139
+ here), and lists departures from the specification (`conformance`:
140
+ version, profile, incomplete or duplicate resources, duplicate or unsafe
141
+ names, missing pages, WARC or index). A path listed twice is hashed once.
142
+
143
+ Every call takes an optional `AbortSignal`; `pages()` and `index()` stop
144
+ before the next item once it aborts.
145
+
146
+ ## Untrusted input
147
+
148
+ Every limit is required, since an archive declares its own sizes: the ZIP's
149
+ (`maxEntries`, `maxEntrySize`, `maxCompressionRatio`, `maxDirectorySize`) and
150
+ `maxDatapackageSize`, `maxIndexSize`, `maxPageLineLength` and
151
+ `maxRecordSize`, past which `WaczLimitError` (or `WarcLimitError` for a
152
+ record) is thrown before anything is buffered. Each is a safe
153
+ non-negative integer, except `maxCompressionRatio`, a finite positive number:
154
+ `NaN` or a missing value would disable a comparison, so
155
+ `openWacz` refuses it with `TypeError` before the first read. Invalid JSON or
156
+ documents of the wrong shape throw `WaczFormatError` naming the entry; ZIP and
157
+ WARC errors pass through unchanged. Pages and index lines are read one at a
158
+ time; memory never holds a whole index or page list.
159
+
160
+ Archives were checked against py-wacz (`wacz validate` accepts them) and
161
+ wabac.js, ReplayWeb.page's engine, which reads the index, hashes and records
162
+ of an archive written by this package as this package does.
163
+
164
+ See the
165
+ [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/format-wacz/)
166
+ for every option and type.
167
+
168
+ ## Further reading
169
+
170
+ - [WACZ 1.1.1](https://specs.webrecorder.net/wacz/1.1.1/) — the format.
171
+ - [WACZ signing and verification 0.1.0](https://specs.webrecorder.net/wacz-auth/0.1.0/)
172
+ — `signedData`.
173
+ - [`@ac-kit/format-warc`](../format-warc/README.md) — the WARC records and
174
+ the CDXJ index inside.
@@ -0,0 +1,36 @@
1
+ import { type WaczDatapackage, type WaczDatapackageDigest, type WaczSignedData } from "./wacz-datapackage.js";
2
+ export declare const DATAPACKAGE_PATH_ = "datapackage.json";
3
+ export declare const DATAPACKAGE_DIGEST_PATH_ = "datapackage-digest.json";
4
+ /**
5
+ * Which wacz-auth 0.1.0 form a `signedData` value takes: every member listed by
6
+ * the specification and a string, `hash`, `created` and `signature` present,
7
+ * and either `publicKey` (anonymous) or `domain`, `domainCert`, `timeSignature`
8
+ * and `timestampCert` (domain identity, `crossSignedCert` optional), not both.
9
+ * `software` and `version` are optional in either.
10
+ */
11
+ export declare function signedDataForm_(value: unknown): "anonymous" | "domain" | "invalid";
12
+ /** Members {@link printDatapackage_} writes, which `extra` may not repeat. */
13
+ export declare const DATAPACKAGE_MEMBERS_: ReadonlySet<string>;
14
+ /** `datapackage.json` from its stored bytes. */
15
+ export declare function parseDatapackage_(bytes: Uint8Array): WaczDatapackage;
16
+ /**
17
+ * `datapackage-digest.json` from its stored bytes, read permissively: a
18
+ * `signedData` is kept when it has string `hash`, `created` and `signature`
19
+ * (its listed string members only), and its wacz-auth form is judged apart.
20
+ */
21
+ export declare function parseDatapackageDigest_(bytes: Uint8Array): {
22
+ readonly digest: WaczDatapackageDigest;
23
+ readonly signedDataForm: "anonymous" | "domain" | "invalid" | undefined;
24
+ };
25
+ /**
26
+ * The members of wacz-auth's lists only, which must then make one complete form
27
+ * ({@link signedDataForm_}).
28
+ *
29
+ * @throws {TypeError} When `value` is not such a structure.
30
+ */
31
+ export declare function checkSignedData_(value: unknown): WaczSignedData;
32
+ /** `datapackage.json` text, two-space indented, members in a fixed order. */
33
+ export declare function printDatapackage_(datapackage: WaczDatapackage): string;
34
+ /** `datapackage-digest.json` text. */
35
+ export declare function printDatapackageDigest_(digest: WaczDatapackageDigest): string;
36
+ //# sourceMappingURL=_datapackage-json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_datapackage-json.d.ts","sourceRoot":"","sources":["../src/_datapackage-json.ts"],"names":[],"mappings":"AAGA,OAAO,EAEN,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAE1B,KAAK,cAAc,EACnB,MAAM,uBAAuB,CAAC;AAG/B,eAAO,MAAM,iBAAiB,qBAAqB,CAAC;AACpD,eAAO,MAAM,wBAAwB,4BAA4B,CAAC;AAoElE;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC9B,KAAK,EAAE,OAAO,GACZ,WAAW,GAAG,QAAQ,GAAG,SAAS,CAsBpC;AAED,8EAA8E;AAC9E,eAAO,MAAM,oBAAoB,EAAE,WAAW,CAAC,MAAM,CAYnD,CAAC;AAkCH,gDAAgD;AAChD,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CA2BpE;AAoBD;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,UAAU,GAAG;IAC3D,QAAQ,CAAC,MAAM,EAAE,qBAAqB,CAAC;IACvC,QAAQ,CAAC,cAAc,EAAE,WAAW,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,CAAC;CACxE,CAiBA;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,CAQ/D;AAED,6EAA6E;AAC7E,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,eAAe,GAAG,MAAM,CA6BtE;AAED,sCAAsC;AACtC,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,qBAAqB,GAAG,MAAM,CAU7E"}
@@ -0,0 +1,247 @@
1
+ import * as z from "zod/mini";
2
+ import { WACZ_VERSION, } from "./wacz-datapackage.js";
3
+ import { WaczFormatError } from "./wacz-error.js";
4
+ export const DATAPACKAGE_PATH_ = "datapackage.json";
5
+ export const DATAPACKAGE_DIGEST_PATH_ = "datapackage-digest.json";
6
+ const count = z.int().check(z.nonnegative());
7
+ const resourceSchema = z.object({
8
+ name: z.optional(z.string()),
9
+ path: z.string(),
10
+ hash: z.optional(z.string()),
11
+ bytes: z.optional(count),
12
+ });
13
+ const datapackageSchema = z.looseObject({
14
+ profile: z.optional(z.string()),
15
+ wacz_version: z.optional(z.string()),
16
+ resources: z.array(resourceSchema),
17
+ title: z.optional(z.string()),
18
+ description: z.optional(z.string()),
19
+ created: z.optional(z.string()),
20
+ modified: z.optional(z.string()),
21
+ software: z.optional(z.string()),
22
+ mainPageUrl: z.optional(z.string()),
23
+ mainPageURL: z.optional(z.string()),
24
+ mainPageDate: z.optional(z.string()),
25
+ });
26
+ const SIGNED_DATA_MEMBERS = [
27
+ "hash",
28
+ "created",
29
+ "software",
30
+ "version",
31
+ "signature",
32
+ "publicKey",
33
+ "domain",
34
+ "domainCert",
35
+ "timeSignature",
36
+ "timestampCert",
37
+ "crossSignedCert",
38
+ ];
39
+ const signedDataSchema = z.object({
40
+ hash: z.string(),
41
+ created: z.string(),
42
+ software: z.optional(z.string()),
43
+ version: z.optional(z.string()),
44
+ signature: z.string(),
45
+ publicKey: z.optional(z.string()),
46
+ domain: z.optional(z.string()),
47
+ domainCert: z.optional(z.string()),
48
+ timeSignature: z.optional(z.string()),
49
+ timestampCert: z.optional(z.string()),
50
+ crossSignedCert: z.optional(z.string()),
51
+ });
52
+ const digestSchema = z.object({
53
+ path: z.string(),
54
+ hash: z.string(),
55
+ signedData: z.optional(z.unknown()),
56
+ });
57
+ const ANONYMOUS_ONLY = ["publicKey"];
58
+ const DOMAIN_REQUIRED = [
59
+ "domain",
60
+ "domainCert",
61
+ "timeSignature",
62
+ "timestampCert",
63
+ ];
64
+ const DOMAIN_ONLY = [...DOMAIN_REQUIRED, "crossSignedCert"];
65
+ /**
66
+ * Which wacz-auth 0.1.0 form a `signedData` value takes: every member listed by
67
+ * the specification and a string, `hash`, `created` and `signature` present,
68
+ * and either `publicKey` (anonymous) or `domain`, `domainCert`, `timeSignature`
69
+ * and `timestampCert` (domain identity, `crossSignedCert` optional), not both.
70
+ * `software` and `version` are optional in either.
71
+ */
72
+ export function signedDataForm_(value) {
73
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
74
+ return "invalid";
75
+ }
76
+ const members = value;
77
+ const listed = new Set(SIGNED_DATA_MEMBERS);
78
+ for (const [name, member] of Object.entries(members)) {
79
+ if (!listed.has(name) || typeof member !== "string") {
80
+ return "invalid";
81
+ }
82
+ }
83
+ const has = (name) => Object.hasOwn(members, name);
84
+ if (!has("hash") || !has("created") || !has("signature")) {
85
+ return "invalid";
86
+ }
87
+ if (ANONYMOUS_ONLY.every(has) && !DOMAIN_ONLY.some(has)) {
88
+ return "anonymous";
89
+ }
90
+ if (DOMAIN_REQUIRED.every(has) && !ANONYMOUS_ONLY.some(has)) {
91
+ return "domain";
92
+ }
93
+ return "invalid";
94
+ }
95
+ /** Members {@link printDatapackage_} writes, which `extra` may not repeat. */
96
+ export const DATAPACKAGE_MEMBERS_ = new Set([
97
+ "profile",
98
+ "wacz_version",
99
+ "resources",
100
+ "title",
101
+ "description",
102
+ "created",
103
+ "modified",
104
+ "software",
105
+ "mainPageUrl",
106
+ "mainPageURL",
107
+ "mainPageDate",
108
+ ]);
109
+ function parseJson(bytes, path) {
110
+ let text;
111
+ try {
112
+ text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
113
+ }
114
+ catch (error) {
115
+ throw new WaczFormatError("Not UTF-8", path, { cause: error });
116
+ }
117
+ try {
118
+ return JSON.parse(text);
119
+ }
120
+ catch (error) {
121
+ throw new WaczFormatError("Invalid JSON", path, { cause: error });
122
+ }
123
+ }
124
+ function check(schema, value, path) {
125
+ const result = schema.safeParse(value);
126
+ if (!result.success) {
127
+ throw new WaczFormatError(`Invalid document: ${z.prettifyError(result.error)}`, path, { cause: result.error });
128
+ }
129
+ return result.data;
130
+ }
131
+ function defined(value) {
132
+ return Object.fromEntries(Object.entries(value).filter(([, member]) => member !== undefined));
133
+ }
134
+ /** `datapackage.json` from its stored bytes. */
135
+ export function parseDatapackage_(bytes) {
136
+ const raw = parseJson(bytes, DATAPACKAGE_PATH_);
137
+ const value = check(datapackageSchema, raw, DATAPACKAGE_PATH_);
138
+ const extra = {};
139
+ for (const [name, member] of Object.entries(raw)) {
140
+ if (!DATAPACKAGE_MEMBERS_.has(name)) {
141
+ Object.defineProperty(extra, name, {
142
+ value: member,
143
+ enumerable: true,
144
+ writable: true,
145
+ configurable: true,
146
+ });
147
+ }
148
+ }
149
+ return defined({
150
+ profile: value.profile,
151
+ waczVersion: value.wacz_version,
152
+ resources: value.resources.map((resource) => defined(resource)),
153
+ title: value.title,
154
+ description: value.description,
155
+ created: value.created,
156
+ modified: value.modified,
157
+ software: value.software,
158
+ mainPageUrl: value.mainPageUrl ?? value.mainPageURL,
159
+ mainPageDate: value.mainPageDate,
160
+ extra: Object.keys(extra).length > 0 ? extra : undefined,
161
+ });
162
+ }
163
+ /** The listed string members of `value`, when it has the required ones. */
164
+ function readSignedData(value) {
165
+ if (typeof value !== "object" || value === null) {
166
+ return undefined;
167
+ }
168
+ const members = value;
169
+ const picked = {};
170
+ for (const name of SIGNED_DATA_MEMBERS) {
171
+ const member = members[name];
172
+ if (Object.hasOwn(members, name) && typeof member === "string") {
173
+ picked[name] = member;
174
+ }
175
+ }
176
+ return signedDataSchema.safeParse(picked).success
177
+ ? picked
178
+ : undefined;
179
+ }
180
+ /**
181
+ * `datapackage-digest.json` from its stored bytes, read permissively: a
182
+ * `signedData` is kept when it has string `hash`, `created` and `signature`
183
+ * (its listed string members only), and its wacz-auth form is judged apart.
184
+ */
185
+ export function parseDatapackageDigest_(bytes) {
186
+ const value = check(digestSchema, parseJson(bytes, DATAPACKAGE_DIGEST_PATH_), DATAPACKAGE_DIGEST_PATH_);
187
+ const signedData = value.signedData === undefined
188
+ ? undefined
189
+ : readSignedData(value.signedData);
190
+ return {
191
+ digest: defined({ path: value.path, hash: value.hash, signedData }),
192
+ signedDataForm: value.signedData === undefined
193
+ ? undefined
194
+ : signedDataForm_(value.signedData),
195
+ };
196
+ }
197
+ /**
198
+ * The members of wacz-auth's lists only, which must then make one complete form
199
+ * ({@link signedDataForm_}).
200
+ *
201
+ * @throws {TypeError} When `value` is not such a structure.
202
+ */
203
+ export function checkSignedData_(value) {
204
+ const picked = readSignedData(value);
205
+ if (picked === undefined || signedDataForm_(picked) === "invalid") {
206
+ throw new TypeError("sign: signedData is neither a complete anonymous (publicKey) nor domain (domain, domainCert, timeSignature, timestampCert) wacz-auth 0.1.0 form");
207
+ }
208
+ return picked;
209
+ }
210
+ /** `datapackage.json` text, two-space indented, members in a fixed order. */
211
+ export function printDatapackage_(datapackage) {
212
+ const members = defined({
213
+ profile: datapackage.profile,
214
+ wacz_version: datapackage.waczVersion ?? WACZ_VERSION,
215
+ title: datapackage.title,
216
+ description: datapackage.description,
217
+ created: datapackage.created,
218
+ modified: datapackage.modified,
219
+ software: datapackage.software,
220
+ mainPageUrl: datapackage.mainPageUrl,
221
+ mainPageDate: datapackage.mainPageDate,
222
+ });
223
+ for (const [name, value] of Object.entries(datapackage.extra ?? {})) {
224
+ Object.defineProperty(members, name, {
225
+ value,
226
+ enumerable: true,
227
+ writable: true,
228
+ configurable: true,
229
+ });
230
+ }
231
+ members["resources"] = datapackage.resources.map((resource) => defined({
232
+ name: resource.name,
233
+ path: resource.path,
234
+ hash: resource.hash,
235
+ bytes: resource.bytes,
236
+ }));
237
+ return `${JSON.stringify(members, null, 2)}\n`;
238
+ }
239
+ /** `datapackage-digest.json` text. */
240
+ export function printDatapackageDigest_(digest) {
241
+ return `${JSON.stringify(defined({
242
+ path: digest.path,
243
+ hash: digest.hash,
244
+ signedData: digest.signedData,
245
+ }), null, 2)}\n`;
246
+ }
247
+ //# sourceMappingURL=_datapackage-json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_datapackage-json.js","sourceRoot":"","sources":["../src/_datapackage-json.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B,OAAO,EACN,YAAY,GAKZ,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,MAAM,CAAC,MAAM,iBAAiB,GAAG,kBAAkB,CAAC;AACpD,MAAM,CAAC,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAElE,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;AAE7C,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC;IAC/B,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC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sB,EAAE,EAAE,CAC3E,OAAO,CAAC;QACP,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,KAAK,EAAE,QAAQ,CAAC,KAAK;KACrB,CAAC,CACF,CAAC;IACF,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC;AAChD,CAAC;AAED,sCAAsC;AACtC,MAAM,UAAU,uBAAuB,CAAC,MAA6B;IACpE,OAAO,GAAG,IAAI,CAAC,SAAS,CACvB,OAAO,CAAC;QACP,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,UAAU,EAAE,MAAM,CAAC,UAAU;KAC7B,CAAC,EACF,IAAI,EACJ,CAAC,CACD,IAAI,CAAC;AACP,CAAC"}
@@ -0,0 +1,23 @@
1
+ /** What a file of the archive weighs and hashes to. */
2
+ export type Fixity_ = {
3
+ /** `sha256:<hex>`. */
4
+ readonly hash: string;
5
+ readonly bytes: number;
6
+ };
7
+ /** The fixity of bytes in memory. */
8
+ export declare function fixityOf_(bytes: Uint8Array<ArrayBuffer>): Promise<Fixity_>;
9
+ /**
10
+ * `source` passed through unchanged while its bytes are counted and hashed;
11
+ * `fixity` settles once the stream ends, and never if it errors or is cancelled
12
+ * (the archive then errors, and no one waits for it).
13
+ */
14
+ export declare function hashing_(source: ReadableStream<Uint8Array>): {
15
+ readonly stream: ReadableStream<Uint8Array>;
16
+ readonly fixity: Promise<Fixity_>;
17
+ };
18
+ /**
19
+ * Whether two `<algorithm>:<hex>` digests are equal, the hex compared without
20
+ * regard to case.
21
+ */
22
+ export declare function sameDigest_(a: string, b: string): boolean;
23
+ //# sourceMappingURL=_hashing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_hashing.d.ts","sourceRoot":"","sources":["../src/_hashing.ts"],"names":[],"mappings":"AAEA,uDAAuD;AACvD,MAAM,MAAM,OAAO,GAAG;IACrB,sBAAsB;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,qCAAqC;AACrC,wBAAsB,SAAS,CAC9B,KAAK,EAAE,UAAU,CAAC,WAAW,CAAC,GAC5B,OAAO,CAAC,OAAO,CAAC,CAGlB;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG;IAC7D,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAC5C,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;CAClC,CAmBA;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAEzD"}
@@ -0,0 +1,39 @@
1
+ import { sha256, Sha256Sink } from "@ac-kit/crypto-hash";
2
+ /** The fixity of bytes in memory. */
3
+ export async function fixityOf_(bytes) {
4
+ const digest = await sha256(bytes);
5
+ return { hash: `sha256:${digest.toHex()}`, bytes: bytes.length };
6
+ }
7
+ /**
8
+ * `source` passed through unchanged while its bytes are counted and hashed;
9
+ * `fixity` settles once the stream ends, and never if it errors or is cancelled
10
+ * (the archive then errors, and no one waits for it).
11
+ */
12
+ export function hashing_(source) {
13
+ const sink = new Sha256Sink();
14
+ const writer = sink.getWriter();
15
+ const fixity = Promise.withResolvers();
16
+ let bytes = 0;
17
+ const transform = new TransformStream({
18
+ async transform(chunk, controller) {
19
+ bytes += chunk.length;
20
+ await writer.write(chunk);
21
+ controller.enqueue(chunk);
22
+ },
23
+ async flush() {
24
+ await writer.close();
25
+ const digest = await sink.digest;
26
+ fixity.resolve({ hash: `sha256:${digest.toHex()}`, bytes });
27
+ },
28
+ });
29
+ const stream = source.pipeThrough(transform);
30
+ return { stream, fixity: fixity.promise };
31
+ }
32
+ /**
33
+ * Whether two `<algorithm>:<hex>` digests are equal, the hex compared without
34
+ * regard to case.
35
+ */
36
+ export function sameDigest_(a, b) {
37
+ return a.toLowerCase() === b.toLowerCase();
38
+ }
39
+ //# sourceMappingURL=_hashing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_hashing.js","sourceRoot":"","sources":["../src/_hashing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AASzD,qCAAqC;AACrC,MAAM,CAAC,KAAK,UAAU,SAAS,CAC9B,KAA8B;IAE9B,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,CAAC;IACnC,OAAO,EAAE,IAAI,EAAE,UAAU,MAAM,CAAC,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;AAClE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAkC;IAI1D,MAAM,IAAI,GAAG,IAAI,UAAU,EAAE,CAAC;IAC9B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;IAChC,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,EAAW,CAAC;IAChD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,SAAS,GAAG,IAAI,eAAe,CAAyB;QAC7D,KAAK,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU;YAChC,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC;YACtB,MAAM,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC1B,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;QACD,KAAK,CAAC,KAAK;YACV,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;YACrB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,UAAU,MAAM,CAAC,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC7D,CAAC;KACD,CAAC,CAAC;IACH,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;IAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC;AAC3C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,CAAS,EAAE,CAAS;IAC/C,OAAO,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The UTF-8 lines of a byte stream, as `@ac-kit/format-core`'s `LineDecoder`
3
+ * frames them (LF or CRLF, a last line without one included), at most
4
+ * `maxLineLength` bytes each: the decoder rescans nothing, and the buffer holds
5
+ * at most one line and one slice of input. Aborting `signal` stops before the
6
+ * next line. Returning or throwing early cancels the stream.
7
+ *
8
+ * @param tooLong Builds the error thrown when a line passes `maxLineLength`.
9
+ */
10
+ export declare function readLines_(stream: ReadableStream<Uint8Array>, maxLineLength: number, tooLong: () => Error, signal?: AbortSignal): AsyncGenerator<string, void, undefined>;
11
+ //# sourceMappingURL=_lines.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_lines.d.ts","sourceRoot":"","sources":["../src/_lines.ts"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AACH,wBAAuB,UAAU,CAChC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,KAAK,EACpB,MAAM,CAAC,EAAE,WAAW,GAClB,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,SAAS,CAAC,CA+DzC"}