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