msgpackr 1.7.1 → 1.7.2

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/pack.js CHANGED
@@ -125,20 +125,44 @@ export class Packr extends Unpackr {
125
125
  writeStruct(value);
126
126
  else
127
127
  pack(value)
128
+ let lastBundle = bundledStrings;
129
+ if (bundledStrings)
130
+ writeBundles(start, pack, 0)
128
131
  if (referenceMap && referenceMap.idsToInsert) {
129
- let incrementPosition = referenceMap.idsToInsert.length * 6;
130
- if (bundledStrings)
131
- writeBundles(start, pack, incrementPosition)
132
- position += incrementPosition
132
+ let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
133
+ let i = idsToInsert.length;
134
+ let incrementPosition = -1;
135
+ while (lastBundle && i > 0) {
136
+ let insertionPoint = idsToInsert[--i].offset + start;
137
+ if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
138
+ incrementPosition = 0;
139
+ if (insertionPoint > (lastBundle.position + start)) {
140
+ if (incrementPosition >= 0)
141
+ incrementPosition += 6;
142
+ } else {
143
+ if (incrementPosition >= 0) {
144
+ // update the bundle reference now
145
+ targetView.setUint32(lastBundle.position + start,
146
+ targetView.getUint32(lastBundle.position + start) + incrementPosition)
147
+ incrementPosition = -1; // reset
148
+ }
149
+ lastBundle = lastBundle.previous;
150
+ i++;
151
+ }
152
+ }
153
+ if (incrementPosition >= 0 && lastBundle) {
154
+ // update the bundle reference now
155
+ targetView.setUint32(lastBundle.position + start,
156
+ targetView.getUint32(lastBundle.position + start) + incrementPosition)
157
+ }
158
+ position += idsToInsert.length * 6;
133
159
  if (position > safeEnd)
134
160
  makeRoom(position)
135
161
  packr.offset = position
136
- let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert)
162
+ let serialized = insertIds(target.subarray(start, position), idsToInsert)
137
163
  referenceMap = null
138
164
  return serialized
139
165
  }
140
- if (bundledStrings)
141
- writeBundles(start, pack, 0)
142
166
  packr.offset = position // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
143
167
  if (encodeOptions & REUSE_BUFFER_MODE) {
144
168
  target.start = start
@@ -196,13 +220,15 @@ export class Packr extends Unpackr {
196
220
  let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10
197
221
  if (position + maxBytes > safeEnd)
198
222
  target = makeRoom(position + maxBytes)
199
- if (bundledStrings.position) { // here we use the 0x62 extension to write the last bundle and reserve sapce for the reference pointer to the next/current bundle
223
+ let lastBundle
224
+ if (bundledStrings.position) { // here we use the 0x62 extension to write the last bundle and reserve space for the reference pointer to the next/current bundle
225
+ lastBundle = bundledStrings
200
226
  target[position] = 0xc8 // ext 16
201
227
  position += 3 // reserve for the writing bundle size
202
228
  target[position++] = 0x62 // 'b'
203
229
  extStart = position - start
204
230
  position += 4 // reserve for writing bundle reference
205
- writeBundles(start, pack) // write the last bundles
231
+ writeBundles(start, pack, 0) // write the last bundles
206
232
  targetView.setUint16(extStart + start - 3, position - start - extStart)
207
233
  } else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
208
234
  target[position++] = 0xd6 // fixext 4
@@ -211,6 +237,7 @@ export class Packr extends Unpackr {
211
237
  position += 4 // reserve for writing bundle reference
212
238
  }
213
239
  bundledStrings = ['', ''] // create new ones
240
+ bundledStrings.previous = lastBundle;
214
241
  bundledStrings.size = 0
215
242
  bundledStrings.position = extStart
216
243
  }
@@ -906,7 +933,6 @@ function insertIds(serialized, idsToInsert) {
906
933
  let nextId
907
934
  let distanceToMove = idsToInsert.length * 6
908
935
  let lastEnd = serialized.length - distanceToMove
909
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1)
910
936
  while (nextId = idsToInsert.pop()) {
911
937
  let offset = nextId.offset
912
938
  let id = nextId.id
@@ -927,6 +953,7 @@ function insertIds(serialized, idsToInsert) {
927
953
  function writeBundles(start, pack, incrementPosition) {
928
954
  if (bundledStrings.length > 0) {
929
955
  targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start)
956
+ bundledStrings.stringsPosition = position - start;
930
957
  let writeStrings = bundledStrings
931
958
  bundledStrings = null
932
959
  pack(writeStrings[0])
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr",
3
3
  "author": "Kris Zyp",
4
- "version": "1.7.1",
4
+ "version": "1.7.2",
5
5
  "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
6
6
  "license": "MIT",
7
7
  "types": "./index.d.ts",
package/unpack.js CHANGED
@@ -869,7 +869,7 @@ function readExt(length) {
869
869
  })
870
870
  }
871
871
  else
872
- throw new Error('Unknown extension type ' + type)
872
+ throw new Error('Unknown extension type ' + type)``
873
873
  }
874
874
 
875
875
  var keyCache = new Array(4096)
@@ -884,7 +884,7 @@ function readKey() {
884
884
  return readFixedString(length)
885
885
  } else { // not cacheable, go back and do a standard read
886
886
  position--
887
- return read()
887
+ return read().toString()
888
888
  }
889
889
  let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff
890
890
  let entry = keyCache[key]
@@ -938,16 +938,7 @@ function readKey() {
938
938
 
939
939
  // the registration of the record definition extension (as "r")
940
940
  const recordDefinition = (id, highByte) => {
941
- let structure
942
- if (currentUnpackr.freezeData) {
943
- currentUnpackr.freezeData = false;
944
- try {
945
- structure = read()
946
- } finally {
947
- currentUnpackr.freezeData = true;
948
- }
949
- } else
950
- structure = read()
941
+ let structure = read().map(property => property.toString()) // ensure that all keys are strings and that the array is mutable
951
942
  let firstByte = id
952
943
  if (highByte !== undefined) {
953
944
  id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id)