msgpackr 1.7.0-alpha2 → 1.7.0-alpha3

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,6 +139,8 @@ 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 || []
138
145
  for (let i = 0, l = loadedStructures.length; i < l; i++) {
139
146
  let structure = loadedStructures[i]
@@ -172,9 +179,8 @@ export function checkedRead() {
172
179
  currentStructures.length = sharedLength
173
180
  }
174
181
  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)
182
+ if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
183
+ result = readStruct(src, position, srcEnd, currentUnpackr)
178
184
  position = srcEnd
179
185
  } else
180
186
  result = read()
@@ -504,7 +510,7 @@ const createSecondByteReader = (firstId, read0) => {
504
510
  }
505
511
  }
506
512
 
507
- function loadStructures() {
513
+ export function loadStructures() {
508
514
  let loadedStructures = saveState(() => {
509
515
  // save the state in case getStructures modifies our buffer
510
516
  src = null
@@ -610,6 +616,16 @@ function readStringJS(length) {
610
616
 
611
617
  return result
612
618
  }
619
+ export function readString(source, start, length) {
620
+ let existingSrc = src;
621
+ src = source;
622
+ position = start;
623
+ try {
624
+ return readStringJS(length);
625
+ } finally {
626
+ src = existingSrc;
627
+ }
628
+ }
613
629
 
614
630
  function readArray(length) {
615
631
  let array = new Array(length)
@@ -830,7 +846,15 @@ function readBin(length) {
830
846
  function readExt(length) {
831
847
  let type = src[position++]
832
848
  if (currentExtensions[type]) {
833
- return currentExtensions[type](src.subarray(position, position += length))
849
+ let end
850
+ return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
851
+ position = readPosition;
852
+ try {
853
+ return read();
854
+ } finally {
855
+ position = end;
856
+ }
857
+ })
834
858
  }
835
859
  else
836
860
  throw new Error('Unknown extension type ' + type)
@@ -1073,6 +1097,7 @@ export function roundFloat32(float32Number) {
1073
1097
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)]
1074
1098
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1075
1099
  }
1076
- export function setReadStruct(func) {
1077
- readStruct = func;
1100
+ export function setReadStruct(updatedReadStruct, loadedStructs) {
1101
+ readStruct = updatedReadStruct;
1102
+ onLoadedStructures = loadedStructs;
1078
1103
  }
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;