mol_vary 0.0.5 → 0.0.7

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/node.test.js CHANGED
@@ -29,60 +29,6 @@ var $;
29
29
  $.$mol_fail = $mol_fail;
30
30
  })($ || ($ = {}));
31
31
 
32
- ;
33
- "use strict";
34
- var $;
35
- (function ($) {
36
- function $mol_hash_numbers(buff, seed = 0) {
37
- let h1 = 0xdeadbeef ^ seed;
38
- let h2 = 0x41c6ce57 ^ seed;
39
- for (let i = 0; i < buff.length; ++i) {
40
- const item = buff[i];
41
- h1 = Math.imul(h1 ^ item, 2654435761);
42
- h2 = Math.imul(h2 ^ item, 1597334677);
43
- }
44
- h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
45
- h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
46
- return 4294967296 * (((1 << 16) - 1) & h2) + (h1 >>> 0);
47
- }
48
- $.$mol_hash_numbers = $mol_hash_numbers;
49
- })($ || ($ = {}));
50
-
51
- ;
52
- "use strict";
53
- var $;
54
- (function ($) {
55
- function $mol_bigint_encode(num) {
56
- const minus = num < 0n ? 255 : 0;
57
- num = minus ? -num - 1n : num;
58
- const bytes = [];
59
- do {
60
- let byte = minus ^ Number(num % 256n);
61
- bytes.push(byte);
62
- if (num >>= 8n)
63
- continue;
64
- if ((minus & 128) !== (byte & 128))
65
- bytes.push(minus);
66
- break;
67
- } while (num);
68
- return new Uint8Array(bytes);
69
- }
70
- $.$mol_bigint_encode = $mol_bigint_encode;
71
- })($ || ($ = {}));
72
-
73
- ;
74
- "use strict";
75
- var $;
76
- (function ($) {
77
- function $mol_hash_string(str, seed = 0) {
78
- let nums = new Array(str.length);
79
- for (let i = 0; i < str.length; ++i)
80
- nums[i] = str.charCodeAt(i);
81
- return $mol_hash_numbers(nums);
82
- }
83
- $.$mol_hash_string = $mol_hash_string;
84
- })($ || ($ = {}));
85
-
86
32
  ;
87
33
  "use strict";
88
34
  var $;
@@ -157,6 +103,59 @@ var $;
157
103
  $.$mol_charset_decode = $mol_charset_decode;
158
104
  })($ || ($ = {}));
159
105
 
106
+ ;
107
+ "use strict";
108
+ var $;
109
+ (function ($) {
110
+ function $mol_charset_decode_from(buffer, from, count) {
111
+ let res = '';
112
+ let pos = from;
113
+ while (pos < buffer.length && res.length < count) {
114
+ const byte1 = buffer[pos++];
115
+ if (byte1 <= 0x7F) {
116
+ res += String.fromCharCode(byte1);
117
+ }
118
+ else if ((byte1 & 0xE0) === 0xC0) {
119
+ if (pos >= buffer.length)
120
+ break;
121
+ const byte2 = buffer[pos++];
122
+ let code = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
123
+ res += String.fromCharCode(code);
124
+ }
125
+ else if ((byte1 & 0xF0) === 0xE0) {
126
+ if (pos + 1 >= buffer.length)
127
+ break;
128
+ const byte2 = buffer[pos++];
129
+ const byte3 = buffer[pos++];
130
+ let code = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
131
+ res += String.fromCharCode(code);
132
+ }
133
+ else if ((byte1 & 0xF8) === 0xF0) {
134
+ if (pos + 2 >= buffer.length)
135
+ break;
136
+ const byte2 = buffer[pos++];
137
+ const byte3 = buffer[pos++];
138
+ const byte4 = buffer[pos++];
139
+ let code = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
140
+ if (code > 0xFFFF) {
141
+ code -= 0x10000;
142
+ const hi = 0xD800 + (code >> 10);
143
+ const lo = 0xDC00 + (code & 0x3FF);
144
+ res += String.fromCharCode(hi, lo);
145
+ }
146
+ else {
147
+ res += String.fromCharCode(code);
148
+ }
149
+ }
150
+ else {
151
+ res += '�';
152
+ }
153
+ }
154
+ return [res, pos - from];
155
+ }
156
+ $.$mol_charset_decode_from = $mol_charset_decode_from;
157
+ })($ || ($ = {}));
158
+
160
159
  ;
161
160
  "use strict";
162
161
  var $;
@@ -188,319 +187,286 @@ var $;
188
187
  $mol_vary_spec[$mol_vary_spec["fp32"] = 94] = "fp32";
189
188
  $mol_vary_spec[$mol_vary_spec["fp64"] = 95] = "fp64";
190
189
  })($mol_vary_spec = $.$mol_vary_spec || ($.$mol_vary_spec = {}));
191
- function num_len(num) {
192
- if (typeof num === 'number' && !Number.isInteger(num))
193
- return 8;
194
- if (num < 0) {
195
- if (num >= -28)
196
- return 0;
197
- if (num >= -(2 ** 7))
198
- return 1;
199
- if (num >= -(2 ** 15))
200
- return 2;
201
- if (num >= -(2 ** 31))
202
- return 4;
203
- if (num >= -(2n ** 63n))
204
- return 8;
205
- $mol_fail(new Error(`Too low numb ${num}`));
206
- }
207
- else {
208
- if (num < 28)
209
- return 0;
210
- if (num < 2 ** 8)
211
- return 1;
212
- if (num < 2 ** 16)
213
- return 2;
214
- if (num < 2 ** 32)
215
- return 4;
216
- if (num < 2n ** 64n)
217
- return 8;
218
- $mol_fail(new Error(`Too high numb ${num}`));
219
- }
220
- }
190
+ let offsets = new Map();
191
+ let sizes = new Map();
221
192
  class $mol_vary extends DataView {
222
- static buffer = new Uint8Array(4096);
223
- static buffer_view = new this(this.buffer.buffer);
224
- static allocate(size) {
225
- if (this.buffer.byteLength > size)
226
- return;
227
- this.buffer = new Uint8Array(Math.ceil(size / 4096) * 4096);
228
- this.buffer_view = new this(this.buffer.buffer);
229
- }
230
193
  static pack(data) {
231
- const leaned = new WeakMap();
232
- const lean = (val) => {
233
- let tupl = leaned.get(val);
234
- if (tupl)
235
- return tupl;
236
- leaned.set(val, tupl = this.lean(val));
237
- return tupl;
194
+ let pos = 0;
195
+ let capacity = 0;
196
+ const acquire = (size) => {
197
+ capacity += size;
198
+ if (buffer.byteLength >= capacity)
199
+ return;
200
+ const buffer2 = new Uint8Array(Math.ceil(capacity / 4096) * 4096);
201
+ buffer2.set(buffer);
202
+ buffer = buffer2;
203
+ pack = new DataView(buffer.buffer);
238
204
  };
239
- const hashes = new Map();
240
- const hash = (val) => {
241
- switch (typeof val) {
242
- case 'undefined':
243
- return 0;
244
- case 'number':
245
- case 'boolean':
246
- return Number(val);
247
- case 'number':
248
- return val;
249
- case 'bigint':
250
- return $mol_hash_numbers($mol_bigint_encode(val), $mol_vary_tip.sint);
251
- case 'string': {
252
- let res = hashes.get(val);
253
- if (res != null)
254
- return res;
255
- res = $mol_hash_string(val, $mol_vary_tip.text);
256
- hashes.set(val, res);
257
- return res;
258
- }
259
- case 'object': {
260
- if (!val)
261
- return $mol_vary_spec.none;
262
- let res = hashes.get(val);
263
- if (res != null)
264
- return res;
265
- if (ArrayBuffer.isView(val))
266
- res = $mol_hash_numbers(new Uint8Array(val.buffer, val.byteOffset, val.byteLength), $mol_vary_tip.blob);
267
- else if (Array.isArray(val))
268
- res = $mol_hash_numbers(val.map(hash), $mol_vary_tip.list);
269
- else {
270
- const [keys, vals] = lean(val);
271
- res = $mol_hash_numbers(vals.map(hash), hash(keys));
272
- }
273
- hashes.set(val, res);
274
- return res;
275
- }
205
+ const release = (size) => {
206
+ capacity -= size;
207
+ };
208
+ const dump_snum = (tip, val) => {
209
+ if (val >= -28) {
210
+ pack.setInt8(pos++, tip | Number(val));
211
+ release(9);
212
+ }
213
+ else if (val >= -(2 ** 7)) {
214
+ pack.setInt8(pos++, tip | (-1 - $.$mol_vary_len[1]));
215
+ pack.setInt8(pos, Number(val));
216
+ pos += 1;
217
+ release(8);
218
+ }
219
+ else if (val >= -(2 ** 15)) {
220
+ pack.setInt8(pos++, tip | (-1 - $.$mol_vary_len[2]));
221
+ pack.setInt16(pos, Number(val), true);
222
+ pos += 2;
223
+ release(7);
224
+ }
225
+ else if (val >= -(2 ** 31)) {
226
+ pack.setInt8(pos++, tip | (-1 - $.$mol_vary_len[4]));
227
+ pack.setInt32(pos, Number(val), true);
228
+ pos += 4;
229
+ release(5);
230
+ }
231
+ else if (val >= -(2n ** 63n)) {
232
+ pack.setInt8(pos++, tip | (-1 - $.$mol_vary_len[8]));
233
+ pack.setBigInt64(pos, BigInt(val), true);
234
+ pos += 8;
235
+ release(1);
236
+ }
237
+ else {
238
+ $mol_fail(new Error('Number too low', { cause: val }));
276
239
  }
277
- $mol_fail(new Error(`Unsupported type`));
278
240
  };
279
- let size = 0;
280
- const offsets = new Map();
281
- const sizes = new Map();
282
- const stream = [];
283
- const dedup = (val, len) => {
284
- const key = hash(val);
285
- let offset = offsets.get(key);
286
- if (offset === undefined) {
287
- offsets.set(key, offset = stream.length);
288
- stream.push(val);
289
- size += len;
290
- return true;
241
+ const dump_unum = (tip, val) => {
242
+ if (val < 28) {
243
+ pack.setUint8(pos++, tip | Number(val));
244
+ release(9);
245
+ }
246
+ else if (val < 2 ** 8) {
247
+ pack.setUint8(pos++, tip | $.$mol_vary_len[1]);
248
+ pack.setUint8(pos, Number(val));
249
+ pos += 1;
250
+ release(8);
251
+ }
252
+ else if (val < 2 ** 16) {
253
+ pack.setUint8(pos++, tip | $.$mol_vary_len[2]);
254
+ pack.setUint16(pos, Number(val), true);
255
+ pos += 2;
256
+ release(7);
257
+ }
258
+ else if (val < 2 ** 32) {
259
+ pack.setUint8(pos++, tip | $.$mol_vary_len[4]);
260
+ pack.setUint32(pos, Number(val), true);
261
+ pos += 4;
262
+ release(5);
263
+ }
264
+ else if (val < 2n ** 64n) {
265
+ pack.setUint8(pos++, tip | $.$mol_vary_len[8]);
266
+ pack.setBigUint64(pos, BigInt(val), true);
267
+ pos += 8;
268
+ release(1);
291
269
  }
292
270
  else {
293
- size += 1 + num_len(offset);
294
- return false;
271
+ $mol_fail(new Error('Number too high', { cause: val }));
295
272
  }
296
273
  };
297
- const calc = (val) => {
298
- if (val == null) {
299
- size += 1;
300
- return true;
274
+ const dump_string = (val) => {
275
+ if (val.length) {
276
+ const offset = offsets.get(val);
277
+ if (offset !== undefined)
278
+ return dump_unum($mol_vary_tip.link, offset);
301
279
  }
302
- switch (typeof val) {
303
- case 'boolean':
304
- size += 1;
305
- return true;
306
- case 'number':
307
- case 'bigint':
308
- size += 1 + num_len(val);
309
- return true;
310
- case 'string': {
311
- let len = sizes.get(val);
312
- if (len == null)
313
- sizes.set(val, len = $mol_charset_encode_size(val));
314
- if (!len) {
315
- size += 1;
316
- return true;
317
- }
318
- return dedup(val, 1 + num_len(len) + len);
319
- }
320
- case 'object': {
321
- if (ArrayBuffer.isView(val)) {
322
- if (!val.byteLength) {
323
- size += 2;
324
- return true;
325
- }
326
- return dedup(val, 2 + num_len(val.byteLength) + val.byteLength);
327
- }
328
- if (Array.isArray(val)) {
329
- if (!val.length) {
330
- size += 1;
331
- return true;
332
- }
333
- const key = hash(val);
334
- let offset = offsets.get(key);
335
- if (offset === undefined) {
336
- for (const item of val)
337
- calc(item);
338
- }
339
- return dedup(val, 1 + num_len(val.length));
340
- }
341
- const [keys, vals] = lean(val);
342
- if (!vals.length) {
343
- size += 2;
344
- return true;
345
- }
346
- const key = hash(val);
347
- let offset = offsets.get(key);
348
- if (offset === undefined) {
349
- calc(keys);
350
- for (const item of vals)
351
- calc(item);
352
- }
353
- return dedup(val, 1 + num_len(vals.length));
354
- }
280
+ dump_unum($mol_vary_tip.text, val.length);
281
+ acquire(val.length * 3);
282
+ const len = $mol_charset_encode_to(val, buffer, pos);
283
+ pos += len;
284
+ release(val.length * 3 - len - 1);
285
+ if (val.length)
286
+ offsets.set(val, offsets.size);
287
+ return;
288
+ };
289
+ const dump_buffer = (val) => {
290
+ if (val.byteLength) {
291
+ const offset = offsets.get(val);
292
+ if (offset !== undefined)
293
+ return dump_unum($mol_vary_tip.link, offset);
355
294
  }
356
- $mol_fail(new Error(`Unsupported type`));
295
+ dump_unum($mol_vary_tip.blob, val.byteLength);
296
+ if (val instanceof Uint8Array)
297
+ pack.setUint8(pos++, $mol_vary_tip.uint | $.$mol_vary_len[1]);
298
+ else if (val instanceof Uint16Array)
299
+ pack.setUint8(pos++, $mol_vary_tip.uint | $.$mol_vary_len[2]);
300
+ else if (val instanceof Uint32Array)
301
+ pack.setUint8(pos++, $mol_vary_tip.uint | $.$mol_vary_len[4]);
302
+ else if (val instanceof BigUint64Array)
303
+ pack.setUint8(pos++, $mol_vary_tip.uint | $.$mol_vary_len[8]);
304
+ else if (val instanceof Int8Array)
305
+ pack.setUint8(pos++, $mol_vary_tip.sint | ~$.$mol_vary_len[1]);
306
+ else if (val instanceof Int16Array)
307
+ pack.setUint8(pos++, $mol_vary_tip.sint | ~$.$mol_vary_len[2]);
308
+ else if (val instanceof Int32Array)
309
+ pack.setUint8(pos++, $mol_vary_tip.sint | ~$.$mol_vary_len[4]);
310
+ else if (val instanceof BigInt64Array)
311
+ pack.setUint8(pos++, $mol_vary_tip.sint | ~$.$mol_vary_len[8]);
312
+ else if (val instanceof Float32Array)
313
+ pack.setUint8(pos++, $mol_vary_spec.fp32);
314
+ else if (val instanceof Float64Array)
315
+ pack.setUint8(pos++, $mol_vary_spec.fp64);
316
+ else
317
+ $mol_fail(new Error(`Unsupported type`));
318
+ const src = (val instanceof Uint8Array) ? val : new Uint8Array(val.buffer, val.byteOffset, val.byteLength);
319
+ acquire(val.byteLength);
320
+ buffer.set(src, pos);
321
+ pos += val.byteLength;
322
+ if (val.byteLength)
323
+ offsets.set(val, offsets.size);
324
+ };
325
+ const dump_list = (val) => {
326
+ if (val.length) {
327
+ const offset = offsets.get(val);
328
+ if (offset !== undefined)
329
+ return dump_unum($mol_vary_tip.link, offset);
330
+ }
331
+ dump_unum($mol_vary_tip.list, val.length);
332
+ acquire(val.length * 10);
333
+ for (const item of val)
334
+ dump(item);
335
+ if (val.length)
336
+ offsets.set(val, offsets.size);
337
+ };
338
+ const dump_object = (val) => {
339
+ const offset = offsets.get(val);
340
+ if (offset !== undefined)
341
+ return dump_unum($mol_vary_tip.link, offset);
342
+ const proto = Reflect.getPrototypeOf(val);
343
+ const lean = this.leanes.get(proto);
344
+ const keys = lean ? this.keys.get(proto) : Object.keys(val);
345
+ const vals = lean ? lean(val) : Object.values(val);
346
+ dump_unum($mol_vary_tip.tupl, vals.length);
347
+ acquire(vals.length * 2 * 10);
348
+ for (const item of keys)
349
+ dump(item);
350
+ for (const item of vals)
351
+ dump(item);
352
+ if (vals.length)
353
+ offsets.set(val, offsets.size);
357
354
  };
358
- calc(data);
359
- this.allocate(size);
360
- const buf = this.buffer;
361
- const pack = this.buffer_view;
362
- const embedded = new Set();
363
- let pos = 0;
364
355
  const dump = (val) => {
365
- if (pos >= size)
366
- $mol_fail(new Error('Wrong buffer length', { cause: buf }));
367
356
  switch (typeof val) {
368
357
  case 'undefined': {
369
- pack.tlen(pos, 'spec', $mol_vary_spec.both);
370
- pos += 1;
358
+ pack.setUint8(pos++, $mol_vary_spec.both);
359
+ release(9);
371
360
  return;
372
361
  }
373
362
  case 'boolean': {
374
- pack.tlen(pos, 'spec', val ? $mol_vary_spec.true : $mol_vary_spec.fake);
375
- pos += 1;
363
+ pack.setUint8(pos++, val ? $mol_vary_spec.true : $mol_vary_spec.fake);
364
+ release(9);
376
365
  return;
377
366
  }
378
367
  case 'number': {
379
368
  if (!Number.isInteger(val)) {
380
- pos += pack.tfp(pos, 'spec', val);
381
- }
382
- else if (val < 0) {
383
- pos += pack.tint(pos, 'sint', val);
384
- }
385
- else {
386
- pos += pack.tnat(pos, 'uint', val);
369
+ pack.setUint8(pos++, $mol_vary_spec.fp64);
370
+ pack.setFloat64(pos, val, true);
371
+ pos += 8;
372
+ release(1);
373
+ return;
387
374
  }
388
- return;
389
375
  }
390
376
  case 'bigint': {
391
377
  if (val < 0) {
392
- pos += pack.tint(pos, 'sint', val);
378
+ dump_snum($mol_vary_tip.sint, val);
393
379
  }
394
380
  else {
395
- pos += pack.tnat(pos, 'uint', val);
381
+ dump_unum($mol_vary_tip.uint, val);
396
382
  }
397
383
  return;
398
384
  }
399
- case 'string': {
400
- const key = hash(val);
401
- if (embedded.has(key)) {
402
- pos += pack.tnat(pos, 'link', offsets.get(key));
403
- return;
404
- }
405
- pos += pack.tnat(pos, 'text', sizes.get(val));
406
- pos += $mol_charset_encode_to(val, buf, pos);
407
- if (val.length)
408
- embedded.add(key);
409
- return;
410
- }
385
+ case 'string': return dump_string(val);
411
386
  case 'object': {
412
387
  if (!val) {
413
- pack.tlen(pos, 'spec', $mol_vary_spec.none);
414
- pos += 1;
415
- return;
416
- }
417
- if (ArrayBuffer.isView(val)) {
418
- const key = hash(val);
419
- if (embedded.has(key)) {
420
- pos += pack.tnat(pos, 'link', offsets.get(key));
421
- return;
422
- }
423
- pos += pack.tnat(pos, 'blob', val.byteLength);
424
- if (val instanceof Uint8Array)
425
- pos += pack.tlen(pos, 'uint', $.$mol_vary_len[1]);
426
- else if (val instanceof Uint16Array)
427
- pos += pack.tlen(pos, 'uint', $.$mol_vary_len[2]);
428
- else if (val instanceof Uint32Array)
429
- pos += pack.tlen(pos, 'uint', $.$mol_vary_len[4]);
430
- else if (val instanceof BigUint64Array)
431
- pos += pack.tlen(pos, 'uint', $.$mol_vary_len[8]);
432
- else if (val instanceof Int8Array)
433
- pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[1]);
434
- else if (val instanceof Int16Array)
435
- pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[2]);
436
- else if (val instanceof Int32Array)
437
- pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[4]);
438
- else if (val instanceof BigInt64Array)
439
- pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[8]);
440
- else if (val instanceof Float32Array)
441
- pos += pack.tlen(pos, 'spec', $mol_vary_spec.fp32);
442
- else if (val instanceof Float64Array)
443
- pos += pack.tlen(pos, 'spec', $mol_vary_spec.fp64);
444
- else
445
- $mol_fail(new Error(`Unsupported type`));
446
- buf.set(new Uint8Array(val.buffer, val.byteOffset, val.byteLength), pos);
447
- pos += val.byteLength;
448
- if (val.byteLength)
449
- embedded.add(key);
450
- return;
451
- }
452
- if (Array.isArray(val)) {
453
- const key = hash(val);
454
- if (embedded.has(key)) {
455
- pos += pack.tnat(pos, 'link', offsets.get(key));
456
- return;
457
- }
458
- pos += pack.tnat(pos, 'list', val.length);
459
- for (const item of val)
460
- dump(item);
461
- if (val.length)
462
- embedded.add(key);
463
- return;
388
+ release(9);
389
+ return pack.setUint8(pos++, $mol_vary_spec.none);
464
390
  }
465
- const key = hash(val);
466
- if (embedded.has(key)) {
467
- pos += pack.tnat(pos, 'link', offsets.get(key));
468
- return;
469
- }
470
- const [keys, vals] = lean(val);
471
- pos += pack.tnat(pos, 'tupl', vals.length);
472
- dump(keys);
473
- for (const item of vals)
474
- dump(item);
475
- if (vals.length)
476
- embedded.add(key);
477
- return;
391
+ if (ArrayBuffer.isView(val))
392
+ return dump_buffer(val);
393
+ if (Array.isArray(val))
394
+ return dump_list(val);
395
+ return dump_object(val);
478
396
  }
479
397
  }
480
398
  $mol_fail(new Error(`Unsupported type`));
481
399
  };
482
400
  dump(data);
483
- return buf.slice(0, size);
401
+ offsets = new Map;
402
+ sizes = new Map;
403
+ return buffer.slice(0, pos);
484
404
  }
485
405
  static take(buf) {
486
406
  const pack = new $mol_vary(buf.buffer, buf.byteOffset, buf.byteLength);
487
407
  const stream = [];
488
408
  let pos = 0;
489
- const read_unum = () => {
490
- const num = pack.unum(pos);
491
- pos += 1 + pack.ulen(pos);
492
- return num;
409
+ const read_unum = (kind) => {
410
+ ++pos;
411
+ const num = kind & 0b11111;
412
+ if (num < 28)
413
+ return num;
414
+ let res = 0;
415
+ if (num === 28) {
416
+ res = pack.getUint8(pos);
417
+ pos += 1;
418
+ }
419
+ else if (num === 29) {
420
+ res = pack.getUint16(pos, true);
421
+ pos += 2;
422
+ }
423
+ else if (num === 30) {
424
+ res = pack.getUint32(pos, true);
425
+ pos += 4;
426
+ }
427
+ else if (num === 31) {
428
+ res = pack.getBigUint64(pos, true);
429
+ if (res <= Number.MAX_SAFE_INTEGER)
430
+ res = Number(res);
431
+ pos += 8;
432
+ }
433
+ else {
434
+ $mol_fail(new Error('Unsupported unum', { cause: { num } }));
435
+ }
436
+ return res;
493
437
  };
494
- const read_snum = () => {
495
- const num = pack.snum(pos);
496
- pos += 1 + pack.slen(pos);
497
- return num;
438
+ const read_snum = (kind) => {
439
+ const num = pack.getInt8(pos++);
440
+ if (num >= -28)
441
+ return num;
442
+ let res = 0;
443
+ if (num === -29) {
444
+ res = pack.getInt8(pos);
445
+ pos += 1;
446
+ }
447
+ else if (num === -30) {
448
+ res = pack.getInt16(pos, true);
449
+ pos += 2;
450
+ }
451
+ else if (num === -31) {
452
+ res = pack.getInt32(pos, true);
453
+ pos += 4;
454
+ }
455
+ else if (num === -32) {
456
+ res = pack.getBigInt64(pos, true);
457
+ if (res >= Number.MIN_SAFE_INTEGER && res <= Number.MAX_SAFE_INTEGER)
458
+ res = Number(res);
459
+ pos += 8;
460
+ }
461
+ else {
462
+ $mol_fail(new Error('Unsupported snum', { cause: { num } }));
463
+ }
464
+ return res;
498
465
  };
499
- const read_text = () => {
500
- const len = read_unum();
501
- const bin = new Uint8Array(buf.buffer, pack.byteOffset + pos, len);
502
- pos += len;
503
- const text = $mol_charset_decode(bin);
466
+ const read_text = (kind) => {
467
+ const len = read_unum(kind);
468
+ const [text, bytes] = $mol_charset_decode_from(buf, pack.byteOffset + pos, len);
469
+ pos += bytes;
504
470
  if (text.length)
505
471
  stream.push(text);
506
472
  return text;
@@ -512,79 +478,90 @@ var $;
512
478
  stream.push(bin);
513
479
  return bin;
514
480
  };
515
- const read_blob = () => {
516
- const len = read_unum();
517
- const kind = pack.getUint8(pos++);
518
- switch (kind) {
519
- case $mol_vary_tip.uint | $.$mol_vary_len[1]: return read_buffer(len, Uint8Array);
520
- case $mol_vary_tip.uint | $.$mol_vary_len[2]: return read_buffer(len, Uint16Array);
521
- case $mol_vary_tip.uint | $.$mol_vary_len[4]: return read_buffer(len, Uint32Array);
522
- case $mol_vary_tip.uint | $.$mol_vary_len[8]: return read_buffer(len, BigUint64Array);
523
- case $mol_vary_tip.sint | ~$.$mol_vary_len[1] + 256: return read_buffer(len, Int8Array);
524
- case $mol_vary_tip.sint | ~$.$mol_vary_len[2] + 256: return read_buffer(len, Int16Array);
525
- case $mol_vary_tip.sint | ~$.$mol_vary_len[4] + 256: return read_buffer(len, Int32Array);
526
- case $mol_vary_tip.sint | ~$.$mol_vary_len[8] + 256: return read_buffer(len, BigInt64Array);
481
+ const read_blob = (kind) => {
482
+ const len = read_unum(kind);
483
+ const kind_item = pack.getUint8(pos++);
484
+ switch (kind_item) {
485
+ case $.$mol_vary_len[1]: return read_buffer(len, Uint8Array);
486
+ case $.$mol_vary_len[2]: return read_buffer(len, Uint16Array);
487
+ case $.$mol_vary_len[4]: return read_buffer(len, Uint32Array);
488
+ case $.$mol_vary_len[8]: return read_buffer(len, BigUint64Array);
489
+ case ~$.$mol_vary_len[1] + 256: return read_buffer(len, Int8Array);
490
+ case ~$.$mol_vary_len[2] + 256: return read_buffer(len, Int16Array);
491
+ case ~$.$mol_vary_len[4] + 256: return read_buffer(len, Int32Array);
492
+ case ~$.$mol_vary_len[8] + 256: return read_buffer(len, BigInt64Array);
493
+ case $mol_vary_tip.spec | $mol_vary_spec.fp16: return read_buffer(len, Float16Array);
527
494
  case $mol_vary_tip.spec | $mol_vary_spec.fp32: return read_buffer(len, Float32Array);
528
495
  case $mol_vary_tip.spec | $mol_vary_spec.fp64: return read_buffer(len, Float64Array);
529
496
  default:
530
- $mol_fail(new Error('Unsupported blob kind', { cause: { kind } }));
497
+ $mol_fail(new Error('Unsupported blob item kind', { cause: { kind_item } }));
531
498
  }
532
499
  };
533
- const read_list = () => {
534
- const len = read_unum();
535
- const list = [];
500
+ const read_list = (kind) => {
501
+ const len = read_unum(kind);
502
+ const list = new Array(len);
536
503
  for (let i = 0; i < len; ++i)
537
- list.push(read_vary());
504
+ list[i] = read_vary();
538
505
  if (len)
539
506
  stream.push(list);
540
507
  return list;
541
508
  };
542
- const read_link = () => {
543
- const index = read_unum();
509
+ const read_link = (kind) => {
510
+ const index = read_unum(kind);
544
511
  if (index >= stream.length)
545
512
  $mol_fail(new Error('Too large index', { cause: { index, exists: stream.length } }));
546
513
  return stream[index];
547
514
  };
548
- const read_tupl = () => {
549
- const len = read_unum();
550
- const keys = pack.tip(pos) === 'link' ? read_link() : read_list();
551
- if (!Array.isArray(keys))
552
- $mol_fail(new Error('Wrong tupl shape type', { cause: { type: typeof keys } }));
553
- const vals = Array.from({ length: len }, () => read_vary());
554
- const obj = this.rich(keys, vals);
515
+ const read_tupl = (kind) => {
516
+ const len = read_unum(kind);
517
+ const keys = new Array(len);
518
+ const vals = new Array(len);
519
+ for (let i = 0; i < len; ++i)
520
+ keys[i] = read_vary();
521
+ for (let i = 0; i < len; ++i)
522
+ vals[i] = read_vary();
523
+ const shape = JSON.stringify([keys, vals.map(v => typeof v)]);
524
+ let obj;
525
+ const rich = this.riches.get(shape);
526
+ if (rich) {
527
+ obj = rich(...vals);
528
+ }
529
+ else {
530
+ obj = {};
531
+ for (let i = 0; i < len; ++i)
532
+ obj[keys[i]] = vals[i];
533
+ }
555
534
  if (vals.length)
556
535
  stream.push(obj);
557
536
  return obj;
558
537
  };
559
- const read_spec = () => {
560
- const kind = pack.getUint8(pos);
561
- const spec = $mol_vary_spec[kind];
562
- switch (spec) {
563
- case 'none':
538
+ const read_spec = (kind) => {
539
+ switch (kind) {
540
+ case $mol_vary_spec.none:
564
541
  ++pos;
565
542
  return null;
566
- case 'fake':
543
+ case $mol_vary_spec.fake:
567
544
  ++pos;
568
545
  return false;
569
- case 'true':
546
+ case $mol_vary_spec.true:
570
547
  ++pos;
571
548
  return true;
572
- case 'both':
549
+ case $mol_vary_spec.both:
573
550
  ++pos;
574
551
  return undefined;
575
- case 'fp16': {
576
- const val = pack.getFloat16(pos + 1, true);
577
- pos += 3;
552
+ case $mol_vary_spec.fp64: {
553
+ const val = pack.getFloat64(++pos, true);
554
+ pos += 8;
578
555
  return val;
579
556
  }
580
- case 'fp32': {
581
- const val = pack.getFloat32(pos + 1, true);
582
- pos += 5;
557
+ case $mol_vary_spec.fp32: {
558
+ const val = pack.getFloat32(++pos, true);
559
+ pos += 4;
583
560
  return val;
584
561
  }
585
- case 'fp64': {
586
- const val = pack.getFloat64(pos + 1, true);
587
- pos += 9;
562
+ case $mol_vary_spec.fp16: {
563
+ const val = pack.getFloat16(++pos, true);
564
+ pos += 2;
588
565
  return val;
589
566
  }
590
567
  default:
@@ -592,181 +569,41 @@ var $;
592
569
  }
593
570
  };
594
571
  const read_vary = () => {
595
- const tip = pack.tip(pos);
572
+ const kind = pack.getUint8(pos);
573
+ const tip = kind & 0b111_00000;
596
574
  switch (tip) {
597
- case 'uint': return read_unum();
598
- case 'sint': return read_snum();
599
- case 'link': return read_link();
600
- case 'text': return read_text();
601
- case 'list': return read_list();
602
- case 'blob': return read_blob();
603
- case 'tupl': return read_tupl();
604
- case 'spec': return read_spec();
575
+ case $mol_vary_tip.uint: return read_unum(kind);
576
+ case $mol_vary_tip.sint: return read_snum(kind);
577
+ case $mol_vary_tip.link: return read_link(kind);
578
+ case $mol_vary_tip.text: return read_text(kind);
579
+ case $mol_vary_tip.list: return read_list(kind);
580
+ case $mol_vary_tip.blob: return read_blob(kind);
581
+ case $mol_vary_tip.tupl: return read_tupl(kind);
582
+ case $mol_vary_tip.spec: return read_spec(kind);
605
583
  default: $mol_fail(new Error('Unsupported tip', { cause: { tip } }));
606
584
  }
607
585
  };
608
586
  return read_vary();
609
587
  }
610
- tlen(pos, tip, len) {
611
- this.setUint8(pos, $mol_vary_tip[tip] | len);
612
- return 1;
613
- }
614
- tnat(pos, tip, num) {
615
- const len = num_len(num);
616
- this.tlen(pos, tip, len ? $.$mol_vary_len[len] : Number(num));
617
- switch (len) {
618
- case 0: break;
619
- case 1:
620
- this.setUint8(pos + 1, Number(num));
621
- break;
622
- case 2:
623
- this.setUint16(pos + 1, Number(num), true);
624
- break;
625
- case 4:
626
- this.setUint32(pos + 1, Number(num), true);
627
- break;
628
- case 8:
629
- this.setBigUint64(pos + 1, BigInt(num), true);
630
- break;
631
- default: $mol_fail(new Error('Unsupported uint len', { cause: { len } }));
632
- }
633
- return 1 + len;
634
- }
635
- tint(pos, tip, num) {
636
- const len = num_len(num);
637
- this.tlen(pos, tip, len ? -$.$mol_vary_len[len] - 1 : Number(num));
638
- switch (len) {
639
- case 0: break;
640
- case 1:
641
- this.setInt8(pos + 1, Number(num));
642
- break;
643
- case 2:
644
- this.setInt16(pos + 1, Number(num), true);
645
- break;
646
- case 4:
647
- this.setInt32(pos + 1, Number(num), true);
648
- break;
649
- case 8:
650
- this.setBigInt64(pos + 1, BigInt(num), true);
651
- break;
652
- default: $mol_fail(new Error('Unsupported sint len', { cause: { len } }));
653
- }
654
- return 1 + len;
655
- }
656
- tfp(pos, tip, num) {
657
- const len = num_len(num);
658
- this.tlen(pos, tip, len ? $.$mol_vary_len[len] : Number(num));
659
- switch (len) {
660
- case 2:
661
- this.setFloat16(pos + 1, num, true);
662
- break;
663
- case 4:
664
- this.setFloat32(pos + 1, num, true);
665
- break;
666
- case 8:
667
- this.setFloat64(pos + 1, num, true);
668
- break;
669
- default: $mol_fail(new Error('Unsupported fp len', { cause: { len } }));
670
- }
671
- return 1 + len;
672
- }
673
- tip(pos) {
674
- return $mol_vary_tip[this.getUint8(pos) & 0b111_00000];
675
- }
676
- ulen(pos) {
677
- const num = this.getUint8(pos) & 0b11111;
678
- if (num < 28)
679
- return 0;
680
- switch (num) {
681
- case 28: return 1;
682
- case 29: return 2;
683
- case 30: return 4;
684
- case 31: return 8;
685
- default: $mol_fail(new Error('Impossible!'));
686
- }
687
- }
688
- slen(pos) {
689
- const num = this.getInt8(pos);
690
- if (num > -29)
691
- return 0;
692
- switch (num) {
693
- case -29: return 1;
694
- case -30: return 2;
695
- case -31: return 4;
696
- case -32: return 8;
697
- default: $mol_fail(new Error('Impossible!'));
698
- }
699
- }
700
- unum(pos) {
701
- const num = this.getUint8(pos) & 0b11111;
702
- if (num < 28)
703
- return num;
704
- switch (num) {
705
- case 28: return this.getUint8(pos + 1);
706
- case 29: return this.getUint16(pos + 1, true);
707
- case 30: return this.getUint32(pos + 1, true);
708
- case 31:
709
- const val = this.getBigUint64(pos + 1, true);
710
- if (val > Number.MAX_SAFE_INTEGER)
711
- return val;
712
- return Number(val);
713
- default: $mol_fail(new Error('Unsupported len', { cause: { num } }));
714
- }
715
- }
716
- snum(pos) {
717
- const num = this.getInt8(pos);
718
- if (num > -29)
719
- return num;
720
- switch (num) {
721
- case -29: return this.getInt8(pos + 1);
722
- case -30: return this.getInt16(pos + 1, true);
723
- case -31: return this.getInt32(pos + 1, true);
724
- case -32:
725
- const val = this.getBigInt64(pos + 1, true);
726
- if (val > Number.MAX_SAFE_INTEGER)
727
- return val;
728
- if (val < Number.MIN_SAFE_INTEGER)
729
- return val;
730
- return Number(val);
731
- default: $mol_fail(new Error('Unsupported len', { cause: { num } }));
732
- }
733
- }
734
- static rich(keys, vals) {
735
- const shape = JSON.stringify([keys, vals.map(v => typeof v)]);
736
- const rich = this.riches.get(shape);
737
- if (rich)
738
- return rich(...vals);
739
- const pairs = keys.map((key, index) => [key, vals[index]]);
740
- const obj = Object.fromEntries(pairs);
741
- return obj;
742
- }
743
- static lean(val) {
744
- const proto = Reflect.getPrototypeOf(val);
745
- const lean = this.leanes.get(proto);
746
- if (lean)
747
- return lean(val);
748
- return [Object.keys(val), Object.values(val)];
749
- }
750
588
  static leanes = new Map();
589
+ static keys = new Map();
751
590
  static riches = new Map();
752
- static type(rich, lean) {
591
+ static type(keys, rich, lean) {
753
592
  const obj = rich();
754
593
  const proto = Reflect.getPrototypeOf(obj);
755
- const [keys, vals] = lean(obj);
594
+ const vals = lean(obj);
756
595
  const shape = JSON.stringify([keys, vals.map(v => typeof v)]);
757
596
  this.leanes.set(proto, lean);
597
+ this.keys.set(proto, keys);
758
598
  this.riches.set(shape, rich);
759
599
  }
760
600
  }
761
601
  $.$mol_vary = $mol_vary;
762
- $mol_vary.type((keys = [], vals = []) => new Map(keys.map((k, i) => [k, vals[i]])), obj => [
763
- ['keys', 'vals'],
764
- [[...obj.keys()], [...obj.values()]],
765
- ]);
766
- $mol_vary.type((vals = []) => new Set(vals), obj => [
767
- ['vals'],
768
- [[...obj.values()]],
769
- ]);
602
+ let buffer = new Uint8Array(128);
603
+ let pack = new DataView(buffer.buffer);
604
+ $mol_vary.type(['keys', 'vals'], (keys = [], vals = []) => new Map(keys.map((k, i) => [k, vals[i]])), obj => [[...obj.keys()], [...obj.values()]]);
605
+ $mol_vary.type(['vals'], (vals = []) => new Set(vals), obj => [[...obj.values()]]);
606
+ $mol_vary.type(['unix_time'], (ts = 0) => new Date(ts * 1000), obj => [obj.valueOf() / 1000]);
770
607
  })($ || ($ = {}));
771
608
 
772
609
  ;
@@ -3973,156 +3810,6 @@ var $;
3973
3810
  });
3974
3811
  })($ || ($ = {}));
3975
3812
 
3976
- ;
3977
- "use strict";
3978
- var $;
3979
- (function ($_1) {
3980
- var $$;
3981
- (function ($$) {
3982
- $mol_test({
3983
- "1 byte int"($) {
3984
- $mol_assert_equal($mol_bigint_encode(0n), new Uint8Array(new Int8Array([0]).buffer));
3985
- $mol_assert_equal($mol_bigint_encode(1n), new Uint8Array(new Int8Array([1]).buffer));
3986
- $mol_assert_equal($mol_bigint_encode(-1n), new Uint8Array(new Int8Array([-1]).buffer));
3987
- $mol_assert_equal($mol_bigint_encode(127n), new Uint8Array(new Int8Array([127]).buffer));
3988
- $mol_assert_equal($mol_bigint_encode(-128n), new Uint8Array(new Int8Array([-128]).buffer));
3989
- },
3990
- "2 byte int"($) {
3991
- $mol_assert_equal($mol_bigint_encode(128n), new Uint8Array(new Int16Array([128]).buffer));
3992
- $mol_assert_equal($mol_bigint_encode(-129n), new Uint8Array(new Int16Array([-129]).buffer));
3993
- $mol_assert_equal($mol_bigint_encode(128n * 256n - 1n), new Uint8Array(new Int16Array([128 * 256 - 1]).buffer));
3994
- $mol_assert_equal($mol_bigint_encode(-128n * 256n), new Uint8Array(new Int16Array([-128 * 256]).buffer));
3995
- },
3996
- "3 byte int"($) {
3997
- $mol_assert_equal($mol_bigint_encode(128n * 256n), new Uint8Array(new Int32Array([128 * 256]).buffer).slice(0, 3));
3998
- $mol_assert_equal($mol_bigint_encode(-128n * 256n - 1n), new Uint8Array(new Int32Array([-128 * 256 - 1]).buffer).slice(0, 3));
3999
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 2n - 1n), new Uint8Array(new Int32Array([128 * 256 ** 2 - 1]).buffer).slice(0, 3));
4000
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 2n), new Uint8Array(new Int32Array([-128 * 256 ** 2]).buffer).slice(0, 3));
4001
- },
4002
- "4 byte int"($) {
4003
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 2n), new Uint8Array(new Int32Array([128 * 256 ** 2]).buffer));
4004
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 2n - 1n), new Uint8Array(new Int32Array([-128 * 256 ** 2 - 1]).buffer));
4005
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 3n - 1n), new Uint8Array(new Int32Array([128 * 256 ** 3 - 1]).buffer));
4006
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 3n), new Uint8Array(new Int32Array([-128 * 256 ** 3]).buffer));
4007
- },
4008
- "8 byte int"($) {
4009
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 7n - 1n), new Uint8Array(new BigInt64Array([128n * 256n ** 7n - 1n]).buffer));
4010
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 7n), new Uint8Array(new BigInt64Array([-128n * 256n ** 7n]).buffer));
4011
- },
4012
- });
4013
- })($$ = $_1.$$ || ($_1.$$ = {}));
4014
- })($ || ($ = {}));
4015
-
4016
- ;
4017
- "use strict";
4018
- var $;
4019
- (function ($) {
4020
- $mol_test({
4021
- 'encode empty'() {
4022
- $mol_assert_equal($mol_charset_encode(''), new Uint8Array([]));
4023
- },
4024
- 'encode 1 octet'() {
4025
- $mol_assert_equal($mol_charset_encode('F'), new Uint8Array([0x46]));
4026
- },
4027
- 'encode 2 octet'() {
4028
- $mol_assert_equal($mol_charset_encode('Б'), new Uint8Array([0xd0, 0x91]));
4029
- },
4030
- 'encode 3 octet'() {
4031
- $mol_assert_equal($mol_charset_encode('ह'), new Uint8Array([0xe0, 0xa4, 0xb9]));
4032
- },
4033
- 'encode 4 octet'() {
4034
- $mol_assert_equal($mol_charset_encode('𐍈'), new Uint8Array([0xf0, 0x90, 0x8d, 0x88]));
4035
- },
4036
- 'encode surrogate pair'() {
4037
- $mol_assert_equal($mol_charset_encode('😀'), new Uint8Array([0xf0, 0x9f, 0x98, 0x80]));
4038
- },
4039
- });
4040
- })($ || ($ = {}));
4041
-
4042
- ;
4043
- "use strict";
4044
- var $;
4045
- (function ($) {
4046
- $mol_test({
4047
- 'decode utf8 string'() {
4048
- const str = 'Hello, ΧΨΩЫ';
4049
- const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
4050
- $mol_assert_equal($mol_charset_decode(encoded), str);
4051
- $mol_assert_equal($mol_charset_decode(encoded, 'utf8'), str);
4052
- },
4053
- 'decode empty string'() {
4054
- const encoded = new Uint8Array([]);
4055
- $mol_assert_equal($mol_charset_decode(encoded), '');
4056
- },
4057
- });
4058
- })($ || ($ = {}));
4059
-
4060
- ;
4061
- "use strict";
4062
- var $;
4063
- (function ($) {
4064
- function $mol_bigint_decode(buf) {
4065
- if (buf.length === 8)
4066
- return new BigInt64Array(buf.buffer, buf.byteOffset, 1)[0];
4067
- if (buf.length === 4)
4068
- return BigInt(new Int32Array(buf.buffer, buf.byteOffset, 1)[0]);
4069
- if (buf.length === 2)
4070
- return BigInt(new Int16Array(buf.buffer, buf.byteOffset, 1)[0]);
4071
- if (buf.length === 1)
4072
- return BigInt(new Int8Array(buf.buffer, buf.byteOffset, 1)[0]);
4073
- const minus = (buf.at(-1) & 128) ? 255 : 0;
4074
- let result = 0n;
4075
- let offset = 0n;
4076
- for (let i = 0; i < buf.length; i++, offset += 8n) {
4077
- result |= BigInt(buf[i] ^ minus) << offset;
4078
- }
4079
- if (minus)
4080
- result = (result + 1n) * -1n;
4081
- return result;
4082
- }
4083
- $.$mol_bigint_decode = $mol_bigint_decode;
4084
- })($ || ($ = {}));
4085
-
4086
- ;
4087
- "use strict";
4088
- var $;
4089
- (function ($_1) {
4090
- var $$;
4091
- (function ($$) {
4092
- $mol_test({
4093
- "1 byte int"($) {
4094
- $mol_assert_equal($mol_bigint_decode(new Uint8Array), 0n);
4095
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([1]).buffer)), 1n);
4096
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([-1]).buffer)), -1n);
4097
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([127]).buffer)), 127n);
4098
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([-128]).buffer)), -128n);
4099
- },
4100
- "2 byte int"($) {
4101
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([128]).buffer)), 128n);
4102
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([-129]).buffer)), -129n);
4103
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([128 * 256 - 1]).buffer)), 128n * 256n - 1n);
4104
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([-128 * 256]).buffer)), -128n * 256n);
4105
- },
4106
- "3 byte int"($) {
4107
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256]).buffer).slice(0, 3)), 128n * 256n);
4108
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 - 1]).buffer).slice(0, 3)), -128n * 256n - 1n);
4109
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256 ** 2 - 1]).buffer).slice(0, 3)), 128n * 256n ** 2n - 1n);
4110
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 ** 2]).buffer).slice(0, 3)), -128n * 256n ** 2n);
4111
- },
4112
- "4 byte int"($) {
4113
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256 ** 2]).buffer)), 128n * 256n ** 2n);
4114
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 ** 2 - 1]).buffer)), -128n * 256n ** 2n - 1n);
4115
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256 ** 3 - 1]).buffer)), 128n * 256n ** 3n - 1n);
4116
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 ** 3]).buffer)), -128n * 256n ** 3n);
4117
- },
4118
- "8 byte int"($) {
4119
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new BigInt64Array([128n * 256n ** 7n - 1n]).buffer)), 128n * 256n ** 7n - 1n);
4120
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new BigInt64Array([-128n * 256n ** 7n]).buffer)), -128n * 256n ** 7n);
4121
- },
4122
- });
4123
- })($$ = $_1.$$ || ($_1.$$ = {}));
4124
- })($ || ($ = {}));
4125
-
4126
3813
  ;
4127
3814
  "use strict";
4128
3815
  var $;
@@ -4908,21 +4595,71 @@ var $;
4908
4595
  ;
4909
4596
  "use strict";
4910
4597
  var $;
4911
- (function ($_1) {
4912
- var $$;
4913
- (function ($$) {
4914
- $mol_test({
4915
- "Zero int"($) {
4916
- $mol_assert_equal($mol_bigint_decode($mol_bigint_encode(0n)), 0n);
4917
- },
4918
- "Large positive int"($) {
4919
- $mol_assert_equal($mol_bigint_decode($mol_bigint_encode(12345678901234567890n)), 12345678901234567890n);
4920
- },
4921
- "Large negative int"($) {
4922
- $mol_assert_equal($mol_bigint_decode($mol_bigint_encode(-12345678901234567890n)), -12345678901234567890n);
4923
- },
4924
- });
4925
- })($$ = $_1.$$ || ($_1.$$ = {}));
4598
+ (function ($) {
4599
+ $mol_test({
4600
+ 'encode empty'() {
4601
+ $mol_assert_equal($mol_charset_encode(''), new Uint8Array([]));
4602
+ },
4603
+ 'encode 1 octet'() {
4604
+ $mol_assert_equal($mol_charset_encode('F'), new Uint8Array([0x46]));
4605
+ },
4606
+ 'encode 2 octet'() {
4607
+ $mol_assert_equal($mol_charset_encode('Б'), new Uint8Array([0xd0, 0x91]));
4608
+ },
4609
+ 'encode 3 octet'() {
4610
+ $mol_assert_equal($mol_charset_encode('ह'), new Uint8Array([0xe0, 0xa4, 0xb9]));
4611
+ },
4612
+ 'encode 4 octet'() {
4613
+ $mol_assert_equal($mol_charset_encode('𐍈'), new Uint8Array([0xf0, 0x90, 0x8d, 0x88]));
4614
+ },
4615
+ 'encode surrogate pair'() {
4616
+ $mol_assert_equal($mol_charset_encode('😀'), new Uint8Array([0xf0, 0x9f, 0x98, 0x80]));
4617
+ },
4618
+ });
4619
+ })($ || ($ = {}));
4620
+
4621
+ ;
4622
+ "use strict";
4623
+ var $;
4624
+ (function ($) {
4625
+ $mol_test({
4626
+ 'decode utf8 string'() {
4627
+ const str = 'Hello, ΧΨΩЫ';
4628
+ const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
4629
+ $mol_assert_equal($mol_charset_decode(encoded), str);
4630
+ $mol_assert_equal($mol_charset_decode(encoded, 'utf8'), str);
4631
+ },
4632
+ 'decode empty string'() {
4633
+ const encoded = new Uint8Array([]);
4634
+ $mol_assert_equal($mol_charset_decode(encoded), '');
4635
+ },
4636
+ });
4637
+ })($ || ($ = {}));
4638
+
4639
+ ;
4640
+ "use strict";
4641
+ var $;
4642
+ (function ($) {
4643
+ $mol_test({
4644
+ 'encode empty'() {
4645
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([]), 0, 0), ['', 0]);
4646
+ },
4647
+ 'encode 1 octet'() {
4648
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0x46]), 0, 1), ['F', 1]);
4649
+ },
4650
+ 'encode 2 octet'() {
4651
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xd0, 0x91]), 0, 1), ['Б', 2]);
4652
+ },
4653
+ 'encode 3 octet'() {
4654
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xe0, 0xa4, 0xb9]), 0, 1), ['ह', 3]);
4655
+ },
4656
+ 'encode 4 octet'() {
4657
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xf0, 0x90, 0x8d, 0x88]), 0, 1), ['𐍈', 4]);
4658
+ },
4659
+ 'encode surrogate pair'() {
4660
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xf0, 0x9f, 0x98, 0x80]), 0, 2), ['😀', 4]);
4661
+ },
4662
+ });
4926
4663
  })($ || ($ = {}));
4927
4664
 
4928
4665
  ;
@@ -4992,23 +4729,25 @@ var $;
4992
4729
  "vary pack float"($) {
4993
4730
  check(1.5, [fp64, ...new Uint8Array(new Float64Array([1.5]).buffer)]);
4994
4731
  },
4732
+ "vary pack list"($) {
4733
+ check([1, 2, 3], [list | 3, 1, 2, 3]);
4734
+ check([[], [1], [2, 3]], [list | 3, list | 0, list | 1, 1, list | 2, 2, 3]);
4735
+ },
4736
+ "vary pack dedup list"($) {
4737
+ const pair = [1, 2];
4738
+ check([pair, pair], [list | 2, list | 2, 1, 2, link | 0]);
4739
+ const seven = [7];
4740
+ const box = [seven];
4741
+ check([box, box, seven], [list | 3, list | 1, list | 1, 7, link | 1, link | 0]);
4742
+ },
4995
4743
  "vary pack text"($) {
4996
4744
  check('foo', [text | 3, ...str('foo')]);
4997
4745
  const long = 'abcdefghijklmnopqrstuvwxyzЖЫ';
4998
- check(long, [text | l1, 30, ...str(long)]);
4746
+ check(long, [text | l1, 28, ...str(long)]);
4999
4747
  },
5000
4748
  "vary pack dedup text"($) {
5001
4749
  check(["f", "f"], [list | 2, text | 1, ...str('f'), link | 0]);
5002
4750
  },
5003
- "vary pack list"($) {
5004
- check([1, 2, 3], [list | 3, 1, 2, 3]);
5005
- check([[], [1], [2, 3]], [list | 3, list | 0, list | 1, 1, list | 2, 2, 3]);
5006
- },
5007
- "vary pack dedup list"($) {
5008
- check([[1, 2], [1, 2]], [list | 2, list | 2, 1, 2, link | 0]);
5009
- check([[[1]], [[1]]], [list | 2, list | 1, list | 1, 1, link | 1]);
5010
- check([[[7]], [[7]], [7]], [list | 3, list | 1, list | 1, 7, link | 1, link | 0]);
5011
- },
5012
4751
  "vary pack blob"($) {
5013
4752
  check(new Uint8Array([1, 255]), [blob | 2, uint | l1, 1, 255]);
5014
4753
  check(new Int8Array([-128, 127]), [blob | 2, sint | ~l1, -128, 127]);
@@ -5020,23 +4759,26 @@ var $;
5020
4759
  check(new Float64Array([1.5]), [blob | 8, fp64, ...new Uint8Array(new Float64Array([1.5]).buffer)]);
5021
4760
  },
5022
4761
  "vary pack dedup blob"($) {
5023
- check([new Uint8Array([1, 2]), new Uint8Array([1, 2])], [list | 2, blob | 2, uint | l1, 1, 2, link | 0]);
4762
+ const part = new Uint8Array([1, 2]);
4763
+ check([part, part], [list | 2, blob | 2, uint | l1, 1, 2, link | 0]);
5024
4764
  },
5025
4765
  "vary pack struct"($) {
5026
- check({ a: 1, b: 2 }, [tupl | 2, list | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
5027
- check({ x: {}, y: { a: 1 } }, [tupl | 2, list | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 0, list | 0, tupl | 1, list | 1, text | 1, ...str('a'), 1]);
5028
- check([{}, { foo: 1 }, { foo: 2 }], [list | 3, tupl | 0, list | 0, tupl | 1, list | 1, text | 3, ...str('foo'), 1, tupl | 1, link | 1, 2]);
4766
+ check({ a: 1, b: 2 }, [tupl | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
4767
+ check({ x: {}, y: { a: 1 } }, [tupl | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 0, tupl | 1, text | 1, ...str('a'), 1]);
5029
4768
  },
5030
- "vary pack struct dedup"($) {
5031
- check({ x: 1, y: { x: 2, y: 3 } }, [tupl | 2, list | 2, text | 1, ...str('x'), text | 1, ...str('y'), 1, tupl | 2, link | 2, 2, 3]);
5032
- check([{ x: 1 }, { x: 1 }], [list | 2, tupl | 1, list | 1, text | 1, ...str('x'), 1, link | 2]);
5033
- check({ x: { x: 1, y: 2 }, y: { x: 1, y: 2 } }, [tupl | 2, list | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 2, link | 2, 1, 2, link | 3]);
4769
+ "vary pack struct full dedup"($) {
4770
+ const item = { x: 1 };
4771
+ check([item, item], [list | 2, tupl | 1, text | 1, ...str('x'), 1, link | 1]);
5034
4772
  },
5035
4773
  "vary pack Map"($) {
5036
- check(new Map([['foo', 1], [2, 'bar']]), [tupl | 2, list | 2, text | 4, ...str('keys'), text | 4, ...str('vals'), list | 2, text | 3, ...str('foo'), 2, list | 2, 1, text | 3, ...str('bar')]);
4774
+ check(new Map([['foo', 1], [2, 'bar']]), [tupl | 2, text | 4, ...str('keys'), text | 4, ...str('vals'), list | 2, text | 3, ...str('foo'), 2, list | 2, 1, text | 3, ...str('bar')]);
5037
4775
  },
5038
4776
  "vary pack Set"($) {
5039
- check(new Set([7, 'foo']), [tupl | 1, list | 1, text | 4, ...str('vals'), list | 2, 7, text | 3, ...str('foo')]);
4777
+ check(new Set([7, 'foo']), [tupl | 1, text | 4, ...str('vals'), list | 2, 7, text | 3, ...str('foo')]);
4778
+ },
4779
+ "vary pack Date"($) {
4780
+ const date = new Date('2025-01-02T03:04:05.678');
4781
+ check(date, [tupl | 1, text | 9, ...str('unix_time'), fp64, ...new Uint8Array(new Float64Array([date.valueOf() / 1000]).buffer)]);
5040
4782
  },
5041
4783
  "vary pack custom class"($) {
5042
4784
  class Foo {
@@ -5051,11 +4793,8 @@ var $;
5051
4793
  return [this.a, this.b].values();
5052
4794
  }
5053
4795
  }
5054
- $mol_vary.type((a = 0, b = 0) => new Foo(a, b), foo => [
5055
- ['a', 'b'],
5056
- [foo.a, foo.b],
5057
- ]);
5058
- check(new Foo(1, 2), [tupl | 2, list | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
4796
+ $mol_vary.type(['a', 'b'], (a = 0, b = 0) => new Foo(a, b), foo => [foo.a, foo.b]);
4797
+ check(new Foo(1, 2), [tupl | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
5059
4798
  },
5060
4799
  });
5061
4800
  })($$ = $_1.$$ || ($_1.$$ = {}));