mol_vary 0.0.31 → 0.0.33

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.mjs CHANGED
@@ -2345,7 +2345,7 @@ var $;
2345
2345
  class $mol_vary extends Object {
2346
2346
  static pack(data) {
2347
2347
  let pos = 0;
2348
- let capacity = 0;
2348
+ let capacity = 9;
2349
2349
  const offsets = new Map();
2350
2350
  const acquire = (size) => {
2351
2351
  if (size < 0)
@@ -2472,6 +2472,7 @@ var $;
2472
2472
  if (offset !== undefined)
2473
2473
  return dump_unum($mol_vary_tip.link, offset);
2474
2474
  dump_unum($mol_vary_tip.blob, val.byteLength);
2475
+ acquire(1 + val.byteLength);
2475
2476
  if (val instanceof Uint8Array)
2476
2477
  buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L1;
2477
2478
  else if (val instanceof Uint16Array)
@@ -2497,7 +2498,6 @@ var $;
2497
2498
  else
2498
2499
  $mol_fail(new Error(`Unsupported type`));
2499
2500
  const src = (val instanceof Uint8Array) ? val : new Uint8Array(val.buffer, val.byteOffset, val.byteLength);
2500
- acquire(val.byteLength);
2501
2501
  buffer.set(src, pos);
2502
2502
  pos += val.byteLength;
2503
2503
  offsets.set(val, offsets.size);
@@ -2512,16 +2512,24 @@ var $;
2512
2512
  dump(item);
2513
2513
  offsets.set(val, offsets.size);
2514
2514
  };
2515
+ const shapes = new Map();
2516
+ const shape = (val) => {
2517
+ const keys1 = Object.keys(val);
2518
+ const key = keys1.join('\0');
2519
+ const keys2 = shapes.get(key);
2520
+ if (keys2)
2521
+ return keys2;
2522
+ shapes.set(key, keys1);
2523
+ return keys1;
2524
+ };
2515
2525
  const dump_object = (val) => {
2516
2526
  const offset = offsets.get(val);
2517
2527
  if (offset !== undefined)
2518
2528
  return dump_unum($mol_vary_tip.link, offset);
2519
- const keys = val[$.$mol_vary_keys] ?? Object.keys(val);
2520
- const vals = val[$.$mol_vary_lean]?.(val) ?? Object.values(val);
2529
+ const [keys, vals] = val[$.$mol_vary_lean]?.(val) ?? [shape(val), Object.values(val)];
2521
2530
  dump_unum($mol_vary_tip.tupl, vals.length);
2522
- acquire(vals.length * 2 * 9);
2523
- for (const item of keys)
2524
- dump(item);
2531
+ acquire((vals.length + 1) * 9);
2532
+ dump_list(keys);
2525
2533
  for (const item of vals)
2526
2534
  dump(item);
2527
2535
  offsets.set(val, offsets.size);
@@ -2567,6 +2575,8 @@ var $;
2567
2575
  $mol_fail(new Error(`Unsupported type`));
2568
2576
  };
2569
2577
  dump(data);
2578
+ if (pos !== capacity)
2579
+ $mol_fail(new Error('Wrong reserved capacity', { cause: { capacity, size: pos, data } }));
2570
2580
  return buffer.slice(0, pos);
2571
2581
  }
2572
2582
  static take(buffer) {
@@ -2684,10 +2694,8 @@ var $;
2684
2694
  };
2685
2695
  const read_tupl = (kind) => {
2686
2696
  const len = read_unum(kind);
2687
- const keys = new Array(len);
2697
+ const keys = read_vary();
2688
2698
  const vals = new Array(len);
2689
- for (let i = 0; i < len; ++i)
2690
- keys[i] = read_vary();
2691
2699
  for (let i = 0; i < len; ++i)
2692
2700
  vals[i] = read_vary();
2693
2701
  const shape = JSON.stringify(keys);
@@ -2763,13 +2771,11 @@ var $;
2763
2771
  static riches = new Map();
2764
2772
  static type(Class, keys, lean, rich) {
2765
2773
  this.riches.set(JSON.stringify(keys), rich);
2766
- Class.prototype[$.$mol_vary_lean] = lean;
2767
- Class.prototype[$.$mol_vary_keys] = keys;
2774
+ Class.prototype[$.$mol_vary_lean] = (val) => [keys, lean(val)];
2768
2775
  }
2769
2776
  }
2770
2777
  $.$mol_vary = $mol_vary;
2771
2778
  $.$mol_vary_lean = Symbol.for('$mol_vary_lean');
2772
- $.$mol_vary_keys = Symbol.for('$mol_vary_keys');
2773
2779
  $mol_vary.type(Map, ['keys', 'vals'], obj => [[...obj.keys()], [...obj.values()]], (keys, vals) => new Map(keys.map((k, i) => [k, vals[i]])));
2774
2780
  $mol_vary.type(Set, ['set'], obj => [[...obj.values()]], vals => new Set(vals));
2775
2781
  $mol_vary.type(Date, ['unix_time'], obj => [obj.valueOf() / 1000], ts => new Date(ts * 1000));
package/node.test.js CHANGED
@@ -2336,7 +2336,7 @@ var $;
2336
2336
  class $mol_vary extends Object {
2337
2337
  static pack(data) {
2338
2338
  let pos = 0;
2339
- let capacity = 0;
2339
+ let capacity = 9;
2340
2340
  const offsets = new Map();
2341
2341
  const acquire = (size) => {
2342
2342
  if (size < 0)
@@ -2463,6 +2463,7 @@ var $;
2463
2463
  if (offset !== undefined)
2464
2464
  return dump_unum($mol_vary_tip.link, offset);
2465
2465
  dump_unum($mol_vary_tip.blob, val.byteLength);
2466
+ acquire(1 + val.byteLength);
2466
2467
  if (val instanceof Uint8Array)
2467
2468
  buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L1;
2468
2469
  else if (val instanceof Uint16Array)
@@ -2488,7 +2489,6 @@ var $;
2488
2489
  else
2489
2490
  $mol_fail(new Error(`Unsupported type`));
2490
2491
  const src = (val instanceof Uint8Array) ? val : new Uint8Array(val.buffer, val.byteOffset, val.byteLength);
2491
- acquire(val.byteLength);
2492
2492
  buffer.set(src, pos);
2493
2493
  pos += val.byteLength;
2494
2494
  offsets.set(val, offsets.size);
@@ -2503,16 +2503,24 @@ var $;
2503
2503
  dump(item);
2504
2504
  offsets.set(val, offsets.size);
2505
2505
  };
2506
+ const shapes = new Map();
2507
+ const shape = (val) => {
2508
+ const keys1 = Object.keys(val);
2509
+ const key = keys1.join('\0');
2510
+ const keys2 = shapes.get(key);
2511
+ if (keys2)
2512
+ return keys2;
2513
+ shapes.set(key, keys1);
2514
+ return keys1;
2515
+ };
2506
2516
  const dump_object = (val) => {
2507
2517
  const offset = offsets.get(val);
2508
2518
  if (offset !== undefined)
2509
2519
  return dump_unum($mol_vary_tip.link, offset);
2510
- const keys = val[$.$mol_vary_keys] ?? Object.keys(val);
2511
- const vals = val[$.$mol_vary_lean]?.(val) ?? Object.values(val);
2520
+ const [keys, vals] = val[$.$mol_vary_lean]?.(val) ?? [shape(val), Object.values(val)];
2512
2521
  dump_unum($mol_vary_tip.tupl, vals.length);
2513
- acquire(vals.length * 2 * 9);
2514
- for (const item of keys)
2515
- dump(item);
2522
+ acquire((vals.length + 1) * 9);
2523
+ dump_list(keys);
2516
2524
  for (const item of vals)
2517
2525
  dump(item);
2518
2526
  offsets.set(val, offsets.size);
@@ -2558,6 +2566,8 @@ var $;
2558
2566
  $mol_fail(new Error(`Unsupported type`));
2559
2567
  };
2560
2568
  dump(data);
2569
+ if (pos !== capacity)
2570
+ $mol_fail(new Error('Wrong reserved capacity', { cause: { capacity, size: pos, data } }));
2561
2571
  return buffer.slice(0, pos);
2562
2572
  }
2563
2573
  static take(buffer) {
@@ -2675,10 +2685,8 @@ var $;
2675
2685
  };
2676
2686
  const read_tupl = (kind) => {
2677
2687
  const len = read_unum(kind);
2678
- const keys = new Array(len);
2688
+ const keys = read_vary();
2679
2689
  const vals = new Array(len);
2680
- for (let i = 0; i < len; ++i)
2681
- keys[i] = read_vary();
2682
2690
  for (let i = 0; i < len; ++i)
2683
2691
  vals[i] = read_vary();
2684
2692
  const shape = JSON.stringify(keys);
@@ -2754,13 +2762,11 @@ var $;
2754
2762
  static riches = new Map();
2755
2763
  static type(Class, keys, lean, rich) {
2756
2764
  this.riches.set(JSON.stringify(keys), rich);
2757
- Class.prototype[$.$mol_vary_lean] = lean;
2758
- Class.prototype[$.$mol_vary_keys] = keys;
2765
+ Class.prototype[$.$mol_vary_lean] = (val) => [keys, lean(val)];
2759
2766
  }
2760
2767
  }
2761
2768
  $.$mol_vary = $mol_vary;
2762
2769
  $.$mol_vary_lean = Symbol.for('$mol_vary_lean');
2763
- $.$mol_vary_keys = Symbol.for('$mol_vary_keys');
2764
2770
  $mol_vary.type(Map, ['keys', 'vals'], obj => [[...obj.keys()], [...obj.values()]], (keys, vals) => new Map(keys.map((k, i) => [k, vals[i]])));
2765
2771
  $mol_vary.type(Set, ['set'], obj => [[...obj.values()]], vals => new Set(vals));
2766
2772
  $mol_vary.type(Date, ['unix_time'], obj => [obj.valueOf() / 1000], ts => new Date(ts * 1000));
@@ -4961,50 +4967,55 @@ var $;
4961
4967
  check([part, part], [list | 2, blob | 2, uint | L1, 1, 2, link | 0]);
4962
4968
  },
4963
4969
  "vary pack struct"($) {
4964
- check({ a: 1, b: 2 }, [tupl | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
4965
- check({ x: {}, y: { a: 1 } }, [tupl | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 0, tupl | 1, text | 1, ...str('a'), 1]);
4970
+ check({ a: 1, b: 2 }, [tupl | 2, list | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
4971
+ 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]);
4972
+ },
4973
+ "vary pack struct shape dedup"($) {
4974
+ check([{}, { foo: 1 }, { foo: 2 }], [list | 3, tupl | 0, list | 0, tupl | 1, list | 1, text | 3, ...str('foo'), 1, tupl | 1, link | 3, 2]);
4975
+ 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]);
4966
4976
  },
4967
4977
  "vary pack struct full dedup"($) {
4968
4978
  const item = { x: 1 };
4969
- check([item, item], [list | 2, tupl | 1, text | 1, ...str('x'), 1, link | 1]);
4979
+ check([item, item], [list | 2, tupl | 1, list | 1, text | 1, ...str('x'), 1, link | 2]);
4980
+ const part = { x: 1, y: 2 };
4981
+ check({ x: part, y: part }, [tupl | 2, list | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 2, link | 2, 1, 2, link | 3]);
4970
4982
  },
4971
4983
  "vary pack Map"($) {
4972
- 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')]);
4984
+ 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')]);
4973
4985
  },
4974
4986
  "vary pack Set"($) {
4975
- check(new Set([7, 'foo']), [tupl | 1, text | 3, ...str('set'), list | 2, 7, text | 3, ...str('foo')]);
4987
+ check(new Set([7, 'foo']), [tupl | 1, list | 1, text | 3, ...str('set'), list | 2, 7, text | 3, ...str('foo')]);
4976
4988
  },
4977
4989
  "vary pack Date"($) {
4978
4990
  const date1 = new Date('2025-01-02T03:04:05');
4979
- check(date1, [tupl | 1, text | 9, ...str('unix_time'), uint | L4, ...new Uint8Array(new Uint32Array([date1.valueOf() / 1000]).buffer)]);
4991
+ check(date1, [tupl | 1, list | 1, text | 9, ...str('unix_time'), uint | L4, ...new Uint8Array(new Uint32Array([date1.valueOf() / 1000]).buffer)]);
4980
4992
  const date2 = new Date('2025-01-02T03:04:05.678');
4981
- check(date2, [tupl | 1, text | 9, ...str('unix_time'), fp64, ...new Uint8Array(new Float64Array([date2.valueOf() / 1000]).buffer)]);
4993
+ check(date2, [tupl | 1, list | 1, text | 9, ...str('unix_time'), fp64, ...new Uint8Array(new Float64Array([date2.valueOf() / 1000]).buffer)]);
4982
4994
  },
4983
4995
  "vary pack Node"($) {
4984
4996
  check($mol_jsx("span", null), [
4985
- tupl | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
4997
+ tupl | 4, list | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
4986
4998
  text | 4, ...str('SPAN'), list | 0, list | 0, list | 0
4987
4999
  ]);
4988
5000
  check($mol_jsx("svg", null), [
4989
- tupl | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
5001
+ tupl | 4, list | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
4990
5002
  text | 3, ...str('SVG'), list | 0, list | 0, list | 0
4991
5003
  ]);
4992
5004
  check($mol_jsx("span", { tabIndex: "0" }), [
4993
- tupl | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
5005
+ tupl | 4, list | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
4994
5006
  text | 4, ...str('SPAN'), list | 1, text | 8, ...str('tabindex'), list | 1, text | 1, ...str('0'), list | 0
4995
5007
  ]);
4996
5008
  check($mol_jsx("span", null, "text"), [
4997
- tupl | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
5009
+ tupl | 4, list | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
4998
5010
  text | 4, ...str('SPAN'), list | 0, list | 0, list | 1,
4999
5011
  text | 4, ...str('text')
5000
5012
  ]);
5001
5013
  check($mol_jsx("div", null,
5002
5014
  $mol_jsx("span", null),
5003
5015
  " "), [
5004
- tupl | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
5016
+ tupl | 4, list | 4, text | 4, ...str('elem'), text | 4, ...str('keys'), text | 4, ...str('vals'), text | 4, ...str('kids'),
5005
5017
  text | 3, ...str('DIV'), list | 0, list | 0, list | 2,
5006
- tupl | 4, link | 0, link | 1, link | 2, link | 3,
5007
- text | 4, ...str('SPAN'), list | 0, list | 0, list | 0,
5018
+ tupl | 4, link | 4, text | 4, ...str('SPAN'), list | 0, list | 0, list | 0,
5008
5019
  text | 1, ...str(' '),
5009
5020
  ]);
5010
5021
  },
@@ -5022,7 +5033,7 @@ var $;
5022
5033
  }
5023
5034
  }
5024
5035
  $mol_vary.type(Foo, ['a', 'b'], foo => [foo.a, foo.b], (a = 0, b = 0) => new Foo(a, b));
5025
- check(new Foo(1, 2), [tupl | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
5036
+ check(new Foo(1, 2), [tupl | 2, list | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
5026
5037
  },
5027
5038
  });
5028
5039
  })($$ = $_1.$$ || ($_1.$$ = {}));