distillate 0.9.0 → 0.10.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 +19 -0
- package/dist/blocked/index.cjs +14 -9
- package/dist/blocked/index.d.cts +7 -6
- package/dist/blocked/index.d.ts +7 -6
- package/dist/blocked/index.js +12 -8
- package/dist/bloom/index.cjs +14 -9
- package/dist/bloom/index.d.cts +5 -4
- package/dist/bloom/index.d.ts +5 -4
- package/dist/bloom/index.js +12 -8
- package/dist/bytes-DCuYtUVS.d.cts +4 -0
- package/dist/bytes-DCuYtUVS.d.ts +4 -0
- package/dist/frame/index.cjs +9 -0
- package/dist/frame/index.d.cts +2 -0
- package/dist/frame/index.d.ts +2 -0
- package/dist/frame/index.js +2 -0
- package/dist/fuse/index.cjs +26 -22
- package/dist/fuse/index.d.cts +7 -6
- package/dist/fuse/index.d.ts +7 -6
- package/dist/fuse/index.js +14 -11
- package/dist/hasher-D8LCBTOM.cjs +276 -0
- package/dist/hasher-DVWXS_vJ.js +229 -0
- package/dist/hll/index.cjs +12 -8
- package/dist/hll/index.d.cts +4 -3
- package/dist/hll/index.d.ts +4 -3
- package/dist/hll/index.js +11 -8
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -3
- package/dist/index.d.ts +2 -3
- package/dist/index.js +1 -1
- package/dist/{serialize-Bv1zWzrh.d.cts → serialize--xR6vGSW.d.cts} +44 -8
- package/dist/{serialize-Bv1zWzrh.d.ts → serialize--xR6vGSW.d.ts} +44 -8
- package/dist/serialize-CHikZ4GH.cjs +296 -0
- package/dist/serialize-CMSYFym-.js +201 -0
- package/package.json +17 -9
- package/dist/serialize-D59Ri81a.js +0 -392
- package/dist/serialize-Ul2LKFJU.cjs +0 -517
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
//#region src/core/base64.ts
|
|
2
|
+
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
3
|
+
const DECODE = new Int16Array(128).fill(-1);
|
|
4
|
+
for (let i = 0; i < 64; i++) DECODE[ALPHABET.charCodeAt(i)] = i;
|
|
5
|
+
const sym = (n) => ALPHABET.charAt(n & 63);
|
|
6
|
+
function toBase64(bytes) {
|
|
7
|
+
let out = "";
|
|
8
|
+
let i = 0;
|
|
9
|
+
for (; i + 3 <= bytes.length; i += 3) {
|
|
10
|
+
const n = (bytes[i] ?? 0) << 16 | (bytes[i + 1] ?? 0) << 8 | (bytes[i + 2] ?? 0);
|
|
11
|
+
out += sym(n >>> 18) + sym(n >>> 12) + sym(n >>> 6) + sym(n);
|
|
12
|
+
}
|
|
13
|
+
const rem = bytes.length - i;
|
|
14
|
+
if (rem === 1) {
|
|
15
|
+
const n = (bytes[i] ?? 0) << 16;
|
|
16
|
+
out += sym(n >>> 18) + sym(n >>> 12) + "==";
|
|
17
|
+
} else if (rem === 2) {
|
|
18
|
+
const n = (bytes[i] ?? 0) << 16 | (bytes[i + 1] ?? 0) << 8;
|
|
19
|
+
out += sym(n >>> 18) + sym(n >>> 12) + sym(n >>> 6) + "=";
|
|
20
|
+
}
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
function fromBase64(s) {
|
|
24
|
+
let len = s.length;
|
|
25
|
+
while (len > 0 && s.charCodeAt(len - 1) === 61) len--;
|
|
26
|
+
if (len % 4 === 1) throw new RangeError(`invalid base64 length ${String(len)}`);
|
|
27
|
+
const out = new Uint8Array(len * 3 >> 2);
|
|
28
|
+
let acc = 0;
|
|
29
|
+
let bits = 0;
|
|
30
|
+
let o = 0;
|
|
31
|
+
for (let i = 0; i < len; i++) {
|
|
32
|
+
const v = DECODE[s.charCodeAt(i)] ?? -1;
|
|
33
|
+
if (v < 0) throw new RangeError(`invalid base64 character at index ${String(i)}`);
|
|
34
|
+
acc = acc << 6 | v;
|
|
35
|
+
bits += 6;
|
|
36
|
+
if (bits >= 8) {
|
|
37
|
+
bits -= 8;
|
|
38
|
+
out[o++] = acc >>> bits & 255;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/core/crc32.ts
|
|
45
|
+
const T1 = 256;
|
|
46
|
+
const T2 = 512;
|
|
47
|
+
const T3 = 768;
|
|
48
|
+
const T4 = 1024;
|
|
49
|
+
const T5 = 1280;
|
|
50
|
+
const T6 = 1536;
|
|
51
|
+
const T7 = 1792;
|
|
52
|
+
const at = (a, i) => a[i] ?? 0;
|
|
53
|
+
let tables;
|
|
54
|
+
function buildTables() {
|
|
55
|
+
const t = new Uint32Array(2048);
|
|
56
|
+
for (let n = 0; n < 256; n++) {
|
|
57
|
+
let c = n;
|
|
58
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
59
|
+
t[n] = c >>> 0;
|
|
60
|
+
}
|
|
61
|
+
for (let k = 1; k < 8; k++) for (let n = 0; n < 256; n++) {
|
|
62
|
+
const p = at(t, (k - 1) * 256 + n);
|
|
63
|
+
t[k * 256 + n] = (at(t, p & 255) ^ p >>> 8) >>> 0;
|
|
64
|
+
}
|
|
65
|
+
return t;
|
|
66
|
+
}
|
|
67
|
+
function crc32(bytes) {
|
|
68
|
+
const t = tables ??= buildTables();
|
|
69
|
+
const len = bytes.length;
|
|
70
|
+
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
71
|
+
let crc = 4294967295;
|
|
72
|
+
let i = 0;
|
|
73
|
+
const last = len - 8;
|
|
74
|
+
while (i <= last) {
|
|
75
|
+
const one = (dv.getUint32(i, true) ^ crc) >>> 0;
|
|
76
|
+
const two = dv.getUint32(i + 4, true);
|
|
77
|
+
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;
|
|
78
|
+
i += 8;
|
|
79
|
+
}
|
|
80
|
+
for (; i < len; i++) crc = (at(t, (crc ^ dv.getUint8(i)) & 255) ^ crc >>> 8) >>> 0;
|
|
81
|
+
return (crc ^ 4294967295) >>> 0;
|
|
82
|
+
}
|
|
83
|
+
const MAGIC = Uint8Array.of(68, 83, 84, 76);
|
|
84
|
+
const HEADER_SIZE = 16;
|
|
85
|
+
const BODY_LENGTH_OFFSET = 8;
|
|
86
|
+
const RESERVED_BYTE_OFFSET = 7;
|
|
87
|
+
const RESERVED_WORD_OFFSET = 12;
|
|
88
|
+
const TRAILER_SIZE = 4;
|
|
89
|
+
const PAYLOAD_ALIGNMENT = 8;
|
|
90
|
+
function writeFrame(header, paramsSize, payloadLength, fill) {
|
|
91
|
+
if ((HEADER_SIZE + paramsSize) % PAYLOAD_ALIGNMENT !== 0) throw new RangeError(`a ${String(paramsSize)}-byte params block puts the payload at ${String(HEADER_SIZE + paramsSize)}, which is not a multiple of ${String(PAYLOAD_ALIGNMENT)}`);
|
|
92
|
+
const bodyLength = paramsSize + payloadLength;
|
|
93
|
+
const frame = new Uint8Array(HEADER_SIZE + bodyLength + TRAILER_SIZE);
|
|
94
|
+
const frameView = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
95
|
+
frame.set(MAGIC, 0);
|
|
96
|
+
frame[4] = header.version;
|
|
97
|
+
frame[5] = header.type;
|
|
98
|
+
frame[6] = header.flags;
|
|
99
|
+
frame[RESERVED_BYTE_OFFSET] = 0;
|
|
100
|
+
frameView.setUint32(BODY_LENGTH_OFFSET, bodyLength, true);
|
|
101
|
+
fill(frame.subarray(HEADER_SIZE, HEADER_SIZE + bodyLength), new DataView(frame.buffer, HEADER_SIZE, bodyLength));
|
|
102
|
+
const crc = crc32(frame.subarray(0, HEADER_SIZE + bodyLength));
|
|
103
|
+
frameView.setUint32(HEADER_SIZE + bodyLength, crc, true);
|
|
104
|
+
return frame;
|
|
105
|
+
}
|
|
106
|
+
var SerializationError = class extends Error {
|
|
107
|
+
name = "SerializationError";
|
|
108
|
+
};
|
|
109
|
+
var TruncatedError = class extends SerializationError {
|
|
110
|
+
name = "TruncatedError";
|
|
111
|
+
};
|
|
112
|
+
var BadMagicError = class extends SerializationError {
|
|
113
|
+
name = "BadMagicError";
|
|
114
|
+
};
|
|
115
|
+
var UnknownVersionError = class extends SerializationError {
|
|
116
|
+
name = "UnknownVersionError";
|
|
117
|
+
};
|
|
118
|
+
var UnknownHashVariantError = class extends SerializationError {
|
|
119
|
+
name = "UnknownHashVariantError";
|
|
120
|
+
};
|
|
121
|
+
var ReservedBitsError = class extends SerializationError {
|
|
122
|
+
name = "ReservedBitsError";
|
|
123
|
+
};
|
|
124
|
+
var ChecksumError = class extends SerializationError {
|
|
125
|
+
name = "ChecksumError";
|
|
126
|
+
};
|
|
127
|
+
const JSON_TAG = "distillate";
|
|
128
|
+
function toJSONEnvelope(bytes) {
|
|
129
|
+
return {
|
|
130
|
+
$: JSON_TAG,
|
|
131
|
+
v: 5,
|
|
132
|
+
data: toBase64(bytes)
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function fromJSONEnvelope(value) {
|
|
136
|
+
if (value === null || typeof value !== "object") throw new SerializationError("not a distillate filter JSON object");
|
|
137
|
+
const o = value;
|
|
138
|
+
if (o.$ !== JSON_TAG) throw new SerializationError(`expected "$":"${JSON_TAG}"`);
|
|
139
|
+
if (o.v !== 5) throw new UnknownVersionError(`unsupported JSON version ${String(o.v)}, expected ${String(5)}`);
|
|
140
|
+
if (typeof o.data !== "string") throw new SerializationError("missing string \"data\"");
|
|
141
|
+
try {
|
|
142
|
+
return fromBase64(o.data);
|
|
143
|
+
} catch {
|
|
144
|
+
throw new SerializationError("envelope data is not valid base64");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function bytesEqual(a, b) {
|
|
148
|
+
if (a.length !== b.length) return false;
|
|
149
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
function assertMinBodyLength(actual, min, context) {
|
|
153
|
+
if (actual < min) throw new TruncatedError(`${context}: body of ${String(actual)} bytes is shorter than the ${String(min)}-byte params block`);
|
|
154
|
+
}
|
|
155
|
+
function assertParamsPadding(body, fieldsEnd, paramsSize, context) {
|
|
156
|
+
for (let at = fieldsEnd; at < paramsSize; at++) if (body[at] !== 0) throw new SerializationError(`${context}: params padding at byte ${String(at)} is not zero`);
|
|
157
|
+
}
|
|
158
|
+
function assertBodyLength(actual, expected, context) {
|
|
159
|
+
if (actual !== expected) throw new TruncatedError(`${context}: body of ${String(actual)} bytes does not match the declared params (expected ${String(expected)})`);
|
|
160
|
+
}
|
|
161
|
+
function readFrameAt(bytes, offset) {
|
|
162
|
+
if (!Number.isInteger(offset) || offset < 0) throw new RangeError(`frame offset must be a non-negative integer, got ${String(offset)}`);
|
|
163
|
+
return readFrame(bytes, offset, false);
|
|
164
|
+
}
|
|
165
|
+
function readFrame(bytes, offset, exact) {
|
|
166
|
+
const available = bytes.length - offset;
|
|
167
|
+
if (available < 20) throw new TruncatedError(`${String(available)} bytes remain at offset ${String(offset)}, fewer than the minimum frame of ${String(20)}`);
|
|
168
|
+
const frame = bytes.subarray(offset);
|
|
169
|
+
for (let i = 0; i < MAGIC.length; i++) if (frame[i] !== MAGIC[i]) throw new BadMagicError("frame does not start with the DSTL magic");
|
|
170
|
+
const version = frame[4] ?? 0;
|
|
171
|
+
if (version !== 5) throw new UnknownVersionError(`unsupported format version ${String(version)}`);
|
|
172
|
+
const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
173
|
+
const bodyLength = view.getUint32(BODY_LENGTH_OFFSET, true);
|
|
174
|
+
const byteLength = HEADER_SIZE + bodyLength + TRAILER_SIZE;
|
|
175
|
+
if (exact ? byteLength !== available : byteLength > available) throw new TruncatedError(`frame declares a ${String(bodyLength)}-byte body but ${String(available - HEADER_SIZE - TRAILER_SIZE)} remain`);
|
|
176
|
+
const trailerAt = byteLength - TRAILER_SIZE;
|
|
177
|
+
if (view.getUint32(trailerAt, true) !== crc32(frame.subarray(0, trailerAt))) throw new ChecksumError("frame CRC32 does not match its contents");
|
|
178
|
+
const flags = frame[6] ?? 0;
|
|
179
|
+
if ((flags & 240) !== 0) throw new ReservedBitsError(`frame sets reserved flags bits 4-7 (flags 0x${flags.toString(16)})`);
|
|
180
|
+
if (frame[RESERVED_BYTE_OFFSET] !== 0) throw new ReservedBitsError(`frame sets reserved byte 7 (0x${(frame[RESERVED_BYTE_OFFSET] ?? 0).toString(16)})`);
|
|
181
|
+
const reservedWord = view.getUint32(RESERVED_WORD_OFFSET, true);
|
|
182
|
+
if (reservedWord !== 0) throw new ReservedBitsError(`frame sets the reserved u32 at offset 12 (0x${reservedWord.toString(16)})`);
|
|
183
|
+
return {
|
|
184
|
+
version,
|
|
185
|
+
type: frame[5] ?? 0,
|
|
186
|
+
flags,
|
|
187
|
+
body: frame.subarray(HEADER_SIZE, HEADER_SIZE + bodyLength),
|
|
188
|
+
byteLength
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function readHeader(frame) {
|
|
192
|
+
const { version, type, flags, body } = readFrame(frame, 0, true);
|
|
193
|
+
return {
|
|
194
|
+
version,
|
|
195
|
+
type,
|
|
196
|
+
flags,
|
|
197
|
+
body
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
//#endregion
|
|
201
|
+
export { TruncatedError as a, assertBodyLength as c, bytesEqual as d, fromJSONEnvelope as f, writeFrame as g, toJSONEnvelope as h, SerializationError as i, assertMinBodyLength as l, readHeader as m, ChecksumError as n, UnknownHashVariantError as o, readFrameAt as p, ReservedBitsError as r, UnknownVersionError as s, BadMagicError as t, assertParamsPadding as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "distillate",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Probabilistic data structures for JavaScript. Space-efficient, approximate-membership filters (Bloom, Blocked Bloom, Binary Fuse) with tunable error and a portable binary format; zero dependencies, universal.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bloom-filter",
|
|
@@ -63,6 +63,14 @@
|
|
|
63
63
|
"import": "./dist/hll/index.js",
|
|
64
64
|
"require": "./dist/hll/index.cjs"
|
|
65
65
|
},
|
|
66
|
+
"./frame": {
|
|
67
|
+
"types": {
|
|
68
|
+
"import": "./dist/frame/index.d.ts",
|
|
69
|
+
"require": "./dist/frame/index.d.cts"
|
|
70
|
+
},
|
|
71
|
+
"import": "./dist/frame/index.js",
|
|
72
|
+
"require": "./dist/frame/index.cjs"
|
|
73
|
+
},
|
|
66
74
|
"./package.json": "./package.json"
|
|
67
75
|
},
|
|
68
76
|
"main": "./dist/index.cjs",
|
|
@@ -73,17 +81,17 @@
|
|
|
73
81
|
],
|
|
74
82
|
"devDependencies": {
|
|
75
83
|
"@arethetypeswrong/cli": "^0.18.5",
|
|
76
|
-
"@microsoft/api-extractor": "^7.
|
|
77
|
-
"@types/node": "^22.
|
|
78
|
-
"@vitest/coverage-v8": "^
|
|
79
|
-
"fast-check": "^4.
|
|
84
|
+
"@microsoft/api-extractor": "^7.59.1",
|
|
85
|
+
"@types/node": "^22.20.3",
|
|
86
|
+
"@vitest/coverage-v8": "^5.0.1",
|
|
87
|
+
"fast-check": "^4.10.1",
|
|
80
88
|
"mitata": "^1.0.34",
|
|
81
|
-
"publint": "^0.3.
|
|
82
|
-
"tsdown": "^0.
|
|
83
|
-
"tsx": "^4.23.
|
|
89
|
+
"publint": "^0.3.24",
|
|
90
|
+
"tsdown": "^0.23.0",
|
|
91
|
+
"tsx": "^4.23.13",
|
|
84
92
|
"typedoc": "^0.28.20",
|
|
85
93
|
"typescript": "^5.9.3",
|
|
86
|
-
"vitest": "^
|
|
94
|
+
"vitest": "^5.0.1"
|
|
87
95
|
},
|
|
88
96
|
"engines": {
|
|
89
97
|
"node": ">=22"
|
|
@@ -1,392 +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
|
-
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 = 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
|
-
function probeInto(key, count, range, seed, out) {
|
|
220
|
-
keyToLanes(key, seed);
|
|
221
|
-
const a = LANES.w0;
|
|
222
|
-
const b = LANES.w1;
|
|
223
|
-
for (let i = 0; i < count; i++) out[i] = reduce(a + Math.imul(i, b) + i * i >>> 0, range);
|
|
224
|
-
}
|
|
225
|
-
//#endregion
|
|
226
|
-
//#region src/core/base64.ts
|
|
227
|
-
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
228
|
-
const DECODE = new Int16Array(128).fill(-1);
|
|
229
|
-
for (let i = 0; i < 64; i++) DECODE[ALPHABET.charCodeAt(i)] = i;
|
|
230
|
-
const sym = (n) => ALPHABET.charAt(n & 63);
|
|
231
|
-
function toBase64(bytes) {
|
|
232
|
-
let out = "";
|
|
233
|
-
let i = 0;
|
|
234
|
-
for (; i + 3 <= bytes.length; i += 3) {
|
|
235
|
-
const n = (bytes[i] ?? 0) << 16 | (bytes[i + 1] ?? 0) << 8 | (bytes[i + 2] ?? 0);
|
|
236
|
-
out += sym(n >>> 18) + sym(n >>> 12) + sym(n >>> 6) + sym(n);
|
|
237
|
-
}
|
|
238
|
-
const rem = bytes.length - i;
|
|
239
|
-
if (rem === 1) {
|
|
240
|
-
const n = (bytes[i] ?? 0) << 16;
|
|
241
|
-
out += sym(n >>> 18) + sym(n >>> 12) + "==";
|
|
242
|
-
} else if (rem === 2) {
|
|
243
|
-
const n = (bytes[i] ?? 0) << 16 | (bytes[i + 1] ?? 0) << 8;
|
|
244
|
-
out += sym(n >>> 18) + sym(n >>> 12) + sym(n >>> 6) + "=";
|
|
245
|
-
}
|
|
246
|
-
return out;
|
|
247
|
-
}
|
|
248
|
-
function fromBase64(s) {
|
|
249
|
-
let len = s.length;
|
|
250
|
-
while (len > 0 && s.charCodeAt(len - 1) === 61) len--;
|
|
251
|
-
if (len % 4 === 1) throw new RangeError(`invalid base64 length ${String(len)}`);
|
|
252
|
-
const out = new Uint8Array(len * 3 >> 2);
|
|
253
|
-
let acc = 0;
|
|
254
|
-
let bits = 0;
|
|
255
|
-
let o = 0;
|
|
256
|
-
for (let i = 0; i < len; i++) {
|
|
257
|
-
const v = DECODE[s.charCodeAt(i)] ?? -1;
|
|
258
|
-
if (v < 0) throw new RangeError(`invalid base64 character at index ${String(i)}`);
|
|
259
|
-
acc = acc << 6 | v;
|
|
260
|
-
bits += 6;
|
|
261
|
-
if (bits >= 8) {
|
|
262
|
-
bits -= 8;
|
|
263
|
-
out[o++] = acc >>> bits & 255;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return out;
|
|
267
|
-
}
|
|
268
|
-
//#endregion
|
|
269
|
-
//#region src/core/crc32.ts
|
|
270
|
-
const T1 = 256;
|
|
271
|
-
const T2 = 512;
|
|
272
|
-
const T3 = 768;
|
|
273
|
-
const T4 = 1024;
|
|
274
|
-
const T5 = 1280;
|
|
275
|
-
const T6 = 1536;
|
|
276
|
-
const T7 = 1792;
|
|
277
|
-
const at = (a, i) => a[i] ?? 0;
|
|
278
|
-
let tables;
|
|
279
|
-
function buildTables() {
|
|
280
|
-
const t = new Uint32Array(8 * 256);
|
|
281
|
-
for (let n = 0; n < 256; n++) {
|
|
282
|
-
let c = n;
|
|
283
|
-
for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
284
|
-
t[n] = c >>> 0;
|
|
285
|
-
}
|
|
286
|
-
for (let k = 1; k < 8; k++) for (let n = 0; n < 256; n++) {
|
|
287
|
-
const p = at(t, (k - 1) * 256 + n);
|
|
288
|
-
t[k * 256 + n] = (at(t, p & 255) ^ p >>> 8) >>> 0;
|
|
289
|
-
}
|
|
290
|
-
return t;
|
|
291
|
-
}
|
|
292
|
-
function crc32(bytes) {
|
|
293
|
-
const t = tables ??= buildTables();
|
|
294
|
-
const len = bytes.length;
|
|
295
|
-
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
296
|
-
let crc = 4294967295;
|
|
297
|
-
let i = 0;
|
|
298
|
-
const last = len - 8;
|
|
299
|
-
while (i <= last) {
|
|
300
|
-
const one = (dv.getUint32(i, true) ^ crc) >>> 0;
|
|
301
|
-
const two = dv.getUint32(i + 4, true);
|
|
302
|
-
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;
|
|
303
|
-
i += 8;
|
|
304
|
-
}
|
|
305
|
-
for (; i < len; i++) crc = (at(t, (crc ^ dv.getUint8(i)) & 255) ^ crc >>> 8) >>> 0;
|
|
306
|
-
return (crc ^ 4294967295) >>> 0;
|
|
307
|
-
}
|
|
308
|
-
const MAGIC = Uint8Array.of(68, 83, 84, 76);
|
|
309
|
-
const HEADER_SIZE = 16;
|
|
310
|
-
const BODY_LENGTH_OFFSET = 8;
|
|
311
|
-
const TRAILER_SIZE = 4;
|
|
312
|
-
function writeFrame(header, bodyLength, fill) {
|
|
313
|
-
const frame = new Uint8Array(HEADER_SIZE + bodyLength + TRAILER_SIZE);
|
|
314
|
-
const frameView = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
315
|
-
frame.set(MAGIC, 0);
|
|
316
|
-
frame[4] = header.version;
|
|
317
|
-
frame[5] = header.type;
|
|
318
|
-
frame[6] = header.flags;
|
|
319
|
-
frame[7] = 0;
|
|
320
|
-
frameView.setUint32(BODY_LENGTH_OFFSET, bodyLength, true);
|
|
321
|
-
fill(frame.subarray(HEADER_SIZE, HEADER_SIZE + bodyLength), new DataView(frame.buffer, HEADER_SIZE, bodyLength));
|
|
322
|
-
const crc = crc32(frame.subarray(0, HEADER_SIZE + bodyLength));
|
|
323
|
-
frameView.setUint32(HEADER_SIZE + bodyLength, crc, true);
|
|
324
|
-
return frame;
|
|
325
|
-
}
|
|
326
|
-
var SerializationError = class extends Error {
|
|
327
|
-
name = "SerializationError";
|
|
328
|
-
};
|
|
329
|
-
var TruncatedError = class extends SerializationError {
|
|
330
|
-
name = "TruncatedError";
|
|
331
|
-
};
|
|
332
|
-
var BadMagicError = class extends SerializationError {
|
|
333
|
-
name = "BadMagicError";
|
|
334
|
-
};
|
|
335
|
-
var UnknownVersionError = class extends SerializationError {
|
|
336
|
-
name = "UnknownVersionError";
|
|
337
|
-
};
|
|
338
|
-
var UnknownHashVariantError = class extends SerializationError {
|
|
339
|
-
name = "UnknownHashVariantError";
|
|
340
|
-
};
|
|
341
|
-
var ChecksumError = class extends SerializationError {
|
|
342
|
-
name = "ChecksumError";
|
|
343
|
-
};
|
|
344
|
-
const JSON_TAG = "distillate";
|
|
345
|
-
function toJSONEnvelope(bytes) {
|
|
346
|
-
return {
|
|
347
|
-
$: JSON_TAG,
|
|
348
|
-
v: 4,
|
|
349
|
-
data: toBase64(bytes)
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
function fromJSONEnvelope(value) {
|
|
353
|
-
if (value === null || typeof value !== "object") throw new SerializationError("not a distillate filter JSON object");
|
|
354
|
-
const o = value;
|
|
355
|
-
if (o.$ !== JSON_TAG) throw new SerializationError(`expected "$":"${JSON_TAG}"`);
|
|
356
|
-
if (o.v !== 4) throw new UnknownVersionError(`unsupported JSON version ${String(o.v)}, expected ${String(4)}`);
|
|
357
|
-
if (typeof o.data !== "string") throw new SerializationError("missing string \"data\"");
|
|
358
|
-
try {
|
|
359
|
-
return fromBase64(o.data);
|
|
360
|
-
} catch {
|
|
361
|
-
throw new SerializationError("envelope data is not valid base64");
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
function bytesEqual(a, b) {
|
|
365
|
-
if (a.length !== b.length) return false;
|
|
366
|
-
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
367
|
-
return true;
|
|
368
|
-
}
|
|
369
|
-
function assertMinBodyLength(actual, min, context) {
|
|
370
|
-
if (actual < min) throw new TruncatedError(`${context}: body of ${String(actual)} bytes is shorter than the ${String(min)}-byte params block`);
|
|
371
|
-
}
|
|
372
|
-
function assertBodyLength(actual, expected, context) {
|
|
373
|
-
if (actual !== expected) throw new TruncatedError(`${context}: body of ${String(actual)} bytes does not match the declared params (expected ${String(expected)})`);
|
|
374
|
-
}
|
|
375
|
-
function readHeader(frame) {
|
|
376
|
-
if (frame.length < 20) throw new TruncatedError(`frame of ${String(frame.length)} bytes is shorter than the minimum ${String(20)}`);
|
|
377
|
-
for (let i = 0; i < MAGIC.length; i++) if (frame[i] !== MAGIC[i]) throw new BadMagicError("frame does not start with the DSTL magic");
|
|
378
|
-
const version = frame[4] ?? 0;
|
|
379
|
-
if (version !== 4) throw new UnknownVersionError(`unsupported format version ${String(version)}`);
|
|
380
|
-
const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
381
|
-
const bodyLength = view.getUint32(BODY_LENGTH_OFFSET, true);
|
|
382
|
-
if (HEADER_SIZE + bodyLength + TRAILER_SIZE !== frame.length) throw new TruncatedError(`frame declares a ${String(bodyLength)}-byte body but holds ${String(frame.length - HEADER_SIZE - TRAILER_SIZE)}`);
|
|
383
|
-
if (view.getUint32(frame.length - TRAILER_SIZE, true) !== crc32(frame.subarray(0, frame.length - TRAILER_SIZE))) throw new ChecksumError("frame CRC32 does not match its contents");
|
|
384
|
-
return {
|
|
385
|
-
version,
|
|
386
|
-
type: frame[5] ?? 0,
|
|
387
|
-
flags: frame[6] ?? 0,
|
|
388
|
-
body: frame.subarray(HEADER_SIZE, HEADER_SIZE + bodyLength)
|
|
389
|
-
};
|
|
390
|
-
}
|
|
391
|
-
//#endregion
|
|
392
|
-
export { hash128KeyInto as _, UnknownHashVariantError as a, probeInto as b, assertMinBodyLength as c, readHeader as d, toJSONEnvelope as f, fmix64 as g, RLO as h, TruncatedError as i, bytesEqual as l, RHI as m, ChecksumError as n, UnknownVersionError as o, writeFrame as p, SerializationError as r, assertBodyLength as s, BadMagicError as t, fromJSONEnvelope as u, hash32x2Into as v, reduce as x, mul64 as y };
|