msgpackr 1.7.0 → 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,19 +125,45 @@ export class Packr extends Unpackr {
125
125
  writeStruct(value);
126
126
  else
127
127
  pack(value)
128
- if (bundledStrings) {
129
- writeBundles(start, pack)
130
- }
131
- packr.offset = position // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
128
+ let lastBundle = bundledStrings;
129
+ if (bundledStrings)
130
+ writeBundles(start, pack, 0)
132
131
  if (referenceMap && referenceMap.idsToInsert) {
133
- position += referenceMap.idsToInsert.length * 6
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;
134
159
  if (position > safeEnd)
135
160
  makeRoom(position)
136
161
  packr.offset = position
137
- let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert)
162
+ let serialized = insertIds(target.subarray(start, position), idsToInsert)
138
163
  referenceMap = null
139
164
  return serialized
140
165
  }
166
+ packr.offset = position // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
141
167
  if (encodeOptions & REUSE_BUFFER_MODE) {
142
168
  target.start = start
143
169
  target.end = position
@@ -194,13 +220,15 @@ export class Packr extends Unpackr {
194
220
  let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10
195
221
  if (position + maxBytes > safeEnd)
196
222
  target = makeRoom(position + maxBytes)
197
- 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
198
226
  target[position] = 0xc8 // ext 16
199
227
  position += 3 // reserve for the writing bundle size
200
228
  target[position++] = 0x62 // 'b'
201
229
  extStart = position - start
202
230
  position += 4 // reserve for writing bundle reference
203
- writeBundles(start, pack) // write the last bundles
231
+ writeBundles(start, pack, 0) // write the last bundles
204
232
  targetView.setUint16(extStart + start - 3, position - start - extStart)
205
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)
206
234
  target[position++] = 0xd6 // fixext 4
@@ -209,6 +237,7 @@ export class Packr extends Unpackr {
209
237
  position += 4 // reserve for writing bundle reference
210
238
  }
211
239
  bundledStrings = ['', ''] // create new ones
240
+ bundledStrings.previous = lastBundle;
212
241
  bundledStrings.size = 0
213
242
  bundledStrings.position = extStart
214
243
  }
@@ -904,7 +933,6 @@ function insertIds(serialized, idsToInsert) {
904
933
  let nextId
905
934
  let distanceToMove = idsToInsert.length * 6
906
935
  let lastEnd = serialized.length - distanceToMove
907
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1)
908
936
  while (nextId = idsToInsert.pop()) {
909
937
  let offset = nextId.offset
910
938
  let id = nextId.id
@@ -922,12 +950,12 @@ function insertIds(serialized, idsToInsert) {
922
950
  return serialized
923
951
  }
924
952
 
925
- function writeBundles(start, pack) {
953
+ function writeBundles(start, pack, incrementPosition) {
926
954
  if (bundledStrings.length > 0) {
927
- targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start)
955
+ targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start)
956
+ bundledStrings.stringsPosition = position - start;
928
957
  let writeStrings = bundledStrings
929
958
  bundledStrings = null
930
- let startPosition = position
931
959
  pack(writeStrings[0])
932
960
  pack(writeStrings[1])
933
961
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "msgpackr",
3
3
  "author": "Kris Zyp",
4
- "version": "1.7.0",
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/struct.js CHANGED
@@ -492,7 +492,7 @@ function readStruct(src, position, srcEnd, unpackr) {
492
492
  case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
493
493
  }
494
494
  }
495
- let structure = unpackr.typedStructs?.[recordId];
495
+ let structure = unpackr.typedStructs && unpackr.typedStructs[recordId];
496
496
  if (!structure) {
497
497
  // copy src buffer because getStructures will override it
498
498
  src = Uint8Array.prototype.slice.call(src, position, srcEnd);
@@ -784,7 +784,7 @@ function prepareStructures(structures, packr) {
784
784
  packr._mergeStructures(existing);
785
785
  return compatible;
786
786
  };
787
- packr.lastTypedStructuresLength = packr.typedStructs?.length;
787
+ packr.lastTypedStructuresLength = packr.typedStructs && packr.typedStructs.length;
788
788
  return structures;
789
789
  }
790
790
 
package/unpack.js CHANGED
@@ -193,7 +193,7 @@ export function checkedRead(options) {
193
193
 
194
194
  if (position == srcEnd) {
195
195
  // finished reading this source, cleanup references
196
- if (currentStructures?.restoreStructures)
196
+ if (currentStructures && currentStructures.restoreStructures)
197
197
  restoreStructures()
198
198
  currentStructures = null
199
199
  src = null
@@ -208,7 +208,7 @@ export function checkedRead(options) {
208
208
  // else more to read, but we are reading sequentially, so don't clear source yet
209
209
  return result
210
210
  } catch(error) {
211
- if (currentStructures?.restoreStructures)
211
+ if (currentStructures && currentStructures.restoreStructures)
212
212
  restoreStructures()
213
213
  clearSource()
214
214
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
@@ -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)