@ai-ecoverse/slicc-kernel 1.53.4 → 1.54.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.
@@ -10706,6 +10706,9 @@ function fingerprint(headers) {
10706
10706
  const fields = MATCHED.map((name2) => header2(headers, name2) ?? "");
10707
10707
  return JSON.stringify([...fields, header2(headers, "authorization") !== void 0]);
10708
10708
  }
10709
+ function unauthorized(print) {
10710
+ return JSON.stringify([...JSON.parse(print).slice(0, -1), false]);
10711
+ }
10709
10712
  function followedWith(status, method) {
10710
10713
  if (status === 303 && method !== "GET" && method !== "HEAD") return "GET";
10711
10714
  if ((status === 301 || status === 302) && method === "POST") return "GET";
@@ -10746,9 +10749,17 @@ function replayingHops(inner, heldMs = HELD_MS) {
10746
10749
  const [hop, ...rest2] = chain.hops;
10747
10750
  const location = hop && header2(hop.headers, "location");
10748
10751
  if (!hop || location === void 0) return chain.final;
10749
- const next2 = new URL(location, hop.url).href;
10752
+ const target = new URL(location, hop.url);
10753
+ target.hash = "";
10750
10754
  const method = followedWith(hop.status, chain.method);
10751
- hold(keyOf2(chain.owner, method, next2), { ...chain, method, hops: rest2 });
10755
+ const away = target.origin !== new URL(hop.url).origin;
10756
+ const print = away ? unauthorized(chain.fingerprint) : chain.fingerprint;
10757
+ hold(keyOf2(chain.owner, method, target.href), {
10758
+ ...chain,
10759
+ method,
10760
+ fingerprint: print,
10761
+ hops: rest2
10762
+ });
10752
10763
  return bodiless(hop);
10753
10764
  };
10754
10765
  return {
@@ -11401,7 +11412,7 @@ function hostfsHandlers(current) {
11401
11412
  const handles = /* @__PURE__ */ new Map();
11402
11413
  let nextFh = 0;
11403
11414
  const fresh = (remote) => {
11404
- if (remote.client.token === remote.token) return remote;
11415
+ if (remote.client.token === remote.token && !remote.client.gone) return remote;
11405
11416
  throw fsError2("ESTALE", "the folder was granted again, so this open file is gone");
11406
11417
  };
11407
11418
  const live = (fh) => {
@@ -11606,10 +11617,126 @@ function keepWatching(loop, signal) {
11606
11617
  function invalidation(event) {
11607
11618
  return event.paths ?? true;
11608
11619
  }
11609
- function grantConnect(hook, fetch2) {
11620
+ var PORT_REQUEST_MS = 6e4;
11621
+ var PORT_MS_PER_MIB = 1e3;
11622
+ var nextPortToken = 0;
11623
+ var PortTransport = class {
11624
+ capabilities;
11625
+ token;
11626
+ port;
11627
+ requestMs;
11628
+ waiting = /* @__PURE__ */ new Map();
11629
+ next = 0;
11630
+ dead;
11631
+ constructor(grant, requestMs = PORT_REQUEST_MS) {
11632
+ this.port = grant.port;
11633
+ this.requestMs = requestMs;
11634
+ this.capabilities = grant.capabilities ?? {};
11635
+ this.token = grant.token ?? `port-${++nextPortToken}`;
11636
+ this.port.addEventListener("message", (event) => {
11637
+ this.waiting.get(event.data?.id)?.(event.data);
11638
+ });
11639
+ this.port.addEventListener("close", () => this.fail("the folder's port closed"));
11640
+ this.port.start();
11641
+ }
11642
+ get maxIo() {
11643
+ return this.capabilities.maxIo ?? HOSTFS_IO;
11644
+ }
11645
+ get gone() {
11646
+ return this.dead !== void 0;
11647
+ }
11648
+ fail(why) {
11649
+ if (this.dead === void 0) this.port.close();
11650
+ this.dead ??= why;
11651
+ for (const settle3 of [...this.waiting.values()])
11652
+ settle3({ id: 0, error: { code: "ENOTCONN", message: why } });
11653
+ }
11654
+ ask(message3, bytes = 0, transfer = []) {
11655
+ const id2 = ++this.next;
11656
+ if (this.dead) return Promise.reject(fsError2("ENOTCONN", this.dead));
11657
+ return new Promise((resolve, reject) => {
11658
+ const timer = setTimeout(
11659
+ () => {
11660
+ this.waiting.delete(id2);
11661
+ reject(fsError2("ETIMEDOUT", "the folder's port did not answer"));
11662
+ this.fail("the folder's port stopped answering");
11663
+ },
11664
+ this.requestMs + Math.ceil(bytes / 1048576) * PORT_MS_PER_MIB
11665
+ );
11666
+ this.waiting.set(id2, (reply) => {
11667
+ clearTimeout(timer);
11668
+ this.waiting.delete(id2);
11669
+ if (reply.error) reject(fsError2(reply.error.code ?? "EIO", reply.error.message ?? ""));
11670
+ else resolve(reply);
11671
+ });
11672
+ try {
11673
+ this.port.postMessage({ id: id2, ...message3 }, transfer);
11674
+ } catch (err) {
11675
+ this.fail(`the folder's port is closed: ${err.message}`);
11676
+ }
11677
+ });
11678
+ }
11679
+ async call(op, body = {}) {
11680
+ return (await this.ask({ op: "call", name: op, body })).result;
11681
+ }
11682
+ async read(fh, offset, size, ifMatch) {
11683
+ const reply = await this.ask(
11684
+ { op: "read", fh, offset, size, ...ifMatch ? { ifMatch } : {} },
11685
+ size
11686
+ );
11687
+ return reply.bytes ?? new Uint8Array(0);
11688
+ }
11689
+ async write(fh, offset, bytes) {
11690
+ await this.ask({ op: "write", fh, offset, bytes }, bytes.length);
11691
+ }
11692
+ watch(signal, onEvent, onOpen = () => void 0) {
11693
+ const id2 = ++this.next;
11694
+ if (this.dead) return Promise.reject(fsError2("ENOTCONN", this.dead));
11695
+ if (signal.aborted) {
11696
+ this.fail("the folder's watch ended");
11697
+ return Promise.resolve();
11698
+ }
11699
+ return new Promise((resolve, reject) => {
11700
+ const finish = () => {
11701
+ this.waiting.delete(id2);
11702
+ signal.removeEventListener("abort", stop);
11703
+ };
11704
+ const stop = () => {
11705
+ finish();
11706
+ try {
11707
+ this.port.postMessage({ id: id2, op: "unwatch" });
11708
+ } catch {
11709
+ }
11710
+ resolve();
11711
+ };
11712
+ this.waiting.set(id2, (reply) => {
11713
+ if (reply.open) return onOpen();
11714
+ if (reply.event) return onEvent(reply.event);
11715
+ finish();
11716
+ if (reply.error) reject(fsError2(reply.error.code ?? "ENOTCONN", reply.error.message ?? ""));
11717
+ else resolve();
11718
+ });
11719
+ signal.addEventListener("abort", stop, { once: true });
11720
+ try {
11721
+ this.port.postMessage({ id: id2, op: "watch" });
11722
+ } catch (err) {
11723
+ finish();
11724
+ reject(fsError2("ENOTCONN", `the folder's port is closed: ${err.message}`));
11725
+ }
11726
+ }).finally(() => this.fail("the folder's watch ended"));
11727
+ }
11728
+ };
11729
+ function grantConnect(hook, fetch2, requestMs) {
11610
11730
  return async (source, options) => {
11611
- const grant = () => hook(source, options);
11612
- return new HostfsClient(await grant(), grant, fetch2);
11731
+ const first = await hook(source, options);
11732
+ if ("port" in first) return new PortTransport(first, requestMs);
11733
+ const grant = async () => {
11734
+ const again = await hook(source, options);
11735
+ if ("port" in again)
11736
+ throw fsError2("ESTALE", "the folder moved to a port, so its open files are gone");
11737
+ return again;
11738
+ };
11739
+ return new HostfsClient(first, grant, fetch2);
11613
11740
  };
11614
11741
  }
11615
11742
  async function openHostfs(spec, connect, timing = {}) {
@@ -12655,7 +12782,11 @@ var Launcher = class {
12655
12782
  throw fsError("ENODEV", "this kernel has no hostfs hook (createKernel({ hostfs }))");
12656
12783
  return openHostfs(
12657
12784
  spec,
12658
- grantConnect(hostfs, hostfsFetch ?? ((url, init) => fetch(url, init))),
12785
+ grantConnect(
12786
+ hostfs,
12787
+ hostfsFetch ?? ((url, init) => fetch(url, init)),
12788
+ hostfsTiming?.requestMs
12789
+ ),
12659
12790
  hostfsTiming
12660
12791
  );
12661
12792
  }