jaelis-node 1.3.2 → 1.4.1
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/README.md +62 -1
- package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +454 -0
- package/lib/JAELIS-VM/lib/adapters/index.js +411 -0
- package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +457 -0
- package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +1097 -0
- package/lib/JAELIS-VM/lib/execution/engine.js +1183 -0
- package/lib/JAELIS-VM/lib/index.js +440 -0
- package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +543 -0
- package/lib/JAELIS-VM/lib/serialization/serializer.js +819 -0
- package/lib/JAELIS-VM/lib/state/state-manager.js +1116 -0
- package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +1222 -0
- package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +836 -0
- package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +1127 -0
- package/lib/JAELIS-VM/lib/unified/index.js +378 -0
- package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +1150 -0
- package/lib/JAELIS-VM/lib/unified/unified-compiler.js +1350 -0
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +12 -0
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +12 -0
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +12 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +12 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +12 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/.package-lock.json +127 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +1 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +17 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +1 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +17 -0
- package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +21 -0
- package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +5 -0
- package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +11 -0
- package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +60 -0
- package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +50 -0
- package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +198 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +21 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/README.md +380 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +11 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +73 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +11 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +2 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +1300 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +1244 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +2509 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +2 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +2508 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +2 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +2629 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +3343 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +1 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +1231 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +79 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/index.js +3 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +85 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +24 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/package.json +94 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +88 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +61 -0
- package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +19 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +201 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/README.md +163 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +14 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +313 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +39 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +51 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +24 -0
- package/lib/JAELIS-VM/node_modules/detect-libc/package.json +44 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +21 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/README.md +372 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +11 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +67 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +2407 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +2 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +2406 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +2 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +3320 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +4540 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +1250 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +91 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +91 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/index.js +5 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +87 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +25 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +1141 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/package.json +104 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +88 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +57 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +815 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +3 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +2 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +2 -0
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +1221 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +21 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +5 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +13 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +63 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +1 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +12 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +12 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +12 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +17 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +28 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +21 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +58 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +82 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +19 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +6 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +236 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +7 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +32 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +50 -0
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +274 -0
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +21 -0
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +58 -0
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +77 -0
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +19 -0
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +224 -0
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +7 -0
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +32 -0
- package/lib/JAELIS-VM/package-lock.json +284 -0
- package/lib/JAELIS-VM/package.json +38 -0
- package/lib/JAELIS-VM/test/comprehensive.test.js +267 -0
- package/lib/JAELIS-VM/test/cross-chain-test.js +470 -0
- package/lib/JAELIS-VM/test/unified-vm-test.js +459 -0
- package/lib/JAELIS-VM/test/unified.test.js +166 -0
- package/lib/JAELIS-VM/test/vm.test.js +599 -0
- package/lib/index.js +240 -4
- package/package.json +2 -2
|
@@ -0,0 +1,3343 @@
|
|
|
1
|
+
(function (stream, module, chai, fs) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
let decoder;
|
|
5
|
+
try {
|
|
6
|
+
decoder = new TextDecoder();
|
|
7
|
+
} catch(error) {}
|
|
8
|
+
let src;
|
|
9
|
+
let srcEnd;
|
|
10
|
+
let position$1 = 0;
|
|
11
|
+
const EMPTY_ARRAY = [];
|
|
12
|
+
const LEGACY_RECORD_INLINE_ID = 105;
|
|
13
|
+
const RECORD_DEFINITIONS_ID = 0xdffe;
|
|
14
|
+
const RECORD_INLINE_ID = 0xdfff; // temporary first-come first-serve tag // proposed tag: 0x7265 // 're'
|
|
15
|
+
const BUNDLED_STRINGS_ID = 0xdff9;
|
|
16
|
+
const PACKED_REFERENCE_TAG_ID = 6;
|
|
17
|
+
const STOP_CODE = {};
|
|
18
|
+
let maxArraySize = 112810000; // This is the maximum array size in V8. We would potentially detect and set it higher
|
|
19
|
+
// for JSC, but this is pretty large and should be sufficient for most use cases
|
|
20
|
+
let maxMapSize = 16810000; // JavaScript has a fixed maximum map size of about 16710000, but JS itself enforces this,
|
|
21
|
+
// many keys in an object, so also probably a reasonable choice there.
|
|
22
|
+
let strings = EMPTY_ARRAY;
|
|
23
|
+
let stringPosition = 0;
|
|
24
|
+
let currentDecoder = {};
|
|
25
|
+
let currentStructures;
|
|
26
|
+
let srcString;
|
|
27
|
+
let srcStringStart = 0;
|
|
28
|
+
let srcStringEnd = 0;
|
|
29
|
+
let bundledStrings$1;
|
|
30
|
+
let referenceMap;
|
|
31
|
+
let currentExtensions = [];
|
|
32
|
+
let currentExtensionRanges = [];
|
|
33
|
+
let packedValues;
|
|
34
|
+
let dataView;
|
|
35
|
+
let restoreMapsAsObject;
|
|
36
|
+
let defaultOptions = {
|
|
37
|
+
useRecords: false,
|
|
38
|
+
mapsAsObjects: true
|
|
39
|
+
};
|
|
40
|
+
let sequentialMode = false;
|
|
41
|
+
let inlineObjectReadThreshold = 2;
|
|
42
|
+
// no-eval build
|
|
43
|
+
try {
|
|
44
|
+
new Function('');
|
|
45
|
+
} catch(error) {
|
|
46
|
+
// if eval variants are not supported, do not create inline object readers ever
|
|
47
|
+
inlineObjectReadThreshold = Infinity;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Decoder {
|
|
53
|
+
constructor(options) {
|
|
54
|
+
if (options) {
|
|
55
|
+
if ((options.keyMap || options._keyMap) && !options.useRecords) {
|
|
56
|
+
options.useRecords = false;
|
|
57
|
+
options.mapsAsObjects = true;
|
|
58
|
+
}
|
|
59
|
+
if (options.useRecords === false && options.mapsAsObjects === undefined)
|
|
60
|
+
options.mapsAsObjects = true;
|
|
61
|
+
if (options.getStructures)
|
|
62
|
+
options.getShared = options.getStructures;
|
|
63
|
+
if (options.getShared && !options.structures)
|
|
64
|
+
(options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
|
|
65
|
+
if (options.keyMap) {
|
|
66
|
+
this.mapKey = new Map();
|
|
67
|
+
for (let [k,v] of Object.entries(options.keyMap)) this.mapKey.set(v,k);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
Object.assign(this, options);
|
|
71
|
+
}
|
|
72
|
+
/*
|
|
73
|
+
decodeKey(key) {
|
|
74
|
+
return this.keyMap
|
|
75
|
+
? Object.keys(this.keyMap)[Object.values(this.keyMap).indexOf(key)] || key
|
|
76
|
+
: key
|
|
77
|
+
}
|
|
78
|
+
*/
|
|
79
|
+
decodeKey(key) {
|
|
80
|
+
return this.keyMap ? this.mapKey.get(key) || key : key
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
encodeKey(key) {
|
|
84
|
+
return this.keyMap && this.keyMap.hasOwnProperty(key) ? this.keyMap[key] : key
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
encodeKeys(rec) {
|
|
88
|
+
if (!this._keyMap) return rec
|
|
89
|
+
let map = new Map();
|
|
90
|
+
for (let [k,v] of Object.entries(rec)) map.set((this._keyMap.hasOwnProperty(k) ? this._keyMap[k] : k), v);
|
|
91
|
+
return map
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
decodeKeys(map) {
|
|
95
|
+
if (!this._keyMap || map.constructor.name != 'Map') return map
|
|
96
|
+
if (!this._mapKey) {
|
|
97
|
+
this._mapKey = new Map();
|
|
98
|
+
for (let [k,v] of Object.entries(this._keyMap)) this._mapKey.set(v,k);
|
|
99
|
+
}
|
|
100
|
+
let res = {};
|
|
101
|
+
//map.forEach((v,k) => res[Object.keys(this._keyMap)[Object.values(this._keyMap).indexOf(k)] || k] = v)
|
|
102
|
+
map.forEach((v,k) => res[safeKey(this._mapKey.has(k) ? this._mapKey.get(k) : k)] = v);
|
|
103
|
+
return res
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
mapDecode(source, end) {
|
|
107
|
+
|
|
108
|
+
let res = this.decode(source);
|
|
109
|
+
if (this._keyMap) {
|
|
110
|
+
//Experiemntal support for Optimised KeyMap decoding
|
|
111
|
+
switch (res.constructor.name) {
|
|
112
|
+
case 'Array': return res.map(r => this.decodeKeys(r))
|
|
113
|
+
//case 'Map': return this.decodeKeys(res)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return res
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
decode(source, end) {
|
|
120
|
+
if (src) {
|
|
121
|
+
// re-entrant execution, save the state and restore it after we do this decode
|
|
122
|
+
return saveState(() => {
|
|
123
|
+
clearSource();
|
|
124
|
+
return this ? this.decode(source, end) : Decoder.prototype.decode.call(defaultOptions, source, end)
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
srcEnd = end > -1 ? end : source.length;
|
|
128
|
+
position$1 = 0;
|
|
129
|
+
stringPosition = 0;
|
|
130
|
+
srcStringEnd = 0;
|
|
131
|
+
srcString = null;
|
|
132
|
+
strings = EMPTY_ARRAY;
|
|
133
|
+
bundledStrings$1 = null;
|
|
134
|
+
src = source;
|
|
135
|
+
// this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
|
|
136
|
+
// technique for getting data from a database where it can be copied into an existing buffer instead of creating
|
|
137
|
+
// new ones
|
|
138
|
+
try {
|
|
139
|
+
dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
|
|
140
|
+
} catch(error) {
|
|
141
|
+
// if it doesn't have a buffer, maybe it is the wrong type of object
|
|
142
|
+
src = null;
|
|
143
|
+
if (source instanceof Uint8Array)
|
|
144
|
+
throw error
|
|
145
|
+
throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
|
|
146
|
+
}
|
|
147
|
+
if (this instanceof Decoder) {
|
|
148
|
+
currentDecoder = this;
|
|
149
|
+
packedValues = this.sharedValues &&
|
|
150
|
+
(this.pack ? new Array(this.maxPrivatePackedValues || 16).concat(this.sharedValues) :
|
|
151
|
+
this.sharedValues);
|
|
152
|
+
if (this.structures) {
|
|
153
|
+
currentStructures = this.structures;
|
|
154
|
+
return checkedRead()
|
|
155
|
+
} else if (!currentStructures || currentStructures.length > 0) {
|
|
156
|
+
currentStructures = [];
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
currentDecoder = defaultOptions;
|
|
160
|
+
if (!currentStructures || currentStructures.length > 0)
|
|
161
|
+
currentStructures = [];
|
|
162
|
+
packedValues = null;
|
|
163
|
+
}
|
|
164
|
+
return checkedRead()
|
|
165
|
+
}
|
|
166
|
+
decodeMultiple(source, forEach) {
|
|
167
|
+
let values, lastPosition = 0;
|
|
168
|
+
try {
|
|
169
|
+
let size = source.length;
|
|
170
|
+
sequentialMode = true;
|
|
171
|
+
let value = this ? this.decode(source, size) : defaultDecoder.decode(source, size);
|
|
172
|
+
if (forEach) {
|
|
173
|
+
if (forEach(value) === false) {
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
while(position$1 < size) {
|
|
177
|
+
lastPosition = position$1;
|
|
178
|
+
if (forEach(checkedRead()) === false) {
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
values = [ value ];
|
|
185
|
+
while(position$1 < size) {
|
|
186
|
+
lastPosition = position$1;
|
|
187
|
+
values.push(checkedRead());
|
|
188
|
+
}
|
|
189
|
+
return values
|
|
190
|
+
}
|
|
191
|
+
} catch(error) {
|
|
192
|
+
error.lastPosition = lastPosition;
|
|
193
|
+
error.values = values;
|
|
194
|
+
throw error
|
|
195
|
+
} finally {
|
|
196
|
+
sequentialMode = false;
|
|
197
|
+
clearSource();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function checkedRead() {
|
|
202
|
+
try {
|
|
203
|
+
let result = read();
|
|
204
|
+
if (bundledStrings$1) {
|
|
205
|
+
if (position$1 >= bundledStrings$1.postBundlePosition) {
|
|
206
|
+
let error = new Error('Unexpected bundle position');
|
|
207
|
+
error.incomplete = true;
|
|
208
|
+
throw error
|
|
209
|
+
}
|
|
210
|
+
// bundled strings to skip past
|
|
211
|
+
position$1 = bundledStrings$1.postBundlePosition;
|
|
212
|
+
bundledStrings$1 = null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (position$1 == srcEnd) {
|
|
216
|
+
// finished reading this source, cleanup references
|
|
217
|
+
currentStructures = null;
|
|
218
|
+
src = null;
|
|
219
|
+
if (referenceMap)
|
|
220
|
+
referenceMap = null;
|
|
221
|
+
} else if (position$1 > srcEnd) {
|
|
222
|
+
// over read
|
|
223
|
+
let error = new Error('Unexpected end of CBOR data');
|
|
224
|
+
error.incomplete = true;
|
|
225
|
+
throw error
|
|
226
|
+
} else if (!sequentialMode) {
|
|
227
|
+
throw new Error('Data read, but end of buffer not reached')
|
|
228
|
+
}
|
|
229
|
+
// else more to read, but we are reading sequentially, so don't clear source yet
|
|
230
|
+
return result
|
|
231
|
+
} catch(error) {
|
|
232
|
+
clearSource();
|
|
233
|
+
if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer')) {
|
|
234
|
+
error.incomplete = true;
|
|
235
|
+
}
|
|
236
|
+
throw error
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function read() {
|
|
241
|
+
let token = src[position$1++];
|
|
242
|
+
let majorType = token >> 5;
|
|
243
|
+
token = token & 0x1f;
|
|
244
|
+
if (token > 0x17) {
|
|
245
|
+
switch (token) {
|
|
246
|
+
case 0x18:
|
|
247
|
+
token = src[position$1++];
|
|
248
|
+
break
|
|
249
|
+
case 0x19:
|
|
250
|
+
if (majorType == 7) {
|
|
251
|
+
return getFloat16()
|
|
252
|
+
}
|
|
253
|
+
token = dataView.getUint16(position$1);
|
|
254
|
+
position$1 += 2;
|
|
255
|
+
break
|
|
256
|
+
case 0x1a:
|
|
257
|
+
if (majorType == 7) {
|
|
258
|
+
let value = dataView.getFloat32(position$1);
|
|
259
|
+
if (currentDecoder.useFloat32 > 2) {
|
|
260
|
+
// this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
261
|
+
let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
|
|
262
|
+
position$1 += 4;
|
|
263
|
+
return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
|
|
264
|
+
}
|
|
265
|
+
position$1 += 4;
|
|
266
|
+
return value
|
|
267
|
+
}
|
|
268
|
+
token = dataView.getUint32(position$1);
|
|
269
|
+
position$1 += 4;
|
|
270
|
+
break
|
|
271
|
+
case 0x1b:
|
|
272
|
+
if (majorType == 7) {
|
|
273
|
+
let value = dataView.getFloat64(position$1);
|
|
274
|
+
position$1 += 8;
|
|
275
|
+
return value
|
|
276
|
+
}
|
|
277
|
+
if (majorType > 1) {
|
|
278
|
+
if (dataView.getUint32(position$1) > 0)
|
|
279
|
+
throw new Error('JavaScript does not support arrays, maps, or strings with length over 4294967295')
|
|
280
|
+
token = dataView.getUint32(position$1 + 4);
|
|
281
|
+
} else if (currentDecoder.int64AsNumber) {
|
|
282
|
+
token = dataView.getUint32(position$1) * 0x100000000;
|
|
283
|
+
token += dataView.getUint32(position$1 + 4);
|
|
284
|
+
} else
|
|
285
|
+
token = dataView.getBigUint64(position$1);
|
|
286
|
+
position$1 += 8;
|
|
287
|
+
break
|
|
288
|
+
case 0x1f:
|
|
289
|
+
// indefinite length
|
|
290
|
+
switch(majorType) {
|
|
291
|
+
case 2: // byte string
|
|
292
|
+
case 3: // text string
|
|
293
|
+
throw new Error('Indefinite length not supported for byte or text strings')
|
|
294
|
+
case 4: // array
|
|
295
|
+
let array = [];
|
|
296
|
+
let value, i = 0;
|
|
297
|
+
while ((value = read()) != STOP_CODE) {
|
|
298
|
+
if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
|
|
299
|
+
array[i++] = value;
|
|
300
|
+
}
|
|
301
|
+
return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
|
|
302
|
+
case 5: // map
|
|
303
|
+
let key;
|
|
304
|
+
if (currentDecoder.mapsAsObjects) {
|
|
305
|
+
let object = {};
|
|
306
|
+
let i = 0;
|
|
307
|
+
if (currentDecoder.keyMap) {
|
|
308
|
+
while((key = read()) != STOP_CODE) {
|
|
309
|
+
if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
|
|
310
|
+
object[safeKey(currentDecoder.decodeKey(key))] = read();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
while ((key = read()) != STOP_CODE) {
|
|
315
|
+
if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
|
|
316
|
+
object[safeKey(key)] = read();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return object
|
|
320
|
+
} else {
|
|
321
|
+
if (restoreMapsAsObject) {
|
|
322
|
+
currentDecoder.mapsAsObjects = true;
|
|
323
|
+
restoreMapsAsObject = false;
|
|
324
|
+
}
|
|
325
|
+
let map = new Map();
|
|
326
|
+
if (currentDecoder.keyMap) {
|
|
327
|
+
let i = 0;
|
|
328
|
+
while((key = read()) != STOP_CODE) {
|
|
329
|
+
if (i++ >= maxMapSize) {
|
|
330
|
+
throw new Error(`Map size exceeds ${maxMapSize}`);
|
|
331
|
+
}
|
|
332
|
+
map.set(currentDecoder.decodeKey(key), read());
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
let i = 0;
|
|
337
|
+
while ((key = read()) != STOP_CODE) {
|
|
338
|
+
if (i++ >= maxMapSize) {
|
|
339
|
+
throw new Error(`Map size exceeds ${maxMapSize}`);
|
|
340
|
+
}
|
|
341
|
+
map.set(key, read());
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return map
|
|
345
|
+
}
|
|
346
|
+
case 7:
|
|
347
|
+
return STOP_CODE
|
|
348
|
+
default:
|
|
349
|
+
throw new Error('Invalid major type for indefinite length ' + majorType)
|
|
350
|
+
}
|
|
351
|
+
default:
|
|
352
|
+
throw new Error('Unknown token ' + token)
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
switch (majorType) {
|
|
356
|
+
case 0: // positive int
|
|
357
|
+
return token
|
|
358
|
+
case 1: // negative int
|
|
359
|
+
return ~token
|
|
360
|
+
case 2: // buffer
|
|
361
|
+
return readBin(token)
|
|
362
|
+
case 3: // string
|
|
363
|
+
if (srcStringEnd >= position$1) {
|
|
364
|
+
return srcString.slice(position$1 - srcStringStart, (position$1 += token) - srcStringStart)
|
|
365
|
+
}
|
|
366
|
+
if (srcStringEnd == 0 && srcEnd < 140 && token < 32) {
|
|
367
|
+
// for small blocks, avoiding the overhead of the extract call is helpful
|
|
368
|
+
let string = token < 16 ? shortStringInJS(token) : longStringInJS(token);
|
|
369
|
+
if (string != null)
|
|
370
|
+
return string
|
|
371
|
+
}
|
|
372
|
+
return readFixedString(token)
|
|
373
|
+
case 4: // array
|
|
374
|
+
if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
|
|
375
|
+
let array = new Array(token);
|
|
376
|
+
//if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
|
|
377
|
+
//else
|
|
378
|
+
for (let i = 0; i < token; i++) array[i] = read();
|
|
379
|
+
return array
|
|
380
|
+
case 5: // map
|
|
381
|
+
if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
|
|
382
|
+
if (currentDecoder.mapsAsObjects) {
|
|
383
|
+
let object = {};
|
|
384
|
+
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
|
|
385
|
+
else for (let i = 0; i < token; i++) object[safeKey(read())] = read();
|
|
386
|
+
return object
|
|
387
|
+
} else {
|
|
388
|
+
if (restoreMapsAsObject) {
|
|
389
|
+
currentDecoder.mapsAsObjects = true;
|
|
390
|
+
restoreMapsAsObject = false;
|
|
391
|
+
}
|
|
392
|
+
let map = new Map();
|
|
393
|
+
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) map.set(currentDecoder.decodeKey(read()),read());
|
|
394
|
+
else for (let i = 0; i < token; i++) map.set(read(), read());
|
|
395
|
+
return map
|
|
396
|
+
}
|
|
397
|
+
case 6: // extension
|
|
398
|
+
if (token >= BUNDLED_STRINGS_ID) {
|
|
399
|
+
let structure = currentStructures[token & 0x1fff]; // check record structures first
|
|
400
|
+
// At some point we may provide an option for dynamic tag assignment with a range like token >= 8 && (token < 16 || (token > 0x80 && token < 0xc0) || (token > 0x130 && token < 0x4000))
|
|
401
|
+
if (structure) {
|
|
402
|
+
if (!structure.read) structure.read = createStructureReader(structure);
|
|
403
|
+
return structure.read()
|
|
404
|
+
}
|
|
405
|
+
if (token < 0x10000) {
|
|
406
|
+
if (token == RECORD_INLINE_ID) { // we do a special check for this so that we can keep the
|
|
407
|
+
// currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
|
|
408
|
+
let length = readJustLength();
|
|
409
|
+
let id = read();
|
|
410
|
+
let structure = read();
|
|
411
|
+
recordDefinition(id, structure);
|
|
412
|
+
let object = {};
|
|
413
|
+
if (currentDecoder.keyMap) for (let i = 2; i < length; i++) {
|
|
414
|
+
let key = currentDecoder.decodeKey(structure[i - 2]);
|
|
415
|
+
object[safeKey(key)] = read();
|
|
416
|
+
}
|
|
417
|
+
else for (let i = 2; i < length; i++) {
|
|
418
|
+
let key = structure[i - 2];
|
|
419
|
+
object[safeKey(key)] = read();
|
|
420
|
+
}
|
|
421
|
+
return object
|
|
422
|
+
}
|
|
423
|
+
else if (token == RECORD_DEFINITIONS_ID) {
|
|
424
|
+
let length = readJustLength();
|
|
425
|
+
let id = read();
|
|
426
|
+
for (let i = 2; i < length; i++) {
|
|
427
|
+
recordDefinition(id++, read());
|
|
428
|
+
}
|
|
429
|
+
return read()
|
|
430
|
+
} else if (token == BUNDLED_STRINGS_ID) {
|
|
431
|
+
return readBundleExt()
|
|
432
|
+
}
|
|
433
|
+
if (currentDecoder.getShared) {
|
|
434
|
+
loadShared();
|
|
435
|
+
structure = currentStructures[token & 0x1fff];
|
|
436
|
+
if (structure) {
|
|
437
|
+
if (!structure.read)
|
|
438
|
+
structure.read = createStructureReader(structure);
|
|
439
|
+
return structure.read()
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
let extension = currentExtensions[token];
|
|
445
|
+
if (extension) {
|
|
446
|
+
if (extension.handlesRead)
|
|
447
|
+
return extension(read)
|
|
448
|
+
else
|
|
449
|
+
return extension(read())
|
|
450
|
+
} else {
|
|
451
|
+
let input = read();
|
|
452
|
+
for (let i = 0; i < currentExtensionRanges.length; i++) {
|
|
453
|
+
let value = currentExtensionRanges[i](token, input);
|
|
454
|
+
if (value !== undefined)
|
|
455
|
+
return value
|
|
456
|
+
}
|
|
457
|
+
return new Tag(input, token)
|
|
458
|
+
}
|
|
459
|
+
case 7: // fixed value
|
|
460
|
+
switch (token) {
|
|
461
|
+
case 0x14: return false
|
|
462
|
+
case 0x15: return true
|
|
463
|
+
case 0x16: return null
|
|
464
|
+
case 0x17: return; // undefined
|
|
465
|
+
case 0x1f:
|
|
466
|
+
default:
|
|
467
|
+
let packedValue = (packedValues || getPackedValues())[token];
|
|
468
|
+
if (packedValue !== undefined)
|
|
469
|
+
return packedValue
|
|
470
|
+
throw new Error('Unknown token ' + token)
|
|
471
|
+
}
|
|
472
|
+
default: // negative int
|
|
473
|
+
if (isNaN(token)) {
|
|
474
|
+
let error = new Error('Unexpected end of CBOR data');
|
|
475
|
+
error.incomplete = true;
|
|
476
|
+
throw error
|
|
477
|
+
}
|
|
478
|
+
throw new Error('Unknown CBOR token ' + token)
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
|
|
482
|
+
function createStructureReader(structure) {
|
|
483
|
+
if (!structure) throw new Error('Structure is required in record definition');
|
|
484
|
+
function readObject() {
|
|
485
|
+
// get the array size from the header
|
|
486
|
+
let length = src[position$1++];
|
|
487
|
+
//let majorType = token >> 5
|
|
488
|
+
length = length & 0x1f;
|
|
489
|
+
if (length > 0x17) {
|
|
490
|
+
switch (length) {
|
|
491
|
+
case 0x18:
|
|
492
|
+
length = src[position$1++];
|
|
493
|
+
break
|
|
494
|
+
case 0x19:
|
|
495
|
+
length = dataView.getUint16(position$1);
|
|
496
|
+
position$1 += 2;
|
|
497
|
+
break
|
|
498
|
+
case 0x1a:
|
|
499
|
+
length = dataView.getUint32(position$1);
|
|
500
|
+
position$1 += 4;
|
|
501
|
+
break
|
|
502
|
+
default:
|
|
503
|
+
throw new Error('Expected array header, but got ' + src[position$1 - 1])
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
|
|
507
|
+
let compiledReader = this.compiledReader; // first look to see if we have the fast compiled function
|
|
508
|
+
while(compiledReader) {
|
|
509
|
+
// we have a fast compiled object literal reader
|
|
510
|
+
if (compiledReader.propertyCount === length)
|
|
511
|
+
return compiledReader(read) // with the right length, so we use it
|
|
512
|
+
compiledReader = compiledReader.next; // see if there is another reader with the right length
|
|
513
|
+
}
|
|
514
|
+
if (this.slowReads++ >= inlineObjectReadThreshold) { // create a fast compiled reader
|
|
515
|
+
let array = this.length == length ? this : this.slice(0, length);
|
|
516
|
+
compiledReader = currentDecoder.keyMap
|
|
517
|
+
? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? safeKey(k) + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
|
|
518
|
+
: new Function('r', 'return {' + array.map(key => validName.test(key) ? safeKey(key) + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
|
|
519
|
+
if (this.compiledReader)
|
|
520
|
+
compiledReader.next = this.compiledReader; // if there is an existing one, we store multiple readers as a linked list because it is usually pretty rare to have multiple readers (of different length) for the same structure
|
|
521
|
+
compiledReader.propertyCount = length;
|
|
522
|
+
this.compiledReader = compiledReader;
|
|
523
|
+
return compiledReader(read)
|
|
524
|
+
}
|
|
525
|
+
let object = {};
|
|
526
|
+
if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[safeKey(currentDecoder.decodeKey(this[i]))] = read();
|
|
527
|
+
else for (let i = 0; i < length; i++) {
|
|
528
|
+
object[safeKey(this[i])] = read();
|
|
529
|
+
}
|
|
530
|
+
return object
|
|
531
|
+
}
|
|
532
|
+
structure.slowReads = 0;
|
|
533
|
+
return readObject
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function safeKey(key) {
|
|
537
|
+
// protect against prototype pollution
|
|
538
|
+
if (typeof key === 'string') return key === '__proto__' ? '__proto_' : key
|
|
539
|
+
if (typeof key === 'number' || typeof key === 'boolean' || typeof key === 'bigint') return key.toString();
|
|
540
|
+
if (key == null) return key + '';
|
|
541
|
+
// protect against expensive (DoS) string conversions
|
|
542
|
+
throw new Error('Invalid property name type ' + typeof key);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
let readFixedString = readStringJS;
|
|
546
|
+
function setExtractor(extractStrings) {
|
|
547
|
+
readFixedString = readString();
|
|
548
|
+
function readString(headerLength) {
|
|
549
|
+
return function readString(length) {
|
|
550
|
+
let string = strings[stringPosition++];
|
|
551
|
+
if (string == null) {
|
|
552
|
+
if (bundledStrings$1)
|
|
553
|
+
return readStringJS(length)
|
|
554
|
+
let extraction = extractStrings(position$1, srcEnd, length, src);
|
|
555
|
+
if (typeof extraction == 'string') {
|
|
556
|
+
string = extraction;
|
|
557
|
+
strings = EMPTY_ARRAY;
|
|
558
|
+
} else {
|
|
559
|
+
strings = extraction;
|
|
560
|
+
stringPosition = 1;
|
|
561
|
+
srcStringEnd = 1; // even if a utf-8 string was decoded, must indicate we are in the midst of extracted strings and can't skip strings
|
|
562
|
+
string = strings[0];
|
|
563
|
+
if (string === undefined)
|
|
564
|
+
throw new Error('Unexpected end of buffer')
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
let srcStringLength = string.length;
|
|
568
|
+
if (srcStringLength <= length) {
|
|
569
|
+
position$1 += length;
|
|
570
|
+
return string
|
|
571
|
+
}
|
|
572
|
+
srcString = string;
|
|
573
|
+
srcStringStart = position$1;
|
|
574
|
+
srcStringEnd = position$1 + srcStringLength;
|
|
575
|
+
position$1 += length;
|
|
576
|
+
return string.slice(0, length) // we know we just want the beginning
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
function readStringJS(length) {
|
|
581
|
+
let result;
|
|
582
|
+
if (length < 16) {
|
|
583
|
+
if (result = shortStringInJS(length))
|
|
584
|
+
return result
|
|
585
|
+
}
|
|
586
|
+
if (length > 64 && decoder)
|
|
587
|
+
return decoder.decode(src.subarray(position$1, position$1 += length))
|
|
588
|
+
const end = position$1 + length;
|
|
589
|
+
const units = [];
|
|
590
|
+
result = '';
|
|
591
|
+
while (position$1 < end) {
|
|
592
|
+
const byte1 = src[position$1++];
|
|
593
|
+
if ((byte1 & 0x80) === 0) {
|
|
594
|
+
// 1 byte
|
|
595
|
+
units.push(byte1);
|
|
596
|
+
} else if ((byte1 & 0xe0) === 0xc0) {
|
|
597
|
+
// 2 bytes
|
|
598
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
599
|
+
units.push(((byte1 & 0x1f) << 6) | byte2);
|
|
600
|
+
} else if ((byte1 & 0xf0) === 0xe0) {
|
|
601
|
+
// 3 bytes
|
|
602
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
603
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
604
|
+
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
|
|
605
|
+
} else if ((byte1 & 0xf8) === 0xf0) {
|
|
606
|
+
// 4 bytes
|
|
607
|
+
const byte2 = src[position$1++] & 0x3f;
|
|
608
|
+
const byte3 = src[position$1++] & 0x3f;
|
|
609
|
+
const byte4 = src[position$1++] & 0x3f;
|
|
610
|
+
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
611
|
+
if (unit > 0xffff) {
|
|
612
|
+
unit -= 0x10000;
|
|
613
|
+
units.push(((unit >>> 10) & 0x3ff) | 0xd800);
|
|
614
|
+
unit = 0xdc00 | (unit & 0x3ff);
|
|
615
|
+
}
|
|
616
|
+
units.push(unit);
|
|
617
|
+
} else {
|
|
618
|
+
units.push(byte1);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (units.length >= 0x1000) {
|
|
622
|
+
result += fromCharCode.apply(String, units);
|
|
623
|
+
units.length = 0;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
if (units.length > 0) {
|
|
628
|
+
result += fromCharCode.apply(String, units);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
return result
|
|
632
|
+
}
|
|
633
|
+
let fromCharCode = String.fromCharCode;
|
|
634
|
+
function longStringInJS(length) {
|
|
635
|
+
let start = position$1;
|
|
636
|
+
let bytes = new Array(length);
|
|
637
|
+
for (let i = 0; i < length; i++) {
|
|
638
|
+
const byte = src[position$1++];
|
|
639
|
+
if ((byte & 0x80) > 0) {
|
|
640
|
+
position$1 = start;
|
|
641
|
+
return
|
|
642
|
+
}
|
|
643
|
+
bytes[i] = byte;
|
|
644
|
+
}
|
|
645
|
+
return fromCharCode.apply(String, bytes)
|
|
646
|
+
}
|
|
647
|
+
function shortStringInJS(length) {
|
|
648
|
+
if (length < 4) {
|
|
649
|
+
if (length < 2) {
|
|
650
|
+
if (length === 0)
|
|
651
|
+
return ''
|
|
652
|
+
else {
|
|
653
|
+
let a = src[position$1++];
|
|
654
|
+
if ((a & 0x80) > 1) {
|
|
655
|
+
position$1 -= 1;
|
|
656
|
+
return
|
|
657
|
+
}
|
|
658
|
+
return fromCharCode(a)
|
|
659
|
+
}
|
|
660
|
+
} else {
|
|
661
|
+
let a = src[position$1++];
|
|
662
|
+
let b = src[position$1++];
|
|
663
|
+
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
664
|
+
position$1 -= 2;
|
|
665
|
+
return
|
|
666
|
+
}
|
|
667
|
+
if (length < 3)
|
|
668
|
+
return fromCharCode(a, b)
|
|
669
|
+
let c = src[position$1++];
|
|
670
|
+
if ((c & 0x80) > 0) {
|
|
671
|
+
position$1 -= 3;
|
|
672
|
+
return
|
|
673
|
+
}
|
|
674
|
+
return fromCharCode(a, b, c)
|
|
675
|
+
}
|
|
676
|
+
} else {
|
|
677
|
+
let a = src[position$1++];
|
|
678
|
+
let b = src[position$1++];
|
|
679
|
+
let c = src[position$1++];
|
|
680
|
+
let d = src[position$1++];
|
|
681
|
+
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
682
|
+
position$1 -= 4;
|
|
683
|
+
return
|
|
684
|
+
}
|
|
685
|
+
if (length < 6) {
|
|
686
|
+
if (length === 4)
|
|
687
|
+
return fromCharCode(a, b, c, d)
|
|
688
|
+
else {
|
|
689
|
+
let e = src[position$1++];
|
|
690
|
+
if ((e & 0x80) > 0) {
|
|
691
|
+
position$1 -= 5;
|
|
692
|
+
return
|
|
693
|
+
}
|
|
694
|
+
return fromCharCode(a, b, c, d, e)
|
|
695
|
+
}
|
|
696
|
+
} else if (length < 8) {
|
|
697
|
+
let e = src[position$1++];
|
|
698
|
+
let f = src[position$1++];
|
|
699
|
+
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
700
|
+
position$1 -= 6;
|
|
701
|
+
return
|
|
702
|
+
}
|
|
703
|
+
if (length < 7)
|
|
704
|
+
return fromCharCode(a, b, c, d, e, f)
|
|
705
|
+
let g = src[position$1++];
|
|
706
|
+
if ((g & 0x80) > 0) {
|
|
707
|
+
position$1 -= 7;
|
|
708
|
+
return
|
|
709
|
+
}
|
|
710
|
+
return fromCharCode(a, b, c, d, e, f, g)
|
|
711
|
+
} else {
|
|
712
|
+
let e = src[position$1++];
|
|
713
|
+
let f = src[position$1++];
|
|
714
|
+
let g = src[position$1++];
|
|
715
|
+
let h = src[position$1++];
|
|
716
|
+
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
717
|
+
position$1 -= 8;
|
|
718
|
+
return
|
|
719
|
+
}
|
|
720
|
+
if (length < 10) {
|
|
721
|
+
if (length === 8)
|
|
722
|
+
return fromCharCode(a, b, c, d, e, f, g, h)
|
|
723
|
+
else {
|
|
724
|
+
let i = src[position$1++];
|
|
725
|
+
if ((i & 0x80) > 0) {
|
|
726
|
+
position$1 -= 9;
|
|
727
|
+
return
|
|
728
|
+
}
|
|
729
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i)
|
|
730
|
+
}
|
|
731
|
+
} else if (length < 12) {
|
|
732
|
+
let i = src[position$1++];
|
|
733
|
+
let j = src[position$1++];
|
|
734
|
+
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
735
|
+
position$1 -= 10;
|
|
736
|
+
return
|
|
737
|
+
}
|
|
738
|
+
if (length < 11)
|
|
739
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j)
|
|
740
|
+
let k = src[position$1++];
|
|
741
|
+
if ((k & 0x80) > 0) {
|
|
742
|
+
position$1 -= 11;
|
|
743
|
+
return
|
|
744
|
+
}
|
|
745
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
|
|
746
|
+
} else {
|
|
747
|
+
let i = src[position$1++];
|
|
748
|
+
let j = src[position$1++];
|
|
749
|
+
let k = src[position$1++];
|
|
750
|
+
let l = src[position$1++];
|
|
751
|
+
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
752
|
+
position$1 -= 12;
|
|
753
|
+
return
|
|
754
|
+
}
|
|
755
|
+
if (length < 14) {
|
|
756
|
+
if (length === 12)
|
|
757
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
|
|
758
|
+
else {
|
|
759
|
+
let m = src[position$1++];
|
|
760
|
+
if ((m & 0x80) > 0) {
|
|
761
|
+
position$1 -= 13;
|
|
762
|
+
return
|
|
763
|
+
}
|
|
764
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
765
|
+
}
|
|
766
|
+
} else {
|
|
767
|
+
let m = src[position$1++];
|
|
768
|
+
let n = src[position$1++];
|
|
769
|
+
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
770
|
+
position$1 -= 14;
|
|
771
|
+
return
|
|
772
|
+
}
|
|
773
|
+
if (length < 15)
|
|
774
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
|
|
775
|
+
let o = src[position$1++];
|
|
776
|
+
if ((o & 0x80) > 0) {
|
|
777
|
+
position$1 -= 15;
|
|
778
|
+
return
|
|
779
|
+
}
|
|
780
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
function readBin(length) {
|
|
788
|
+
return currentDecoder.copyBuffers ?
|
|
789
|
+
// specifically use the copying slice (not the node one)
|
|
790
|
+
Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
|
|
791
|
+
src.subarray(position$1, position$1 += length)
|
|
792
|
+
}
|
|
793
|
+
let f32Array = new Float32Array(1);
|
|
794
|
+
let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
|
|
795
|
+
function getFloat16() {
|
|
796
|
+
let byte0 = src[position$1++];
|
|
797
|
+
let byte1 = src[position$1++];
|
|
798
|
+
let exponent = (byte0 & 0x7f) >> 2;
|
|
799
|
+
if (exponent === 0x1f) { // specials
|
|
800
|
+
if (byte1 || (byte0 & 3))
|
|
801
|
+
return NaN;
|
|
802
|
+
return (byte0 & 0x80) ? -Infinity : Infinity;
|
|
803
|
+
}
|
|
804
|
+
if (exponent === 0) { // sub-normals
|
|
805
|
+
// significand with 10 fractional bits and divided by 2^14
|
|
806
|
+
let abs = (((byte0 & 3) << 8) | byte1) / (1 << 24);
|
|
807
|
+
return (byte0 & 0x80) ? -abs : abs
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
u8Array[3] = (byte0 & 0x80) | // sign bit
|
|
811
|
+
((exponent >> 1) + 56); // 4 of 5 of the exponent bits, re-offset-ed
|
|
812
|
+
u8Array[2] = ((byte0 & 7) << 5) | // last exponent bit and first two mantissa bits
|
|
813
|
+
(byte1 >> 3); // next 5 bits of mantissa
|
|
814
|
+
u8Array[1] = byte1 << 5; // last three bits of mantissa
|
|
815
|
+
u8Array[0] = 0;
|
|
816
|
+
return f32Array[0];
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
new Array(4096);
|
|
820
|
+
|
|
821
|
+
class Tag {
|
|
822
|
+
constructor(value, tag) {
|
|
823
|
+
this.value = value;
|
|
824
|
+
this.tag = tag;
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
currentExtensions[0] = (dateString) => {
|
|
829
|
+
// string date extension
|
|
830
|
+
return new Date(dateString)
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
currentExtensions[1] = (epochSec) => {
|
|
834
|
+
// numeric date extension
|
|
835
|
+
return new Date(Math.round(epochSec * 1000))
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
currentExtensions[2] = (buffer) => {
|
|
839
|
+
// bigint extension
|
|
840
|
+
let value = BigInt(0);
|
|
841
|
+
for (let i = 0, l = buffer.byteLength; i < l; i++) {
|
|
842
|
+
value = BigInt(buffer[i]) + (value << BigInt(8));
|
|
843
|
+
}
|
|
844
|
+
return value
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
currentExtensions[3] = (buffer) => {
|
|
848
|
+
// negative bigint extension
|
|
849
|
+
return BigInt(-1) - currentExtensions[2](buffer)
|
|
850
|
+
};
|
|
851
|
+
currentExtensions[4] = (fraction) => {
|
|
852
|
+
// best to reparse to maintain accuracy
|
|
853
|
+
return +(fraction[1] + 'e' + fraction[0])
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
currentExtensions[5] = (fraction) => {
|
|
857
|
+
// probably not sufficiently accurate
|
|
858
|
+
return fraction[1] * Math.exp(fraction[0] * Math.log(2))
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
// the registration of the record definition extension
|
|
862
|
+
const recordDefinition = (id, structure) => {
|
|
863
|
+
id = id - 0xe000;
|
|
864
|
+
let existingStructure = currentStructures[id];
|
|
865
|
+
if (existingStructure && existingStructure.isShared) {
|
|
866
|
+
(currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
|
|
867
|
+
}
|
|
868
|
+
currentStructures[id] = structure;
|
|
869
|
+
|
|
870
|
+
structure.read = createStructureReader(structure);
|
|
871
|
+
};
|
|
872
|
+
currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => {
|
|
873
|
+
let length = data.length;
|
|
874
|
+
let structure = data[1];
|
|
875
|
+
recordDefinition(data[0], structure);
|
|
876
|
+
let object = {};
|
|
877
|
+
for (let i = 2; i < length; i++) {
|
|
878
|
+
let key = structure[i - 2];
|
|
879
|
+
object[safeKey(key)] = data[i];
|
|
880
|
+
}
|
|
881
|
+
return object
|
|
882
|
+
};
|
|
883
|
+
currentExtensions[14] = (value) => {
|
|
884
|
+
if (bundledStrings$1)
|
|
885
|
+
return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 += value)
|
|
886
|
+
return new Tag(value, 14)
|
|
887
|
+
};
|
|
888
|
+
currentExtensions[15] = (value) => {
|
|
889
|
+
if (bundledStrings$1)
|
|
890
|
+
return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
|
|
891
|
+
return new Tag(value, 15)
|
|
892
|
+
};
|
|
893
|
+
let glbl = { Error, RegExp };
|
|
894
|
+
currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
|
|
895
|
+
return (glbl[data[0]] || Error)(data[1], data[2])
|
|
896
|
+
};
|
|
897
|
+
const packedTable = (read) => {
|
|
898
|
+
if (src[position$1++] != 0x84) {
|
|
899
|
+
let error = new Error('Packed values structure must be followed by a 4 element array');
|
|
900
|
+
if (src.length < position$1)
|
|
901
|
+
error.incomplete = true;
|
|
902
|
+
throw error
|
|
903
|
+
}
|
|
904
|
+
let newPackedValues = read(); // packed values
|
|
905
|
+
if (!newPackedValues || !newPackedValues.length) {
|
|
906
|
+
let error = new Error('Packed values structure must be followed by a 4 element array');
|
|
907
|
+
error.incomplete = true;
|
|
908
|
+
throw error
|
|
909
|
+
}
|
|
910
|
+
packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
|
|
911
|
+
packedValues.prefixes = read();
|
|
912
|
+
packedValues.suffixes = read();
|
|
913
|
+
return read() // read the rump
|
|
914
|
+
};
|
|
915
|
+
packedTable.handlesRead = true;
|
|
916
|
+
currentExtensions[51] = packedTable;
|
|
917
|
+
|
|
918
|
+
currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference
|
|
919
|
+
if (!packedValues) {
|
|
920
|
+
if (currentDecoder.getShared)
|
|
921
|
+
loadShared();
|
|
922
|
+
else
|
|
923
|
+
return new Tag(data, PACKED_REFERENCE_TAG_ID)
|
|
924
|
+
}
|
|
925
|
+
if (typeof data == 'number')
|
|
926
|
+
return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
|
|
927
|
+
let error = new Error('No support for non-integer packed references yet');
|
|
928
|
+
if (data === undefined)
|
|
929
|
+
error.incomplete = true;
|
|
930
|
+
throw error
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
// The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
|
|
934
|
+
// the real thing would need to implemennt more logic to populate the stringRefs table and
|
|
935
|
+
// maintain a stack of stringRef "namespaces".
|
|
936
|
+
//
|
|
937
|
+
// currentExtensions[25] = (id) => {
|
|
938
|
+
// return stringRefs[id]
|
|
939
|
+
// }
|
|
940
|
+
// currentExtensions[256] = (read) => {
|
|
941
|
+
// stringRefs = []
|
|
942
|
+
// try {
|
|
943
|
+
// return read()
|
|
944
|
+
// } finally {
|
|
945
|
+
// stringRefs = null
|
|
946
|
+
// }
|
|
947
|
+
// }
|
|
948
|
+
// currentExtensions[256].handlesRead = true
|
|
949
|
+
|
|
950
|
+
currentExtensions[28] = (read) => {
|
|
951
|
+
// shareable http://cbor.schmorp.de/value-sharing (for structured clones)
|
|
952
|
+
if (!referenceMap) {
|
|
953
|
+
referenceMap = new Map();
|
|
954
|
+
referenceMap.id = 0;
|
|
955
|
+
}
|
|
956
|
+
let id = referenceMap.id++;
|
|
957
|
+
let startingPosition = position$1;
|
|
958
|
+
let token = src[position$1];
|
|
959
|
+
let target;
|
|
960
|
+
// TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
|
|
961
|
+
// ahead past references to record structure definitions
|
|
962
|
+
if ((token >> 5) == 4)
|
|
963
|
+
target = [];
|
|
964
|
+
else
|
|
965
|
+
target = {};
|
|
966
|
+
|
|
967
|
+
let refEntry = { target }; // a placeholder object
|
|
968
|
+
referenceMap.set(id, refEntry);
|
|
969
|
+
let targetProperties = read(); // read the next value as the target object to id
|
|
970
|
+
if (refEntry.used) {// there is a cycle, so we have to assign properties to original target
|
|
971
|
+
if (Object.getPrototypeOf(target) !== Object.getPrototypeOf(targetProperties)) {
|
|
972
|
+
// this means that the returned target does not match the targetProperties, so we need rerun the read to
|
|
973
|
+
// have the correctly create instance be assigned as a reference, then we do the copy the properties back to the
|
|
974
|
+
// target
|
|
975
|
+
// reset the position so that the read can be repeated
|
|
976
|
+
position$1 = startingPosition;
|
|
977
|
+
// the returned instance is our new target for references
|
|
978
|
+
target = targetProperties;
|
|
979
|
+
referenceMap.set(id, { target });
|
|
980
|
+
targetProperties = read();
|
|
981
|
+
}
|
|
982
|
+
return Object.assign(target, targetProperties)
|
|
983
|
+
}
|
|
984
|
+
refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
|
|
985
|
+
return targetProperties // no cycle, can just use the returned read object
|
|
986
|
+
};
|
|
987
|
+
currentExtensions[28].handlesRead = true;
|
|
988
|
+
|
|
989
|
+
currentExtensions[29] = (id) => {
|
|
990
|
+
// sharedref http://cbor.schmorp.de/value-sharing (for structured clones)
|
|
991
|
+
let refEntry = referenceMap.get(id);
|
|
992
|
+
refEntry.used = true;
|
|
993
|
+
return refEntry.target
|
|
994
|
+
};
|
|
995
|
+
|
|
996
|
+
currentExtensions[258] = (array) => new Set(array); // https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md
|
|
997
|
+
(currentExtensions[259] = (read) => {
|
|
998
|
+
// https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec
|
|
999
|
+
// for decoding as a standard Map
|
|
1000
|
+
if (currentDecoder.mapsAsObjects) {
|
|
1001
|
+
currentDecoder.mapsAsObjects = false;
|
|
1002
|
+
restoreMapsAsObject = true;
|
|
1003
|
+
}
|
|
1004
|
+
return read()
|
|
1005
|
+
}).handlesRead = true;
|
|
1006
|
+
function combine(a, b) {
|
|
1007
|
+
if (typeof a === 'string')
|
|
1008
|
+
return a + b
|
|
1009
|
+
if (a instanceof Array)
|
|
1010
|
+
return a.concat(b)
|
|
1011
|
+
return Object.assign({}, a, b)
|
|
1012
|
+
}
|
|
1013
|
+
function getPackedValues() {
|
|
1014
|
+
if (!packedValues) {
|
|
1015
|
+
if (currentDecoder.getShared)
|
|
1016
|
+
loadShared();
|
|
1017
|
+
else
|
|
1018
|
+
throw new Error('No packed values available')
|
|
1019
|
+
}
|
|
1020
|
+
return packedValues
|
|
1021
|
+
}
|
|
1022
|
+
const SHARED_DATA_TAG_ID = 0x53687264; // ascii 'Shrd'
|
|
1023
|
+
currentExtensionRanges.push((tag, input) => {
|
|
1024
|
+
if (tag >= 225 && tag <= 255)
|
|
1025
|
+
return combine(getPackedValues().prefixes[tag - 224], input)
|
|
1026
|
+
if (tag >= 28704 && tag <= 32767)
|
|
1027
|
+
return combine(getPackedValues().prefixes[tag - 28672], input)
|
|
1028
|
+
if (tag >= 1879052288 && tag <= 2147483647)
|
|
1029
|
+
return combine(getPackedValues().prefixes[tag - 1879048192], input)
|
|
1030
|
+
if (tag >= 216 && tag <= 223)
|
|
1031
|
+
return combine(input, getPackedValues().suffixes[tag - 216])
|
|
1032
|
+
if (tag >= 27647 && tag <= 28671)
|
|
1033
|
+
return combine(input, getPackedValues().suffixes[tag - 27639])
|
|
1034
|
+
if (tag >= 1811940352 && tag <= 1879048191)
|
|
1035
|
+
return combine(input, getPackedValues().suffixes[tag - 1811939328])
|
|
1036
|
+
if (tag == SHARED_DATA_TAG_ID) {// we do a special check for this so that we can keep the currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
|
|
1037
|
+
return {
|
|
1038
|
+
packedValues: packedValues,
|
|
1039
|
+
structures: currentStructures.slice(0),
|
|
1040
|
+
version: input,
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
if (tag == 55799) // self-descriptive CBOR tag, just return input value
|
|
1044
|
+
return input
|
|
1045
|
+
});
|
|
1046
|
+
|
|
1047
|
+
const isLittleEndianMachine$1 = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
1048
|
+
const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1049
|
+
typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
1050
|
+
typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
|
|
1051
|
+
const typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 85, 86];
|
|
1052
|
+
for (let i = 0; i < typedArrays.length; i++) {
|
|
1053
|
+
registerTypedArray(typedArrays[i], typedArrayTags[i]);
|
|
1054
|
+
}
|
|
1055
|
+
function registerTypedArray(TypedArray, tag) {
|
|
1056
|
+
let dvMethod = 'get' + TypedArray.name.slice(0, -5);
|
|
1057
|
+
let bytesPerElement;
|
|
1058
|
+
if (typeof TypedArray === 'function')
|
|
1059
|
+
bytesPerElement = TypedArray.BYTES_PER_ELEMENT;
|
|
1060
|
+
else
|
|
1061
|
+
TypedArray = null;
|
|
1062
|
+
for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
|
|
1063
|
+
if (!littleEndian && bytesPerElement == 1)
|
|
1064
|
+
continue
|
|
1065
|
+
let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : bytesPerElement == 8 ? 3 : 0;
|
|
1066
|
+
currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
|
|
1067
|
+
if (!TypedArray)
|
|
1068
|
+
throw new Error('Could not find typed array for code ' + tag)
|
|
1069
|
+
if (!currentDecoder.copyBuffers) {
|
|
1070
|
+
// try provide a direct view, but will only work if we are byte-aligned
|
|
1071
|
+
if (bytesPerElement === 1 ||
|
|
1072
|
+
bytesPerElement === 2 && !(buffer.byteOffset & 1) ||
|
|
1073
|
+
bytesPerElement === 4 && !(buffer.byteOffset & 3) ||
|
|
1074
|
+
bytesPerElement === 8 && !(buffer.byteOffset & 7))
|
|
1075
|
+
return new TypedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength >> sizeShift);
|
|
1076
|
+
}
|
|
1077
|
+
// we have to slice/copy here to get a new ArrayBuffer, if we are not word/byte aligned
|
|
1078
|
+
return new TypedArray(Uint8Array.prototype.slice.call(buffer, 0).buffer)
|
|
1079
|
+
} : buffer => {
|
|
1080
|
+
if (!TypedArray)
|
|
1081
|
+
throw new Error('Could not find typed array for code ' + tag)
|
|
1082
|
+
let dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
1083
|
+
let elements = buffer.length >> sizeShift;
|
|
1084
|
+
let ta = new TypedArray(elements);
|
|
1085
|
+
let method = dv[dvMethod];
|
|
1086
|
+
for (let i = 0; i < elements; i++) {
|
|
1087
|
+
ta[i] = method.call(dv, i << sizeShift, littleEndian);
|
|
1088
|
+
}
|
|
1089
|
+
return ta
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
function readBundleExt() {
|
|
1095
|
+
let length = readJustLength();
|
|
1096
|
+
let bundlePosition = position$1 + read();
|
|
1097
|
+
for (let i = 2; i < length; i++) {
|
|
1098
|
+
// skip past bundles that were already read
|
|
1099
|
+
let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
|
|
1100
|
+
position$1 += bundleLength;
|
|
1101
|
+
}
|
|
1102
|
+
let dataPosition = position$1;
|
|
1103
|
+
position$1 = bundlePosition;
|
|
1104
|
+
bundledStrings$1 = [readStringJS(readJustLength()), readStringJS(readJustLength())];
|
|
1105
|
+
bundledStrings$1.position0 = 0;
|
|
1106
|
+
bundledStrings$1.position1 = 0;
|
|
1107
|
+
bundledStrings$1.postBundlePosition = position$1;
|
|
1108
|
+
position$1 = dataPosition;
|
|
1109
|
+
return read()
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
function readJustLength() {
|
|
1113
|
+
let token = src[position$1++] & 0x1f;
|
|
1114
|
+
if (token > 0x17) {
|
|
1115
|
+
switch (token) {
|
|
1116
|
+
case 0x18:
|
|
1117
|
+
token = src[position$1++];
|
|
1118
|
+
break
|
|
1119
|
+
case 0x19:
|
|
1120
|
+
token = dataView.getUint16(position$1);
|
|
1121
|
+
position$1 += 2;
|
|
1122
|
+
break
|
|
1123
|
+
case 0x1a:
|
|
1124
|
+
token = dataView.getUint32(position$1);
|
|
1125
|
+
position$1 += 4;
|
|
1126
|
+
break
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return token
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
function loadShared() {
|
|
1133
|
+
if (currentDecoder.getShared) {
|
|
1134
|
+
let sharedData = saveState(() => {
|
|
1135
|
+
// save the state in case getShared modifies our buffer
|
|
1136
|
+
src = null;
|
|
1137
|
+
return currentDecoder.getShared()
|
|
1138
|
+
}) || {};
|
|
1139
|
+
let updatedStructures = sharedData.structures || [];
|
|
1140
|
+
currentDecoder.sharedVersion = sharedData.version;
|
|
1141
|
+
packedValues = currentDecoder.sharedValues = sharedData.packedValues;
|
|
1142
|
+
if (currentStructures === true)
|
|
1143
|
+
currentDecoder.structures = currentStructures = updatedStructures;
|
|
1144
|
+
else
|
|
1145
|
+
currentStructures.splice.apply(currentStructures, [0, updatedStructures.length].concat(updatedStructures));
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
function saveState(callback) {
|
|
1150
|
+
let savedSrcEnd = srcEnd;
|
|
1151
|
+
let savedPosition = position$1;
|
|
1152
|
+
let savedStringPosition = stringPosition;
|
|
1153
|
+
let savedSrcStringStart = srcStringStart;
|
|
1154
|
+
let savedSrcStringEnd = srcStringEnd;
|
|
1155
|
+
let savedSrcString = srcString;
|
|
1156
|
+
let savedStrings = strings;
|
|
1157
|
+
let savedReferenceMap = referenceMap;
|
|
1158
|
+
let savedBundledStrings = bundledStrings$1;
|
|
1159
|
+
|
|
1160
|
+
// TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
|
|
1161
|
+
let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
|
|
1162
|
+
let savedStructures = currentStructures;
|
|
1163
|
+
let savedDecoder = currentDecoder;
|
|
1164
|
+
let savedSequentialMode = sequentialMode;
|
|
1165
|
+
let value = callback();
|
|
1166
|
+
srcEnd = savedSrcEnd;
|
|
1167
|
+
position$1 = savedPosition;
|
|
1168
|
+
stringPosition = savedStringPosition;
|
|
1169
|
+
srcStringStart = savedSrcStringStart;
|
|
1170
|
+
srcStringEnd = savedSrcStringEnd;
|
|
1171
|
+
srcString = savedSrcString;
|
|
1172
|
+
strings = savedStrings;
|
|
1173
|
+
referenceMap = savedReferenceMap;
|
|
1174
|
+
bundledStrings$1 = savedBundledStrings;
|
|
1175
|
+
src = savedSrc;
|
|
1176
|
+
sequentialMode = savedSequentialMode;
|
|
1177
|
+
currentStructures = savedStructures;
|
|
1178
|
+
currentDecoder = savedDecoder;
|
|
1179
|
+
dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
|
|
1180
|
+
return value
|
|
1181
|
+
}
|
|
1182
|
+
function clearSource() {
|
|
1183
|
+
src = null;
|
|
1184
|
+
referenceMap = null;
|
|
1185
|
+
currentStructures = null;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
function addExtension$2(extension) {
|
|
1189
|
+
currentExtensions[extension.tag] = extension.decode;
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
|
|
1193
|
+
for (let i = 0; i < 256; i++) {
|
|
1194
|
+
mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
|
|
1195
|
+
}
|
|
1196
|
+
let defaultDecoder = new Decoder({ useRecords: false });
|
|
1197
|
+
const decode$1 = defaultDecoder.decode;
|
|
1198
|
+
const decodeMultiple = defaultDecoder.decodeMultiple;
|
|
1199
|
+
const FLOAT32_OPTIONS = {
|
|
1200
|
+
NEVER: 0,
|
|
1201
|
+
ALWAYS: 1,
|
|
1202
|
+
DECIMAL_ROUND: 3,
|
|
1203
|
+
DECIMAL_FIT: 4
|
|
1204
|
+
};
|
|
1205
|
+
|
|
1206
|
+
let textEncoder;
|
|
1207
|
+
try {
|
|
1208
|
+
textEncoder = new TextEncoder();
|
|
1209
|
+
} catch (error) {}
|
|
1210
|
+
let extensions, extensionClasses;
|
|
1211
|
+
const Buffer$1 = typeof globalThis === 'object' && globalThis.Buffer;
|
|
1212
|
+
const hasNodeBuffer = typeof Buffer$1 !== 'undefined';
|
|
1213
|
+
const ByteArrayAllocate = hasNodeBuffer ? Buffer$1.allocUnsafeSlow : Uint8Array;
|
|
1214
|
+
const ByteArray = hasNodeBuffer ? Buffer$1 : Uint8Array;
|
|
1215
|
+
const MAX_STRUCTURES = 0x100;
|
|
1216
|
+
const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
|
|
1217
|
+
let throwOnIterable;
|
|
1218
|
+
let target;
|
|
1219
|
+
let targetView;
|
|
1220
|
+
let position = 0;
|
|
1221
|
+
let safeEnd;
|
|
1222
|
+
let bundledStrings = null;
|
|
1223
|
+
const MAX_BUNDLE_SIZE = 0xf000;
|
|
1224
|
+
const hasNonLatin = /[\u0080-\uFFFF]/;
|
|
1225
|
+
const RECORD_SYMBOL = Symbol('record-id');
|
|
1226
|
+
let Encoder$1 = class Encoder extends Decoder {
|
|
1227
|
+
constructor(options) {
|
|
1228
|
+
super(options);
|
|
1229
|
+
this.offset = 0;
|
|
1230
|
+
let start;
|
|
1231
|
+
let sharedStructures;
|
|
1232
|
+
let hasSharedUpdate;
|
|
1233
|
+
let structures;
|
|
1234
|
+
let referenceMap;
|
|
1235
|
+
options = options || {};
|
|
1236
|
+
let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
|
|
1237
|
+
return target.utf8Write(string, position, maxBytes)
|
|
1238
|
+
} : (textEncoder && textEncoder.encodeInto) ?
|
|
1239
|
+
function(string, position) {
|
|
1240
|
+
return textEncoder.encodeInto(string, target.subarray(position)).written
|
|
1241
|
+
} : false;
|
|
1242
|
+
|
|
1243
|
+
let encoder = this;
|
|
1244
|
+
let hasSharedStructures = options.structures || options.saveStructures;
|
|
1245
|
+
let maxSharedStructures = options.maxSharedStructures;
|
|
1246
|
+
if (maxSharedStructures == null)
|
|
1247
|
+
maxSharedStructures = hasSharedStructures ? 128 : 0;
|
|
1248
|
+
if (maxSharedStructures > 8190)
|
|
1249
|
+
throw new Error('Maximum maxSharedStructure is 8190')
|
|
1250
|
+
let isSequential = options.sequential;
|
|
1251
|
+
if (isSequential) {
|
|
1252
|
+
maxSharedStructures = 0;
|
|
1253
|
+
}
|
|
1254
|
+
if (!this.structures)
|
|
1255
|
+
this.structures = [];
|
|
1256
|
+
if (this.saveStructures)
|
|
1257
|
+
this.saveShared = this.saveStructures;
|
|
1258
|
+
let samplingPackedValues, packedObjectMap, sharedValues = options.sharedValues;
|
|
1259
|
+
let sharedPackedObjectMap;
|
|
1260
|
+
if (sharedValues) {
|
|
1261
|
+
sharedPackedObjectMap = Object.create(null);
|
|
1262
|
+
for (let i = 0, l = sharedValues.length; i < l; i++) {
|
|
1263
|
+
sharedPackedObjectMap[sharedValues[i]] = i;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
let recordIdsToRemove = [];
|
|
1267
|
+
let transitionsCount = 0;
|
|
1268
|
+
let serializationsSinceTransitionRebuild = 0;
|
|
1269
|
+
|
|
1270
|
+
this.mapEncode = function(value, encodeOptions) {
|
|
1271
|
+
// Experimental support for premapping keys using _keyMap instad of keyMap - not optiimised yet)
|
|
1272
|
+
if (this._keyMap && !this._mapped) {
|
|
1273
|
+
//console.log('encoding ', value)
|
|
1274
|
+
switch (value.constructor.name) {
|
|
1275
|
+
case 'Array':
|
|
1276
|
+
value = value.map(r => this.encodeKeys(r));
|
|
1277
|
+
break
|
|
1278
|
+
//case 'Map':
|
|
1279
|
+
// value = this.encodeKeys(value)
|
|
1280
|
+
// break
|
|
1281
|
+
}
|
|
1282
|
+
//this._mapped = true
|
|
1283
|
+
}
|
|
1284
|
+
return this.encode(value, encodeOptions)
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
this.encode = function(value, encodeOptions) {
|
|
1288
|
+
if (!target) {
|
|
1289
|
+
target = new ByteArrayAllocate(8192);
|
|
1290
|
+
targetView = new DataView(target.buffer, 0, 8192);
|
|
1291
|
+
position = 0;
|
|
1292
|
+
}
|
|
1293
|
+
safeEnd = target.length - 10;
|
|
1294
|
+
if (safeEnd - position < 0x800) {
|
|
1295
|
+
// don't start too close to the end,
|
|
1296
|
+
target = new ByteArrayAllocate(target.length);
|
|
1297
|
+
targetView = new DataView(target.buffer, 0, target.length);
|
|
1298
|
+
safeEnd = target.length - 10;
|
|
1299
|
+
position = 0;
|
|
1300
|
+
} else if (encodeOptions === REUSE_BUFFER_MODE)
|
|
1301
|
+
position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
|
|
1302
|
+
start = position;
|
|
1303
|
+
if (encoder.useSelfDescribedHeader) {
|
|
1304
|
+
targetView.setUint32(position, 0xd9d9f700); // tag two byte, then self-descriptive tag
|
|
1305
|
+
position += 3;
|
|
1306
|
+
}
|
|
1307
|
+
referenceMap = encoder.structuredClone ? new Map() : null;
|
|
1308
|
+
if (encoder.bundleStrings && typeof value !== 'string') {
|
|
1309
|
+
bundledStrings = [];
|
|
1310
|
+
bundledStrings.size = Infinity; // force a new bundle start on first string
|
|
1311
|
+
} else
|
|
1312
|
+
bundledStrings = null;
|
|
1313
|
+
|
|
1314
|
+
sharedStructures = encoder.structures;
|
|
1315
|
+
if (sharedStructures) {
|
|
1316
|
+
if (sharedStructures.uninitialized) {
|
|
1317
|
+
let sharedData = encoder.getShared() || {};
|
|
1318
|
+
encoder.structures = sharedStructures = sharedData.structures || [];
|
|
1319
|
+
encoder.sharedVersion = sharedData.version;
|
|
1320
|
+
let sharedValues = encoder.sharedValues = sharedData.packedValues;
|
|
1321
|
+
if (sharedValues) {
|
|
1322
|
+
sharedPackedObjectMap = {};
|
|
1323
|
+
for (let i = 0, l = sharedValues.length; i < l; i++)
|
|
1324
|
+
sharedPackedObjectMap[sharedValues[i]] = i;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
let sharedStructuresLength = sharedStructures.length;
|
|
1328
|
+
if (sharedStructuresLength > maxSharedStructures && !isSequential)
|
|
1329
|
+
sharedStructuresLength = maxSharedStructures;
|
|
1330
|
+
if (!sharedStructures.transitions) {
|
|
1331
|
+
// rebuild our structure transitions
|
|
1332
|
+
sharedStructures.transitions = Object.create(null);
|
|
1333
|
+
for (let i = 0; i < sharedStructuresLength; i++) {
|
|
1334
|
+
let keys = sharedStructures[i];
|
|
1335
|
+
//console.log('shared struct keys:', keys)
|
|
1336
|
+
if (!keys)
|
|
1337
|
+
continue
|
|
1338
|
+
let nextTransition, transition = sharedStructures.transitions;
|
|
1339
|
+
for (let j = 0, l = keys.length; j < l; j++) {
|
|
1340
|
+
if (transition[RECORD_SYMBOL] === undefined)
|
|
1341
|
+
transition[RECORD_SYMBOL] = i;
|
|
1342
|
+
let key = keys[j];
|
|
1343
|
+
nextTransition = transition[key];
|
|
1344
|
+
if (!nextTransition) {
|
|
1345
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1346
|
+
}
|
|
1347
|
+
transition = nextTransition;
|
|
1348
|
+
}
|
|
1349
|
+
transition[RECORD_SYMBOL] = i | 0x100000;
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
if (!isSequential)
|
|
1353
|
+
sharedStructures.nextId = sharedStructuresLength;
|
|
1354
|
+
}
|
|
1355
|
+
if (hasSharedUpdate)
|
|
1356
|
+
hasSharedUpdate = false;
|
|
1357
|
+
structures = sharedStructures || [];
|
|
1358
|
+
packedObjectMap = sharedPackedObjectMap;
|
|
1359
|
+
if (options.pack) {
|
|
1360
|
+
let packedValues = new Map();
|
|
1361
|
+
packedValues.values = [];
|
|
1362
|
+
packedValues.encoder = encoder;
|
|
1363
|
+
packedValues.maxValues = options.maxPrivatePackedValues || (sharedPackedObjectMap ? 16 : Infinity);
|
|
1364
|
+
packedValues.objectMap = sharedPackedObjectMap || false;
|
|
1365
|
+
packedValues.samplingPackedValues = samplingPackedValues;
|
|
1366
|
+
findRepetitiveStrings(value, packedValues);
|
|
1367
|
+
if (packedValues.values.length > 0) {
|
|
1368
|
+
target[position++] = 0xd8; // one-byte tag
|
|
1369
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1370
|
+
writeArrayHeader(4);
|
|
1371
|
+
let valuesArray = packedValues.values;
|
|
1372
|
+
encode(valuesArray);
|
|
1373
|
+
writeArrayHeader(0); // prefixes
|
|
1374
|
+
writeArrayHeader(0); // suffixes
|
|
1375
|
+
packedObjectMap = Object.create(sharedPackedObjectMap || null);
|
|
1376
|
+
for (let i = 0, l = valuesArray.length; i < l; i++) {
|
|
1377
|
+
packedObjectMap[valuesArray[i]] = i;
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
throwOnIterable = encodeOptions & THROW_ON_ITERABLE;
|
|
1382
|
+
try {
|
|
1383
|
+
if (throwOnIterable)
|
|
1384
|
+
return;
|
|
1385
|
+
encode(value);
|
|
1386
|
+
if (bundledStrings) {
|
|
1387
|
+
writeBundles(start, encode);
|
|
1388
|
+
}
|
|
1389
|
+
encoder.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
|
|
1390
|
+
if (referenceMap && referenceMap.idsToInsert) {
|
|
1391
|
+
position += referenceMap.idsToInsert.length * 2;
|
|
1392
|
+
if (position > safeEnd)
|
|
1393
|
+
makeRoom(position);
|
|
1394
|
+
encoder.offset = position;
|
|
1395
|
+
let serialized = insertIds(target.subarray(start, position), referenceMap.idsToInsert);
|
|
1396
|
+
referenceMap = null;
|
|
1397
|
+
return serialized
|
|
1398
|
+
}
|
|
1399
|
+
if (encodeOptions & REUSE_BUFFER_MODE) {
|
|
1400
|
+
target.start = start;
|
|
1401
|
+
target.end = position;
|
|
1402
|
+
return target
|
|
1403
|
+
}
|
|
1404
|
+
return target.subarray(start, position) // position can change if we call encode again in saveShared, so we get the buffer now
|
|
1405
|
+
} finally {
|
|
1406
|
+
if (sharedStructures) {
|
|
1407
|
+
if (serializationsSinceTransitionRebuild < 10)
|
|
1408
|
+
serializationsSinceTransitionRebuild++;
|
|
1409
|
+
if (sharedStructures.length > maxSharedStructures)
|
|
1410
|
+
sharedStructures.length = maxSharedStructures;
|
|
1411
|
+
if (transitionsCount > 10000) {
|
|
1412
|
+
// force a rebuild occasionally after a lot of transitions so it can get cleaned up
|
|
1413
|
+
sharedStructures.transitions = null;
|
|
1414
|
+
serializationsSinceTransitionRebuild = 0;
|
|
1415
|
+
transitionsCount = 0;
|
|
1416
|
+
if (recordIdsToRemove.length > 0)
|
|
1417
|
+
recordIdsToRemove = [];
|
|
1418
|
+
} else if (recordIdsToRemove.length > 0 && !isSequential) {
|
|
1419
|
+
for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
|
|
1420
|
+
recordIdsToRemove[i][RECORD_SYMBOL] = undefined;
|
|
1421
|
+
}
|
|
1422
|
+
recordIdsToRemove = [];
|
|
1423
|
+
//sharedStructures.nextId = maxSharedStructures
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
if (hasSharedUpdate && encoder.saveShared) {
|
|
1427
|
+
if (encoder.structures.length > maxSharedStructures) {
|
|
1428
|
+
encoder.structures = encoder.structures.slice(0, maxSharedStructures);
|
|
1429
|
+
}
|
|
1430
|
+
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
|
|
1431
|
+
let returnBuffer = target.subarray(start, position);
|
|
1432
|
+
if (encoder.updateSharedData() === false)
|
|
1433
|
+
return encoder.encode(value) // re-encode if it fails
|
|
1434
|
+
return returnBuffer
|
|
1435
|
+
}
|
|
1436
|
+
if (encodeOptions & RESET_BUFFER_MODE)
|
|
1437
|
+
position = start;
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
1440
|
+
this.findCommonStringsToPack = () => {
|
|
1441
|
+
samplingPackedValues = new Map();
|
|
1442
|
+
if (!sharedPackedObjectMap)
|
|
1443
|
+
sharedPackedObjectMap = Object.create(null);
|
|
1444
|
+
return (options) => {
|
|
1445
|
+
let threshold = options && options.threshold || 4;
|
|
1446
|
+
let position = this.pack ? options.maxPrivatePackedValues || 16 : 0;
|
|
1447
|
+
if (!sharedValues)
|
|
1448
|
+
sharedValues = this.sharedValues = [];
|
|
1449
|
+
for (let [ key, status ] of samplingPackedValues) {
|
|
1450
|
+
if (status.count > threshold) {
|
|
1451
|
+
sharedPackedObjectMap[key] = position++;
|
|
1452
|
+
sharedValues.push(key);
|
|
1453
|
+
hasSharedUpdate = true;
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
while (this.saveShared && this.updateSharedData() === false) {}
|
|
1457
|
+
samplingPackedValues = null;
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
const encode = (value) => {
|
|
1461
|
+
if (position > safeEnd)
|
|
1462
|
+
target = makeRoom(position);
|
|
1463
|
+
|
|
1464
|
+
var type = typeof value;
|
|
1465
|
+
var length;
|
|
1466
|
+
if (type === 'string') {
|
|
1467
|
+
if (packedObjectMap) {
|
|
1468
|
+
let packedPosition = packedObjectMap[value];
|
|
1469
|
+
if (packedPosition >= 0) {
|
|
1470
|
+
if (packedPosition < 16)
|
|
1471
|
+
target[position++] = packedPosition + 0xe0; // simple values, defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1472
|
+
else {
|
|
1473
|
+
target[position++] = 0xc6; // tag 6 defined in https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
1474
|
+
if (packedPosition & 1)
|
|
1475
|
+
encode((15 - packedPosition) >> 1);
|
|
1476
|
+
else
|
|
1477
|
+
encode((packedPosition - 16) >> 1);
|
|
1478
|
+
}
|
|
1479
|
+
return
|
|
1480
|
+
/* } else if (packedStatus.serializationId != serializationId) {
|
|
1481
|
+
packedStatus.serializationId = serializationId
|
|
1482
|
+
packedStatus.count = 1
|
|
1483
|
+
if (options.sharedPack) {
|
|
1484
|
+
let sharedCount = packedStatus.sharedCount = (packedStatus.sharedCount || 0) + 1
|
|
1485
|
+
if (shareCount > (options.sharedPack.threshold || 5)) {
|
|
1486
|
+
let sharedPosition = packedStatus.position = packedStatus.nextSharedPosition
|
|
1487
|
+
hasSharedUpdate = true
|
|
1488
|
+
if (sharedPosition < 16)
|
|
1489
|
+
target[position++] = sharedPosition + 0xc0
|
|
1490
|
+
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
} // else any in-doc incrementation?*/
|
|
1494
|
+
} else if (samplingPackedValues && !options.pack) {
|
|
1495
|
+
let status = samplingPackedValues.get(value);
|
|
1496
|
+
if (status)
|
|
1497
|
+
status.count++;
|
|
1498
|
+
else
|
|
1499
|
+
samplingPackedValues.set(value, {
|
|
1500
|
+
count: 1,
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
let strLength = value.length;
|
|
1505
|
+
if (bundledStrings && strLength >= 4 && strLength < 0x400) {
|
|
1506
|
+
if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
|
|
1507
|
+
let extStart;
|
|
1508
|
+
let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
|
|
1509
|
+
if (position + maxBytes > safeEnd)
|
|
1510
|
+
target = makeRoom(position + maxBytes);
|
|
1511
|
+
target[position++] = 0xd9; // tag 16-bit
|
|
1512
|
+
target[position++] = 0xdf; // tag 0xdff9
|
|
1513
|
+
target[position++] = 0xf9;
|
|
1514
|
+
// TODO: If we only have one bundle with any string data, only write one string bundle
|
|
1515
|
+
target[position++] = bundledStrings.position ? 0x84 : 0x82; // array of 4 or 2 elements depending on if we write bundles
|
|
1516
|
+
target[position++] = 0x1a; // 32-bit unsigned int
|
|
1517
|
+
extStart = position - start;
|
|
1518
|
+
position += 4; // reserve for writing bundle reference
|
|
1519
|
+
if (bundledStrings.position) {
|
|
1520
|
+
writeBundles(start, encode); // write the last bundles
|
|
1521
|
+
}
|
|
1522
|
+
bundledStrings = ['', '']; // create new ones
|
|
1523
|
+
bundledStrings.size = 0;
|
|
1524
|
+
bundledStrings.position = extStart;
|
|
1525
|
+
}
|
|
1526
|
+
let twoByte = hasNonLatin.test(value);
|
|
1527
|
+
bundledStrings[twoByte ? 0 : 1] += value;
|
|
1528
|
+
target[position++] = twoByte ? 0xce : 0xcf;
|
|
1529
|
+
encode(strLength);
|
|
1530
|
+
return
|
|
1531
|
+
}
|
|
1532
|
+
let headerSize;
|
|
1533
|
+
// first we estimate the header size, so we can write to the correct location
|
|
1534
|
+
if (strLength < 0x20) {
|
|
1535
|
+
headerSize = 1;
|
|
1536
|
+
} else if (strLength < 0x100) {
|
|
1537
|
+
headerSize = 2;
|
|
1538
|
+
} else if (strLength < 0x10000) {
|
|
1539
|
+
headerSize = 3;
|
|
1540
|
+
} else {
|
|
1541
|
+
headerSize = 5;
|
|
1542
|
+
}
|
|
1543
|
+
let maxBytes = strLength * 3;
|
|
1544
|
+
if (position + maxBytes > safeEnd)
|
|
1545
|
+
target = makeRoom(position + maxBytes);
|
|
1546
|
+
|
|
1547
|
+
if (strLength < 0x40 || !encodeUtf8) {
|
|
1548
|
+
let i, c1, c2, strPosition = position + headerSize;
|
|
1549
|
+
for (i = 0; i < strLength; i++) {
|
|
1550
|
+
c1 = value.charCodeAt(i);
|
|
1551
|
+
if (c1 < 0x80) {
|
|
1552
|
+
target[strPosition++] = c1;
|
|
1553
|
+
} else if (c1 < 0x800) {
|
|
1554
|
+
target[strPosition++] = c1 >> 6 | 0xc0;
|
|
1555
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1556
|
+
} else if (
|
|
1557
|
+
(c1 & 0xfc00) === 0xd800 &&
|
|
1558
|
+
((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
|
|
1559
|
+
) {
|
|
1560
|
+
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
1561
|
+
i++;
|
|
1562
|
+
target[strPosition++] = c1 >> 18 | 0xf0;
|
|
1563
|
+
target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
|
|
1564
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1565
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1566
|
+
} else {
|
|
1567
|
+
target[strPosition++] = c1 >> 12 | 0xe0;
|
|
1568
|
+
target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
|
|
1569
|
+
target[strPosition++] = c1 & 0x3f | 0x80;
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
length = strPosition - position - headerSize;
|
|
1573
|
+
} else {
|
|
1574
|
+
length = encodeUtf8(value, position + headerSize, maxBytes);
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
if (length < 0x18) {
|
|
1578
|
+
target[position++] = 0x60 | length;
|
|
1579
|
+
} else if (length < 0x100) {
|
|
1580
|
+
if (headerSize < 2) {
|
|
1581
|
+
target.copyWithin(position + 2, position + 1, position + 1 + length);
|
|
1582
|
+
}
|
|
1583
|
+
target[position++] = 0x78;
|
|
1584
|
+
target[position++] = length;
|
|
1585
|
+
} else if (length < 0x10000) {
|
|
1586
|
+
if (headerSize < 3) {
|
|
1587
|
+
target.copyWithin(position + 3, position + 2, position + 2 + length);
|
|
1588
|
+
}
|
|
1589
|
+
target[position++] = 0x79;
|
|
1590
|
+
target[position++] = length >> 8;
|
|
1591
|
+
target[position++] = length & 0xff;
|
|
1592
|
+
} else {
|
|
1593
|
+
if (headerSize < 5) {
|
|
1594
|
+
target.copyWithin(position + 5, position + 3, position + 3 + length);
|
|
1595
|
+
}
|
|
1596
|
+
target[position++] = 0x7a;
|
|
1597
|
+
targetView.setUint32(position, length);
|
|
1598
|
+
position += 4;
|
|
1599
|
+
}
|
|
1600
|
+
position += length;
|
|
1601
|
+
} else if (type === 'number') {
|
|
1602
|
+
if (!this.alwaysUseFloat && value >>> 0 === value) {// positive integer, 32-bit or less
|
|
1603
|
+
// positive uint
|
|
1604
|
+
if (value < 0x18) {
|
|
1605
|
+
target[position++] = value;
|
|
1606
|
+
} else if (value < 0x100) {
|
|
1607
|
+
target[position++] = 0x18;
|
|
1608
|
+
target[position++] = value;
|
|
1609
|
+
} else if (value < 0x10000) {
|
|
1610
|
+
target[position++] = 0x19;
|
|
1611
|
+
target[position++] = value >> 8;
|
|
1612
|
+
target[position++] = value & 0xff;
|
|
1613
|
+
} else {
|
|
1614
|
+
target[position++] = 0x1a;
|
|
1615
|
+
targetView.setUint32(position, value);
|
|
1616
|
+
position += 4;
|
|
1617
|
+
}
|
|
1618
|
+
} else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
|
|
1619
|
+
if (value >= -0x18) {
|
|
1620
|
+
target[position++] = 0x1f - value;
|
|
1621
|
+
} else if (value >= -0x100) {
|
|
1622
|
+
target[position++] = 0x38;
|
|
1623
|
+
target[position++] = ~value;
|
|
1624
|
+
} else if (value >= -0x10000) {
|
|
1625
|
+
target[position++] = 0x39;
|
|
1626
|
+
targetView.setUint16(position, ~value);
|
|
1627
|
+
position += 2;
|
|
1628
|
+
} else {
|
|
1629
|
+
target[position++] = 0x3a;
|
|
1630
|
+
targetView.setUint32(position, ~value);
|
|
1631
|
+
position += 4;
|
|
1632
|
+
}
|
|
1633
|
+
} else {
|
|
1634
|
+
let useFloat32;
|
|
1635
|
+
if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
|
|
1636
|
+
target[position++] = 0xfa;
|
|
1637
|
+
targetView.setFloat32(position, value);
|
|
1638
|
+
let xShifted;
|
|
1639
|
+
if (useFloat32 < 4 ||
|
|
1640
|
+
// this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
|
|
1641
|
+
((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
|
|
1642
|
+
position += 4;
|
|
1643
|
+
return
|
|
1644
|
+
} else
|
|
1645
|
+
position--; // move back into position for writing a double
|
|
1646
|
+
}
|
|
1647
|
+
target[position++] = 0xfb;
|
|
1648
|
+
targetView.setFloat64(position, value);
|
|
1649
|
+
position += 8;
|
|
1650
|
+
}
|
|
1651
|
+
} else if (type === 'object') {
|
|
1652
|
+
if (!value)
|
|
1653
|
+
target[position++] = 0xf6;
|
|
1654
|
+
else {
|
|
1655
|
+
if (referenceMap) {
|
|
1656
|
+
let referee = referenceMap.get(value);
|
|
1657
|
+
if (referee) {
|
|
1658
|
+
target[position++] = 0xd8;
|
|
1659
|
+
target[position++] = 29; // http://cbor.schmorp.de/value-sharing
|
|
1660
|
+
target[position++] = 0x19; // 16-bit uint
|
|
1661
|
+
if (!referee.references) {
|
|
1662
|
+
let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
|
|
1663
|
+
referee.references = [];
|
|
1664
|
+
idsToInsert.push(referee);
|
|
1665
|
+
}
|
|
1666
|
+
referee.references.push(position - start);
|
|
1667
|
+
position += 2; // TODO: also support 32-bit
|
|
1668
|
+
return
|
|
1669
|
+
} else
|
|
1670
|
+
referenceMap.set(value, { offset: position - start });
|
|
1671
|
+
}
|
|
1672
|
+
let constructor = value.constructor;
|
|
1673
|
+
if (constructor === Object) {
|
|
1674
|
+
writeObject(value);
|
|
1675
|
+
} else if (constructor === Array) {
|
|
1676
|
+
length = value.length;
|
|
1677
|
+
if (length < 0x18) {
|
|
1678
|
+
target[position++] = 0x80 | length;
|
|
1679
|
+
} else {
|
|
1680
|
+
writeArrayHeader(length);
|
|
1681
|
+
}
|
|
1682
|
+
for (let i = 0; i < length; i++) {
|
|
1683
|
+
encode(value[i]);
|
|
1684
|
+
}
|
|
1685
|
+
} else if (constructor === Map) {
|
|
1686
|
+
if (this.mapsAsObjects ? this.useTag259ForMaps !== false : this.useTag259ForMaps) {
|
|
1687
|
+
// use Tag 259 (https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec--explicit-maps.md) for maps if the user wants it that way
|
|
1688
|
+
target[position++] = 0xd9;
|
|
1689
|
+
target[position++] = 1;
|
|
1690
|
+
target[position++] = 3;
|
|
1691
|
+
}
|
|
1692
|
+
length = value.size;
|
|
1693
|
+
if (length < 0x18) {
|
|
1694
|
+
target[position++] = 0xa0 | length;
|
|
1695
|
+
} else if (length < 0x100) {
|
|
1696
|
+
target[position++] = 0xb8;
|
|
1697
|
+
target[position++] = length;
|
|
1698
|
+
} else if (length < 0x10000) {
|
|
1699
|
+
target[position++] = 0xb9;
|
|
1700
|
+
target[position++] = length >> 8;
|
|
1701
|
+
target[position++] = length & 0xff;
|
|
1702
|
+
} else {
|
|
1703
|
+
target[position++] = 0xba;
|
|
1704
|
+
targetView.setUint32(position, length);
|
|
1705
|
+
position += 4;
|
|
1706
|
+
}
|
|
1707
|
+
if (encoder.keyMap) {
|
|
1708
|
+
for (let [ key, entryValue ] of value) {
|
|
1709
|
+
encode(encoder.encodeKey(key));
|
|
1710
|
+
encode(entryValue);
|
|
1711
|
+
}
|
|
1712
|
+
} else {
|
|
1713
|
+
for (let [ key, entryValue ] of value) {
|
|
1714
|
+
encode(key);
|
|
1715
|
+
encode(entryValue);
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
} else {
|
|
1719
|
+
for (let i = 0, l = extensions.length; i < l; i++) {
|
|
1720
|
+
let extensionClass = extensionClasses[i];
|
|
1721
|
+
if (value instanceof extensionClass) {
|
|
1722
|
+
let extension = extensions[i];
|
|
1723
|
+
let tag = extension.tag;
|
|
1724
|
+
if (tag == undefined)
|
|
1725
|
+
tag = extension.getTag && extension.getTag.call(this, value);
|
|
1726
|
+
if (tag < 0x18) {
|
|
1727
|
+
target[position++] = 0xc0 | tag;
|
|
1728
|
+
} else if (tag < 0x100) {
|
|
1729
|
+
target[position++] = 0xd8;
|
|
1730
|
+
target[position++] = tag;
|
|
1731
|
+
} else if (tag < 0x10000) {
|
|
1732
|
+
target[position++] = 0xd9;
|
|
1733
|
+
target[position++] = tag >> 8;
|
|
1734
|
+
target[position++] = tag & 0xff;
|
|
1735
|
+
} else if (tag > -1) {
|
|
1736
|
+
target[position++] = 0xda;
|
|
1737
|
+
targetView.setUint32(position, tag);
|
|
1738
|
+
position += 4;
|
|
1739
|
+
} // else undefined, don't write tag
|
|
1740
|
+
extension.encode.call(this, value, encode, makeRoom);
|
|
1741
|
+
return
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
if (value[Symbol.iterator]) {
|
|
1745
|
+
if (throwOnIterable) {
|
|
1746
|
+
let error = new Error('Iterable should be serialized as iterator');
|
|
1747
|
+
error.iteratorNotHandled = true;
|
|
1748
|
+
throw error;
|
|
1749
|
+
}
|
|
1750
|
+
target[position++] = 0x9f; // indefinite length array
|
|
1751
|
+
for (let entry of value) {
|
|
1752
|
+
encode(entry);
|
|
1753
|
+
}
|
|
1754
|
+
target[position++] = 0xff; // stop-code
|
|
1755
|
+
return
|
|
1756
|
+
}
|
|
1757
|
+
if (value[Symbol.asyncIterator] || isBlob(value)) {
|
|
1758
|
+
let error = new Error('Iterable/blob should be serialized as iterator');
|
|
1759
|
+
error.iteratorNotHandled = true;
|
|
1760
|
+
throw error;
|
|
1761
|
+
}
|
|
1762
|
+
if (this.useToJSON && value.toJSON) {
|
|
1763
|
+
const json = value.toJSON();
|
|
1764
|
+
// if for some reason value.toJSON returns itself it'll loop forever
|
|
1765
|
+
if (json !== value)
|
|
1766
|
+
return encode(json)
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
// no extension found, write as a plain object
|
|
1770
|
+
writeObject(value);
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
} else if (type === 'boolean') {
|
|
1774
|
+
target[position++] = value ? 0xf5 : 0xf4;
|
|
1775
|
+
} else if (type === 'bigint') {
|
|
1776
|
+
if (value < (BigInt(1)<<BigInt(64)) && value >= 0) {
|
|
1777
|
+
// use an unsigned int as long as it fits
|
|
1778
|
+
target[position++] = 0x1b;
|
|
1779
|
+
targetView.setBigUint64(position, value);
|
|
1780
|
+
} else if (value > -(BigInt(1)<<BigInt(64)) && value < 0) {
|
|
1781
|
+
// if we can fit an unsigned int, use that
|
|
1782
|
+
target[position++] = 0x3b;
|
|
1783
|
+
targetView.setBigUint64(position, -value - BigInt(1));
|
|
1784
|
+
} else {
|
|
1785
|
+
// overflow
|
|
1786
|
+
if (this.largeBigIntToFloat) {
|
|
1787
|
+
target[position++] = 0xfb;
|
|
1788
|
+
targetView.setFloat64(position, Number(value));
|
|
1789
|
+
} else {
|
|
1790
|
+
if (value >= BigInt(0))
|
|
1791
|
+
target[position++] = 0xc2; // tag 2
|
|
1792
|
+
else {
|
|
1793
|
+
target[position++] = 0xc3; // tag 2
|
|
1794
|
+
value = BigInt(-1) - value;
|
|
1795
|
+
}
|
|
1796
|
+
let bytes = [];
|
|
1797
|
+
while (value) {
|
|
1798
|
+
bytes.push(Number(value & BigInt(0xff)));
|
|
1799
|
+
value >>= BigInt(8);
|
|
1800
|
+
}
|
|
1801
|
+
writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
|
|
1802
|
+
return;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
position += 8;
|
|
1806
|
+
} else if (type === 'undefined') {
|
|
1807
|
+
target[position++] = 0xf7;
|
|
1808
|
+
} else {
|
|
1809
|
+
throw new Error('Unknown type: ' + type)
|
|
1810
|
+
}
|
|
1811
|
+
};
|
|
1812
|
+
|
|
1813
|
+
const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
|
|
1814
|
+
// this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
|
|
1815
|
+
let keys = Object.keys(object);
|
|
1816
|
+
let vals = Object.values(object);
|
|
1817
|
+
let length = keys.length;
|
|
1818
|
+
if (length < 0x18) {
|
|
1819
|
+
target[position++] = 0xa0 | length;
|
|
1820
|
+
} else if (length < 0x100) {
|
|
1821
|
+
target[position++] = 0xb8;
|
|
1822
|
+
target[position++] = length;
|
|
1823
|
+
} else if (length < 0x10000) {
|
|
1824
|
+
target[position++] = 0xb9;
|
|
1825
|
+
target[position++] = length >> 8;
|
|
1826
|
+
target[position++] = length & 0xff;
|
|
1827
|
+
} else {
|
|
1828
|
+
target[position++] = 0xba;
|
|
1829
|
+
targetView.setUint32(position, length);
|
|
1830
|
+
position += 4;
|
|
1831
|
+
}
|
|
1832
|
+
if (encoder.keyMap) {
|
|
1833
|
+
for (let i = 0; i < length; i++) {
|
|
1834
|
+
encode(encoder.encodeKey(keys[i]));
|
|
1835
|
+
encode(vals[i]);
|
|
1836
|
+
}
|
|
1837
|
+
} else {
|
|
1838
|
+
for (let i = 0; i < length; i++) {
|
|
1839
|
+
encode(keys[i]);
|
|
1840
|
+
encode(vals[i]);
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
} :
|
|
1844
|
+
(object) => {
|
|
1845
|
+
target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
|
|
1846
|
+
let objectOffset = position - start;
|
|
1847
|
+
position += 2;
|
|
1848
|
+
let size = 0;
|
|
1849
|
+
if (encoder.keyMap) {
|
|
1850
|
+
for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
1851
|
+
encode(encoder.encodeKey(key));
|
|
1852
|
+
encode(object[key]);
|
|
1853
|
+
size++;
|
|
1854
|
+
}
|
|
1855
|
+
} else {
|
|
1856
|
+
for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
1857
|
+
encode(key);
|
|
1858
|
+
encode(object[key]);
|
|
1859
|
+
size++;
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
target[objectOffset++ + start] = size >> 8;
|
|
1863
|
+
target[objectOffset + start] = size & 0xff;
|
|
1864
|
+
} :
|
|
1865
|
+
(object, skipValues) => {
|
|
1866
|
+
let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
|
|
1867
|
+
let newTransitions = 0;
|
|
1868
|
+
let length = 0;
|
|
1869
|
+
let parentRecordId;
|
|
1870
|
+
let keys;
|
|
1871
|
+
if (this.keyMap) {
|
|
1872
|
+
keys = Object.keys(object).map(k => this.encodeKey(k));
|
|
1873
|
+
length = keys.length;
|
|
1874
|
+
for (let i = 0; i < length; i++) {
|
|
1875
|
+
let key = keys[i];
|
|
1876
|
+
nextTransition = transition[key];
|
|
1877
|
+
if (!nextTransition) {
|
|
1878
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1879
|
+
newTransitions++;
|
|
1880
|
+
}
|
|
1881
|
+
transition = nextTransition;
|
|
1882
|
+
}
|
|
1883
|
+
} else {
|
|
1884
|
+
for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
|
|
1885
|
+
nextTransition = transition[key];
|
|
1886
|
+
if (!nextTransition) {
|
|
1887
|
+
if (transition[RECORD_SYMBOL] & 0x100000) {// this indicates it is a brancheable/extendable terminal node, so we will use this record id and extend it
|
|
1888
|
+
parentRecordId = transition[RECORD_SYMBOL] & 0xffff;
|
|
1889
|
+
}
|
|
1890
|
+
nextTransition = transition[key] = Object.create(null);
|
|
1891
|
+
newTransitions++;
|
|
1892
|
+
}
|
|
1893
|
+
transition = nextTransition;
|
|
1894
|
+
length++;
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
let recordId = transition[RECORD_SYMBOL];
|
|
1898
|
+
if (recordId !== undefined) {
|
|
1899
|
+
recordId &= 0xffff;
|
|
1900
|
+
target[position++] = 0xd9;
|
|
1901
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1902
|
+
target[position++] = recordId & 0xff;
|
|
1903
|
+
} else {
|
|
1904
|
+
if (!keys)
|
|
1905
|
+
keys = transition.__keys__ || (transition.__keys__ = Object.keys(object));
|
|
1906
|
+
if (parentRecordId === undefined) {
|
|
1907
|
+
recordId = structures.nextId++;
|
|
1908
|
+
if (!recordId) {
|
|
1909
|
+
recordId = 0;
|
|
1910
|
+
structures.nextId = 1;
|
|
1911
|
+
}
|
|
1912
|
+
if (recordId >= MAX_STRUCTURES) {// cycle back around
|
|
1913
|
+
structures.nextId = (recordId = maxSharedStructures) + 1;
|
|
1914
|
+
}
|
|
1915
|
+
} else {
|
|
1916
|
+
recordId = parentRecordId;
|
|
1917
|
+
}
|
|
1918
|
+
structures[recordId] = keys;
|
|
1919
|
+
if (recordId < maxSharedStructures) {
|
|
1920
|
+
target[position++] = 0xd9;
|
|
1921
|
+
target[position++] = (recordId >> 8) | 0xe0;
|
|
1922
|
+
target[position++] = recordId & 0xff;
|
|
1923
|
+
transition = structures.transitions;
|
|
1924
|
+
for (let i = 0; i < length; i++) {
|
|
1925
|
+
if (transition[RECORD_SYMBOL] === undefined || (transition[RECORD_SYMBOL] & 0x100000))
|
|
1926
|
+
transition[RECORD_SYMBOL] = recordId;
|
|
1927
|
+
transition = transition[keys[i]];
|
|
1928
|
+
}
|
|
1929
|
+
transition[RECORD_SYMBOL] = recordId | 0x100000; // indicates it is a extendable terminal
|
|
1930
|
+
hasSharedUpdate = true;
|
|
1931
|
+
} else {
|
|
1932
|
+
transition[RECORD_SYMBOL] = recordId;
|
|
1933
|
+
targetView.setUint32(position, 0xd9dfff00); // tag two byte, then record definition id
|
|
1934
|
+
position += 3;
|
|
1935
|
+
if (newTransitions)
|
|
1936
|
+
transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
|
|
1937
|
+
// record the removal of the id, we can maintain our shared structure
|
|
1938
|
+
if (recordIdsToRemove.length >= MAX_STRUCTURES - maxSharedStructures)
|
|
1939
|
+
recordIdsToRemove.shift()[RECORD_SYMBOL] = undefined; // we are cycling back through, and have to remove old ones
|
|
1940
|
+
recordIdsToRemove.push(transition);
|
|
1941
|
+
writeArrayHeader(length + 2);
|
|
1942
|
+
encode(0xe000 + recordId);
|
|
1943
|
+
encode(keys);
|
|
1944
|
+
if (skipValues) return; // special exit for iterator
|
|
1945
|
+
for (let key in object)
|
|
1946
|
+
if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key))
|
|
1947
|
+
encode(object[key]);
|
|
1948
|
+
return
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
if (length < 0x18) { // write the array header
|
|
1952
|
+
target[position++] = 0x80 | length;
|
|
1953
|
+
} else {
|
|
1954
|
+
writeArrayHeader(length);
|
|
1955
|
+
}
|
|
1956
|
+
if (skipValues) return; // special exit for iterator
|
|
1957
|
+
for (let key in object)
|
|
1958
|
+
if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key))
|
|
1959
|
+
encode(object[key]);
|
|
1960
|
+
};
|
|
1961
|
+
const makeRoom = (end) => {
|
|
1962
|
+
let newSize;
|
|
1963
|
+
if (end > 0x1000000) {
|
|
1964
|
+
// special handling for really large buffers
|
|
1965
|
+
if ((end - start) > MAX_BUFFER_SIZE)
|
|
1966
|
+
throw new Error('Encoded buffer would be larger than maximum buffer size')
|
|
1967
|
+
newSize = Math.min(MAX_BUFFER_SIZE,
|
|
1968
|
+
Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
|
|
1969
|
+
} else // faster handling for smaller buffers
|
|
1970
|
+
newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
|
|
1971
|
+
let newBuffer = new ByteArrayAllocate(newSize);
|
|
1972
|
+
targetView = new DataView(newBuffer.buffer, 0, newSize);
|
|
1973
|
+
if (target.copy)
|
|
1974
|
+
target.copy(newBuffer, 0, start, end);
|
|
1975
|
+
else
|
|
1976
|
+
newBuffer.set(target.slice(start, end));
|
|
1977
|
+
position -= start;
|
|
1978
|
+
start = 0;
|
|
1979
|
+
safeEnd = newBuffer.length - 10;
|
|
1980
|
+
return target = newBuffer
|
|
1981
|
+
};
|
|
1982
|
+
let chunkThreshold = 100;
|
|
1983
|
+
let continuedChunkThreshold = 1000;
|
|
1984
|
+
this.encodeAsIterable = function(value, options) {
|
|
1985
|
+
return startEncoding(value, options, encodeObjectAsIterable);
|
|
1986
|
+
};
|
|
1987
|
+
this.encodeAsAsyncIterable = function(value, options) {
|
|
1988
|
+
return startEncoding(value, options, encodeObjectAsAsyncIterable);
|
|
1989
|
+
};
|
|
1990
|
+
|
|
1991
|
+
function* encodeObjectAsIterable(object, iterateProperties, finalIterable) {
|
|
1992
|
+
let constructor = object.constructor;
|
|
1993
|
+
if (constructor === Object) {
|
|
1994
|
+
let useRecords = encoder.useRecords !== false;
|
|
1995
|
+
if (useRecords)
|
|
1996
|
+
writeObject(object, true); // write the record identifier
|
|
1997
|
+
else
|
|
1998
|
+
writeEntityLength(Object.keys(object).length, 0xa0);
|
|
1999
|
+
for (let key in object) {
|
|
2000
|
+
let value = object[key];
|
|
2001
|
+
if (!useRecords) encode(key);
|
|
2002
|
+
if (value && typeof value === 'object') {
|
|
2003
|
+
if (iterateProperties[key])
|
|
2004
|
+
yield* encodeObjectAsIterable(value, iterateProperties[key]);
|
|
2005
|
+
else
|
|
2006
|
+
yield* tryEncode(value, iterateProperties, key);
|
|
2007
|
+
} else encode(value);
|
|
2008
|
+
}
|
|
2009
|
+
} else if (constructor === Array) {
|
|
2010
|
+
let length = object.length;
|
|
2011
|
+
writeArrayHeader(length);
|
|
2012
|
+
for (let i = 0; i < length; i++) {
|
|
2013
|
+
let value = object[i];
|
|
2014
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
2015
|
+
if (iterateProperties.element)
|
|
2016
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
2017
|
+
else
|
|
2018
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
2019
|
+
} else encode(value);
|
|
2020
|
+
}
|
|
2021
|
+
} else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
|
|
2022
|
+
target[position++] = 0x9f; // start indefinite array
|
|
2023
|
+
for (let value of object) {
|
|
2024
|
+
if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
|
|
2025
|
+
if (iterateProperties.element)
|
|
2026
|
+
yield* encodeObjectAsIterable(value, iterateProperties.element);
|
|
2027
|
+
else
|
|
2028
|
+
yield* tryEncode(value, iterateProperties, 'element');
|
|
2029
|
+
} else encode(value);
|
|
2030
|
+
}
|
|
2031
|
+
target[position++] = 0xff; // stop byte
|
|
2032
|
+
} else if (isBlob(object)){
|
|
2033
|
+
writeEntityLength(object.size, 0x40); // encode as binary data
|
|
2034
|
+
yield target.subarray(start, position);
|
|
2035
|
+
yield object; // directly return blobs, they have to be encoded asynchronously
|
|
2036
|
+
restartEncoding();
|
|
2037
|
+
} else if (object[Symbol.asyncIterator]) {
|
|
2038
|
+
target[position++] = 0x9f; // start indefinite array
|
|
2039
|
+
yield target.subarray(start, position);
|
|
2040
|
+
yield object; // directly return async iterators, they have to be encoded asynchronously
|
|
2041
|
+
restartEncoding();
|
|
2042
|
+
target[position++] = 0xff; // stop byte
|
|
2043
|
+
} else {
|
|
2044
|
+
encode(object);
|
|
2045
|
+
}
|
|
2046
|
+
if (finalIterable && position > start) yield target.subarray(start, position);
|
|
2047
|
+
else if (position - start > chunkThreshold) {
|
|
2048
|
+
yield target.subarray(start, position);
|
|
2049
|
+
restartEncoding();
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
function* tryEncode(value, iterateProperties, key) {
|
|
2053
|
+
let restart = position - start;
|
|
2054
|
+
try {
|
|
2055
|
+
encode(value);
|
|
2056
|
+
if (position - start > chunkThreshold) {
|
|
2057
|
+
yield target.subarray(start, position);
|
|
2058
|
+
restartEncoding();
|
|
2059
|
+
}
|
|
2060
|
+
} catch (error) {
|
|
2061
|
+
if (error.iteratorNotHandled) {
|
|
2062
|
+
iterateProperties[key] = {};
|
|
2063
|
+
position = start + restart; // restart our position so we don't have partial data from last encode
|
|
2064
|
+
yield* encodeObjectAsIterable.call(this, value, iterateProperties[key]);
|
|
2065
|
+
} else throw error;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
function restartEncoding() {
|
|
2069
|
+
chunkThreshold = continuedChunkThreshold;
|
|
2070
|
+
encoder.encode(null, THROW_ON_ITERABLE); // restart encoding
|
|
2071
|
+
}
|
|
2072
|
+
function startEncoding(value, options, encodeIterable) {
|
|
2073
|
+
if (options && options.chunkThreshold) // explicitly specified chunk sizes
|
|
2074
|
+
chunkThreshold = continuedChunkThreshold = options.chunkThreshold;
|
|
2075
|
+
else // we start with a smaller threshold to get initial bytes sent quickly
|
|
2076
|
+
chunkThreshold = 100;
|
|
2077
|
+
if (value && typeof value === 'object') {
|
|
2078
|
+
encoder.encode(null, THROW_ON_ITERABLE); // start encoding
|
|
2079
|
+
return encodeIterable(value, encoder.iterateProperties || (encoder.iterateProperties = {}), true);
|
|
2080
|
+
}
|
|
2081
|
+
return [encoder.encode(value)];
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
async function* encodeObjectAsAsyncIterable(value, iterateProperties) {
|
|
2085
|
+
for (let encodedValue of encodeObjectAsIterable(value, iterateProperties, true)) {
|
|
2086
|
+
let constructor = encodedValue.constructor;
|
|
2087
|
+
if (constructor === ByteArray || constructor === Uint8Array)
|
|
2088
|
+
yield encodedValue;
|
|
2089
|
+
else if (isBlob(encodedValue)) {
|
|
2090
|
+
let reader = encodedValue.stream().getReader();
|
|
2091
|
+
let next;
|
|
2092
|
+
while (!(next = await reader.read()).done) {
|
|
2093
|
+
yield next.value;
|
|
2094
|
+
}
|
|
2095
|
+
} else if (encodedValue[Symbol.asyncIterator]) {
|
|
2096
|
+
for await (let asyncValue of encodedValue) {
|
|
2097
|
+
restartEncoding();
|
|
2098
|
+
if (asyncValue)
|
|
2099
|
+
yield* encodeObjectAsAsyncIterable(asyncValue, iterateProperties.async || (iterateProperties.async = {}));
|
|
2100
|
+
else yield encoder.encode(asyncValue);
|
|
2101
|
+
}
|
|
2102
|
+
} else {
|
|
2103
|
+
yield encodedValue;
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
useBuffer(buffer) {
|
|
2109
|
+
// this means we are finished using our own buffer and we can write over it safely
|
|
2110
|
+
target = buffer;
|
|
2111
|
+
targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
|
|
2112
|
+
position = 0;
|
|
2113
|
+
}
|
|
2114
|
+
clearSharedData() {
|
|
2115
|
+
if (this.structures)
|
|
2116
|
+
this.structures = [];
|
|
2117
|
+
if (this.sharedValues)
|
|
2118
|
+
this.sharedValues = undefined;
|
|
2119
|
+
}
|
|
2120
|
+
updateSharedData() {
|
|
2121
|
+
let lastVersion = this.sharedVersion || 0;
|
|
2122
|
+
this.sharedVersion = lastVersion + 1;
|
|
2123
|
+
let structuresCopy = this.structures.slice(0);
|
|
2124
|
+
let sharedData = new SharedData(structuresCopy, this.sharedValues, this.sharedVersion);
|
|
2125
|
+
let saveResults = this.saveShared(sharedData,
|
|
2126
|
+
existingShared => (existingShared && existingShared.version || 0) == lastVersion);
|
|
2127
|
+
if (saveResults === false) {
|
|
2128
|
+
// get updated structures and try again if the update failed
|
|
2129
|
+
sharedData = this.getShared() || {};
|
|
2130
|
+
this.structures = sharedData.structures || [];
|
|
2131
|
+
this.sharedValues = sharedData.packedValues;
|
|
2132
|
+
this.sharedVersion = sharedData.version;
|
|
2133
|
+
this.structures.nextId = this.structures.length;
|
|
2134
|
+
} else {
|
|
2135
|
+
// restore structures
|
|
2136
|
+
structuresCopy.forEach((structure, i) => this.structures[i] = structure);
|
|
2137
|
+
}
|
|
2138
|
+
// saveShared may fail to write and reload, or may have reloaded to check compatibility and overwrite saved data, either way load the correct shared data
|
|
2139
|
+
return saveResults
|
|
2140
|
+
}
|
|
2141
|
+
};
|
|
2142
|
+
function writeEntityLength(length, majorValue) {
|
|
2143
|
+
if (length < 0x18)
|
|
2144
|
+
target[position++] = majorValue | length;
|
|
2145
|
+
else if (length < 0x100) {
|
|
2146
|
+
target[position++] = majorValue | 0x18;
|
|
2147
|
+
target[position++] = length;
|
|
2148
|
+
} else if (length < 0x10000) {
|
|
2149
|
+
target[position++] = majorValue | 0x19;
|
|
2150
|
+
target[position++] = length >> 8;
|
|
2151
|
+
target[position++] = length & 0xff;
|
|
2152
|
+
} else {
|
|
2153
|
+
target[position++] = majorValue | 0x1a;
|
|
2154
|
+
targetView.setUint32(position, length);
|
|
2155
|
+
position += 4;
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
}
|
|
2159
|
+
class SharedData {
|
|
2160
|
+
constructor(structures, values, version) {
|
|
2161
|
+
this.structures = structures;
|
|
2162
|
+
this.packedValues = values;
|
|
2163
|
+
this.version = version;
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2167
|
+
function writeArrayHeader(length) {
|
|
2168
|
+
if (length < 0x18)
|
|
2169
|
+
target[position++] = 0x80 | length;
|
|
2170
|
+
else if (length < 0x100) {
|
|
2171
|
+
target[position++] = 0x98;
|
|
2172
|
+
target[position++] = length;
|
|
2173
|
+
} else if (length < 0x10000) {
|
|
2174
|
+
target[position++] = 0x99;
|
|
2175
|
+
target[position++] = length >> 8;
|
|
2176
|
+
target[position++] = length & 0xff;
|
|
2177
|
+
} else {
|
|
2178
|
+
target[position++] = 0x9a;
|
|
2179
|
+
targetView.setUint32(position, length);
|
|
2180
|
+
position += 4;
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
const BlobConstructor = typeof Blob === 'undefined' ? function(){} : Blob;
|
|
2185
|
+
function isBlob(object) {
|
|
2186
|
+
if (object instanceof BlobConstructor)
|
|
2187
|
+
return true;
|
|
2188
|
+
let tag = object[Symbol.toStringTag];
|
|
2189
|
+
return tag === 'Blob' || tag === 'File';
|
|
2190
|
+
}
|
|
2191
|
+
function findRepetitiveStrings(value, packedValues) {
|
|
2192
|
+
switch(typeof value) {
|
|
2193
|
+
case 'string':
|
|
2194
|
+
if (value.length > 3) {
|
|
2195
|
+
if (packedValues.objectMap[value] > -1 || packedValues.values.length >= packedValues.maxValues)
|
|
2196
|
+
return
|
|
2197
|
+
let packedStatus = packedValues.get(value);
|
|
2198
|
+
if (packedStatus) {
|
|
2199
|
+
if (++packedStatus.count == 2) {
|
|
2200
|
+
packedValues.values.push(value);
|
|
2201
|
+
}
|
|
2202
|
+
} else {
|
|
2203
|
+
packedValues.set(value, {
|
|
2204
|
+
count: 1,
|
|
2205
|
+
});
|
|
2206
|
+
if (packedValues.samplingPackedValues) {
|
|
2207
|
+
let status = packedValues.samplingPackedValues.get(value);
|
|
2208
|
+
if (status)
|
|
2209
|
+
status.count++;
|
|
2210
|
+
else
|
|
2211
|
+
packedValues.samplingPackedValues.set(value, {
|
|
2212
|
+
count: 1,
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
break
|
|
2218
|
+
case 'object':
|
|
2219
|
+
if (value) {
|
|
2220
|
+
if (value instanceof Array) {
|
|
2221
|
+
for (let i = 0, l = value.length; i < l; i++) {
|
|
2222
|
+
findRepetitiveStrings(value[i], packedValues);
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
} else {
|
|
2226
|
+
let includeKeys = !packedValues.encoder.useRecords;
|
|
2227
|
+
for (var key in value) {
|
|
2228
|
+
if (value.hasOwnProperty(key)) {
|
|
2229
|
+
if (includeKeys)
|
|
2230
|
+
findRepetitiveStrings(key, packedValues);
|
|
2231
|
+
findRepetitiveStrings(value[key], packedValues);
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
break
|
|
2237
|
+
case 'function': console.log(value);
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
2240
|
+
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
|
|
2241
|
+
extensionClasses = [ Date, Set, Error, RegExp, Tag, ArrayBuffer,
|
|
2242
|
+
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
2243
|
+
typeof BigUint64Array == 'undefined' ? function() {} : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
2244
|
+
typeof BigInt64Array == 'undefined' ? function() {} : BigInt64Array,
|
|
2245
|
+
Float32Array, Float64Array, SharedData ];
|
|
2246
|
+
|
|
2247
|
+
//Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/
|
|
2248
|
+
extensions = [{ // Date
|
|
2249
|
+
tag: 1,
|
|
2250
|
+
encode(date, encode) {
|
|
2251
|
+
let seconds = date.getTime() / 1000;
|
|
2252
|
+
if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
|
|
2253
|
+
// Timestamp 32
|
|
2254
|
+
target[position++] = 0x1a;
|
|
2255
|
+
targetView.setUint32(position, seconds);
|
|
2256
|
+
position += 4;
|
|
2257
|
+
} else {
|
|
2258
|
+
// Timestamp float64
|
|
2259
|
+
target[position++] = 0xfb;
|
|
2260
|
+
targetView.setFloat64(position, seconds);
|
|
2261
|
+
position += 8;
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
}, { // Set
|
|
2265
|
+
tag: 258, // https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md
|
|
2266
|
+
encode(set, encode) {
|
|
2267
|
+
let array = Array.from(set);
|
|
2268
|
+
encode(array);
|
|
2269
|
+
}
|
|
2270
|
+
}, { // Error
|
|
2271
|
+
tag: 27, // http://cbor.schmorp.de/generic-object
|
|
2272
|
+
encode(error, encode) {
|
|
2273
|
+
encode([ error.name, error.message ]);
|
|
2274
|
+
}
|
|
2275
|
+
}, { // RegExp
|
|
2276
|
+
tag: 27, // http://cbor.schmorp.de/generic-object
|
|
2277
|
+
encode(regex, encode) {
|
|
2278
|
+
encode([ 'RegExp', regex.source, regex.flags ]);
|
|
2279
|
+
}
|
|
2280
|
+
}, { // Tag
|
|
2281
|
+
getTag(tag) {
|
|
2282
|
+
return tag.tag
|
|
2283
|
+
},
|
|
2284
|
+
encode(tag, encode) {
|
|
2285
|
+
encode(tag.value);
|
|
2286
|
+
}
|
|
2287
|
+
}, { // ArrayBuffer
|
|
2288
|
+
encode(arrayBuffer, encode, makeRoom) {
|
|
2289
|
+
writeBuffer(arrayBuffer, makeRoom);
|
|
2290
|
+
}
|
|
2291
|
+
}, { // Uint8Array
|
|
2292
|
+
getTag(typedArray) {
|
|
2293
|
+
if (typedArray.constructor === Uint8Array) {
|
|
2294
|
+
if (this.tagUint8Array || hasNodeBuffer && this.tagUint8Array !== false)
|
|
2295
|
+
return 64;
|
|
2296
|
+
} // else no tag
|
|
2297
|
+
},
|
|
2298
|
+
encode(typedArray, encode, makeRoom) {
|
|
2299
|
+
writeBuffer(typedArray, makeRoom);
|
|
2300
|
+
}
|
|
2301
|
+
},
|
|
2302
|
+
typedArrayEncoder(68, 1),
|
|
2303
|
+
typedArrayEncoder(69, 2),
|
|
2304
|
+
typedArrayEncoder(70, 4),
|
|
2305
|
+
typedArrayEncoder(71, 8),
|
|
2306
|
+
typedArrayEncoder(72, 1),
|
|
2307
|
+
typedArrayEncoder(77, 2),
|
|
2308
|
+
typedArrayEncoder(78, 4),
|
|
2309
|
+
typedArrayEncoder(79, 8),
|
|
2310
|
+
typedArrayEncoder(85, 4),
|
|
2311
|
+
typedArrayEncoder(86, 8),
|
|
2312
|
+
{
|
|
2313
|
+
encode(sharedData, encode) { // write SharedData
|
|
2314
|
+
let packedValues = sharedData.packedValues || [];
|
|
2315
|
+
let sharedStructures = sharedData.structures || [];
|
|
2316
|
+
if (packedValues.values.length > 0) {
|
|
2317
|
+
target[position++] = 0xd8; // one-byte tag
|
|
2318
|
+
target[position++] = 51; // tag 51 for packed shared structures https://www.potaroo.net/ietf/ids/draft-ietf-cbor-packed-03.txt
|
|
2319
|
+
writeArrayHeader(4);
|
|
2320
|
+
let valuesArray = packedValues.values;
|
|
2321
|
+
encode(valuesArray);
|
|
2322
|
+
writeArrayHeader(0); // prefixes
|
|
2323
|
+
writeArrayHeader(0); // suffixes
|
|
2324
|
+
packedObjectMap = Object.create(sharedPackedObjectMap || null);
|
|
2325
|
+
for (let i = 0, l = valuesArray.length; i < l; i++) {
|
|
2326
|
+
packedObjectMap[valuesArray[i]] = i;
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
if (sharedStructures) {
|
|
2330
|
+
targetView.setUint32(position, 0xd9dffe00);
|
|
2331
|
+
position += 3;
|
|
2332
|
+
let definitions = sharedStructures.slice(0);
|
|
2333
|
+
definitions.unshift(0xe000);
|
|
2334
|
+
definitions.push(new Tag(sharedData.version, 0x53687264));
|
|
2335
|
+
encode(definitions);
|
|
2336
|
+
} else
|
|
2337
|
+
encode(new Tag(sharedData.version, 0x53687264));
|
|
2338
|
+
}
|
|
2339
|
+
}];
|
|
2340
|
+
function typedArrayEncoder(tag, size) {
|
|
2341
|
+
if (!isLittleEndianMachine && size > 1)
|
|
2342
|
+
tag -= 4; // the big endian equivalents are 4 less
|
|
2343
|
+
return {
|
|
2344
|
+
tag: tag,
|
|
2345
|
+
encode: function writeExtBuffer(typedArray, encode) {
|
|
2346
|
+
let length = typedArray.byteLength;
|
|
2347
|
+
let offset = typedArray.byteOffset || 0;
|
|
2348
|
+
let buffer = typedArray.buffer || typedArray;
|
|
2349
|
+
encode(hasNodeBuffer ? Buffer$1.from(buffer, offset, length) :
|
|
2350
|
+
new Uint8Array(buffer, offset, length));
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
function writeBuffer(buffer, makeRoom) {
|
|
2355
|
+
let length = buffer.byteLength;
|
|
2356
|
+
if (length < 0x18) {
|
|
2357
|
+
target[position++] = 0x40 + length;
|
|
2358
|
+
} else if (length < 0x100) {
|
|
2359
|
+
target[position++] = 0x58;
|
|
2360
|
+
target[position++] = length;
|
|
2361
|
+
} else if (length < 0x10000) {
|
|
2362
|
+
target[position++] = 0x59;
|
|
2363
|
+
target[position++] = length >> 8;
|
|
2364
|
+
target[position++] = length & 0xff;
|
|
2365
|
+
} else {
|
|
2366
|
+
target[position++] = 0x5a;
|
|
2367
|
+
targetView.setUint32(position, length);
|
|
2368
|
+
position += 4;
|
|
2369
|
+
}
|
|
2370
|
+
if (position + length >= target.length) {
|
|
2371
|
+
makeRoom(position + length);
|
|
2372
|
+
}
|
|
2373
|
+
// if it is already a typed array (has an ArrayBuffer), use that, but if it is an ArrayBuffer itself,
|
|
2374
|
+
// must wrap it to set it.
|
|
2375
|
+
target.set(buffer.buffer ? buffer : new Uint8Array(buffer), position);
|
|
2376
|
+
position += length;
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
function insertIds(serialized, idsToInsert) {
|
|
2380
|
+
// insert the ids that need to be referenced for structured clones
|
|
2381
|
+
let nextId;
|
|
2382
|
+
let distanceToMove = idsToInsert.length * 2;
|
|
2383
|
+
let lastEnd = serialized.length - distanceToMove;
|
|
2384
|
+
idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
|
|
2385
|
+
for (let id = 0; id < idsToInsert.length; id++) {
|
|
2386
|
+
let referee = idsToInsert[id];
|
|
2387
|
+
referee.id = id;
|
|
2388
|
+
for (let position of referee.references) {
|
|
2389
|
+
serialized[position++] = id >> 8;
|
|
2390
|
+
serialized[position] = id & 0xff;
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
while (nextId = idsToInsert.pop()) {
|
|
2394
|
+
let offset = nextId.offset;
|
|
2395
|
+
serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
|
|
2396
|
+
distanceToMove -= 2;
|
|
2397
|
+
let position = offset + distanceToMove;
|
|
2398
|
+
serialized[position++] = 0xd8;
|
|
2399
|
+
serialized[position++] = 28; // http://cbor.schmorp.de/value-sharing
|
|
2400
|
+
lastEnd = offset;
|
|
2401
|
+
}
|
|
2402
|
+
return serialized
|
|
2403
|
+
}
|
|
2404
|
+
function writeBundles(start, encode) {
|
|
2405
|
+
targetView.setUint32(bundledStrings.position + start, position - bundledStrings.position - start + 1); // the offset to bundle
|
|
2406
|
+
let writeStrings = bundledStrings;
|
|
2407
|
+
bundledStrings = null;
|
|
2408
|
+
encode(writeStrings[0]);
|
|
2409
|
+
encode(writeStrings[1]);
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
function addExtension$1(extension) {
|
|
2413
|
+
if (extension.Class) {
|
|
2414
|
+
if (!extension.encode)
|
|
2415
|
+
throw new Error('Extension has no encode function')
|
|
2416
|
+
extensionClasses.unshift(extension.Class);
|
|
2417
|
+
extensions.unshift(extension);
|
|
2418
|
+
}
|
|
2419
|
+
addExtension$2(extension);
|
|
2420
|
+
}
|
|
2421
|
+
let defaultEncoder = new Encoder$1({ useRecords: false });
|
|
2422
|
+
const encode$1 = defaultEncoder.encode;
|
|
2423
|
+
const encodeAsIterable$1 = defaultEncoder.encodeAsIterable;
|
|
2424
|
+
const encodeAsAsyncIterable$1 = defaultEncoder.encodeAsAsyncIterable;
|
|
2425
|
+
const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT: DECIMAL_FIT$1 } = FLOAT32_OPTIONS;
|
|
2426
|
+
const REUSE_BUFFER_MODE = 512;
|
|
2427
|
+
const RESET_BUFFER_MODE = 1024;
|
|
2428
|
+
const THROW_ON_ITERABLE = 2048;
|
|
2429
|
+
|
|
2430
|
+
const nativeAccelerationDisabled = process.env.CBOR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.CBOR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
|
|
2431
|
+
|
|
2432
|
+
if (!nativeAccelerationDisabled) {
|
|
2433
|
+
let extractor;
|
|
2434
|
+
try {
|
|
2435
|
+
if (typeof require == 'function')
|
|
2436
|
+
extractor = require('cbor-extract');
|
|
2437
|
+
else
|
|
2438
|
+
extractor = module.createRequire((document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))('cbor-extract');
|
|
2439
|
+
if (extractor)
|
|
2440
|
+
setExtractor(extractor.extractStrings);
|
|
2441
|
+
} catch (error) {
|
|
2442
|
+
// native module is optional
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
const sampleData = JSON.parse(fs.readFileSync(new URL('./example4.json', (document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))));
|
|
2447
|
+
|
|
2448
|
+
const senmlData = [
|
|
2449
|
+
{ bn: '/3303/0/5700', bt: 1278887, v: 35.5 },
|
|
2450
|
+
{ t: 10, v: 34 },
|
|
2451
|
+
{ t: 20, v: 33 },
|
|
2452
|
+
{ t: 30, v: 32 },
|
|
2453
|
+
{ t: 40, v: 31 },
|
|
2454
|
+
{ t: 50, v: 30 }
|
|
2455
|
+
];
|
|
2456
|
+
|
|
2457
|
+
const senmlKeys = { bs: -6, bv: -5, bu: -4, bt: -3, bn: -2, n: 0, u: 1, v: 2, vs: 3, t: 6, ut: 7, vd: 8 };
|
|
2458
|
+
|
|
2459
|
+
//import inspector from 'inspector'; inspector.open(9229, null, true); debugger
|
|
2460
|
+
function tryRequire(module) {
|
|
2461
|
+
try {
|
|
2462
|
+
return require(module)
|
|
2463
|
+
} catch(error) {
|
|
2464
|
+
return {}
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
var assert = chai.assert;
|
|
2468
|
+
|
|
2469
|
+
var Encoder = Encoder$1;
|
|
2470
|
+
var decode = decode$1;
|
|
2471
|
+
var encode = encode$1;
|
|
2472
|
+
var encodeAsIterable = encodeAsIterable$1;
|
|
2473
|
+
var encodeAsAsyncIterable = encodeAsAsyncIterable$1;
|
|
2474
|
+
var DECIMAL_FIT = DECIMAL_FIT$1;
|
|
2475
|
+
|
|
2476
|
+
var addExtension = addExtension$1;
|
|
2477
|
+
|
|
2478
|
+
var zlib = tryRequire('zlib');
|
|
2479
|
+
zlib.deflateSync;
|
|
2480
|
+
zlib.inflateSync;
|
|
2481
|
+
zlib.brotliCompressSync;
|
|
2482
|
+
zlib.brotliDecompressSync;
|
|
2483
|
+
zlib.constants;
|
|
2484
|
+
|
|
2485
|
+
var ITERATIONS = 4000;
|
|
2486
|
+
|
|
2487
|
+
suite('CBOR basic tests', function(){
|
|
2488
|
+
test('encode/decode with keyMaps (basic)', function() {
|
|
2489
|
+
var data = senmlData;
|
|
2490
|
+
let cborSenml = new Encoder({ useRecords: false, keyMap: senmlKeys });
|
|
2491
|
+
let cborBasic = new Encoder();
|
|
2492
|
+
var serialized = cborSenml.encode(data);
|
|
2493
|
+
var deserialized = cborSenml.decode(serialized);
|
|
2494
|
+
assert(serialized.length < cborBasic.encode(data).length);
|
|
2495
|
+
assert.deepEqual(deserialized, data);
|
|
2496
|
+
});
|
|
2497
|
+
|
|
2498
|
+
test('encode/decode with keyMaps and Records)', function() {
|
|
2499
|
+
var data = senmlData;
|
|
2500
|
+
let cborSenml = new Encoder({ useRecords: true, keyMap: senmlKeys });
|
|
2501
|
+
let cborBasic = new Encoder();
|
|
2502
|
+
var serialized = cborSenml.encode(data);
|
|
2503
|
+
var deserialized = cborSenml.decode(serialized);
|
|
2504
|
+
assert(serialized.length < cborBasic.encode(data).length);
|
|
2505
|
+
assert.deepEqual(deserialized, data);
|
|
2506
|
+
});
|
|
2507
|
+
|
|
2508
|
+
test('encode/decode data', function(){
|
|
2509
|
+
var data = {
|
|
2510
|
+
data: [
|
|
2511
|
+
{ a: 1, name: 'one', type: 'odd', isOdd: true },
|
|
2512
|
+
{ a: 2, name: 'two', type: 'even'},
|
|
2513
|
+
{ a: 3, name: 'three', type: 'odd', isOdd: true },
|
|
2514
|
+
{ a: 4, name: 'four', type: 'even'},
|
|
2515
|
+
{ a: 5, name: 'five', type: 'odd', isOdd: true },
|
|
2516
|
+
{ a: 6, name: 'six', type: 'even', isOdd: null }
|
|
2517
|
+
],
|
|
2518
|
+
description: 'some names',
|
|
2519
|
+
types: ['odd', 'even'],
|
|
2520
|
+
convertEnumToNum: [
|
|
2521
|
+
{ prop: 'test' },
|
|
2522
|
+
{ prop: 'test' },
|
|
2523
|
+
{ prop: 'test' },
|
|
2524
|
+
{ prop: 1 },
|
|
2525
|
+
{ prop: 2 },
|
|
2526
|
+
{ prop: [undefined] },
|
|
2527
|
+
{ prop: null }
|
|
2528
|
+
]
|
|
2529
|
+
};
|
|
2530
|
+
let structures = [];
|
|
2531
|
+
let encoder = new Encoder({ structures });
|
|
2532
|
+
var serialized = encoder.encode(data);
|
|
2533
|
+
serialized = encoder.encode(data);
|
|
2534
|
+
var deserialized = encoder.decode(serialized);
|
|
2535
|
+
assert.deepEqual(deserialized, data);
|
|
2536
|
+
});
|
|
2537
|
+
|
|
2538
|
+
test('mixed structures, shared', function(){
|
|
2539
|
+
let data1 = { a: 1, b: 2, c: 3 };
|
|
2540
|
+
let data2 = { a: 1, b: 2, d: 4 };
|
|
2541
|
+
let data3 = { a: 1, b: 2, e: 5 };
|
|
2542
|
+
let structures = [];
|
|
2543
|
+
let encoder = new Encoder({ structures });
|
|
2544
|
+
var serialized = encoder.encode(data1);
|
|
2545
|
+
var deserialized = encoder.decode(serialized);
|
|
2546
|
+
assert.deepEqual(deserialized, data1);
|
|
2547
|
+
var serialized = encoder.encode(data2);
|
|
2548
|
+
var deserialized = encoder.decode(serialized);
|
|
2549
|
+
assert.deepEqual(deserialized, data2);
|
|
2550
|
+
var serialized = encoder.encode(data3);
|
|
2551
|
+
var deserialized = encoder.decode(serialized);
|
|
2552
|
+
assert.deepEqual(deserialized, data3);
|
|
2553
|
+
});
|
|
2554
|
+
|
|
2555
|
+
test('mixed structures, unshared', function(){
|
|
2556
|
+
let data = [];
|
|
2557
|
+
let encoder = new Encoder({ });
|
|
2558
|
+
for (let i = 0; i< 1000; i++) {
|
|
2559
|
+
data.push({a: 1, ['test' + i]: i});
|
|
2560
|
+
}
|
|
2561
|
+
var serialized = encoder.encode(data);
|
|
2562
|
+
var deserialized = encoder.decode(serialized);
|
|
2563
|
+
assert.deepEqual(deserialized, data);
|
|
2564
|
+
serialized = encoder.encode(data);
|
|
2565
|
+
deserialized = encoder.decode(serialized);
|
|
2566
|
+
assert.deepEqual(deserialized, data);
|
|
2567
|
+
});
|
|
2568
|
+
|
|
2569
|
+
test('mixed array', function(){
|
|
2570
|
+
var data = [
|
|
2571
|
+
'one',
|
|
2572
|
+
'two',
|
|
2573
|
+
'one',
|
|
2574
|
+
10,
|
|
2575
|
+
11,
|
|
2576
|
+
null,
|
|
2577
|
+
true,
|
|
2578
|
+
'three',
|
|
2579
|
+
'three',
|
|
2580
|
+
'one', [
|
|
2581
|
+
3, -5, -50, -400,1.3, -5.3, true
|
|
2582
|
+
]
|
|
2583
|
+
];
|
|
2584
|
+
let structures = [];
|
|
2585
|
+
let encoder = new Encoder({ structures });
|
|
2586
|
+
var serialized = encoder.encode(data);
|
|
2587
|
+
var deserialized = encoder.decode(serialized);
|
|
2588
|
+
assert.deepEqual(deserialized, data);
|
|
2589
|
+
});
|
|
2590
|
+
|
|
2591
|
+
test('255 chars', function() {
|
|
2592
|
+
const data = 'RRZG9A6I7xupPeOZhxcOcioFsuhszGOdyDUcbRf4Zef2kdPIfC9RaLO4jTM5JhuZvTsF09fbRHMGtqk7YAgu3vespeTe9l61ziZ6VrMnYu2CamK96wCkmz0VUXyqaiUoTPgzk414LS9yYrd5uh7w18ksJF5SlC2e91rukWvNqAZJjYN3jpkqHNOFchCwFrhbxq2Lrv1kSJPYCx9blRg2hGmYqTbElLTZHv20iNqwZeQbRMgSBPT6vnbCBPnOh1W';
|
|
2593
|
+
var serialized = encode$1(data);
|
|
2594
|
+
var deserialized = decode$1(serialized);
|
|
2595
|
+
assert.equal(deserialized, data);
|
|
2596
|
+
});
|
|
2597
|
+
|
|
2598
|
+
test('encode/decode sample data', function(){
|
|
2599
|
+
var data = sampleData;
|
|
2600
|
+
var serialized = encode$1(data);
|
|
2601
|
+
var deserialized = decode$1(serialized);
|
|
2602
|
+
assert.deepEqual(deserialized, data);
|
|
2603
|
+
var serialized = encode$1(data);
|
|
2604
|
+
var deserialized = decode$1(serialized);
|
|
2605
|
+
assert.deepEqual(deserialized, data);
|
|
2606
|
+
});
|
|
2607
|
+
test('encode/decode sample data with records', function(){
|
|
2608
|
+
var data = sampleData;
|
|
2609
|
+
let sharedSerialized;
|
|
2610
|
+
let encoder = new Encoder({ getStructures() { return }, saveStructures(shared) { sharedSerialized = encode(shared); }, useRecords: true });
|
|
2611
|
+
var serialized = encoder.encode(data);
|
|
2612
|
+
encoder = new Encoder({ getStructures() { return decode(sharedSerialized) }, saveStructures(shared) { sharedSerialized = encode(shared); }, useRecords: true });
|
|
2613
|
+
var deserialized = encoder.decode(serialized);
|
|
2614
|
+
assert.deepEqual(deserialized, data);
|
|
2615
|
+
});
|
|
2616
|
+
test('encode/decode sample data with packing', function(){
|
|
2617
|
+
var data = sampleData;
|
|
2618
|
+
let encoder = new Encoder({ pack: true, useRecords: false });
|
|
2619
|
+
var serialized = encoder.encode(data);
|
|
2620
|
+
var deserialized = encoder.decode(serialized);
|
|
2621
|
+
assert.deepEqual(deserialized, data);
|
|
2622
|
+
});
|
|
2623
|
+
test('encode/decode sample data with packing and records', function(){
|
|
2624
|
+
var data = sampleData;
|
|
2625
|
+
let encoder = new Encoder({ useStringRefs: true });
|
|
2626
|
+
var serialized = encoder.encode(data);
|
|
2627
|
+
var deserialized = encoder.decode(serialized);
|
|
2628
|
+
assert.deepEqual(deserialized, data);
|
|
2629
|
+
});
|
|
2630
|
+
test('encode/decode sample data with shared packing and records', function(){
|
|
2631
|
+
let encoder = new Encoder({ useRecords: true });
|
|
2632
|
+
let finishPack = encoder.findCommonStringsToPack();
|
|
2633
|
+
for (let i = 0; i < 20; i++) {
|
|
2634
|
+
let data = {
|
|
2635
|
+
shouldShare: 'same each time',
|
|
2636
|
+
shouldShare2: 'same each time 2',
|
|
2637
|
+
shouldntShare: 'different each time ' + i
|
|
2638
|
+
};
|
|
2639
|
+
if (i == 10)
|
|
2640
|
+
finishPack({});
|
|
2641
|
+
var serialized = encoder.encode(data);
|
|
2642
|
+
var deserialized = encoder.decode(serialized);
|
|
2643
|
+
assert.deepEqual(deserialized, data);
|
|
2644
|
+
}
|
|
2645
|
+
});
|
|
2646
|
+
test('encode/decode sample data with individual packing, shared packing and records', function(){
|
|
2647
|
+
let encoder = new Encoder({ pack: true, useRecords: true });
|
|
2648
|
+
let finishPack = encoder.findCommonStringsToPack();
|
|
2649
|
+
for (let i = 0; i < 20; i++) {
|
|
2650
|
+
let data = {
|
|
2651
|
+
shouldShare: 'same each time',
|
|
2652
|
+
shouldShare2: 'same each time',
|
|
2653
|
+
shouldntShare: 'different each time ' + i,
|
|
2654
|
+
shouldntShare2: 'different each time ' + i,
|
|
2655
|
+
noPack: 'no packing ' + i,
|
|
2656
|
+
};
|
|
2657
|
+
if (i == 10)
|
|
2658
|
+
finishPack({ threshold: 5 });
|
|
2659
|
+
var serialized = encoder.encode(data);
|
|
2660
|
+
var deserialized = encoder.decode(serialized);
|
|
2661
|
+
assert.deepEqual(deserialized, data);
|
|
2662
|
+
}
|
|
2663
|
+
});
|
|
2664
|
+
test('pack/unpack sample data with bundled strings', function(){
|
|
2665
|
+
var data = sampleData;
|
|
2666
|
+
let encoder = new Encoder({ /*structures,*/ useRecords: false, bundleStrings: true });
|
|
2667
|
+
var serialized = encoder.encode(data);
|
|
2668
|
+
var deserialized = encoder.decode(serialized);
|
|
2669
|
+
assert.deepEqual(deserialized, data);
|
|
2670
|
+
});
|
|
2671
|
+
test('pack/unpack sample data with self-descriptive header', function(){
|
|
2672
|
+
var data = sampleData;
|
|
2673
|
+
let encoder = new Encoder({ useSelfDescribedHeader: true });
|
|
2674
|
+
var serialized = encoder.encode(data);
|
|
2675
|
+
var deserialized = encoder.decode(serialized);
|
|
2676
|
+
assert.deepEqual(deserialized, data);
|
|
2677
|
+
assert.equal(serialized[0], 0xd9);
|
|
2678
|
+
assert.equal(serialized[1], 0xd9);
|
|
2679
|
+
assert.equal(serialized[2], 0xf7);
|
|
2680
|
+
});
|
|
2681
|
+
if (typeof Buffer != 'undefined')
|
|
2682
|
+
test('replace data', function(){
|
|
2683
|
+
var data1 = {
|
|
2684
|
+
data: [
|
|
2685
|
+
{ a: 1, name: 'one', type: 'odd', isOdd: true, a: '13 characters' },
|
|
2686
|
+
{ a: 2, name: 'two', type: 'even', a: '11 characte' },
|
|
2687
|
+
{ a: 3, name: 'three', type: 'odd', isOdd: true, a: '12 character' },
|
|
2688
|
+
{ a: 4, name: 'four', type: 'even', a: '9 charact'},
|
|
2689
|
+
{ a: 5, name: 'five', type: 'odd', isOdd: true, a: '14 characters!' },
|
|
2690
|
+
{ a: 6, name: 'six', type: 'even', isOdd: null }
|
|
2691
|
+
],
|
|
2692
|
+
};
|
|
2693
|
+
var data2 = {
|
|
2694
|
+
data: [
|
|
2695
|
+
{ foo: 7, name: 'one', type: 'odd', isOdd: true },
|
|
2696
|
+
{ foo: 8, name: 'two', type: 'even'},
|
|
2697
|
+
{ foo: 9, name: 'three', type: 'odd', isOdd: true },
|
|
2698
|
+
{ foo: 10, name: 'four', type: 'even'},
|
|
2699
|
+
{ foo: 11, name: 'five', type: 'odd', isOdd: true },
|
|
2700
|
+
{ foo: 12, name: 'six', type: 'even', isOdd: null }
|
|
2701
|
+
],
|
|
2702
|
+
};
|
|
2703
|
+
var serialized1 = encode(data1);
|
|
2704
|
+
var serialized2 = encode(data2);
|
|
2705
|
+
var b = Buffer.alloc(8000);
|
|
2706
|
+
serialized1.copy(b);
|
|
2707
|
+
var deserialized1 = decode(b, serialized1.length);
|
|
2708
|
+
serialized2.copy(b);
|
|
2709
|
+
var deserialized2 = decode(b, serialized2.length);
|
|
2710
|
+
assert.deepEqual(deserialized1, data1);
|
|
2711
|
+
assert.deepEqual(deserialized2, data2);
|
|
2712
|
+
});
|
|
2713
|
+
test('extended class encode/decode', function() {
|
|
2714
|
+
function Extended() {
|
|
2715
|
+
|
|
2716
|
+
}
|
|
2717
|
+
|
|
2718
|
+
Extended.prototype.getDouble = function() {
|
|
2719
|
+
return this.value * 2
|
|
2720
|
+
};
|
|
2721
|
+
var instance = new Extended();
|
|
2722
|
+
instance.value = 4;
|
|
2723
|
+
instance.string = 'decode this: ᾜ';
|
|
2724
|
+
let encoder = new Encoder();
|
|
2725
|
+
addExtension({
|
|
2726
|
+
Class: Extended,
|
|
2727
|
+
tag: 300,
|
|
2728
|
+
decode: function(data) {
|
|
2729
|
+
let e = new Extended();
|
|
2730
|
+
e.value = data[0];
|
|
2731
|
+
e.string = data[1];
|
|
2732
|
+
return e
|
|
2733
|
+
},
|
|
2734
|
+
encode: function(instance) {
|
|
2735
|
+
return encoder.encode([instance.value, instance.string])
|
|
2736
|
+
}
|
|
2737
|
+
});
|
|
2738
|
+
});
|
|
2739
|
+
test('extended class encode/decode with self reference in structered clone', function(){
|
|
2740
|
+
function Extended() {
|
|
2741
|
+
|
|
2742
|
+
}
|
|
2743
|
+
addExtension({
|
|
2744
|
+
Class: Extended,
|
|
2745
|
+
tag: 301,
|
|
2746
|
+
decode: function(data) {
|
|
2747
|
+
let e = new Extended();
|
|
2748
|
+
e.value = data[0];
|
|
2749
|
+
e.string = data[1];
|
|
2750
|
+
return e
|
|
2751
|
+
},
|
|
2752
|
+
encode: function(instance, encode) {
|
|
2753
|
+
return encode([instance.value, instance.string])
|
|
2754
|
+
}
|
|
2755
|
+
});
|
|
2756
|
+
var instance = new Extended();
|
|
2757
|
+
instance.value = instance;
|
|
2758
|
+
instance.string = 'hi';
|
|
2759
|
+
let data = {
|
|
2760
|
+
extended: instance
|
|
2761
|
+
};
|
|
2762
|
+
let encoder = new Encoder({
|
|
2763
|
+
structuredClone: true,
|
|
2764
|
+
});
|
|
2765
|
+
let serialized = encoder.encode(data);
|
|
2766
|
+
encoder.decode(serialized);
|
|
2767
|
+
assert(data.extended.value.value === data.extended);
|
|
2768
|
+
assert(data.extended instanceof Extended);
|
|
2769
|
+
});
|
|
2770
|
+
|
|
2771
|
+
test('addExtension with map', function(){
|
|
2772
|
+
function Extended() {
|
|
2773
|
+
}
|
|
2774
|
+
var instance = new Extended();
|
|
2775
|
+
instance.value = 4;
|
|
2776
|
+
instance.map = new Map();
|
|
2777
|
+
instance.map.set('key', 'value');
|
|
2778
|
+
var data = {
|
|
2779
|
+
extendedInstance: instance,
|
|
2780
|
+
};
|
|
2781
|
+
let encoder = new Encoder();
|
|
2782
|
+
addExtension({
|
|
2783
|
+
Class: Extended,
|
|
2784
|
+
tag: 301,
|
|
2785
|
+
decode: function(data) {
|
|
2786
|
+
let e = new Extended();
|
|
2787
|
+
e.value = data[0];
|
|
2788
|
+
e.map = data[1];
|
|
2789
|
+
return e
|
|
2790
|
+
},
|
|
2791
|
+
encode: function(instance, encode) {
|
|
2792
|
+
return encode([instance.value, instance.map])
|
|
2793
|
+
}
|
|
2794
|
+
});
|
|
2795
|
+
var serialized = encoder.encode(data);
|
|
2796
|
+
var deserialized = encoder.decode(serialized);
|
|
2797
|
+
assert.deepEqual(data, deserialized);
|
|
2798
|
+
});
|
|
2799
|
+
|
|
2800
|
+
test.skip('text decoder', function() {
|
|
2801
|
+
let td = new TextDecoder('ISO-8859-15');
|
|
2802
|
+
let b = Buffer.alloc(3);
|
|
2803
|
+
for (var i = 0; i < 256; i++) {
|
|
2804
|
+
b[0] = i;
|
|
2805
|
+
b[1] = 0;
|
|
2806
|
+
b[2] = 0;
|
|
2807
|
+
let s = td.decode(b);
|
|
2808
|
+
if (!require('CBOR-extract').isOneByte(s)) {
|
|
2809
|
+
console.log(i.toString(16), s.length);
|
|
2810
|
+
}
|
|
2811
|
+
}
|
|
2812
|
+
});
|
|
2813
|
+
|
|
2814
|
+
test('structured cloning: self reference', function() {
|
|
2815
|
+
let object = {
|
|
2816
|
+
test: 'string',
|
|
2817
|
+
children: [
|
|
2818
|
+
{ name: 'child' }
|
|
2819
|
+
]
|
|
2820
|
+
};
|
|
2821
|
+
object.self = object;
|
|
2822
|
+
object.children[1] = object;
|
|
2823
|
+
object.children[2] = object.children[0];
|
|
2824
|
+
object.childrenAgain = object.children;
|
|
2825
|
+
let encoder = new Encoder({
|
|
2826
|
+
structuredClone: true,
|
|
2827
|
+
});
|
|
2828
|
+
var serialized = encoder.encode(object);
|
|
2829
|
+
var deserialized = encoder.decode(serialized);
|
|
2830
|
+
assert.equal(deserialized.self, deserialized);
|
|
2831
|
+
assert.equal(deserialized.children[0].name, 'child');
|
|
2832
|
+
assert.equal(deserialized.children[1], deserialized);
|
|
2833
|
+
assert.equal(deserialized.children[0], deserialized.children[2]);
|
|
2834
|
+
assert.equal(deserialized.children, deserialized.childrenAgain);
|
|
2835
|
+
});
|
|
2836
|
+
test('nested same key', function() {
|
|
2837
|
+
const encoder = new Encoder();
|
|
2838
|
+
const r_key = "key";
|
|
2839
|
+
const d_key = "key";
|
|
2840
|
+
const data = { [r_key]: { [d_key]: "foo" } };
|
|
2841
|
+
const enc = encoder.encode(data);
|
|
2842
|
+
const dec = encoder.decode(enc);
|
|
2843
|
+
assert.deepEqual(dec, data);
|
|
2844
|
+
});
|
|
2845
|
+
test('decode float 16', function() {
|
|
2846
|
+
assert.equal(decode(new Uint8Array([0xF9, 0x4A, 0x60])), 12.75);
|
|
2847
|
+
assert.equal(decode(new Uint8Array([0xF9, 0xC4, 0x80])), -4.5);
|
|
2848
|
+
assert.equal(decode(new Uint8Array([0xF9, 0x5A, 0xF9])), 223.125);
|
|
2849
|
+
assert.equal(decode(new Uint8Array([0xF9, 0x45, 0x80])), 5.5);
|
|
2850
|
+
assert.equal(decode(new Uint8Array([0xF9, 0x7C, 0])), Infinity);
|
|
2851
|
+
assert.equal(decode(new Uint8Array([0xF9, 0xFC, 0])), -Infinity);
|
|
2852
|
+
assert.isNaN(decode(new Uint8Array([0xF9, 0x7E, 0])));
|
|
2853
|
+
});
|
|
2854
|
+
test('structured cloning: types', function() {
|
|
2855
|
+
let b = typeof Buffer != 'undefined' ? Buffer.alloc(20) : new Uint8Array(20);
|
|
2856
|
+
let fa = new Float32Array(b.buffer, 8, 2);
|
|
2857
|
+
fa[0] = 2.25;
|
|
2858
|
+
fa[1] = 6;
|
|
2859
|
+
let f64a = new Float64Array([2.3, 4.7]);
|
|
2860
|
+
let map = new Map();
|
|
2861
|
+
map.set('key', 'value');
|
|
2862
|
+
let object = {
|
|
2863
|
+
error: new Error('test'),
|
|
2864
|
+
set: new Set(['a', 'b']),
|
|
2865
|
+
regexp: /test/gi,
|
|
2866
|
+
map,
|
|
2867
|
+
float32Array: fa,
|
|
2868
|
+
float64Array: f64a,
|
|
2869
|
+
uint16Array: new Uint16Array([3,4])
|
|
2870
|
+
};
|
|
2871
|
+
let encoder = new Encoder({
|
|
2872
|
+
structuredClone: true,
|
|
2873
|
+
});
|
|
2874
|
+
var serialized = encoder.encode(object);
|
|
2875
|
+
var deserialized = encoder.decode(serialized);
|
|
2876
|
+
assert.deepEqual(Array.from(deserialized.set), Array.from(object.set));
|
|
2877
|
+
assert.equal(deserialized.map.get('key'), 'value');
|
|
2878
|
+
assert.equal(deserialized.error.message, object.error.message);
|
|
2879
|
+
assert.equal(deserialized.regexp.test('TEST'), true);
|
|
2880
|
+
assert.equal(deserialized.float32Array.constructor.name, 'Float32Array');
|
|
2881
|
+
assert.equal(deserialized.float32Array[0], 2.25);
|
|
2882
|
+
assert.equal(deserialized.float32Array[1], 6);
|
|
2883
|
+
assert.equal(deserialized.float64Array[0], 2.3);
|
|
2884
|
+
assert.equal(deserialized.float64Array[1], 4.7);
|
|
2885
|
+
assert.equal(deserialized.uint16Array.constructor.name, 'Uint16Array');
|
|
2886
|
+
assert.equal(deserialized.uint16Array[0], 3);
|
|
2887
|
+
assert.equal(deserialized.uint16Array[1], 4);
|
|
2888
|
+
});
|
|
2889
|
+
|
|
2890
|
+
test('explicit maps and sets', function () {
|
|
2891
|
+
let map = new Map();
|
|
2892
|
+
map.set('key', { inside: 'value'});
|
|
2893
|
+
let object = {
|
|
2894
|
+
set: new Set(['a', 'b']),
|
|
2895
|
+
map,
|
|
2896
|
+
};
|
|
2897
|
+
var serialized = encode(object); // default encoder
|
|
2898
|
+
var deserialized = decode(serialized);
|
|
2899
|
+
assert.deepEqual(Array.from(deserialized.set), Array.from(object.set));
|
|
2900
|
+
assert.equal(deserialized.map.get('key').inside, 'value');
|
|
2901
|
+
});
|
|
2902
|
+
|
|
2903
|
+
test('object without prototype', function(){
|
|
2904
|
+
var data = Object.create(null);
|
|
2905
|
+
data.test = 3;
|
|
2906
|
+
var serialized = encode(data);
|
|
2907
|
+
var deserialized = decode(serialized);
|
|
2908
|
+
assert.deepEqual(deserialized, data);
|
|
2909
|
+
});
|
|
2910
|
+
test('object with __proto__', function(){
|
|
2911
|
+
const data = { foo: 'bar', __proto__: { isAdmin: true } };
|
|
2912
|
+
var serialized = encode(data);
|
|
2913
|
+
var deserialized = decode(serialized);
|
|
2914
|
+
assert.deepEqual(deserialized, { foo: 'bar' });
|
|
2915
|
+
});
|
|
2916
|
+
|
|
2917
|
+
test('big buffer', function() {
|
|
2918
|
+
var size = 100000000;
|
|
2919
|
+
var data = new Uint8Array(size).fill(1);
|
|
2920
|
+
var encoded = encode(data);
|
|
2921
|
+
var decoded = decode(encoded);
|
|
2922
|
+
assert.equal(decoded.length, size);
|
|
2923
|
+
});
|
|
2924
|
+
test('little buffer', function() {
|
|
2925
|
+
var data = typeof Buffer == 'undefined' ? new Uint8Array(0) : Buffer.alloc(0);
|
|
2926
|
+
var encoded = encode(data);
|
|
2927
|
+
assert.equal(encoded.length, 1); // make sure to use canonical form
|
|
2928
|
+
var decoded = decode(encoded);
|
|
2929
|
+
assert.equal(decoded.length, 0);
|
|
2930
|
+
});
|
|
2931
|
+
|
|
2932
|
+
test('random strings', function(){
|
|
2933
|
+
var data = [];
|
|
2934
|
+
for (var i = 0; i < 2000; i++) {
|
|
2935
|
+
var str = 'test';
|
|
2936
|
+
while (Math.random() < 0.7 && str.length < 0x100000) {
|
|
2937
|
+
str = str + String.fromCharCode(90/(Math.random() + 0.01)) + str;
|
|
2938
|
+
}
|
|
2939
|
+
data.push(str);
|
|
2940
|
+
}
|
|
2941
|
+
var serialized = encode(data);
|
|
2942
|
+
var deserialized = decode(serialized);
|
|
2943
|
+
assert.deepEqual(deserialized, data);
|
|
2944
|
+
});
|
|
2945
|
+
|
|
2946
|
+
test('map/date', function(){
|
|
2947
|
+
var map = new Map();
|
|
2948
|
+
map.set(4, 'four');
|
|
2949
|
+
map.set('three', 3);
|
|
2950
|
+
let year2039 = new Date('2039-07-05T16:22:35.792Z');
|
|
2951
|
+
let year2038 = new Date('2038-08-06T00:19:02.911Z');
|
|
2952
|
+
|
|
2953
|
+
var data = {
|
|
2954
|
+
map: map,
|
|
2955
|
+
date: new Date(1532219539733),
|
|
2956
|
+
farFutureDate: new Date(3532219539133),
|
|
2957
|
+
ancient: new Date(-3532219539133),
|
|
2958
|
+
year2038,
|
|
2959
|
+
year2039,
|
|
2960
|
+
invalidDate: new Date('invalid')
|
|
2961
|
+
};
|
|
2962
|
+
let encoder = new Encoder();
|
|
2963
|
+
var serialized = encoder.encode(data);
|
|
2964
|
+
var deserialized = encoder.decode(serialized);
|
|
2965
|
+
assert.equal(deserialized.map.get(4), 'four');
|
|
2966
|
+
assert.equal(deserialized.map.get('three'), 3);
|
|
2967
|
+
assert.equal(deserialized.date.getTime(), 1532219539733);
|
|
2968
|
+
assert.equal(deserialized.farFutureDate.getTime(), 3532219539133);
|
|
2969
|
+
assert.equal(deserialized.ancient.getTime(), -3532219539133);
|
|
2970
|
+
assert.equal(deserialized.year2038.getTime(), year2038.getTime());
|
|
2971
|
+
assert.equal(deserialized.year2039.getTime(), year2039.getTime());
|
|
2972
|
+
assert.equal(deserialized.invalidDate.toString(), 'Invalid Date');
|
|
2973
|
+
});
|
|
2974
|
+
test('map/date with options', function(){
|
|
2975
|
+
var map = new Map();
|
|
2976
|
+
map.set(4, 'four');
|
|
2977
|
+
map.set('three', 3);
|
|
2978
|
+
var data = {
|
|
2979
|
+
map: map,
|
|
2980
|
+
date: new Date(1532219539011),
|
|
2981
|
+
invalidDate: new Date('invalid')
|
|
2982
|
+
};
|
|
2983
|
+
let encoder = new Encoder({
|
|
2984
|
+
mapsAsObjects: true,
|
|
2985
|
+
useTimestamp32: true,
|
|
2986
|
+
useTag259ForMaps: false,
|
|
2987
|
+
});
|
|
2988
|
+
var serialized = encoder.encode(data);
|
|
2989
|
+
var deserialized = encoder.decode(serialized);
|
|
2990
|
+
assert.equal(deserialized.map[4], 'four');
|
|
2991
|
+
assert.equal(deserialized.map.three, 3);
|
|
2992
|
+
assert.equal(deserialized.date.getTime(), 1532219539000);
|
|
2993
|
+
assert.isTrue(isNaN(deserialized.invalidDate.getTime()));
|
|
2994
|
+
});
|
|
2995
|
+
test('key caching', function() {
|
|
2996
|
+
var data = {
|
|
2997
|
+
foo: 2,
|
|
2998
|
+
bar: 'test',
|
|
2999
|
+
four: 4,
|
|
3000
|
+
seven: 7,
|
|
3001
|
+
foz: 3,
|
|
3002
|
+
};
|
|
3003
|
+
var serialized = encode$1(data);
|
|
3004
|
+
var deserialized = decode$1(serialized);
|
|
3005
|
+
assert.deepEqual(deserialized, data);
|
|
3006
|
+
// do multiple times to test caching
|
|
3007
|
+
var serialized = encode$1(data);
|
|
3008
|
+
var deserialized = decode$1(serialized);
|
|
3009
|
+
assert.deepEqual(deserialized, data);
|
|
3010
|
+
var serialized = encode$1(data);
|
|
3011
|
+
var deserialized = decode$1(serialized);
|
|
3012
|
+
assert.deepEqual(deserialized, data);
|
|
3013
|
+
});
|
|
3014
|
+
test('strings', function() {
|
|
3015
|
+
var data = [''];
|
|
3016
|
+
var serialized = encode(data);
|
|
3017
|
+
var deserialized = decode(serialized);
|
|
3018
|
+
assert.deepEqual(deserialized, data);
|
|
3019
|
+
// do multiple times
|
|
3020
|
+
var serialized = encode(data);
|
|
3021
|
+
var deserialized = decode(serialized);
|
|
3022
|
+
assert.deepEqual(deserialized, data);
|
|
3023
|
+
data = 'decode this: ᾜ';
|
|
3024
|
+
var serialized = encode(data);
|
|
3025
|
+
var deserialized = decode(serialized);
|
|
3026
|
+
assert.deepEqual(deserialized, data);
|
|
3027
|
+
data = 'decode this that is longer but without any non-latin characters';
|
|
3028
|
+
var serialized = encode(data);
|
|
3029
|
+
var deserialized = decode(serialized);
|
|
3030
|
+
assert.deepEqual(deserialized, data);
|
|
3031
|
+
});
|
|
3032
|
+
test('decimal float32', function() {
|
|
3033
|
+
var data = {
|
|
3034
|
+
a: 2.526,
|
|
3035
|
+
b: 0.0035235,
|
|
3036
|
+
c: 0.00000000000352501,
|
|
3037
|
+
d: 3252.77,
|
|
3038
|
+
};
|
|
3039
|
+
let encoder = new Encoder({
|
|
3040
|
+
useFloat32: DECIMAL_FIT
|
|
3041
|
+
});
|
|
3042
|
+
var serialized = encoder.encode(data);
|
|
3043
|
+
assert.equal(serialized.length, 36);
|
|
3044
|
+
var deserialized = encoder.decode(serialized);
|
|
3045
|
+
assert.deepEqual(deserialized, data);
|
|
3046
|
+
});
|
|
3047
|
+
test('decimal alwaysUseFloat', function() {
|
|
3048
|
+
var data = 123;
|
|
3049
|
+
let encoder = new Encoder({
|
|
3050
|
+
alwaysUseFloat: true
|
|
3051
|
+
});
|
|
3052
|
+
var serialized = encoder.encode(data);
|
|
3053
|
+
assert.equal(serialized.length, 9);
|
|
3054
|
+
var deserialized = encoder.decode(serialized);
|
|
3055
|
+
assert.equal(deserialized, data);
|
|
3056
|
+
});
|
|
3057
|
+
test('bigint to float', function() {
|
|
3058
|
+
var data = {
|
|
3059
|
+
a: 325283295382932843n
|
|
3060
|
+
};
|
|
3061
|
+
let encoder = new Encoder({
|
|
3062
|
+
int64AsNumber: true
|
|
3063
|
+
});
|
|
3064
|
+
var serialized = encoder.encode(data);
|
|
3065
|
+
var deserialized = encoder.decode(serialized);
|
|
3066
|
+
assert.deepEqual(deserialized.a, 325283295382932843);
|
|
3067
|
+
});
|
|
3068
|
+
test('numbers', function(){
|
|
3069
|
+
var data = {
|
|
3070
|
+
bigEncodable: 48978578104322,
|
|
3071
|
+
dateEpoch: 1530886513200,
|
|
3072
|
+
realBig: 3432235352353255323,
|
|
3073
|
+
decimal: 32.55234,
|
|
3074
|
+
negative: -34.11,
|
|
3075
|
+
exponential: 0.234e123,
|
|
3076
|
+
tiny: 3.233e-120,
|
|
3077
|
+
zero: 0,
|
|
3078
|
+
//negativeZero: -0,
|
|
3079
|
+
Infinity: Infinity
|
|
3080
|
+
};
|
|
3081
|
+
var serialized = encode(data);
|
|
3082
|
+
var deserialized = decode(serialized);
|
|
3083
|
+
assert.deepEqual(deserialized, data);
|
|
3084
|
+
});
|
|
3085
|
+
|
|
3086
|
+
test('encode ArrayBuffer', function() {
|
|
3087
|
+
let ua = new Uint8Array([3, 4, 5]);
|
|
3088
|
+
let encoded = encode(ua.buffer);
|
|
3089
|
+
let decoded = decode(encoded);
|
|
3090
|
+
assert.equal(decoded[0], 3);
|
|
3091
|
+
assert.equal(decoded[0], 3);
|
|
3092
|
+
assert.equal(decoded[1], 4);
|
|
3093
|
+
assert.equal(decoded[2], 5);
|
|
3094
|
+
assert.equal(decoded.byteLength, 3);
|
|
3095
|
+
});
|
|
3096
|
+
|
|
3097
|
+
test('iterator/indefinite length array', function(){
|
|
3098
|
+
class NotArray {
|
|
3099
|
+
}
|
|
3100
|
+
let data = ['a', 'b', 'c', ['d']]; // iterable
|
|
3101
|
+
data.constructor = NotArray;
|
|
3102
|
+
var serialized = encode(data);
|
|
3103
|
+
var deserialized = decode(serialized);
|
|
3104
|
+
assert.deepEqual(deserialized, data);
|
|
3105
|
+
});
|
|
3106
|
+
test('bigint', function(){
|
|
3107
|
+
var data = {
|
|
3108
|
+
bigintSmall: 352n,
|
|
3109
|
+
bigintSmallNegative: -333335252n,
|
|
3110
|
+
bigintBig: 2n**64n - 1n, // biggest 64-bit possible
|
|
3111
|
+
bigintBigNegative: -(2n**63n), // largest negative
|
|
3112
|
+
mixedWithNormal: 44,
|
|
3113
|
+
};
|
|
3114
|
+
var serialized = encode(data);
|
|
3115
|
+
var deserialized = decode(serialized);
|
|
3116
|
+
assert.deepEqual(deserialized, data);
|
|
3117
|
+
var evenBiggerInt = {
|
|
3118
|
+
big: 2n**66n,
|
|
3119
|
+
bigger: 53285732853728573289573289573289573289583725892358732859532n,
|
|
3120
|
+
negBig: -93025879203578903275903285903285903289502n,
|
|
3121
|
+
};
|
|
3122
|
+
var serialized = encode(evenBiggerInt);
|
|
3123
|
+
var deserialized = decode(serialized);
|
|
3124
|
+
assert.deepEqual(deserialized, evenBiggerInt);
|
|
3125
|
+
let encoder = new Encoder({
|
|
3126
|
+
largeBigIntToFloat: true
|
|
3127
|
+
});
|
|
3128
|
+
serialized = encoder.encode(evenBiggerInt);
|
|
3129
|
+
deserialized = decode(serialized);
|
|
3130
|
+
assert.isTrue(deserialized.bigger > 2n**65n);
|
|
3131
|
+
});
|
|
3132
|
+
|
|
3133
|
+
test('buffers', function() {
|
|
3134
|
+
var data = {
|
|
3135
|
+
buffer1: new Uint8Array([2,3,4]),
|
|
3136
|
+
buffer2: new Uint8Array(encode(sampleData))
|
|
3137
|
+
};
|
|
3138
|
+
var serialized = encode(data);
|
|
3139
|
+
var deserialized = decode(serialized);
|
|
3140
|
+
assert.deepEqual(deserialized, data);
|
|
3141
|
+
let encoder = new Encoder({ tagUint8Array: true });
|
|
3142
|
+
serialized = encoder.encode(new Uint8Array([2,3,4]));
|
|
3143
|
+
assert.equal(serialized[0], 0xd8);
|
|
3144
|
+
encoder = new Encoder({ tagUint8Array: false });
|
|
3145
|
+
serialized = encoder.encode(new Uint8Array([2,3,4]));
|
|
3146
|
+
assert.equal(serialized[0], 0x43);
|
|
3147
|
+
});
|
|
3148
|
+
|
|
3149
|
+
test('noteencode test', function() {
|
|
3150
|
+
const data = {
|
|
3151
|
+
foo: 1,
|
|
3152
|
+
bar: [1, 2, 3, 4, 'abc', 'def'],
|
|
3153
|
+
foobar: {
|
|
3154
|
+
foo: true,
|
|
3155
|
+
bar: -2147483649,
|
|
3156
|
+
foobar: {
|
|
3157
|
+
foo: new Uint8Array([1, 2, 3, 4, 5]),
|
|
3158
|
+
bar: 1.5,
|
|
3159
|
+
foobar: [true, false, 'abcdefghijkmonpqrstuvwxyz']
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
};
|
|
3163
|
+
var serialized = encode(data);
|
|
3164
|
+
var deserialized = decode(serialized);
|
|
3165
|
+
var deserialized = decode(serialized);
|
|
3166
|
+
var deserialized = decode(serialized);
|
|
3167
|
+
assert.deepEqual(deserialized, data);
|
|
3168
|
+
});
|
|
3169
|
+
|
|
3170
|
+
test('utf16 causing expansion', function() {
|
|
3171
|
+
this.timeout(10000);
|
|
3172
|
+
let data = {fixstr: 'ᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝ', str8:'ᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝᾐᾑᾒᾓᾔᾕᾖᾗᾘᾙᾚᾛᾜᾝ'};
|
|
3173
|
+
var serialized = encode(data);
|
|
3174
|
+
var deserialized = decode(serialized);
|
|
3175
|
+
assert.deepEqual(deserialized, data);
|
|
3176
|
+
});
|
|
3177
|
+
test('decodeMultiple', () => {
|
|
3178
|
+
let values = decodeMultiple(new Uint8Array([1, 2, 3, 4]));
|
|
3179
|
+
assert.deepEqual(values, [1, 2, 3, 4]);
|
|
3180
|
+
values = [];
|
|
3181
|
+
decodeMultiple(new Uint8Array([1, 2, 3, 4]), value => values.push(value));
|
|
3182
|
+
assert.deepEqual(values, [1, 2, 3, 4]);
|
|
3183
|
+
});
|
|
3184
|
+
test('bad input', function() {
|
|
3185
|
+
let badInput = Buffer.from('7b2273657269616c6e6f223a2265343a30222c226970223a223139322e3136382e312e3335222c226b6579223a226770735f736563726574227d', 'hex');
|
|
3186
|
+
assert.throws(function(){ decode(badInput); }); // should throw, not crash
|
|
3187
|
+
});
|
|
3188
|
+
test('buffer key', function() {
|
|
3189
|
+
let encoder = new Encoder({ mapsAsObjects: false });
|
|
3190
|
+
let test = encoder.decode(Buffer.from('D87982A1446E616D654361626301', 'hex'));
|
|
3191
|
+
console.log(test);
|
|
3192
|
+
});
|
|
3193
|
+
test('encode as iterator', function() {
|
|
3194
|
+
let hasIterables = {
|
|
3195
|
+
a: 1,
|
|
3196
|
+
iterator: (function*() {
|
|
3197
|
+
yield 2;
|
|
3198
|
+
yield {
|
|
3199
|
+
b: (function*() {
|
|
3200
|
+
yield 3;
|
|
3201
|
+
})(),
|
|
3202
|
+
};
|
|
3203
|
+
})()
|
|
3204
|
+
};
|
|
3205
|
+
let encodedIterable = encodeAsIterable(hasIterables);
|
|
3206
|
+
let result = [...encodedIterable];
|
|
3207
|
+
result = Buffer.concat(result);
|
|
3208
|
+
let deserialized = decode(result);
|
|
3209
|
+
const expectedResult = {
|
|
3210
|
+
a: 1,
|
|
3211
|
+
iterator: [2, { b: [3]}]
|
|
3212
|
+
};
|
|
3213
|
+
assert.deepEqual(deserialized, expectedResult);
|
|
3214
|
+
});
|
|
3215
|
+
if (typeof Blob !== 'undefined')
|
|
3216
|
+
test('encode as iterator with async/blob parts', function() {
|
|
3217
|
+
let blob = new Blob([Buffer.from([4,5])]);
|
|
3218
|
+
let hasIterables = {
|
|
3219
|
+
a: 1,
|
|
3220
|
+
iterator: (async function*() {
|
|
3221
|
+
yield 2;
|
|
3222
|
+
yield {
|
|
3223
|
+
b: (function*() {
|
|
3224
|
+
yield 3;
|
|
3225
|
+
})(),
|
|
3226
|
+
};
|
|
3227
|
+
})(),
|
|
3228
|
+
blob
|
|
3229
|
+
};
|
|
3230
|
+
let encodedIterable = encodeAsIterable(hasIterables);
|
|
3231
|
+
let result = [...encodedIterable];
|
|
3232
|
+
assert.equal(result[result.length - 1].constructor, Blob);
|
|
3233
|
+
});
|
|
3234
|
+
if (typeof Blob !== 'undefined')
|
|
3235
|
+
test('encode as async iterator with async/blob parts', async function() {
|
|
3236
|
+
let blob = new Blob([Buffer.from([4, 5])]);
|
|
3237
|
+
let hasIterables = {
|
|
3238
|
+
a: 1,
|
|
3239
|
+
iterator: (async function* () {
|
|
3240
|
+
yield 2;
|
|
3241
|
+
yield {
|
|
3242
|
+
b: (function* () {
|
|
3243
|
+
yield 3;
|
|
3244
|
+
})(),
|
|
3245
|
+
};
|
|
3246
|
+
})(),
|
|
3247
|
+
blob
|
|
3248
|
+
};
|
|
3249
|
+
let encodedIterable = encodeAsAsyncIterable(hasIterables);
|
|
3250
|
+
let result = [];
|
|
3251
|
+
for await (let encodedPart of encodedIterable) {
|
|
3252
|
+
result.push(encodedPart);
|
|
3253
|
+
}
|
|
3254
|
+
let deserialized = decode(Buffer.concat(result));
|
|
3255
|
+
const expectedResult = {
|
|
3256
|
+
a: 1,
|
|
3257
|
+
iterator: [2, { b: [3]}],
|
|
3258
|
+
blob: Buffer.from([4,5]),
|
|
3259
|
+
};
|
|
3260
|
+
assert.deepEqual(deserialized, expectedResult);
|
|
3261
|
+
});
|
|
3262
|
+
test.skip('encode as iterator performance', async function() {
|
|
3263
|
+
function* iterator() {
|
|
3264
|
+
for (let i = 0; i < 1000; i++) {
|
|
3265
|
+
yield {
|
|
3266
|
+
a: 1,
|
|
3267
|
+
b: 'hello, world',
|
|
3268
|
+
c: true,
|
|
3269
|
+
sub: {
|
|
3270
|
+
d: 'inside',
|
|
3271
|
+
e: 3
|
|
3272
|
+
}
|
|
3273
|
+
};
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
let result;
|
|
3277
|
+
let start = performance.now();
|
|
3278
|
+
for (let i = 0; i < 1000; i++) {
|
|
3279
|
+
let encodedIterable = encodeAsIterable(iterator());
|
|
3280
|
+
result = [...encodedIterable];
|
|
3281
|
+
}
|
|
3282
|
+
decode(Buffer.concat(result));
|
|
3283
|
+
console.log(performance.now() - start, result.length);
|
|
3284
|
+
});
|
|
3285
|
+
|
|
3286
|
+
test('little-endian typed array with aligned data', function() {
|
|
3287
|
+
// array[1] { uint32-little-endian-typed-array { bytes <00 00 00 00> } }
|
|
3288
|
+
let data = new Uint8Array([ 129, 216, 70, 68, 0, 0, 0, 0 ]);
|
|
3289
|
+
assert.deepEqual(decode(data), [new Uint32Array([0])]);
|
|
3290
|
+
|
|
3291
|
+
let value = {x: new Float32Array([1, 2, 3])};
|
|
3292
|
+
assert.deepEqual(decode(encode(value)), value);
|
|
3293
|
+
});
|
|
3294
|
+
});
|
|
3295
|
+
suite('CBOR performance tests', function(){
|
|
3296
|
+
test('performance JSON.parse', function() {
|
|
3297
|
+
var data = sampleData;
|
|
3298
|
+
this.timeout(10000);
|
|
3299
|
+
var serialized = JSON.stringify(data);
|
|
3300
|
+
console.log('JSON size', serialized.length);
|
|
3301
|
+
for (var i = 0; i < ITERATIONS; i++) {
|
|
3302
|
+
JSON.parse(serialized);
|
|
3303
|
+
}
|
|
3304
|
+
});
|
|
3305
|
+
test('performance JSON.stringify', function() {
|
|
3306
|
+
var data = sampleData;
|
|
3307
|
+
this.timeout(10000);
|
|
3308
|
+
for (var i = 0; i < ITERATIONS; i++) {
|
|
3309
|
+
JSON.stringify(data);
|
|
3310
|
+
}
|
|
3311
|
+
});
|
|
3312
|
+
test('performance decode', function() {
|
|
3313
|
+
var data = sampleData;
|
|
3314
|
+
this.timeout(10000);
|
|
3315
|
+
let structures = [];
|
|
3316
|
+
var serialized = encode(data);
|
|
3317
|
+
console.log('CBOR size', serialized.length);
|
|
3318
|
+
let encoder = new Encoder({ structures, bundleStrings: true });
|
|
3319
|
+
var serialized = encoder.encode(data);
|
|
3320
|
+
console.log('CBOR w/ record ext size', serialized.length);
|
|
3321
|
+
for (var i = 0; i < ITERATIONS; i++) {
|
|
3322
|
+
encoder.decode(serialized);
|
|
3323
|
+
}
|
|
3324
|
+
});
|
|
3325
|
+
test('performance encode', function() {
|
|
3326
|
+
var data = sampleData;
|
|
3327
|
+
this.timeout(10000);
|
|
3328
|
+
let structures = [];
|
|
3329
|
+
let encoder = new Encoder({ structures, bundleStrings: true });
|
|
3330
|
+
let buffer = typeof Buffer != 'undefined' ? Buffer.alloc(0x10000) : new Uint8Array(0x10000);
|
|
3331
|
+
|
|
3332
|
+
for (var i = 0; i < ITERATIONS; i++) {
|
|
3333
|
+
//serialized = encode(data, { shared: sharedStructure })
|
|
3334
|
+
encoder.useBuffer(buffer);
|
|
3335
|
+
encoder.encode(data);
|
|
3336
|
+
//var serializedGzip = deflateSync(serialized)
|
|
3337
|
+
}
|
|
3338
|
+
//console.log('serialized', serialized.length, global.propertyComparisons)
|
|
3339
|
+
});
|
|
3340
|
+
});
|
|
3341
|
+
|
|
3342
|
+
})(null, module, chai, fs);
|
|
3343
|
+
//# sourceMappingURL=test.js.map
|