@based/schema 5.0.0-alpha.9 → 5.0.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.
Files changed (48) hide show
  1. package/dist/def/addEdges.js +26 -3
  2. package/dist/def/createEmptyDef.d.ts +13 -9
  3. package/dist/def/createEmptyDef.js +7 -3
  4. package/dist/def/defaultMap.js +28 -24
  5. package/dist/def/fillEmptyMain.d.ts +2 -2
  6. package/dist/def/fillEmptyMain.js +13 -4
  7. package/dist/def/index.d.ts +0 -1
  8. package/dist/def/index.js +0 -1
  9. package/dist/def/makeSeparateSort.js +6 -6
  10. package/dist/def/makeSeparateTextSort.js +12 -12
  11. package/dist/def/refSet.d.ts +7 -0
  12. package/dist/def/refSet.js +25 -0
  13. package/dist/def/selvaBuffer.js +77 -28
  14. package/dist/def/typeDef.d.ts +9 -3
  15. package/dist/def/typeDef.js +136 -59
  16. package/dist/def/typeIndexes.d.ts +40 -0
  17. package/dist/def/typeIndexes.js +50 -0
  18. package/dist/def/types.d.ts +36 -57
  19. package/dist/def/types.js +22 -28
  20. package/dist/def/utils.d.ts +5 -3
  21. package/dist/def/utils.js +44 -2
  22. package/dist/def/validation.d.ts +2 -0
  23. package/dist/def/validation.js +89 -41
  24. package/dist/index.d.ts +3 -1
  25. package/dist/index.js +3 -1
  26. package/dist/infer.d.ts +82 -0
  27. package/dist/infer.js +5 -0
  28. package/dist/lang.d.ts +3 -1
  29. package/dist/lang.js +6 -0
  30. package/dist/parse/assert.d.ts +4 -0
  31. package/dist/parse/assert.js +19 -2
  32. package/dist/parse/index.d.ts +2 -0
  33. package/dist/parse/index.js +58 -4
  34. package/dist/parse/props.d.ts +1 -0
  35. package/dist/parse/props.js +171 -54
  36. package/dist/serialize.d.ts +14 -0
  37. package/dist/serialize.js +543 -0
  38. package/dist/types.d.ts +74 -20
  39. package/dist/types.js +3 -1
  40. package/package.json +6 -4
  41. package/dist/def/getPropLen.d.ts +0 -3
  42. package/dist/def/getPropLen.js +0 -23
  43. package/dist/def/makePacked.d.ts +0 -3
  44. package/dist/def/makePacked.js +0 -50
  45. package/dist/def/readFromPacked.d.ts +0 -3
  46. package/dist/def/readFromPacked.js +0 -140
  47. package/dist/mermaid.d.ts +0 -3
  48. package/dist/mermaid.js +0 -24
@@ -0,0 +1,543 @@
1
+ // import * as deflate from 'fflate'
2
+ import { stringFormats } from './types.js';
3
+ import { ENUM, REVERSE_TYPE_INDEX_MAP, TYPE_INDEX_MAP } from './def/types.js';
4
+ import { readDoubleLE, readUint16, readUint24, readUint32, writeDoubleLE, writeUint16, writeUint24, writeUint32, ENCODER, DECODER, } from '@based/utils';
5
+ const UINT8 = 245;
6
+ const FALSE = 246;
7
+ const TRUE = 247;
8
+ const FUNCTION = 248;
9
+ const STRING = 249;
10
+ const ARRAY = 250;
11
+ const BINARY = 251;
12
+ const UINT32 = 252;
13
+ const FLOAT64 = 253;
14
+ const SCHEMA_PROP = 254;
15
+ const OBJECT = 255;
16
+ // Key Address encoding types
17
+ const KEY_ADDRESS_1_BYTE = 0;
18
+ const KEY_ADDRESS_2_BYTES = 1;
19
+ const KEY_ADDRESS_3_BYTES = 2;
20
+ // Key types
21
+ const PROPS = 3;
22
+ const TYPES = 4;
23
+ const READONLY = 5;
24
+ const FORMAT = 6;
25
+ const REQUIRED = 7;
26
+ const REF = 8;
27
+ const PROP = 9;
28
+ const KEY_OPTS = PROP;
29
+ const ensureCapacity = (required) => {
30
+ if (schemaBuffer.len + required > schemaBuffer.buf.length) {
31
+ const newBuf = new Uint8Array(Math.max(schemaBuffer.buf.length * 2, schemaBuffer.len + required));
32
+ newBuf.set(schemaBuffer.buf);
33
+ schemaBuffer.buf = newBuf;
34
+ }
35
+ };
36
+ let schemaBuffer;
37
+ const handleSingleValue = (ops, val, obj, prev, fromObject, key) => {
38
+ const type = typeof val;
39
+ // typed Array - single PROP
40
+ if (val instanceof Uint8Array) {
41
+ ensureCapacity(1 + 2 + val.byteLength);
42
+ schemaBuffer.buf[schemaBuffer.len] = BINARY;
43
+ schemaBuffer.len += 1;
44
+ schemaBuffer.buf[schemaBuffer.len] = val.byteLength;
45
+ schemaBuffer.len += 1;
46
+ schemaBuffer.buf[schemaBuffer.len] = val.byteLength >>> 8;
47
+ schemaBuffer.len += 1;
48
+ schemaBuffer.buf.set(val, schemaBuffer.len);
49
+ schemaBuffer.len += val.byteLength;
50
+ }
51
+ else if (type === 'function') {
52
+ // Support both arrow functions and methods (including shorthand method syntax)
53
+ let str = val.toString();
54
+ if (/^[a-zA-Z0-9_$]+\s*\(/.test(str)) {
55
+ str = 'function ' + str;
56
+ }
57
+ ensureCapacity(1 + 2 + str.length * 4);
58
+ schemaBuffer.buf[schemaBuffer.len] = FUNCTION;
59
+ schemaBuffer.len += 1;
60
+ const sizeIndex = schemaBuffer.len;
61
+ schemaBuffer.len += 2;
62
+ // encodeInto is much faster as it avoids intermediate allocation.
63
+ const r = ENCODER.encodeInto(str, schemaBuffer.buf.subarray(schemaBuffer.len));
64
+ schemaBuffer.len += r.written;
65
+ schemaBuffer.buf[sizeIndex] = r.written;
66
+ schemaBuffer.buf[sizeIndex + 1] = r.written >>> 8;
67
+ }
68
+ else if (type === 'object') {
69
+ // fromObject
70
+ if (val === null) {
71
+ }
72
+ else {
73
+ if (!fromObject && key === 'props' && obj.type === 'object') {
74
+ walk(ops, val, obj, prev, true, schemaBuffer);
75
+ }
76
+ else {
77
+ walk(ops, val, obj, prev, fromObject, schemaBuffer);
78
+ }
79
+ }
80
+ }
81
+ else if (type === 'boolean') {
82
+ ensureCapacity(1);
83
+ schemaBuffer.buf[schemaBuffer.len] = val ? TRUE : FALSE;
84
+ schemaBuffer.len += 1;
85
+ }
86
+ else if (type === 'string') {
87
+ // Pessimistically assume 4 bytes per char for UTF-8 to be safe.
88
+ ensureCapacity(1 + 2 + val.length * 4);
89
+ schemaBuffer.buf[schemaBuffer.len] = STRING;
90
+ schemaBuffer.len += 1;
91
+ const sizeIndex = schemaBuffer.len;
92
+ schemaBuffer.len += 2;
93
+ // encodeInto is much faster as it avoids intermediate allocation.
94
+ const r = ENCODER.encodeInto(val, schemaBuffer.buf.subarray(schemaBuffer.len));
95
+ schemaBuffer.len += r.written;
96
+ schemaBuffer.buf[sizeIndex] = r.written;
97
+ schemaBuffer.buf[sizeIndex + 1] = r.written >>> 8;
98
+ }
99
+ else if (type === 'number') {
100
+ const isInt = val % 1 === 0;
101
+ if (val < 256 && val > 0 && isInt) {
102
+ ensureCapacity(2);
103
+ schemaBuffer.buf[schemaBuffer.len] = UINT8;
104
+ schemaBuffer.len += 1;
105
+ schemaBuffer.buf[schemaBuffer.len] = val;
106
+ schemaBuffer.len += 1;
107
+ }
108
+ else if (val < 4294967295 && val > 0 && isInt) {
109
+ ensureCapacity(5);
110
+ schemaBuffer.buf[schemaBuffer.len] = UINT32;
111
+ schemaBuffer.len += 1;
112
+ writeUint32(schemaBuffer.buf, val, schemaBuffer.len);
113
+ schemaBuffer.len += 4;
114
+ }
115
+ else {
116
+ ensureCapacity(9);
117
+ schemaBuffer.buf[schemaBuffer.len] = FLOAT64;
118
+ schemaBuffer.len += 1;
119
+ writeDoubleLE(schemaBuffer.buf, val, schemaBuffer.len);
120
+ schemaBuffer.len += 8;
121
+ }
122
+ }
123
+ };
124
+ const encodeKey = (key, schemaBuffer) => {
125
+ let dictKey = schemaBuffer.dictMap[key];
126
+ // if len == 1 never from address
127
+ if (!dictKey) {
128
+ dictKey = {
129
+ changed: 0,
130
+ address: 0,
131
+ // used: [],
132
+ };
133
+ // pessimistically assume 4 bytes per char for UTF-8 to be safe.
134
+ ensureCapacity(1 + key.length * 4);
135
+ dictKey.address = schemaBuffer.len;
136
+ schemaBuffer.len += 1;
137
+ const r = ENCODER.encodeInto(key, schemaBuffer.buf.subarray(schemaBuffer.len));
138
+ schemaBuffer.buf[dictKey.address] = r.written + KEY_OPTS;
139
+ schemaBuffer.len += r.written;
140
+ // USED is the problem now
141
+ schemaBuffer.dictMapArr.push(dictKey);
142
+ schemaBuffer.dictMap[key] = dictKey;
143
+ }
144
+ else {
145
+ ensureCapacity(4);
146
+ // updated address? maybe
147
+ const dictMapUsed = { address: schemaBuffer.len, key: dictKey };
148
+ schemaBuffer.dictMapUsed.push(dictMapUsed);
149
+ // used can be handled differently - also pass to
150
+ // dictKey.used.push(dictMapUsed)
151
+ // console.log('USE KEY!', key)
152
+ // have to check this to correct - correctly
153
+ if (dictKey.address > 65025) {
154
+ schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_3_BYTES;
155
+ schemaBuffer.len += 1;
156
+ writeUint24(schemaBuffer.buf, dictKey.address, schemaBuffer.len);
157
+ schemaBuffer.len += 3;
158
+ }
159
+ else if (dictKey.address > 255) {
160
+ schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_2_BYTES;
161
+ schemaBuffer.len += 1;
162
+ writeUint16(schemaBuffer.buf, dictKey.address, schemaBuffer.len);
163
+ schemaBuffer.len += 2;
164
+ }
165
+ else {
166
+ schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_1_BYTE;
167
+ schemaBuffer.len += 1;
168
+ schemaBuffer.buf[schemaBuffer.len] = dictKey.address;
169
+ schemaBuffer.len += 1;
170
+ }
171
+ }
172
+ };
173
+ // 3 level
174
+ // 0 for queries (min)
175
+ // 1 for modify
176
+ // 2 fulls schema
177
+ const walk = (opts, obj, prev, prev2, fromObject, schemaBuffer) => {
178
+ let start = schemaBuffer.len;
179
+ const isArray = Array.isArray(obj);
180
+ const isFromObj = prev2?.type === 'object' || fromObject === false;
181
+ const isSchemaProp = ('enum' in obj || ('type' in obj && TYPE_INDEX_MAP[obj.type])) && isFromObj;
182
+ ensureCapacity(1 + 5); // Type byte + size
183
+ if (isSchemaProp) {
184
+ schemaBuffer.buf[schemaBuffer.len++] = SCHEMA_PROP;
185
+ const typeIndex = TYPE_INDEX_MAP['enum' in obj ? 'enum' : obj.type];
186
+ schemaBuffer.buf[schemaBuffer.len++] = typeIndex;
187
+ }
188
+ else {
189
+ schemaBuffer.buf[schemaBuffer.len++] = isArray ? ARRAY : OBJECT;
190
+ }
191
+ let sizeIndex = schemaBuffer.len;
192
+ schemaBuffer.len += 4;
193
+ if (isArray) {
194
+ const len = obj.length;
195
+ ensureCapacity(2 * len + 2);
196
+ writeUint16(schemaBuffer.buf, len, schemaBuffer.len);
197
+ schemaBuffer.len += 2;
198
+ for (let j = 0; j < len; j++) {
199
+ if (len < 256) {
200
+ schemaBuffer.buf[schemaBuffer.len] = j;
201
+ schemaBuffer.len += 1;
202
+ }
203
+ else {
204
+ writeUint16(schemaBuffer.buf, j, schemaBuffer.len);
205
+ schemaBuffer.len += 2;
206
+ }
207
+ handleSingleValue(opts, obj[j], obj, prev, fromObject, j);
208
+ }
209
+ }
210
+ else {
211
+ for (const key in obj) {
212
+ if (opts.readOnly &&
213
+ isFromObj &&
214
+ (key === 'validation' || key === 'default')) {
215
+ if (key === 'validation' && typeof obj[key] === 'function') {
216
+ continue;
217
+ }
218
+ else if (key === 'default') {
219
+ continue;
220
+ }
221
+ }
222
+ else if (isFromObj &&
223
+ (opts.stripMetaInformation || opts.readOnly) &&
224
+ (key === 'title' ||
225
+ key === 'description' ||
226
+ key === 'format' ||
227
+ key === 'display') &&
228
+ typeof obj[key] === 'string') {
229
+ continue;
230
+ }
231
+ else if (key === 'type' && isSchemaProp) {
232
+ continue;
233
+ }
234
+ else if (key === 'required' && obj[key] === true) {
235
+ ensureCapacity(1);
236
+ schemaBuffer.buf[schemaBuffer.len] = REQUIRED;
237
+ schemaBuffer.len += 1;
238
+ continue;
239
+ }
240
+ // Add this later
241
+ else if (key == 'ref' && isFromObj && typeof obj.ref === 'string') {
242
+ ensureCapacity(1);
243
+ schemaBuffer.buf[schemaBuffer.len] = REF;
244
+ schemaBuffer.len += 1;
245
+ encodeKey(obj[key], schemaBuffer);
246
+ continue;
247
+ }
248
+ else if (key === 'prop' && isFromObj && typeof obj.prop === 'string') {
249
+ ensureCapacity(1);
250
+ schemaBuffer.buf[schemaBuffer.len] = PROP;
251
+ schemaBuffer.len += 1;
252
+ encodeKey(obj[key], schemaBuffer);
253
+ continue;
254
+ }
255
+ else if (key === 'readOnly' && obj[key] === true) {
256
+ ensureCapacity(1);
257
+ schemaBuffer.buf[schemaBuffer.len] = READONLY;
258
+ schemaBuffer.len += 1;
259
+ continue;
260
+ }
261
+ else if (key === 'format' && isFromObj) {
262
+ ensureCapacity(2);
263
+ schemaBuffer.buf[schemaBuffer.len] = FORMAT;
264
+ schemaBuffer.len += 1;
265
+ schemaBuffer.buf[schemaBuffer.len] = stringFormats.indexOf(obj.format);
266
+ schemaBuffer.len += 1;
267
+ continue;
268
+ }
269
+ else {
270
+ if (key === 'types') {
271
+ ensureCapacity(1);
272
+ schemaBuffer.buf[schemaBuffer.len] = TYPES;
273
+ schemaBuffer.len += 1;
274
+ }
275
+ else if (key === 'props') {
276
+ ensureCapacity(1);
277
+ schemaBuffer.buf[schemaBuffer.len] = PROPS;
278
+ schemaBuffer.len += 1;
279
+ }
280
+ else {
281
+ encodeKey(key, schemaBuffer);
282
+ }
283
+ // important to handle the size here...
284
+ handleSingleValue(opts, obj[key], obj, prev, fromObject, key);
285
+ }
286
+ }
287
+ }
288
+ let size = schemaBuffer.len - start;
289
+ // 3 different sizes? 3, 2, 1 ?
290
+ if (size < 252) {
291
+ schemaBuffer.keyChangeIndex++;
292
+ schemaBuffer.buf[sizeIndex] = size; // + 3 - 3
293
+ for (let i = schemaBuffer.dictMapArr.length - 1; i > -1; i--) {
294
+ const keyDict = schemaBuffer.dictMapArr[i];
295
+ if (keyDict.address < start) {
296
+ break;
297
+ }
298
+ else {
299
+ keyDict.changed = schemaBuffer.keyChangeIndex;
300
+ keyDict.address -= 3;
301
+ }
302
+ }
303
+ for (let i = schemaBuffer.dictMapUsed.length - 1; i > -1; i--) {
304
+ const keyDictUsed = schemaBuffer.dictMapUsed[i];
305
+ if (keyDictUsed.address < start) {
306
+ break;
307
+ }
308
+ else {
309
+ const keyDict = keyDictUsed.key;
310
+ if (keyDict.changed === schemaBuffer.keyChangeIndex) {
311
+ const addressSize = schemaBuffer.buf[keyDictUsed.address];
312
+ // aslo correct if its smaller... :|
313
+ if (addressSize === KEY_ADDRESS_3_BYTES) {
314
+ writeUint24(schemaBuffer.buf, keyDict.address, keyDictUsed.address + 1);
315
+ }
316
+ else if (addressSize === KEY_ADDRESS_2_BYTES) {
317
+ writeUint16(schemaBuffer.buf, keyDict.address, keyDictUsed.address + 1);
318
+ }
319
+ else if (addressSize === KEY_ADDRESS_1_BYTE) {
320
+ schemaBuffer.buf[keyDictUsed.address + 1] = keyDict.address;
321
+ }
322
+ }
323
+ keyDictUsed.address -= 3;
324
+ }
325
+ }
326
+ schemaBuffer.buf.copyWithin(sizeIndex + 1, sizeIndex + 4, sizeIndex + size);
327
+ schemaBuffer.len -= 3;
328
+ }
329
+ else {
330
+ schemaBuffer.buf[sizeIndex] = 0; // means 4
331
+ writeUint24(schemaBuffer.buf, size, sizeIndex + 1);
332
+ }
333
+ };
334
+ export const serialize = (schema, opts = {}) => {
335
+ if (!schemaBuffer) {
336
+ schemaBuffer = {
337
+ buf: new Uint8Array(5e3), // 5kb default
338
+ len: 0,
339
+ dictMap: {},
340
+ dictMapArr: [],
341
+ dictMapUsed: [],
342
+ keyChangeIndex: 0,
343
+ };
344
+ }
345
+ schemaBuffer.keyChangeIndex = 0;
346
+ schemaBuffer.len = 0;
347
+ schemaBuffer.dictMap = {};
348
+ schemaBuffer.dictMapArr = [];
349
+ schemaBuffer.dictMapUsed = [];
350
+ // defalte not supported in unpacking yet
351
+ const isDeflate = 0; // opts.deflate ? 1 : 0
352
+ walk(opts, schema, undefined, undefined, false, schemaBuffer);
353
+ const packed = new Uint8Array(schemaBuffer.buf.subarray(0, schemaBuffer.len));
354
+ // if (isDeflate) {
355
+ // // add extra byte! see if nessecary
356
+ // return deflate.deflateSync(packed)
357
+ // } else {
358
+ // console.log('USED', schemaBuffer.dictMapUsed.length)
359
+ return packed;
360
+ // }
361
+ };
362
+ // -------------
363
+ export const deSerializeKey = (buf, keySize, i) => {
364
+ let size = 0;
365
+ let value;
366
+ if (keySize === KEY_ADDRESS_3_BYTES) {
367
+ const dictAddress = readUint24(buf, i);
368
+ size += 3;
369
+ const actualKeySize = buf[dictAddress] - KEY_OPTS;
370
+ value = DECODER.decode(buf.subarray(dictAddress + 1, actualKeySize + dictAddress + 1));
371
+ }
372
+ else if (keySize === KEY_ADDRESS_2_BYTES) {
373
+ const dictAddress = readUint16(buf, i);
374
+ size += 2;
375
+ const actualKeySize = buf[dictAddress] - KEY_OPTS;
376
+ value = DECODER.decode(buf.subarray(dictAddress + 1, actualKeySize + dictAddress + 1));
377
+ }
378
+ else if (keySize === KEY_ADDRESS_1_BYTE) {
379
+ const dictAddress = buf[i];
380
+ size += 1;
381
+ const actualKeySize = buf[dictAddress] - KEY_OPTS;
382
+ value = DECODER.decode(buf.subarray(dictAddress + 1, actualKeySize + dictAddress + 1));
383
+ }
384
+ else {
385
+ const actualKeySize = keySize - KEY_OPTS;
386
+ value = DECODER.decode(buf.subarray(i, actualKeySize + i));
387
+ size += actualKeySize;
388
+ }
389
+ return { size, value };
390
+ };
391
+ export const deSerializeInner = (buf, obj, start, fromArray) => {
392
+ let i = start;
393
+ const isSchemaProp = buf[i] === SCHEMA_PROP;
394
+ i += 1;
395
+ if (isSchemaProp) {
396
+ const type = buf[i];
397
+ const parsedType = REVERSE_TYPE_INDEX_MAP[type];
398
+ if (type !== ENUM) {
399
+ obj.type = parsedType;
400
+ }
401
+ i += 1;
402
+ }
403
+ let size;
404
+ if (buf[i] === 0) {
405
+ size = readUint24(buf, i + 1);
406
+ i += 4;
407
+ }
408
+ else {
409
+ size = buf[i] - 3;
410
+ i += 1;
411
+ }
412
+ const end = size + start;
413
+ if (fromArray) {
414
+ i += 2;
415
+ }
416
+ while (i < end) {
417
+ let key;
418
+ if (fromArray) {
419
+ if (obj.length < 256) {
420
+ key = buf[i];
421
+ i += 1;
422
+ }
423
+ else {
424
+ key = readUint16(buf, i);
425
+ i += 2;
426
+ }
427
+ }
428
+ else {
429
+ let keySize = buf[i];
430
+ i += 1;
431
+ // format!
432
+ if (keySize === REQUIRED) {
433
+ obj.required = true;
434
+ continue;
435
+ }
436
+ else if (keySize === FORMAT) {
437
+ obj.format = stringFormats[buf[i]];
438
+ i += 1;
439
+ continue;
440
+ }
441
+ else if (keySize === READONLY) {
442
+ obj.readOnly = true;
443
+ continue;
444
+ }
445
+ else if (keySize === TYPES) {
446
+ key = 'types';
447
+ }
448
+ else if (keySize === PROPS) {
449
+ key = 'props';
450
+ }
451
+ else if (keySize === REF) {
452
+ const valueKeySize = buf[i];
453
+ i += 1;
454
+ const { size, value } = deSerializeKey(buf, valueKeySize, i);
455
+ i += size;
456
+ obj.ref = value;
457
+ continue;
458
+ }
459
+ else if (keySize === PROP) {
460
+ const valueKeySize = buf[i];
461
+ i += 1;
462
+ const { size, value } = deSerializeKey(buf, valueKeySize, i);
463
+ i += size;
464
+ obj.prop = value;
465
+ continue;
466
+ }
467
+ else {
468
+ const { size, value } = deSerializeKey(buf, keySize, i);
469
+ i += size;
470
+ key = value;
471
+ }
472
+ }
473
+ if (buf[i] === UINT8) {
474
+ i += 1;
475
+ obj[key] = buf[i];
476
+ i += 1;
477
+ }
478
+ else if (buf[i] === FALSE) {
479
+ i += 1;
480
+ obj[key] = false;
481
+ }
482
+ else if (buf[i] === TRUE) {
483
+ i += 1;
484
+ obj[key] = true;
485
+ }
486
+ else if (buf[i] === FUNCTION) {
487
+ i += 1;
488
+ const size = readUint16(buf, i);
489
+ i += 2;
490
+ const fn = `return (${DECODER.decode(buf.subarray(i, i + size))})(payload, prop)`;
491
+ obj[key] = new Function('payload', 'prop', fn);
492
+ i += size;
493
+ }
494
+ else if (buf[i] === STRING) {
495
+ i += 1;
496
+ const size = readUint16(buf, i);
497
+ i += 2;
498
+ obj[key] = DECODER.decode(buf.subarray(i, i + size));
499
+ i += size;
500
+ }
501
+ else if (buf[i] === BINARY) {
502
+ i += 1;
503
+ const size = readUint16(buf, i);
504
+ i += 2;
505
+ obj[key] = buf.subarray(i, size + i);
506
+ i += size;
507
+ }
508
+ else if (buf[i] === UINT32) {
509
+ obj[key] = readUint32(buf, i + 1);
510
+ i += 5;
511
+ }
512
+ else if (buf[i] === FLOAT64) {
513
+ obj[key] = readDoubleLE(buf, i + 1);
514
+ i += 9;
515
+ }
516
+ else if (buf[i] === OBJECT || buf[i] === SCHEMA_PROP) {
517
+ const nest = (obj[key] = {});
518
+ const fieldSize = deSerializeInner(buf, nest, i, false);
519
+ i += fieldSize;
520
+ }
521
+ else if (buf[i] === ARRAY) {
522
+ const len = readUint16(buf, i + 3);
523
+ const nest = (obj[key] = new Array(len));
524
+ const fieldSize = deSerializeInner(buf, nest, i, true);
525
+ i += fieldSize;
526
+ }
527
+ else {
528
+ console.warn('Invalid value type', buf[i], 'skip');
529
+ // Invalid value type
530
+ i += 1;
531
+ const size = buf[i] | ((buf[i + 1] << 8) >>> 0);
532
+ i += size;
533
+ }
534
+ }
535
+ return i - start;
536
+ };
537
+ export const deSerialize = (buf) => {
538
+ // if first byte is deflate
539
+ const schema = {};
540
+ deSerializeInner(buf, schema, 0, false);
541
+ return schema;
542
+ };
543
+ //# sourceMappingURL=serialize.js.map