@ai-ecoverse/slicc-kernel 1.30.0 → 1.31.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.
@@ -2369,6 +2369,21 @@ function toHostLoopback(href) {
2369
2369
  url.hostname = "127.0.0.1";
2370
2370
  return url.href;
2371
2371
  }
2372
+ var DEFAULT_HOSTNAME = "slicc";
2373
+ var HOSTNAME_ADDRESS = "127.0.1.1";
2374
+ var LIBC_HOSTNAMES = ["emscripten", "wasmer.sh"];
2375
+ var LABEL = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i;
2376
+ function reserved(name2) {
2377
+ const lower = name2.toLowerCase();
2378
+ return lower === "localhost" || lower.endsWith(".localhost") || lower === HOST_LOOPBACK || lower === "wasmer.sh" || /^\d+(\.\d+){3}$/.test(lower);
2379
+ }
2380
+ function isHostname(name2) {
2381
+ if (typeof name2 !== "string" || name2.length > 253 || reserved(name2)) return false;
2382
+ return name2.split(".").every((l) => LABEL.test(l));
2383
+ }
2384
+ function ownNames(hostname) {
2385
+ return [.../* @__PURE__ */ new Set([hostname.toLowerCase(), ...LIBC_HOSTNAMES])];
2386
+ }
2372
2387
 
2373
2388
  // src/kernel/net/http1.ts
2374
2389
  var HttpError = class extends Error {
@@ -3617,14 +3632,15 @@ var PROXY_URL = `http://127.0.0.1:${REALM_PROXY_PORT}`;
3617
3632
  var NO_PROXY = "localhost,.localhost,127.0.0.1,127.0.0.0/8";
3618
3633
  var CA_OWNER = "slicc-kernel";
3619
3634
  var CA_DB = "slicc-kernel-ca";
3620
- function networkEnv() {
3635
+ function networkEnv(hostname = DEFAULT_HOSTNAME) {
3636
+ const bypass = [NO_PROXY, ...ownNames(hostname).filter((n) => n !== "wasmer.sh")].join(",");
3621
3637
  return {
3622
3638
  http_proxy: PROXY_URL,
3623
3639
  https_proxy: PROXY_URL,
3624
3640
  HTTP_PROXY: PROXY_URL,
3625
3641
  HTTPS_PROXY: PROXY_URL,
3626
- no_proxy: NO_PROXY,
3627
- NO_PROXY,
3642
+ no_proxy: bypass,
3643
+ NO_PROXY: bypass,
3628
3644
  SSL_CERT_FILE: CA_PATH,
3629
3645
  CURL_CA_BUNDLE: CA_PATH,
3630
3646
  GIT_SSL_CAINFO: CA_PATH,
@@ -6372,7 +6388,11 @@ var RESOLVE_TTL_MAX = 60;
6372
6388
  var RESOLVE_TIMEOUT_MS = 1e4;
6373
6389
  var RESOLVE_CACHE_MAX = 256;
6374
6390
  var LOOPBACK_NAMES2 = /* @__PURE__ */ new Set(["localhost", "localhost.localdomain", "ip6-localhost", "::1"]);
6375
- function hostsEntry(name2) {
6391
+ function loopbackName(name2, own) {
6392
+ return LOOPBACK_NAMES2.has(name2) || name2.endsWith(".localhost") || own.includes(name2);
6393
+ }
6394
+ function hostsEntry(name2, own = ownNames(DEFAULT_HOSTNAME)) {
6395
+ if (own.includes(name2)) return HOSTNAME_ADDRESS;
6376
6396
  if (LOOPBACK_NAMES2.has(name2) || name2.endsWith(".localhost")) return "127.0.0.1";
6377
6397
  if (name2 === HOST_LOOPBACK) return HOST_LOOPBACK_ADDRESS.join(".");
6378
6398
  return ipv4Bytes(name2) ? name2 : void 0;
@@ -6383,6 +6403,7 @@ var Resolver = class {
6383
6403
  now;
6384
6404
  timeoutMs;
6385
6405
  log;
6406
+ own;
6386
6407
  cache = /* @__PURE__ */ new Map();
6387
6408
  pending = /* @__PURE__ */ new Map();
6388
6409
  constructor(options) {
@@ -6391,16 +6412,17 @@ var Resolver = class {
6391
6412
  this.now = options.now ?? Date.now;
6392
6413
  this.timeoutMs = options.timeoutMs ?? RESOLVE_TIMEOUT_MS;
6393
6414
  this.log = options.log ?? ((message2) => console.warn(message2));
6415
+ this.own = ownNames(options.hostname ?? DEFAULT_HOSTNAME);
6394
6416
  }
6395
6417
  async resolve(name2, family, blocking) {
6396
6418
  const key = name2.toLowerCase().replace(/\.$/, "");
6397
- const local = hostsEntry(key);
6419
+ const local = hostsEntry(key, this.own);
6398
6420
  if (ipv6Groups(key)) {
6399
6421
  const v4 = family !== 6 && local ? [local] : [];
6400
6422
  return family === 4 ? v4 : [...v4, key];
6401
6423
  }
6402
6424
  if (local) {
6403
- const six = LOOPBACK_NAMES2.has(key) || key.endsWith(".localhost") ? ["::1"] : [];
6425
+ const six = loopbackName(key, this.own) ? ["::1"] : [];
6404
6426
  return family === 6 ? six : family === 4 ? [local] : [local, ...six];
6405
6427
  }
6406
6428
  if (!this.uplink || key === "") return [];
@@ -7876,7 +7898,7 @@ var WasmProcess = class {
7876
7898
  for (const listener of this.stateListeners) listener("continued", SIG.CONT);
7877
7899
  }
7878
7900
  async syscall(req) {
7879
- this.options.onSyscall?.();
7901
+ this.options.onSyscall?.(req);
7880
7902
  for (; ; ) {
7881
7903
  await this.resumed;
7882
7904
  const stops = this.stops;
@@ -8670,6 +8692,57 @@ var JobTable = class {
8670
8692
  }
8671
8693
  };
8672
8694
 
8695
+ // src/kernel/settling.ts
8696
+ var SETUP_OPS = /* @__PURE__ */ new Set([
8697
+ "proc-list",
8698
+ "proc-identity",
8699
+ "fd-info",
8700
+ "sig-mask",
8701
+ "proc-setpgid",
8702
+ "tty-pgrp-set"
8703
+ ]);
8704
+ var SETTLE_MS = 250;
8705
+ var KEY_SIGNALS = /* @__PURE__ */ new Set([2, 3, 20]);
8706
+ var SettlingChildren = class {
8707
+ children = /* @__PURE__ */ new Map();
8708
+ held = [];
8709
+ pgidOf;
8710
+ settleMs;
8711
+ constructor(pgidOf, settleMs = SETTLE_MS) {
8712
+ this.pgidOf = pgidOf;
8713
+ this.settleMs = settleMs;
8714
+ }
8715
+ started(pid, ppid) {
8716
+ this.children.set(pid, { ppid });
8717
+ }
8718
+ syscall(pid, op) {
8719
+ const child = this.children.get(pid);
8720
+ if (!child) return;
8721
+ if (!SETUP_OPS.has(op)) {
8722
+ this.settled(pid);
8723
+ return;
8724
+ }
8725
+ child.timer ??= setTimeout(() => this.settled(pid), this.settleMs);
8726
+ }
8727
+ settled(pid) {
8728
+ const child = this.children.get(pid);
8729
+ if (!child) return;
8730
+ clearTimeout(child.timer);
8731
+ this.children.delete(pid);
8732
+ const ready2 = this.held.filter((h) => !this.blocking(h.pgid));
8733
+ this.held = this.held.filter((h) => this.blocking(h.pgid));
8734
+ for (const h of ready2) h.send();
8735
+ }
8736
+ deliver(sig, pgid, send) {
8737
+ if (KEY_SIGNALS.has(sig) && this.blocking(pgid)) this.held.push({ pgid, send });
8738
+ else send();
8739
+ }
8740
+ blocking(pgid) {
8741
+ for (const { ppid } of this.children.values()) if (this.pgidOf(ppid) === pgid) return true;
8742
+ return false;
8743
+ }
8744
+ };
8745
+
8673
8746
  // src/mount/driver.ts
8674
8747
  function fsError2(code, message2 = code) {
8675
8748
  return Object.assign(new Error(message2), { code });
@@ -9930,7 +10003,14 @@ var Launcher = class {
9930
10003
  described = /* @__PURE__ */ new Map();
9931
10004
  jobs = new JobTable();
9932
10005
  locks = new LockTable();
9933
- ptys = new PtyTable((tty, sig) => this.jobs.signalOwnedForeground(tty, sig));
10006
+ settling = new SettlingChildren((pid) => this.jobs.pgidOf(pid));
10007
+ ptys = new PtyTable(
10008
+ (tty, sig) => this.settling.deliver(
10009
+ sig,
10010
+ this.jobs.tcgetpgrp(tty, 0),
10011
+ () => this.jobs.signalOwnedForeground(tty, sig)
10012
+ )
10013
+ );
9934
10014
  net = new LoopbackNet();
9935
10015
  cdp;
9936
10016
  ca;
@@ -9938,6 +10018,7 @@ var Launcher = class {
9938
10018
  uplink;
9939
10019
  routes;
9940
10020
  resolver;
10021
+ hostname;
9941
10022
  watchers = new FsWatchers();
9942
10023
  pnpmHome;
9943
10024
  mounts;
@@ -9976,13 +10057,16 @@ var Launcher = class {
9976
10057
  this.pnpmHome = options.env?.PNPM_HOME ?? PNPM_HOME;
9977
10058
  this.modulesDir = options.modules ?? "/node_modules";
9978
10059
  this.cdp = new CdpHosts(options.cdp);
9979
- this.env = { ...networkEnv(), SLICC_CDP_URL: this.cdp.url, ...options.env };
10060
+ const hostname = options.hostname ?? DEFAULT_HOSTNAME;
10061
+ if (!isHostname(hostname)) throw new Error(`not a host name: ${String(hostname)}`);
10062
+ this.hostname = hostname;
10063
+ this.env = { ...networkEnv(hostname), SLICC_CDP_URL: this.cdp.url, ...options.env };
9980
10064
  this.ca = kernelCa(options.caStore ?? memoryCaStore());
9981
10065
  this.transport = options.transport ?? missingTransport();
9982
10066
  this.uplink = options.uplink;
9983
10067
  this.routes = new Routes(options.uplink?.traits.ipv6 === true);
9984
10068
  if (options.uplink?.routes) this.routes.set(options.uplink.routes);
9985
- this.resolver = new Resolver({ uplink: options.uplink, routes: this.routes });
10069
+ this.resolver = new Resolver({ uplink: options.uplink, routes: this.routes, hostname });
9986
10070
  if (options.uplink) this.net.useUplink({ uplink: options.uplink, routes: this.routes });
9987
10071
  enableNetwork(this.net, {
9988
10072
  transport: this.transport,
@@ -10138,8 +10222,10 @@ var Launcher = class {
10138
10222
  }
10139
10223
  async launch(planned, req) {
10140
10224
  const { target, args } = planned;
10141
- const env = { ...expandDefaults(target.env, { ...req.env, cwd: req.cwd }), ...req.env };
10225
+ const seen = { ...req.env, HOSTNAME: this.hostname, cwd: req.cwd };
10226
+ const env = { ...expandDefaults(target.env, seen), ...req.env };
10142
10227
  for (const key of target.unset ?? []) delete env[key];
10228
+ env.HOSTNAME = this.hostname;
10143
10229
  let program;
10144
10230
  try {
10145
10231
  program = await this.program(target, env);
@@ -10170,7 +10256,10 @@ var Launcher = class {
10170
10256
  await (asked ??= decided.then(count));
10171
10257
  return this.identityOf(pid);
10172
10258
  },
10173
- onSyscall: () => this.decide(pid),
10259
+ onSyscall: (call) => {
10260
+ this.decide(pid);
10261
+ this.settling.syscall(pid, call.op);
10262
+ },
10174
10263
  ...req.ignored ? { ignored: req.ignored } : {},
10175
10264
  program: req.program,
10176
10265
  argv0: req.argv0,
@@ -10203,6 +10292,7 @@ var Launcher = class {
10203
10292
  ...this.shownIds(req)
10204
10293
  });
10205
10294
  this.processes.set(pid, handle);
10295
+ if (req.ppid !== void 0) this.settling.started(pid, req.ppid);
10206
10296
  this.jobs.add(pid, req.ppid, (sig) => handle.signal(sig), terminal);
10207
10297
  if (req.exec && req.ppid !== void 0) this.jobs.exec(req.ppid, pid, true);
10208
10298
  if (req.pgid !== void 0) this.jobs.join(pid, req.pgid);
@@ -10213,6 +10303,7 @@ var Launcher = class {
10213
10303
  });
10214
10304
  void handle.exited.then(() => {
10215
10305
  this.processes.delete(pid);
10306
+ this.settling.settled(pid);
10216
10307
  if (req.ppid === void 0 || this.orphans.delete(pid)) this.forget(pid);
10217
10308
  else this.zombies.add(pid);
10218
10309
  });
@@ -10525,7 +10616,11 @@ var Launcher = class {
10525
10616
  let leader = 0;
10526
10617
  const tty = new KernelTty(
10527
10618
  { write: (bytes) => options.onData(bytes.slice()) },
10528
- (sig) => this.jobs.signalForeground(tty, leader, sig)
10619
+ (sig) => this.settling.deliver(
10620
+ sig,
10621
+ this.jobs.tcgetpgrp(tty, leader),
10622
+ () => this.jobs.signalForeground(tty, leader, sig)
10623
+ )
10529
10624
  );
10530
10625
  tty.name = `/dev/tty${++this.terminals}`;
10531
10626
  tty.setSize(options.cols ?? 80, options.rows ?? 24);
@@ -10715,6 +10810,7 @@ function serveKernel(port, deps) {
10715
10810
  ...deps.createDriverWorker ? { createDriverWorker: deps.createDriverWorker } : {},
10716
10811
  ...req.modules ? { modules: req.modules } : {},
10717
10812
  ...req.env ? { env: req.env } : {},
10813
+ ...req.hostname !== void 0 ? { hostname: req.hostname } : {},
10718
10814
  ...net,
10719
10815
  ...media ? { media } : {},
10720
10816
  onMountPending: (medium) => port.postMessage({ medium }),