msgpackr 1.7.0-alpha2 → 1.7.0-alpha5

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
@@ -90,7 +95,7 @@ export class Unpackr {
90
95
  currentUnpackr = this
91
96
  if (this.structures) {
92
97
  currentStructures = this.structures
93
- return checkedRead()
98
+ return checkedRead(options)
94
99
  } else if (!currentStructures || currentStructures.length > 0) {
95
100
  currentStructures = []
96
101
  }
@@ -99,7 +104,7 @@ export class Unpackr {
99
104
  if (!currentStructures || currentStructures.length > 0)
100
105
  currentStructures = []
101
106
  }
102
- return checkedRead()
107
+ return checkedRead(options)
103
108
  }
104
109
  unpackMultiple(source, forEach) {
105
110
  let values, lastPosition = 0
@@ -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) {
@@ -164,7 +173,7 @@ export class Unpackr {
164
173
  export function getPosition() {
165
174
  return position
166
175
  }
167
- export function checkedRead() {
176
+ export function checkedRead(options) {
168
177
  try {
169
178
  if (!currentUnpackr.trusted && !sequentialMode) {
170
179
  let sharedLength = currentStructures.sharedLength || 0
@@ -172,9 +181,10 @@ 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)
186
+ if (!(options && options.lazy) && result)
187
+ result = result.toJSON()
178
188
  position = srcEnd
179
189
  } else
180
190
  result = read()
@@ -504,7 +514,7 @@ const createSecondByteReader = (firstId, read0) => {
504
514
  }
505
515
  }
506
516
 
507
- function loadStructures() {
517
+ export function loadStructures() {
508
518
  let loadedStructures = saveState(() => {
509
519
  // save the state in case getStructures modifies our buffer
510
520
  src = null
@@ -610,6 +620,16 @@ function readStringJS(length) {
610
620
 
611
621
  return result
612
622
  }
623
+ export function readString(source, start, length) {
624
+ let existingSrc = src;
625
+ src = source;
626
+ position = start;
627
+ try {
628
+ return readStringJS(length);
629
+ } finally {
630
+ src = existingSrc;
631
+ }
632
+ }
613
633
 
614
634
  function readArray(length) {
615
635
  let array = new Array(length)
@@ -830,7 +850,15 @@ function readBin(length) {
830
850
  function readExt(length) {
831
851
  let type = src[position++]
832
852
  if (currentExtensions[type]) {
833
- return currentExtensions[type](src.subarray(position, position += length))
853
+ let end
854
+ return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
855
+ position = readPosition;
856
+ try {
857
+ return read();
858
+ } finally {
859
+ position = end;
860
+ }
861
+ })
834
862
  }
835
863
  else
836
864
  throw new Error('Unknown extension type ' + type)
@@ -1073,6 +1101,7 @@ export function roundFloat32(float32Number) {
1073
1101
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)]
1074
1102
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1075
1103
  }
1076
- export function setReadStruct(func) {
1077
- readStruct = func;
1104
+ export function setReadStruct(updatedReadStruct, loadedStructs) {
1105
+ readStruct = updatedReadStruct;
1106
+ onLoadedStructures = loadedStructs;
1078
1107
  }
package/dist/str.cjs DELETED
@@ -1,100 +0,0 @@
1
- let utfz = require('../../msgpack-benchmark/node_modules/utfz-lib')
2
-
3
- var safeEnd = 1000000;
4
- var b = new Uint8Array(32768)
5
- function writeString(value, target, position) {
6
- var length, strLength = value.length;
7
- let headerSize;
8
- // first we estimate the header size, so we can write to the correct location
9
- if (strLength < 0x20) {
10
- headerSize = 1;
11
- } else if (strLength < 0x100) {
12
- headerSize = 2;
13
- } else if (strLength < 0x10000) {
14
- headerSize = 3;
15
- } else {
16
- headerSize = 5;
17
- }
18
- let maxBytes = strLength * 3;
19
- //if (position + maxBytes > safeEnd)
20
- // target = makeRoom(position + maxBytes);
21
- for (let i = 0; i < 100; i++) {
22
- length = pack(value, strLength, target, position + headerSize);
23
- }
24
- if (strLength < 0x40 || !encodeUtf8) {
25
- var strPosition = position + headerSize;
26
- var c2 = 0;
27
- for (let i = 0; i < strLength; i++) {
28
- const c1 = value.charCodeAt(i);
29
- if (c1 < 0x80) {
30
- target[strPosition++] = c1;
31
- } else if (c1 < 0x800) {
32
- target[strPosition++] = c1 >> 6 | 0xc0;
33
- target[strPosition++] = c1 & 0x3f | 0x80;
34
- } else if (
35
- (c1 & 0xfc00) === 0xd800 &&
36
- ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
37
- ) {
38
- c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
39
- i++;
40
- target[strPosition++] = c1 >> 18 | 0xf0;
41
- target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
42
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
43
- target[strPosition++] = c1 & 0x3f | 0x80;
44
- } else {
45
- target[strPosition++] = c1 >> 12 | 0xe0;
46
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
47
- target[strPosition++] = c1 & 0x3f | 0x80;
48
- }
49
- }
50
- length = strPosition - position - headerSize;
51
- } else {
52
- length = encodeUtf8(value, position + headerSize, maxBytes);
53
- }
54
-
55
-
56
- if (length < 0x20) {
57
- target[position++] = 0xa0 | length;
58
- } else if (length < 0x100) {
59
- if (headerSize < 2) {
60
- target.copyWithin(position + 2, position + 1, position + 1 + length);
61
- }
62
- target[position++] = 0xd9;
63
- target[position++] = length;
64
- } else if (length < 0x10000) {
65
- if (headerSize < 3) {
66
- target.copyWithin(position + 3, position + 2, position + 2 + length);
67
- }
68
- target[position++] = 0xda;
69
- target[position++] = length >> 8;
70
- target[position++] = length & 0xff;
71
- } else {
72
- if (headerSize < 5) {
73
- target.copyWithin(position + 5, position + 3, position + 3 + length);
74
- }
75
- target[position++] = 0xdb;
76
- targetView.setUint32(position, length);
77
- position += 4;
78
- }
79
- return position + length
80
- };
81
- const pack = (str, length, buf, offset) => {
82
- const start = offset;
83
- let currHigh = 0;
84
- for (let i = 0; i < length; i++) {
85
- const code = str.charCodeAt(i);
86
- const high = code >> 8;
87
- if (high !== currHigh) {
88
- buf[i + offset++] = 0;
89
- buf[i + offset++] = high;
90
- currHigh = high;
91
- }
92
- const low = code & 0xff;
93
- buf[i + offset] = low;
94
- if (!low) {
95
- buf[i + ++offset] = currHigh;
96
- }
97
- }
98
- return length + offset - start;
99
- };
100
- module.exports = writeString;