ordered-binary
Advanced tools
Comparing version 1.5.1 to 1.5.2
31
index.js
@@ -17,4 +17,2 @@ /* | ||
import exp from "constants" | ||
const float64Array = new Float64Array(2) | ||
@@ -179,3 +177,2 @@ const int32Array = new Int32Array(float64Array.buffer, 0, 4) | ||
export function readKey(buffer, start, end, inSequence) { | ||
buffer[end] = 0 // make sure it is null terminated | ||
position = start | ||
@@ -194,3 +191,3 @@ let controlByte = buffer[position] | ||
} else if (controlByte == 2) { | ||
value = Symbol.for(readString(buffer)) | ||
value = Symbol.for(readStringSafely(buffer, end)) | ||
} else | ||
@@ -214,3 +211,3 @@ return Uint8Array.prototype.slice.call(buffer, start, end) | ||
if (size <= 6) { // clear the last bits | ||
lowInt &= -0x1000 | ||
lowInt &= -0x10000 | ||
} | ||
@@ -248,11 +245,3 @@ lowInt = lowInt << 4 | ||
} | ||
value = readString(buffer) | ||
/*let strStart = position | ||
let strEnd = end | ||
for (; position < end; position++) { | ||
if (buffer[position] == 0) { | ||
break | ||
} | ||
} | ||
value = buffer.toString('utf8', strStart, position++)*/ | ||
value = readStringSafely(buffer, end) | ||
} | ||
@@ -386,3 +375,15 @@ while (position < end) { | ||
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) { | ||
@@ -389,0 +390,0 @@ // compare with type consistency that matches binary comparison |
{ | ||
"name": "ordered-binary", | ||
"author": "Kris Zyp", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -30,2 +30,3 @@ import { assert } from 'chai' | ||
assert.strictEqual(fromBufferKey(toBufferKey(Math.PI)), Math.PI) | ||
assert.strictEqual(fromBufferKey(toBufferKey(2002225)), 2002225) | ||
assert.strictEqual(fromBufferKey(toBufferKey(9377288)), 9377288) | ||
@@ -39,2 +40,14 @@ assert.strictEqual(fromBufferKey(toBufferKey(1503579323825)), 1503579323825) | ||
}) | ||
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', () => { | ||
@@ -73,2 +86,5 @@ assertBufferComparison(toBufferKey(4), toBufferKey(5)) | ||
assertBufferComparison(toBufferKey(12345678901234567890), toBufferKey(12345678901234567890n)) | ||
assertBufferComparison(toBufferKey(6135421331404949076605986n), toBufferKey(6135421331404949076605987n)) | ||
assertBufferComparison(toBufferKey(-6135421331404949076605986n), toBufferKey(-6135421331404949076605985n)) | ||
assertBufferComparison(toBufferKey(-35913040084491349n), toBufferKey(-35913040084491348n)) | ||
}) | ||
@@ -75,0 +91,0 @@ |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
59139
1059
0