-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
413 lines (391 loc) · 10.7 KB
/
Copy pathutils.ts
File metadata and controls
413 lines (391 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import type { Callback, Merge, KeyPath, LevelPath } from './types.js';
import * as errors from './errors.js';
/**
* Separator is a single null byte
* This special symbol must not appear in the encoded parts
*/
const sep = Buffer.from([0x00]);
/**
* Empty parts will be encoded as a single 0x01 byte
*/
const empty = Buffer.from([0x01]);
/**
* Lexicographically ordered base 128 alphabet
* The alphabet starts at 0x02 (skipping 0x00 and 0x01)
*/
const alphabet = Buffer.from(
Array.from({ length: 128 }, (_, i) => {
return i + 2;
}),
);
/**
* Encode level or key part using base 128 encoding
* Empty parts are encoded with the special empty symbol
*/
function encodePart(part: Buffer): Buffer {
if (part.byteLength === 0) {
return empty;
}
// Start encoding
const mask = (1 << 7) - 1;
const out: Array<number> = [];
let bits = 0; // Number of bits currently in the buffer
let buffer = 0; // Bits waiting to be written out, MSB first
for (let i = 0; i < part.length; ++i) {
// Slurp data into the buffer
buffer = (buffer << 8) | part[i];
bits += 8;
// Write out as much as we can
while (bits > 7) {
bits -= 7;
out.push(alphabet[mask & (buffer >> bits)]);
}
}
// Partial character
if (bits) {
out.push(alphabet[mask & (buffer << (7 - bits))]);
}
return Buffer.from(out);
}
/**
* Decode level or key part from base 128
* The special empty symbol is decoded as an empty buffer
*/
function decodePart(data: Buffer): Buffer {
if (data.equals(empty)) {
return Buffer.allocUnsafe(0);
}
const codes: Record<number, number> = {};
for (let i = 0; i < alphabet.length; ++i) {
codes[alphabet[i]] = i;
}
// Allocate the output
const out = new Uint8Array(((data.length * 7) / 8) | 0);
// Parse the data
let bits = 0; // Number of bits currently in the buffer
let buffer = 0; // Bits waiting to be written out, MSB first
let written = 0; // Next byte to write
for (let i = 0; i < data.length; ++i) {
// Read one character from the input
const value = codes[data[i]];
if (value === undefined) {
throw new SyntaxError(`Non-Base128 character`);
}
// Append the bits to the buffer
buffer = (buffer << 7) | value;
bits += 7;
// Write out some bits if the buffer has a byte's worth
if (bits >= 8) {
bits -= 8;
out[written++] = 0xff & (buffer >> bits);
}
}
return Buffer.from(out);
}
/**
* Used to convert possible KeyPath into legal KeyPath
* Returns a copy which can be mutated
*/
function toKeyPath(keyPath: KeyPath | string | Buffer): KeyPath {
if (!Array.isArray(keyPath)) {
keyPath = [keyPath] as KeyPath;
}
if (keyPath.length < 1) {
keyPath = [''];
}
return keyPath;
}
/**
* Converts KeyPath to key buffer
* e.g. ['A', 'B'] => !A!B (where ! is the sep)
* An empty key path is converted to `['']`
* Level parts is allowed to contain the separator
* Key actual part is allowed to contain the separator
*/
function keyPathToKey(keyPath: KeyPath): Buffer {
if (keyPath.length < 1) {
keyPath = [''];
}
const keyPart = keyPath.slice(-1)[0];
const levelPath = keyPath.slice(0, -1);
return Buffer.concat([
levelPathToKey(levelPath),
encodePart(
typeof keyPart === 'string' ? Buffer.from(keyPart, 'utf-8') : keyPart,
),
]);
}
/**
* Converts LevelPath to key buffer
* e.g. ['A', 'B'] => !A!!B! (where ! is the sep)
* Level parts are allowed to contain the separator before encoding
*/
function levelPathToKey(levelPath: LevelPath): Buffer {
return Buffer.concat(
levelPath.map((p) => {
p = typeof p === 'string' ? Buffer.from(p, 'utf-8') : p;
p = encodePart(p);
return Buffer.concat([sep, p, sep]);
}),
);
}
/**
* Converts key buffer back into KeyPath
* e.g. !A!!B!C => ['A', 'B', 'C'] (where ! is the sep)
* Returned parts are always buffers
*
* BNF grammar of key buffer:
* path => levels:ls keyActual:k -> [...ls, k] | keyActual:k -> [k]
* levels => level:l levels:ls -> [l, ...ls] | '' -> []
* level => sep .*?:l sep -> l
* sep => 0x00
* keyActual => .*:k -> [k]
*/
function parseKey(key: Buffer): KeyPath {
const [bufs] = parsePath(key);
if (bufs.length < 1) {
throw new TypeError('Buffer is not a key');
}
for (let i = 0; i < bufs.length; i++) {
bufs[i] = decodePart(bufs[i]);
}
return bufs;
}
function parsePath(input: Buffer): [Array<Buffer>, Buffer] {
try {
let output: Array<Buffer> = [];
let input_: Buffer = input;
let output_: Array<Buffer>;
[output_, input_] = parseLevels(input_);
output = output.concat(output_);
[output_, input_] = parseKeyActual(input_);
output = output.concat(output_);
return [output, input_];
} catch {
let output: Array<Buffer> = [];
let input_: Buffer = input;
let output_: Array<Buffer>;
// eslint-disable-next-line prefer-const
[output_, input_] = parseKeyActual(input_);
output = output.concat(output_);
return [output, input_];
}
}
function parseLevels(
input: Buffer,
): [output: Array<Buffer>, remaining: Buffer] {
let output: Array<Buffer> = [];
try {
let input_: Buffer = input;
let output_: Array<Buffer>;
[output_, input_] = parseLevel(input_);
output = output.concat(output_);
[output_, input_] = parseLevels(input_);
output = output.concat(output_);
return [output, input_];
} catch {
return [[], input];
}
}
function parseLevel(input: Buffer): [Array<Buffer>, Buffer] {
const sepStart = input.indexOf(sep);
if (sepStart === -1) {
throw new errors.ErrorDBParseKey('Missing separator start');
}
let sepEnd: number | undefined;
const levelBytes: Array<number> = [];
const buf = input.subarray(sepStart + 1);
for (let i = 0; i < buf.byteLength; i++) {
const b = buf[i];
if (b === sep[0]) {
// Note that `buf` is a subarray offset from the input
// therefore the `sepEnd` must be offset by the same length
sepEnd = i + (sepStart + 1);
break;
} else {
levelBytes.push(b);
}
}
if (sepEnd == null) {
throw new errors.ErrorDBParseKey('Missing separator end');
}
const level = Buffer.from(levelBytes);
const remaining = input.subarray(sepEnd + 1);
return [[level], remaining];
}
function parseKeyActual(input: Buffer): [Array<Buffer>, Buffer] {
return [[input], input.subarray(input.byteLength)];
}
/**
* Checks if the separator exists in a string or buffer
* This only needs to applied to the LevelPath, not the final key
*/
function sepExists(data: string | Buffer): boolean {
if (typeof data === 'string') {
return data.includes(sep.toString('utf-8'));
} else {
return data.includes(sep);
}
}
function serialize<T>(value: T): Buffer {
return Buffer.from(JSON.stringify(value), 'utf-8');
}
function deserialize<T>(value_: Buffer): T {
try {
return JSON.parse(value_.toString('utf-8'));
} catch (e) {
if (e instanceof SyntaxError) {
throw new errors.ErrorDBParseValue(e.message, { cause: e });
}
throw e;
}
}
/**
* Slice-copies the Node Buffer to a new ArrayBuffer
*/
function toArrayBuffer(b: Buffer): ArrayBuffer {
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
}
/**
* Wraps ArrayBuffer in Node Buffer with zero copy
*/
function fromArrayBuffer(
b: ArrayBuffer,
offset?: number,
length?: number,
): Buffer {
return Buffer.from(b, offset, length);
}
/**
* Convert callback-style to promise-style
* If this is applied to overloaded function
* it will only choose one of the function signatures to use
*/
function promisify<
T extends Array<unknown>,
P extends Array<unknown>,
R extends T extends [] ? void : T extends [unknown] ? T[0] : T,
>(
f: (...args: [...params: P, callback: Callback<T>]) => unknown,
): (...params: P) => Promise<R> {
// Uses a regular function so that `this` can be bound
const g = function (...params: P): Promise<R> {
return new Promise((resolve, reject) => {
const callback = (error, ...values) => {
if (error != null) {
return reject(error);
}
if (values.length === 0) {
(resolve as () => void)();
} else if (values.length === 1) {
resolve(values[0] as R);
} else {
resolve(values as R);
}
return;
};
params.push(callback);
f.apply(this, params);
});
};
Object.defineProperty(g, 'name', { value: f.name });
return g;
}
/**
* Native addons expect strict optional properties
* Properties that have the value undefined may be misinterpreted
* Apply these to options objects before passing them to the native addon
*/
function filterUndefined(o: object): void {
Object.keys(o).forEach((k) => {
if (o[k] === undefined) {
delete o[k];
}
});
}
function iterationOptions<
O extends {
gt?: KeyPath | Buffer | string;
gte?: KeyPath | Buffer | string;
lt?: KeyPath | Buffer | string;
lte?: KeyPath | Buffer | string;
},
>(
options: O,
levelPath: LevelPath,
): Merge<
O,
{
gt?: Buffer;
gte?: Buffer;
lt?: Buffer;
lte?: Buffer;
keyEncoding: 'buffer';
valueEncoding: 'buffer';
}
> {
const options_ = {
...options,
// Internally we always use the buffer
keyEncoding: 'buffer' as const,
valueEncoding: 'buffer' as const,
} as Merge<
O,
{
gt?: Buffer;
gte?: Buffer;
lt?: Buffer;
lte?: Buffer;
keyEncoding: 'buffer';
valueEncoding: 'buffer';
}
>;
if (options?.gt != null) {
options_.gt = keyPathToKey(levelPath.concat(toKeyPath(options.gt)));
}
if (options?.gte != null) {
options_.gte = keyPathToKey(levelPath.concat(toKeyPath(options.gte)));
}
if (options?.gt == null && options?.gte == null) {
// If the level path is empty then all keys are allowed
if (levelPath.length > 0) {
options_.gt = levelPathToKey(levelPath);
}
}
if (options?.lt != null) {
options_.lt = keyPathToKey(levelPath.concat(toKeyPath(options.lt)));
}
if (options?.lte != null) {
options_.lte = keyPathToKey(levelPath.concat(toKeyPath(options.lte)));
}
if (options?.lt == null && options?.lte == null) {
// If the level path is empty then all keys are allowed
if (levelPath.length > 0) {
const levelKeyEnd = levelPathToKey(levelPath);
// This works because the separator byte is 0x00
// Therefore we have `sep level sep`
// and we can acquire keys less than `sep level sep+1`
levelKeyEnd[levelKeyEnd.length - 1] += 1;
options_.lt = levelKeyEnd;
}
}
filterUndefined(options_);
return options_;
}
export {
sep,
encodePart,
decodePart,
toKeyPath,
keyPathToKey,
levelPathToKey,
parseKey,
sepExists,
serialize,
deserialize,
toArrayBuffer,
fromArrayBuffer,
promisify,
filterUndefined,
iterationOptions,
};