@loro-dev/flock-wasm 0.1.1 → 0.1.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.
package/base64/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { RawFlock, callPendingEvents } from "./wasm";
2
- import { EventBatcher } from "./event-batcher";
1
+ import { RawFlock, callPendingEvents } from "./wasm.js";
2
+ import { EventBatcher } from "./event-batcher.js";
3
3
  export function encodeVersionVector(vector) {
4
4
  return encodeVersionVectorBinary(vector);
5
5
  }
@@ -1,5 +1,198 @@
1
- import * as wasm from "./flock_wasm_bg.wasm";
2
- export * from "./flock_wasm_bg.js";
1
+ import * as wasmModule from "./flock_wasm_bg.wasm";
2
+ import * as wasmBg from "./flock_wasm_bg.js";
3
3
  import { __wbg_set_wasm } from "./flock_wasm_bg.js";
4
- __wbg_set_wasm(wasm);
5
- wasm.__wbindgen_start();
4
+
5
+ export * from "./flock_wasm_bg.js";
6
+
7
+ let wasm;
8
+ let initPromise;
9
+
10
+ function isRecordLike(value) {
11
+ return (typeof value === "object" || typeof value === "function") && value !== null;
12
+ }
13
+
14
+ function toWasmExports(value) {
15
+ if (value instanceof WebAssembly.Instance) {
16
+ return value.exports;
17
+ }
18
+ if (!isRecordLike(value)) {
19
+ return undefined;
20
+ }
21
+ if (value.memory instanceof WebAssembly.Memory) {
22
+ return value;
23
+ }
24
+ return undefined;
25
+ }
26
+
27
+ function pickExistingWasmExports(value) {
28
+ const queue = [value];
29
+ const seen = new Set();
30
+ while (queue.length > 0) {
31
+ const current = queue.shift();
32
+ const exports = toWasmExports(current);
33
+ if (exports !== undefined) {
34
+ return exports;
35
+ }
36
+ if (!isRecordLike(current)) {
37
+ continue;
38
+ }
39
+ if (seen.has(current)) {
40
+ continue;
41
+ }
42
+ seen.add(current);
43
+ if ("default" in current) {
44
+ queue.push(current.default);
45
+ }
46
+ if ("instance" in current) {
47
+ queue.push(current.instance);
48
+ }
49
+ if ("module" in current) {
50
+ queue.push(current.module);
51
+ }
52
+ if ("exports" in current) {
53
+ queue.push(current.exports);
54
+ }
55
+ }
56
+ return undefined;
57
+ }
58
+
59
+ function resolveModuleSource(moduleSource) {
60
+ if (typeof moduleSource !== "string") {
61
+ return moduleSource;
62
+ }
63
+ if (typeof URL !== "function") {
64
+ return moduleSource;
65
+ }
66
+ try {
67
+ return new URL(moduleSource);
68
+ } catch (_error) {
69
+ return new URL(moduleSource, import.meta.url);
70
+ }
71
+ }
72
+
73
+ async function readFileUrlBytes(fileUrl) {
74
+ const { readFile } = await import("node:fs/promises");
75
+ const { fileURLToPath } = await import("node:url");
76
+ const filePath = fileURLToPath(fileUrl);
77
+ return readFile(filePath);
78
+ }
79
+
80
+ async function instantiateWasm(moduleSource) {
81
+ if (moduleSource && typeof moduleSource.then === "function") {
82
+ moduleSource = await moduleSource;
83
+ }
84
+
85
+ const existing = pickExistingWasmExports(moduleSource);
86
+ if (existing !== undefined) {
87
+ return existing;
88
+ }
89
+
90
+ // Some bundlers expose wasm assets as namespace objects with a default export.
91
+ if (isRecordLike(moduleSource)) {
92
+ const nestedCandidates = [
93
+ "default",
94
+ "module_or_path",
95
+ "module",
96
+ "wasm",
97
+ "url",
98
+ ];
99
+ for (const key of nestedCandidates) {
100
+ if (!(key in moduleSource)) {
101
+ continue;
102
+ }
103
+ const nested = moduleSource[key];
104
+ if (nested === undefined || nested === moduleSource) {
105
+ continue;
106
+ }
107
+ return instantiateWasm(nested);
108
+ }
109
+ }
110
+
111
+ const imports = { "./flock_wasm_bg.js": wasmBg };
112
+
113
+ if (moduleSource instanceof WebAssembly.Module) {
114
+ return new WebAssembly.Instance(moduleSource, imports).exports;
115
+ }
116
+
117
+ if (
118
+ moduleSource instanceof ArrayBuffer ||
119
+ ArrayBuffer.isView(moduleSource)
120
+ ) {
121
+ const result = await WebAssembly.instantiate(moduleSource, imports);
122
+ if (result instanceof WebAssembly.Instance) {
123
+ return result.exports;
124
+ }
125
+ return result.instance.exports;
126
+ }
127
+
128
+ if (
129
+ typeof Response === "function" &&
130
+ moduleSource instanceof Response
131
+ ) {
132
+ if (typeof WebAssembly.instantiateStreaming === "function") {
133
+ try {
134
+ const responseForStreaming = moduleSource.clone();
135
+ const streaming = await WebAssembly.instantiateStreaming(
136
+ responseForStreaming,
137
+ imports,
138
+ );
139
+ return streaming.instance.exports;
140
+ } catch (_error) {
141
+ // Fall back to arrayBuffer in runtimes with strict MIME handling.
142
+ }
143
+ }
144
+ const bytes = await moduleSource.arrayBuffer();
145
+ const result = await WebAssembly.instantiate(bytes, imports);
146
+ if (result instanceof WebAssembly.Instance) {
147
+ return result.exports;
148
+ }
149
+ return result.instance.exports;
150
+ }
151
+
152
+ if (
153
+ typeof moduleSource === "string" ||
154
+ (typeof URL === "function" && moduleSource instanceof URL) ||
155
+ (typeof Request === "function" && moduleSource instanceof Request)
156
+ ) {
157
+ let requestTarget = moduleSource;
158
+ if (typeof moduleSource === "string") {
159
+ requestTarget = resolveModuleSource(moduleSource);
160
+ }
161
+ if (typeof URL === "function" && requestTarget instanceof URL && requestTarget.protocol === "file:") {
162
+ const bytes = await readFileUrlBytes(requestTarget);
163
+ return instantiateWasm(bytes);
164
+ }
165
+ if (typeof fetch !== "function") {
166
+ throw new TypeError("fetch is not available to load the wasm module");
167
+ }
168
+ return instantiateWasm(fetch(requestTarget));
169
+ }
170
+
171
+ throw new TypeError("Unsupported wasm module shape for flock-wasm bundler init");
172
+ }
173
+
174
+ async function __wbg_init(moduleOrExports = wasmModule) {
175
+ if (wasm !== undefined) {
176
+ return wasm;
177
+ }
178
+ if (initPromise !== undefined) {
179
+ return initPromise;
180
+ }
181
+ initPromise = (async () => {
182
+ const resolved = await instantiateWasm(moduleOrExports);
183
+ __wbg_set_wasm(resolved);
184
+ if (typeof resolved.__wbindgen_start === "function") {
185
+ resolved.__wbindgen_start();
186
+ }
187
+ wasm = resolved;
188
+ return wasm;
189
+ })();
190
+ try {
191
+ return await initPromise;
192
+ } catch (error) {
193
+ initPromise = undefined;
194
+ throw error;
195
+ }
196
+ }
197
+
198
+ export default __wbg_init;
package/bundler/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { RawFlock, callPendingEvents } from "./wasm";
2
- import { EventBatcher } from "./event-batcher";
1
+ import { RawFlock, callPendingEvents } from "./wasm.js";
2
+ import { EventBatcher } from "./event-batcher.js";
3
3
  export function encodeVersionVector(vector) {
4
4
  return encodeVersionVectorBinary(vector);
5
5
  }
package/nodejs/index.js CHANGED
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Flock = void 0;
4
4
  exports.encodeVersionVector = encodeVersionVector;
5
5
  exports.decodeVersionVector = decodeVersionVector;
6
- const wasm_1 = require("./wasm");
7
- const event_batcher_1 = require("./event-batcher");
6
+ const wasm_js_1 = require("./wasm.js");
7
+ const event_batcher_js_1 = require("./event-batcher.js");
8
8
  function encodeVersionVector(vector) {
9
9
  return encodeVersionVectorBinary(vector);
10
10
  }
@@ -552,8 +552,8 @@ class Flock {
552
552
  nativeSubscriberId;
553
553
  eventBatcher;
554
554
  constructor(peerId) {
555
- this.inner = new wasm_1.RawFlock(normalizePeerId(peerId));
556
- this.eventBatcher = new event_batcher_1.EventBatcher({
555
+ this.inner = new wasm_js_1.RawFlock(normalizePeerId(peerId));
556
+ this.eventBatcher = new event_batcher_js_1.EventBatcher({
557
557
  runtime: defaultEventBatcherRuntime,
558
558
  emit: (source, events) => {
559
559
  this.deliverBatch({ source, events });
@@ -565,7 +565,7 @@ class Flock {
565
565
  flock.inner = inner;
566
566
  flock.listeners = new Set();
567
567
  flock.nativeSubscriberId = undefined;
568
- flock.eventBatcher = new event_batcher_1.EventBatcher({
568
+ flock.eventBatcher = new event_batcher_js_1.EventBatcher({
569
569
  runtime: defaultEventBatcherRuntime,
570
570
  emit: (source, events) => {
571
571
  flock.deliverBatch({ source, events });
@@ -574,11 +574,11 @@ class Flock {
574
574
  return flock;
575
575
  }
576
576
  static fromJson(bundle, peerId) {
577
- const inner = wasm_1.RawFlock.fromJson(bundle, normalizePeerId(peerId));
577
+ const inner = wasm_js_1.RawFlock.fromJson(bundle, normalizePeerId(peerId));
578
578
  return Flock.fromInner(inner);
579
579
  }
580
580
  static fromFile(bytes, peerId) {
581
- const inner = wasm_1.RawFlock.fromFile(normalizePeerId(peerId), bytes);
581
+ const inner = wasm_js_1.RawFlock.fromFile(normalizePeerId(peerId), bytes);
582
582
  return Flock.fromInner(inner);
583
583
  }
584
584
  checkInvariants() {
@@ -816,7 +816,7 @@ class Flock {
816
816
  return;
817
817
  }
818
818
  this.nativeSubscriberId = this.inner.subscribe((payload) => {
819
- (0, wasm_1.callPendingEvents)();
819
+ (0, wasm_js_1.callPendingEvents)();
820
820
  const batch = decodeEventBatch(payload);
821
821
  this.handleBatch(batch);
822
822
  });
@@ -975,7 +975,7 @@ function decorateMethod(prototype, method) {
975
975
  if (result && typeof result.then === "function") {
976
976
  void result
977
977
  .finally(() => {
978
- (0, wasm_1.callPendingEvents)();
978
+ (0, wasm_js_1.callPendingEvents)();
979
979
  })
980
980
  .catch(() => {
981
981
  // Ignore: this promise is only for triggering pending event draining.
@@ -983,7 +983,7 @@ function decorateMethod(prototype, method) {
983
983
  });
984
984
  }
985
985
  else {
986
- (0, wasm_1.callPendingEvents)();
986
+ (0, wasm_js_1.callPendingEvents)();
987
987
  }
988
988
  }
989
989
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loro-dev/flock-wasm",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "WASM bindings for flock-rs.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
package/web/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { RawFlock, callPendingEvents } from "./wasm";
2
- import { EventBatcher } from "./event-batcher";
1
+ import { RawFlock, callPendingEvents } from "./wasm.js";
2
+ import { EventBatcher } from "./event-batcher.js";
3
3
  export function encodeVersionVector(vector) {
4
4
  return encodeVersionVectorBinary(vector);
5
5
  }