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,1141 +0,0 @@
1
- import { Unpackr, mult10, C1Type, typedArrays, addExtension as unpackAddExtension } from './unpack.js'
2
- let textEncoder
3
- try {
4
- textEncoder = new TextEncoder()
5
- } catch (error) {}
6
- let extensions, extensionClasses
7
- const hasNodeBuffer = typeof Buffer !== 'undefined'
8
- const ByteArrayAllocate = hasNodeBuffer ?
9
- function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array
10
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array
11
- const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000
12
- let target, keysTarget
13
- let targetView
14
- let position = 0
15
- let safeEnd
16
- let bundledStrings = null
17
- let writeStructSlots
18
- const MAX_BUNDLE_SIZE = 0x5500 // maximum characters such that the encoded bytes fits in 16 bits.
19
- const hasNonLatin = /[\u0080-\uFFFF]/
20
- export const RECORD_SYMBOL = Symbol('record-id')
21
- export class Packr extends Unpackr {
22
- constructor(options) {
23
- super(options)
24
- this.offset = 0
25
- let typeBuffer
26
- let start
27
- let hasSharedUpdate
28
- let structures
29
- let referenceMap
30
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
31
- return target.utf8Write(string, position, target.byteLength - position)
32
- } : (textEncoder && textEncoder.encodeInto) ?
33
- function(string, position) {
34
- return textEncoder.encodeInto(string, target.subarray(position)).written
35
- } : false
36
-
37
- let packr = this
38
- if (!options)
39
- options = {}
40
- let isSequential = options && options.sequential
41
- let hasSharedStructures = options.structures || options.saveStructures
42
- let maxSharedStructures = options.maxSharedStructures
43
- if (maxSharedStructures == null)
44
- maxSharedStructures = hasSharedStructures ? 32 : 0
45
- if (maxSharedStructures > 8160)
46
- throw new Error('Maximum maxSharedStructure is 8160')
47
- if (options.structuredClone && options.moreTypes == undefined) {
48
- this.moreTypes = true
49
- }
50
- let maxOwnStructures = options.maxOwnStructures
51
- if (maxOwnStructures == null)
52
- maxOwnStructures = hasSharedStructures ? 32 : 64
53
- if (!this.structures && options.useRecords != false)
54
- this.structures = []
55
- // two byte record ids for shared structures
56
- let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64)
57
- let sharedLimitId = maxSharedStructures + 0x40
58
- let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40
59
- if (maxStructureId > 8256) {
60
- throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
61
- }
62
- let recordIdsToRemove = []
63
- let transitionsCount = 0
64
- let serializationsSinceTransitionRebuild = 0
65
-
66
- this.pack = this.encode = function(value, encodeOptions) {
67
- if (!target) {
68
- target = new ByteArrayAllocate(8192)
69
- targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192))
70
- position = 0
71
- }
72
- safeEnd = target.length - 10
73
- if (safeEnd - position < 0x800) {
74
- // don't start too close to the end,
75
- target = new ByteArrayAllocate(target.length)
76
- targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))
77
- safeEnd = target.length - 10
78
- position = 0
79
- } else
80
- position = (position + 7) & 0x7ffffff8 // Word align to make any future copying of this buffer faster
81
- start = position
82
- if (encodeOptions & RESERVE_START_SPACE) position += (encodeOptions & 0xff)
83
- referenceMap = packr.structuredClone ? new Map() : null
84
- if (packr.bundleStrings && typeof value !== 'string') {
85
- bundledStrings = []
86
- bundledStrings.size = Infinity // force a new bundle start on first string
87
- } else
88
- bundledStrings = null
89
- structures = packr.structures
90
- if (structures) {
91
- if (structures.uninitialized)
92
- structures = packr._mergeStructures(packr.getStructures())
93
- let sharedLength = structures.sharedLength || 0
94
- if (sharedLength > maxSharedStructures) {
95
- //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
96
- throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
97
- }
98
- if (!structures.transitions) {
99
- // rebuild our structure transitions
100
- structures.transitions = Object.create(null)
101
- for (let i = 0; i < sharedLength; i++) {
102
- let keys = structures[i]
103
- if (!keys)
104
- continue
105
- let nextTransition, transition = structures.transitions
106
- for (let j = 0, l = keys.length; j < l; j++) {
107
- let key = keys[j]
108
- nextTransition = transition[key]
109
- if (!nextTransition) {
110
- nextTransition = transition[key] = Object.create(null)
111
- }
112
- transition = nextTransition
113
- }
114
- transition[RECORD_SYMBOL] = i + 0x40
115
- }
116
- this.lastNamedStructuresLength = sharedLength
117
- }
118
- if (!isSequential) {
119
- structures.nextId = sharedLength + 0x40
120
- }
121
- }
122
- if (hasSharedUpdate)
123
- hasSharedUpdate = false
124
- let encodingError;
125
- try {
126
- if (packr.randomAccessStructure && value && typeof value === 'object') {
127
- if (value.constructor === Object) writeStruct(value); // simple object
128
- else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
129
- // allow user classes, if they don't need special handling (but do use toJSON if available)
130
- writeStruct(value.toJSON ? value.toJSON() : value);
131
- } else pack(value)
132
- } else
133
- pack(value)
134
- let lastBundle = bundledStrings;
135
- if (bundledStrings)
136
- writeBundles(start, pack, 0)
137
- if (referenceMap && referenceMap.idsToInsert) {
138
- let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
139
- let i = idsToInsert.length;
140
- let incrementPosition = -1;
141
- while (lastBundle && i > 0) {
142
- let insertionPoint = idsToInsert[--i].offset + start;
143
- if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
144
- incrementPosition = 0;
145
- if (insertionPoint > (lastBundle.position + start)) {
146
- if (incrementPosition >= 0)
147
- incrementPosition += 6;
148
- } else {
149
- if (incrementPosition >= 0) {
150
- // update the bundle reference now
151
- targetView.setUint32(lastBundle.position + start,
152
- targetView.getUint32(lastBundle.position + start) + incrementPosition)
153
- incrementPosition = -1; // reset
154
- }
155
- lastBundle = lastBundle.previous;
156
- i++;
157
- }
158
- }
159
- if (incrementPosition >= 0 && lastBundle) {
160
- // update the bundle reference now
161
- targetView.setUint32(lastBundle.position + start,
162
- targetView.getUint32(lastBundle.position + start) + incrementPosition)
163
- }
164
- position += idsToInsert.length * 6;
165
- if (position > safeEnd)
166
- makeRoom(position)
167
- packr.offset = position
168
- let serialized = insertIds(target.subarray(start, position), idsToInsert)
169
- referenceMap = null
170
- return serialized
171
- }
172
- packr.offset = position // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
173
- if (encodeOptions & REUSE_BUFFER_MODE) {
174
- target.start = start
175
- target.end = position
176
- return target
177
- }
178
- return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
179
- } catch(error) {
180
- encodingError = error;
181
- throw error;
182
- } finally {
183
- if (structures) {
184
- resetStructures();
185
- if (hasSharedUpdate && packr.saveStructures) {
186
- let sharedLength = structures.sharedLength || 0
187
- // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
188
- let returnBuffer = target.subarray(start, position)
189
- let newSharedData = prepareStructures(structures, packr);
190
- if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
191
- if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
192
- // get updated structures and try again if the update failed
193
- return packr.pack(value, encodeOptions)
194
- }
195
- packr.lastNamedStructuresLength = sharedLength
196
- // don't keep large buffers around
197
- if (target.length > 0x40000000) target = null
198
- return returnBuffer
199
- }
200
- }
201
- }
202
- // don't keep large buffers around, they take too much memory and cause problems (limit at 1GB)
203
- if (target.length > 0x40000000) target = null
204
- if (encodeOptions & RESET_BUFFER_MODE)
205
- position = start
206
- }
207
- }
208
- const resetStructures = () => {
209
- if (serializationsSinceTransitionRebuild < 10)
210
- serializationsSinceTransitionRebuild++
211
- let sharedLength = structures.sharedLength || 0
212
- if (structures.length > sharedLength && !isSequential)
213
- structures.length = sharedLength
214
- if (transitionsCount > 10000) {
215
- // force a rebuild occasionally after a lot of transitions so it can get cleaned up
216
- structures.transitions = null
217
- serializationsSinceTransitionRebuild = 0
218
- transitionsCount = 0
219
- if (recordIdsToRemove.length > 0)
220
- recordIdsToRemove = []
221
- } else if (recordIdsToRemove.length > 0 && !isSequential) {
222
- for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
223
- recordIdsToRemove[i][RECORD_SYMBOL] = 0
224
- }
225
- recordIdsToRemove = []
226
- }
227
- }
228
- const packArray = (value) => {
229
- var length = value.length
230
- if (length < 0x10) {
231
- target[position++] = 0x90 | length
232
- } else if (length < 0x10000) {
233
- target[position++] = 0xdc
234
- target[position++] = length >> 8
235
- target[position++] = length & 0xff
236
- } else {
237
- target[position++] = 0xdd
238
- targetView.setUint32(position, length)
239
- position += 4
240
- }
241
- for (let i = 0; i < length; i++) {
242
- pack(value[i])
243
- }
244
- }
245
- const pack = (value) => {
246
- if (position > safeEnd)
247
- target = makeRoom(position)
248
-
249
- var type = typeof value
250
- var length
251
- if (type === 'string') {
252
- let strLength = value.length
253
- if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
254
- if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
255
- let extStart
256
- let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10
257
- if (position + maxBytes > safeEnd)
258
- target = makeRoom(position + maxBytes)
259
- let lastBundle
260
- if (bundledStrings.position) { // here we use the 0x62 extension to write the last bundle and reserve space for the reference pointer to the next/current bundle
261
- lastBundle = bundledStrings
262
- target[position] = 0xc8 // ext 16
263
- position += 3 // reserve for the writing bundle size
264
- target[position++] = 0x62 // 'b'
265
- extStart = position - start
266
- position += 4 // reserve for writing bundle reference
267
- writeBundles(start, pack, 0) // write the last bundles
268
- targetView.setUint16(extStart + start - 3, position - start - extStart)
269
- } else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
270
- target[position++] = 0xd6 // fixext 4
271
- target[position++] = 0x62 // 'b'
272
- extStart = position - start
273
- position += 4 // reserve for writing bundle reference
274
- }
275
- bundledStrings = ['', ''] // create new ones
276
- bundledStrings.previous = lastBundle;
277
- bundledStrings.size = 0
278
- bundledStrings.position = extStart
279
- }
280
- let twoByte = hasNonLatin.test(value)
281
- bundledStrings[twoByte ? 0 : 1] += value
282
- target[position++] = 0xc1
283
- pack(twoByte ? -strLength : strLength);
284
- return
285
- }
286
- let headerSize
287
- // first we estimate the header size, so we can write to the correct location
288
- if (strLength < 0x20) {
289
- headerSize = 1
290
- } else if (strLength < 0x100) {
291
- headerSize = 2
292
- } else if (strLength < 0x10000) {
293
- headerSize = 3
294
- } else {
295
- headerSize = 5
296
- }
297
- let maxBytes = strLength * 3
298
- if (position + maxBytes > safeEnd)
299
- target = makeRoom(position + maxBytes)
300
-
301
- if (strLength < 0x40 || !encodeUtf8) {
302
- let i, c1, c2, strPosition = position + headerSize
303
- for (i = 0; i < strLength; i++) {
304
- c1 = value.charCodeAt(i)
305
- if (c1 < 0x80) {
306
- target[strPosition++] = c1
307
- } else if (c1 < 0x800) {
308
- target[strPosition++] = c1 >> 6 | 0xc0
309
- target[strPosition++] = c1 & 0x3f | 0x80
310
- } else if (
311
- (c1 & 0xfc00) === 0xd800 &&
312
- ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
313
- ) {
314
- c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff)
315
- i++
316
- target[strPosition++] = c1 >> 18 | 0xf0
317
- target[strPosition++] = c1 >> 12 & 0x3f | 0x80
318
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80
319
- target[strPosition++] = c1 & 0x3f | 0x80
320
- } else {
321
- target[strPosition++] = c1 >> 12 | 0xe0
322
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80
323
- target[strPosition++] = c1 & 0x3f | 0x80
324
- }
325
- }
326
- length = strPosition - position - headerSize
327
- } else {
328
- length = encodeUtf8(value, position + headerSize)
329
- }
330
-
331
- if (length < 0x20) {
332
- target[position++] = 0xa0 | length
333
- } else if (length < 0x100) {
334
- if (headerSize < 2) {
335
- target.copyWithin(position + 2, position + 1, position + 1 + length)
336
- }
337
- target[position++] = 0xd9
338
- target[position++] = length
339
- } else if (length < 0x10000) {
340
- if (headerSize < 3) {
341
- target.copyWithin(position + 3, position + 2, position + 2 + length)
342
- }
343
- target[position++] = 0xda
344
- target[position++] = length >> 8
345
- target[position++] = length & 0xff
346
- } else {
347
- if (headerSize < 5) {
348
- target.copyWithin(position + 5, position + 3, position + 3 + length)
349
- }
350
- target[position++] = 0xdb
351
- targetView.setUint32(position, length)
352
- position += 4
353
- }
354
- position += length
355
- } else if (type === 'number') {
356
- if (value >>> 0 === value) {// positive integer, 32-bit or less
357
- // positive uint
358
- if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
359
- target[position++] = value
360
- } else if (value < 0x100) {
361
- target[position++] = 0xcc
362
- target[position++] = value
363
- } else if (value < 0x10000) {
364
- target[position++] = 0xcd
365
- target[position++] = value >> 8
366
- target[position++] = value & 0xff
367
- } else {
368
- target[position++] = 0xce
369
- targetView.setUint32(position, value)
370
- position += 4
371
- }
372
- } else if (value >> 0 === value) { // negative integer
373
- if (value >= -0x20) {
374
- target[position++] = 0x100 + value
375
- } else if (value >= -0x80) {
376
- target[position++] = 0xd0
377
- target[position++] = value + 0x100
378
- } else if (value >= -0x8000) {
379
- target[position++] = 0xd1
380
- targetView.setInt16(position, value)
381
- position += 2
382
- } else {
383
- target[position++] = 0xd2
384
- targetView.setInt32(position, value)
385
- position += 4
386
- }
387
- } else {
388
- let useFloat32
389
- if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
390
- target[position++] = 0xca
391
- targetView.setFloat32(position, value)
392
- let xShifted
393
- if (useFloat32 < 4 ||
394
- // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
395
- ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
396
- position += 4
397
- return
398
- } else
399
- position-- // move back into position for writing a double
400
- }
401
- target[position++] = 0xcb
402
- targetView.setFloat64(position, value)
403
- position += 8
404
- }
405
- } else if (type === 'object' || type === 'function') {
406
- if (!value)
407
- target[position++] = 0xc0
408
- else {
409
- if (referenceMap) {
410
- let referee = referenceMap.get(value)
411
- if (referee) {
412
- if (!referee.id) {
413
- let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = [])
414
- referee.id = idsToInsert.push(referee)
415
- }
416
- target[position++] = 0xd6 // fixext 4
417
- target[position++] = 0x70 // "p" for pointer
418
- targetView.setUint32(position, referee.id)
419
- position += 4
420
- return
421
- } else
422
- referenceMap.set(value, { offset: position - start })
423
- }
424
- let constructor = value.constructor
425
- if (constructor === Object) {
426
- writeObject(value)
427
- } else if (constructor === Array) {
428
- packArray(value)
429
- } else if (constructor === Map) {
430
- if (this.mapAsEmptyObject) target[position++] = 0x80
431
- else {
432
- length = value.size
433
- if (length < 0x10) {
434
- target[position++] = 0x80 | length
435
- } else if (length < 0x10000) {
436
- target[position++] = 0xde
437
- target[position++] = length >> 8
438
- target[position++] = length & 0xff
439
- } else {
440
- target[position++] = 0xdf
441
- targetView.setUint32(position, length)
442
- position += 4
443
- }
444
- for (let [key, entryValue] of value) {
445
- pack(key)
446
- pack(entryValue)
447
- }
448
- }
449
- } else {
450
- for (let i = 0, l = extensions.length; i < l; i++) {
451
- let extensionClass = extensionClasses[i]
452
- if (value instanceof extensionClass) {
453
- let extension = extensions[i]
454
- if (extension.write) {
455
- if (extension.type) {
456
- target[position++] = 0xd4 // one byte "tag" extension
457
- target[position++] = extension.type
458
- target[position++] = 0
459
- }
460
- let writeResult = extension.write.call(this, value)
461
- if (writeResult === value) { // avoid infinite recursion
462
- if (Array.isArray(value)) {
463
- packArray(value)
464
- } else {
465
- writeObject(value)
466
- }
467
- } else {
468
- pack(writeResult)
469
- }
470
- return
471
- }
472
- let currentTarget = target
473
- let currentTargetView = targetView
474
- let currentPosition = position
475
- target = null
476
- let result
477
- try {
478
- result = extension.pack.call(this, value, (size) => {
479
- // restore target and use it
480
- target = currentTarget
481
- currentTarget = null
482
- position += size
483
- if (position > safeEnd)
484
- makeRoom(position)
485
- return {
486
- target, targetView, position: position - size
487
- }
488
- }, pack)
489
- } finally {
490
- // restore current target information (unless already restored)
491
- if (currentTarget) {
492
- target = currentTarget
493
- targetView = currentTargetView
494
- position = currentPosition
495
- safeEnd = target.length - 10
496
- }
497
- }
498
- if (result) {
499
- if (result.length + position > safeEnd)
500
- makeRoom(result.length + position)
501
- position = writeExtensionData(result, target, position, extension.type)
502
- }
503
- return
504
- }
505
- }
506
- // check isArray after extensions, because extensions can extend Array
507
- if (Array.isArray(value)) {
508
- packArray(value)
509
- } else {
510
- // use this as an alternate mechanism for expressing how to serialize
511
- if (value.toJSON) {
512
- const json = value.toJSON()
513
- // if for some reason value.toJSON returns itself it'll loop forever
514
- if (json !== value)
515
- return pack(json)
516
- }
517
-
518
- // if there is a writeFunction, use it, otherwise just encode as undefined
519
- if (type === 'function')
520
- return pack(this.writeFunction && this.writeFunction(value));
521
-
522
- // no extension found, write as plain object
523
- writeObject(value)
524
- }
525
- }
526
- }
527
- } else if (type === 'boolean') {
528
- target[position++] = value ? 0xc3 : 0xc2
529
- } else if (type === 'bigint') {
530
- if (value < 0x8000000000000000 && value >= -0x8000000000000000) {
531
- // use a signed int as long as it fits
532
- target[position++] = 0xd3
533
- targetView.setBigInt64(position, value)
534
- } else if (value < 0x10000000000000000 && value > 0) {
535
- // if we can fit an unsigned int, use that
536
- target[position++] = 0xcf
537
- targetView.setBigUint64(position, value)
538
- } else {
539
- // overflow
540
- if (this.largeBigIntToFloat) {
541
- target[position++] = 0xcb
542
- targetView.setFloat64(position, Number(value))
543
- } else if (this.largeBigIntToString) {
544
- return pack(value.toString());
545
- } else if (this.useBigIntExtension || this.moreTypes) {
546
- let empty = value < 0 ? BigInt(-1) : BigInt(0)
547
-
548
- let array
549
- if (value >> BigInt(0x10000) === empty) {
550
- let mask = BigInt(0x10000000000000000) - BigInt(1) // literal would overflow
551
- let chunks = []
552
- while (true) {
553
- chunks.push(value & mask)
554
- if ((value >> BigInt(63)) === empty) break
555
- value >>= BigInt(64)
556
- }
557
-
558
- array = new Uint8Array(new BigUint64Array(chunks).buffer)
559
- array.reverse()
560
- } else {
561
- let invert = value < 0
562
- let string = (invert ? ~value : value).toString(16)
563
- if (string.length % 2) {
564
- string = '0' + string
565
- } else if (parseInt(string.charAt(0), 16) >= 8) {
566
- string = '00' + string
567
- }
568
-
569
- if (hasNodeBuffer) {
570
- array = Buffer.from(string, 'hex')
571
- } else {
572
- array = new Uint8Array(string.length / 2)
573
- for (let i = 0; i < array.length; i++) {
574
- array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16)
575
- }
576
- }
577
-
578
- if (invert) {
579
- for (let i = 0; i < array.length; i++) array[i] = ~array[i]
580
- }
581
- }
582
-
583
- if (array.length + position > safeEnd)
584
- makeRoom(array.length + position)
585
- position = writeExtensionData(array, target, position, 0x42)
586
- return
587
- } else {
588
- throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
589
- ' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
590
- ' largeBigIntToString to convert to string')
591
- }
592
- }
593
- position += 8
594
- } else if (type === 'undefined') {
595
- if (this.encodeUndefinedAsNil)
596
- target[position++] = 0xc0
597
- else {
598
- target[position++] = 0xd4 // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
599
- target[position++] = 0
600
- target[position++] = 0
601
- }
602
- } else {
603
- throw new Error('Unknown type: ' + type)
604
- }
605
- }
606
-
607
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
608
- // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
609
- let keys;
610
- if (this.skipValues) {
611
- keys = [];
612
- for (let key in object) {
613
- if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
614
- !this.skipValues.includes(object[key]))
615
- keys.push(key);
616
- }
617
- } else {
618
- keys = Object.keys(object)
619
- }
620
- let length = keys.length
621
- if (length < 0x10) {
622
- target[position++] = 0x80 | length
623
- } else if (length < 0x10000) {
624
- target[position++] = 0xde
625
- target[position++] = length >> 8
626
- target[position++] = length & 0xff
627
- } else {
628
- target[position++] = 0xdf
629
- targetView.setUint32(position, length)
630
- position += 4
631
- }
632
- let key
633
- if (this.coercibleKeyAsNumber) {
634
- for (let i = 0; i < length; i++) {
635
- key = keys[i]
636
- let num = Number(key)
637
- pack(isNaN(num) ? key : num)
638
- pack(object[key])
639
- }
640
-
641
- } else {
642
- for (let i = 0; i < length; i++) {
643
- pack(key = keys[i])
644
- pack(object[key])
645
- }
646
- }
647
- } :
648
- (object) => {
649
- target[position++] = 0xde // always using map 16, so we can preallocate and set the length afterwards
650
- let objectOffset = position - start
651
- position += 2
652
- let size = 0
653
- for (let key in object) {
654
- if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
655
- pack(key)
656
- pack(object[key])
657
- size++
658
- }
659
- }
660
- if (size > 0xffff) {
661
- throw new Error('Object is too large to serialize with fast 16-bit map size,' +
662
- ' use the "variableMapSize" option to serialize this object');
663
- }
664
- target[objectOffset++ + start] = size >> 8
665
- target[objectOffset + start] = size & 0xff
666
- }
667
-
668
- const writeRecord = this.useRecords === false ? writePlainObject :
669
- (options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
670
- (object) => {
671
- let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null))
672
- let objectOffset = position++ - start
673
- let wroteKeys
674
- for (let key in object) {
675
- if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
676
- nextTransition = transition[key]
677
- if (nextTransition)
678
- transition = nextTransition
679
- else {
680
- // record doesn't exist, create full new record and insert it
681
- let keys = Object.keys(object)
682
- let lastTransition = transition
683
- transition = structures.transitions
684
- let newTransitions = 0
685
- for (let i = 0, l = keys.length; i < l; i++) {
686
- let key = keys[i]
687
- nextTransition = transition[key]
688
- if (!nextTransition) {
689
- nextTransition = transition[key] = Object.create(null)
690
- newTransitions++
691
- }
692
- transition = nextTransition
693
- }
694
- if (objectOffset + start + 1 == position) {
695
- // first key, so we don't need to insert, we can just write record directly
696
- position--
697
- newRecord(transition, keys, newTransitions)
698
- } else // otherwise we need to insert the record, moving existing data after the record
699
- insertNewRecord(transition, keys, objectOffset, newTransitions)
700
- wroteKeys = true
701
- transition = lastTransition[key]
702
- }
703
- pack(object[key])
704
- }
705
- }
706
- if (!wroteKeys) {
707
- let recordId = transition[RECORD_SYMBOL]
708
- if (recordId)
709
- target[objectOffset + start] = recordId
710
- else
711
- insertNewRecord(transition, Object.keys(object), objectOffset, 0)
712
- }
713
- } :
714
- (object) => {
715
- let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null))
716
- let newTransitions = 0
717
- for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
718
- nextTransition = transition[key]
719
- if (!nextTransition) {
720
- nextTransition = transition[key] = Object.create(null)
721
- newTransitions++
722
- }
723
- transition = nextTransition
724
- }
725
- let recordId = transition[RECORD_SYMBOL]
726
- if (recordId) {
727
- if (recordId >= 0x60 && useTwoByteRecords) {
728
- target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60
729
- target[position++] = recordId >> 5
730
- } else
731
- target[position++] = recordId
732
- } else {
733
- newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions)
734
- }
735
- // now write the values
736
- for (let key in object)
737
- if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
738
- pack(object[key])
739
- }
740
- }
741
-
742
- // create reference to useRecords if useRecords is a function
743
- const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
744
-
745
- const writeObject = checkUseRecords ? (object) => {
746
- checkUseRecords(object) ? writeRecord(object) : writePlainObject(object)
747
- } : writeRecord
748
-
749
- const makeRoom = (end) => {
750
- let newSize
751
- if (end > 0x1000000) {
752
- // special handling for really large buffers
753
- if ((end - start) > MAX_BUFFER_SIZE)
754
- throw new Error('Packed buffer would be larger than maximum buffer size')
755
- newSize = Math.min(MAX_BUFFER_SIZE,
756
- Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000)
757
- } else // faster handling for smaller buffers
758
- newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12
759
- let newBuffer = new ByteArrayAllocate(newSize)
760
- targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize))
761
- end = Math.min(end, target.length)
762
- if (target.copy)
763
- target.copy(newBuffer, 0, start, end)
764
- else
765
- newBuffer.set(target.slice(start, end))
766
- position -= start
767
- start = 0
768
- safeEnd = newBuffer.length - 10
769
- return target = newBuffer
770
- }
771
- const newRecord = (transition, keys, newTransitions) => {
772
- let recordId = structures.nextId
773
- if (!recordId)
774
- recordId = 0x40
775
- if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
776
- recordId = structures.nextOwnId
777
- if (!(recordId < maxStructureId))
778
- recordId = sharedLimitId
779
- structures.nextOwnId = recordId + 1
780
- } else {
781
- if (recordId >= maxStructureId)// cycle back around
782
- recordId = sharedLimitId
783
- structures.nextId = recordId + 1
784
- }
785
- let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1
786
- transition[RECORD_SYMBOL] = recordId
787
- transition.__keys__ = keys
788
- structures[recordId - 0x40] = keys
789
-
790
- if (recordId < sharedLimitId) {
791
- keys.isShared = true
792
- structures.sharedLength = recordId - 0x3f
793
- hasSharedUpdate = true
794
- if (highByte >= 0) {
795
- target[position++] = (recordId & 0x1f) + 0x60
796
- target[position++] = highByte
797
- } else {
798
- target[position++] = recordId
799
- }
800
- } else {
801
- if (highByte >= 0) {
802
- target[position++] = 0xd5 // fixext 2
803
- target[position++] = 0x72 // "r" record defintion extension type
804
- target[position++] = (recordId & 0x1f) + 0x60
805
- target[position++] = highByte
806
- } else {
807
- target[position++] = 0xd4 // fixext 1
808
- target[position++] = 0x72 // "r" record defintion extension type
809
- target[position++] = recordId
810
- }
811
-
812
- if (newTransitions)
813
- transitionsCount += serializationsSinceTransitionRebuild * newTransitions
814
- // record the removal of the id, we can maintain our shared structure
815
- if (recordIdsToRemove.length >= maxOwnStructures)
816
- recordIdsToRemove.shift()[RECORD_SYMBOL] = 0 // we are cycling back through, and have to remove old ones
817
- recordIdsToRemove.push(transition)
818
- pack(keys)
819
- }
820
- }
821
- const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
822
- let mainTarget = target
823
- let mainPosition = position
824
- let mainSafeEnd = safeEnd
825
- let mainStart = start
826
- target = keysTarget
827
- position = 0
828
- start = 0
829
- if (!target)
830
- keysTarget = target = new ByteArrayAllocate(8192)
831
- safeEnd = target.length - 10
832
- newRecord(transition, keys, newTransitions)
833
- keysTarget = target
834
- let keysPosition = position
835
- target = mainTarget
836
- position = mainPosition
837
- safeEnd = mainSafeEnd
838
- start = mainStart
839
- if (keysPosition > 1) {
840
- let newEnd = position + keysPosition - 1
841
- if (newEnd > safeEnd)
842
- makeRoom(newEnd)
843
- let insertionPosition = insertionOffset + start
844
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position)
845
- target.set(keysTarget.slice(0, keysPosition), insertionPosition)
846
- position = newEnd
847
- } else {
848
- target[insertionOffset + start] = keysTarget[0]
849
- }
850
- }
851
- const writeStruct = (object) => {
852
- let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
853
- if (notifySharedUpdate)
854
- return hasSharedUpdate = true;
855
- position = newPosition;
856
- let startTarget = target;
857
- pack(value);
858
- resetStructures();
859
- if (startTarget !== target) {
860
- return { position, targetView, target }; // indicate the buffer was re-allocated
861
- }
862
- return position;
863
- }, this);
864
- if (newPosition === 0) // bail and go to a msgpack object
865
- return writeObject(object);
866
- position = newPosition;
867
- }
868
- }
869
- useBuffer(buffer) {
870
- // this means we are finished using our own buffer and we can write over it safely
871
- target = buffer
872
- target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength))
873
- targetView = target.dataView;
874
- position = 0
875
- }
876
- set position (value) {
877
- position = value;
878
- }
879
- get position() {
880
- return position;
881
- }
882
- clearSharedData() {
883
- if (this.structures)
884
- this.structures = []
885
- if (this.typedStructs)
886
- this.typedStructs = []
887
- }
888
- }
889
-
890
- extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, DataView, C1Type ]
891
- extensions = [{
892
- pack(date, allocateForWrite, pack) {
893
- let seconds = date.getTime() / 1000
894
- if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
895
- // Timestamp 32
896
- let { target, targetView, position} = allocateForWrite(6)
897
- target[position++] = 0xd6
898
- target[position++] = 0xff
899
- targetView.setUint32(position, seconds)
900
- } else if (seconds > 0 && seconds < 0x100000000) {
901
- // Timestamp 64
902
- let { target, targetView, position} = allocateForWrite(10)
903
- target[position++] = 0xd7
904
- target[position++] = 0xff
905
- targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0))
906
- targetView.setUint32(position + 4, seconds)
907
- } else if (isNaN(seconds)) {
908
- if (this.onInvalidDate) {
909
- allocateForWrite(0)
910
- return pack(this.onInvalidDate())
911
- }
912
- // Intentionally invalid timestamp
913
- let { target, targetView, position} = allocateForWrite(3)
914
- target[position++] = 0xd4
915
- target[position++] = 0xff
916
- target[position++] = 0xff
917
- } else {
918
- // Timestamp 96
919
- let { target, targetView, position} = allocateForWrite(15)
920
- target[position++] = 0xc7
921
- target[position++] = 12
922
- target[position++] = 0xff
923
- targetView.setUint32(position, date.getMilliseconds() * 1000000)
924
- targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)))
925
- }
926
- }
927
- }, {
928
- pack(set, allocateForWrite, pack) {
929
- if (this.setAsEmptyObject) {
930
- allocateForWrite(0);
931
- return pack({})
932
- }
933
- let array = Array.from(set)
934
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
935
- if (this.moreTypes) {
936
- target[position++] = 0xd4
937
- target[position++] = 0x73 // 's' for Set
938
- target[position++] = 0
939
- }
940
- pack(array)
941
- }
942
- }, {
943
- pack(error, allocateForWrite, pack) {
944
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
945
- if (this.moreTypes) {
946
- target[position++] = 0xd4
947
- target[position++] = 0x65 // 'e' for error
948
- target[position++] = 0
949
- }
950
- pack([ error.name, error.message, error.cause ])
951
- }
952
- }, {
953
- pack(regex, allocateForWrite, pack) {
954
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0)
955
- if (this.moreTypes) {
956
- target[position++] = 0xd4
957
- target[position++] = 0x78 // 'x' for regeXp
958
- target[position++] = 0
959
- }
960
- pack([ regex.source, regex.flags ])
961
- }
962
- }, {
963
- pack(arrayBuffer, allocateForWrite) {
964
- if (this.moreTypes)
965
- writeExtBuffer(arrayBuffer, 0x10, allocateForWrite)
966
- else
967
- writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite)
968
- }
969
- }, {
970
- pack(typedArray, allocateForWrite) {
971
- let constructor = typedArray.constructor
972
- if (constructor !== ByteArray && this.moreTypes)
973
- writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite)
974
- else
975
- writeBuffer(typedArray, allocateForWrite)
976
- }
977
- }, {
978
- pack(arrayBuffer, allocateForWrite) {
979
- if (this.moreTypes)
980
- writeExtBuffer(arrayBuffer, 0x11, allocateForWrite)
981
- else
982
- writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite)
983
- }
984
- }, {
985
- pack(c1, allocateForWrite) { // specific 0xC1 object
986
- let { target, position} = allocateForWrite(1)
987
- target[position] = 0xc1
988
- }
989
- }]
990
-
991
- function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
992
- let length = typedArray.byteLength
993
- if (length + 1 < 0x100) {
994
- var { target, position } = allocateForWrite(4 + length)
995
- target[position++] = 0xc7
996
- target[position++] = length + 1
997
- } else if (length + 1 < 0x10000) {
998
- var { target, position } = allocateForWrite(5 + length)
999
- target[position++] = 0xc8
1000
- target[position++] = (length + 1) >> 8
1001
- target[position++] = (length + 1) & 0xff
1002
- } else {
1003
- var { target, position, targetView } = allocateForWrite(7 + length)
1004
- target[position++] = 0xc9
1005
- targetView.setUint32(position, length + 1) // plus one for the type byte
1006
- position += 4
1007
- }
1008
- target[position++] = 0x74 // "t" for typed array
1009
- target[position++] = type
1010
- if (!typedArray.buffer) typedArray = new Uint8Array(typedArray)
1011
- target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position)
1012
- }
1013
- function writeBuffer(buffer, allocateForWrite) {
1014
- let length = buffer.byteLength
1015
- var target, position
1016
- if (length < 0x100) {
1017
- var { target, position } = allocateForWrite(length + 2)
1018
- target[position++] = 0xc4
1019
- target[position++] = length
1020
- } else if (length < 0x10000) {
1021
- var { target, position } = allocateForWrite(length + 3)
1022
- target[position++] = 0xc5
1023
- target[position++] = length >> 8
1024
- target[position++] = length & 0xff
1025
- } else {
1026
- var { target, position, targetView } = allocateForWrite(length + 5)
1027
- target[position++] = 0xc6
1028
- targetView.setUint32(position, length)
1029
- position += 4
1030
- }
1031
- target.set(buffer, position)
1032
- }
1033
-
1034
- function writeExtensionData(result, target, position, type) {
1035
- let length = result.length
1036
- switch (length) {
1037
- case 1:
1038
- target[position++] = 0xd4
1039
- break
1040
- case 2:
1041
- target[position++] = 0xd5
1042
- break
1043
- case 4:
1044
- target[position++] = 0xd6
1045
- break
1046
- case 8:
1047
- target[position++] = 0xd7
1048
- break
1049
- case 16:
1050
- target[position++] = 0xd8
1051
- break
1052
- default:
1053
- if (length < 0x100) {
1054
- target[position++] = 0xc7
1055
- target[position++] = length
1056
- } else if (length < 0x10000) {
1057
- target[position++] = 0xc8
1058
- target[position++] = length >> 8
1059
- target[position++] = length & 0xff
1060
- } else {
1061
- target[position++] = 0xc9
1062
- target[position++] = length >> 24
1063
- target[position++] = (length >> 16) & 0xff
1064
- target[position++] = (length >> 8) & 0xff
1065
- target[position++] = length & 0xff
1066
- }
1067
- }
1068
- target[position++] = type
1069
- target.set(result, position)
1070
- position += length
1071
- return position
1072
- }
1073
-
1074
- function insertIds(serialized, idsToInsert) {
1075
- // insert the ids that need to be referenced for structured clones
1076
- let nextId
1077
- let distanceToMove = idsToInsert.length * 6
1078
- let lastEnd = serialized.length - distanceToMove
1079
- while (nextId = idsToInsert.pop()) {
1080
- let offset = nextId.offset
1081
- let id = nextId.id
1082
- serialized.copyWithin(offset + distanceToMove, offset, lastEnd)
1083
- distanceToMove -= 6
1084
- let position = offset + distanceToMove
1085
- serialized[position++] = 0xd6
1086
- serialized[position++] = 0x69 // 'i'
1087
- serialized[position++] = id >> 24
1088
- serialized[position++] = (id >> 16) & 0xff
1089
- serialized[position++] = (id >> 8) & 0xff
1090
- serialized[position++] = id & 0xff
1091
- lastEnd = offset
1092
- }
1093
- return serialized
1094
- }
1095
-
1096
- function writeBundles(start, pack, incrementPosition) {
1097
- if (bundledStrings.length > 0) {
1098
- targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start)
1099
- bundledStrings.stringsPosition = position - start;
1100
- let writeStrings = bundledStrings
1101
- bundledStrings = null
1102
- pack(writeStrings[0])
1103
- pack(writeStrings[1])
1104
- }
1105
- }
1106
-
1107
- export function addExtension(extension) {
1108
- if (extension.Class) {
1109
- if (!extension.pack && !extension.write)
1110
- throw new Error('Extension has no pack or write function')
1111
- if (extension.pack && !extension.type)
1112
- throw new Error('Extension has no type (numeric code to identify the extension)')
1113
- extensionClasses.unshift(extension.Class)
1114
- extensions.unshift(extension)
1115
- }
1116
- unpackAddExtension(extension)
1117
- }
1118
- function prepareStructures(structures, packr) {
1119
- structures.isCompatible = (existingStructures) => {
1120
- let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length)
1121
- if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
1122
- packr._mergeStructures(existingStructures);
1123
- return compatible;
1124
- }
1125
- return structures
1126
- }
1127
- export function setWriteStructSlots(writeSlots, makeStructures) {
1128
- writeStructSlots = writeSlots;
1129
- prepareStructures = makeStructures;
1130
- }
1131
-
1132
- let defaultPackr = new Packr({ useRecords: false })
1133
- export const pack = defaultPackr.pack
1134
- export const encode = defaultPackr.pack
1135
- export const Encoder = Packr
1136
- export { FLOAT32_OPTIONS } from './unpack.js'
1137
- import { FLOAT32_OPTIONS } from './unpack.js'
1138
- export const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS
1139
- export const REUSE_BUFFER_MODE = 512
1140
- export const RESET_BUFFER_MODE = 1024
1141
- export const RESERVE_START_SPACE = 2048