@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
package/lib/src/oci.js
CHANGED
|
@@ -79,6 +79,20 @@ export function parsePlatform(p) {
|
|
|
79
79
|
throw new SitefileError('E_USAGE', `platform must be os/arch (e.g. linux/amd64), got '${p}'`);
|
|
80
80
|
return { os: m[1], arch: m[2] };
|
|
81
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* `--platform` as a list: `os/arch[,os/arch...]`, each element checked like {@link parsePlatform}, duplicates
|
|
84
|
+
* dropped, sorted (so the order on the command line never changes the image). Nothing given: an empty list.
|
|
85
|
+
* One platform is the image it has always been; two or more make an image index ({@link buildOciIndex}).
|
|
86
|
+
*/
|
|
87
|
+
export function parsePlatforms(p) {
|
|
88
|
+
if (p === undefined)
|
|
89
|
+
return [];
|
|
90
|
+
const list = p.split(',').map((x) => {
|
|
91
|
+
const { os, arch } = parsePlatform(x);
|
|
92
|
+
return `${os}/${arch}`;
|
|
93
|
+
});
|
|
94
|
+
return [...new Set(list)].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
95
|
+
}
|
|
82
96
|
/** Validate an in-image root path (absolute, no `..`); returns the normalized form. */
|
|
83
97
|
export function normalizeOciRoot(raw) {
|
|
84
98
|
return normalizeVirtual(raw ?? DEFAULT_OCI_ROOT, undefined, 'OCI root').path;
|
|
@@ -86,10 +100,8 @@ export function normalizeOciRoot(raw) {
|
|
|
86
100
|
function isoOf(epoch) {
|
|
87
101
|
return epoch === undefined ? EPOCH_ISO : new Date(epoch * 1000).toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
88
102
|
}
|
|
89
|
-
|
|
90
|
-
export function buildOciImage(tree, options = {}) {
|
|
103
|
+
function buildSiteLayers(tree, options) {
|
|
91
104
|
const root = normalizeOciRoot(options.root);
|
|
92
|
-
const { os, arch } = parsePlatform(options.platform);
|
|
93
105
|
const segs = root === '/' ? [] : root.slice(1).split('/');
|
|
94
106
|
const dirs = segs.map((_, i) => segs.slice(0, i + 1).join('/'));
|
|
95
107
|
const uncompressed = options.compression === 'none';
|
|
@@ -100,12 +112,24 @@ export function buildOciImage(tree, options = {}) {
|
|
|
100
112
|
const parts = Array.from({ length: n }, () => []);
|
|
101
113
|
for (const [p, f] of tree.entries())
|
|
102
114
|
parts[layerOf(p, n)].push([p, f]);
|
|
103
|
-
const
|
|
115
|
+
const layers = parts.map((files) => {
|
|
104
116
|
const tar = writeTar(Tree.fromFiles(Tree.fromFiles(files).mountedAt(root)), { epoch: options.epoch ?? 0, dirs });
|
|
105
117
|
const bytes = uncompressed ? tar : gzipTar(tar);
|
|
106
118
|
const diffId = sha256(tar);
|
|
107
119
|
return { bytes, diffId, digest: uncompressed ? diffId : sha256(bytes) };
|
|
108
120
|
});
|
|
121
|
+
return { root, uncompressed, n, layers };
|
|
122
|
+
}
|
|
123
|
+
/** Build the single-layer image for `tree` (the site's files, rooted at `/`). */
|
|
124
|
+
export function buildOciImage(tree, options = {}, prebuilt) {
|
|
125
|
+
if (!prebuilt)
|
|
126
|
+
normalizeOciRoot(options.root); // errors come in the order they always did: root, platform, layers
|
|
127
|
+
// A list that names one platform (twice, say) is that platform; two or more are an index (buildOciIndex).
|
|
128
|
+
const platforms = parsePlatforms(options.platform);
|
|
129
|
+
if (platforms.length > 1)
|
|
130
|
+
throw new SitefileError('E_USAGE', 'internal: buildOciImage builds one platform; use buildOciIndex');
|
|
131
|
+
const { os, arch } = parsePlatform(platforms[0]);
|
|
132
|
+
const { root, uncompressed, n, layers: site } = prebuilt ?? buildSiteLayers(tree, options);
|
|
109
133
|
const { bytes: layerBytes, diffId, digest: layerDigest } = site[0];
|
|
110
134
|
const size = tree.entries().reduce((n, [, f]) => n + f.bytes.byteLength, 0);
|
|
111
135
|
const base = options.base;
|
|
@@ -168,6 +192,52 @@ export function buildOciImage(tree, options = {}) {
|
|
|
168
192
|
const extraLayers = n > 1 ? new Map(site.slice(1).map((l) => [l.digest, l.bytes])) : undefined;
|
|
169
193
|
return { manifestBytes, manifestDigest: sha256(manifestBytes), configBytes, configDigest, layerBytes, layerDigest, diffId, root, baseLayers, ...(extraLayers ? { extraLayers } : {}) };
|
|
170
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Build the image for every platform in `options.platform` (`os/arch,os/arch...`, two or more): the site layers
|
|
197
|
+
* are built once and shared by every manifest, each manifest builds on that platform's base
|
|
198
|
+
* (`options.bases`, else `options.base`), and one image index lists them sorted by `os/arch`. The index is
|
|
199
|
+
* canonical JSON like every other hashed document, so the bytes are the same in both implementations.
|
|
200
|
+
*/
|
|
201
|
+
export function buildOciIndex(tree, options = {}) {
|
|
202
|
+
const platforms = parsePlatforms(options.platform);
|
|
203
|
+
if (platforms.length < 2)
|
|
204
|
+
throw new SitefileError('E_USAGE', 'internal: buildOciIndex needs two or more platforms');
|
|
205
|
+
normalizeOciRoot(options.root);
|
|
206
|
+
const site = buildSiteLayers(tree, options);
|
|
207
|
+
const images = platforms.map((platform) => ({ platform, image: buildOciImage(tree, { ...options, platform, base: options.bases?.get(platform) ?? options.base }, site) }));
|
|
208
|
+
const size = tree.entries().reduce((n, [, f]) => n + f.bytes.byteLength, 0);
|
|
209
|
+
const annotations = { [ANN.files]: String(tree.size), [ANN.size]: String(size) };
|
|
210
|
+
if (options.title)
|
|
211
|
+
annotations[ANN.title] = options.title;
|
|
212
|
+
const indexBytes = enc.encode(canonicalJson({
|
|
213
|
+
schemaVersion: 2,
|
|
214
|
+
mediaType: MEDIA.index,
|
|
215
|
+
manifests: images.map(({ platform, image }) => {
|
|
216
|
+
const { os, arch } = parsePlatform(platform);
|
|
217
|
+
return { mediaType: MEDIA.manifest, digest: image.manifestDigest, size: image.manifestBytes.byteLength, platform: { architecture: arch, os } };
|
|
218
|
+
}),
|
|
219
|
+
annotations,
|
|
220
|
+
}));
|
|
221
|
+
return { indexBytes, indexDigest: sha256(indexBytes), images };
|
|
222
|
+
}
|
|
223
|
+
/** Read an image index (OCI) or manifest list (Docker): each child's digest, size and `os/arch[/variant]`. */
|
|
224
|
+
export function parseIndex(bytes) {
|
|
225
|
+
const j = JSON.parse(Buffer.from(bytes).toString('utf8'));
|
|
226
|
+
if (j.schemaVersion !== 2 || (j.mediaType !== undefined && j.mediaType !== MEDIA.index && j.mediaType !== MEDIA.dockerList) || !Array.isArray(j.manifests))
|
|
227
|
+
throw new Error('not an image index');
|
|
228
|
+
const manifests = j.manifests.map((m) => {
|
|
229
|
+
if (typeof m?.digest !== 'string' || !SHA.test(m.digest) || typeof m.size !== 'number' || !Number.isSafeInteger(m.size) || m.size < 0)
|
|
230
|
+
throw new Error('bad index entry');
|
|
231
|
+
const p = (m.platform ?? {});
|
|
232
|
+
const platform = `${String(p.os ?? '')}/${String(p.architecture ?? '')}${typeof p.variant === 'string' && p.variant !== '' ? `/${p.variant}` : ''}`;
|
|
233
|
+
return { digest: m.digest, size: m.size, platform };
|
|
234
|
+
});
|
|
235
|
+
const annotations = {};
|
|
236
|
+
for (const [k, v] of Object.entries((j.annotations ?? {})))
|
|
237
|
+
if (typeof v === 'string')
|
|
238
|
+
annotations[k] = v;
|
|
239
|
+
return { manifests, annotations };
|
|
240
|
+
}
|
|
171
241
|
function isRecord(v) {
|
|
172
242
|
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
173
243
|
}
|
|
@@ -198,7 +268,7 @@ export function encodeIndex(entries) {
|
|
|
198
268
|
if (e.stored)
|
|
199
269
|
annotations[ANN.stored] = e.stored;
|
|
200
270
|
Object.assign(annotations, e.extra);
|
|
201
|
-
return { mediaType: MEDIA.manifest, digest: e.digest, size: e.size, artifactType: e.artifactType, ...(Object.keys(annotations).length ? { annotations } : {}) };
|
|
271
|
+
return { mediaType: e.mediaType ?? MEDIA.manifest, digest: e.digest, size: e.size, artifactType: e.artifactType, ...(Object.keys(annotations).length ? { annotations } : {}) };
|
|
202
272
|
});
|
|
203
273
|
return enc.encode(canonicalJson({ schemaVersion: 2, mediaType: MEDIA.index, manifests }));
|
|
204
274
|
}
|
|
@@ -214,7 +284,7 @@ export function decodeIndex(bytes) {
|
|
|
214
284
|
const a = (m.annotations ?? {});
|
|
215
285
|
const ref = a[ANN.refName];
|
|
216
286
|
const stored = a[ANN.stored];
|
|
217
|
-
return { digest: m.digest, size: m.size, refName: typeof ref === 'string' ? ref : undefined, stored: typeof stored === 'string' ? stored : undefined };
|
|
287
|
+
return { ...(m.mediaType === MEDIA.index ? { mediaType: MEDIA.index } : {}), digest: m.digest, size: m.size, refName: typeof ref === 'string' ? ref : undefined, stored: typeof stored === 'string' ? stored : undefined };
|
|
218
288
|
});
|
|
219
289
|
}
|
|
220
290
|
export function blobPathOf(digest) {
|
|
@@ -230,18 +300,38 @@ export function blobPathOf(digest) {
|
|
|
230
300
|
export function layoutTree(img, refName,
|
|
231
301
|
/** Blobs of the layers below the site layer (digest -> bytes), for images built on a BASE. */
|
|
232
302
|
baseBlobs = new Map(), referrers = []) {
|
|
303
|
+
const blobs = new Map([[img.manifestDigest, img.manifestBytes], [img.configDigest, img.configBytes], [img.layerDigest, img.layerBytes], ...(img.extraLayers ?? []), ...baseBlobs]);
|
|
304
|
+
return layoutFromBlobs(img.manifestDigest, img.manifestBytes.byteLength, undefined, blobs, refName, referrers);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* The layout of a multi-platform image: index.json names the image index, and the blobs hold the index,
|
|
308
|
+
* every platform's manifest and config, and every layer (the site layers once). `baseBlobs` are the layers
|
|
309
|
+
* below the site layers, of every platform.
|
|
310
|
+
*/
|
|
311
|
+
export function layoutTreeIndex(img, refName, baseBlobs = new Map(), referrers = []) {
|
|
312
|
+
const blobs = new Map([[img.indexDigest, img.indexBytes]]);
|
|
313
|
+
for (const { image: i } of img.images) {
|
|
314
|
+
for (const [d, b] of [[i.manifestDigest, i.manifestBytes], [i.configDigest, i.configBytes], [i.layerDigest, i.layerBytes], ...(i.extraLayers ?? [])])
|
|
315
|
+
blobs.set(d, b);
|
|
316
|
+
}
|
|
317
|
+
for (const [d, b] of baseBlobs)
|
|
318
|
+
blobs.set(d, b);
|
|
319
|
+
return layoutFromBlobs(img.indexDigest, img.indexBytes.byteLength, MEDIA.index, blobs, refName, referrers);
|
|
320
|
+
}
|
|
321
|
+
/** index.json naming the root (image or index), `oci-layout`, and the blobs. */
|
|
322
|
+
export function layoutFromBlobs(rootDigest, rootSize, rootType, blobs, refName, referrers) {
|
|
233
323
|
const entries = [
|
|
234
|
-
{ digest:
|
|
324
|
+
{ ...(rootType ? { mediaType: rootType } : {}), digest: rootDigest, size: rootSize, ...(refName ? { extra: exportNameAnnotations(refName) } : {}) },
|
|
235
325
|
...referrers.map((r) => ({ digest: r.descriptor.digest, size: r.descriptor.size, artifactType: r.descriptor.artifactType, extra: r.descriptor.annotations })),
|
|
236
326
|
];
|
|
237
|
-
const
|
|
327
|
+
const all = new Map(blobs);
|
|
238
328
|
for (const r of referrers)
|
|
239
329
|
for (const [d, b] of r.blobs)
|
|
240
|
-
|
|
330
|
+
all.set(d, b);
|
|
241
331
|
const files = [
|
|
242
332
|
['/oci-layout', makeFile(OCI_LAYOUT_BYTES, 'oci')],
|
|
243
333
|
['/index.json', makeFile(encodeIndex(entries), 'oci')],
|
|
244
|
-
...[...
|
|
334
|
+
...[...all].map(([d, b]) => [blobPathOf(d), makeFile(b, 'oci')]),
|
|
245
335
|
];
|
|
246
336
|
return Tree.fromFiles(files);
|
|
247
337
|
}
|
|
@@ -249,6 +339,10 @@ baseBlobs = new Map(), referrers = []) {
|
|
|
249
339
|
export function ociArchive(img, refName, epoch, baseBlobs, referrers = []) {
|
|
250
340
|
return writeTar(layoutTree(img, refName, baseBlobs, referrers), { epoch: epoch ?? 0 });
|
|
251
341
|
}
|
|
342
|
+
/** Deterministic tar of {@link layoutTreeIndex}. */
|
|
343
|
+
export function ociIndexArchive(img, refName, epoch, baseBlobs, referrers = []) {
|
|
344
|
+
return writeTar(layoutTreeIndex(img, refName, baseBlobs, referrers), { epoch: epoch ?? 0 });
|
|
345
|
+
}
|
|
252
346
|
function desc(d, what) {
|
|
253
347
|
const o = d;
|
|
254
348
|
if (typeof o?.mediaType !== 'string' || typeof o.digest !== 'string' || !SHA.test(o.digest) || typeof o.size !== 'number' || !Number.isSafeInteger(o.size) || o.size < 0) {
|
package/lib/src/paths.d.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { type SourceSpan } from './errors.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* context, archive entries, plugin outputs) is stored in NFC, so NFC and NFD
|
|
5
|
-
* spellings of a name are the same path on every OS.
|
|
6
|
-
*/
|
|
7
|
-
export declare function nfc(s: string): string;
|
|
2
|
+
import { nfc } from './nfc.js';
|
|
3
|
+
export { nfc };
|
|
8
4
|
/**
|
|
9
5
|
* Unicode simple case folding (CaseFolding.txt status C and S, one code
|
|
10
6
|
* point to one code point; no full or Turkic mappings), applied code point
|
package/lib/src/paths.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
import { posix } from 'node:path';
|
|
2
2
|
import { SitefileError } from './errors.js';
|
|
3
3
|
import { CASEFOLD_RUNS } from './casefold-data.js';
|
|
4
|
+
import { nfc } from './nfc.js';
|
|
4
5
|
const ASCII = /^[\x00-\x7f]*$/;
|
|
5
|
-
|
|
6
|
-
* Unicode NFC. Every virtual path (Sitefile operands, names read from the
|
|
7
|
-
* context, archive entries, plugin outputs) is stored in NFC, so NFC and NFD
|
|
8
|
-
* spellings of a name are the same path on every OS.
|
|
9
|
-
*/
|
|
10
|
-
export function nfc(s) {
|
|
11
|
-
return ASCII.test(s) ? s : s.normalize('NFC');
|
|
12
|
-
}
|
|
6
|
+
export { nfc };
|
|
13
7
|
function foldCodePoint(c) {
|
|
14
8
|
let lo = 0;
|
|
15
9
|
let hi = CASEFOLD_RUNS.length - 1;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment of a process a build starts for a plugin or a tool.
|
|
3
|
+
*
|
|
4
|
+
* It is scrubbed so that nothing from the build's own environment (tokens, keys) reaches
|
|
5
|
+
* the child. On Windows a child that has no `SystemRoot` cannot load the system's random
|
|
6
|
+
* number provider, and Node 26.14 asserts at start-up (`ncrypto::CSPRNG`, exit code 134),
|
|
7
|
+
* so there, and only there, `SystemRoot` is copied from the parent. Nothing else is added:
|
|
8
|
+
* no `windir`, no `SystemDrive`, no `PATH`. If the parent has no `SystemRoot`, none is made up.
|
|
9
|
+
*
|
|
10
|
+
* The platform and the parent's environment are parameters so that the Windows result can be
|
|
11
|
+
* tested on any system. Keep in step with `rust/src/plugins/env.rs`.
|
|
12
|
+
*/
|
|
13
|
+
/** The only variables copied from the parent on Windows. */
|
|
14
|
+
export declare const WINDOWS_KEPT_ENV: readonly string[];
|
|
15
|
+
/** What a process needs in order to start on `platform`, taken from `parent`. Empty off Windows. */
|
|
16
|
+
export declare function startupEnv(platform?: string, parent?: Readonly<Record<string, string | undefined>>): Record<string, string>;
|
|
17
|
+
/** The environment of a JavaScript plugin's Node process: empty, plus `startupEnv`. */
|
|
18
|
+
export declare function pluginEnv(platform?: string, parent?: Readonly<Record<string, string | undefined>>): Record<string, string>;
|
|
19
|
+
/** The environment of an allow-listed tool: a fixed `PATH`, plus `startupEnv`. */
|
|
20
|
+
export declare function toolEnv(base: Readonly<Record<string, string>>, platform?: string, parent?: Readonly<Record<string, string | undefined>>): Record<string, string>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment of a process a build starts for a plugin or a tool.
|
|
3
|
+
*
|
|
4
|
+
* It is scrubbed so that nothing from the build's own environment (tokens, keys) reaches
|
|
5
|
+
* the child. On Windows a child that has no `SystemRoot` cannot load the system's random
|
|
6
|
+
* number provider, and Node 26.14 asserts at start-up (`ncrypto::CSPRNG`, exit code 134),
|
|
7
|
+
* so there, and only there, `SystemRoot` is copied from the parent. Nothing else is added:
|
|
8
|
+
* no `windir`, no `SystemDrive`, no `PATH`. If the parent has no `SystemRoot`, none is made up.
|
|
9
|
+
*
|
|
10
|
+
* The platform and the parent's environment are parameters so that the Windows result can be
|
|
11
|
+
* tested on any system. Keep in step with `rust/src/plugins/env.rs`.
|
|
12
|
+
*/
|
|
13
|
+
/** The only variables copied from the parent on Windows. */
|
|
14
|
+
export const WINDOWS_KEPT_ENV = ['SystemRoot'];
|
|
15
|
+
/** What a process needs in order to start on `platform`, taken from `parent`. Empty off Windows. */
|
|
16
|
+
export function startupEnv(platform = process.platform, parent = process.env) {
|
|
17
|
+
const out = {};
|
|
18
|
+
if (platform !== 'win32')
|
|
19
|
+
return out;
|
|
20
|
+
for (const name of WINDOWS_KEPT_ENV) {
|
|
21
|
+
// Windows names are case-insensitive; the parent may spell it SYSTEMROOT.
|
|
22
|
+
const key = Object.keys(parent).find((k) => k.toLowerCase() === name.toLowerCase());
|
|
23
|
+
const value = key === undefined ? undefined : parent[key];
|
|
24
|
+
if (value !== undefined && value !== '')
|
|
25
|
+
out[name] = value;
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
/** The environment of a JavaScript plugin's Node process: empty, plus `startupEnv`. */
|
|
30
|
+
export function pluginEnv(platform, parent) {
|
|
31
|
+
return startupEnv(platform, parent);
|
|
32
|
+
}
|
|
33
|
+
/** The environment of an allow-listed tool: a fixed `PATH`, plus `startupEnv`. */
|
|
34
|
+
export function toolEnv(base, platform, parent) {
|
|
35
|
+
return { ...base, ...startupEnv(platform, parent) };
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=env.js.map
|
|
@@ -22,7 +22,7 @@ export interface ResolvedTool {
|
|
|
22
22
|
readonly size: number;
|
|
23
23
|
readonly mtimeMs: number;
|
|
24
24
|
}
|
|
25
|
-
/** The environment given to every tool: nothing but a fixed PATH (tools are started by absolute path). */
|
|
25
|
+
/** The environment given to every tool: nothing but a fixed PATH (tools are started by absolute path); on Windows also `SystemRoot` (see env.ts). */
|
|
26
26
|
export declare const TOOL_ENV: Readonly<Record<string, string>>;
|
|
27
27
|
/**
|
|
28
28
|
* Resolve `--allow-exec name[=path]`. A bare name is looked up on PATH; an explicit
|
package/lib/src/plugins/exec.js
CHANGED
|
@@ -5,8 +5,9 @@ import os from 'node:os';
|
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { SitefileError } from '../errors.js';
|
|
7
7
|
import { TOOL_PLACEHOLDER_RE } from '../language/validate.js';
|
|
8
|
+
import { toolEnv } from './env.js';
|
|
8
9
|
export const DEFAULT_TOOL_POLICY = { timeoutMs: 60_000, maxOutputBytes: 64 * 1024 * 1024 };
|
|
9
|
-
/** The environment given to every tool: nothing but a fixed PATH (tools are started by absolute path). */
|
|
10
|
+
/** The environment given to every tool: nothing but a fixed PATH (tools are started by absolute path); on Windows also `SystemRoot` (see env.ts). */
|
|
10
11
|
export const TOOL_ENV = { PATH: '/usr/bin:/bin' };
|
|
11
12
|
const FLAG_HINT = (name) => `--allow-exec ${name}`;
|
|
12
13
|
/**
|
|
@@ -76,7 +77,7 @@ export async function runTool(tool, call, policy, log, span) {
|
|
|
76
77
|
const outputFile = `output${safeExt(call.dest)}`;
|
|
77
78
|
const argv = substituteArgs(call.args, inputFile, outputFile, call.options);
|
|
78
79
|
const { code, signal, stderr, stdout, timedOut, oversized, spawnError } = await new Promise((resolve) => {
|
|
79
|
-
const child = spawn(tool.path, argv, { cwd: dir, env:
|
|
80
|
+
const child = spawn(tool.path, argv, { cwd: dir, env: toolEnv(TOOL_ENV), shell: false, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
|
|
80
81
|
let stdout = '';
|
|
81
82
|
let stderr = '';
|
|
82
83
|
let timedOut = false;
|
package/lib/src/plugins/host.js
CHANGED
|
@@ -3,6 +3,7 @@ import { realpath, stat } from 'node:fs/promises';
|
|
|
3
3
|
import os from 'node:os';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { SitefileError } from '../errors.js';
|
|
6
|
+
import { pluginEnv } from './env.js';
|
|
6
7
|
import { WasmPlugin } from './wasm.js';
|
|
7
8
|
/**
|
|
8
9
|
* Plugins run one Node process each, under Node's permission model:
|
|
@@ -57,7 +58,7 @@ export async function checkNode(node, timeoutMs) {
|
|
|
57
58
|
catch {
|
|
58
59
|
throw new Error(`could not start: no Node binary at ${given}`);
|
|
59
60
|
}
|
|
60
|
-
const out = await new Promise((resolve, reject) => execFile(real, ['--version'], { env:
|
|
61
|
+
const out = await new Promise((resolve, reject) => execFile(real, ['--version'], { env: pluginEnv(), timeout: timeoutMs, maxBuffer: 4096 }, (e, stdout) => (e ? reject(e) : resolve(String(stdout))))).catch(() => undefined);
|
|
61
62
|
const parsed = out === undefined ? undefined : parseNodeVersion(out);
|
|
62
63
|
if (!parsed)
|
|
63
64
|
throw new Error(`could not start: ${real} --version did not print a Node version; ${choose}`);
|
|
@@ -164,7 +165,7 @@ class PluginProcess {
|
|
|
164
165
|
this.policy = policy;
|
|
165
166
|
this.log = log;
|
|
166
167
|
const dir = path.dirname(file);
|
|
167
|
-
this.child = spawn(node, ['--permission', `--allow-fs-read=${dir}`, `--max-old-space-size=${policy.maxMemoryMiB}`, '--disable-warning=ExperimentalWarning', '--input-type=module', '-e', RUNNER], { cwd: dir, env:
|
|
168
|
+
this.child = spawn(node, ['--permission', `--allow-fs-read=${dir}`, `--max-old-space-size=${policy.maxMemoryMiB}`, '--disable-warning=ExperimentalWarning', '--input-type=module', '-e', RUNNER], { cwd: dir, env: pluginEnv(), stdio: ['pipe', 'pipe', 'pipe'] });
|
|
168
169
|
this.child.stdout.setEncoding('utf8');
|
|
169
170
|
this.child.stderr.setEncoding('utf8');
|
|
170
171
|
this.child.stdout.on('data', (chunk) => this.onStdout(chunk));
|
package/lib/src/precompress.d.ts
CHANGED
|
@@ -7,6 +7,12 @@ export declare const COMPRESSIBLE: Set<string>;
|
|
|
7
7
|
export declare function compressible(path: string): boolean;
|
|
8
8
|
/** The Brotli encoder version rust/vendor/brotli holds; br sidecars match the Rust port's only while Node bundles it. */
|
|
9
9
|
export declare const BROTLI_VERSION = "1.2.0";
|
|
10
|
+
/**
|
|
11
|
+
* Node Brotli versions verified to write exactly the bytes of BROTLI_VERSION (#335: 1.2.1 matched on
|
|
12
|
+
* the pinned digests and on a 130-file corpus). Add a version only after the same check; a version
|
|
13
|
+
* that writes other bytes means updating rust/vendor/brotli and the pinned digests instead.
|
|
14
|
+
*/
|
|
15
|
+
export declare const BROTLI_SAME_BYTES: readonly string[];
|
|
10
16
|
/** `data` as Brotli: quality 11, window 22 (4 MiB), generic mode, one FINISH over the whole input (rust/src/brotli.rs). */
|
|
11
17
|
export declare function brotli(data: Uint8Array): Uint8Array;
|
|
12
18
|
/** The file a sidecar compresses: `path` without its `.gz` or `.br`. */
|
package/lib/src/precompress.js
CHANGED
|
@@ -21,6 +21,12 @@ export function compressible(path) {
|
|
|
21
21
|
}
|
|
22
22
|
/** The Brotli encoder version rust/vendor/brotli holds; br sidecars match the Rust port's only while Node bundles it. */
|
|
23
23
|
export const BROTLI_VERSION = '1.2.0';
|
|
24
|
+
/**
|
|
25
|
+
* Node Brotli versions verified to write exactly the bytes of BROTLI_VERSION (#335: 1.2.1 matched on
|
|
26
|
+
* the pinned digests and on a 130-file corpus). Add a version only after the same check; a version
|
|
27
|
+
* that writes other bytes means updating rust/vendor/brotli and the pinned digests instead.
|
|
28
|
+
*/
|
|
29
|
+
export const BROTLI_SAME_BYTES = ['1.2.0', '1.2.1'];
|
|
24
30
|
/** `data` as Brotli: quality 11, window 22 (4 MiB), generic mode, one FINISH over the whole input (rust/src/brotli.rs). */
|
|
25
31
|
export function brotli(data) {
|
|
26
32
|
return new Uint8Array(brotliCompressSync(data, {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { SitefileError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* The default recipe file name (issue #332; port of `rust/src/recipe.rs`).
|
|
4
|
+
*
|
|
5
|
+
* Without `-f`, a build reads the recipe from the context directory: the first of an ordered list of
|
|
6
|
+
* names that is a file there, `["Sitefile"]` by default. Code that embeds the library can change the list
|
|
7
|
+
* for one build (`BuildOptions.recipeNames`) or for the whole process (`setRecipeNames`); a CLI user can
|
|
8
|
+
* opt in with `$SITEFILE_RECIPE_NAMES` (comma-separated). The list in force is, in order: the one given
|
|
9
|
+
* for this call, the one set with `setRecipeNames`, `$SITEFILE_RECIPE_NAMES`, then the default.
|
|
10
|
+
* An explicit `-f` always wins. Only the recipe is renamed: `Sitefile.lock` and the `IMPORT … AS sitefile`
|
|
11
|
+
* lookup keep their names.
|
|
12
|
+
*/
|
|
13
|
+
export declare const DEFAULT_RECIPE_NAMES: readonly string[];
|
|
14
|
+
/** `$SITEFILE_RECIPE_NAMES`: comma-separated recipe names, searched in order. */
|
|
15
|
+
export declare const RECIPE_NAMES_ENV = "SITEFILE_RECIPE_NAMES";
|
|
16
|
+
/** Parse a comma-separated list (`$SITEFILE_RECIPE_NAMES`), dropping empty and invalid entries. */
|
|
17
|
+
export declare function parseRecipeNames(list: string): string[];
|
|
18
|
+
/**
|
|
19
|
+
* Set the recipe names for the whole process, replacing an earlier call. Entries that are not bare file
|
|
20
|
+
* names are dropped; a list with none left restores the default (the environment, then `DEFAULT_RECIPE_NAMES`).
|
|
21
|
+
*/
|
|
22
|
+
export declare function setRecipeNames(names: readonly string[]): void;
|
|
23
|
+
/** The names in force for a call that gave none. Never empty. */
|
|
24
|
+
export declare function recipeNames(): string[];
|
|
25
|
+
/** `names` when given and non-empty after cleaning, else `recipeNames()`. */
|
|
26
|
+
export declare function effectiveRecipeNames(names?: readonly string[]): string[];
|
|
27
|
+
/** Whether `names` is exactly the default list (the one whose "not found" message is the original one). */
|
|
28
|
+
export declare function isDefaultRecipeNames(names: readonly string[]): boolean;
|
|
29
|
+
export declare function isFileSync(p: string): boolean;
|
|
30
|
+
/** The first of `names` (else the names in force) that is a file in `dir`. */
|
|
31
|
+
export declare function findRecipeName(dir: string, names?: readonly string[]): string | undefined;
|
|
32
|
+
/** `no recipe found in '<dir>' (tried: A, B)`; without a directory `no recipe found (tried: A, B)`. */
|
|
33
|
+
export declare function recipeNotFound(dir: string | undefined, names: readonly string[]): SitefileError;
|
|
34
|
+
/** The recipe of `contextDir`: the first of the names that exists there. `E_NOT_FOUND` naming every file tried when none does. */
|
|
35
|
+
export declare function resolveRecipe(contextDir: string, names?: readonly string[]): string;
|
|
36
|
+
/** The recipe path a build of `contextDir` reads: the first existing name, else the first name (so the read that follows fails naming it). */
|
|
37
|
+
export declare function recipePath(contextDir: string, names?: readonly string[]): string;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { statSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { SitefileError } from './errors.js';
|
|
4
|
+
/**
|
|
5
|
+
* The default recipe file name (issue #332; port of `rust/src/recipe.rs`).
|
|
6
|
+
*
|
|
7
|
+
* Without `-f`, a build reads the recipe from the context directory: the first of an ordered list of
|
|
8
|
+
* names that is a file there, `["Sitefile"]` by default. Code that embeds the library can change the list
|
|
9
|
+
* for one build (`BuildOptions.recipeNames`) or for the whole process (`setRecipeNames`); a CLI user can
|
|
10
|
+
* opt in with `$SITEFILE_RECIPE_NAMES` (comma-separated). The list in force is, in order: the one given
|
|
11
|
+
* for this call, the one set with `setRecipeNames`, `$SITEFILE_RECIPE_NAMES`, then the default.
|
|
12
|
+
* An explicit `-f` always wins. Only the recipe is renamed: `Sitefile.lock` and the `IMPORT … AS sitefile`
|
|
13
|
+
* lookup keep their names.
|
|
14
|
+
*/
|
|
15
|
+
export const DEFAULT_RECIPE_NAMES = ['Sitefile'];
|
|
16
|
+
/** `$SITEFILE_RECIPE_NAMES`: comma-separated recipe names, searched in order. */
|
|
17
|
+
export const RECIPE_NAMES_ENV = 'SITEFILE_RECIPE_NAMES';
|
|
18
|
+
let processNames;
|
|
19
|
+
/** A recipe name is a bare file name: not empty, no path separator, not `.` or `..`. */
|
|
20
|
+
function isValidName(n) {
|
|
21
|
+
return n !== '' && n !== '.' && n !== '..' && !/[/\\\0]/.test(n);
|
|
22
|
+
}
|
|
23
|
+
/** The valid names of `names`, trimmed, without repeats, in order. */
|
|
24
|
+
function clean(names) {
|
|
25
|
+
const out = [];
|
|
26
|
+
for (const raw of names) {
|
|
27
|
+
const n = raw.trim();
|
|
28
|
+
if (isValidName(n) && !out.includes(n))
|
|
29
|
+
out.push(n);
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
/** Parse a comma-separated list (`$SITEFILE_RECIPE_NAMES`), dropping empty and invalid entries. */
|
|
34
|
+
export function parseRecipeNames(list) {
|
|
35
|
+
return clean(list.split(','));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Set the recipe names for the whole process, replacing an earlier call. Entries that are not bare file
|
|
39
|
+
* names are dropped; a list with none left restores the default (the environment, then `DEFAULT_RECIPE_NAMES`).
|
|
40
|
+
*/
|
|
41
|
+
export function setRecipeNames(names) {
|
|
42
|
+
const v = clean(names);
|
|
43
|
+
processNames = v.length > 0 ? v : undefined;
|
|
44
|
+
}
|
|
45
|
+
/** The names in force for a call that gave none. Never empty. */
|
|
46
|
+
export function recipeNames() {
|
|
47
|
+
if (processNames)
|
|
48
|
+
return [...processNames];
|
|
49
|
+
const raw = process.env[RECIPE_NAMES_ENV];
|
|
50
|
+
if (raw !== undefined) {
|
|
51
|
+
const v = parseRecipeNames(raw);
|
|
52
|
+
if (v.length > 0)
|
|
53
|
+
return v;
|
|
54
|
+
}
|
|
55
|
+
return [...DEFAULT_RECIPE_NAMES];
|
|
56
|
+
}
|
|
57
|
+
/** `names` when given and non-empty after cleaning, else `recipeNames()`. */
|
|
58
|
+
export function effectiveRecipeNames(names) {
|
|
59
|
+
const v = names ? clean(names) : [];
|
|
60
|
+
return v.length > 0 ? v : recipeNames();
|
|
61
|
+
}
|
|
62
|
+
/** Whether `names` is exactly the default list (the one whose "not found" message is the original one). */
|
|
63
|
+
export function isDefaultRecipeNames(names) {
|
|
64
|
+
return names.length === DEFAULT_RECIPE_NAMES.length && names.every((n, i) => n === DEFAULT_RECIPE_NAMES[i]);
|
|
65
|
+
}
|
|
66
|
+
export function isFileSync(p) {
|
|
67
|
+
try {
|
|
68
|
+
return statSync(p).isFile();
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** The first of `names` (else the names in force) that is a file in `dir`. */
|
|
75
|
+
export function findRecipeName(dir, names) {
|
|
76
|
+
return effectiveRecipeNames(names).find((n) => isFileSync(path.join(dir, n)));
|
|
77
|
+
}
|
|
78
|
+
/** `no recipe found in '<dir>' (tried: A, B)`; without a directory `no recipe found (tried: A, B)`. */
|
|
79
|
+
export function recipeNotFound(dir, names) {
|
|
80
|
+
const tried = names.join(', ');
|
|
81
|
+
return new SitefileError('E_NOT_FOUND', dir !== undefined ? `no recipe found in '${dir}' (tried: ${tried})` : `no recipe found (tried: ${tried})`);
|
|
82
|
+
}
|
|
83
|
+
/** The recipe of `contextDir`: the first of the names that exists there. `E_NOT_FOUND` naming every file tried when none does. */
|
|
84
|
+
export function resolveRecipe(contextDir, names) {
|
|
85
|
+
const list = effectiveRecipeNames(names);
|
|
86
|
+
const found = list.find((n) => isFileSync(path.join(contextDir, n)));
|
|
87
|
+
if (found === undefined)
|
|
88
|
+
throw recipeNotFound(contextDir, list);
|
|
89
|
+
return path.join(contextDir, found);
|
|
90
|
+
}
|
|
91
|
+
/** The recipe path a build of `contextDir` reads: the first existing name, else the first name (so the read that follows fails naming it). */
|
|
92
|
+
export function recipePath(contextDir, names) {
|
|
93
|
+
const list = effectiveRecipeNames(names);
|
|
94
|
+
return path.join(contextDir, list.find((n) => isFileSync(path.join(contextDir, n))) ?? list[0]);
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=recipe.js.map
|
|
@@ -40,7 +40,11 @@ export interface PushResult {
|
|
|
40
40
|
/** Attestations pushed (as OCI 1.1 referrers of the image). */
|
|
41
41
|
readonly attestations: number;
|
|
42
42
|
}
|
|
43
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Push a stored image to `ref` (a registry reference): blobs that are missing, then the manifest. A
|
|
45
|
+
* multi-platform image (an image index) pushes every platform's blobs and manifest (by digest, the shared site
|
|
46
|
+
* layers once), then the index under the tag.
|
|
47
|
+
*/
|
|
44
48
|
export declare function pushImage(store: Store, client: RegistryClient, id: string, ref: ImageRef, o?: PushOptions): Promise<PushResult>;
|
|
45
49
|
/**
|
|
46
50
|
* Push attestations as OCI 1.1 referrers of `subject` (distribution spec v1.1): each artifact
|