msgpackr 1.7.0-alpha3 → 1.7.0-alpha6
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/dist/index.js +19 -7
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +61 -60
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +49 -31
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +50 -32
- package/dist/test.js.map +1 -1
- package/pack.js +14 -6
- package/package.json +1 -1
- package/struct.js +28 -22
- package/unpack.js +7 -3
package/pack.js
CHANGED
|
@@ -15,7 +15,7 @@ let targetView
|
|
|
15
15
|
let position = 0
|
|
16
16
|
let safeEnd
|
|
17
17
|
let bundledStrings = null
|
|
18
|
-
let writeStructSlots
|
|
18
|
+
let writeStructSlots
|
|
19
19
|
const MAX_BUNDLE_SIZE = 0xf000
|
|
20
20
|
const hasNonLatin = /[\u0080-\uFFFF]/
|
|
21
21
|
export const RECORD_SYMBOL = Symbol('record-id')
|
|
@@ -149,7 +149,7 @@ export class Packr extends Unpackr {
|
|
|
149
149
|
if (structures) {
|
|
150
150
|
if (serializationsSinceTransitionRebuild < 10)
|
|
151
151
|
serializationsSinceTransitionRebuild++
|
|
152
|
-
let sharedLength = structures.sharedLength ||
|
|
152
|
+
let sharedLength = structures.sharedLength || 0
|
|
153
153
|
if (structures.length > sharedLength)
|
|
154
154
|
structures.length = sharedLength
|
|
155
155
|
if (transitionsCount > 10000) {
|
|
@@ -168,10 +168,9 @@ export class Packr extends Unpackr {
|
|
|
168
168
|
if (hasSharedUpdate && packr.saveStructures) {
|
|
169
169
|
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
170
170
|
let returnBuffer = target.subarray(start, position)
|
|
171
|
-
let newSharedData = prepareStructures
|
|
172
|
-
if (packr.saveStructures(newSharedData, newSharedData.isCompatible
|
|
171
|
+
let newSharedData = prepareStructures(structures, packr);
|
|
172
|
+
if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
|
|
173
173
|
// get updated structures and try again if the update failed
|
|
174
|
-
packr._mergeStructures(packr.getStructures())
|
|
175
174
|
return packr.pack(value)
|
|
176
175
|
}
|
|
177
176
|
packr.lastNamedStructuresLength = sharedLength
|
|
@@ -694,7 +693,7 @@ export class Packr extends Unpackr {
|
|
|
694
693
|
const writeStruct = (object, safePrototype) => {
|
|
695
694
|
let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
|
|
696
695
|
if (notifySharedUpdate)
|
|
697
|
-
hasSharedUpdate = true;
|
|
696
|
+
return hasSharedUpdate = true;
|
|
698
697
|
position = newPosition;
|
|
699
698
|
if (start > 0) {
|
|
700
699
|
pack(value);
|
|
@@ -946,6 +945,15 @@ export function addExtension(extension) {
|
|
|
946
945
|
}
|
|
947
946
|
unpackAddExtension(extension)
|
|
948
947
|
}
|
|
948
|
+
function prepareStructures(structures, packr) {
|
|
949
|
+
structures.isCompatible = (existingStructures) => {
|
|
950
|
+
let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length)
|
|
951
|
+
if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
|
|
952
|
+
packr._mergeStructures(existingStructures);
|
|
953
|
+
return compatible;
|
|
954
|
+
}
|
|
955
|
+
return structures
|
|
956
|
+
}
|
|
949
957
|
export function setWriteStructSlots(writeSlots, makeStructures) {
|
|
950
958
|
writeStructSlots = writeSlots;
|
|
951
959
|
prepareStructures = makeStructures;
|
package/package.json
CHANGED
package/struct.js
CHANGED
|
@@ -62,7 +62,6 @@ setWriteStructSlots(writeStruct, prepareStructures);
|
|
|
62
62
|
function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
|
|
63
63
|
let typedStructs = packr.typedStructs || (packr.typedStructs = []);
|
|
64
64
|
// note that we rely on pack.js to load stored structures before we get to this point
|
|
65
|
-
packr.lastTypedStructuresLength = typedStructs.length;
|
|
66
65
|
let targetView = target.dataView;
|
|
67
66
|
let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
|
|
68
67
|
let safeEnd = target.length - 10;
|
|
@@ -199,7 +198,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
|
|
|
199
198
|
refPosition += encodeUtf8(target, value, refPosition);
|
|
200
199
|
isNotAscii = refPosition - strStart > strLength;
|
|
201
200
|
}
|
|
202
|
-
if (refOffset <
|
|
201
|
+
if (refOffset < 0xf6) {
|
|
203
202
|
if (isNotAscii)
|
|
204
203
|
transition = nextTransition.string8 || createTypeTransition(nextTransition, UTF8, 1);
|
|
205
204
|
else
|
|
@@ -307,7 +306,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
|
|
|
307
306
|
targetView.setUint32(position, refOffset, true);
|
|
308
307
|
position += 4;
|
|
309
308
|
}
|
|
310
|
-
} else {
|
|
309
|
+
} else { // null or undefined
|
|
311
310
|
transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
|
|
312
311
|
targetView.setInt16(position, value === null ? -10 : -9, true);
|
|
313
312
|
position += 2;
|
|
@@ -415,6 +414,8 @@ function onLoadedStructures(sharedData) {
|
|
|
415
414
|
if (!(sharedData instanceof Map))
|
|
416
415
|
return sharedData;
|
|
417
416
|
let typed = sharedData.get('typed') || [];
|
|
417
|
+
if (Object.isFrozen(typed))
|
|
418
|
+
typed = typed.map(structure => structure.slice(0));
|
|
418
419
|
let named = sharedData.get('named');
|
|
419
420
|
let transitions = Object.create(null);
|
|
420
421
|
for (let i = 0, l = typed.length; i < l; i++) {
|
|
@@ -442,6 +443,7 @@ function onLoadedStructures(sharedData) {
|
|
|
442
443
|
}
|
|
443
444
|
typed.transitions = transitions;
|
|
444
445
|
this.typedStructs = typed;
|
|
446
|
+
this.lastTypedStructuresLength = typed.length;
|
|
445
447
|
return named;
|
|
446
448
|
}
|
|
447
449
|
var sourceSymbol = Symbol('source')
|
|
@@ -473,15 +475,16 @@ function readStruct(src, position, srcEnd, unpackr) {
|
|
|
473
475
|
}
|
|
474
476
|
var construct = structure.construct;
|
|
475
477
|
if (!construct) {
|
|
476
|
-
construct = structure.construct = function() {
|
|
478
|
+
construct = structure.construct = function LazyObject() {
|
|
477
479
|
}
|
|
478
480
|
var prototype = construct.prototype;
|
|
481
|
+
let properties = [];
|
|
479
482
|
Object.defineProperty(prototype, 'toJSON', {
|
|
480
|
-
|
|
483
|
+
value() {
|
|
481
484
|
// return an enumerable object with own properties to JSON stringify
|
|
482
485
|
let resolved = {};
|
|
483
|
-
for (let i = 0, l =
|
|
484
|
-
let key =
|
|
486
|
+
for (let i = 0, l = properties.length; i < l; i++) {
|
|
487
|
+
let key = properties[i].key;
|
|
485
488
|
resolved[key] = this[key];
|
|
486
489
|
}
|
|
487
490
|
return resolved;
|
|
@@ -490,7 +493,6 @@ function readStruct(src, position, srcEnd, unpackr) {
|
|
|
490
493
|
});
|
|
491
494
|
let currentOffset = 0;
|
|
492
495
|
let lastRefProperty;
|
|
493
|
-
let properties = [];
|
|
494
496
|
for (let i = 0, l = structure.length; i < l; i++) {
|
|
495
497
|
let definition = structure[i];
|
|
496
498
|
let [ type, size, key, enumerationOffset ] = definition;
|
|
@@ -555,7 +557,7 @@ function readStruct(src, position, srcEnd, unpackr) {
|
|
|
555
557
|
next = next.next;
|
|
556
558
|
}
|
|
557
559
|
if (end == null)
|
|
558
|
-
end = srcEnd - refStart;
|
|
560
|
+
end = source.srcEnd - refStart;
|
|
559
561
|
if (source.srcString) {
|
|
560
562
|
return source.srcString.slice(ref, end);
|
|
561
563
|
}
|
|
@@ -571,7 +573,7 @@ function readStruct(src, position, srcEnd, unpackr) {
|
|
|
571
573
|
asciiEnd = null;
|
|
572
574
|
} while((next = next.next));
|
|
573
575
|
if (asciiEnd == null)
|
|
574
|
-
asciiEnd = srcEnd - refStart
|
|
576
|
+
asciiEnd = source.srcEnd - refStart
|
|
575
577
|
source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
|
|
576
578
|
return source.srcString.slice(ref, end);
|
|
577
579
|
}
|
|
@@ -606,7 +608,7 @@ function readStruct(src, position, srcEnd, unpackr) {
|
|
|
606
608
|
next = next.next;
|
|
607
609
|
}
|
|
608
610
|
if (end == null)
|
|
609
|
-
end = srcEnd - refStart;
|
|
611
|
+
end = source.srcEnd - refStart;
|
|
610
612
|
if (type === UTF8) {
|
|
611
613
|
return src.toString('utf8', ref + refStart, end + refStart);
|
|
612
614
|
} else {
|
|
@@ -683,28 +685,32 @@ function toConstant(code) {
|
|
|
683
685
|
throw new Error('Unknown constant');
|
|
684
686
|
}
|
|
685
687
|
function prepareStructures(structures, packr) {
|
|
686
|
-
if (
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
688
|
+
if (packr.typedStructs) {
|
|
689
|
+
let structMap = new Map();
|
|
690
|
+
structMap.set('named', structures);
|
|
691
|
+
structMap.set('typed', packr.typedStructs);
|
|
692
|
+
structures = structMap;
|
|
693
|
+
}
|
|
691
694
|
let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
|
|
692
|
-
|
|
695
|
+
structures.isCompatible = existing => {
|
|
696
|
+
let compatible = true;
|
|
693
697
|
if (existing instanceof Map) {
|
|
694
698
|
let named = existing.get('named') || [];
|
|
695
699
|
if (named.length !== (packr.lastNamedStructuresLength || 0))
|
|
696
|
-
|
|
700
|
+
compatible = false;
|
|
697
701
|
let typed = existing.get('typed') || [];
|
|
698
702
|
if (typed.length !== lastTypedStructuresLength)
|
|
699
|
-
|
|
703
|
+
compatible = false;
|
|
700
704
|
} else if (existing instanceof Array) {
|
|
701
705
|
if (existing.length !== (packr.lastNamedStructuresLength || 0))
|
|
702
|
-
|
|
706
|
+
compatible = false;
|
|
703
707
|
}
|
|
704
|
-
|
|
708
|
+
if (!compatible)
|
|
709
|
+
packr._mergeStructures(existing);
|
|
710
|
+
return compatible;
|
|
705
711
|
};
|
|
706
712
|
packr.lastTypedStructuresLength = packr.typedStructs?.length;
|
|
707
|
-
return
|
|
713
|
+
return structures;
|
|
708
714
|
}
|
|
709
715
|
|
|
710
716
|
setReadStruct(readStruct, onLoadedStructures);
|
package/unpack.js
CHANGED
|
@@ -95,7 +95,7 @@ export class Unpackr {
|
|
|
95
95
|
currentUnpackr = this
|
|
96
96
|
if (this.structures) {
|
|
97
97
|
currentStructures = this.structures
|
|
98
|
-
return checkedRead()
|
|
98
|
+
return checkedRead(options)
|
|
99
99
|
} else if (!currentStructures || currentStructures.length > 0) {
|
|
100
100
|
currentStructures = []
|
|
101
101
|
}
|
|
@@ -104,7 +104,7 @@ export class Unpackr {
|
|
|
104
104
|
if (!currentStructures || currentStructures.length > 0)
|
|
105
105
|
currentStructures = []
|
|
106
106
|
}
|
|
107
|
-
return checkedRead()
|
|
107
|
+
return checkedRead(options)
|
|
108
108
|
}
|
|
109
109
|
unpackMultiple(source, forEach) {
|
|
110
110
|
let values, lastPosition = 0
|
|
@@ -142,6 +142,8 @@ export class Unpackr {
|
|
|
142
142
|
if (onLoadedStructures)
|
|
143
143
|
loadedStructures = onLoadedStructures.call(this, loadedStructures);
|
|
144
144
|
loadedStructures = loadedStructures || []
|
|
145
|
+
if (Object.isFrozen(loadedStructures))
|
|
146
|
+
loadedStructures = loadedStructures.map(structure => structure.slice(0))
|
|
145
147
|
for (let i = 0, l = loadedStructures.length; i < l; i++) {
|
|
146
148
|
let structure = loadedStructures[i]
|
|
147
149
|
if (structure) {
|
|
@@ -171,7 +173,7 @@ export class Unpackr {
|
|
|
171
173
|
export function getPosition() {
|
|
172
174
|
return position
|
|
173
175
|
}
|
|
174
|
-
export function checkedRead() {
|
|
176
|
+
export function checkedRead(options) {
|
|
175
177
|
try {
|
|
176
178
|
if (!currentUnpackr.trusted && !sequentialMode) {
|
|
177
179
|
let sharedLength = currentStructures.sharedLength || 0
|
|
@@ -181,6 +183,8 @@ export function checkedRead() {
|
|
|
181
183
|
let result
|
|
182
184
|
if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
|
|
183
185
|
result = readStruct(src, position, srcEnd, currentUnpackr)
|
|
186
|
+
if (!(options && options.lazy) && result)
|
|
187
|
+
result = result.toJSON()
|
|
184
188
|
position = srcEnd
|
|
185
189
|
} else
|
|
186
190
|
result = read()
|