ordered-binary 1.2.0 → 1.2.4

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 CHANGED
@@ -32,3 +32,8 @@ The main module exports these functions:
32
32
  `toBufferKey(jsPrimitive)` - This accepts a string, number, or boolean as the argument, and returns a `Buffer`.
33
33
 
34
34
  `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).
35
+
36
+ And these constants:
37
+
38
+ `MINIMUM_KEY` - The minimum key supported (`null`, which is represented as single zero byte)
39
+ `MAXIMUM_KEY` - A maximum key larger than any supported primitive (single 0xff byte)
package/dist/index.cjs CHANGED
@@ -66,10 +66,13 @@ function writeKey(key, target, position, inSequence) {
66
66
  target[position++] = c1 & 0x3f | 0x80;
67
67
  }
68
68
  }
69
- } else if (target.utf8Write) {
70
- position += target.utf8Write(key, position, 2000);
71
69
  } else {
72
- position += textEncoder.encodeInto(key, target.subarray(position)).written;
70
+ if (target.utf8Write)
71
+ position += target.utf8Write(key, position);
72
+ else
73
+ position += textEncoder.encodeInto(key, target.subarray(position)).written;
74
+ if (position > target.length - 4)
75
+ throw new RangeError('String does not fit in target buffer')
73
76
  }
74
77
  break
75
78
  case 'number':
@@ -209,9 +212,12 @@ function readKey(buffer, start, end, inSequence) {
209
212
  }
210
213
  return value
211
214
  }
215
+ const enableNullTermination = () => nullTerminate = true;
216
+
212
217
  const encoder = {
213
218
  writeKey,
214
219
  readKey,
220
+ enableNullTermination,
215
221
  };
216
222
  const toBufferKey = (key) => {
217
223
  let buffer = Buffer.alloc(2048);
@@ -276,8 +282,6 @@ function finishUtf8(byte1, src) {
276
282
  }
277
283
  }
278
284
 
279
- const enableNullTermination = () => nullTerminate = true;
280
-
281
285
  const readString = eval(makeStringBuilder());
282
286
 
283
287
  function compareKeys(a, b) {
@@ -330,7 +334,7 @@ const typeOrder = {
330
334
  string: 4
331
335
  };
332
336
  const MINIMUM_KEY = null;
333
- const MAXIMUM_KEY = Buffer.from([0xff]);
337
+ const MAXIMUM_KEY = new Uint8Array([0xff]);
334
338
 
335
339
  exports.MAXIMUM_KEY = MAXIMUM_KEY;
336
340
  exports.MINIMUM_KEY = MINIMUM_KEY;
package/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ type Key = Key[] | string | symbol | number | boolean | Uint8Array;
2
+ /** Writes a key (a primitive value) to the target buffer, starting at the given position */
3
+ export function writeKey(key: Key, target: Uint8Array, position: number, inSequence?: boolean): number;
4
+ /** Reads a key from the provided buffer, from the given range */
5
+ export function readKey(buffer: Uint8Array, start: number, end: number, inSequence?: boolean): Key;
6
+ /** 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 */
7
+ export function compareKeys(a: Key, b: Key): number;
8
+ /** The minimum key, with the "first" binary representation (one byte of zero) */
9
+ export const MINIMUM_KEY: null
10
+ /** A maximum key, with a binary representation after all other JS primitives (one byte of 0xff) */
11
+ export const MAXIMUM_KEY: Uint8Array
12
+ /** 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 */
13
+ export function enableNullTermination(): void;
14
+ /** An object that holds the functions for encapsulation as a single encoder */
15
+ export const encoder: {
16
+ writeKey: typeof writeKey,
17
+ readKey: typeof readKey,
18
+ enableNullTermination: typeof enableNullTermination,
19
+ }
package/index.js CHANGED
@@ -62,10 +62,13 @@ export function writeKey(key, target, position, inSequence) {
62
62
  target[position++] = c1 & 0x3f | 0x80
63
63
  }
64
64
  }
65
- } else if (target.utf8Write) {
66
- position += target.utf8Write(key, position, 2000)
67
65
  } else {
68
- position += textEncoder.encodeInto(key, target.subarray(position)).written
66
+ if (target.utf8Write)
67
+ position += target.utf8Write(key, position)
68
+ else
69
+ position += textEncoder.encodeInto(key, target.subarray(position)).written
70
+ if (position > target.length - 4)
71
+ throw new RangeError('String does not fit in target buffer')
69
72
  }
70
73
  break
71
74
  case 'number':
@@ -205,9 +208,12 @@ export function readKey(buffer, start, end, inSequence) {
205
208
  }
206
209
  return value
207
210
  }
211
+ export const enableNullTermination = () => nullTerminate = true
212
+
208
213
  export const encoder = {
209
214
  writeKey,
210
215
  readKey,
216
+ enableNullTermination,
211
217
  }
212
218
  export const toBufferKey = (key) => {
213
219
  let buffer = Buffer.alloc(2048)
@@ -272,8 +278,6 @@ function finishUtf8(byte1, src) {
272
278
  }
273
279
  }
274
280
 
275
- export const enableNullTermination = () => nullTerminate = true
276
-
277
281
  const readString = eval(makeStringBuilder())
278
282
 
279
283
  export function compareKeys(a, b) {
@@ -326,4 +330,4 @@ const typeOrder = {
326
330
  string: 4
327
331
  }
328
332
  export const MINIMUM_KEY = null
329
- export const MAXIMUM_KEY = Buffer.from([0xff])
333
+ export const MAXIMUM_KEY = new Uint8Array([0xff])
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ordered-binary",
3
3
  "author": "Kris Zyp",
4
- "version": "1.2.0",
4
+ "version": "1.2.4",
5
5
  "description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -14,6 +14,7 @@
14
14
  "test": "mocha tests -u tdd"
15
15
  },
16
16
  "type": "module",
17
+ "main": "dist/index.cjs",
17
18
  "module": "index.js",
18
19
  "exports": {
19
20
  ".": {
@@ -30,7 +31,7 @@
30
31
  "devDependencies": {
31
32
  "@types/node": "latest",
32
33
  "chai": "^4",
33
- "mocha": "^8.1.3",
34
- "rollup": "^1.20.3"
34
+ "mocha": "^9.2.0",
35
+ "rollup": "^2.61.1"
35
36
  }
36
37
  }