@ai-ecoverse/slicc-kernel 1.65.2 → 1.66.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/index.js CHANGED
@@ -43,6 +43,11 @@ function serveCdpPort(port, hook, request, onDone) {
43
43
  };
44
44
  }
45
45
 
46
+ // src/bytes.ts
47
+ function fresh(bytes, start = 0, end = bytes.length) {
48
+ return new Uint8Array(bytes.subarray(start, end));
49
+ }
50
+
46
51
  // src/client/dial-stream.ts
47
52
  function errorWithCode(message) {
48
53
  const code = /^E[A-Z]+/.exec(message)?.[0];
@@ -96,7 +101,7 @@ function dialStream(port, onClose) {
96
101
  const writable = new WritableStream({
97
102
  write(chunk) {
98
103
  if (closed) throw errorWithCode("EPIPE: the connection is closed");
99
- const copy = chunk.slice();
104
+ const copy = fresh(chunk);
100
105
  const acked = new Promise((resolve, reject) => acks.push({ resolve, reject }));
101
106
  port.postMessage(copy, [copy.buffer]);
102
107
  return acked;
@@ -854,7 +859,7 @@ function serveTransport(peer, transport, { closeWaitMs = CLOSE_WAIT_MS } = {}) {
854
859
  }
855
860
  if (typeof data === "string") peer.postMessage({ net: "ws-message", nid, data });
856
861
  else {
857
- const bytes = data.slice();
862
+ const bytes = fresh(data);
858
863
  peer.postMessage({ net: "ws-message", nid, data: bytes }, [bytes.buffer]);
859
864
  }
860
865
  flow.inflight += payloadSize(data);
@@ -892,7 +897,7 @@ function serveTransport(peer, transport, { closeWaitMs = CLOSE_WAIT_MS } = {}) {
892
897
  open.delete(nid);
893
898
  peer.postMessage({ net: "end", nid });
894
899
  } else {
895
- const bytes = next.value.slice();
900
+ const bytes = fresh(next.value);
896
901
  peer.postMessage({ net: "chunk", nid, bytes }, [bytes.buffer]);
897
902
  }
898
903
  } catch (e) {
@@ -1095,7 +1100,7 @@ var RemoteTransport = class {
1095
1100
  };
1096
1101
 
1097
1102
  // src/client/protocol.ts
1098
- var PROTOCOL = [1, 8];
1103
+ var PROTOCOL = [1, 9];
1099
1104
  function locksOf(scope = globalThis) {
1100
1105
  const locks = scope.navigator?.locks;
1101
1106
  return typeof locks?.request === "function" ? locks : void 0;
@@ -1799,6 +1804,7 @@ function initCall(options, transport) {
1799
1804
  ...options.hostname !== void 0 ? { hostname: options.hostname } : {},
1800
1805
  ...options.users !== void 0 ? { users: options.users } : {},
1801
1806
  ...options.onDenial ? { denials: true } : {},
1807
+ ...options.approve ? { approve: true } : {},
1802
1808
  ...options.processMounts !== void 0 ? {
1803
1809
  processMounts: typeof options.processMounts === "function" ? "ask" : options.processMounts
1804
1810
  } : {}
@@ -1853,6 +1859,38 @@ async function mountPolicy(options, worker, { id, req }) {
1853
1859
  }
1854
1860
  worker.postMessage({ mountAllowed: { id, allowed } });
1855
1861
  }
1862
+ function approver(approve, worker) {
1863
+ const asked = /* @__PURE__ */ new Map();
1864
+ const handlers = {
1865
+ handle(data) {
1866
+ if ("sudoAsk" in data) void handlers.ask(data.sudoAsk);
1867
+ else if ("sudoAbort" in data) handlers.abort(data.sudoAbort);
1868
+ else return false;
1869
+ return true;
1870
+ },
1871
+ async ask(req) {
1872
+ const control = new AbortController();
1873
+ asked.set(req.id, control);
1874
+ let answer = "deny";
1875
+ try {
1876
+ answer = await approve?.(req, { signal: control.signal });
1877
+ } catch {
1878
+ }
1879
+ asked.delete(req.id);
1880
+ if (control.signal.aborted) return;
1881
+ worker.postMessage({ sudoAnswer: { id: req.id, answer } });
1882
+ },
1883
+ abort({ id, reason }) {
1884
+ asked.get(id)?.abort(reason);
1885
+ asked.delete(id);
1886
+ },
1887
+ close() {
1888
+ for (const control of asked.values()) control.abort("withdrawn");
1889
+ asked.clear();
1890
+ }
1891
+ };
1892
+ return handlers;
1893
+ }
1856
1894
  async function createKernel(options = {}) {
1857
1895
  if (!globalThis.crossOriginIsolated) throw new Error(ISOLATION);
1858
1896
  if (options.hostname !== void 0 && !isHostname(options.hostname)) {
@@ -1865,9 +1903,11 @@ async function createKernel(options = {}) {
1865
1903
  let failure3;
1866
1904
  const dials = dialer((req) => call(req));
1867
1905
  const cdp = cdpBridge(options.cdp);
1906
+ const approvals = approver(options.approve, worker);
1868
1907
  const fail = (error) => {
1869
1908
  dials.reset();
1870
1909
  cdp.close();
1910
+ approvals.close();
1871
1911
  failure3 = error;
1872
1912
  bridge?.close();
1873
1913
  uplinks?.close();
@@ -1902,27 +1942,23 @@ async function createKernel(options = {}) {
1902
1942
  source,
1903
1943
  insert: async () => void await call({ op: "insert", target, source, handle: await chosen(handle) })
1904
1944
  });
1905
- worker.addEventListener(
1906
- "message",
1907
- ({
1908
- data
1909
- }) => {
1910
- if ("denial" in data) return void options.onDenial?.(data.denial);
1911
- if ("net" in data) return bridge?.answer(data);
1912
- if ("uplink" in data) return uplinks?.answer(data);
1913
- if ("cdpOpen" in data) return cdp.open(data);
1914
- if ("mountPolicy" in data) return void policy(data.mountPolicy);
1915
- if ("medium" in data) return pendingMedium(data.medium);
1916
- if ("hostfs" in data) return void grantHostfs(data.hostfs);
1917
- const call2 = pending.get(data.id);
1918
- if (!call2) return;
1919
- if (data.fd !== void 0) return call2.output?.(data.fd, data.bytes);
1920
- if (data.started !== void 0) return call2.started?.(data.started);
1921
- pending.delete(data.id);
1922
- if (data.error !== void 0) call2.reject(failed(data.error, data.code));
1923
- else call2.resolve(data.result);
1924
- }
1925
- );
1945
+ worker.addEventListener("message", ({ data }) => {
1946
+ if ("denial" in data) return void options.onDenial?.(data.denial);
1947
+ if (approvals.handle(data)) return;
1948
+ if ("net" in data) return bridge?.answer(data);
1949
+ if ("uplink" in data) return uplinks?.answer(data);
1950
+ if ("cdpOpen" in data) return cdp.open(data);
1951
+ if ("mountPolicy" in data) return void policy(data.mountPolicy);
1952
+ if ("medium" in data) return pendingMedium(data.medium);
1953
+ if ("hostfs" in data) return void grantHostfs(data.hostfs);
1954
+ const call2 = pending.get(data.id);
1955
+ if (!call2) return;
1956
+ if (data.fd !== void 0) return call2.output?.(data.fd, data.bytes);
1957
+ if (data.started !== void 0) return call2.started?.(data.started);
1958
+ pending.delete(data.id);
1959
+ if (data.error !== void 0) call2.reject(failed(data.error, data.code));
1960
+ else call2.resolve(data.result);
1961
+ });
1926
1962
  worker.addEventListener("error", (event) => {
1927
1963
  event.preventDefault();
1928
1964
  fail(new Error(`slicc-kernel worker failed: ${event.message}`));