@ai-ecoverse/slicc-kernel 1.41.4 → 1.42.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.
@@ -69,6 +69,11 @@ var init_loopback_names = __esm({
69
69
  });
70
70
 
71
71
  // src/kernel/signals.ts
72
+ function defaultAction(sig) {
73
+ if (sig === SIG.CONT) return "continue";
74
+ if (STOPPING.has(sig)) return "stop";
75
+ return IGNORED_BY_DEFAULT.has(sig) ? "ignore" : "terminate";
76
+ }
72
77
  function sigbit(sig) {
73
78
  return 1 << sig;
74
79
  }
@@ -417,6 +422,9 @@ function wasiErrnoOf(code2) {
417
422
  if (name === "NOMEDIUM") return E.NODEV;
418
423
  return E[name] ?? E.IO;
419
424
  }
425
+ function wasixSignal(sig) {
426
+ return Number.isInteger(sig) && sig >= 1 && sig <= 31 ? sig : void 0;
427
+ }
420
428
  var E, FILETYPE, RIGHTS, OFLAGS, FDFLAGS, LOOKUP_SYMLINK_FOLLOW, WHENCE, CLOCK, FSTFLAGS, EVENTTYPE, SUBCLOCK_ABSTIME, EVENT_FD_READWRITE_HANGUP, RIFLAGS, SDFLAGS, PREOPENTYPE_DIR, SIZE, WASI_SIGNAL_TO_POSIX;
421
429
  var init_wasi_abi = __esm({
422
430
  "src/process/wasi/wasi-abi.ts"() {
@@ -1454,7 +1462,7 @@ var init_sync_sab_wire = __esm({
1454
1462
  });
1455
1463
 
1456
1464
  // src/realm/sync-sab-bridge.ts
1457
- function createSyncSabTransport(sab, port2, deps = {}) {
1465
+ function createSyncSabTransport(sab, port3, deps = {}) {
1458
1466
  const views = sabViews(sab);
1459
1467
  const { header, window } = views;
1460
1468
  const wait = deps.wait ?? ((a, i, v, t) => Atomics.wait(a, i, v, t));
@@ -1485,8 +1493,8 @@ function createSyncSabTransport(sab, port2, deps = {}) {
1485
1493
  Atomics.store(header, SAB_I_STATE, SAB_STATE_PENDING);
1486
1494
  const msg = offset === 0 ? { type: SYNC_SAB_REQ_MSG, id, req } : { type: SYNC_SAB_NEXT_MSG, id, offset };
1487
1495
  try {
1488
- if (offset === 0 && transfer.length > 0) port2.postMessage(msg, transfer);
1489
- else port2.postMessage(msg);
1496
+ if (offset === 0 && transfer.length > 0) port3.postMessage(msg, transfer);
1497
+ else port3.postMessage(msg);
1490
1498
  } catch {
1491
1499
  throw syncError("EIO", label);
1492
1500
  }
@@ -2727,11 +2735,13 @@ function vfsPromoter(Fs, sys, streams, livePath) {
2727
2735
  const ranged = stream.node.live?.ranged;
2728
2736
  const version = ranged?.pinnedVersion;
2729
2737
  const pin = ranged && version !== void 0 ? { version, size: ranged.size() } : void 0;
2738
+ const handle = stream.node.live?.handle;
2739
+ const held = { ...pin ? { pin } : {}, ...handle !== void 0 ? { handle } : {} };
2730
2740
  kfd = sys.openVfs(
2731
2741
  livePath(stream),
2732
2742
  stream.flags,
2733
2743
  stream.position,
2734
- contents !== void 0 ? { contents, orphan: true } : pin ? { pin } : void 0
2744
+ contents !== void 0 ? { contents, orphan: true } : Object.keys(held).length > 0 ? held : void 0
2735
2745
  );
2736
2746
  promoted.set(stream.shared, kfd);
2737
2747
  }
@@ -3800,7 +3810,8 @@ function kernelSys(transport) {
3800
3810
  ...opts?.orphan ? { orphan: true } : {},
3801
3811
  ...opts?.truncate ? { truncate: true } : {},
3802
3812
  ...opts?.create ? { create: true } : {},
3803
- ...opts?.pin ? { pin: opts.pin } : {}
3813
+ ...opts?.pin ? { pin: opts.pin } : {},
3814
+ ...opts?.handle !== void 0 ? { handle: opts.handle } : {}
3804
3815
  },
3805
3816
  `fd-open-vfs ${path}`
3806
3817
  )
@@ -3833,6 +3844,9 @@ function kernelSys(transport) {
3833
3844
  heldMeta(fd) {
3834
3845
  return json(call2({ op: "fd-info", fd }, `fd-info ${fd}`)).meta;
3835
3846
  },
3847
+ access(fd) {
3848
+ return json(call2({ op: "fd-info", fd }, `fd-info ${fd}`)).access;
3849
+ },
3836
3850
  size(fd) {
3837
3851
  return json(call2({ op: "fd-vfs-stat", fd }, `fd-vfs-stat ${fd}`)).size;
3838
3852
  },
@@ -3893,8 +3907,7 @@ function wireKernelFd(Fs, streams, entry) {
3893
3907
  });
3894
3908
  }
3895
3909
  function openDevice(Fs, fd, meta) {
3896
- const flags = meta.access === "read" ? O_RDONLY : meta.access === "write" ? O_WRONLY3 : O_RDWR3;
3897
- return openAt(Fs, fd, `/dev/${meta.device}`, flags);
3910
+ return openAt(Fs, fd, `/dev/${meta.device}`, accessMode(meta.access));
3898
3911
  }
3899
3912
  function openHeld(Fs, fd, meta) {
3900
3913
  return "dir" in meta ? openAt(Fs, fd, meta.dir, O_RDONLY | O_DIRECTORY) : openDevice(Fs, fd, meta);
@@ -3920,7 +3933,7 @@ function wireKernelStdio(Fs, streams, sys) {
3920
3933
  const meta = kind === "device" || kind === "held" ? sys?.heldMeta?.(fd) : void 0;
3921
3934
  if (meta && openHeld(Fs, fd, meta)) continue;
3922
3935
  if (kind && PLACED.has(kind)) {
3923
- const { flags } = stream;
3936
+ const flags = sys?.access ? stream.flags & ~O_ACCMODE3 | accessMode(sys.access(fd)) : stream.flags;
3924
3937
  Fs.closeStream(fd);
3925
3938
  const kernel = { fd, kernel: fd, kind, flags };
3926
3939
  placeKernelStream(Fs, streams, kernel).flags = flags;
@@ -3962,9 +3975,9 @@ function reportGrowth(imports, publish) {
3962
3975
  return grown;
3963
3976
  };
3964
3977
  }
3965
- async function runWasmProcess(init, port2, deps = {}) {
3978
+ async function runWasmProcess(init, port3, deps = {}) {
3966
3979
  const signals = new SignalGate(
3967
- createSyncSabTransport(init.sab, port2, {
3980
+ createSyncSabTransport(init.sab, port3, {
3968
3981
  memory: () => memory?.buffer.byteLength ?? 0
3969
3982
  }),
3970
3983
  new Int32Array(init.sab, 0, SAB_HEADER_I32),
@@ -4170,7 +4183,7 @@ function runMain(running, init) {
4170
4183
  return status2;
4171
4184
  }
4172
4185
  }
4173
- var FS_OPS, O_RDONLY, O_WRONLY3, O_RDWR3, O_DIRECTORY, PLACED, GLUE_TRAILER, evaluateGlue, EVAL_BLOCKED;
4186
+ var FS_OPS, O_RDONLY, O_WRONLY3, O_RDWR3, O_ACCMODE3, O_DIRECTORY, accessMode, PLACED, GLUE_TRAILER, evaluateGlue, EVAL_BLOCKED;
4174
4187
  var init_process_runtime = __esm({
4175
4188
  "src/process/process-runtime.ts"() {
4176
4189
  "use strict";
@@ -4195,7 +4208,9 @@ var init_process_runtime = __esm({
4195
4208
  O_RDONLY = 0;
4196
4209
  O_WRONLY3 = 1;
4197
4210
  O_RDWR3 = 2;
4211
+ O_ACCMODE3 = 3;
4198
4212
  O_DIRECTORY = 65536;
4213
+ accessMode = (access) => access === "read" ? O_RDONLY : access === "write" ? O_WRONLY3 : O_RDWR3;
4199
4214
  PLACED = /* @__PURE__ */ new Set(["stream", "file", "socket"]);
4200
4215
  GLUE_TRAILER = [
4201
4216
  "if (typeof ENV !== 'undefined') Object.assign(ENV, Module.sliccEnv);",
@@ -4675,7 +4690,8 @@ function promoteRequest(fd, f, handed) {
4675
4690
  ...joins ? {} : { contents: f.buffer.contents() },
4676
4691
  ...orphan ? { orphan: true } : {},
4677
4692
  ...!orphan && f.buffer.pin !== void 0 ? { pin: { version: f.buffer.pin, size: f.buffer.size() } } : {},
4678
- ...!joins && f.buffer.isDirty() ? { dirty: true } : {}
4693
+ ...!joins && f.buffer.isDirty() ? { dirty: true } : {},
4694
+ ...f.buffer.handle !== void 0 ? { handle: f.buffer.handle } : {}
4679
4695
  };
4680
4696
  }
4681
4697
  var SYNTHETIC_DIRS2, O_NONBLOCK2, O_WRONLY4, O_RDWR4, O_APPEND, DEVICES2, WasiFds, GEN;
@@ -6207,18 +6223,18 @@ var init_wasi_module = __esm({
6207
6223
  });
6208
6224
 
6209
6225
  // src/process/wasi/wasi-signals.ts
6210
- var DELIVERED, DELIVERED_MASK, WasiSignals;
6226
+ var DELIVERED_MASK, WasiSignals;
6211
6227
  var init_wasi_signals = __esm({
6212
6228
  "src/process/wasi/wasi-signals.ts"() {
6213
6229
  "use strict";
6214
6230
  init_signals();
6215
- DELIVERED = [SIG.HUP, SIG.INT, SIG.QUIT, SIG.USR1, SIG.USR2, SIG.ALRM, SIG.TERM];
6216
- DELIVERED_MASK = DELIVERED.reduce((m, sig) => m | sigbit(sig), 0);
6231
+ DELIVERED_MASK = Object.values(SIG).filter((sig) => sig !== SIG.KILL && sig !== SIG.STOP).reduce((m, sig) => m | sigbit(sig), 0);
6217
6232
  WasiSignals = class {
6218
6233
  exports;
6219
6234
  callback;
6220
6235
  uncaught = 0;
6221
6236
  delivering;
6237
+ defaulted = false;
6222
6238
  fallBack;
6223
6239
  onKilled;
6224
6240
  constructor(fallBack, onKilled) {
@@ -6241,25 +6257,29 @@ var init_wasi_signals = __esm({
6241
6257
  raise(sig) {
6242
6258
  const handler = this.handler();
6243
6259
  if (!handler) return;
6244
- const outer = this.delivering;
6260
+ const outer = { sig: this.delivering, defaulted: this.defaulted };
6245
6261
  this.delivering = sig;
6262
+ this.defaulted = false;
6246
6263
  try {
6247
6264
  handler(sig);
6248
6265
  } catch (e) {
6249
6266
  if (!(e instanceof WebAssembly.RuntimeError)) throw e;
6250
- this.defaultAction(sig);
6267
+ if (!this.defaulted) this.defaultAction(sig);
6251
6268
  } finally {
6252
- this.delivering = outer;
6269
+ this.delivering = outer.sig;
6270
+ this.defaulted = outer.defaulted;
6253
6271
  }
6254
6272
  }
6255
6273
  raised(sig) {
6256
6274
  if (sig !== SIG.ABRT || this.delivering === void 0) return false;
6257
- this.defaultAction(this.delivering);
6275
+ if (!this.defaulted) this.defaultAction(this.delivering);
6258
6276
  return true;
6259
6277
  }
6260
6278
  defaultAction(sig) {
6279
+ this.defaulted = true;
6261
6280
  this.uncaught |= sigbit(sig);
6262
6281
  this.fallBack(sig);
6282
+ if (defaultAction(sig) !== "terminate") this.uncaught &= ~sigbit(sig);
6263
6283
  }
6264
6284
  handler() {
6265
6285
  const fn = this.callback ? this.exports?.[this.callback] : void 0;
@@ -6350,8 +6370,8 @@ var init_wasi_threads = __esm({
6350
6370
  memory;
6351
6371
  cap;
6352
6372
  tid;
6353
- constructor(port2, memory, cap, tid, ids) {
6354
- this.port = port2;
6373
+ constructor(port3, memory, cap, tid, ids) {
6374
+ this.port = port3;
6355
6375
  this.memory = memory;
6356
6376
  this.cap = cap;
6357
6377
  this.tid = tid;
@@ -6900,17 +6920,13 @@ var init_physical_path = __esm({
6900
6920
  function resolveFrom(cwd, path) {
6901
6921
  return normalize(path.startsWith("/") ? path : `${cwd}/${path}`);
6902
6922
  }
6903
- var POSIX_TO_WASI_SIGNAL, O_WRONLY5, O_RDWR5, O_APPEND2, OFLAG_CREAT, OFLAG_TRUNC, WasixProcess;
6923
+ var O_WRONLY5, O_RDWR5, O_APPEND2, OFLAG_CREAT, OFLAG_TRUNC, WasixProcess;
6904
6924
  var init_wasix_process = __esm({
6905
6925
  "src/process/wasi/wasix-process.ts"() {
6906
6926
  "use strict";
6907
6927
  init_physical_path();
6908
- init_wasi_abi();
6909
6928
  init_wasi_files();
6910
6929
  init_wasi_host();
6911
- POSIX_TO_WASI_SIGNAL = Object.fromEntries(
6912
- Object.entries(WASI_SIGNAL_TO_POSIX).map(([wasi, posix]) => [posix, Number(wasi)])
6913
- );
6914
6930
  O_WRONLY5 = 1;
6915
6931
  O_RDWR5 = 2;
6916
6932
  O_APPEND2 = 1024;
@@ -7046,7 +7062,7 @@ var init_wasix_process = __esm({
7046
7062
  const sig = status2 & 127;
7047
7063
  if (sig) {
7048
7064
  v.setUint8(statusPtr, 2);
7049
- v.setUint8(statusPtr + 4, POSIX_TO_WASI_SIGNAL[sig] ?? sig);
7065
+ v.setUint8(statusPtr + 4, sig);
7050
7066
  } else {
7051
7067
  v.setUint8(statusPtr, 1);
7052
7068
  v.setUint16(statusPtr + 2, status2 >> 8 & 255, true);
@@ -7153,13 +7169,13 @@ function readAddr(view, ptr) {
7153
7169
  );
7154
7170
  return { family: "unix", path };
7155
7171
  }
7156
- const port2 = view.getUint16(ptr + 2, true);
7172
+ const port3 = view.getUint16(ptr + 2, true);
7157
7173
  if (tag === INET4) {
7158
7174
  const host = [0, 1, 2, 3].map((i) => view.getUint8(ptr + 4 + i)).join(".");
7159
- return { family: "inet", host, port: port2 };
7175
+ return { family: "inet", host, port: port3 };
7160
7176
  }
7161
7177
  if (tag === INET6) {
7162
- return { family: "inet", host: ipv6Host(new Uint8Array(view.buffer, ptr + 4, 16)), port: port2 };
7178
+ return { family: "inet", host: ipv6Host(new Uint8Array(view.buffer, ptr + 4, 16)), port: port3 };
7163
7179
  }
7164
7180
  throw new WasiError("EAFNOSUPPORT");
7165
7181
  }
@@ -7355,7 +7371,7 @@ var init_wasix_sockets = __esm({
7355
7371
 
7356
7372
  // src/process/wasi/wasix-host.ts
7357
7373
  function posixSignal(sig) {
7358
- const posix = WASI_SIGNAL_TO_POSIX[sig];
7374
+ const posix = wasixSignal(sig);
7359
7375
  if (posix === void 0) throw new WasiError("EINVAL");
7360
7376
  return posix;
7361
7377
  }
@@ -7558,7 +7574,7 @@ var init_wasix_host = __esm({
7558
7574
  stack_restore: (snapPtr, val) => void this.driver.restore(snapPtr, val),
7559
7575
  proc_join: (pidPtr, flags, statusPtr) => void process.join(pidPtr, flags, statusPtr),
7560
7576
  proc_signal: (pid, sig) => {
7561
- const posix = sig === 0 ? 0 : WASI_SIGNAL_TO_POSIX[sig];
7577
+ const posix = sig === 0 ? 0 : wasixSignal(sig);
7562
7578
  if (posix === void 0) throw new WasiError("EINVAL");
7563
7579
  if (pid === this.host.o.pid && posix !== 0 && this.host.onRaise?.(posix)) return;
7564
7580
  this.host.o.kernel.call({ op: "proc-kill", pid, sig: posix });
@@ -7659,8 +7675,7 @@ var init_wasix_host = __esm({
7659
7675
  },
7660
7676
  thread_signal: (tid, sig) => {
7661
7677
  if (!(this.threads?.known(tid) ?? tid === MAIN_TID)) throw new WasiError("ESRCH");
7662
- const posix = WASI_SIGNAL_TO_POSIX[sig];
7663
- if (posix === void 0) throw new WasiError("EINVAL");
7678
+ const posix = posixSignal(sig);
7664
7679
  if (host.onRaise?.(posix)) return;
7665
7680
  host.o.kernel.call({ op: "proc-kill", pid: host.o.pid, sig: posix });
7666
7681
  },
@@ -7919,10 +7934,10 @@ function sizeOf(main) {
7919
7934
  function memoryOf(instance, imported) {
7920
7935
  return imported ?? instance.exports.memory;
7921
7936
  }
7922
- function kernelOf(init, port2, hooks = { masks: () => null, raise: () => {
7937
+ function kernelOf(init, port3, hooks = { masks: () => null, raise: () => {
7923
7938
  } }, memory) {
7924
7939
  const transport = new SignalGate(
7925
- createSyncSabTransport(init.sab, port2, memory ? { memory } : {}),
7940
+ createSyncSabTransport(init.sab, port3, memory ? { memory } : {}),
7926
7941
  new Int32Array(init.sab, 0, SAB_HEADER_I32),
7927
7942
  hooks
7928
7943
  ).transport();
@@ -8046,19 +8061,19 @@ function descriptors(init, fork, threads) {
8046
8061
  if (fork) return { forked: { fds: fork.fds, cloexec: fork.cloexec } };
8047
8062
  return { inherited: init.fds ?? [], ...init.program.preopenRoot ? { preopenRoot: true } : {} };
8048
8063
  }
8049
- async function runWasiProcess(init, port2) {
8064
+ async function runWasiProcess(init, port3) {
8050
8065
  captureBacktraces(init.env);
8051
8066
  const signals = new WasiSignals(
8052
8067
  (sig) => {
8053
8068
  call2({ op: "proc-kill", pid: init.pid, sig });
8054
- throw new WasiExit(128 + sig);
8069
+ if (defaultAction(sig) === "terminate") throw new WasiExit(128 + sig);
8055
8070
  },
8056
8071
  (code2) => {
8057
8072
  throw new WasiExit(code2);
8058
8073
  }
8059
8074
  );
8060
8075
  const main = {};
8061
- const { transport, sys, call: kernelCall, say } = kernelOf(init, port2, signals, sizeOf(main));
8076
+ const { transport, sys, call: kernelCall, say } = kernelOf(init, port3, signals, sizeOf(main));
8062
8077
  const id = identify(transport, init);
8063
8078
  const stats = init.env.SLICC_WASI_STATS === "1" ? new WasiStats() : void 0;
8064
8079
  const call2 = stats ? timedCalls(stats, kernelCall) : kernelCall;
@@ -8075,7 +8090,7 @@ async function runWasiProcess(init, port2) {
8075
8090
  }
8076
8091
  const fork = init.fork?.wasi;
8077
8092
  const memory = createImportedMemory(init.program.memory, fork ? init.fork?.memory : void 0);
8078
- const threads = memory?.buffer instanceof SharedArrayBuffer && spawnsThreads(module) ? new WasiThreads(port2, memory, threadCap(init.env), MAIN_TID) : void 0;
8093
+ const threads = memory?.buffer instanceof SharedArrayBuffer && spawnsThreads(module) ? new WasiThreads(port3, memory, threadCap(init.env), MAIN_TID) : void 0;
8079
8094
  const host = new WasiHost({
8080
8095
  args: [init.argv0, ...init.args],
8081
8096
  env: init.env,
@@ -8150,11 +8165,11 @@ function captureBacktraces(env) {
8150
8165
  const v8 = Error;
8151
8166
  v8.stackTraceLimit = Math.max(v8.stackTraceLimit ?? 0, BACKTRACE_FRAMES + 20);
8152
8167
  }
8153
- async function runWasiThread(init, port2) {
8168
+ async function runWasiThread(init, port3) {
8154
8169
  captureBacktraces(init.env);
8155
- const { transport, sys, call: call2, say } = kernelOf(init, port2);
8170
+ const { transport, sys, call: call2, say } = kernelOf(init, port3);
8156
8171
  const { thread } = init;
8157
- const threads = new WasiThreads(port2, thread.memory, threadCap(init.env), thread.tid, thread.ids);
8172
+ const threads = new WasiThreads(port3, thread.memory, threadCap(init.env), thread.tid, thread.ids);
8158
8173
  threads.received = thread.modules;
8159
8174
  const id = identify(transport, init);
8160
8175
  const host = new WasiHost({
@@ -8184,7 +8199,7 @@ async function runWasiThread(init, port2) {
8184
8199
  return;
8185
8200
  }
8186
8201
  if (e instanceof WasiExit) {
8187
- port2.postMessage({ type: WASM_PROCESS_EXIT, code: e.code });
8202
+ port3.postMessage({ type: WASM_PROCESS_EXIT, code: e.code });
8188
8203
  return;
8189
8204
  }
8190
8205
  if (e instanceof WebAssembly.RuntimeError) {
@@ -8192,7 +8207,7 @@ async function runWasiThread(init, port2) {
8192
8207
  say(trapMessage(e, init.env, ` in thread ${thread.tid}`, programNames(host, init.program)));
8193
8208
  } catch {
8194
8209
  }
8195
- port2.postMessage({ type: WASM_PROCESS_EXIT, code: TRAPPED });
8210
+ port3.postMessage({ type: WASM_PROCESS_EXIT, code: TRAPPED });
8196
8211
  return;
8197
8212
  }
8198
8213
  throw e;
@@ -8203,6 +8218,7 @@ var init_wasi_runtime = __esm({
8203
8218
  "src/process/wasi/wasi-runtime.ts"() {
8204
8219
  "use strict";
8205
8220
  init_protocol();
8221
+ init_signals();
8206
8222
  init_sync_sab_bridge();
8207
8223
  init_sync_sab_wire();
8208
8224
  init_kernel_streams();
@@ -8279,7 +8295,7 @@ async function changed(header, index2, seen, waitAsync = defaultWaitAsync) {
8279
8295
  const waited = waitAsync(header, index2, seen);
8280
8296
  if (waited.async) await waited.value;
8281
8297
  }
8282
- function createAsyncSabTransport(sab, port2, waitAsync = defaultWaitAsync) {
8298
+ function createAsyncSabTransport(sab, port3, waitAsync = defaultWaitAsync) {
8283
8299
  const { header, window } = sabViews(sab);
8284
8300
  let seq = 0;
8285
8301
  let tail = Promise.resolve();
@@ -8301,7 +8317,7 @@ function createAsyncSabTransport(sab, port2, waitAsync = defaultWaitAsync) {
8301
8317
  try {
8302
8318
  for (; ; ) {
8303
8319
  Atomics.store(header, SAB_I_STATE, SAB_STATE_PENDING);
8304
- port2.postMessage(
8320
+ port3.postMessage(
8305
8321
  offset === 0 ? { type: SYNC_SAB_REQ_MSG, id, req } : { type: SYNC_SAB_NEXT_MSG, id, offset }
8306
8322
  );
8307
8323
  await ready(id);
@@ -8706,6 +8722,145 @@ var init_js_io = __esm({
8706
8722
  }
8707
8723
  });
8708
8724
 
8725
+ // src/process/js/js-net.ts
8726
+ function errnoName(errno2) {
8727
+ const found = Object.entries(E).find(([, n]) => n === errno2);
8728
+ return found ? `E${found[0]}` : "EIO";
8729
+ }
8730
+ function addressOf(addr) {
8731
+ const inet = addr;
8732
+ return { host: inet.host, port: inet.port };
8733
+ }
8734
+ function port(n) {
8735
+ if (!Number.isInteger(n) || n < 0 || n > 65535) throw new JsCallError("EINVAL", `port ${n}`);
8736
+ return n;
8737
+ }
8738
+ function netOps(kernel, io) {
8739
+ const name = async (fd, peer) => addressOf(await kernel.json({ op: "sock-name", fd, peer }));
8740
+ const connection = async (fd) => {
8741
+ const [local, remote] = [await name(fd, false), await name(fd, true)];
8742
+ const stop = new AbortController();
8743
+ let closed;
8744
+ const close = () => {
8745
+ stop.abort(new JsCallError("EBADF", "socket closed"));
8746
+ closed ??= io.close(fd);
8747
+ return closed;
8748
+ };
8749
+ const shut = (how) => kernel.raw({ op: "sock-shutdown", fd, how });
8750
+ const readable2 = new ReadableStream(
8751
+ {
8752
+ async pull(controller) {
8753
+ const chunk = await io.read(fd, void 0, stop.signal);
8754
+ if (chunk.length > 0) controller.enqueue(chunk);
8755
+ else controller.close();
8756
+ },
8757
+ cancel: async () => void await shut(SHUT_RD)
8758
+ },
8759
+ { highWaterMark: 0 }
8760
+ );
8761
+ const writable2 = new WritableStream({
8762
+ write: (chunk, controller) => io.send(fd, chunk, AbortSignal.any([controller.signal, stop.signal])),
8763
+ close: async () => void await shut(SHUT_WR),
8764
+ abort: () => close()
8765
+ });
8766
+ return { fd, local, remote, readable: readable2, writable: writable2, close };
8767
+ };
8768
+ const resolve = async (host, signal) => {
8769
+ if (IPV4.test(host)) return host;
8770
+ const r = await kernel.blocking({ op: "sock-resolve", name: host, family: 4 }, signal);
8771
+ return (r.ok && r.kind === "json" ? r.json : [])[0];
8772
+ };
8773
+ const opened = async (use) => {
8774
+ const fd = await kernel.json({ op: "sock-open", domain: "inet" });
8775
+ try {
8776
+ return await use(fd);
8777
+ } catch (err) {
8778
+ await io.close(fd);
8779
+ throw err;
8780
+ }
8781
+ };
8782
+ const listener = (fd, at) => {
8783
+ const stop = new AbortController();
8784
+ const accept = async (options = {}) => {
8785
+ const signal = AbortSignal.any([options.signal ?? NEVER, stop.signal]);
8786
+ for (; ; ) {
8787
+ signal.throwIfAborted();
8788
+ const r = await kernel.raw({ op: "sock-accept", fd, nonblock: true });
8789
+ if (r.ok) return connection((r.kind === "json" ? r.json : {}).fd);
8790
+ if (r.errno !== "EAGAIN") throw new JsCallError(r.errno, "accept");
8791
+ await kernel.blocking({ op: "fd-select", read: [fd], write: [], timeoutMs: -1 }, signal);
8792
+ }
8793
+ };
8794
+ return {
8795
+ fd,
8796
+ ...at,
8797
+ accept,
8798
+ async close() {
8799
+ stop.abort(new JsCallError("EBADF", "listener closed"));
8800
+ await io.close(fd);
8801
+ },
8802
+ async *[Symbol.asyncIterator]() {
8803
+ for (; ; ) {
8804
+ try {
8805
+ yield await accept();
8806
+ } catch (err) {
8807
+ if (stop.signal.aborted) return;
8808
+ throw err;
8809
+ }
8810
+ }
8811
+ }
8812
+ };
8813
+ };
8814
+ return {
8815
+ listen: (options = {}) => opened(async (fd) => {
8816
+ const addr = {
8817
+ family: "inet",
8818
+ host: options.host ?? "127.0.0.1",
8819
+ port: port(options.port ?? 0)
8820
+ };
8821
+ await kernel.call({ op: "sock-bind", fd, addr });
8822
+ await kernel.call({ op: "sock-listen", fd, backlog: options.backlog ?? 128 });
8823
+ return listener(fd, await name(fd, false));
8824
+ }),
8825
+ connect: async (options) => {
8826
+ const signal = options.signal ?? NEVER;
8827
+ signal.throwIfAborted();
8828
+ const addr = {
8829
+ family: "inet",
8830
+ host: await resolve(options.host, signal),
8831
+ port: port(options.port)
8832
+ };
8833
+ return opened(async (fd) => {
8834
+ const r = await kernel.raw({ op: "sock-connect", fd, addr, nonblock: true });
8835
+ if (!r.ok && r.errno !== "EINPROGRESS") throw new JsCallError(r.errno, "connect");
8836
+ await kernel.blocking({ op: "fd-select", read: [], write: [fd], timeoutMs: -1 }, signal);
8837
+ const failed2 = await kernel.json({
8838
+ op: "sock-getopt",
8839
+ fd,
8840
+ level: SOL_SOCKET,
8841
+ name: SO_ERROR
8842
+ });
8843
+ if (failed2 !== 0) throw new JsCallError(errnoName(failed2), "connect");
8844
+ return connection(fd);
8845
+ });
8846
+ }
8847
+ };
8848
+ }
8849
+ var SHUT_RD, SHUT_WR, IPV4, NEVER, SOL_SOCKET, SO_ERROR;
8850
+ var init_js_net = __esm({
8851
+ "src/process/js/js-net.ts"() {
8852
+ "use strict";
8853
+ init_wasi_abi();
8854
+ init_js_kernel();
8855
+ SHUT_RD = 0;
8856
+ SHUT_WR = 1;
8857
+ IPV4 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
8858
+ NEVER = new AbortController().signal;
8859
+ SOL_SOCKET = 1;
8860
+ SO_ERROR = 4;
8861
+ }
8862
+ });
8863
+
8709
8864
  // src/process/js/js-sync.ts
8710
8865
  function laneOf(lane) {
8711
8866
  if (!lane) return () => ({ ok: false, errno: "ENOSYS", message: "no sync lane" });
@@ -9048,11 +9203,8 @@ function createContext(o) {
9048
9203
  infos,
9049
9204
  handles
9050
9205
  });
9051
- const children = childOps(
9052
- kernel,
9053
- { read, send: writeAll, close: (fd) => ctx.close(fd) },
9054
- { env: o.env, cwd: o.cwd, signal: signalNumber }
9055
- );
9206
+ const io = { read, send: writeAll, close: (fd) => ctx.close(fd) };
9207
+ const children = childOps(kernel, io, { env: o.env, cwd: o.cwd, signal: signalNumber });
9056
9208
  const ctx = {
9057
9209
  argv: o.argv,
9058
9210
  env: o.env,
@@ -9096,6 +9248,7 @@ function createContext(o) {
9096
9248
  },
9097
9249
  sync,
9098
9250
  ...children,
9251
+ net: netOps(kernel, io),
9099
9252
  exit
9100
9253
  };
9101
9254
  const drain2 = async () => {
@@ -9231,6 +9384,7 @@ var init_js_context = __esm({
9231
9384
  init_js_children();
9232
9385
  init_js_io();
9233
9386
  init_js_kernel();
9387
+ init_js_net();
9234
9388
  init_js_sync();
9235
9389
  JsExit = class extends Error {
9236
9390
  status;
@@ -9338,8 +9492,8 @@ function mainOf(module) {
9338
9492
  const main = typeof module.default === "function" ? module.default : module.main;
9339
9493
  return typeof main === "function" ? main : void 0;
9340
9494
  }
9341
- async function runJsProcess(init, port2, deps = {}) {
9342
- const transport = createAsyncSabTransport(init.sab, port2, deps.waitAsync);
9495
+ async function runJsProcess(init, port3, deps = {}) {
9496
+ const transport = createAsyncSabTransport(init.sab, port3, deps.waitAsync);
9343
9497
  let resolveExit;
9344
9498
  const exit = new Promise((resolve) => resolveExit = resolve);
9345
9499
  let exitedWith;
@@ -9422,29 +9576,29 @@ init_protocol();
9422
9576
  init_process_runtime();
9423
9577
  var loadWasi = () => Promise.resolve().then(() => (init_wasi_runtime(), wasi_runtime_exports));
9424
9578
  var loadJs = () => Promise.resolve().then(() => (init_js_runtime(), js_runtime_exports));
9425
- function started(init, port2, run, wasi, js) {
9579
+ function started(init, port3, run, wasi, js) {
9426
9580
  if (init.program?.abi === "js") {
9427
9581
  const jsInit = init;
9428
- return js().then((m) => m.runJsProcess(jsInit, port2));
9582
+ return js().then((m) => m.runJsProcess(jsInit, port3));
9429
9583
  }
9430
9584
  const wasmInit = init;
9431
- if (wasmInit.program?.abi === "wasi") return wasi().then((m) => m.runWasiProcess(wasmInit, port2));
9432
- return run(wasmInit, port2);
9585
+ if (wasmInit.program?.abi === "wasi") return wasi().then((m) => m.runWasiProcess(wasmInit, port3));
9586
+ return run(wasmInit, port3);
9433
9587
  }
9434
- function processEntry(port2, run = runWasmProcess, wasi = loadWasi, js = loadJs) {
9435
- const failed2 = (err) => port2.postMessage({
9588
+ function processEntry(port3, run = runWasmProcess, wasi = loadWasi, js = loadJs) {
9589
+ const failed2 = (err) => port3.postMessage({
9436
9590
  type: WASM_PROCESS_ERROR,
9437
9591
  message: err instanceof Error ? err.stack ?? err.message : String(err)
9438
9592
  });
9439
9593
  return (data) => {
9440
9594
  const type = data?.type;
9441
9595
  if (type === WASM_THREAD_INIT) {
9442
- wasi().then((m) => m.runWasiThread(data, port2)).catch(failed2);
9596
+ wasi().then((m) => m.runWasiThread(data, port3)).catch(failed2);
9443
9597
  return;
9444
9598
  }
9445
9599
  if (type !== WASM_PROCESS_INIT) return;
9446
- started(data, port2, run, wasi, js).then(
9447
- (code2) => port2.postMessage({ type: WASM_PROCESS_EXIT, code: code2 }),
9600
+ started(data, port3, run, wasi, js).then(
9601
+ (code2) => port3.postMessage({ type: WASM_PROCESS_EXIT, code: code2 }),
9448
9602
  failed2
9449
9603
  );
9450
9604
  };
@@ -9455,7 +9609,7 @@ var scope = globalThis;
9455
9609
  scope.require ??= createRequire(import.meta.url);
9456
9610
  scope.__filename ??= new URL(import.meta.url).pathname;
9457
9611
  scope.__dirname ??= new URL(".", import.meta.url).pathname;
9458
- var port = parentPort;
9459
- var onMessage = processEntry({ postMessage: (message2) => port.postMessage(message2) });
9460
- port.on("message", onMessage);
9612
+ var port2 = parentPort;
9613
+ var onMessage = processEntry({ postMessage: (message2) => port2.postMessage(message2) });
9614
+ port2.on("message", onMessage);
9461
9615
  //# sourceMappingURL=node-process-worker.js.map