@crdt-sync/core 0.3.1 → 0.3.3

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,30 +1,529 @@
1
+ /* @ts-self-types="./crdt_sync.d.ts" */
2
+
1
3
  /**
2
- * Stub WASM module for @crdt-sync/core.
3
- *
4
- * This file is a placeholder that allows the package to be imported in
5
- * environments where the real compiled WebAssembly is not yet available
6
- * (e.g. test environments that mock the module, or bundler/SSR setups).
4
+ * A WebAssembly-compatible wrapper around [`StateStore`].
7
5
  *
8
- * In production, replace this file with the real wasm-pack–generated output
9
- * (run `npm run build:wasm` from the workspace root).
6
+ * Methods accept and return JSON-encoded strings at the Wasm boundary so that
7
+ * [`Envelope`] payloads and CRDT values can be exchanged between Rust and
8
+ * JavaScript without sharing memory structures directly.
10
9
  */
11
-
12
10
  export class WasmStateStore {
13
- constructor(_nodeId) {
14
- throw new Error(
15
- '@crdt-sync/core: WasmStateStore is not available. ' +
16
- 'Please build the WebAssembly module with `npm run build:wasm`.'
17
- );
11
+ __destroy_into_raw() {
12
+ const ptr = this.__wbg_ptr;
13
+ this.__wbg_ptr = 0;
14
+ WasmStateStoreFinalization.unregister(this);
15
+ return ptr;
16
+ }
17
+ free() {
18
+ const ptr = this.__destroy_into_raw();
19
+ wasm.__wbg_wasmstatestore_free(ptr, 0);
20
+ }
21
+ /**
22
+ * Apply a remote [`Envelope`] (serialised as a JSON string) to this store.
23
+ *
24
+ * Throws a JavaScript error if the JSON cannot be deserialised.
25
+ * @param {string} envelope_json
26
+ */
27
+ apply_envelope(envelope_json) {
28
+ const ptr0 = passStringToWasm0(envelope_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
29
+ const len0 = WASM_VECTOR_LEN;
30
+ const ret = wasm.wasmstatestore_apply_envelope(this.__wbg_ptr, ptr0, len0);
31
+ if (ret[1]) {
32
+ throw takeFromExternrefTable0(ret[0]);
33
+ }
34
+ }
35
+ /**
36
+ * Return the current Lamport clock value.
37
+ *
38
+ * Returned as `f64` because JavaScript's `Number` type cannot safely
39
+ * represent all `u64` values. Values up to `2^53 − 1`
40
+ * (`Number.MAX_SAFE_INTEGER`) are represented exactly. For distributed
41
+ * systems that could conceivably tick the clock beyond that threshold,
42
+ * treat the returned value as approximate or use `BigInt` on the JS side.
43
+ * @returns {number}
44
+ */
45
+ clock() {
46
+ const ret = wasm.wasmstatestore_clock(this.__wbg_ptr);
47
+ return ret;
48
+ }
49
+ /**
50
+ * Read the current value of a named LWW register as a JSON string.
51
+ *
52
+ * Returns `undefined` in JavaScript if the key has never been written.
53
+ * @param {string} key
54
+ * @returns {string | undefined}
55
+ */
56
+ get_register(key) {
57
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
58
+ const len0 = WASM_VECTOR_LEN;
59
+ const ret = wasm.wasmstatestore_get_register(this.__wbg_ptr, ptr0, len0);
60
+ let v2;
61
+ if (ret[0] !== 0) {
62
+ v2 = getStringFromWasm0(ret[0], ret[1]).slice();
63
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
64
+ }
65
+ return v2;
66
+ }
67
+ /**
68
+ * Create a new `WasmStateStore` for the given node identifier.
69
+ * @param {string} node_id
70
+ */
71
+ constructor(node_id) {
72
+ const ptr0 = passStringToWasm0(node_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
73
+ const len0 = WASM_VECTOR_LEN;
74
+ const ret = wasm.wasmstatestore_new(ptr0, len0);
75
+ this.__wbg_ptr = ret >>> 0;
76
+ WasmStateStoreFinalization.register(this, this.__wbg_ptr, this);
77
+ return this;
78
+ }
79
+ /**
80
+ * Delete the element at visible `index` in the named RGA sequence.
81
+ *
82
+ * Returns the resulting [`Envelope`] as a JSON string, or `undefined` if
83
+ * `index` is out of bounds.
84
+ * @param {string} key
85
+ * @param {number} index
86
+ * @returns {string | undefined}
87
+ */
88
+ seq_delete(key, index) {
89
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
90
+ const len0 = WASM_VECTOR_LEN;
91
+ const ret = wasm.wasmstatestore_seq_delete(this.__wbg_ptr, ptr0, len0, index);
92
+ let v2;
93
+ if (ret[0] !== 0) {
94
+ v2 = getStringFromWasm0(ret[0], ret[1]).slice();
95
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
96
+ }
97
+ return v2;
98
+ }
99
+ /**
100
+ * Insert a JSON-encoded element at `index` in the named RGA sequence.
101
+ *
102
+ * Returns the resulting [`Envelope`] as a JSON string.
103
+ * @param {string} key
104
+ * @param {number} index
105
+ * @param {string} value_json
106
+ * @returns {string}
107
+ */
108
+ seq_insert(key, index, value_json) {
109
+ let deferred4_0;
110
+ let deferred4_1;
111
+ try {
112
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
113
+ const len0 = WASM_VECTOR_LEN;
114
+ const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
115
+ const len1 = WASM_VECTOR_LEN;
116
+ const ret = wasm.wasmstatestore_seq_insert(this.__wbg_ptr, ptr0, len0, index, ptr1, len1);
117
+ var ptr3 = ret[0];
118
+ var len3 = ret[1];
119
+ if (ret[3]) {
120
+ ptr3 = 0; len3 = 0;
121
+ throw takeFromExternrefTable0(ret[2]);
122
+ }
123
+ deferred4_0 = ptr3;
124
+ deferred4_1 = len3;
125
+ return getStringFromWasm0(ptr3, len3);
126
+ } finally {
127
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
128
+ }
129
+ }
130
+ /**
131
+ * Return all visible elements of the named sequence as a JSON array string.
132
+ *
133
+ * Throws a JavaScript error if serialisation fails.
134
+ * @param {string} key
135
+ * @returns {string}
136
+ */
137
+ seq_items(key) {
138
+ let deferred3_0;
139
+ let deferred3_1;
140
+ try {
141
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
142
+ const len0 = WASM_VECTOR_LEN;
143
+ const ret = wasm.wasmstatestore_seq_items(this.__wbg_ptr, ptr0, len0);
144
+ var ptr2 = ret[0];
145
+ var len2 = ret[1];
146
+ if (ret[3]) {
147
+ ptr2 = 0; len2 = 0;
148
+ throw takeFromExternrefTable0(ret[2]);
149
+ }
150
+ deferred3_0 = ptr2;
151
+ deferred3_1 = len2;
152
+ return getStringFromWasm0(ptr2, len2);
153
+ } finally {
154
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
155
+ }
156
+ }
157
+ /**
158
+ * Return the number of visible elements in the named sequence.
159
+ * @param {string} key
160
+ * @returns {number}
161
+ */
162
+ seq_len(key) {
163
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
164
+ const len0 = WASM_VECTOR_LEN;
165
+ const ret = wasm.wasmstatestore_seq_len(this.__wbg_ptr, ptr0, len0);
166
+ return ret >>> 0;
167
+ }
168
+ /**
169
+ * Add a JSON-encoded element to the named OR-Set.
170
+ *
171
+ * Returns the resulting [`Envelope`] as a JSON string.
172
+ * @param {string} key
173
+ * @param {string} value_json
174
+ * @returns {string}
175
+ */
176
+ set_add(key, value_json) {
177
+ let deferred4_0;
178
+ let deferred4_1;
179
+ try {
180
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
181
+ const len0 = WASM_VECTOR_LEN;
182
+ const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
183
+ const len1 = WASM_VECTOR_LEN;
184
+ const ret = wasm.wasmstatestore_set_add(this.__wbg_ptr, ptr0, len0, ptr1, len1);
185
+ var ptr3 = ret[0];
186
+ var len3 = ret[1];
187
+ if (ret[3]) {
188
+ ptr3 = 0; len3 = 0;
189
+ throw takeFromExternrefTable0(ret[2]);
190
+ }
191
+ deferred4_0 = ptr3;
192
+ deferred4_1 = len3;
193
+ return getStringFromWasm0(ptr3, len3);
194
+ } finally {
195
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
196
+ }
197
+ }
198
+ /**
199
+ * Returns `true` if the named OR-Set contains the JSON-encoded `value`.
200
+ * @param {string} key
201
+ * @param {string} value_json
202
+ * @returns {boolean}
203
+ */
204
+ set_contains(key, value_json) {
205
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
206
+ const len0 = WASM_VECTOR_LEN;
207
+ const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
208
+ const len1 = WASM_VECTOR_LEN;
209
+ const ret = wasm.wasmstatestore_set_contains(this.__wbg_ptr, ptr0, len0, ptr1, len1);
210
+ return ret !== 0;
211
+ }
212
+ /**
213
+ * Return all elements of the named OR-Set as a JSON array string.
214
+ *
215
+ * Throws a JavaScript error if serialisation fails.
216
+ * @param {string} key
217
+ * @returns {string}
218
+ */
219
+ set_items(key) {
220
+ let deferred3_0;
221
+ let deferred3_1;
222
+ try {
223
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
224
+ const len0 = WASM_VECTOR_LEN;
225
+ const ret = wasm.wasmstatestore_set_items(this.__wbg_ptr, ptr0, len0);
226
+ var ptr2 = ret[0];
227
+ var len2 = ret[1];
228
+ if (ret[3]) {
229
+ ptr2 = 0; len2 = 0;
230
+ throw takeFromExternrefTable0(ret[2]);
231
+ }
232
+ deferred3_0 = ptr2;
233
+ deferred3_1 = len2;
234
+ return getStringFromWasm0(ptr2, len2);
235
+ } finally {
236
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
237
+ }
238
+ }
239
+ /**
240
+ * Write a JSON-encoded value to the named LWW register.
241
+ *
242
+ * Returns the resulting [`Envelope`] serialised as a JSON string, ready
243
+ * to broadcast to peer nodes.
244
+ * @param {string} key
245
+ * @param {string} value_json
246
+ * @returns {string}
247
+ */
248
+ set_register(key, value_json) {
249
+ let deferred4_0;
250
+ let deferred4_1;
251
+ try {
252
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
253
+ const len0 = WASM_VECTOR_LEN;
254
+ const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
255
+ const len1 = WASM_VECTOR_LEN;
256
+ const ret = wasm.wasmstatestore_set_register(this.__wbg_ptr, ptr0, len0, ptr1, len1);
257
+ var ptr3 = ret[0];
258
+ var len3 = ret[1];
259
+ if (ret[3]) {
260
+ ptr3 = 0; len3 = 0;
261
+ throw takeFromExternrefTable0(ret[2]);
262
+ }
263
+ deferred4_0 = ptr3;
264
+ deferred4_1 = len3;
265
+ return getStringFromWasm0(ptr3, len3);
266
+ } finally {
267
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
268
+ }
269
+ }
270
+ /**
271
+ * Remove a JSON-encoded element from the named OR-Set.
272
+ *
273
+ * Returns the resulting [`Envelope`] as a JSON string, or `undefined` if
274
+ * the element was not present in the set.
275
+ *
276
+ * Throws a JavaScript error if `value_json` is not valid JSON.
277
+ * @param {string} key
278
+ * @param {string} value_json
279
+ * @returns {string | undefined}
280
+ */
281
+ set_remove(key, value_json) {
282
+ const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
283
+ const len0 = WASM_VECTOR_LEN;
284
+ const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
285
+ const len1 = WASM_VECTOR_LEN;
286
+ const ret = wasm.wasmstatestore_set_remove(this.__wbg_ptr, ptr0, len0, ptr1, len1);
287
+ if (ret[3]) {
288
+ throw takeFromExternrefTable0(ret[2]);
289
+ }
290
+ let v3;
291
+ if (ret[0] !== 0) {
292
+ v3 = getStringFromWasm0(ret[0], ret[1]).slice();
293
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
294
+ }
295
+ return v3;
296
+ }
297
+ }
298
+ if (Symbol.dispose) WasmStateStore.prototype[Symbol.dispose] = WasmStateStore.prototype.free;
299
+
300
+ function __wbg_get_imports() {
301
+ const import0 = {
302
+ __proto__: null,
303
+ __wbg___wbindgen_throw_df03e93053e0f4bc: function(arg0, arg1) {
304
+ throw new Error(getStringFromWasm0(arg0, arg1));
305
+ },
306
+ __wbg_getRandomValues_3dda8830c2565714: function() { return handleError(function (arg0, arg1) {
307
+ globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1));
308
+ }, arguments); },
309
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
310
+ // Cast intrinsic for `Ref(String) -> Externref`.
311
+ const ret = getStringFromWasm0(arg0, arg1);
312
+ return ret;
313
+ },
314
+ __wbindgen_init_externref_table: function() {
315
+ const table = wasm.__wbindgen_externrefs;
316
+ const offset = table.grow(4);
317
+ table.set(0, undefined);
318
+ table.set(offset + 0, undefined);
319
+ table.set(offset + 1, null);
320
+ table.set(offset + 2, true);
321
+ table.set(offset + 3, false);
322
+ },
323
+ };
324
+ return {
325
+ __proto__: null,
326
+ "./crdt_sync_bg.js": import0,
327
+ };
328
+ }
329
+
330
+ const WasmStateStoreFinalization = (typeof FinalizationRegistry === 'undefined')
331
+ ? { register: () => {}, unregister: () => {} }
332
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmstatestore_free(ptr >>> 0, 1));
333
+
334
+ function addToExternrefTable0(obj) {
335
+ const idx = wasm.__externref_table_alloc();
336
+ wasm.__wbindgen_externrefs.set(idx, obj);
337
+ return idx;
338
+ }
339
+
340
+ function getArrayU8FromWasm0(ptr, len) {
341
+ ptr = ptr >>> 0;
342
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
343
+ }
344
+
345
+ function getStringFromWasm0(ptr, len) {
346
+ ptr = ptr >>> 0;
347
+ return decodeText(ptr, len);
348
+ }
349
+
350
+ let cachedUint8ArrayMemory0 = null;
351
+ function getUint8ArrayMemory0() {
352
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
353
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
18
354
  }
355
+ return cachedUint8ArrayMemory0;
356
+ }
19
357
 
20
- set_register(_key, _value_json) { return ''; }
21
- get_register(_key) { return undefined; }
22
- apply_envelope(_envelope_json) {}
358
+ function handleError(f, args) {
359
+ try {
360
+ return f.apply(this, args);
361
+ } catch (e) {
362
+ const idx = addToExternrefTable0(e);
363
+ wasm.__wbindgen_exn_store(idx);
364
+ }
23
365
  }
24
366
 
25
- export default async function init(_input) {
26
- throw new Error(
27
- '@crdt-sync/core: WebAssembly module not built. ' +
28
- 'Please run `npm run build:wasm` to compile the Rust WASM module.'
29
- );
367
+ function passStringToWasm0(arg, malloc, realloc) {
368
+ if (realloc === undefined) {
369
+ const buf = cachedTextEncoder.encode(arg);
370
+ const ptr = malloc(buf.length, 1) >>> 0;
371
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
372
+ WASM_VECTOR_LEN = buf.length;
373
+ return ptr;
374
+ }
375
+
376
+ let len = arg.length;
377
+ let ptr = malloc(len, 1) >>> 0;
378
+
379
+ const mem = getUint8ArrayMemory0();
380
+
381
+ let offset = 0;
382
+
383
+ for (; offset < len; offset++) {
384
+ const code = arg.charCodeAt(offset);
385
+ if (code > 0x7F) break;
386
+ mem[ptr + offset] = code;
387
+ }
388
+ if (offset !== len) {
389
+ if (offset !== 0) {
390
+ arg = arg.slice(offset);
391
+ }
392
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
393
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
394
+ const ret = cachedTextEncoder.encodeInto(arg, view);
395
+
396
+ offset += ret.written;
397
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
398
+ }
399
+
400
+ WASM_VECTOR_LEN = offset;
401
+ return ptr;
30
402
  }
403
+
404
+ function takeFromExternrefTable0(idx) {
405
+ const value = wasm.__wbindgen_externrefs.get(idx);
406
+ wasm.__externref_table_dealloc(idx);
407
+ return value;
408
+ }
409
+
410
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
411
+ cachedTextDecoder.decode();
412
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
413
+ let numBytesDecoded = 0;
414
+ function decodeText(ptr, len) {
415
+ numBytesDecoded += len;
416
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
417
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
418
+ cachedTextDecoder.decode();
419
+ numBytesDecoded = len;
420
+ }
421
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
422
+ }
423
+
424
+ const cachedTextEncoder = new TextEncoder();
425
+
426
+ if (!('encodeInto' in cachedTextEncoder)) {
427
+ cachedTextEncoder.encodeInto = function (arg, view) {
428
+ const buf = cachedTextEncoder.encode(arg);
429
+ view.set(buf);
430
+ return {
431
+ read: arg.length,
432
+ written: buf.length
433
+ };
434
+ };
435
+ }
436
+
437
+ let WASM_VECTOR_LEN = 0;
438
+
439
+ let wasmModule, wasm;
440
+ function __wbg_finalize_init(instance, module) {
441
+ wasm = instance.exports;
442
+ wasmModule = module;
443
+ cachedUint8ArrayMemory0 = null;
444
+ wasm.__wbindgen_start();
445
+ return wasm;
446
+ }
447
+
448
+ async function __wbg_load(module, imports) {
449
+ if (typeof Response === 'function' && module instanceof Response) {
450
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
451
+ try {
452
+ return await WebAssembly.instantiateStreaming(module, imports);
453
+ } catch (e) {
454
+ const validResponse = module.ok && expectedResponseType(module.type);
455
+
456
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
457
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
458
+
459
+ } else { throw e; }
460
+ }
461
+ }
462
+
463
+ const bytes = await module.arrayBuffer();
464
+ return await WebAssembly.instantiate(bytes, imports);
465
+ } else {
466
+ const instance = await WebAssembly.instantiate(module, imports);
467
+
468
+ if (instance instanceof WebAssembly.Instance) {
469
+ return { instance, module };
470
+ } else {
471
+ return instance;
472
+ }
473
+ }
474
+
475
+ function expectedResponseType(type) {
476
+ switch (type) {
477
+ case 'basic': case 'cors': case 'default': return true;
478
+ }
479
+ return false;
480
+ }
481
+ }
482
+
483
+ function initSync(module) {
484
+ if (wasm !== undefined) return wasm;
485
+
486
+
487
+ if (module !== undefined) {
488
+ if (Object.getPrototypeOf(module) === Object.prototype) {
489
+ ({module} = module)
490
+ } else {
491
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
492
+ }
493
+ }
494
+
495
+ const imports = __wbg_get_imports();
496
+ if (!(module instanceof WebAssembly.Module)) {
497
+ module = new WebAssembly.Module(module);
498
+ }
499
+ const instance = new WebAssembly.Instance(module, imports);
500
+ return __wbg_finalize_init(instance, module);
501
+ }
502
+
503
+ async function __wbg_init(module_or_path) {
504
+ if (wasm !== undefined) return wasm;
505
+
506
+
507
+ if (module_or_path !== undefined) {
508
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
509
+ ({module_or_path} = module_or_path)
510
+ } else {
511
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
512
+ }
513
+ }
514
+
515
+ if (module_or_path === undefined) {
516
+ module_or_path = new URL('crdt_sync_bg.wasm', import.meta.url);
517
+ }
518
+ const imports = __wbg_get_imports();
519
+
520
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
521
+ module_or_path = fetch(module_or_path);
522
+ }
523
+
524
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
525
+
526
+ return __wbg_finalize_init(instance, module);
527
+ }
528
+
529
+ export { initSync, __wbg_init as default };