@ai-ecoverse/slicc-kernel 1.115.0 → 1.116.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.
package/dist/node.js CHANGED
@@ -1865,6 +1865,14 @@ var LoopbackNet = class {
1865
1865
  facades = /* @__PURE__ */ new Map();
1866
1866
  nextEphemeral = EPHEMERAL_FIRST;
1867
1867
  route;
1868
+ origins;
1869
+ useOrigins(serve) {
1870
+ this.origins = serve;
1871
+ }
1872
+ serveOrigin(conn, origin) {
1873
+ if (!this.origins) throw new KernelError("ENETUNREACH");
1874
+ void this.origins(conn, origin).catch(() => conn.close());
1875
+ }
1868
1876
  useUplink(route) {
1869
1877
  this.route = route;
1870
1878
  }
@@ -5650,6 +5658,12 @@ var Incoming = class {
5650
5658
  this.buf = this.buf.subarray(n);
5651
5659
  return out;
5652
5660
  }
5661
+ async firstLine(max, signal) {
5662
+ while (this.buf.length < max && !this.buf.includes(10) && await this.fill(signal)) ;
5663
+ const end = this.buf.indexOf(10);
5664
+ const bytes = this.buf.subarray(0, end >= 0 ? end + 1 : Math.min(max, this.buf.length));
5665
+ return String.fromCharCode(...bytes);
5666
+ }
5653
5667
  async peek(signal) {
5654
5668
  if (this.buf.length === 0) await this.fill(signal);
5655
5669
  return this.buf[0];
@@ -6545,7 +6559,7 @@ function satisfies(version, range2) {
6545
6559
  }
6546
6560
 
6547
6561
  // src/version.ts
6548
- var KERNEL_VERSION = true ? "1.115.0" : "0.0.0-dev";
6562
+ var KERNEL_VERSION = true ? "1.116.0" : "0.0.0-dev";
6549
6563
  var OS_RELEASE = "6.0.0-slicc";
6550
6564
  function procVersion() {
6551
6565
  return `Linux version ${OS_RELEASE} (slicc-kernel ${KERNEL_VERSION}) #1 SMP PREEMPT_DYNAMIC
@@ -7683,6 +7697,9 @@ var TLS_HANDSHAKE = 22;
7683
7697
  var TOKEN2 = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
7684
7698
  var NO_WEBSOCKET2 = "WebSocket not supported by this transport (fetchTransport needs { webSocket: true })";
7685
7699
  var REALM_PROXY_PORT = 3128;
7700
+ var REQUEST_LINE_MAX = 8192;
7701
+ var REQUEST_LINE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+ \S+ HTTP\/1\.[01]\r?\n$/;
7702
+ var NOT_HTTP = "not HTTP/1.1: TLS through the transport carries only HTTP/1.1 and WebSocket";
7686
7703
  var DEFAULT_LIMITS = {
7687
7704
  maxConnections: 64,
7688
7705
  maxHead: 64 * 1024,
@@ -7971,6 +7988,26 @@ var RealmProxy = class {
7971
7988
  conn.close();
7972
7989
  }
7973
7990
  }
7991
+ async serveOrigin(conn, origin) {
7992
+ if (this.stop.signal.aborted) {
7993
+ conn.close();
7994
+ return;
7995
+ }
7996
+ this.connections.add(conn);
7997
+ const idle = { ms: this.limits.idleMs };
7998
+ const incoming = new Incoming(timedSource(conn, idle, this.stop.signal));
7999
+ try {
8000
+ const line = await incoming.firstLine(REQUEST_LINE_MAX).catch(() => "");
8001
+ if (line !== "" && !REQUEST_LINE.test(line)) {
8002
+ await this.refuse(conn, new HttpError(400, NOT_HTTP));
8003
+ return;
8004
+ }
8005
+ await this.requests({ sink: conn, incoming, origin, socket: conn, idle });
8006
+ } finally {
8007
+ this.connections.delete(conn);
8008
+ conn.close();
8009
+ }
8010
+ }
7974
8011
  async requests(ctx) {
7975
8012
  try {
7976
8013
  for (; ; ) {
@@ -14421,7 +14458,7 @@ async function writeCaFile(fs, ca, locks = globalThis.navigator?.locks) {
14421
14458
  function enableNetwork(net, options) {
14422
14459
  let proxy;
14423
14460
  const transport = options.transport ?? missingTransport();
14424
- net.activate({ family: "inet", host: "127.0.0.1", port: REALM_PROXY_PORT }, () => {
14461
+ const start2 = () => {
14425
14462
  const tls2 = new TlsTerminator({
14426
14463
  ca: options.ca,
14427
14464
  engine: options.engine,
@@ -14440,6 +14477,11 @@ function enableNetwork(net, options) {
14440
14477
  if (proxy === started) proxy = void 0;
14441
14478
  await tls2.close();
14442
14479
  });
14480
+ };
14481
+ net.activate({ family: "inet", host: "127.0.0.1", port: REALM_PROXY_PORT }, start2);
14482
+ net.useOrigins((conn, origin) => {
14483
+ if (!proxy) start2();
14484
+ return proxy.serveOrigin(conn, origin);
14443
14485
  });
14444
14486
  return () => proxy;
14445
14487
  }
@@ -16245,7 +16287,8 @@ var SOCKET_OPS = [
16245
16287
  "sock-setopt",
16246
16288
  "sock-resolve",
16247
16289
  "sock-sendto",
16248
- "sock-recvfrom"
16290
+ "sock-recvfrom",
16291
+ "sock-tls"
16249
16292
  ];
16250
16293
  function socketAt(fds, fd) {
16251
16294
  const file = anySocketAt(fds, fd);
@@ -16317,6 +16360,8 @@ async function socketSyscall(req, proc) {
16317
16360
  if (addresses.length === 0) throw new KernelError("ENOENT");
16318
16361
  return ok3(addresses);
16319
16362
  }
16363
+ case "sock-tls":
16364
+ return ok3(tlsOverTransport(req, proc));
16320
16365
  default:
16321
16366
  return datagram(req, proc);
16322
16367
  }
@@ -16420,6 +16465,48 @@ async function accept(proc, fd, nonblock) {
16420
16465
  const socket = await listener.accept(waits ? proc.blocking() : void 0, nonblock);
16421
16466
  return { fd: proc.fds.install(new OpenFile(socket), 3), peer: socket.peer };
16422
16467
  }
16468
+ var NOT_HTTP_PORTS = /* @__PURE__ */ new Set([
16469
+ 465,
16470
+ 563,
16471
+ 585,
16472
+ 636,
16473
+ 853,
16474
+ 989,
16475
+ 990,
16476
+ 992,
16477
+ 993,
16478
+ 994,
16479
+ 995,
16480
+ 5061,
16481
+ 5671,
16482
+ 6697,
16483
+ 8883
16484
+ ]);
16485
+ var HOST_NAME = /^[A-Za-z0-9.-]+$|^\[?[0-9A-Fa-f:.]+\]?$/;
16486
+ function tlsOverTransport(req, proc) {
16487
+ if (req.ca) throw new KernelError("ENOTSUP");
16488
+ if (req.alpn && req.alpn.length > 0 && !req.alpn.includes("http/1.1")) {
16489
+ throw new KernelError("EPROTONOSUPPORT");
16490
+ }
16491
+ if (!Number.isInteger(req.port) || req.port < 1 || req.port > 65535)
16492
+ throw new KernelError("EINVAL");
16493
+ if (NOT_HTTP_PORTS.has(req.port)) throw new KernelError("EPROTONOSUPPORT");
16494
+ const servername = req.servername || req.host;
16495
+ if (!HOST_NAME.test(servername)) throw new KernelError("EINVAL");
16496
+ if (isLoopbackHost(servername) || isLoopbackHost(req.host)) throw new KernelError("ENOTSUP");
16497
+ const host = servername.includes(":") && !servername.startsWith("[") ? `[${servername}]` : servername;
16498
+ const origin = req.port === 443 ? `https://${host}` : `https://${host}:${req.port}`;
16499
+ const [mine, theirs] = KernelSocket.pair(proc.net, "inet", proc.cred?.());
16500
+ mine.peer = { family: "inet", host: req.host, port: req.port };
16501
+ try {
16502
+ proc.net.serveOrigin(theirs, origin);
16503
+ } catch (err) {
16504
+ mine.close();
16505
+ theirs.close();
16506
+ throw err;
16507
+ }
16508
+ return { fd: proc.fds.install(new OpenFile(mine), 3), alpnProtocol: "http/1.1", servername };
16509
+ }
16423
16510
 
16424
16511
  // src/kernel/vfs-file.ts
16425
16512
  var O_ACCMODE = 3;