@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
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { closeSync, fstatSync, openSync, readSync } from 'node:fs';
|
|
2
|
+
import { crc32, gunzip as gunzipCb, inflateRaw as inflateRawCb, inflateRawSync } from 'node:zlib';
|
|
2
3
|
import { promisify } from 'node:util';
|
|
3
4
|
import { SitefileError } from '../errors.js';
|
|
4
5
|
import { normalizeVirtual } from '../paths.js';
|
|
@@ -21,6 +22,27 @@ const TAR_OVERHEAD_PER_ENTRY = 4 * 512;
|
|
|
21
22
|
export async function extractArchive(bytes, origin, limits = DEFAULT_ARCHIVE_LIMITS, span) {
|
|
22
23
|
if (isWebBundle(bytes))
|
|
23
24
|
return webBundleToTree(readWebBundle(bytes, origin, limits, span), origin, span);
|
|
25
|
+
const { add, files } = collector(origin, limits, span, true);
|
|
26
|
+
if (isGzip(bytes)) {
|
|
27
|
+
const tar = await gunzip(bytes, origin, limits, span);
|
|
28
|
+
if (!isTar(tar))
|
|
29
|
+
throw new SitefileError('E_TYPE', `${origin}: gzip content is not a tar archive`, span);
|
|
30
|
+
readTar(tar, origin, limits, span, add);
|
|
31
|
+
}
|
|
32
|
+
else if (isZip(bytes)) {
|
|
33
|
+
await readZip(bytes, origin, limits, span, add);
|
|
34
|
+
}
|
|
35
|
+
else if (isTar(bytes)) {
|
|
36
|
+
readTar(bytes, origin, limits, span, add);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const what = otherFormat(bytes);
|
|
40
|
+
throw new SitefileError('E_TYPE', `${origin}: not a recognized archive (${what ?? 'expected tar, tar.gz, zip or web bundle'})`, span);
|
|
41
|
+
}
|
|
42
|
+
return Tree.fromFiles(files);
|
|
43
|
+
}
|
|
44
|
+
/** Collects archive entries into files, enforcing paths, conflicts and the limits. `retain` false keeps only digests. */
|
|
45
|
+
function collector(origin, limits, span, retain) {
|
|
24
46
|
const files = new Map();
|
|
25
47
|
const dirs = new Set();
|
|
26
48
|
let total = 0;
|
|
@@ -42,24 +64,50 @@ export async function extractArchive(bytes, origin, limits = DEFAULT_ARCHIVE_LIM
|
|
|
42
64
|
if (total > limits.maxTotalBytes) {
|
|
43
65
|
throw new SitefileError('E_ARCHIVE_LIMIT', `${origin}: archive expands beyond ${limits.maxTotalBytes} bytes`, span);
|
|
44
66
|
}
|
|
45
|
-
|
|
67
|
+
const node = makeFile(data, `${origin}:${name}`, mode);
|
|
68
|
+
files.set(path, retain ? node : { ...node, bytes: EMPTY });
|
|
46
69
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return
|
|
70
|
+
return { add, files };
|
|
71
|
+
}
|
|
72
|
+
const EMPTY = new Uint8Array(0);
|
|
73
|
+
/**
|
|
74
|
+
* OS metadata that archivers and file managers add next to real files: macOS
|
|
75
|
+
* resource forks (`__MACOSX/`, AppleDouble `._name`), Finder's `.DS_Store` and
|
|
76
|
+
* custom folder `Icon\r`, and Windows' `Thumbs.db` and `desktop.ini`
|
|
77
|
+
* (case-insensitive, as Windows writes them).
|
|
78
|
+
*/
|
|
79
|
+
export function isOsMetadata(path) {
|
|
80
|
+
const segs = path.split('/');
|
|
81
|
+
if (segs.includes('__MACOSX'))
|
|
82
|
+
return true;
|
|
83
|
+
const base = segs[segs.length - 1] ?? '';
|
|
84
|
+
const lower = base.toLowerCase();
|
|
85
|
+
return base.startsWith('._') || base === '.DS_Store' || base === 'Icon\r' || lower === 'thumbs.db' || lower === 'desktop.ini';
|
|
86
|
+
}
|
|
87
|
+
/** `tree` without its OS metadata files ({@link isOsMetadata}), and how many were dropped. */
|
|
88
|
+
export function withoutOsMetadata(tree) {
|
|
89
|
+
const kept = tree.entries().filter(([p]) => !isOsMetadata(p));
|
|
90
|
+
const skipped = tree.size - kept.length;
|
|
91
|
+
return { tree: skipped === 0 ? tree : Tree.fromFiles(kept), skipped };
|
|
92
|
+
}
|
|
93
|
+
/** The warning for {@link withoutOsMetadata} dropping `skipped` files of `what`. */
|
|
94
|
+
export function osMetadataWarning(what, skipped) {
|
|
95
|
+
return `${what}: skipped ${skipped} OS metadata ${skipped === 1 ? 'file' : 'files'} (__MACOSX, ._*, .DS_Store, Thumbs.db, desktop.ini, Icon\\r); --keep-os-metadata keeps them`;
|
|
96
|
+
}
|
|
97
|
+
/** Formats sitefile does not read, by magic bytes: what the file is and what to do instead. */
|
|
98
|
+
function otherFormat(b) {
|
|
99
|
+
const starts = (...m) => b.length >= m.length && m.every((x, i) => b[i] === x);
|
|
100
|
+
if (starts(0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00))
|
|
101
|
+
return 'it is xz-compressed, e.g. .tar.xz; recompress it as .tar.gz or zip';
|
|
102
|
+
if (starts(0x28, 0xb5, 0x2f, 0xfd))
|
|
103
|
+
return 'it is zstd-compressed, e.g. .tar.zst; recompress it as .tar.gz or zip';
|
|
104
|
+
if (starts(0x42, 0x5a, 0x68) && b.length > 3 && b[3] >= 0x31 && b[3] <= 0x39)
|
|
105
|
+
return 'it is bzip2-compressed, e.g. .tar.bz2; recompress it as .tar.gz or zip';
|
|
106
|
+
if (starts(0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c))
|
|
107
|
+
return 'it is a 7z archive; convert it to zip or tar';
|
|
108
|
+
if (starts(0x52, 0x61, 0x72, 0x21, 0x1a, 0x07))
|
|
109
|
+
return 'it is a RAR archive; convert it to zip or tar';
|
|
110
|
+
return undefined;
|
|
63
111
|
}
|
|
64
112
|
const isGzip = (b) => b.length > 2 && b[0] === 0x1f && b[1] === 0x8b;
|
|
65
113
|
const isZip = (b) => b.length >= 4 && b[0] === 0x50 && b[1] === 0x4b && ((b[2] === 3 && b[3] === 4) || (b[2] === 5 && b[3] === 6));
|
|
@@ -211,52 +259,156 @@ function parsePax(data, bad) {
|
|
|
211
259
|
}
|
|
212
260
|
return out;
|
|
213
261
|
}
|
|
214
|
-
|
|
262
|
+
/** IBM437, bytes 0x80–0xFF (0x00–0x7F are ASCII). */
|
|
263
|
+
const CP437_HIGH = '\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5' +
|
|
264
|
+
'\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00a2\u00a3\u00a5\u20a7\u0192' +
|
|
265
|
+
'\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u2310\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb' +
|
|
266
|
+
'\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510' +
|
|
267
|
+
'\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567' +
|
|
268
|
+
'\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580' +
|
|
269
|
+
'\u03b1\u00df\u0393\u03c0\u03a3\u03c3\u00b5\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u03c6\u03b5\u2229' +
|
|
270
|
+
'\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u00a0';
|
|
271
|
+
/** Strict UTF-8 (a BOM is kept), or undefined. */
|
|
272
|
+
function utf8Strict(b) {
|
|
273
|
+
try {
|
|
274
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(b);
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* A zip entry name (APPNOTE 4.4.4, 4.6.9, appendix D). With general-purpose bit 11 it is UTF-8.
|
|
282
|
+
* Without it: the Info-ZIP Unicode Path field (0x7075, version 1) when its CRC-32 matches the raw
|
|
283
|
+
* name; else the raw name as UTF-8 when it is valid UTF-8 (macOS, 7-Zip and many tools write UTF-8
|
|
284
|
+
* without the flag; CP437 text of non-ASCII letters is practically never valid UTF-8); else IBM437.
|
|
285
|
+
*/
|
|
286
|
+
function zipName(raw, flags, extra) {
|
|
287
|
+
if (flags & 0x800)
|
|
288
|
+
return Buffer.from(raw).toString('utf8');
|
|
289
|
+
for (let i = 0; i + 4 <= extra.length;) {
|
|
290
|
+
const id = extra[i] | (extra[i + 1] << 8);
|
|
291
|
+
const size = extra[i + 2] | (extra[i + 3] << 8);
|
|
292
|
+
if (i + 4 + size > extra.length)
|
|
293
|
+
break;
|
|
294
|
+
if (id === 0x7075 && size >= 5 && extra[i + 4] === 1) {
|
|
295
|
+
const stored = (extra[i + 5] | (extra[i + 6] << 8) | (extra[i + 7] << 16) | (extra[i + 8] << 24)) >>> 0;
|
|
296
|
+
const name = stored === crc32(raw) ? utf8Strict(extra.subarray(i + 9, i + 4 + size)) : undefined;
|
|
297
|
+
if (name !== undefined)
|
|
298
|
+
return name;
|
|
299
|
+
}
|
|
300
|
+
i += 4 + size;
|
|
301
|
+
}
|
|
302
|
+
const utf8 = utf8Strict(raw);
|
|
303
|
+
if (utf8 !== undefined)
|
|
304
|
+
return utf8;
|
|
305
|
+
let out = '';
|
|
306
|
+
for (const c of raw)
|
|
307
|
+
out += c < 0x80 ? String.fromCharCode(c) : CP437_HIGH[c - 0x80];
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
/** The data of the first extra field `id` in `extra`, or undefined. */
|
|
311
|
+
function extraField(extra, id) {
|
|
312
|
+
for (let i = 0; i + 4 <= extra.length;) {
|
|
313
|
+
const size = extra[i + 2] | (extra[i + 3] << 8);
|
|
314
|
+
if (i + 4 + size > extra.length)
|
|
315
|
+
return undefined;
|
|
316
|
+
if ((extra[i] | (extra[i + 1] << 8)) === id)
|
|
317
|
+
return extra.subarray(i + 4, i + 4 + size);
|
|
318
|
+
i += 4 + size;
|
|
319
|
+
}
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
/** A little-endian 64-bit integer as a number (values beyond 2^53 are rounded; they fail the limits anyway). */
|
|
323
|
+
const u64 = (b, at) => Number(new DataView(b.buffer, b.byteOffset, b.byteLength).getBigUint64(at, true));
|
|
324
|
+
const memorySource = (b) => ({ length: b.length, read: (at, len) => b.subarray(at, at + len) });
|
|
325
|
+
/**
|
|
326
|
+
* Read and check a zip's central directory and local headers (no entry data): sizes and
|
|
327
|
+
* counts against the limits, ZIP64 records, names, unsafe entry types, local headers that
|
|
328
|
+
* disagree with the central directory, overlapping entries. In central-directory order.
|
|
329
|
+
*/
|
|
330
|
+
function zipIndex(src, origin, limits, span) {
|
|
215
331
|
const bad = (why) => new SitefileError('E_ARCHIVE', `${origin}: ${why}`, span);
|
|
216
|
-
const
|
|
332
|
+
const r16 = (at) => { const x = src.read(at, 2); return x[0] | (x[1] << 8); };
|
|
333
|
+
const r32 = (at) => { const x = src.read(at, 4); return (x[0] | (x[1] << 8) | (x[2] << 16) | (x[3] << 24)) >>> 0; };
|
|
334
|
+
const r64 = (at) => u64(src.read(at, 8), 0);
|
|
335
|
+
const length = src.length;
|
|
217
336
|
let eocd = -1;
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
337
|
+
{
|
|
338
|
+
// The end record is in the last 22 + 65535 bytes (its comment is at most 65535 bytes).
|
|
339
|
+
const from = Math.max(0, length - 22 - 0xffff);
|
|
340
|
+
const tail = length >= 22 ? src.read(from, length - from) : new Uint8Array(0);
|
|
341
|
+
for (let i = tail.length - 22; i >= 0; i--) {
|
|
342
|
+
if (tail[i] === 0x50 && tail[i + 1] === 0x4b && tail[i + 2] === 5 && tail[i + 3] === 6) {
|
|
343
|
+
eocd = from + i;
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
222
346
|
}
|
|
223
347
|
}
|
|
224
348
|
if (eocd < 0)
|
|
225
349
|
throw bad('zip end-of-central-directory record not found');
|
|
226
|
-
|
|
227
|
-
let p =
|
|
228
|
-
|
|
229
|
-
|
|
350
|
+
let count = r16(eocd + 10);
|
|
351
|
+
let p = r32(eocd + 16);
|
|
352
|
+
// ZIP64 (APPNOTE 4.3.14-4.3.15): a locator right before the end record points at the zip64 end
|
|
353
|
+
// record, whose counts and offsets replace the 16- and 32-bit ones. Streaming writers add it even to small archives.
|
|
354
|
+
if (eocd >= 20 && r32(eocd - 20) === 0x07064b50) {
|
|
355
|
+
const r = r64(eocd - 12);
|
|
356
|
+
if (r > length - 56 || r32(r) !== 0x06064b50)
|
|
357
|
+
throw bad('corrupt zip64 end-of-central-directory record');
|
|
358
|
+
count = r64(r + 32);
|
|
359
|
+
p = r64(r + 48);
|
|
360
|
+
}
|
|
230
361
|
if (count > limits.maxEntries) {
|
|
231
362
|
throw new SitefileError('E_ARCHIVE_LIMIT', `${origin}: archive has more than ${limits.maxEntries} entries`, span);
|
|
232
363
|
}
|
|
233
364
|
const list = [];
|
|
234
365
|
let declared = 0;
|
|
235
366
|
for (let i = 0; i < count; i++) {
|
|
236
|
-
if (p + 46 >
|
|
367
|
+
if (p + 46 > length || r32(p) !== 0x02014b50)
|
|
237
368
|
throw bad('corrupt zip central directory');
|
|
238
|
-
const nameLen =
|
|
239
|
-
const
|
|
240
|
-
|
|
369
|
+
const nameLen = r16(p + 28);
|
|
370
|
+
const extraLen = r16(p + 30);
|
|
371
|
+
const advance = 46 + nameLen + extraLen + r16(p + 32);
|
|
372
|
+
if (p + advance > length)
|
|
241
373
|
throw bad('corrupt zip central directory');
|
|
242
|
-
const unix =
|
|
243
|
-
const attr =
|
|
374
|
+
const unix = r16(p + 4) >> 8 === 3;
|
|
375
|
+
const attr = r32(p + 38) >>> 16;
|
|
244
376
|
const fileType = attr & 0o170000;
|
|
245
|
-
const
|
|
377
|
+
const flags = r16(p + 8);
|
|
378
|
+
const rawName = src.read(p + 46, nameLen);
|
|
379
|
+
const extra = src.read(p + 46 + nameLen, extraLen);
|
|
380
|
+
const name = zipName(rawName, flags, extra);
|
|
246
381
|
const e = {
|
|
247
382
|
name,
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
383
|
+
rawName,
|
|
384
|
+
method: r16(p + 10),
|
|
385
|
+
crc: r32(p + 16),
|
|
386
|
+
csize: r32(p + 20),
|
|
387
|
+
usize: r32(p + 24),
|
|
388
|
+
local: r32(p + 42),
|
|
252
389
|
mode: unix ? attr & 0o7777 : 0o644,
|
|
253
390
|
// Some tools mark directories only by mode, without a trailing slash.
|
|
254
391
|
isDir: name.endsWith('/') || (unix && fileType === 0o040000),
|
|
255
392
|
};
|
|
256
|
-
if (
|
|
393
|
+
if (flags & 1)
|
|
257
394
|
throw bad(`encrypted zip entry '${e.name}' is not supported`);
|
|
258
|
-
|
|
259
|
-
|
|
395
|
+
// ZIP64 extra field (0x0001): the 64-bit values of the fields that are 0xffffffff, in this order.
|
|
396
|
+
if (e.usize === 0xffffffff || e.csize === 0xffffffff || e.local === 0xffffffff) {
|
|
397
|
+
const z = extraField(extra, 0x0001);
|
|
398
|
+
let at = 0;
|
|
399
|
+
const next = () => {
|
|
400
|
+
if (!z || at + 8 > z.length)
|
|
401
|
+
throw bad(`corrupt zip64 extra field for '${e.name}'`);
|
|
402
|
+
at += 8;
|
|
403
|
+
return u64(z, at - 8);
|
|
404
|
+
};
|
|
405
|
+
if (e.usize === 0xffffffff)
|
|
406
|
+
e.usize = next();
|
|
407
|
+
if (e.csize === 0xffffffff)
|
|
408
|
+
e.csize = next();
|
|
409
|
+
if (e.local === 0xffffffff)
|
|
410
|
+
e.local = next();
|
|
411
|
+
}
|
|
260
412
|
if (unix && fileType !== 0 && fileType !== 0o100000 && fileType !== 0o040000) {
|
|
261
413
|
throw new SitefileError('E_ARCHIVE_UNSAFE', `unsafe archive entry '${e.name}': symlink or special file entries are not allowed`, span);
|
|
262
414
|
}
|
|
@@ -267,40 +419,159 @@ async function readZip(b, origin, limits, span, add) {
|
|
|
267
419
|
if (declared > limits.maxTotalBytes) {
|
|
268
420
|
throw new SitefileError('E_ARCHIVE_LIMIT', `${origin}: archive expands beyond ${limits.maxTotalBytes} bytes`, span);
|
|
269
421
|
}
|
|
270
|
-
if (declared > Math.max(
|
|
422
|
+
if (declared > Math.max(length * limits.maxRatio, RATIO_FLOOR)) {
|
|
271
423
|
throw new SitefileError('E_ARCHIVE_LIMIT', `${origin}: compression ratio exceeds ${limits.maxRatio}:1`, span);
|
|
272
424
|
}
|
|
425
|
+
// Every local header must agree with its central record, and no two entries' bytes may overlap:
|
|
426
|
+
// otherwise other tools (unzip, browsers, Archive Utility) would see different files (#184).
|
|
427
|
+
const unsafe = (e, why) => new SitefileError('E_ARCHIVE_UNSAFE', `unsafe archive entry '${e.name}': ${why}`, span);
|
|
428
|
+
const spans = [];
|
|
273
429
|
for (const e of list) {
|
|
274
|
-
if (e.isDir) {
|
|
275
|
-
add(e.name, 'dir', new Uint8Array(0), 0o755);
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
430
|
const h = e.local;
|
|
279
|
-
if (h + 30 >
|
|
431
|
+
if (h + 30 > length || r32(h) !== 0x04034b50)
|
|
280
432
|
throw bad(`corrupt zip local header for '${e.name}'`);
|
|
281
|
-
const
|
|
282
|
-
|
|
433
|
+
const nameLen = r16(h + 26);
|
|
434
|
+
const start = h + 30 + nameLen + r16(h + 28);
|
|
435
|
+
if (start + e.csize > length)
|
|
283
436
|
throw bad(`truncated zip entry '${e.name}'`);
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (e.method
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
437
|
+
if (Buffer.compare(src.read(h + 30, nameLen), e.rawName) !== 0)
|
|
438
|
+
throw unsafe(e, 'its local header has a different name');
|
|
439
|
+
if (r16(h + 8) !== e.method)
|
|
440
|
+
throw unsafe(e, 'its local header has a different compression method');
|
|
441
|
+
// With a data descriptor (bit 3) the local sizes may be zero; the descriptor follows the data.
|
|
442
|
+
if (!(r16(h + 6) & 8)) {
|
|
443
|
+
let csize = r32(h + 18);
|
|
444
|
+
let usize = r32(h + 22);
|
|
445
|
+
if (csize === 0xffffffff || usize === 0xffffffff) {
|
|
446
|
+
// A local ZIP64 extra field holds both sizes (APPNOTE 4.5.3); Python's force_zip64 writes one.
|
|
447
|
+
const z = extraField(src.read(h + 30 + nameLen, start - (h + 30 + nameLen)), 0x0001);
|
|
448
|
+
if (z && z.length >= 16)
|
|
449
|
+
[usize, csize] = [u64(z, 0), u64(z, 8)];
|
|
296
450
|
}
|
|
451
|
+
if (csize !== e.csize || usize !== e.usize)
|
|
452
|
+
throw unsafe(e, 'its local header has different sizes');
|
|
297
453
|
}
|
|
298
|
-
|
|
299
|
-
|
|
454
|
+
spans.push({ e, from: h, start, end: start + e.csize });
|
|
455
|
+
}
|
|
456
|
+
const byOffset = [...spans].sort((a, c) => a.from - c.from);
|
|
457
|
+
for (let i = 1; i < byOffset.length; i++) {
|
|
458
|
+
const prev = byOffset[i - 1];
|
|
459
|
+
const cur = byOffset[i];
|
|
460
|
+
if (cur.from < prev.end)
|
|
461
|
+
throw unsafe(cur.e, `its data overlaps entry '${prev.e.name}'`);
|
|
462
|
+
}
|
|
463
|
+
return spans.map(({ e, start }) => ({ e, start }));
|
|
464
|
+
}
|
|
465
|
+
/** Check an inflated (or stored) entry against its header: size and CRC-32. */
|
|
466
|
+
function checkZipData(data, e, bad) {
|
|
467
|
+
if (data.length !== e.usize)
|
|
468
|
+
throw bad(`zip entry '${e.name}' size does not match its header`);
|
|
469
|
+
// Inflate cannot notice a corrupt stored entry; the CRC-32 can.
|
|
470
|
+
if (crc32(data) !== e.crc)
|
|
471
|
+
throw bad(`zip entry '${e.name}' fails its CRC-32 check`);
|
|
472
|
+
return data;
|
|
473
|
+
}
|
|
474
|
+
/** An entry's bytes, inflated (never beyond its declared size) and checked. */
|
|
475
|
+
async function zipData(src, { e, start }, origin, limits, span) {
|
|
476
|
+
const bad = (why) => new SitefileError('E_ARCHIVE', `${origin}: ${why}`, span);
|
|
477
|
+
const raw = src.read(start, e.csize);
|
|
478
|
+
let data;
|
|
479
|
+
if (e.method === 0) {
|
|
480
|
+
data = raw;
|
|
481
|
+
}
|
|
482
|
+
else if (e.method === 8) {
|
|
483
|
+
try {
|
|
484
|
+
// Headers can lie: never inflate more than the declared size.
|
|
485
|
+
data = await inflateRawAsync(raw, { maxOutputLength: Math.max(e.usize, 1) });
|
|
486
|
+
}
|
|
487
|
+
catch (err) {
|
|
488
|
+
throw inflateError(err, origin, false, limits, span);
|
|
300
489
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
throw bad(`zip entry '${e.name}' uses unsupported compression method ${e.method}`);
|
|
493
|
+
}
|
|
494
|
+
return checkZipData(data, e, bad);
|
|
495
|
+
}
|
|
496
|
+
async function readZip(b, origin, limits, span, add) {
|
|
497
|
+
const src = memorySource(b);
|
|
498
|
+
for (const s of zipIndex(src, origin, limits, span)) {
|
|
499
|
+
if (s.e.isDir) {
|
|
500
|
+
add(s.e.name, 'dir', EMPTY, 0o755);
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
add(s.e.name, 'file', await zipData(src, s, origin, limits, span), s.e.mode);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Open a zip file for serving without extracting it into memory: the central directory and
|
|
508
|
+
* local headers are read and checked as {@link extractArchive} checks them, and every entry is
|
|
509
|
+
* inflated once (one at a time) for its CRC-32, digest and the limits. Afterwards only the
|
|
510
|
+
* entry index is held; `read` reads an entry from the file on demand. A file that changed on
|
|
511
|
+
* disk since is reported as `E_ARCHIVE` by `read`.
|
|
512
|
+
*/
|
|
513
|
+
export async function openZipFile(file, origin, limits = DEFAULT_ARCHIVE_LIMITS) {
|
|
514
|
+
const fd = openSync(file, 'r');
|
|
515
|
+
try {
|
|
516
|
+
const length = fstatSync(fd).size;
|
|
517
|
+
const src = {
|
|
518
|
+
length,
|
|
519
|
+
read(at, len) {
|
|
520
|
+
const buf = Buffer.alloc(len);
|
|
521
|
+
let got = 0;
|
|
522
|
+
while (got < len) {
|
|
523
|
+
const n = readSync(fd, buf, got, len - got, at + got);
|
|
524
|
+
if (n === 0)
|
|
525
|
+
throw new SitefileError('E_ARCHIVE', `${origin}: the file changed while it was read`);
|
|
526
|
+
got += n;
|
|
527
|
+
}
|
|
528
|
+
return buf;
|
|
529
|
+
},
|
|
530
|
+
};
|
|
531
|
+
const { add, files } = collector(origin, limits, undefined, false);
|
|
532
|
+
const byPath = new Map();
|
|
533
|
+
for (const s of zipIndex(src, origin, limits, undefined)) {
|
|
534
|
+
if (s.e.isDir) {
|
|
535
|
+
add(s.e.name, 'dir', EMPTY, 0o755);
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
add(s.e.name, 'file', await zipData(src, s, origin, limits, undefined), s.e.mode);
|
|
539
|
+
byPath.set(archivePath(s.e.name), s);
|
|
540
|
+
}
|
|
541
|
+
const tree = Tree.fromFiles(files);
|
|
542
|
+
const bad = (why) => new SitefileError('E_ARCHIVE', `${origin}: ${why}`);
|
|
543
|
+
return {
|
|
544
|
+
tree,
|
|
545
|
+
read(path) {
|
|
546
|
+
const s = byPath.get(path);
|
|
547
|
+
if (!s)
|
|
548
|
+
return undefined;
|
|
549
|
+
const raw = src.read(s.start, s.e.csize);
|
|
550
|
+
let data = raw;
|
|
551
|
+
if (s.e.method === 8) {
|
|
552
|
+
try {
|
|
553
|
+
data = inflateRawSync(raw, { maxOutputLength: Math.max(s.e.usize, 1) });
|
|
554
|
+
}
|
|
555
|
+
catch {
|
|
556
|
+
throw bad(`zip entry '${s.e.name}' changed since the server started`);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (data.length !== s.e.usize || crc32(data) !== s.e.crc)
|
|
560
|
+
throw bad(`zip entry '${s.e.name}' changed since the server started`);
|
|
561
|
+
return data;
|
|
562
|
+
},
|
|
563
|
+
deflated(path) {
|
|
564
|
+
const s = byPath.get(path);
|
|
565
|
+
if (!s || s.e.method !== 8)
|
|
566
|
+
return undefined;
|
|
567
|
+
return { deflate: src.read(s.start, s.e.csize), crc: s.e.crc, size: s.e.usize };
|
|
568
|
+
},
|
|
569
|
+
close: () => closeSync(fd),
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
catch (e) {
|
|
573
|
+
closeSync(fd);
|
|
574
|
+
throw e;
|
|
304
575
|
}
|
|
305
576
|
}
|
|
306
577
|
//# sourceMappingURL=archive.js.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SitefileError } from '../errors.js';
|
|
2
2
|
import { sha256 } from '../digest.js';
|
|
3
3
|
import { extractArchive } from './archive.js';
|
|
4
|
+
import { looksLikeWarc, readWarc } from '../warc.js';
|
|
4
5
|
import { markdownDocument } from './documents.js';
|
|
5
6
|
import { bundleValue } from './bundle-data.js';
|
|
6
7
|
import { readWebBundle } from './wbn-reader.js';
|
|
@@ -21,7 +22,8 @@ export async function bytesToResource(bytes, kind, info, span, archiveLimits = D
|
|
|
21
22
|
const provenance = info.resolvedUrl === undefined ? { uri: info.uri, digest } : { uri: info.uri, resolvedUrl: info.resolvedUrl, digest };
|
|
22
23
|
if (kind === 'tree') {
|
|
23
24
|
// The provenance digest is that of the archive bytes, which is what pins and the lockfile cover.
|
|
24
|
-
|
|
25
|
+
const tree = looksLikeWarc(bytes) ? readWarc(bytes, info.uri, archiveLimits, span) : await extractArchive(bytes, info.uri, archiveLimits, span);
|
|
26
|
+
return { kind: 'tree', tree, provenance };
|
|
25
27
|
}
|
|
26
28
|
if (kind === 'markdown') {
|
|
27
29
|
return { kind: 'value', format: 'markdown', value: markdownDocument(bytes, '/' + info.basename, info.uri, span), provenance };
|
|
@@ -9,6 +9,9 @@ export interface FetchResult {
|
|
|
9
9
|
readonly finalUrl: string;
|
|
10
10
|
/** Media type essence from `content-type` (lowercase, parameters removed). */
|
|
11
11
|
readonly mediaType?: string;
|
|
12
|
+
/** `raw` requests only: the status and the response headers (lowercase names, values in order). */
|
|
13
|
+
readonly status?: number;
|
|
14
|
+
readonly headers?: ReadonlyArray<readonly [string, readonly string[]]>;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
14
17
|
* What to send. `headers` go to every hop; `originHeaders` (credentials: IMPORT … SECRET) only to hops
|
|
@@ -16,7 +19,12 @@ export interface FetchResult {
|
|
|
16
19
|
* A request with a body (POST) never follows redirects (E_REDIRECT).
|
|
17
20
|
*/
|
|
18
21
|
export interface FetchRequest {
|
|
19
|
-
readonly method?: 'GET' | 'POST';
|
|
22
|
+
readonly method?: 'GET' | 'POST' | 'HEAD';
|
|
23
|
+
/**
|
|
24
|
+
* One hop only, for `serve`'s proxy rules: redirects are not followed, every status and content
|
|
25
|
+
* encoding is returned as it is (with the headers), and the body is still size- and time-limited.
|
|
26
|
+
*/
|
|
27
|
+
readonly raw?: boolean;
|
|
20
28
|
readonly body?: Uint8Array;
|
|
21
29
|
readonly headers?: Readonly<Record<string, string>>;
|
|
22
30
|
readonly originHeaders?: Readonly<Record<string, string>>;
|
|
@@ -127,6 +127,42 @@ export async function fetchRemote(url, policy, span, request = {}) {
|
|
|
127
127
|
const headers = current.origin === origin ? { ...request.headers, ...request.originHeaders } : { ...request.headers };
|
|
128
128
|
const res = await openHop(current, policy, controller.signal, state, span, timeoutError, method, headers, request.body);
|
|
129
129
|
const status = res.statusCode ?? 0;
|
|
130
|
+
if (request.raw) {
|
|
131
|
+
const chunks = [];
|
|
132
|
+
let total = 0;
|
|
133
|
+
try {
|
|
134
|
+
for await (const chunk of res) {
|
|
135
|
+
total += chunk.length;
|
|
136
|
+
if (total > policy.maxBytes) {
|
|
137
|
+
res.destroy();
|
|
138
|
+
throw new SitefileError('E_TOO_LARGE', `${redactUrl(current)} exceeded the limit of ${policy.maxBytes} bytes`, span);
|
|
139
|
+
}
|
|
140
|
+
chunks.push(chunk);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
catch (e) {
|
|
144
|
+
if (isSitefileError(e))
|
|
145
|
+
throw e;
|
|
146
|
+
if (controller.signal.aborted)
|
|
147
|
+
throw timeoutError();
|
|
148
|
+
if (state.failure)
|
|
149
|
+
throw state.failure;
|
|
150
|
+
throw new SitefileError('E_FETCH', `error reading ${redactUrl(current)}: ${e.message}`, span, { cause: e });
|
|
151
|
+
}
|
|
152
|
+
if (controller.signal.aborted)
|
|
153
|
+
throw timeoutError();
|
|
154
|
+
const grouped = [];
|
|
155
|
+
for (let i = 0; i + 1 < res.rawHeaders.length; i += 2) {
|
|
156
|
+
const name = res.rawHeaders[i].toLowerCase();
|
|
157
|
+
const found = grouped.find(([n]) => n === name);
|
|
158
|
+
if (found)
|
|
159
|
+
found[1].push(res.rawHeaders[i + 1]);
|
|
160
|
+
else
|
|
161
|
+
grouped.push([name, [res.rawHeaders[i + 1]]]);
|
|
162
|
+
}
|
|
163
|
+
const buf = Buffer.concat(chunks, total);
|
|
164
|
+
return { bytes: new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength), finalUrl: current.href, status, headers: grouped };
|
|
165
|
+
}
|
|
130
166
|
if (REDIRECT_STATUSES.has(status) && method !== 'GET') {
|
|
131
167
|
res.destroy();
|
|
132
168
|
throw new SitefileError('E_REDIRECT', `${method} ${redactUrl(current)} was redirected (HTTP ${status}); redirects are only followed for GET`, span);
|
|
@@ -14,6 +14,14 @@ export interface NetworkSettings {
|
|
|
14
14
|
readonly allowHosts?: readonly string[] | undefined;
|
|
15
15
|
/** Hosts never contacted; a deny rule beats an allow rule. */
|
|
16
16
|
readonly denyHosts?: readonly string[] | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Strict network mode (`--network strict`, `SITEFILE_NETWORK=strict`): every host must match an
|
|
19
|
+
* allow-hosts rule, also when there is none (so nothing is contacted), and an `IMPORT … SECRET` needs
|
|
20
|
+
* a {@link secretHosts} rule for its host.
|
|
21
|
+
*/
|
|
22
|
+
readonly strict?: boolean | undefined;
|
|
23
|
+
/** `<secret id>=<host pattern>` (`--allow-secret-host`): where, in strict mode, that secret may be sent. */
|
|
24
|
+
readonly secretHosts?: readonly string[] | undefined;
|
|
17
25
|
/** Time limit for establishing the TCP connection (to the target, or to the proxy), ms. */
|
|
18
26
|
readonly connectTimeoutMs?: number | undefined;
|
|
19
27
|
/** Longest wait for data once connected (idle time), ms. */
|
|
@@ -49,6 +57,15 @@ export declare function urlPort(url: URL): number;
|
|
|
49
57
|
* Throws E_HOST_BLOCKED naming the rule; a deny rule beats an allow rule.
|
|
50
58
|
*/
|
|
51
59
|
export declare function checkHostRules(url: URL, network: NetworkSettings | undefined, shown: string, span?: SourceSpan): void;
|
|
60
|
+
/** True when `host:port` may be contacted under `network`'s host rules (see {@link checkHostRules}). */
|
|
61
|
+
export declare function hostAllowed(host: string, port: number, network: NetworkSettings | undefined): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Before an `IMPORT … SECRET` request to `target` (the import's own origin, the only one its secrets are
|
|
64
|
+
* sent to): in strict network mode each secret needs an `--allow-secret-host <id>=<pattern>` that matches
|
|
65
|
+
* the host. Throws E_HOST_BLOCKED with a `needs`. Nothing is checked outside strict mode, and a host the
|
|
66
|
+
* host rules refuse is left to the request itself (its E_HOST_BLOCKED asks for `--allow-hosts` first).
|
|
67
|
+
*/
|
|
68
|
+
export declare function checkSecretHosts(importName: string, secretIds: readonly string[], target: string, network: NetworkSettings | undefined, span?: SourceSpan): void;
|
|
52
69
|
/** The proxy to use for `url`, or undefined for a direct connection (no proxy, or a no-proxy match). */
|
|
53
70
|
export declare function proxyFor(url: URL, network: NetworkSettings | undefined): ProxyConfig | undefined;
|
|
54
71
|
/** A usage problem in `network` (bad proxy URL, host pattern or timeout), or undefined. */
|
|
@@ -58,13 +75,18 @@ export declare function parseFetchTimeout(raw: string): {
|
|
|
58
75
|
connectTimeoutMs?: number;
|
|
59
76
|
readTimeoutMs?: number;
|
|
60
77
|
};
|
|
78
|
+
/** Parse a network mode (`open` or `strict`; empty: unset); `what` names the source in the E_USAGE message. */
|
|
79
|
+
export declare function parseNetworkMode(raw: string, what: string): boolean | undefined;
|
|
61
80
|
/**
|
|
62
81
|
* Network settings from `SITEFILE_PROXY`, `SITEFILE_NO_PROXY`, `SITEFILE_ALLOW_HOSTS`,
|
|
63
|
-
* `SITEFILE_DENY_HOSTS` (comma- or space-separated)
|
|
64
|
-
* `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` are never read.
|
|
82
|
+
* `SITEFILE_DENY_HOSTS` (comma- or space-separated), `SITEFILE_FETCH_TIMEOUT` and `SITEFILE_NETWORK`
|
|
83
|
+
* (`open` or `strict`). Empty values are unset. `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` are never read.
|
|
65
84
|
*/
|
|
66
85
|
export declare function networkFromEnv(env?: NodeJS.ProcessEnv): NetworkSettings;
|
|
67
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* `over` on top of `base`: the proxy and timeouts are replaced, the host lists are joined (duplicates
|
|
88
|
+
* dropped), and strict mode is on when either is strict.
|
|
89
|
+
*/
|
|
68
90
|
export declare function mergeNetwork(base: NetworkSettings, over: NetworkSettings): NetworkSettings;
|
|
69
91
|
/** True when any setting is present. */
|
|
70
92
|
export declare function hasNetworkSettings(n: NetworkSettings | undefined): boolean;
|