@johnhenry/sitefile 0.0.7 → 0.0.8
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 +84 -1
- package/README.md +5 -0
- package/lib/src/attest.d.ts +12 -3
- package/lib/src/attest.js +15 -10
- package/lib/src/bake.js +2 -1
- package/lib/src/base-image.d.ts +2 -0
- package/lib/src/base-image.js +8 -0
- package/lib/src/cli/build-image.js +4 -2
- package/lib/src/cli/images.js +8 -5
- package/lib/src/cli/servers.js +59 -21
- package/lib/src/cli/watch.d.ts +2 -0
- package/lib/src/cli/watch.js +2 -1
- package/lib/src/cli.js +6 -3
- package/lib/src/dictionary.d.ts +1 -7
- package/lib/src/dictionary.js +1 -35
- package/lib/src/executor.d.ts +16 -5
- package/lib/src/executor.js +127 -58
- package/lib/src/index.d.ts +2 -0
- package/lib/src/index.js +2 -0
- package/lib/src/mime-type.d.ts +6 -0
- package/lib/src/mime-type.js +20 -0
- package/lib/src/nfc.d.ts +6 -0
- package/lib/src/nfc.js +10 -0
- package/lib/src/oci.d.ts +65 -1
- package/lib/src/oci.js +104 -10
- package/lib/src/paths.d.ts +2 -6
- package/lib/src/paths.js +2 -8
- package/lib/src/plugins/env.d.ts +20 -0
- package/lib/src/plugins/env.js +37 -0
- package/lib/src/plugins/exec.d.ts +1 -1
- package/lib/src/plugins/exec.js +3 -2
- package/lib/src/plugins/host.js +3 -2
- package/lib/src/precompress.d.ts +6 -0
- package/lib/src/precompress.js +6 -0
- package/lib/src/recipe.d.ts +37 -0
- package/lib/src/recipe.js +96 -0
- package/lib/src/registry-ops.d.ts +5 -1
- package/lib/src/registry-ops.js +69 -45
- package/lib/src/request-path.d.ts +10 -0
- package/lib/src/request-path.js +42 -0
- package/lib/src/serve-dictionary.d.ts +11 -0
- package/lib/src/serve-dictionary.js +45 -0
- package/lib/src/serve-rules.js +1 -1
- package/lib/src/server.d.ts +16 -229
- package/lib/src/server.js +135 -795
- package/lib/src/servers.d.ts +15 -0
- package/lib/src/servers.js +42 -2
- package/lib/src/site-core.d.ts +368 -0
- package/lib/src/site-core.js +1065 -0
- package/lib/src/site-handler.d.ts +14 -0
- package/lib/src/site-handler.js +32 -0
- package/lib/src/store.d.ts +31 -2
- package/lib/src/store.js +140 -21
- package/lib/src/wbn.d.ts +1 -6
- package/lib/src/wbn.js +2 -18
- package/package.json +5 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type SiteHandlerOptions, type SiteReply, type SiteRequest, type SiteTree } from './site-core.js';
|
|
2
|
+
export type { CachePreview, HtmlHandling, RoutingProfile, SiteContent, SiteFile, SiteHandlerOptions, SiteProxy, SiteProxyResponse, SiteReply, SiteRequest, SiteTree, } from './site-core.js';
|
|
3
|
+
/**
|
|
4
|
+
* The site handler as a `(Request) => Promise<Response>`, for Deno, Bun, edge runtimes, service workers and tests:
|
|
5
|
+
* `createSiteHandler(tree, { spa: true })(new Request('https://site.test/about'))`. The tree is a {@link SiteTree}
|
|
6
|
+
* (or a function returning one, read per request). Throws when `base` or `rewriteBase` cannot be normalized.
|
|
7
|
+
* `GET` and `HEAD` only (and `OPTIONS` with `cors`).
|
|
8
|
+
*/
|
|
9
|
+
export declare function createSiteHandler(site: SiteTree | (() => SiteTree), options?: SiteHandlerOptions): (request: Request) => Promise<Response>;
|
|
10
|
+
/**
|
|
11
|
+
* The same on request parts (`{ method, target, headers }`), where `target` is exactly as received (`/a/../b` is a
|
|
12
|
+
* `400`, not normalized away), for servers that have no `Request` to give.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createSiteResponder(site: SiteTree | (() => SiteTree), options?: SiteHandlerOptions): (request: SiteRequest) => Promise<SiteReply>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The site handler's public surface (`@johnhenry/sitefile/site-handler`): what `serve` answers for a request, with no
|
|
2
|
+
// listener and no `node:` import. Two functions and their types, nothing else. The implementation (`./site-core.ts`)
|
|
3
|
+
// is not a package entry point: the Node adapter (`./server.ts`) and the tests import it by path, and so reach the
|
|
4
|
+
// hooks and helpers an embedder does not need (`log`, `warn`, `injectReload`, the reply's `note`, the page constants,
|
|
5
|
+
// the byte-range and rewrite helpers). Documented in docs/CLI.md, "serve", under "The site handler".
|
|
6
|
+
import { createSiteHandler as coreHandler, createSiteResponder as coreResponder, } from './site-core.js';
|
|
7
|
+
/** The options an embedder passes, without anything only an adapter may set (a plain-JavaScript caller cannot smuggle those in). */
|
|
8
|
+
function publicOptions(options) {
|
|
9
|
+
const { injectReload: _reload, warn: _warn, log: _log, operator: _operator, ...rest } = options;
|
|
10
|
+
return rest;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The site handler as a `(Request) => Promise<Response>`, for Deno, Bun, edge runtimes, service workers and tests:
|
|
14
|
+
* `createSiteHandler(tree, { spa: true })(new Request('https://site.test/about'))`. The tree is a {@link SiteTree}
|
|
15
|
+
* (or a function returning one, read per request). Throws when `base` or `rewriteBase` cannot be normalized.
|
|
16
|
+
* `GET` and `HEAD` only (and `OPTIONS` with `cors`).
|
|
17
|
+
*/
|
|
18
|
+
export function createSiteHandler(site, options = {}) {
|
|
19
|
+
return coreHandler(site, publicOptions(options));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The same on request parts (`{ method, target, headers }`), where `target` is exactly as received (`/a/../b` is a
|
|
23
|
+
* `400`, not normalized away), for servers that have no `Request` to give.
|
|
24
|
+
*/
|
|
25
|
+
export function createSiteResponder(site, options = {}) {
|
|
26
|
+
const respond = coreResponder(site, publicOptions(options));
|
|
27
|
+
return async (request) => {
|
|
28
|
+
const { response, bytes } = await respond(request);
|
|
29
|
+
return { response, bytes };
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=site-handler.js.map
|
package/lib/src/store.d.ts
CHANGED
|
@@ -17,7 +17,10 @@ import type { Tree } from './tree.js';
|
|
|
17
17
|
* servers/ site server state and logs (see servers.ts)
|
|
18
18
|
* lock advisory lock for mutations
|
|
19
19
|
*
|
|
20
|
-
* IMAGE ID = the manifest digest
|
|
20
|
+
* IMAGE ID = the manifest digest, or the image index digest for a multi-platform image (`build --platform
|
|
21
|
+
* linux/amd64,linux/arm64`): its index entry has mediaType application/vnd.oci.image.index.v1+json, the blobs
|
|
22
|
+
* hold the index and one manifest per platform, and the site layers are shared. Everything that reads an image's
|
|
23
|
+
* files reads them from the first platform's manifest (they are the same in every one). All writes are temp file + rename, mutations hold the lock, and every
|
|
21
24
|
* blob is digest-verified when read (corruption is E_STORE).
|
|
22
25
|
*/
|
|
23
26
|
export interface ImageSummary {
|
|
@@ -29,6 +32,8 @@ export interface ImageSummary {
|
|
|
29
32
|
readonly files: number;
|
|
30
33
|
/** Total bytes of the site's files (uncompressed). */
|
|
31
34
|
readonly size: number;
|
|
35
|
+
/** `os/arch` of each platform: one for an image, several (sorted) for a multi-platform image. */
|
|
36
|
+
readonly platforms: readonly string[];
|
|
32
37
|
}
|
|
33
38
|
export interface ImageDetails extends ImageSummary {
|
|
34
39
|
readonly manifest: ParsedManifest;
|
|
@@ -86,7 +91,20 @@ export declare class Store {
|
|
|
86
91
|
id: string;
|
|
87
92
|
created: boolean;
|
|
88
93
|
}>;
|
|
89
|
-
/**
|
|
94
|
+
/**
|
|
95
|
+
* Store a multi-platform image (`options.platform` lists two or more): the site layers once, a config and a
|
|
96
|
+
* manifest per platform, and the image index, whose digest is the image ID.
|
|
97
|
+
*/
|
|
98
|
+
private addImageIndex;
|
|
99
|
+
/**
|
|
100
|
+
* The platform manifests of an image index in the store: `{digest, size, platform}`, in index order.
|
|
101
|
+
* Undefined when `id` is an ordinary image manifest.
|
|
102
|
+
*/
|
|
103
|
+
private indexChildren;
|
|
104
|
+
/**
|
|
105
|
+
* Manifest and config of an image, digest-verified (not its layer). For a multi-platform image these are the
|
|
106
|
+
* first platform's (its site layers are every platform's), `id` stays the index digest, and `platforms` lists all.
|
|
107
|
+
*/
|
|
90
108
|
details(id: string): Promise<ImageDetails>;
|
|
91
109
|
/**
|
|
92
110
|
* Load an image's files (under its root, or under `o.root`), verifying manifest, config, layer digest
|
|
@@ -128,6 +146,11 @@ export declare class Store {
|
|
|
128
146
|
untagged: string[];
|
|
129
147
|
deleted: string | undefined;
|
|
130
148
|
}>;
|
|
149
|
+
/**
|
|
150
|
+
* Blob hexes an image uses: its manifest, config and layers, and for a multi-platform image the index and
|
|
151
|
+
* every platform's. An unreadable manifest throws, so nothing is evicted on a guess.
|
|
152
|
+
*/
|
|
153
|
+
private imageBlobHexes;
|
|
131
154
|
/**
|
|
132
155
|
* What the size limit may evict: `live` holds the blob hexes of tagged images (never evicted),
|
|
133
156
|
* `untagged` each untagged image with the blobs it uses. An unreadable tagged manifest throws
|
|
@@ -149,3 +172,9 @@ export declare class Store {
|
|
|
149
172
|
bytes: number;
|
|
150
173
|
}>;
|
|
151
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Whether creating the lock file failed because another writer is deleting it: on Windows a file
|
|
177
|
+
* pending deletion can't be created again (EPERM or EACCES). Never true elsewhere, where a
|
|
178
|
+
* permission error is a real fault (#337).
|
|
179
|
+
*/
|
|
180
|
+
export declare function lockDeletePending(e: unknown, platform?: NodeJS.Platform): boolean;
|
package/lib/src/store.js
CHANGED
|
@@ -5,7 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import { decodeReferrersIndex, encodeReferrersIndex } from './attest.js';
|
|
6
6
|
import { sha256 } from './digest.js';
|
|
7
7
|
import { SitefileError } from './errors.js';
|
|
8
|
-
import { ANN, blobPathOf, buildOciImage, decodeIndex, encodeIndex, extractImageFiles, layoutTree, OCI_LAYOUT_BYTES, parseConfig, parseManifest, siteLayerIndex, siteLayerIndices, } from './oci.js';
|
|
8
|
+
import { ANN, blobPathOf, buildOciImage, decodeIndex, encodeIndex, extractImageFiles, buildOciIndex, layoutFromBlobs, layoutTree, MEDIA, OCI_LAYOUT_BYTES, parseConfig, parseIndex, parseManifest, parsePlatforms, siteLayerIndex, siteLayerIndices, } from './oci.js';
|
|
9
9
|
import { looksLikeImageId, parseLocalRef, refKey } from './ref.js';
|
|
10
10
|
const HEX64 = /^[0-9a-f]{64}$/;
|
|
11
11
|
export function resolveStoreHome(env = process.env) {
|
|
@@ -15,6 +15,26 @@ export function resolveStoreHome(env = process.env) {
|
|
|
15
15
|
function storeError(msg, cause) {
|
|
16
16
|
return new SitefileError('E_STORE', msg, undefined, cause === undefined ? undefined : { cause });
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* An older sitefile does not know image indexes: its `tag`, `rmi` and `build -t` rewrite every entry of
|
|
20
|
+
* `index.json` as a plain manifest, so a multi-platform image they touched is listed as a single image whose
|
|
21
|
+
* blob is an index. Said plainly, with the way out, instead of as a generic corruption.
|
|
22
|
+
*/
|
|
23
|
+
function refuseIndexAsImage(id, bytes) {
|
|
24
|
+
let j;
|
|
25
|
+
try {
|
|
26
|
+
j = JSON.parse(Buffer.from(bytes).toString('utf8'));
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const o = j;
|
|
32
|
+
if (o === null || typeof o !== 'object' || !Array.isArray(o.manifests) || (o.mediaType !== undefined && o.mediaType !== MEDIA.index))
|
|
33
|
+
return;
|
|
34
|
+
throw storeError(`image ${shortId(id)} is a multi-platform image (an image index) but index.json lists it as a single-platform image: ` +
|
|
35
|
+
`an older sitefile that ran tag, rmi or build -t on this store rewrote the entry. Rebuild it (sitefile build -t <name> --platform ...) or pull it again; ` +
|
|
36
|
+
`'sitefile rmi' and 'image prune' remove the entry and its blobs`);
|
|
37
|
+
}
|
|
18
38
|
function errCode(e) {
|
|
19
39
|
return e?.code;
|
|
20
40
|
}
|
|
@@ -56,6 +76,12 @@ export class Store {
|
|
|
56
76
|
break;
|
|
57
77
|
}
|
|
58
78
|
catch (e) {
|
|
79
|
+
// Windows: creating a file that another writer is deleting fails with EPERM/EACCES until
|
|
80
|
+
// the delete completes (#337). That is a busy lock: wait, and report the error only at the deadline.
|
|
81
|
+
if (lockDeletePending(e) && Date.now() <= deadline) {
|
|
82
|
+
await new Promise((r) => setTimeout(r, 20 + Math.random() * 30));
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
59
85
|
if (errCode(e) !== 'EEXIST')
|
|
60
86
|
throw storeError(`cannot lock the store at '${this.home}': ${e.message}`, e);
|
|
61
87
|
if (await this.lockIsStale(lock)) {
|
|
@@ -258,6 +284,8 @@ export class Store {
|
|
|
258
284
|
// ------------------------------------------------------------ images
|
|
259
285
|
/** Store a tree as an OCI image (idempotent: identical content and options give the same ID). Untagged. */
|
|
260
286
|
async addImage(tree, options = {}, now = new Date()) {
|
|
287
|
+
if (parsePlatforms(options.platform).length > 1)
|
|
288
|
+
return this.addImageIndex(tree, options, now);
|
|
261
289
|
const img = buildOciImage(tree, options);
|
|
262
290
|
return this.withLock(async () => {
|
|
263
291
|
await this.putBlob(img.layerDigest, img.layerBytes);
|
|
@@ -273,13 +301,61 @@ export class Store {
|
|
|
273
301
|
return { id: img.manifestDigest, created: true };
|
|
274
302
|
});
|
|
275
303
|
}
|
|
276
|
-
/**
|
|
304
|
+
/**
|
|
305
|
+
* Store a multi-platform image (`options.platform` lists two or more): the site layers once, a config and a
|
|
306
|
+
* manifest per platform, and the image index, whose digest is the image ID.
|
|
307
|
+
*/
|
|
308
|
+
async addImageIndex(tree, options, now) {
|
|
309
|
+
const idx = buildOciIndex(tree, options);
|
|
310
|
+
return this.withLock(async () => {
|
|
311
|
+
for (const { image: img } of idx.images) {
|
|
312
|
+
await this.putBlob(img.layerDigest, img.layerBytes);
|
|
313
|
+
for (const [d, b] of img.extraLayers ?? [])
|
|
314
|
+
await this.putBlob(d, b);
|
|
315
|
+
await this.putBlob(img.configDigest, img.configBytes);
|
|
316
|
+
await this.putBlob(img.manifestDigest, img.manifestBytes);
|
|
317
|
+
}
|
|
318
|
+
await this.putBlob(idx.indexDigest, idx.indexBytes);
|
|
319
|
+
const entries = await this.readIndex();
|
|
320
|
+
if (entries.some((e) => e.digest === idx.indexDigest))
|
|
321
|
+
return { id: idx.indexDigest, created: false };
|
|
322
|
+
await fs.rm(this.referrersFile(idx.indexDigest), { force: true });
|
|
323
|
+
await this.writeIndex([...entries, { mediaType: MEDIA.index, digest: idx.indexDigest, size: idx.indexBytes.byteLength, stored: now.toISOString() }]);
|
|
324
|
+
return { id: idx.indexDigest, created: true };
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The platform manifests of an image index in the store: `{digest, size, platform}`, in index order.
|
|
329
|
+
* Undefined when `id` is an ordinary image manifest.
|
|
330
|
+
*/
|
|
331
|
+
async indexChildren(entry) {
|
|
332
|
+
if (entry.mediaType !== MEDIA.index)
|
|
333
|
+
return undefined;
|
|
334
|
+
const bytes = await this.readBlob(entry.digest, entry.size);
|
|
335
|
+
try {
|
|
336
|
+
const idx = parseIndex(bytes);
|
|
337
|
+
if (idx.manifests.length === 0)
|
|
338
|
+
throw new Error('it lists no platform');
|
|
339
|
+
return idx;
|
|
340
|
+
}
|
|
341
|
+
catch (e) {
|
|
342
|
+
throw storeError(`image ${shortId(entry.digest)} is corrupt: ${e.message}`, e);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Manifest and config of an image, digest-verified (not its layer). For a multi-platform image these are the
|
|
347
|
+
* first platform's (its site layers are every platform's), `id` stays the index digest, and `platforms` lists all.
|
|
348
|
+
*/
|
|
277
349
|
async details(id) {
|
|
278
350
|
const entries = (await this.readIndex()).filter((e) => e.digest === id);
|
|
279
351
|
const first = entries[0];
|
|
280
352
|
if (!first)
|
|
281
353
|
throw new SitefileError('E_NOT_FOUND', `no such image: ${shortId(id)}`);
|
|
282
|
-
const
|
|
354
|
+
const children = await this.indexChildren(first);
|
|
355
|
+
const child = children?.manifests[0];
|
|
356
|
+
const manifestBytes = child ? await this.readBlob(child.digest, child.size) : await this.readBlob(id, first.size);
|
|
357
|
+
if (!child)
|
|
358
|
+
refuseIndexAsImage(id, manifestBytes);
|
|
283
359
|
let manifest;
|
|
284
360
|
let config;
|
|
285
361
|
try {
|
|
@@ -291,13 +367,15 @@ export class Store {
|
|
|
291
367
|
throw e;
|
|
292
368
|
throw storeError(`image ${shortId(id)} is corrupt: ${e.message}`, e);
|
|
293
369
|
}
|
|
370
|
+
const notes = children?.annotations ?? manifest.annotations;
|
|
294
371
|
const stored = entries.map((e) => e.stored).filter((s) => !!s).sort()[0] ?? config.created;
|
|
295
372
|
return {
|
|
296
373
|
id,
|
|
297
374
|
tags: entries.flatMap((e) => (e.refName ? [e.refName] : [])),
|
|
298
375
|
created: stored,
|
|
299
|
-
files: Number(
|
|
300
|
-
size: Number(
|
|
376
|
+
files: Number(notes[ANN.files] ?? 0),
|
|
377
|
+
size: Number(notes[ANN.size] ?? 0),
|
|
378
|
+
platforms: children ? children.manifests.map((m) => m.platform) : [`${config.os}/${config.architecture}`],
|
|
301
379
|
manifest,
|
|
302
380
|
config,
|
|
303
381
|
root: config.labels[ANN.root] ?? '/',
|
|
@@ -324,6 +402,22 @@ export class Store {
|
|
|
324
402
|
* Includes every layer, so an image built on a BASE is self-contained.
|
|
325
403
|
*/
|
|
326
404
|
async layoutOf(id, refName) {
|
|
405
|
+
const first = (await this.readIndex()).find((e) => e.digest === id);
|
|
406
|
+
const children = first ? await this.indexChildren(first) : undefined;
|
|
407
|
+
if (first && children) {
|
|
408
|
+
// A multi-platform image: the index, then every platform's manifest, config and layers.
|
|
409
|
+
const blobs = new Map([[id, await this.readBlob(id, first.size)]]);
|
|
410
|
+
for (const c of children.manifests) {
|
|
411
|
+
const bytes = await this.readBlob(c.digest, c.size);
|
|
412
|
+
blobs.set(c.digest, bytes);
|
|
413
|
+
const m = parseManifest(bytes);
|
|
414
|
+
blobs.set(m.config.digest, await this.readBlob(m.config.digest, m.config.size));
|
|
415
|
+
for (const l of m.layers)
|
|
416
|
+
if (!blobs.has(l.digest))
|
|
417
|
+
blobs.set(l.digest, await this.readBlob(l.digest, l.size));
|
|
418
|
+
}
|
|
419
|
+
return layoutFromBlobs(id, first.size, MEDIA.index, blobs, refName, await this.referrers(id));
|
|
420
|
+
}
|
|
327
421
|
const d = await this.details(id);
|
|
328
422
|
const site = d.manifest.layers[siteLayerIndex(d.manifest, d.config)];
|
|
329
423
|
if (!site)
|
|
@@ -424,8 +518,8 @@ export class Store {
|
|
|
424
518
|
async list() {
|
|
425
519
|
const out = [];
|
|
426
520
|
for (const id of [...new Set((await this.readIndex()).map((e) => e.digest))]) {
|
|
427
|
-
const { id: _i, tags, created, files, size } = await this.details(id);
|
|
428
|
-
out.push({ id, tags, created, files, size });
|
|
521
|
+
const { id: _i, tags, created, files, size, platforms } = await this.details(id);
|
|
522
|
+
out.push({ id, tags, created, files, size, platforms });
|
|
429
523
|
}
|
|
430
524
|
return out.sort((a, b) => (a.created < b.created ? 1 : a.created > b.created ? -1 : a.id < b.id ? -1 : 1));
|
|
431
525
|
}
|
|
@@ -470,7 +564,7 @@ export class Store {
|
|
|
470
564
|
if (previous && !next.some((e) => e.digest === previous.digest))
|
|
471
565
|
next.push({ ...previous, refName: undefined });
|
|
472
566
|
const stored = entries.find((e) => e.digest === id && e.stored)?.stored;
|
|
473
|
-
next.push({ digest: id, size: mine.size, refName: key, stored });
|
|
567
|
+
next.push({ ...(mine.mediaType ? { mediaType: mine.mediaType } : {}), digest: id, size: mine.size, refName: key, stored });
|
|
474
568
|
await this.writeIndex(next);
|
|
475
569
|
});
|
|
476
570
|
return key;
|
|
@@ -516,24 +610,42 @@ export class Store {
|
|
|
516
610
|
});
|
|
517
611
|
}
|
|
518
612
|
/**
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
* (its blobs are unknown, so nothing may be evicted).
|
|
613
|
+
* Blob hexes an image uses: its manifest, config and layers, and for a multi-platform image the index and
|
|
614
|
+
* every platform's. An unreadable manifest throws, so nothing is evicted on a guess.
|
|
522
615
|
*/
|
|
523
|
-
async
|
|
524
|
-
const
|
|
525
|
-
const
|
|
616
|
+
async imageBlobHexes(id, entries) {
|
|
617
|
+
const entry = entries.find((e) => e.digest === id);
|
|
618
|
+
const manifestsOf = async (digest, size) => {
|
|
526
619
|
let m;
|
|
527
620
|
try {
|
|
528
|
-
|
|
621
|
+
const bytes = await this.readBlob(digest, size);
|
|
622
|
+
if (digest === id)
|
|
623
|
+
refuseIndexAsImage(id, bytes);
|
|
624
|
+
m = parseManifest(bytes);
|
|
529
625
|
}
|
|
530
626
|
catch (e) {
|
|
531
627
|
if (e instanceof SitefileError)
|
|
532
628
|
throw e;
|
|
533
629
|
throw storeError(`image ${shortId(id)} is corrupt: ${e.message}`, e);
|
|
534
630
|
}
|
|
535
|
-
return [
|
|
631
|
+
return [digest, m.config.digest, ...m.layers.map((l) => l.digest)].map((d) => d.slice(7));
|
|
536
632
|
};
|
|
633
|
+
const children = entry ? await this.indexChildren(entry) : undefined;
|
|
634
|
+
if (!children)
|
|
635
|
+
return manifestsOf(id);
|
|
636
|
+
const out = [id.slice(7)];
|
|
637
|
+
for (const c of children.manifests)
|
|
638
|
+
out.push(...(await manifestsOf(c.digest, c.size)));
|
|
639
|
+
return out;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* What the size limit may evict: `live` holds the blob hexes of tagged images (never evicted),
|
|
643
|
+
* `untagged` each untagged image with the blobs it uses. An unreadable tagged manifest throws
|
|
644
|
+
* (its blobs are unknown, so nothing may be evicted).
|
|
645
|
+
*/
|
|
646
|
+
async gcView() {
|
|
647
|
+
const entries = await this.readIndex();
|
|
648
|
+
const blobsOf = async (id) => [...(await this.imageBlobHexes(id, entries)), ...(await this.referrerBlobHexes(id))];
|
|
537
649
|
const live = new Set();
|
|
538
650
|
for (const id of new Set(entries.filter((e) => e.refName).map((e) => e.digest)))
|
|
539
651
|
for (const h of await blobsOf(id))
|
|
@@ -565,11 +677,9 @@ export class Store {
|
|
|
565
677
|
await this.writeIndex(keep);
|
|
566
678
|
const live = new Set();
|
|
567
679
|
for (const id of new Set(keep.map((e) => e.digest))) {
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
for (const l of d.manifest.layers)
|
|
572
|
-
live.add(l.digest.slice(7));
|
|
680
|
+
await this.details(id); // a corrupt image stops the prune before anything is deleted
|
|
681
|
+
for (const h of await this.imageBlobHexes(id, entries))
|
|
682
|
+
live.add(h);
|
|
573
683
|
for (const h of await this.referrerBlobHexes(id))
|
|
574
684
|
live.add(h);
|
|
575
685
|
}
|
|
@@ -597,4 +707,13 @@ export class Store {
|
|
|
597
707
|
});
|
|
598
708
|
}
|
|
599
709
|
}
|
|
710
|
+
/**
|
|
711
|
+
* Whether creating the lock file failed because another writer is deleting it: on Windows a file
|
|
712
|
+
* pending deletion can't be created again (EPERM or EACCES). Never true elsewhere, where a
|
|
713
|
+
* permission error is a real fault (#337).
|
|
714
|
+
*/
|
|
715
|
+
export function lockDeletePending(e, platform = process.platform) {
|
|
716
|
+
const code = errCode(e);
|
|
717
|
+
return platform === 'win32' && (code === 'EPERM' || code === 'EACCES');
|
|
718
|
+
}
|
|
600
719
|
//# sourceMappingURL=store.js.map
|
package/lib/src/wbn.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import type { Tree } from './tree.js';
|
|
2
|
-
|
|
3
|
-
* The content type `serve` and Web Bundle export give a tree path: a well-known extensionless path
|
|
4
|
-
* (`/.well-known/apple-app-site-association`, ...) by its exact name, else by the extension, ASCII
|
|
5
|
-
* case-insensitively (`application/octet-stream` when unknown).
|
|
6
|
-
*/
|
|
7
|
-
export declare function mimeTypeFor(path: string): string;
|
|
2
|
+
export { mimeTypeFor } from './mime-type.js';
|
|
8
3
|
/** An absolute https URL ending in `/`, without credentials, query or fragment. Returns its normalized form. */
|
|
9
4
|
export declare function validateBaseUrl(raw: string | undefined): string;
|
|
10
5
|
export declare function writeWebBundle(tree: Tree, baseUrl: string | undefined): Uint8Array;
|
package/lib/src/wbn.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { SitefileError } from './errors.js';
|
|
2
2
|
import { cborArray, cborBytes, cborMap, cborText, cborUint } from './cbor.js';
|
|
3
3
|
import { headersFor, isPlainRule, matchRedirect, parseHeaderRules, parseRedirectRules } from './serve-rules.js';
|
|
4
|
-
import {
|
|
4
|
+
import { mimeTypeFor } from './mime-type.js';
|
|
5
5
|
/**
|
|
6
6
|
* Unsigned Web Bundle, format version "b2"
|
|
7
7
|
* (draft-ietf-wpack-bundled-responses). Layout, as one CBOR array:
|
|
@@ -28,23 +28,7 @@ import { MIME_TYPES, WELL_KNOWN_TYPES } from './mime-data.js';
|
|
|
28
28
|
*/
|
|
29
29
|
const MAGIC = Buffer.from('f09f8c90f09f93a6', 'hex');
|
|
30
30
|
const VERSION = Buffer.from([0x62, 0x32, 0x00, 0x00]);
|
|
31
|
-
|
|
32
|
-
const MIME = new Map(MIME_TYPES);
|
|
33
|
-
const WELL_KNOWN = new Map(WELL_KNOWN_TYPES);
|
|
34
|
-
/**
|
|
35
|
-
* The content type `serve` and Web Bundle export give a tree path: a well-known extensionless path
|
|
36
|
-
* (`/.well-known/apple-app-site-association`, ...) by its exact name, else by the extension, ASCII
|
|
37
|
-
* case-insensitively (`application/octet-stream` when unknown).
|
|
38
|
-
*/
|
|
39
|
-
export function mimeTypeFor(path) {
|
|
40
|
-
const known = WELL_KNOWN.get(path);
|
|
41
|
-
if (known !== undefined)
|
|
42
|
-
return known;
|
|
43
|
-
const base = path.slice(path.lastIndexOf('/') + 1);
|
|
44
|
-
const dot = base.lastIndexOf('.');
|
|
45
|
-
const ext = dot < 0 ? '' : base.slice(dot + 1).replace(/[A-Z]/g, (c) => c.toLowerCase());
|
|
46
|
-
return MIME.get(ext) ?? 'application/octet-stream';
|
|
47
|
-
}
|
|
31
|
+
export { mimeTypeFor } from './mime-type.js';
|
|
48
32
|
/** An absolute https URL ending in `/`, without credentials, query or fragment. Returns its normalized form. */
|
|
49
33
|
export function validateBaseUrl(raw) {
|
|
50
34
|
if (raw === undefined || raw === '') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/sitefile",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Docker-inspired static artifact builder: compose immutable file trees from local and remote sources, render templates, and export a deployable site. No container runtime, no shell, no runtime dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"static-site",
|
|
@@ -30,6 +30,10 @@
|
|
|
30
30
|
".": {
|
|
31
31
|
"types": "./lib/src/index.d.ts",
|
|
32
32
|
"default": "./lib/src/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./site-handler": {
|
|
35
|
+
"types": "./lib/src/site-handler.d.ts",
|
|
36
|
+
"default": "./lib/src/site-handler.js"
|
|
33
37
|
}
|
|
34
38
|
},
|
|
35
39
|
"types": "./lib/src/index.d.ts",
|