@johnhenry/sitefile 0.0.2 → 0.0.3
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.
- package/CHANGELOG.md +467 -0
- package/README.md +55 -6
- package/lib/src/archive-writers.d.ts +12 -2
- package/lib/src/archive-writers.js +53 -10
- package/lib/src/canonical.d.ts +16 -0
- package/lib/src/canonical.js +46 -0
- package/lib/src/car.d.ts +34 -0
- package/lib/src/car.js +203 -0
- package/lib/src/checklinks.js +8 -2
- package/lib/src/cli/bake.js +4 -3
- package/lib/src/cli/build-image.d.ts +1 -0
- package/lib/src/cli/build-image.js +1 -0
- package/lib/src/cli/common.js +4 -1
- package/lib/src/cli/hosts.d.ts +5 -0
- package/lib/src/cli/hosts.js +78 -0
- package/lib/src/cli/images.js +23 -5
- package/lib/src/cli/import-config.d.ts +2 -0
- package/lib/src/cli/import-config.js +56 -0
- package/lib/src/cli/index.d.ts +3 -1
- package/lib/src/cli/index.js +31 -2
- package/lib/src/cli/integrity.d.ts +2 -0
- package/lib/src/cli/integrity.js +77 -0
- package/lib/src/cli/network.d.ts +7 -0
- package/lib/src/cli/network.js +14 -3
- package/lib/src/cli/serve-keys.d.ts +39 -0
- package/lib/src/cli/serve-keys.js +106 -0
- package/lib/src/cli/serve-worker.js +46 -8
- package/lib/src/cli/servers.d.ts +124 -1
- package/lib/src/cli/servers.js +705 -62
- package/lib/src/cli/up.d.ts +2 -0
- package/lib/src/cli/up.js +193 -0
- package/lib/src/cli/watch.d.ts +14 -1
- package/lib/src/cli/watch.js +20 -3
- package/lib/src/cli.js +138 -18
- package/lib/src/csp.d.ts +17 -0
- package/lib/src/csp.js +52 -0
- package/lib/src/dictionary.d.ts +16 -0
- package/lib/src/dictionary.js +89 -0
- package/lib/src/epub.d.ts +5 -0
- package/lib/src/epub.js +113 -0
- package/lib/src/errors.d.ts +22 -9
- package/lib/src/errors.js +20 -1
- package/lib/src/executor.d.ts +31 -1
- package/lib/src/executor.js +192 -22
- package/lib/src/explain.js +33 -3
- package/lib/src/export-for.d.ts +42 -0
- package/lib/src/export-for.js +466 -0
- package/lib/src/export-formats.d.ts +2 -2
- package/lib/src/export-formats.js +52 -2
- package/lib/src/fingerprint.d.ts +3 -1
- package/lib/src/fingerprint.js +32 -5
- package/lib/src/host-config.d.ts +34 -0
- package/lib/src/host-config.js +645 -0
- package/lib/src/hosts.d.ts +47 -0
- package/lib/src/hosts.js +106 -0
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +1 -1
- package/lib/src/integrity.d.ts +115 -0
- package/lib/src/integrity.js +273 -0
- package/lib/src/language/ast.d.ts +116 -3
- package/lib/src/language/ast.js +7 -1
- package/lib/src/language/parser.js +176 -7
- package/lib/src/language/validate.js +21 -0
- package/lib/src/links.d.ts +33 -0
- package/lib/src/links.js +271 -3
- package/lib/src/lint.d.ts +1 -1
- package/lib/src/lint.js +26 -1
- package/lib/src/mime-data.d.ts +2 -0
- package/lib/src/mime-data.js +92 -0
- package/lib/src/oci.d.ts +20 -2
- package/lib/src/oci.js +70 -28
- package/lib/src/outdated.d.ts +3 -0
- package/lib/src/outdated.js +4 -4
- package/lib/src/planner.d.ts +69 -2
- package/lib/src/planner.js +150 -8
- package/lib/src/plugins/cache.d.ts +11 -0
- package/lib/src/plugins/cache.js +29 -0
- package/lib/src/plugins/host.d.ts +51 -2
- package/lib/src/plugins/host.js +141 -16
- package/lib/src/plugins/wasm.js +1 -3
- package/lib/src/portable.d.ts +8 -0
- package/lib/src/portable.js +47 -0
- package/lib/src/precache.d.ts +33 -0
- package/lib/src/precache.js +75 -0
- package/lib/src/precompress.d.ts +19 -0
- package/lib/src/precompress.js +35 -0
- package/lib/src/qr.d.ts +18 -0
- package/lib/src/qr.js +372 -0
- package/lib/src/registry-ops.js +4 -2
- package/lib/src/reload-client.d.ts +2 -0
- package/lib/src/reload-client.js +304 -0
- package/lib/src/resources/archive.d.ts +36 -0
- package/lib/src/resources/archive.js +338 -67
- package/lib/src/resources/convert.js +3 -1
- package/lib/src/resources/https.d.ts +9 -1
- package/lib/src/resources/https.js +36 -0
- package/lib/src/resources/network.d.ts +25 -3
- package/lib/src/resources/network.js +77 -6
- package/lib/src/secrets.d.ts +12 -3
- package/lib/src/secrets.js +18 -5
- package/lib/src/serve-auth.d.ts +32 -0
- package/lib/src/serve-auth.js +112 -0
- package/lib/src/serve-rules.d.ts +83 -7
- package/lib/src/serve-rules.js +264 -53
- package/lib/src/server.d.ts +437 -10
- package/lib/src/server.js +1236 -52
- package/lib/src/servers-file.d.ts +36 -0
- package/lib/src/servers-file.js +101 -0
- package/lib/src/servers.d.ts +109 -7
- package/lib/src/servers.js +151 -29
- package/lib/src/single-file.d.ts +12 -0
- package/lib/src/single-file.js +147 -0
- package/lib/src/site.d.ts +54 -0
- package/lib/src/site.js +204 -6
- package/lib/src/store.js +9 -5
- package/lib/src/swbn.d.ts +10 -0
- package/lib/src/swbn.js +86 -0
- package/lib/src/toml.d.ts +17 -0
- package/lib/src/toml.js +323 -0
- package/lib/src/wacz.d.ts +6 -0
- package/lib/src/wacz.js +96 -0
- package/lib/src/warc.d.ts +28 -0
- package/lib/src/warc.js +252 -0
- package/lib/src/watch.d.ts +12 -1
- package/lib/src/watch.js +67 -1
- package/lib/src/wbn.d.ts +9 -1
- package/lib/src/wbn.js +23 -33
- package/lib/src/zstd.d.ts +4 -0
- package/lib/src/zstd.js +328 -0
- package/package.json +1 -1
package/lib/src/toml.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { SitefileError } from './errors.js';
|
|
2
|
+
const BARE = /[A-Za-z0-9_-]/;
|
|
3
|
+
class Parser {
|
|
4
|
+
s;
|
|
5
|
+
file;
|
|
6
|
+
i = 0;
|
|
7
|
+
line = 1;
|
|
8
|
+
/** Tables created by a `[header]` (they may not be defined twice) and inline tables (closed). */
|
|
9
|
+
defined = new Set();
|
|
10
|
+
closed = new Set();
|
|
11
|
+
/** Arrays created by `[[header]]` (only those take more tables). */
|
|
12
|
+
tableArrays = new Set();
|
|
13
|
+
constructor(s, file) {
|
|
14
|
+
this.s = s;
|
|
15
|
+
this.file = file;
|
|
16
|
+
}
|
|
17
|
+
fail(msg) {
|
|
18
|
+
throw new SitefileError('E_DATA', `${this.file}:${this.line}: ${msg}`);
|
|
19
|
+
}
|
|
20
|
+
peek(n = 0) {
|
|
21
|
+
return this.s[this.i + n] ?? '';
|
|
22
|
+
}
|
|
23
|
+
/** Spaces and tabs. */
|
|
24
|
+
ws() {
|
|
25
|
+
while (this.peek() === ' ' || this.peek() === '\t')
|
|
26
|
+
this.i++;
|
|
27
|
+
}
|
|
28
|
+
comment() {
|
|
29
|
+
if (this.peek() === '#')
|
|
30
|
+
while (this.i < this.s.length && this.peek() !== '\n')
|
|
31
|
+
this.i++;
|
|
32
|
+
}
|
|
33
|
+
/** Whitespace, comments and newlines. */
|
|
34
|
+
blank() {
|
|
35
|
+
for (;;) {
|
|
36
|
+
this.ws();
|
|
37
|
+
this.comment();
|
|
38
|
+
if (this.peek() === '\r' && this.peek(1) === '\n')
|
|
39
|
+
this.i++;
|
|
40
|
+
if (this.peek() === '\n') {
|
|
41
|
+
this.i++;
|
|
42
|
+
this.line++;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
endOfLine() {
|
|
49
|
+
this.ws();
|
|
50
|
+
this.comment();
|
|
51
|
+
if (this.peek() === '\r' && this.peek(1) === '\n')
|
|
52
|
+
this.i++;
|
|
53
|
+
if (this.i >= this.s.length)
|
|
54
|
+
return;
|
|
55
|
+
if (this.peek() !== '\n')
|
|
56
|
+
this.fail('expected the end of the line');
|
|
57
|
+
}
|
|
58
|
+
key() {
|
|
59
|
+
const c = this.peek();
|
|
60
|
+
if (c === '"' || c === "'") {
|
|
61
|
+
if (this.s.startsWith(c.repeat(3), this.i))
|
|
62
|
+
this.fail('multi-line strings are not supported');
|
|
63
|
+
return c === '"' ? this.basic() : this.literal();
|
|
64
|
+
}
|
|
65
|
+
const start = this.i;
|
|
66
|
+
while (BARE.test(this.peek()))
|
|
67
|
+
this.i++;
|
|
68
|
+
if (this.i === start)
|
|
69
|
+
this.fail('expected a key');
|
|
70
|
+
return this.s.slice(start, this.i);
|
|
71
|
+
}
|
|
72
|
+
keyPath() {
|
|
73
|
+
const path = [this.key()];
|
|
74
|
+
for (;;) {
|
|
75
|
+
this.ws();
|
|
76
|
+
if (this.peek() !== '.')
|
|
77
|
+
return path;
|
|
78
|
+
this.i++;
|
|
79
|
+
this.ws();
|
|
80
|
+
path.push(this.key());
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
basic() {
|
|
84
|
+
this.i++; // "
|
|
85
|
+
let out = '';
|
|
86
|
+
for (;;) {
|
|
87
|
+
const c = this.peek();
|
|
88
|
+
if (c === '' || c === '\n')
|
|
89
|
+
this.fail('unterminated string');
|
|
90
|
+
this.i++;
|
|
91
|
+
if (c === '"')
|
|
92
|
+
return out;
|
|
93
|
+
if (c !== '\\') {
|
|
94
|
+
out += c;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const e = this.peek();
|
|
98
|
+
this.i++;
|
|
99
|
+
const simple = { b: '\b', t: '\t', n: '\n', f: '\f', r: '\r', '"': '"', '\\': '\\' };
|
|
100
|
+
if (e in simple)
|
|
101
|
+
out += simple[e];
|
|
102
|
+
else if (e === 'u' || e === 'U') {
|
|
103
|
+
const n = e === 'u' ? 4 : 8;
|
|
104
|
+
const hex = this.s.slice(this.i, this.i + n);
|
|
105
|
+
if (!new RegExp(`^[0-9A-Fa-f]{${n}}$`).test(hex))
|
|
106
|
+
this.fail('invalid escape in a string');
|
|
107
|
+
const cp = parseInt(hex, 16);
|
|
108
|
+
if (cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff))
|
|
109
|
+
this.fail('invalid escape in a string');
|
|
110
|
+
out += String.fromCodePoint(cp);
|
|
111
|
+
this.i += n;
|
|
112
|
+
}
|
|
113
|
+
else
|
|
114
|
+
this.fail('invalid escape in a string');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
literal() {
|
|
118
|
+
this.i++; // '
|
|
119
|
+
const start = this.i;
|
|
120
|
+
for (;;) {
|
|
121
|
+
const c = this.peek();
|
|
122
|
+
if (c === '' || c === '\n')
|
|
123
|
+
this.fail('unterminated string');
|
|
124
|
+
if (c === "'") {
|
|
125
|
+
const v = this.s.slice(start, this.i);
|
|
126
|
+
this.i++;
|
|
127
|
+
return v;
|
|
128
|
+
}
|
|
129
|
+
this.i++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
value() {
|
|
133
|
+
const c = this.peek();
|
|
134
|
+
if (c === '"' || c === "'") {
|
|
135
|
+
if (this.s.startsWith(c.repeat(3), this.i))
|
|
136
|
+
this.fail('multi-line strings are not supported');
|
|
137
|
+
return c === '"' ? this.basic() : this.literal();
|
|
138
|
+
}
|
|
139
|
+
if (c === '[')
|
|
140
|
+
return this.array();
|
|
141
|
+
if (c === '{')
|
|
142
|
+
return this.inlineTable();
|
|
143
|
+
const start = this.i;
|
|
144
|
+
while (/[A-Za-z0-9_+\-.:]/.test(this.peek()))
|
|
145
|
+
this.i++;
|
|
146
|
+
const word = this.s.slice(start, this.i);
|
|
147
|
+
if (word === 'true')
|
|
148
|
+
return true;
|
|
149
|
+
if (word === 'false')
|
|
150
|
+
return false;
|
|
151
|
+
if (/^[+-]?(0|[1-9](_?[0-9])*)$/.test(word))
|
|
152
|
+
return Number(word.replace(/_/g, ''));
|
|
153
|
+
if (/^[+-]?(0|[1-9](_?[0-9])*)(\.[0-9](_?[0-9])*)?([eE][+-]?[0-9](_?[0-9])*)?$/.test(word))
|
|
154
|
+
return Number(word.replace(/_/g, ''));
|
|
155
|
+
if (/^[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(word) || /^[0-9]{2}:[0-9]{2}/.test(word))
|
|
156
|
+
this.fail('dates and times are not supported');
|
|
157
|
+
if (word === '')
|
|
158
|
+
this.fail('expected a value');
|
|
159
|
+
this.fail(`unsupported value '${word}'`);
|
|
160
|
+
}
|
|
161
|
+
array() {
|
|
162
|
+
this.i++; // [
|
|
163
|
+
const out = [];
|
|
164
|
+
for (;;) {
|
|
165
|
+
this.blank();
|
|
166
|
+
if (this.peek() === ']') {
|
|
167
|
+
this.i++;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
out.push(this.value());
|
|
171
|
+
this.blank();
|
|
172
|
+
if (this.peek() === ',') {
|
|
173
|
+
this.i++;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (this.peek() === ']') {
|
|
177
|
+
this.i++;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
this.fail("expected ',' or ']' in an array");
|
|
181
|
+
}
|
|
182
|
+
this.closed.add(out);
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
inlineTable() {
|
|
186
|
+
this.i++; // {
|
|
187
|
+
const out = {};
|
|
188
|
+
this.ws();
|
|
189
|
+
if (this.peek() === '}') {
|
|
190
|
+
this.i++;
|
|
191
|
+
this.closed.add(out);
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
for (;;) {
|
|
195
|
+
this.ws();
|
|
196
|
+
const path = this.keyPath();
|
|
197
|
+
this.ws();
|
|
198
|
+
if (this.peek() !== '=')
|
|
199
|
+
this.fail("expected '=' after a key");
|
|
200
|
+
this.i++;
|
|
201
|
+
this.ws();
|
|
202
|
+
this.assign(out, path, this.value());
|
|
203
|
+
this.ws();
|
|
204
|
+
if (this.peek() === ',') {
|
|
205
|
+
this.i++;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (this.peek() === '}') {
|
|
209
|
+
this.i++;
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
this.fail("expected ',' or '}' in an inline table");
|
|
213
|
+
}
|
|
214
|
+
this.closed.add(out);
|
|
215
|
+
return out;
|
|
216
|
+
}
|
|
217
|
+
/** Set `path` = `value` under `table`, creating the intermediate tables of a dotted key. */
|
|
218
|
+
assign(table, path, value) {
|
|
219
|
+
let t = table;
|
|
220
|
+
for (const k of path.slice(0, -1)) {
|
|
221
|
+
const cur = Object.hasOwn(t, k) ? t[k] : undefined;
|
|
222
|
+
if (cur === undefined) {
|
|
223
|
+
const next = {};
|
|
224
|
+
t[k] = next;
|
|
225
|
+
t = next;
|
|
226
|
+
}
|
|
227
|
+
else if (isTable(cur) && !this.closed.has(cur) && !this.defined.has(cur))
|
|
228
|
+
t = cur;
|
|
229
|
+
else
|
|
230
|
+
this.fail(`key '${k}' is already defined`);
|
|
231
|
+
}
|
|
232
|
+
const last = path[path.length - 1];
|
|
233
|
+
if (Object.hasOwn(t, last))
|
|
234
|
+
this.fail(`key '${last}' is already defined`);
|
|
235
|
+
t[last] = value;
|
|
236
|
+
}
|
|
237
|
+
/** The table a header path names (through the last element of arrays of tables), creating tables. */
|
|
238
|
+
walk(root, path) {
|
|
239
|
+
let t = root;
|
|
240
|
+
for (const k of path) {
|
|
241
|
+
const cur = Object.hasOwn(t, k) ? t[k] : undefined;
|
|
242
|
+
if (cur === undefined) {
|
|
243
|
+
const next = {};
|
|
244
|
+
t[k] = next;
|
|
245
|
+
t = next;
|
|
246
|
+
}
|
|
247
|
+
else if (Array.isArray(cur) && this.tableArrays.has(cur))
|
|
248
|
+
t = cur[cur.length - 1];
|
|
249
|
+
else if (isTable(cur) && !this.closed.has(cur))
|
|
250
|
+
t = cur;
|
|
251
|
+
else
|
|
252
|
+
this.fail(`key '${k}' is already defined`);
|
|
253
|
+
}
|
|
254
|
+
return t;
|
|
255
|
+
}
|
|
256
|
+
document() {
|
|
257
|
+
const root = {};
|
|
258
|
+
let current = root;
|
|
259
|
+
for (;;) {
|
|
260
|
+
this.blank();
|
|
261
|
+
if (this.i >= this.s.length)
|
|
262
|
+
return root;
|
|
263
|
+
if (this.peek() === '[') {
|
|
264
|
+
const many = this.peek(1) === '[';
|
|
265
|
+
this.i += many ? 2 : 1;
|
|
266
|
+
this.ws();
|
|
267
|
+
const path = this.keyPath();
|
|
268
|
+
this.ws();
|
|
269
|
+
if (!this.s.startsWith(many ? ']]' : ']', this.i))
|
|
270
|
+
this.fail(many ? "expected ']]'" : "expected ']'");
|
|
271
|
+
this.i += many ? 2 : 1;
|
|
272
|
+
this.endOfLine();
|
|
273
|
+
if (many) {
|
|
274
|
+
const parent = this.walk(root, path.slice(0, -1));
|
|
275
|
+
const k = path[path.length - 1];
|
|
276
|
+
const cur = Object.hasOwn(parent, k) ? parent[k] : undefined;
|
|
277
|
+
let arr;
|
|
278
|
+
if (cur === undefined) {
|
|
279
|
+
arr = [];
|
|
280
|
+
parent[k] = arr;
|
|
281
|
+
this.tableArrays.add(arr);
|
|
282
|
+
}
|
|
283
|
+
else if (Array.isArray(cur) && this.tableArrays.has(cur))
|
|
284
|
+
arr = cur;
|
|
285
|
+
else
|
|
286
|
+
this.fail(`key '${k}' is already defined`);
|
|
287
|
+
const t = {};
|
|
288
|
+
arr.push(t);
|
|
289
|
+
current = t;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const t = this.walk(root, path);
|
|
293
|
+
if (this.defined.has(t))
|
|
294
|
+
this.fail(`table '${path.join('.')}' is defined twice`);
|
|
295
|
+
this.defined.add(t);
|
|
296
|
+
current = t;
|
|
297
|
+
}
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
const path = this.keyPath();
|
|
301
|
+
this.ws();
|
|
302
|
+
if (this.peek() !== '=')
|
|
303
|
+
this.fail("expected '=' after a key");
|
|
304
|
+
this.i++;
|
|
305
|
+
this.ws();
|
|
306
|
+
const line = this.line;
|
|
307
|
+
const v = this.value();
|
|
308
|
+
const end = this.line;
|
|
309
|
+
this.line = line;
|
|
310
|
+
this.assign(current, path, v);
|
|
311
|
+
this.line = end;
|
|
312
|
+
this.endOfLine();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function isTable(v) {
|
|
317
|
+
return v !== undefined && typeof v === 'object' && !Array.isArray(v);
|
|
318
|
+
}
|
|
319
|
+
/** Parse a TOML document (the subset above). Errors are E_DATA `file:line: message`. */
|
|
320
|
+
export function parseToml(text, file) {
|
|
321
|
+
return new Parser(text.replace(/^/, ''), file).document();
|
|
322
|
+
}
|
|
323
|
+
//# sourceMappingURL=toml.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Tree } from './tree.js';
|
|
2
|
+
import { type WarcOptions } from './warc.js';
|
|
3
|
+
/** The SURT form of an http(s) URL: `org,example)/path` (host labels reversed, a leading `www.` dropped). */
|
|
4
|
+
export declare function surt(url: string): string;
|
|
5
|
+
/** `EXPORT … AS wacz`: the WACZ file. */
|
|
6
|
+
export declare function writeWacz(tree: Tree, options: WarcOptions): Uint8Array;
|
package/lib/src/wacz.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { writeZip } from './archive-writers.js';
|
|
3
|
+
import { gzip } from './deflate.js';
|
|
4
|
+
import { SitefileError } from './errors.js';
|
|
5
|
+
import { makeFile, Tree } from './tree.js';
|
|
6
|
+
import { isoDate as iso, warcRecords } from './warc.js';
|
|
7
|
+
/**
|
|
8
|
+
* `EXPORT / AS wacz BASE <url>` (issue #206): a WACZ 1.1.1 package (Webrecorder), the format
|
|
9
|
+
* ReplayWeb.page opens. A zip of:
|
|
10
|
+
*
|
|
11
|
+
* - `archive/data.warc.gz`: the site's WARC (as `AS warc COMPRESSION gzip` writes it), one gzip
|
|
12
|
+
* member per record;
|
|
13
|
+
* - `indexes/index.cdx`: a CDXJ line per response record, `<surt> <timestamp> {json}` sorted, with the
|
|
14
|
+
* record's offset and length in `data.warc.gz`;
|
|
15
|
+
* - `pages/pages.jsonl`: the HTML pages (an index page by its directory URL), with their `<title>`;
|
|
16
|
+
* - `datapackage.json` (every file with its SHA-256 and size) and `datapackage-digest.json`.
|
|
17
|
+
*
|
|
18
|
+
* Dates come from `SOURCE_DATE_EPOCH` (default the Unix epoch), so the package is deterministic.
|
|
19
|
+
*/
|
|
20
|
+
const enc = new TextEncoder();
|
|
21
|
+
const dec = new TextDecoder();
|
|
22
|
+
/** `YYYYMMDDhhmmss`. */
|
|
23
|
+
function timestamp14(epoch) {
|
|
24
|
+
return iso(epoch).replace(/[-:TZ]/g, '');
|
|
25
|
+
}
|
|
26
|
+
/** The SURT form of an http(s) URL: `org,example)/path` (host labels reversed, a leading `www.` dropped). */
|
|
27
|
+
export function surt(url) {
|
|
28
|
+
const m = /^https?:\/\/([^/?#]*)(.*)$/i.exec(url);
|
|
29
|
+
if (!m)
|
|
30
|
+
return url;
|
|
31
|
+
const labels = m[1].toLowerCase().split('.');
|
|
32
|
+
if (labels[0] === 'www' && labels.length > 1)
|
|
33
|
+
labels.shift();
|
|
34
|
+
return `${labels.reverse().join(',')})${m[2] === '' ? '/' : m[2]}`;
|
|
35
|
+
}
|
|
36
|
+
function titleOf(bytes) {
|
|
37
|
+
const m = /<title(?:\s[^>]*)?>([^<]*)<\/title>/i.exec(dec.decode(bytes));
|
|
38
|
+
const t = m?.[1]?.replace(/\s+/g, ' ').trim();
|
|
39
|
+
return t ? t : undefined;
|
|
40
|
+
}
|
|
41
|
+
const sha256 = (b) => 'sha256:' + createHash('sha256').update(b).digest('hex');
|
|
42
|
+
/** `EXPORT … AS wacz`: the WACZ file. */
|
|
43
|
+
export function writeWacz(tree, options) {
|
|
44
|
+
if (options.baseUrl === undefined || options.baseUrl === '') {
|
|
45
|
+
throw new SitefileError('E_VALIDATION', "wacz export needs a base URL: use 'EXPORT <path> AS wacz BASE https://example.com/' or --base-url");
|
|
46
|
+
}
|
|
47
|
+
const records = warcRecords(tree, options);
|
|
48
|
+
const ts = timestamp14(options.epoch);
|
|
49
|
+
const members = [];
|
|
50
|
+
const cdx = [];
|
|
51
|
+
const pages = [];
|
|
52
|
+
let offset = 0;
|
|
53
|
+
for (const r of records) {
|
|
54
|
+
const member = gzip(r.bytes);
|
|
55
|
+
members.push(member);
|
|
56
|
+
const res = r.response;
|
|
57
|
+
if (res) {
|
|
58
|
+
const entry = { url: res.uri, mime: res.type.split(';')[0].trim(), status: '200', digest: res.digest, length: member.byteLength, offset, filename: 'data.warc.gz' };
|
|
59
|
+
cdx.push(`${surt(res.uri)} ${ts} ${JSON.stringify(entry)}`);
|
|
60
|
+
// An index page once, by its directory URL; other HTML pages by their own.
|
|
61
|
+
const index = res.path === '/index.html' || res.path.endsWith('/index.html');
|
|
62
|
+
if (res.type.startsWith('text/html') && (!index || res.uri.endsWith('/'))) {
|
|
63
|
+
const title = titleOf(tree.get(res.path).bytes);
|
|
64
|
+
pages.push(JSON.stringify({ id: sha256(enc.encode(res.uri)).slice(7, 39), url: res.uri, ts: iso(options.epoch), ...(title !== undefined ? { title } : {}) }));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
offset += member.byteLength;
|
|
68
|
+
}
|
|
69
|
+
cdx.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
70
|
+
const warc = new Uint8Array(offset);
|
|
71
|
+
let at = 0;
|
|
72
|
+
for (const m of members) {
|
|
73
|
+
warc.set(m, at);
|
|
74
|
+
at += m.byteLength;
|
|
75
|
+
}
|
|
76
|
+
const files = [
|
|
77
|
+
['archive/data.warc.gz', warc],
|
|
78
|
+
['indexes/index.cdx', enc.encode(cdx.map((l) => l + '\n').join(''))],
|
|
79
|
+
['pages/pages.jsonl', enc.encode([JSON.stringify({ format: 'json-pages-1.0', id: 'pages', title: 'All Pages' }), ...pages].map((l) => l + '\n').join(''))],
|
|
80
|
+
];
|
|
81
|
+
const datapackage = enc.encode(JSON.stringify({
|
|
82
|
+
profile: 'data-package',
|
|
83
|
+
resources: files.map(([path, bytes]) => ({ name: path.slice(path.lastIndexOf('/') + 1), path, hash: sha256(bytes), bytes: bytes.byteLength })),
|
|
84
|
+
wacz_version: '1.1.1',
|
|
85
|
+
software: 'sitefile',
|
|
86
|
+
created: iso(options.epoch),
|
|
87
|
+
}, null, 2) + '\n');
|
|
88
|
+
const digest = enc.encode(JSON.stringify({ path: 'datapackage.json', hash: sha256(datapackage) }, null, 2) + '\n');
|
|
89
|
+
const out = [
|
|
90
|
+
...files.map(([p, b]) => [`/${p}`, makeFile(b, 'wacz')]),
|
|
91
|
+
['/datapackage.json', makeFile(datapackage, 'wacz')],
|
|
92
|
+
['/datapackage-digest.json', makeFile(digest, 'wacz')],
|
|
93
|
+
];
|
|
94
|
+
return writeZip(Tree.fromFiles(out), { epoch: options.epoch });
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=wacz.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type SourceSpan } from './errors.js';
|
|
2
|
+
import { type ArchiveLimits } from './resources/types.js';
|
|
3
|
+
import { Tree } from './tree.js';
|
|
4
|
+
export declare function isoDate(epoch: number | undefined): string;
|
|
5
|
+
export interface WarcOptions {
|
|
6
|
+
readonly baseUrl?: string | undefined;
|
|
7
|
+
/** SOURCE_DATE_EPOCH: the WARC-Date of every record. */
|
|
8
|
+
readonly epoch?: number | undefined;
|
|
9
|
+
readonly compression?: 'gzip' | 'none' | undefined;
|
|
10
|
+
}
|
|
11
|
+
/** One WARC record and, for a response, what an index needs (WACZ). */
|
|
12
|
+
export interface WarcRecord {
|
|
13
|
+
readonly bytes: Uint8Array;
|
|
14
|
+
readonly response?: {
|
|
15
|
+
readonly uri: string;
|
|
16
|
+
readonly path: string;
|
|
17
|
+
readonly type: string;
|
|
18
|
+
readonly digest: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** `EXPORT … AS warc`: the tree as WARC/1.1 records (see the top of this file). */
|
|
22
|
+
export declare function writeWarc(tree: Tree, options: WarcOptions): Uint8Array;
|
|
23
|
+
/** The records {@link writeWarc} writes, in order. */
|
|
24
|
+
export declare function warcRecords(tree: Tree, options: WarcOptions): WarcRecord[];
|
|
25
|
+
/** True when `bytes` start a WARC file, plain or gzipped (the gzip is inflated to check). */
|
|
26
|
+
export declare function looksLikeWarc(bytes: Uint8Array): boolean;
|
|
27
|
+
/** The files of a WARC (plain or gzipped): see the top of this file. */
|
|
28
|
+
export declare function readWarc(input: Uint8Array, origin: string, limits?: ArchiveLimits, span?: SourceSpan): Tree;
|