@crdt-sync/core 0.1.1 → 0.2.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.
- package/dist/index.d.mts +233 -0
- package/dist/index.d.ts +233 -5
- package/dist/index.js +220 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +194 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +12 -3
- package/pkg/bundler/README.md +346 -0
- package/pkg/bundler/crdt_sync.d.ts +95 -0
- package/pkg/bundler/crdt_sync.js +9 -0
- package/pkg/bundler/crdt_sync_bg.js +430 -0
- package/pkg/bundler/crdt_sync_bg.wasm +0 -0
- package/pkg/bundler/crdt_sync_bg.wasm.d.ts +25 -0
- package/pkg/bundler/package.json +30 -0
- package/pkg/web/README.md +346 -0
- package/pkg/web/crdt_sync.d.ts +145 -0
- package/pkg/web/crdt_sync.js +529 -0
- package/pkg/web/crdt_sync_bg.wasm +0 -0
- package/pkg/web/crdt_sync_bg.wasm.d.ts +25 -0
- package/pkg/web/package.json +28 -0
- package/dist/CrdtStateProxy.d.ts +0 -119
- package/dist/CrdtStateProxy.d.ts.map +0 -1
- package/dist/CrdtStateProxy.js +0 -120
- package/dist/CrdtStateProxy.js.map +0 -1
- package/dist/WebSocketManager.d.ts +0 -115
- package/dist/WebSocketManager.d.ts.map +0 -1
- package/dist/WebSocketManager.js +0 -179
- package/dist/WebSocketManager.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
/* @ts-self-types="./crdt_sync.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A WebAssembly-compatible wrapper around [`StateStore`].
|
|
5
|
+
*
|
|
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.
|
|
9
|
+
*/
|
|
10
|
+
export class WasmStateStore {
|
|
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);
|
|
354
|
+
}
|
|
355
|
+
return cachedUint8ArrayMemory0;
|
|
356
|
+
}
|
|
357
|
+
|
|
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
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
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;
|
|
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 };
|
|
Binary file
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const __wbg_wasmstatestore_free: (a: number, b: number) => void;
|
|
5
|
+
export const wasmstatestore_apply_envelope: (a: number, b: number, c: number) => [number, number];
|
|
6
|
+
export const wasmstatestore_clock: (a: number) => number;
|
|
7
|
+
export const wasmstatestore_get_register: (a: number, b: number, c: number) => [number, number];
|
|
8
|
+
export const wasmstatestore_new: (a: number, b: number) => number;
|
|
9
|
+
export const wasmstatestore_seq_delete: (a: number, b: number, c: number, d: number) => [number, number];
|
|
10
|
+
export const wasmstatestore_seq_insert: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
|
|
11
|
+
export const wasmstatestore_seq_items: (a: number, b: number, c: number) => [number, number, number, number];
|
|
12
|
+
export const wasmstatestore_seq_len: (a: number, b: number, c: number) => number;
|
|
13
|
+
export const wasmstatestore_set_add: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
14
|
+
export const wasmstatestore_set_contains: (a: number, b: number, c: number, d: number, e: number) => number;
|
|
15
|
+
export const wasmstatestore_set_items: (a: number, b: number, c: number) => [number, number, number, number];
|
|
16
|
+
export const wasmstatestore_set_register: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
17
|
+
export const wasmstatestore_set_remove: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
18
|
+
export const __wbindgen_exn_store: (a: number) => void;
|
|
19
|
+
export const __externref_table_alloc: () => number;
|
|
20
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
21
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
22
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
23
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
24
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
25
|
+
export const __wbindgen_start: () => void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "crdt-sync",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"description": "A generic state synchronization engine based on CRDTs (Conflict-free Replicated Data Types) for keeping distributed state in harmony without conflicts.",
|
|
5
|
+
"version": "0.1.0",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/franruedaesq/CRDT-sync"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"crdt_sync_bg.wasm",
|
|
13
|
+
"crdt_sync.js",
|
|
14
|
+
"crdt_sync.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"main": "crdt_sync.js",
|
|
17
|
+
"types": "crdt_sync.d.ts",
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./snippets/*"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"crdt",
|
|
23
|
+
"sync",
|
|
24
|
+
"distributed",
|
|
25
|
+
"state",
|
|
26
|
+
"conflict-free"
|
|
27
|
+
]
|
|
28
|
+
}
|
package/dist/CrdtStateProxy.d.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.
|
|
3
|
-
*
|
|
4
|
-
* In production, import the real `WasmStateStore` from the compiled Wasm
|
|
5
|
-
* package (e.g. `import { WasmStateStore } from './crdt_sync.js'`).
|
|
6
|
-
* In tests the interface can be satisfied by any mock object.
|
|
7
|
-
*/
|
|
8
|
-
export interface WasmStateStore {
|
|
9
|
-
/** Write a JSON-encoded value to the named LWW register. Returns the Envelope JSON. */
|
|
10
|
-
set_register(key: string, value_json: string): string;
|
|
11
|
-
/** Read the current value of a named LWW register as a JSON string, or `undefined`. */
|
|
12
|
-
get_register(key: string): string | undefined;
|
|
13
|
-
/** Apply a remote Envelope (serialised as JSON) to this store. */
|
|
14
|
-
apply_envelope(envelope_json: string): void;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Payload delivered to every `onUpdate` listener when a property is written
|
|
18
|
-
* through the proxy.
|
|
19
|
-
*/
|
|
20
|
-
export interface UpdateEvent {
|
|
21
|
-
/** Dot-separated key path that was updated (e.g. `"robot.speed"`). */
|
|
22
|
-
key: string;
|
|
23
|
-
/** The new JavaScript value. */
|
|
24
|
-
value: unknown;
|
|
25
|
-
/**
|
|
26
|
-
* The CRDT Envelope returned by `WasmStateStore.set_register`, serialised
|
|
27
|
-
* as a JSON string. Broadcast this to peer nodes via `apply_envelope`.
|
|
28
|
-
*/
|
|
29
|
-
envelope: string;
|
|
30
|
-
}
|
|
31
|
-
/** Callback type for `onUpdate` listeners. */
|
|
32
|
-
export type UpdateHandler = (event: UpdateEvent) => void;
|
|
33
|
-
/**
|
|
34
|
-
* A TypeScript proxy wrapper around `WasmStateStore` that gives frontend
|
|
35
|
-
* developers a **magical, object-oriented** experience.
|
|
36
|
-
*
|
|
37
|
-
* ## How it works
|
|
38
|
-
*
|
|
39
|
-
* 1. **JS `Proxy` interception** – accessing a nested path on `state` returns
|
|
40
|
-
* another `Proxy`. Assigning a value anywhere in the tree intercepts the
|
|
41
|
-
* write and forwards it to the underlying Wasm store via
|
|
42
|
-
* `set_register(dotPath, JSON.stringify(value))`.
|
|
43
|
-
*
|
|
44
|
-
* 2. **Wasm call** – the interceptor immediately calls
|
|
45
|
-
* `WasmStateStore.set_register()` so the CRDT operation is recorded and
|
|
46
|
-
* returns an `Envelope` JSON string ready for broadcasting.
|
|
47
|
-
*
|
|
48
|
-
* 3. **Event emitter** – every write fires all `onUpdate` listeners with the
|
|
49
|
-
* full `UpdateEvent` (key, value, envelope), enabling React / Vue and other
|
|
50
|
-
* UI frameworks to react to state changes.
|
|
51
|
-
*
|
|
52
|
-
* ## Usage
|
|
53
|
-
*
|
|
54
|
-
* ```ts
|
|
55
|
-
* import init, { WasmStateStore } from './crdt_sync.js';
|
|
56
|
-
* import { CrdtStateProxy } from './CrdtStateProxy.js';
|
|
57
|
-
*
|
|
58
|
-
* await init();
|
|
59
|
-
* const store = new WasmStateStore('node-1');
|
|
60
|
-
* const proxy = new CrdtStateProxy(store);
|
|
61
|
-
*
|
|
62
|
-
* // Register a listener (e.g. trigger a React re-render).
|
|
63
|
-
* const unsubscribe = proxy.onUpdate(({ key, value, envelope }) => {
|
|
64
|
-
* console.log(`${key} =`, value);
|
|
65
|
-
* broadcast(envelope); // send to peers
|
|
66
|
-
* });
|
|
67
|
-
*
|
|
68
|
-
* // Write through the proxy — the interceptor handles everything.
|
|
69
|
-
* proxy.state.speed = 100;
|
|
70
|
-
* proxy.state.robot.x = 42;
|
|
71
|
-
*
|
|
72
|
-
* // Clean up when done.
|
|
73
|
-
* unsubscribe();
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
export declare class CrdtStateProxy {
|
|
77
|
-
private readonly _store;
|
|
78
|
-
private readonly _handlers;
|
|
79
|
-
private readonly _state;
|
|
80
|
-
/**
|
|
81
|
-
* Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.
|
|
82
|
-
*
|
|
83
|
-
* @param store - The Wasm state store instance to proxy.
|
|
84
|
-
*/
|
|
85
|
-
constructor(store: WasmStateStore);
|
|
86
|
-
/**
|
|
87
|
-
* The proxied state object.
|
|
88
|
-
*
|
|
89
|
-
* Assigning any property (or nested property) on this object will
|
|
90
|
-
* automatically call `WasmStateStore.set_register` and fire `onUpdate`
|
|
91
|
-
* listeners.
|
|
92
|
-
*
|
|
93
|
-
* ```ts
|
|
94
|
-
* proxy.state.speed = 100; // key: "speed"
|
|
95
|
-
* proxy.state.robot.speed = 100; // key: "robot.speed"
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
get state(): Record<string, unknown>;
|
|
99
|
-
/**
|
|
100
|
-
* Register a listener that is called whenever a property is written through
|
|
101
|
-
* `proxy.state`.
|
|
102
|
-
*
|
|
103
|
-
* @param handler - Callback receiving an `UpdateEvent`.
|
|
104
|
-
* @returns An unsubscribe function — call it to remove the listener.
|
|
105
|
-
*/
|
|
106
|
-
onUpdate(handler: UpdateHandler): () => void;
|
|
107
|
-
/**
|
|
108
|
-
* Recursively build a `Proxy` for the given dot-path `prefix`.
|
|
109
|
-
*
|
|
110
|
-
* - **`get` trap**: returns a child proxy for the nested path so that deep
|
|
111
|
-
* assignments like `proxy.state.robot.speed = 100` work correctly.
|
|
112
|
-
* - **`set` trap**: serialises the value, calls `set_register`, and fires
|
|
113
|
-
* all `onUpdate` listeners.
|
|
114
|
-
*/
|
|
115
|
-
private _makeProxy;
|
|
116
|
-
/** Dispatch an `UpdateEvent` to all registered handlers. */
|
|
117
|
-
private _emit;
|
|
118
|
-
}
|
|
119
|
-
//# sourceMappingURL=CrdtStateProxy.d.ts.map
|