msgpackr 1.7.0-alpha3 → 1.7.0-alpha4
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 +14 -4
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +58 -57
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +30 -15
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +30 -15
- package/dist/test.js.map +1 -1
- package/pack.js +14 -6
- package/package.json +1 -1
- package/struct.js +14 -9
- package/unpack.js +2 -0
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;
|
|
@@ -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,7 +475,7 @@ 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;
|
|
479
481
|
Object.defineProperty(prototype, 'toJSON', {
|
|
@@ -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 {
|
|
@@ -690,18 +692,21 @@ function prepareStructures(structures, packr) {
|
|
|
690
692
|
structMap.set('typed', packr.typedStructs);
|
|
691
693
|
let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
|
|
692
694
|
structMap.isCompatible = existing => {
|
|
695
|
+
let compatible = true;
|
|
693
696
|
if (existing instanceof Map) {
|
|
694
697
|
let named = existing.get('named') || [];
|
|
695
698
|
if (named.length !== (packr.lastNamedStructuresLength || 0))
|
|
696
|
-
|
|
699
|
+
compatible = false;
|
|
697
700
|
let typed = existing.get('typed') || [];
|
|
698
701
|
if (typed.length !== lastTypedStructuresLength)
|
|
699
|
-
|
|
702
|
+
compatible = false;
|
|
700
703
|
} else if (existing instanceof Array) {
|
|
701
704
|
if (existing.length !== (packr.lastNamedStructuresLength || 0))
|
|
702
|
-
|
|
705
|
+
compatible = false;
|
|
703
706
|
}
|
|
704
|
-
|
|
707
|
+
if (!compatible)
|
|
708
|
+
packr._mergeStructures(existing);
|
|
709
|
+
return compatible;
|
|
705
710
|
};
|
|
706
711
|
packr.lastTypedStructuresLength = packr.typedStructs?.length;
|
|
707
712
|
return structMap;
|
package/unpack.js
CHANGED
|
@@ -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) {
|