ordered-binary 1.5.3 → 1.6.1

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/dist/index.cjs CHANGED
@@ -20,6 +20,12 @@ control character types:
20
20
 
21
21
  const float64Array = new Float64Array(2);
22
22
  const int32Array = new Int32Array(float64Array.buffer, 0, 4);
23
+ const {lowIdx, highIdx} = (() => {
24
+ if (new Uint8Array(new Uint32Array([0xFFEE1100]).buffer)[0] === 0xFF) {
25
+ return { lowIdx: 1, highIdx: 0 };
26
+ }
27
+ return { lowIdx: 0, highIdx: 1 };
28
+ })();
23
29
  let nullTerminate = false;
24
30
  let textEncoder;
25
31
  try {
@@ -78,8 +84,8 @@ function writeKey(key, target, position, inSequence) {
78
84
  break
79
85
  case 'number':
80
86
  float64Array[0] = key;
81
- let lowInt = int32Array[0];
82
- let highInt = int32Array[1];
87
+ let lowInt = int32Array[lowIdx];
88
+ let highInt = int32Array[highIdx];
83
89
  let length;
84
90
  if (key < 0) {
85
91
  targetView.setInt32(position + 4, ~((lowInt >>> 4) | (highInt << 28)));
@@ -127,18 +133,18 @@ function writeKey(key, target, position, inSequence) {
127
133
  if (BigInt(asFloat) > key) {
128
134
  float64Array[0] = asFloat;
129
135
  if (asFloat > 0) {
130
- if (int32Array[0])
131
- int32Array[0]--;
136
+ if (int32Array[lowIdx])
137
+ int32Array[lowIdx]--;
132
138
  else {
133
- int32Array[1]--;
134
- int32Array[0] = 0xffffffff;
139
+ int32Array[highIdx]--;
140
+ int32Array[lowIdx] = 0xffffffff;
135
141
  }
136
142
  } else {
137
- if (int32Array[0] < 0xffffffff)
138
- int32Array[0]++;
143
+ if (int32Array[lowIdx] < 0xffffffff)
144
+ int32Array[lowIdx]++;
139
145
  else {
140
- int32Array[1]++;
141
- int32Array[0] = 0;
146
+ int32Array[highIdx]++;
147
+ int32Array[lowIdx] = 0;
142
148
  }
143
149
  }
144
150
  asFloat = float64Array[0];
@@ -148,7 +154,7 @@ function writeKey(key, target, position, inSequence) {
148
154
  return writeKey(asFloat, target, position, inSequence)
149
155
  writeKey(asFloat, target, position, inSequence);
150
156
  position += 9; // always increment by 9 if we are adding fractional bits
151
- let exponent = BigInt((int32Array[1] >> 20 & 0x7ff) - 1079);
157
+ let exponent = BigInt((int32Array[highIdx] >> 20 & 0x7ff) - 1079);
152
158
  let nextByte = difference >> exponent;
153
159
  target[position - 1] |= Number(nextByte);
154
160
  difference -= nextByte << exponent;
@@ -206,7 +212,12 @@ function readKey(buffer, start, end, inSequence) {
206
212
  let size = end - position;
207
213
  let lowInt;
208
214
  if (size > 4) {
209
- lowInt = dataView.getInt32(position + 4);
215
+ lowInt = position + 8 <= buffer.length ? dataView.getInt32(position + 4) : (
216
+ buffer[position + 4] << 24 |
217
+ buffer[position + 5] << 16 |
218
+ buffer[position + 6] << 8 |
219
+ buffer[position + 7]
220
+ );
210
221
  highInt |= lowInt >>> 28;
211
222
  if (size <= 6) { // clear the last bits
212
223
  lowInt &= -0x10000;
@@ -222,8 +233,8 @@ function readKey(buffer, start, end, inSequence) {
222
233
  highInt = highInt ^ 0x7fffffff;
223
234
  lowInt = ~lowInt;
224
235
  }
225
- int32Array[1] = highInt;
226
- int32Array[0] = lowInt;
236
+ int32Array[highIdx] = highInt;
237
+ int32Array[lowIdx] = lowInt;
227
238
  value = float64Array[0];
228
239
  position += 9;
229
240
  if (size > 9 && buffer[position] > 0) {
@@ -243,6 +254,7 @@ function readKey(buffer, start, end, inSequence) {
243
254
  position++;
244
255
  }
245
256
  value = readStringSafely(buffer, end);
257
+ if (position < end) position--; // if have a null terminator for the string, count that as the array separator
246
258
  }
247
259
  while (position < end) {
248
260
  if (buffer[position] === 0)
package/index.js CHANGED
@@ -16,6 +16,12 @@ control character types:
16
16
 
17
17
  const float64Array = new Float64Array(2)
18
18
  const int32Array = new Int32Array(float64Array.buffer, 0, 4)
19
+ const {lowIdx, highIdx} = (() => {
20
+ if (new Uint8Array(new Uint32Array([0xFFEE1100]).buffer)[0] === 0xFF) {
21
+ return { lowIdx: 1, highIdx: 0 };
22
+ }
23
+ return { lowIdx: 0, highIdx: 1 };
24
+ })();
19
25
  let nullTerminate = false
20
26
  let textEncoder
21
27
  try {
@@ -74,8 +80,8 @@ export function writeKey(key, target, position, inSequence) {
74
80
  break
75
81
  case 'number':
76
82
  float64Array[0] = key
77
- let lowInt = int32Array[0]
78
- let highInt = int32Array[1]
83
+ let lowInt = int32Array[lowIdx]
84
+ let highInt = int32Array[highIdx]
79
85
  let length
80
86
  if (key < 0) {
81
87
  targetView.setInt32(position + 4, ~((lowInt >>> 4) | (highInt << 28)))
@@ -123,18 +129,18 @@ export function writeKey(key, target, position, inSequence) {
123
129
  if (BigInt(asFloat) > key) {
124
130
  float64Array[0] = asFloat;
125
131
  if (asFloat > 0) {
126
- if (int32Array[0])
127
- int32Array[0]--;
132
+ if (int32Array[lowIdx])
133
+ int32Array[lowIdx]--;
128
134
  else {
129
- int32Array[1]--;
130
- int32Array[0] = 0xffffffff;
135
+ int32Array[highIdx]--;
136
+ int32Array[lowIdx] = 0xffffffff;
131
137
  }
132
138
  } else {
133
- if (int32Array[0] < 0xffffffff)
134
- int32Array[0]++;
139
+ if (int32Array[lowIdx] < 0xffffffff)
140
+ int32Array[lowIdx]++;
135
141
  else {
136
- int32Array[1]++;
137
- int32Array[0] = 0;
142
+ int32Array[highIdx]++;
143
+ int32Array[lowIdx] = 0;
138
144
  }
139
145
  }
140
146
  asFloat = float64Array[0];
@@ -144,7 +150,7 @@ export function writeKey(key, target, position, inSequence) {
144
150
  return writeKey(asFloat, target, position, inSequence)
145
151
  writeKey(asFloat, target, position, inSequence)
146
152
  position += 9; // always increment by 9 if we are adding fractional bits
147
- let exponent = BigInt((int32Array[1] >> 20 & 0x7ff) - 1079);
153
+ let exponent = BigInt((int32Array[highIdx] >> 20 & 0x7ff) - 1079);
148
154
  let nextByte = difference >> exponent;
149
155
  target[position - 1] |= Number(nextByte);
150
156
  difference -= nextByte << exponent;
@@ -202,7 +208,12 @@ export function readKey(buffer, start, end, inSequence) {
202
208
  let size = end - position
203
209
  let lowInt
204
210
  if (size > 4) {
205
- lowInt = dataView.getInt32(position + 4)
211
+ lowInt = position + 8 <= buffer.length ? dataView.getInt32(position + 4) : (
212
+ buffer[position + 4] << 24 |
213
+ buffer[position + 5] << 16 |
214
+ buffer[position + 6] << 8 |
215
+ buffer[position + 7]
216
+ )
206
217
  highInt |= lowInt >>> 28
207
218
  if (size <= 6) { // clear the last bits
208
219
  lowInt &= -0x10000
@@ -218,8 +229,8 @@ export function readKey(buffer, start, end, inSequence) {
218
229
  highInt = highInt ^ 0x7fffffff
219
230
  lowInt = ~lowInt
220
231
  }
221
- int32Array[1] = highInt
222
- int32Array[0] = lowInt
232
+ int32Array[highIdx] = highInt
233
+ int32Array[lowIdx] = lowInt
223
234
  value = float64Array[0]
224
235
  position += 9
225
236
  if (size > 9 && buffer[position] > 0) {
@@ -239,6 +250,7 @@ export function readKey(buffer, start, end, inSequence) {
239
250
  position++
240
251
  }
241
252
  value = readStringSafely(buffer, end)
253
+ if (position < end) position-- // if have a null terminator for the string, count that as the array separator
242
254
  }
243
255
  while (position < end) {
244
256
  if (buffer[position] === 0)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ordered-binary",
3
3
  "author": "Kris Zyp",
4
- "version": "1.5.3",
4
+ "version": "1.6.1",
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": {
package/tests/test.js CHANGED
@@ -28,6 +28,8 @@ suite('key buffers', () => {
28
28
  assert.strictEqual(fromBufferKey(toBufferKey(3.4)), 3.4)
29
29
  assert.strictEqual(fromBufferKey(toBufferKey(Math.PI)), Math.PI)
30
30
  assert.strictEqual(fromBufferKey(toBufferKey(2002225)), 2002225)
31
+ // check to make sure it works with a limited ArrayBuffer
32
+ assert.strictEqual(fromBufferKey(new Uint8Array(toBufferKey(2002225))), 2002225)
31
33
  assert.strictEqual(fromBufferKey(toBufferKey(9377288)), 9377288)
32
34
  assert.strictEqual(fromBufferKey(toBufferKey(1503579323825)), 1503579323825)
33
35
  assert.strictEqual(fromBufferKey(toBufferKey(1503579323825.3523532)), 1503579323825.3523532)
@@ -111,6 +113,8 @@ suite('key buffers', () => {
111
113
  [4, 5])
112
114
  assert.deepEqual(fromBufferKey(toBufferKey(['hello', 5.25])),
113
115
  ['hello', 5.25])
116
+ assert.deepEqual(fromBufferKey(toBufferKey([5, 'hello', null, 5.25])),
117
+ [5, 'hello', null, 5.25])
114
118
  assert.deepEqual(fromBufferKey(toBufferKey([true, 1503579323825])),
115
119
  [true, 1503579323825])
116
120
  assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'sec\x00nd'])),