@ai-ecoverse/slicc-kernel 1.188.1 → 1.189.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.
package/dist/node.js CHANGED
@@ -4362,7 +4362,10 @@ function remoteWorker(open, options = {}) {
4362
4362
  opened2 = await open(wire);
4363
4363
  } catch (err) {
4364
4364
  kernel.end();
4365
- queueMicrotask(() => fail3(err instanceof Error ? err.message : String(err)));
4365
+ const status = err?.exitStatus;
4366
+ const message3 = err instanceof Error ? err.message : String(err);
4367
+ if (typeof status === "number") refuse2(message3, status);
4368
+ else queueMicrotask(() => fail3(message3));
4366
4369
  return;
4367
4370
  }
4368
4371
  if (ended) {
@@ -7190,7 +7193,7 @@ function satisfies(version, range2) {
7190
7193
  }
7191
7194
 
7192
7195
  // src/version.ts
7193
- var KERNEL_VERSION = true ? "1.188.1" : "0.0.0-dev";
7196
+ var KERNEL_VERSION = true ? "1.189.1" : "0.0.0-dev";
7194
7197
  var OS_RELEASE = "6.0.0-slicc";
7195
7198
  function procVersion() {
7196
7199
  return `Linux version ${OS_RELEASE} (slicc-kernel ${KERNEL_VERSION}) #1 SMP PREEMPT_DYNAMIC
@@ -12787,6 +12790,35 @@ function statOf2(attr2, path, mount) {
12787
12790
  } : {}
12788
12791
  };
12789
12792
  }
12793
+ var Gathered = class {
12794
+ bytes = new Uint8Array(0);
12795
+ owned = false;
12796
+ size;
12797
+ length = 0;
12798
+ constructor(size) {
12799
+ this.size = size;
12800
+ }
12801
+ add(chunk2) {
12802
+ const need2 = this.length + chunk2.length;
12803
+ if (this.length === 0) {
12804
+ this.bytes = chunk2;
12805
+ } else {
12806
+ if (!this.owned || this.bytes.length < need2) {
12807
+ const grown = new Uint8Array(
12808
+ Math.max(this.size, need2, this.owned ? this.bytes.length * 2 : 0)
12809
+ );
12810
+ grown.set(this.bytes.subarray(0, this.length));
12811
+ this.bytes = grown;
12812
+ this.owned = true;
12813
+ }
12814
+ this.bytes.set(chunk2, this.length);
12815
+ }
12816
+ this.length = need2;
12817
+ }
12818
+ get whole() {
12819
+ return this.length === this.bytes.length ? this.bytes : this.bytes.subarray(0, this.length);
12820
+ }
12821
+ };
12790
12822
  function concat4(chunks2, total) {
12791
12823
  if (chunks2.length === 1 && chunks2[0]?.length === total) return chunks2[0];
12792
12824
  const out = new Uint8Array(total);
@@ -13279,25 +13311,23 @@ var MountFs = class _MountFs {
13279
13311
  }
13280
13312
  const fh = await this.open(mount, rel2, false);
13281
13313
  const chunk2 = mount.caps.maxIo ?? DEFAULT_IO;
13282
- const chunks2 = [];
13283
- let total = 0;
13314
+ const out = new Gathered(attr2.size);
13284
13315
  try {
13285
13316
  for (; ; ) {
13286
13317
  const bytes = await mount.conn.call({
13287
13318
  op: "read",
13288
13319
  fh,
13289
- offset: total,
13320
+ offset: out.length,
13290
13321
  size: chunk2
13291
13322
  });
13292
13323
  if (bytes.length === 0) break;
13293
- chunks2.push(bytes);
13294
- total += bytes.length;
13295
- if (bytes.length < chunk2 && total >= attr2.size) break;
13324
+ out.add(bytes);
13325
+ if (bytes.length < chunk2 && out.length >= attr2.size) break;
13296
13326
  }
13297
13327
  } finally {
13298
13328
  await mount.conn.call({ op: "release", fh });
13299
13329
  }
13300
- return concat4(chunks2, total);
13330
+ return out.whole;
13301
13331
  }
13302
13332
  async writeFile(path, content, owner, redirects = 0) {
13303
13333
  const real = await this.follow(path, 0, true);
@@ -21371,6 +21401,462 @@ var HttpHandles = class {
21371
21401
  }
21372
21402
  };
21373
21403
 
21404
+ // src/kernel/net/dns-stub.ts
21405
+ var DNS_PORT = 53;
21406
+ var DNS_STUB_ADDRESS = "127.0.0.53";
21407
+ var RESOLV_CONF_PATH = "/etc/resolv.conf";
21408
+ var RESOLV_CONF = `nameserver ${DNS_STUB_ADDRESS}
21409
+ options edns0
21410
+ `;
21411
+ var DNS_STUB_TTL = 30;
21412
+ var DNS_FORWARD_TIMEOUT_MS = 5e3;
21413
+ var DNS_TCP_IDLE_MS = 1e4;
21414
+ var DNS_TCP_MAX = 64;
21415
+ var DNS_UDP_INFLIGHT = 64;
21416
+ var TYPE_A = 1;
21417
+ var TYPE_PTR = 12;
21418
+ var TYPE_AAAA = 28;
21419
+ var TYPE_OPT = 41;
21420
+ var CLASS_IN = 1;
21421
+ var NOERROR = 0;
21422
+ var FORMERR = 1;
21423
+ var SERVFAIL = 2;
21424
+ var NXDOMAIN = 3;
21425
+ var NOTIMP = 4;
21426
+ var QR = 32768;
21427
+ var AA = 1024;
21428
+ var TC = 512;
21429
+ var RD = 256;
21430
+ var RA = 128;
21431
+ var CD = 16;
21432
+ var UDP_PLAIN = 512;
21433
+ var UDP_EDNS = 1232;
21434
+ var UDP_MAX = 4096;
21435
+ var DOH_MAX = 65535;
21436
+ var LOCAL_ZONES = ["localhost", "slicc.internal"];
21437
+ var MESSAGE = "application/dns-message";
21438
+ var u16 = (b, at) => b[at] << 8 | b[at + 1];
21439
+ function skipName(b, at) {
21440
+ let i = at;
21441
+ for (let hops = 0; hops < 128; hops++) {
21442
+ if (i >= b.length) return void 0;
21443
+ const len = b[i];
21444
+ if (len === 0) return i + 1;
21445
+ if ((len & 192) === 192) return i + 2 <= b.length ? i + 2 : void 0;
21446
+ if (len & 192) return void 0;
21447
+ i += len + 1;
21448
+ }
21449
+ return void 0;
21450
+ }
21451
+ function readName(b, at) {
21452
+ const labels = [];
21453
+ let i = at;
21454
+ let size = 0;
21455
+ for (; ; ) {
21456
+ if (i >= b.length) return void 0;
21457
+ const len = b[i];
21458
+ if (len === 0) break;
21459
+ if (len & 192) return void 0;
21460
+ size += len + 1;
21461
+ if (size > 254 || i + 1 + len > b.length) return void 0;
21462
+ labels.push(new TextDecoder().decode(b.subarray(i + 1, i + 1 + len)));
21463
+ i += len + 1;
21464
+ }
21465
+ return { name: labels.join(".").toLowerCase(), end: i + 1 };
21466
+ }
21467
+ function parseQuery(b) {
21468
+ if (b.length < 12) return void 0;
21469
+ const id2 = u16(b, 0);
21470
+ const flags2 = u16(b, 2);
21471
+ if (flags2 & QR) return void 0;
21472
+ if (flags2 >> 11 & 15) return { ok: false, id: id2, flags: flags2, rcode: NOTIMP };
21473
+ if (u16(b, 4) !== 1) return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
21474
+ const read = readName(b, 12);
21475
+ if (!read || read.end + 4 > b.length) return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
21476
+ const type = u16(b, read.end);
21477
+ const klass = u16(b, read.end + 2);
21478
+ const end = read.end + 4;
21479
+ let edns;
21480
+ let at = end;
21481
+ const records = u16(b, 6) + u16(b, 8) + u16(b, 10);
21482
+ for (let n = 0; n < records; n++) {
21483
+ const after = skipName(b, at);
21484
+ if (after === void 0 || after + 10 > b.length)
21485
+ return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
21486
+ if (u16(b, after) === TYPE_OPT) edns = Math.max(UDP_PLAIN, u16(b, after + 2));
21487
+ at = after + 10 + u16(b, after + 8);
21488
+ if (at > b.length) return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
21489
+ }
21490
+ return { ok: true, question: { id: id2, flags: flags2, name: read.name, type, klass, end, edns } };
21491
+ }
21492
+ function encodeName(name2) {
21493
+ const out = [];
21494
+ for (const label of name2.split(".").filter(Boolean)) {
21495
+ const bytes = new TextEncoder().encode(label);
21496
+ out.push(bytes.length, ...bytes);
21497
+ }
21498
+ out.push(0);
21499
+ return out;
21500
+ }
21501
+ function header2(id2, flags2, counts) {
21502
+ const out = [id2 >> 8, id2 & 255, flags2 >> 8, flags2 & 255];
21503
+ for (const c of counts) out.push(c >> 8, c & 255);
21504
+ return out;
21505
+ }
21506
+ function opt() {
21507
+ return [0, 0, TYPE_OPT, UDP_EDNS >> 8, UDP_EDNS & 255, 0, 0, 0, 0, 0, 0];
21508
+ }
21509
+ function record2(type, rdata) {
21510
+ const ttl = DNS_STUB_TTL;
21511
+ return [
21512
+ 192,
21513
+ 12,
21514
+ type >> 8,
21515
+ type & 255,
21516
+ 0,
21517
+ CLASS_IN,
21518
+ ttl >>> 24 & 255,
21519
+ ttl >>> 16 & 255,
21520
+ ttl >>> 8 & 255,
21521
+ ttl & 255,
21522
+ rdata.length >> 8,
21523
+ rdata.length & 255,
21524
+ ...rdata
21525
+ ];
21526
+ }
21527
+ function replyFlags(query, rcode, aa) {
21528
+ return QR | query & 30720 | (aa ? AA : 0) | query & RD | RA | query & CD | rcode;
21529
+ }
21530
+ function answers(reply, q) {
21531
+ if (reply.length < 12 || !(u16(reply, 2) & QR) || u16(reply, 4) !== 1) return false;
21532
+ const read = readName(reply, 12);
21533
+ if (!read || read.end + 4 > reply.length) return false;
21534
+ return read.name === q.name && u16(reply, read.end) === q.type && u16(reply, read.end + 2) === q.klass;
21535
+ }
21536
+ function errorReply2(id2, flags2, rcode) {
21537
+ return new Uint8Array(header2(id2, replyFlags(flags2, rcode, false), [0, 0, 0, 0]));
21538
+ }
21539
+ function answerReply(query, q, rcode, answers2, aa) {
21540
+ const extra = q.edns !== void 0 ? [opt()] : [];
21541
+ return new Uint8Array([
21542
+ ...header2(q.id, replyFlags(q.flags, rcode, aa), [1, answers2.length, 0, extra.length]),
21543
+ ...query.subarray(12, q.end),
21544
+ ...answers2.flat(),
21545
+ ...extra.flat()
21546
+ ]);
21547
+ }
21548
+ function truncated(reply, q, query) {
21549
+ const limit = q.edns !== void 0 ? Math.min(q.edns, UDP_MAX) : UDP_PLAIN;
21550
+ if (reply.length <= limit) return reply;
21551
+ const flags2 = u16(reply, 2) | TC;
21552
+ const extra = q.edns !== void 0 ? [opt()] : [];
21553
+ return new Uint8Array([
21554
+ ...header2(q.id, flags2, [1, 0, 0, extra.length]),
21555
+ ...query.subarray(12, q.end),
21556
+ ...extra.flat()
21557
+ ]);
21558
+ }
21559
+ function ipv6Text(groups) {
21560
+ return groups.map((g) => g.toString(16)).join(":");
21561
+ }
21562
+ function reverseAddress(name2) {
21563
+ if (name2.endsWith(".in-addr.arpa")) {
21564
+ const parts2 = name2.slice(0, -".in-addr.arpa".length).split(".");
21565
+ if (parts2.length !== 4) return void 0;
21566
+ const text = parts2.reverse().join(".");
21567
+ return ipv4Bytes(text) ? text.split(".").map(Number).join(".") : void 0;
21568
+ }
21569
+ if (name2.endsWith(".ip6.arpa")) {
21570
+ const nibbles = name2.slice(0, -".ip6.arpa".length).split(".");
21571
+ if (nibbles.length !== 32 || !nibbles.every((n) => /^[0-9a-f]$/.test(n))) return void 0;
21572
+ const hex4 = nibbles.reverse().join("");
21573
+ const groups = Array.from(
21574
+ { length: 8 },
21575
+ (_, i) => Number.parseInt(hex4.slice(i * 4, i * 4 + 4), 16)
21576
+ );
21577
+ return ipv6Text(groups);
21578
+ }
21579
+ return void 0;
21580
+ }
21581
+ function local(name2) {
21582
+ return LOCAL_ZONES.some((zone) => name2 === zone || name2.endsWith(`.${zone}`));
21583
+ }
21584
+ function literal2(name2) {
21585
+ return ipv4Bytes(name2) !== void 0 || ipv6Groups(name2) !== void 0;
21586
+ }
21587
+ function addressData(address2, type) {
21588
+ if (type === TYPE_A) return ipv4Bytes(address2);
21589
+ const groups = type === TYPE_AAAA ? ipv6Groups(address2) : void 0;
21590
+ return groups?.flatMap((g) => [g >> 8, g & 255]);
21591
+ }
21592
+ var DnsStub = class {
21593
+ resolver;
21594
+ forwarder;
21595
+ timeoutMs;
21596
+ constructor(options) {
21597
+ this.resolver = options.resolver;
21598
+ this.forwarder = options.forward;
21599
+ this.timeoutMs = options.timeoutMs ?? DNS_FORWARD_TIMEOUT_MS;
21600
+ }
21601
+ async answer(query, udp) {
21602
+ const parsed3 = parseQuery(query);
21603
+ if (!parsed3) return void 0;
21604
+ if (!parsed3.ok) return errorReply2(parsed3.id, parsed3.flags, parsed3.rcode);
21605
+ const q = parsed3.question;
21606
+ const reply = await this.reply(query, q).catch(
21607
+ () => answerReply(query, q, SERVFAIL, [], false)
21608
+ );
21609
+ return udp ? truncated(reply, q, query) : reply;
21610
+ }
21611
+ async reply(query, q) {
21612
+ if (q.klass !== CLASS_IN || literal2(q.name)) return this.forward(query, q);
21613
+ if (q.type === TYPE_PTR) {
21614
+ const address2 = reverseAddress(q.name);
21615
+ const name2 = address2 !== void 0 ? await this.reverse(address2) : void 0;
21616
+ if (name2 !== void 0)
21617
+ return answerReply(query, q, NOERROR, [record2(TYPE_PTR, encodeName(name2))], true);
21618
+ return this.forward(query, q);
21619
+ }
21620
+ if (q.type === TYPE_A || q.type === TYPE_AAAA) {
21621
+ const addresses = q.name === "" ? [] : await this.lookup(q.name);
21622
+ const answers2 = addresses.flatMap((a) => {
21623
+ const data = addressData(a, q.type);
21624
+ return data ? [record2(q.type, data)] : [];
21625
+ });
21626
+ if (answers2.length > 0 || local(q.name) || await this.known(q.name)) {
21627
+ const rcode = addresses.length > 0 ? NOERROR : NXDOMAIN;
21628
+ return answerReply(query, q, rcode, answers2, local(q.name));
21629
+ }
21630
+ return this.forward(query, q);
21631
+ }
21632
+ if (await this.known(q.name)) return answerReply(query, q, NOERROR, [], true);
21633
+ if (local(q.name)) {
21634
+ const there = (await this.lookup(q.name)).length > 0;
21635
+ return answerReply(query, q, there ? NOERROR : NXDOMAIN, [], true);
21636
+ }
21637
+ return this.forward(query, q);
21638
+ }
21639
+ lookup(name2) {
21640
+ return this.resolver.resolve(name2, 0, void 0, false).catch(() => []);
21641
+ }
21642
+ known(name2) {
21643
+ return this.resolver.known(name2).catch(() => false);
21644
+ }
21645
+ reverse(address2) {
21646
+ return this.resolver.reverse(address2).catch(() => void 0);
21647
+ }
21648
+ async forward(query, q) {
21649
+ if (!this.forwarder) return answerReply(query, q, SERVFAIL, [], false);
21650
+ const asked = query.slice();
21651
+ asked[0] = 0;
21652
+ asked[1] = 0;
21653
+ const got = await this.forwarder(asked, AbortSignal.timeout(this.timeoutMs)).catch(
21654
+ () => void 0
21655
+ );
21656
+ if (!got || !answers(got, q)) return answerReply(query, q, SERVFAIL, [], false);
21657
+ const reply = got.slice();
21658
+ reply[0] = q.id >> 8;
21659
+ reply[1] = q.id & 255;
21660
+ return reply;
21661
+ }
21662
+ };
21663
+ async function collect(body, max) {
21664
+ const parts2 = [];
21665
+ let size = 0;
21666
+ for await (const part of body) {
21667
+ size += part.length;
21668
+ if (size > max) return void 0;
21669
+ parts2.push(part);
21670
+ }
21671
+ const out = new Uint8Array(size);
21672
+ let at = 0;
21673
+ for (const part of parts2) {
21674
+ out.set(part, at);
21675
+ at += part.length;
21676
+ }
21677
+ return out;
21678
+ }
21679
+ function dohForward(transport, url) {
21680
+ return async (query, signal) => {
21681
+ const res = await transport.fetch({
21682
+ url,
21683
+ method: "POST",
21684
+ headers: [
21685
+ ["content-type", MESSAGE],
21686
+ ["accept", MESSAGE]
21687
+ ],
21688
+ body: query,
21689
+ signal
21690
+ });
21691
+ const type = res.headers.find(([k]) => k.toLowerCase() === "content-type")?.[1] ?? "";
21692
+ if (res.status !== 200 || type.split(";")[0].trim().toLowerCase() !== MESSAGE) {
21693
+ await res.cancel().catch(() => void 0);
21694
+ return void 0;
21695
+ }
21696
+ return collect(res.body, DOH_MAX);
21697
+ };
21698
+ }
21699
+ async function writeAll3(conn, bytes) {
21700
+ for (let at = 0; at < bytes.length; ) {
21701
+ const n = await conn.write(bytes.subarray(at));
21702
+ if (!(n > 0)) throw new Error("the uplink wrote nothing");
21703
+ at += n;
21704
+ }
21705
+ }
21706
+ function uplinkForward(uplink, server) {
21707
+ return async (query, signal) => {
21708
+ const conn = await uplink.dial({ network: "tcp", host: server, port: DNS_PORT, signal });
21709
+ const stop = () => conn.close();
21710
+ signal.addEventListener("abort", stop, { once: true });
21711
+ try {
21712
+ await writeAll3(conn, new Uint8Array([query.length >> 8, query.length & 255, ...query]));
21713
+ let got = new Uint8Array();
21714
+ for (; ; ) {
21715
+ if (got.length >= 2 && got.length >= 2 + u16(got, 0)) return got.slice(2, 2 + u16(got, 0));
21716
+ const piece = await conn.read();
21717
+ if (!piece) return void 0;
21718
+ const next3 = new Uint8Array(got.length + piece.length);
21719
+ next3.set(got);
21720
+ next3.set(piece, got.length);
21721
+ got = next3;
21722
+ }
21723
+ } finally {
21724
+ signal.removeEventListener("abort", stop);
21725
+ conn.close();
21726
+ }
21727
+ };
21728
+ }
21729
+ var u32 = (b, at) => u16(b, at) * 65536 + u16(b, at + 2);
21730
+ function addressText(type, rdata) {
21731
+ if (type === TYPE_A && rdata.length === 4) return rdata.join(".");
21732
+ if (type !== TYPE_AAAA || rdata.length !== 16) return void 0;
21733
+ return ipv6Text(Array.from({ length: 8 }, (_, i) => u16(rdata, i * 2)));
21734
+ }
21735
+ function addressesOf(reply, type) {
21736
+ if (reply.length < 12 || !(u16(reply, 2) & QR)) return void 0;
21737
+ const rcode = u16(reply, 2) & 15;
21738
+ if (rcode === NXDOMAIN) return { addresses: [] };
21739
+ if (rcode !== NOERROR) return void 0;
21740
+ let at = 12;
21741
+ for (let n = u16(reply, 4); n > 0; n--) {
21742
+ const after = skipName(reply, at);
21743
+ if (after === void 0) return void 0;
21744
+ at = after + 4;
21745
+ }
21746
+ const addresses = [];
21747
+ let ttl;
21748
+ for (let n = u16(reply, 6); n > 0; n--) {
21749
+ const after = skipName(reply, at);
21750
+ if (after === void 0 || after + 10 > reply.length) return void 0;
21751
+ const end = after + 10 + u16(reply, after + 8);
21752
+ if (end > reply.length) return void 0;
21753
+ const text = u16(reply, after) === type && u16(reply, after + 2) === CLASS_IN ? addressText(type, reply.subarray(after + 10, end)) : void 0;
21754
+ if (text !== void 0) {
21755
+ addresses.push(text);
21756
+ ttl = Math.min(ttl ?? Number.POSITIVE_INFINITY, u32(reply, after + 4));
21757
+ }
21758
+ at = end;
21759
+ }
21760
+ return { addresses, ...ttl !== void 0 ? { ttl } : {} };
21761
+ }
21762
+ function addressQuery(name2, type) {
21763
+ return new Uint8Array([
21764
+ ...header2(0, RD, [1, 0, 0, 0]),
21765
+ ...encodeName(name2),
21766
+ type >> 8,
21767
+ type & 255,
21768
+ 0,
21769
+ CLASS_IN
21770
+ ]);
21771
+ }
21772
+ async function forwardedLookup(forward, name2, family, signal) {
21773
+ const types = family === 4 ? [TYPE_A] : family === 6 ? [TYPE_AAAA] : [TYPE_A, TYPE_AAAA];
21774
+ const answers2 = await Promise.all(
21775
+ types.map(async (type) => {
21776
+ const reply = await forward(addressQuery(name2, type), signal);
21777
+ const answer2 = reply ? addressesOf(reply, type) : void 0;
21778
+ if (!answer2) throw new Error(`the DNS forwarder gave no answer for ${name2}`);
21779
+ return answer2;
21780
+ })
21781
+ );
21782
+ const ttls = answers2.flatMap((a) => a.ttl === void 0 ? [] : [a.ttl]);
21783
+ return {
21784
+ addresses: answers2.flatMap((a) => a.addresses),
21785
+ ...ttls.length > 0 ? { ttl: Math.min(...ttls) } : {}
21786
+ };
21787
+ }
21788
+ function dnsForward(uplink, transport, doh) {
21789
+ const server = uplink?.nameserver;
21790
+ if (uplink && server && (ipv4Bytes(server) || ipv6Groups(server)))
21791
+ return uplinkForward(uplink, server);
21792
+ return typeof doh === "string" ? dohForward(transport, doh) : void 0;
21793
+ }
21794
+ async function readExactly(conn, size, signal) {
21795
+ const out = new Uint8Array(size);
21796
+ let at = 0;
21797
+ while (at < size) {
21798
+ const piece = await conn.read(size - at, signal);
21799
+ if (piece.length === 0) return void 0;
21800
+ out.set(piece, at);
21801
+ at += piece.length;
21802
+ }
21803
+ return out;
21804
+ }
21805
+ async function serveTcp(stub, conn) {
21806
+ try {
21807
+ for (; ; ) {
21808
+ const idle2 = AbortSignal.timeout(DNS_TCP_IDLE_MS);
21809
+ const size = await readExactly(conn, 2, idle2);
21810
+ if (!size) return;
21811
+ const query = await readExactly(conn, u16(size, 0), idle2);
21812
+ if (!query) return;
21813
+ const reply = await stub.answer(query, false);
21814
+ if (!reply) return;
21815
+ const framed = new Uint8Array([reply.length >> 8, reply.length & 255, ...reply]);
21816
+ const sent = await conn.write(framed, AbortSignal.timeout(DNS_TCP_IDLE_MS));
21817
+ if (sent < framed.length) return;
21818
+ }
21819
+ } catch {
21820
+ return;
21821
+ } finally {
21822
+ conn.close();
21823
+ }
21824
+ }
21825
+ function enableDnsStub(net, stub) {
21826
+ const at = { family: "inet", host: "127.0.0.1", port: DNS_PORT };
21827
+ net.reserve(DNS_PORT);
21828
+ net.kernelOnly(DNS_PORT, true);
21829
+ net.activate(at, () => {
21830
+ const listener = net.listen(at);
21831
+ const open = /* @__PURE__ */ new Set();
21832
+ void (async () => {
21833
+ for (; ; ) {
21834
+ const conn = await listener.accept().catch(() => void 0);
21835
+ if (!conn) return;
21836
+ if (open.size >= DNS_TCP_MAX) {
21837
+ conn.close();
21838
+ continue;
21839
+ }
21840
+ open.add(conn);
21841
+ void serveTcp(stub, conn).finally(() => open.delete(conn));
21842
+ }
21843
+ })();
21844
+ });
21845
+ let inflight = 0;
21846
+ net.serveDgram(DNS_PORT, (datagram2, to) => {
21847
+ if (inflight >= DNS_UDP_INFLIGHT) return;
21848
+ inflight++;
21849
+ void stub.answer(datagram2.bytes, true).then(
21850
+ (reply) => {
21851
+ if (!reply) return;
21852
+ const from = { family: "inet", host: to.host, port: DNS_PORT };
21853
+ net.deliverDgram(datagram2.from.port, { bytes: reply, from });
21854
+ },
21855
+ () => void 0
21856
+ ).finally(() => inflight--);
21857
+ });
21858
+ }
21859
+
21374
21860
  // src/kernel/net/hosts-file.ts
21375
21861
  var HOSTS_PATH = "/etc/hosts";
21376
21862
  function parseHosts(text) {
@@ -21447,6 +21933,7 @@ var Resolver = class {
21447
21933
  cache = /* @__PURE__ */ new Map();
21448
21934
  pending = /* @__PURE__ */ new Map();
21449
21935
  hosts;
21936
+ forward;
21450
21937
  constructor(options) {
21451
21938
  this.uplink = options.uplink;
21452
21939
  this.routes = options.routes;
@@ -21456,6 +21943,7 @@ var Resolver = class {
21456
21943
  this.own = ownNames(options.hostname ?? DEFAULT_HOSTNAME);
21457
21944
  this.builtin = parseHosts(hostsFile(options.hostname ?? DEFAULT_HOSTNAME));
21458
21945
  this.hosts = options.hosts;
21946
+ this.forward = options.forward;
21459
21947
  }
21460
21948
  async fromFile(key, family) {
21461
21949
  const listed = (await this.hosts?.().catch(() => void 0))?.get(key);
@@ -21480,7 +21968,7 @@ var Resolver = class {
21480
21968
  if (ipv4Bytes(address2)?.[0] === 127) return "localhost";
21481
21969
  return void 0;
21482
21970
  }
21483
- async resolve(name2, family, blocking) {
21971
+ async resolve(name2, family, blocking, forwarded = true) {
21484
21972
  const key = name2.toLowerCase().replace(/\.$/, "");
21485
21973
  const local2 = hostsEntry(key, this.own);
21486
21974
  if (ipv6Groups(key)) {
@@ -21496,40 +21984,60 @@ var Resolver = class {
21496
21984
  if (!this.uplink || key === "") return [];
21497
21985
  const asked = this.uplink.traits.ipv6 ? family : 4;
21498
21986
  if (family === 6 && asked !== 6) return [];
21499
- const id2 = `${asked}:${key}`;
21987
+ return this.remote(key, asked, blocking, forwarded);
21988
+ }
21989
+ remote(key, asked, blocking, forwarded) {
21990
+ const id2 = `${forwarded && this.forward ? "forwarded" : "uplink"}:${asked}:${key}`;
21500
21991
  const hit = this.cache.get(id2);
21501
- if (hit && hit.expires > this.now()) return hit.addresses;
21992
+ if (hit && hit.expires > this.now()) return Promise.resolve(hit.addresses);
21502
21993
  const signal = blocking?.();
21503
- if (signal?.aborted) throw new KernelError("EINTR");
21994
+ if (signal?.aborted) return Promise.reject(new KernelError("EINTR"));
21504
21995
  let lookup4 = this.pending.get(id2);
21505
21996
  if (!lookup4) {
21506
21997
  const abort = new AbortController();
21507
- lookup4 = { work: this.lookup(key, asked, id2, abort.signal), abort, waiters: 0 };
21998
+ const work = this.lookup(key, asked, id2, abort.signal, forwarded);
21999
+ lookup4 = { work, abort, waiters: 0 };
21508
22000
  this.pending.set(id2, lookup4);
21509
22001
  const done4 = () => this.pending.delete(id2);
21510
22002
  void lookup4.work.then(done4, done4);
21511
22003
  }
21512
22004
  return waitFor(lookup4, signal);
21513
22005
  }
21514
- async lookup(name2, family, id2, abandoned) {
21515
- const uplink = this.uplink;
22006
+ async lookup(name2, family, id2, abandoned, forwarded) {
21516
22007
  const timeout = AbortSignal.any([AbortSignal.timeout(this.timeoutMs), abandoned]);
21517
- let answer2;
22008
+ const own = await this.ask(
22009
+ name2,
22010
+ `resolve ${name2}: the uplink gave no answer`,
22011
+ timeout,
22012
+ () => this.uplink?.resolve ? this.uplink.resolve(name2, family, timeout) : Promise.resolve([])
22013
+ );
22014
+ let answer2 = { ...own, addresses: own.addresses.filter((a) => this.usable(name2, a, family)) };
22015
+ const forward = this.forward;
22016
+ if (answer2.addresses.length === 0 && forwarded && forward && !local(name2)) {
22017
+ const found = await this.ask(
22018
+ name2,
22019
+ `resolve ${name2}: the DNS forwarder gave no answer`,
22020
+ timeout,
22021
+ () => forwardedLookup(forward, name2, family, timeout)
22022
+ );
22023
+ answer2 = { ...found, addresses: found.addresses.filter((a) => this.usable(name2, a, family)) };
22024
+ }
22025
+ if (answer2.addresses.length > 0) this.remember(id2, answer2.addresses, answer2.ttl);
22026
+ return answer2.addresses;
22027
+ }
22028
+ async ask(name2, failure, timeout, work) {
21518
22029
  try {
21519
- answer2 = answerOf2(
22030
+ return answerOf2(
21520
22031
  await Promise.race([
21521
- uplink.resolve(name2, family, timeout),
22032
+ work(),
21522
22033
  new Promise(
21523
22034
  (_, reject) => timeout.addEventListener("abort", () => reject(timeout.reason), { once: true })
21524
22035
  )
21525
22036
  ])
21526
22037
  );
21527
22038
  } catch {
21528
- throw new KernelError("ETIMEDOUT", `resolve ${name2}: the uplink gave no answer`);
22039
+ throw new KernelError("ETIMEDOUT", failure);
21529
22040
  }
21530
- const addresses = answer2.addresses.filter((a) => this.usable(name2, a, family));
21531
- if (addresses.length > 0) this.remember(id2, addresses, answer2.ttl);
21532
- return addresses;
21533
22041
  }
21534
22042
  usable(name2, address2, family) {
21535
22043
  const v4 = ipv4Bytes(address2) !== void 0;
@@ -23090,7 +23598,7 @@ function bytesOf2(text) {
23090
23598
  for (let i = 0; i < text.length; i++) out[i] = text.charCodeAt(i);
23091
23599
  return out;
23092
23600
  }
23093
- async function writeAll3(conn, bytes) {
23601
+ async function writeAll4(conn, bytes) {
23094
23602
  for (let at = 0; at < bytes.length; ) at += await conn.write(bytes.subarray(at));
23095
23603
  }
23096
23604
  var CRLF2 = [13, 10, 13, 10];
@@ -23240,7 +23748,7 @@ var SessionWriter = class {
23240
23748
  const line = bytesOf2(`${out.line}\r
23241
23749
  `);
23242
23750
  try {
23243
- await writeAll3(this.conn, line);
23751
+ await writeAll4(this.conn, line);
23244
23752
  } finally {
23245
23753
  line.fill(0);
23246
23754
  }
@@ -23263,7 +23771,7 @@ var SessionWriter = class {
23263
23771
  const out = started ? await this.pgClient.message(message3).catch(() => ({ bytes: message3, done: true })) : { bytes: message3, done: false };
23264
23772
  if ("values" in out && out.values) this.remember(out.values);
23265
23773
  try {
23266
- await writeAll3(this.conn, out.bytes);
23774
+ await writeAll4(this.conn, out.bytes);
23267
23775
  } finally {
23268
23776
  out.bytes.fill(0);
23269
23777
  message3.fill(0);
@@ -23281,7 +23789,7 @@ var SessionWriter = class {
23281
23789
  const out = this.buffer;
23282
23790
  this.buffer = new Uint8Array(0);
23283
23791
  try {
23284
- await writeAll3(this.conn, out);
23792
+ await writeAll4(this.conn, out);
23285
23793
  } finally {
23286
23794
  out.fill(0);
23287
23795
  }
@@ -23297,8 +23805,8 @@ var SessionWriter = class {
23297
23805
  const head = done4 ? bytesOf2(done4.head) : all.subarray(0, end);
23298
23806
  if (done4) this.remember(done4.values);
23299
23807
  try {
23300
- await writeAll3(this.conn, head);
23301
- await writeAll3(this.conn, all.subarray(end));
23808
+ await writeAll4(this.conn, head);
23809
+ await writeAll4(this.conn, all.subarray(end));
23302
23810
  } finally {
23303
23811
  head.fill(0);
23304
23812
  all.fill(0);
@@ -29157,406 +29665,6 @@ function isMessage(data) {
29157
29665
  return adopt !== null && Array.isArray(adopt.records) && Array.isArray(adopt.flocks);
29158
29666
  }
29159
29667
 
29160
- // src/kernel/net/dns-stub.ts
29161
- var DNS_PORT = 53;
29162
- var DNS_STUB_ADDRESS = "127.0.0.53";
29163
- var RESOLV_CONF_PATH = "/etc/resolv.conf";
29164
- var RESOLV_CONF = `nameserver ${DNS_STUB_ADDRESS}
29165
- options edns0
29166
- `;
29167
- var DNS_STUB_TTL = 30;
29168
- var DNS_FORWARD_TIMEOUT_MS = 5e3;
29169
- var DNS_TCP_IDLE_MS = 1e4;
29170
- var DNS_TCP_MAX = 64;
29171
- var DNS_UDP_INFLIGHT = 64;
29172
- var TYPE_A = 1;
29173
- var TYPE_PTR = 12;
29174
- var TYPE_AAAA = 28;
29175
- var TYPE_OPT = 41;
29176
- var CLASS_IN = 1;
29177
- var NOERROR = 0;
29178
- var FORMERR = 1;
29179
- var SERVFAIL = 2;
29180
- var NXDOMAIN = 3;
29181
- var NOTIMP = 4;
29182
- var QR = 32768;
29183
- var AA = 1024;
29184
- var TC = 512;
29185
- var RD = 256;
29186
- var RA = 128;
29187
- var CD = 16;
29188
- var UDP_PLAIN = 512;
29189
- var UDP_EDNS = 1232;
29190
- var UDP_MAX = 4096;
29191
- var DOH_MAX = 65535;
29192
- var LOCAL_ZONES = ["localhost", "slicc.internal"];
29193
- var MESSAGE = "application/dns-message";
29194
- var u16 = (b, at) => b[at] << 8 | b[at + 1];
29195
- function skipName(b, at) {
29196
- let i = at;
29197
- for (let hops = 0; hops < 128; hops++) {
29198
- if (i >= b.length) return void 0;
29199
- const len = b[i];
29200
- if (len === 0) return i + 1;
29201
- if ((len & 192) === 192) return i + 2 <= b.length ? i + 2 : void 0;
29202
- if (len & 192) return void 0;
29203
- i += len + 1;
29204
- }
29205
- return void 0;
29206
- }
29207
- function readName(b, at) {
29208
- const labels = [];
29209
- let i = at;
29210
- let size = 0;
29211
- for (; ; ) {
29212
- if (i >= b.length) return void 0;
29213
- const len = b[i];
29214
- if (len === 0) break;
29215
- if (len & 192) return void 0;
29216
- size += len + 1;
29217
- if (size > 254 || i + 1 + len > b.length) return void 0;
29218
- labels.push(new TextDecoder().decode(b.subarray(i + 1, i + 1 + len)));
29219
- i += len + 1;
29220
- }
29221
- return { name: labels.join(".").toLowerCase(), end: i + 1 };
29222
- }
29223
- function parseQuery(b) {
29224
- if (b.length < 12) return void 0;
29225
- const id2 = u16(b, 0);
29226
- const flags2 = u16(b, 2);
29227
- if (flags2 & QR) return void 0;
29228
- if (flags2 >> 11 & 15) return { ok: false, id: id2, flags: flags2, rcode: NOTIMP };
29229
- if (u16(b, 4) !== 1) return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
29230
- const read = readName(b, 12);
29231
- if (!read || read.end + 4 > b.length) return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
29232
- const type = u16(b, read.end);
29233
- const klass = u16(b, read.end + 2);
29234
- const end = read.end + 4;
29235
- let edns;
29236
- let at = end;
29237
- const records = u16(b, 6) + u16(b, 8) + u16(b, 10);
29238
- for (let n = 0; n < records; n++) {
29239
- const after = skipName(b, at);
29240
- if (after === void 0 || after + 10 > b.length)
29241
- return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
29242
- if (u16(b, after) === TYPE_OPT) edns = Math.max(UDP_PLAIN, u16(b, after + 2));
29243
- at = after + 10 + u16(b, after + 8);
29244
- if (at > b.length) return { ok: false, id: id2, flags: flags2, rcode: FORMERR };
29245
- }
29246
- return { ok: true, question: { id: id2, flags: flags2, name: read.name, type, klass, end, edns } };
29247
- }
29248
- function encodeName(name2) {
29249
- const out = [];
29250
- for (const label of name2.split(".").filter(Boolean)) {
29251
- const bytes = new TextEncoder().encode(label);
29252
- out.push(bytes.length, ...bytes);
29253
- }
29254
- out.push(0);
29255
- return out;
29256
- }
29257
- function header2(id2, flags2, counts) {
29258
- const out = [id2 >> 8, id2 & 255, flags2 >> 8, flags2 & 255];
29259
- for (const c of counts) out.push(c >> 8, c & 255);
29260
- return out;
29261
- }
29262
- function opt() {
29263
- return [0, 0, TYPE_OPT, UDP_EDNS >> 8, UDP_EDNS & 255, 0, 0, 0, 0, 0, 0];
29264
- }
29265
- function record2(type, rdata) {
29266
- const ttl = DNS_STUB_TTL;
29267
- return [
29268
- 192,
29269
- 12,
29270
- type >> 8,
29271
- type & 255,
29272
- 0,
29273
- CLASS_IN,
29274
- ttl >>> 24 & 255,
29275
- ttl >>> 16 & 255,
29276
- ttl >>> 8 & 255,
29277
- ttl & 255,
29278
- rdata.length >> 8,
29279
- rdata.length & 255,
29280
- ...rdata
29281
- ];
29282
- }
29283
- function replyFlags(query, rcode, aa) {
29284
- return QR | query & 30720 | (aa ? AA : 0) | query & RD | RA | query & CD | rcode;
29285
- }
29286
- function answers(reply, q) {
29287
- if (reply.length < 12 || !(u16(reply, 2) & QR) || u16(reply, 4) !== 1) return false;
29288
- const read = readName(reply, 12);
29289
- if (!read || read.end + 4 > reply.length) return false;
29290
- return read.name === q.name && u16(reply, read.end) === q.type && u16(reply, read.end + 2) === q.klass;
29291
- }
29292
- function errorReply2(id2, flags2, rcode) {
29293
- return new Uint8Array(header2(id2, replyFlags(flags2, rcode, false), [0, 0, 0, 0]));
29294
- }
29295
- function answerReply(query, q, rcode, answers2, aa) {
29296
- const extra = q.edns !== void 0 ? [opt()] : [];
29297
- return new Uint8Array([
29298
- ...header2(q.id, replyFlags(q.flags, rcode, aa), [1, answers2.length, 0, extra.length]),
29299
- ...query.subarray(12, q.end),
29300
- ...answers2.flat(),
29301
- ...extra.flat()
29302
- ]);
29303
- }
29304
- function truncated(reply, q, query) {
29305
- const limit = q.edns !== void 0 ? Math.min(q.edns, UDP_MAX) : UDP_PLAIN;
29306
- if (reply.length <= limit) return reply;
29307
- const flags2 = u16(reply, 2) | TC;
29308
- const extra = q.edns !== void 0 ? [opt()] : [];
29309
- return new Uint8Array([
29310
- ...header2(q.id, flags2, [1, 0, 0, extra.length]),
29311
- ...query.subarray(12, q.end),
29312
- ...extra.flat()
29313
- ]);
29314
- }
29315
- function ipv6Text(groups) {
29316
- return groups.map((g) => g.toString(16)).join(":");
29317
- }
29318
- function reverseAddress(name2) {
29319
- if (name2.endsWith(".in-addr.arpa")) {
29320
- const parts2 = name2.slice(0, -".in-addr.arpa".length).split(".");
29321
- if (parts2.length !== 4) return void 0;
29322
- const text = parts2.reverse().join(".");
29323
- return ipv4Bytes(text) ? text.split(".").map(Number).join(".") : void 0;
29324
- }
29325
- if (name2.endsWith(".ip6.arpa")) {
29326
- const nibbles = name2.slice(0, -".ip6.arpa".length).split(".");
29327
- if (nibbles.length !== 32 || !nibbles.every((n) => /^[0-9a-f]$/.test(n))) return void 0;
29328
- const hex4 = nibbles.reverse().join("");
29329
- const groups = Array.from(
29330
- { length: 8 },
29331
- (_, i) => Number.parseInt(hex4.slice(i * 4, i * 4 + 4), 16)
29332
- );
29333
- return ipv6Text(groups);
29334
- }
29335
- return void 0;
29336
- }
29337
- function local(name2) {
29338
- return LOCAL_ZONES.some((zone) => name2 === zone || name2.endsWith(`.${zone}`));
29339
- }
29340
- function literal2(name2) {
29341
- return ipv4Bytes(name2) !== void 0 || ipv6Groups(name2) !== void 0;
29342
- }
29343
- function addressData(address2, type) {
29344
- if (type === TYPE_A) return ipv4Bytes(address2);
29345
- const groups = type === TYPE_AAAA ? ipv6Groups(address2) : void 0;
29346
- return groups?.flatMap((g) => [g >> 8, g & 255]);
29347
- }
29348
- var DnsStub = class {
29349
- resolver;
29350
- forwarder;
29351
- timeoutMs;
29352
- constructor(options) {
29353
- this.resolver = options.resolver;
29354
- this.forwarder = options.forward;
29355
- this.timeoutMs = options.timeoutMs ?? DNS_FORWARD_TIMEOUT_MS;
29356
- }
29357
- async answer(query, udp) {
29358
- const parsed3 = parseQuery(query);
29359
- if (!parsed3) return void 0;
29360
- if (!parsed3.ok) return errorReply2(parsed3.id, parsed3.flags, parsed3.rcode);
29361
- const q = parsed3.question;
29362
- const reply = await this.reply(query, q).catch(
29363
- () => answerReply(query, q, SERVFAIL, [], false)
29364
- );
29365
- return udp ? truncated(reply, q, query) : reply;
29366
- }
29367
- async reply(query, q) {
29368
- if (q.klass !== CLASS_IN || literal2(q.name)) return this.forward(query, q);
29369
- if (q.type === TYPE_PTR) {
29370
- const address2 = reverseAddress(q.name);
29371
- const name2 = address2 !== void 0 ? await this.reverse(address2) : void 0;
29372
- if (name2 !== void 0)
29373
- return answerReply(query, q, NOERROR, [record2(TYPE_PTR, encodeName(name2))], true);
29374
- return this.forward(query, q);
29375
- }
29376
- if (q.type === TYPE_A || q.type === TYPE_AAAA) {
29377
- const addresses = q.name === "" ? [] : await this.lookup(q.name);
29378
- const answers2 = addresses.flatMap((a) => {
29379
- const data = addressData(a, q.type);
29380
- return data ? [record2(q.type, data)] : [];
29381
- });
29382
- if (answers2.length > 0 || local(q.name) || await this.known(q.name)) {
29383
- const rcode = addresses.length > 0 ? NOERROR : NXDOMAIN;
29384
- return answerReply(query, q, rcode, answers2, local(q.name));
29385
- }
29386
- return this.forward(query, q);
29387
- }
29388
- if (await this.known(q.name)) return answerReply(query, q, NOERROR, [], true);
29389
- if (local(q.name)) {
29390
- const there = (await this.lookup(q.name)).length > 0;
29391
- return answerReply(query, q, there ? NOERROR : NXDOMAIN, [], true);
29392
- }
29393
- return this.forward(query, q);
29394
- }
29395
- lookup(name2) {
29396
- return this.resolver.resolve(name2, 0).catch(() => []);
29397
- }
29398
- known(name2) {
29399
- return this.resolver.known(name2).catch(() => false);
29400
- }
29401
- reverse(address2) {
29402
- return this.resolver.reverse(address2).catch(() => void 0);
29403
- }
29404
- async forward(query, q) {
29405
- if (!this.forwarder) return answerReply(query, q, SERVFAIL, [], false);
29406
- const asked = query.slice();
29407
- asked[0] = 0;
29408
- asked[1] = 0;
29409
- const got = await this.forwarder(asked, AbortSignal.timeout(this.timeoutMs)).catch(
29410
- () => void 0
29411
- );
29412
- if (!got || !answers(got, q)) return answerReply(query, q, SERVFAIL, [], false);
29413
- const reply = got.slice();
29414
- reply[0] = q.id >> 8;
29415
- reply[1] = q.id & 255;
29416
- return reply;
29417
- }
29418
- };
29419
- async function collect(body, max) {
29420
- const parts2 = [];
29421
- let size = 0;
29422
- for await (const part of body) {
29423
- size += part.length;
29424
- if (size > max) return void 0;
29425
- parts2.push(part);
29426
- }
29427
- const out = new Uint8Array(size);
29428
- let at = 0;
29429
- for (const part of parts2) {
29430
- out.set(part, at);
29431
- at += part.length;
29432
- }
29433
- return out;
29434
- }
29435
- function dohForward(transport, url) {
29436
- return async (query, signal) => {
29437
- const res = await transport.fetch({
29438
- url,
29439
- method: "POST",
29440
- headers: [
29441
- ["content-type", MESSAGE],
29442
- ["accept", MESSAGE]
29443
- ],
29444
- body: query,
29445
- signal
29446
- });
29447
- const type = res.headers.find(([k]) => k.toLowerCase() === "content-type")?.[1] ?? "";
29448
- if (res.status !== 200 || type.split(";")[0].trim().toLowerCase() !== MESSAGE) {
29449
- await res.cancel().catch(() => void 0);
29450
- return void 0;
29451
- }
29452
- return collect(res.body, DOH_MAX);
29453
- };
29454
- }
29455
- async function writeAll4(conn, bytes) {
29456
- for (let at = 0; at < bytes.length; ) {
29457
- const n = await conn.write(bytes.subarray(at));
29458
- if (!(n > 0)) throw new Error("the uplink wrote nothing");
29459
- at += n;
29460
- }
29461
- }
29462
- function uplinkForward(uplink, server) {
29463
- return async (query, signal) => {
29464
- const conn = await uplink.dial({ network: "tcp", host: server, port: DNS_PORT, signal });
29465
- const stop = () => conn.close();
29466
- signal.addEventListener("abort", stop, { once: true });
29467
- try {
29468
- await writeAll4(conn, new Uint8Array([query.length >> 8, query.length & 255, ...query]));
29469
- let got = new Uint8Array();
29470
- for (; ; ) {
29471
- if (got.length >= 2 && got.length >= 2 + u16(got, 0)) return got.slice(2, 2 + u16(got, 0));
29472
- const piece = await conn.read();
29473
- if (!piece) return void 0;
29474
- const next3 = new Uint8Array(got.length + piece.length);
29475
- next3.set(got);
29476
- next3.set(piece, got.length);
29477
- got = next3;
29478
- }
29479
- } finally {
29480
- signal.removeEventListener("abort", stop);
29481
- conn.close();
29482
- }
29483
- };
29484
- }
29485
- function dnsForward(uplink, transport, doh) {
29486
- const server = uplink?.nameserver;
29487
- if (uplink && server && (ipv4Bytes(server) || ipv6Groups(server)))
29488
- return uplinkForward(uplink, server);
29489
- return typeof doh === "string" ? dohForward(transport, doh) : void 0;
29490
- }
29491
- function kernelDnsStub(resolver, transport, uplink, doh) {
29492
- return new DnsStub({ resolver, forward: dnsForward(uplink, transport, doh) });
29493
- }
29494
- async function readExactly(conn, size, signal) {
29495
- const out = new Uint8Array(size);
29496
- let at = 0;
29497
- while (at < size) {
29498
- const piece = await conn.read(size - at, signal);
29499
- if (piece.length === 0) return void 0;
29500
- out.set(piece, at);
29501
- at += piece.length;
29502
- }
29503
- return out;
29504
- }
29505
- async function serveTcp(stub, conn) {
29506
- try {
29507
- for (; ; ) {
29508
- const idle2 = AbortSignal.timeout(DNS_TCP_IDLE_MS);
29509
- const size = await readExactly(conn, 2, idle2);
29510
- if (!size) return;
29511
- const query = await readExactly(conn, u16(size, 0), idle2);
29512
- if (!query) return;
29513
- const reply = await stub.answer(query, false);
29514
- if (!reply) return;
29515
- const framed = new Uint8Array([reply.length >> 8, reply.length & 255, ...reply]);
29516
- const sent = await conn.write(framed, AbortSignal.timeout(DNS_TCP_IDLE_MS));
29517
- if (sent < framed.length) return;
29518
- }
29519
- } catch {
29520
- return;
29521
- } finally {
29522
- conn.close();
29523
- }
29524
- }
29525
- function enableDnsStub(net, stub) {
29526
- const at = { family: "inet", host: "127.0.0.1", port: DNS_PORT };
29527
- net.reserve(DNS_PORT);
29528
- net.kernelOnly(DNS_PORT, true);
29529
- net.activate(at, () => {
29530
- const listener = net.listen(at);
29531
- const open = /* @__PURE__ */ new Set();
29532
- void (async () => {
29533
- for (; ; ) {
29534
- const conn = await listener.accept().catch(() => void 0);
29535
- if (!conn) return;
29536
- if (open.size >= DNS_TCP_MAX) {
29537
- conn.close();
29538
- continue;
29539
- }
29540
- open.add(conn);
29541
- void serveTcp(stub, conn).finally(() => open.delete(conn));
29542
- }
29543
- })();
29544
- });
29545
- let inflight = 0;
29546
- net.serveDgram(DNS_PORT, (datagram2, to) => {
29547
- if (inflight >= DNS_UDP_INFLIGHT) return;
29548
- inflight++;
29549
- void stub.answer(datagram2.bytes, true).then(
29550
- (reply) => {
29551
- if (!reply) return;
29552
- const from = { family: "inet", host: to.host, port: DNS_PORT };
29553
- net.deliverDgram(datagram2.from.port, { bytes: reply, from });
29554
- },
29555
- () => void 0
29556
- ).finally(() => inflight--);
29557
- });
29558
- }
29559
-
29560
29668
  // src/kernel/net/remote-transport.ts
29561
29669
  var TransportError = class extends Error {
29562
29670
  status;
@@ -34378,7 +34486,7 @@ function tmpfs() {
34378
34486
  // src/process/wasi/wasi-module.ts
34379
34487
  function importedMemory(bytes) {
34380
34488
  let at = 8;
34381
- const u32 = () => {
34489
+ const u322 = () => {
34382
34490
  let result = 0;
34383
34491
  let shift = 0;
34384
34492
  for (; ; ) {
@@ -34389,31 +34497,31 @@ function importedMemory(bytes) {
34389
34497
  }
34390
34498
  };
34391
34499
  const name2 = () => {
34392
- const len = u32();
34500
+ const len = u322();
34393
34501
  const s = new TextDecoder().decode(bytes.subarray(at, at + len));
34394
34502
  at += len;
34395
34503
  return s;
34396
34504
  };
34397
34505
  const limits = () => {
34398
34506
  const flags2 = bytes[at++];
34399
- const initial3 = u32();
34400
- const maximum = flags2 & 1 ? u32() : void 0;
34507
+ const initial3 = u322();
34508
+ const maximum = flags2 & 1 ? u322() : void 0;
34401
34509
  return { initial: initial3, maximum, shared: (flags2 & 2) !== 0 };
34402
34510
  };
34403
34511
  while (at < bytes.length) {
34404
34512
  const id2 = bytes[at++];
34405
- const size = u32();
34513
+ const size = u322();
34406
34514
  const end = at + size;
34407
34515
  if (id2 !== 2) {
34408
34516
  at = end;
34409
34517
  continue;
34410
34518
  }
34411
- const count2 = u32();
34519
+ const count2 = u322();
34412
34520
  for (let i = 0; i < count2; i++) {
34413
34521
  const module = name2();
34414
34522
  const field4 = name2();
34415
34523
  const kind = bytes[at++];
34416
- if (kind === 0) u32();
34524
+ if (kind === 0) u322();
34417
34525
  else if (kind === 1) {
34418
34526
  at++;
34419
34527
  limits();
@@ -34423,7 +34531,7 @@ function importedMemory(bytes) {
34423
34531
  } else if (kind === 3) at += 2;
34424
34532
  else if (kind === 4) {
34425
34533
  at++;
34426
- u32();
34534
+ u322();
34427
34535
  }
34428
34536
  }
34429
34537
  return void 0;
@@ -35361,8 +35469,10 @@ var Launcher = class {
35361
35469
  this.uplink = options.uplink;
35362
35470
  this.routes = new Routes(options.uplink?.traits.ipv6 === true);
35363
35471
  if (options.uplink?.routes) this.routes.set(options.uplink.routes);
35472
+ const forward = dnsForward(options.uplink, this.transport, options.doh);
35364
35473
  this.resolver = new Resolver({
35365
35474
  uplink: options.uplink,
35475
+ forward,
35366
35476
  routes: this.routes,
35367
35477
  hostname,
35368
35478
  hosts: hostsReader(this.base)
@@ -35374,10 +35484,7 @@ var Launcher = class {
35374
35484
  roots: packageTlsRoots(this.verifiedFs(options.fs), this.modulesDir),
35375
35485
  ca: this.ca
35376
35486
  });
35377
- enableDnsStub(
35378
- this.net,
35379
- kernelDnsStub(this.resolver, this.transport, options.uplink, options.doh)
35380
- );
35487
+ enableDnsStub(this.net, new DnsStub({ resolver: this.resolver, forward }));
35381
35488
  enableCdp(this.net, this.cdp);
35382
35489
  this.facades.enable(this.net);
35383
35490
  const fs = this.commandFs(this.base);
@@ -36193,7 +36300,7 @@ var Launcher = class {
36193
36300
  const foreign = foreignImports(bytes);
36194
36301
  const libc = libcVersionOf(bytes);
36195
36302
  const named = customSectionsOf(bytes, "name").length > 0;
36196
- const compiled = this.compile ? await this.compile(bytes, key) : await WebAssembly.compile(bytes);
36303
+ const compiled = this.compile ? await this.compile(bytes, key, this.sourceOf(path)) : await WebAssembly.compile(bytes);
36197
36304
  if (libc !== void 0) this.libcs.set(compiled, libc);
36198
36305
  return {
36199
36306
  value: { module: compiled, ...memory ? { memory } : {}, foreign, named },
@@ -36305,10 +36412,20 @@ var Launcher = class {
36305
36412
  );
36306
36413
  }
36307
36414
  reread(target, read) {
36415
+ this.forgetCompiled(target.wasm);
36416
+ return read();
36417
+ }
36418
+ forgetCompiled(path) {
36308
36419
  for (const key of this.compiled.keys()) {
36309
- if (key.startsWith(`${target.wasm}:`)) this.compiled.delete(key);
36420
+ if (key.startsWith(`${path}:`) || key.startsWith(`${path}\0`)) this.compiled.delete(key);
36310
36421
  }
36311
- return read();
36422
+ }
36423
+ sourceOf(path) {
36424
+ return {
36425
+ path,
36426
+ read: () => this.base.readFileBuffer(path),
36427
+ stale: () => this.forgetCompiled(path)
36428
+ };
36312
36429
  }
36313
36430
  exeId(target) {
36314
36431
  if (target.abi !== "wasi" || (target.copy ?? target.glue) !== target.wasm) {
@@ -39380,7 +39497,7 @@ function connectRefusal(address2, port, place) {
39380
39497
  const why = place === "link-local" ? `nodeTransport refuses link-local ${where}` : `nodeTransport refuses the host's loopback ${where} (see nodeTransport({ hostLoopback }))`;
39381
39498
  return Object.assign(new Error(why), { status: 403, code: "EACCES" });
39382
39499
  }
39383
- async function addressesOf(host, resolve2) {
39500
+ async function addressesOf2(host, resolve2) {
39384
39501
  const plain3 = host.replace(/^\[(.*)\]$/, "$1");
39385
39502
  if (isIP3(plain3)) return [plain3];
39386
39503
  if (loopbackName2(plain3)) return ["127.0.0.1"];
@@ -39388,7 +39505,7 @@ async function addressesOf(host, resolve2) {
39388
39505
  }
39389
39506
  async function checkDestination(target, policy, resolve2 = resolveHost) {
39390
39507
  const url = new URL(target);
39391
- for (const address2 of await addressesOf(url.hostname, resolve2)) {
39508
+ for (const address2 of await addressesOf2(url.hostname, resolve2)) {
39392
39509
  const place = placeOf(address2);
39393
39510
  if (!place) continue;
39394
39511
  if (place === "loopback" && allows(policy, portOf2(url))) continue;