mol_vary 0.0.1
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/.nojekyll +0 -0
- package/README.md +57 -0
- package/node.audit.js +2 -0
- package/node.d.ts +94 -0
- package/node.d.ts.map +1 -0
- package/node.deps.json +1 -0
- package/node.js +770 -0
- package/node.js.map +1 -0
- package/node.meta.tree +21 -0
- package/node.mjs +771 -0
- package/node.test.js +5053 -0
- package/node.test.js.map +1 -0
- package/package.json +32 -0
- package/test.html +11 -0
- package/web.audit.js +2 -0
- package/web.css +1 -0
- package/web.css.map +1 -0
- package/web.d.ts +94 -0
- package/web.d.ts.map +1 -0
- package/web.deps.json +1 -0
- package/web.js +770 -0
- package/web.js.map +1 -0
- package/web.meta.tree +21 -0
- package/web.mjs +771 -0
- package/web.test.js +1754 -0
- package/web.test.js.map +1 -0
package/node.mjs
ADDED
|
@@ -0,0 +1,771 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var exports = void 0;
|
|
4
|
+
|
|
5
|
+
var $node = $node || {}
|
|
6
|
+
void function( module ) { var exports = module.exports = this; function require( id ) { return $node[ id.replace( /^.\// , "../" ) ] };
|
|
7
|
+
;
|
|
8
|
+
"use strict";
|
|
9
|
+
Error.stackTraceLimit = 50;
|
|
10
|
+
var $;
|
|
11
|
+
(function ($) {
|
|
12
|
+
})($ || ($ = {}));
|
|
13
|
+
module.exports = $;
|
|
14
|
+
|
|
15
|
+
;
|
|
16
|
+
|
|
17
|
+
$node[ "../mam.ts" ] = $node[ "../mam.ts" ] = module.exports }.call( {} , {} )
|
|
18
|
+
;
|
|
19
|
+
"use strict"
|
|
20
|
+
|
|
21
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
22
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
23
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
24
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if ((d = decorators[i])) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
25
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
var $ = ( typeof module === 'object' ) ? ( module['export'+'s'] = globalThis ) : globalThis
|
|
29
|
+
$.$$ = $
|
|
30
|
+
|
|
31
|
+
;
|
|
32
|
+
"use strict";
|
|
33
|
+
var $;
|
|
34
|
+
(function ($) {
|
|
35
|
+
function $mol_fail(error) {
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
$.$mol_fail = $mol_fail;
|
|
39
|
+
})($ || ($ = {}));
|
|
40
|
+
|
|
41
|
+
;
|
|
42
|
+
"use strict";
|
|
43
|
+
var $;
|
|
44
|
+
(function ($) {
|
|
45
|
+
function $mol_hash_numbers(buff, seed = 0) {
|
|
46
|
+
let h1 = 0xdeadbeef ^ seed;
|
|
47
|
+
let h2 = 0x41c6ce57 ^ seed;
|
|
48
|
+
for (let i = 0; i < buff.length; ++i) {
|
|
49
|
+
const item = buff[i];
|
|
50
|
+
h1 = Math.imul(h1 ^ item, 2654435761);
|
|
51
|
+
h2 = Math.imul(h2 ^ item, 1597334677);
|
|
52
|
+
}
|
|
53
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
54
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
55
|
+
return 4294967296 * (((1 << 16) - 1) & h2) + (h1 >>> 0);
|
|
56
|
+
}
|
|
57
|
+
$.$mol_hash_numbers = $mol_hash_numbers;
|
|
58
|
+
})($ || ($ = {}));
|
|
59
|
+
|
|
60
|
+
;
|
|
61
|
+
"use strict";
|
|
62
|
+
var $;
|
|
63
|
+
(function ($) {
|
|
64
|
+
function $mol_bigint_encode(num) {
|
|
65
|
+
const minus = num < 0n ? 255 : 0;
|
|
66
|
+
num = minus ? -num - 1n : num;
|
|
67
|
+
const bytes = [];
|
|
68
|
+
do {
|
|
69
|
+
let byte = minus ^ Number(num % 256n);
|
|
70
|
+
bytes.push(byte);
|
|
71
|
+
if (num >>= 8n)
|
|
72
|
+
continue;
|
|
73
|
+
if ((minus & 128) !== (byte & 128))
|
|
74
|
+
bytes.push(minus);
|
|
75
|
+
break;
|
|
76
|
+
} while (num);
|
|
77
|
+
return new Uint8Array(bytes);
|
|
78
|
+
}
|
|
79
|
+
$.$mol_bigint_encode = $mol_bigint_encode;
|
|
80
|
+
})($ || ($ = {}));
|
|
81
|
+
|
|
82
|
+
;
|
|
83
|
+
"use strict";
|
|
84
|
+
var $;
|
|
85
|
+
(function ($) {
|
|
86
|
+
function $mol_hash_string(str, seed = 0) {
|
|
87
|
+
return $mol_hash_numbers([...str].map(ch => ch.codePointAt(0)));
|
|
88
|
+
}
|
|
89
|
+
$.$mol_hash_string = $mol_hash_string;
|
|
90
|
+
})($ || ($ = {}));
|
|
91
|
+
|
|
92
|
+
;
|
|
93
|
+
"use strict";
|
|
94
|
+
var $;
|
|
95
|
+
(function ($) {
|
|
96
|
+
let buf = new Uint8Array(2 ** 12);
|
|
97
|
+
function $mol_charset_encode(str) {
|
|
98
|
+
const capacity = str.length * 3;
|
|
99
|
+
if (buf.byteLength < capacity)
|
|
100
|
+
buf = new Uint8Array(capacity);
|
|
101
|
+
return buf.slice(0, $mol_charset_encode_to(str, buf));
|
|
102
|
+
}
|
|
103
|
+
$.$mol_charset_encode = $mol_charset_encode;
|
|
104
|
+
function $mol_charset_encode_to(str, buf, from = 0) {
|
|
105
|
+
let pos = from;
|
|
106
|
+
for (let i = 0; i < str.length; i++) {
|
|
107
|
+
let code = str.charCodeAt(i);
|
|
108
|
+
if (code < 0x80) {
|
|
109
|
+
buf[pos++] = code;
|
|
110
|
+
}
|
|
111
|
+
else if (code < 0x800) {
|
|
112
|
+
buf[pos++] = 0xc0 | (code >> 6);
|
|
113
|
+
buf[pos++] = 0x80 | (code & 0x3f);
|
|
114
|
+
}
|
|
115
|
+
else if (code < 0xd800 || code >= 0xe000) {
|
|
116
|
+
buf[pos++] = 0xe0 | (code >> 12);
|
|
117
|
+
buf[pos++] = 0x80 | ((code >> 6) & 0x3f);
|
|
118
|
+
buf[pos++] = 0x80 | (code & 0x3f);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const point = ((code - 0xd800) << 10) + str.charCodeAt(++i) + 0x2400;
|
|
122
|
+
buf[pos++] = 0xf0 | (point >> 18);
|
|
123
|
+
buf[pos++] = 0x80 | ((point >> 12) & 0x3f);
|
|
124
|
+
buf[pos++] = 0x80 | ((point >> 6) & 0x3f);
|
|
125
|
+
buf[pos++] = 0x80 | (point & 0x3f);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return pos - from;
|
|
129
|
+
}
|
|
130
|
+
$.$mol_charset_encode_to = $mol_charset_encode_to;
|
|
131
|
+
function $mol_charset_encode_size(str) {
|
|
132
|
+
let size = 0;
|
|
133
|
+
for (let i = 0; i < str.length; i++) {
|
|
134
|
+
let code = str.charCodeAt(i);
|
|
135
|
+
if (code < 0x80)
|
|
136
|
+
size += 1;
|
|
137
|
+
else if (code < 0x800)
|
|
138
|
+
size += 2;
|
|
139
|
+
else if (code < 0xd800 || code >= 0xe000)
|
|
140
|
+
size += 3;
|
|
141
|
+
else
|
|
142
|
+
size += 4;
|
|
143
|
+
}
|
|
144
|
+
return size;
|
|
145
|
+
}
|
|
146
|
+
$.$mol_charset_encode_size = $mol_charset_encode_size;
|
|
147
|
+
})($ || ($ = {}));
|
|
148
|
+
|
|
149
|
+
;
|
|
150
|
+
"use strict";
|
|
151
|
+
|
|
152
|
+
;
|
|
153
|
+
"use strict";
|
|
154
|
+
var $;
|
|
155
|
+
(function ($) {
|
|
156
|
+
const decoders = {};
|
|
157
|
+
function $mol_charset_decode(buffer, encoding = 'utf8') {
|
|
158
|
+
let decoder = decoders[encoding];
|
|
159
|
+
if (!decoder)
|
|
160
|
+
decoder = decoders[encoding] = new TextDecoder(encoding);
|
|
161
|
+
return decoder.decode(buffer);
|
|
162
|
+
}
|
|
163
|
+
$.$mol_charset_decode = $mol_charset_decode;
|
|
164
|
+
})($ || ($ = {}));
|
|
165
|
+
|
|
166
|
+
;
|
|
167
|
+
"use strict";
|
|
168
|
+
var $;
|
|
169
|
+
(function ($) {
|
|
170
|
+
let $mol_vary_tip;
|
|
171
|
+
(function ($mol_vary_tip) {
|
|
172
|
+
$mol_vary_tip[$mol_vary_tip["uint"] = 0] = "uint";
|
|
173
|
+
$mol_vary_tip[$mol_vary_tip["link"] = 32] = "link";
|
|
174
|
+
$mol_vary_tip[$mol_vary_tip["spec"] = 64] = "spec";
|
|
175
|
+
$mol_vary_tip[$mol_vary_tip["list"] = 96] = "list";
|
|
176
|
+
$mol_vary_tip[$mol_vary_tip["text"] = 128] = "text";
|
|
177
|
+
$mol_vary_tip[$mol_vary_tip["blob"] = 160] = "blob";
|
|
178
|
+
$mol_vary_tip[$mol_vary_tip["tupl"] = 192] = "tupl";
|
|
179
|
+
$mol_vary_tip[$mol_vary_tip["sint"] = 224] = "sint";
|
|
180
|
+
})($mol_vary_tip = $.$mol_vary_tip || ($.$mol_vary_tip = {}));
|
|
181
|
+
$.$mol_vary_len = {
|
|
182
|
+
1: 28,
|
|
183
|
+
2: 29,
|
|
184
|
+
4: 30,
|
|
185
|
+
8: 31,
|
|
186
|
+
};
|
|
187
|
+
let $mol_vary_spec;
|
|
188
|
+
(function ($mol_vary_spec) {
|
|
189
|
+
$mol_vary_spec[$mol_vary_spec["none"] = 'N'.charCodeAt(0)] = "none";
|
|
190
|
+
$mol_vary_spec[$mol_vary_spec["true"] = 'T'.charCodeAt(0)] = "true";
|
|
191
|
+
$mol_vary_spec[$mol_vary_spec["fake"] = 'F'.charCodeAt(0)] = "fake";
|
|
192
|
+
$mol_vary_spec[$mol_vary_spec["both"] = 'B'.charCodeAt(0)] = "both";
|
|
193
|
+
$mol_vary_spec[$mol_vary_spec["fp16"] = 93] = "fp16";
|
|
194
|
+
$mol_vary_spec[$mol_vary_spec["fp32"] = 94] = "fp32";
|
|
195
|
+
$mol_vary_spec[$mol_vary_spec["fp64"] = 95] = "fp64";
|
|
196
|
+
})($mol_vary_spec = $.$mol_vary_spec || ($.$mol_vary_spec = {}));
|
|
197
|
+
function num_len(num) {
|
|
198
|
+
if (typeof num === 'number' && !Number.isInteger(num))
|
|
199
|
+
return 8;
|
|
200
|
+
if (num < 0) {
|
|
201
|
+
if (num >= -28)
|
|
202
|
+
return 0;
|
|
203
|
+
if (num >= -(2 ** 7))
|
|
204
|
+
return 1;
|
|
205
|
+
if (num >= -(2 ** 15))
|
|
206
|
+
return 2;
|
|
207
|
+
if (num >= -(2 ** 31))
|
|
208
|
+
return 4;
|
|
209
|
+
if (num >= -(2n ** 63n))
|
|
210
|
+
return 8;
|
|
211
|
+
$mol_fail(new Error(`Too low numb ${num}`));
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
if (num < 28)
|
|
215
|
+
return 0;
|
|
216
|
+
if (num < 2 ** 8)
|
|
217
|
+
return 1;
|
|
218
|
+
if (num < 2 ** 16)
|
|
219
|
+
return 2;
|
|
220
|
+
if (num < 2 ** 32)
|
|
221
|
+
return 4;
|
|
222
|
+
if (num < 2n ** 64n)
|
|
223
|
+
return 8;
|
|
224
|
+
$mol_fail(new Error(`Too high numb ${num}`));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
class $mol_vary extends DataView {
|
|
228
|
+
static pack(data) {
|
|
229
|
+
const leaned = new WeakMap();
|
|
230
|
+
const lean = (val) => {
|
|
231
|
+
let tupl = leaned.get(val);
|
|
232
|
+
if (tupl)
|
|
233
|
+
return tupl;
|
|
234
|
+
leaned.set(val, tupl = this.lean(val));
|
|
235
|
+
return tupl;
|
|
236
|
+
};
|
|
237
|
+
const hashes = new Map();
|
|
238
|
+
const hash = (val) => {
|
|
239
|
+
switch (typeof val) {
|
|
240
|
+
case 'undefined':
|
|
241
|
+
return 0;
|
|
242
|
+
case 'number':
|
|
243
|
+
case 'boolean':
|
|
244
|
+
return Number(val);
|
|
245
|
+
case 'number':
|
|
246
|
+
return val;
|
|
247
|
+
case 'bigint':
|
|
248
|
+
return $mol_hash_numbers($mol_bigint_encode(val), $mol_vary_tip.sint);
|
|
249
|
+
case 'string': {
|
|
250
|
+
let res = hashes.get(val);
|
|
251
|
+
if (res != null)
|
|
252
|
+
return res;
|
|
253
|
+
res = $mol_hash_string(val, $mol_vary_tip.text);
|
|
254
|
+
hashes.set(val, res);
|
|
255
|
+
return res;
|
|
256
|
+
}
|
|
257
|
+
case 'object': {
|
|
258
|
+
if (!val)
|
|
259
|
+
return $mol_vary_spec.none;
|
|
260
|
+
let res = hashes.get(val);
|
|
261
|
+
if (res != null)
|
|
262
|
+
return res;
|
|
263
|
+
if (ArrayBuffer.isView(val))
|
|
264
|
+
res = $mol_hash_numbers(new Uint8Array(val.buffer, val.byteOffset, val.byteLength), $mol_vary_tip.blob);
|
|
265
|
+
else if (Array.isArray(val))
|
|
266
|
+
res = $mol_hash_numbers(val.map(hash), $mol_vary_tip.list);
|
|
267
|
+
else {
|
|
268
|
+
const [keys, vals] = lean(val);
|
|
269
|
+
res = $mol_hash_numbers([hash(keys), ...vals.map(hash)], $mol_vary_tip.tupl);
|
|
270
|
+
}
|
|
271
|
+
hashes.set(val, res);
|
|
272
|
+
return res;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
$mol_fail(new Error(`Unsupported type`));
|
|
276
|
+
};
|
|
277
|
+
let size = 0;
|
|
278
|
+
const offsets = new Map();
|
|
279
|
+
const sizes = new Map();
|
|
280
|
+
const stream = [];
|
|
281
|
+
const dedup = (val, len) => {
|
|
282
|
+
const key = hash(val);
|
|
283
|
+
let offset = offsets.get(key);
|
|
284
|
+
if (offset === undefined) {
|
|
285
|
+
offsets.set(key, offset = stream.length);
|
|
286
|
+
stream.push(val);
|
|
287
|
+
size += len;
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
size += 1 + num_len(offset);
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
const calc = (val) => {
|
|
296
|
+
if (val == null) {
|
|
297
|
+
size += 1;
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
switch (typeof val) {
|
|
301
|
+
case 'boolean':
|
|
302
|
+
size += 1;
|
|
303
|
+
return true;
|
|
304
|
+
case 'number':
|
|
305
|
+
case 'bigint':
|
|
306
|
+
size += 1 + num_len(val);
|
|
307
|
+
return true;
|
|
308
|
+
case 'string': {
|
|
309
|
+
let len = sizes.get(val);
|
|
310
|
+
if (len == null)
|
|
311
|
+
sizes.set(val, len = $mol_charset_encode_size(val));
|
|
312
|
+
if (!len) {
|
|
313
|
+
size += 1;
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
return dedup(val, 1 + num_len(len) + len);
|
|
317
|
+
}
|
|
318
|
+
case 'object': {
|
|
319
|
+
if (ArrayBuffer.isView(val)) {
|
|
320
|
+
if (!val.byteLength) {
|
|
321
|
+
size += 2;
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
return dedup(val, 2 + num_len(val.byteLength) + val.byteLength);
|
|
325
|
+
}
|
|
326
|
+
if (Array.isArray(val)) {
|
|
327
|
+
if (!val.length) {
|
|
328
|
+
size += 1;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
const key = hash(val);
|
|
332
|
+
let offset = offsets.get(key);
|
|
333
|
+
if (offset === undefined) {
|
|
334
|
+
for (const item of val)
|
|
335
|
+
calc(item);
|
|
336
|
+
}
|
|
337
|
+
return dedup(val, 1 + num_len(val.length));
|
|
338
|
+
}
|
|
339
|
+
const [keys, vals] = lean(val);
|
|
340
|
+
if (!vals.length) {
|
|
341
|
+
size += 2;
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
const key = hash(val);
|
|
345
|
+
let offset = offsets.get(key);
|
|
346
|
+
if (offset === undefined) {
|
|
347
|
+
calc(keys);
|
|
348
|
+
for (const item of vals)
|
|
349
|
+
calc(item);
|
|
350
|
+
}
|
|
351
|
+
return dedup(val, 1 + num_len(vals.length));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
$mol_fail(new Error(`Unsupported type`));
|
|
355
|
+
};
|
|
356
|
+
calc(data);
|
|
357
|
+
const buf = new Uint8Array(size);
|
|
358
|
+
const pack = new this(buf.buffer);
|
|
359
|
+
const embedded = new Set();
|
|
360
|
+
let pos = 0;
|
|
361
|
+
const dump = (val) => {
|
|
362
|
+
if (pos >= size)
|
|
363
|
+
$mol_fail(new Error('Wrong buffer length', { cause: buf }));
|
|
364
|
+
switch (typeof val) {
|
|
365
|
+
case 'undefined': {
|
|
366
|
+
pack.tlen(pos, 'spec', $mol_vary_spec.both);
|
|
367
|
+
pos += 1;
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
case 'boolean': {
|
|
371
|
+
pack.tlen(pos, 'spec', val ? $mol_vary_spec.true : $mol_vary_spec.fake);
|
|
372
|
+
pos += 1;
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
case 'number': {
|
|
376
|
+
if (!Number.isInteger(val)) {
|
|
377
|
+
pos += pack.tfp(pos, 'spec', val);
|
|
378
|
+
}
|
|
379
|
+
else if (val < 0) {
|
|
380
|
+
pos += pack.tint(pos, 'sint', val);
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
pos += pack.tnat(pos, 'uint', val);
|
|
384
|
+
}
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
case 'bigint': {
|
|
388
|
+
if (val < 0) {
|
|
389
|
+
pos += pack.tint(pos, 'sint', val);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
pos += pack.tnat(pos, 'uint', val);
|
|
393
|
+
}
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
case 'string': {
|
|
397
|
+
const key = hash(val);
|
|
398
|
+
if (embedded.has(key)) {
|
|
399
|
+
pos += pack.tnat(pos, 'link', offsets.get(key));
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
pos += pack.tnat(pos, 'text', sizes.get(val));
|
|
403
|
+
pos += $mol_charset_encode_to(val, buf, pos);
|
|
404
|
+
if (val.length)
|
|
405
|
+
embedded.add(key);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
case 'object': {
|
|
409
|
+
if (!val) {
|
|
410
|
+
pack.tlen(pos, 'spec', $mol_vary_spec.none);
|
|
411
|
+
pos += 1;
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (ArrayBuffer.isView(val)) {
|
|
415
|
+
const key = hash(val);
|
|
416
|
+
if (embedded.has(key)) {
|
|
417
|
+
pos += pack.tnat(pos, 'link', offsets.get(key));
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
pos += pack.tnat(pos, 'blob', val.byteLength);
|
|
421
|
+
if (val instanceof Uint8Array)
|
|
422
|
+
pos += pack.tlen(pos, 'uint', $.$mol_vary_len[1]);
|
|
423
|
+
else if (val instanceof Uint16Array)
|
|
424
|
+
pos += pack.tlen(pos, 'uint', $.$mol_vary_len[2]);
|
|
425
|
+
else if (val instanceof Uint32Array)
|
|
426
|
+
pos += pack.tlen(pos, 'uint', $.$mol_vary_len[4]);
|
|
427
|
+
else if (val instanceof BigUint64Array)
|
|
428
|
+
pos += pack.tlen(pos, 'uint', $.$mol_vary_len[8]);
|
|
429
|
+
else if (val instanceof Int8Array)
|
|
430
|
+
pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[1]);
|
|
431
|
+
else if (val instanceof Int16Array)
|
|
432
|
+
pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[2]);
|
|
433
|
+
else if (val instanceof Int32Array)
|
|
434
|
+
pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[4]);
|
|
435
|
+
else if (val instanceof BigInt64Array)
|
|
436
|
+
pos += pack.tlen(pos, 'sint', ~$.$mol_vary_len[8]);
|
|
437
|
+
else if (val instanceof Float32Array)
|
|
438
|
+
pos += pack.tlen(pos, 'spec', $mol_vary_spec.fp32);
|
|
439
|
+
else if (val instanceof Float64Array)
|
|
440
|
+
pos += pack.tlen(pos, 'spec', $mol_vary_spec.fp64);
|
|
441
|
+
else
|
|
442
|
+
$mol_fail(new Error(`Unsupported type`));
|
|
443
|
+
buf.set(new Uint8Array(val.buffer, val.byteOffset, val.byteLength), pos);
|
|
444
|
+
pos += val.byteLength;
|
|
445
|
+
if (val.byteLength)
|
|
446
|
+
embedded.add(key);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (Array.isArray(val)) {
|
|
450
|
+
const key = hash(val);
|
|
451
|
+
if (embedded.has(key)) {
|
|
452
|
+
pos += pack.tnat(pos, 'link', offsets.get(key));
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
pos += pack.tnat(pos, 'list', val.length);
|
|
456
|
+
for (const item of val)
|
|
457
|
+
dump(item);
|
|
458
|
+
if (val.length)
|
|
459
|
+
embedded.add(key);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const key = hash(val);
|
|
463
|
+
if (embedded.has(key)) {
|
|
464
|
+
pos += pack.tnat(pos, 'link', offsets.get(key));
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
const [keys, vals] = lean(val);
|
|
468
|
+
pos += pack.tnat(pos, 'tupl', vals.length);
|
|
469
|
+
dump(keys);
|
|
470
|
+
for (const item of vals)
|
|
471
|
+
dump(item);
|
|
472
|
+
if (vals.length)
|
|
473
|
+
embedded.add(key);
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
$mol_fail(new Error(`Unsupported type`));
|
|
478
|
+
};
|
|
479
|
+
dump(data);
|
|
480
|
+
return buf;
|
|
481
|
+
}
|
|
482
|
+
static take(buf) {
|
|
483
|
+
const pack = new $mol_vary(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
484
|
+
const stream = [];
|
|
485
|
+
let pos = 0;
|
|
486
|
+
const read_unum = () => {
|
|
487
|
+
const num = pack.unum(pos);
|
|
488
|
+
pos += 1 + pack.ulen(pos);
|
|
489
|
+
return num;
|
|
490
|
+
};
|
|
491
|
+
const read_snum = () => {
|
|
492
|
+
const num = pack.snum(pos);
|
|
493
|
+
pos += 1 + pack.slen(pos);
|
|
494
|
+
return num;
|
|
495
|
+
};
|
|
496
|
+
const read_text = () => {
|
|
497
|
+
const len = read_unum();
|
|
498
|
+
const bin = new Uint8Array(buf.buffer, pack.byteOffset + pos, len);
|
|
499
|
+
pos += len;
|
|
500
|
+
const text = $mol_charset_decode(bin);
|
|
501
|
+
if (text.length)
|
|
502
|
+
stream.push(text);
|
|
503
|
+
return text;
|
|
504
|
+
};
|
|
505
|
+
const read_buffer = (len, TypedArray) => {
|
|
506
|
+
const bin = new TypedArray(buf.slice(pos, pos + len).buffer);
|
|
507
|
+
pos += len;
|
|
508
|
+
if (len)
|
|
509
|
+
stream.push(bin);
|
|
510
|
+
return bin;
|
|
511
|
+
};
|
|
512
|
+
const read_blob = () => {
|
|
513
|
+
const len = read_unum();
|
|
514
|
+
const kind = pack.getUint8(pos++);
|
|
515
|
+
switch (kind) {
|
|
516
|
+
case $mol_vary_tip.uint | $.$mol_vary_len[1]: return read_buffer(len, Uint8Array);
|
|
517
|
+
case $mol_vary_tip.uint | $.$mol_vary_len[2]: return read_buffer(len, Uint16Array);
|
|
518
|
+
case $mol_vary_tip.uint | $.$mol_vary_len[4]: return read_buffer(len, Uint32Array);
|
|
519
|
+
case $mol_vary_tip.uint | $.$mol_vary_len[8]: return read_buffer(len, BigUint64Array);
|
|
520
|
+
case $mol_vary_tip.sint | ~$.$mol_vary_len[1] + 256: return read_buffer(len, Int8Array);
|
|
521
|
+
case $mol_vary_tip.sint | ~$.$mol_vary_len[2] + 256: return read_buffer(len, Int16Array);
|
|
522
|
+
case $mol_vary_tip.sint | ~$.$mol_vary_len[4] + 256: return read_buffer(len, Int32Array);
|
|
523
|
+
case $mol_vary_tip.sint | ~$.$mol_vary_len[8] + 256: return read_buffer(len, BigInt64Array);
|
|
524
|
+
case $mol_vary_tip.spec | $mol_vary_spec.fp32: return read_buffer(len, Float32Array);
|
|
525
|
+
case $mol_vary_tip.spec | $mol_vary_spec.fp64: return read_buffer(len, Float64Array);
|
|
526
|
+
default:
|
|
527
|
+
$mol_fail(new Error('Unsupported blob kind', { cause: { kind } }));
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
const read_list = () => {
|
|
531
|
+
const len = read_unum();
|
|
532
|
+
const list = [];
|
|
533
|
+
for (let i = 0; i < len; ++i)
|
|
534
|
+
list.push(read_vary());
|
|
535
|
+
if (len)
|
|
536
|
+
stream.push(list);
|
|
537
|
+
return list;
|
|
538
|
+
};
|
|
539
|
+
const read_link = () => {
|
|
540
|
+
const index = read_unum();
|
|
541
|
+
if (index >= stream.length)
|
|
542
|
+
$mol_fail(new Error('Too large index', { cause: { index, exists: stream.length } }));
|
|
543
|
+
return stream[index];
|
|
544
|
+
};
|
|
545
|
+
const read_tupl = () => {
|
|
546
|
+
const len = read_unum();
|
|
547
|
+
const keys = pack.tip(pos) === 'link' ? read_link() : read_list();
|
|
548
|
+
if (!Array.isArray(keys))
|
|
549
|
+
$mol_fail(new Error('Wrong tupl shape type', { cause: { type: typeof keys } }));
|
|
550
|
+
const vals = Array.from({ length: len }, () => read_vary());
|
|
551
|
+
const obj = this.rich(keys, vals);
|
|
552
|
+
if (vals.length)
|
|
553
|
+
stream.push(obj);
|
|
554
|
+
return obj;
|
|
555
|
+
};
|
|
556
|
+
const read_spec = () => {
|
|
557
|
+
const kind = pack.getUint8(pos);
|
|
558
|
+
const spec = $mol_vary_spec[kind];
|
|
559
|
+
switch (spec) {
|
|
560
|
+
case 'none':
|
|
561
|
+
++pos;
|
|
562
|
+
return null;
|
|
563
|
+
case 'fake':
|
|
564
|
+
++pos;
|
|
565
|
+
return false;
|
|
566
|
+
case 'true':
|
|
567
|
+
++pos;
|
|
568
|
+
return true;
|
|
569
|
+
case 'both':
|
|
570
|
+
++pos;
|
|
571
|
+
return undefined;
|
|
572
|
+
case 'fp16': {
|
|
573
|
+
const val = pack.getFloat16(pos + 1, true);
|
|
574
|
+
pos += 3;
|
|
575
|
+
return val;
|
|
576
|
+
}
|
|
577
|
+
case 'fp32': {
|
|
578
|
+
const val = pack.getFloat32(pos + 1, true);
|
|
579
|
+
pos += 5;
|
|
580
|
+
return val;
|
|
581
|
+
}
|
|
582
|
+
case 'fp64': {
|
|
583
|
+
const val = pack.getFloat64(pos + 1, true);
|
|
584
|
+
pos += 9;
|
|
585
|
+
return val;
|
|
586
|
+
}
|
|
587
|
+
default:
|
|
588
|
+
$mol_fail(new Error('Unsupported spec', { cause: { kind } }));
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
const read_vary = () => {
|
|
592
|
+
const tip = pack.tip(pos);
|
|
593
|
+
switch (tip) {
|
|
594
|
+
case 'uint': return read_unum();
|
|
595
|
+
case 'sint': return read_snum();
|
|
596
|
+
case 'link': return read_link();
|
|
597
|
+
case 'text': return read_text();
|
|
598
|
+
case 'list': return read_list();
|
|
599
|
+
case 'blob': return read_blob();
|
|
600
|
+
case 'tupl': return read_tupl();
|
|
601
|
+
case 'spec': return read_spec();
|
|
602
|
+
default: $mol_fail(new Error('Unsupported tip', { cause: { tip } }));
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
return read_vary();
|
|
606
|
+
}
|
|
607
|
+
tlen(pos, tip, len) {
|
|
608
|
+
this.setUint8(pos, $mol_vary_tip[tip] | len);
|
|
609
|
+
return 1;
|
|
610
|
+
}
|
|
611
|
+
tnat(pos, tip, num) {
|
|
612
|
+
const len = num_len(num);
|
|
613
|
+
this.tlen(pos, tip, len ? $.$mol_vary_len[len] : Number(num));
|
|
614
|
+
switch (len) {
|
|
615
|
+
case 0: break;
|
|
616
|
+
case 1:
|
|
617
|
+
this.setUint8(pos + 1, Number(num));
|
|
618
|
+
break;
|
|
619
|
+
case 2:
|
|
620
|
+
this.setUint16(pos + 1, Number(num), true);
|
|
621
|
+
break;
|
|
622
|
+
case 4:
|
|
623
|
+
this.setUint32(pos + 1, Number(num), true);
|
|
624
|
+
break;
|
|
625
|
+
case 8:
|
|
626
|
+
this.setBigUint64(pos + 1, BigInt(num), true);
|
|
627
|
+
break;
|
|
628
|
+
default: $mol_fail(new Error('Unsupported uint len', { cause: { len } }));
|
|
629
|
+
}
|
|
630
|
+
return 1 + len;
|
|
631
|
+
}
|
|
632
|
+
tint(pos, tip, num) {
|
|
633
|
+
const len = num_len(num);
|
|
634
|
+
this.tlen(pos, tip, len ? -$.$mol_vary_len[len] - 1 : Number(num));
|
|
635
|
+
switch (len) {
|
|
636
|
+
case 0: break;
|
|
637
|
+
case 1:
|
|
638
|
+
this.setInt8(pos + 1, Number(num));
|
|
639
|
+
break;
|
|
640
|
+
case 2:
|
|
641
|
+
this.setInt16(pos + 1, Number(num), true);
|
|
642
|
+
break;
|
|
643
|
+
case 4:
|
|
644
|
+
this.setInt32(pos + 1, Number(num), true);
|
|
645
|
+
break;
|
|
646
|
+
case 8:
|
|
647
|
+
this.setBigInt64(pos + 1, BigInt(num), true);
|
|
648
|
+
break;
|
|
649
|
+
default: $mol_fail(new Error('Unsupported sint len', { cause: { len } }));
|
|
650
|
+
}
|
|
651
|
+
return 1 + len;
|
|
652
|
+
}
|
|
653
|
+
tfp(pos, tip, num) {
|
|
654
|
+
const len = num_len(num);
|
|
655
|
+
this.tlen(pos, tip, len ? $.$mol_vary_len[len] : Number(num));
|
|
656
|
+
switch (len) {
|
|
657
|
+
case 2:
|
|
658
|
+
this.setFloat16(pos + 1, num, true);
|
|
659
|
+
break;
|
|
660
|
+
case 4:
|
|
661
|
+
this.setFloat32(pos + 1, num, true);
|
|
662
|
+
break;
|
|
663
|
+
case 8:
|
|
664
|
+
this.setFloat64(pos + 1, num, true);
|
|
665
|
+
break;
|
|
666
|
+
default: $mol_fail(new Error('Unsupported fp len', { cause: { len } }));
|
|
667
|
+
}
|
|
668
|
+
return 1 + len;
|
|
669
|
+
}
|
|
670
|
+
tip(pos) {
|
|
671
|
+
return $mol_vary_tip[this.getUint8(pos) & 0b111_00000];
|
|
672
|
+
}
|
|
673
|
+
ulen(pos) {
|
|
674
|
+
const num = this.getUint8(pos) & 0b11111;
|
|
675
|
+
if (num < 28)
|
|
676
|
+
return 0;
|
|
677
|
+
switch (num) {
|
|
678
|
+
case 28: return 1;
|
|
679
|
+
case 29: return 2;
|
|
680
|
+
case 30: return 4;
|
|
681
|
+
case 31: return 8;
|
|
682
|
+
default: $mol_fail(new Error('Impossible!'));
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
slen(pos) {
|
|
686
|
+
const num = this.getInt8(pos);
|
|
687
|
+
if (num > -29)
|
|
688
|
+
return 0;
|
|
689
|
+
switch (num) {
|
|
690
|
+
case -29: return 1;
|
|
691
|
+
case -30: return 2;
|
|
692
|
+
case -31: return 4;
|
|
693
|
+
case -32: return 8;
|
|
694
|
+
default: $mol_fail(new Error('Impossible!'));
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
unum(pos) {
|
|
698
|
+
const num = this.getUint8(pos) & 0b11111;
|
|
699
|
+
if (num < 28)
|
|
700
|
+
return num;
|
|
701
|
+
switch (num) {
|
|
702
|
+
case 28: return this.getUint8(pos + 1);
|
|
703
|
+
case 29: return this.getUint16(pos + 1, true);
|
|
704
|
+
case 30: return this.getUint32(pos + 1, true);
|
|
705
|
+
case 31:
|
|
706
|
+
const val = this.getBigUint64(pos + 1, true);
|
|
707
|
+
if (val > Number.MAX_SAFE_INTEGER)
|
|
708
|
+
return val;
|
|
709
|
+
return Number(val);
|
|
710
|
+
default: $mol_fail(new Error('Unsupported len', { cause: { num } }));
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
snum(pos) {
|
|
714
|
+
const num = this.getInt8(pos);
|
|
715
|
+
if (num > -29)
|
|
716
|
+
return num;
|
|
717
|
+
switch (num) {
|
|
718
|
+
case -29: return this.getInt8(pos + 1);
|
|
719
|
+
case -30: return this.getInt16(pos + 1, true);
|
|
720
|
+
case -31: return this.getInt32(pos + 1, true);
|
|
721
|
+
case -32:
|
|
722
|
+
const val = this.getBigInt64(pos + 1, true);
|
|
723
|
+
if (val > Number.MAX_SAFE_INTEGER)
|
|
724
|
+
return val;
|
|
725
|
+
if (val < Number.MIN_SAFE_INTEGER)
|
|
726
|
+
return val;
|
|
727
|
+
return Number(val);
|
|
728
|
+
default: $mol_fail(new Error('Unsupported len', { cause: { num } }));
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
static rich(keys, vals) {
|
|
732
|
+
const shape = JSON.stringify([keys, vals.map(v => typeof v)]);
|
|
733
|
+
const rich = this.riches.get(shape);
|
|
734
|
+
if (rich)
|
|
735
|
+
return rich(...vals);
|
|
736
|
+
const pairs = keys.map((key, index) => [key, vals[index]]);
|
|
737
|
+
const obj = Object.fromEntries(pairs);
|
|
738
|
+
return obj;
|
|
739
|
+
}
|
|
740
|
+
static lean(val) {
|
|
741
|
+
const proto = Reflect.getPrototypeOf(val);
|
|
742
|
+
const lean = this.leanes.get(proto);
|
|
743
|
+
if (lean)
|
|
744
|
+
return lean(val);
|
|
745
|
+
return [Object.keys(val), Object.values(val)];
|
|
746
|
+
}
|
|
747
|
+
static leanes = new Map();
|
|
748
|
+
static riches = new Map();
|
|
749
|
+
static register(rich, lean) {
|
|
750
|
+
const obj = rich();
|
|
751
|
+
const proto = Reflect.getPrototypeOf(obj);
|
|
752
|
+
const [keys, vals] = lean(obj);
|
|
753
|
+
const shape = JSON.stringify([keys, vals.map(v => typeof v)]);
|
|
754
|
+
this.leanes.set(proto, lean);
|
|
755
|
+
this.riches.set(shape, rich);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
$.$mol_vary = $mol_vary;
|
|
759
|
+
$mol_vary.register((keys = [], vals = []) => new Map(keys.map((k, i) => [k, vals[i]])), obj => [
|
|
760
|
+
['keys', 'vals'],
|
|
761
|
+
[[...obj.keys()], [...obj.values()]],
|
|
762
|
+
]);
|
|
763
|
+
$mol_vary.register((vals = []) => new Set(vals), obj => [
|
|
764
|
+
['vals'],
|
|
765
|
+
[[...obj.values()]],
|
|
766
|
+
]);
|
|
767
|
+
})($ || ($ = {}));
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
export default $
|
|
771
|
+
//# sourceMappingURL=node.js.map
|