jaelis-node 1.3.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +97 -8
  2. package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +454 -0
  3. package/lib/JAELIS-VM/lib/adapters/index.js +411 -0
  4. package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +457 -0
  5. package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +1097 -0
  6. package/lib/JAELIS-VM/lib/execution/engine.js +1183 -0
  7. package/lib/JAELIS-VM/lib/index.js +440 -0
  8. package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +543 -0
  9. package/lib/JAELIS-VM/lib/serialization/serializer.js +819 -0
  10. package/lib/JAELIS-VM/lib/state/state-manager.js +1116 -0
  11. package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +1222 -0
  12. package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +836 -0
  13. package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +1127 -0
  14. package/lib/JAELIS-VM/lib/unified/index.js +378 -0
  15. package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +1150 -0
  16. package/lib/JAELIS-VM/lib/unified/unified-compiler.js +1350 -0
  17. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +12 -0
  18. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +17 -0
  19. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +28 -0
  20. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +12 -0
  21. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +17 -0
  22. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +28 -0
  23. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +12 -0
  24. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +12 -0
  25. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +17 -0
  26. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +28 -0
  27. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +12 -0
  28. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +17 -0
  29. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +28 -0
  30. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +17 -0
  31. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +28 -0
  32. package/lib/JAELIS-VM/node_modules/.package-lock.json +127 -0
  33. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +1 -0
  34. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
  35. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
  36. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
  37. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +17 -0
  38. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +1 -0
  39. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
  40. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
  41. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
  42. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +17 -0
  43. package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +21 -0
  44. package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +5 -0
  45. package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +11 -0
  46. package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +60 -0
  47. package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +1 -0
  48. package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +50 -0
  49. package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +198 -0
  50. package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +21 -0
  51. package/lib/JAELIS-VM/node_modules/cbor-x/README.md +380 -0
  52. package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +11 -0
  53. package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +73 -0
  54. package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +11 -0
  55. package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +2 -0
  56. package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +1300 -0
  57. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +1244 -0
  58. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +1 -0
  59. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +2509 -0
  60. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +1 -0
  61. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +2 -0
  62. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +1 -0
  63. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +2508 -0
  64. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +1 -0
  65. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +2 -0
  66. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +1 -0
  67. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +2629 -0
  68. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +1 -0
  69. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +3343 -0
  70. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +1 -0
  71. package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +1 -0
  72. package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +1231 -0
  73. package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +79 -0
  74. package/lib/JAELIS-VM/node_modules/cbor-x/index.js +3 -0
  75. package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +85 -0
  76. package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +24 -0
  77. package/lib/JAELIS-VM/node_modules/cbor-x/package.json +94 -0
  78. package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +88 -0
  79. package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +61 -0
  80. package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +19 -0
  81. package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +201 -0
  82. package/lib/JAELIS-VM/node_modules/detect-libc/README.md +163 -0
  83. package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +14 -0
  84. package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +313 -0
  85. package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +39 -0
  86. package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +51 -0
  87. package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +24 -0
  88. package/lib/JAELIS-VM/node_modules/detect-libc/package.json +44 -0
  89. package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +21 -0
  90. package/lib/JAELIS-VM/node_modules/msgpackr/README.md +372 -0
  91. package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +11 -0
  92. package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +67 -0
  93. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +2407 -0
  94. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +1 -0
  95. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +2 -0
  96. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +1 -0
  97. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +2406 -0
  98. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +1 -0
  99. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +2 -0
  100. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +1 -0
  101. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +3320 -0
  102. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +1 -0
  103. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +4540 -0
  104. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +1 -0
  105. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +1250 -0
  106. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +1 -0
  107. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +91 -0
  108. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +91 -0
  109. package/lib/JAELIS-VM/node_modules/msgpackr/index.js +5 -0
  110. package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +87 -0
  111. package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +25 -0
  112. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +1 -0
  113. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +1 -0
  114. package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +1141 -0
  115. package/lib/JAELIS-VM/node_modules/msgpackr/package.json +104 -0
  116. package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +88 -0
  117. package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +57 -0
  118. package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +815 -0
  119. package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +3 -0
  120. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +2 -0
  121. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +2 -0
  122. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +1221 -0
  123. package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +21 -0
  124. package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +5 -0
  125. package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +13 -0
  126. package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +63 -0
  127. package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +1 -0
  128. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +12 -0
  129. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +12 -0
  130. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +17 -0
  131. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +28 -0
  132. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +12 -0
  133. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +17 -0
  134. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +28 -0
  135. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +17 -0
  136. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +28 -0
  137. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +21 -0
  138. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +58 -0
  139. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +82 -0
  140. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +19 -0
  141. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +6 -0
  142. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +236 -0
  143. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +7 -0
  144. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +32 -0
  145. package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +50 -0
  146. package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +274 -0
  147. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +21 -0
  148. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +58 -0
  149. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +77 -0
  150. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +19 -0
  151. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +224 -0
  152. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +7 -0
  153. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +32 -0
  154. package/lib/JAELIS-VM/package-lock.json +284 -0
  155. package/lib/JAELIS-VM/package.json +38 -0
  156. package/lib/JAELIS-VM/test/comprehensive.test.js +267 -0
  157. package/lib/JAELIS-VM/test/cross-chain-test.js +470 -0
  158. package/lib/JAELIS-VM/test/unified-vm-test.js +459 -0
  159. package/lib/JAELIS-VM/test/unified.test.js +166 -0
  160. package/lib/JAELIS-VM/test/vm.test.js +599 -0
  161. package/lib/index.js +310 -4
  162. package/package.json +2 -2
@@ -0,0 +1,2407 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.msgpackr = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ var decoder;
8
+ try {
9
+ decoder = new TextDecoder();
10
+ } catch(error) {}
11
+ var src;
12
+ var srcEnd;
13
+ var position$1 = 0;
14
+ var currentUnpackr = {};
15
+ var currentStructures;
16
+ var srcString;
17
+ var srcStringStart = 0;
18
+ var srcStringEnd = 0;
19
+ var bundledStrings$1;
20
+ var referenceMap;
21
+ var currentExtensions = [];
22
+ var dataView;
23
+ var defaultOptions = {
24
+ useRecords: false,
25
+ mapsAsObjects: true
26
+ };
27
+ class C1Type {}
28
+ const C1 = new C1Type();
29
+ C1.name = 'MessagePack 0xC1';
30
+ var sequentialMode = false;
31
+ var inlineObjectReadThreshold = 2;
32
+ var readStruct;
33
+ var BlockedFunction; // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
34
+ // no-eval build
35
+ try {
36
+ new BlockedFunction ('');
37
+ } catch(error) {
38
+ // if eval variants are not supported, do not create inline object readers ever
39
+ inlineObjectReadThreshold = Infinity;
40
+ }
41
+
42
+ class Unpackr {
43
+ constructor(options) {
44
+ if (options) {
45
+ if (options.useRecords === false && options.mapsAsObjects === undefined)
46
+ options.mapsAsObjects = true;
47
+ if (options.sequential && options.trusted !== false) {
48
+ options.trusted = true;
49
+ if (!options.structures && options.useRecords != false) {
50
+ options.structures = [];
51
+ if (!options.maxSharedStructures)
52
+ options.maxSharedStructures = 0;
53
+ }
54
+ }
55
+ if (options.structures)
56
+ options.structures.sharedLength = options.structures.length;
57
+ else if (options.getStructures) {
58
+ (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
59
+ options.structures.sharedLength = 0;
60
+ }
61
+ if (options.int64AsNumber) {
62
+ options.int64AsType = 'number';
63
+ }
64
+ }
65
+ Object.assign(this, options);
66
+ }
67
+ unpack(source, options) {
68
+ if (src) {
69
+ // re-entrant execution, save the state and restore it after we do this unpack
70
+ return saveState(() => {
71
+ clearSource();
72
+ return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
73
+ })
74
+ }
75
+ if (!source.buffer && source.constructor === ArrayBuffer)
76
+ source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
77
+ if (typeof options === 'object') {
78
+ srcEnd = options.end || source.length;
79
+ position$1 = options.start || 0;
80
+ } else {
81
+ position$1 = 0;
82
+ srcEnd = options > -1 ? options : source.length;
83
+ }
84
+ srcStringEnd = 0;
85
+ srcString = null;
86
+ bundledStrings$1 = 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$1) === false) return;
123
+ while(position$1 < size) {
124
+ lastPosition = position$1;
125
+ if (forEach(checkedRead(), lastPosition, position$1) === false) {
126
+ return
127
+ }
128
+ }
129
+ }
130
+ else {
131
+ values = [ value ];
132
+ while(position$1 < size) {
133
+ lastPosition = position$1;
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
+ loadedStructures = loadedStructures || [];
149
+ if (Object.isFrozen(loadedStructures))
150
+ loadedStructures = loadedStructures.map(structure => structure.slice(0));
151
+ for (let i = 0, l = loadedStructures.length; i < l; i++) {
152
+ let structure = loadedStructures[i];
153
+ if (structure) {
154
+ structure.isShared = true;
155
+ if (i >= 32)
156
+ structure.highByte = (i - 32) >> 5;
157
+ }
158
+ }
159
+ loadedStructures.sharedLength = loadedStructures.length;
160
+ for (let id in existingStructures || []) {
161
+ if (id >= 0) {
162
+ let structure = loadedStructures[id];
163
+ let existing = existingStructures[id];
164
+ if (existing) {
165
+ if (structure)
166
+ (loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
167
+ loadedStructures[id] = existing;
168
+ }
169
+ }
170
+ }
171
+ return this.structures = loadedStructures
172
+ }
173
+ decode(source, options) {
174
+ return this.unpack(source, options)
175
+ }
176
+ }
177
+ function checkedRead(options) {
178
+ try {
179
+ if (!currentUnpackr.trusted && !sequentialMode) {
180
+ let sharedLength = currentStructures.sharedLength || 0;
181
+ if (sharedLength < currentStructures.length)
182
+ currentStructures.length = sharedLength;
183
+ }
184
+ let result;
185
+ if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct) {
186
+ result = readStruct(src, position$1, srcEnd, currentUnpackr);
187
+ src = null; // dispose of this so that recursive unpack calls don't save state
188
+ if (!(options && options.lazy) && result)
189
+ result = result.toJSON();
190
+ position$1 = srcEnd;
191
+ } else
192
+ result = read();
193
+ if (bundledStrings$1) { // bundled strings to skip past
194
+ position$1 = bundledStrings$1.postBundlePosition;
195
+ bundledStrings$1 = null;
196
+ }
197
+ if (sequentialMode)
198
+ // we only need to restore the structures if there was an error, but if we completed a read,
199
+ // we can clear this out and keep the structures we read
200
+ currentStructures.restoreStructures = null;
201
+
202
+ if (position$1 == srcEnd) {
203
+ // finished reading this source, cleanup references
204
+ if (currentStructures && currentStructures.restoreStructures)
205
+ restoreStructures();
206
+ currentStructures = null;
207
+ src = null;
208
+ if (referenceMap)
209
+ referenceMap = null;
210
+ } else if (position$1 > srcEnd) {
211
+ // over read
212
+ throw new Error('Unexpected end of MessagePack data')
213
+ } else if (!sequentialMode) {
214
+ let jsonView;
215
+ try {
216
+ jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
217
+ } catch(error) {
218
+ jsonView = '(JSON view not available ' + error + ')';
219
+ }
220
+ throw new Error('Data read, but end of buffer not reached ' + jsonView)
221
+ }
222
+ // else more to read, but we are reading sequentially, so don't clear source yet
223
+ return result
224
+ } catch(error) {
225
+ if (currentStructures && currentStructures.restoreStructures)
226
+ restoreStructures();
227
+ clearSource();
228
+ if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
229
+ error.incomplete = true;
230
+ }
231
+ throw error
232
+ }
233
+ }
234
+
235
+ function restoreStructures() {
236
+ for (let id in currentStructures.restoreStructures) {
237
+ currentStructures[id] = currentStructures.restoreStructures[id];
238
+ }
239
+ currentStructures.restoreStructures = null;
240
+ }
241
+
242
+ function read() {
243
+ let token = src[position$1++];
244
+ if (token < 0xa0) {
245
+ if (token < 0x80) {
246
+ if (token < 0x40)
247
+ return token
248
+ else {
249
+ let structure = currentStructures[token & 0x3f] ||
250
+ currentUnpackr.getStructures && loadStructures()[token & 0x3f];
251
+ if (structure) {
252
+ if (!structure.read) {
253
+ structure.read = createStructureReader(structure, token & 0x3f);
254
+ }
255
+ return structure.read()
256
+ } else
257
+ return token
258
+ }
259
+ } else if (token < 0x90) {
260
+ // map
261
+ token -= 0x80;
262
+ if (currentUnpackr.mapsAsObjects) {
263
+ let object = {};
264
+ for (let i = 0; i < token; i++) {
265
+ let key = readKey();
266
+ if (key === '__proto__')
267
+ key = '__proto_';
268
+ object[key] = read();
269
+ }
270
+ return object
271
+ } else {
272
+ let map = new Map();
273
+ for (let i = 0; i < token; i++) {
274
+ map.set(read(), read());
275
+ }
276
+ return map
277
+ }
278
+ } else {
279
+ token -= 0x90;
280
+ let array = new Array(token);
281
+ for (let i = 0; i < token; i++) {
282
+ array[i] = read();
283
+ }
284
+ if (currentUnpackr.freezeData)
285
+ return Object.freeze(array)
286
+ return array
287
+ }
288
+ } else if (token < 0xc0) {
289
+ // fixstr
290
+ let length = token - 0xa0;
291
+ if (srcStringEnd >= position$1) {
292
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
293
+ }
294
+ if (srcStringEnd == 0 && srcEnd < 140) {
295
+ // for small blocks, avoiding the overhead of the extract call is helpful
296
+ let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
297
+ if (string != null)
298
+ return string
299
+ }
300
+ return readFixedString(length)
301
+ } else {
302
+ let value;
303
+ switch (token) {
304
+ case 0xc0: return null
305
+ case 0xc1:
306
+ if (bundledStrings$1) {
307
+ value = read(); // followed by the length of the string in characters (not bytes!)
308
+ if (value > 0)
309
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
310
+ else
311
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
312
+ }
313
+ return C1; // "never-used", return special object to denote that
314
+ case 0xc2: return false
315
+ case 0xc3: return true
316
+ case 0xc4:
317
+ // bin 8
318
+ value = src[position$1++];
319
+ if (value === undefined)
320
+ throw new Error('Unexpected end of buffer')
321
+ return readBin(value)
322
+ case 0xc5:
323
+ // bin 16
324
+ value = dataView.getUint16(position$1);
325
+ position$1 += 2;
326
+ return readBin(value)
327
+ case 0xc6:
328
+ // bin 32
329
+ value = dataView.getUint32(position$1);
330
+ position$1 += 4;
331
+ return readBin(value)
332
+ case 0xc7:
333
+ // ext 8
334
+ return readExt(src[position$1++])
335
+ case 0xc8:
336
+ // ext 16
337
+ value = dataView.getUint16(position$1);
338
+ position$1 += 2;
339
+ return readExt(value)
340
+ case 0xc9:
341
+ // ext 32
342
+ value = dataView.getUint32(position$1);
343
+ position$1 += 4;
344
+ return readExt(value)
345
+ case 0xca:
346
+ value = dataView.getFloat32(position$1);
347
+ if (currentUnpackr.useFloat32 > 2) {
348
+ // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
349
+ let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
350
+ position$1 += 4;
351
+ return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
352
+ }
353
+ position$1 += 4;
354
+ return value
355
+ case 0xcb:
356
+ value = dataView.getFloat64(position$1);
357
+ position$1 += 8;
358
+ return value
359
+ // uint handlers
360
+ case 0xcc:
361
+ return src[position$1++]
362
+ case 0xcd:
363
+ value = dataView.getUint16(position$1);
364
+ position$1 += 2;
365
+ return value
366
+ case 0xce:
367
+ value = dataView.getUint32(position$1);
368
+ position$1 += 4;
369
+ return value
370
+ case 0xcf:
371
+ if (currentUnpackr.int64AsType === 'number') {
372
+ value = dataView.getUint32(position$1) * 0x100000000;
373
+ value += dataView.getUint32(position$1 + 4);
374
+ } else if (currentUnpackr.int64AsType === 'string') {
375
+ value = dataView.getBigUint64(position$1).toString();
376
+ } else if (currentUnpackr.int64AsType === 'auto') {
377
+ value = dataView.getBigUint64(position$1);
378
+ if (value<=BigInt(2)<<BigInt(52)) value=Number(value);
379
+ } else
380
+ value = dataView.getBigUint64(position$1);
381
+ position$1 += 8;
382
+ return value
383
+
384
+ // int handlers
385
+ case 0xd0:
386
+ return dataView.getInt8(position$1++)
387
+ case 0xd1:
388
+ value = dataView.getInt16(position$1);
389
+ position$1 += 2;
390
+ return value
391
+ case 0xd2:
392
+ value = dataView.getInt32(position$1);
393
+ position$1 += 4;
394
+ return value
395
+ case 0xd3:
396
+ if (currentUnpackr.int64AsType === 'number') {
397
+ value = dataView.getInt32(position$1) * 0x100000000;
398
+ value += dataView.getUint32(position$1 + 4);
399
+ } else if (currentUnpackr.int64AsType === 'string') {
400
+ value = dataView.getBigInt64(position$1).toString();
401
+ } else if (currentUnpackr.int64AsType === 'auto') {
402
+ value = dataView.getBigInt64(position$1);
403
+ if (value>=BigInt(-2)<<BigInt(52)&&value<=BigInt(2)<<BigInt(52)) value=Number(value);
404
+ } else
405
+ value = dataView.getBigInt64(position$1);
406
+ position$1 += 8;
407
+ return value
408
+
409
+ case 0xd4:
410
+ // fixext 1
411
+ value = src[position$1++];
412
+ if (value == 0x72) {
413
+ return recordDefinition(src[position$1++] & 0x3f)
414
+ } else {
415
+ let extension = currentExtensions[value];
416
+ if (extension) {
417
+ if (extension.read) {
418
+ position$1++; // skip filler byte
419
+ return extension.read(read())
420
+ } else if (extension.noBuffer) {
421
+ position$1++; // skip filler byte
422
+ return extension()
423
+ } else
424
+ return extension(src.subarray(position$1, ++position$1))
425
+ } else
426
+ throw new Error('Unknown extension ' + value)
427
+ }
428
+ case 0xd5:
429
+ // fixext 2
430
+ value = src[position$1];
431
+ if (value == 0x72) {
432
+ position$1++;
433
+ return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
434
+ } else
435
+ return readExt(2)
436
+ case 0xd6:
437
+ // fixext 4
438
+ return readExt(4)
439
+ case 0xd7:
440
+ // fixext 8
441
+ return readExt(8)
442
+ case 0xd8:
443
+ // fixext 16
444
+ return readExt(16)
445
+ case 0xd9:
446
+ // str 8
447
+ value = src[position$1++];
448
+ if (srcStringEnd >= position$1) {
449
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
450
+ }
451
+ return readString8(value)
452
+ case 0xda:
453
+ // str 16
454
+ value = dataView.getUint16(position$1);
455
+ position$1 += 2;
456
+ if (srcStringEnd >= position$1) {
457
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
458
+ }
459
+ return readString16(value)
460
+ case 0xdb:
461
+ // str 32
462
+ value = dataView.getUint32(position$1);
463
+ position$1 += 4;
464
+ if (srcStringEnd >= position$1) {
465
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
466
+ }
467
+ return readString32(value)
468
+ case 0xdc:
469
+ // array 16
470
+ value = dataView.getUint16(position$1);
471
+ position$1 += 2;
472
+ return readArray(value)
473
+ case 0xdd:
474
+ // array 32
475
+ value = dataView.getUint32(position$1);
476
+ position$1 += 4;
477
+ return readArray(value)
478
+ case 0xde:
479
+ // map 16
480
+ value = dataView.getUint16(position$1);
481
+ position$1 += 2;
482
+ return readMap(value)
483
+ case 0xdf:
484
+ // map 32
485
+ value = dataView.getUint32(position$1);
486
+ position$1 += 4;
487
+ return readMap(value)
488
+ default: // negative int
489
+ if (token >= 0xe0)
490
+ return token - 0x100
491
+ if (token === undefined) {
492
+ let error = new Error('Unexpected end of MessagePack data');
493
+ error.incomplete = true;
494
+ throw error
495
+ }
496
+ throw new Error('Unknown MessagePack token ' + token)
497
+
498
+ }
499
+ }
500
+ }
501
+ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
502
+ function createStructureReader(structure, firstId) {
503
+ function readObject() {
504
+ // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
505
+ if (readObject.count++ > inlineObjectReadThreshold) {
506
+ let readObject = structure.read = (new BlockedFunction ('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
507
+ '({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
508
+ if (structure.highByte === 0)
509
+ structure.read = createSecondByteReader(firstId, structure.read);
510
+ return readObject() // second byte is already read, if there is one so immediately read object
511
+ }
512
+ let object = {};
513
+ for (let i = 0, l = structure.length; i < l; i++) {
514
+ let key = structure[i];
515
+ if (key === '__proto__')
516
+ key = '__proto_';
517
+ object[key] = read();
518
+ }
519
+ if (currentUnpackr.freezeData)
520
+ return Object.freeze(object);
521
+ return object
522
+ }
523
+ readObject.count = 0;
524
+ if (structure.highByte === 0) {
525
+ return createSecondByteReader(firstId, readObject)
526
+ }
527
+ return readObject
528
+ }
529
+
530
+ const createSecondByteReader = (firstId, read0) => {
531
+ return function() {
532
+ let highByte = src[position$1++];
533
+ if (highByte === 0)
534
+ return read0()
535
+ let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
536
+ let structure = currentStructures[id] || loadStructures()[id];
537
+ if (!structure) {
538
+ throw new Error('Record id is not defined for ' + id)
539
+ }
540
+ if (!structure.read)
541
+ structure.read = createStructureReader(structure, firstId);
542
+ return structure.read()
543
+ }
544
+ };
545
+
546
+ function loadStructures() {
547
+ let loadedStructures = saveState(() => {
548
+ // save the state in case getStructures modifies our buffer
549
+ src = null;
550
+ return currentUnpackr.getStructures()
551
+ });
552
+ return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
553
+ }
554
+
555
+ var readFixedString = readStringJS;
556
+ var readString8 = readStringJS;
557
+ var readString16 = readStringJS;
558
+ var readString32 = readStringJS;
559
+ let isNativeAccelerationEnabled = false;
560
+ function readStringJS(length) {
561
+ let result;
562
+ if (length < 16) {
563
+ if (result = shortStringInJS(length))
564
+ return result
565
+ }
566
+ if (length > 64 && decoder)
567
+ return decoder.decode(src.subarray(position$1, position$1 += length))
568
+ const end = position$1 + length;
569
+ const units = [];
570
+ result = '';
571
+ while (position$1 < end) {
572
+ const byte1 = src[position$1++];
573
+ if ((byte1 & 0x80) === 0) {
574
+ // 1 byte
575
+ units.push(byte1);
576
+ } else if ((byte1 & 0xe0) === 0xc0) {
577
+ // 2 bytes
578
+ const byte2 = src[position$1++] & 0x3f;
579
+ units.push(((byte1 & 0x1f) << 6) | byte2);
580
+ } else if ((byte1 & 0xf0) === 0xe0) {
581
+ // 3 bytes
582
+ const byte2 = src[position$1++] & 0x3f;
583
+ const byte3 = src[position$1++] & 0x3f;
584
+ units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
585
+ } else if ((byte1 & 0xf8) === 0xf0) {
586
+ // 4 bytes
587
+ const byte2 = src[position$1++] & 0x3f;
588
+ const byte3 = src[position$1++] & 0x3f;
589
+ const byte4 = src[position$1++] & 0x3f;
590
+ let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
591
+ if (unit > 0xffff) {
592
+ unit -= 0x10000;
593
+ units.push(((unit >>> 10) & 0x3ff) | 0xd800);
594
+ unit = 0xdc00 | (unit & 0x3ff);
595
+ }
596
+ units.push(unit);
597
+ } else {
598
+ units.push(byte1);
599
+ }
600
+
601
+ if (units.length >= 0x1000) {
602
+ result += fromCharCode.apply(String, units);
603
+ units.length = 0;
604
+ }
605
+ }
606
+
607
+ if (units.length > 0) {
608
+ result += fromCharCode.apply(String, units);
609
+ }
610
+
611
+ return result
612
+ }
613
+
614
+ function readArray(length) {
615
+ let array = new Array(length);
616
+ for (let i = 0; i < length; i++) {
617
+ array[i] = read();
618
+ }
619
+ if (currentUnpackr.freezeData)
620
+ return Object.freeze(array)
621
+ return array
622
+ }
623
+
624
+ function readMap(length) {
625
+ if (currentUnpackr.mapsAsObjects) {
626
+ let object = {};
627
+ for (let i = 0; i < length; i++) {
628
+ let key = readKey();
629
+ if (key === '__proto__')
630
+ key = '__proto_';
631
+ object[key] = read();
632
+ }
633
+ return object
634
+ } else {
635
+ let map = new Map();
636
+ for (let i = 0; i < length; i++) {
637
+ map.set(read(), read());
638
+ }
639
+ return map
640
+ }
641
+ }
642
+
643
+ var fromCharCode = String.fromCharCode;
644
+ function longStringInJS(length) {
645
+ let start = position$1;
646
+ let bytes = new Array(length);
647
+ for (let i = 0; i < length; i++) {
648
+ const byte = src[position$1++];
649
+ if ((byte & 0x80) > 0) {
650
+ position$1 = start;
651
+ return
652
+ }
653
+ bytes[i] = byte;
654
+ }
655
+ return fromCharCode.apply(String, bytes)
656
+ }
657
+ function shortStringInJS(length) {
658
+ if (length < 4) {
659
+ if (length < 2) {
660
+ if (length === 0)
661
+ return ''
662
+ else {
663
+ let a = src[position$1++];
664
+ if ((a & 0x80) > 1) {
665
+ position$1 -= 1;
666
+ return
667
+ }
668
+ return fromCharCode(a)
669
+ }
670
+ } else {
671
+ let a = src[position$1++];
672
+ let b = src[position$1++];
673
+ if ((a & 0x80) > 0 || (b & 0x80) > 0) {
674
+ position$1 -= 2;
675
+ return
676
+ }
677
+ if (length < 3)
678
+ return fromCharCode(a, b)
679
+ let c = src[position$1++];
680
+ if ((c & 0x80) > 0) {
681
+ position$1 -= 3;
682
+ return
683
+ }
684
+ return fromCharCode(a, b, c)
685
+ }
686
+ } else {
687
+ let a = src[position$1++];
688
+ let b = src[position$1++];
689
+ let c = src[position$1++];
690
+ let d = src[position$1++];
691
+ if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
692
+ position$1 -= 4;
693
+ return
694
+ }
695
+ if (length < 6) {
696
+ if (length === 4)
697
+ return fromCharCode(a, b, c, d)
698
+ else {
699
+ let e = src[position$1++];
700
+ if ((e & 0x80) > 0) {
701
+ position$1 -= 5;
702
+ return
703
+ }
704
+ return fromCharCode(a, b, c, d, e)
705
+ }
706
+ } else if (length < 8) {
707
+ let e = src[position$1++];
708
+ let f = src[position$1++];
709
+ if ((e & 0x80) > 0 || (f & 0x80) > 0) {
710
+ position$1 -= 6;
711
+ return
712
+ }
713
+ if (length < 7)
714
+ return fromCharCode(a, b, c, d, e, f)
715
+ let g = src[position$1++];
716
+ if ((g & 0x80) > 0) {
717
+ position$1 -= 7;
718
+ return
719
+ }
720
+ return fromCharCode(a, b, c, d, e, f, g)
721
+ } else {
722
+ let e = src[position$1++];
723
+ let f = src[position$1++];
724
+ let g = src[position$1++];
725
+ let h = src[position$1++];
726
+ if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
727
+ position$1 -= 8;
728
+ return
729
+ }
730
+ if (length < 10) {
731
+ if (length === 8)
732
+ return fromCharCode(a, b, c, d, e, f, g, h)
733
+ else {
734
+ let i = src[position$1++];
735
+ if ((i & 0x80) > 0) {
736
+ position$1 -= 9;
737
+ return
738
+ }
739
+ return fromCharCode(a, b, c, d, e, f, g, h, i)
740
+ }
741
+ } else if (length < 12) {
742
+ let i = src[position$1++];
743
+ let j = src[position$1++];
744
+ if ((i & 0x80) > 0 || (j & 0x80) > 0) {
745
+ position$1 -= 10;
746
+ return
747
+ }
748
+ if (length < 11)
749
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j)
750
+ let k = src[position$1++];
751
+ if ((k & 0x80) > 0) {
752
+ position$1 -= 11;
753
+ return
754
+ }
755
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
756
+ } else {
757
+ let i = src[position$1++];
758
+ let j = src[position$1++];
759
+ let k = src[position$1++];
760
+ let l = src[position$1++];
761
+ if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
762
+ position$1 -= 12;
763
+ return
764
+ }
765
+ if (length < 14) {
766
+ if (length === 12)
767
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
768
+ else {
769
+ let m = src[position$1++];
770
+ if ((m & 0x80) > 0) {
771
+ position$1 -= 13;
772
+ return
773
+ }
774
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
775
+ }
776
+ } else {
777
+ let m = src[position$1++];
778
+ let n = src[position$1++];
779
+ if ((m & 0x80) > 0 || (n & 0x80) > 0) {
780
+ position$1 -= 14;
781
+ return
782
+ }
783
+ if (length < 15)
784
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
785
+ let o = src[position$1++];
786
+ if ((o & 0x80) > 0) {
787
+ position$1 -= 15;
788
+ return
789
+ }
790
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
791
+ }
792
+ }
793
+ }
794
+ }
795
+ }
796
+
797
+ function readOnlyJSString() {
798
+ let token = src[position$1++];
799
+ let length;
800
+ if (token < 0xc0) {
801
+ // fixstr
802
+ length = token - 0xa0;
803
+ } else {
804
+ switch(token) {
805
+ case 0xd9:
806
+ // str 8
807
+ length = src[position$1++];
808
+ break
809
+ case 0xda:
810
+ // str 16
811
+ length = dataView.getUint16(position$1);
812
+ position$1 += 2;
813
+ break
814
+ case 0xdb:
815
+ // str 32
816
+ length = dataView.getUint32(position$1);
817
+ position$1 += 4;
818
+ break
819
+ default:
820
+ throw new Error('Expected string')
821
+ }
822
+ }
823
+ return readStringJS(length)
824
+ }
825
+
826
+
827
+ function readBin(length) {
828
+ return currentUnpackr.copyBuffers ?
829
+ // specifically use the copying slice (not the node one)
830
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
831
+ src.subarray(position$1, position$1 += length)
832
+ }
833
+ function readExt(length) {
834
+ let type = src[position$1++];
835
+ if (currentExtensions[type]) {
836
+ let end;
837
+ return currentExtensions[type](src.subarray(position$1, end = (position$1 += length)), (readPosition) => {
838
+ position$1 = readPosition;
839
+ try {
840
+ return read();
841
+ } finally {
842
+ position$1 = end;
843
+ }
844
+ })
845
+ }
846
+ else
847
+ throw new Error('Unknown extension type ' + type)
848
+ }
849
+
850
+ var keyCache = new Array(4096);
851
+ function readKey() {
852
+ let length = src[position$1++];
853
+ if (length >= 0xa0 && length < 0xc0) {
854
+ // fixstr, potentially use key cache
855
+ length = length - 0xa0;
856
+ if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
857
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
858
+ else if (!(srcStringEnd == 0 && srcEnd < 180))
859
+ return readFixedString(length)
860
+ } else { // not cacheable, go back and do a standard read
861
+ position$1--;
862
+ return asSafeString(read())
863
+ }
864
+ let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
865
+ let entry = keyCache[key];
866
+ let checkPosition = position$1;
867
+ let end = position$1 + length - 3;
868
+ let chunk;
869
+ let i = 0;
870
+ if (entry && entry.bytes == length) {
871
+ while (checkPosition < end) {
872
+ chunk = dataView.getUint32(checkPosition);
873
+ if (chunk != entry[i++]) {
874
+ checkPosition = 0x70000000;
875
+ break
876
+ }
877
+ checkPosition += 4;
878
+ }
879
+ end += 3;
880
+ while (checkPosition < end) {
881
+ chunk = src[checkPosition++];
882
+ if (chunk != entry[i++]) {
883
+ checkPosition = 0x70000000;
884
+ break
885
+ }
886
+ }
887
+ if (checkPosition === end) {
888
+ position$1 = checkPosition;
889
+ return entry.string
890
+ }
891
+ end -= 3;
892
+ checkPosition = position$1;
893
+ }
894
+ entry = [];
895
+ keyCache[key] = entry;
896
+ entry.bytes = length;
897
+ while (checkPosition < end) {
898
+ chunk = dataView.getUint32(checkPosition);
899
+ entry.push(chunk);
900
+ checkPosition += 4;
901
+ }
902
+ end += 3;
903
+ while (checkPosition < end) {
904
+ chunk = src[checkPosition++];
905
+ entry.push(chunk);
906
+ }
907
+ // for small blocks, avoiding the overhead of the extract call is helpful
908
+ let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
909
+ if (string != null)
910
+ return entry.string = string
911
+ return entry.string = readFixedString(length)
912
+ }
913
+
914
+ function asSafeString(property) {
915
+ // protect against expensive (DoS) string conversions
916
+ if (typeof property === 'string') return property;
917
+ if (typeof property === 'number' || typeof property === 'boolean' || typeof property === 'bigint') return property.toString();
918
+ if (property == null) return property + '';
919
+ if (currentUnpackr.allowArraysInMapKeys && Array.isArray(property) && property.flat().every(item => ['string', 'number', 'boolean', 'bigint'].includes(typeof item))) {
920
+ return property.flat().toString();
921
+ }
922
+ throw new Error(`Invalid property type for record: ${typeof property}`);
923
+ }
924
+ // the registration of the record definition extension (as "r")
925
+ const recordDefinition = (id, highByte) => {
926
+ let structure = read().map(asSafeString); // ensure that all keys are strings and
927
+ // that the array is mutable
928
+ let firstByte = id;
929
+ if (highByte !== undefined) {
930
+ id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
931
+ structure.highByte = highByte;
932
+ }
933
+ let existingStructure = currentStructures[id];
934
+ // If it is a shared structure, we need to restore any changes after reading.
935
+ // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
936
+ // to the state prior to an incomplete read in order to properly resume.
937
+ if (existingStructure && (existingStructure.isShared || sequentialMode)) {
938
+ (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
939
+ }
940
+ currentStructures[id] = structure;
941
+ structure.read = createStructureReader(structure, firstByte);
942
+ return structure.read()
943
+ };
944
+ currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
945
+ currentExtensions[0].noBuffer = true;
946
+
947
+ currentExtensions[0x42] = data => {
948
+ let headLength = (data.byteLength % 8) || 8;
949
+ let head = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
950
+ for (let i = 1; i < headLength; i++) {
951
+ head <<= BigInt(8);
952
+ head += BigInt(data[i]);
953
+ }
954
+ if (data.byteLength !== headLength) {
955
+ let view = new DataView(data.buffer, data.byteOffset, data.byteLength);
956
+ let decode = (start, end) => {
957
+ let length = end - start;
958
+ if (length <= 40) {
959
+ let out = view.getBigUint64(start);
960
+ for (let i = start + 8; i < end; i += 8) {
961
+ out <<= BigInt(64n);
962
+ out |= view.getBigUint64(i);
963
+ }
964
+ return out
965
+ }
966
+ // if (length === 8) return view.getBigUint64(start)
967
+ let middle = start + (length >> 4 << 3);
968
+ let left = decode(start, middle);
969
+ let right = decode(middle, end);
970
+ return (left << BigInt((end - middle) * 8)) | right
971
+ };
972
+ head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
973
+ }
974
+ return head
975
+ };
976
+
977
+ let errors = {
978
+ Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError: typeof AggregateError === 'function' ? AggregateError : null,
979
+ };
980
+ currentExtensions[0x65] = () => {
981
+ let data = read();
982
+ if (!errors[data[0]]) {
983
+ let error = Error(data[1], { cause: data[2] });
984
+ error.name = data[0];
985
+ return error
986
+ }
987
+ return errors[data[0]](data[1], { cause: data[2] })
988
+ };
989
+
990
+ currentExtensions[0x69] = (data) => {
991
+ // id extension (for structured clones)
992
+ if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
993
+ let id = dataView.getUint32(position$1 - 4);
994
+ if (!referenceMap)
995
+ referenceMap = new Map();
996
+ let token = src[position$1];
997
+ let target;
998
+ // TODO: handle any other types that can cycle and make the code more robust if there are other extensions
999
+ if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
1000
+ target = [];
1001
+ else if (token >= 0x80 && token < 0x90 || token == 0xde || token == 0xdf)
1002
+ target = new Map();
1003
+ else if ((token >= 0xc7 && token <= 0xc9 || token >= 0xd4 && token <= 0xd8) && src[position$1 + 1] === 0x73)
1004
+ target = new Set();
1005
+ else
1006
+ target = {};
1007
+
1008
+ let refEntry = { target }; // a placeholder object
1009
+ referenceMap.set(id, refEntry);
1010
+ let targetProperties = read(); // read the next value as the target object to id
1011
+ if (!refEntry.used) {
1012
+ // no cycle, can just use the returned read object
1013
+ return refEntry.target = targetProperties // replace the placeholder with the real one
1014
+ } else {
1015
+ // there is a cycle, so we have to assign properties to original target
1016
+ Object.assign(target, targetProperties);
1017
+ }
1018
+
1019
+ // copy over map/set entries if we're able to
1020
+ if (target instanceof Map)
1021
+ for (let [k, v] of targetProperties.entries()) target.set(k, v);
1022
+ if (target instanceof Set)
1023
+ for (let i of Array.from(targetProperties)) target.add(i);
1024
+ return target
1025
+ };
1026
+
1027
+ currentExtensions[0x70] = (data) => {
1028
+ // pointer extension (for structured clones)
1029
+ if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1030
+ let id = dataView.getUint32(position$1 - 4);
1031
+ let refEntry = referenceMap.get(id);
1032
+ refEntry.used = true;
1033
+ return refEntry.target
1034
+ };
1035
+
1036
+ currentExtensions[0x73] = () => new Set(read());
1037
+
1038
+ const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
1039
+
1040
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
1041
+ currentExtensions[0x74] = (data) => {
1042
+ let typeCode = data[0];
1043
+ // we always have to slice to get a new ArrayBuffer that is aligned
1044
+ let buffer = Uint8Array.prototype.slice.call(data, 1).buffer;
1045
+
1046
+ let typedArrayName = typedArrays[typeCode];
1047
+ if (!typedArrayName) {
1048
+ if (typeCode === 16) return buffer
1049
+ if (typeCode === 17) return new DataView(buffer)
1050
+ throw new Error('Could not find typed array for code ' + typeCode)
1051
+ }
1052
+ return new glbl[typedArrayName](buffer)
1053
+ };
1054
+ currentExtensions[0x78] = () => {
1055
+ let data = read();
1056
+ return new RegExp(data[0], data[1])
1057
+ };
1058
+ const TEMP_BUNDLE = [];
1059
+ currentExtensions[0x62] = (data) => {
1060
+ let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
1061
+ let dataPosition = position$1;
1062
+ position$1 += dataSize - data.length;
1063
+ bundledStrings$1 = TEMP_BUNDLE;
1064
+ bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
1065
+ bundledStrings$1.position0 = 0;
1066
+ bundledStrings$1.position1 = 0;
1067
+ bundledStrings$1.postBundlePosition = position$1;
1068
+ position$1 = dataPosition;
1069
+ return read()
1070
+ };
1071
+
1072
+ currentExtensions[0xff] = (data) => {
1073
+ // 32-bit date extension
1074
+ if (data.length == 4)
1075
+ return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
1076
+ else if (data.length == 8)
1077
+ return new Date(
1078
+ ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
1079
+ ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
1080
+ else if (data.length == 12)
1081
+ return new Date(
1082
+ ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
1083
+ (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
1084
+ else
1085
+ return new Date('invalid')
1086
+ };
1087
+ // registration of bulk record definition?
1088
+ // currentExtensions[0x52] = () =>
1089
+
1090
+ function saveState(callback) {
1091
+ let savedSrcEnd = srcEnd;
1092
+ let savedPosition = position$1;
1093
+ let savedSrcStringStart = srcStringStart;
1094
+ let savedSrcStringEnd = srcStringEnd;
1095
+ let savedSrcString = srcString;
1096
+ let savedReferenceMap = referenceMap;
1097
+ let savedBundledStrings = bundledStrings$1;
1098
+
1099
+ // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1100
+ let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
1101
+ let savedStructures = currentStructures;
1102
+ let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
1103
+ let savedPackr = currentUnpackr;
1104
+ let savedSequentialMode = sequentialMode;
1105
+ let value = callback();
1106
+ srcEnd = savedSrcEnd;
1107
+ position$1 = savedPosition;
1108
+ srcStringStart = savedSrcStringStart;
1109
+ srcStringEnd = savedSrcStringEnd;
1110
+ srcString = savedSrcString;
1111
+ referenceMap = savedReferenceMap;
1112
+ bundledStrings$1 = savedBundledStrings;
1113
+ src = savedSrc;
1114
+ sequentialMode = savedSequentialMode;
1115
+ currentStructures = savedStructures;
1116
+ currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
1117
+ currentUnpackr = savedPackr;
1118
+ dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1119
+ return value
1120
+ }
1121
+ function clearSource() {
1122
+ src = null;
1123
+ referenceMap = null;
1124
+ currentStructures = null;
1125
+ }
1126
+
1127
+ function addExtension$1(extension) {
1128
+ if (extension.unpack)
1129
+ currentExtensions[extension.type] = extension.unpack;
1130
+ else
1131
+ currentExtensions[extension.type] = extension;
1132
+ }
1133
+
1134
+ const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1135
+ for (let i = 0; i < 256; i++) {
1136
+ mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1137
+ }
1138
+ const Decoder = Unpackr;
1139
+ var defaultUnpackr = new Unpackr({ useRecords: false });
1140
+ const unpack = defaultUnpackr.unpack;
1141
+ const unpackMultiple = defaultUnpackr.unpackMultiple;
1142
+ const decode = defaultUnpackr.unpack;
1143
+ const FLOAT32_OPTIONS = {
1144
+ NEVER: 0,
1145
+ ALWAYS: 1,
1146
+ DECIMAL_ROUND: 3,
1147
+ DECIMAL_FIT: 4
1148
+ };
1149
+ let f32Array = new Float32Array(1);
1150
+ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1151
+ function roundFloat32(float32Number) {
1152
+ f32Array[0] = float32Number;
1153
+ let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1154
+ return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1155
+ }
1156
+
1157
+ let textEncoder;
1158
+ try {
1159
+ textEncoder = new TextEncoder();
1160
+ } catch (error) {}
1161
+ let extensions, extensionClasses;
1162
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
1163
+ const ByteArrayAllocate = hasNodeBuffer ?
1164
+ function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1165
+ const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1166
+ const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1167
+ let target, keysTarget;
1168
+ let targetView;
1169
+ let position = 0;
1170
+ let safeEnd;
1171
+ let bundledStrings = null;
1172
+ let writeStructSlots;
1173
+ const MAX_BUNDLE_SIZE = 0x5500; // maximum characters such that the encoded bytes fits in 16 bits.
1174
+ const hasNonLatin = /[\u0080-\uFFFF]/;
1175
+ const RECORD_SYMBOL = Symbol('record-id');
1176
+ class Packr extends Unpackr {
1177
+ constructor(options) {
1178
+ super(options);
1179
+ this.offset = 0;
1180
+ let start;
1181
+ let hasSharedUpdate;
1182
+ let structures;
1183
+ let referenceMap;
1184
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1185
+ return target.utf8Write(string, position, target.byteLength - position)
1186
+ } : (textEncoder && textEncoder.encodeInto) ?
1187
+ function(string, position) {
1188
+ return textEncoder.encodeInto(string, target.subarray(position)).written
1189
+ } : false;
1190
+
1191
+ let packr = this;
1192
+ if (!options)
1193
+ options = {};
1194
+ let isSequential = options && options.sequential;
1195
+ let hasSharedStructures = options.structures || options.saveStructures;
1196
+ let maxSharedStructures = options.maxSharedStructures;
1197
+ if (maxSharedStructures == null)
1198
+ maxSharedStructures = hasSharedStructures ? 32 : 0;
1199
+ if (maxSharedStructures > 8160)
1200
+ throw new Error('Maximum maxSharedStructure is 8160')
1201
+ if (options.structuredClone && options.moreTypes == undefined) {
1202
+ this.moreTypes = true;
1203
+ }
1204
+ let maxOwnStructures = options.maxOwnStructures;
1205
+ if (maxOwnStructures == null)
1206
+ maxOwnStructures = hasSharedStructures ? 32 : 64;
1207
+ if (!this.structures && options.useRecords != false)
1208
+ this.structures = [];
1209
+ // two byte record ids for shared structures
1210
+ let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1211
+ let sharedLimitId = maxSharedStructures + 0x40;
1212
+ let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1213
+ if (maxStructureId > 8256) {
1214
+ throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
1215
+ }
1216
+ let recordIdsToRemove = [];
1217
+ let transitionsCount = 0;
1218
+ let serializationsSinceTransitionRebuild = 0;
1219
+
1220
+ this.pack = this.encode = function(value, encodeOptions) {
1221
+ if (!target) {
1222
+ target = new ByteArrayAllocate(8192);
1223
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
1224
+ position = 0;
1225
+ }
1226
+ safeEnd = target.length - 10;
1227
+ if (safeEnd - position < 0x800) {
1228
+ // don't start too close to the end,
1229
+ target = new ByteArrayAllocate(target.length);
1230
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1231
+ safeEnd = target.length - 10;
1232
+ position = 0;
1233
+ } else
1234
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1235
+ start = position;
1236
+ if (encodeOptions & RESERVE_START_SPACE) position += (encodeOptions & 0xff);
1237
+ referenceMap = packr.structuredClone ? new Map() : null;
1238
+ if (packr.bundleStrings && typeof value !== 'string') {
1239
+ bundledStrings = [];
1240
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1241
+ } else
1242
+ bundledStrings = null;
1243
+ structures = packr.structures;
1244
+ if (structures) {
1245
+ if (structures.uninitialized)
1246
+ structures = packr._mergeStructures(packr.getStructures());
1247
+ let sharedLength = structures.sharedLength || 0;
1248
+ if (sharedLength > maxSharedStructures) {
1249
+ //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
1250
+ throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
1251
+ }
1252
+ if (!structures.transitions) {
1253
+ // rebuild our structure transitions
1254
+ structures.transitions = Object.create(null);
1255
+ for (let i = 0; i < sharedLength; i++) {
1256
+ let keys = structures[i];
1257
+ if (!keys)
1258
+ continue
1259
+ let nextTransition, transition = structures.transitions;
1260
+ for (let j = 0, l = keys.length; j < l; j++) {
1261
+ let key = keys[j];
1262
+ nextTransition = transition[key];
1263
+ if (!nextTransition) {
1264
+ nextTransition = transition[key] = Object.create(null);
1265
+ }
1266
+ transition = nextTransition;
1267
+ }
1268
+ transition[RECORD_SYMBOL] = i + 0x40;
1269
+ }
1270
+ this.lastNamedStructuresLength = sharedLength;
1271
+ }
1272
+ if (!isSequential) {
1273
+ structures.nextId = sharedLength + 0x40;
1274
+ }
1275
+ }
1276
+ if (hasSharedUpdate)
1277
+ hasSharedUpdate = false;
1278
+ let encodingError;
1279
+ try {
1280
+ if (packr.randomAccessStructure && value && typeof value === 'object') {
1281
+ if (value.constructor === Object) writeStruct(value); // simple object
1282
+ else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
1283
+ // allow user classes, if they don't need special handling (but do use toJSON if available)
1284
+ writeStruct(value.toJSON ? value.toJSON() : value);
1285
+ } else pack(value);
1286
+ } else
1287
+ pack(value);
1288
+ let lastBundle = bundledStrings;
1289
+ if (bundledStrings)
1290
+ writeBundles(start, pack, 0);
1291
+ if (referenceMap && referenceMap.idsToInsert) {
1292
+ let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1293
+ let i = idsToInsert.length;
1294
+ let incrementPosition = -1;
1295
+ while (lastBundle && i > 0) {
1296
+ let insertionPoint = idsToInsert[--i].offset + start;
1297
+ if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
1298
+ incrementPosition = 0;
1299
+ if (insertionPoint > (lastBundle.position + start)) {
1300
+ if (incrementPosition >= 0)
1301
+ incrementPosition += 6;
1302
+ } else {
1303
+ if (incrementPosition >= 0) {
1304
+ // update the bundle reference now
1305
+ targetView.setUint32(lastBundle.position + start,
1306
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1307
+ incrementPosition = -1; // reset
1308
+ }
1309
+ lastBundle = lastBundle.previous;
1310
+ i++;
1311
+ }
1312
+ }
1313
+ if (incrementPosition >= 0 && lastBundle) {
1314
+ // update the bundle reference now
1315
+ targetView.setUint32(lastBundle.position + start,
1316
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1317
+ }
1318
+ position += idsToInsert.length * 6;
1319
+ if (position > safeEnd)
1320
+ makeRoom(position);
1321
+ packr.offset = position;
1322
+ let serialized = insertIds(target.subarray(start, position), idsToInsert);
1323
+ referenceMap = null;
1324
+ return serialized
1325
+ }
1326
+ packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1327
+ if (encodeOptions & REUSE_BUFFER_MODE) {
1328
+ target.start = start;
1329
+ target.end = position;
1330
+ return target
1331
+ }
1332
+ return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1333
+ } catch(error) {
1334
+ encodingError = error;
1335
+ throw error;
1336
+ } finally {
1337
+ if (structures) {
1338
+ resetStructures();
1339
+ if (hasSharedUpdate && packr.saveStructures) {
1340
+ let sharedLength = structures.sharedLength || 0;
1341
+ // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1342
+ let returnBuffer = target.subarray(start, position);
1343
+ let newSharedData = prepareStructures(structures, packr);
1344
+ if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1345
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1346
+ // get updated structures and try again if the update failed
1347
+ return packr.pack(value, encodeOptions)
1348
+ }
1349
+ packr.lastNamedStructuresLength = sharedLength;
1350
+ // don't keep large buffers around
1351
+ if (target.length > 0x40000000) target = null;
1352
+ return returnBuffer
1353
+ }
1354
+ }
1355
+ }
1356
+ // don't keep large buffers around, they take too much memory and cause problems (limit at 1GB)
1357
+ if (target.length > 0x40000000) target = null;
1358
+ if (encodeOptions & RESET_BUFFER_MODE)
1359
+ position = start;
1360
+ }
1361
+ };
1362
+ const resetStructures = () => {
1363
+ if (serializationsSinceTransitionRebuild < 10)
1364
+ serializationsSinceTransitionRebuild++;
1365
+ let sharedLength = structures.sharedLength || 0;
1366
+ if (structures.length > sharedLength && !isSequential)
1367
+ structures.length = sharedLength;
1368
+ if (transitionsCount > 10000) {
1369
+ // force a rebuild occasionally after a lot of transitions so it can get cleaned up
1370
+ structures.transitions = null;
1371
+ serializationsSinceTransitionRebuild = 0;
1372
+ transitionsCount = 0;
1373
+ if (recordIdsToRemove.length > 0)
1374
+ recordIdsToRemove = [];
1375
+ } else if (recordIdsToRemove.length > 0 && !isSequential) {
1376
+ for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
1377
+ recordIdsToRemove[i][RECORD_SYMBOL] = 0;
1378
+ }
1379
+ recordIdsToRemove = [];
1380
+ }
1381
+ };
1382
+ const packArray = (value) => {
1383
+ var length = value.length;
1384
+ if (length < 0x10) {
1385
+ target[position++] = 0x90 | length;
1386
+ } else if (length < 0x10000) {
1387
+ target[position++] = 0xdc;
1388
+ target[position++] = length >> 8;
1389
+ target[position++] = length & 0xff;
1390
+ } else {
1391
+ target[position++] = 0xdd;
1392
+ targetView.setUint32(position, length);
1393
+ position += 4;
1394
+ }
1395
+ for (let i = 0; i < length; i++) {
1396
+ pack(value[i]);
1397
+ }
1398
+ };
1399
+ const pack = (value) => {
1400
+ if (position > safeEnd)
1401
+ target = makeRoom(position);
1402
+
1403
+ var type = typeof value;
1404
+ var length;
1405
+ if (type === 'string') {
1406
+ let strLength = value.length;
1407
+ if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
1408
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1409
+ let extStart;
1410
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1411
+ if (position + maxBytes > safeEnd)
1412
+ target = makeRoom(position + maxBytes);
1413
+ let lastBundle;
1414
+ 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
1415
+ lastBundle = bundledStrings;
1416
+ target[position] = 0xc8; // ext 16
1417
+ position += 3; // reserve for the writing bundle size
1418
+ target[position++] = 0x62; // 'b'
1419
+ extStart = position - start;
1420
+ position += 4; // reserve for writing bundle reference
1421
+ writeBundles(start, pack, 0); // write the last bundles
1422
+ targetView.setUint16(extStart + start - 3, position - start - extStart);
1423
+ } 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)
1424
+ target[position++] = 0xd6; // fixext 4
1425
+ target[position++] = 0x62; // 'b'
1426
+ extStart = position - start;
1427
+ position += 4; // reserve for writing bundle reference
1428
+ }
1429
+ bundledStrings = ['', '']; // create new ones
1430
+ bundledStrings.previous = lastBundle;
1431
+ bundledStrings.size = 0;
1432
+ bundledStrings.position = extStart;
1433
+ }
1434
+ let twoByte = hasNonLatin.test(value);
1435
+ bundledStrings[twoByte ? 0 : 1] += value;
1436
+ target[position++] = 0xc1;
1437
+ pack(twoByte ? -strLength : strLength);
1438
+ return
1439
+ }
1440
+ let headerSize;
1441
+ // first we estimate the header size, so we can write to the correct location
1442
+ if (strLength < 0x20) {
1443
+ headerSize = 1;
1444
+ } else if (strLength < 0x100) {
1445
+ headerSize = 2;
1446
+ } else if (strLength < 0x10000) {
1447
+ headerSize = 3;
1448
+ } else {
1449
+ headerSize = 5;
1450
+ }
1451
+ let maxBytes = strLength * 3;
1452
+ if (position + maxBytes > safeEnd)
1453
+ target = makeRoom(position + maxBytes);
1454
+
1455
+ if (strLength < 0x40 || !encodeUtf8) {
1456
+ let i, c1, c2, strPosition = position + headerSize;
1457
+ for (i = 0; i < strLength; i++) {
1458
+ c1 = value.charCodeAt(i);
1459
+ if (c1 < 0x80) {
1460
+ target[strPosition++] = c1;
1461
+ } else if (c1 < 0x800) {
1462
+ target[strPosition++] = c1 >> 6 | 0xc0;
1463
+ target[strPosition++] = c1 & 0x3f | 0x80;
1464
+ } else if (
1465
+ (c1 & 0xfc00) === 0xd800 &&
1466
+ ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
1467
+ ) {
1468
+ c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
1469
+ i++;
1470
+ target[strPosition++] = c1 >> 18 | 0xf0;
1471
+ target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
1472
+ target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1473
+ target[strPosition++] = c1 & 0x3f | 0x80;
1474
+ } else {
1475
+ target[strPosition++] = c1 >> 12 | 0xe0;
1476
+ target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1477
+ target[strPosition++] = c1 & 0x3f | 0x80;
1478
+ }
1479
+ }
1480
+ length = strPosition - position - headerSize;
1481
+ } else {
1482
+ length = encodeUtf8(value, position + headerSize);
1483
+ }
1484
+
1485
+ if (length < 0x20) {
1486
+ target[position++] = 0xa0 | length;
1487
+ } else if (length < 0x100) {
1488
+ if (headerSize < 2) {
1489
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1490
+ }
1491
+ target[position++] = 0xd9;
1492
+ target[position++] = length;
1493
+ } else if (length < 0x10000) {
1494
+ if (headerSize < 3) {
1495
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1496
+ }
1497
+ target[position++] = 0xda;
1498
+ target[position++] = length >> 8;
1499
+ target[position++] = length & 0xff;
1500
+ } else {
1501
+ if (headerSize < 5) {
1502
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1503
+ }
1504
+ target[position++] = 0xdb;
1505
+ targetView.setUint32(position, length);
1506
+ position += 4;
1507
+ }
1508
+ position += length;
1509
+ } else if (type === 'number') {
1510
+ if (value >>> 0 === value) {// positive integer, 32-bit or less
1511
+ // positive uint
1512
+ if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1513
+ target[position++] = value;
1514
+ } else if (value < 0x100) {
1515
+ target[position++] = 0xcc;
1516
+ target[position++] = value;
1517
+ } else if (value < 0x10000) {
1518
+ target[position++] = 0xcd;
1519
+ target[position++] = value >> 8;
1520
+ target[position++] = value & 0xff;
1521
+ } else {
1522
+ target[position++] = 0xce;
1523
+ targetView.setUint32(position, value);
1524
+ position += 4;
1525
+ }
1526
+ } else if (value >> 0 === value) { // negative integer
1527
+ if (value >= -0x20) {
1528
+ target[position++] = 0x100 + value;
1529
+ } else if (value >= -0x80) {
1530
+ target[position++] = 0xd0;
1531
+ target[position++] = value + 0x100;
1532
+ } else if (value >= -0x8000) {
1533
+ target[position++] = 0xd1;
1534
+ targetView.setInt16(position, value);
1535
+ position += 2;
1536
+ } else {
1537
+ target[position++] = 0xd2;
1538
+ targetView.setInt32(position, value);
1539
+ position += 4;
1540
+ }
1541
+ } else {
1542
+ let useFloat32;
1543
+ if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1544
+ target[position++] = 0xca;
1545
+ targetView.setFloat32(position, value);
1546
+ let xShifted;
1547
+ if (useFloat32 < 4 ||
1548
+ // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1549
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1550
+ position += 4;
1551
+ return
1552
+ } else
1553
+ position--; // move back into position for writing a double
1554
+ }
1555
+ target[position++] = 0xcb;
1556
+ targetView.setFloat64(position, value);
1557
+ position += 8;
1558
+ }
1559
+ } else if (type === 'object' || type === 'function') {
1560
+ if (!value)
1561
+ target[position++] = 0xc0;
1562
+ else {
1563
+ if (referenceMap) {
1564
+ let referee = referenceMap.get(value);
1565
+ if (referee) {
1566
+ if (!referee.id) {
1567
+ let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1568
+ referee.id = idsToInsert.push(referee);
1569
+ }
1570
+ target[position++] = 0xd6; // fixext 4
1571
+ target[position++] = 0x70; // "p" for pointer
1572
+ targetView.setUint32(position, referee.id);
1573
+ position += 4;
1574
+ return
1575
+ } else
1576
+ referenceMap.set(value, { offset: position - start });
1577
+ }
1578
+ let constructor = value.constructor;
1579
+ if (constructor === Object) {
1580
+ writeObject(value);
1581
+ } else if (constructor === Array) {
1582
+ packArray(value);
1583
+ } else if (constructor === Map) {
1584
+ if (this.mapAsEmptyObject) target[position++] = 0x80;
1585
+ else {
1586
+ length = value.size;
1587
+ if (length < 0x10) {
1588
+ target[position++] = 0x80 | length;
1589
+ } else if (length < 0x10000) {
1590
+ target[position++] = 0xde;
1591
+ target[position++] = length >> 8;
1592
+ target[position++] = length & 0xff;
1593
+ } else {
1594
+ target[position++] = 0xdf;
1595
+ targetView.setUint32(position, length);
1596
+ position += 4;
1597
+ }
1598
+ for (let [key, entryValue] of value) {
1599
+ pack(key);
1600
+ pack(entryValue);
1601
+ }
1602
+ }
1603
+ } else {
1604
+ for (let i = 0, l = extensions.length; i < l; i++) {
1605
+ let extensionClass = extensionClasses[i];
1606
+ if (value instanceof extensionClass) {
1607
+ let extension = extensions[i];
1608
+ if (extension.write) {
1609
+ if (extension.type) {
1610
+ target[position++] = 0xd4; // one byte "tag" extension
1611
+ target[position++] = extension.type;
1612
+ target[position++] = 0;
1613
+ }
1614
+ let writeResult = extension.write.call(this, value);
1615
+ if (writeResult === value) { // avoid infinite recursion
1616
+ if (Array.isArray(value)) {
1617
+ packArray(value);
1618
+ } else {
1619
+ writeObject(value);
1620
+ }
1621
+ } else {
1622
+ pack(writeResult);
1623
+ }
1624
+ return
1625
+ }
1626
+ let currentTarget = target;
1627
+ let currentTargetView = targetView;
1628
+ let currentPosition = position;
1629
+ target = null;
1630
+ let result;
1631
+ try {
1632
+ result = extension.pack.call(this, value, (size) => {
1633
+ // restore target and use it
1634
+ target = currentTarget;
1635
+ currentTarget = null;
1636
+ position += size;
1637
+ if (position > safeEnd)
1638
+ makeRoom(position);
1639
+ return {
1640
+ target, targetView, position: position - size
1641
+ }
1642
+ }, pack);
1643
+ } finally {
1644
+ // restore current target information (unless already restored)
1645
+ if (currentTarget) {
1646
+ target = currentTarget;
1647
+ targetView = currentTargetView;
1648
+ position = currentPosition;
1649
+ safeEnd = target.length - 10;
1650
+ }
1651
+ }
1652
+ if (result) {
1653
+ if (result.length + position > safeEnd)
1654
+ makeRoom(result.length + position);
1655
+ position = writeExtensionData(result, target, position, extension.type);
1656
+ }
1657
+ return
1658
+ }
1659
+ }
1660
+ // check isArray after extensions, because extensions can extend Array
1661
+ if (Array.isArray(value)) {
1662
+ packArray(value);
1663
+ } else {
1664
+ // use this as an alternate mechanism for expressing how to serialize
1665
+ if (value.toJSON) {
1666
+ const json = value.toJSON();
1667
+ // if for some reason value.toJSON returns itself it'll loop forever
1668
+ if (json !== value)
1669
+ return pack(json)
1670
+ }
1671
+
1672
+ // if there is a writeFunction, use it, otherwise just encode as undefined
1673
+ if (type === 'function')
1674
+ return pack(this.writeFunction && this.writeFunction(value));
1675
+
1676
+ // no extension found, write as plain object
1677
+ writeObject(value);
1678
+ }
1679
+ }
1680
+ }
1681
+ } else if (type === 'boolean') {
1682
+ target[position++] = value ? 0xc3 : 0xc2;
1683
+ } else if (type === 'bigint') {
1684
+ if (value < 0x8000000000000000 && value >= -0x8000000000000000) {
1685
+ // use a signed int as long as it fits
1686
+ target[position++] = 0xd3;
1687
+ targetView.setBigInt64(position, value);
1688
+ } else if (value < 0x10000000000000000 && value > 0) {
1689
+ // if we can fit an unsigned int, use that
1690
+ target[position++] = 0xcf;
1691
+ targetView.setBigUint64(position, value);
1692
+ } else {
1693
+ // overflow
1694
+ if (this.largeBigIntToFloat) {
1695
+ target[position++] = 0xcb;
1696
+ targetView.setFloat64(position, Number(value));
1697
+ } else if (this.largeBigIntToString) {
1698
+ return pack(value.toString());
1699
+ } else if (this.useBigIntExtension || this.moreTypes) {
1700
+ let empty = value < 0 ? BigInt(-1) : BigInt(0);
1701
+
1702
+ let array;
1703
+ if (value >> BigInt(0x10000) === empty) {
1704
+ let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
1705
+ let chunks = [];
1706
+ while (true) {
1707
+ chunks.push(value & mask);
1708
+ if ((value >> BigInt(63)) === empty) break
1709
+ value >>= BigInt(64);
1710
+ }
1711
+
1712
+ array = new Uint8Array(new BigUint64Array(chunks).buffer);
1713
+ array.reverse();
1714
+ } else {
1715
+ let invert = value < 0;
1716
+ let string = (invert ? ~value : value).toString(16);
1717
+ if (string.length % 2) {
1718
+ string = '0' + string;
1719
+ } else if (parseInt(string.charAt(0), 16) >= 8) {
1720
+ string = '00' + string;
1721
+ }
1722
+
1723
+ if (hasNodeBuffer) {
1724
+ array = Buffer.from(string, 'hex');
1725
+ } else {
1726
+ array = new Uint8Array(string.length / 2);
1727
+ for (let i = 0; i < array.length; i++) {
1728
+ array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
1729
+ }
1730
+ }
1731
+
1732
+ if (invert) {
1733
+ for (let i = 0; i < array.length; i++) array[i] = ~array[i];
1734
+ }
1735
+ }
1736
+
1737
+ if (array.length + position > safeEnd)
1738
+ makeRoom(array.length + position);
1739
+ position = writeExtensionData(array, target, position, 0x42);
1740
+ return
1741
+ } else {
1742
+ throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
1743
+ ' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
1744
+ ' largeBigIntToString to convert to string')
1745
+ }
1746
+ }
1747
+ position += 8;
1748
+ } else if (type === 'undefined') {
1749
+ if (this.encodeUndefinedAsNil)
1750
+ target[position++] = 0xc0;
1751
+ else {
1752
+ target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1753
+ target[position++] = 0;
1754
+ target[position++] = 0;
1755
+ }
1756
+ } else {
1757
+ throw new Error('Unknown type: ' + type)
1758
+ }
1759
+ };
1760
+
1761
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
1762
+ // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1763
+ let keys;
1764
+ if (this.skipValues) {
1765
+ keys = [];
1766
+ for (let key in object) {
1767
+ if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
1768
+ !this.skipValues.includes(object[key]))
1769
+ keys.push(key);
1770
+ }
1771
+ } else {
1772
+ keys = Object.keys(object);
1773
+ }
1774
+ let length = keys.length;
1775
+ if (length < 0x10) {
1776
+ target[position++] = 0x80 | length;
1777
+ } else if (length < 0x10000) {
1778
+ target[position++] = 0xde;
1779
+ target[position++] = length >> 8;
1780
+ target[position++] = length & 0xff;
1781
+ } else {
1782
+ target[position++] = 0xdf;
1783
+ targetView.setUint32(position, length);
1784
+ position += 4;
1785
+ }
1786
+ let key;
1787
+ if (this.coercibleKeyAsNumber) {
1788
+ for (let i = 0; i < length; i++) {
1789
+ key = keys[i];
1790
+ let num = Number(key);
1791
+ pack(isNaN(num) ? key : num);
1792
+ pack(object[key]);
1793
+ }
1794
+
1795
+ } else {
1796
+ for (let i = 0; i < length; i++) {
1797
+ pack(key = keys[i]);
1798
+ pack(object[key]);
1799
+ }
1800
+ }
1801
+ } :
1802
+ (object) => {
1803
+ target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1804
+ let objectOffset = position - start;
1805
+ position += 2;
1806
+ let size = 0;
1807
+ for (let key in object) {
1808
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1809
+ pack(key);
1810
+ pack(object[key]);
1811
+ size++;
1812
+ }
1813
+ }
1814
+ if (size > 0xffff) {
1815
+ throw new Error('Object is too large to serialize with fast 16-bit map size,' +
1816
+ ' use the "variableMapSize" option to serialize this object');
1817
+ }
1818
+ target[objectOffset++ + start] = size >> 8;
1819
+ target[objectOffset + start] = size & 0xff;
1820
+ };
1821
+
1822
+ const writeRecord = this.useRecords === false ? writePlainObject :
1823
+ (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)
1824
+ (object) => {
1825
+ let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1826
+ let objectOffset = position++ - start;
1827
+ let wroteKeys;
1828
+ for (let key in object) {
1829
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1830
+ nextTransition = transition[key];
1831
+ if (nextTransition)
1832
+ transition = nextTransition;
1833
+ else {
1834
+ // record doesn't exist, create full new record and insert it
1835
+ let keys = Object.keys(object);
1836
+ let lastTransition = transition;
1837
+ transition = structures.transitions;
1838
+ let newTransitions = 0;
1839
+ for (let i = 0, l = keys.length; i < l; i++) {
1840
+ let key = keys[i];
1841
+ nextTransition = transition[key];
1842
+ if (!nextTransition) {
1843
+ nextTransition = transition[key] = Object.create(null);
1844
+ newTransitions++;
1845
+ }
1846
+ transition = nextTransition;
1847
+ }
1848
+ if (objectOffset + start + 1 == position) {
1849
+ // first key, so we don't need to insert, we can just write record directly
1850
+ position--;
1851
+ newRecord(transition, keys, newTransitions);
1852
+ } else // otherwise we need to insert the record, moving existing data after the record
1853
+ insertNewRecord(transition, keys, objectOffset, newTransitions);
1854
+ wroteKeys = true;
1855
+ transition = lastTransition[key];
1856
+ }
1857
+ pack(object[key]);
1858
+ }
1859
+ }
1860
+ if (!wroteKeys) {
1861
+ let recordId = transition[RECORD_SYMBOL];
1862
+ if (recordId)
1863
+ target[objectOffset + start] = recordId;
1864
+ else
1865
+ insertNewRecord(transition, Object.keys(object), objectOffset, 0);
1866
+ }
1867
+ } :
1868
+ (object) => {
1869
+ let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1870
+ let newTransitions = 0;
1871
+ for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1872
+ nextTransition = transition[key];
1873
+ if (!nextTransition) {
1874
+ nextTransition = transition[key] = Object.create(null);
1875
+ newTransitions++;
1876
+ }
1877
+ transition = nextTransition;
1878
+ }
1879
+ let recordId = transition[RECORD_SYMBOL];
1880
+ if (recordId) {
1881
+ if (recordId >= 0x60 && useTwoByteRecords) {
1882
+ target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1883
+ target[position++] = recordId >> 5;
1884
+ } else
1885
+ target[position++] = recordId;
1886
+ } else {
1887
+ newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1888
+ }
1889
+ // now write the values
1890
+ for (let key in object)
1891
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1892
+ pack(object[key]);
1893
+ }
1894
+ };
1895
+
1896
+ // create reference to useRecords if useRecords is a function
1897
+ const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1898
+
1899
+ const writeObject = checkUseRecords ? (object) => {
1900
+ checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1901
+ } : writeRecord;
1902
+
1903
+ const makeRoom = (end) => {
1904
+ let newSize;
1905
+ if (end > 0x1000000) {
1906
+ // special handling for really large buffers
1907
+ if ((end - start) > MAX_BUFFER_SIZE)
1908
+ throw new Error('Packed buffer would be larger than maximum buffer size')
1909
+ newSize = Math.min(MAX_BUFFER_SIZE,
1910
+ Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
1911
+ } else // faster handling for smaller buffers
1912
+ newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1913
+ let newBuffer = new ByteArrayAllocate(newSize);
1914
+ targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
1915
+ end = Math.min(end, target.length);
1916
+ if (target.copy)
1917
+ target.copy(newBuffer, 0, start, end);
1918
+ else
1919
+ newBuffer.set(target.slice(start, end));
1920
+ position -= start;
1921
+ start = 0;
1922
+ safeEnd = newBuffer.length - 10;
1923
+ return target = newBuffer
1924
+ };
1925
+ const newRecord = (transition, keys, newTransitions) => {
1926
+ let recordId = structures.nextId;
1927
+ if (!recordId)
1928
+ recordId = 0x40;
1929
+ if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
1930
+ recordId = structures.nextOwnId;
1931
+ if (!(recordId < maxStructureId))
1932
+ recordId = sharedLimitId;
1933
+ structures.nextOwnId = recordId + 1;
1934
+ } else {
1935
+ if (recordId >= maxStructureId)// cycle back around
1936
+ recordId = sharedLimitId;
1937
+ structures.nextId = recordId + 1;
1938
+ }
1939
+ let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1;
1940
+ transition[RECORD_SYMBOL] = recordId;
1941
+ transition.__keys__ = keys;
1942
+ structures[recordId - 0x40] = keys;
1943
+
1944
+ if (recordId < sharedLimitId) {
1945
+ keys.isShared = true;
1946
+ structures.sharedLength = recordId - 0x3f;
1947
+ hasSharedUpdate = true;
1948
+ if (highByte >= 0) {
1949
+ target[position++] = (recordId & 0x1f) + 0x60;
1950
+ target[position++] = highByte;
1951
+ } else {
1952
+ target[position++] = recordId;
1953
+ }
1954
+ } else {
1955
+ if (highByte >= 0) {
1956
+ target[position++] = 0xd5; // fixext 2
1957
+ target[position++] = 0x72; // "r" record defintion extension type
1958
+ target[position++] = (recordId & 0x1f) + 0x60;
1959
+ target[position++] = highByte;
1960
+ } else {
1961
+ target[position++] = 0xd4; // fixext 1
1962
+ target[position++] = 0x72; // "r" record defintion extension type
1963
+ target[position++] = recordId;
1964
+ }
1965
+
1966
+ if (newTransitions)
1967
+ transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
1968
+ // record the removal of the id, we can maintain our shared structure
1969
+ if (recordIdsToRemove.length >= maxOwnStructures)
1970
+ recordIdsToRemove.shift()[RECORD_SYMBOL] = 0; // we are cycling back through, and have to remove old ones
1971
+ recordIdsToRemove.push(transition);
1972
+ pack(keys);
1973
+ }
1974
+ };
1975
+ const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1976
+ let mainTarget = target;
1977
+ let mainPosition = position;
1978
+ let mainSafeEnd = safeEnd;
1979
+ let mainStart = start;
1980
+ target = keysTarget;
1981
+ position = 0;
1982
+ start = 0;
1983
+ if (!target)
1984
+ keysTarget = target = new ByteArrayAllocate(8192);
1985
+ safeEnd = target.length - 10;
1986
+ newRecord(transition, keys, newTransitions);
1987
+ keysTarget = target;
1988
+ let keysPosition = position;
1989
+ target = mainTarget;
1990
+ position = mainPosition;
1991
+ safeEnd = mainSafeEnd;
1992
+ start = mainStart;
1993
+ if (keysPosition > 1) {
1994
+ let newEnd = position + keysPosition - 1;
1995
+ if (newEnd > safeEnd)
1996
+ makeRoom(newEnd);
1997
+ let insertionPosition = insertionOffset + start;
1998
+ target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
1999
+ target.set(keysTarget.slice(0, keysPosition), insertionPosition);
2000
+ position = newEnd;
2001
+ } else {
2002
+ target[insertionOffset + start] = keysTarget[0];
2003
+ }
2004
+ };
2005
+ const writeStruct = (object) => {
2006
+ let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
2007
+ if (notifySharedUpdate)
2008
+ return hasSharedUpdate = true;
2009
+ position = newPosition;
2010
+ let startTarget = target;
2011
+ pack(value);
2012
+ resetStructures();
2013
+ if (startTarget !== target) {
2014
+ return { position, targetView, target }; // indicate the buffer was re-allocated
2015
+ }
2016
+ return position;
2017
+ }, this);
2018
+ if (newPosition === 0) // bail and go to a msgpack object
2019
+ return writeObject(object);
2020
+ position = newPosition;
2021
+ };
2022
+ }
2023
+ useBuffer(buffer) {
2024
+ // this means we are finished using our own buffer and we can write over it safely
2025
+ target = buffer;
2026
+ target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2027
+ targetView = target.dataView;
2028
+ position = 0;
2029
+ }
2030
+ set position (value) {
2031
+ position = value;
2032
+ }
2033
+ get position() {
2034
+ return position;
2035
+ }
2036
+ clearSharedData() {
2037
+ if (this.structures)
2038
+ this.structures = [];
2039
+ if (this.typedStructs)
2040
+ this.typedStructs = [];
2041
+ }
2042
+ }
2043
+
2044
+ extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, DataView, C1Type ];
2045
+ extensions = [{
2046
+ pack(date, allocateForWrite, pack) {
2047
+ let seconds = date.getTime() / 1000;
2048
+ if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
2049
+ // Timestamp 32
2050
+ let { target, targetView, position} = allocateForWrite(6);
2051
+ target[position++] = 0xd6;
2052
+ target[position++] = 0xff;
2053
+ targetView.setUint32(position, seconds);
2054
+ } else if (seconds > 0 && seconds < 0x100000000) {
2055
+ // Timestamp 64
2056
+ let { target, targetView, position} = allocateForWrite(10);
2057
+ target[position++] = 0xd7;
2058
+ target[position++] = 0xff;
2059
+ targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0));
2060
+ targetView.setUint32(position + 4, seconds);
2061
+ } else if (isNaN(seconds)) {
2062
+ if (this.onInvalidDate) {
2063
+ allocateForWrite(0);
2064
+ return pack(this.onInvalidDate())
2065
+ }
2066
+ // Intentionally invalid timestamp
2067
+ let { target, targetView, position} = allocateForWrite(3);
2068
+ target[position++] = 0xd4;
2069
+ target[position++] = 0xff;
2070
+ target[position++] = 0xff;
2071
+ } else {
2072
+ // Timestamp 96
2073
+ let { target, targetView, position} = allocateForWrite(15);
2074
+ target[position++] = 0xc7;
2075
+ target[position++] = 12;
2076
+ target[position++] = 0xff;
2077
+ targetView.setUint32(position, date.getMilliseconds() * 1000000);
2078
+ targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)));
2079
+ }
2080
+ }
2081
+ }, {
2082
+ pack(set, allocateForWrite, pack) {
2083
+ if (this.setAsEmptyObject) {
2084
+ allocateForWrite(0);
2085
+ return pack({})
2086
+ }
2087
+ let array = Array.from(set);
2088
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
2089
+ if (this.moreTypes) {
2090
+ target[position++] = 0xd4;
2091
+ target[position++] = 0x73; // 's' for Set
2092
+ target[position++] = 0;
2093
+ }
2094
+ pack(array);
2095
+ }
2096
+ }, {
2097
+ pack(error, allocateForWrite, pack) {
2098
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
2099
+ if (this.moreTypes) {
2100
+ target[position++] = 0xd4;
2101
+ target[position++] = 0x65; // 'e' for error
2102
+ target[position++] = 0;
2103
+ }
2104
+ pack([ error.name, error.message, error.cause ]);
2105
+ }
2106
+ }, {
2107
+ pack(regex, allocateForWrite, pack) {
2108
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
2109
+ if (this.moreTypes) {
2110
+ target[position++] = 0xd4;
2111
+ target[position++] = 0x78; // 'x' for regeXp
2112
+ target[position++] = 0;
2113
+ }
2114
+ pack([ regex.source, regex.flags ]);
2115
+ }
2116
+ }, {
2117
+ pack(arrayBuffer, allocateForWrite) {
2118
+ if (this.moreTypes)
2119
+ writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
2120
+ else
2121
+ writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2122
+ }
2123
+ }, {
2124
+ pack(typedArray, allocateForWrite) {
2125
+ let constructor = typedArray.constructor;
2126
+ if (constructor !== ByteArray && this.moreTypes)
2127
+ writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
2128
+ else
2129
+ writeBuffer(typedArray, allocateForWrite);
2130
+ }
2131
+ }, {
2132
+ pack(arrayBuffer, allocateForWrite) {
2133
+ if (this.moreTypes)
2134
+ writeExtBuffer(arrayBuffer, 0x11, allocateForWrite);
2135
+ else
2136
+ writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2137
+ }
2138
+ }, {
2139
+ pack(c1, allocateForWrite) { // specific 0xC1 object
2140
+ let { target, position} = allocateForWrite(1);
2141
+ target[position] = 0xc1;
2142
+ }
2143
+ }];
2144
+
2145
+ function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
2146
+ let length = typedArray.byteLength;
2147
+ if (length + 1 < 0x100) {
2148
+ var { target, position } = allocateForWrite(4 + length);
2149
+ target[position++] = 0xc7;
2150
+ target[position++] = length + 1;
2151
+ } else if (length + 1 < 0x10000) {
2152
+ var { target, position } = allocateForWrite(5 + length);
2153
+ target[position++] = 0xc8;
2154
+ target[position++] = (length + 1) >> 8;
2155
+ target[position++] = (length + 1) & 0xff;
2156
+ } else {
2157
+ var { target, position, targetView } = allocateForWrite(7 + length);
2158
+ target[position++] = 0xc9;
2159
+ targetView.setUint32(position, length + 1); // plus one for the type byte
2160
+ position += 4;
2161
+ }
2162
+ target[position++] = 0x74; // "t" for typed array
2163
+ target[position++] = type;
2164
+ if (!typedArray.buffer) typedArray = new Uint8Array(typedArray);
2165
+ target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
2166
+ }
2167
+ function writeBuffer(buffer, allocateForWrite) {
2168
+ let length = buffer.byteLength;
2169
+ var target, position;
2170
+ if (length < 0x100) {
2171
+ var { target, position } = allocateForWrite(length + 2);
2172
+ target[position++] = 0xc4;
2173
+ target[position++] = length;
2174
+ } else if (length < 0x10000) {
2175
+ var { target, position } = allocateForWrite(length + 3);
2176
+ target[position++] = 0xc5;
2177
+ target[position++] = length >> 8;
2178
+ target[position++] = length & 0xff;
2179
+ } else {
2180
+ var { target, position, targetView } = allocateForWrite(length + 5);
2181
+ target[position++] = 0xc6;
2182
+ targetView.setUint32(position, length);
2183
+ position += 4;
2184
+ }
2185
+ target.set(buffer, position);
2186
+ }
2187
+
2188
+ function writeExtensionData(result, target, position, type) {
2189
+ let length = result.length;
2190
+ switch (length) {
2191
+ case 1:
2192
+ target[position++] = 0xd4;
2193
+ break
2194
+ case 2:
2195
+ target[position++] = 0xd5;
2196
+ break
2197
+ case 4:
2198
+ target[position++] = 0xd6;
2199
+ break
2200
+ case 8:
2201
+ target[position++] = 0xd7;
2202
+ break
2203
+ case 16:
2204
+ target[position++] = 0xd8;
2205
+ break
2206
+ default:
2207
+ if (length < 0x100) {
2208
+ target[position++] = 0xc7;
2209
+ target[position++] = length;
2210
+ } else if (length < 0x10000) {
2211
+ target[position++] = 0xc8;
2212
+ target[position++] = length >> 8;
2213
+ target[position++] = length & 0xff;
2214
+ } else {
2215
+ target[position++] = 0xc9;
2216
+ target[position++] = length >> 24;
2217
+ target[position++] = (length >> 16) & 0xff;
2218
+ target[position++] = (length >> 8) & 0xff;
2219
+ target[position++] = length & 0xff;
2220
+ }
2221
+ }
2222
+ target[position++] = type;
2223
+ target.set(result, position);
2224
+ position += length;
2225
+ return position
2226
+ }
2227
+
2228
+ function insertIds(serialized, idsToInsert) {
2229
+ // insert the ids that need to be referenced for structured clones
2230
+ let nextId;
2231
+ let distanceToMove = idsToInsert.length * 6;
2232
+ let lastEnd = serialized.length - distanceToMove;
2233
+ while (nextId = idsToInsert.pop()) {
2234
+ let offset = nextId.offset;
2235
+ let id = nextId.id;
2236
+ serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
2237
+ distanceToMove -= 6;
2238
+ let position = offset + distanceToMove;
2239
+ serialized[position++] = 0xd6;
2240
+ serialized[position++] = 0x69; // 'i'
2241
+ serialized[position++] = id >> 24;
2242
+ serialized[position++] = (id >> 16) & 0xff;
2243
+ serialized[position++] = (id >> 8) & 0xff;
2244
+ serialized[position++] = id & 0xff;
2245
+ lastEnd = offset;
2246
+ }
2247
+ return serialized
2248
+ }
2249
+
2250
+ function writeBundles(start, pack, incrementPosition) {
2251
+ if (bundledStrings.length > 0) {
2252
+ targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
2253
+ bundledStrings.stringsPosition = position - start;
2254
+ let writeStrings = bundledStrings;
2255
+ bundledStrings = null;
2256
+ pack(writeStrings[0]);
2257
+ pack(writeStrings[1]);
2258
+ }
2259
+ }
2260
+
2261
+ function addExtension(extension) {
2262
+ if (extension.Class) {
2263
+ if (!extension.pack && !extension.write)
2264
+ throw new Error('Extension has no pack or write function')
2265
+ if (extension.pack && !extension.type)
2266
+ throw new Error('Extension has no type (numeric code to identify the extension)')
2267
+ extensionClasses.unshift(extension.Class);
2268
+ extensions.unshift(extension);
2269
+ }
2270
+ addExtension$1(extension);
2271
+ }
2272
+ function prepareStructures(structures, packr) {
2273
+ structures.isCompatible = (existingStructures) => {
2274
+ let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
2275
+ if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
2276
+ packr._mergeStructures(existingStructures);
2277
+ return compatible;
2278
+ };
2279
+ return structures
2280
+ }
2281
+
2282
+ let defaultPackr = new Packr({ useRecords: false });
2283
+ const pack = defaultPackr.pack;
2284
+ const encode = defaultPackr.pack;
2285
+ const Encoder = Packr;
2286
+ const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
2287
+ const REUSE_BUFFER_MODE = 512;
2288
+ const RESET_BUFFER_MODE = 1024;
2289
+ const RESERVE_START_SPACE = 2048;
2290
+
2291
+ /**
2292
+ * Given an Iterable first argument, returns an Iterable where each value is packed as a Buffer
2293
+ * If the argument is only Async Iterable, the return value will be an Async Iterable.
2294
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
2295
+ * @param {options} [options] - msgpackr pack options
2296
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
2297
+ */
2298
+ function packIter (objectIterator, options = {}) {
2299
+ if (!objectIterator || typeof objectIterator !== 'object') {
2300
+ throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
2301
+ } else if (typeof objectIterator[Symbol.iterator] === 'function') {
2302
+ return packIterSync(objectIterator, options)
2303
+ } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
2304
+ return packIterAsync(objectIterator, options)
2305
+ } else {
2306
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
2307
+ }
2308
+ }
2309
+
2310
+ function * packIterSync (objectIterator, options) {
2311
+ const packr = new Packr(options);
2312
+ for (const value of objectIterator) {
2313
+ yield packr.pack(value);
2314
+ }
2315
+ }
2316
+
2317
+ async function * packIterAsync (objectIterator, options) {
2318
+ const packr = new Packr(options);
2319
+ for await (const value of objectIterator) {
2320
+ yield packr.pack(value);
2321
+ }
2322
+ }
2323
+
2324
+ /**
2325
+ * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
2326
+ * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
2327
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
2328
+ * @param {object} [options] - unpackr options
2329
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator}
2330
+ */
2331
+ function unpackIter (bufferIterator, options = {}) {
2332
+ if (!bufferIterator || typeof bufferIterator !== 'object') {
2333
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
2334
+ }
2335
+
2336
+ const unpackr = new Unpackr(options);
2337
+ let incomplete;
2338
+ const parser = (chunk) => {
2339
+ let yields;
2340
+ // if there's incomplete data from previous chunk, concatinate and try again
2341
+ if (incomplete) {
2342
+ chunk = Buffer.concat([incomplete, chunk]);
2343
+ incomplete = undefined;
2344
+ }
2345
+
2346
+ try {
2347
+ yields = unpackr.unpackMultiple(chunk);
2348
+ } catch (err) {
2349
+ if (err.incomplete) {
2350
+ incomplete = chunk.slice(err.lastPosition);
2351
+ yields = err.values;
2352
+ } else {
2353
+ throw err
2354
+ }
2355
+ }
2356
+ return yields
2357
+ };
2358
+
2359
+ if (typeof bufferIterator[Symbol.iterator] === 'function') {
2360
+ return (function * iter () {
2361
+ for (const value of bufferIterator) {
2362
+ yield * parser(value);
2363
+ }
2364
+ })()
2365
+ } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
2366
+ return (async function * iter () {
2367
+ for await (const value of bufferIterator) {
2368
+ yield * parser(value);
2369
+ }
2370
+ })()
2371
+ }
2372
+ }
2373
+ const decodeIter = unpackIter;
2374
+ const encodeIter = packIter;
2375
+
2376
+ const useRecords = false;
2377
+ const mapsAsObjects = true;
2378
+
2379
+ exports.ALWAYS = ALWAYS;
2380
+ exports.C1 = C1;
2381
+ exports.DECIMAL_FIT = DECIMAL_FIT;
2382
+ exports.DECIMAL_ROUND = DECIMAL_ROUND;
2383
+ exports.Decoder = Decoder;
2384
+ exports.Encoder = Encoder;
2385
+ exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
2386
+ exports.NEVER = NEVER;
2387
+ exports.Packr = Packr;
2388
+ exports.RESERVE_START_SPACE = RESERVE_START_SPACE;
2389
+ exports.RESET_BUFFER_MODE = RESET_BUFFER_MODE;
2390
+ exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
2391
+ exports.Unpackr = Unpackr;
2392
+ exports.addExtension = addExtension;
2393
+ exports.clearSource = clearSource;
2394
+ exports.decode = decode;
2395
+ exports.decodeIter = decodeIter;
2396
+ exports.encode = encode;
2397
+ exports.encodeIter = encodeIter;
2398
+ exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
2399
+ exports.mapsAsObjects = mapsAsObjects;
2400
+ exports.pack = pack;
2401
+ exports.roundFloat32 = roundFloat32;
2402
+ exports.unpack = unpack;
2403
+ exports.unpackMultiple = unpackMultiple;
2404
+ exports.useRecords = useRecords;
2405
+
2406
+ }));
2407
+ //# sourceMappingURL=index-no-eval.cjs.map