msgpackr 1.7.0-alpha4 → 1.7.0-alpha7
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 +6 -4
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +59 -59
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +89 -33
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +95 -35
- package/dist/test.js.map +1 -1
- package/package.json +1 -1
- package/struct.js +78 -28
- package/unpack.js +11 -6
package/dist/test.js
CHANGED
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
C1.name = 'MessagePack 0xC1';
|
|
30
30
|
var sequentialMode = false;
|
|
31
31
|
var inlineObjectReadThreshold = 2;
|
|
32
|
-
var readStruct, onLoadedStructures;
|
|
32
|
+
var readStruct, onLoadedStructures, onSaveState;
|
|
33
33
|
try {
|
|
34
34
|
new Function('');
|
|
35
35
|
} catch(error) {
|
|
@@ -94,7 +94,7 @@
|
|
|
94
94
|
currentUnpackr = this;
|
|
95
95
|
if (this.structures) {
|
|
96
96
|
currentStructures = this.structures;
|
|
97
|
-
return checkedRead()
|
|
97
|
+
return checkedRead(options)
|
|
98
98
|
} else if (!currentStructures || currentStructures.length > 0) {
|
|
99
99
|
currentStructures = [];
|
|
100
100
|
}
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
if (!currentStructures || currentStructures.length > 0)
|
|
104
104
|
currentStructures = [];
|
|
105
105
|
}
|
|
106
|
-
return checkedRead()
|
|
106
|
+
return checkedRead(options)
|
|
107
107
|
}
|
|
108
108
|
unpackMultiple(source, forEach) {
|
|
109
109
|
let values, lastPosition = 0;
|
|
@@ -169,7 +169,7 @@
|
|
|
169
169
|
return this.unpack(source, end)
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
|
-
function checkedRead() {
|
|
172
|
+
function checkedRead(options) {
|
|
173
173
|
try {
|
|
174
174
|
if (!currentUnpackr.trusted && !sequentialMode) {
|
|
175
175
|
let sharedLength = currentStructures.sharedLength || 0;
|
|
@@ -179,6 +179,8 @@
|
|
|
179
179
|
let result;
|
|
180
180
|
if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
|
|
181
181
|
result = readStruct(src, position, srcEnd, currentUnpackr);
|
|
182
|
+
if (!(options && options.lazy) && result)
|
|
183
|
+
result = result.toJSON();
|
|
182
184
|
position = srcEnd;
|
|
183
185
|
} else
|
|
184
186
|
result = read();
|
|
@@ -202,7 +204,7 @@
|
|
|
202
204
|
// else more to read, but we are reading sequentially, so don't clear source yet
|
|
203
205
|
return result
|
|
204
206
|
} catch(error) {
|
|
205
|
-
if (currentStructures
|
|
207
|
+
if (currentStructures?.restoreStructures)
|
|
206
208
|
restoreStructures();
|
|
207
209
|
clearSource();
|
|
208
210
|
if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
|
|
@@ -986,6 +988,8 @@
|
|
|
986
988
|
// currentExtensions[0x52] = () =>
|
|
987
989
|
|
|
988
990
|
function saveState(callback) {
|
|
991
|
+
if (onSaveState)
|
|
992
|
+
onSaveState();
|
|
989
993
|
let savedSrcEnd = srcEnd;
|
|
990
994
|
let savedPosition = position;
|
|
991
995
|
let savedSrcStringStart = srcStringStart;
|
|
@@ -1027,9 +1031,10 @@
|
|
|
1027
1031
|
mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
|
|
1028
1032
|
}
|
|
1029
1033
|
var defaultUnpackr = new Unpackr({ useRecords: false });
|
|
1030
|
-
function setReadStruct(updatedReadStruct, loadedStructs) {
|
|
1034
|
+
function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
|
|
1031
1035
|
readStruct = updatedReadStruct;
|
|
1032
1036
|
onLoadedStructures = loadedStructs;
|
|
1037
|
+
onSaveState = saveState;
|
|
1033
1038
|
}
|
|
1034
1039
|
|
|
1035
1040
|
let textEncoder;
|
|
@@ -1979,11 +1984,13 @@
|
|
|
1979
1984
|
const NUMBER = 0;
|
|
1980
1985
|
const UTF8 = 2;
|
|
1981
1986
|
const OBJECT_DATA = 1;
|
|
1987
|
+
const DATE = 16;
|
|
1982
1988
|
const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
|
|
1989
|
+
TYPE_NAMES[DATE] = 'date';
|
|
1983
1990
|
const float32Headers = [false, true, true, false, false, true, true, false];
|
|
1984
1991
|
let updatedPosition;
|
|
1985
1992
|
const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
|
|
1986
|
-
let textEncoder$1;
|
|
1993
|
+
let textEncoder$1, currentSource;
|
|
1987
1994
|
try {
|
|
1988
1995
|
textEncoder$1 = new TextEncoder();
|
|
1989
1996
|
} catch (error) {}
|
|
@@ -2024,6 +2031,7 @@
|
|
|
2024
2031
|
return 0;
|
|
2025
2032
|
position += headerSize;
|
|
2026
2033
|
let queuedReferences = [];
|
|
2034
|
+
let usedAscii0;
|
|
2027
2035
|
let keyIndex = 0;
|
|
2028
2036
|
for (let key in object) {
|
|
2029
2037
|
let value = object[key];
|
|
@@ -2039,7 +2047,8 @@
|
|
|
2039
2047
|
string16: null,
|
|
2040
2048
|
object16: null,
|
|
2041
2049
|
num32: null,
|
|
2042
|
-
float64: null
|
|
2050
|
+
float64: null,
|
|
2051
|
+
date64: null
|
|
2043
2052
|
};
|
|
2044
2053
|
}
|
|
2045
2054
|
if (position > safeEnd) {
|
|
@@ -2132,25 +2141,47 @@
|
|
|
2132
2141
|
refPosition += encodeUtf8(target, value, refPosition);
|
|
2133
2142
|
isNotAscii = refPosition - strStart > strLength;
|
|
2134
2143
|
}
|
|
2135
|
-
if (refOffset <
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2144
|
+
if (refOffset < 0xa0 || (refOffset < 0xf6 && (nextTransition.ascii8 || nextTransition.string8))) {
|
|
2145
|
+
// short strings
|
|
2146
|
+
if (isNotAscii) {
|
|
2147
|
+
if (!(transition = nextTransition.string8)) {
|
|
2148
|
+
if (typedStructs.length > 10 && (transition = nextTransition.ascii8)) {
|
|
2149
|
+
// we can safely change ascii to utf8 in place since they are compatible
|
|
2150
|
+
transition.__type = UTF8;
|
|
2151
|
+
nextTransition.ascii8 = null;
|
|
2152
|
+
nextTransition.string8 = transition;
|
|
2153
|
+
pack(null, 0, true); // special call to notify that structures have been updated
|
|
2154
|
+
} else {
|
|
2155
|
+
transition = createTypeTransition(nextTransition, UTF8, 1);
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
} else if (refOffset === 0 && !usedAscii0) {
|
|
2159
|
+
usedAscii0 = true;
|
|
2160
|
+
transition = nextTransition.ascii0 || createTypeTransition(nextTransition, ASCII, 0);
|
|
2161
|
+
break; // don't increment position
|
|
2162
|
+
}// else ascii:
|
|
2163
|
+
else if (!(transition = nextTransition.ascii8) && !(typedStructs.length > 10 && (transition = nextTransition.string8)))
|
|
2164
|
+
transition = createTypeTransition(nextTransition, ASCII, 1);
|
|
2140
2165
|
target[position++] = refOffset;
|
|
2141
2166
|
} else {
|
|
2142
|
-
|
|
2167
|
+
// TODO: Enable ascii16 at some point, but get the logic right
|
|
2168
|
+
//if (isNotAscii)
|
|
2143
2169
|
transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
|
|
2144
|
-
else
|
|
2145
|
-
transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
|
|
2170
|
+
//else
|
|
2171
|
+
//transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
|
|
2146
2172
|
targetView.setUint16(position, refOffset, true);
|
|
2147
2173
|
position += 2;
|
|
2148
2174
|
}
|
|
2149
2175
|
break;
|
|
2150
2176
|
case 'object':
|
|
2151
2177
|
if (value) {
|
|
2152
|
-
|
|
2153
|
-
|
|
2178
|
+
if (value.constructor === Date) {
|
|
2179
|
+
transition = nextTransition.date64 || createTypeTransition(nextTransition, DATE, 8);
|
|
2180
|
+
targetView.setFloat64(position, value.getTime(), true);
|
|
2181
|
+
position += 8;
|
|
2182
|
+
} else {
|
|
2183
|
+
queuedReferences.push(key, value, keyIndex);
|
|
2184
|
+
}
|
|
2154
2185
|
break;
|
|
2155
2186
|
} else { // null
|
|
2156
2187
|
nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
|
|
@@ -2240,7 +2271,7 @@
|
|
|
2240
2271
|
targetView.setUint32(position, refOffset, true);
|
|
2241
2272
|
position += 4;
|
|
2242
2273
|
}
|
|
2243
|
-
} else {
|
|
2274
|
+
} else { // null or undefined
|
|
2244
2275
|
transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
|
|
2245
2276
|
targetView.setInt16(position, value === null ? -10 : -9, true);
|
|
2246
2277
|
position += 2;
|
|
@@ -2368,7 +2399,8 @@
|
|
|
2368
2399
|
string16: null,
|
|
2369
2400
|
object16: null,
|
|
2370
2401
|
num32: null,
|
|
2371
|
-
float64: null
|
|
2402
|
+
float64: null,
|
|
2403
|
+
date64: null,
|
|
2372
2404
|
};
|
|
2373
2405
|
}
|
|
2374
2406
|
transition = createTypeTransition(nextTransition, type, size);
|
|
@@ -2412,12 +2444,13 @@
|
|
|
2412
2444
|
construct = structure.construct = function LazyObject() {
|
|
2413
2445
|
};
|
|
2414
2446
|
var prototype = construct.prototype;
|
|
2447
|
+
let properties = [];
|
|
2415
2448
|
Object.defineProperty(prototype, 'toJSON', {
|
|
2416
|
-
|
|
2449
|
+
value() {
|
|
2417
2450
|
// return an enumerable object with own properties to JSON stringify
|
|
2418
2451
|
let resolved = {};
|
|
2419
|
-
for (let i = 0, l =
|
|
2420
|
-
let key =
|
|
2452
|
+
for (let i = 0, l = properties.length; i < l; i++) {
|
|
2453
|
+
let key = properties[i].key;
|
|
2421
2454
|
resolved[key] = this[key];
|
|
2422
2455
|
}
|
|
2423
2456
|
return resolved;
|
|
@@ -2426,7 +2459,6 @@
|
|
|
2426
2459
|
});
|
|
2427
2460
|
let currentOffset = 0;
|
|
2428
2461
|
let lastRefProperty;
|
|
2429
|
-
let properties = [];
|
|
2430
2462
|
for (let i = 0, l = structure.length; i < l; i++) {
|
|
2431
2463
|
let definition = structure[i];
|
|
2432
2464
|
let [ type, size, key, enumerationOffset ] = definition;
|
|
@@ -2546,7 +2578,12 @@
|
|
|
2546
2578
|
if (type === UTF8) {
|
|
2547
2579
|
return src.toString('utf8', ref + refStart, end + refStart);
|
|
2548
2580
|
} else {
|
|
2549
|
-
|
|
2581
|
+
currentSource = source;
|
|
2582
|
+
try {
|
|
2583
|
+
return unpackr.unpack(src, { start: ref + refStart, end: end + refStart });
|
|
2584
|
+
} finally {
|
|
2585
|
+
currentSource = null;
|
|
2586
|
+
}
|
|
2550
2587
|
}
|
|
2551
2588
|
};
|
|
2552
2589
|
break;
|
|
@@ -2594,6 +2631,16 @@
|
|
|
2594
2631
|
};
|
|
2595
2632
|
break;
|
|
2596
2633
|
}
|
|
2634
|
+
break;
|
|
2635
|
+
case DATE:
|
|
2636
|
+
get = function () {
|
|
2637
|
+
let source = this[sourceSymbol];
|
|
2638
|
+
let src = source.src;
|
|
2639
|
+
let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
|
|
2640
|
+
return new Date(dataView.getFloat64(source.position + property.offset, true));
|
|
2641
|
+
};
|
|
2642
|
+
break;
|
|
2643
|
+
|
|
2597
2644
|
}
|
|
2598
2645
|
property.get = get;
|
|
2599
2646
|
}
|
|
@@ -2618,14 +2665,23 @@
|
|
|
2618
2665
|
}
|
|
2619
2666
|
throw new Error('Unknown constant');
|
|
2620
2667
|
}
|
|
2668
|
+
|
|
2669
|
+
function saveState$1() {
|
|
2670
|
+
if (currentSource) {
|
|
2671
|
+
currentSource.src = Uint8Array.prototype.slice.call(currentSource.src, currentSource.position, currentSource.srcEnd);
|
|
2672
|
+
currentSource.position = 0;
|
|
2673
|
+
currentSource.srcEnd = currentSource.src.length;
|
|
2674
|
+
}
|
|
2675
|
+
}
|
|
2621
2676
|
function prepareStructures$1(structures, packr) {
|
|
2622
|
-
if (
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2677
|
+
if (packr.typedStructs) {
|
|
2678
|
+
let structMap = new Map();
|
|
2679
|
+
structMap.set('named', structures);
|
|
2680
|
+
structMap.set('typed', packr.typedStructs);
|
|
2681
|
+
structures = structMap;
|
|
2682
|
+
}
|
|
2627
2683
|
let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
|
|
2628
|
-
|
|
2684
|
+
structures.isCompatible = existing => {
|
|
2629
2685
|
let compatible = true;
|
|
2630
2686
|
if (existing instanceof Map) {
|
|
2631
2687
|
let named = existing.get('named') || [];
|
|
@@ -2643,15 +2699,20 @@
|
|
|
2643
2699
|
return compatible;
|
|
2644
2700
|
};
|
|
2645
2701
|
packr.lastTypedStructuresLength = packr.typedStructs?.length;
|
|
2646
|
-
return
|
|
2702
|
+
return structures;
|
|
2647
2703
|
}
|
|
2648
2704
|
|
|
2649
|
-
setReadStruct(readStruct$1, onLoadedStructures$1);
|
|
2705
|
+
setReadStruct(readStruct$1, onLoadedStructures$1, saveState$1);
|
|
2650
2706
|
|
|
2651
2707
|
let allSampleData = [];
|
|
2652
2708
|
for (let i = 1; i < 6; i++) {
|
|
2653
2709
|
allSampleData.push(JSON.parse(fs.readFileSync(new URL(`./example${i > 1 ? i : ''}.json`, (document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href)))));
|
|
2654
2710
|
}
|
|
2711
|
+
allSampleData.push({
|
|
2712
|
+
name: 'some other types',
|
|
2713
|
+
date: new Date(),
|
|
2714
|
+
empty: '',
|
|
2715
|
+
});
|
|
2655
2716
|
const sampleData = allSampleData[3];
|
|
2656
2717
|
function tryRequire(module) {
|
|
2657
2718
|
try {
|
|
@@ -2815,7 +2876,7 @@
|
|
|
2815
2876
|
} });
|
|
2816
2877
|
for (let i = 0; i < 20; i++) {
|
|
2817
2878
|
var serialized = packr.pack(data);
|
|
2818
|
-
var deserialized = packr.unpack(serialized);
|
|
2879
|
+
var deserialized = packr.unpack(serialized, { lazy: true });
|
|
2819
2880
|
var copied = {};
|
|
2820
2881
|
for (let key in deserialized) {
|
|
2821
2882
|
copied[key] = deserialized[key];
|
|
@@ -2830,7 +2891,6 @@
|
|
|
2830
2891
|
var deserialized = packr.unpack(serialized);
|
|
2831
2892
|
assert.deepEqual(deserialized, data);
|
|
2832
2893
|
});
|
|
2833
|
-
break;
|
|
2834
2894
|
}
|
|
2835
2895
|
|
|
2836
2896
|
test('pack/unpack empty data with bundled strings', function(){
|