@specific.dev/spectest 0.92.1 → 0.93.1

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.
@@ -0,0 +1,61 @@
1
+ /** Distribution v2 publication. Blob data stays in containerd, mounted
2
+ * read-only in the registry. Repository metadata and uploads MUST live on
3
+ * the environment rootfs, outside persistent image/build caches. */
4
+ import { mkdir, stat, symlink, writeFile } from "node:fs/promises";
5
+ import { dirname } from "node:path";
6
+ import { hostImage } from "./built-image.js";
7
+ import { canonicalImageReference } from "./component-images.js";
8
+ export const REGISTRY_DATA = "/var/lib/spectest-registry";
9
+ export const REGISTRY_CONTENT = "/spectest-registry-content";
10
+ export const CONTENT_BLOBS = "io.containerd.content.v1.content/blobs/sha256";
11
+ export const REGISTRY_IMAGE = "registry:2.8.3";
12
+ export const registryDirectory = (service) => `/var/lib/spectest-registries/${service}`;
13
+ /** Distribution 2.8 registry/storage/paths.go defines this layout. Seed
14
+ * before starting the registry so it cannot cache partial publication.
15
+ * The prepared build's containerd image record retains the referenced blobs. */
16
+ export async function seedRegistryImage(directory, contentDirectory, reference, image, mountedContent = REGISTRY_CONTENT) {
17
+ const canonical = canonicalImageReference(reference);
18
+ if (canonical.includes("@"))
19
+ throw new Error("registry publication must name a tag");
20
+ const repository = canonical.slice(canonical.indexOf("/") + 1, canonical.lastIndexOf(":"));
21
+ const tag = canonical.slice(canonical.lastIndexOf(":") + 1);
22
+ const root = `${directory}/docker/registry/v2`;
23
+ const repo = `${root}/repositories/${repository}`;
24
+ const link = async (path, value) => {
25
+ await mkdir(dirname(path), { recursive: true });
26
+ await writeFile(path, value);
27
+ };
28
+ for (const [id, descriptor] of image.allowed) {
29
+ if (!/^sha256:[a-f0-9]{64}$/.test(id))
30
+ throw new Error(`invalid registry seed digest ${id}`);
31
+ const hex = id.slice(7);
32
+ const info = await stat(`${contentDirectory}/${hex}`);
33
+ if (!info.isFile() || info.size !== descriptor.size)
34
+ throw new Error(`missing or incomplete registry seed blob ${id}`);
35
+ const destination = `${root}/blobs/sha256/${hex.slice(0, 2)}/${hex}/data`;
36
+ await mkdir(dirname(destination), { recursive: true });
37
+ try {
38
+ await symlink(`${mountedContent}/${hex}`, destination);
39
+ }
40
+ catch (error) {
41
+ if (error.code !== "EEXIST")
42
+ throw error;
43
+ }
44
+ await link(`${repo}/_layers/sha256/${hex}/link`, id);
45
+ if (image.metadata.has(id))
46
+ await link(`${repo}/_manifests/revisions/sha256/${hex}/link`, id);
47
+ }
48
+ const id = image.target.digest;
49
+ await link(`${repo}/_manifests/tags/${tag}/index/sha256/${id.slice(7)}/link`, id);
50
+ await link(`${repo}/_manifests/tags/${tag}/current/link`, id);
51
+ }
52
+ export async function seedRegistry(service, contentDirectory, images) {
53
+ // Sequential metadata updates avoid races when several tags share blobs.
54
+ // Builds themselves still run concurrently with all environment preparation.
55
+ for (const image of images) {
56
+ await image.ready;
57
+ const built = await hostImage(image.source);
58
+ await seedRegistryImage(registryDirectory(service), contentDirectory, image.reference, built);
59
+ console.log(`[k3s-registry] ${service}: ${image.reference} @ ${built.target.digest} (cached blobs, no export)`);
60
+ }
61
+ }
package/dist/index.d.ts CHANGED
@@ -831,7 +831,8 @@ export type BuildImage = (Omit<Extract<NormalizedServiceImage, {
831
831
  }>, "type">) & {
832
832
  /** Full image reference, e.g. "k8s.internal:5000/runtime:spectest".
833
833
  * Optional for a service; required for a Kubernetes build. This names a
834
- * local image and does not publish it to a registry. */
834
+ * local image. In k3s, builds targeting `registry.host` are also published
835
+ * to that component's registry; other references are only preloaded. */
835
836
  reference?: string;
836
837
  };
837
838
  /** A registry reference or Dockerfile build. Tagged objects from older SDKs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specific.dev/spectest",
3
- "version": "0.92.1",
3
+ "version": "0.93.1",
4
4
  "description": "Spectest SDK for defining test environments in TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,3 +1,4 @@
1
+ import { rawFetch } from "../harness/raw-fetch.js";
1
2
  // `email()` — a real SMTP server as a spectest service. The app under test
2
3
  // points its SMTP transport at `<key>:1025` and sends mail exactly as it
3
4
  // would in production; every message is captured instead of delivered.
@@ -19,10 +20,8 @@
19
20
 
20
21
  import type { ServiceDefinition } from "../index.js";
21
22
  import {
22
- pauseRecording,
23
23
  recordEmail,
24
24
  reserveEvent,
25
- resumeRecording,
26
25
  truncateUtf8,
27
26
  type EmailEventMessage,
28
27
  type EmailEventSummary,
@@ -262,26 +261,19 @@ async function instrumented<T>(
262
261
  }
263
262
  }
264
263
 
265
- /** Query the mail server's HTTP API. Recording is paused around the fetch
266
- * so these internal polls don't land as `http` events on the timeline —
267
- * the helper records one consolidated `email` event instead. */
264
+ /** Internal HTTP stays native; the public helper records one email event. */
268
265
  async function api(base: string, path: string, init?: RequestInit): Promise<unknown> {
269
- pauseRecording();
266
+ const res = await rawFetch(`${base}${path}`, init);
267
+ if (!res.ok) {
268
+ throw new Error(`email server API ${path} failed: HTTP ${res.status}`);
269
+ }
270
+ const text = await res.text();
271
+ if (text === "") return undefined;
270
272
  try {
271
- const res = await fetch(`${base}${path}`, init);
272
- if (!res.ok) {
273
- throw new Error(`email server API ${path} failed: HTTP ${res.status}`);
274
- }
275
- const text = await res.text();
276
- if (text === "") return undefined;
277
- try {
278
- return JSON.parse(text);
279
- } catch {
280
- // Mutating endpoints reply with a plain-text acknowledgement.
281
- return text;
282
- }
283
- } finally {
284
- resumeRecording();
273
+ return JSON.parse(text);
274
+ } catch {
275
+ // Mutating endpoints reply with a plain-text acknowledgement.
276
+ return text;
285
277
  }
286
278
  }
287
279
 
@@ -26,6 +26,7 @@ export { SQL, type SqlClient, type SqlOptions } from "../sql.js";
26
26
  export {
27
27
  k3s,
28
28
  type K3sOptions,
29
+ type K3sRegistryOptions,
29
30
  type KubernetesImage,
30
31
  type K3sHelpers,
31
32
  type K3sClient,
@@ -0,0 +1,109 @@
1
+ import { expect, test } from "bun:test";
2
+ import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+
6
+ // Exercise public component hooks with the same dispatcher and async scope as
7
+ // bootstrap. A child process isolates global fetch and recorder state.
8
+ async function check(code: string, workspace?: string): Promise<void> {
9
+ const moduleUrl = (file: string) => JSON.stringify(new URL(file, import.meta.url).href);
10
+ const script = `
11
+ import { strict as assert } from "node:assert";
12
+ import { s3 } from ${moduleUrl("./s3.ts")};
13
+ import { supabase } from ${moduleUrl("./supabase.ts")};
14
+ import { emailHelpers } from ${moduleUrl("./email.ts")};
15
+ import { installFetchWrapper } from ${moduleUrl("../harness/fetch.ts")};
16
+ import { createInstrumentationScope, runInstrumented } from ${moduleUrl("../harness/instrumentation-scope.ts")};
17
+ import { startRecording, stopRecording } from ${moduleUrl("../recorder.ts")};
18
+ let respond = () => Response.json({});
19
+ const requests = [];
20
+ globalThis.fetch = async (input, init) => {
21
+ requests.push({ url: String(input), method: init?.method ?? "GET" });
22
+ return respond();
23
+ };
24
+ const restore = installFetchWrapper();
25
+ const scope = createInstrumentationScope(false);
26
+ try {
27
+ await runInstrumented(scope, async () => {
28
+ assert.equal((await fetch("http://probe/")).status.unwrap(), 200);
29
+ requests.length = 0;
30
+ ${code}
31
+ });
32
+ } finally { scope.active = false; restore(); }
33
+ `;
34
+ const child = Bun.spawn([process.execPath, "--eval", script], {
35
+ env: { ...process.env, ...(workspace ? { SPECTEST_WORKSPACE: workspace } : {}) },
36
+ stdout: "pipe", stderr: "pipe",
37
+ });
38
+ const [exit, stdout, stderr] = await Promise.all([
39
+ child.exited, new Response(child.stdout).text(), new Response(child.stderr).text(),
40
+ ]);
41
+ expect(exit, stdout + stderr).toBe(0);
42
+ }
43
+
44
+ test.each([200, 201, 409, 403, 500])("S3 setup handles HTTP %i inside a wrapped setup scope", async (status) => {
45
+ await check(`
46
+ respond = () => new Response("bucket response", { status: ${status} });
47
+ const setup = s3({ buckets: ["uploads", "assets"] }).setup;
48
+ ${status === 403 || status === 500
49
+ ? `await assert.rejects(() => setup({ name: "storage" }), /failed to create bucket uploads: ${status}/);
50
+ assert.equal(requests.length, 1);`
51
+ : `await setup({ name: "storage" });
52
+ assert.deepEqual(requests, [
53
+ { url: "http://storage:9090/uploads", method: "PUT" },
54
+ { url: "http://storage:9090/assets", method: "PUT" },
55
+ ]);`}
56
+ `);
57
+ });
58
+
59
+ test("S3 setup propagates transport failures", async () => {
60
+ await check(`
61
+ respond = () => { throw new Error("connection refused"); };
62
+ await assert.rejects(() => s3({ buckets: ["uploads"] }).setup({ name: "storage" }), /connection refused/);
63
+ `);
64
+ });
65
+
66
+ test("Supabase warm-up handles errors and reads native JSON in a wrapped setup scope", async () => {
67
+ const root = await mkdtemp(join(tmpdir(), "spectest-component-fetch-"));
68
+ try {
69
+ await mkdir(join(root, "supabase/functions/hello"), { recursive: true });
70
+ await writeFile(join(root, "supabase/functions/hello/index.ts"), "export default {};");
71
+ await check(`
72
+ const parts = supabase({ dir: "supabase" }).group.services({
73
+ name: "supabase", key: part => part === "gateway" ? "supabase" : "supabase-" + part,
74
+ });
75
+ const warnings = [], logs = [];
76
+ console.warn = (...args) => warnings.push(args.join(" "));
77
+ console.log = (...args) => logs.push(args.join(" "));
78
+ respond = () => new Response("unavailable", { status: 503 });
79
+ await parts.functions.setup();
80
+ assert.equal(warnings.length, 1);
81
+ assert.match(warnings[0], /warm-up answered 503/);
82
+ respond = () => Response.json({ mode: "shared", warmed: ["hello"], failed: {} });
83
+ await parts.functions.setup();
84
+ assert.equal(logs.length, 1);
85
+ assert.match(logs[0], /warmed 1 edge function in one isolate/);
86
+ `, root);
87
+ } finally { await rm(root, { recursive: true, force: true }); }
88
+ });
89
+
90
+ test("email helpers reject HTTP errors and retain their public event without internal HTTP events", async () => {
91
+ await check(`
92
+ const mail = emailHelpers("mail");
93
+ scope.recording = true;
94
+ startRecording();
95
+ respond = () => new Response("unavailable", { status: 503 });
96
+ await assert.rejects(() => mail.clear(), /email server API .* failed: HTTP 503/);
97
+ let events = stopRecording();
98
+ assert.deepEqual(events.map(e => e.kind), ["email"]);
99
+ assert.match(events[0].error, /HTTP 503/);
100
+ startRecording();
101
+ respond = () => Response.json({ messages: [] });
102
+ assert.deepEqual((await mail.emails()).unwrap(), []);
103
+ respond = () => new Response(null, { status: 204 });
104
+ await mail.clear();
105
+ events = stopRecording();
106
+ assert.deepEqual(events.map(e => e.kind), ["email", "email"]);
107
+ assert.equal(events.some(e => e.error), false);
108
+ `);
109
+ });
@@ -0,0 +1,50 @@
1
+ import { expect, test } from "bun:test";
2
+ import { k3s } from "./k3s";
3
+ import { REGISTRY_CONTENT, REGISTRY_DATA } from "../harness/registry-seed";
4
+
5
+ test("HTTPS registry is component-owned, with explicit containerd trust and no Traefik dependency", () => {
6
+ const cluster = k3s({ registry: { host: "Registry.Test" }, traefik: false });
7
+ expect(cluster.tls).toEqual([{ hostname: "registry.test", port: 5000 }]);
8
+ const registries = cluster.files.find(file => file.path.endsWith("registries.yaml"))!.content;
9
+ expect(registries).toContain('"registry.test":\n tls:\n ca_file:');
10
+ expect(registries).not.toContain('"https://127.0.0.1');
11
+ expect(registries).toContain('"http://127.0.0.1:5000"');
12
+ });
13
+
14
+ test("only builds targeting the configured registry require publication storage", () => {
15
+ const image = { content: "FROM scratch", reference: "registry.test/codenode:latest" };
16
+ const cluster = k3s({ registry: { host: "registry.test" }, images: [image] });
17
+ expect(cluster.imageBuilds).toHaveLength(1);
18
+ expect(cluster.volumes.find(v => v.target === REGISTRY_DATA)?.source).toBe(
19
+ "/var/lib/spectest-registries/{{SPECTEST_SERVICE}}",
20
+ );
21
+ expect(cluster.volumes.find(v => v.target === REGISTRY_CONTENT)?.readOnly).toBe(true);
22
+ for (const registry of [undefined, false, true, { host: "other.test" }]) {
23
+ expect(k3s({ registry, images: [image] }).volumes.some(v => v.target === REGISTRY_DATA)).toBe(false);
24
+ }
25
+ expect(k3s({ registry: { host: "registry.test" }, images: [image.reference] }).volumes.some(v => v.target === REGISTRY_DATA)).toBe(false);
26
+ });
27
+
28
+ test("registry host rejects ports, URLs, wildcard and reserved hostnames", () => {
29
+ for (const host of ["", "https://registry.test", "registry.test:443", "*.registry.test", "k8s.internal", "registry.test/path"]) {
30
+ expect(() => k3s({ registry: { host } })).toThrow();
31
+ }
32
+ for (const registry of [null, "registry.test", {}, { host: 123 }]) {
33
+ expect(() => k3s({ registry } as never)).toThrow();
34
+ }
35
+ });
36
+
37
+ test("registry upload URLs are relative and local pre-pulls wait for the registry", async () => {
38
+ const cluster = k3s({ registry: { host: "registry.test" }, traefik: false, images: ["registry.test/app:latest"] });
39
+ let ready = false;
40
+ await cluster.setup({ name: "cluster", helpers: {
41
+ apply: async (manifest: string) => {
42
+ expect(manifest).toContain('REGISTRY_HTTP_RELATIVEURLS\n value: "true"');
43
+ expect(manifest).toContain("emptyDir: {}");
44
+ },
45
+ waitForRollout: async () => { ready = true; },
46
+ }, exec: async (_name: string, args: string[]) => {
47
+ if (args[3] === "pull") expect(ready).toBe(true);
48
+ return { exitCode: 0, stdout: "", stderr: "" };
49
+ } } as never);
50
+ });
@@ -1,6 +1,7 @@
1
1
  import { isIP } from "node:net";
2
2
  import { installPodTrust } from "./k3s-trust.js";
3
3
  import { waitForAll } from "../harness/concurrency.js";
4
+ import { CONTENT_BLOBS, REGISTRY_CONTENT, REGISTRY_DATA, REGISTRY_IMAGE, registryDirectory, seedRegistry } from "../harness/registry-seed.js";
4
5
  import { loadK3sBuildImages } from "../harness/k3s-image-loader.js";
5
6
  import { AsyncLocalStorage } from "node:async_hooks";
6
7
  import { spawn as nodeSpawn } from "node:child_process";
@@ -60,6 +61,15 @@ export type {
60
61
  KubernetesListObject,
61
62
  } from "@kubernetes/client-node";
62
63
 
64
+ /** HTTPS exposure and publication for the component's writable registry. */
65
+ export interface K3sRegistryOptions {
66
+ /** Exact DNS hostname exposed through Spectest's VM HTTPS proxy on port 443.
67
+ * Clients and Kubernetes's containerd trust the environment CA automatically.
68
+ * Builds in `images` whose reference starts with this host are also published
69
+ * here, using their original cached blobs and manifest digests. */
70
+ host: string;
71
+ }
72
+
63
73
  /** Kubernetes pods receive read-only environment CA mounts during admission.
64
74
  * Image bytes and digests stay unchanged; explicit pod trust settings win. */
65
75
  export interface K3sOptions {
@@ -67,6 +77,9 @@ export interface K3sOptions {
67
77
  * or Dockerfile builds with a full `reference` matching your manifests. Builds
68
78
  * support path/content, context, exclude, target and buildArgs, and run in
69
79
  * parallel with service builds through the standard BuildKit pipeline.
80
+ * With `registry: { host }`, builds targeting that host are also published
81
+ * to the writable registry before setup completes. Other images remain
82
+ * direct preloads; no external registry receives pushes.
70
83
  * Setup waits for every pull and fails if a requested image cannot be pulled.
71
84
  * Sticky-enabled projects retain image content and unpacked layers between
72
85
  * fresh builds automatically. Digest references can reuse the cache offline;
@@ -141,10 +154,15 @@ export interface K3sOptions {
141
154
  * into your platform, e.g. `env: { REGISTRY_URL: "cluster.internal:5000" }`.
142
155
  * Plain HTTP, so configure your push client for an insecure registry.
143
156
  *
157
+ * Set `{ host: "registry.test" }` for verified HTTPS on port 443 through
158
+ * the built-in VM proxy, including containerd trust and relative upload URLs.
159
+ * This does not require Traefik. Builds targeting that host in `images` are
160
+ * published automatically; tags and test uploads remain isolated per fork.
161
+ *
144
162
  * On by default. Set `false` for clusters that only ever run public
145
163
  * images — that skips the extra pod.
146
164
  */
147
- registry?: boolean;
165
+ registry?: boolean | K3sRegistryOptions;
148
166
  /**
149
167
  * Domains to route into this cluster's ingress via **wildcard DNS**. For
150
168
  * each `"example.com"`, spectest-resolver answers any `*.example.com`
@@ -1290,7 +1308,7 @@ const HUB_MIRROR = { registry: "docker.io", mirror: "https://mirror.gcr.io", ups
1290
1308
  * shares the node's netns, so this needs no in-container DNS and
1291
1309
  * can't be broken by a clobbered `hostnames`.
1292
1310
  */
1293
- function buildRegistriesYaml(registryEnabled: boolean): string | null {
1311
+ function buildRegistriesYaml(registryEnabled: boolean, host?: string): string | null {
1294
1312
  const lines: string[] = [
1295
1313
  "mirrors:",
1296
1314
  ` "${HUB_MIRROR.registry}":`,
@@ -1312,6 +1330,11 @@ function buildRegistriesYaml(registryEnabled: boolean): string | null {
1312
1330
  ` insecure_skip_verify: true`,
1313
1331
  );
1314
1332
  }
1333
+ if (host) {
1334
+ // This names the verified HTTPS endpoint itself; do not mirror it to HTTP.
1335
+ // container-run mounts the environment CA before k3s/containerd starts.
1336
+ lines.push(` "${host}":`, " tls:", ` ca_file: ${JSON.stringify(CA_PATH)}`);
1337
+ }
1315
1338
  return lines.join("\n") + "\n";
1316
1339
  }
1317
1340
 
@@ -1320,11 +1343,13 @@ function buildRegistriesYaml(registryEnabled: boolean): string | null {
1320
1343
  * binds the cluster container's `:5000` directly — the same trick
1321
1344
  * Traefik uses — so peer services reach it at `<cluster-key>.internal:5000`
1322
1345
  * (the cluster service's own alias) and the node's own containerd reaches
1323
- * it at `127.0.0.1:5000`. Storage is an `emptyDir`, so pushed images live
1324
- * in the cluster and are captured by snapshot / isolated per test fork
1325
- * like all other in-VM state.
1346
+ * it at `127.0.0.1:5000`. Storage is an `emptyDir` by default. Declared
1347
+ * publications use a rootfs directory plus a read-only content mount; both
1348
+ * variants keep writable registry state outside persistent image caches and
1349
+ * capture it with the VM snapshot, isolated per test fork.
1326
1350
  */
1327
- const REGISTRY_MANIFEST = `apiVersion: apps/v1
1351
+ function registryManifest(seeded: boolean): string {
1352
+ return `apiVersion: apps/v1
1328
1353
  kind: Deployment
1329
1354
  metadata:
1330
1355
  name: spectest-registry
@@ -1347,11 +1372,13 @@ spec:
1347
1372
  - operator: Exists
1348
1373
  containers:
1349
1374
  - name: registry
1350
- image: registry:2
1375
+ image: ${seeded ? REGISTRY_IMAGE : "registry:2"}
1351
1376
  imagePullPolicy: IfNotPresent
1352
1377
  env:
1353
1378
  - name: REGISTRY_HTTP_ADDR
1354
1379
  value: ":${K3S_REGISTRY_PORT}"
1380
+ - name: REGISTRY_HTTP_RELATIVEURLS
1381
+ value: "true"
1355
1382
  - name: REGISTRY_STORAGE_DELETE_ENABLED
1356
1383
  value: "true"
1357
1384
  ports:
@@ -1360,10 +1387,20 @@ spec:
1360
1387
  volumeMounts:
1361
1388
  - name: data
1362
1389
  mountPath: /var/lib/registry
1363
- volumes:
1390
+ ${seeded ? ` - name: content
1391
+ mountPath: ${REGISTRY_CONTENT}
1392
+ readOnly: true
1393
+ ` : ""} volumes:
1364
1394
  - name: data
1365
- emptyDir: {}
1395
+ ${seeded ? ` hostPath:
1396
+ path: ${REGISTRY_DATA}
1397
+ type: Directory
1398
+ - name: content
1399
+ hostPath:
1400
+ path: ${REGISTRY_CONTENT}
1401
+ type: Directory` : " emptyDir: {}"}
1366
1402
  `;
1403
+ }
1367
1404
 
1368
1405
  // ────────────────────────────────────────────────────────────────────────
1369
1406
  // Apply — server-side, converging
@@ -1883,7 +1920,7 @@ async function waitForJob(
1883
1920
  async function setupK3sCluster(
1884
1921
  name: string,
1885
1922
  helpers: K3sHelpers,
1886
- opts: { registry: boolean; ingressDomains: string[]; traefik: boolean },
1923
+ opts: { registry: boolean; ingressDomains: string[]; traefik: boolean; seed?: () => Promise<void> },
1887
1924
  ): Promise<void> {
1888
1925
  // The registry can start while Traefik's certificates are being prepared.
1889
1926
  const wait = (deployment: string): Promise<void> =>
@@ -1927,7 +1964,8 @@ async function setupK3sCluster(
1927
1964
  })(),
1928
1965
  (async () => {
1929
1966
  if (!opts.registry) return;
1930
- await helpers.apply(REGISTRY_MANIFEST);
1967
+ await opts.seed?.();
1968
+ await helpers.apply(registryManifest(!!opts.seed));
1931
1969
  await wait("spectest-registry");
1932
1970
  })(),
1933
1971
  ]);
@@ -2061,6 +2099,16 @@ export function k3s(opts: K3sOptions = {}) {
2061
2099
  const extra = opts.extraArgs ?? [];
2062
2100
  assertUsableExtraArgs(extra);
2063
2101
  const registryEnabled = opts.registry !== false;
2102
+ let registryHost: string | undefined;
2103
+ if (opts.registry !== undefined && typeof opts.registry !== "boolean") {
2104
+ if (!opts.registry || typeof opts.registry.host !== "string" || opts.registry.host.startsWith("*.")) {
2105
+ throw new Error("k3s(): registry must be a boolean or { host: an exact DNS hostname }");
2106
+ }
2107
+ // Reuse ingress validation (no scheme, port, path, or reserved .internal).
2108
+ registryHost = dnsName(opts.registry.host, { ingress: true }).hostname;
2109
+ }
2110
+ const isPublished = (reference: string) => !!registryHost && reference.startsWith(`${registryHost}/`);
2111
+ const publishBuilds = images.builds.filter(image => isPublished(image.reference));
2064
2112
  const traefikEnabled = opts.traefik !== false;
2065
2113
  const loadBalancerEnabled = opts.loadBalancer !== false;
2066
2114
  // Wildcard ingress domains. Drives both the `provides(... dnsName)`
@@ -2070,12 +2118,13 @@ export function k3s(opts: K3sOptions = {}) {
2070
2118
  // `/etc/rancher/k3s/registries.yaml` (host-cache mirrors + trust for
2071
2119
  // the in-cluster registry). Seeded via `files` because k3s reads it
2072
2120
  // only at startup, before any setup hook could run.
2073
- const registriesYaml = buildRegistriesYaml(registryEnabled);
2121
+ const registriesYaml = buildRegistriesYaml(registryEnabled, registryHost);
2074
2122
  // The environment's image cache, when this VM carries one: the
2075
2123
  // cluster's store goes on its root disk and its layers come off the
2076
2124
  // shared layers disk (see the store section above).
2077
2125
  const sticky = stickyDiskPathSync();
2078
2126
  const cache = sticky ? null : imageCachePathsSync();
2127
+ const contentDirectory = `${sticky ? `${sticky}/containerd` : cache?.root ?? "/var/lib/containerd"}/${CONTENT_BLOBS}`;
2079
2128
  const serverArgs = [
2080
2129
  "k3s",
2081
2130
  "server",
@@ -2175,6 +2224,7 @@ export function k3s(opts: K3sOptions = {}) {
2175
2224
  const def = {
2176
2225
  image: { type: "registry", reference: `rancher/k3s:${version}` },
2177
2226
  imageBuilds: images.builds,
2227
+ tls: registryHost ? [{ hostname: registryHost, port: K3S_REGISTRY_PORT }] : [],
2178
2228
  command: cmd,
2179
2229
  privileged: true,
2180
2230
  tmpfs: ["/run", "/var/run"],
@@ -2196,7 +2246,7 @@ export function k3s(opts: K3sOptions = {}) {
2196
2246
  // in one environment get separate roots either way (the service
2197
2247
  // token names the directory), because two containerd daemons cannot
2198
2248
  // share a store: bolt takes an exclusive lock.
2199
- volumes: sticky
2249
+ volumes: [...(sticky
2200
2250
  ? [{ source: `${stickyKubernetesDirectory(SELF_SERVICE_TOKEN, version)}/containerd`, target: CLUSTER_STORE_TARGET }]
2201
2251
  : cache ? [
2202
2252
  { source: nestedStoreDir(cache, SELF_SERVICE_TOKEN), target: CLUSTER_STORE_TARGET },
@@ -2204,7 +2254,12 @@ export function k3s(opts: K3sOptions = {}) {
2204
2254
  { source: MKFS_EROFS_PATH, target: MKFS_EROFS_PATH, readOnly: true },
2205
2255
  { source: STORE_ADOPT_PATH, target: STORE_ADOPT_PATH, readOnly: true },
2206
2256
  ]
2207
- : [{ target: CLUSTER_STORE_TARGET }],
2257
+ : [{ target: CLUSTER_STORE_TARGET }]),
2258
+ ...(publishBuilds.length ? [
2259
+ { source: registryDirectory(SELF_SERVICE_TOKEN), target: REGISTRY_DATA },
2260
+ { source: contentDirectory, target: REGISTRY_CONTENT, readOnly: true },
2261
+ ] : []),
2262
+ ],
2208
2263
  files: [
2209
2264
  ...(registriesYaml ? [{ path: "/etc/rancher/k3s/registries.yaml", content: registriesYaml }] : []),
2210
2265
  ...(cache
@@ -2223,49 +2278,56 @@ export function k3s(opts: K3sOptions = {}) {
2223
2278
  const { name, helpers, exec } = ctx;
2224
2279
  const runtime = componentRuntime(ctx);
2225
2280
  if (images.builds.length && !runtime) throw new Error(`k3s(${name}): prepared image inputs are missing`);
2226
- await waitForAll([
2227
- runtime ? loadK3sBuildImages(name, runtime.images, runtime.gateway, exec) : Promise.resolve(),
2228
- prePullK3sImages(name, images.pulls, exec),
2229
- (async () => {
2230
- if (runtime?.caBundle && caPresent()) {
2231
- const { cert, key } = await issueIngressCert([runtime.gateway]);
2232
- await installPodTrust({ name, gateway: runtime.gateway, cert, key,
2233
- ca: await readFile(CA_PATH, "utf8"), bundle: await readFile(runtime.caBundle, "utf8"),
2234
- }, {
2235
- apply: async configuration => { await helpers.apply(JSON.stringify(configuration)); },
2236
- ensureBundle: async (namespace, configMap, bundle) => {
2237
- try {
2238
- await helpers.client.core.createNamespacedConfigMap({ namespace, body: {
2239
- metadata: { name: configMap, namespace }, immutable: true, data: { "ca-bundle.crt": bundle },
2240
- } });
2241
- } catch (err) {
2242
- // A simultaneous Pod may have created the same namespace's bundle.
2243
- const existing = (await helpers.client.core.readNamespacedConfigMap({ namespace, name: configMap })).unwrap();
2244
- if (existing.data?.["ca-bundle.crt"] !== bundle || existing.immutable !== true) throw err;
2245
- }
2246
- },
2247
- });
2248
- // The API server observes webhook registration asynchronously. A dry-run
2249
- // Pod proves TLS reachability and admission before any project workload.
2250
- const deadline = Date.now() + 30_000;
2251
- for (;;) {
2281
+ const infrastructure = (async () => {
2282
+ if (runtime?.caBundle && caPresent()) {
2283
+ const { cert, key } = await issueIngressCert([runtime.gateway]);
2284
+ await installPodTrust({ name, gateway: runtime.gateway, cert, key,
2285
+ ca: await readFile(CA_PATH, "utf8"), bundle: await readFile(runtime.caBundle, "utf8"),
2286
+ }, {
2287
+ apply: async configuration => { await helpers.apply(JSON.stringify(configuration)); },
2288
+ ensureBundle: async (namespace, configMap, bundle) => {
2252
2289
  try {
2253
- const pod = (await helpers.client.core.createNamespacedPod({ namespace: "default", dryRun: "All", body: {
2254
- metadata: { generateName: "spectest-trust-probe-" }, spec: { containers: [{ name: "probe", image: "registry.k8s.io/pause:3.10" }] },
2255
- } })).unwrap();
2256
- if (pod.spec?.containers[0].env?.some(e => e.name === "SSL_CERT_FILE" && e.value === "/spectest/ca-bundle.crt")) break;
2257
- if (Date.now() >= deadline) throw new Error(`k3s(${name}): Pod CA mounts did not become ready`);
2290
+ await helpers.client.core.createNamespacedConfigMap({ namespace, body: {
2291
+ metadata: { name: configMap, namespace }, immutable: true, data: { "ca-bundle.crt": bundle },
2292
+ } });
2258
2293
  } catch (err) {
2259
- if (Date.now() >= deadline) throw err;
2294
+ // A simultaneous Pod may have created the same namespace's bundle.
2295
+ const existing = (await helpers.client.core.readNamespacedConfigMap({ namespace, name: configMap })).unwrap();
2296
+ if (existing.data?.["ca-bundle.crt"] !== bundle || existing.immutable !== true) throw err;
2260
2297
  }
2261
- await new Promise(resolve => setTimeout(resolve, 100));
2298
+ },
2299
+ });
2300
+ // The API server observes webhook registration asynchronously. A dry-run
2301
+ // Pod proves TLS reachability and admission before any project workload.
2302
+ const deadline = Date.now() + 30_000;
2303
+ for (;;) {
2304
+ try {
2305
+ const pod = (await helpers.client.core.createNamespacedPod({ namespace: "default", dryRun: "All", body: {
2306
+ metadata: { generateName: "spectest-trust-probe-" }, spec: { containers: [{ name: "probe", image: "registry.k8s.io/pause:3.10" }] },
2307
+ } })).unwrap();
2308
+ if (pod.spec?.containers[0].env?.some(e => e.name === "SSL_CERT_FILE" && e.value === "/spectest/ca-bundle.crt")) break;
2309
+ if (Date.now() >= deadline) throw new Error(`k3s(${name}): Pod CA mounts did not become ready`);
2310
+ } catch (err) {
2311
+ if (Date.now() >= deadline) throw err;
2262
2312
  }
2313
+ await new Promise(resolve => setTimeout(resolve, 100));
2263
2314
  }
2264
- await setupK3sCluster(name, helpers, {
2265
- registry: registryEnabled,
2266
- ingressDomains,
2267
- traefik: traefikEnabled,
2268
- });
2315
+ }
2316
+ await setupK3sCluster(name, helpers, {
2317
+ registry: registryEnabled,
2318
+ seed: publishBuilds.length ? () => seedRegistry(name, contentDirectory,
2319
+ runtime!.images.filter(image => isPublished(image.reference))) : undefined,
2320
+ ingressDomains,
2321
+ traefik: traefikEnabled,
2322
+ });
2323
+ })();
2324
+ await waitForAll([
2325
+ runtime ? loadK3sBuildImages(name, runtime.images, runtime.gateway, exec) : Promise.resolve(),
2326
+ prePullK3sImages(name, images.pulls.filter(ref => !isPublished(ref)), exec),
2327
+ (async () => {
2328
+ // Registry-local references must not race registry startup/publication.
2329
+ await infrastructure;
2330
+ await prePullK3sImages(name, images.pulls.filter(isPublished), exec);
2269
2331
  })(),
2270
2332
  ]);
2271
2333
  if (sticky) await registerStickyKubernetesStore(name, version);
@@ -1,3 +1,4 @@
1
+ import { rawFetch } from "../harness/raw-fetch.js";
1
2
  import type { ServiceDefinition } from "../index.js";
2
3
  import { S3Client, type S3ClientLike } from "../s3.js";
3
4
 
@@ -123,7 +124,7 @@ export function s3(opts: S3Options = {}) {
123
124
  // CreateBucket is a plain `PUT` on the bucket root; the store
124
125
  // returns 200 (or 409 if it already exists — idempotent enough to
125
126
  // ignore).
126
- const res = await fetch(`http://${name}:${S3_PORT}/${bucket}`, {
127
+ const res = await rawFetch(`http://${name}:${S3_PORT}/${bucket}`, {
127
128
  method: "PUT",
128
129
  });
129
130
  if (!res.ok && res.status !== 409) {
@@ -1,3 +1,4 @@
1
+ import { rawFetch } from "../harness/raw-fetch.js";
1
2
  // A ready-to-use, self-hosted **Supabase** stack, expressed as a set of
2
3
  // spectest services (one container each) wired together on spectest-net.
3
4
  //
@@ -2300,7 +2301,7 @@ export function supabase(opts: SupabaseOptions = {}): SupabaseStack {
2300
2301
  // snapshot and a test's diff is what its requests mutate.
2301
2302
  setup: async () => {
2302
2303
  const t0 = Date.now();
2303
- const res = await fetch(`http://${g.key("functions")}:9000/_spectest/warm`);
2304
+ const res = await rawFetch(`http://${g.key("functions")}:9000/_spectest/warm`);
2304
2305
  if (!res.ok) {
2305
2306
  console.warn(`supabase(): edge functions warm-up answered ${res.status}; functions boot on first call instead.`);
2306
2307
  return;
package/src/daemon.ts CHANGED
@@ -2417,6 +2417,9 @@ function bindIngressServer(
2417
2417
  // {@link stopIngressServers} depends on: a half-alive listener shares
2418
2418
  // the port silently instead of colliding loudly.
2419
2419
  reusePort: true,
2420
+ // OCI layer uploads can exceed Bun's default 128 MiB request limit.
2421
+ // Large proxy bodies stream through; this limit does not allocate a buffer.
2422
+ maxRequestBodySize: 1024 ** 4,
2420
2423
  // Bun.serve defaults to a 10s idleTimeout, which kills any proxied
2421
2424
  // request whose upstream takes >10s to produce bytes — under parallel
2422
2425
  // test load that surfaced as "fetch failed"/"other side closed" on