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,1221 +0,0 @@
1
- var decoder
2
- try {
3
- decoder = new TextDecoder()
4
- } catch(error) {}
5
- var src
6
- var srcEnd
7
- var position = 0
8
- var alreadySet
9
- const EMPTY_ARRAY = []
10
- var strings = EMPTY_ARRAY
11
- var stringPosition = 0
12
- var currentUnpackr = {}
13
- var currentStructures
14
- var srcString
15
- var srcStringStart = 0
16
- var srcStringEnd = 0
17
- var bundledStrings
18
- var referenceMap
19
- var currentExtensions = []
20
- var dataView
21
- var defaultOptions = {
22
- useRecords: false,
23
- mapsAsObjects: true
24
- }
25
- export class C1Type {}
26
- export const C1 = new C1Type()
27
- C1.name = 'MessagePack 0xC1'
28
- var sequentialMode = false
29
- var inlineObjectReadThreshold = 2
30
- var readStruct, onLoadedStructures, onSaveState
31
- var BlockedFunction // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
32
- // no-eval build
33
- try {
34
- new Function('')
35
- } catch(error) {
36
- // if eval variants are not supported, do not create inline object readers ever
37
- inlineObjectReadThreshold = Infinity
38
- }
39
-
40
- export class Unpackr {
41
- constructor(options) {
42
- if (options) {
43
- if (options.useRecords === false && options.mapsAsObjects === undefined)
44
- options.mapsAsObjects = true
45
- if (options.sequential && options.trusted !== false) {
46
- options.trusted = true;
47
- if (!options.structures && options.useRecords != false) {
48
- options.structures = []
49
- if (!options.maxSharedStructures)
50
- options.maxSharedStructures = 0
51
- }
52
- }
53
- if (options.structures)
54
- options.structures.sharedLength = options.structures.length
55
- else if (options.getStructures) {
56
- (options.structures = []).uninitialized = true // this is what we use to denote an uninitialized structures
57
- options.structures.sharedLength = 0
58
- }
59
- if (options.int64AsNumber) {
60
- options.int64AsType = 'number'
61
- }
62
- }
63
- Object.assign(this, options)
64
- }
65
- unpack(source, options) {
66
- if (src) {
67
- // re-entrant execution, save the state and restore it after we do this unpack
68
- return saveState(() => {
69
- clearSource()
70
- return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
71
- })
72
- }
73
- if (!source.buffer && source.constructor === ArrayBuffer)
74
- source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
75
- if (typeof options === 'object') {
76
- srcEnd = options.end || source.length
77
- position = options.start || 0
78
- } else {
79
- position = 0
80
- srcEnd = options > -1 ? options : source.length
81
- }
82
- stringPosition = 0
83
- srcStringEnd = 0
84
- srcString = null
85
- strings = EMPTY_ARRAY
86
- bundledStrings = null
87
- src = source
88
- // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
89
- // technique for getting data from a database where it can be copied into an existing buffer instead of creating
90
- // new ones
91
- try {
92
- dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength))
93
- } catch(error) {
94
- // if it doesn't have a buffer, maybe it is the wrong type of object
95
- src = null
96
- if (source instanceof Uint8Array)
97
- throw error
98
- throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
99
- }
100
- if (this instanceof Unpackr) {
101
- currentUnpackr = this
102
- if (this.structures) {
103
- currentStructures = this.structures
104
- return checkedRead(options)
105
- } else if (!currentStructures || currentStructures.length > 0) {
106
- currentStructures = []
107
- }
108
- } else {
109
- currentUnpackr = defaultOptions
110
- if (!currentStructures || currentStructures.length > 0)
111
- currentStructures = []
112
- }
113
- return checkedRead(options)
114
- }
115
- unpackMultiple(source, forEach) {
116
- let values, lastPosition = 0
117
- try {
118
- sequentialMode = true
119
- let size = source.length
120
- let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size)
121
- if (forEach) {
122
- if (forEach(value, lastPosition, position) === false) return;
123
- while(position < size) {
124
- lastPosition = position
125
- if (forEach(checkedRead(), lastPosition, position) === false) {
126
- return
127
- }
128
- }
129
- }
130
- else {
131
- values = [ value ]
132
- while(position < size) {
133
- lastPosition = position
134
- values.push(checkedRead())
135
- }
136
- return values
137
- }
138
- } catch(error) {
139
- error.lastPosition = lastPosition
140
- error.values = values
141
- throw error
142
- } finally {
143
- sequentialMode = false
144
- clearSource()
145
- }
146
- }
147
- _mergeStructures(loadedStructures, existingStructures) {
148
- if (onLoadedStructures)
149
- loadedStructures = onLoadedStructures.call(this, loadedStructures);
150
- loadedStructures = loadedStructures || []
151
- if (Object.isFrozen(loadedStructures))
152
- loadedStructures = loadedStructures.map(structure => structure.slice(0))
153
- for (let i = 0, l = loadedStructures.length; i < l; i++) {
154
- let structure = loadedStructures[i]
155
- if (structure) {
156
- structure.isShared = true
157
- if (i >= 32)
158
- structure.highByte = (i - 32) >> 5
159
- }
160
- }
161
- loadedStructures.sharedLength = loadedStructures.length
162
- for (let id in existingStructures || []) {
163
- if (id >= 0) {
164
- let structure = loadedStructures[id]
165
- let existing = existingStructures[id]
166
- if (existing) {
167
- if (structure)
168
- (loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure
169
- loadedStructures[id] = existing
170
- }
171
- }
172
- }
173
- return this.structures = loadedStructures
174
- }
175
- decode(source, options) {
176
- return this.unpack(source, options)
177
- }
178
- }
179
- export function getPosition() {
180
- return position
181
- }
182
- export function checkedRead(options) {
183
- try {
184
- if (!currentUnpackr.trusted && !sequentialMode) {
185
- let sharedLength = currentStructures.sharedLength || 0
186
- if (sharedLength < currentStructures.length)
187
- currentStructures.length = sharedLength
188
- }
189
- let result
190
- if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
191
- result = readStruct(src, position, srcEnd, currentUnpackr)
192
- src = null // dispose of this so that recursive unpack calls don't save state
193
- if (!(options && options.lazy) && result)
194
- result = result.toJSON()
195
- position = srcEnd
196
- } else
197
- result = read()
198
- if (bundledStrings) { // bundled strings to skip past
199
- position = bundledStrings.postBundlePosition
200
- bundledStrings = null
201
- }
202
- if (sequentialMode)
203
- // we only need to restore the structures if there was an error, but if we completed a read,
204
- // we can clear this out and keep the structures we read
205
- currentStructures.restoreStructures = null
206
-
207
- if (position == srcEnd) {
208
- // finished reading this source, cleanup references
209
- if (currentStructures && currentStructures.restoreStructures)
210
- restoreStructures()
211
- currentStructures = null
212
- src = null
213
- if (referenceMap)
214
- referenceMap = null
215
- } else if (position > srcEnd) {
216
- // over read
217
- throw new Error('Unexpected end of MessagePack data')
218
- } else if (!sequentialMode) {
219
- let jsonView;
220
- try {
221
- jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100)
222
- } catch(error) {
223
- jsonView = '(JSON view not available ' + error + ')'
224
- }
225
- throw new Error('Data read, but end of buffer not reached ' + jsonView)
226
- }
227
- // else more to read, but we are reading sequentially, so don't clear source yet
228
- return result
229
- } catch(error) {
230
- if (currentStructures && currentStructures.restoreStructures)
231
- restoreStructures()
232
- clearSource()
233
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
234
- error.incomplete = true
235
- }
236
- throw error
237
- }
238
- }
239
-
240
- function restoreStructures() {
241
- for (let id in currentStructures.restoreStructures) {
242
- currentStructures[id] = currentStructures.restoreStructures[id]
243
- }
244
- currentStructures.restoreStructures = null
245
- }
246
-
247
- export function read() {
248
- let token = src[position++]
249
- if (token < 0xa0) {
250
- if (token < 0x80) {
251
- if (token < 0x40)
252
- return token
253
- else {
254
- let structure = currentStructures[token & 0x3f] ||
255
- currentUnpackr.getStructures && loadStructures()[token & 0x3f]
256
- if (structure) {
257
- if (!structure.read) {
258
- structure.read = createStructureReader(structure, token & 0x3f)
259
- }
260
- return structure.read()
261
- } else
262
- return token
263
- }
264
- } else if (token < 0x90) {
265
- // map
266
- token -= 0x80
267
- if (currentUnpackr.mapsAsObjects) {
268
- let object = {}
269
- for (let i = 0; i < token; i++) {
270
- let key = readKey()
271
- if (key === '__proto__')
272
- key = '__proto_'
273
- object[key] = read()
274
- }
275
- return object
276
- } else {
277
- let map = new Map()
278
- for (let i = 0; i < token; i++) {
279
- map.set(read(), read())
280
- }
281
- return map
282
- }
283
- } else {
284
- token -= 0x90
285
- let array = new Array(token)
286
- for (let i = 0; i < token; i++) {
287
- array[i] = read()
288
- }
289
- if (currentUnpackr.freezeData)
290
- return Object.freeze(array)
291
- return array
292
- }
293
- } else if (token < 0xc0) {
294
- // fixstr
295
- let length = token - 0xa0
296
- if (srcStringEnd >= position) {
297
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
298
- }
299
- if (srcStringEnd == 0 && srcEnd < 140) {
300
- // for small blocks, avoiding the overhead of the extract call is helpful
301
- let string = length < 16 ? shortStringInJS(length) : longStringInJS(length)
302
- if (string != null)
303
- return string
304
- }
305
- return readFixedString(length)
306
- } else {
307
- let value
308
- switch (token) {
309
- case 0xc0: return null
310
- case 0xc1:
311
- if (bundledStrings) {
312
- value = read() // followed by the length of the string in characters (not bytes!)
313
- if (value > 0)
314
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
315
- else
316
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
317
- }
318
- return C1; // "never-used", return special object to denote that
319
- case 0xc2: return false
320
- case 0xc3: return true
321
- case 0xc4:
322
- // bin 8
323
- value = src[position++]
324
- if (value === undefined)
325
- throw new Error('Unexpected end of buffer')
326
- return readBin(value)
327
- case 0xc5:
328
- // bin 16
329
- value = dataView.getUint16(position)
330
- position += 2
331
- return readBin(value)
332
- case 0xc6:
333
- // bin 32
334
- value = dataView.getUint32(position)
335
- position += 4
336
- return readBin(value)
337
- case 0xc7:
338
- // ext 8
339
- return readExt(src[position++])
340
- case 0xc8:
341
- // ext 16
342
- value = dataView.getUint16(position)
343
- position += 2
344
- return readExt(value)
345
- case 0xc9:
346
- // ext 32
347
- value = dataView.getUint32(position)
348
- position += 4
349
- return readExt(value)
350
- case 0xca:
351
- value = dataView.getFloat32(position)
352
- if (currentUnpackr.useFloat32 > 2) {
353
- // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
354
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)]
355
- position += 4
356
- return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
357
- }
358
- position += 4
359
- return value
360
- case 0xcb:
361
- value = dataView.getFloat64(position)
362
- position += 8
363
- return value
364
- // uint handlers
365
- case 0xcc:
366
- return src[position++]
367
- case 0xcd:
368
- value = dataView.getUint16(position)
369
- position += 2
370
- return value
371
- case 0xce:
372
- value = dataView.getUint32(position)
373
- position += 4
374
- return value
375
- case 0xcf:
376
- if (currentUnpackr.int64AsType === 'number') {
377
- value = dataView.getUint32(position) * 0x100000000
378
- value += dataView.getUint32(position + 4)
379
- } else if (currentUnpackr.int64AsType === 'string') {
380
- value = dataView.getBigUint64(position).toString()
381
- } else if (currentUnpackr.int64AsType === 'auto') {
382
- value = dataView.getBigUint64(position)
383
- if (value<=BigInt(2)<<BigInt(52)) value=Number(value)
384
- } else
385
- value = dataView.getBigUint64(position)
386
- position += 8
387
- return value
388
-
389
- // int handlers
390
- case 0xd0:
391
- return dataView.getInt8(position++)
392
- case 0xd1:
393
- value = dataView.getInt16(position)
394
- position += 2
395
- return value
396
- case 0xd2:
397
- value = dataView.getInt32(position)
398
- position += 4
399
- return value
400
- case 0xd3:
401
- if (currentUnpackr.int64AsType === 'number') {
402
- value = dataView.getInt32(position) * 0x100000000
403
- value += dataView.getUint32(position + 4)
404
- } else if (currentUnpackr.int64AsType === 'string') {
405
- value = dataView.getBigInt64(position).toString()
406
- } else if (currentUnpackr.int64AsType === 'auto') {
407
- value = dataView.getBigInt64(position)
408
- if (value>=BigInt(-2)<<BigInt(52)&&value<=BigInt(2)<<BigInt(52)) value=Number(value)
409
- } else
410
- value = dataView.getBigInt64(position)
411
- position += 8
412
- return value
413
-
414
- case 0xd4:
415
- // fixext 1
416
- value = src[position++]
417
- if (value == 0x72) {
418
- return recordDefinition(src[position++] & 0x3f)
419
- } else {
420
- let extension = currentExtensions[value]
421
- if (extension) {
422
- if (extension.read) {
423
- position++ // skip filler byte
424
- return extension.read(read())
425
- } else if (extension.noBuffer) {
426
- position++ // skip filler byte
427
- return extension()
428
- } else
429
- return extension(src.subarray(position, ++position))
430
- } else
431
- throw new Error('Unknown extension ' + value)
432
- }
433
- case 0xd5:
434
- // fixext 2
435
- value = src[position]
436
- if (value == 0x72) {
437
- position++
438
- return recordDefinition(src[position++] & 0x3f, src[position++])
439
- } else
440
- return readExt(2)
441
- case 0xd6:
442
- // fixext 4
443
- return readExt(4)
444
- case 0xd7:
445
- // fixext 8
446
- return readExt(8)
447
- case 0xd8:
448
- // fixext 16
449
- return readExt(16)
450
- case 0xd9:
451
- // str 8
452
- value = src[position++]
453
- if (srcStringEnd >= position) {
454
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
455
- }
456
- return readString8(value)
457
- case 0xda:
458
- // str 16
459
- value = dataView.getUint16(position)
460
- position += 2
461
- if (srcStringEnd >= position) {
462
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
463
- }
464
- return readString16(value)
465
- case 0xdb:
466
- // str 32
467
- value = dataView.getUint32(position)
468
- position += 4
469
- if (srcStringEnd >= position) {
470
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
471
- }
472
- return readString32(value)
473
- case 0xdc:
474
- // array 16
475
- value = dataView.getUint16(position)
476
- position += 2
477
- return readArray(value)
478
- case 0xdd:
479
- // array 32
480
- value = dataView.getUint32(position)
481
- position += 4
482
- return readArray(value)
483
- case 0xde:
484
- // map 16
485
- value = dataView.getUint16(position)
486
- position += 2
487
- return readMap(value)
488
- case 0xdf:
489
- // map 32
490
- value = dataView.getUint32(position)
491
- position += 4
492
- return readMap(value)
493
- default: // negative int
494
- if (token >= 0xe0)
495
- return token - 0x100
496
- if (token === undefined) {
497
- let error = new Error('Unexpected end of MessagePack data')
498
- error.incomplete = true
499
- throw error
500
- }
501
- throw new Error('Unknown MessagePack token ' + token)
502
-
503
- }
504
- }
505
- }
506
- const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/
507
- function createStructureReader(structure, firstId) {
508
- function readObject() {
509
- // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
510
- if (readObject.count++ > inlineObjectReadThreshold) {
511
- let readObject = structure.read = (new Function('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
512
- '({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read)
513
- if (structure.highByte === 0)
514
- structure.read = createSecondByteReader(firstId, structure.read)
515
- return readObject() // second byte is already read, if there is one so immediately read object
516
- }
517
- let object = {}
518
- for (let i = 0, l = structure.length; i < l; i++) {
519
- let key = structure[i]
520
- if (key === '__proto__')
521
- key = '__proto_'
522
- object[key] = read()
523
- }
524
- if (currentUnpackr.freezeData)
525
- return Object.freeze(object);
526
- return object
527
- }
528
- readObject.count = 0
529
- if (structure.highByte === 0) {
530
- return createSecondByteReader(firstId, readObject)
531
- }
532
- return readObject
533
- }
534
-
535
- const createSecondByteReader = (firstId, read0) => {
536
- return function() {
537
- let highByte = src[position++]
538
- if (highByte === 0)
539
- return read0()
540
- let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5)
541
- let structure = currentStructures[id] || loadStructures()[id]
542
- if (!structure) {
543
- throw new Error('Record id is not defined for ' + id)
544
- }
545
- if (!structure.read)
546
- structure.read = createStructureReader(structure, firstId)
547
- return structure.read()
548
- }
549
- }
550
-
551
- export function loadStructures() {
552
- let loadedStructures = saveState(() => {
553
- // save the state in case getStructures modifies our buffer
554
- src = null
555
- return currentUnpackr.getStructures()
556
- })
557
- return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
558
- }
559
-
560
- var readFixedString = readStringJS
561
- var readString8 = readStringJS
562
- var readString16 = readStringJS
563
- var readString32 = readStringJS
564
- export let isNativeAccelerationEnabled = false
565
-
566
- export function setExtractor(extractStrings) {
567
- isNativeAccelerationEnabled = true
568
- readFixedString = readString(1)
569
- readString8 = readString(2)
570
- readString16 = readString(3)
571
- readString32 = readString(5)
572
- function readString(headerLength) {
573
- return function readString(length) {
574
- let string = strings[stringPosition++]
575
- if (string == null) {
576
- if (bundledStrings)
577
- return readStringJS(length)
578
- let byteOffset = src.byteOffset
579
- let extraction = extractStrings(position - headerLength + byteOffset, srcEnd + byteOffset, src.buffer)
580
- if (typeof extraction == 'string') {
581
- string = extraction
582
- strings = EMPTY_ARRAY
583
- } else {
584
- strings = extraction
585
- stringPosition = 1
586
- 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
587
- string = strings[0]
588
- if (string === undefined)
589
- throw new Error('Unexpected end of buffer')
590
- }
591
- }
592
- let srcStringLength = string.length
593
- if (srcStringLength <= length) {
594
- position += length
595
- return string
596
- }
597
- srcString = string
598
- srcStringStart = position
599
- srcStringEnd = position + srcStringLength
600
- position += length
601
- return string.slice(0, length) // we know we just want the beginning
602
- }
603
- }
604
- }
605
- function readStringJS(length) {
606
- let result
607
- if (length < 16) {
608
- if (result = shortStringInJS(length))
609
- return result
610
- }
611
- if (length > 64 && decoder)
612
- return decoder.decode(src.subarray(position, position += length))
613
- const end = position + length
614
- const units = []
615
- result = ''
616
- while (position < end) {
617
- const byte1 = src[position++]
618
- if ((byte1 & 0x80) === 0) {
619
- // 1 byte
620
- units.push(byte1)
621
- } else if ((byte1 & 0xe0) === 0xc0) {
622
- // 2 bytes
623
- const byte2 = src[position++] & 0x3f
624
- units.push(((byte1 & 0x1f) << 6) | byte2)
625
- } else if ((byte1 & 0xf0) === 0xe0) {
626
- // 3 bytes
627
- const byte2 = src[position++] & 0x3f
628
- const byte3 = src[position++] & 0x3f
629
- units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3)
630
- } else if ((byte1 & 0xf8) === 0xf0) {
631
- // 4 bytes
632
- const byte2 = src[position++] & 0x3f
633
- const byte3 = src[position++] & 0x3f
634
- const byte4 = src[position++] & 0x3f
635
- let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4
636
- if (unit > 0xffff) {
637
- unit -= 0x10000
638
- units.push(((unit >>> 10) & 0x3ff) | 0xd800)
639
- unit = 0xdc00 | (unit & 0x3ff)
640
- }
641
- units.push(unit)
642
- } else {
643
- units.push(byte1)
644
- }
645
-
646
- if (units.length >= 0x1000) {
647
- result += fromCharCode.apply(String, units)
648
- units.length = 0
649
- }
650
- }
651
-
652
- if (units.length > 0) {
653
- result += fromCharCode.apply(String, units)
654
- }
655
-
656
- return result
657
- }
658
- export function readString(source, start, length) {
659
- let existingSrc = src;
660
- src = source;
661
- position = start;
662
- try {
663
- return readStringJS(length);
664
- } finally {
665
- src = existingSrc;
666
- }
667
- }
668
-
669
- function readArray(length) {
670
- let array = new Array(length)
671
- for (let i = 0; i < length; i++) {
672
- array[i] = read()
673
- }
674
- if (currentUnpackr.freezeData)
675
- return Object.freeze(array)
676
- return array
677
- }
678
-
679
- function readMap(length) {
680
- if (currentUnpackr.mapsAsObjects) {
681
- let object = {}
682
- for (let i = 0; i < length; i++) {
683
- let key = readKey()
684
- if (key === '__proto__')
685
- key = '__proto_';
686
- object[key] = read()
687
- }
688
- return object
689
- } else {
690
- let map = new Map()
691
- for (let i = 0; i < length; i++) {
692
- map.set(read(), read())
693
- }
694
- return map
695
- }
696
- }
697
-
698
- var fromCharCode = String.fromCharCode
699
- function longStringInJS(length) {
700
- let start = position
701
- let bytes = new Array(length)
702
- for (let i = 0; i < length; i++) {
703
- const byte = src[position++];
704
- if ((byte & 0x80) > 0) {
705
- position = start
706
- return
707
- }
708
- bytes[i] = byte
709
- }
710
- return fromCharCode.apply(String, bytes)
711
- }
712
- function shortStringInJS(length) {
713
- if (length < 4) {
714
- if (length < 2) {
715
- if (length === 0)
716
- return ''
717
- else {
718
- let a = src[position++]
719
- if ((a & 0x80) > 1) {
720
- position -= 1
721
- return
722
- }
723
- return fromCharCode(a)
724
- }
725
- } else {
726
- let a = src[position++]
727
- let b = src[position++]
728
- if ((a & 0x80) > 0 || (b & 0x80) > 0) {
729
- position -= 2
730
- return
731
- }
732
- if (length < 3)
733
- return fromCharCode(a, b)
734
- let c = src[position++]
735
- if ((c & 0x80) > 0) {
736
- position -= 3
737
- return
738
- }
739
- return fromCharCode(a, b, c)
740
- }
741
- } else {
742
- let a = src[position++]
743
- let b = src[position++]
744
- let c = src[position++]
745
- let d = src[position++]
746
- if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
747
- position -= 4
748
- return
749
- }
750
- if (length < 6) {
751
- if (length === 4)
752
- return fromCharCode(a, b, c, d)
753
- else {
754
- let e = src[position++]
755
- if ((e & 0x80) > 0) {
756
- position -= 5
757
- return
758
- }
759
- return fromCharCode(a, b, c, d, e)
760
- }
761
- } else if (length < 8) {
762
- let e = src[position++]
763
- let f = src[position++]
764
- if ((e & 0x80) > 0 || (f & 0x80) > 0) {
765
- position -= 6
766
- return
767
- }
768
- if (length < 7)
769
- return fromCharCode(a, b, c, d, e, f)
770
- let g = src[position++]
771
- if ((g & 0x80) > 0) {
772
- position -= 7
773
- return
774
- }
775
- return fromCharCode(a, b, c, d, e, f, g)
776
- } else {
777
- let e = src[position++]
778
- let f = src[position++]
779
- let g = src[position++]
780
- let h = src[position++]
781
- if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
782
- position -= 8
783
- return
784
- }
785
- if (length < 10) {
786
- if (length === 8)
787
- return fromCharCode(a, b, c, d, e, f, g, h)
788
- else {
789
- let i = src[position++]
790
- if ((i & 0x80) > 0) {
791
- position -= 9
792
- return
793
- }
794
- return fromCharCode(a, b, c, d, e, f, g, h, i)
795
- }
796
- } else if (length < 12) {
797
- let i = src[position++]
798
- let j = src[position++]
799
- if ((i & 0x80) > 0 || (j & 0x80) > 0) {
800
- position -= 10
801
- return
802
- }
803
- if (length < 11)
804
- return fromCharCode(a, b, c, d, e, f, g, h, i, j)
805
- let k = src[position++]
806
- if ((k & 0x80) > 0) {
807
- position -= 11
808
- return
809
- }
810
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
811
- } else {
812
- let i = src[position++]
813
- let j = src[position++]
814
- let k = src[position++]
815
- let l = src[position++]
816
- if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
817
- position -= 12
818
- return
819
- }
820
- if (length < 14) {
821
- if (length === 12)
822
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
823
- else {
824
- let m = src[position++]
825
- if ((m & 0x80) > 0) {
826
- position -= 13
827
- return
828
- }
829
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
830
- }
831
- } else {
832
- let m = src[position++]
833
- let n = src[position++]
834
- if ((m & 0x80) > 0 || (n & 0x80) > 0) {
835
- position -= 14
836
- return
837
- }
838
- if (length < 15)
839
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
840
- let o = src[position++]
841
- if ((o & 0x80) > 0) {
842
- position -= 15
843
- return
844
- }
845
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
846
- }
847
- }
848
- }
849
- }
850
- }
851
-
852
- function readOnlyJSString() {
853
- let token = src[position++]
854
- let length
855
- if (token < 0xc0) {
856
- // fixstr
857
- length = token - 0xa0
858
- } else {
859
- switch(token) {
860
- case 0xd9:
861
- // str 8
862
- length = src[position++]
863
- break
864
- case 0xda:
865
- // str 16
866
- length = dataView.getUint16(position)
867
- position += 2
868
- break
869
- case 0xdb:
870
- // str 32
871
- length = dataView.getUint32(position)
872
- position += 4
873
- break
874
- default:
875
- throw new Error('Expected string')
876
- }
877
- }
878
- return readStringJS(length)
879
- }
880
-
881
-
882
- function readBin(length) {
883
- return currentUnpackr.copyBuffers ?
884
- // specifically use the copying slice (not the node one)
885
- Uint8Array.prototype.slice.call(src, position, position += length) :
886
- src.subarray(position, position += length)
887
- }
888
- function readExt(length) {
889
- let type = src[position++]
890
- if (currentExtensions[type]) {
891
- let end
892
- return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
893
- position = readPosition;
894
- try {
895
- return read();
896
- } finally {
897
- position = end;
898
- }
899
- })
900
- }
901
- else
902
- throw new Error('Unknown extension type ' + type)
903
- }
904
-
905
- var keyCache = new Array(4096)
906
- function readKey() {
907
- let length = src[position++]
908
- if (length >= 0xa0 && length < 0xc0) {
909
- // fixstr, potentially use key cache
910
- length = length - 0xa0
911
- if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
912
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
913
- else if (!(srcStringEnd == 0 && srcEnd < 180))
914
- return readFixedString(length)
915
- } else { // not cacheable, go back and do a standard read
916
- position--
917
- return asSafeString(read())
918
- }
919
- let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff
920
- let entry = keyCache[key]
921
- let checkPosition = position
922
- let end = position + length - 3
923
- let chunk
924
- let i = 0
925
- if (entry && entry.bytes == length) {
926
- while (checkPosition < end) {
927
- chunk = dataView.getUint32(checkPosition)
928
- if (chunk != entry[i++]) {
929
- checkPosition = 0x70000000
930
- break
931
- }
932
- checkPosition += 4
933
- }
934
- end += 3
935
- while (checkPosition < end) {
936
- chunk = src[checkPosition++]
937
- if (chunk != entry[i++]) {
938
- checkPosition = 0x70000000
939
- break
940
- }
941
- }
942
- if (checkPosition === end) {
943
- position = checkPosition
944
- return entry.string
945
- }
946
- end -= 3
947
- checkPosition = position
948
- }
949
- entry = []
950
- keyCache[key] = entry
951
- entry.bytes = length
952
- while (checkPosition < end) {
953
- chunk = dataView.getUint32(checkPosition)
954
- entry.push(chunk)
955
- checkPosition += 4
956
- }
957
- end += 3
958
- while (checkPosition < end) {
959
- chunk = src[checkPosition++]
960
- entry.push(chunk)
961
- }
962
- // for small blocks, avoiding the overhead of the extract call is helpful
963
- let string = length < 16 ? shortStringInJS(length) : longStringInJS(length)
964
- if (string != null)
965
- return entry.string = string
966
- return entry.string = readFixedString(length)
967
- }
968
-
969
- function asSafeString(property) {
970
- // protect against expensive (DoS) string conversions
971
- if (typeof property === 'string') return property;
972
- if (typeof property === 'number' || typeof property === 'boolean' || typeof property === 'bigint') return property.toString();
973
- if (property == null) return property + '';
974
- if (currentUnpackr.allowArraysInMapKeys && Array.isArray(property) && property.flat().every(item => ['string', 'number', 'boolean', 'bigint'].includes(typeof item))) {
975
- return property.flat().toString();
976
- }
977
- throw new Error(`Invalid property type for record: ${typeof property}`);
978
- }
979
- // the registration of the record definition extension (as "r")
980
- const recordDefinition = (id, highByte) => {
981
- let structure = read().map(asSafeString) // ensure that all keys are strings and
982
- // that the array is mutable
983
- let firstByte = id
984
- if (highByte !== undefined) {
985
- id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id)
986
- structure.highByte = highByte
987
- }
988
- let existingStructure = currentStructures[id]
989
- // If it is a shared structure, we need to restore any changes after reading.
990
- // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
991
- // to the state prior to an incomplete read in order to properly resume.
992
- if (existingStructure && (existingStructure.isShared || sequentialMode)) {
993
- (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure
994
- }
995
- currentStructures[id] = structure
996
- structure.read = createStructureReader(structure, firstByte)
997
- return structure.read()
998
- }
999
- currentExtensions[0] = () => {} // notepack defines extension 0 to mean undefined, so use that as the default here
1000
- currentExtensions[0].noBuffer = true
1001
-
1002
- currentExtensions[0x42] = data => {
1003
- let headLength = (data.byteLength % 8) || 8
1004
- let head = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0])
1005
- for (let i = 1; i < headLength; i++) {
1006
- head <<= BigInt(8)
1007
- head += BigInt(data[i])
1008
- }
1009
- if (data.byteLength !== headLength) {
1010
- let view = new DataView(data.buffer, data.byteOffset, data.byteLength)
1011
- let decode = (start, end) => {
1012
- let length = end - start
1013
- if (length <= 40) {
1014
- let out = view.getBigUint64(start)
1015
- for (let i = start + 8; i < end; i += 8) {
1016
- out <<= BigInt(64n)
1017
- out |= view.getBigUint64(i)
1018
- }
1019
- return out
1020
- }
1021
- // if (length === 8) return view.getBigUint64(start)
1022
- let middle = start + (length >> 4 << 3)
1023
- let left = decode(start, middle)
1024
- let right = decode(middle, end)
1025
- return (left << BigInt((end - middle) * 8)) | right
1026
- }
1027
- head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength)
1028
- }
1029
- return head
1030
- }
1031
-
1032
- let errors = {
1033
- Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError: typeof AggregateError === 'function' ? AggregateError : null,
1034
- }
1035
- currentExtensions[0x65] = () => {
1036
- let data = read()
1037
- if (!errors[data[0]]) {
1038
- let error = Error(data[1], { cause: data[2] })
1039
- error.name = data[0]
1040
- return error
1041
- }
1042
- return errors[data[0]](data[1], { cause: data[2] })
1043
- }
1044
-
1045
- currentExtensions[0x69] = (data) => {
1046
- // id extension (for structured clones)
1047
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1048
- let id = dataView.getUint32(position - 4)
1049
- if (!referenceMap)
1050
- referenceMap = new Map()
1051
- let token = src[position]
1052
- let target
1053
- // TODO: handle any other types that can cycle and make the code more robust if there are other extensions
1054
- if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
1055
- target = []
1056
- else if (token >= 0x80 && token < 0x90 || token == 0xde || token == 0xdf)
1057
- target = new Map()
1058
- else if ((token >= 0xc7 && token <= 0xc9 || token >= 0xd4 && token <= 0xd8) && src[position + 1] === 0x73)
1059
- target = new Set()
1060
- else
1061
- target = {}
1062
-
1063
- let refEntry = { target } // a placeholder object
1064
- referenceMap.set(id, refEntry)
1065
- let targetProperties = read() // read the next value as the target object to id
1066
- if (!refEntry.used) {
1067
- // no cycle, can just use the returned read object
1068
- return refEntry.target = targetProperties // replace the placeholder with the real one
1069
- } else {
1070
- // there is a cycle, so we have to assign properties to original target
1071
- Object.assign(target, targetProperties)
1072
- }
1073
-
1074
- // copy over map/set entries if we're able to
1075
- if (target instanceof Map)
1076
- for (let [k, v] of targetProperties.entries()) target.set(k, v)
1077
- if (target instanceof Set)
1078
- for (let i of Array.from(targetProperties)) target.add(i)
1079
- return target
1080
- }
1081
-
1082
- currentExtensions[0x70] = (data) => {
1083
- // pointer extension (for structured clones)
1084
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1085
- let id = dataView.getUint32(position - 4)
1086
- let refEntry = referenceMap.get(id)
1087
- refEntry.used = true
1088
- return refEntry.target
1089
- }
1090
-
1091
- currentExtensions[0x73] = () => new Set(read())
1092
-
1093
- export const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array')
1094
-
1095
- let glbl = typeof globalThis === 'object' ? globalThis : window;
1096
- currentExtensions[0x74] = (data) => {
1097
- let typeCode = data[0]
1098
- // we always have to slice to get a new ArrayBuffer that is aligned
1099
- let buffer = Uint8Array.prototype.slice.call(data, 1).buffer
1100
-
1101
- let typedArrayName = typedArrays[typeCode]
1102
- if (!typedArrayName) {
1103
- if (typeCode === 16) return buffer
1104
- if (typeCode === 17) return new DataView(buffer)
1105
- throw new Error('Could not find typed array for code ' + typeCode)
1106
- }
1107
- return new glbl[typedArrayName](buffer)
1108
- }
1109
- currentExtensions[0x78] = () => {
1110
- let data = read()
1111
- return new RegExp(data[0], data[1])
1112
- }
1113
- const TEMP_BUNDLE = []
1114
- currentExtensions[0x62] = (data) => {
1115
- let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]
1116
- let dataPosition = position
1117
- position += dataSize - data.length
1118
- bundledStrings = TEMP_BUNDLE
1119
- bundledStrings = [readOnlyJSString(), readOnlyJSString()]
1120
- bundledStrings.position0 = 0
1121
- bundledStrings.position1 = 0
1122
- bundledStrings.postBundlePosition = position
1123
- position = dataPosition
1124
- return read()
1125
- }
1126
-
1127
- currentExtensions[0xff] = (data) => {
1128
- // 32-bit date extension
1129
- if (data.length == 4)
1130
- return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
1131
- else if (data.length == 8)
1132
- return new Date(
1133
- ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
1134
- ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
1135
- else if (data.length == 12)
1136
- return new Date(
1137
- ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
1138
- (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
1139
- else
1140
- return new Date('invalid')
1141
- }
1142
- // registration of bulk record definition?
1143
- // currentExtensions[0x52] = () =>
1144
-
1145
- function saveState(callback) {
1146
- if (onSaveState)
1147
- onSaveState();
1148
- let savedSrcEnd = srcEnd
1149
- let savedPosition = position
1150
- let savedStringPosition = stringPosition
1151
- let savedSrcStringStart = srcStringStart
1152
- let savedSrcStringEnd = srcStringEnd
1153
- let savedSrcString = srcString
1154
- let savedStrings = strings
1155
- let savedReferenceMap = referenceMap
1156
- let savedBundledStrings = bundledStrings
1157
-
1158
- // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1159
- let savedSrc = new Uint8Array(src.slice(0, srcEnd)) // we copy the data in case it changes while external data is processed
1160
- let savedStructures = currentStructures
1161
- let savedStructuresContents = currentStructures.slice(0, currentStructures.length)
1162
- let savedPackr = currentUnpackr
1163
- let savedSequentialMode = sequentialMode
1164
- let value = callback()
1165
- srcEnd = savedSrcEnd
1166
- position = savedPosition
1167
- stringPosition = savedStringPosition
1168
- srcStringStart = savedSrcStringStart
1169
- srcStringEnd = savedSrcStringEnd
1170
- srcString = savedSrcString
1171
- strings = savedStrings
1172
- referenceMap = savedReferenceMap
1173
- bundledStrings = savedBundledStrings
1174
- src = savedSrc
1175
- sequentialMode = savedSequentialMode
1176
- currentStructures = savedStructures
1177
- currentStructures.splice(0, currentStructures.length, ...savedStructuresContents)
1178
- currentUnpackr = savedPackr
1179
- dataView = new DataView(src.buffer, src.byteOffset, src.byteLength)
1180
- return value
1181
- }
1182
- export function clearSource() {
1183
- src = null
1184
- referenceMap = null
1185
- currentStructures = null
1186
- }
1187
-
1188
- export function addExtension(extension) {
1189
- if (extension.unpack)
1190
- currentExtensions[extension.type] = extension.unpack
1191
- else
1192
- currentExtensions[extension.type] = extension
1193
- }
1194
-
1195
- export const mult10 = new Array(147) // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1196
- for (let i = 0; i < 256; i++) {
1197
- mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103))
1198
- }
1199
- export const Decoder = Unpackr
1200
- var defaultUnpackr = new Unpackr({ useRecords: false })
1201
- export const unpack = defaultUnpackr.unpack
1202
- export const unpackMultiple = defaultUnpackr.unpackMultiple
1203
- export const decode = defaultUnpackr.unpack
1204
- export const FLOAT32_OPTIONS = {
1205
- NEVER: 0,
1206
- ALWAYS: 1,
1207
- DECIMAL_ROUND: 3,
1208
- DECIMAL_FIT: 4
1209
- }
1210
- let f32Array = new Float32Array(1)
1211
- let u8Array = new Uint8Array(f32Array.buffer, 0, 4)
1212
- export function roundFloat32(float32Number) {
1213
- f32Array[0] = float32Number
1214
- let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)]
1215
- return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1216
- }
1217
- export function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1218
- readStruct = updatedReadStruct;
1219
- onLoadedStructures = loadedStructs;
1220
- onSaveState = saveState;
1221
- }