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
|
@@ -1,355 +0,0 @@
|
|
|
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
|
-
const C1LO = 289559509;
|
|
11
|
-
const C1HI = 2277735313;
|
|
12
|
-
const C2LO = 658871167;
|
|
13
|
-
const C2HI = 1291169091;
|
|
14
|
-
let RLO = 0;
|
|
15
|
-
let RHI = 0;
|
|
16
|
-
const LANES = {
|
|
17
|
-
h1lo: 0,
|
|
18
|
-
h1hi: 0,
|
|
19
|
-
h2lo: 0,
|
|
20
|
-
h2hi: 0
|
|
21
|
-
};
|
|
22
|
-
function mul64(alo, ahi, blo, bhi) {
|
|
23
|
-
const a0 = alo & 65535;
|
|
24
|
-
const a1 = alo >>> 16;
|
|
25
|
-
const a2 = ahi & 65535;
|
|
26
|
-
const a3 = ahi >>> 16;
|
|
27
|
-
const b0 = blo & 65535;
|
|
28
|
-
const b1 = blo >>> 16;
|
|
29
|
-
const b2 = bhi & 65535;
|
|
30
|
-
const b3 = bhi >>> 16;
|
|
31
|
-
let c0 = a0 * b0;
|
|
32
|
-
let c1 = c0 >>> 16;
|
|
33
|
-
c0 &= 65535;
|
|
34
|
-
c1 += a1 * b0;
|
|
35
|
-
let c2 = c1 >>> 16;
|
|
36
|
-
c1 &= 65535;
|
|
37
|
-
c1 += a0 * b1;
|
|
38
|
-
c2 += c1 >>> 16;
|
|
39
|
-
c1 &= 65535;
|
|
40
|
-
c2 += a2 * b0;
|
|
41
|
-
let c3 = c2 >>> 16;
|
|
42
|
-
c2 &= 65535;
|
|
43
|
-
c2 += a1 * b1;
|
|
44
|
-
c3 += c2 >>> 16;
|
|
45
|
-
c2 &= 65535;
|
|
46
|
-
c2 += a0 * b2;
|
|
47
|
-
c3 += c2 >>> 16;
|
|
48
|
-
c2 &= 65535;
|
|
49
|
-
c3 += a3 * b0 + a2 * b1 + a1 * b2 + a0 * b3;
|
|
50
|
-
c3 &= 65535;
|
|
51
|
-
RLO = (c1 << 16 | c0) >>> 0;
|
|
52
|
-
RHI = (c3 << 16 | c2) >>> 0;
|
|
53
|
-
}
|
|
54
|
-
function rotl64(lo, hi, r) {
|
|
55
|
-
if (r < 32) {
|
|
56
|
-
RLO = (lo << r | hi >>> 32 - r) >>> 0;
|
|
57
|
-
RHI = (hi << r | lo >>> 32 - r) >>> 0;
|
|
58
|
-
} else {
|
|
59
|
-
const s = r - 32;
|
|
60
|
-
RLO = (hi << s | lo >>> 32 - s) >>> 0;
|
|
61
|
-
RHI = (lo << s | hi >>> 32 - s) >>> 0;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function add64(alo, ahi, blo, bhi) {
|
|
65
|
-
const lo = (alo >>> 0) + (blo >>> 0);
|
|
66
|
-
RHI = ahi + bhi + (lo > 4294967295 ? 1 : 0) >>> 0;
|
|
67
|
-
RLO = lo >>> 0;
|
|
68
|
-
}
|
|
69
|
-
function fmix64(lo, hi) {
|
|
70
|
-
let l = lo;
|
|
71
|
-
let h = hi;
|
|
72
|
-
l = (l ^ h >>> 1) >>> 0;
|
|
73
|
-
mul64(l, h, 3981806797, 4283543511);
|
|
74
|
-
l = RLO;
|
|
75
|
-
h = RHI;
|
|
76
|
-
l = (l ^ h >>> 1) >>> 0;
|
|
77
|
-
mul64(l, h, 444984403, 3301882366);
|
|
78
|
-
l = RLO;
|
|
79
|
-
h = RHI;
|
|
80
|
-
l = (l ^ h >>> 1) >>> 0;
|
|
81
|
-
RLO = l;
|
|
82
|
-
RHI = h;
|
|
83
|
-
}
|
|
84
|
-
function computeLanes(bytes, seed, len) {
|
|
85
|
-
const nblocks = len >>> 4;
|
|
86
|
-
let h1lo = seed >>> 0;
|
|
87
|
-
let h1hi = 0;
|
|
88
|
-
let h2lo = seed >>> 0;
|
|
89
|
-
let h2hi = 0;
|
|
90
|
-
for (let i = 0; i < nblocks; i++) {
|
|
91
|
-
const b = i * 16;
|
|
92
|
-
let k1lo = ((bytes[b] ?? 0) | (bytes[b + 1] ?? 0) << 8 | (bytes[b + 2] ?? 0) << 16 | (bytes[b + 3] ?? 0) << 24) >>> 0;
|
|
93
|
-
let k1hi = ((bytes[b + 4] ?? 0) | (bytes[b + 5] ?? 0) << 8 | (bytes[b + 6] ?? 0) << 16 | (bytes[b + 7] ?? 0) << 24) >>> 0;
|
|
94
|
-
let k2lo = ((bytes[b + 8] ?? 0) | (bytes[b + 9] ?? 0) << 8 | (bytes[b + 10] ?? 0) << 16 | (bytes[b + 11] ?? 0) << 24) >>> 0;
|
|
95
|
-
let k2hi = ((bytes[b + 12] ?? 0) | (bytes[b + 13] ?? 0) << 8 | (bytes[b + 14] ?? 0) << 16 | (bytes[b + 15] ?? 0) << 24) >>> 0;
|
|
96
|
-
mul64(k1lo, k1hi, C1LO, C1HI);
|
|
97
|
-
rotl64(RLO, RHI, 31);
|
|
98
|
-
mul64(RLO, RHI, C2LO, C2HI);
|
|
99
|
-
k1lo = RLO;
|
|
100
|
-
k1hi = RHI;
|
|
101
|
-
h1lo ^= k1lo;
|
|
102
|
-
h1hi ^= k1hi;
|
|
103
|
-
rotl64(h1lo, h1hi, 27);
|
|
104
|
-
add64(RLO, RHI, h2lo, h2hi);
|
|
105
|
-
mul64(RLO, RHI, 5, 0);
|
|
106
|
-
add64(RLO, RHI, 1390208809, 0);
|
|
107
|
-
h1lo = RLO;
|
|
108
|
-
h1hi = RHI;
|
|
109
|
-
mul64(k2lo, k2hi, C2LO, C2HI);
|
|
110
|
-
rotl64(RLO, RHI, 33);
|
|
111
|
-
mul64(RLO, RHI, C1LO, C1HI);
|
|
112
|
-
k2lo = RLO;
|
|
113
|
-
k2hi = RHI;
|
|
114
|
-
h2lo ^= k2lo;
|
|
115
|
-
h2hi ^= k2hi;
|
|
116
|
-
rotl64(h2lo, h2hi, 31);
|
|
117
|
-
add64(RLO, RHI, h1lo, h1hi);
|
|
118
|
-
mul64(RLO, RHI, 5, 0);
|
|
119
|
-
add64(RLO, RHI, 944331445, 0);
|
|
120
|
-
h2lo = RLO;
|
|
121
|
-
h2hi = RHI;
|
|
122
|
-
}
|
|
123
|
-
let k1lo = 0;
|
|
124
|
-
let k1hi = 0;
|
|
125
|
-
let k2lo = 0;
|
|
126
|
-
let k2hi = 0;
|
|
127
|
-
const tail = nblocks * 16;
|
|
128
|
-
const rem = len & 15;
|
|
129
|
-
if (rem >= 15) k2hi ^= (bytes[tail + 14] ?? 0) << 16;
|
|
130
|
-
if (rem >= 14) k2hi ^= (bytes[tail + 13] ?? 0) << 8;
|
|
131
|
-
if (rem >= 13) k2hi ^= bytes[tail + 12] ?? 0;
|
|
132
|
-
if (rem >= 12) k2lo ^= (bytes[tail + 11] ?? 0) << 24;
|
|
133
|
-
if (rem >= 11) k2lo ^= (bytes[tail + 10] ?? 0) << 16;
|
|
134
|
-
if (rem >= 10) k2lo ^= (bytes[tail + 9] ?? 0) << 8;
|
|
135
|
-
if (rem >= 9) {
|
|
136
|
-
k2lo ^= bytes[tail + 8] ?? 0;
|
|
137
|
-
k2lo >>>= 0;
|
|
138
|
-
k2hi >>>= 0;
|
|
139
|
-
mul64(k2lo, k2hi, C2LO, C2HI);
|
|
140
|
-
rotl64(RLO, RHI, 33);
|
|
141
|
-
mul64(RLO, RHI, C1LO, C1HI);
|
|
142
|
-
h2lo ^= RLO;
|
|
143
|
-
h2hi ^= RHI;
|
|
144
|
-
}
|
|
145
|
-
if (rem >= 8) k1hi ^= (bytes[tail + 7] ?? 0) << 24;
|
|
146
|
-
if (rem >= 7) k1hi ^= (bytes[tail + 6] ?? 0) << 16;
|
|
147
|
-
if (rem >= 6) k1hi ^= (bytes[tail + 5] ?? 0) << 8;
|
|
148
|
-
if (rem >= 5) k1hi ^= bytes[tail + 4] ?? 0;
|
|
149
|
-
if (rem >= 4) k1lo ^= (bytes[tail + 3] ?? 0) << 24;
|
|
150
|
-
if (rem >= 3) k1lo ^= (bytes[tail + 2] ?? 0) << 16;
|
|
151
|
-
if (rem >= 2) k1lo ^= (bytes[tail + 1] ?? 0) << 8;
|
|
152
|
-
if (rem >= 1) {
|
|
153
|
-
k1lo ^= bytes[tail] ?? 0;
|
|
154
|
-
k1lo >>>= 0;
|
|
155
|
-
k1hi >>>= 0;
|
|
156
|
-
mul64(k1lo, k1hi, C1LO, C1HI);
|
|
157
|
-
rotl64(RLO, RHI, 31);
|
|
158
|
-
mul64(RLO, RHI, C2LO, C2HI);
|
|
159
|
-
h1lo ^= RLO;
|
|
160
|
-
h1hi ^= RHI;
|
|
161
|
-
}
|
|
162
|
-
h1lo = (h1lo ^ len) >>> 0;
|
|
163
|
-
h2lo = (h2lo ^ len) >>> 0;
|
|
164
|
-
add64(h1lo, h1hi, h2lo, h2hi);
|
|
165
|
-
h1lo = RLO;
|
|
166
|
-
h1hi = RHI;
|
|
167
|
-
add64(h2lo, h2hi, h1lo, h1hi);
|
|
168
|
-
h2lo = RLO;
|
|
169
|
-
h2hi = RHI;
|
|
170
|
-
fmix64(h1lo, h1hi);
|
|
171
|
-
h1lo = RLO;
|
|
172
|
-
h1hi = RHI;
|
|
173
|
-
fmix64(h2lo, h2hi);
|
|
174
|
-
h2lo = RLO;
|
|
175
|
-
h2hi = RHI;
|
|
176
|
-
add64(h1lo, h1hi, h2lo, h2hi);
|
|
177
|
-
h1lo = RLO;
|
|
178
|
-
h1hi = RHI;
|
|
179
|
-
add64(h2lo, h2hi, h1lo, h1hi);
|
|
180
|
-
h2lo = RLO;
|
|
181
|
-
h2hi = RHI;
|
|
182
|
-
LANES.h1lo = h1lo >>> 0;
|
|
183
|
-
LANES.h1hi = h1hi >>> 0;
|
|
184
|
-
LANES.h2lo = h2lo >>> 0;
|
|
185
|
-
LANES.h2hi = h2hi >>> 0;
|
|
186
|
-
}
|
|
187
|
-
const keyEncoder = new TextEncoder();
|
|
188
|
-
let keyBuf = /* @__PURE__ */ new Uint8Array(256);
|
|
189
|
-
function keyToLanes(key, seed) {
|
|
190
|
-
if (typeof key === "string") {
|
|
191
|
-
const cap = key.length * 3;
|
|
192
|
-
if (keyBuf.length < cap) keyBuf = new Uint8Array(cap);
|
|
193
|
-
const { written } = keyEncoder.encodeInto(key, keyBuf);
|
|
194
|
-
computeLanes(keyBuf, seed, written);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
const bytes = normalize(key);
|
|
198
|
-
computeLanes(bytes, seed, bytes.length);
|
|
199
|
-
}
|
|
200
|
-
function hash128KeyInto(key, seed, out) {
|
|
201
|
-
keyToLanes(key, seed);
|
|
202
|
-
out.h1lo = LANES.h1lo;
|
|
203
|
-
out.h1hi = LANES.h1hi;
|
|
204
|
-
out.h2lo = LANES.h2lo;
|
|
205
|
-
out.h2hi = LANES.h2hi;
|
|
206
|
-
}
|
|
207
|
-
function reduce(x, range) {
|
|
208
|
-
const xlo = x & 65535;
|
|
209
|
-
const xhi = x >>> 16;
|
|
210
|
-
const rlo = range & 65535;
|
|
211
|
-
const rhi = range >>> 16;
|
|
212
|
-
const lolo = xlo * rlo;
|
|
213
|
-
const lohi = xlo * rhi;
|
|
214
|
-
const hilo = xhi * rlo;
|
|
215
|
-
const hihi = xhi * rhi;
|
|
216
|
-
const carry = (lolo >>> 16) + (lohi & 65535) + (hilo & 65535);
|
|
217
|
-
return hihi + (lohi >>> 16) + (hilo >>> 16) + (carry >>> 16) >>> 0;
|
|
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.h1lo ^ LANES.h1hi) >>> 0;
|
|
227
|
-
const b = (LANES.h2lo ^ LANES.h2hi) >>> 0;
|
|
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/crc32.ts
|
|
232
|
-
let table;
|
|
233
|
-
function buildTable() {
|
|
234
|
-
const t = /* @__PURE__ */ new Uint32Array(256);
|
|
235
|
-
for (let n = 0; n < 256; n++) {
|
|
236
|
-
let c = n;
|
|
237
|
-
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
238
|
-
t[n] = c >>> 0;
|
|
239
|
-
}
|
|
240
|
-
return t;
|
|
241
|
-
}
|
|
242
|
-
function crc32(bytes) {
|
|
243
|
-
table ??= buildTable();
|
|
244
|
-
let crc = 4294967295;
|
|
245
|
-
for (const byte of bytes) crc = (table[(crc ^ byte) & 255] ?? 0) ^ crc >>> 8;
|
|
246
|
-
return (crc ^ 4294967295) >>> 0;
|
|
247
|
-
}
|
|
248
|
-
const HEADER_SIZE = 8;
|
|
249
|
-
const TRAILER_SIZE = 4;
|
|
250
|
-
function writeHeader(header, body) {
|
|
251
|
-
const frame = new Uint8Array(HEADER_SIZE + body.length + TRAILER_SIZE);
|
|
252
|
-
frame[0] = 65;
|
|
253
|
-
frame[1] = 77;
|
|
254
|
-
frame[2] = 81;
|
|
255
|
-
frame[3] = 70;
|
|
256
|
-
frame[4] = header.version;
|
|
257
|
-
frame[5] = header.type;
|
|
258
|
-
frame[6] = header.flags;
|
|
259
|
-
frame[7] = 0;
|
|
260
|
-
frame.set(body, HEADER_SIZE);
|
|
261
|
-
const crc = crc32(frame.subarray(0, HEADER_SIZE + body.length));
|
|
262
|
-
new DataView(frame.buffer, frame.byteOffset, frame.byteLength).setUint32(HEADER_SIZE + body.length, crc, true);
|
|
263
|
-
return frame;
|
|
264
|
-
}
|
|
265
|
-
var SerializationError = class extends Error {
|
|
266
|
-
name = "SerializationError";
|
|
267
|
-
};
|
|
268
|
-
var TruncatedError = class extends SerializationError {
|
|
269
|
-
name = "TruncatedError";
|
|
270
|
-
};
|
|
271
|
-
var BadMagicError = class extends SerializationError {
|
|
272
|
-
name = "BadMagicError";
|
|
273
|
-
};
|
|
274
|
-
var UnknownVersionError = class extends SerializationError {
|
|
275
|
-
name = "UnknownVersionError";
|
|
276
|
-
};
|
|
277
|
-
var ChecksumError = class extends SerializationError {
|
|
278
|
-
name = "ChecksumError";
|
|
279
|
-
};
|
|
280
|
-
/**
|
|
281
|
-
* Asserts a frame body is long enough to hold its fixed params block, so the
|
|
282
|
-
* params can be read without running off the end.
|
|
283
|
-
*/
|
|
284
|
-
function assertMinBodyLength(actual, min, context) {
|
|
285
|
-
if (actual < min) throw new TruncatedError(`${context}: body of ${String(actual)} bytes is shorter than the ${String(min)}-byte params block`);
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Asserts a frame body is exactly the length its declared params imply, so a
|
|
289
|
-
* hostile or truncated frame is rejected before any backing store is allocated.
|
|
290
|
-
*/
|
|
291
|
-
function assertBodyLength(actual, expected, context) {
|
|
292
|
-
if (actual !== expected) throw new TruncatedError(`${context}: body of ${String(actual)} bytes does not match the declared params (expected ${String(expected)})`);
|
|
293
|
-
}
|
|
294
|
-
function readHeader(frame) {
|
|
295
|
-
if (frame.length < 12) throw new TruncatedError(`frame of ${String(frame.length)} bytes is shorter than the minimum ${String(12)}`);
|
|
296
|
-
if (frame[0] !== 65 || frame[1] !== 77 || frame[2] !== 81 || frame[3] !== 70) throw new BadMagicError("frame does not start with the AMQF magic");
|
|
297
|
-
const version = frame[4] ?? 0;
|
|
298
|
-
if (version !== 2) throw new UnknownVersionError(`unsupported format version ${String(version)}`);
|
|
299
|
-
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");
|
|
300
|
-
return {
|
|
301
|
-
version,
|
|
302
|
-
type: frame[5] ?? 0,
|
|
303
|
-
flags: frame[6] ?? 0,
|
|
304
|
-
body: frame.subarray(HEADER_SIZE, frame.length - TRAILER_SIZE)
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
//#endregion
|
|
308
|
-
Object.defineProperty(exports, "SerializationError", {
|
|
309
|
-
enumerable: true,
|
|
310
|
-
get: function() {
|
|
311
|
-
return SerializationError;
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
Object.defineProperty(exports, "assertBodyLength", {
|
|
315
|
-
enumerable: true,
|
|
316
|
-
get: function() {
|
|
317
|
-
return assertBodyLength;
|
|
318
|
-
}
|
|
319
|
-
});
|
|
320
|
-
Object.defineProperty(exports, "assertMinBodyLength", {
|
|
321
|
-
enumerable: true,
|
|
322
|
-
get: function() {
|
|
323
|
-
return assertMinBodyLength;
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
Object.defineProperty(exports, "hash128KeyInto", {
|
|
327
|
-
enumerable: true,
|
|
328
|
-
get: function() {
|
|
329
|
-
return hash128KeyInto;
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
Object.defineProperty(exports, "probeInto", {
|
|
333
|
-
enumerable: true,
|
|
334
|
-
get: function() {
|
|
335
|
-
return probeInto;
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
Object.defineProperty(exports, "readHeader", {
|
|
339
|
-
enumerable: true,
|
|
340
|
-
get: function() {
|
|
341
|
-
return readHeader;
|
|
342
|
-
}
|
|
343
|
-
});
|
|
344
|
-
Object.defineProperty(exports, "reduce", {
|
|
345
|
-
enumerable: true,
|
|
346
|
-
get: function() {
|
|
347
|
-
return reduce;
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
Object.defineProperty(exports, "writeHeader", {
|
|
351
|
-
enumerable: true,
|
|
352
|
-
get: function() {
|
|
353
|
-
return writeHeader;
|
|
354
|
-
}
|
|
355
|
-
});
|
|
@@ -1,308 +0,0 @@
|
|
|
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
|
-
const C1LO = 289559509;
|
|
11
|
-
const C1HI = 2277735313;
|
|
12
|
-
const C2LO = 658871167;
|
|
13
|
-
const C2HI = 1291169091;
|
|
14
|
-
let RLO = 0;
|
|
15
|
-
let RHI = 0;
|
|
16
|
-
const LANES = {
|
|
17
|
-
h1lo: 0,
|
|
18
|
-
h1hi: 0,
|
|
19
|
-
h2lo: 0,
|
|
20
|
-
h2hi: 0
|
|
21
|
-
};
|
|
22
|
-
function mul64(alo, ahi, blo, bhi) {
|
|
23
|
-
const a0 = alo & 65535;
|
|
24
|
-
const a1 = alo >>> 16;
|
|
25
|
-
const a2 = ahi & 65535;
|
|
26
|
-
const a3 = ahi >>> 16;
|
|
27
|
-
const b0 = blo & 65535;
|
|
28
|
-
const b1 = blo >>> 16;
|
|
29
|
-
const b2 = bhi & 65535;
|
|
30
|
-
const b3 = bhi >>> 16;
|
|
31
|
-
let c0 = a0 * b0;
|
|
32
|
-
let c1 = c0 >>> 16;
|
|
33
|
-
c0 &= 65535;
|
|
34
|
-
c1 += a1 * b0;
|
|
35
|
-
let c2 = c1 >>> 16;
|
|
36
|
-
c1 &= 65535;
|
|
37
|
-
c1 += a0 * b1;
|
|
38
|
-
c2 += c1 >>> 16;
|
|
39
|
-
c1 &= 65535;
|
|
40
|
-
c2 += a2 * b0;
|
|
41
|
-
let c3 = c2 >>> 16;
|
|
42
|
-
c2 &= 65535;
|
|
43
|
-
c2 += a1 * b1;
|
|
44
|
-
c3 += c2 >>> 16;
|
|
45
|
-
c2 &= 65535;
|
|
46
|
-
c2 += a0 * b2;
|
|
47
|
-
c3 += c2 >>> 16;
|
|
48
|
-
c2 &= 65535;
|
|
49
|
-
c3 += a3 * b0 + a2 * b1 + a1 * b2 + a0 * b3;
|
|
50
|
-
c3 &= 65535;
|
|
51
|
-
RLO = (c1 << 16 | c0) >>> 0;
|
|
52
|
-
RHI = (c3 << 16 | c2) >>> 0;
|
|
53
|
-
}
|
|
54
|
-
function rotl64(lo, hi, r) {
|
|
55
|
-
if (r < 32) {
|
|
56
|
-
RLO = (lo << r | hi >>> 32 - r) >>> 0;
|
|
57
|
-
RHI = (hi << r | lo >>> 32 - r) >>> 0;
|
|
58
|
-
} else {
|
|
59
|
-
const s = r - 32;
|
|
60
|
-
RLO = (hi << s | lo >>> 32 - s) >>> 0;
|
|
61
|
-
RHI = (lo << s | hi >>> 32 - s) >>> 0;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function add64(alo, ahi, blo, bhi) {
|
|
65
|
-
const lo = (alo >>> 0) + (blo >>> 0);
|
|
66
|
-
RHI = ahi + bhi + (lo > 4294967295 ? 1 : 0) >>> 0;
|
|
67
|
-
RLO = lo >>> 0;
|
|
68
|
-
}
|
|
69
|
-
function fmix64(lo, hi) {
|
|
70
|
-
let l = lo;
|
|
71
|
-
let h = hi;
|
|
72
|
-
l = (l ^ h >>> 1) >>> 0;
|
|
73
|
-
mul64(l, h, 3981806797, 4283543511);
|
|
74
|
-
l = RLO;
|
|
75
|
-
h = RHI;
|
|
76
|
-
l = (l ^ h >>> 1) >>> 0;
|
|
77
|
-
mul64(l, h, 444984403, 3301882366);
|
|
78
|
-
l = RLO;
|
|
79
|
-
h = RHI;
|
|
80
|
-
l = (l ^ h >>> 1) >>> 0;
|
|
81
|
-
RLO = l;
|
|
82
|
-
RHI = h;
|
|
83
|
-
}
|
|
84
|
-
function computeLanes(bytes, seed, len) {
|
|
85
|
-
const nblocks = len >>> 4;
|
|
86
|
-
let h1lo = seed >>> 0;
|
|
87
|
-
let h1hi = 0;
|
|
88
|
-
let h2lo = seed >>> 0;
|
|
89
|
-
let h2hi = 0;
|
|
90
|
-
for (let i = 0; i < nblocks; i++) {
|
|
91
|
-
const b = i * 16;
|
|
92
|
-
let k1lo = ((bytes[b] ?? 0) | (bytes[b + 1] ?? 0) << 8 | (bytes[b + 2] ?? 0) << 16 | (bytes[b + 3] ?? 0) << 24) >>> 0;
|
|
93
|
-
let k1hi = ((bytes[b + 4] ?? 0) | (bytes[b + 5] ?? 0) << 8 | (bytes[b + 6] ?? 0) << 16 | (bytes[b + 7] ?? 0) << 24) >>> 0;
|
|
94
|
-
let k2lo = ((bytes[b + 8] ?? 0) | (bytes[b + 9] ?? 0) << 8 | (bytes[b + 10] ?? 0) << 16 | (bytes[b + 11] ?? 0) << 24) >>> 0;
|
|
95
|
-
let k2hi = ((bytes[b + 12] ?? 0) | (bytes[b + 13] ?? 0) << 8 | (bytes[b + 14] ?? 0) << 16 | (bytes[b + 15] ?? 0) << 24) >>> 0;
|
|
96
|
-
mul64(k1lo, k1hi, C1LO, C1HI);
|
|
97
|
-
rotl64(RLO, RHI, 31);
|
|
98
|
-
mul64(RLO, RHI, C2LO, C2HI);
|
|
99
|
-
k1lo = RLO;
|
|
100
|
-
k1hi = RHI;
|
|
101
|
-
h1lo ^= k1lo;
|
|
102
|
-
h1hi ^= k1hi;
|
|
103
|
-
rotl64(h1lo, h1hi, 27);
|
|
104
|
-
add64(RLO, RHI, h2lo, h2hi);
|
|
105
|
-
mul64(RLO, RHI, 5, 0);
|
|
106
|
-
add64(RLO, RHI, 1390208809, 0);
|
|
107
|
-
h1lo = RLO;
|
|
108
|
-
h1hi = RHI;
|
|
109
|
-
mul64(k2lo, k2hi, C2LO, C2HI);
|
|
110
|
-
rotl64(RLO, RHI, 33);
|
|
111
|
-
mul64(RLO, RHI, C1LO, C1HI);
|
|
112
|
-
k2lo = RLO;
|
|
113
|
-
k2hi = RHI;
|
|
114
|
-
h2lo ^= k2lo;
|
|
115
|
-
h2hi ^= k2hi;
|
|
116
|
-
rotl64(h2lo, h2hi, 31);
|
|
117
|
-
add64(RLO, RHI, h1lo, h1hi);
|
|
118
|
-
mul64(RLO, RHI, 5, 0);
|
|
119
|
-
add64(RLO, RHI, 944331445, 0);
|
|
120
|
-
h2lo = RLO;
|
|
121
|
-
h2hi = RHI;
|
|
122
|
-
}
|
|
123
|
-
let k1lo = 0;
|
|
124
|
-
let k1hi = 0;
|
|
125
|
-
let k2lo = 0;
|
|
126
|
-
let k2hi = 0;
|
|
127
|
-
const tail = nblocks * 16;
|
|
128
|
-
const rem = len & 15;
|
|
129
|
-
if (rem >= 15) k2hi ^= (bytes[tail + 14] ?? 0) << 16;
|
|
130
|
-
if (rem >= 14) k2hi ^= (bytes[tail + 13] ?? 0) << 8;
|
|
131
|
-
if (rem >= 13) k2hi ^= bytes[tail + 12] ?? 0;
|
|
132
|
-
if (rem >= 12) k2lo ^= (bytes[tail + 11] ?? 0) << 24;
|
|
133
|
-
if (rem >= 11) k2lo ^= (bytes[tail + 10] ?? 0) << 16;
|
|
134
|
-
if (rem >= 10) k2lo ^= (bytes[tail + 9] ?? 0) << 8;
|
|
135
|
-
if (rem >= 9) {
|
|
136
|
-
k2lo ^= bytes[tail + 8] ?? 0;
|
|
137
|
-
k2lo >>>= 0;
|
|
138
|
-
k2hi >>>= 0;
|
|
139
|
-
mul64(k2lo, k2hi, C2LO, C2HI);
|
|
140
|
-
rotl64(RLO, RHI, 33);
|
|
141
|
-
mul64(RLO, RHI, C1LO, C1HI);
|
|
142
|
-
h2lo ^= RLO;
|
|
143
|
-
h2hi ^= RHI;
|
|
144
|
-
}
|
|
145
|
-
if (rem >= 8) k1hi ^= (bytes[tail + 7] ?? 0) << 24;
|
|
146
|
-
if (rem >= 7) k1hi ^= (bytes[tail + 6] ?? 0) << 16;
|
|
147
|
-
if (rem >= 6) k1hi ^= (bytes[tail + 5] ?? 0) << 8;
|
|
148
|
-
if (rem >= 5) k1hi ^= bytes[tail + 4] ?? 0;
|
|
149
|
-
if (rem >= 4) k1lo ^= (bytes[tail + 3] ?? 0) << 24;
|
|
150
|
-
if (rem >= 3) k1lo ^= (bytes[tail + 2] ?? 0) << 16;
|
|
151
|
-
if (rem >= 2) k1lo ^= (bytes[tail + 1] ?? 0) << 8;
|
|
152
|
-
if (rem >= 1) {
|
|
153
|
-
k1lo ^= bytes[tail] ?? 0;
|
|
154
|
-
k1lo >>>= 0;
|
|
155
|
-
k1hi >>>= 0;
|
|
156
|
-
mul64(k1lo, k1hi, C1LO, C1HI);
|
|
157
|
-
rotl64(RLO, RHI, 31);
|
|
158
|
-
mul64(RLO, RHI, C2LO, C2HI);
|
|
159
|
-
h1lo ^= RLO;
|
|
160
|
-
h1hi ^= RHI;
|
|
161
|
-
}
|
|
162
|
-
h1lo = (h1lo ^ len) >>> 0;
|
|
163
|
-
h2lo = (h2lo ^ len) >>> 0;
|
|
164
|
-
add64(h1lo, h1hi, h2lo, h2hi);
|
|
165
|
-
h1lo = RLO;
|
|
166
|
-
h1hi = RHI;
|
|
167
|
-
add64(h2lo, h2hi, h1lo, h1hi);
|
|
168
|
-
h2lo = RLO;
|
|
169
|
-
h2hi = RHI;
|
|
170
|
-
fmix64(h1lo, h1hi);
|
|
171
|
-
h1lo = RLO;
|
|
172
|
-
h1hi = RHI;
|
|
173
|
-
fmix64(h2lo, h2hi);
|
|
174
|
-
h2lo = RLO;
|
|
175
|
-
h2hi = RHI;
|
|
176
|
-
add64(h1lo, h1hi, h2lo, h2hi);
|
|
177
|
-
h1lo = RLO;
|
|
178
|
-
h1hi = RHI;
|
|
179
|
-
add64(h2lo, h2hi, h1lo, h1hi);
|
|
180
|
-
h2lo = RLO;
|
|
181
|
-
h2hi = RHI;
|
|
182
|
-
LANES.h1lo = h1lo >>> 0;
|
|
183
|
-
LANES.h1hi = h1hi >>> 0;
|
|
184
|
-
LANES.h2lo = h2lo >>> 0;
|
|
185
|
-
LANES.h2hi = h2hi >>> 0;
|
|
186
|
-
}
|
|
187
|
-
const keyEncoder = new TextEncoder();
|
|
188
|
-
let keyBuf = /* @__PURE__ */ new Uint8Array(256);
|
|
189
|
-
function keyToLanes(key, seed) {
|
|
190
|
-
if (typeof key === "string") {
|
|
191
|
-
const cap = key.length * 3;
|
|
192
|
-
if (keyBuf.length < cap) keyBuf = new Uint8Array(cap);
|
|
193
|
-
const { written } = keyEncoder.encodeInto(key, keyBuf);
|
|
194
|
-
computeLanes(keyBuf, seed, written);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
const bytes = normalize(key);
|
|
198
|
-
computeLanes(bytes, seed, bytes.length);
|
|
199
|
-
}
|
|
200
|
-
function hash128KeyInto(key, seed, out) {
|
|
201
|
-
keyToLanes(key, seed);
|
|
202
|
-
out.h1lo = LANES.h1lo;
|
|
203
|
-
out.h1hi = LANES.h1hi;
|
|
204
|
-
out.h2lo = LANES.h2lo;
|
|
205
|
-
out.h2hi = LANES.h2hi;
|
|
206
|
-
}
|
|
207
|
-
function reduce(x, range) {
|
|
208
|
-
const xlo = x & 65535;
|
|
209
|
-
const xhi = x >>> 16;
|
|
210
|
-
const rlo = range & 65535;
|
|
211
|
-
const rhi = range >>> 16;
|
|
212
|
-
const lolo = xlo * rlo;
|
|
213
|
-
const lohi = xlo * rhi;
|
|
214
|
-
const hilo = xhi * rlo;
|
|
215
|
-
const hihi = xhi * rhi;
|
|
216
|
-
const carry = (lolo >>> 16) + (lohi & 65535) + (hilo & 65535);
|
|
217
|
-
return hihi + (lohi >>> 16) + (hilo >>> 16) + (carry >>> 16) >>> 0;
|
|
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.h1lo ^ LANES.h1hi) >>> 0;
|
|
227
|
-
const b = (LANES.h2lo ^ LANES.h2hi) >>> 0;
|
|
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/crc32.ts
|
|
232
|
-
let table;
|
|
233
|
-
function buildTable() {
|
|
234
|
-
const t = /* @__PURE__ */ new Uint32Array(256);
|
|
235
|
-
for (let n = 0; n < 256; n++) {
|
|
236
|
-
let c = n;
|
|
237
|
-
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
238
|
-
t[n] = c >>> 0;
|
|
239
|
-
}
|
|
240
|
-
return t;
|
|
241
|
-
}
|
|
242
|
-
function crc32(bytes) {
|
|
243
|
-
table ??= buildTable();
|
|
244
|
-
let crc = 4294967295;
|
|
245
|
-
for (const byte of bytes) crc = (table[(crc ^ byte) & 255] ?? 0) ^ crc >>> 8;
|
|
246
|
-
return (crc ^ 4294967295) >>> 0;
|
|
247
|
-
}
|
|
248
|
-
const HEADER_SIZE = 8;
|
|
249
|
-
const TRAILER_SIZE = 4;
|
|
250
|
-
function writeHeader(header, body) {
|
|
251
|
-
const frame = new Uint8Array(HEADER_SIZE + body.length + TRAILER_SIZE);
|
|
252
|
-
frame[0] = 65;
|
|
253
|
-
frame[1] = 77;
|
|
254
|
-
frame[2] = 81;
|
|
255
|
-
frame[3] = 70;
|
|
256
|
-
frame[4] = header.version;
|
|
257
|
-
frame[5] = header.type;
|
|
258
|
-
frame[6] = header.flags;
|
|
259
|
-
frame[7] = 0;
|
|
260
|
-
frame.set(body, HEADER_SIZE);
|
|
261
|
-
const crc = crc32(frame.subarray(0, HEADER_SIZE + body.length));
|
|
262
|
-
new DataView(frame.buffer, frame.byteOffset, frame.byteLength).setUint32(HEADER_SIZE + body.length, crc, true);
|
|
263
|
-
return frame;
|
|
264
|
-
}
|
|
265
|
-
var SerializationError = class extends Error {
|
|
266
|
-
name = "SerializationError";
|
|
267
|
-
};
|
|
268
|
-
var TruncatedError = class extends SerializationError {
|
|
269
|
-
name = "TruncatedError";
|
|
270
|
-
};
|
|
271
|
-
var BadMagicError = class extends SerializationError {
|
|
272
|
-
name = "BadMagicError";
|
|
273
|
-
};
|
|
274
|
-
var UnknownVersionError = class extends SerializationError {
|
|
275
|
-
name = "UnknownVersionError";
|
|
276
|
-
};
|
|
277
|
-
var ChecksumError = class extends SerializationError {
|
|
278
|
-
name = "ChecksumError";
|
|
279
|
-
};
|
|
280
|
-
/**
|
|
281
|
-
* Asserts a frame body is long enough to hold its fixed params block, so the
|
|
282
|
-
* params can be read without running off the end.
|
|
283
|
-
*/
|
|
284
|
-
function assertMinBodyLength(actual, min, context) {
|
|
285
|
-
if (actual < min) throw new TruncatedError(`${context}: body of ${String(actual)} bytes is shorter than the ${String(min)}-byte params block`);
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Asserts a frame body is exactly the length its declared params imply, so a
|
|
289
|
-
* hostile or truncated frame is rejected before any backing store is allocated.
|
|
290
|
-
*/
|
|
291
|
-
function assertBodyLength(actual, expected, context) {
|
|
292
|
-
if (actual !== expected) throw new TruncatedError(`${context}: body of ${String(actual)} bytes does not match the declared params (expected ${String(expected)})`);
|
|
293
|
-
}
|
|
294
|
-
function readHeader(frame) {
|
|
295
|
-
if (frame.length < 12) throw new TruncatedError(`frame of ${String(frame.length)} bytes is shorter than the minimum ${String(12)}`);
|
|
296
|
-
if (frame[0] !== 65 || frame[1] !== 77 || frame[2] !== 81 || frame[3] !== 70) throw new BadMagicError("frame does not start with the AMQF magic");
|
|
297
|
-
const version = frame[4] ?? 0;
|
|
298
|
-
if (version !== 2) throw new UnknownVersionError(`unsupported format version ${String(version)}`);
|
|
299
|
-
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");
|
|
300
|
-
return {
|
|
301
|
-
version,
|
|
302
|
-
type: frame[5] ?? 0,
|
|
303
|
-
flags: frame[6] ?? 0,
|
|
304
|
-
body: frame.subarray(HEADER_SIZE, frame.length - TRAILER_SIZE)
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
//#endregion
|
|
308
|
-
export { writeHeader as a, reduce as c, readHeader as i, assertBodyLength as n, hash128KeyInto as o, assertMinBodyLength as r, probeInto as s, SerializationError as t };
|