@perspective-dev/client 4.5.0 → 4.5.2

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.
@@ -8,6 +8,16 @@ var __export = (target, all) => {
8
8
  import WebSocket, { WebSocketServer as HttpWebSocketServer } from "ws";
9
9
  import stoppable from "stoppable";
10
10
  import { promises as fs } from "node:fs";
11
+ import {
12
+ openSync,
13
+ readSync,
14
+ writeSync,
15
+ closeSync,
16
+ mkdirSync,
17
+ unlinkSync,
18
+ rmSync
19
+ } from "node:fs";
20
+ import os from "node:os";
11
21
  import http from "node:http";
12
22
  import path from "node:path";
13
23
  import { webcrypto } from "node:crypto";
@@ -2025,7 +2035,7 @@ function __wbg_get_imports() {
2025
2035
  const a = state0.a;
2026
2036
  state0.a = 0;
2027
2037
  try {
2028
- return __wasm_bindgen_func_elem_11006(a, state0.b, arg02, arg12);
2038
+ return __wasm_bindgen_func_elem_10943(a, state0.b, arg02, arg12);
2029
2039
  } finally {
2030
2040
  state0.a = a;
2031
2041
  }
@@ -2189,11 +2199,11 @@ function __wbg_get_imports() {
2189
2199
  console.warn(getObject(arg0));
2190
2200
  },
2191
2201
  __wbindgen_cast_0000000000000001: function(arg0, arg1) {
2192
- const ret = makeClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_860, __wasm_bindgen_func_elem_1768);
2202
+ const ret = makeClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_1085, __wasm_bindgen_func_elem_1757);
2193
2203
  return addHeapObject(ret);
2194
2204
  },
2195
2205
  __wbindgen_cast_0000000000000002: function(arg0, arg1) {
2196
- const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_4015, __wasm_bindgen_func_elem_4032);
2206
+ const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_4003, __wasm_bindgen_func_elem_4020);
2197
2207
  return addHeapObject(ret);
2198
2208
  },
2199
2209
  __wbindgen_cast_0000000000000003: function(arg0) {
@@ -2258,15 +2268,15 @@ function __wbg_get_imports() {
2258
2268
  "./perspective-js.wasm_bg.js": import0
2259
2269
  };
2260
2270
  }
2261
- function __wasm_bindgen_func_elem_1768(arg0, arg1) {
2262
- const ret = wasm.__wasm_bindgen_func_elem_1768(arg0, arg1);
2271
+ function __wasm_bindgen_func_elem_1757(arg0, arg1) {
2272
+ const ret = wasm.__wasm_bindgen_func_elem_1757(arg0, arg1);
2263
2273
  return takeObject(ret);
2264
2274
  }
2265
- function __wasm_bindgen_func_elem_4032(arg0, arg1, arg2) {
2266
- wasm.__wasm_bindgen_func_elem_4032(arg0, arg1, addHeapObject(arg2));
2275
+ function __wasm_bindgen_func_elem_4020(arg0, arg1, arg2) {
2276
+ wasm.__wasm_bindgen_func_elem_4020(arg0, arg1, addHeapObject(arg2));
2267
2277
  }
2268
- function __wasm_bindgen_func_elem_11006(arg0, arg1, arg2, arg3) {
2269
- wasm.__wasm_bindgen_func_elem_11006(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
2278
+ function __wasm_bindgen_func_elem_10943(arg0, arg1, arg2, arg3) {
2279
+ wasm.__wasm_bindgen_func_elem_10943(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
2270
2280
  }
2271
2281
  var ClientFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
2272
2282
  }, unregister: () => {
@@ -2666,15 +2676,34 @@ async function load_wasm_stage_0(wasm2) {
2666
2676
  }
2667
2677
 
2668
2678
  // src/ts/wasm/engine.ts
2679
+ var AsyncLock = class {
2680
+ tail = Promise.resolve();
2681
+ run(fn) {
2682
+ const result = this.tail.then(fn, fn);
2683
+ this.tail = result.then(
2684
+ () => void 0,
2685
+ () => void 0
2686
+ );
2687
+ return result;
2688
+ }
2689
+ };
2690
+ async function residency_safepoint(mod, lock, server) {
2691
+ const safepoint = mod.psp_residency_safepoint;
2692
+ if (safepoint) {
2693
+ await lock.run(() => safepoint(server));
2694
+ }
2695
+ }
2669
2696
  var PerspectiveServer = class {
2670
2697
  clients;
2671
2698
  server;
2672
2699
  module;
2673
2700
  on_poll_request;
2701
+ lock;
2674
2702
  constructor(module, options) {
2675
2703
  this.clients = /* @__PURE__ */ new Map();
2676
2704
  this.module = module;
2677
2705
  this.on_poll_request = options?.on_poll_request;
2706
+ this.lock = new AsyncLock();
2678
2707
  this.server = module._psp_new_server(
2679
2708
  !!options?.on_poll_request ? 1 : 0
2680
2709
  );
@@ -2690,11 +2719,14 @@ var PerspectiveServer = class {
2690
2719
  this.server,
2691
2720
  client_id,
2692
2721
  this.clients,
2722
+ this.lock,
2693
2723
  this.on_poll_request && (() => this.on_poll_request(this))
2694
2724
  );
2695
2725
  }
2696
2726
  async poll() {
2697
- const polled = this.module._psp_poll(this.server);
2727
+ const polled = await this.lock.run(
2728
+ () => this.module._psp_poll(this.server)
2729
+ );
2698
2730
  await decode_api_responses(
2699
2731
  this.module,
2700
2732
  polled,
@@ -2702,17 +2734,19 @@ var PerspectiveServer = class {
2702
2734
  await this.clients.get(msg.client_id)(msg.data);
2703
2735
  }
2704
2736
  );
2737
+ await residency_safepoint(this.module, this.lock, this.server);
2705
2738
  }
2706
2739
  delete() {
2707
2740
  this.module._psp_delete_server(this.server);
2708
2741
  }
2709
2742
  };
2710
2743
  var PerspectiveSession = class {
2711
- constructor(mod, server, client_id, client_map, on_poll_request) {
2744
+ constructor(mod, server, client_id, client_map, lock, on_poll_request) {
2712
2745
  this.mod = mod;
2713
2746
  this.server = server;
2714
2747
  this.client_id = client_id;
2715
2748
  this.client_map = client_map;
2749
+ this.lock = lock;
2716
2750
  this.on_poll_request = on_poll_request;
2717
2751
  }
2718
2752
  async handle_request(view) {
@@ -2720,11 +2754,14 @@ var PerspectiveSession = class {
2720
2754
  this.mod,
2721
2755
  view,
2722
2756
  async (viewPtr) => {
2723
- return this.mod._psp_handle_request(
2724
- this.server,
2725
- this.client_id,
2726
- viewPtr,
2727
- this.mod._psp_is_memory64() ? BigInt(view.byteLength) : view.byteLength
2757
+ const len = this.mod._psp_is_memory64() ? BigInt(view.byteLength) : view.byteLength;
2758
+ return this.lock.run(
2759
+ () => this.mod._psp_handle_request(
2760
+ this.server,
2761
+ this.client_id,
2762
+ viewPtr,
2763
+ len
2764
+ )
2728
2765
  );
2729
2766
  }
2730
2767
  );
@@ -2736,9 +2773,12 @@ var PerspectiveSession = class {
2736
2773
  } else {
2737
2774
  await this.poll();
2738
2775
  }
2776
+ await residency_safepoint(this.mod, this.lock, this.server);
2739
2777
  }
2740
2778
  async poll() {
2741
- const polled = this.mod._psp_poll(this.server);
2779
+ const polled = await this.lock.run(
2780
+ () => this.mod._psp_poll(this.server)
2781
+ );
2742
2782
  await decode_api_responses(
2743
2783
  this.mod,
2744
2784
  polled,
@@ -2759,16 +2799,16 @@ async function convert_typed_array_to_pointer(core, array, callback) {
2759
2799
  const ptr = core._psp_alloc(
2760
2800
  core._psp_is_memory64() ? BigInt(array.byteLength) : array.byteLength
2761
2801
  );
2762
- core.HEAPU8.set(array, Number(ptr));
2763
- const msg = await callback(ptr);
2764
- core._psp_free(ptr);
2802
+ core.HEAPU8.set(array, Number(ptr) >>> 0);
2803
+ const msg = await callback(Number(ptr) >>> 0);
2804
+ core._psp_free(Number(ptr) >>> 0);
2765
2805
  return msg;
2766
2806
  }
2767
2807
  async function decode_api_responses(core, ptr, callback) {
2768
2808
  const is_64 = core._psp_is_memory64();
2769
2809
  const response = new DataView(
2770
2810
  core.HEAPU8.buffer,
2771
- Number(ptr),
2811
+ Number(ptr) >>> 0,
2772
2812
  is_64 ? 12 : 8
2773
2813
  );
2774
2814
  const num_msgs = response.getUint32(0, true);
@@ -2837,6 +2877,105 @@ async function perspective_server_poly_default(obj) {
2837
2877
  let psp_module;
2838
2878
  let is_memory64 = false;
2839
2879
  let wasm_memory;
2880
+ const heap2 = () => new Uint8Array(wasm_memory.buffer);
2881
+ const toAddr = (p) => is_memory64 ? Number(p) : p >>> 0;
2882
+ function readCString(p) {
2883
+ const addr = toAddr(p);
2884
+ const h = heap2();
2885
+ let end = addr;
2886
+ while (h[end]) ++end;
2887
+ return UTF8Decoder.decode(h.subarray(addr, end));
2888
+ }
2889
+ const disk_helpers = { heap: heap2, toAddr, readCString };
2890
+ function makeOpfsBridge() {
2891
+ async function opfsOpenFile(name, create) {
2892
+ const parts = name.split("/").filter((s) => s.length > 0);
2893
+ let dir = await navigator.storage.getDirectory();
2894
+ for (let i = 0; i < parts.length - 1; i++) {
2895
+ dir = await dir.getDirectoryHandle(parts[i], { create });
2896
+ }
2897
+ return await dir.getFileHandle(parts[parts.length - 1], { create });
2898
+ }
2899
+ const handles = /* @__PURE__ */ new Map();
2900
+ return {
2901
+ async ensureOpen(name) {
2902
+ if (handles.has(name)) {
2903
+ return;
2904
+ }
2905
+ const fh = await opfsOpenFile(name, true);
2906
+ const ah = await fh.createSyncAccessHandle();
2907
+ handles.set(name, ah);
2908
+ },
2909
+ store(namePtr, dataPtr, len) {
2910
+ try {
2911
+ const ah = handles.get(readCString(namePtr));
2912
+ if (!ah) {
2913
+ return -1;
2914
+ }
2915
+ ah.truncate(len);
2916
+ if (len > 0) {
2917
+ const addr = toAddr(dataPtr);
2918
+ ah.write(heap2().subarray(addr, addr + len), { at: 0 });
2919
+ }
2920
+ ah.flush();
2921
+ return len;
2922
+ } catch (e) {
2923
+ err("psp_opfs_store failed", e);
2924
+ return -1;
2925
+ }
2926
+ },
2927
+ load(namePtr, dataPtr, len) {
2928
+ try {
2929
+ if (len <= 0) {
2930
+ return 0;
2931
+ }
2932
+ const ah = handles.get(readCString(namePtr));
2933
+ if (!ah) {
2934
+ return 0;
2935
+ }
2936
+ const addr = toAddr(dataPtr);
2937
+ return ah.read(heap2().subarray(addr, addr + len), {
2938
+ at: 0
2939
+ });
2940
+ } catch (e) {
2941
+ return 0;
2942
+ }
2943
+ },
2944
+ remove(namePtr) {
2945
+ try {
2946
+ const name = readCString(namePtr);
2947
+ const ah = handles.get(name);
2948
+ if (ah) {
2949
+ try {
2950
+ ah.close();
2951
+ } catch (e) {
2952
+ }
2953
+ handles.delete(name);
2954
+ }
2955
+ void (async () => {
2956
+ try {
2957
+ const parts = name.split("/").filter((s) => s.length > 0);
2958
+ let dir = await navigator.storage.getDirectory();
2959
+ for (let i = 0; i < parts.length - 1; i++) {
2960
+ dir = await dir.getDirectoryHandle(parts[i], {
2961
+ create: false
2962
+ });
2963
+ }
2964
+ await dir.removeEntry(parts[parts.length - 1]);
2965
+ } catch (e) {
2966
+ }
2967
+ })();
2968
+ } catch (e) {
2969
+ }
2970
+ }
2971
+ };
2972
+ }
2973
+ const disk = obj.make_disk_bridge ? obj.make_disk_bridge(disk_helpers) : makeOpfsBridge();
2974
+ const opfs_imports = {
2975
+ psp_opfs_store: disk.store,
2976
+ psp_opfs_load: disk.load,
2977
+ psp_opfs_remove: disk.remove
2978
+ };
2840
2979
  const module = {
2841
2980
  HaveOffsetConverter() {
2842
2981
  console.error("HaveOffsetConverter");
@@ -2972,7 +3111,8 @@ async function perspective_server_poly_default(obj) {
2972
3111
  proc_exit(e) {
2973
3112
  console.error("proc_exit", e);
2974
3113
  return 0;
2975
- }
3114
+ },
3115
+ ...opfs_imports
2976
3116
  };
2977
3117
  const x = await obj.instantiateWasm(
2978
3118
  { env: module, wasi_snapshot_preview1: module },
@@ -2987,9 +3127,26 @@ async function perspective_server_poly_default(obj) {
2987
3127
  for (const [name, func] of Object.entries(x)) {
2988
3128
  extensions[`_${name}`] = func;
2989
3129
  }
3130
+ const psp_residency_safepoint = async (server) => {
3131
+ const mod = psp_module;
3132
+ if (!mod.psp_residency_prepare) {
3133
+ return;
3134
+ }
3135
+ const n = Number(mod.psp_residency_prepare(server));
3136
+ for (let i = 0; i < n; i++) {
3137
+ const fname = readCString(
3138
+ mod.psp_residency_victim_fname(server, i)
3139
+ );
3140
+ if (fname) {
3141
+ await disk.ensureOpen(fname);
3142
+ }
3143
+ }
3144
+ mod.psp_residency_commit(server);
3145
+ };
2990
3146
  return {
2991
3147
  ...x,
2992
3148
  ...extensions,
3149
+ psp_residency_safepoint,
2993
3150
  get HEAPU8() {
2994
3151
  return new Uint8Array(wasm_memory.buffer);
2995
3152
  }
@@ -2997,11 +3154,12 @@ async function perspective_server_poly_default(obj) {
2997
3154
  }
2998
3155
 
2999
3156
  // src/ts/wasm/emscripten_api.ts
3000
- async function compile_perspective(wasmBinary) {
3157
+ async function compile_perspective(wasmBinary, opts) {
3001
3158
  const module = await perspective_server_poly_default({
3002
3159
  locateFile(x) {
3003
3160
  return x;
3004
3161
  },
3162
+ make_disk_bridge: opts?.make_disk_bridge,
3005
3163
  instantiateWasm: async (imports, receive) => {
3006
3164
  imports["env"] = {
3007
3165
  ...imports["env"],
@@ -3132,9 +3290,100 @@ if (!globalThis.crypto) {
3132
3290
  }
3133
3291
  var uncompressed_client_wasm = await fs.readFile(path.join(__dirname, "../../dist/wasm/perspective-js.wasm")).then((buffer) => load_wasm_stage_0(buffer.buffer));
3134
3292
  await __wbg_init({ module_or_path: uncompressed_client_wasm });
3293
+ function make_node_disk_bridge({
3294
+ heap: heap2,
3295
+ toAddr,
3296
+ readCString
3297
+ }) {
3298
+ const root = path.join(os.tmpdir(), `perspective-${process.pid}`);
3299
+ const resolve_path = (name) => {
3300
+ const p = path.join(root, name);
3301
+ if (!p.startsWith(root)) {
3302
+ throw new Error(`refusing disk path outside root: ${name}`);
3303
+ }
3304
+ return p;
3305
+ };
3306
+ let cleaned = false;
3307
+ const cleanup = () => {
3308
+ if (cleaned) return;
3309
+ cleaned = true;
3310
+ try {
3311
+ rmSync(root, { recursive: true, force: true });
3312
+ } catch (e) {
3313
+ }
3314
+ };
3315
+ process.on("exit", cleanup);
3316
+ process.on("SIGINT", () => {
3317
+ cleanup();
3318
+ process.exit(130);
3319
+ });
3320
+ return {
3321
+ // node:fs is synchronous — nothing to pre-open between safepoint phases.
3322
+ async ensureOpen(_name) {
3323
+ },
3324
+ store(namePtr, dataPtr, len) {
3325
+ try {
3326
+ const p = resolve_path(readCString(namePtr));
3327
+ mkdirSync(path.dirname(p), { recursive: true });
3328
+ const fd = openSync(p, "w");
3329
+ try {
3330
+ if (len > 0) {
3331
+ const addr = toAddr(dataPtr);
3332
+ const view = heap2().subarray(addr, addr + len);
3333
+ let off = 0;
3334
+ while (off < len) {
3335
+ off += writeSync(fd, view, off, len - off, off);
3336
+ }
3337
+ }
3338
+ } finally {
3339
+ closeSync(fd);
3340
+ }
3341
+ return len;
3342
+ } catch (e) {
3343
+ console.error("node disk store failed", e);
3344
+ return -1;
3345
+ }
3346
+ },
3347
+ load(namePtr, dataPtr, len) {
3348
+ try {
3349
+ if (len <= 0) return 0;
3350
+ let fd;
3351
+ try {
3352
+ fd = openSync(resolve_path(readCString(namePtr)), "r");
3353
+ } catch (e) {
3354
+ return 0;
3355
+ }
3356
+ try {
3357
+ const addr = toAddr(dataPtr);
3358
+ const view = heap2().subarray(addr, addr + len);
3359
+ let off = 0;
3360
+ let n;
3361
+ while (off < len && (n = readSync(fd, view, off, len - off, off)) > 0) {
3362
+ off += n;
3363
+ }
3364
+ return off;
3365
+ } finally {
3366
+ closeSync(fd);
3367
+ }
3368
+ } catch (e) {
3369
+ return 0;
3370
+ }
3371
+ },
3372
+ remove(namePtr) {
3373
+ try {
3374
+ unlinkSync(resolve_path(readCString(namePtr)));
3375
+ } catch (e) {
3376
+ }
3377
+ }
3378
+ };
3379
+ }
3135
3380
  var SYNC_MODULE = await fs.readFile(
3136
3381
  resolve("@perspective-dev/server/dist/wasm/perspective-server.wasm")
3137
- ).then((buffer) => load_wasm_stage_0(buffer.buffer)).then((buffer) => compile_perspective(buffer.buffer));
3382
+ ).then((buffer) => load_wasm_stage_0(buffer.buffer)).then(
3383
+ (buffer) => compile_perspective(buffer.buffer, {
3384
+ make_disk_bridge: make_node_disk_bridge
3385
+ })
3386
+ );
3138
3387
  var SYNC_CLIENT;
3139
3388
  var SYNC_SERVER = new PerspectiveServer(SYNC_MODULE);
3140
3389
  var SYNC_SESSION = SYNC_SERVER.make_session(