msgpackr 1.7.0-alpha1 → 1.7.0-alpha4

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/unpack.js CHANGED
@@ -28,7 +28,7 @@ export const C1 = new C1Type()
28
28
  C1.name = 'MessagePack 0xC1'
29
29
  var sequentialMode = false
30
30
  var inlineObjectReadThreshold = 2
31
- var readStruct
31
+ var readStruct, onLoadedStructures
32
32
  try {
33
33
  new Function('')
34
34
  } catch(error) {
@@ -58,16 +58,21 @@ export class Unpackr {
58
58
  }
59
59
  Object.assign(this, options)
60
60
  }
61
- unpack(source, end) {
61
+ unpack(source, options) {
62
62
  if (src) {
63
63
  // re-entrant execution, save the state and restore it after we do this unpack
64
64
  return saveState(() => {
65
65
  clearSource()
66
- return this ? this.unpack(source, end) : Unpackr.prototype.unpack.call(defaultOptions, source, end)
66
+ return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
67
67
  })
68
68
  }
69
- srcEnd = end > -1 ? end : source.length
70
- position = 0
69
+ if (typeof options === 'object') {
70
+ srcEnd = options.end || source.length
71
+ position = options.start || 0
72
+ } else {
73
+ position = 0
74
+ srcEnd = options > -1 ? options : source.length
75
+ }
71
76
  stringPosition = 0
72
77
  srcStringEnd = 0
73
78
  srcString = null
@@ -134,7 +139,11 @@ export class Unpackr {
134
139
  }
135
140
  }
136
141
  _mergeStructures(loadedStructures, existingStructures) {
142
+ if (onLoadedStructures)
143
+ loadedStructures = onLoadedStructures.call(this, loadedStructures);
137
144
  loadedStructures = loadedStructures || []
145
+ if (Object.isFrozen(loadedStructures))
146
+ loadedStructures = loadedStructures.map(structure => structure.slice(0))
138
147
  for (let i = 0, l = loadedStructures.length; i < l; i++) {
139
148
  let structure = loadedStructures[i]
140
149
  if (structure) {
@@ -172,9 +181,8 @@ export function checkedRead() {
172
181
  currentStructures.length = sharedLength
173
182
  }
174
183
  let result
175
- if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && readStruct) {
176
- let id = (src[position++] << 8) + src[position++];
177
- result = readStruct(src, position, srcEnd, currentStructures[id - 0x40] || loadStructures()[id - 0x40], currentUnpackr)
184
+ if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
185
+ result = readStruct(src, position, srcEnd, currentUnpackr)
178
186
  position = srcEnd
179
187
  } else
180
188
  result = read()
@@ -504,7 +512,7 @@ const createSecondByteReader = (firstId, read0) => {
504
512
  }
505
513
  }
506
514
 
507
- function loadStructures() {
515
+ export function loadStructures() {
508
516
  let loadedStructures = saveState(() => {
509
517
  // save the state in case getStructures modifies our buffer
510
518
  src = null
@@ -610,6 +618,16 @@ function readStringJS(length) {
610
618
 
611
619
  return result
612
620
  }
621
+ export function readString(source, start, length) {
622
+ let existingSrc = src;
623
+ src = source;
624
+ position = start;
625
+ try {
626
+ return readStringJS(length);
627
+ } finally {
628
+ src = existingSrc;
629
+ }
630
+ }
613
631
 
614
632
  function readArray(length) {
615
633
  let array = new Array(length)
@@ -830,7 +848,15 @@ function readBin(length) {
830
848
  function readExt(length) {
831
849
  let type = src[position++]
832
850
  if (currentExtensions[type]) {
833
- return currentExtensions[type](src.subarray(position, position += length))
851
+ let end
852
+ return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
853
+ position = readPosition;
854
+ try {
855
+ return read();
856
+ } finally {
857
+ position = end;
858
+ }
859
+ })
834
860
  }
835
861
  else
836
862
  throw new Error('Unknown extension type ' + type)
@@ -1073,6 +1099,7 @@ export function roundFloat32(float32Number) {
1073
1099
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)]
1074
1100
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1075
1101
  }
1076
- export function setReadStruct(func) {
1077
- readStruct = func;
1102
+ export function setReadStruct(updatedReadStruct, loadedStructs) {
1103
+ readStruct = updatedReadStruct;
1104
+ onLoadedStructures = loadedStructs;
1078
1105
  }