@specific.dev/spectest 0.26.0 → 0.28.0

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.
Files changed (74) hide show
  1. package/dist/aws-sigv4.d.ts +42 -0
  2. package/dist/aws-sigv4.js +166 -0
  3. package/dist/browser.d.ts +314 -0
  4. package/dist/browser.js +1320 -0
  5. package/dist/components/email.d.ts +135 -0
  6. package/dist/components/email.js +271 -0
  7. package/dist/components/expo.d.ts +69 -0
  8. package/dist/components/expo.js +125 -0
  9. package/dist/components/index.d.ts +8 -0
  10. package/dist/components/index.js +18 -0
  11. package/dist/components/k3s.d.ts +172 -0
  12. package/dist/components/k3s.js +1124 -0
  13. package/dist/components/postgres.d.ts +93 -0
  14. package/dist/components/postgres.js +58 -0
  15. package/dist/components/replayFake.d.ts +169 -0
  16. package/dist/components/replayFake.js +738 -0
  17. package/dist/components/s3.d.ts +99 -0
  18. package/dist/components/s3.js +81 -0
  19. package/dist/components/supabase.d.ts +197 -0
  20. package/dist/components/supabase.js +1003 -0
  21. package/dist/daemon.d.ts +1 -0
  22. package/dist/daemon.js +4611 -0
  23. package/dist/ids.d.ts +2 -0
  24. package/{src/ids.ts → dist/ids.js} +46 -50
  25. package/dist/index.d.ts +1328 -0
  26. package/dist/index.js +769 -0
  27. package/dist/ingress.d.ts +114 -0
  28. package/dist/ingress.js +210 -0
  29. package/dist/inspect.d.ts +228 -0
  30. package/dist/inspect.js +429 -0
  31. package/dist/locator.d.ts +260 -0
  32. package/dist/locator.js +293 -0
  33. package/dist/mobile.d.ts +71 -0
  34. package/dist/mobile.js +65 -0
  35. package/dist/record-secrets.d.ts +9 -0
  36. package/{src/record-secrets.ts → dist/record-secrets.js} +13 -15
  37. package/dist/recorder.d.ts +527 -0
  38. package/dist/recorder.js +219 -0
  39. package/dist/redis.d.ts +54 -0
  40. package/dist/redis.js +126 -0
  41. package/dist/replay-bundle.d.ts +38 -0
  42. package/{src/replay-bundle.ts → dist/replay-bundle.js} +29 -47
  43. package/dist/resolver.d.ts +1 -0
  44. package/dist/resolver.js +309 -0
  45. package/dist/s3.d.ts +89 -0
  46. package/dist/s3.js +198 -0
  47. package/dist/sql.d.ts +74 -0
  48. package/dist/sql.js +151 -0
  49. package/dist/terminal.d.ts +161 -0
  50. package/dist/terminal.js +538 -0
  51. package/package.json +24 -9
  52. package/src/browser.ts +0 -1819
  53. package/src/components/email.ts +0 -398
  54. package/src/components/expo.ts +0 -167
  55. package/src/components/index.ts +0 -63
  56. package/src/components/k3s.ts +0 -1312
  57. package/src/components/postgres.ts +0 -105
  58. package/src/components/replayFake.ts +0 -848
  59. package/src/components/s3.ts +0 -132
  60. package/src/components/supabase.ts +0 -1299
  61. package/src/daemon.ts +0 -4969
  62. package/src/index.ts +0 -2350
  63. package/src/ingress.ts +0 -288
  64. package/src/inspect.ts +0 -673
  65. package/src/locator.ts +0 -594
  66. package/src/mobile.ts +0 -133
  67. package/src/recorder.ts +0 -817
  68. package/src/redis.ts +0 -202
  69. package/src/resolver.ts +0 -351
  70. package/src/s3.ts +0 -333
  71. package/src/sql.ts +0 -243
  72. package/src/terminal.ts +0 -740
  73. package/src/vendor/rrweb-plugin-console-record.umd.js +0 -521
  74. package/src/vendor/rrweb-record.min.js +0 -5061
package/src/redis.ts DELETED
@@ -1,202 +0,0 @@
1
- // `RedisClient` — a drop-in for `Bun.RedisClient` (`Bun.redis`) that records
2
- // every command on the test event log and returns its reply inspect-wrapped,
3
- // so `expect(await client.get("k"))` links the assertion back to the command in
4
- // the timeline (same provenance mechanism as the wrapped `fetch` / `SQL`). Swap
5
- // `new Bun.RedisClient(url)` → `new RedisClient(url)` and nothing else changes
6
- // at the call site; replies come back `Wrapped<T>` (that wrapper is what carries
7
- // provenance), so `.unwrap()` before a `===` or before feeding a value onward.
8
- //
9
- // Reach for it in service `helpers` when you stand up a redis/valkey yourself,
10
- // or directly in a test against a connection string a fake handed back.
11
-
12
- import { recordRedis, reserveEvent, safeSerialize } from "./recorder.js";
13
- import { wrap } from "./inspect.js";
14
- import type { Wrapped } from "./inspect.js";
15
-
16
- /**
17
- * Structural type for the parts of `Bun.RedisClient` we use, declared locally
18
- * so user projects don't need `@types/bun`. The real implementation comes from
19
- * Bun at runtime via `globalThis.Bun.RedisClient`. Commands resolve to their
20
- * reply; lifecycle methods (`connect`/`close`/`subscribe`/…) are passed through
21
- * un-instrumented.
22
- */
23
- interface RawRedisClient {
24
- send(command: string, args: string[]): Promise<unknown>;
25
- close?(): void;
26
- [method: string]: unknown;
27
- }
28
-
29
- /** The instrumented Redis surface — same shape as `Bun.RedisClient`, but every
30
- * command resolves to a {@link Wrapped} reply recorded as a `redis` event.
31
- * Indexable so every Bun command method is reachable without re-declaring the
32
- * whole (large) surface; the common ones are typed for ergonomics. */
33
- export interface RedisClientLike {
34
- get(key: string): Promise<Wrapped<string | null>>;
35
- set(key: string, value: string): Promise<Wrapped<string>>;
36
- del(...keys: string[]): Promise<Wrapped<number>>;
37
- exists(...keys: string[]): Promise<Wrapped<number>>;
38
- incr(key: string): Promise<Wrapped<number>>;
39
- hgetall(key: string): Promise<Wrapped<Record<string, string>>>;
40
- /** Run an arbitrary command — `client.send("SET", ["k", "v"])`. */
41
- send(command: string, args: string[]): Promise<Wrapped<unknown>>;
42
- close(): void;
43
- [method: string]: (...args: any[]) => any;
44
- }
45
-
46
- /** Options accepted by the {@link RedisClient} constructor beyond Bun's own. */
47
- export interface RedisOptions {
48
- /** Label shown on each recorded `redis` event (the timeline step's tag).
49
- * Defaults to the connection URL's host (usually the service name). */
50
- label?: string;
51
- }
52
-
53
- interface BunGlobal {
54
- RedisClient: new (url?: string, options?: unknown) => RawRedisClient;
55
- }
56
-
57
- interface RedisConstructor {
58
- new (url?: string, opts?: RedisOptions): RedisClientLike;
59
- (url?: string, opts?: RedisOptions): RedisClientLike;
60
- }
61
-
62
- // Long-lived / connection-management methods that don't map to a single
63
- // request→reply step — passed straight through, never recorded.
64
- const REDIS_PASSTHROUGH = new Set([
65
- "connect",
66
- "close",
67
- "disconnect",
68
- "duplicate",
69
- "subscribe",
70
- "unsubscribe",
71
- "psubscribe",
72
- "punsubscribe",
73
- "ssubscribe",
74
- "sunsubscribe",
75
- "onclose",
76
- "ref",
77
- "unref",
78
- ]);
79
-
80
- const MAX_RESULT_BYTES = 2048;
81
-
82
- /**
83
- * Open an instrumented Redis client against `url` (default: Bun's own
84
- * `REDIS_URL`/`VALKEY_URL`). Usable with or without `new`. Requires the Bun
85
- * runtime — it runs inside the spectest daemon.
86
- */
87
- export const RedisClient = function RedisClient(
88
- url?: string,
89
- opts?: RedisOptions,
90
- ): RedisClientLike {
91
- const bun = (globalThis as unknown as { Bun?: BunGlobal }).Bun;
92
- if (!bun?.RedisClient) {
93
- throw new Error(
94
- "RedisClient requires the Bun runtime (Bun >= 1.2.9) — it runs inside the spectest daemon.",
95
- );
96
- }
97
- const raw = url !== undefined ? new bun.RedisClient(url, opts) : new bun.RedisClient();
98
- return instrumentRedis(raw, opts?.label ?? hostLabel(url));
99
- } as unknown as RedisConstructor;
100
-
101
- function hostLabel(url?: string): string {
102
- if (!url) return "redis";
103
- try {
104
- return new URL(url).hostname || "redis";
105
- } catch {
106
- return "redis";
107
- }
108
- }
109
-
110
- /**
111
- * Proxy a `Bun.RedisClient` so each command method (and `send(...)`) emits a
112
- * `redis` event when its promise settles and resolves to a {@link Wrapped}
113
- * reply. Lifecycle methods and non-function properties pass through.
114
- *
115
- * Exported so a client built another way can opt into the same instrumentation.
116
- */
117
- export function instrumentRedis(raw: RawRedisClient, label: string): RedisClientLike {
118
- const run = (
119
- command: string,
120
- key: string | undefined,
121
- extra: unknown[],
122
- invoke: () => unknown,
123
- ): Promise<unknown> => {
124
- const started = Date.now();
125
- const resv = reserveEvent();
126
- return Promise.resolve(invoke()).then(
127
- (value) => {
128
- const { preview, truncated } = capValue(value);
129
- const seq = recordRedis(
130
- {
131
- service: label,
132
- command,
133
- key,
134
- args: extra.length > 0 ? extra.map(safeSerialize) : undefined,
135
- result: preview,
136
- resultTruncated: truncated,
137
- durationMs: Date.now() - started,
138
- },
139
- resv,
140
- );
141
- return wrap(value, seq);
142
- },
143
- (err) => {
144
- const e = err as Error;
145
- recordRedis(
146
- {
147
- service: label,
148
- command,
149
- key,
150
- args: extra.length > 0 ? extra.map(safeSerialize) : undefined,
151
- durationMs: Date.now() - started,
152
- error: e?.message ?? String(err),
153
- },
154
- resv,
155
- );
156
- throw err;
157
- },
158
- );
159
- };
160
-
161
- const handler: ProxyHandler<RawRedisClient> = {
162
- get(target, prop, receiver) {
163
- if (typeof prop !== "string") return Reflect.get(target, prop, receiver);
164
- const v = Reflect.get(target, prop, receiver);
165
- if (typeof v !== "function") return v;
166
- const fn = v as (...a: unknown[]) => unknown;
167
- if (REDIS_PASSTHROUGH.has(prop)) return fn.bind(target);
168
- // `send(command, args)` carries the real command in its first arg.
169
- if (prop === "send") {
170
- return (command: string, args: string[] = []) =>
171
- run(String(command).toUpperCase(), args[0], args.slice(1), () =>
172
- fn.call(target, command, args),
173
- );
174
- }
175
- return (...args: unknown[]) =>
176
- run(
177
- prop.toUpperCase(),
178
- typeof args[0] === "string" ? args[0] : undefined,
179
- args.slice(1),
180
- () => fn.apply(target, args),
181
- );
182
- },
183
- };
184
- return new Proxy(raw, handler) as unknown as RedisClientLike;
185
- }
186
-
187
- /** Best-effort JSON-safe preview of a reply, capped at {@link MAX_RESULT_BYTES}. */
188
- function capValue(value: unknown): { preview?: unknown; truncated?: boolean } {
189
- if (value === null || value === undefined) return {};
190
- const safe = safeSerialize(value);
191
- const json = (() => {
192
- try {
193
- return JSON.stringify(safe);
194
- } catch {
195
- return undefined;
196
- }
197
- })();
198
- if (json !== undefined && json.length > MAX_RESULT_BYTES) {
199
- return { preview: `${json.slice(0, MAX_RESULT_BYTES)}…`, truncated: true };
200
- }
201
- return { preview: safe };
202
- }
package/src/resolver.ts DELETED
@@ -1,351 +0,0 @@
1
- // spectest-resolver: a tiny DNS server that mirrors Docker's embedded
2
- // DNS, but from the VM host instead of from inside a container.
3
- //
4
- // Single-label lookups (no dots, e.g. `api`) hit the Docker socket by
5
- // container name. Multi-label lookups (e.g. `api.stripe.com`) scan
6
- // containers on `spectest-net` for one whose network aliases include the
7
- // queried name — set via service `hostnames` in env.ts. Anything we
8
- // can't resolve from Docker is forwarded to an upstream DNS server
9
- // (default 1.1.1.1). The daemon and any test code running on the VM
10
- // host can do `fetch("http://api:8000")` or `fetch("http://api.stripe.com")`
11
- // exactly like a container on the same bridge would.
12
- //
13
- // Designed to be tiny: one process, no caching beyond DNS TTL (5s), no
14
- // docker event subscription — every query asks Docker fresh. The latency
15
- // is ~1ms per query and the failure mode (Docker socket down) is rare
16
- // enough that simplicity wins.
17
- //
18
- // Listens on two *specific* addresses (never 0.0.0.0 — see the bind logic
19
- // at the bottom): 127.0.0.53, the nameserver the VM's /etc/resolv.conf
20
- // points at (daemon, host-side test code, and dockerd's embedded DNS as
21
- // its ExtServer all use it), and the spectest-net bridge gateway IP,
22
- // which is how an in-cluster k3s pod reaches us (k3s CoreDNS is pointed
23
- // there; see components/k3s.ts). Binding the specific gateway IP rather
24
- // than 0.0.0.0 matters: a 0.0.0.0 socket replies to a 127.0.0.53 query
25
- // from a kernel-chosen source (127.0.0.1), and glibc's resolver drops the
26
- // answer because its source doesn't match the queried server.
27
-
28
- import { createSocket, type RemoteInfo } from "node:dgram";
29
- import { request as httpRequest } from "node:http";
30
- import { readFile, stat } from "node:fs/promises";
31
- import * as dnsPacket from "dns-packet";
32
-
33
- const NETWORK_NAME = process.env.SPECTEST_NETWORK ?? "spectest-net";
34
- const DOCKER_SOCKET = process.env.DOCKER_SOCKET ?? "/var/run/docker.sock";
35
- const UPSTREAM_DNS = process.env.SPECTEST_UPSTREAM_DNS ?? "1.1.1.1";
36
- const UPSTREAM_PORT = Number(process.env.SPECTEST_UPSTREAM_PORT ?? "53");
37
- // Primary listen address: the loopback nameserver in the VM's
38
- // /etc/resolv.conf. Always bound at startup.
39
- const LISTEN_ADDR = process.env.SPECTEST_RESOLVER_ADDR ?? "127.0.0.53";
40
- const LISTEN_PORT = Number(process.env.SPECTEST_RESOLVER_PORT ?? "53");
41
- const TTL_SECONDS = Number(process.env.SPECTEST_RESOLVER_TTL ?? "5");
42
- /** Path the spectest-daemon writes when it brings fakes up. */
43
- const FAKES_REGISTRY_PATH =
44
- process.env.SPECTEST_FAKES_REGISTRY ?? "/run/spectest-fakes.json";
45
-
46
- function dockerGet(path: string): Promise<unknown | null> {
47
- return new Promise((resolve) => {
48
- const req = httpRequest(
49
- { socketPath: DOCKER_SOCKET, path, method: "GET" },
50
- (res) => {
51
- if (res.statusCode !== 200) {
52
- res.resume();
53
- resolve(null);
54
- return;
55
- }
56
- const chunks: Buffer[] = [];
57
- res.on("data", (c) => chunks.push(c));
58
- res.on("end", () => {
59
- try {
60
- resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")));
61
- } catch {
62
- resolve(null);
63
- }
64
- });
65
- res.on("error", () => resolve(null));
66
- },
67
- );
68
- req.on("error", () => resolve(null));
69
- req.end();
70
- });
71
- }
72
-
73
- /**
74
- * Single-label lookup: ask Docker for the container with this exact name
75
- * and read its IP on spectest-net. Cheap — one container fetch.
76
- */
77
- async function dockerLookupByName(name: string): Promise<string | null> {
78
- const body = (await dockerGet(`/containers/${encodeURIComponent(name)}/json`)) as
79
- | { NetworkSettings?: { Networks?: Record<string, { IPAddress?: string }> } }
80
- | null;
81
- const ip = body?.NetworkSettings?.Networks?.[NETWORK_NAME]?.IPAddress;
82
- return ip && ip.length > 0 ? ip : null;
83
- }
84
-
85
- /**
86
- * Multi-label lookup: scan containers on spectest-net for one whose
87
- * Aliases include `name`. Aliases are set with `--network-alias` at
88
- * `docker run` time (see runContainer in daemon.ts).
89
- *
90
- * `/containers/json` returns `Aliases: null` in many Docker versions
91
- * even when --network-alias was set, so we use it only to enumerate
92
- * container IDs and then inspect each one — inspect reliably surfaces
93
- * the alias list. The fan-out is bounded by service count (~handful),
94
- * so the extra roundtrips are cheap.
95
- */
96
- async function dockerLookupByAlias(name: string): Promise<string | null> {
97
- const filters = encodeURIComponent(JSON.stringify({ network: [NETWORK_NAME] }));
98
- const list = (await dockerGet(`/containers/json?filters=${filters}`)) as
99
- | Array<{ Id?: string }>
100
- | null;
101
- if (!Array.isArray(list)) return null;
102
- for (const c of list) {
103
- if (!c.Id) continue;
104
- const info = (await dockerGet(`/containers/${c.Id}/json`)) as
105
- | { NetworkSettings?: { Networks?: Record<string, { Aliases?: string[]; IPAddress?: string }> } }
106
- | null;
107
- const net = info?.NetworkSettings?.Networks?.[NETWORK_NAME];
108
- if (!net) continue;
109
- if ((net.Aliases ?? []).includes(name) && net.IPAddress) {
110
- return net.IPAddress;
111
- }
112
- }
113
- return null;
114
- }
115
-
116
- // Cache the names registry file by mtime so we re-read only when the
117
- // daemon has rewritten it (at /bootstrap, and live whenever a test calls
118
- // ctx.dnsName). A miss returns empty tables — the registry is optional,
119
- // and the daemon may not have written the file yet on a fresh boot.
120
- //
121
- // `hosts` are exact hostname → IP (fakes, TLS proxies, dnsName(→ingress),
122
- // dynamic exact registrations). `wildcards` are suffix → IP, e.g.
123
- // `*.example.com` stored as `{ suffix: ".example.com", ip }`; matched only
124
- // after exact lookups (registry + docker) miss, so an exact name always
125
- // wins over a wildcard.
126
- interface RegistryCache {
127
- mtimeMs: number;
128
- hosts: Record<string, string>;
129
- wildcards: Array<{ suffix: string; ip: string }>;
130
- }
131
- let registryCache: RegistryCache = { mtimeMs: 0, hosts: {}, wildcards: [] };
132
-
133
- async function refreshRegistry(): Promise<void> {
134
- try {
135
- const st = await stat(FAKES_REGISTRY_PATH);
136
- if (st.mtimeMs !== registryCache.mtimeMs) {
137
- const raw = await readFile(FAKES_REGISTRY_PATH, "utf8");
138
- const parsed = JSON.parse(raw) as {
139
- hosts?: Record<string, string>;
140
- wildcards?: Array<{ suffix: string; ip: string }>;
141
- };
142
- registryCache = {
143
- mtimeMs: st.mtimeMs,
144
- hosts: parsed.hosts ?? {},
145
- wildcards: parsed.wildcards ?? [],
146
- };
147
- }
148
- } catch {
149
- // File missing or unreadable — treat as empty.
150
- registryCache = { mtimeMs: 0, hosts: {}, wildcards: [] };
151
- }
152
- }
153
-
154
- /** Exact names-registry lookup. */
155
- async function lookupFake(name: string): Promise<string | null> {
156
- await refreshRegistry();
157
- return registryCache.hosts[name] ?? null;
158
- }
159
-
160
- /** Wildcard suffix lookup — consulted only after an exact miss. The
161
- * longest (most specific) matching suffix wins. */
162
- async function lookupWildcard(name: string): Promise<string | null> {
163
- await refreshRegistry();
164
- let best: { suffix: string; ip: string } | null = null;
165
- for (const w of registryCache.wildcards) {
166
- if (name.endsWith(w.suffix) && (!best || w.suffix.length > best.suffix.length)) {
167
- best = w;
168
- }
169
- }
170
- return best?.ip ?? null;
171
- }
172
-
173
- async function forwardUpstream(query: Buffer): Promise<Buffer | null> {
174
- return new Promise((resolve) => {
175
- const sock = createSocket("udp4");
176
- let done = false;
177
- const finish = (b: Buffer | null) => {
178
- if (done) return;
179
- done = true;
180
- try {
181
- sock.close();
182
- } catch {
183
- // ignore
184
- }
185
- resolve(b);
186
- };
187
- sock.on("message", (msg) => finish(msg));
188
- sock.on("error", () => finish(null));
189
- sock.send(query, UPSTREAM_PORT, UPSTREAM_DNS, (err) => {
190
- if (err) finish(null);
191
- });
192
- setTimeout(() => finish(null), 3_000);
193
- });
194
- }
195
-
196
- function emptyAnswer(query: dnsPacket.Packet): Buffer {
197
- return dnsPacket.encode({
198
- type: "response",
199
- id: query.id,
200
- flags: dnsPacket.AUTHORITATIVE_ANSWER | dnsPacket.RECURSION_DESIRED,
201
- questions: query.questions,
202
- answers: [],
203
- });
204
- }
205
-
206
- function aAnswer(query: dnsPacket.Packet, name: string, ip: string): Buffer {
207
- return dnsPacket.encode({
208
- type: "response",
209
- id: query.id,
210
- flags: dnsPacket.AUTHORITATIVE_ANSWER | dnsPacket.RECURSION_DESIRED,
211
- questions: query.questions,
212
- answers: [{ type: "A", name, ttl: TTL_SECONDS, data: ip }],
213
- });
214
- }
215
-
216
- // Shared query handler. Replies are sent back through the *same* socket
217
- // the query arrived on so the reply's source address matches the address
218
- // the client sent to — which is why each listener is bound to a specific
219
- // IP rather than 0.0.0.0 (glibc drops answers whose source differs from
220
- // the queried server).
221
- async function handleQuery(
222
- sock: ReturnType<typeof createSocket>,
223
- msg: Buffer,
224
- rinfo: RemoteInfo,
225
- ): Promise<void> {
226
- let query: dnsPacket.Packet;
227
- try {
228
- query = dnsPacket.decode(msg);
229
- } catch {
230
- return;
231
- }
232
- const q = query.questions?.[0];
233
- if (q && (q.type === "A" || q.type === "AAAA")) {
234
- const name = q.name.toLowerCase();
235
- const isSingleLabel = !name.includes(".");
236
-
237
- // Fakes win over docker — they're explicitly registered by the
238
- // daemon and a fake's hostname (e.g. api.stripe.com) might collide
239
- // with a real upstream we don't want to call.
240
- const fakeIp = isSingleLabel ? null : await lookupFake(name);
241
- const ip = fakeIp
242
- ?? (isSingleLabel
243
- ? await dockerLookupByName(name)
244
- : await dockerLookupByAlias(name));
245
-
246
- if (ip) {
247
- // For AAAA we still answer empty — Docker bridges are IPv4 only, but
248
- // we own this name so libc should fall back to A instead of chasing
249
- // a stray upstream AAAA for the real public hostname.
250
- const resp = q.type === "A" ? aAnswer(query, q.name, ip) : emptyAnswer(query);
251
- sock.send(resp, rinfo.port, rinfo.address);
252
- return;
253
- }
254
-
255
- if (isSingleLabel) {
256
- // Bare hostname we don't own — reply NOERROR with no answers so the
257
- // resolver moves on quickly. Forwarding bare hostnames upstream just
258
- // causes timeouts.
259
- sock.send(emptyAnswer(query), rinfo.port, rinfo.address);
260
- return;
261
- }
262
- // Multi-label exact miss: try wildcard suffixes (e.g. *.example.com
263
- // → the k3s cluster) before forwarding upstream. Exact lookups above
264
- // always win, so a specific alias beats a covering wildcard.
265
- const wildIp = await lookupWildcard(name);
266
- if (wildIp) {
267
- const resp = q.type === "A" ? aAnswer(query, q.name, wildIp) : emptyAnswer(query);
268
- sock.send(resp, rinfo.port, rinfo.address);
269
- return;
270
- }
271
- // Still nothing — fall through and forward to upstream below.
272
- }
273
- // Anything we don't own — forward.
274
- const upstream = await forwardUpstream(msg);
275
- if (upstream) {
276
- sock.send(upstream, rinfo.port, rinfo.address);
277
- return;
278
- }
279
- // Upstream unreachable; return SERVFAIL (rcode 2 in low 4 bits of flags).
280
- const resp = dnsPacket.encode({
281
- type: "response",
282
- id: query.id,
283
- flags: dnsPacket.RECURSION_DESIRED | 0x2,
284
- questions: query.questions ?? [],
285
- });
286
- sock.send(resp, rinfo.port, rinfo.address);
287
- }
288
-
289
- /** Bind one UDP listener on `addr`, wired to the shared handler. A bind
290
- * error on the primary loopback address is fatal (DNS is fully down);
291
- * on the secondary bridge address it's logged and retried by the caller. */
292
- function bindListener(addr: string, fatalOnError: boolean): Promise<boolean> {
293
- return new Promise((resolve) => {
294
- const sock = createSocket("udp4");
295
- sock.on("message", (msg: Buffer, rinfo: RemoteInfo) => {
296
- void handleQuery(sock, msg, rinfo);
297
- });
298
- sock.on("error", (err) => {
299
- // eslint-disable-next-line no-console
300
- console.error(`[spectest-resolver] socket error on ${addr}:`, err);
301
- if (fatalOnError) process.exit(1);
302
- try {
303
- sock.close();
304
- } catch {
305
- // ignore
306
- }
307
- resolve(false);
308
- });
309
- sock.bind(LISTEN_PORT, addr, () => {
310
- // eslint-disable-next-line no-console
311
- console.log(
312
- `[spectest-resolver] listening on ${addr}:${LISTEN_PORT} (network=${NETWORK_NAME}, upstream=${UPSTREAM_DNS}:${UPSTREAM_PORT})`,
313
- );
314
- resolve(true);
315
- });
316
- });
317
- }
318
-
319
- /** Read the spectest-net bridge gateway IP from Docker — the address an
320
- * in-cluster k3s pod (or any off-bridge client routed through the node)
321
- * uses to reach the VM host. Null until the network exists. */
322
- async function bridgeGatewayIp(): Promise<string | null> {
323
- const net = (await dockerGet(`/networks/${encodeURIComponent(NETWORK_NAME)}`)) as
324
- | { IPAM?: { Config?: Array<{ Gateway?: string }> } }
325
- | null;
326
- const gw = net?.IPAM?.Config?.find((c) => c.Gateway)?.Gateway;
327
- return gw && gw.length > 0 ? gw : null;
328
- }
329
-
330
- // Primary listener: the loopback nameserver. Fatal if it can't bind.
331
- void bindListener(LISTEN_ADDR, true);
332
-
333
- // Secondary listener: the spectest-net bridge gateway, for k3s pods.
334
- // The network is created by the daemon at /load — after this process
335
- // starts and after it's captured into the base snapshot — so poll until
336
- // the gateway appears, bind once, then stop. The gateway is stable for a
337
- // VM's life (the daemon only creates the network when it's missing), so a
338
- // single successful bind survives snapshot/restore/fork. Opt out with
339
- // SPECTEST_RESOLVER_NO_BRIDGE=1.
340
- if (process.env.SPECTEST_RESOLVER_NO_BRIDGE !== "1") {
341
- let bound = false;
342
- const poll = setInterval(async () => {
343
- if (bound) return;
344
- const gw = await bridgeGatewayIp().catch(() => null);
345
- if (!gw) return;
346
- bound = true;
347
- if (!(await bindListener(gw, false))) bound = false; // retry on failure
348
- }, 2_000);
349
- // Don't keep the event loop alive solely for the poll timer.
350
- poll.unref?.();
351
- }