@openheart/tavio-renderer 2.3.1-withWasm → 2.3.2-with-wasm

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.
@@ -0,0 +1,117 @@
1
+ let wasm = null;
2
+ globalThis.allocateSharedArrayBuffer = (oldSharedArrayBuffer, newByteSize) => {
3
+ const newSharedArrayBuffer = new SharedArrayBuffer(newByteSize);
4
+ if (oldSharedArrayBuffer) {
5
+ new Uint8Array(newSharedArrayBuffer).set(
6
+ new Uint8Array(oldSharedArrayBuffer)
7
+ );
8
+ }
9
+ return newSharedArrayBuffer;
10
+ };
11
+ self.onmessage = async (e) => {
12
+ switch (e.data.kind) {
13
+ case "init":
14
+ await init(e.data.options);
15
+ break;
16
+ case "registerEventHandler":
17
+ wasm.registerEventHandler((e2) => {
18
+ const message = {
19
+ category: "wasmCallback",
20
+ data: e2
21
+ };
22
+ postMessage(message, collectTransferables(e2));
23
+ });
24
+ break;
25
+ default:
26
+ if ("id" in e.data && "args" in e.data) {
27
+ const { kind, id, args } = e.data;
28
+ try {
29
+ const fn = wasm[kind];
30
+ const result = await fn(...args);
31
+ const message = { kind, id, result };
32
+ postMessage(message);
33
+ } catch (error) {
34
+ const message = {
35
+ category: "wasmError",
36
+ id,
37
+ error
38
+ };
39
+ postMessage(message);
40
+ }
41
+ }
42
+ break;
43
+ }
44
+ };
45
+ async function importWasmBindings(useSharedArrayBuffer) {
46
+ const href = useSharedArrayBuffer ? new URL(
47
+ /* @vite-ignore */
48
+ "./wasm/pkg/thread/tavio.js",
49
+ import.meta.url
50
+ ).href : new URL(
51
+ /* @vite-ignore */
52
+ "./wasm/pkg/no-thread/tavio.js",
53
+ import.meta.url
54
+ ).href;
55
+ return await import(
56
+ /* @vite-ignore */
57
+ href
58
+ );
59
+ }
60
+ async function init(options) {
61
+ const useSharedArrayBuffer = options?.useSharedArrayBuffer == true;
62
+ const threadCount = Math.min(
63
+ options?.threadCount ?? 1,
64
+ navigator.hardwareConcurrency
65
+ );
66
+ const isParallel = useSharedArrayBuffer && threadCount > 1;
67
+ wasm = await importWasmBindings(useSharedArrayBuffer);
68
+ try {
69
+ await wasm.default();
70
+ console.info(
71
+ ` WASM${useSharedArrayBuffer ? " (thread)" : "(no-thread)"} initialized successfully`
72
+ );
73
+ } catch (e) {
74
+ console.error("[WasmWorker] Failed to initialize WASM (tavio_bg.wasm):", e);
75
+ console.error("[WasmWorker] This error typically occurs when:");
76
+ console.error(
77
+ " 1. The tavio_bg.wasm file is not deployed to the correct path"
78
+ );
79
+ console.error(" 2. CORS policy is blocking the WASM file");
80
+ console.error(
81
+ " 3. The WASM file has incorrect MIME type (should be application/wasm)"
82
+ );
83
+ throw e;
84
+ }
85
+ if (isParallel) {
86
+ try {
87
+ console.info(
88
+ `[WasmWorker] Initializing thread pool with ${threadCount} threads...`
89
+ );
90
+ wasm.initThreadPool(threadCount);
91
+ console.info("[WasmWorker] Thread pool initialized successfully");
92
+ } catch (e) {
93
+ console.error("[WasmWorker] Failed to initialize thread pool:", e);
94
+ console.error("[WasmWorker] This error typically occurs when:");
95
+ console.error(
96
+ " 1. workerHelpers.js is not deployed to the correct path"
97
+ );
98
+ console.error(
99
+ " 2. SharedArrayBuffer is not available (requires secure context and COOP/COEP headers)"
100
+ );
101
+ throw e;
102
+ }
103
+ }
104
+ postMessage({ category: "wasmLoaded" });
105
+ }
106
+ function collectTransferables(value) {
107
+ if (!value || typeof value !== "object") return [];
108
+ const set = /* @__PURE__ */ new Set();
109
+ for (const v of Object.values(value)) {
110
+ if (v instanceof ArrayBuffer) {
111
+ set.add(v);
112
+ } else if (ArrayBuffer.isView(v)) {
113
+ set.add(v.buffer);
114
+ }
115
+ }
116
+ return Array.from(set);
117
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openheart/tavio-renderer",
3
- "version": "2.3.1-withWasm",
3
+ "version": "2.3.2-with-wasm",
4
4
  "author": "OpenHeart Inc.",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Rendering Engine for Tavio Format 3D data",
@@ -23,6 +23,7 @@
23
23
  "build:iife": "vite build -c vite.config.iife.ts",
24
24
  "build:wasm": "wasm/build.sh $@",
25
25
  "start": "vite -c vite.config.demo.ts",
26
+ "demo-esm": "vite -c demo-esm/vite.config.ts",
26
27
  "clean": "(test -d build && rm -r build) || true",
27
28
  "prepublishOnly": "cp README.md .README.md.bak && cp npm/README.md README.md",
28
29
  "postpublish": "mv .README.md.bak README.md"