@matrixai/db
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -107,4 +107,4 @@ /// <reference types="node" /> | ||
*/ | ||
dump<V>(levelPath?: LevelPath, raw?: false, root?: boolean): Promise<Array<[string, V]>>; | ||
dump(levelPath: LevelPath | undefined, raw: true, root?: boolean): Promise<Array<[Buffer, Buffer]>>; | ||
dump<V>(levelPath?: LevelPath, raw?: false, root?: boolean): Promise<Array<[KeyPath, V]>>; | ||
dump(levelPath: LevelPath | undefined, raw: true, root?: boolean): Promise<Array<[KeyPath, Buffer]>>; | ||
serializeEncrypt(value: any, raw: false): Promise<Buffer>; | ||
@@ -111,0 +111,0 @@ serializeEncrypt(value: Buffer, raw: true): Promise<Buffer>; |
@@ -371,10 +371,6 @@ "use strict"; | ||
for await (const [keyPath, v] of this._iterator({ | ||
keyAsBuffer: true, | ||
keyAsBuffer: raw, | ||
valueAsBuffer: raw, | ||
}, levelPath)) { | ||
let k = utils.keyPathToKey(keyPath); | ||
if (!raw) { | ||
k = k.toString('utf-8'); | ||
} | ||
records.push([k, v]); | ||
records.push([keyPath, v]); | ||
} | ||
@@ -381,0 +377,0 @@ return records; |
@@ -79,4 +79,4 @@ /// <reference types="node" /> | ||
*/ | ||
dump<V>(levelPath?: LevelPath, raw?: false): Promise<Array<[string, V]>>; | ||
dump(levelPath: LevelPath | undefined, raw: true): Promise<Array<[Buffer, Buffer]>>; | ||
dump<V>(levelPath?: LevelPath, raw?: false): Promise<Array<[KeyPath, V]>>; | ||
dump(levelPath: LevelPath | undefined, raw: true): Promise<Array<[KeyPath, Buffer]>>; | ||
queueSuccess(f: () => any): void; | ||
@@ -83,0 +83,0 @@ queueFailure(f: (e?: Error) => any): void; |
@@ -10,7 +10,10 @@ /// <reference types="node" /> | ||
/** | ||
* Escape is a single `\` byte | ||
* This is used to escape the separator and literal `\` | ||
* Encode level or key part using base 128 encoding | ||
*/ | ||
declare const esc: Buffer; | ||
declare function encodePart(part: Buffer): Buffer; | ||
/** | ||
* Decode level or key part from base 128 | ||
*/ | ||
declare function decodePart(data: Buffer): Buffer; | ||
/** | ||
* Used to convert possible KeyPath into legal KeyPath | ||
@@ -30,14 +33,6 @@ */ | ||
* e.g. ['A', 'B'] => !A!!B! (where ! is the sep) | ||
* Level parts must not contain the separator | ||
* Level parts are allowed to contain the separator before encoding | ||
*/ | ||
declare function levelPathToKey(levelPath: LevelPath): Buffer; | ||
/** | ||
* Escapes the level and key parts for escape and separator | ||
*/ | ||
declare function escapePart(buf: Buffer): Buffer; | ||
/** | ||
* Unescapes the level and key part of escape and separator | ||
*/ | ||
declare function unescapePart(buf: Buffer): Buffer; | ||
/** | ||
* Converts key buffer back into KeyPath | ||
@@ -52,3 +47,2 @@ * e.g. !A!!B!C => ['A', 'B', 'C'] (where ! is the sep) | ||
* sep => 0x00 | ||
* escape => 0x5c | ||
* keyActual => .*:k -> [k] | ||
@@ -72,2 +66,2 @@ */ | ||
declare function fromArrayBuffer(b: ArrayBuffer, offset?: number, length?: number): Buffer; | ||
export { sep, esc, toKeyPath, keyPathToKey, levelPathToKey, escapePart, unescapePart, parseKey, sepExists, serialize, deserialize, toArrayBuffer, fromArrayBuffer, }; | ||
export { sep, encodePart, decodePart, toKeyPath, keyPathToKey, levelPathToKey, parseKey, sepExists, serialize, deserialize, toArrayBuffer, fromArrayBuffer, }; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fromArrayBuffer = exports.toArrayBuffer = exports.deserialize = exports.serialize = exports.sepExists = exports.parseKey = exports.unescapePart = exports.escapePart = exports.levelPathToKey = exports.keyPathToKey = exports.toKeyPath = exports.esc = exports.sep = void 0; | ||
exports.fromArrayBuffer = exports.toArrayBuffer = exports.deserialize = exports.serialize = exports.sepExists = exports.parseKey = exports.levelPathToKey = exports.keyPathToKey = exports.toKeyPath = exports.decodePart = exports.encodePart = exports.sep = void 0; | ||
const errors = __importStar(require("./errors")); | ||
@@ -37,8 +37,66 @@ /** | ||
/** | ||
* Escape is a single `\` byte | ||
* This is used to escape the separator and literal `\` | ||
* Lexicographically ordered base 128 alphabet | ||
*/ | ||
const esc = Buffer.from([92]); | ||
exports.esc = esc; | ||
const alphabet = Buffer.from(Array.from({ length: 128 }, (_, i) => { | ||
return i + 1; | ||
})); | ||
/** | ||
* Encode level or key part using base 128 encoding | ||
*/ | ||
function encodePart(part) { | ||
// Start encoding | ||
const mask = (1 << 7) - 1; | ||
const out = []; | ||
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); | ||
} | ||
exports.encodePart = encodePart; | ||
/** | ||
* Decode level or key part from base 128 | ||
*/ | ||
function decodePart(data) { | ||
const codes = {}; | ||
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); | ||
} | ||
exports.decodePart = decodePart; | ||
/** | ||
* Used to convert possible KeyPath into legal KeyPath | ||
@@ -71,3 +129,3 @@ */ | ||
levelPathToKey(levelPath), | ||
escapePart(typeof keyPart === 'string' ? Buffer.from(keyPart, 'utf-8') : keyPart), | ||
encodePart(typeof keyPart === 'string' ? Buffer.from(keyPart, 'utf-8') : keyPart), | ||
]); | ||
@@ -79,3 +137,3 @@ } | ||
* e.g. ['A', 'B'] => !A!!B! (where ! is the sep) | ||
* Level parts must not contain the separator | ||
* Level parts are allowed to contain the separator before encoding | ||
*/ | ||
@@ -85,3 +143,3 @@ function levelPathToKey(levelPath) { | ||
p = typeof p === 'string' ? Buffer.from(p, 'utf-8') : p; | ||
p = escapePart(p); | ||
p = encodePart(p); | ||
return Buffer.concat([sep, p, sep]); | ||
@@ -92,49 +150,2 @@ })); | ||
/** | ||
* Escapes the level and key parts for escape and separator | ||
*/ | ||
function escapePart(buf) { | ||
const bytes = []; | ||
for (let i = 0; i < buf.byteLength; i++) { | ||
const b = buf[i]; | ||
if (b === esc[0]) { | ||
bytes.push(esc[0], b); | ||
} | ||
else if (b === sep[0]) { | ||
bytes.push(esc[0], b); | ||
} | ||
else { | ||
bytes.push(b); | ||
} | ||
} | ||
return Buffer.from(bytes); | ||
} | ||
exports.escapePart = escapePart; | ||
/** | ||
* Unescapes the level and key part of escape and separator | ||
*/ | ||
function unescapePart(buf) { | ||
const bytes = []; | ||
for (let i = 0; i < buf.byteLength; i++) { | ||
const b = buf[i]; | ||
if (b === esc[0]) { | ||
const n = buf[i + 1]; | ||
if (n === esc[0]) { | ||
bytes.push(n); | ||
} | ||
else if (n === sep[0]) { | ||
bytes.push(n); | ||
} | ||
else { | ||
throw new SyntaxError('Invalid escape sequence'); | ||
} | ||
i++; | ||
} | ||
else { | ||
bytes.push(b); | ||
} | ||
} | ||
return Buffer.from(bytes); | ||
} | ||
exports.unescapePart = unescapePart; | ||
/** | ||
* Converts key buffer back into KeyPath | ||
@@ -149,3 +160,2 @@ * e.g. !A!!B!C => ['A', 'B', 'C'] (where ! is the sep) | ||
* sep => 0x00 | ||
* escape => 0x5c | ||
* keyActual => .*:k -> [k] | ||
@@ -159,3 +169,3 @@ */ | ||
for (let i = 0; i < bufs.length; i++) { | ||
bufs[i] = unescapePart(bufs[i]); | ||
bufs[i] = decodePart(bufs[i]); | ||
} | ||
@@ -217,13 +227,2 @@ return bufs; | ||
} | ||
else if (b === esc[0]) { | ||
const n = buf[i + 1]; | ||
// Even if undefined | ||
if (n !== esc[0] && n !== sep[0]) { | ||
throw new errors.ErrorDBParseKey('Invalid escape sequence'); | ||
} | ||
// Push the n | ||
levelBytes.push(b, n); | ||
// Skip the n | ||
i++; | ||
} | ||
else { | ||
@@ -230,0 +229,0 @@ levelBytes.push(b); |
{ | ||
"name": "@matrixai/db", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"author": "Roger Qiu", | ||
@@ -5,0 +5,0 @@ "description": "DB", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110519
1656