cbor-x 1.6.8 → 1.6.9

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/node.cjs CHANGED
@@ -17,7 +17,6 @@ const RECORD_DEFINITIONS_ID = 0xdffe;
17
17
  const RECORD_INLINE_ID = 0xdfff; // temporary first-come first-serve tag // proposed tag: 0x7265 // 're'
18
18
  const BUNDLED_STRINGS_ID = 0xdff9;
19
19
  const PACKED_REFERENCE_TAG_ID = 6;
20
- const STOP_CODE = {};
21
20
  let maxArraySize = 112810000; // This is the maximum array size in V8. We would potentially detect and set it higher
22
21
  // for JSC, but this is pretty large and should be sufficient for most use cases
23
22
  let maxMapSize = 16810000; // JavaScript has a fixed maximum map size of about 16710000, but JS itself enforces this,
@@ -128,16 +127,11 @@ class Decoder {
128
127
  // re-entrant execution, save the state and restore it after we do this decode
129
128
  return saveState(() => {
130
129
  clearSource();
131
- try {
132
- return this ? this.decode(source, end) : Decoder.prototype.decode.call(defaultOptions, source, end)
133
- } catch (error) {
134
- if (error && typeof error == 'object')
135
- nestedDecodeErrors.add(error);
136
- throw error
137
- }
130
+ return this ? this.decode(source, end) : Decoder.prototype.decode.call(defaultOptions, source, end)
138
131
  })
139
132
  }
140
- srcEnd = end > -1 ? end : source.length;
133
+ // past the view, the JS string readers would decode the missing bytes as zeros instead of failing as truncated
134
+ srcEnd = end > -1 && end < source.length ? end : source.length;
141
135
  position$1 = 0;
142
136
  stringPosition = 0;
143
137
  srcStringEnd = 0;
@@ -177,18 +171,28 @@ class Decoder {
177
171
  return checkedRead()
178
172
  }
179
173
  decodeMultiple(source, forEach) {
174
+ if (src) {
175
+ // Keep the complete nested sequence inside its own decoder state.
176
+ return saveState(() => {
177
+ clearSource();
178
+ return this ? this.decodeMultiple(source, forEach) : defaultDecoder.decodeMultiple(source, forEach)
179
+ })
180
+ }
180
181
  let values, lastPosition = 0;
181
182
  try {
182
183
  let size = source.length;
183
184
  sequentialMode = true;
184
185
  let value = this ? this.decode(source, size) : defaultDecoder.decode(source, size);
185
186
  if (forEach) {
187
+ let end = position$1;
186
188
  if (forEach(value) === false) {
187
189
  return
188
190
  }
189
- while(position$1 < size) {
190
- lastPosition = position$1;
191
- if (forEach(checkedRead()) === false) {
191
+ while(end < size) {
192
+ lastPosition = end;
193
+ value = checkedRead();
194
+ end = position$1;
195
+ if (forEach(value) === false) {
192
196
  return
193
197
  }
194
198
  }
@@ -202,8 +206,10 @@ class Decoder {
202
206
  return values
203
207
  }
204
208
  } catch(error) {
205
- error.lastPosition = lastPosition;
206
- error.values = values;
209
+ if (error && (typeof error == 'object' || typeof error == 'function')) {
210
+ error.lastPosition = lastPosition;
211
+ error.values = values;
212
+ }
207
213
  throw error
208
214
  } finally {
209
215
  sequentialMode = false;
@@ -257,9 +263,21 @@ function checkedRead() {
257
263
  }
258
264
  }
259
265
 
260
- function endOfCBORError() {
266
+ function readBreak() {
267
+ if (position$1 < srcEnd && src[position$1] === 0xff) {
268
+ position$1++;
269
+ return true
270
+ }
271
+ return false
272
+ }
273
+ // neededEnd, from a length check, is a position that the source must extend to before the item can decode. It is a
274
+ // lower bound for any completion of the source: every byte read before the check was within the source, so a longer
275
+ // source reaches the same check with the same length. Streams wait for it instead of decoding on every chunk.
276
+ function endOfCBORError(neededEnd) {
261
277
  let error = new Error('Unexpected end of CBOR data');
262
278
  error.incomplete = true;
279
+ if (neededEnd)
280
+ error.neededEnd = neededEnd;
263
281
  return error
264
282
  }
265
283
 
@@ -311,6 +329,9 @@ function read() {
311
329
  } else if (currentDecoder.int64AsNumber) {
312
330
  token = dataView.getUint32(position$1) * 0x100000000;
313
331
  token += dataView.getUint32(position$1 + 4);
332
+ position$1 += 8;
333
+ if (majorType === 1) return -1 - token
334
+ break
314
335
  } else token = dataView.getBigUint64(position$1);
315
336
  position$1 += 8;
316
337
  break
@@ -322,10 +343,10 @@ function read() {
322
343
  throw new Error('Indefinite length not supported for byte or text strings')
323
344
  case 4: // array
324
345
  let array = [];
325
- let value, i = 0;
326
- while ((value = read()) != STOP_CODE) {
346
+ let i = 0;
347
+ while (!readBreak()) {
327
348
  if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
328
- array[i++] = value;
349
+ array[i++] = read();
329
350
  }
330
351
  return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
331
352
  case 5: // map
@@ -334,13 +355,15 @@ function read() {
334
355
  let object = {};
335
356
  let i = 0;
336
357
  if (currentDecoder.keyMap) {
337
- while((key = read()) != STOP_CODE) {
358
+ while (!readBreak()) {
359
+ key = read();
338
360
  if (i++ >= objectSizeLimit) throw new Error(`Property count exceeds ${objectSizeLimit}`)
339
361
  object[safeKey(currentDecoder.decodeKey(key))] = read();
340
362
  }
341
363
  }
342
364
  else {
343
- while ((key = read()) != STOP_CODE) {
365
+ while (!readBreak()) {
366
+ key = read();
344
367
  if (i++ >= objectSizeLimit) throw new Error(`Property count exceeds ${objectSizeLimit}`)
345
368
  object[safeKey(key)] = read();
346
369
  }
@@ -354,7 +377,8 @@ function read() {
354
377
  let map = new Map();
355
378
  if (currentDecoder.keyMap) {
356
379
  let i = 0;
357
- while((key = read()) != STOP_CODE) {
380
+ while (!readBreak()) {
381
+ key = read();
358
382
  if (i++ >= maxMapSize) {
359
383
  throw new Error(`Map size exceeds ${maxMapSize}`);
360
384
  }
@@ -363,7 +387,8 @@ function read() {
363
387
  }
364
388
  else {
365
389
  let i = 0;
366
- while ((key = read()) != STOP_CODE) {
390
+ while (!readBreak()) {
391
+ key = read();
367
392
  if (i++ >= maxMapSize) {
368
393
  throw new Error(`Map size exceeds ${maxMapSize}`);
369
394
  }
@@ -373,7 +398,8 @@ function read() {
373
398
  return map
374
399
  }
375
400
  case 7:
376
- return STOP_CODE
401
+ // readBreak consumes every break that ends an indefinite-length item
402
+ throw new Error('Unexpected break stop code outside of an indefinite-length item')
377
403
  default:
378
404
  throw new Error('Invalid major type for indefinite length ' + majorType)
379
405
  }
@@ -387,13 +413,13 @@ function read() {
387
413
  case 1: // negative int
388
414
  return ~token
389
415
  case 2: // buffer
390
- if (token > srcEnd - position$1) throw endOfCBORError()
416
+ if (token > srcEnd - position$1) throw endOfCBORError(position$1 + token)
391
417
  return readBin(token)
392
418
  case 3: // string
393
419
  if (srcStringEnd >= position$1) { // already extracted, which only covers complete strings
394
420
  return srcString.slice(position$1 - srcStringStart, (position$1 += token) - srcStringStart)
395
421
  }
396
- if (token > srcEnd - position$1) throw endOfCBORError()
422
+ if (token > srcEnd - position$1) throw endOfCBORError(position$1 + token)
397
423
  if (srcStringEnd == 0 && srcEnd < 140 && token < 32) {
398
424
  // for small blocks, avoiding the overhead of the extract call is helpful
399
425
  let string = token < 16 ? shortStringInJS(token) : longStringInJS(token);
@@ -407,7 +433,7 @@ function read() {
407
433
  // never be satisfied; check before allocating so a tiny header can not force a huge allocation
408
434
  // nested arrays each pass the check above yet still preallocate, so also spend from a
409
435
  // per-decode budget of the remaining bytes; the summed lengths of any valid tree stay under it
410
- if (token > srcEnd - position$1 || (arrayAllocationBudget -= token) < 0) throw endOfCBORError()
436
+ if (token > srcEnd - position$1 || (arrayAllocationBudget -= token) < 0) throw endOfCBORError(position$1 + token)
411
437
  let array = new Array(token);
412
438
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
413
439
  //else
@@ -416,7 +442,7 @@ function read() {
416
442
  case 5: // map
417
443
  if (currentDecoder.mapsAsObjects) {
418
444
  if (token > objectSizeLimit) throw new Error(`Property count exceeds ${objectSizeLimit}`)
419
- if (token > (srcEnd - position$1) / 2) throw endOfCBORError() // each entry needs at least two bytes
445
+ if (token > (srcEnd - position$1) / 2) throw endOfCBORError(position$1 + 2 * token) // each entry needs at least two bytes
420
446
  let object = {};
421
447
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
422
448
  else for (let i = 0; i < token; i++) object[safeKey(read())] = read();
@@ -427,7 +453,7 @@ function read() {
427
453
  restoreMapsAsObject = false;
428
454
  }
429
455
  if (token > maxMapSize) throw new Error(`Map size exceeds ${maxMapSize}`)
430
- if (token > (srcEnd - position$1) / 2) throw endOfCBORError()
456
+ if (token > (srcEnd - position$1) / 2) throw endOfCBORError(position$1 + 2 * token)
431
457
  let map = new Map();
432
458
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) map.set(currentDecoder.decodeKey(read()),read());
433
459
  else for (let i = 0; i < token; i++) map.set(read(), read());
@@ -943,6 +969,7 @@ currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => {
943
969
  }
944
970
  return object
945
971
  };
972
+ const legacyRecordDefinition = currentExtensions[LEGACY_RECORD_INLINE_ID];
946
973
  currentExtensions[14] = (value) => {
947
974
  if (bundledStrings$1)
948
975
  return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
@@ -1215,6 +1242,9 @@ function loadShared() {
1215
1242
  }
1216
1243
 
1217
1244
  function saveState(callback) {
1245
+ let savedFragmentEnd = fragmentEnd;
1246
+ let savedFragmentReader = fragmentReadString;
1247
+ let savedReadFixedString = readFixedString;
1218
1248
  let savedSrcEnd = srcEnd;
1219
1249
  let savedPosition = position$1;
1220
1250
  let savedStringPosition = stringPosition;
@@ -1224,6 +1254,7 @@ function saveState(callback) {
1224
1254
  let savedStrings = strings;
1225
1255
  let savedReferenceMap = referenceMap;
1226
1256
  let savedBundledStrings = bundledStrings$1;
1257
+ let savedPackedValues = packedValues;
1227
1258
 
1228
1259
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1229
1260
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1232,10 +1263,22 @@ function saveState(callback) {
1232
1263
  let savedSequentialMode = sequentialMode;
1233
1264
  let savedRestoreMapsAsObject = restoreMapsAsObject;
1234
1265
  let savedArrayAllocationBudget = arrayAllocationBudget;
1266
+ fragmentEnd = 0;
1267
+ if (fragmentReadString) readFixedString = fragmentReadString;
1268
+ fragmentReadString = null;
1235
1269
  restoreMapsAsObject = false;
1236
1270
  try {
1237
1271
  return callback()
1272
+ } catch (error) {
1273
+ // the callback decodes its own source (a shared structures blob, or an extension's embedded data), so a
1274
+ // truncation there cannot be completed by more of the outer input; checkedRead() clears the flag
1275
+ if (error && (typeof error == 'object' || typeof error == 'function'))
1276
+ nestedDecodeErrors.add(error);
1277
+ throw error
1238
1278
  } finally {
1279
+ fragmentEnd = savedFragmentEnd;
1280
+ fragmentReadString = savedFragmentReader;
1281
+ readFixedString = savedReadFixedString;
1239
1282
  srcEnd = savedSrcEnd;
1240
1283
  position$1 = savedPosition;
1241
1284
  stringPosition = savedStringPosition;
@@ -1245,6 +1288,7 @@ function saveState(callback) {
1245
1288
  strings = savedStrings;
1246
1289
  referenceMap = savedReferenceMap;
1247
1290
  bundledStrings$1 = savedBundledStrings;
1291
+ packedValues = savedPackedValues;
1248
1292
  src = savedSrc;
1249
1293
  sequentialMode = savedSequentialMode;
1250
1294
  currentStructures = savedStructures;
@@ -1260,8 +1304,87 @@ function clearSource() {
1260
1304
  currentStructures = null;
1261
1305
  }
1262
1306
 
1307
+ const registeredExtensions = new Map();
1308
+ let fragmentEnd = 0;
1309
+ let fragmentReadString;
1310
+
1311
+ const builtinScanTags = new Map([[LEGACY_RECORD_INLINE_ID, legacyRecordDefinition], [259, currentExtensions[259]]]);
1312
+ function isBuiltinScanTag(tag) {
1313
+ return builtinScanTags.has(tag) && currentExtensions[tag] === builtinScanTags.get(tag)
1314
+ }
1315
+
1316
+ function getScanTag(tag, decoder) {
1317
+ return tag >= BUNDLED_STRINGS_ID && decoder.structures && decoder.structures[tag & 0x1fff]
1318
+ }
1319
+
1320
+ function checkScanContainer(major, length, mapsAsObjects) {
1321
+ if (major === 4) {
1322
+ if (length > maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
1323
+ } else if (mapsAsObjects) {
1324
+ if (length > objectSizeLimit) throw new Error(`Property count exceeds ${objectSizeLimit}`)
1325
+ } else if (length > maxMapSize) throw new Error(`Map size exceeds ${maxMapSize}`)
1326
+ }
1327
+
1328
+ function decodeScanned(decoder, source, end) {
1329
+ const boundStrings = exports.isNativeAccelerationEnabled && end < source.length;
1330
+ if (!registeredExtensions.size && !boundStrings) return decoder.decodeMultiple(source)
1331
+ const savedEnd = fragmentEnd;
1332
+ const savedReader = readFixedString;
1333
+ const savedFragmentReader = fragmentReadString;
1334
+ const originals = [];
1335
+ fragmentEnd = end;
1336
+ fragmentReadString = savedReader;
1337
+ try {
1338
+ if (boundStrings) readFixedString = (length) => {
1339
+ if (!fragmentEnd || position$1 >= fragmentEnd) return savedReader(length)
1340
+ const savedSrcEnd = srcEnd;
1341
+ srcEnd = fragmentEnd;
1342
+ try { return savedReader(length) } finally { srcEnd = savedSrcEnd; }
1343
+ };
1344
+ for (const [tag, extension] of registeredExtensions) {
1345
+ if (!extension.handlesRead) continue
1346
+ const original = currentExtensions[tag];
1347
+ const guarded = (read) => {
1348
+ if (!fragmentEnd || position$1 >= fragmentEnd) return extension(read)
1349
+ let calls = 0, framingError;
1350
+ const value = extension(() => {
1351
+ if (calls++) {
1352
+ framingError = new Error('Tag ' + tag + ': fragmented decoding cannot frame multi-item handlesRead extensions');
1353
+ throw framingError
1354
+ }
1355
+ return read()
1356
+ });
1357
+ if (framingError) throw framingError
1358
+ return value
1359
+ };
1360
+ guarded.handlesRead = true;
1361
+ originals.push([tag, original, guarded]);
1362
+ currentExtensions[tag] = guarded;
1363
+ }
1364
+ return decoder.decodeMultiple(source)
1365
+ } finally {
1366
+ fragmentEnd = savedEnd;
1367
+ fragmentReadString = savedFragmentReader;
1368
+ readFixedString = savedReader;
1369
+ for (const [tag, original, guarded] of originals) {
1370
+ if (currentExtensions[tag] === guarded) currentExtensions[tag] = original;
1371
+ }
1372
+ }
1373
+ }
1374
+
1375
+ // Internal streaming hooks; not part of the public decoder API.
1376
+ const _stream = {
1377
+ getTag: getScanTag,
1378
+ isBuiltinTag: isBuiltinScanTag,
1379
+ checkContainer: checkScanContainer,
1380
+ decode: decodeScanned,
1381
+ get readThreshold() { return inlineObjectReadThreshold }
1382
+ };
1383
+
1263
1384
  function addExtension$1(extension) {
1264
1385
  currentExtensions[extension.tag] = extension.decode;
1386
+ if (typeof extension.decode === 'function') registeredExtensions.set(extension.tag, extension.decode);
1387
+ else registeredExtensions.delete(extension.tag);
1265
1388
  }
1266
1389
 
1267
1390
  function setSizeLimits(limits) {
@@ -1861,6 +1984,11 @@ class Encoder extends Decoder {
1861
1984
  return encode(json)
1862
1985
  }
1863
1986
 
1987
+ // not in extensions, so class instances that reach writeObject do not pay another instanceof
1988
+ if (constructor === DataView) {
1989
+ writeBuffer(new Uint8Array(value.buffer, value.byteOffset, value.byteLength), makeRoom);
1990
+ return
1991
+ }
1864
1992
  // no extension found, write as a plain object
1865
1993
  writeObject(value);
1866
1994
  }
@@ -1954,8 +2082,22 @@ class Encoder extends Decoder {
1954
2082
  size++;
1955
2083
  }
1956
2084
  }
1957
- target[objectOffset++ + start] = size >> 8;
1958
- target[objectOffset + start] = size & 0xff;
2085
+ if (size < 0x10000) {
2086
+ // fits in a map-16 (uint16) count: back-patch the reserved 2 bytes
2087
+ target[objectOffset++ + start] = size >> 8;
2088
+ target[objectOffset + start] = size & 0xff;
2089
+ } else {
2090
+ // too many entries for a map-16 count: widen the already-written header
2091
+ // to map-32, mirroring writeArrayHeader's 0x9a/setUint32 path. Shift the
2092
+ // encoded entries 2 bytes forward to make room for the wider (uint32) count.
2093
+ if (position + 2 > safeEnd)
2094
+ makeRoom(position + 2);
2095
+ let headerPosition = objectOffset + start;
2096
+ target.copyWithin(headerPosition + 4, headerPosition + 2, position);
2097
+ position += 2;
2098
+ target[headerPosition - 1] = 0xba; // map 32
2099
+ targetView.setUint32(headerPosition, size);
2100
+ }
1959
2101
  } :
1960
2102
  (object, skipValues) => {
1961
2103
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
@@ -2522,59 +2664,256 @@ const REUSE_BUFFER_MODE = 512;
2522
2664
  const RESET_BUFFER_MODE = 1024;
2523
2665
  const THROW_ON_ITERABLE = 2048;
2524
2666
 
2525
- class EncoderStream extends stream.Transform {
2526
- constructor(options) {
2527
- if (!options)
2528
- options = {};
2529
- options.writableObjectMode = true;
2530
- super(options);
2531
- options.sequential = true;
2532
- this.encoder = options.encoder || new Encoder(options);
2533
- }
2534
- async _transform(value, encoding, callback) {
2535
- try {
2536
- for await (let chunk of this.encoder.encodeAsAsyncIterable(value)) {
2537
- this.push(chunk);
2538
- }
2539
- callback();
2540
- } catch(error) { callback (error); }
2541
- }
2542
- }
2667
+ // Positions are relative to the retained value, including bundle pointers. Frames and their storage survive appends.
2668
+ class StructureScanner {
2669
+ constructor (decoder) {
2670
+ this.decoder = decoder;
2671
+ this.frames = [];
2672
+ this.records = new Map();
2673
+ this.readers = new Map();
2674
+ this.reset();
2675
+ }
2543
2676
 
2544
- class DecoderStream extends stream.Transform {
2545
- constructor(options) {
2546
- if (!options)
2547
- options = {};
2548
- options.objectMode = true;
2549
- super(options);
2550
- options.structures = [];
2551
- this.decoder = options.decoder || new Decoder(options);
2552
- }
2553
- _transform(chunk, encoding, callback) {
2554
- if (this.incompleteBuffer) {
2555
- chunk = Buffer.concat([this.incompleteBuffer, chunk]);
2556
- this.incompleteBuffer = null;
2557
- }
2558
- let values, fatalError;
2559
- try {
2560
- values = this.decoder.decodeMultiple(chunk);
2561
- } catch(error) {
2562
- values = error.values;
2563
- if (error.incomplete)
2564
- this.incompleteBuffer = chunk.slice(error.lastPosition);
2565
- else
2566
- fatalError = error;
2567
- }
2568
- for (let value of values || []) {
2569
- if (value === null)
2570
- value = this.getNullValue();
2571
- this.push(value);
2572
- }
2573
- callback(fatalError);
2574
- }
2575
- getNullValue() {
2576
- return Symbol.for(null)
2577
- }
2677
+ reset () {
2678
+ this.position = 0;
2679
+ this.depth = 0;
2680
+ this.neededLength = 0;
2681
+ this.postBundlePosition = 0;
2682
+ this.headers = 0;
2683
+ this.mapsAsObjects = !!this.decoder.mapsAsObjects;
2684
+ this.restoreMap = false;
2685
+ this.records.clear();
2686
+ this.readers.clear();
2687
+ this.push('items', 1);
2688
+ }
2689
+
2690
+ push (kind, remaining) {
2691
+ if (this.depth === 16384) throw new RangeError('Maximum CBOR nesting depth exceeded')
2692
+ const frame = this.frames[this.depth] || (this.frames[this.depth] = {});
2693
+ frame.kind = kind;
2694
+ frame.remaining = remaining;
2695
+ frame.stage = 0;
2696
+ frame.id = undefined;
2697
+ frame.restoreMap = false;
2698
+ frame.legacy = false;
2699
+ frame.definition = undefined;
2700
+ frame.limitMajor = 0;
2701
+ frame.count = 0;
2702
+ this.depth++;
2703
+ return frame
2704
+ }
2705
+
2706
+ header (source, end, lengthOnly = false) {
2707
+ const start = this.position;
2708
+ if (start >= end) {
2709
+ this.neededLength = start + 1;
2710
+ return false
2711
+ }
2712
+ const byte = source[start];
2713
+ const major = byte >> 5;
2714
+ const info = byte & 31;
2715
+ const width = info < 24 || (lengthOnly && info > 26) ? 0 : info === 24 ? 1 : info === 25 ? 2 : info === 26 ? 4 : info === 27 ? 8 : 0;
2716
+ if (!lengthOnly && info >= 28 && info <= 30) throw new Error('Unknown token ' + info)
2717
+ if (start + 1 + width > end) {
2718
+ this.neededLength = start + 1 + width;
2719
+ return false
2720
+ }
2721
+ let value = info;
2722
+ if (width) {
2723
+ value = 0;
2724
+ for (let i = start + 1; i <= start + width; i++) value = value * 256 + source[i];
2725
+ if (width === 8 && major > 1 && major !== 7 && value > 0xffffffff) {
2726
+ throw new Error('JavaScript does not support arrays, maps, or strings with length over 4294967295')
2727
+ }
2728
+ }
2729
+ this.major = major;
2730
+ this.info = info;
2731
+ this.argument = value;
2732
+ this.position = start + 1 + width;
2733
+ this.headers++;
2734
+ return true
2735
+ }
2736
+
2737
+ scan (source, end) {
2738
+ while (this.depth) {
2739
+ if (this.position > end) {
2740
+ this.neededLength = this.position;
2741
+ return false
2742
+ }
2743
+ const frame = this.frames[this.depth - 1];
2744
+ if (frame.kind === 'bundle') {
2745
+ if (frame.stage === 0) {
2746
+ if (!this.header(source, end, true)) return false
2747
+ frame.remaining = Math.max(0, this.argument - 2);
2748
+ frame.stage = 1;
2749
+ }
2750
+ if (frame.stage === 1) {
2751
+ const offsetStart = this.position;
2752
+ if (!this.header(source, end)) return false
2753
+ if (this.major > 1 || this.info === 31) throw new Error('Invalid bundled string offset')
2754
+ frame.target = offsetStart + (this.major === 1 ? -1 - this.argument : this.argument);
2755
+ if (!Number.isSafeInteger(frame.target) || frame.target < 0) throw new Error('Invalid bundled string offset')
2756
+ frame.stage = 2;
2757
+ }
2758
+ if (frame.stage === 2) {
2759
+ if (frame.remaining) {
2760
+ if (!this.header(source, end, true)) return false
2761
+ this.position += this.argument;
2762
+ frame.remaining--;
2763
+ continue
2764
+ }
2765
+ frame.resume = this.position;
2766
+ this.position = frame.target;
2767
+ frame.remaining = 2;
2768
+ frame.stage = 3;
2769
+ continue
2770
+ }
2771
+ if (frame.stage === 3) {
2772
+ if (frame.remaining) {
2773
+ if (!this.header(source, end, true)) return false
2774
+ this.position += this.argument;
2775
+ frame.remaining--;
2776
+ continue
2777
+ }
2778
+ this.postBundlePosition = this.position;
2779
+ this.position = frame.resume;
2780
+ frame.kind = 'items';
2781
+ frame.remaining = 1;
2782
+ continue
2783
+ }
2784
+ }
2785
+ if (frame.kind === 'record') {
2786
+ if (!this.header(source, end, true)) return false
2787
+ if (this.info > 26) throw new Error('Expected array header, but got ' + source[this.position - 1])
2788
+ frame.kind = 'items';
2789
+ frame.remaining = this.argument;
2790
+ const reader = frame.reader;
2791
+ if (reader.compiled.has(this.argument)) frame.remaining = Math.min(this.argument, reader.length);
2792
+ else if (reader.slowReads++ >= _stream.readThreshold) {
2793
+ reader.compiled.add(this.argument);
2794
+ frame.remaining = Math.min(this.argument, reader.length);
2795
+ }
2796
+ continue
2797
+ }
2798
+ if (frame.kind === 'definition' && frame.stage === 0) {
2799
+ const start = this.position;
2800
+ if (!this.header(source, end, !frame.legacy)) return false
2801
+ if (frame.legacy && this.major !== 4) {
2802
+ this.position = start;
2803
+ this.headers--;
2804
+ frame.kind = 'items';
2805
+ frame.remaining = 1;
2806
+ continue
2807
+ }
2808
+ frame.remaining = frame.legacy ? (this.info === 31 ? -1 : this.argument) : Math.max(2, this.argument);
2809
+ if (frame.legacy) {
2810
+ if (frame.remaining >= 0) _stream.checkContainer(4, frame.remaining);
2811
+ else frame.limitMajor = 4;
2812
+ frame.index = 0;
2813
+ }
2814
+ frame.total = frame.remaining;
2815
+ frame.stage = 1;
2816
+ continue
2817
+ }
2818
+ if (frame.remaining === 0) {
2819
+ if (frame.definition) this.records.set(frame.id - 0xe000, frame.definition);
2820
+ if (frame.restoreMap && this.restoreMap) {
2821
+ this.mapsAsObjects = true;
2822
+ this.restoreMap = false;
2823
+ }
2824
+ this.depth--;
2825
+ continue
2826
+ }
2827
+ if (!this.header(source, end)) return false
2828
+ let { major, argument, info } = this;
2829
+ if (frame.kind === 'definition') {
2830
+ const index = frame.legacy ? frame.index++ : frame.total - frame.remaining;
2831
+ if (index === 0 && major === 0) frame.id = argument;
2832
+ if ((frame.multiple ? index >= 1 && index < frame.total - 1 : index === 1) && major === 4 && info !== 31 && frame.id !== undefined) {
2833
+ const definition = { length: argument, slowReads: 0, compiled: new Set() };
2834
+ if (frame.legacy) frame.definition = definition;
2835
+ else this.records.set(frame.id - 0xe000 + (frame.multiple ? index - 1 : 0), definition);
2836
+ }
2837
+ }
2838
+ if (major === 7 && info === 31 && (frame.remaining === -1 || frame.remaining === -2)) {
2839
+ if (frame.definition) this.records.set(frame.id - 0xe000, frame.definition);
2840
+ this.depth--;
2841
+ continue
2842
+ }
2843
+ if (frame.limitMajor && (frame.remaining === -1 || frame.remaining === -2)) {
2844
+ _stream.checkContainer(frame.limitMajor, ++frame.count, frame.mapsAsObjects);
2845
+ }
2846
+ if (frame.remaining > 0) frame.remaining--;
2847
+ else if (frame.remaining === -2) frame.remaining = -3;
2848
+ else if (frame.remaining === -3) frame.remaining = -2;
2849
+ if (info === 31) {
2850
+ if (major === 2 || major === 3) throw new Error('Indefinite length not supported for byte or text strings')
2851
+ if (major === 4 || major === 5) {
2852
+ const container = this.push('items', major === 4 ? -1 : -2);
2853
+ container.limitMajor = major;
2854
+ container.mapsAsObjects = this.mapsAsObjects;
2855
+ if (major === 5 && this.restoreMap) {
2856
+ this.mapsAsObjects = true;
2857
+ this.restoreMap = false;
2858
+ }
2859
+ }
2860
+ else if (major === 7) throw new Error('Unexpected break stop code outside of an indefinite-length item')
2861
+ else throw new Error('Invalid major type for indefinite length ' + major)
2862
+ continue
2863
+ }
2864
+ switch (major) {
2865
+ case 2:
2866
+ case 3:
2867
+ this.position += argument;
2868
+ break
2869
+ case 4:
2870
+ case 5:
2871
+ _stream.checkContainer(major, argument, this.mapsAsObjects);
2872
+ if (major === 5 && this.restoreMap) {
2873
+ this.mapsAsObjects = true;
2874
+ this.restoreMap = false;
2875
+ }
2876
+ if (argument) this.push('items', major === 5 ? argument * 2 : argument);
2877
+ break
2878
+ case 6: {
2879
+ const structure = _stream.getTag(argument, this.decoder);
2880
+ let reader = this.records.get(argument & 0x1fff);
2881
+ if (argument >= 0xdff9 && (structure || reader)) {
2882
+ if (!reader) {
2883
+ reader = this.readers.get(structure);
2884
+ if (!reader) {
2885
+ reader = { length: structure.length, slowReads: structure.slowReads || 0, compiled: new Set() };
2886
+ for (let compiled = structure.compiledReader; compiled; compiled = compiled.next) reader.compiled.add(compiled.propertyCount);
2887
+ this.readers.set(structure, reader);
2888
+ }
2889
+ }
2890
+ this.push('record').reader = reader;
2891
+ } else if (argument === 0xdff9) {
2892
+ this.push('bundle');
2893
+ } else if (argument === 0xdfff || argument === 0xdffe) {
2894
+ this.push('definition').multiple = argument === 0xdffe;
2895
+ } else if (argument === 259 && _stream.isBuiltinTag(argument) && this.mapsAsObjects) {
2896
+ this.push('items', 1).restoreMap = true;
2897
+ this.mapsAsObjects = false;
2898
+ this.restoreMap = true;
2899
+ } else if (argument === 105 && _stream.isBuiltinTag(argument)) {
2900
+ const definition = this.push('definition');
2901
+ definition.multiple = false;
2902
+ definition.legacy = true;
2903
+ } else {
2904
+ this.push('items', 1);
2905
+ }
2906
+ break
2907
+ }
2908
+ }
2909
+ }
2910
+ if (this.postBundlePosition) {
2911
+ if (this.position >= this.postBundlePosition) throw new Error('Unexpected bundle position')
2912
+ this.position = this.postBundlePosition;
2913
+ }
2914
+ if (this.frames.length > 256) this.frames.length = 256;
2915
+ return true
2916
+ }
2578
2917
  }
2579
2918
 
2580
2919
  /**
@@ -2614,7 +2953,7 @@ async function * encodeIterAsync (objectIterator, options) {
2614
2953
  * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
2615
2954
  * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
2616
2955
  * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
2617
- * @param {object} [options] - Decoder options
2956
+ * @param {object} [options] - Decoder options, and maxIncompleteBufferSize
2618
2957
  * @returns {IterableIterator|Promise.<AsyncIterableIterator}
2619
2958
  */
2620
2959
  function decodeIter (bufferIterator, options = {}) {
@@ -2623,26 +2962,15 @@ function decodeIter (bufferIterator, options = {}) {
2623
2962
  }
2624
2963
 
2625
2964
  const decoder = new Decoder(options);
2626
- let incomplete, fatalError;
2965
+ const chunkDecoder = new ChunkDecoder(decoder, options.maxIncompleteBufferSize);
2966
+ let fatalError;
2627
2967
  const parser = (chunk) => {
2628
- let yields;
2629
- // if there's incomplete data from previous chunk, concatinate and try again
2630
- if (incomplete) {
2631
- chunk = Buffer.concat([incomplete, chunk]);
2632
- incomplete = undefined;
2633
- }
2634
-
2635
2968
  try {
2636
- yields = decoder.decodeMultiple(chunk);
2637
- } catch (err) {
2638
- yields = err.values;
2639
- if (err.incomplete) {
2640
- incomplete = chunk.slice(err.lastPosition);
2641
- } else {
2642
- fatalError = err;
2643
- }
2969
+ return chunkDecoder.buffer ? chunkDecoder.decodeAfterIncomplete(chunk) : decoder.decodeMultiple(chunk)
2970
+ } catch (error) {
2971
+ fatalError = chunkDecoder.keepIncomplete(error, chunk);
2972
+ return (error && error.values) || []
2644
2973
  }
2645
- return yields || []
2646
2974
  };
2647
2975
 
2648
2976
  if (typeof bufferIterator[Symbol.iterator] === 'function') {
@@ -2651,6 +2979,8 @@ function decodeIter (bufferIterator, options = {}) {
2651
2979
  yield * parser(value);
2652
2980
  if (fatalError) throw fatalError
2653
2981
  }
2982
+ const endError = chunkDecoder.end();
2983
+ if (endError) throw endError
2654
2984
  })()
2655
2985
  } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
2656
2986
  return (async function * iter () {
@@ -2658,10 +2988,175 @@ function decodeIter (bufferIterator, options = {}) {
2658
2988
  yield * parser(value);
2659
2989
  if (fatalError) throw fatalError
2660
2990
  }
2991
+ const endError = chunkDecoder.end();
2992
+ if (endError) throw endError
2661
2993
  })()
2662
2994
  }
2663
2995
  }
2664
2996
 
2997
+ /**
2998
+ * Keeps the bytes of a value that a chunk of a stream ends inside of, for DecoderStream and decodeIter, and decodes
2999
+ * them again once enough have arrived to possibly complete it. Callers decode a chunk with decodeAfterIncomplete if
3000
+ * buffer is set, or else directly with decodeMultiple, and pass any error to keepIncomplete, which returns the error
3001
+ * to report, if any. end returns an error if the input ended inside a value.
3002
+ */
3003
+ class ChunkDecoder {
3004
+ constructor (decoder, maxIncompleteBufferSize) {
3005
+ this.decoder = decoder;
3006
+ if (maxIncompleteBufferSize === undefined) maxIncompleteBufferSize = 0x4000000;
3007
+ if (typeof maxIncompleteBufferSize !== 'number' || !(maxIncompleteBufferSize >= 0)) {
3008
+ throw new TypeError('maxIncompleteBufferSize must be a non-negative number')
3009
+ }
3010
+ this.maxIncompleteBufferSize = Math.floor(maxIncompleteBufferSize);
3011
+ // buffer[0, bufferEnd) is the incomplete value; no decoded value is a view of buffer while it is kept here
3012
+ this.buffer = null;
3013
+ this.bufferEnd = 0;
3014
+ this.appends = 0;
3015
+ this.scanner = null;
3016
+ this.scanned = false;
3017
+ this.resetScanner = false;
3018
+ this.neededLength = 0; // a lower bound on the incomplete value's length
3019
+ }
3020
+
3021
+ decodeAfterIncomplete (chunk) {
3022
+ if (!(ArrayBuffer.isView(chunk) && chunk.BYTES_PER_ELEMENT === 1)) {
3023
+ throw new TypeError('Source must be a Uint8Array or Buffer')
3024
+ }
3025
+ const length = this.bufferEnd + chunk.length;
3026
+ if (length < this.neededLength && length > this.maxIncompleteBufferSize) throw maxIncompleteBufferSizeError()
3027
+ if (length > this.buffer.length) {
3028
+ // Values usually complete within two appends, which are sized exactly. After that the buffer grows by at least
3029
+ // the bytes already kept, so that they are copied a bounded number of times however small the chunks are.
3030
+ const size = this.appends < 2 ? length
3031
+ : Math.max(length, Math.min(length + this.bufferEnd, this.maxIncompleteBufferSize));
3032
+ const buffer = Buffer.allocUnsafe(size);
3033
+ if (size > length) buffer.fill(0, length); // decoded values are views of this buffer, so no uninitialized memory
3034
+ buffer.set(this.buffer.subarray(0, this.bufferEnd));
3035
+ this.buffer = buffer;
3036
+ }
3037
+ this.buffer.set(chunk, this.bufferEnd);
3038
+ this.bufferEnd = length;
3039
+ this.appends++;
3040
+ if (length < this.neededLength) return []
3041
+ if (!this.scanner) this.scanner = new StructureScanner(this.decoder);
3042
+ if (this.resetScanner) {
3043
+ this.scanner.reset();
3044
+ this.resetScanner = false;
3045
+ }
3046
+ if (!this.scanner.scan(this.buffer, length)) {
3047
+ this.neededLength = this.scanner.neededLength;
3048
+ if (length > this.maxIncompleteBufferSize) throw maxIncompleteBufferSizeError()
3049
+ return []
3050
+ }
3051
+ this.scanned = true;
3052
+ const values = _stream.decode(this.decoder, this.buffer.subarray(0, length), this.scanner.position);
3053
+ this.scanned = false;
3054
+ this.buffer = null;
3055
+ return values
3056
+ }
3057
+
3058
+ keepIncomplete (error, chunk) {
3059
+ // an extension may throw any value, and a falsy one would read as success
3060
+ if (!error) return this.fail(new Error('Decoding failed: ' + error))
3061
+ if (!error.incomplete) return this.fail(error)
3062
+ if (this.scanned && error.lastPosition === 0) {
3063
+ error.incomplete = false;
3064
+ return this.fail(error)
3065
+ }
3066
+ const source = this.buffer ? this.buffer.subarray(0, this.bufferEnd) : chunk;
3067
+ const length = source.length - error.lastPosition;
3068
+ if (length > this.maxIncompleteBufferSize) return this.fail(maxIncompleteBufferSizeError())
3069
+ if (!this.buffer || error.lastPosition > 0) {
3070
+ if (length === 0) return
3071
+ try {
3072
+ // copied, so that the remainder does not keep in memory the chunk, or the buffer that the values before it
3073
+ // are views of
3074
+ this.buffer = Buffer.from(source.subarray(error.lastPosition));
3075
+ } catch (copyError) {
3076
+ return this.fail(copyError)
3077
+ }
3078
+ this.bufferEnd = length;
3079
+ this.appends = 0;
3080
+ }
3081
+ this.scanned = false;
3082
+ this.resetScanner = true;
3083
+ this.neededLength = Math.max(length + 1, error.neededEnd ? error.neededEnd - error.lastPosition : 0);
3084
+ }
3085
+
3086
+ end () {
3087
+ if (this.buffer) {
3088
+ const error = this.fail(new Error('Unexpected end of CBOR data'));
3089
+ error.incomplete = true;
3090
+ return error
3091
+ }
3092
+ }
3093
+
3094
+ fail (error) {
3095
+ this.buffer = null;
3096
+ this.scanner = null;
3097
+ this.scanned = false;
3098
+ this.resetScanner = false;
3099
+ return error
3100
+ }
3101
+ }
3102
+
3103
+ function maxIncompleteBufferSizeError () {
3104
+ return new Error('Maximum incomplete buffer size exceeded')
3105
+ }
3106
+
3107
+ class EncoderStream extends stream.Transform {
3108
+ constructor(options) {
3109
+ if (!options)
3110
+ options = {};
3111
+ options.writableObjectMode = true;
3112
+ super(options);
3113
+ options.sequential = true;
3114
+ this.encoder = options.encoder || new Encoder(options);
3115
+ }
3116
+ async _transform(value, encoding, callback) {
3117
+ try {
3118
+ for await (let chunk of this.encoder.encodeAsAsyncIterable(value)) {
3119
+ this.push(chunk);
3120
+ }
3121
+ callback();
3122
+ } catch(error) { callback (error); }
3123
+ }
3124
+ }
3125
+
3126
+ class DecoderStream extends stream.Transform {
3127
+ constructor(options) {
3128
+ if (!options)
3129
+ options = {};
3130
+ options.objectMode = true;
3131
+ super(options);
3132
+ options.structures = [];
3133
+ this.decoder = options.decoder || new Decoder(options);
3134
+ this.chunkDecoder = new ChunkDecoder(this.decoder, options.maxIncompleteBufferSize);
3135
+ }
3136
+ _transform(chunk, encoding, callback) {
3137
+ let values, fatalError;
3138
+ try {
3139
+ // decodeMultiple is called directly, since another call level here measurably slows small writes
3140
+ values = this.chunkDecoder.buffer ? this.chunkDecoder.decodeAfterIncomplete(chunk) : this.decoder.decodeMultiple(chunk);
3141
+ } catch(error) {
3142
+ values = error && error.values;
3143
+ fatalError = this.chunkDecoder.keepIncomplete(error, chunk);
3144
+ }
3145
+ for (let value of values || []) {
3146
+ if (value === null)
3147
+ value = this.getNullValue();
3148
+ this.push(value);
3149
+ }
3150
+ callback(fatalError);
3151
+ }
3152
+ _flush(callback) {
3153
+ callback(this.chunkDecoder.end());
3154
+ }
3155
+ getNullValue() {
3156
+ return Symbol.for(null)
3157
+ }
3158
+ }
3159
+
2665
3160
  const useRecords = false;
2666
3161
  const mapsAsObjects = true;
2667
3162