Primeiro commit do projeto Angular
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/ordered-binary.iml" filepath="$PROJECT_DIR$/.idea/ordered-binary.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.idea" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/dist" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{}
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"parent":null,"pid":240712,"argv":["/home/kzyp/.nvm/versions/node/v20.0.0/bin/node","/home/kzyp/dev/ordered-binary/node_modules/mocha/bin/mocha","--ui","tdd","--reporter","/home/kzyp/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/222.4167.35.plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js","/home/kzyp/dev/ordered-binary/tests/test.js"],"execArgv":[],"cwd":"/home/kzyp/dev/ordered-binary","time":1688411366364,"ppid":240701,"coverageFilename":"/home/kzyp/dev/ordered-binary/.nyc_output/5b6bd808-0399-4ee6-9395-344688761dfa.json","externalId":"","uuid":"5b6bd808-0399-4ee6-9395-344688761dfa","files":[]}
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"processes":{"5b6bd808-0399-4ee6-9395-344688761dfa":{"parent":null,"children":[]}},"files":{},"externalIds":{}}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Kris Zyp
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
[](https://www.npmjs.org/package/ordered-binary)
|
||||
[](https://www.npmjs.org/package/ordered-binary)
|
||||
[](LICENSE)
|
||||
|
||||
The ordered-binary package provides a representation of JavaScript primitives, serialized into binary format (NodeJS Buffers or Uint8Arrays), such that the binary values are naturally ordered such that it matches the natural ordering or values. For example, since -2.0321 > -2.04, then `toBufferKey(-2.0321)` will be greater than `toBufferKey(-2.04)` as a binary representation, in left-to-right evaluation. This is particular useful for storing keys as binaries with something like LMDB or LevelDB, to avoid any custom sorting.
|
||||
|
||||
The ordered-binary package supports strings, numbers, booleans, symbols, null, as well as an array of primitives. Here is an example of ordering of primitive values:
|
||||
```
|
||||
Buffer.from([0]) // buffers are left unchanged, and this is the minimum value
|
||||
Symbol.for('even symbols')
|
||||
-10 // negative supported
|
||||
-1.1 // decimals supported
|
||||
400
|
||||
3E10
|
||||
'Hello'
|
||||
['Hello', 'World']
|
||||
'World'
|
||||
'hello'
|
||||
['hello', 1, 'world']
|
||||
['hello', 'world']
|
||||
Buffer.from([0xff])
|
||||
```
|
||||
|
||||
|
||||
The main module exports these functions:
|
||||
|
||||
`writeKey(key: string | number | boolean | null | Array, target: Buffer, position: integer, inSequence?: boolean)` - Writes the provide key to the target buffer
|
||||
|
||||
`readKey(buffer, start, end, inSequence)` - Reads the key from the buffer, given the provided start and end, as a primitive value
|
||||
|
||||
`toBufferKey(jsPrimitive)` - This accepts a string, number, or boolean as the argument, and returns a `Buffer`.
|
||||
|
||||
`fromBufferKey(bufferKey, multiple)` - This accepts a Buffer and returns a JavaScript primitive value. This can also parse buffers that hold multiple values delimited by a byte `30`, by setting the second argument to true (in which case it will return an array).
|
||||
|
||||
And these constants:
|
||||
|
||||
`MINIMUM_KEY` - The minimum key supported (`null`, which is represented as single zero byte)
|
||||
`MAXIMUM_KEY` - A maximum key larger than any supported primitive (single 0xff byte)
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
+459
@@ -0,0 +1,459 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
/*
|
||||
control character types:
|
||||
1 - metadata
|
||||
2 - symbols
|
||||
6 - false
|
||||
7 - true
|
||||
8- 16 - negative doubles
|
||||
16-24 positive doubles
|
||||
27 - String starts with a character 27 or less or is an empty string
|
||||
0 - multipart separator
|
||||
> 27 normal string characters
|
||||
*/
|
||||
/*
|
||||
* Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
|
||||
*/
|
||||
|
||||
const float64Array = new Float64Array(2);
|
||||
const int32Array = new Int32Array(float64Array.buffer, 0, 4);
|
||||
const {lowIdx, highIdx} = (() => {
|
||||
if (new Uint8Array(new Uint32Array([0xFFEE1100]).buffer)[0] === 0xFF) {
|
||||
return { lowIdx: 1, highIdx: 0 };
|
||||
}
|
||||
return { lowIdx: 0, highIdx: 1 };
|
||||
})();
|
||||
let nullTerminate = false;
|
||||
let textEncoder;
|
||||
try {
|
||||
textEncoder = new TextEncoder();
|
||||
} catch (error) {}
|
||||
|
||||
/*
|
||||
* Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
|
||||
*/
|
||||
function writeKey(key, target, position, inSequence) {
|
||||
let targetView = target.dataView;
|
||||
if (!targetView)
|
||||
targetView = target.dataView = new DataView(target.buffer, target.byteOffset, ((target.byteLength + 3) >> 2) << 2);
|
||||
switch (typeof key) {
|
||||
case 'string':
|
||||
let strLength = key.length;
|
||||
let c1 = key.charCodeAt(0);
|
||||
if (!(c1 >= 28)) // escape character
|
||||
target[position++] = 27;
|
||||
if (strLength < 0x40) {
|
||||
let i, c2;
|
||||
for (i = 0; i < strLength; i++) {
|
||||
c1 = key.charCodeAt(i);
|
||||
if (c1 <= 4) {
|
||||
target[position++] = 4;
|
||||
target[position++] = c1;
|
||||
} else if (c1 < 0x80) {
|
||||
target[position++] = c1;
|
||||
} else if (c1 < 0x800) {
|
||||
target[position++] = c1 >> 6 | 0xc0;
|
||||
target[position++] = c1 & 0x3f | 0x80;
|
||||
} else if (
|
||||
(c1 & 0xfc00) === 0xd800 &&
|
||||
((c2 = key.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
||||
) {
|
||||
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
||||
i++;
|
||||
target[position++] = c1 >> 18 | 0xf0;
|
||||
target[position++] = c1 >> 12 & 0x3f | 0x80;
|
||||
target[position++] = c1 >> 6 & 0x3f | 0x80;
|
||||
target[position++] = c1 & 0x3f | 0x80;
|
||||
} else {
|
||||
target[position++] = c1 >> 12 | 0xe0;
|
||||
target[position++] = c1 >> 6 & 0x3f | 0x80;
|
||||
target[position++] = c1 & 0x3f | 0x80;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (target.utf8Write)
|
||||
position += target.utf8Write(key, position, target.byteLength - position);
|
||||
else
|
||||
position += textEncoder.encodeInto(key, target.subarray(position)).written;
|
||||
if (position > target.length - 4)
|
||||
throw new RangeError('String does not fit in target buffer')
|
||||
}
|
||||
break
|
||||
case 'number':
|
||||
float64Array[0] = key;
|
||||
let lowInt = int32Array[lowIdx];
|
||||
let highInt = int32Array[highIdx];
|
||||
let length;
|
||||
if (key < 0) {
|
||||
targetView.setInt32(position + 4, ~((lowInt >>> 4) | (highInt << 28)));
|
||||
targetView.setInt32(position + 0, (highInt ^ 0x7fffffff) >>> 4);
|
||||
targetView.setInt32(position + 8, ((lowInt & 0xf) ^ 0xf) << 4, true); // just always do the null termination here
|
||||
return position + 9
|
||||
} else if ((lowInt & 0xf) || inSequence) {
|
||||
length = 9;
|
||||
} else if (lowInt & 0xfffff)
|
||||
length = 8;
|
||||
else if (lowInt || (highInt & 0xf))
|
||||
length = 6;
|
||||
else
|
||||
length = 4;
|
||||
// switching order to go to little endian
|
||||
targetView.setInt32(position + 0, (highInt >>> 4) | 0x10000000);
|
||||
targetView.setInt32(position + 4, (lowInt >>> 4) | (highInt << 28));
|
||||
// if (length == 9 || nullTerminate)
|
||||
targetView.setInt32(position + 8, (lowInt & 0xf) << 4, true);
|
||||
return position + length;
|
||||
case 'object':
|
||||
if (key) {
|
||||
if (Array.isArray(key)) {
|
||||
for (let i = 0, l = key.length; i < l; i++) {
|
||||
if (i > 0)
|
||||
target[position++] = 0;
|
||||
position = writeKey(key[i], target, position, true);
|
||||
}
|
||||
break
|
||||
} else if (key instanceof Uint8Array) {
|
||||
target.set(key, position);
|
||||
position += key.length;
|
||||
break
|
||||
} else {
|
||||
throw new Error('Unable to serialize object as a key: ' + JSON.stringify(key))
|
||||
}
|
||||
} else // null
|
||||
target[position++] = 0;
|
||||
break
|
||||
case 'boolean':
|
||||
targetView.setUint32(position++, key ? 7 : 6, true);
|
||||
return position
|
||||
case 'bigint':
|
||||
let asFloat = Number(key);
|
||||
if (BigInt(asFloat) > key) {
|
||||
float64Array[0] = asFloat;
|
||||
if (asFloat > 0) {
|
||||
if (int32Array[lowIdx])
|
||||
int32Array[lowIdx]--;
|
||||
else {
|
||||
int32Array[highIdx]--;
|
||||
int32Array[lowIdx] = 0xffffffff;
|
||||
}
|
||||
} else {
|
||||
if (int32Array[lowIdx] < 0xffffffff)
|
||||
int32Array[lowIdx]++;
|
||||
else {
|
||||
int32Array[highIdx]++;
|
||||
int32Array[lowIdx] = 0;
|
||||
}
|
||||
}
|
||||
asFloat = float64Array[0];
|
||||
}
|
||||
let difference = key - BigInt(asFloat);
|
||||
if (difference === 0n)
|
||||
return writeKey(asFloat, target, position, inSequence)
|
||||
writeKey(asFloat, target, position, inSequence);
|
||||
position += 9; // always increment by 9 if we are adding fractional bits
|
||||
let exponent = BigInt((int32Array[highIdx] >> 20 & 0x7ff) - 1079);
|
||||
let nextByte = difference >> exponent;
|
||||
target[position - 1] |= Number(nextByte);
|
||||
difference -= nextByte << exponent;
|
||||
let first = true;
|
||||
while (difference || first) {
|
||||
first = false;
|
||||
exponent -= 7n;
|
||||
let nextByte = difference >> exponent;
|
||||
target[position++] = Number(nextByte) | 0x80;
|
||||
difference -= nextByte << exponent;
|
||||
}
|
||||
return position;
|
||||
case 'undefined':
|
||||
return position
|
||||
// undefined is interpreted as the absence of a key, signified by zero length
|
||||
case 'symbol':
|
||||
target[position++] = 2;
|
||||
return writeKey(key.description, target, position, inSequence)
|
||||
default:
|
||||
throw new Error('Can not serialize key of type ' + typeof key)
|
||||
}
|
||||
if (nullTerminate && !inSequence)
|
||||
targetView.setUint32(position, 0);
|
||||
return position
|
||||
}
|
||||
|
||||
let position;
|
||||
function readKey(buffer, start, end, inSequence) {
|
||||
position = start;
|
||||
let controlByte = buffer[position];
|
||||
let value;
|
||||
if (controlByte < 24) {
|
||||
if (controlByte < 8) {
|
||||
position++;
|
||||
if (controlByte == 6) {
|
||||
value = false;
|
||||
} else if (controlByte == 7) {
|
||||
value = true;
|
||||
} else if (controlByte == 0) {
|
||||
value = null;
|
||||
} else if (controlByte == 2) {
|
||||
value = Symbol.for(readStringSafely(buffer, end));
|
||||
} else
|
||||
return Uint8Array.prototype.slice.call(buffer, start, end)
|
||||
} else {
|
||||
let dataView;
|
||||
try {
|
||||
dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2));
|
||||
} catch(error) {
|
||||
// if it is write at the end of the ArrayBuffer, we may need to retry with the exact remaining bytes
|
||||
dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.buffer.byteLength - buffer.byteOffset));
|
||||
}
|
||||
|
||||
let highInt = dataView.getInt32(position) << 4;
|
||||
let size = end - position;
|
||||
let lowInt;
|
||||
if (size > 4) {
|
||||
lowInt = position + 8 <= buffer.length ? dataView.getInt32(position + 4) : (
|
||||
buffer[position + 4] << 24 |
|
||||
buffer[position + 5] << 16 |
|
||||
buffer[position + 6] << 8 |
|
||||
buffer[position + 7]
|
||||
);
|
||||
highInt |= lowInt >>> 28;
|
||||
if (size <= 6) { // clear the last bits
|
||||
lowInt &= -0x10000;
|
||||
}
|
||||
lowInt = lowInt << 4;
|
||||
if (size > 8) {
|
||||
lowInt = lowInt | buffer[position + 8] >> 4;
|
||||
}
|
||||
} else
|
||||
lowInt = 0;
|
||||
if (controlByte < 16) {
|
||||
// negative gets negated
|
||||
highInt = highInt ^ 0x7fffffff;
|
||||
lowInt = ~lowInt;
|
||||
}
|
||||
int32Array[highIdx] = highInt;
|
||||
int32Array[lowIdx] = lowInt;
|
||||
value = float64Array[0];
|
||||
position += 9;
|
||||
if (size > 9 && buffer[position] > 0) {
|
||||
// convert the float to bigint, and then we will add precision as we enumerate through the
|
||||
// extra bytes
|
||||
value = BigInt(value);
|
||||
let exponent = highInt >> 20 & 0x7ff;
|
||||
let next_byte = buffer[position - 1] & 0xf;
|
||||
value += BigInt(next_byte) << BigInt(exponent - 1079);
|
||||
while ((next_byte = buffer[position]) > 0 && position++ < end) {
|
||||
value += BigInt(next_byte & 0x7f) << BigInt((start - position) * 7 + exponent - 1016);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (controlByte == 27) {
|
||||
position++;
|
||||
}
|
||||
value = readStringSafely(buffer, end);
|
||||
if (position < end) position--; // if have a null terminator for the string, count that as the array separator
|
||||
}
|
||||
while (position < end) {
|
||||
if (buffer[position] === 0)
|
||||
position++;
|
||||
if (inSequence) {
|
||||
encoder.position = position;
|
||||
return value
|
||||
}
|
||||
let nextValue = readKey(buffer, position, end, true);
|
||||
if (value instanceof Array) {
|
||||
value.push(nextValue);
|
||||
} else
|
||||
value = [ value, nextValue ];
|
||||
}
|
||||
return value
|
||||
}
|
||||
const enableNullTermination = () => nullTerminate = true;
|
||||
|
||||
const encoder = {
|
||||
writeKey,
|
||||
readKey,
|
||||
enableNullTermination,
|
||||
};
|
||||
let targetBuffer = [];
|
||||
let targetPosition = 0;
|
||||
const hasNodeBuffer = typeof Buffer !== 'undefined';
|
||||
const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array;
|
||||
const toBufferKey = (key) => {
|
||||
let newBuffer;
|
||||
if (targetPosition + 100 > targetBuffer.length) {
|
||||
targetBuffer = new ByteArrayAllocate(8192);
|
||||
targetPosition = 0;
|
||||
newBuffer = true;
|
||||
}
|
||||
try {
|
||||
let result = targetBuffer.slice(targetPosition, targetPosition = writeKey(key, targetBuffer, targetPosition));
|
||||
if (targetPosition > targetBuffer.length) {
|
||||
if (newBuffer)
|
||||
throw new Error('Key is too large')
|
||||
return toBufferKey(key)
|
||||
}
|
||||
return result
|
||||
} catch(error) {
|
||||
if (newBuffer)
|
||||
throw error
|
||||
targetPosition = targetBuffer.length;
|
||||
return toBufferKey(key)
|
||||
}
|
||||
};
|
||||
const fromBufferKey = (sourceBuffer) => {
|
||||
return readKey(sourceBuffer, 0, sourceBuffer.length)
|
||||
};
|
||||
const fromCharCode = String.fromCharCode;
|
||||
function makeStringBuilder() {
|
||||
let stringBuildCode = '(source) => {';
|
||||
let previous = [];
|
||||
for (let i = 0; i < 0x30; i++) {
|
||||
let v = fromCharCode((i & 0xf) + 97) + fromCharCode((i >> 4) + 97);
|
||||
stringBuildCode += `
|
||||
let ${v} = source[position++]
|
||||
if (${v} > 4) {
|
||||
if (${v} >= 0x80) ${v} = finishUtf8(${v}, source)
|
||||
} else {
|
||||
if (${v} === 4)
|
||||
${v} = source[position++]
|
||||
else
|
||||
return fromCharCode(${previous})
|
||||
}
|
||||
`;
|
||||
previous.push(v);
|
||||
if (i == 1000000) // this just exists to prevent rollup from doing dead code elimination on finishUtf8
|
||||
finishUtf8();
|
||||
}
|
||||
stringBuildCode += `return fromCharCode(${previous}) + readString(source)}`;
|
||||
return stringBuildCode
|
||||
}
|
||||
|
||||
let pendingSurrogate;
|
||||
function finishUtf8(byte1, src) {
|
||||
if ((byte1 & 0xe0) === 0xc0) {
|
||||
// 2 bytes
|
||||
const byte2 = src[position++] & 0x3f;
|
||||
return ((byte1 & 0x1f) << 6) | byte2
|
||||
} else if ((byte1 & 0xf0) === 0xe0) {
|
||||
// 3 bytes
|
||||
const byte2 = src[position++] & 0x3f;
|
||||
const byte3 = src[position++] & 0x3f;
|
||||
return ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3
|
||||
} else if ((byte1 & 0xf8) === 0xf0) {
|
||||
// 4 bytes
|
||||
if (pendingSurrogate) {
|
||||
byte1 = pendingSurrogate;
|
||||
pendingSurrogate = null;
|
||||
position += 3;
|
||||
return byte1
|
||||
}
|
||||
const byte2 = src[position++] & 0x3f;
|
||||
const byte3 = src[position++] & 0x3f;
|
||||
const byte4 = src[position++] & 0x3f;
|
||||
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
||||
if (unit > 0xffff) {
|
||||
pendingSurrogate = 0xdc00 | (unit & 0x3ff);
|
||||
unit = (((unit - 0x10000) >>> 10) & 0x3ff) | 0xd800;
|
||||
position -= 4; // reset so we can return the next part of the surrogate pair
|
||||
}
|
||||
return unit
|
||||
} else {
|
||||
return byte1
|
||||
}
|
||||
}
|
||||
|
||||
const readString =
|
||||
typeof process !== 'undefined' && process.isBun ? // the eval in bun doesn't properly closure on position, so we
|
||||
// have to manually update it
|
||||
(function(reading) {
|
||||
let { setPosition, getPosition, readString } = reading;
|
||||
return (source) => {
|
||||
setPosition(position);
|
||||
let value = readString(source);
|
||||
position = getPosition();
|
||||
return value;
|
||||
};
|
||||
})((new Function('fromCharCode', 'let position; let readString = ' + makeStringBuilder() +
|
||||
';return {' +
|
||||
'setPosition(p) { position = p },' +
|
||||
'getPosition() { return position },' +
|
||||
'readString }'))(fromCharCode)) :
|
||||
eval(makeStringBuilder());
|
||||
function readStringSafely(source, end) {
|
||||
if (source[end] > 0) {
|
||||
let previous = source[end];
|
||||
try {
|
||||
// read string expects a null terminator, that is a 0 or undefined from reading past the end of the buffer, so we
|
||||
// have to ensure that, but do so safely, restoring the buffer to its original state
|
||||
source[end] = 0;
|
||||
return readString(source)
|
||||
} finally {
|
||||
source[end] = previous;
|
||||
}
|
||||
} else return readString(source);
|
||||
}
|
||||
function compareKeys(a, b) {
|
||||
// compare with type consistency that matches binary comparison
|
||||
if (typeof a == 'object') {
|
||||
if (!a) {
|
||||
return b == null ? 0 : -1
|
||||
}
|
||||
if (a.compare) {
|
||||
if (b == null) {
|
||||
return 1
|
||||
} else if (b.compare) {
|
||||
return a.compare(b)
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
let arrayComparison;
|
||||
if (b instanceof Array) {
|
||||
let i = 0;
|
||||
while((arrayComparison = compareKeys(a[i], b[i])) == 0 && i <= a.length) {
|
||||
i++;
|
||||
}
|
||||
return arrayComparison
|
||||
}
|
||||
arrayComparison = compareKeys(a[0], b);
|
||||
if (arrayComparison == 0 && a.length > 1)
|
||||
return 1
|
||||
return arrayComparison
|
||||
} else if (typeof a == typeof b) {
|
||||
if (typeof a === 'symbol') {
|
||||
a = Symbol.keyFor(a);
|
||||
b = Symbol.keyFor(b);
|
||||
}
|
||||
return a < b ? -1 : a === b ? 0 : 1
|
||||
}
|
||||
else if (typeof b == 'object') {
|
||||
if (b instanceof Array)
|
||||
return -compareKeys(b, a)
|
||||
return 1
|
||||
} else {
|
||||
return typeOrder[typeof a] < typeOrder[typeof b] ? -1 : 1
|
||||
}
|
||||
}
|
||||
const typeOrder = {
|
||||
symbol: 0,
|
||||
undefined: 1,
|
||||
boolean: 2,
|
||||
number: 3,
|
||||
string: 4
|
||||
};
|
||||
const MINIMUM_KEY = null;
|
||||
const MAXIMUM_KEY = new Uint8Array([0xff]);
|
||||
|
||||
exports.MAXIMUM_KEY = MAXIMUM_KEY;
|
||||
exports.MINIMUM_KEY = MINIMUM_KEY;
|
||||
exports.compareKeys = compareKeys;
|
||||
exports.enableNullTermination = enableNullTermination;
|
||||
exports.encoder = encoder;
|
||||
exports.fromBufferKey = fromBufferKey;
|
||||
exports.readKey = readKey;
|
||||
exports.toBufferKey = toBufferKey;
|
||||
exports.writeKey = writeKey;
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
type Key = Key[] | string | symbol | number | boolean | Uint8Array;
|
||||
/** Writes a key (a primitive value) to the target buffer, starting at the given position */
|
||||
export function writeKey(key: Key, target: Uint8Array, position: number, inSequence?: boolean): number;
|
||||
/** Reads a key from the provided buffer, from the given range */
|
||||
export function readKey(buffer: Uint8Array, start: number, end?: number, inSequence?: boolean): Key;
|
||||
/** Converts key to a Buffer. This is generally much slower than using writeKey since it involves a full buffer allocation, and should be avoided for performance sensitive code. */
|
||||
export function toBufferKey(key: Key): Buffer;
|
||||
/** Converts Buffer to Key */
|
||||
export function fromBufferKey(source: Buffer): Key;
|
||||
/** Compares two keys, returning -1 if `a` comes before `b` in the ordered binary representation of the keys, or 1 if `a` comes after `b`, or 0 if they are equivalent */
|
||||
export function compareKeys(a: Key, b: Key): number;
|
||||
/** The minimum key, with the "first" binary representation (one byte of zero) */
|
||||
export const MINIMUM_KEY: null
|
||||
/** A maximum key, with a binary representation after all other JS primitives (one byte of 0xff) */
|
||||
export const MAXIMUM_KEY: Uint8Array
|
||||
/** Enables null termination, ensuring that writing keys to buffers will end with a padding of zeros at the end to complete the following 32-bit word */
|
||||
export function enableNullTermination(): void;
|
||||
/** An object that holds the functions for encapsulation as a single encoder */
|
||||
export const encoder: {
|
||||
writeKey: typeof writeKey,
|
||||
readKey: typeof readKey,
|
||||
enableNullTermination: typeof enableNullTermination,
|
||||
}
|
||||
+445
@@ -0,0 +1,445 @@
|
||||
/*
|
||||
control character types:
|
||||
1 - metadata
|
||||
2 - symbols
|
||||
6 - false
|
||||
7 - true
|
||||
8- 16 - negative doubles
|
||||
16-24 positive doubles
|
||||
27 - String starts with a character 27 or less or is an empty string
|
||||
0 - multipart separator
|
||||
> 27 normal string characters
|
||||
*/
|
||||
/*
|
||||
* Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
|
||||
*/
|
||||
|
||||
const float64Array = new Float64Array(2)
|
||||
const int32Array = new Int32Array(float64Array.buffer, 0, 4)
|
||||
const {lowIdx, highIdx} = (() => {
|
||||
if (new Uint8Array(new Uint32Array([0xFFEE1100]).buffer)[0] === 0xFF) {
|
||||
return { lowIdx: 1, highIdx: 0 };
|
||||
}
|
||||
return { lowIdx: 0, highIdx: 1 };
|
||||
})();
|
||||
let nullTerminate = false
|
||||
let textEncoder
|
||||
try {
|
||||
textEncoder = new TextEncoder()
|
||||
} catch (error) {}
|
||||
|
||||
/*
|
||||
* Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
|
||||
*/
|
||||
export function writeKey(key, target, position, inSequence) {
|
||||
let targetView = target.dataView
|
||||
if (!targetView)
|
||||
targetView = target.dataView = new DataView(target.buffer, target.byteOffset, ((target.byteLength + 3) >> 2) << 2)
|
||||
switch (typeof key) {
|
||||
case 'string':
|
||||
let strLength = key.length
|
||||
let c1 = key.charCodeAt(0)
|
||||
if (!(c1 >= 28)) // escape character
|
||||
target[position++] = 27
|
||||
if (strLength < 0x40) {
|
||||
let i, c2
|
||||
for (i = 0; i < strLength; i++) {
|
||||
c1 = key.charCodeAt(i)
|
||||
if (c1 <= 4) {
|
||||
target[position++] = 4
|
||||
target[position++] = c1
|
||||
} else if (c1 < 0x80) {
|
||||
target[position++] = c1
|
||||
} else if (c1 < 0x800) {
|
||||
target[position++] = c1 >> 6 | 0xc0
|
||||
target[position++] = c1 & 0x3f | 0x80
|
||||
} else if (
|
||||
(c1 & 0xfc00) === 0xd800 &&
|
||||
((c2 = key.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
||||
) {
|
||||
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff)
|
||||
i++
|
||||
target[position++] = c1 >> 18 | 0xf0
|
||||
target[position++] = c1 >> 12 & 0x3f | 0x80
|
||||
target[position++] = c1 >> 6 & 0x3f | 0x80
|
||||
target[position++] = c1 & 0x3f | 0x80
|
||||
} else {
|
||||
target[position++] = c1 >> 12 | 0xe0
|
||||
target[position++] = c1 >> 6 & 0x3f | 0x80
|
||||
target[position++] = c1 & 0x3f | 0x80
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (target.utf8Write)
|
||||
position += target.utf8Write(key, position, target.byteLength - position)
|
||||
else
|
||||
position += textEncoder.encodeInto(key, target.subarray(position)).written
|
||||
if (position > target.length - 4)
|
||||
throw new RangeError('String does not fit in target buffer')
|
||||
}
|
||||
break
|
||||
case 'number':
|
||||
float64Array[0] = key
|
||||
let lowInt = int32Array[lowIdx]
|
||||
let highInt = int32Array[highIdx]
|
||||
let length
|
||||
if (key < 0) {
|
||||
targetView.setInt32(position + 4, ~((lowInt >>> 4) | (highInt << 28)))
|
||||
targetView.setInt32(position + 0, (highInt ^ 0x7fffffff) >>> 4)
|
||||
targetView.setInt32(position + 8, ((lowInt & 0xf) ^ 0xf) << 4, true) // just always do the null termination here
|
||||
return position + 9
|
||||
} else if ((lowInt & 0xf) || inSequence) {
|
||||
length = 9
|
||||
} else if (lowInt & 0xfffff)
|
||||
length = 8
|
||||
else if (lowInt || (highInt & 0xf))
|
||||
length = 6
|
||||
else
|
||||
length = 4
|
||||
// switching order to go to little endian
|
||||
targetView.setInt32(position + 0, (highInt >>> 4) | 0x10000000)
|
||||
targetView.setInt32(position + 4, (lowInt >>> 4) | (highInt << 28))
|
||||
// if (length == 9 || nullTerminate)
|
||||
targetView.setInt32(position + 8, (lowInt & 0xf) << 4, true)
|
||||
return position + length;
|
||||
case 'object':
|
||||
if (key) {
|
||||
if (Array.isArray(key)) {
|
||||
for (let i = 0, l = key.length; i < l; i++) {
|
||||
if (i > 0)
|
||||
target[position++] = 0
|
||||
position = writeKey(key[i], target, position, true)
|
||||
}
|
||||
break
|
||||
} else if (key instanceof Uint8Array) {
|
||||
target.set(key, position)
|
||||
position += key.length
|
||||
break
|
||||
} else {
|
||||
throw new Error('Unable to serialize object as a key: ' + JSON.stringify(key))
|
||||
}
|
||||
} else // null
|
||||
target[position++] = 0
|
||||
break
|
||||
case 'boolean':
|
||||
targetView.setUint32(position++, key ? 7 : 6, true)
|
||||
return position
|
||||
case 'bigint':
|
||||
let asFloat = Number(key)
|
||||
if (BigInt(asFloat) > key) {
|
||||
float64Array[0] = asFloat;
|
||||
if (asFloat > 0) {
|
||||
if (int32Array[lowIdx])
|
||||
int32Array[lowIdx]--;
|
||||
else {
|
||||
int32Array[highIdx]--;
|
||||
int32Array[lowIdx] = 0xffffffff;
|
||||
}
|
||||
} else {
|
||||
if (int32Array[lowIdx] < 0xffffffff)
|
||||
int32Array[lowIdx]++;
|
||||
else {
|
||||
int32Array[highIdx]++;
|
||||
int32Array[lowIdx] = 0;
|
||||
}
|
||||
}
|
||||
asFloat = float64Array[0];
|
||||
}
|
||||
let difference = key - BigInt(asFloat);
|
||||
if (difference === 0n)
|
||||
return writeKey(asFloat, target, position, inSequence)
|
||||
writeKey(asFloat, target, position, inSequence)
|
||||
position += 9; // always increment by 9 if we are adding fractional bits
|
||||
let exponent = BigInt((int32Array[highIdx] >> 20 & 0x7ff) - 1079);
|
||||
let nextByte = difference >> exponent;
|
||||
target[position - 1] |= Number(nextByte);
|
||||
difference -= nextByte << exponent;
|
||||
let first = true;
|
||||
while (difference || first) {
|
||||
first = false;
|
||||
exponent -= 7n;
|
||||
let nextByte = difference >> exponent;
|
||||
target[position++] = Number(nextByte) | 0x80;
|
||||
difference -= nextByte << exponent;
|
||||
}
|
||||
return position;
|
||||
case 'undefined':
|
||||
return position
|
||||
// undefined is interpreted as the absence of a key, signified by zero length
|
||||
case 'symbol':
|
||||
target[position++] = 2
|
||||
return writeKey(key.description, target, position, inSequence)
|
||||
default:
|
||||
throw new Error('Can not serialize key of type ' + typeof key)
|
||||
}
|
||||
if (nullTerminate && !inSequence)
|
||||
targetView.setUint32(position, 0)
|
||||
return position
|
||||
}
|
||||
|
||||
let position
|
||||
export function readKey(buffer, start, end, inSequence) {
|
||||
position = start
|
||||
let controlByte = buffer[position]
|
||||
let value
|
||||
if (controlByte < 24) {
|
||||
if (controlByte < 8) {
|
||||
position++
|
||||
if (controlByte == 6) {
|
||||
value = false
|
||||
} else if (controlByte == 7) {
|
||||
value = true
|
||||
} else if (controlByte == 0) {
|
||||
value = null
|
||||
} else if (controlByte == 2) {
|
||||
value = Symbol.for(readStringSafely(buffer, end))
|
||||
} else
|
||||
return Uint8Array.prototype.slice.call(buffer, start, end)
|
||||
} else {
|
||||
let dataView;
|
||||
try {
|
||||
dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, ((buffer.byteLength + 3) >> 2) << 2))
|
||||
} catch(error) {
|
||||
// if it is write at the end of the ArrayBuffer, we may need to retry with the exact remaining bytes
|
||||
dataView = buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.buffer.byteLength - buffer.byteOffset))
|
||||
}
|
||||
|
||||
let highInt = dataView.getInt32(position) << 4
|
||||
let size = end - position
|
||||
let lowInt
|
||||
if (size > 4) {
|
||||
lowInt = position + 8 <= buffer.length ? dataView.getInt32(position + 4) : (
|
||||
buffer[position + 4] << 24 |
|
||||
buffer[position + 5] << 16 |
|
||||
buffer[position + 6] << 8 |
|
||||
buffer[position + 7]
|
||||
)
|
||||
highInt |= lowInt >>> 28
|
||||
if (size <= 6) { // clear the last bits
|
||||
lowInt &= -0x10000
|
||||
}
|
||||
lowInt = lowInt << 4
|
||||
if (size > 8) {
|
||||
lowInt = lowInt | buffer[position + 8] >> 4
|
||||
}
|
||||
} else
|
||||
lowInt = 0
|
||||
if (controlByte < 16) {
|
||||
// negative gets negated
|
||||
highInt = highInt ^ 0x7fffffff
|
||||
lowInt = ~lowInt
|
||||
}
|
||||
int32Array[highIdx] = highInt
|
||||
int32Array[lowIdx] = lowInt
|
||||
value = float64Array[0]
|
||||
position += 9
|
||||
if (size > 9 && buffer[position] > 0) {
|
||||
// convert the float to bigint, and then we will add precision as we enumerate through the
|
||||
// extra bytes
|
||||
value = BigInt(value);
|
||||
let exponent = highInt >> 20 & 0x7ff;
|
||||
let next_byte = buffer[position - 1] & 0xf;
|
||||
value += BigInt(next_byte) << BigInt(exponent - 1079);
|
||||
while ((next_byte = buffer[position]) > 0 && position++ < end) {
|
||||
value += BigInt(next_byte & 0x7f) << BigInt((start - position) * 7 + exponent - 1016);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (controlByte == 27) {
|
||||
position++
|
||||
}
|
||||
value = readStringSafely(buffer, end)
|
||||
if (position < end) position-- // if have a null terminator for the string, count that as the array separator
|
||||
}
|
||||
while (position < end) {
|
||||
if (buffer[position] === 0)
|
||||
position++
|
||||
if (inSequence) {
|
||||
encoder.position = position
|
||||
return value
|
||||
}
|
||||
let nextValue = readKey(buffer, position, end, true)
|
||||
if (value instanceof Array) {
|
||||
value.push(nextValue)
|
||||
} else
|
||||
value = [ value, nextValue ]
|
||||
}
|
||||
return value
|
||||
}
|
||||
export const enableNullTermination = () => nullTerminate = true
|
||||
|
||||
export const encoder = {
|
||||
writeKey,
|
||||
readKey,
|
||||
enableNullTermination,
|
||||
}
|
||||
let targetBuffer = []
|
||||
let targetPosition = 0
|
||||
const hasNodeBuffer = typeof Buffer !== 'undefined'
|
||||
const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array
|
||||
export const toBufferKey = (key) => {
|
||||
let newBuffer
|
||||
if (targetPosition + 100 > targetBuffer.length) {
|
||||
targetBuffer = new ByteArrayAllocate(8192)
|
||||
targetPosition = 0
|
||||
newBuffer = true
|
||||
}
|
||||
try {
|
||||
let result = targetBuffer.slice(targetPosition, targetPosition = writeKey(key, targetBuffer, targetPosition))
|
||||
if (targetPosition > targetBuffer.length) {
|
||||
if (newBuffer)
|
||||
throw new Error('Key is too large')
|
||||
return toBufferKey(key)
|
||||
}
|
||||
return result
|
||||
} catch(error) {
|
||||
if (newBuffer)
|
||||
throw error
|
||||
targetPosition = targetBuffer.length
|
||||
return toBufferKey(key)
|
||||
}
|
||||
}
|
||||
export const fromBufferKey = (sourceBuffer) => {
|
||||
return readKey(sourceBuffer, 0, sourceBuffer.length)
|
||||
}
|
||||
const fromCharCode = String.fromCharCode
|
||||
function makeStringBuilder() {
|
||||
let stringBuildCode = '(source) => {'
|
||||
let previous = []
|
||||
for (let i = 0; i < 0x30; i++) {
|
||||
let v = fromCharCode((i & 0xf) + 97) + fromCharCode((i >> 4) + 97)
|
||||
stringBuildCode += `
|
||||
let ${v} = source[position++]
|
||||
if (${v} > 4) {
|
||||
if (${v} >= 0x80) ${v} = finishUtf8(${v}, source)
|
||||
} else {
|
||||
if (${v} === 4)
|
||||
${v} = source[position++]
|
||||
else
|
||||
return fromCharCode(${previous})
|
||||
}
|
||||
`
|
||||
previous.push(v)
|
||||
if (i == 1000000) // this just exists to prevent rollup from doing dead code elimination on finishUtf8
|
||||
finishUtf8()
|
||||
}
|
||||
stringBuildCode += `return fromCharCode(${previous}) + readString(source)}`
|
||||
return stringBuildCode
|
||||
}
|
||||
|
||||
let pendingSurrogate
|
||||
function finishUtf8(byte1, src) {
|
||||
if ((byte1 & 0xe0) === 0xc0) {
|
||||
// 2 bytes
|
||||
const byte2 = src[position++] & 0x3f
|
||||
return ((byte1 & 0x1f) << 6) | byte2
|
||||
} else if ((byte1 & 0xf0) === 0xe0) {
|
||||
// 3 bytes
|
||||
const byte2 = src[position++] & 0x3f
|
||||
const byte3 = src[position++] & 0x3f
|
||||
return ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3
|
||||
} else if ((byte1 & 0xf8) === 0xf0) {
|
||||
// 4 bytes
|
||||
if (pendingSurrogate) {
|
||||
byte1 = pendingSurrogate
|
||||
pendingSurrogate = null
|
||||
position += 3
|
||||
return byte1
|
||||
}
|
||||
const byte2 = src[position++] & 0x3f
|
||||
const byte3 = src[position++] & 0x3f
|
||||
const byte4 = src[position++] & 0x3f
|
||||
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4
|
||||
if (unit > 0xffff) {
|
||||
pendingSurrogate = 0xdc00 | (unit & 0x3ff)
|
||||
unit = (((unit - 0x10000) >>> 10) & 0x3ff) | 0xd800
|
||||
position -= 4 // reset so we can return the next part of the surrogate pair
|
||||
}
|
||||
return unit
|
||||
} else {
|
||||
return byte1
|
||||
}
|
||||
}
|
||||
|
||||
const readString =
|
||||
typeof process !== 'undefined' && process.isBun ? // the eval in bun doesn't properly closure on position, so we
|
||||
// have to manually update it
|
||||
(function(reading) {
|
||||
let { setPosition, getPosition, readString } = reading;
|
||||
return (source) => {
|
||||
setPosition(position);
|
||||
let value = readString(source);
|
||||
position = getPosition();
|
||||
return value;
|
||||
};
|
||||
})((new Function('fromCharCode', 'let position; let readString = ' + makeStringBuilder() +
|
||||
';return {' +
|
||||
'setPosition(p) { position = p },' +
|
||||
'getPosition() { return position },' +
|
||||
'readString }'))(fromCharCode)) :
|
||||
eval(makeStringBuilder())
|
||||
function readStringSafely(source, end) {
|
||||
if (source[end] > 0) {
|
||||
let previous = source[end]
|
||||
try {
|
||||
// read string expects a null terminator, that is a 0 or undefined from reading past the end of the buffer, so we
|
||||
// have to ensure that, but do so safely, restoring the buffer to its original state
|
||||
source[end] = 0
|
||||
return readString(source)
|
||||
} finally {
|
||||
source[end] = previous
|
||||
}
|
||||
} else return readString(source);
|
||||
}
|
||||
export function compareKeys(a, b) {
|
||||
// compare with type consistency that matches binary comparison
|
||||
if (typeof a == 'object') {
|
||||
if (!a) {
|
||||
return b == null ? 0 : -1
|
||||
}
|
||||
if (a.compare) {
|
||||
if (b == null) {
|
||||
return 1
|
||||
} else if (b.compare) {
|
||||
return a.compare(b)
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
let arrayComparison
|
||||
if (b instanceof Array) {
|
||||
let i = 0
|
||||
while((arrayComparison = compareKeys(a[i], b[i])) == 0 && i <= a.length) {
|
||||
i++
|
||||
}
|
||||
return arrayComparison
|
||||
}
|
||||
arrayComparison = compareKeys(a[0], b)
|
||||
if (arrayComparison == 0 && a.length > 1)
|
||||
return 1
|
||||
return arrayComparison
|
||||
} else if (typeof a == typeof b) {
|
||||
if (typeof a === 'symbol') {
|
||||
a = Symbol.keyFor(a)
|
||||
b = Symbol.keyFor(b)
|
||||
}
|
||||
return a < b ? -1 : a === b ? 0 : 1
|
||||
}
|
||||
else if (typeof b == 'object') {
|
||||
if (b instanceof Array)
|
||||
return -compareKeys(b, a)
|
||||
return 1
|
||||
} else {
|
||||
return typeOrder[typeof a] < typeOrder[typeof b] ? -1 : 1
|
||||
}
|
||||
}
|
||||
const typeOrder = {
|
||||
symbol: 0,
|
||||
undefined: 1,
|
||||
boolean: 2,
|
||||
number: 3,
|
||||
string: 4
|
||||
}
|
||||
export const MINIMUM_KEY = null
|
||||
export const MAXIMUM_KEY = new Uint8Array([0xff])
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "ordered-binary",
|
||||
"author": "Kris Zyp",
|
||||
"version": "1.6.1",
|
||||
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/kriszyp/ordered-binary"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "rollup -c",
|
||||
"prepare": "rollup -c",
|
||||
"test": "mocha tests -u tdd"
|
||||
},
|
||||
"type": "module",
|
||||
"main": "dist/index.cjs",
|
||||
"module": "index.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./index.js"
|
||||
},
|
||||
"./index.js": {
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./index.js"
|
||||
}
|
||||
},
|
||||
"typings": "./index.d.ts",
|
||||
"optionalDependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/node": "latest",
|
||||
"chai": "^4",
|
||||
"mocha": "^9.2.0",
|
||||
"rollup": "^2.61.1"
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
export default [
|
||||
{
|
||||
input: "index.js",
|
||||
output: [
|
||||
{
|
||||
file: "dist/index.cjs",
|
||||
format: "cjs"
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
import { assert } from 'chai'
|
||||
|
||||
import { toBufferKey, fromBufferKey, readKey, writeKey } from '../index.js'
|
||||
|
||||
function assertBufferComparison(lesser, greater) {
|
||||
for (let i = 0; i < lesser.length; i++) {
|
||||
if (lesser[i] < greater[i]) {
|
||||
return
|
||||
}
|
||||
if (lesser[i] > (greater[i] || 0)) {
|
||||
assert.fail('Byte ' + i + 'should not be ' + lesser[i] + '>' + greater[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
//var inspector = require('inspector'); inspector.open(9330, null, true); debugger
|
||||
let seed = 0;
|
||||
export function random() {
|
||||
seed++;
|
||||
let a = seed * 15485863;
|
||||
return ((a * a * a) % 2038074743) / 2038074743;
|
||||
}
|
||||
|
||||
suite('key buffers', () => {
|
||||
|
||||
test('numbers equivalence', () => {
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(4)), 4)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(-4)), -4)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(3.4)), 3.4)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(Math.PI)), Math.PI)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(2002225)), 2002225)
|
||||
// check to make sure it works with a limited ArrayBuffer
|
||||
assert.strictEqual(fromBufferKey(new Uint8Array(toBufferKey(2002225))), 2002225)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(9377288)), 9377288)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(1503579323825)), 1503579323825)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(1503579323825.3523532)), 1503579323825.3523532)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(-1503579323825)), -1503579323825)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(0.00005032)), 0.00005032)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(-0.00005032)), -0.00005032)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(0.00000000000000000000000005431)), 0.00000000000000000000000005431)
|
||||
})
|
||||
test('within buffer equivalence', () => {
|
||||
let buffer = Buffer.alloc(1024, 0xff);
|
||||
let length = writeKey(2002225, buffer, 0);
|
||||
let position = length;
|
||||
buffer[position++] = 0xff;
|
||||
buffer[position++] = 0xff;
|
||||
assert.strictEqual(readKey(buffer, 0, length), 2002225);
|
||||
length = writeKey('hello, world', buffer, 0);
|
||||
assert.strictEqual(readKey(buffer, 0, length), 'hello, world');
|
||||
// ensure that reading the string didn't modify the buffer after the string (or is restored at least)
|
||||
assert.strictEqual(buffer[length], 0xff);
|
||||
});
|
||||
test('number comparison', () => {
|
||||
assertBufferComparison(toBufferKey(4), toBufferKey(5))
|
||||
assertBufferComparison(toBufferKey(1503579323824), toBufferKey(1503579323825))
|
||||
assertBufferComparison(toBufferKey(1.4), toBufferKey(2))
|
||||
assertBufferComparison(toBufferKey(0.000000001), toBufferKey(0.00000001))
|
||||
assertBufferComparison(toBufferKey(-4), toBufferKey(3))
|
||||
assertBufferComparison(toBufferKey(0), toBufferKey(1))
|
||||
assertBufferComparison(toBufferKey(-0.001), toBufferKey(0))
|
||||
assertBufferComparison(toBufferKey(-0.001), toBufferKey(-0.000001))
|
||||
assertBufferComparison(toBufferKey(-5236532532532), toBufferKey(-5236532532531))
|
||||
})
|
||||
test('bigint equivalence', () => {
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(-35913040084491349n)), -35913040084491349n)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(6135421331404949076605986n)), 6135421331404949076605986n)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(0xfffffffffffffffffffffn)), 0xfffffffffffffffffffffn)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(12345678901234567890n)), 12345678901234567890n)
|
||||
assert.deepEqual(fromBufferKey(toBufferKey([12345678901234567890n, 44])), [12345678901234567890n, 44])
|
||||
assert.deepEqual(fromBufferKey(toBufferKey(['hi', 12345678901234567890n])), ['hi', 12345678901234567890n])
|
||||
assert.deepEqual(fromBufferKey(toBufferKey([6135421331404949076605986n, 'after'])), [6135421331404949076605986n, 'after'])
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(132923456789012345678903533235253252353211125n)), 132923456789012345678903533235253252353211125n)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(352n)), 352)
|
||||
let num = 5325n
|
||||
for (let i = 0; i < 1100; i++) {
|
||||
num *= BigInt(Math.floor(random() * 3 + 1));
|
||||
num -= BigInt(Math.floor(random() * 1000));
|
||||
assert.strictEqual(BigInt(fromBufferKey(toBufferKey(num))), num)
|
||||
assert.strictEqual(BigInt(fromBufferKey(toBufferKey(-num))), -num)
|
||||
}
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(-352n)), -352)
|
||||
})
|
||||
test('bigint comparison', () => {
|
||||
assertBufferComparison(toBufferKey(0xfffffffffffffffffffffn), toBufferKey(0x100fffffffffffffffffffn))
|
||||
assertBufferComparison(toBufferKey(12345678901234567890), toBufferKey(12345678901234567890n))
|
||||
assertBufferComparison(toBufferKey(6135421331404949076605986n), toBufferKey(6135421331404949076605987n))
|
||||
assertBufferComparison(toBufferKey(-6135421331404949076605986n), toBufferKey(-6135421331404949076605985n))
|
||||
assertBufferComparison(toBufferKey(-35913040084491349n), toBufferKey(-35913040084491348n))
|
||||
})
|
||||
|
||||
test('string equivalence', () => {
|
||||
assert.strictEqual(fromBufferKey(toBufferKey('4')), '4')
|
||||
assert.strictEqual(fromBufferKey(toBufferKey('hello')), 'hello')
|
||||
assert.strictEqual(fromBufferKey(toBufferKey('')), '')
|
||||
assert.strictEqual(fromBufferKey(toBufferKey('\x00')), '\x00')
|
||||
assert.strictEqual(fromBufferKey(toBufferKey('\x03test\x01\x00')), '\x03test\x01\x00')
|
||||
assert.strictEqual(fromBufferKey(toBufferKey('prance 🧚🏻♀️🩷')), 'prance 🧚🏻♀️🩷')
|
||||
})
|
||||
test('string comparison', () => {
|
||||
assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
|
||||
assertBufferComparison(toBufferKey('and'), toBufferKey('bad'))
|
||||
assertBufferComparison(toBufferKey('hello'), toBufferKey('hello2'))
|
||||
let buffer = Buffer.alloc(1024)
|
||||
let end = writeKey(['this is a test', 5.25], buffer, 0)
|
||||
})
|
||||
test('boolean equivalence', () => {
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(true)), true)
|
||||
assert.strictEqual(fromBufferKey(toBufferKey(false)), false)
|
||||
})
|
||||
|
||||
test('multipart equivalence', () => {
|
||||
assert.deepEqual(fromBufferKey(toBufferKey([4, 5])),
|
||||
[4, 5])
|
||||
assert.deepEqual(fromBufferKey(toBufferKey(['hello', 5.25])),
|
||||
['hello', 5.25])
|
||||
assert.deepEqual(fromBufferKey(toBufferKey([5, 'hello', null, 5.25])),
|
||||
[5, 'hello', null, 5.25])
|
||||
assert.deepEqual(fromBufferKey(toBufferKey([true, 1503579323825])),
|
||||
[true, 1503579323825])
|
||||
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'sec\x00nd'])),
|
||||
[-0.2525, 'sec\x00nd'])
|
||||
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '2nd', '3rd'])),
|
||||
[-0.2525, '2nd', '3rd'])
|
||||
})
|
||||
|
||||
test('multipart comparison', () => {
|
||||
assertBufferComparison(
|
||||
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
|
||||
Buffer.concat([toBufferKey(5), Buffer.from([30]), toBufferKey(5)]))
|
||||
assertBufferComparison(
|
||||
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(5)]),
|
||||
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey(6)]))
|
||||
assertBufferComparison(
|
||||
Buffer.concat([toBufferKey('and'), Buffer.from([30]), toBufferKey(5)]),
|
||||
Buffer.concat([toBufferKey('and2'), Buffer.from([30]), toBufferKey(5)]))
|
||||
assertBufferComparison(
|
||||
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('and')]),
|
||||
Buffer.concat([toBufferKey(4), Buffer.from([30]), toBufferKey('cat')]))
|
||||
})
|
||||
test('performance', () => {
|
||||
let buffer = Buffer.alloc(1024)
|
||||
let start = process.hrtime.bigint()
|
||||
let end, value
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
end = writeKey('this is a test of a longer string to read and write', buffer, 0)
|
||||
}
|
||||
console.log('writeKey string time', nextTime(), end)
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
value = readKey(buffer, 0, end)
|
||||
}
|
||||
console.log('readKey string time', nextTime(), value)
|
||||
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
end = writeKey(33456, buffer, 0)
|
||||
}
|
||||
console.log('writeKey number time', nextTime(), end)
|
||||
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
value = readKey(buffer, 2, end)
|
||||
}
|
||||
console.log('readKey number time', nextTime(), value)
|
||||
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
end = writeKey(['hello', 33456], buffer, 0)
|
||||
}
|
||||
console.log('writeKey array time', nextTime(), end, buffer.slice(0, end))
|
||||
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
value = readKey(buffer, 0, end)
|
||||
}
|
||||
console.log('readKey array time', nextTime(), value)
|
||||
|
||||
function nextTime() {
|
||||
let ns = process.hrtime.bigint()
|
||||
let elapsed = ns - start
|
||||
start = ns
|
||||
return Number(elapsed) / 1000000 + 'ns'
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user