cbor-x 1.4.1 → 1.5.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/README.md CHANGED
@@ -52,6 +52,26 @@ receivingStream.on('data', (data) => {
52
52
  ```
53
53
  The `EncoderStream` and `DecoderStream` instances will have also the record structure extension enabled by default (see below).
54
54
 
55
+ ### Iterables
56
+ In addition to using CBOR with streams, CBOR can also encode to an iterable that can be iterated as a sequence of binary chunks with `encodeAsIterable`, which facilitates progressive encoding:
57
+ ```JavaScript
58
+ import { encodeAsIterable } from 'cbor-x';
59
+
60
+ for (let binaryChunk of encodeAsIterable(data)){
61
+ // progressively get binary chunks as data is encoded
62
+ }
63
+ ```
64
+ And `encodeAsAsyncIterable` is also available, which returns an async iterable, and can be used to encode data from async iterables as well as Blob data.
65
+
66
+ ```JavaScript
67
+ import { encodeAsAsyncIterable } from 'cbor-x';
68
+
69
+ let data = { blob: new Blob(...) };
70
+ for await (let binaryChunk of encodeAsAsyncIterable(data)){
71
+ // progressively get binary chunks as asynchronous data source is encoded
72
+ }
73
+ ```
74
+
55
75
  ## Deno Usage
56
76
  Cbor-x modules are standard ESM modules and can be loaded directly from the [deno.land registry for cbor](https://deno.land/x/cbor) for use in Deno. The standard pack/encode and unpack/decode functionality is available on Deno, like other platforms.
57
77
 
package/decode.js CHANGED
@@ -197,6 +197,7 @@ export function checkedRead() {
197
197
  }
198
198
  // bundled strings to skip past
199
199
  position = bundledStrings.postBundlePosition;
200
+ bundledStrings = null;
200
201
  }
201
202
 
202
203
  if (position == srcEnd) {
@@ -360,13 +361,28 @@ export function read() {
360
361
  return structure.read()
361
362
  }
362
363
  if (token < 0x10000) {
363
- if (token == RECORD_INLINE_ID) // we do a special check for this so that we can keep the currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
364
- return recordDefinition(read())
364
+ if (token == RECORD_INLINE_ID) { // we do a special check for this so that we can keep the
365
+ // currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
366
+ let length = readJustLength()
367
+ let id = read()
368
+ let structure = read()
369
+ recordDefinition(id, structure)
370
+ let object = {}
371
+ if (currentDecoder.keyMap) for (let i = 2; i < length; i++) {
372
+ let key = currentDecoder.decodeKey(structure[i - 2])
373
+ object[safeKey(key)] = read()
374
+ }
375
+ else for (let i = 2; i < length; i++) {
376
+ let key = structure[i - 2]
377
+ object[safeKey(key)] = read()
378
+ }
379
+ return object
380
+ }
365
381
  else if (token == RECORD_DEFINITIONS_ID) {
366
382
  let length = readJustLength()
367
383
  let id = read()
368
384
  for (let i = 2; i < length; i++) {
369
- recordDefinition([id++, read()])
385
+ recordDefinition(id++, read())
370
386
  }
371
387
  return read()
372
388
  } else if (token == BUNDLED_STRINGS_ID) {
@@ -874,9 +890,8 @@ currentExtensions[5] = (fraction) => {
874
890
  }
875
891
 
876
892
  // the registration of the record definition extension
877
- const recordDefinition = (definition) => {
878
- let id = definition[0] - 0xe000
879
- let structure = definition[1]
893
+ const recordDefinition = (id, structure) => {
894
+ id = id - 0xe000
880
895
  let existingStructure = currentStructures[id]
881
896
  if (existingStructure && existingStructure.isShared) {
882
897
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure
@@ -884,18 +899,18 @@ const recordDefinition = (definition) => {
884
899
  currentStructures[id] = structure
885
900
 
886
901
  structure.read = createStructureReader(structure)
902
+ }
903
+ currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => {
904
+ let length = data.length
905
+ let structure = data[1]
906
+ recordDefinition(data[0], structure)
887
907
  let object = {}
888
- if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
889
- let key = currentDecoder.decodeKey(structure[i - 2])
890
- object[safeKey(key)] = definition[i]
891
- }
892
- else for (let i = 2,l = definition.length; i < l; i++) {
893
- let key = structure[i - 2]
894
- object[safeKey(key)] = definition[i]
895
- }
908
+ for (let i = 2; i < length; i++) {
909
+ let key = structure[i - 2]
910
+ object[safeKey(key)] = data[i]
911
+ }
896
912
  return object
897
913
  }
898
- currentExtensions[LEGACY_RECORD_INLINE_ID] = recordDefinition
899
914
  currentExtensions[14] = (value) => {
900
915
  if (bundledStrings)
901
916
  return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value)
@@ -933,18 +948,23 @@ currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference
933
948
  return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
934
949
  throw new Error('No support for non-integer packed references yet')
935
950
  }
936
- currentExtensions[25] = (id) => {
937
- return stringRefs[id]
938
- }
939
- currentExtensions[256] = (read) => {
940
- stringRefs = []
941
- try {
942
- return read()
943
- } finally {
944
- stringRefs = null
945
- }
946
- }
947
- currentExtensions[256].handlesRead = true
951
+
952
+ // The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
953
+ // the real thing would need to implemennt more logic to populate the stringRefs table and
954
+ // maintain a stack of stringRef "namespaces".
955
+ //
956
+ // currentExtensions[25] = (id) => {
957
+ // return stringRefs[id]
958
+ // }
959
+ // currentExtensions[256] = (read) => {
960
+ // stringRefs = []
961
+ // try {
962
+ // return read()
963
+ // } finally {
964
+ // stringRefs = null
965
+ // }
966
+ // }
967
+ // currentExtensions[256].handlesRead = true
948
968
 
949
969
  currentExtensions[28] = (read) => {
950
970
  // shareable http://cbor.schmorp.de/value-sharing (for structured clones)