jaelis-node 1.9.0 → 2.0.0

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.
Files changed (173) hide show
  1. package/README.md +147 -433
  2. package/bin/jaelis-node.js +79 -504
  3. package/lib/index.js +31 -2740
  4. package/lib/node.js +271 -0
  5. package/lib/rpc.js +315 -0
  6. package/lib/storage.js +198 -0
  7. package/lib/sync.js +366 -0
  8. package/package.json +19 -53
  9. package/config/default.json +0 -74
  10. package/config/mainnet.json +0 -30
  11. package/config/testnet.json +0 -26
  12. package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +0 -454
  13. package/lib/JAELIS-VM/lib/adapters/index.js +0 -411
  14. package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +0 -457
  15. package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +0 -1097
  16. package/lib/JAELIS-VM/lib/execution/engine.js +0 -1183
  17. package/lib/JAELIS-VM/lib/index.js +0 -440
  18. package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +0 -543
  19. package/lib/JAELIS-VM/lib/serialization/serializer.js +0 -819
  20. package/lib/JAELIS-VM/lib/state/state-manager.js +0 -1116
  21. package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +0 -1222
  22. package/lib/JAELIS-VM/lib/unified/cross-chain-deploy.js +0 -1678
  23. package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +0 -836
  24. package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +0 -1127
  25. package/lib/JAELIS-VM/lib/unified/index.js +0 -456
  26. package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +0 -1150
  27. package/lib/JAELIS-VM/lib/unified/unified-compiler.js +0 -1350
  28. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +0 -12
  29. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +0 -17
  30. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +0 -28
  31. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +0 -12
  32. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +0 -17
  33. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +0 -28
  34. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  35. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  36. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  37. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  38. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  39. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  40. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  41. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  42. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  43. package/lib/JAELIS-VM/node_modules/.package-lock.json +0 -127
  44. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +0 -1
  45. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
  46. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
  47. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
  48. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +0 -17
  49. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +0 -1
  50. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
  51. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
  52. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
  53. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +0 -17
  54. package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +0 -21
  55. package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +0 -5
  56. package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +0 -11
  57. package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +0 -60
  58. package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +0 -1
  59. package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +0 -50
  60. package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +0 -198
  61. package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +0 -21
  62. package/lib/JAELIS-VM/node_modules/cbor-x/README.md +0 -380
  63. package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +0 -11
  64. package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +0 -73
  65. package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +0 -11
  66. package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +0 -2
  67. package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +0 -1300
  68. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +0 -1244
  69. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +0 -1
  70. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +0 -2509
  71. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +0 -1
  72. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +0 -2
  73. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +0 -1
  74. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +0 -2508
  75. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +0 -1
  76. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +0 -2
  77. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +0 -1
  78. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +0 -2629
  79. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +0 -1
  80. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +0 -3343
  81. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +0 -1
  82. package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +0 -1
  83. package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +0 -1231
  84. package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +0 -79
  85. package/lib/JAELIS-VM/node_modules/cbor-x/index.js +0 -3
  86. package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +0 -85
  87. package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +0 -24
  88. package/lib/JAELIS-VM/node_modules/cbor-x/package.json +0 -94
  89. package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +0 -88
  90. package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +0 -61
  91. package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +0 -19
  92. package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +0 -201
  93. package/lib/JAELIS-VM/node_modules/detect-libc/README.md +0 -163
  94. package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +0 -14
  95. package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +0 -313
  96. package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +0 -39
  97. package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +0 -51
  98. package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +0 -24
  99. package/lib/JAELIS-VM/node_modules/detect-libc/package.json +0 -44
  100. package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +0 -21
  101. package/lib/JAELIS-VM/node_modules/msgpackr/README.md +0 -372
  102. package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +0 -11
  103. package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +0 -67
  104. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +0 -2407
  105. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +0 -1
  106. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +0 -2
  107. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +0 -1
  108. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +0 -2406
  109. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +0 -1
  110. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +0 -2
  111. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +0 -1
  112. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +0 -3320
  113. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +0 -1
  114. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +0 -4540
  115. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +0 -1
  116. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +0 -1250
  117. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +0 -1
  118. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +0 -91
  119. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +0 -91
  120. package/lib/JAELIS-VM/node_modules/msgpackr/index.js +0 -5
  121. package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +0 -87
  122. package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +0 -25
  123. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +0 -1
  124. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +0 -1
  125. package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +0 -1141
  126. package/lib/JAELIS-VM/node_modules/msgpackr/package.json +0 -104
  127. package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +0 -88
  128. package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +0 -57
  129. package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +0 -815
  130. package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +0 -3
  131. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +0 -2
  132. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +0 -2
  133. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +0 -1221
  134. package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +0 -21
  135. package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +0 -5
  136. package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +0 -13
  137. package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +0 -63
  138. package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +0 -1
  139. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  140. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  141. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  142. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  143. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  144. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  145. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  146. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  147. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  148. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  149. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  150. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +0 -82
  151. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  152. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +0 -6
  153. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +0 -236
  154. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  155. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  156. package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +0 -50
  157. package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +0 -274
  158. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  159. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  160. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +0 -77
  161. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  162. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +0 -224
  163. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  164. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  165. package/lib/JAELIS-VM/package-lock.json +0 -284
  166. package/lib/JAELIS-VM/package.json +0 -38
  167. package/lib/JAELIS-VM/test/comprehensive.test.js +0 -267
  168. package/lib/JAELIS-VM/test/cross-chain-test.js +0 -470
  169. package/lib/JAELIS-VM/test/unified-vm-test.js +0 -459
  170. package/lib/JAELIS-VM/test/unified.test.js +0 -166
  171. package/lib/JAELIS-VM/test/vm.test.js +0 -599
  172. package/lib/settlement-server.js +0 -999
  173. package/lib/vm/index.js +0 -397
@@ -1,1244 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CBOR = {}));
5
- })(this, (function (exports) { 'use strict';
6
-
7
- let decoder;
8
- try {
9
- decoder = new TextDecoder();
10
- } catch(error) {}
11
- let src;
12
- let srcEnd;
13
- let position = 0;
14
- const EMPTY_ARRAY = [];
15
- const LEGACY_RECORD_INLINE_ID = 105;
16
- const RECORD_DEFINITIONS_ID = 0xdffe;
17
- const RECORD_INLINE_ID = 0xdfff; // temporary first-come first-serve tag // proposed tag: 0x7265 // 're'
18
- const BUNDLED_STRINGS_ID = 0xdff9;
19
- const PACKED_REFERENCE_TAG_ID = 6;
20
- const STOP_CODE = {};
21
- let maxArraySize = 112810000; // This is the maximum array size in V8. We would potentially detect and set it higher
22
- // for JSC, but this is pretty large and should be sufficient for most use cases
23
- let maxMapSize = 16810000; // JavaScript has a fixed maximum map size of about 16710000, but JS itself enforces this,
24
- // many keys in an object, so also probably a reasonable choice there.
25
- let strings = EMPTY_ARRAY;
26
- let stringPosition = 0;
27
- let currentDecoder = {};
28
- let currentStructures;
29
- let srcString;
30
- let srcStringStart = 0;
31
- let srcStringEnd = 0;
32
- let bundledStrings;
33
- let referenceMap;
34
- let currentExtensions = [];
35
- let currentExtensionRanges = [];
36
- let packedValues;
37
- let dataView;
38
- let restoreMapsAsObject;
39
- let defaultOptions = {
40
- useRecords: false,
41
- mapsAsObjects: true
42
- };
43
- let sequentialMode = false;
44
- let inlineObjectReadThreshold = 2;
45
- var BlockedFunction; // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
46
- // no-eval build
47
- try {
48
- new BlockedFunction ('');
49
- } catch(error) {
50
- // if eval variants are not supported, do not create inline object readers ever
51
- inlineObjectReadThreshold = Infinity;
52
- }
53
-
54
-
55
-
56
- class Decoder {
57
- constructor(options) {
58
- if (options) {
59
- if ((options.keyMap || options._keyMap) && !options.useRecords) {
60
- options.useRecords = false;
61
- options.mapsAsObjects = true;
62
- }
63
- if (options.useRecords === false && options.mapsAsObjects === undefined)
64
- options.mapsAsObjects = true;
65
- if (options.getStructures)
66
- options.getShared = options.getStructures;
67
- if (options.getShared && !options.structures)
68
- (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
69
- if (options.keyMap) {
70
- this.mapKey = new Map();
71
- for (let [k,v] of Object.entries(options.keyMap)) this.mapKey.set(v,k);
72
- }
73
- }
74
- Object.assign(this, options);
75
- }
76
- /*
77
- decodeKey(key) {
78
- return this.keyMap
79
- ? Object.keys(this.keyMap)[Object.values(this.keyMap).indexOf(key)] || key
80
- : key
81
- }
82
- */
83
- decodeKey(key) {
84
- return this.keyMap ? this.mapKey.get(key) || key : key
85
- }
86
-
87
- encodeKey(key) {
88
- return this.keyMap && this.keyMap.hasOwnProperty(key) ? this.keyMap[key] : key
89
- }
90
-
91
- encodeKeys(rec) {
92
- if (!this._keyMap) return rec
93
- let map = new Map();
94
- for (let [k,v] of Object.entries(rec)) map.set((this._keyMap.hasOwnProperty(k) ? this._keyMap[k] : k), v);
95
- return map
96
- }
97
-
98
- decodeKeys(map) {
99
- if (!this._keyMap || map.constructor.name != 'Map') return map
100
- if (!this._mapKey) {
101
- this._mapKey = new Map();
102
- for (let [k,v] of Object.entries(this._keyMap)) this._mapKey.set(v,k);
103
- }
104
- let res = {};
105
- //map.forEach((v,k) => res[Object.keys(this._keyMap)[Object.values(this._keyMap).indexOf(k)] || k] = v)
106
- map.forEach((v,k) => res[safeKey(this._mapKey.has(k) ? this._mapKey.get(k) : k)] = v);
107
- return res
108
- }
109
-
110
- mapDecode(source, end) {
111
-
112
- let res = this.decode(source);
113
- if (this._keyMap) {
114
- //Experiemntal support for Optimised KeyMap decoding
115
- switch (res.constructor.name) {
116
- case 'Array': return res.map(r => this.decodeKeys(r))
117
- //case 'Map': return this.decodeKeys(res)
118
- }
119
- }
120
- return res
121
- }
122
-
123
- decode(source, end) {
124
- if (src) {
125
- // re-entrant execution, save the state and restore it after we do this decode
126
- return saveState(() => {
127
- clearSource();
128
- return this ? this.decode(source, end) : Decoder.prototype.decode.call(defaultOptions, source, end)
129
- })
130
- }
131
- srcEnd = end > -1 ? end : source.length;
132
- position = 0;
133
- stringPosition = 0;
134
- srcStringEnd = 0;
135
- srcString = null;
136
- strings = EMPTY_ARRAY;
137
- bundledStrings = null;
138
- src = source;
139
- // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
140
- // technique for getting data from a database where it can be copied into an existing buffer instead of creating
141
- // new ones
142
- try {
143
- dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
144
- } catch(error) {
145
- // if it doesn't have a buffer, maybe it is the wrong type of object
146
- src = null;
147
- if (source instanceof Uint8Array)
148
- throw error
149
- throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
150
- }
151
- if (this instanceof Decoder) {
152
- currentDecoder = this;
153
- packedValues = this.sharedValues &&
154
- (this.pack ? new Array(this.maxPrivatePackedValues || 16).concat(this.sharedValues) :
155
- this.sharedValues);
156
- if (this.structures) {
157
- currentStructures = this.structures;
158
- return checkedRead()
159
- } else if (!currentStructures || currentStructures.length > 0) {
160
- currentStructures = [];
161
- }
162
- } else {
163
- currentDecoder = defaultOptions;
164
- if (!currentStructures || currentStructures.length > 0)
165
- currentStructures = [];
166
- packedValues = null;
167
- }
168
- return checkedRead()
169
- }
170
- decodeMultiple(source, forEach) {
171
- let values, lastPosition = 0;
172
- try {
173
- let size = source.length;
174
- sequentialMode = true;
175
- let value = this ? this.decode(source, size) : defaultDecoder.decode(source, size);
176
- if (forEach) {
177
- if (forEach(value) === false) {
178
- return
179
- }
180
- while(position < size) {
181
- lastPosition = position;
182
- if (forEach(checkedRead()) === false) {
183
- return
184
- }
185
- }
186
- }
187
- else {
188
- values = [ value ];
189
- while(position < size) {
190
- lastPosition = position;
191
- values.push(checkedRead());
192
- }
193
- return values
194
- }
195
- } catch(error) {
196
- error.lastPosition = lastPosition;
197
- error.values = values;
198
- throw error
199
- } finally {
200
- sequentialMode = false;
201
- clearSource();
202
- }
203
- }
204
- }
205
- function getPosition() {
206
- return position
207
- }
208
- function checkedRead() {
209
- try {
210
- let result = read();
211
- if (bundledStrings) {
212
- if (position >= bundledStrings.postBundlePosition) {
213
- let error = new Error('Unexpected bundle position');
214
- error.incomplete = true;
215
- throw error
216
- }
217
- // bundled strings to skip past
218
- position = bundledStrings.postBundlePosition;
219
- bundledStrings = null;
220
- }
221
-
222
- if (position == srcEnd) {
223
- // finished reading this source, cleanup references
224
- currentStructures = null;
225
- src = null;
226
- if (referenceMap)
227
- referenceMap = null;
228
- } else if (position > srcEnd) {
229
- // over read
230
- let error = new Error('Unexpected end of CBOR data');
231
- error.incomplete = true;
232
- throw error
233
- } else if (!sequentialMode) {
234
- throw new Error('Data read, but end of buffer not reached')
235
- }
236
- // else more to read, but we are reading sequentially, so don't clear source yet
237
- return result
238
- } catch(error) {
239
- clearSource();
240
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer')) {
241
- error.incomplete = true;
242
- }
243
- throw error
244
- }
245
- }
246
-
247
- function read() {
248
- let token = src[position++];
249
- let majorType = token >> 5;
250
- token = token & 0x1f;
251
- if (token > 0x17) {
252
- switch (token) {
253
- case 0x18:
254
- token = src[position++];
255
- break
256
- case 0x19:
257
- if (majorType == 7) {
258
- return getFloat16()
259
- }
260
- token = dataView.getUint16(position);
261
- position += 2;
262
- break
263
- case 0x1a:
264
- if (majorType == 7) {
265
- let value = dataView.getFloat32(position);
266
- if (currentDecoder.useFloat32 > 2) {
267
- // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
268
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
269
- position += 4;
270
- return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
271
- }
272
- position += 4;
273
- return value
274
- }
275
- token = dataView.getUint32(position);
276
- position += 4;
277
- break
278
- case 0x1b:
279
- if (majorType == 7) {
280
- let value = dataView.getFloat64(position);
281
- position += 8;
282
- return value
283
- }
284
- if (majorType > 1) {
285
- if (dataView.getUint32(position) > 0)
286
- throw new Error('JavaScript does not support arrays, maps, or strings with length over 4294967295')
287
- token = dataView.getUint32(position + 4);
288
- } else if (currentDecoder.int64AsNumber) {
289
- token = dataView.getUint32(position) * 0x100000000;
290
- token += dataView.getUint32(position + 4);
291
- } else
292
- token = dataView.getBigUint64(position);
293
- position += 8;
294
- break
295
- case 0x1f:
296
- // indefinite length
297
- switch(majorType) {
298
- case 2: // byte string
299
- case 3: // text string
300
- throw new Error('Indefinite length not supported for byte or text strings')
301
- case 4: // array
302
- let array = [];
303
- let value, i = 0;
304
- while ((value = read()) != STOP_CODE) {
305
- if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
306
- array[i++] = value;
307
- }
308
- return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
309
- case 5: // map
310
- let key;
311
- if (currentDecoder.mapsAsObjects) {
312
- let object = {};
313
- let i = 0;
314
- if (currentDecoder.keyMap) {
315
- while((key = read()) != STOP_CODE) {
316
- if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
317
- object[safeKey(currentDecoder.decodeKey(key))] = read();
318
- }
319
- }
320
- else {
321
- while ((key = read()) != STOP_CODE) {
322
- if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
323
- object[safeKey(key)] = read();
324
- }
325
- }
326
- return object
327
- } else {
328
- if (restoreMapsAsObject) {
329
- currentDecoder.mapsAsObjects = true;
330
- restoreMapsAsObject = false;
331
- }
332
- let map = new Map();
333
- if (currentDecoder.keyMap) {
334
- let i = 0;
335
- while((key = read()) != STOP_CODE) {
336
- if (i++ >= maxMapSize) {
337
- throw new Error(`Map size exceeds ${maxMapSize}`);
338
- }
339
- map.set(currentDecoder.decodeKey(key), read());
340
- }
341
- }
342
- else {
343
- let i = 0;
344
- while ((key = read()) != STOP_CODE) {
345
- if (i++ >= maxMapSize) {
346
- throw new Error(`Map size exceeds ${maxMapSize}`);
347
- }
348
- map.set(key, read());
349
- }
350
- }
351
- return map
352
- }
353
- case 7:
354
- return STOP_CODE
355
- default:
356
- throw new Error('Invalid major type for indefinite length ' + majorType)
357
- }
358
- default:
359
- throw new Error('Unknown token ' + token)
360
- }
361
- }
362
- switch (majorType) {
363
- case 0: // positive int
364
- return token
365
- case 1: // negative int
366
- return ~token
367
- case 2: // buffer
368
- return readBin(token)
369
- case 3: // string
370
- if (srcStringEnd >= position) {
371
- return srcString.slice(position - srcStringStart, (position += token) - srcStringStart)
372
- }
373
- if (srcStringEnd == 0 && srcEnd < 140 && token < 32) {
374
- // for small blocks, avoiding the overhead of the extract call is helpful
375
- let string = token < 16 ? shortStringInJS(token) : longStringInJS(token);
376
- if (string != null)
377
- return string
378
- }
379
- return readFixedString(token)
380
- case 4: // array
381
- if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
382
- let array = new Array(token);
383
- //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
384
- //else
385
- for (let i = 0; i < token; i++) array[i] = read();
386
- return array
387
- case 5: // map
388
- if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
389
- if (currentDecoder.mapsAsObjects) {
390
- let object = {};
391
- if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
392
- else for (let i = 0; i < token; i++) object[safeKey(read())] = read();
393
- return object
394
- } else {
395
- if (restoreMapsAsObject) {
396
- currentDecoder.mapsAsObjects = true;
397
- restoreMapsAsObject = false;
398
- }
399
- let map = new Map();
400
- if (currentDecoder.keyMap) for (let i = 0; i < token; i++) map.set(currentDecoder.decodeKey(read()),read());
401
- else for (let i = 0; i < token; i++) map.set(read(), read());
402
- return map
403
- }
404
- case 6: // extension
405
- if (token >= BUNDLED_STRINGS_ID) {
406
- let structure = currentStructures[token & 0x1fff]; // check record structures first
407
- // 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))
408
- if (structure) {
409
- if (!structure.read) structure.read = createStructureReader(structure);
410
- return structure.read()
411
- }
412
- if (token < 0x10000) {
413
- if (token == RECORD_INLINE_ID) { // we do a special check for this so that we can keep the
414
- // currentExtensions as densely stored array (v8 stores arrays densely under about 3000 elements)
415
- let length = readJustLength();
416
- let id = read();
417
- let structure = read();
418
- recordDefinition(id, structure);
419
- let object = {};
420
- if (currentDecoder.keyMap) for (let i = 2; i < length; i++) {
421
- let key = currentDecoder.decodeKey(structure[i - 2]);
422
- object[safeKey(key)] = read();
423
- }
424
- else for (let i = 2; i < length; i++) {
425
- let key = structure[i - 2];
426
- object[safeKey(key)] = read();
427
- }
428
- return object
429
- }
430
- else if (token == RECORD_DEFINITIONS_ID) {
431
- let length = readJustLength();
432
- let id = read();
433
- for (let i = 2; i < length; i++) {
434
- recordDefinition(id++, read());
435
- }
436
- return read()
437
- } else if (token == BUNDLED_STRINGS_ID) {
438
- return readBundleExt()
439
- }
440
- if (currentDecoder.getShared) {
441
- loadShared();
442
- structure = currentStructures[token & 0x1fff];
443
- if (structure) {
444
- if (!structure.read)
445
- structure.read = createStructureReader(structure);
446
- return structure.read()
447
- }
448
- }
449
- }
450
- }
451
- let extension = currentExtensions[token];
452
- if (extension) {
453
- if (extension.handlesRead)
454
- return extension(read)
455
- else
456
- return extension(read())
457
- } else {
458
- let input = read();
459
- for (let i = 0; i < currentExtensionRanges.length; i++) {
460
- let value = currentExtensionRanges[i](token, input);
461
- if (value !== undefined)
462
- return value
463
- }
464
- return new Tag(input, token)
465
- }
466
- case 7: // fixed value
467
- switch (token) {
468
- case 0x14: return false
469
- case 0x15: return true
470
- case 0x16: return null
471
- case 0x17: return; // undefined
472
- case 0x1f:
473
- default:
474
- let packedValue = (packedValues || getPackedValues())[token];
475
- if (packedValue !== undefined)
476
- return packedValue
477
- throw new Error('Unknown token ' + token)
478
- }
479
- default: // negative int
480
- if (isNaN(token)) {
481
- let error = new Error('Unexpected end of CBOR data');
482
- error.incomplete = true;
483
- throw error
484
- }
485
- throw new Error('Unknown CBOR token ' + token)
486
- }
487
- }
488
- const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
489
- function createStructureReader(structure) {
490
- if (!structure) throw new Error('Structure is required in record definition');
491
- function readObject() {
492
- // get the array size from the header
493
- let length = src[position++];
494
- //let majorType = token >> 5
495
- length = length & 0x1f;
496
- if (length > 0x17) {
497
- switch (length) {
498
- case 0x18:
499
- length = src[position++];
500
- break
501
- case 0x19:
502
- length = dataView.getUint16(position);
503
- position += 2;
504
- break
505
- case 0x1a:
506
- length = dataView.getUint32(position);
507
- position += 4;
508
- break
509
- default:
510
- throw new Error('Expected array header, but got ' + src[position - 1])
511
- }
512
- }
513
- // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
514
- let compiledReader = this.compiledReader; // first look to see if we have the fast compiled function
515
- while(compiledReader) {
516
- // we have a fast compiled object literal reader
517
- if (compiledReader.propertyCount === length)
518
- return compiledReader(read) // with the right length, so we use it
519
- compiledReader = compiledReader.next; // see if there is another reader with the right length
520
- }
521
- if (this.slowReads++ >= inlineObjectReadThreshold) { // create a fast compiled reader
522
- let array = this.length == length ? this : this.slice(0, length);
523
- compiledReader = currentDecoder.keyMap
524
- ? new BlockedFunction ('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? safeKey(k) + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
525
- : new BlockedFunction ('r', 'return {' + array.map(key => validName.test(key) ? safeKey(key) + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}');
526
- if (this.compiledReader)
527
- 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
528
- compiledReader.propertyCount = length;
529
- this.compiledReader = compiledReader;
530
- return compiledReader(read)
531
- }
532
- let object = {};
533
- if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[safeKey(currentDecoder.decodeKey(this[i]))] = read();
534
- else for (let i = 0; i < length; i++) {
535
- object[safeKey(this[i])] = read();
536
- }
537
- return object
538
- }
539
- structure.slowReads = 0;
540
- return readObject
541
- }
542
-
543
- function safeKey(key) {
544
- // protect against prototype pollution
545
- if (typeof key === 'string') return key === '__proto__' ? '__proto_' : key
546
- if (typeof key === 'number' || typeof key === 'boolean' || typeof key === 'bigint') return key.toString();
547
- if (key == null) return key + '';
548
- // protect against expensive (DoS) string conversions
549
- throw new Error('Invalid property name type ' + typeof key);
550
- }
551
-
552
- let readFixedString = readStringJS;
553
-
554
- exports.isNativeAccelerationEnabled = false;
555
- function setExtractor(extractStrings) {
556
- exports.isNativeAccelerationEnabled = true;
557
- readFixedString = readString();
558
- function readString(headerLength) {
559
- return function readString(length) {
560
- let string = strings[stringPosition++];
561
- if (string == null) {
562
- if (bundledStrings)
563
- return readStringJS(length)
564
- let extraction = extractStrings(position, srcEnd, length, src);
565
- if (typeof extraction == 'string') {
566
- string = extraction;
567
- strings = EMPTY_ARRAY;
568
- } else {
569
- strings = extraction;
570
- stringPosition = 1;
571
- 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
572
- string = strings[0];
573
- if (string === undefined)
574
- throw new Error('Unexpected end of buffer')
575
- }
576
- }
577
- let srcStringLength = string.length;
578
- if (srcStringLength <= length) {
579
- position += length;
580
- return string
581
- }
582
- srcString = string;
583
- srcStringStart = position;
584
- srcStringEnd = position + srcStringLength;
585
- position += length;
586
- return string.slice(0, length) // we know we just want the beginning
587
- }
588
- }
589
- }
590
- function readStringJS(length) {
591
- let result;
592
- if (length < 16) {
593
- if (result = shortStringInJS(length))
594
- return result
595
- }
596
- if (length > 64 && decoder)
597
- return decoder.decode(src.subarray(position, position += length))
598
- const end = position + length;
599
- const units = [];
600
- result = '';
601
- while (position < end) {
602
- const byte1 = src[position++];
603
- if ((byte1 & 0x80) === 0) {
604
- // 1 byte
605
- units.push(byte1);
606
- } else if ((byte1 & 0xe0) === 0xc0) {
607
- // 2 bytes
608
- const byte2 = src[position++] & 0x3f;
609
- units.push(((byte1 & 0x1f) << 6) | byte2);
610
- } else if ((byte1 & 0xf0) === 0xe0) {
611
- // 3 bytes
612
- const byte2 = src[position++] & 0x3f;
613
- const byte3 = src[position++] & 0x3f;
614
- units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
615
- } else if ((byte1 & 0xf8) === 0xf0) {
616
- // 4 bytes
617
- const byte2 = src[position++] & 0x3f;
618
- const byte3 = src[position++] & 0x3f;
619
- const byte4 = src[position++] & 0x3f;
620
- let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
621
- if (unit > 0xffff) {
622
- unit -= 0x10000;
623
- units.push(((unit >>> 10) & 0x3ff) | 0xd800);
624
- unit = 0xdc00 | (unit & 0x3ff);
625
- }
626
- units.push(unit);
627
- } else {
628
- units.push(byte1);
629
- }
630
-
631
- if (units.length >= 0x1000) {
632
- result += fromCharCode.apply(String, units);
633
- units.length = 0;
634
- }
635
- }
636
-
637
- if (units.length > 0) {
638
- result += fromCharCode.apply(String, units);
639
- }
640
-
641
- return result
642
- }
643
- let fromCharCode = String.fromCharCode;
644
- function longStringInJS(length) {
645
- let start = position;
646
- let bytes = new Array(length);
647
- for (let i = 0; i < length; i++) {
648
- const byte = src[position++];
649
- if ((byte & 0x80) > 0) {
650
- position = start;
651
- return
652
- }
653
- bytes[i] = byte;
654
- }
655
- return fromCharCode.apply(String, bytes)
656
- }
657
- function shortStringInJS(length) {
658
- if (length < 4) {
659
- if (length < 2) {
660
- if (length === 0)
661
- return ''
662
- else {
663
- let a = src[position++];
664
- if ((a & 0x80) > 1) {
665
- position -= 1;
666
- return
667
- }
668
- return fromCharCode(a)
669
- }
670
- } else {
671
- let a = src[position++];
672
- let b = src[position++];
673
- if ((a & 0x80) > 0 || (b & 0x80) > 0) {
674
- position -= 2;
675
- return
676
- }
677
- if (length < 3)
678
- return fromCharCode(a, b)
679
- let c = src[position++];
680
- if ((c & 0x80) > 0) {
681
- position -= 3;
682
- return
683
- }
684
- return fromCharCode(a, b, c)
685
- }
686
- } else {
687
- let a = src[position++];
688
- let b = src[position++];
689
- let c = src[position++];
690
- let d = src[position++];
691
- if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
692
- position -= 4;
693
- return
694
- }
695
- if (length < 6) {
696
- if (length === 4)
697
- return fromCharCode(a, b, c, d)
698
- else {
699
- let e = src[position++];
700
- if ((e & 0x80) > 0) {
701
- position -= 5;
702
- return
703
- }
704
- return fromCharCode(a, b, c, d, e)
705
- }
706
- } else if (length < 8) {
707
- let e = src[position++];
708
- let f = src[position++];
709
- if ((e & 0x80) > 0 || (f & 0x80) > 0) {
710
- position -= 6;
711
- return
712
- }
713
- if (length < 7)
714
- return fromCharCode(a, b, c, d, e, f)
715
- let g = src[position++];
716
- if ((g & 0x80) > 0) {
717
- position -= 7;
718
- return
719
- }
720
- return fromCharCode(a, b, c, d, e, f, g)
721
- } else {
722
- let e = src[position++];
723
- let f = src[position++];
724
- let g = src[position++];
725
- let h = src[position++];
726
- if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
727
- position -= 8;
728
- return
729
- }
730
- if (length < 10) {
731
- if (length === 8)
732
- return fromCharCode(a, b, c, d, e, f, g, h)
733
- else {
734
- let i = src[position++];
735
- if ((i & 0x80) > 0) {
736
- position -= 9;
737
- return
738
- }
739
- return fromCharCode(a, b, c, d, e, f, g, h, i)
740
- }
741
- } else if (length < 12) {
742
- let i = src[position++];
743
- let j = src[position++];
744
- if ((i & 0x80) > 0 || (j & 0x80) > 0) {
745
- position -= 10;
746
- return
747
- }
748
- if (length < 11)
749
- return fromCharCode(a, b, c, d, e, f, g, h, i, j)
750
- let k = src[position++];
751
- if ((k & 0x80) > 0) {
752
- position -= 11;
753
- return
754
- }
755
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
756
- } else {
757
- let i = src[position++];
758
- let j = src[position++];
759
- let k = src[position++];
760
- let l = src[position++];
761
- if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
762
- position -= 12;
763
- return
764
- }
765
- if (length < 14) {
766
- if (length === 12)
767
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
768
- else {
769
- let m = src[position++];
770
- if ((m & 0x80) > 0) {
771
- position -= 13;
772
- return
773
- }
774
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
775
- }
776
- } else {
777
- let m = src[position++];
778
- let n = src[position++];
779
- if ((m & 0x80) > 0 || (n & 0x80) > 0) {
780
- position -= 14;
781
- return
782
- }
783
- if (length < 15)
784
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
785
- let o = src[position++];
786
- if ((o & 0x80) > 0) {
787
- position -= 15;
788
- return
789
- }
790
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
791
- }
792
- }
793
- }
794
- }
795
- }
796
-
797
- function readBin(length) {
798
- return currentDecoder.copyBuffers ?
799
- // specifically use the copying slice (not the node one)
800
- Uint8Array.prototype.slice.call(src, position, position += length) :
801
- src.subarray(position, position += length)
802
- }
803
- let f32Array = new Float32Array(1);
804
- let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
805
- function getFloat16() {
806
- let byte0 = src[position++];
807
- let byte1 = src[position++];
808
- let exponent = (byte0 & 0x7f) >> 2;
809
- if (exponent === 0x1f) { // specials
810
- if (byte1 || (byte0 & 3))
811
- return NaN;
812
- return (byte0 & 0x80) ? -Infinity : Infinity;
813
- }
814
- if (exponent === 0) { // sub-normals
815
- // significand with 10 fractional bits and divided by 2^14
816
- let abs = (((byte0 & 3) << 8) | byte1) / (1 << 24);
817
- return (byte0 & 0x80) ? -abs : abs
818
- }
819
-
820
- u8Array[3] = (byte0 & 0x80) | // sign bit
821
- ((exponent >> 1) + 56); // 4 of 5 of the exponent bits, re-offset-ed
822
- u8Array[2] = ((byte0 & 7) << 5) | // last exponent bit and first two mantissa bits
823
- (byte1 >> 3); // next 5 bits of mantissa
824
- u8Array[1] = byte1 << 5; // last three bits of mantissa
825
- u8Array[0] = 0;
826
- return f32Array[0];
827
- }
828
-
829
- new Array(4096);
830
-
831
- class Tag {
832
- constructor(value, tag) {
833
- this.value = value;
834
- this.tag = tag;
835
- }
836
- }
837
-
838
- currentExtensions[0] = (dateString) => {
839
- // string date extension
840
- return new Date(dateString)
841
- };
842
-
843
- currentExtensions[1] = (epochSec) => {
844
- // numeric date extension
845
- return new Date(Math.round(epochSec * 1000))
846
- };
847
-
848
- currentExtensions[2] = (buffer) => {
849
- // bigint extension
850
- let value = BigInt(0);
851
- for (let i = 0, l = buffer.byteLength; i < l; i++) {
852
- value = BigInt(buffer[i]) + (value << BigInt(8));
853
- }
854
- return value
855
- };
856
-
857
- currentExtensions[3] = (buffer) => {
858
- // negative bigint extension
859
- return BigInt(-1) - currentExtensions[2](buffer)
860
- };
861
- currentExtensions[4] = (fraction) => {
862
- // best to reparse to maintain accuracy
863
- return +(fraction[1] + 'e' + fraction[0])
864
- };
865
-
866
- currentExtensions[5] = (fraction) => {
867
- // probably not sufficiently accurate
868
- return fraction[1] * Math.exp(fraction[0] * Math.log(2))
869
- };
870
-
871
- // the registration of the record definition extension
872
- const recordDefinition = (id, structure) => {
873
- id = id - 0xe000;
874
- let existingStructure = currentStructures[id];
875
- if (existingStructure && existingStructure.isShared) {
876
- (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
877
- }
878
- currentStructures[id] = structure;
879
-
880
- structure.read = createStructureReader(structure);
881
- };
882
- currentExtensions[LEGACY_RECORD_INLINE_ID] = (data) => {
883
- let length = data.length;
884
- let structure = data[1];
885
- recordDefinition(data[0], structure);
886
- let object = {};
887
- for (let i = 2; i < length; i++) {
888
- let key = structure[i - 2];
889
- object[safeKey(key)] = data[i];
890
- }
891
- return object
892
- };
893
- currentExtensions[14] = (value) => {
894
- if (bundledStrings)
895
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 += value)
896
- return new Tag(value, 14)
897
- };
898
- currentExtensions[15] = (value) => {
899
- if (bundledStrings)
900
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
901
- return new Tag(value, 15)
902
- };
903
- let glbl = { Error, RegExp };
904
- currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
905
- return (glbl[data[0]] || Error)(data[1], data[2])
906
- };
907
- const packedTable = (read) => {
908
- if (src[position++] != 0x84) {
909
- let error = new Error('Packed values structure must be followed by a 4 element array');
910
- if (src.length < position)
911
- error.incomplete = true;
912
- throw error
913
- }
914
- let newPackedValues = read(); // packed values
915
- if (!newPackedValues || !newPackedValues.length) {
916
- let error = new Error('Packed values structure must be followed by a 4 element array');
917
- error.incomplete = true;
918
- throw error
919
- }
920
- packedValues = packedValues ? newPackedValues.concat(packedValues.slice(newPackedValues.length)) : newPackedValues;
921
- packedValues.prefixes = read();
922
- packedValues.suffixes = read();
923
- return read() // read the rump
924
- };
925
- packedTable.handlesRead = true;
926
- currentExtensions[51] = packedTable;
927
-
928
- currentExtensions[PACKED_REFERENCE_TAG_ID] = (data) => { // packed reference
929
- if (!packedValues) {
930
- if (currentDecoder.getShared)
931
- loadShared();
932
- else
933
- return new Tag(data, PACKED_REFERENCE_TAG_ID)
934
- }
935
- if (typeof data == 'number')
936
- return packedValues[16 + (data >= 0 ? 2 * data : (-2 * data - 1))]
937
- let error = new Error('No support for non-integer packed references yet');
938
- if (data === undefined)
939
- error.incomplete = true;
940
- throw error
941
- };
942
-
943
- // The following code is an incomplete implementation of http://cbor.schmorp.de/stringref
944
- // the real thing would need to implemennt more logic to populate the stringRefs table and
945
- // maintain a stack of stringRef "namespaces".
946
- //
947
- // currentExtensions[25] = (id) => {
948
- // return stringRefs[id]
949
- // }
950
- // currentExtensions[256] = (read) => {
951
- // stringRefs = []
952
- // try {
953
- // return read()
954
- // } finally {
955
- // stringRefs = null
956
- // }
957
- // }
958
- // currentExtensions[256].handlesRead = true
959
-
960
- currentExtensions[28] = (read) => {
961
- // shareable http://cbor.schmorp.de/value-sharing (for structured clones)
962
- if (!referenceMap) {
963
- referenceMap = new Map();
964
- referenceMap.id = 0;
965
- }
966
- let id = referenceMap.id++;
967
- let startingPosition = position;
968
- let token = src[position];
969
- let target;
970
- // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
971
- // ahead past references to record structure definitions
972
- if ((token >> 5) == 4)
973
- target = [];
974
- else
975
- target = {};
976
-
977
- let refEntry = { target }; // a placeholder object
978
- referenceMap.set(id, refEntry);
979
- let targetProperties = read(); // read the next value as the target object to id
980
- if (refEntry.used) {// there is a cycle, so we have to assign properties to original target
981
- if (Object.getPrototypeOf(target) !== Object.getPrototypeOf(targetProperties)) {
982
- // this means that the returned target does not match the targetProperties, so we need rerun the read to
983
- // have the correctly create instance be assigned as a reference, then we do the copy the properties back to the
984
- // target
985
- // reset the position so that the read can be repeated
986
- position = startingPosition;
987
- // the returned instance is our new target for references
988
- target = targetProperties;
989
- referenceMap.set(id, { target });
990
- targetProperties = read();
991
- }
992
- return Object.assign(target, targetProperties)
993
- }
994
- refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
995
- return targetProperties // no cycle, can just use the returned read object
996
- };
997
- currentExtensions[28].handlesRead = true;
998
-
999
- currentExtensions[29] = (id) => {
1000
- // sharedref http://cbor.schmorp.de/value-sharing (for structured clones)
1001
- let refEntry = referenceMap.get(id);
1002
- refEntry.used = true;
1003
- return refEntry.target
1004
- };
1005
-
1006
- currentExtensions[258] = (array) => new Set(array); // https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md
1007
- (currentExtensions[259] = (read) => {
1008
- // https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec
1009
- // for decoding as a standard Map
1010
- if (currentDecoder.mapsAsObjects) {
1011
- currentDecoder.mapsAsObjects = false;
1012
- restoreMapsAsObject = true;
1013
- }
1014
- return read()
1015
- }).handlesRead = true;
1016
- function combine(a, b) {
1017
- if (typeof a === 'string')
1018
- return a + b
1019
- if (a instanceof Array)
1020
- return a.concat(b)
1021
- return Object.assign({}, a, b)
1022
- }
1023
- function getPackedValues() {
1024
- if (!packedValues) {
1025
- if (currentDecoder.getShared)
1026
- loadShared();
1027
- else
1028
- throw new Error('No packed values available')
1029
- }
1030
- return packedValues
1031
- }
1032
- const SHARED_DATA_TAG_ID = 0x53687264; // ascii 'Shrd'
1033
- currentExtensionRanges.push((tag, input) => {
1034
- if (tag >= 225 && tag <= 255)
1035
- return combine(getPackedValues().prefixes[tag - 224], input)
1036
- if (tag >= 28704 && tag <= 32767)
1037
- return combine(getPackedValues().prefixes[tag - 28672], input)
1038
- if (tag >= 1879052288 && tag <= 2147483647)
1039
- return combine(getPackedValues().prefixes[tag - 1879048192], input)
1040
- if (tag >= 216 && tag <= 223)
1041
- return combine(input, getPackedValues().suffixes[tag - 216])
1042
- if (tag >= 27647 && tag <= 28671)
1043
- return combine(input, getPackedValues().suffixes[tag - 27639])
1044
- if (tag >= 1811940352 && tag <= 1879048191)
1045
- return combine(input, getPackedValues().suffixes[tag - 1811939328])
1046
- 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)
1047
- return {
1048
- packedValues: packedValues,
1049
- structures: currentStructures.slice(0),
1050
- version: input,
1051
- }
1052
- }
1053
- if (tag == 55799) // self-descriptive CBOR tag, just return input value
1054
- return input
1055
- });
1056
-
1057
- const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1;
1058
- const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
1059
- typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
1060
- typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array];
1061
- const typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 85, 86];
1062
- for (let i = 0; i < typedArrays.length; i++) {
1063
- registerTypedArray(typedArrays[i], typedArrayTags[i]);
1064
- }
1065
- function registerTypedArray(TypedArray, tag) {
1066
- let dvMethod = 'get' + TypedArray.name.slice(0, -5);
1067
- let bytesPerElement;
1068
- if (typeof TypedArray === 'function')
1069
- bytesPerElement = TypedArray.BYTES_PER_ELEMENT;
1070
- else
1071
- TypedArray = null;
1072
- for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
1073
- if (!littleEndian && bytesPerElement == 1)
1074
- continue
1075
- let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : bytesPerElement == 8 ? 3 : 0;
1076
- currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine) ? (buffer) => {
1077
- if (!TypedArray)
1078
- throw new Error('Could not find typed array for code ' + tag)
1079
- if (!currentDecoder.copyBuffers) {
1080
- // try provide a direct view, but will only work if we are byte-aligned
1081
- if (bytesPerElement === 1 ||
1082
- bytesPerElement === 2 && !(buffer.byteOffset & 1) ||
1083
- bytesPerElement === 4 && !(buffer.byteOffset & 3) ||
1084
- bytesPerElement === 8 && !(buffer.byteOffset & 7))
1085
- return new TypedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength >> sizeShift);
1086
- }
1087
- // we have to slice/copy here to get a new ArrayBuffer, if we are not word/byte aligned
1088
- return new TypedArray(Uint8Array.prototype.slice.call(buffer, 0).buffer)
1089
- } : buffer => {
1090
- if (!TypedArray)
1091
- throw new Error('Could not find typed array for code ' + tag)
1092
- let dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
1093
- let elements = buffer.length >> sizeShift;
1094
- let ta = new TypedArray(elements);
1095
- let method = dv[dvMethod];
1096
- for (let i = 0; i < elements; i++) {
1097
- ta[i] = method.call(dv, i << sizeShift, littleEndian);
1098
- }
1099
- return ta
1100
- };
1101
- }
1102
- }
1103
-
1104
- function readBundleExt() {
1105
- let length = readJustLength();
1106
- let bundlePosition = position + read();
1107
- for (let i = 2; i < length; i++) {
1108
- // skip past bundles that were already read
1109
- let bundleLength = readJustLength(); // this will increment position, so must add to position afterwards
1110
- position += bundleLength;
1111
- }
1112
- let dataPosition = position;
1113
- position = bundlePosition;
1114
- bundledStrings = [readStringJS(readJustLength()), readStringJS(readJustLength())];
1115
- bundledStrings.position0 = 0;
1116
- bundledStrings.position1 = 0;
1117
- bundledStrings.postBundlePosition = position;
1118
- position = dataPosition;
1119
- return read()
1120
- }
1121
-
1122
- function readJustLength() {
1123
- let token = src[position++] & 0x1f;
1124
- if (token > 0x17) {
1125
- switch (token) {
1126
- case 0x18:
1127
- token = src[position++];
1128
- break
1129
- case 0x19:
1130
- token = dataView.getUint16(position);
1131
- position += 2;
1132
- break
1133
- case 0x1a:
1134
- token = dataView.getUint32(position);
1135
- position += 4;
1136
- break
1137
- }
1138
- }
1139
- return token
1140
- }
1141
-
1142
- function loadShared() {
1143
- if (currentDecoder.getShared) {
1144
- let sharedData = saveState(() => {
1145
- // save the state in case getShared modifies our buffer
1146
- src = null;
1147
- return currentDecoder.getShared()
1148
- }) || {};
1149
- let updatedStructures = sharedData.structures || [];
1150
- currentDecoder.sharedVersion = sharedData.version;
1151
- packedValues = currentDecoder.sharedValues = sharedData.packedValues;
1152
- if (currentStructures === true)
1153
- currentDecoder.structures = currentStructures = updatedStructures;
1154
- else
1155
- currentStructures.splice.apply(currentStructures, [0, updatedStructures.length].concat(updatedStructures));
1156
- }
1157
- }
1158
-
1159
- function saveState(callback) {
1160
- let savedSrcEnd = srcEnd;
1161
- let savedPosition = position;
1162
- let savedStringPosition = stringPosition;
1163
- let savedSrcStringStart = srcStringStart;
1164
- let savedSrcStringEnd = srcStringEnd;
1165
- let savedSrcString = srcString;
1166
- let savedStrings = strings;
1167
- let savedReferenceMap = referenceMap;
1168
- let savedBundledStrings = bundledStrings;
1169
-
1170
- // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1171
- let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
1172
- let savedStructures = currentStructures;
1173
- let savedDecoder = currentDecoder;
1174
- let savedSequentialMode = sequentialMode;
1175
- let value = callback();
1176
- srcEnd = savedSrcEnd;
1177
- position = savedPosition;
1178
- stringPosition = savedStringPosition;
1179
- srcStringStart = savedSrcStringStart;
1180
- srcStringEnd = savedSrcStringEnd;
1181
- srcString = savedSrcString;
1182
- strings = savedStrings;
1183
- referenceMap = savedReferenceMap;
1184
- bundledStrings = savedBundledStrings;
1185
- src = savedSrc;
1186
- sequentialMode = savedSequentialMode;
1187
- currentStructures = savedStructures;
1188
- currentDecoder = savedDecoder;
1189
- dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1190
- return value
1191
- }
1192
- function clearSource() {
1193
- src = null;
1194
- referenceMap = null;
1195
- currentStructures = null;
1196
- }
1197
-
1198
- function addExtension(extension) {
1199
- currentExtensions[extension.tag] = extension.decode;
1200
- }
1201
-
1202
- function setSizeLimits(limits) {
1203
- if (limits.maxMapSize) maxMapSize = limits.maxMapSize;
1204
- if (limits.maxArraySize) maxArraySize = limits.maxArraySize;
1205
- if (limits.maxObjectSize) limits.maxObjectSize;
1206
- }
1207
-
1208
- const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1209
- for (let i = 0; i < 256; i++) {
1210
- mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1211
- }
1212
- let defaultDecoder = new Decoder({ useRecords: false });
1213
- const decode = defaultDecoder.decode;
1214
- const decodeMultiple = defaultDecoder.decodeMultiple;
1215
- const FLOAT32_OPTIONS = {
1216
- NEVER: 0,
1217
- ALWAYS: 1,
1218
- DECIMAL_ROUND: 3,
1219
- DECIMAL_FIT: 4
1220
- };
1221
- function roundFloat32(float32Number) {
1222
- f32Array[0] = float32Number;
1223
- let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1224
- return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1225
- }
1226
-
1227
- exports.Decoder = Decoder;
1228
- exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
1229
- exports.Tag = Tag;
1230
- exports.addExtension = addExtension;
1231
- exports.checkedRead = checkedRead;
1232
- exports.clearSource = clearSource;
1233
- exports.decode = decode;
1234
- exports.decodeMultiple = decodeMultiple;
1235
- exports.getPosition = getPosition;
1236
- exports.mult10 = mult10;
1237
- exports.read = read;
1238
- exports.roundFloat32 = roundFloat32;
1239
- exports.setExtractor = setExtractor;
1240
- exports.setSizeLimits = setSizeLimits;
1241
- exports.typedArrays = typedArrays;
1242
-
1243
- }));
1244
- //# sourceMappingURL=decode-no-eval.cjs.map