distillate 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/dist/blocked/index.cjs +115 -46
- package/dist/blocked/index.d.cts +40 -3
- package/dist/blocked/index.d.ts +40 -3
- package/dist/blocked/index.js +115 -46
- package/dist/bloom/index.cjs +69 -26
- package/dist/bloom/index.d.cts +34 -2
- package/dist/bloom/index.d.ts +34 -2
- package/dist/bloom/index.js +69 -26
- package/dist/fuse/index.cjs +73 -77
- package/dist/fuse/index.d.cts +34 -2
- package/dist/fuse/index.d.ts +34 -2
- package/dist/fuse/index.js +63 -67
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{params-J8p3bKq5.cjs → params--8CNXYWu.cjs} +10 -0
- package/dist/{params-ChTRNxM9.js → params-BrWuBC2A.js} +5 -1
- package/dist/serialize-BIIKUHH6.cjs +515 -0
- package/dist/serialize-BnwtzcMw.js +414 -0
- package/dist/serialize-DRKh6QOr.d.cts +15 -0
- package/dist/serialize-DRKh6QOr.d.ts +15 -0
- package/package.json +5 -28
- package/dist/bytes-DCuYtUVS.d.cts +0 -4
- package/dist/bytes-DCuYtUVS.d.ts +0 -4
- package/dist/serialize-DPdiySxq.cjs +0 -355
- package/dist/serialize-DkkBKDT0.js +0 -308
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
//#region src/core/bytes.ts
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
function normalize(input) {
|
|
4
|
+
if (typeof input === "string") return encoder.encode(input);
|
|
5
|
+
if (input instanceof Uint8Array) return input;
|
|
6
|
+
return new Uint8Array(input);
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/core/hasher.ts
|
|
10
|
+
let RLO = 0;
|
|
11
|
+
let RHI = 0;
|
|
12
|
+
const LANES = {
|
|
13
|
+
w0: 0,
|
|
14
|
+
w1: 0,
|
|
15
|
+
w2: 0,
|
|
16
|
+
w3: 0
|
|
17
|
+
};
|
|
18
|
+
function mul64(alo, ahi, blo, bhi) {
|
|
19
|
+
const a0 = alo & 65535;
|
|
20
|
+
const a1 = alo >>> 16;
|
|
21
|
+
const a2 = ahi & 65535;
|
|
22
|
+
const a3 = ahi >>> 16;
|
|
23
|
+
const b0 = blo & 65535;
|
|
24
|
+
const b1 = blo >>> 16;
|
|
25
|
+
const b2 = bhi & 65535;
|
|
26
|
+
const b3 = bhi >>> 16;
|
|
27
|
+
let c0 = a0 * b0;
|
|
28
|
+
let c1 = c0 >>> 16;
|
|
29
|
+
c0 &= 65535;
|
|
30
|
+
c1 += a1 * b0;
|
|
31
|
+
let c2 = c1 >>> 16;
|
|
32
|
+
c1 &= 65535;
|
|
33
|
+
c1 += a0 * b1;
|
|
34
|
+
c2 += c1 >>> 16;
|
|
35
|
+
c1 &= 65535;
|
|
36
|
+
c2 += a2 * b0;
|
|
37
|
+
let c3 = c2 >>> 16;
|
|
38
|
+
c2 &= 65535;
|
|
39
|
+
c2 += a1 * b1;
|
|
40
|
+
c3 += c2 >>> 16;
|
|
41
|
+
c2 &= 65535;
|
|
42
|
+
c2 += a0 * b2;
|
|
43
|
+
c3 += c2 >>> 16;
|
|
44
|
+
c2 &= 65535;
|
|
45
|
+
c3 += a3 * b0 + a2 * b1 + a1 * b2 + a0 * b3;
|
|
46
|
+
c3 &= 65535;
|
|
47
|
+
RLO = (c1 << 16 | c0) >>> 0;
|
|
48
|
+
RHI = (c3 << 16 | c2) >>> 0;
|
|
49
|
+
}
|
|
50
|
+
function fmix64(lo, hi) {
|
|
51
|
+
let l = lo;
|
|
52
|
+
let h = hi;
|
|
53
|
+
l = (l ^ h >>> 1) >>> 0;
|
|
54
|
+
mul64(l, h, 3981806797, 4283543511);
|
|
55
|
+
l = RLO;
|
|
56
|
+
h = RHI;
|
|
57
|
+
l = (l ^ h >>> 1) >>> 0;
|
|
58
|
+
mul64(l, h, 444984403, 3301882366);
|
|
59
|
+
l = RLO;
|
|
60
|
+
h = RHI;
|
|
61
|
+
l = (l ^ h >>> 1) >>> 0;
|
|
62
|
+
RLO = l;
|
|
63
|
+
RHI = h;
|
|
64
|
+
}
|
|
65
|
+
function rotl32(x, r) {
|
|
66
|
+
return (x << r | x >>> 32 - r) >>> 0;
|
|
67
|
+
}
|
|
68
|
+
function fmix32(h) {
|
|
69
|
+
let x = h;
|
|
70
|
+
x ^= x >>> 16;
|
|
71
|
+
x = Math.imul(x, 2246822507);
|
|
72
|
+
x ^= x >>> 13;
|
|
73
|
+
x = Math.imul(x, 3266489909);
|
|
74
|
+
x ^= x >>> 16;
|
|
75
|
+
return x >>> 0;
|
|
76
|
+
}
|
|
77
|
+
const K1 = 597399067;
|
|
78
|
+
const K2 = 2869860233;
|
|
79
|
+
const K3 = 951274213;
|
|
80
|
+
const K4 = 2716044179;
|
|
81
|
+
function computeLanes(bytes, seed, len) {
|
|
82
|
+
const nblocks = len >>> 4;
|
|
83
|
+
let h1 = seed >>> 0;
|
|
84
|
+
let h2 = seed >>> 0;
|
|
85
|
+
let h3 = seed >>> 0;
|
|
86
|
+
let h4 = seed >>> 0;
|
|
87
|
+
for (let i = 0; i < nblocks; i++) {
|
|
88
|
+
const b = i * 16;
|
|
89
|
+
let k1 = ((bytes[b] ?? 0) | (bytes[b + 1] ?? 0) << 8 | (bytes[b + 2] ?? 0) << 16 | (bytes[b + 3] ?? 0) << 24) >>> 0;
|
|
90
|
+
let k2 = ((bytes[b + 4] ?? 0) | (bytes[b + 5] ?? 0) << 8 | (bytes[b + 6] ?? 0) << 16 | (bytes[b + 7] ?? 0) << 24) >>> 0;
|
|
91
|
+
let k3 = ((bytes[b + 8] ?? 0) | (bytes[b + 9] ?? 0) << 8 | (bytes[b + 10] ?? 0) << 16 | (bytes[b + 11] ?? 0) << 24) >>> 0;
|
|
92
|
+
let k4 = ((bytes[b + 12] ?? 0) | (bytes[b + 13] ?? 0) << 8 | (bytes[b + 14] ?? 0) << 16 | (bytes[b + 15] ?? 0) << 24) >>> 0;
|
|
93
|
+
k1 = Math.imul(rotl32(Math.imul(k1, K1), 15), K2);
|
|
94
|
+
h1 ^= k1;
|
|
95
|
+
h1 = rotl32(h1, 19);
|
|
96
|
+
h1 = h1 + h2 >>> 0;
|
|
97
|
+
h1 = Math.imul(h1, 5) + 1444728091 >>> 0;
|
|
98
|
+
k2 = Math.imul(rotl32(Math.imul(k2, K2), 16), K3);
|
|
99
|
+
h2 ^= k2;
|
|
100
|
+
h2 = rotl32(h2, 17);
|
|
101
|
+
h2 = h2 + h3 >>> 0;
|
|
102
|
+
h2 = Math.imul(h2, 5) + 197830471 >>> 0;
|
|
103
|
+
k3 = Math.imul(rotl32(Math.imul(k3, K3), 17), K4);
|
|
104
|
+
h3 ^= k3;
|
|
105
|
+
h3 = rotl32(h3, 15);
|
|
106
|
+
h3 = h3 + h4 >>> 0;
|
|
107
|
+
h3 = Math.imul(h3, 5) + 2530024501 >>> 0;
|
|
108
|
+
k4 = Math.imul(rotl32(Math.imul(k4, K4), 18), K1);
|
|
109
|
+
h4 ^= k4;
|
|
110
|
+
h4 = rotl32(h4, 13);
|
|
111
|
+
h4 = h4 + h1 >>> 0;
|
|
112
|
+
h4 = Math.imul(h4, 5) + 850148119 >>> 0;
|
|
113
|
+
}
|
|
114
|
+
let k1 = 0;
|
|
115
|
+
let k2 = 0;
|
|
116
|
+
let k3 = 0;
|
|
117
|
+
let k4 = 0;
|
|
118
|
+
const tail = nblocks * 16;
|
|
119
|
+
const rem = len & 15;
|
|
120
|
+
if (rem >= 15) k4 ^= (bytes[tail + 14] ?? 0) << 16;
|
|
121
|
+
if (rem >= 14) k4 ^= (bytes[tail + 13] ?? 0) << 8;
|
|
122
|
+
if (rem >= 13) {
|
|
123
|
+
k4 ^= bytes[tail + 12] ?? 0;
|
|
124
|
+
k4 = Math.imul(rotl32(Math.imul(k4, K4), 18), K1);
|
|
125
|
+
h4 ^= k4;
|
|
126
|
+
}
|
|
127
|
+
if (rem >= 12) k3 ^= (bytes[tail + 11] ?? 0) << 24;
|
|
128
|
+
if (rem >= 11) k3 ^= (bytes[tail + 10] ?? 0) << 16;
|
|
129
|
+
if (rem >= 10) k3 ^= (bytes[tail + 9] ?? 0) << 8;
|
|
130
|
+
if (rem >= 9) {
|
|
131
|
+
k3 ^= bytes[tail + 8] ?? 0;
|
|
132
|
+
k3 = Math.imul(rotl32(Math.imul(k3, K3), 17), K4);
|
|
133
|
+
h3 ^= k3;
|
|
134
|
+
}
|
|
135
|
+
if (rem >= 8) k2 ^= (bytes[tail + 7] ?? 0) << 24;
|
|
136
|
+
if (rem >= 7) k2 ^= (bytes[tail + 6] ?? 0) << 16;
|
|
137
|
+
if (rem >= 6) k2 ^= (bytes[tail + 5] ?? 0) << 8;
|
|
138
|
+
if (rem >= 5) {
|
|
139
|
+
k2 ^= bytes[tail + 4] ?? 0;
|
|
140
|
+
k2 = Math.imul(rotl32(Math.imul(k2, K2), 16), K3);
|
|
141
|
+
h2 ^= k2;
|
|
142
|
+
}
|
|
143
|
+
if (rem >= 4) k1 ^= (bytes[tail + 3] ?? 0) << 24;
|
|
144
|
+
if (rem >= 3) k1 ^= (bytes[tail + 2] ?? 0) << 16;
|
|
145
|
+
if (rem >= 2) k1 ^= (bytes[tail + 1] ?? 0) << 8;
|
|
146
|
+
if (rem >= 1) {
|
|
147
|
+
k1 ^= bytes[tail] ?? 0;
|
|
148
|
+
k1 = Math.imul(rotl32(Math.imul(k1, K1), 15), K2);
|
|
149
|
+
h1 ^= k1;
|
|
150
|
+
}
|
|
151
|
+
h1 = (h1 ^ len) >>> 0;
|
|
152
|
+
h2 = (h2 ^ len) >>> 0;
|
|
153
|
+
h3 = (h3 ^ len) >>> 0;
|
|
154
|
+
h4 = (h4 ^ len) >>> 0;
|
|
155
|
+
h1 = h1 + h2 >>> 0;
|
|
156
|
+
h1 = h1 + h3 >>> 0;
|
|
157
|
+
h1 = h1 + h4 >>> 0;
|
|
158
|
+
h2 = h2 + h1 >>> 0;
|
|
159
|
+
h3 = h3 + h1 >>> 0;
|
|
160
|
+
h4 = h4 + h1 >>> 0;
|
|
161
|
+
h1 = fmix32(h1);
|
|
162
|
+
h2 = fmix32(h2);
|
|
163
|
+
h3 = fmix32(h3);
|
|
164
|
+
h4 = fmix32(h4);
|
|
165
|
+
h1 = h1 + h2 >>> 0;
|
|
166
|
+
h1 = h1 + h3 >>> 0;
|
|
167
|
+
h1 = h1 + h4 >>> 0;
|
|
168
|
+
h2 = h2 + h1 >>> 0;
|
|
169
|
+
h3 = h3 + h1 >>> 0;
|
|
170
|
+
h4 = h4 + h1 >>> 0;
|
|
171
|
+
LANES.w0 = h1;
|
|
172
|
+
LANES.w1 = h2;
|
|
173
|
+
LANES.w2 = h3;
|
|
174
|
+
LANES.w3 = h4;
|
|
175
|
+
}
|
|
176
|
+
const keyEncoder = new TextEncoder();
|
|
177
|
+
let keyBuf = /* @__PURE__ */ new Uint8Array(256);
|
|
178
|
+
let encBytes = keyBuf;
|
|
179
|
+
let encLen = 0;
|
|
180
|
+
function encodeKey(key) {
|
|
181
|
+
if (typeof key === "string") {
|
|
182
|
+
const cap = key.length * 3;
|
|
183
|
+
if (keyBuf.length < cap) keyBuf = new Uint8Array(cap);
|
|
184
|
+
encLen = keyEncoder.encodeInto(key, keyBuf).written;
|
|
185
|
+
encBytes = keyBuf;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
encBytes = normalize(key);
|
|
189
|
+
encLen = encBytes.length;
|
|
190
|
+
}
|
|
191
|
+
function keyToLanes(key, seed) {
|
|
192
|
+
encodeKey(key);
|
|
193
|
+
computeLanes(encBytes, seed, encLen);
|
|
194
|
+
}
|
|
195
|
+
function hash128KeyInto(key, seed, out) {
|
|
196
|
+
keyToLanes(key, seed);
|
|
197
|
+
out.w0 = LANES.w0;
|
|
198
|
+
out.w1 = LANES.w1;
|
|
199
|
+
out.w2 = LANES.w2;
|
|
200
|
+
out.w3 = LANES.w3;
|
|
201
|
+
}
|
|
202
|
+
function reduce(x, range) {
|
|
203
|
+
const xlo = x & 65535;
|
|
204
|
+
const xhi = x >>> 16;
|
|
205
|
+
const rlo = range & 65535;
|
|
206
|
+
const rhi = range >>> 16;
|
|
207
|
+
const lolo = xlo * rlo;
|
|
208
|
+
const lohi = xlo * rhi;
|
|
209
|
+
const hilo = xhi * rlo;
|
|
210
|
+
const hihi = xhi * rhi;
|
|
211
|
+
const carry = (lolo >>> 16) + (lohi & 65535) + (hilo & 65535);
|
|
212
|
+
return hihi + (lohi >>> 16) + (hilo >>> 16) + (carry >>> 16) >>> 0;
|
|
213
|
+
}
|
|
214
|
+
function hash32x2Into(key, seed, out) {
|
|
215
|
+
keyToLanes(key, seed);
|
|
216
|
+
out[0] = LANES.w0;
|
|
217
|
+
out[1] = LANES.w1;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Derive `count` bucket indices in `[0, range)` from a key using
|
|
221
|
+
* Kirsch-Mitzenmacher enhanced double hashing `g_i = h1 + i*h2 + i^2`
|
|
222
|
+
* (the RocksDB `+i^2` fix), reduced into range with Lemire multiply-shift.
|
|
223
|
+
*/
|
|
224
|
+
function probeInto(key, count, range, seed, out) {
|
|
225
|
+
keyToLanes(key, seed);
|
|
226
|
+
const a = LANES.w0;
|
|
227
|
+
const b = LANES.w1;
|
|
228
|
+
for (let i = 0; i < count; i++) out[i] = reduce(a + Math.imul(i, b) + i * i >>> 0, range);
|
|
229
|
+
}
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/core/base64.ts
|
|
232
|
+
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
233
|
+
const DECODE = (/* @__PURE__ */ new Int16Array(128)).fill(-1);
|
|
234
|
+
for (let i = 0; i < 64; i++) DECODE[ALPHABET.charCodeAt(i)] = i;
|
|
235
|
+
const sym = (n) => ALPHABET.charAt(n & 63);
|
|
236
|
+
function toBase64(bytes) {
|
|
237
|
+
let out = "";
|
|
238
|
+
let i = 0;
|
|
239
|
+
for (; i + 3 <= bytes.length; i += 3) {
|
|
240
|
+
const n = (bytes[i] ?? 0) << 16 | (bytes[i + 1] ?? 0) << 8 | (bytes[i + 2] ?? 0);
|
|
241
|
+
out += sym(n >>> 18) + sym(n >>> 12) + sym(n >>> 6) + sym(n);
|
|
242
|
+
}
|
|
243
|
+
const rem = bytes.length - i;
|
|
244
|
+
if (rem === 1) {
|
|
245
|
+
const n = (bytes[i] ?? 0) << 16;
|
|
246
|
+
out += sym(n >>> 18) + sym(n >>> 12) + "==";
|
|
247
|
+
} else if (rem === 2) {
|
|
248
|
+
const n = (bytes[i] ?? 0) << 16 | (bytes[i + 1] ?? 0) << 8;
|
|
249
|
+
out += sym(n >>> 18) + sym(n >>> 12) + sym(n >>> 6) + "=";
|
|
250
|
+
}
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
253
|
+
function fromBase64(s) {
|
|
254
|
+
let len = s.length;
|
|
255
|
+
while (len > 0 && s.charCodeAt(len - 1) === 61) len--;
|
|
256
|
+
if (len % 4 === 1) throw new RangeError(`invalid base64 length ${String(len)}`);
|
|
257
|
+
const out = new Uint8Array(len * 3 >> 2);
|
|
258
|
+
let acc = 0;
|
|
259
|
+
let bits = 0;
|
|
260
|
+
let o = 0;
|
|
261
|
+
for (let i = 0; i < len; i++) {
|
|
262
|
+
const v = DECODE[s.charCodeAt(i)] ?? -1;
|
|
263
|
+
if (v < 0) throw new RangeError(`invalid base64 character at index ${String(i)}`);
|
|
264
|
+
acc = acc << 6 | v;
|
|
265
|
+
bits += 6;
|
|
266
|
+
if (bits >= 8) {
|
|
267
|
+
bits -= 8;
|
|
268
|
+
out[o++] = acc >>> bits & 255;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
//#endregion
|
|
274
|
+
//#region src/core/crc32.ts
|
|
275
|
+
const T1 = 256;
|
|
276
|
+
const T2 = 512;
|
|
277
|
+
const T3 = 768;
|
|
278
|
+
const T4 = 1024;
|
|
279
|
+
const T5 = 1280;
|
|
280
|
+
const T6 = 1536;
|
|
281
|
+
const T7 = 1792;
|
|
282
|
+
const at = (a, i) => a[i] ?? 0;
|
|
283
|
+
let tables;
|
|
284
|
+
function buildTables() {
|
|
285
|
+
const t = new Uint32Array(8 * 256);
|
|
286
|
+
for (let n = 0; n < 256; n++) {
|
|
287
|
+
let c = n;
|
|
288
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
289
|
+
t[n] = c >>> 0;
|
|
290
|
+
}
|
|
291
|
+
for (let k = 1; k < 8; k++) for (let n = 0; n < 256; n++) {
|
|
292
|
+
const p = at(t, (k - 1) * 256 + n);
|
|
293
|
+
t[k * 256 + n] = (at(t, p & 255) ^ p >>> 8) >>> 0;
|
|
294
|
+
}
|
|
295
|
+
return t;
|
|
296
|
+
}
|
|
297
|
+
function crc32(bytes) {
|
|
298
|
+
const t = tables ??= buildTables();
|
|
299
|
+
const len = bytes.length;
|
|
300
|
+
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
301
|
+
let crc = 4294967295;
|
|
302
|
+
let i = 0;
|
|
303
|
+
const last = len - 8;
|
|
304
|
+
while (i <= last) {
|
|
305
|
+
const one = (dv.getUint32(i, true) ^ crc) >>> 0;
|
|
306
|
+
const two = dv.getUint32(i + 4, true);
|
|
307
|
+
crc = (at(t, T7 + (one & 255)) ^ at(t, T6 + (one >>> 8 & 255)) ^ at(t, T5 + (one >>> 16 & 255)) ^ at(t, T4 + (one >>> 24 & 255)) ^ at(t, T3 + (two & 255)) ^ at(t, T2 + (two >>> 8 & 255)) ^ at(t, T1 + (two >>> 16 & 255)) ^ at(t, two >>> 24 & 255)) >>> 0;
|
|
308
|
+
i += 8;
|
|
309
|
+
}
|
|
310
|
+
for (; i < len; i++) crc = (at(t, (crc ^ dv.getUint8(i)) & 255) ^ crc >>> 8) >>> 0;
|
|
311
|
+
return (crc ^ 4294967295) >>> 0;
|
|
312
|
+
}
|
|
313
|
+
const HEADER_SIZE = 8;
|
|
314
|
+
const TRAILER_SIZE = 4;
|
|
315
|
+
/**
|
|
316
|
+
* Allocates a full frame once, hands `fill` a writable view over the body region
|
|
317
|
+
* (and a `DataView` scoped to it), then seals the CRC trailer. The body view
|
|
318
|
+
* aliases the frame's buffer, so callers write fields and payload straight into
|
|
319
|
+
* the frame with no intermediate body allocation or copy.
|
|
320
|
+
*/
|
|
321
|
+
function writeFrame(header, bodyLength, fill) {
|
|
322
|
+
const frame = new Uint8Array(HEADER_SIZE + bodyLength + TRAILER_SIZE);
|
|
323
|
+
frame[0] = 65;
|
|
324
|
+
frame[1] = 77;
|
|
325
|
+
frame[2] = 81;
|
|
326
|
+
frame[3] = 70;
|
|
327
|
+
frame[4] = header.version;
|
|
328
|
+
frame[5] = header.type;
|
|
329
|
+
frame[6] = header.flags;
|
|
330
|
+
frame[7] = 0;
|
|
331
|
+
fill(frame.subarray(HEADER_SIZE, HEADER_SIZE + bodyLength), new DataView(frame.buffer, HEADER_SIZE, bodyLength));
|
|
332
|
+
const crc = crc32(frame.subarray(0, HEADER_SIZE + bodyLength));
|
|
333
|
+
new DataView(frame.buffer, frame.byteOffset, frame.byteLength).setUint32(HEADER_SIZE + bodyLength, crc, true);
|
|
334
|
+
return frame;
|
|
335
|
+
}
|
|
336
|
+
var SerializationError = class extends Error {
|
|
337
|
+
name = "SerializationError";
|
|
338
|
+
};
|
|
339
|
+
var TruncatedError = class extends SerializationError {
|
|
340
|
+
name = "TruncatedError";
|
|
341
|
+
};
|
|
342
|
+
var BadMagicError = class extends SerializationError {
|
|
343
|
+
name = "BadMagicError";
|
|
344
|
+
};
|
|
345
|
+
var UnknownVersionError = class extends SerializationError {
|
|
346
|
+
name = "UnknownVersionError";
|
|
347
|
+
};
|
|
348
|
+
var UnknownHashVariantError = class extends SerializationError {
|
|
349
|
+
name = "UnknownHashVariantError";
|
|
350
|
+
};
|
|
351
|
+
var ChecksumError = class extends SerializationError {
|
|
352
|
+
name = "ChecksumError";
|
|
353
|
+
};
|
|
354
|
+
const JSON_TAG = "distillate";
|
|
355
|
+
/** Wraps a serialized frame in the JSON envelope. */
|
|
356
|
+
function toJSONEnvelope(bytes) {
|
|
357
|
+
return {
|
|
358
|
+
$: JSON_TAG,
|
|
359
|
+
v: 3,
|
|
360
|
+
data: toBase64(bytes)
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Validates a JSON envelope and returns the raw frame bytes for a structure's
|
|
365
|
+
* own `fromBytes` to decode. Throws {@link SerializationError} on any envelope
|
|
366
|
+
* defect; the frame itself is checked downstream.
|
|
367
|
+
*/
|
|
368
|
+
function fromJSONEnvelope(value) {
|
|
369
|
+
if (value === null || typeof value !== "object") throw new SerializationError("not a distillate filter JSON object");
|
|
370
|
+
const o = value;
|
|
371
|
+
if (o.$ !== JSON_TAG) throw new SerializationError(`expected "$":"${JSON_TAG}"`);
|
|
372
|
+
if (o.v !== 3) throw new UnknownVersionError(`unsupported JSON version ${String(o.v)}, expected ${String(3)}`);
|
|
373
|
+
if (typeof o.data !== "string") throw new SerializationError("missing string \"data\"");
|
|
374
|
+
try {
|
|
375
|
+
return fromBase64(o.data);
|
|
376
|
+
} catch {
|
|
377
|
+
throw new SerializationError("envelope data is not valid base64");
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
/** Byte-wise equality of two frames; the basis for structure `equals`. */
|
|
381
|
+
function bytesEqual(a, b) {
|
|
382
|
+
if (a.length !== b.length) return false;
|
|
383
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Asserts a frame body is long enough to hold its fixed params block, so the
|
|
388
|
+
* params can be read without running off the end.
|
|
389
|
+
*/
|
|
390
|
+
function assertMinBodyLength(actual, min, context) {
|
|
391
|
+
if (actual < min) throw new TruncatedError(`${context}: body of ${String(actual)} bytes is shorter than the ${String(min)}-byte params block`);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Asserts a frame body is exactly the length its declared params imply, so a
|
|
395
|
+
* hostile or truncated frame is rejected before any backing store is allocated.
|
|
396
|
+
*/
|
|
397
|
+
function assertBodyLength(actual, expected, context) {
|
|
398
|
+
if (actual !== expected) throw new TruncatedError(`${context}: body of ${String(actual)} bytes does not match the declared params (expected ${String(expected)})`);
|
|
399
|
+
}
|
|
400
|
+
function readHeader(frame) {
|
|
401
|
+
if (frame.length < 12) throw new TruncatedError(`frame of ${String(frame.length)} bytes is shorter than the minimum ${String(12)}`);
|
|
402
|
+
if (frame[0] !== 65 || frame[1] !== 77 || frame[2] !== 81 || frame[3] !== 70) throw new BadMagicError("frame does not start with the AMQF magic");
|
|
403
|
+
const version = frame[4] ?? 0;
|
|
404
|
+
if (version !== 3) throw new UnknownVersionError(`unsupported format version ${String(version)}`);
|
|
405
|
+
if (new DataView(frame.buffer, frame.byteOffset, frame.byteLength).getUint32(frame.length - TRAILER_SIZE, true) !== crc32(frame.subarray(0, frame.length - TRAILER_SIZE))) throw new ChecksumError("frame CRC32 does not match its contents");
|
|
406
|
+
return {
|
|
407
|
+
version,
|
|
408
|
+
type: frame[5] ?? 0,
|
|
409
|
+
flags: frame[6] ?? 0,
|
|
410
|
+
body: frame.subarray(HEADER_SIZE, frame.length - TRAILER_SIZE)
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
//#endregion
|
|
414
|
+
Object.defineProperty(exports, "RHI", {
|
|
415
|
+
enumerable: true,
|
|
416
|
+
get: function() {
|
|
417
|
+
return RHI;
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
Object.defineProperty(exports, "RLO", {
|
|
421
|
+
enumerable: true,
|
|
422
|
+
get: function() {
|
|
423
|
+
return RLO;
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
Object.defineProperty(exports, "SerializationError", {
|
|
427
|
+
enumerable: true,
|
|
428
|
+
get: function() {
|
|
429
|
+
return SerializationError;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
Object.defineProperty(exports, "UnknownHashVariantError", {
|
|
433
|
+
enumerable: true,
|
|
434
|
+
get: function() {
|
|
435
|
+
return UnknownHashVariantError;
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
Object.defineProperty(exports, "assertBodyLength", {
|
|
439
|
+
enumerable: true,
|
|
440
|
+
get: function() {
|
|
441
|
+
return assertBodyLength;
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
Object.defineProperty(exports, "assertMinBodyLength", {
|
|
445
|
+
enumerable: true,
|
|
446
|
+
get: function() {
|
|
447
|
+
return assertMinBodyLength;
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
Object.defineProperty(exports, "bytesEqual", {
|
|
451
|
+
enumerable: true,
|
|
452
|
+
get: function() {
|
|
453
|
+
return bytesEqual;
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
Object.defineProperty(exports, "fmix64", {
|
|
457
|
+
enumerable: true,
|
|
458
|
+
get: function() {
|
|
459
|
+
return fmix64;
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
Object.defineProperty(exports, "fromJSONEnvelope", {
|
|
463
|
+
enumerable: true,
|
|
464
|
+
get: function() {
|
|
465
|
+
return fromJSONEnvelope;
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
Object.defineProperty(exports, "hash128KeyInto", {
|
|
469
|
+
enumerable: true,
|
|
470
|
+
get: function() {
|
|
471
|
+
return hash128KeyInto;
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
Object.defineProperty(exports, "hash32x2Into", {
|
|
475
|
+
enumerable: true,
|
|
476
|
+
get: function() {
|
|
477
|
+
return hash32x2Into;
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
Object.defineProperty(exports, "mul64", {
|
|
481
|
+
enumerable: true,
|
|
482
|
+
get: function() {
|
|
483
|
+
return mul64;
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
Object.defineProperty(exports, "probeInto", {
|
|
487
|
+
enumerable: true,
|
|
488
|
+
get: function() {
|
|
489
|
+
return probeInto;
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
Object.defineProperty(exports, "readHeader", {
|
|
493
|
+
enumerable: true,
|
|
494
|
+
get: function() {
|
|
495
|
+
return readHeader;
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
Object.defineProperty(exports, "reduce", {
|
|
499
|
+
enumerable: true,
|
|
500
|
+
get: function() {
|
|
501
|
+
return reduce;
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
Object.defineProperty(exports, "toJSONEnvelope", {
|
|
505
|
+
enumerable: true,
|
|
506
|
+
get: function() {
|
|
507
|
+
return toJSONEnvelope;
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
Object.defineProperty(exports, "writeFrame", {
|
|
511
|
+
enumerable: true,
|
|
512
|
+
get: function() {
|
|
513
|
+
return writeFrame;
|
|
514
|
+
}
|
|
515
|
+
});
|