@1kbgz/transports 0.3.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/cdn/index.js +608 -0
- package/dist/cdn/index.js.map +7 -0
- package/dist/css/index.css +0 -0
- package/dist/esm/index.js +608 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/html/index.html +13 -0
- package/dist/index.html +13 -0
- package/dist/pkg/transports.d.ts +115 -0
- package/dist/pkg/transports.js +501 -0
- package/dist/pkg/transports_bg.wasm +0 -0
- package/dist/pkg/transports_bg.wasm.d.ts +23 -0
- package/package.json +61 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// dist/pkg/transports.js
|
|
8
|
+
var transports_exports = {};
|
|
9
|
+
__export(transports_exports, {
|
|
10
|
+
Store: () => Store,
|
|
11
|
+
apply: () => apply,
|
|
12
|
+
decode: () => decode,
|
|
13
|
+
decode_as: () => decode_as,
|
|
14
|
+
default: () => __wbg_init,
|
|
15
|
+
diff: () => diff,
|
|
16
|
+
encode: () => encode,
|
|
17
|
+
encode_as: () => encode_as,
|
|
18
|
+
initSync: () => initSync,
|
|
19
|
+
json_to_msgpack: () => json_to_msgpack,
|
|
20
|
+
msgpack_to_json: () => msgpack_to_json
|
|
21
|
+
});
|
|
22
|
+
var Store = class {
|
|
23
|
+
__destroy_into_raw() {
|
|
24
|
+
const ptr = this.__wbg_ptr;
|
|
25
|
+
this.__wbg_ptr = 0;
|
|
26
|
+
StoreFinalization.unregister(this);
|
|
27
|
+
return ptr;
|
|
28
|
+
}
|
|
29
|
+
free() {
|
|
30
|
+
const ptr = this.__destroy_into_raw();
|
|
31
|
+
wasm.__wbg_store_free(ptr, 0);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Apply a JSON patch to a mirrored model; returns whether the id was known.
|
|
35
|
+
* @param {bigint} id
|
|
36
|
+
* @param {string} patch_json
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
apply(id, patch_json) {
|
|
40
|
+
const ptr0 = passStringToWasm0(patch_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
41
|
+
const len0 = WASM_VECTOR_LEN;
|
|
42
|
+
const ret = wasm.store_apply(this.__wbg_ptr, id, ptr0, len0);
|
|
43
|
+
if (ret[2]) {
|
|
44
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
45
|
+
}
|
|
46
|
+
return ret[0] !== 0;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Host a model from its JSON; returns the assigned id.
|
|
50
|
+
* @param {string} type_name
|
|
51
|
+
* @param {string} value_json
|
|
52
|
+
* @returns {bigint}
|
|
53
|
+
*/
|
|
54
|
+
host(type_name, value_json) {
|
|
55
|
+
const ptr0 = passStringToWasm0(type_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
56
|
+
const len0 = WASM_VECTOR_LEN;
|
|
57
|
+
const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
58
|
+
const len1 = WASM_VECTOR_LEN;
|
|
59
|
+
const ret = wasm.store_host(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
60
|
+
if (ret[2]) {
|
|
61
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
62
|
+
}
|
|
63
|
+
return BigInt.asUintN(64, ret[0]);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Replace a hosted model from JSON; returns the JSON patch (or `undefined` if id unknown).
|
|
67
|
+
* @param {bigint} id
|
|
68
|
+
* @param {string} value_json
|
|
69
|
+
* @returns {string | undefined}
|
|
70
|
+
*/
|
|
71
|
+
mutate(id, value_json) {
|
|
72
|
+
const ptr0 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
73
|
+
const len0 = WASM_VECTOR_LEN;
|
|
74
|
+
const ret = wasm.store_mutate(this.__wbg_ptr, id, ptr0, len0);
|
|
75
|
+
if (ret[3]) {
|
|
76
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
77
|
+
}
|
|
78
|
+
let v2;
|
|
79
|
+
if (ret[0] !== 0) {
|
|
80
|
+
v2 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
81
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
82
|
+
}
|
|
83
|
+
return v2;
|
|
84
|
+
}
|
|
85
|
+
constructor() {
|
|
86
|
+
const ret = wasm.store_new();
|
|
87
|
+
this.__wbg_ptr = ret;
|
|
88
|
+
StoreFinalization.register(this, this.__wbg_ptr, this);
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* `{"type_name":..,"rev":..,"value":..}` for a hosted model, or `undefined`.
|
|
93
|
+
* @param {bigint} id
|
|
94
|
+
* @returns {string | undefined}
|
|
95
|
+
*/
|
|
96
|
+
snapshot(id) {
|
|
97
|
+
const ret = wasm.store_snapshot(this.__wbg_ptr, id);
|
|
98
|
+
let v1;
|
|
99
|
+
if (ret[0] !== 0) {
|
|
100
|
+
v1 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
101
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
102
|
+
}
|
|
103
|
+
return v1;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
if (Symbol.dispose) Store.prototype[Symbol.dispose] = Store.prototype.free;
|
|
107
|
+
function apply(value, patch) {
|
|
108
|
+
let deferred4_0;
|
|
109
|
+
let deferred4_1;
|
|
110
|
+
try {
|
|
111
|
+
const ptr0 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
112
|
+
const len0 = WASM_VECTOR_LEN;
|
|
113
|
+
const ptr1 = passStringToWasm0(patch, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
114
|
+
const len1 = WASM_VECTOR_LEN;
|
|
115
|
+
const ret = wasm.apply(ptr0, len0, ptr1, len1);
|
|
116
|
+
var ptr3 = ret[0];
|
|
117
|
+
var len3 = ret[1];
|
|
118
|
+
if (ret[3]) {
|
|
119
|
+
ptr3 = 0;
|
|
120
|
+
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
|
+
function decode(data) {
|
|
131
|
+
let deferred3_0;
|
|
132
|
+
let deferred3_1;
|
|
133
|
+
try {
|
|
134
|
+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
135
|
+
const len0 = WASM_VECTOR_LEN;
|
|
136
|
+
const ret = wasm.decode(ptr0, len0);
|
|
137
|
+
var ptr2 = ret[0];
|
|
138
|
+
var len2 = ret[1];
|
|
139
|
+
if (ret[3]) {
|
|
140
|
+
ptr2 = 0;
|
|
141
|
+
len2 = 0;
|
|
142
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
143
|
+
}
|
|
144
|
+
deferred3_0 = ptr2;
|
|
145
|
+
deferred3_1 = len2;
|
|
146
|
+
return getStringFromWasm0(ptr2, len2);
|
|
147
|
+
} finally {
|
|
148
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function decode_as(data, codec) {
|
|
152
|
+
let deferred4_0;
|
|
153
|
+
let deferred4_1;
|
|
154
|
+
try {
|
|
155
|
+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
156
|
+
const len0 = WASM_VECTOR_LEN;
|
|
157
|
+
const ptr1 = passStringToWasm0(codec, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
158
|
+
const len1 = WASM_VECTOR_LEN;
|
|
159
|
+
const ret = wasm.decode_as(ptr0, len0, ptr1, len1);
|
|
160
|
+
var ptr3 = ret[0];
|
|
161
|
+
var len3 = ret[1];
|
|
162
|
+
if (ret[3]) {
|
|
163
|
+
ptr3 = 0;
|
|
164
|
+
len3 = 0;
|
|
165
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
166
|
+
}
|
|
167
|
+
deferred4_0 = ptr3;
|
|
168
|
+
deferred4_1 = len3;
|
|
169
|
+
return getStringFromWasm0(ptr3, len3);
|
|
170
|
+
} finally {
|
|
171
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function diff(old, _new) {
|
|
175
|
+
let deferred4_0;
|
|
176
|
+
let deferred4_1;
|
|
177
|
+
try {
|
|
178
|
+
const ptr0 = passStringToWasm0(old, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
179
|
+
const len0 = WASM_VECTOR_LEN;
|
|
180
|
+
const ptr1 = passStringToWasm0(_new, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
181
|
+
const len1 = WASM_VECTOR_LEN;
|
|
182
|
+
const ret = wasm.diff(ptr0, len0, ptr1, len1);
|
|
183
|
+
var ptr3 = ret[0];
|
|
184
|
+
var len3 = ret[1];
|
|
185
|
+
if (ret[3]) {
|
|
186
|
+
ptr3 = 0;
|
|
187
|
+
len3 = 0;
|
|
188
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
189
|
+
}
|
|
190
|
+
deferred4_0 = ptr3;
|
|
191
|
+
deferred4_1 = len3;
|
|
192
|
+
return getStringFromWasm0(ptr3, len3);
|
|
193
|
+
} finally {
|
|
194
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function encode(value) {
|
|
198
|
+
const ptr0 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
199
|
+
const len0 = WASM_VECTOR_LEN;
|
|
200
|
+
const ret = wasm.encode(ptr0, len0);
|
|
201
|
+
if (ret[3]) {
|
|
202
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
203
|
+
}
|
|
204
|
+
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
205
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
206
|
+
return v2;
|
|
207
|
+
}
|
|
208
|
+
function encode_as(value, codec) {
|
|
209
|
+
const ptr0 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
210
|
+
const len0 = WASM_VECTOR_LEN;
|
|
211
|
+
const ptr1 = passStringToWasm0(codec, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
212
|
+
const len1 = WASM_VECTOR_LEN;
|
|
213
|
+
const ret = wasm.encode_as(ptr0, len0, ptr1, len1);
|
|
214
|
+
if (ret[3]) {
|
|
215
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
216
|
+
}
|
|
217
|
+
var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
218
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
219
|
+
return v3;
|
|
220
|
+
}
|
|
221
|
+
function json_to_msgpack(json) {
|
|
222
|
+
const ptr0 = passStringToWasm0(json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
223
|
+
const len0 = WASM_VECTOR_LEN;
|
|
224
|
+
const ret = wasm.json_to_msgpack(ptr0, len0);
|
|
225
|
+
if (ret[3]) {
|
|
226
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
227
|
+
}
|
|
228
|
+
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
229
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
230
|
+
return v2;
|
|
231
|
+
}
|
|
232
|
+
function msgpack_to_json(data) {
|
|
233
|
+
let deferred3_0;
|
|
234
|
+
let deferred3_1;
|
|
235
|
+
try {
|
|
236
|
+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
237
|
+
const len0 = WASM_VECTOR_LEN;
|
|
238
|
+
const ret = wasm.msgpack_to_json(ptr0, len0);
|
|
239
|
+
var ptr2 = ret[0];
|
|
240
|
+
var len2 = ret[1];
|
|
241
|
+
if (ret[3]) {
|
|
242
|
+
ptr2 = 0;
|
|
243
|
+
len2 = 0;
|
|
244
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
245
|
+
}
|
|
246
|
+
deferred3_0 = ptr2;
|
|
247
|
+
deferred3_1 = len2;
|
|
248
|
+
return getStringFromWasm0(ptr2, len2);
|
|
249
|
+
} finally {
|
|
250
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function __wbg_get_imports() {
|
|
254
|
+
const import0 = {
|
|
255
|
+
__proto__: null,
|
|
256
|
+
__wbg_Error_bce6d499ff0a4aff: function(arg0, arg1) {
|
|
257
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
258
|
+
return ret;
|
|
259
|
+
},
|
|
260
|
+
__wbg___wbindgen_throw_9c31b086c2b26051: function(arg0, arg1) {
|
|
261
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
262
|
+
},
|
|
263
|
+
__wbindgen_init_externref_table: function() {
|
|
264
|
+
const table = wasm.__wbindgen_externrefs;
|
|
265
|
+
const offset = table.grow(4);
|
|
266
|
+
table.set(0, void 0);
|
|
267
|
+
table.set(offset + 0, void 0);
|
|
268
|
+
table.set(offset + 1, null);
|
|
269
|
+
table.set(offset + 2, true);
|
|
270
|
+
table.set(offset + 3, false);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
return {
|
|
274
|
+
__proto__: null,
|
|
275
|
+
"./transports_bg.js": import0
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
var StoreFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
|
|
279
|
+
}, unregister: () => {
|
|
280
|
+
} } : new FinalizationRegistry((ptr) => wasm.__wbg_store_free(ptr, 1));
|
|
281
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
282
|
+
ptr = ptr >>> 0;
|
|
283
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
284
|
+
}
|
|
285
|
+
function getStringFromWasm0(ptr, len) {
|
|
286
|
+
return decodeText(ptr >>> 0, len);
|
|
287
|
+
}
|
|
288
|
+
var cachedUint8ArrayMemory0 = null;
|
|
289
|
+
function getUint8ArrayMemory0() {
|
|
290
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
291
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
292
|
+
}
|
|
293
|
+
return cachedUint8ArrayMemory0;
|
|
294
|
+
}
|
|
295
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
296
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
297
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
298
|
+
WASM_VECTOR_LEN = arg.length;
|
|
299
|
+
return ptr;
|
|
300
|
+
}
|
|
301
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
302
|
+
if (realloc === void 0) {
|
|
303
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
304
|
+
const ptr2 = malloc(buf.length, 1) >>> 0;
|
|
305
|
+
getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
|
|
306
|
+
WASM_VECTOR_LEN = buf.length;
|
|
307
|
+
return ptr2;
|
|
308
|
+
}
|
|
309
|
+
let len = arg.length;
|
|
310
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
311
|
+
const mem = getUint8ArrayMemory0();
|
|
312
|
+
let offset = 0;
|
|
313
|
+
for (; offset < len; offset++) {
|
|
314
|
+
const code = arg.charCodeAt(offset);
|
|
315
|
+
if (code > 127) break;
|
|
316
|
+
mem[ptr + offset] = code;
|
|
317
|
+
}
|
|
318
|
+
if (offset !== len) {
|
|
319
|
+
if (offset !== 0) {
|
|
320
|
+
arg = arg.slice(offset);
|
|
321
|
+
}
|
|
322
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
323
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
324
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
325
|
+
offset += ret.written;
|
|
326
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
327
|
+
}
|
|
328
|
+
WASM_VECTOR_LEN = offset;
|
|
329
|
+
return ptr;
|
|
330
|
+
}
|
|
331
|
+
function takeFromExternrefTable0(idx) {
|
|
332
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
333
|
+
wasm.__externref_table_dealloc(idx);
|
|
334
|
+
return value;
|
|
335
|
+
}
|
|
336
|
+
var cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
|
|
337
|
+
cachedTextDecoder.decode();
|
|
338
|
+
var MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
339
|
+
var numBytesDecoded = 0;
|
|
340
|
+
function decodeText(ptr, len) {
|
|
341
|
+
numBytesDecoded += len;
|
|
342
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
343
|
+
cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
|
|
344
|
+
cachedTextDecoder.decode();
|
|
345
|
+
numBytesDecoded = len;
|
|
346
|
+
}
|
|
347
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
348
|
+
}
|
|
349
|
+
var cachedTextEncoder = new TextEncoder();
|
|
350
|
+
if (!("encodeInto" in cachedTextEncoder)) {
|
|
351
|
+
cachedTextEncoder.encodeInto = function(arg, view) {
|
|
352
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
353
|
+
view.set(buf);
|
|
354
|
+
return {
|
|
355
|
+
read: arg.length,
|
|
356
|
+
written: buf.length
|
|
357
|
+
};
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
var WASM_VECTOR_LEN = 0;
|
|
361
|
+
var wasmModule;
|
|
362
|
+
var wasmInstance;
|
|
363
|
+
var wasm;
|
|
364
|
+
function __wbg_finalize_init(instance, module) {
|
|
365
|
+
wasmInstance = instance;
|
|
366
|
+
wasm = instance.exports;
|
|
367
|
+
wasmModule = module;
|
|
368
|
+
cachedUint8ArrayMemory0 = null;
|
|
369
|
+
wasm.__wbindgen_start();
|
|
370
|
+
return wasm;
|
|
371
|
+
}
|
|
372
|
+
async function __wbg_load(module, imports) {
|
|
373
|
+
if (typeof Response === "function" && module instanceof Response) {
|
|
374
|
+
if (typeof WebAssembly.instantiateStreaming === "function") {
|
|
375
|
+
try {
|
|
376
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
377
|
+
} catch (e) {
|
|
378
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
379
|
+
if (validResponse && module.headers.get("Content-Type") !== "application/wasm") {
|
|
380
|
+
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);
|
|
381
|
+
} else {
|
|
382
|
+
throw e;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const bytes = await module.arrayBuffer();
|
|
387
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
388
|
+
} else {
|
|
389
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
390
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
391
|
+
return { instance, module };
|
|
392
|
+
} else {
|
|
393
|
+
return instance;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function expectedResponseType(type) {
|
|
397
|
+
switch (type) {
|
|
398
|
+
case "basic":
|
|
399
|
+
case "cors":
|
|
400
|
+
case "default":
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
function initSync(module) {
|
|
407
|
+
if (wasm !== void 0) return wasm;
|
|
408
|
+
if (module !== void 0) {
|
|
409
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
410
|
+
({ module } = module);
|
|
411
|
+
} else {
|
|
412
|
+
console.warn("using deprecated parameters for `initSync()`; pass a single object instead");
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
const imports = __wbg_get_imports();
|
|
416
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
417
|
+
module = new WebAssembly.Module(module);
|
|
418
|
+
}
|
|
419
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
420
|
+
return __wbg_finalize_init(instance, module);
|
|
421
|
+
}
|
|
422
|
+
async function __wbg_init(module_or_path) {
|
|
423
|
+
if (wasm !== void 0) return wasm;
|
|
424
|
+
if (module_or_path !== void 0) {
|
|
425
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
426
|
+
({ module_or_path } = module_or_path);
|
|
427
|
+
} else {
|
|
428
|
+
console.warn("using deprecated parameters for the initialization function; pass a single object instead");
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
if (module_or_path === void 0) {
|
|
432
|
+
module_or_path = new URL("transports_bg.wasm", import.meta.url);
|
|
433
|
+
}
|
|
434
|
+
const imports = __wbg_get_imports();
|
|
435
|
+
if (typeof module_or_path === "string" || typeof Request === "function" && module_or_path instanceof Request || typeof URL === "function" && module_or_path instanceof URL) {
|
|
436
|
+
module_or_path = fetch(module_or_path);
|
|
437
|
+
}
|
|
438
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
439
|
+
return __wbg_finalize_init(instance, module);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// src/ts/bridge.ts
|
|
443
|
+
function toValue(v) {
|
|
444
|
+
if (v === null || v === void 0) return "Null";
|
|
445
|
+
if (typeof v === "boolean") return { Bool: v };
|
|
446
|
+
if (typeof v === "number")
|
|
447
|
+
return Number.isInteger(v) ? { Int: v } : { Float: v };
|
|
448
|
+
if (typeof v === "string") return { Str: v };
|
|
449
|
+
if (Array.isArray(v)) return { List: v.map(toValue) };
|
|
450
|
+
if (typeof v === "object") {
|
|
451
|
+
const m = {};
|
|
452
|
+
for (const [k, x] of Object.entries(v)) m[k] = toValue(x);
|
|
453
|
+
return { Map: m };
|
|
454
|
+
}
|
|
455
|
+
throw new TypeError(`unsupported value for transports: ${typeof v}`);
|
|
456
|
+
}
|
|
457
|
+
function fromValue(value) {
|
|
458
|
+
if (value === "Null") return null;
|
|
459
|
+
const [tag, inner] = Object.entries(value)[0];
|
|
460
|
+
switch (tag) {
|
|
461
|
+
case "Bool":
|
|
462
|
+
case "Int":
|
|
463
|
+
case "Float":
|
|
464
|
+
case "Str":
|
|
465
|
+
case "Submodel":
|
|
466
|
+
return inner;
|
|
467
|
+
case "List":
|
|
468
|
+
return inner.map(fromValue);
|
|
469
|
+
case "Map": {
|
|
470
|
+
const o = {};
|
|
471
|
+
for (const [k, x] of Object.entries(inner))
|
|
472
|
+
o[k] = fromValue(x);
|
|
473
|
+
return o;
|
|
474
|
+
}
|
|
475
|
+
default:
|
|
476
|
+
throw new Error(`unrecognized tagged value: ${JSON.stringify(value)}`);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// src/ts/codecs.ts
|
|
481
|
+
var BUILTIN = /* @__PURE__ */ new Set([
|
|
482
|
+
"",
|
|
483
|
+
"json",
|
|
484
|
+
"application/json",
|
|
485
|
+
"msgpack",
|
|
486
|
+
"application/msgpack",
|
|
487
|
+
"x-msgpack",
|
|
488
|
+
"application/x-msgpack"
|
|
489
|
+
]);
|
|
490
|
+
var registry = /* @__PURE__ */ new Map();
|
|
491
|
+
function registerCodec(contentType, encode3, decode3) {
|
|
492
|
+
if (BUILTIN.has(contentType)) {
|
|
493
|
+
throw new Error(`cannot override built-in codec: ${contentType}`);
|
|
494
|
+
}
|
|
495
|
+
registry.set(contentType, { encode: encode3, decode: decode3 });
|
|
496
|
+
}
|
|
497
|
+
function unregisterCodec(contentType) {
|
|
498
|
+
registry.delete(contentType);
|
|
499
|
+
}
|
|
500
|
+
function codecFor(contentType) {
|
|
501
|
+
return registry.get(contentType);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// src/ts/client.ts
|
|
505
|
+
var Client = class {
|
|
506
|
+
constructor(codec = "json") {
|
|
507
|
+
this.codec = codec;
|
|
508
|
+
}
|
|
509
|
+
codec;
|
|
510
|
+
values = /* @__PURE__ */ new Map();
|
|
511
|
+
revs = /* @__PURE__ */ new Map();
|
|
512
|
+
/** Apply an inbound snapshot or patch frame to the mirror.
|
|
513
|
+
*
|
|
514
|
+
* Decodes by the client's codec: a registered custom codec, else built-in JSON (text) / msgpack
|
|
515
|
+
* (binary).
|
|
516
|
+
*/
|
|
517
|
+
recv(data) {
|
|
518
|
+
const custom = codecFor(this.codec);
|
|
519
|
+
const msg = custom ? custom.decode(data) : JSON.parse(typeof data === "string" ? data : msgpackToJson(data));
|
|
520
|
+
if (msg.t === "snapshot") {
|
|
521
|
+
this.values.set(msg.id, msg.value);
|
|
522
|
+
this.revs.set(msg.id, msg.rev);
|
|
523
|
+
} else if (msg.t === "patch") {
|
|
524
|
+
const cur = JSON.stringify(this.values.get(msg.id));
|
|
525
|
+
this.values.set(
|
|
526
|
+
msg.id,
|
|
527
|
+
JSON.parse(apply2(cur, JSON.stringify(msg.patch)))
|
|
528
|
+
);
|
|
529
|
+
this.revs.set(msg.id, msg.patch.rev);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
/** The current mirrored core `Value` of a model. */
|
|
533
|
+
value(id) {
|
|
534
|
+
return this.values.get(id);
|
|
535
|
+
}
|
|
536
|
+
ids() {
|
|
537
|
+
return [...this.values.keys()];
|
|
538
|
+
}
|
|
539
|
+
/** Propose an edit to a mirrored model; returns the patch frame to send (encoded in this codec).
|
|
540
|
+
*
|
|
541
|
+
* Server-authoritative: the local mirror updates when the server echoes the authoritative patch
|
|
542
|
+
* back via `recv`, not optimistically.
|
|
543
|
+
*/
|
|
544
|
+
edit(id, value) {
|
|
545
|
+
const patch = JSON.parse(
|
|
546
|
+
diff2(JSON.stringify(this.values.get(id)), JSON.stringify(value))
|
|
547
|
+
);
|
|
548
|
+
const msg = { t: "patch", id, patch };
|
|
549
|
+
const custom = codecFor(this.codec);
|
|
550
|
+
if (custom) return custom.encode(msg);
|
|
551
|
+
const s = JSON.stringify(msg);
|
|
552
|
+
return this.codec === "msgpack" ? jsonToMsgpack(s) : s;
|
|
553
|
+
}
|
|
554
|
+
/** Connect to a transports server and mirror it. Returns the `WebSocket`. */
|
|
555
|
+
connect(url) {
|
|
556
|
+
const sep = url.includes("?") ? "&" : "?";
|
|
557
|
+
const ws = new WebSocket(`${url}${sep}codec=${this.codec}`);
|
|
558
|
+
ws.binaryType = "arraybuffer";
|
|
559
|
+
ws.addEventListener("message", (e) => {
|
|
560
|
+
const data = e.data;
|
|
561
|
+
this.recv(
|
|
562
|
+
typeof data === "string" ? data : new Uint8Array(data)
|
|
563
|
+
);
|
|
564
|
+
});
|
|
565
|
+
return ws;
|
|
566
|
+
}
|
|
567
|
+
/** Mirror a server over Server-Sent Events (receive-only, JSON). Returns the `EventSource`. */
|
|
568
|
+
connectSSE(url) {
|
|
569
|
+
const es = new EventSource(url);
|
|
570
|
+
es.addEventListener(
|
|
571
|
+
"message",
|
|
572
|
+
(e) => this.recv(e.data)
|
|
573
|
+
);
|
|
574
|
+
return es;
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
// src/ts/index.ts
|
|
579
|
+
var placeholder = "";
|
|
580
|
+
var diff2 = (oldModel, newModel) => diff(oldModel, newModel);
|
|
581
|
+
var apply2 = (model, patch) => apply(model, patch);
|
|
582
|
+
var encode2 = (model) => encode(model);
|
|
583
|
+
var decode2 = (bytes) => decode(bytes);
|
|
584
|
+
var encodeAs = (model, codec) => encode_as(model, codec);
|
|
585
|
+
var decodeAs = (bytes, codec) => decode_as(bytes, codec);
|
|
586
|
+
var jsonToMsgpack = (json) => json_to_msgpack(json);
|
|
587
|
+
var msgpackToJson = (bytes) => msgpack_to_json(bytes);
|
|
588
|
+
var Store2 = Store;
|
|
589
|
+
export {
|
|
590
|
+
Client,
|
|
591
|
+
Store2 as Store,
|
|
592
|
+
apply2 as apply,
|
|
593
|
+
codecFor,
|
|
594
|
+
decode2 as decode,
|
|
595
|
+
decodeAs,
|
|
596
|
+
diff2 as diff,
|
|
597
|
+
encode2 as encode,
|
|
598
|
+
encodeAs,
|
|
599
|
+
fromValue,
|
|
600
|
+
jsonToMsgpack,
|
|
601
|
+
msgpackToJson,
|
|
602
|
+
placeholder,
|
|
603
|
+
registerCodec,
|
|
604
|
+
toValue,
|
|
605
|
+
unregisterCodec,
|
|
606
|
+
transports_exports as wasm
|
|
607
|
+
};
|
|
608
|
+
//# sourceMappingURL=index.js.map
|