loro-crdt 1.8.9 → 1.9.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.
@@ -1,34 +1,96 @@
1
- // See https://github.com/loro-dev/loro/issues/440
2
- // Without this patch, Cloudflare Worker would raise issue like: "Uncaught TypeError: wasm2.__wbindgen_start is not a function"
3
- import * as wasm from "./loro_wasm_bg.wasm";
4
- import * as imports from "./loro_wasm_bg.js";
1
+ import * as rawWasm from './loro_wasm_bg.wasm';
2
+ import * as imports from './loro_wasm_bg.js';
5
3
 
6
- if (wasm.__wbindgen_start) {
7
- imports.__wbg_set_wasm(wasm);
8
- wasm.__wbindgen_start();
9
- } else if ("Bun" in globalThis) {
10
- const { instance } = await WebAssembly.instantiateStreaming(
11
- fetch(Bun.pathToFileURL(wasm.default)),
12
- {
13
- "./loro_wasm_bg.js": imports,
14
- },
15
- );
16
- imports.__wbg_set_wasm(instance.exports);
4
+ // Normalize how bundlers expose the wasm module/exports.
5
+ const toModuleOrExports = (wasm) => {
6
+ if (!wasm) return wasm;
7
+ if (wasm instanceof WebAssembly.Module) return wasm;
8
+ if (typeof wasm === 'object' && 'default' in wasm) {
9
+ return wasm.default ?? wasm;
10
+ }
11
+ // rsbuild doesn't provide a default export when importing wasm.
12
+ return wasm;
13
+ };
14
+
15
+ const wasmModuleOrExports = toModuleOrExports(rawWasm);
16
+
17
+ // Helper: ensure we end up with exports + optionally run externref init.
18
+ const finalize = (exports) => {
19
+ imports.__wbg_set_wasm(exports);
20
+ if (typeof imports.__wbindgen_init_externref_table === 'function') {
21
+ imports.__wbindgen_init_externref_table();
22
+ }
23
+ tryStart(imports);
24
+ };
25
+
26
+ function tryStart(imports) {
27
+ if (typeof imports.__wbindgen_start === 'function') {
28
+ // Some bundlers require explicit start invocation.
29
+ imports.__wbindgen_start();
30
+ }
31
+ }
17
32
 
33
+ if (wasmModuleOrExports && wasmModuleOrExports.__wbindgen_start) {
34
+ // See https://github.com/loro-dev/loro/issues/440
35
+ // Without this patch, Cloudflare Worker would raise issue like: "Uncaught TypeError: wasm2.__wbindgen_start is not a function"
36
+ // Already the initialized exports object (Cloudflare Workers path).
37
+ finalize(wasmModuleOrExports);
38
+ } else if ('Bun' in globalThis) {
18
39
  // Bun's wasm runtime (1.3.0 as of Oct 2025) sometimes reads externref slot 1
19
40
  // (reserved for booleans by wasm-bindgen) as the global object, causing APIs
20
41
  // like `LoroText.toDelta()` to return cyclic structures. Re-running the
21
42
  // wasm-bindgen externref table initializer after instantiation resets the
22
43
  // table so booleans stay primitives and avoids the infinite recursion seen in
23
44
  // Bun tests during `pnpm release-wasm`.
24
- if (typeof imports.__wbindgen_init_externref_table === "function") {
25
- imports.__wbindgen_init_externref_table();
45
+ let instance;
46
+ if (wasmModuleOrExports instanceof WebAssembly.Module) {
47
+ ({ instance } = await WebAssembly.instantiate(wasmModuleOrExports, {
48
+ './loro_wasm_bg.js': imports,
49
+ }));
50
+ } else {
51
+ const url = Bun.pathToFileURL(wasmModuleOrExports);
52
+ ({ instance } = await WebAssembly.instantiateStreaming(fetch(url), {
53
+ './loro_wasm_bg.js': imports,
54
+ }));
26
55
  }
56
+ finalize(instance.exports);
27
57
  } else {
28
- const wkmod = await import("./loro_wasm_bg.wasm");
29
- const instance = new WebAssembly.Instance(wkmod.default, {
30
- "./loro_wasm_bg.js": imports,
31
- });
32
- imports.__wbg_set_wasm(instance.exports);
58
+ // Browser/node-like bundlers: either we already have exports, or a Module/URL.
59
+ const wkmod =
60
+ wasmModuleOrExports instanceof WebAssembly.Module
61
+ ? wasmModuleOrExports
62
+ : await import('./loro_wasm_bg.wasm');
63
+ const module =
64
+ wkmod instanceof WebAssembly.Module
65
+ ? wkmod
66
+ : (wkmod && wkmod.default) || wkmod;
67
+ let instance;
68
+ if (module instanceof WebAssembly.Instance) {
69
+ instance = module;
70
+ } else if (module instanceof WebAssembly.Module) {
71
+ instance = await WebAssembly.instantiate(module, {
72
+ './loro_wasm_bg.js': imports,
73
+ });
74
+ } else if (module instanceof ArrayBuffer || ArrayBuffer.isView(module)) {
75
+ const { instance: inst } = await WebAssembly.instantiate(module, {
76
+ './loro_wasm_bg.js': imports,
77
+ });
78
+ instance = inst;
79
+ } else if (typeof module === 'string' || module instanceof URL) {
80
+ const response = await fetch(module);
81
+ const { instance: inst } = await WebAssembly.instantiateStreaming(
82
+ response,
83
+ {
84
+ './loro_wasm_bg.js': imports,
85
+ }
86
+ );
87
+ instance = inst;
88
+ } else {
89
+ console.error('Unsupported wasm import type:', module);
90
+ throw new Error('Unsupported wasm import type: ' + typeof module);
91
+ }
92
+
93
+ finalize(instance.exports ?? instance);
33
94
  }
34
- export * from "./loro_wasm_bg.js";
95
+
96
+ export * from './loro_wasm_bg.js';