jaelis-node 1.9.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +147 -433
  2. package/bin/jaelis-node.js +79 -504
  3. package/lib/index.js +31 -2740
  4. package/lib/node.js +271 -0
  5. package/lib/rpc.js +315 -0
  6. package/lib/storage.js +198 -0
  7. package/lib/sync.js +366 -0
  8. package/package.json +19 -53
  9. package/config/default.json +0 -74
  10. package/config/mainnet.json +0 -30
  11. package/config/testnet.json +0 -26
  12. package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +0 -454
  13. package/lib/JAELIS-VM/lib/adapters/index.js +0 -411
  14. package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +0 -457
  15. package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +0 -1097
  16. package/lib/JAELIS-VM/lib/execution/engine.js +0 -1183
  17. package/lib/JAELIS-VM/lib/index.js +0 -440
  18. package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +0 -543
  19. package/lib/JAELIS-VM/lib/serialization/serializer.js +0 -819
  20. package/lib/JAELIS-VM/lib/state/state-manager.js +0 -1116
  21. package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +0 -1222
  22. package/lib/JAELIS-VM/lib/unified/cross-chain-deploy.js +0 -1678
  23. package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +0 -836
  24. package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +0 -1127
  25. package/lib/JAELIS-VM/lib/unified/index.js +0 -456
  26. package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +0 -1150
  27. package/lib/JAELIS-VM/lib/unified/unified-compiler.js +0 -1350
  28. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +0 -12
  29. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +0 -17
  30. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +0 -28
  31. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +0 -12
  32. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +0 -17
  33. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +0 -28
  34. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  35. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  36. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  37. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  38. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  39. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  40. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  41. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  42. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  43. package/lib/JAELIS-VM/node_modules/.package-lock.json +0 -127
  44. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +0 -1
  45. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
  46. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
  47. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
  48. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +0 -17
  49. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +0 -1
  50. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
  51. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
  52. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
  53. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +0 -17
  54. package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +0 -21
  55. package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +0 -5
  56. package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +0 -11
  57. package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +0 -60
  58. package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +0 -1
  59. package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +0 -50
  60. package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +0 -198
  61. package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +0 -21
  62. package/lib/JAELIS-VM/node_modules/cbor-x/README.md +0 -380
  63. package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +0 -11
  64. package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +0 -73
  65. package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +0 -11
  66. package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +0 -2
  67. package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +0 -1300
  68. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +0 -1244
  69. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +0 -1
  70. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +0 -2509
  71. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +0 -1
  72. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +0 -2
  73. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +0 -1
  74. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +0 -2508
  75. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +0 -1
  76. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +0 -2
  77. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +0 -1
  78. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +0 -2629
  79. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +0 -1
  80. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +0 -3343
  81. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +0 -1
  82. package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +0 -1
  83. package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +0 -1231
  84. package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +0 -79
  85. package/lib/JAELIS-VM/node_modules/cbor-x/index.js +0 -3
  86. package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +0 -85
  87. package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +0 -24
  88. package/lib/JAELIS-VM/node_modules/cbor-x/package.json +0 -94
  89. package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +0 -88
  90. package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +0 -61
  91. package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +0 -19
  92. package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +0 -201
  93. package/lib/JAELIS-VM/node_modules/detect-libc/README.md +0 -163
  94. package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +0 -14
  95. package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +0 -313
  96. package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +0 -39
  97. package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +0 -51
  98. package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +0 -24
  99. package/lib/JAELIS-VM/node_modules/detect-libc/package.json +0 -44
  100. package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +0 -21
  101. package/lib/JAELIS-VM/node_modules/msgpackr/README.md +0 -372
  102. package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +0 -11
  103. package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +0 -67
  104. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +0 -2407
  105. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +0 -1
  106. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +0 -2
  107. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +0 -1
  108. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +0 -2406
  109. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +0 -1
  110. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +0 -2
  111. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +0 -1
  112. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +0 -3320
  113. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +0 -1
  114. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +0 -4540
  115. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +0 -1
  116. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +0 -1250
  117. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +0 -1
  118. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +0 -91
  119. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +0 -91
  120. package/lib/JAELIS-VM/node_modules/msgpackr/index.js +0 -5
  121. package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +0 -87
  122. package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +0 -25
  123. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +0 -1
  124. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +0 -1
  125. package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +0 -1141
  126. package/lib/JAELIS-VM/node_modules/msgpackr/package.json +0 -104
  127. package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +0 -88
  128. package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +0 -57
  129. package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +0 -815
  130. package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +0 -3
  131. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +0 -2
  132. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +0 -2
  133. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +0 -1221
  134. package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +0 -21
  135. package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +0 -5
  136. package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +0 -13
  137. package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +0 -63
  138. package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +0 -1
  139. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  140. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  141. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  142. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  143. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  144. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  145. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  146. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  147. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  148. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  149. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  150. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +0 -82
  151. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  152. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +0 -6
  153. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +0 -236
  154. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  155. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  156. package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +0 -50
  157. package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +0 -274
  158. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  159. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  160. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +0 -77
  161. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  162. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +0 -224
  163. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  164. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  165. package/lib/JAELIS-VM/package-lock.json +0 -284
  166. package/lib/JAELIS-VM/package.json +0 -38
  167. package/lib/JAELIS-VM/test/comprehensive.test.js +0 -267
  168. package/lib/JAELIS-VM/test/cross-chain-test.js +0 -470
  169. package/lib/JAELIS-VM/test/unified-vm-test.js +0 -459
  170. package/lib/JAELIS-VM/test/unified.test.js +0 -166
  171. package/lib/JAELIS-VM/test/vm.test.js +0 -599
  172. package/lib/settlement-server.js +0 -999
  173. package/lib/vm/index.js +0 -397
@@ -1,3320 +0,0 @@
1
- 'use strict';
2
-
3
- var stream = require('stream');
4
- var module$1 = require('module');
5
-
6
- var decoder;
7
- try {
8
- decoder = new TextDecoder();
9
- } catch(error) {}
10
- var src;
11
- var srcEnd;
12
- var position$1 = 0;
13
- const EMPTY_ARRAY = [];
14
- var strings = EMPTY_ARRAY;
15
- var stringPosition = 0;
16
- var currentUnpackr = {};
17
- var currentStructures;
18
- var srcString;
19
- var srcStringStart = 0;
20
- var srcStringEnd = 0;
21
- var bundledStrings$1;
22
- var referenceMap;
23
- var currentExtensions = [];
24
- var dataView;
25
- var defaultOptions = {
26
- useRecords: false,
27
- mapsAsObjects: true
28
- };
29
- class C1Type {}
30
- const C1 = new C1Type();
31
- C1.name = 'MessagePack 0xC1';
32
- var sequentialMode = false;
33
- var inlineObjectReadThreshold = 2;
34
- var readStruct$1, onLoadedStructures$1, onSaveState;
35
- // no-eval build
36
- try {
37
- new Function('');
38
- } catch(error) {
39
- // if eval variants are not supported, do not create inline object readers ever
40
- inlineObjectReadThreshold = Infinity;
41
- }
42
-
43
- class Unpackr {
44
- constructor(options) {
45
- if (options) {
46
- if (options.useRecords === false && options.mapsAsObjects === undefined)
47
- options.mapsAsObjects = true;
48
- if (options.sequential && options.trusted !== false) {
49
- options.trusted = true;
50
- if (!options.structures && options.useRecords != false) {
51
- options.structures = [];
52
- if (!options.maxSharedStructures)
53
- options.maxSharedStructures = 0;
54
- }
55
- }
56
- if (options.structures)
57
- options.structures.sharedLength = options.structures.length;
58
- else if (options.getStructures) {
59
- (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
60
- options.structures.sharedLength = 0;
61
- }
62
- if (options.int64AsNumber) {
63
- options.int64AsType = 'number';
64
- }
65
- }
66
- Object.assign(this, options);
67
- }
68
- unpack(source, options) {
69
- if (src) {
70
- // re-entrant execution, save the state and restore it after we do this unpack
71
- return saveState$1(() => {
72
- clearSource();
73
- return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
74
- })
75
- }
76
- if (!source.buffer && source.constructor === ArrayBuffer)
77
- source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
78
- if (typeof options === 'object') {
79
- srcEnd = options.end || source.length;
80
- position$1 = options.start || 0;
81
- } else {
82
- position$1 = 0;
83
- srcEnd = options > -1 ? options : source.length;
84
- }
85
- stringPosition = 0;
86
- srcStringEnd = 0;
87
- srcString = null;
88
- strings = EMPTY_ARRAY;
89
- bundledStrings$1 = null;
90
- src = source;
91
- // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
92
- // technique for getting data from a database where it can be copied into an existing buffer instead of creating
93
- // new ones
94
- try {
95
- dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
96
- } catch(error) {
97
- // if it doesn't have a buffer, maybe it is the wrong type of object
98
- src = null;
99
- if (source instanceof Uint8Array)
100
- throw error
101
- throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
102
- }
103
- if (this instanceof Unpackr) {
104
- currentUnpackr = this;
105
- if (this.structures) {
106
- currentStructures = this.structures;
107
- return checkedRead(options)
108
- } else if (!currentStructures || currentStructures.length > 0) {
109
- currentStructures = [];
110
- }
111
- } else {
112
- currentUnpackr = defaultOptions;
113
- if (!currentStructures || currentStructures.length > 0)
114
- currentStructures = [];
115
- }
116
- return checkedRead(options)
117
- }
118
- unpackMultiple(source, forEach) {
119
- let values, lastPosition = 0;
120
- try {
121
- sequentialMode = true;
122
- let size = source.length;
123
- let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
124
- if (forEach) {
125
- if (forEach(value, lastPosition, position$1) === false) return;
126
- while(position$1 < size) {
127
- lastPosition = position$1;
128
- if (forEach(checkedRead(), lastPosition, position$1) === false) {
129
- return
130
- }
131
- }
132
- }
133
- else {
134
- values = [ value ];
135
- while(position$1 < size) {
136
- lastPosition = position$1;
137
- values.push(checkedRead());
138
- }
139
- return values
140
- }
141
- } catch(error) {
142
- error.lastPosition = lastPosition;
143
- error.values = values;
144
- throw error
145
- } finally {
146
- sequentialMode = false;
147
- clearSource();
148
- }
149
- }
150
- _mergeStructures(loadedStructures, existingStructures) {
151
- if (onLoadedStructures$1)
152
- loadedStructures = onLoadedStructures$1.call(this, loadedStructures);
153
- loadedStructures = loadedStructures || [];
154
- if (Object.isFrozen(loadedStructures))
155
- loadedStructures = loadedStructures.map(structure => structure.slice(0));
156
- for (let i = 0, l = loadedStructures.length; i < l; i++) {
157
- let structure = loadedStructures[i];
158
- if (structure) {
159
- structure.isShared = true;
160
- if (i >= 32)
161
- structure.highByte = (i - 32) >> 5;
162
- }
163
- }
164
- loadedStructures.sharedLength = loadedStructures.length;
165
- for (let id in existingStructures || []) {
166
- if (id >= 0) {
167
- let structure = loadedStructures[id];
168
- let existing = existingStructures[id];
169
- if (existing) {
170
- if (structure)
171
- (loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
172
- loadedStructures[id] = existing;
173
- }
174
- }
175
- }
176
- return this.structures = loadedStructures
177
- }
178
- decode(source, options) {
179
- return this.unpack(source, options)
180
- }
181
- }
182
- function checkedRead(options) {
183
- try {
184
- if (!currentUnpackr.trusted && !sequentialMode) {
185
- let sharedLength = currentStructures.sharedLength || 0;
186
- if (sharedLength < currentStructures.length)
187
- currentStructures.length = sharedLength;
188
- }
189
- let result;
190
- if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct$1) {
191
- result = readStruct$1(src, position$1, srcEnd, currentUnpackr);
192
- src = null; // dispose of this so that recursive unpack calls don't save state
193
- if (!(options && options.lazy) && result)
194
- result = result.toJSON();
195
- position$1 = srcEnd;
196
- } else
197
- result = read();
198
- if (bundledStrings$1) { // bundled strings to skip past
199
- position$1 = bundledStrings$1.postBundlePosition;
200
- bundledStrings$1 = null;
201
- }
202
- if (sequentialMode)
203
- // we only need to restore the structures if there was an error, but if we completed a read,
204
- // we can clear this out and keep the structures we read
205
- currentStructures.restoreStructures = null;
206
-
207
- if (position$1 == srcEnd) {
208
- // finished reading this source, cleanup references
209
- if (currentStructures && currentStructures.restoreStructures)
210
- restoreStructures();
211
- currentStructures = null;
212
- src = null;
213
- if (referenceMap)
214
- referenceMap = null;
215
- } else if (position$1 > srcEnd) {
216
- // over read
217
- throw new Error('Unexpected end of MessagePack data')
218
- } else if (!sequentialMode) {
219
- let jsonView;
220
- try {
221
- jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
222
- } catch(error) {
223
- jsonView = '(JSON view not available ' + error + ')';
224
- }
225
- throw new Error('Data read, but end of buffer not reached ' + jsonView)
226
- }
227
- // else more to read, but we are reading sequentially, so don't clear source yet
228
- return result
229
- } catch(error) {
230
- if (currentStructures && currentStructures.restoreStructures)
231
- restoreStructures();
232
- clearSource();
233
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
234
- error.incomplete = true;
235
- }
236
- throw error
237
- }
238
- }
239
-
240
- function restoreStructures() {
241
- for (let id in currentStructures.restoreStructures) {
242
- currentStructures[id] = currentStructures.restoreStructures[id];
243
- }
244
- currentStructures.restoreStructures = null;
245
- }
246
-
247
- function read() {
248
- let token = src[position$1++];
249
- if (token < 0xa0) {
250
- if (token < 0x80) {
251
- if (token < 0x40)
252
- return token
253
- else {
254
- let structure = currentStructures[token & 0x3f] ||
255
- currentUnpackr.getStructures && loadStructures()[token & 0x3f];
256
- if (structure) {
257
- if (!structure.read) {
258
- structure.read = createStructureReader(structure, token & 0x3f);
259
- }
260
- return structure.read()
261
- } else
262
- return token
263
- }
264
- } else if (token < 0x90) {
265
- // map
266
- token -= 0x80;
267
- if (currentUnpackr.mapsAsObjects) {
268
- let object = {};
269
- for (let i = 0; i < token; i++) {
270
- let key = readKey();
271
- if (key === '__proto__')
272
- key = '__proto_';
273
- object[key] = read();
274
- }
275
- return object
276
- } else {
277
- let map = new Map();
278
- for (let i = 0; i < token; i++) {
279
- map.set(read(), read());
280
- }
281
- return map
282
- }
283
- } else {
284
- token -= 0x90;
285
- let array = new Array(token);
286
- for (let i = 0; i < token; i++) {
287
- array[i] = read();
288
- }
289
- if (currentUnpackr.freezeData)
290
- return Object.freeze(array)
291
- return array
292
- }
293
- } else if (token < 0xc0) {
294
- // fixstr
295
- let length = token - 0xa0;
296
- if (srcStringEnd >= position$1) {
297
- return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
298
- }
299
- if (srcStringEnd == 0 && srcEnd < 140) {
300
- // for small blocks, avoiding the overhead of the extract call is helpful
301
- let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
302
- if (string != null)
303
- return string
304
- }
305
- return readFixedString(length)
306
- } else {
307
- let value;
308
- switch (token) {
309
- case 0xc0: return null
310
- case 0xc1:
311
- if (bundledStrings$1) {
312
- value = read(); // followed by the length of the string in characters (not bytes!)
313
- if (value > 0)
314
- return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
315
- else
316
- return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
317
- }
318
- return C1; // "never-used", return special object to denote that
319
- case 0xc2: return false
320
- case 0xc3: return true
321
- case 0xc4:
322
- // bin 8
323
- value = src[position$1++];
324
- if (value === undefined)
325
- throw new Error('Unexpected end of buffer')
326
- return readBin(value)
327
- case 0xc5:
328
- // bin 16
329
- value = dataView.getUint16(position$1);
330
- position$1 += 2;
331
- return readBin(value)
332
- case 0xc6:
333
- // bin 32
334
- value = dataView.getUint32(position$1);
335
- position$1 += 4;
336
- return readBin(value)
337
- case 0xc7:
338
- // ext 8
339
- return readExt(src[position$1++])
340
- case 0xc8:
341
- // ext 16
342
- value = dataView.getUint16(position$1);
343
- position$1 += 2;
344
- return readExt(value)
345
- case 0xc9:
346
- // ext 32
347
- value = dataView.getUint32(position$1);
348
- position$1 += 4;
349
- return readExt(value)
350
- case 0xca:
351
- value = dataView.getFloat32(position$1);
352
- if (currentUnpackr.useFloat32 > 2) {
353
- // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
354
- let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
355
- position$1 += 4;
356
- return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
357
- }
358
- position$1 += 4;
359
- return value
360
- case 0xcb:
361
- value = dataView.getFloat64(position$1);
362
- position$1 += 8;
363
- return value
364
- // uint handlers
365
- case 0xcc:
366
- return src[position$1++]
367
- case 0xcd:
368
- value = dataView.getUint16(position$1);
369
- position$1 += 2;
370
- return value
371
- case 0xce:
372
- value = dataView.getUint32(position$1);
373
- position$1 += 4;
374
- return value
375
- case 0xcf:
376
- if (currentUnpackr.int64AsType === 'number') {
377
- value = dataView.getUint32(position$1) * 0x100000000;
378
- value += dataView.getUint32(position$1 + 4);
379
- } else if (currentUnpackr.int64AsType === 'string') {
380
- value = dataView.getBigUint64(position$1).toString();
381
- } else if (currentUnpackr.int64AsType === 'auto') {
382
- value = dataView.getBigUint64(position$1);
383
- if (value<=BigInt(2)<<BigInt(52)) value=Number(value);
384
- } else
385
- value = dataView.getBigUint64(position$1);
386
- position$1 += 8;
387
- return value
388
-
389
- // int handlers
390
- case 0xd0:
391
- return dataView.getInt8(position$1++)
392
- case 0xd1:
393
- value = dataView.getInt16(position$1);
394
- position$1 += 2;
395
- return value
396
- case 0xd2:
397
- value = dataView.getInt32(position$1);
398
- position$1 += 4;
399
- return value
400
- case 0xd3:
401
- if (currentUnpackr.int64AsType === 'number') {
402
- value = dataView.getInt32(position$1) * 0x100000000;
403
- value += dataView.getUint32(position$1 + 4);
404
- } else if (currentUnpackr.int64AsType === 'string') {
405
- value = dataView.getBigInt64(position$1).toString();
406
- } else if (currentUnpackr.int64AsType === 'auto') {
407
- value = dataView.getBigInt64(position$1);
408
- if (value>=BigInt(-2)<<BigInt(52)&&value<=BigInt(2)<<BigInt(52)) value=Number(value);
409
- } else
410
- value = dataView.getBigInt64(position$1);
411
- position$1 += 8;
412
- return value
413
-
414
- case 0xd4:
415
- // fixext 1
416
- value = src[position$1++];
417
- if (value == 0x72) {
418
- return recordDefinition(src[position$1++] & 0x3f)
419
- } else {
420
- let extension = currentExtensions[value];
421
- if (extension) {
422
- if (extension.read) {
423
- position$1++; // skip filler byte
424
- return extension.read(read())
425
- } else if (extension.noBuffer) {
426
- position$1++; // skip filler byte
427
- return extension()
428
- } else
429
- return extension(src.subarray(position$1, ++position$1))
430
- } else
431
- throw new Error('Unknown extension ' + value)
432
- }
433
- case 0xd5:
434
- // fixext 2
435
- value = src[position$1];
436
- if (value == 0x72) {
437
- position$1++;
438
- return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
439
- } else
440
- return readExt(2)
441
- case 0xd6:
442
- // fixext 4
443
- return readExt(4)
444
- case 0xd7:
445
- // fixext 8
446
- return readExt(8)
447
- case 0xd8:
448
- // fixext 16
449
- return readExt(16)
450
- case 0xd9:
451
- // str 8
452
- value = src[position$1++];
453
- if (srcStringEnd >= position$1) {
454
- return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
455
- }
456
- return readString8(value)
457
- case 0xda:
458
- // str 16
459
- value = dataView.getUint16(position$1);
460
- position$1 += 2;
461
- if (srcStringEnd >= position$1) {
462
- return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
463
- }
464
- return readString16(value)
465
- case 0xdb:
466
- // str 32
467
- value = dataView.getUint32(position$1);
468
- position$1 += 4;
469
- if (srcStringEnd >= position$1) {
470
- return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
471
- }
472
- return readString32(value)
473
- case 0xdc:
474
- // array 16
475
- value = dataView.getUint16(position$1);
476
- position$1 += 2;
477
- return readArray(value)
478
- case 0xdd:
479
- // array 32
480
- value = dataView.getUint32(position$1);
481
- position$1 += 4;
482
- return readArray(value)
483
- case 0xde:
484
- // map 16
485
- value = dataView.getUint16(position$1);
486
- position$1 += 2;
487
- return readMap(value)
488
- case 0xdf:
489
- // map 32
490
- value = dataView.getUint32(position$1);
491
- position$1 += 4;
492
- return readMap(value)
493
- default: // negative int
494
- if (token >= 0xe0)
495
- return token - 0x100
496
- if (token === undefined) {
497
- let error = new Error('Unexpected end of MessagePack data');
498
- error.incomplete = true;
499
- throw error
500
- }
501
- throw new Error('Unknown MessagePack token ' + token)
502
-
503
- }
504
- }
505
- }
506
- const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
507
- function createStructureReader(structure, firstId) {
508
- function readObject() {
509
- // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
510
- if (readObject.count++ > inlineObjectReadThreshold) {
511
- let readObject = structure.read = (new Function('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
512
- '({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
513
- if (structure.highByte === 0)
514
- structure.read = createSecondByteReader(firstId, structure.read);
515
- return readObject() // second byte is already read, if there is one so immediately read object
516
- }
517
- let object = {};
518
- for (let i = 0, l = structure.length; i < l; i++) {
519
- let key = structure[i];
520
- if (key === '__proto__')
521
- key = '__proto_';
522
- object[key] = read();
523
- }
524
- if (currentUnpackr.freezeData)
525
- return Object.freeze(object);
526
- return object
527
- }
528
- readObject.count = 0;
529
- if (structure.highByte === 0) {
530
- return createSecondByteReader(firstId, readObject)
531
- }
532
- return readObject
533
- }
534
-
535
- const createSecondByteReader = (firstId, read0) => {
536
- return function() {
537
- let highByte = src[position$1++];
538
- if (highByte === 0)
539
- return read0()
540
- let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
541
- let structure = currentStructures[id] || loadStructures()[id];
542
- if (!structure) {
543
- throw new Error('Record id is not defined for ' + id)
544
- }
545
- if (!structure.read)
546
- structure.read = createStructureReader(structure, firstId);
547
- return structure.read()
548
- }
549
- };
550
-
551
- function loadStructures() {
552
- let loadedStructures = saveState$1(() => {
553
- // save the state in case getStructures modifies our buffer
554
- src = null;
555
- return currentUnpackr.getStructures()
556
- });
557
- return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
558
- }
559
-
560
- var readFixedString = readStringJS;
561
- var readString8 = readStringJS;
562
- var readString16 = readStringJS;
563
- var readString32 = readStringJS;
564
- exports.isNativeAccelerationEnabled = false;
565
-
566
- function setExtractor(extractStrings) {
567
- exports.isNativeAccelerationEnabled = true;
568
- readFixedString = readString(1);
569
- readString8 = readString(2);
570
- readString16 = readString(3);
571
- readString32 = readString(5);
572
- function readString(headerLength) {
573
- return function readString(length) {
574
- let string = strings[stringPosition++];
575
- if (string == null) {
576
- if (bundledStrings$1)
577
- return readStringJS(length)
578
- let byteOffset = src.byteOffset;
579
- let extraction = extractStrings(position$1 - headerLength + byteOffset, srcEnd + byteOffset, src.buffer);
580
- if (typeof extraction == 'string') {
581
- string = extraction;
582
- strings = EMPTY_ARRAY;
583
- } else {
584
- strings = extraction;
585
- stringPosition = 1;
586
- srcStringEnd = 1; // even if a utf-8 string was decoded, must indicate we are in the midst of extracted strings and can't skip strings
587
- string = strings[0];
588
- if (string === undefined)
589
- throw new Error('Unexpected end of buffer')
590
- }
591
- }
592
- let srcStringLength = string.length;
593
- if (srcStringLength <= length) {
594
- position$1 += length;
595
- return string
596
- }
597
- srcString = string;
598
- srcStringStart = position$1;
599
- srcStringEnd = position$1 + srcStringLength;
600
- position$1 += length;
601
- return string.slice(0, length) // we know we just want the beginning
602
- }
603
- }
604
- }
605
- function readStringJS(length) {
606
- let result;
607
- if (length < 16) {
608
- if (result = shortStringInJS(length))
609
- return result
610
- }
611
- if (length > 64 && decoder)
612
- return decoder.decode(src.subarray(position$1, position$1 += length))
613
- const end = position$1 + length;
614
- const units = [];
615
- result = '';
616
- while (position$1 < end) {
617
- const byte1 = src[position$1++];
618
- if ((byte1 & 0x80) === 0) {
619
- // 1 byte
620
- units.push(byte1);
621
- } else if ((byte1 & 0xe0) === 0xc0) {
622
- // 2 bytes
623
- const byte2 = src[position$1++] & 0x3f;
624
- units.push(((byte1 & 0x1f) << 6) | byte2);
625
- } else if ((byte1 & 0xf0) === 0xe0) {
626
- // 3 bytes
627
- const byte2 = src[position$1++] & 0x3f;
628
- const byte3 = src[position$1++] & 0x3f;
629
- units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
630
- } else if ((byte1 & 0xf8) === 0xf0) {
631
- // 4 bytes
632
- const byte2 = src[position$1++] & 0x3f;
633
- const byte3 = src[position$1++] & 0x3f;
634
- const byte4 = src[position$1++] & 0x3f;
635
- let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
636
- if (unit > 0xffff) {
637
- unit -= 0x10000;
638
- units.push(((unit >>> 10) & 0x3ff) | 0xd800);
639
- unit = 0xdc00 | (unit & 0x3ff);
640
- }
641
- units.push(unit);
642
- } else {
643
- units.push(byte1);
644
- }
645
-
646
- if (units.length >= 0x1000) {
647
- result += fromCharCode.apply(String, units);
648
- units.length = 0;
649
- }
650
- }
651
-
652
- if (units.length > 0) {
653
- result += fromCharCode.apply(String, units);
654
- }
655
-
656
- return result
657
- }
658
- function readString(source, start, length) {
659
- let existingSrc = src;
660
- src = source;
661
- position$1 = start;
662
- try {
663
- return readStringJS(length);
664
- } finally {
665
- src = existingSrc;
666
- }
667
- }
668
-
669
- function readArray(length) {
670
- let array = new Array(length);
671
- for (let i = 0; i < length; i++) {
672
- array[i] = read();
673
- }
674
- if (currentUnpackr.freezeData)
675
- return Object.freeze(array)
676
- return array
677
- }
678
-
679
- function readMap(length) {
680
- if (currentUnpackr.mapsAsObjects) {
681
- let object = {};
682
- for (let i = 0; i < length; i++) {
683
- let key = readKey();
684
- if (key === '__proto__')
685
- key = '__proto_';
686
- object[key] = read();
687
- }
688
- return object
689
- } else {
690
- let map = new Map();
691
- for (let i = 0; i < length; i++) {
692
- map.set(read(), read());
693
- }
694
- return map
695
- }
696
- }
697
-
698
- var fromCharCode = String.fromCharCode;
699
- function longStringInJS(length) {
700
- let start = position$1;
701
- let bytes = new Array(length);
702
- for (let i = 0; i < length; i++) {
703
- const byte = src[position$1++];
704
- if ((byte & 0x80) > 0) {
705
- position$1 = start;
706
- return
707
- }
708
- bytes[i] = byte;
709
- }
710
- return fromCharCode.apply(String, bytes)
711
- }
712
- function shortStringInJS(length) {
713
- if (length < 4) {
714
- if (length < 2) {
715
- if (length === 0)
716
- return ''
717
- else {
718
- let a = src[position$1++];
719
- if ((a & 0x80) > 1) {
720
- position$1 -= 1;
721
- return
722
- }
723
- return fromCharCode(a)
724
- }
725
- } else {
726
- let a = src[position$1++];
727
- let b = src[position$1++];
728
- if ((a & 0x80) > 0 || (b & 0x80) > 0) {
729
- position$1 -= 2;
730
- return
731
- }
732
- if (length < 3)
733
- return fromCharCode(a, b)
734
- let c = src[position$1++];
735
- if ((c & 0x80) > 0) {
736
- position$1 -= 3;
737
- return
738
- }
739
- return fromCharCode(a, b, c)
740
- }
741
- } else {
742
- let a = src[position$1++];
743
- let b = src[position$1++];
744
- let c = src[position$1++];
745
- let d = src[position$1++];
746
- if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
747
- position$1 -= 4;
748
- return
749
- }
750
- if (length < 6) {
751
- if (length === 4)
752
- return fromCharCode(a, b, c, d)
753
- else {
754
- let e = src[position$1++];
755
- if ((e & 0x80) > 0) {
756
- position$1 -= 5;
757
- return
758
- }
759
- return fromCharCode(a, b, c, d, e)
760
- }
761
- } else if (length < 8) {
762
- let e = src[position$1++];
763
- let f = src[position$1++];
764
- if ((e & 0x80) > 0 || (f & 0x80) > 0) {
765
- position$1 -= 6;
766
- return
767
- }
768
- if (length < 7)
769
- return fromCharCode(a, b, c, d, e, f)
770
- let g = src[position$1++];
771
- if ((g & 0x80) > 0) {
772
- position$1 -= 7;
773
- return
774
- }
775
- return fromCharCode(a, b, c, d, e, f, g)
776
- } else {
777
- let e = src[position$1++];
778
- let f = src[position$1++];
779
- let g = src[position$1++];
780
- let h = src[position$1++];
781
- if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
782
- position$1 -= 8;
783
- return
784
- }
785
- if (length < 10) {
786
- if (length === 8)
787
- return fromCharCode(a, b, c, d, e, f, g, h)
788
- else {
789
- let i = src[position$1++];
790
- if ((i & 0x80) > 0) {
791
- position$1 -= 9;
792
- return
793
- }
794
- return fromCharCode(a, b, c, d, e, f, g, h, i)
795
- }
796
- } else if (length < 12) {
797
- let i = src[position$1++];
798
- let j = src[position$1++];
799
- if ((i & 0x80) > 0 || (j & 0x80) > 0) {
800
- position$1 -= 10;
801
- return
802
- }
803
- if (length < 11)
804
- return fromCharCode(a, b, c, d, e, f, g, h, i, j)
805
- let k = src[position$1++];
806
- if ((k & 0x80) > 0) {
807
- position$1 -= 11;
808
- return
809
- }
810
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
811
- } else {
812
- let i = src[position$1++];
813
- let j = src[position$1++];
814
- let k = src[position$1++];
815
- let l = src[position$1++];
816
- if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
817
- position$1 -= 12;
818
- return
819
- }
820
- if (length < 14) {
821
- if (length === 12)
822
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
823
- else {
824
- let m = src[position$1++];
825
- if ((m & 0x80) > 0) {
826
- position$1 -= 13;
827
- return
828
- }
829
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
830
- }
831
- } else {
832
- let m = src[position$1++];
833
- let n = src[position$1++];
834
- if ((m & 0x80) > 0 || (n & 0x80) > 0) {
835
- position$1 -= 14;
836
- return
837
- }
838
- if (length < 15)
839
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
840
- let o = src[position$1++];
841
- if ((o & 0x80) > 0) {
842
- position$1 -= 15;
843
- return
844
- }
845
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
846
- }
847
- }
848
- }
849
- }
850
- }
851
-
852
- function readOnlyJSString() {
853
- let token = src[position$1++];
854
- let length;
855
- if (token < 0xc0) {
856
- // fixstr
857
- length = token - 0xa0;
858
- } else {
859
- switch(token) {
860
- case 0xd9:
861
- // str 8
862
- length = src[position$1++];
863
- break
864
- case 0xda:
865
- // str 16
866
- length = dataView.getUint16(position$1);
867
- position$1 += 2;
868
- break
869
- case 0xdb:
870
- // str 32
871
- length = dataView.getUint32(position$1);
872
- position$1 += 4;
873
- break
874
- default:
875
- throw new Error('Expected string')
876
- }
877
- }
878
- return readStringJS(length)
879
- }
880
-
881
-
882
- function readBin(length) {
883
- return currentUnpackr.copyBuffers ?
884
- // specifically use the copying slice (not the node one)
885
- Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
886
- src.subarray(position$1, position$1 += length)
887
- }
888
- function readExt(length) {
889
- let type = src[position$1++];
890
- if (currentExtensions[type]) {
891
- let end;
892
- return currentExtensions[type](src.subarray(position$1, end = (position$1 += length)), (readPosition) => {
893
- position$1 = readPosition;
894
- try {
895
- return read();
896
- } finally {
897
- position$1 = end;
898
- }
899
- })
900
- }
901
- else
902
- throw new Error('Unknown extension type ' + type)
903
- }
904
-
905
- var keyCache = new Array(4096);
906
- function readKey() {
907
- let length = src[position$1++];
908
- if (length >= 0xa0 && length < 0xc0) {
909
- // fixstr, potentially use key cache
910
- length = length - 0xa0;
911
- if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
912
- return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
913
- else if (!(srcStringEnd == 0 && srcEnd < 180))
914
- return readFixedString(length)
915
- } else { // not cacheable, go back and do a standard read
916
- position$1--;
917
- return asSafeString(read())
918
- }
919
- let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
920
- let entry = keyCache[key];
921
- let checkPosition = position$1;
922
- let end = position$1 + length - 3;
923
- let chunk;
924
- let i = 0;
925
- if (entry && entry.bytes == length) {
926
- while (checkPosition < end) {
927
- chunk = dataView.getUint32(checkPosition);
928
- if (chunk != entry[i++]) {
929
- checkPosition = 0x70000000;
930
- break
931
- }
932
- checkPosition += 4;
933
- }
934
- end += 3;
935
- while (checkPosition < end) {
936
- chunk = src[checkPosition++];
937
- if (chunk != entry[i++]) {
938
- checkPosition = 0x70000000;
939
- break
940
- }
941
- }
942
- if (checkPosition === end) {
943
- position$1 = checkPosition;
944
- return entry.string
945
- }
946
- end -= 3;
947
- checkPosition = position$1;
948
- }
949
- entry = [];
950
- keyCache[key] = entry;
951
- entry.bytes = length;
952
- while (checkPosition < end) {
953
- chunk = dataView.getUint32(checkPosition);
954
- entry.push(chunk);
955
- checkPosition += 4;
956
- }
957
- end += 3;
958
- while (checkPosition < end) {
959
- chunk = src[checkPosition++];
960
- entry.push(chunk);
961
- }
962
- // for small blocks, avoiding the overhead of the extract call is helpful
963
- let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
964
- if (string != null)
965
- return entry.string = string
966
- return entry.string = readFixedString(length)
967
- }
968
-
969
- function asSafeString(property) {
970
- // protect against expensive (DoS) string conversions
971
- if (typeof property === 'string') return property;
972
- if (typeof property === 'number' || typeof property === 'boolean' || typeof property === 'bigint') return property.toString();
973
- if (property == null) return property + '';
974
- if (currentUnpackr.allowArraysInMapKeys && Array.isArray(property) && property.flat().every(item => ['string', 'number', 'boolean', 'bigint'].includes(typeof item))) {
975
- return property.flat().toString();
976
- }
977
- throw new Error(`Invalid property type for record: ${typeof property}`);
978
- }
979
- // the registration of the record definition extension (as "r")
980
- const recordDefinition = (id, highByte) => {
981
- let structure = read().map(asSafeString); // ensure that all keys are strings and
982
- // that the array is mutable
983
- let firstByte = id;
984
- if (highByte !== undefined) {
985
- id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
986
- structure.highByte = highByte;
987
- }
988
- let existingStructure = currentStructures[id];
989
- // If it is a shared structure, we need to restore any changes after reading.
990
- // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
991
- // to the state prior to an incomplete read in order to properly resume.
992
- if (existingStructure && (existingStructure.isShared || sequentialMode)) {
993
- (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
994
- }
995
- currentStructures[id] = structure;
996
- structure.read = createStructureReader(structure, firstByte);
997
- return structure.read()
998
- };
999
- currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
1000
- currentExtensions[0].noBuffer = true;
1001
-
1002
- currentExtensions[0x42] = data => {
1003
- let headLength = (data.byteLength % 8) || 8;
1004
- let head = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
1005
- for (let i = 1; i < headLength; i++) {
1006
- head <<= BigInt(8);
1007
- head += BigInt(data[i]);
1008
- }
1009
- if (data.byteLength !== headLength) {
1010
- let view = new DataView(data.buffer, data.byteOffset, data.byteLength);
1011
- let decode = (start, end) => {
1012
- let length = end - start;
1013
- if (length <= 40) {
1014
- let out = view.getBigUint64(start);
1015
- for (let i = start + 8; i < end; i += 8) {
1016
- out <<= BigInt(64n);
1017
- out |= view.getBigUint64(i);
1018
- }
1019
- return out
1020
- }
1021
- // if (length === 8) return view.getBigUint64(start)
1022
- let middle = start + (length >> 4 << 3);
1023
- let left = decode(start, middle);
1024
- let right = decode(middle, end);
1025
- return (left << BigInt((end - middle) * 8)) | right
1026
- };
1027
- head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
1028
- }
1029
- return head
1030
- };
1031
-
1032
- let errors = {
1033
- Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError: typeof AggregateError === 'function' ? AggregateError : null,
1034
- };
1035
- currentExtensions[0x65] = () => {
1036
- let data = read();
1037
- if (!errors[data[0]]) {
1038
- let error = Error(data[1], { cause: data[2] });
1039
- error.name = data[0];
1040
- return error
1041
- }
1042
- return errors[data[0]](data[1], { cause: data[2] })
1043
- };
1044
-
1045
- currentExtensions[0x69] = (data) => {
1046
- // id extension (for structured clones)
1047
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1048
- let id = dataView.getUint32(position$1 - 4);
1049
- if (!referenceMap)
1050
- referenceMap = new Map();
1051
- let token = src[position$1];
1052
- let target;
1053
- // TODO: handle any other types that can cycle and make the code more robust if there are other extensions
1054
- if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
1055
- target = [];
1056
- else if (token >= 0x80 && token < 0x90 || token == 0xde || token == 0xdf)
1057
- target = new Map();
1058
- else if ((token >= 0xc7 && token <= 0xc9 || token >= 0xd4 && token <= 0xd8) && src[position$1 + 1] === 0x73)
1059
- target = new Set();
1060
- else
1061
- target = {};
1062
-
1063
- let refEntry = { target }; // a placeholder object
1064
- referenceMap.set(id, refEntry);
1065
- let targetProperties = read(); // read the next value as the target object to id
1066
- if (!refEntry.used) {
1067
- // no cycle, can just use the returned read object
1068
- return refEntry.target = targetProperties // replace the placeholder with the real one
1069
- } else {
1070
- // there is a cycle, so we have to assign properties to original target
1071
- Object.assign(target, targetProperties);
1072
- }
1073
-
1074
- // copy over map/set entries if we're able to
1075
- if (target instanceof Map)
1076
- for (let [k, v] of targetProperties.entries()) target.set(k, v);
1077
- if (target instanceof Set)
1078
- for (let i of Array.from(targetProperties)) target.add(i);
1079
- return target
1080
- };
1081
-
1082
- currentExtensions[0x70] = (data) => {
1083
- // pointer extension (for structured clones)
1084
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1085
- let id = dataView.getUint32(position$1 - 4);
1086
- let refEntry = referenceMap.get(id);
1087
- refEntry.used = true;
1088
- return refEntry.target
1089
- };
1090
-
1091
- currentExtensions[0x73] = () => new Set(read());
1092
-
1093
- const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
1094
-
1095
- let glbl = typeof globalThis === 'object' ? globalThis : window;
1096
- currentExtensions[0x74] = (data) => {
1097
- let typeCode = data[0];
1098
- // we always have to slice to get a new ArrayBuffer that is aligned
1099
- let buffer = Uint8Array.prototype.slice.call(data, 1).buffer;
1100
-
1101
- let typedArrayName = typedArrays[typeCode];
1102
- if (!typedArrayName) {
1103
- if (typeCode === 16) return buffer
1104
- if (typeCode === 17) return new DataView(buffer)
1105
- throw new Error('Could not find typed array for code ' + typeCode)
1106
- }
1107
- return new glbl[typedArrayName](buffer)
1108
- };
1109
- currentExtensions[0x78] = () => {
1110
- let data = read();
1111
- return new RegExp(data[0], data[1])
1112
- };
1113
- const TEMP_BUNDLE = [];
1114
- currentExtensions[0x62] = (data) => {
1115
- let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
1116
- let dataPosition = position$1;
1117
- position$1 += dataSize - data.length;
1118
- bundledStrings$1 = TEMP_BUNDLE;
1119
- bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
1120
- bundledStrings$1.position0 = 0;
1121
- bundledStrings$1.position1 = 0;
1122
- bundledStrings$1.postBundlePosition = position$1;
1123
- position$1 = dataPosition;
1124
- return read()
1125
- };
1126
-
1127
- currentExtensions[0xff] = (data) => {
1128
- // 32-bit date extension
1129
- if (data.length == 4)
1130
- return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
1131
- else if (data.length == 8)
1132
- return new Date(
1133
- ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
1134
- ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
1135
- else if (data.length == 12)
1136
- return new Date(
1137
- ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
1138
- (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
1139
- else
1140
- return new Date('invalid')
1141
- };
1142
- // registration of bulk record definition?
1143
- // currentExtensions[0x52] = () =>
1144
-
1145
- function saveState$1(callback) {
1146
- if (onSaveState)
1147
- onSaveState();
1148
- let savedSrcEnd = srcEnd;
1149
- let savedPosition = position$1;
1150
- let savedStringPosition = stringPosition;
1151
- let savedSrcStringStart = srcStringStart;
1152
- let savedSrcStringEnd = srcStringEnd;
1153
- let savedSrcString = srcString;
1154
- let savedStrings = strings;
1155
- let savedReferenceMap = referenceMap;
1156
- let savedBundledStrings = bundledStrings$1;
1157
-
1158
- // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1159
- let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
1160
- let savedStructures = currentStructures;
1161
- let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
1162
- let savedPackr = currentUnpackr;
1163
- let savedSequentialMode = sequentialMode;
1164
- let value = callback();
1165
- srcEnd = savedSrcEnd;
1166
- position$1 = savedPosition;
1167
- stringPosition = savedStringPosition;
1168
- srcStringStart = savedSrcStringStart;
1169
- srcStringEnd = savedSrcStringEnd;
1170
- srcString = savedSrcString;
1171
- strings = savedStrings;
1172
- referenceMap = savedReferenceMap;
1173
- bundledStrings$1 = savedBundledStrings;
1174
- src = savedSrc;
1175
- sequentialMode = savedSequentialMode;
1176
- currentStructures = savedStructures;
1177
- currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
1178
- currentUnpackr = savedPackr;
1179
- dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1180
- return value
1181
- }
1182
- function clearSource() {
1183
- src = null;
1184
- referenceMap = null;
1185
- currentStructures = null;
1186
- }
1187
-
1188
- function addExtension$1(extension) {
1189
- if (extension.unpack)
1190
- currentExtensions[extension.type] = extension.unpack;
1191
- else
1192
- currentExtensions[extension.type] = extension;
1193
- }
1194
-
1195
- const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1196
- for (let i = 0; i < 256; i++) {
1197
- mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1198
- }
1199
- const Decoder = Unpackr;
1200
- var defaultUnpackr = new Unpackr({ useRecords: false });
1201
- const unpack = defaultUnpackr.unpack;
1202
- const unpackMultiple = defaultUnpackr.unpackMultiple;
1203
- const decode = defaultUnpackr.unpack;
1204
- const FLOAT32_OPTIONS = {
1205
- NEVER: 0,
1206
- ALWAYS: 1,
1207
- DECIMAL_ROUND: 3,
1208
- DECIMAL_FIT: 4
1209
- };
1210
- let f32Array = new Float32Array(1);
1211
- let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1212
- function roundFloat32(float32Number) {
1213
- f32Array[0] = float32Number;
1214
- let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1215
- return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1216
- }
1217
- function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1218
- readStruct$1 = updatedReadStruct;
1219
- onLoadedStructures$1 = loadedStructs;
1220
- onSaveState = saveState;
1221
- }
1222
-
1223
- let textEncoder$1;
1224
- try {
1225
- textEncoder$1 = new TextEncoder();
1226
- } catch (error) {}
1227
- let extensions, extensionClasses;
1228
- const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
1229
- const ByteArrayAllocate = hasNodeBuffer$1 ?
1230
- function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1231
- const ByteArray = hasNodeBuffer$1 ? Buffer : Uint8Array;
1232
- const MAX_BUFFER_SIZE = hasNodeBuffer$1 ? 0x100000000 : 0x7fd00000;
1233
- let target, keysTarget;
1234
- let targetView;
1235
- let position = 0;
1236
- let safeEnd;
1237
- let bundledStrings = null;
1238
- let writeStructSlots;
1239
- const MAX_BUNDLE_SIZE = 0x5500; // maximum characters such that the encoded bytes fits in 16 bits.
1240
- const hasNonLatin = /[\u0080-\uFFFF]/;
1241
- const RECORD_SYMBOL = Symbol('record-id');
1242
- class Packr extends Unpackr {
1243
- constructor(options) {
1244
- super(options);
1245
- this.offset = 0;
1246
- let start;
1247
- let hasSharedUpdate;
1248
- let structures;
1249
- let referenceMap;
1250
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1251
- return target.utf8Write(string, position, target.byteLength - position)
1252
- } : (textEncoder$1 && textEncoder$1.encodeInto) ?
1253
- function(string, position) {
1254
- return textEncoder$1.encodeInto(string, target.subarray(position)).written
1255
- } : false;
1256
-
1257
- let packr = this;
1258
- if (!options)
1259
- options = {};
1260
- let isSequential = options && options.sequential;
1261
- let hasSharedStructures = options.structures || options.saveStructures;
1262
- let maxSharedStructures = options.maxSharedStructures;
1263
- if (maxSharedStructures == null)
1264
- maxSharedStructures = hasSharedStructures ? 32 : 0;
1265
- if (maxSharedStructures > 8160)
1266
- throw new Error('Maximum maxSharedStructure is 8160')
1267
- if (options.structuredClone && options.moreTypes == undefined) {
1268
- this.moreTypes = true;
1269
- }
1270
- let maxOwnStructures = options.maxOwnStructures;
1271
- if (maxOwnStructures == null)
1272
- maxOwnStructures = hasSharedStructures ? 32 : 64;
1273
- if (!this.structures && options.useRecords != false)
1274
- this.structures = [];
1275
- // two byte record ids for shared structures
1276
- let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1277
- let sharedLimitId = maxSharedStructures + 0x40;
1278
- let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1279
- if (maxStructureId > 8256) {
1280
- throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
1281
- }
1282
- let recordIdsToRemove = [];
1283
- let transitionsCount = 0;
1284
- let serializationsSinceTransitionRebuild = 0;
1285
-
1286
- this.pack = this.encode = function(value, encodeOptions) {
1287
- if (!target) {
1288
- target = new ByteArrayAllocate(8192);
1289
- targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
1290
- position = 0;
1291
- }
1292
- safeEnd = target.length - 10;
1293
- if (safeEnd - position < 0x800) {
1294
- // don't start too close to the end,
1295
- target = new ByteArrayAllocate(target.length);
1296
- targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1297
- safeEnd = target.length - 10;
1298
- position = 0;
1299
- } else
1300
- position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1301
- start = position;
1302
- if (encodeOptions & RESERVE_START_SPACE) position += (encodeOptions & 0xff);
1303
- referenceMap = packr.structuredClone ? new Map() : null;
1304
- if (packr.bundleStrings && typeof value !== 'string') {
1305
- bundledStrings = [];
1306
- bundledStrings.size = Infinity; // force a new bundle start on first string
1307
- } else
1308
- bundledStrings = null;
1309
- structures = packr.structures;
1310
- if (structures) {
1311
- if (structures.uninitialized)
1312
- structures = packr._mergeStructures(packr.getStructures());
1313
- let sharedLength = structures.sharedLength || 0;
1314
- if (sharedLength > maxSharedStructures) {
1315
- //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
1316
- throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
1317
- }
1318
- if (!structures.transitions) {
1319
- // rebuild our structure transitions
1320
- structures.transitions = Object.create(null);
1321
- for (let i = 0; i < sharedLength; i++) {
1322
- let keys = structures[i];
1323
- if (!keys)
1324
- continue
1325
- let nextTransition, transition = structures.transitions;
1326
- for (let j = 0, l = keys.length; j < l; j++) {
1327
- let key = keys[j];
1328
- nextTransition = transition[key];
1329
- if (!nextTransition) {
1330
- nextTransition = transition[key] = Object.create(null);
1331
- }
1332
- transition = nextTransition;
1333
- }
1334
- transition[RECORD_SYMBOL] = i + 0x40;
1335
- }
1336
- this.lastNamedStructuresLength = sharedLength;
1337
- }
1338
- if (!isSequential) {
1339
- structures.nextId = sharedLength + 0x40;
1340
- }
1341
- }
1342
- if (hasSharedUpdate)
1343
- hasSharedUpdate = false;
1344
- let encodingError;
1345
- try {
1346
- if (packr.randomAccessStructure && value && typeof value === 'object') {
1347
- if (value.constructor === Object) writeStruct(value); // simple object
1348
- else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
1349
- // allow user classes, if they don't need special handling (but do use toJSON if available)
1350
- writeStruct(value.toJSON ? value.toJSON() : value);
1351
- } else pack(value);
1352
- } else
1353
- pack(value);
1354
- let lastBundle = bundledStrings;
1355
- if (bundledStrings)
1356
- writeBundles(start, pack, 0);
1357
- if (referenceMap && referenceMap.idsToInsert) {
1358
- let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1359
- let i = idsToInsert.length;
1360
- let incrementPosition = -1;
1361
- while (lastBundle && i > 0) {
1362
- let insertionPoint = idsToInsert[--i].offset + start;
1363
- if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
1364
- incrementPosition = 0;
1365
- if (insertionPoint > (lastBundle.position + start)) {
1366
- if (incrementPosition >= 0)
1367
- incrementPosition += 6;
1368
- } else {
1369
- if (incrementPosition >= 0) {
1370
- // update the bundle reference now
1371
- targetView.setUint32(lastBundle.position + start,
1372
- targetView.getUint32(lastBundle.position + start) + incrementPosition);
1373
- incrementPosition = -1; // reset
1374
- }
1375
- lastBundle = lastBundle.previous;
1376
- i++;
1377
- }
1378
- }
1379
- if (incrementPosition >= 0 && lastBundle) {
1380
- // update the bundle reference now
1381
- targetView.setUint32(lastBundle.position + start,
1382
- targetView.getUint32(lastBundle.position + start) + incrementPosition);
1383
- }
1384
- position += idsToInsert.length * 6;
1385
- if (position > safeEnd)
1386
- makeRoom(position);
1387
- packr.offset = position;
1388
- let serialized = insertIds(target.subarray(start, position), idsToInsert);
1389
- referenceMap = null;
1390
- return serialized
1391
- }
1392
- packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1393
- if (encodeOptions & REUSE_BUFFER_MODE) {
1394
- target.start = start;
1395
- target.end = position;
1396
- return target
1397
- }
1398
- return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1399
- } catch(error) {
1400
- encodingError = error;
1401
- throw error;
1402
- } finally {
1403
- if (structures) {
1404
- resetStructures();
1405
- if (hasSharedUpdate && packr.saveStructures) {
1406
- let sharedLength = structures.sharedLength || 0;
1407
- // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1408
- let returnBuffer = target.subarray(start, position);
1409
- let newSharedData = prepareStructures$1(structures, packr);
1410
- if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1411
- if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1412
- // get updated structures and try again if the update failed
1413
- return packr.pack(value, encodeOptions)
1414
- }
1415
- packr.lastNamedStructuresLength = sharedLength;
1416
- // don't keep large buffers around
1417
- if (target.length > 0x40000000) target = null;
1418
- return returnBuffer
1419
- }
1420
- }
1421
- }
1422
- // don't keep large buffers around, they take too much memory and cause problems (limit at 1GB)
1423
- if (target.length > 0x40000000) target = null;
1424
- if (encodeOptions & RESET_BUFFER_MODE)
1425
- position = start;
1426
- }
1427
- };
1428
- const resetStructures = () => {
1429
- if (serializationsSinceTransitionRebuild < 10)
1430
- serializationsSinceTransitionRebuild++;
1431
- let sharedLength = structures.sharedLength || 0;
1432
- if (structures.length > sharedLength && !isSequential)
1433
- structures.length = sharedLength;
1434
- if (transitionsCount > 10000) {
1435
- // force a rebuild occasionally after a lot of transitions so it can get cleaned up
1436
- structures.transitions = null;
1437
- serializationsSinceTransitionRebuild = 0;
1438
- transitionsCount = 0;
1439
- if (recordIdsToRemove.length > 0)
1440
- recordIdsToRemove = [];
1441
- } else if (recordIdsToRemove.length > 0 && !isSequential) {
1442
- for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
1443
- recordIdsToRemove[i][RECORD_SYMBOL] = 0;
1444
- }
1445
- recordIdsToRemove = [];
1446
- }
1447
- };
1448
- const packArray = (value) => {
1449
- var length = value.length;
1450
- if (length < 0x10) {
1451
- target[position++] = 0x90 | length;
1452
- } else if (length < 0x10000) {
1453
- target[position++] = 0xdc;
1454
- target[position++] = length >> 8;
1455
- target[position++] = length & 0xff;
1456
- } else {
1457
- target[position++] = 0xdd;
1458
- targetView.setUint32(position, length);
1459
- position += 4;
1460
- }
1461
- for (let i = 0; i < length; i++) {
1462
- pack(value[i]);
1463
- }
1464
- };
1465
- const pack = (value) => {
1466
- if (position > safeEnd)
1467
- target = makeRoom(position);
1468
-
1469
- var type = typeof value;
1470
- var length;
1471
- if (type === 'string') {
1472
- let strLength = value.length;
1473
- if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
1474
- if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1475
- let extStart;
1476
- let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1477
- if (position + maxBytes > safeEnd)
1478
- target = makeRoom(position + maxBytes);
1479
- let lastBundle;
1480
- 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
1481
- lastBundle = bundledStrings;
1482
- target[position] = 0xc8; // ext 16
1483
- position += 3; // reserve for the writing bundle size
1484
- target[position++] = 0x62; // 'b'
1485
- extStart = position - start;
1486
- position += 4; // reserve for writing bundle reference
1487
- writeBundles(start, pack, 0); // write the last bundles
1488
- targetView.setUint16(extStart + start - 3, position - start - extStart);
1489
- } 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)
1490
- target[position++] = 0xd6; // fixext 4
1491
- target[position++] = 0x62; // 'b'
1492
- extStart = position - start;
1493
- position += 4; // reserve for writing bundle reference
1494
- }
1495
- bundledStrings = ['', '']; // create new ones
1496
- bundledStrings.previous = lastBundle;
1497
- bundledStrings.size = 0;
1498
- bundledStrings.position = extStart;
1499
- }
1500
- let twoByte = hasNonLatin.test(value);
1501
- bundledStrings[twoByte ? 0 : 1] += value;
1502
- target[position++] = 0xc1;
1503
- pack(twoByte ? -strLength : strLength);
1504
- return
1505
- }
1506
- let headerSize;
1507
- // first we estimate the header size, so we can write to the correct location
1508
- if (strLength < 0x20) {
1509
- headerSize = 1;
1510
- } else if (strLength < 0x100) {
1511
- headerSize = 2;
1512
- } else if (strLength < 0x10000) {
1513
- headerSize = 3;
1514
- } else {
1515
- headerSize = 5;
1516
- }
1517
- let maxBytes = strLength * 3;
1518
- if (position + maxBytes > safeEnd)
1519
- target = makeRoom(position + maxBytes);
1520
-
1521
- if (strLength < 0x40 || !encodeUtf8) {
1522
- let i, c1, c2, strPosition = position + headerSize;
1523
- for (i = 0; i < strLength; i++) {
1524
- c1 = value.charCodeAt(i);
1525
- if (c1 < 0x80) {
1526
- target[strPosition++] = c1;
1527
- } else if (c1 < 0x800) {
1528
- target[strPosition++] = c1 >> 6 | 0xc0;
1529
- target[strPosition++] = c1 & 0x3f | 0x80;
1530
- } else if (
1531
- (c1 & 0xfc00) === 0xd800 &&
1532
- ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
1533
- ) {
1534
- c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
1535
- i++;
1536
- target[strPosition++] = c1 >> 18 | 0xf0;
1537
- target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
1538
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1539
- target[strPosition++] = c1 & 0x3f | 0x80;
1540
- } else {
1541
- target[strPosition++] = c1 >> 12 | 0xe0;
1542
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1543
- target[strPosition++] = c1 & 0x3f | 0x80;
1544
- }
1545
- }
1546
- length = strPosition - position - headerSize;
1547
- } else {
1548
- length = encodeUtf8(value, position + headerSize);
1549
- }
1550
-
1551
- if (length < 0x20) {
1552
- target[position++] = 0xa0 | length;
1553
- } else if (length < 0x100) {
1554
- if (headerSize < 2) {
1555
- target.copyWithin(position + 2, position + 1, position + 1 + length);
1556
- }
1557
- target[position++] = 0xd9;
1558
- target[position++] = length;
1559
- } else if (length < 0x10000) {
1560
- if (headerSize < 3) {
1561
- target.copyWithin(position + 3, position + 2, position + 2 + length);
1562
- }
1563
- target[position++] = 0xda;
1564
- target[position++] = length >> 8;
1565
- target[position++] = length & 0xff;
1566
- } else {
1567
- if (headerSize < 5) {
1568
- target.copyWithin(position + 5, position + 3, position + 3 + length);
1569
- }
1570
- target[position++] = 0xdb;
1571
- targetView.setUint32(position, length);
1572
- position += 4;
1573
- }
1574
- position += length;
1575
- } else if (type === 'number') {
1576
- if (value >>> 0 === value) {// positive integer, 32-bit or less
1577
- // positive uint
1578
- if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1579
- target[position++] = value;
1580
- } else if (value < 0x100) {
1581
- target[position++] = 0xcc;
1582
- target[position++] = value;
1583
- } else if (value < 0x10000) {
1584
- target[position++] = 0xcd;
1585
- target[position++] = value >> 8;
1586
- target[position++] = value & 0xff;
1587
- } else {
1588
- target[position++] = 0xce;
1589
- targetView.setUint32(position, value);
1590
- position += 4;
1591
- }
1592
- } else if (value >> 0 === value) { // negative integer
1593
- if (value >= -0x20) {
1594
- target[position++] = 0x100 + value;
1595
- } else if (value >= -0x80) {
1596
- target[position++] = 0xd0;
1597
- target[position++] = value + 0x100;
1598
- } else if (value >= -0x8000) {
1599
- target[position++] = 0xd1;
1600
- targetView.setInt16(position, value);
1601
- position += 2;
1602
- } else {
1603
- target[position++] = 0xd2;
1604
- targetView.setInt32(position, value);
1605
- position += 4;
1606
- }
1607
- } else {
1608
- let useFloat32;
1609
- if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1610
- target[position++] = 0xca;
1611
- targetView.setFloat32(position, value);
1612
- let xShifted;
1613
- if (useFloat32 < 4 ||
1614
- // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1615
- ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1616
- position += 4;
1617
- return
1618
- } else
1619
- position--; // move back into position for writing a double
1620
- }
1621
- target[position++] = 0xcb;
1622
- targetView.setFloat64(position, value);
1623
- position += 8;
1624
- }
1625
- } else if (type === 'object' || type === 'function') {
1626
- if (!value)
1627
- target[position++] = 0xc0;
1628
- else {
1629
- if (referenceMap) {
1630
- let referee = referenceMap.get(value);
1631
- if (referee) {
1632
- if (!referee.id) {
1633
- let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1634
- referee.id = idsToInsert.push(referee);
1635
- }
1636
- target[position++] = 0xd6; // fixext 4
1637
- target[position++] = 0x70; // "p" for pointer
1638
- targetView.setUint32(position, referee.id);
1639
- position += 4;
1640
- return
1641
- } else
1642
- referenceMap.set(value, { offset: position - start });
1643
- }
1644
- let constructor = value.constructor;
1645
- if (constructor === Object) {
1646
- writeObject(value);
1647
- } else if (constructor === Array) {
1648
- packArray(value);
1649
- } else if (constructor === Map) {
1650
- if (this.mapAsEmptyObject) target[position++] = 0x80;
1651
- else {
1652
- length = value.size;
1653
- if (length < 0x10) {
1654
- target[position++] = 0x80 | length;
1655
- } else if (length < 0x10000) {
1656
- target[position++] = 0xde;
1657
- target[position++] = length >> 8;
1658
- target[position++] = length & 0xff;
1659
- } else {
1660
- target[position++] = 0xdf;
1661
- targetView.setUint32(position, length);
1662
- position += 4;
1663
- }
1664
- for (let [key, entryValue] of value) {
1665
- pack(key);
1666
- pack(entryValue);
1667
- }
1668
- }
1669
- } else {
1670
- for (let i = 0, l = extensions.length; i < l; i++) {
1671
- let extensionClass = extensionClasses[i];
1672
- if (value instanceof extensionClass) {
1673
- let extension = extensions[i];
1674
- if (extension.write) {
1675
- if (extension.type) {
1676
- target[position++] = 0xd4; // one byte "tag" extension
1677
- target[position++] = extension.type;
1678
- target[position++] = 0;
1679
- }
1680
- let writeResult = extension.write.call(this, value);
1681
- if (writeResult === value) { // avoid infinite recursion
1682
- if (Array.isArray(value)) {
1683
- packArray(value);
1684
- } else {
1685
- writeObject(value);
1686
- }
1687
- } else {
1688
- pack(writeResult);
1689
- }
1690
- return
1691
- }
1692
- let currentTarget = target;
1693
- let currentTargetView = targetView;
1694
- let currentPosition = position;
1695
- target = null;
1696
- let result;
1697
- try {
1698
- result = extension.pack.call(this, value, (size) => {
1699
- // restore target and use it
1700
- target = currentTarget;
1701
- currentTarget = null;
1702
- position += size;
1703
- if (position > safeEnd)
1704
- makeRoom(position);
1705
- return {
1706
- target, targetView, position: position - size
1707
- }
1708
- }, pack);
1709
- } finally {
1710
- // restore current target information (unless already restored)
1711
- if (currentTarget) {
1712
- target = currentTarget;
1713
- targetView = currentTargetView;
1714
- position = currentPosition;
1715
- safeEnd = target.length - 10;
1716
- }
1717
- }
1718
- if (result) {
1719
- if (result.length + position > safeEnd)
1720
- makeRoom(result.length + position);
1721
- position = writeExtensionData(result, target, position, extension.type);
1722
- }
1723
- return
1724
- }
1725
- }
1726
- // check isArray after extensions, because extensions can extend Array
1727
- if (Array.isArray(value)) {
1728
- packArray(value);
1729
- } else {
1730
- // use this as an alternate mechanism for expressing how to serialize
1731
- if (value.toJSON) {
1732
- const json = value.toJSON();
1733
- // if for some reason value.toJSON returns itself it'll loop forever
1734
- if (json !== value)
1735
- return pack(json)
1736
- }
1737
-
1738
- // if there is a writeFunction, use it, otherwise just encode as undefined
1739
- if (type === 'function')
1740
- return pack(this.writeFunction && this.writeFunction(value));
1741
-
1742
- // no extension found, write as plain object
1743
- writeObject(value);
1744
- }
1745
- }
1746
- }
1747
- } else if (type === 'boolean') {
1748
- target[position++] = value ? 0xc3 : 0xc2;
1749
- } else if (type === 'bigint') {
1750
- if (value < 0x8000000000000000 && value >= -0x8000000000000000) {
1751
- // use a signed int as long as it fits
1752
- target[position++] = 0xd3;
1753
- targetView.setBigInt64(position, value);
1754
- } else if (value < 0x10000000000000000 && value > 0) {
1755
- // if we can fit an unsigned int, use that
1756
- target[position++] = 0xcf;
1757
- targetView.setBigUint64(position, value);
1758
- } else {
1759
- // overflow
1760
- if (this.largeBigIntToFloat) {
1761
- target[position++] = 0xcb;
1762
- targetView.setFloat64(position, Number(value));
1763
- } else if (this.largeBigIntToString) {
1764
- return pack(value.toString());
1765
- } else if (this.useBigIntExtension || this.moreTypes) {
1766
- let empty = value < 0 ? BigInt(-1) : BigInt(0);
1767
-
1768
- let array;
1769
- if (value >> BigInt(0x10000) === empty) {
1770
- let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
1771
- let chunks = [];
1772
- while (true) {
1773
- chunks.push(value & mask);
1774
- if ((value >> BigInt(63)) === empty) break
1775
- value >>= BigInt(64);
1776
- }
1777
-
1778
- array = new Uint8Array(new BigUint64Array(chunks).buffer);
1779
- array.reverse();
1780
- } else {
1781
- let invert = value < 0;
1782
- let string = (invert ? ~value : value).toString(16);
1783
- if (string.length % 2) {
1784
- string = '0' + string;
1785
- } else if (parseInt(string.charAt(0), 16) >= 8) {
1786
- string = '00' + string;
1787
- }
1788
-
1789
- if (hasNodeBuffer$1) {
1790
- array = Buffer.from(string, 'hex');
1791
- } else {
1792
- array = new Uint8Array(string.length / 2);
1793
- for (let i = 0; i < array.length; i++) {
1794
- array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
1795
- }
1796
- }
1797
-
1798
- if (invert) {
1799
- for (let i = 0; i < array.length; i++) array[i] = ~array[i];
1800
- }
1801
- }
1802
-
1803
- if (array.length + position > safeEnd)
1804
- makeRoom(array.length + position);
1805
- position = writeExtensionData(array, target, position, 0x42);
1806
- return
1807
- } else {
1808
- throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
1809
- ' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
1810
- ' largeBigIntToString to convert to string')
1811
- }
1812
- }
1813
- position += 8;
1814
- } else if (type === 'undefined') {
1815
- if (this.encodeUndefinedAsNil)
1816
- target[position++] = 0xc0;
1817
- else {
1818
- target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1819
- target[position++] = 0;
1820
- target[position++] = 0;
1821
- }
1822
- } else {
1823
- throw new Error('Unknown type: ' + type)
1824
- }
1825
- };
1826
-
1827
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
1828
- // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1829
- let keys;
1830
- if (this.skipValues) {
1831
- keys = [];
1832
- for (let key in object) {
1833
- if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
1834
- !this.skipValues.includes(object[key]))
1835
- keys.push(key);
1836
- }
1837
- } else {
1838
- keys = Object.keys(object);
1839
- }
1840
- let length = keys.length;
1841
- if (length < 0x10) {
1842
- target[position++] = 0x80 | length;
1843
- } else if (length < 0x10000) {
1844
- target[position++] = 0xde;
1845
- target[position++] = length >> 8;
1846
- target[position++] = length & 0xff;
1847
- } else {
1848
- target[position++] = 0xdf;
1849
- targetView.setUint32(position, length);
1850
- position += 4;
1851
- }
1852
- let key;
1853
- if (this.coercibleKeyAsNumber) {
1854
- for (let i = 0; i < length; i++) {
1855
- key = keys[i];
1856
- let num = Number(key);
1857
- pack(isNaN(num) ? key : num);
1858
- pack(object[key]);
1859
- }
1860
-
1861
- } else {
1862
- for (let i = 0; i < length; i++) {
1863
- pack(key = keys[i]);
1864
- pack(object[key]);
1865
- }
1866
- }
1867
- } :
1868
- (object) => {
1869
- target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1870
- let objectOffset = position - start;
1871
- position += 2;
1872
- let size = 0;
1873
- for (let key in object) {
1874
- if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1875
- pack(key);
1876
- pack(object[key]);
1877
- size++;
1878
- }
1879
- }
1880
- if (size > 0xffff) {
1881
- throw new Error('Object is too large to serialize with fast 16-bit map size,' +
1882
- ' use the "variableMapSize" option to serialize this object');
1883
- }
1884
- target[objectOffset++ + start] = size >> 8;
1885
- target[objectOffset + start] = size & 0xff;
1886
- };
1887
-
1888
- const writeRecord = this.useRecords === false ? writePlainObject :
1889
- (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)
1890
- (object) => {
1891
- let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1892
- let objectOffset = position++ - start;
1893
- let wroteKeys;
1894
- for (let key in object) {
1895
- if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1896
- nextTransition = transition[key];
1897
- if (nextTransition)
1898
- transition = nextTransition;
1899
- else {
1900
- // record doesn't exist, create full new record and insert it
1901
- let keys = Object.keys(object);
1902
- let lastTransition = transition;
1903
- transition = structures.transitions;
1904
- let newTransitions = 0;
1905
- for (let i = 0, l = keys.length; i < l; i++) {
1906
- let key = keys[i];
1907
- nextTransition = transition[key];
1908
- if (!nextTransition) {
1909
- nextTransition = transition[key] = Object.create(null);
1910
- newTransitions++;
1911
- }
1912
- transition = nextTransition;
1913
- }
1914
- if (objectOffset + start + 1 == position) {
1915
- // first key, so we don't need to insert, we can just write record directly
1916
- position--;
1917
- newRecord(transition, keys, newTransitions);
1918
- } else // otherwise we need to insert the record, moving existing data after the record
1919
- insertNewRecord(transition, keys, objectOffset, newTransitions);
1920
- wroteKeys = true;
1921
- transition = lastTransition[key];
1922
- }
1923
- pack(object[key]);
1924
- }
1925
- }
1926
- if (!wroteKeys) {
1927
- let recordId = transition[RECORD_SYMBOL];
1928
- if (recordId)
1929
- target[objectOffset + start] = recordId;
1930
- else
1931
- insertNewRecord(transition, Object.keys(object), objectOffset, 0);
1932
- }
1933
- } :
1934
- (object) => {
1935
- let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1936
- let newTransitions = 0;
1937
- for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1938
- nextTransition = transition[key];
1939
- if (!nextTransition) {
1940
- nextTransition = transition[key] = Object.create(null);
1941
- newTransitions++;
1942
- }
1943
- transition = nextTransition;
1944
- }
1945
- let recordId = transition[RECORD_SYMBOL];
1946
- if (recordId) {
1947
- if (recordId >= 0x60 && useTwoByteRecords) {
1948
- target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1949
- target[position++] = recordId >> 5;
1950
- } else
1951
- target[position++] = recordId;
1952
- } else {
1953
- newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1954
- }
1955
- // now write the values
1956
- for (let key in object)
1957
- if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1958
- pack(object[key]);
1959
- }
1960
- };
1961
-
1962
- // create reference to useRecords if useRecords is a function
1963
- const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1964
-
1965
- const writeObject = checkUseRecords ? (object) => {
1966
- checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1967
- } : writeRecord;
1968
-
1969
- const makeRoom = (end) => {
1970
- let newSize;
1971
- if (end > 0x1000000) {
1972
- // special handling for really large buffers
1973
- if ((end - start) > MAX_BUFFER_SIZE)
1974
- throw new Error('Packed buffer would be larger than maximum buffer size')
1975
- newSize = Math.min(MAX_BUFFER_SIZE,
1976
- Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
1977
- } else // faster handling for smaller buffers
1978
- newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1979
- let newBuffer = new ByteArrayAllocate(newSize);
1980
- targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
1981
- end = Math.min(end, target.length);
1982
- if (target.copy)
1983
- target.copy(newBuffer, 0, start, end);
1984
- else
1985
- newBuffer.set(target.slice(start, end));
1986
- position -= start;
1987
- start = 0;
1988
- safeEnd = newBuffer.length - 10;
1989
- return target = newBuffer
1990
- };
1991
- const newRecord = (transition, keys, newTransitions) => {
1992
- let recordId = structures.nextId;
1993
- if (!recordId)
1994
- recordId = 0x40;
1995
- if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
1996
- recordId = structures.nextOwnId;
1997
- if (!(recordId < maxStructureId))
1998
- recordId = sharedLimitId;
1999
- structures.nextOwnId = recordId + 1;
2000
- } else {
2001
- if (recordId >= maxStructureId)// cycle back around
2002
- recordId = sharedLimitId;
2003
- structures.nextId = recordId + 1;
2004
- }
2005
- let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1;
2006
- transition[RECORD_SYMBOL] = recordId;
2007
- transition.__keys__ = keys;
2008
- structures[recordId - 0x40] = keys;
2009
-
2010
- if (recordId < sharedLimitId) {
2011
- keys.isShared = true;
2012
- structures.sharedLength = recordId - 0x3f;
2013
- hasSharedUpdate = true;
2014
- if (highByte >= 0) {
2015
- target[position++] = (recordId & 0x1f) + 0x60;
2016
- target[position++] = highByte;
2017
- } else {
2018
- target[position++] = recordId;
2019
- }
2020
- } else {
2021
- if (highByte >= 0) {
2022
- target[position++] = 0xd5; // fixext 2
2023
- target[position++] = 0x72; // "r" record defintion extension type
2024
- target[position++] = (recordId & 0x1f) + 0x60;
2025
- target[position++] = highByte;
2026
- } else {
2027
- target[position++] = 0xd4; // fixext 1
2028
- target[position++] = 0x72; // "r" record defintion extension type
2029
- target[position++] = recordId;
2030
- }
2031
-
2032
- if (newTransitions)
2033
- transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
2034
- // record the removal of the id, we can maintain our shared structure
2035
- if (recordIdsToRemove.length >= maxOwnStructures)
2036
- recordIdsToRemove.shift()[RECORD_SYMBOL] = 0; // we are cycling back through, and have to remove old ones
2037
- recordIdsToRemove.push(transition);
2038
- pack(keys);
2039
- }
2040
- };
2041
- const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
2042
- let mainTarget = target;
2043
- let mainPosition = position;
2044
- let mainSafeEnd = safeEnd;
2045
- let mainStart = start;
2046
- target = keysTarget;
2047
- position = 0;
2048
- start = 0;
2049
- if (!target)
2050
- keysTarget = target = new ByteArrayAllocate(8192);
2051
- safeEnd = target.length - 10;
2052
- newRecord(transition, keys, newTransitions);
2053
- keysTarget = target;
2054
- let keysPosition = position;
2055
- target = mainTarget;
2056
- position = mainPosition;
2057
- safeEnd = mainSafeEnd;
2058
- start = mainStart;
2059
- if (keysPosition > 1) {
2060
- let newEnd = position + keysPosition - 1;
2061
- if (newEnd > safeEnd)
2062
- makeRoom(newEnd);
2063
- let insertionPosition = insertionOffset + start;
2064
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
2065
- target.set(keysTarget.slice(0, keysPosition), insertionPosition);
2066
- position = newEnd;
2067
- } else {
2068
- target[insertionOffset + start] = keysTarget[0];
2069
- }
2070
- };
2071
- const writeStruct = (object) => {
2072
- let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
2073
- if (notifySharedUpdate)
2074
- return hasSharedUpdate = true;
2075
- position = newPosition;
2076
- let startTarget = target;
2077
- pack(value);
2078
- resetStructures();
2079
- if (startTarget !== target) {
2080
- return { position, targetView, target }; // indicate the buffer was re-allocated
2081
- }
2082
- return position;
2083
- }, this);
2084
- if (newPosition === 0) // bail and go to a msgpack object
2085
- return writeObject(object);
2086
- position = newPosition;
2087
- };
2088
- }
2089
- useBuffer(buffer) {
2090
- // this means we are finished using our own buffer and we can write over it safely
2091
- target = buffer;
2092
- target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2093
- targetView = target.dataView;
2094
- position = 0;
2095
- }
2096
- set position (value) {
2097
- position = value;
2098
- }
2099
- get position() {
2100
- return position;
2101
- }
2102
- clearSharedData() {
2103
- if (this.structures)
2104
- this.structures = [];
2105
- if (this.typedStructs)
2106
- this.typedStructs = [];
2107
- }
2108
- }
2109
-
2110
- extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, DataView, C1Type ];
2111
- extensions = [{
2112
- pack(date, allocateForWrite, pack) {
2113
- let seconds = date.getTime() / 1000;
2114
- if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
2115
- // Timestamp 32
2116
- let { target, targetView, position} = allocateForWrite(6);
2117
- target[position++] = 0xd6;
2118
- target[position++] = 0xff;
2119
- targetView.setUint32(position, seconds);
2120
- } else if (seconds > 0 && seconds < 0x100000000) {
2121
- // Timestamp 64
2122
- let { target, targetView, position} = allocateForWrite(10);
2123
- target[position++] = 0xd7;
2124
- target[position++] = 0xff;
2125
- targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0));
2126
- targetView.setUint32(position + 4, seconds);
2127
- } else if (isNaN(seconds)) {
2128
- if (this.onInvalidDate) {
2129
- allocateForWrite(0);
2130
- return pack(this.onInvalidDate())
2131
- }
2132
- // Intentionally invalid timestamp
2133
- let { target, targetView, position} = allocateForWrite(3);
2134
- target[position++] = 0xd4;
2135
- target[position++] = 0xff;
2136
- target[position++] = 0xff;
2137
- } else {
2138
- // Timestamp 96
2139
- let { target, targetView, position} = allocateForWrite(15);
2140
- target[position++] = 0xc7;
2141
- target[position++] = 12;
2142
- target[position++] = 0xff;
2143
- targetView.setUint32(position, date.getMilliseconds() * 1000000);
2144
- targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)));
2145
- }
2146
- }
2147
- }, {
2148
- pack(set, allocateForWrite, pack) {
2149
- if (this.setAsEmptyObject) {
2150
- allocateForWrite(0);
2151
- return pack({})
2152
- }
2153
- let array = Array.from(set);
2154
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
2155
- if (this.moreTypes) {
2156
- target[position++] = 0xd4;
2157
- target[position++] = 0x73; // 's' for Set
2158
- target[position++] = 0;
2159
- }
2160
- pack(array);
2161
- }
2162
- }, {
2163
- pack(error, allocateForWrite, pack) {
2164
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
2165
- if (this.moreTypes) {
2166
- target[position++] = 0xd4;
2167
- target[position++] = 0x65; // 'e' for error
2168
- target[position++] = 0;
2169
- }
2170
- pack([ error.name, error.message, error.cause ]);
2171
- }
2172
- }, {
2173
- pack(regex, allocateForWrite, pack) {
2174
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
2175
- if (this.moreTypes) {
2176
- target[position++] = 0xd4;
2177
- target[position++] = 0x78; // 'x' for regeXp
2178
- target[position++] = 0;
2179
- }
2180
- pack([ regex.source, regex.flags ]);
2181
- }
2182
- }, {
2183
- pack(arrayBuffer, allocateForWrite) {
2184
- if (this.moreTypes)
2185
- writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
2186
- else
2187
- writeBuffer(hasNodeBuffer$1 ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2188
- }
2189
- }, {
2190
- pack(typedArray, allocateForWrite) {
2191
- let constructor = typedArray.constructor;
2192
- if (constructor !== ByteArray && this.moreTypes)
2193
- writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
2194
- else
2195
- writeBuffer(typedArray, allocateForWrite);
2196
- }
2197
- }, {
2198
- pack(arrayBuffer, allocateForWrite) {
2199
- if (this.moreTypes)
2200
- writeExtBuffer(arrayBuffer, 0x11, allocateForWrite);
2201
- else
2202
- writeBuffer(hasNodeBuffer$1 ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2203
- }
2204
- }, {
2205
- pack(c1, allocateForWrite) { // specific 0xC1 object
2206
- let { target, position} = allocateForWrite(1);
2207
- target[position] = 0xc1;
2208
- }
2209
- }];
2210
-
2211
- function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
2212
- let length = typedArray.byteLength;
2213
- if (length + 1 < 0x100) {
2214
- var { target, position } = allocateForWrite(4 + length);
2215
- target[position++] = 0xc7;
2216
- target[position++] = length + 1;
2217
- } else if (length + 1 < 0x10000) {
2218
- var { target, position } = allocateForWrite(5 + length);
2219
- target[position++] = 0xc8;
2220
- target[position++] = (length + 1) >> 8;
2221
- target[position++] = (length + 1) & 0xff;
2222
- } else {
2223
- var { target, position, targetView } = allocateForWrite(7 + length);
2224
- target[position++] = 0xc9;
2225
- targetView.setUint32(position, length + 1); // plus one for the type byte
2226
- position += 4;
2227
- }
2228
- target[position++] = 0x74; // "t" for typed array
2229
- target[position++] = type;
2230
- if (!typedArray.buffer) typedArray = new Uint8Array(typedArray);
2231
- target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
2232
- }
2233
- function writeBuffer(buffer, allocateForWrite) {
2234
- let length = buffer.byteLength;
2235
- var target, position;
2236
- if (length < 0x100) {
2237
- var { target, position } = allocateForWrite(length + 2);
2238
- target[position++] = 0xc4;
2239
- target[position++] = length;
2240
- } else if (length < 0x10000) {
2241
- var { target, position } = allocateForWrite(length + 3);
2242
- target[position++] = 0xc5;
2243
- target[position++] = length >> 8;
2244
- target[position++] = length & 0xff;
2245
- } else {
2246
- var { target, position, targetView } = allocateForWrite(length + 5);
2247
- target[position++] = 0xc6;
2248
- targetView.setUint32(position, length);
2249
- position += 4;
2250
- }
2251
- target.set(buffer, position);
2252
- }
2253
-
2254
- function writeExtensionData(result, target, position, type) {
2255
- let length = result.length;
2256
- switch (length) {
2257
- case 1:
2258
- target[position++] = 0xd4;
2259
- break
2260
- case 2:
2261
- target[position++] = 0xd5;
2262
- break
2263
- case 4:
2264
- target[position++] = 0xd6;
2265
- break
2266
- case 8:
2267
- target[position++] = 0xd7;
2268
- break
2269
- case 16:
2270
- target[position++] = 0xd8;
2271
- break
2272
- default:
2273
- if (length < 0x100) {
2274
- target[position++] = 0xc7;
2275
- target[position++] = length;
2276
- } else if (length < 0x10000) {
2277
- target[position++] = 0xc8;
2278
- target[position++] = length >> 8;
2279
- target[position++] = length & 0xff;
2280
- } else {
2281
- target[position++] = 0xc9;
2282
- target[position++] = length >> 24;
2283
- target[position++] = (length >> 16) & 0xff;
2284
- target[position++] = (length >> 8) & 0xff;
2285
- target[position++] = length & 0xff;
2286
- }
2287
- }
2288
- target[position++] = type;
2289
- target.set(result, position);
2290
- position += length;
2291
- return position
2292
- }
2293
-
2294
- function insertIds(serialized, idsToInsert) {
2295
- // insert the ids that need to be referenced for structured clones
2296
- let nextId;
2297
- let distanceToMove = idsToInsert.length * 6;
2298
- let lastEnd = serialized.length - distanceToMove;
2299
- while (nextId = idsToInsert.pop()) {
2300
- let offset = nextId.offset;
2301
- let id = nextId.id;
2302
- serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
2303
- distanceToMove -= 6;
2304
- let position = offset + distanceToMove;
2305
- serialized[position++] = 0xd6;
2306
- serialized[position++] = 0x69; // 'i'
2307
- serialized[position++] = id >> 24;
2308
- serialized[position++] = (id >> 16) & 0xff;
2309
- serialized[position++] = (id >> 8) & 0xff;
2310
- serialized[position++] = id & 0xff;
2311
- lastEnd = offset;
2312
- }
2313
- return serialized
2314
- }
2315
-
2316
- function writeBundles(start, pack, incrementPosition) {
2317
- if (bundledStrings.length > 0) {
2318
- targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
2319
- bundledStrings.stringsPosition = position - start;
2320
- let writeStrings = bundledStrings;
2321
- bundledStrings = null;
2322
- pack(writeStrings[0]);
2323
- pack(writeStrings[1]);
2324
- }
2325
- }
2326
-
2327
- function addExtension(extension) {
2328
- if (extension.Class) {
2329
- if (!extension.pack && !extension.write)
2330
- throw new Error('Extension has no pack or write function')
2331
- if (extension.pack && !extension.type)
2332
- throw new Error('Extension has no type (numeric code to identify the extension)')
2333
- extensionClasses.unshift(extension.Class);
2334
- extensions.unshift(extension);
2335
- }
2336
- addExtension$1(extension);
2337
- }
2338
- function prepareStructures$1(structures, packr) {
2339
- structures.isCompatible = (existingStructures) => {
2340
- let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
2341
- if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
2342
- packr._mergeStructures(existingStructures);
2343
- return compatible;
2344
- };
2345
- return structures
2346
- }
2347
- function setWriteStructSlots(writeSlots, makeStructures) {
2348
- writeStructSlots = writeSlots;
2349
- prepareStructures$1 = makeStructures;
2350
- }
2351
-
2352
- let defaultPackr = new Packr({ useRecords: false });
2353
- const pack = defaultPackr.pack;
2354
- const encode = defaultPackr.pack;
2355
- const Encoder = Packr;
2356
- const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
2357
- const REUSE_BUFFER_MODE = 512;
2358
- const RESET_BUFFER_MODE = 1024;
2359
- const RESERVE_START_SPACE = 2048;
2360
-
2361
- const ASCII = 3; // the MIBenum from https://www.iana.org/assignments/character-sets/character-sets.xhtml (and other character encodings could be referenced by MIBenum)
2362
- const NUMBER = 0;
2363
- const UTF8 = 2;
2364
- const OBJECT_DATA = 1;
2365
- const DATE = 16;
2366
- const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
2367
- TYPE_NAMES[DATE] = 'date';
2368
- const float32Headers = [false, true, true, false, false, true, true, false];
2369
- let evalSupported;
2370
- try {
2371
- new Function('');
2372
- evalSupported = true;
2373
- } catch(error) {
2374
- // if eval variants are not supported, do not create inline object readers ever
2375
- }
2376
-
2377
- let updatedPosition;
2378
- const hasNodeBuffer = typeof Buffer !== 'undefined';
2379
- let textEncoder, currentSource;
2380
- try {
2381
- textEncoder = new TextEncoder();
2382
- } catch (error) {}
2383
- const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2384
- return target.utf8Write(string, position, target.byteLength - position)
2385
- } : (textEncoder && textEncoder.encodeInto) ?
2386
- function(target, string, position) {
2387
- return textEncoder.encodeInto(string, target.subarray(position)).written
2388
- } : false;
2389
- setWriteStructSlots(writeStruct, prepareStructures);
2390
- function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
2391
- let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2392
- // note that we rely on pack.js to load stored structures before we get to this point
2393
- let targetView = target.dataView;
2394
- let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
2395
- let safeEnd = target.length - 10;
2396
- let start = position;
2397
- if (position > safeEnd) {
2398
- target = makeRoom(position);
2399
- targetView = target.dataView;
2400
- position -= encodingStart;
2401
- start -= encodingStart;
2402
- refsStartPosition -= encodingStart;
2403
- encodingStart = 0;
2404
- safeEnd = target.length - 10;
2405
- }
2406
-
2407
- let refOffset, refPosition = refsStartPosition;
2408
-
2409
- let transition = typedStructs.transitions || (typedStructs.transitions = Object.create(null));
2410
- let nextId = typedStructs.nextId || typedStructs.length;
2411
- let headerSize =
2412
- nextId < 0xf ? 1 :
2413
- nextId < 0xf0 ? 2 :
2414
- nextId < 0xf000 ? 3 :
2415
- nextId < 0xf00000 ? 4 : 0;
2416
- if (headerSize === 0)
2417
- return 0;
2418
- position += headerSize;
2419
- let queuedReferences = [];
2420
- let usedAscii0;
2421
- let keyIndex = 0;
2422
- for (let key in object) {
2423
- let value = object[key];
2424
- let nextTransition = transition[key];
2425
- if (!nextTransition) {
2426
- transition[key] = nextTransition = {
2427
- key,
2428
- parent: transition,
2429
- enumerationOffset: 0,
2430
- ascii0: null,
2431
- ascii8: null,
2432
- num8: null,
2433
- string16: null,
2434
- object16: null,
2435
- num32: null,
2436
- float64: null,
2437
- date64: null
2438
- };
2439
- }
2440
- if (position > safeEnd) {
2441
- target = makeRoom(position);
2442
- targetView = target.dataView;
2443
- position -= encodingStart;
2444
- start -= encodingStart;
2445
- refsStartPosition -= encodingStart;
2446
- refPosition -= encodingStart;
2447
- encodingStart = 0;
2448
- safeEnd = target.length - 10;
2449
- }
2450
- switch (typeof value) {
2451
- case 'number':
2452
- let number = value;
2453
- // first check to see if we are using a lot of ids and should default to wide/common format
2454
- if (nextId < 200 || !nextTransition.num64) {
2455
- if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
2456
- if (number < 0xf6 && number >= 0 && (nextTransition.num8 && !(nextId > 200 && nextTransition.num32) || number < 0x20 && !nextTransition.num32)) {
2457
- transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
2458
- target[position++] = number;
2459
- } else {
2460
- transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2461
- targetView.setUint32(position, number, true);
2462
- position += 4;
2463
- }
2464
- break;
2465
- } else if (number < 0x100000000 && number >= -0x80000000) {
2466
- targetView.setFloat32(position, number, true);
2467
- if (float32Headers[target[position + 3] >>> 5]) {
2468
- let xShifted;
2469
- // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2470
- if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
2471
- transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2472
- position += 4;
2473
- break;
2474
- }
2475
- }
2476
- }
2477
- }
2478
- transition = nextTransition.num64 || createTypeTransition(nextTransition, NUMBER, 8);
2479
- targetView.setFloat64(position, number, true);
2480
- position += 8;
2481
- break;
2482
- case 'string':
2483
- let strLength = value.length;
2484
- refOffset = refPosition - refsStartPosition;
2485
- if ((strLength << 2) + refPosition > safeEnd) {
2486
- target = makeRoom((strLength << 2) + refPosition);
2487
- targetView = target.dataView;
2488
- position -= encodingStart;
2489
- start -= encodingStart;
2490
- refsStartPosition -= encodingStart;
2491
- refPosition -= encodingStart;
2492
- encodingStart = 0;
2493
- safeEnd = target.length - 10;
2494
- }
2495
- if (strLength > ((0xff00 + refOffset) >> 2)) {
2496
- queuedReferences.push(key, value, position - start);
2497
- break;
2498
- }
2499
- let isNotAscii;
2500
- let strStart = refPosition;
2501
- if (strLength < 0x40) {
2502
- let i, c1, c2;
2503
- for (i = 0; i < strLength; i++) {
2504
- c1 = value.charCodeAt(i);
2505
- if (c1 < 0x80) {
2506
- target[refPosition++] = c1;
2507
- } else if (c1 < 0x800) {
2508
- isNotAscii = true;
2509
- target[refPosition++] = c1 >> 6 | 0xc0;
2510
- target[refPosition++] = c1 & 0x3f | 0x80;
2511
- } else if (
2512
- (c1 & 0xfc00) === 0xd800 &&
2513
- ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
2514
- ) {
2515
- isNotAscii = true;
2516
- c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
2517
- i++;
2518
- target[refPosition++] = c1 >> 18 | 0xf0;
2519
- target[refPosition++] = c1 >> 12 & 0x3f | 0x80;
2520
- target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
2521
- target[refPosition++] = c1 & 0x3f | 0x80;
2522
- } else {
2523
- isNotAscii = true;
2524
- target[refPosition++] = c1 >> 12 | 0xe0;
2525
- target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
2526
- target[refPosition++] = c1 & 0x3f | 0x80;
2527
- }
2528
- }
2529
- } else {
2530
- refPosition += encodeUtf8(target, value, refPosition);
2531
- isNotAscii = refPosition - strStart > strLength;
2532
- }
2533
- if (refOffset < 0xa0 || (refOffset < 0xf6 && (nextTransition.ascii8 || nextTransition.string8))) {
2534
- // short strings
2535
- if (isNotAscii) {
2536
- if (!(transition = nextTransition.string8)) {
2537
- if (typedStructs.length > 10 && (transition = nextTransition.ascii8)) {
2538
- // we can safely change ascii to utf8 in place since they are compatible
2539
- transition.__type = UTF8;
2540
- nextTransition.ascii8 = null;
2541
- nextTransition.string8 = transition;
2542
- pack(null, 0, true); // special call to notify that structures have been updated
2543
- } else {
2544
- transition = createTypeTransition(nextTransition, UTF8, 1);
2545
- }
2546
- }
2547
- } else if (refOffset === 0 && !usedAscii0) {
2548
- usedAscii0 = true;
2549
- transition = nextTransition.ascii0 || createTypeTransition(nextTransition, ASCII, 0);
2550
- break; // don't increment position
2551
- }// else ascii:
2552
- else if (!(transition = nextTransition.ascii8) && !(typedStructs.length > 10 && (transition = nextTransition.string8)))
2553
- transition = createTypeTransition(nextTransition, ASCII, 1);
2554
- target[position++] = refOffset;
2555
- } else {
2556
- // TODO: Enable ascii16 at some point, but get the logic right
2557
- //if (isNotAscii)
2558
- transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
2559
- //else
2560
- //transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
2561
- targetView.setUint16(position, refOffset, true);
2562
- position += 2;
2563
- }
2564
- break;
2565
- case 'object':
2566
- if (value) {
2567
- if (value.constructor === Date) {
2568
- transition = nextTransition.date64 || createTypeTransition(nextTransition, DATE, 8);
2569
- targetView.setFloat64(position, value.getTime(), true);
2570
- position += 8;
2571
- } else {
2572
- queuedReferences.push(key, value, keyIndex);
2573
- }
2574
- break;
2575
- } else { // null
2576
- nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
2577
- if (nextTransition) {
2578
- transition = nextTransition;
2579
- position = updatedPosition;
2580
- } else queuedReferences.push(key, value, keyIndex);
2581
- }
2582
- break;
2583
- case 'boolean':
2584
- transition = nextTransition.num8 || nextTransition.ascii8 || createTypeTransition(nextTransition, NUMBER, 1);
2585
- target[position++] = value ? 0xf9 : 0xf8; // match CBOR with these
2586
- break;
2587
- case 'undefined':
2588
- nextTransition = anyType(nextTransition, position, targetView, -9); // match CBOR with this
2589
- if (nextTransition) {
2590
- transition = nextTransition;
2591
- position = updatedPosition;
2592
- } else queuedReferences.push(key, value, keyIndex);
2593
- break;
2594
- default:
2595
- queuedReferences.push(key, value, keyIndex);
2596
- }
2597
- keyIndex++;
2598
- }
2599
-
2600
- for (let i = 0, l = queuedReferences.length; i < l;) {
2601
- let key = queuedReferences[i++];
2602
- let value = queuedReferences[i++];
2603
- let propertyIndex = queuedReferences[i++];
2604
- let nextTransition = transition[key];
2605
- if (!nextTransition) {
2606
- transition[key] = nextTransition = {
2607
- key,
2608
- parent: transition,
2609
- enumerationOffset: propertyIndex - keyIndex,
2610
- ascii0: null,
2611
- ascii8: null,
2612
- num8: null,
2613
- string16: null,
2614
- object16: null,
2615
- num32: null,
2616
- float64: null
2617
- };
2618
- }
2619
- let newPosition;
2620
- if (value) {
2621
- /*if (typeof value === 'string') { // TODO: we could re-enable long strings
2622
- if (position + value.length * 3 > safeEnd) {
2623
- target = makeRoom(position + value.length * 3);
2624
- position -= start;
2625
- targetView = target.dataView;
2626
- start = 0;
2627
- }
2628
- newPosition = position + target.utf8Write(value, position, 0xffffffff);
2629
- } else { */
2630
- let size;
2631
- refOffset = refPosition - refsStartPosition;
2632
- if (refOffset < 0xff00) {
2633
- transition = nextTransition.object16;
2634
- if (transition)
2635
- size = 2;
2636
- else if ((transition = nextTransition.object32))
2637
- size = 4;
2638
- else {
2639
- transition = createTypeTransition(nextTransition, OBJECT_DATA, 2);
2640
- size = 2;
2641
- }
2642
- } else {
2643
- transition = nextTransition.object32 || createTypeTransition(nextTransition, OBJECT_DATA, 4);
2644
- size = 4;
2645
- }
2646
- newPosition = pack(value, refPosition);
2647
- //}
2648
- if (typeof newPosition === 'object') {
2649
- // re-allocated
2650
- refPosition = newPosition.position;
2651
- targetView = newPosition.targetView;
2652
- target = newPosition.target;
2653
- refsStartPosition -= encodingStart;
2654
- position -= encodingStart;
2655
- start -= encodingStart;
2656
- encodingStart = 0;
2657
- } else
2658
- refPosition = newPosition;
2659
- if (size === 2) {
2660
- targetView.setUint16(position, refOffset, true);
2661
- position += 2;
2662
- } else {
2663
- targetView.setUint32(position, refOffset, true);
2664
- position += 4;
2665
- }
2666
- } else { // null or undefined
2667
- transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
2668
- targetView.setInt16(position, value === null ? -10 : -9, true);
2669
- position += 2;
2670
- }
2671
- keyIndex++;
2672
- }
2673
-
2674
-
2675
- let recordId = transition[RECORD_SYMBOL];
2676
- if (recordId == null) {
2677
- recordId = packr.typedStructs.length;
2678
- let structure = [];
2679
- let nextTransition = transition;
2680
- let key, type;
2681
- while ((type = nextTransition.__type) !== undefined) {
2682
- let size = nextTransition.__size;
2683
- nextTransition = nextTransition.__parent;
2684
- key = nextTransition.key;
2685
- let property = [type, size, key];
2686
- if (nextTransition.enumerationOffset)
2687
- property.push(nextTransition.enumerationOffset);
2688
- structure.push(property);
2689
- nextTransition = nextTransition.parent;
2690
- }
2691
- structure.reverse();
2692
- transition[RECORD_SYMBOL] = recordId;
2693
- packr.typedStructs[recordId] = structure;
2694
- pack(null, 0, true); // special call to notify that structures have been updated
2695
- }
2696
-
2697
-
2698
- switch (headerSize) {
2699
- case 1:
2700
- if (recordId >= 0x10) return 0;
2701
- target[start] = recordId + 0x20;
2702
- break;
2703
- case 2:
2704
- if (recordId >= 0x100) return 0;
2705
- target[start] = 0x38;
2706
- target[start + 1] = recordId;
2707
- break;
2708
- case 3:
2709
- if (recordId >= 0x10000) return 0;
2710
- target[start] = 0x39;
2711
- targetView.setUint16(start + 1, recordId, true);
2712
- break;
2713
- case 4:
2714
- if (recordId >= 0x1000000) return 0;
2715
- targetView.setUint32(start, (recordId << 8) + 0x3a, true);
2716
- break;
2717
- }
2718
-
2719
- if (position < refsStartPosition) {
2720
- if (refsStartPosition === refPosition)
2721
- return position; // no refs
2722
- // adjust positioning
2723
- target.copyWithin(position, refsStartPosition, refPosition);
2724
- refPosition += position - refsStartPosition;
2725
- typedStructs.lastStringStart = position - start;
2726
- } else if (position > refsStartPosition) {
2727
- if (refsStartPosition === refPosition)
2728
- return position; // no refs
2729
- typedStructs.lastStringStart = position - start;
2730
- return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
2731
- }
2732
- return refPosition;
2733
- }
2734
- function anyType(transition, position, targetView, value) {
2735
- let nextTransition;
2736
- if ((nextTransition = transition.ascii8 || transition.num8)) {
2737
- targetView.setInt8(position, value, true);
2738
- updatedPosition = position + 1;
2739
- return nextTransition;
2740
- }
2741
- if ((nextTransition = transition.string16 || transition.object16)) {
2742
- targetView.setInt16(position, value, true);
2743
- updatedPosition = position + 2;
2744
- return nextTransition;
2745
- }
2746
- if (nextTransition = transition.num32) {
2747
- targetView.setUint32(position, 0xe0000100 + value, true);
2748
- updatedPosition = position + 4;
2749
- return nextTransition;
2750
- }
2751
- // transition.float64
2752
- if (nextTransition = transition.num64) {
2753
- targetView.setFloat64(position, NaN, true);
2754
- targetView.setInt8(position, value);
2755
- updatedPosition = position + 8;
2756
- return nextTransition;
2757
- }
2758
- updatedPosition = position;
2759
- // TODO: can we do an "any" type where we defer the decision?
2760
- return;
2761
- }
2762
- function createTypeTransition(transition, type, size) {
2763
- let typeName = TYPE_NAMES[type] + (size << 3);
2764
- let newTransition = transition[typeName] || (transition[typeName] = Object.create(null));
2765
- newTransition.__type = type;
2766
- newTransition.__size = size;
2767
- newTransition.__parent = transition;
2768
- return newTransition;
2769
- }
2770
- function onLoadedStructures(sharedData) {
2771
- if (!(sharedData instanceof Map))
2772
- return sharedData;
2773
- let typed = sharedData.get('typed') || [];
2774
- if (Object.isFrozen(typed))
2775
- typed = typed.map(structure => structure.slice(0));
2776
- let named = sharedData.get('named');
2777
- let transitions = Object.create(null);
2778
- for (let i = 0, l = typed.length; i < l; i++) {
2779
- let structure = typed[i];
2780
- let transition = transitions;
2781
- for (let [type, size, key] of structure) {
2782
- let nextTransition = transition[key];
2783
- if (!nextTransition) {
2784
- transition[key] = nextTransition = {
2785
- key,
2786
- parent: transition,
2787
- enumerationOffset: 0,
2788
- ascii0: null,
2789
- ascii8: null,
2790
- num8: null,
2791
- string16: null,
2792
- object16: null,
2793
- num32: null,
2794
- float64: null,
2795
- date64: null,
2796
- };
2797
- }
2798
- transition = createTypeTransition(nextTransition, type, size);
2799
- }
2800
- transition[RECORD_SYMBOL] = i;
2801
- }
2802
- typed.transitions = transitions;
2803
- this.typedStructs = typed;
2804
- this.lastTypedStructuresLength = typed.length;
2805
- return named;
2806
- }
2807
- var sourceSymbol = Symbol.for('source');
2808
- function readStruct(src, position, srcEnd, unpackr) {
2809
- let recordId = src[position++] - 0x20;
2810
- if (recordId >= 24) {
2811
- switch(recordId) {
2812
- case 24: recordId = src[position++]; break;
2813
- // little endian:
2814
- case 25: recordId = src[position++] + (src[position++] << 8); break;
2815
- case 26: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16); break;
2816
- case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
2817
- }
2818
- }
2819
- let structure = unpackr.typedStructs && unpackr.typedStructs[recordId];
2820
- if (!structure) {
2821
- // copy src buffer because getStructures will override it
2822
- src = Uint8Array.prototype.slice.call(src, position, srcEnd);
2823
- srcEnd -= position;
2824
- position = 0;
2825
- if (!unpackr.getStructures)
2826
- throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
2827
- unpackr._mergeStructures(unpackr.getStructures());
2828
- if (!unpackr.typedStructs)
2829
- throw new Error('Could not find any shared typed structures');
2830
- unpackr.lastTypedStructuresLength = unpackr.typedStructs.length;
2831
- structure = unpackr.typedStructs[recordId];
2832
- if (!structure)
2833
- throw new Error('Could not find typed structure ' + recordId);
2834
- }
2835
- var construct = structure.construct;
2836
- var fullConstruct = structure.fullConstruct;
2837
- if (!construct) {
2838
- construct = structure.construct = function LazyObject() {
2839
- };
2840
- fullConstruct = structure.fullConstruct = function LoadedObject() {
2841
- };
2842
- fullConstruct.prototype = unpackr.structPrototype || {};
2843
- var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
2844
- let properties = [];
2845
- let currentOffset = 0;
2846
- let lastRefProperty;
2847
- for (let i = 0, l = structure.length; i < l; i++) {
2848
- let definition = structure[i];
2849
- let [ type, size, key, enumerationOffset ] = definition;
2850
- if (key === '__proto__')
2851
- key = '__proto_';
2852
- let property = {
2853
- key,
2854
- offset: currentOffset,
2855
- };
2856
- if (enumerationOffset)
2857
- properties.splice(i + enumerationOffset, 0, property);
2858
- else
2859
- properties.push(property);
2860
- let getRef;
2861
- switch(size) { // TODO: Move into a separate function
2862
- case 0: getRef = () => 0; break;
2863
- case 1:
2864
- getRef = (source, position) => {
2865
- let ref = source.bytes[position + property.offset];
2866
- return ref >= 0xf6 ? toConstant(ref) : ref;
2867
- };
2868
- break;
2869
- case 2:
2870
- getRef = (source, position) => {
2871
- let src = source.bytes;
2872
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2873
- let ref = dataView.getUint16(position + property.offset, true);
2874
- return ref >= 0xff00 ? toConstant(ref & 0xff) : ref;
2875
- };
2876
- break;
2877
- case 4:
2878
- getRef = (source, position) => {
2879
- let src = source.bytes;
2880
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2881
- let ref = dataView.getUint32(position + property.offset, true);
2882
- return ref >= 0xffffff00 ? toConstant(ref & 0xff) : ref;
2883
- };
2884
- break;
2885
- }
2886
- property.getRef = getRef;
2887
- currentOffset += size;
2888
- let get;
2889
- switch(type) {
2890
- case ASCII:
2891
- if (lastRefProperty && !lastRefProperty.next)
2892
- lastRefProperty.next = property;
2893
- lastRefProperty = property;
2894
- property.multiGetCount = 0;
2895
- get = function(source) {
2896
- let src = source.bytes;
2897
- let position = source.position;
2898
- let refStart = currentOffset + position;
2899
- let ref = getRef(source, position);
2900
- if (typeof ref !== 'number') return ref;
2901
-
2902
- let end, next = property.next;
2903
- while(next) {
2904
- end = next.getRef(source, position);
2905
- if (typeof end === 'number')
2906
- break;
2907
- else
2908
- end = null;
2909
- next = next.next;
2910
- }
2911
- if (end == null)
2912
- end = source.bytesEnd - refStart;
2913
- if (source.srcString) {
2914
- return source.srcString.slice(ref, end);
2915
- }
2916
- /*if (property.multiGetCount > 0) {
2917
- let asciiEnd;
2918
- next = firstRefProperty;
2919
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2920
- do {
2921
- asciiEnd = dataView.getUint16(source.position + next.offset, true);
2922
- if (asciiEnd < 0xff00)
2923
- break;
2924
- else
2925
- asciiEnd = null;
2926
- } while((next = next.next));
2927
- if (asciiEnd == null)
2928
- asciiEnd = source.bytesEnd - refStart
2929
- source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
2930
- return source.srcString.slice(ref, end);
2931
- }
2932
- if (source.prevStringGet) {
2933
- source.prevStringGet.multiGetCount += 2;
2934
- } else {
2935
- source.prevStringGet = property;
2936
- property.multiGetCount--;
2937
- }*/
2938
- return readString(src, ref + refStart, end - ref);
2939
- //return src.toString('latin1', ref + refStart, end + refStart);
2940
- };
2941
- break;
2942
- case UTF8: case OBJECT_DATA:
2943
- if (lastRefProperty && !lastRefProperty.next)
2944
- lastRefProperty.next = property;
2945
- lastRefProperty = property;
2946
- get = function(source) {
2947
- let position = source.position;
2948
- let refStart = currentOffset + position;
2949
- let ref = getRef(source, position);
2950
- if (typeof ref !== 'number') return ref;
2951
- let src = source.bytes;
2952
- let end, next = property.next;
2953
- while(next) {
2954
- end = next.getRef(source, position);
2955
- if (typeof end === 'number')
2956
- break;
2957
- else
2958
- end = null;
2959
- next = next.next;
2960
- }
2961
- if (end == null)
2962
- end = source.bytesEnd - refStart;
2963
- if (type === UTF8) {
2964
- return src.toString('utf8', ref + refStart, end + refStart);
2965
- } else {
2966
- currentSource = source;
2967
- try {
2968
- return unpackr.unpack(src, { start: ref + refStart, end: end + refStart });
2969
- } finally {
2970
- currentSource = null;
2971
- }
2972
- }
2973
- };
2974
- break;
2975
- case NUMBER:
2976
- switch(size) {
2977
- case 4:
2978
- get = function (source) {
2979
- let src = source.bytes;
2980
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2981
- let position = source.position + property.offset;
2982
- let value = dataView.getInt32(position, true);
2983
- if (value < 0x20000000) {
2984
- if (value > -0x1f000000)
2985
- return value;
2986
- if (value > -0x20000000)
2987
- return toConstant(value & 0xff);
2988
- }
2989
- let fValue = dataView.getFloat32(position, true);
2990
- // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2991
- let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)];
2992
- return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
2993
- };
2994
- break;
2995
- case 8:
2996
- get = function (source) {
2997
- let src = source.bytes;
2998
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2999
- let value = dataView.getFloat64(source.position + property.offset, true);
3000
- if (isNaN(value)) {
3001
- let byte = src[source.position + property.offset];
3002
- if (byte >= 0xf6)
3003
- return toConstant(byte);
3004
- }
3005
- return value;
3006
- };
3007
- break;
3008
- case 1:
3009
- get = function (source) {
3010
- let src = source.bytes;
3011
- let value = src[source.position + property.offset];
3012
- return value < 0xf6 ? value : toConstant(value);
3013
- };
3014
- break;
3015
- }
3016
- break;
3017
- case DATE:
3018
- get = function (source) {
3019
- let src = source.bytes;
3020
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
3021
- return new Date(dataView.getFloat64(source.position + property.offset, true));
3022
- };
3023
- break;
3024
-
3025
- }
3026
- property.get = get;
3027
- }
3028
- // TODO: load the srcString for faster string decoding on toJSON
3029
- if (evalSupported) {
3030
- let objectLiteralProperties = [];
3031
- let args = [];
3032
- let i = 0;
3033
- let hasInheritedProperties;
3034
- for (let property of properties) { // assign in enumeration order
3035
- if (unpackr.alwaysLazyProperty && unpackr.alwaysLazyProperty(property.key)) {
3036
- // these properties are not eagerly evaluated and this can be used for creating properties
3037
- // that are not serialized as JSON
3038
- hasInheritedProperties = true;
3039
- continue;
3040
- }
3041
- Object.defineProperty(prototype, property.key, { get: withSource(property.get), enumerable: true });
3042
- let valueFunction = 'v' + i++;
3043
- args.push(valueFunction);
3044
- objectLiteralProperties.push('o[' + JSON.stringify(property.key) + ']=' + valueFunction + '(s)');
3045
- }
3046
- if (hasInheritedProperties) {
3047
- objectLiteralProperties.push('__proto__:this');
3048
- }
3049
- let toObject = (new Function(...args, 'var c=this;return function(s){var o=new c();' + objectLiteralProperties.join(';') + ';return o;}')).apply(fullConstruct, properties.map(prop => prop.get));
3050
- Object.defineProperty(prototype, 'toJSON', {
3051
- value(omitUnderscoredProperties) {
3052
- return toObject.call(this, this[sourceSymbol]);
3053
- }
3054
- });
3055
- } else {
3056
- Object.defineProperty(prototype, 'toJSON', {
3057
- value(omitUnderscoredProperties) {
3058
- // return an enumerable object with own properties to JSON stringify
3059
- let resolved = {};
3060
- for (let i = 0, l = properties.length; i < l; i++) {
3061
- // TODO: check alwaysLazyProperty
3062
- let key = properties[i].key;
3063
-
3064
- resolved[key] = this[key];
3065
- }
3066
- return resolved;
3067
- },
3068
- // not enumerable or anything
3069
- });
3070
- }
3071
- }
3072
- var instance = new construct();
3073
- instance[sourceSymbol] = {
3074
- bytes: src,
3075
- position,
3076
- srcString: '',
3077
- bytesEnd: srcEnd
3078
- };
3079
- return instance;
3080
- }
3081
- function toConstant(code) {
3082
- switch(code) {
3083
- case 0xf6: return null;
3084
- case 0xf7: return undefined;
3085
- case 0xf8: return false;
3086
- case 0xf9: return true;
3087
- }
3088
- throw new Error('Unknown constant');
3089
- }
3090
- function withSource(get) {
3091
- return function() {
3092
- return get(this[sourceSymbol]);
3093
- }
3094
- }
3095
-
3096
- function saveState() {
3097
- if (currentSource) {
3098
- currentSource.bytes = Uint8Array.prototype.slice.call(currentSource.bytes, currentSource.position, currentSource.bytesEnd);
3099
- currentSource.position = 0;
3100
- currentSource.bytesEnd = currentSource.bytes.length;
3101
- }
3102
- }
3103
- function prepareStructures(structures, packr) {
3104
- if (packr.typedStructs) {
3105
- let structMap = new Map();
3106
- structMap.set('named', structures);
3107
- structMap.set('typed', packr.typedStructs);
3108
- structures = structMap;
3109
- }
3110
- let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
3111
- structures.isCompatible = existing => {
3112
- let compatible = true;
3113
- if (existing instanceof Map) {
3114
- let named = existing.get('named') || [];
3115
- if (named.length !== (packr.lastNamedStructuresLength || 0))
3116
- compatible = false;
3117
- let typed = existing.get('typed') || [];
3118
- if (typed.length !== lastTypedStructuresLength)
3119
- compatible = false;
3120
- } else if (existing instanceof Array || Array.isArray(existing)) {
3121
- if (existing.length !== (packr.lastNamedStructuresLength || 0))
3122
- compatible = false;
3123
- }
3124
- if (!compatible)
3125
- packr._mergeStructures(existing);
3126
- return compatible;
3127
- };
3128
- packr.lastTypedStructuresLength = packr.typedStructs && packr.typedStructs.length;
3129
- return structures;
3130
- }
3131
-
3132
- setReadStruct(readStruct, onLoadedStructures, saveState);
3133
-
3134
- class PackrStream extends stream.Transform {
3135
- constructor(options) {
3136
- if (!options)
3137
- options = {};
3138
- options.writableObjectMode = true;
3139
- super(options);
3140
- options.sequential = true;
3141
- this.packr = options.packr || new Packr(options);
3142
- }
3143
- _transform(value, encoding, callback) {
3144
- this.push(this.packr.pack(value));
3145
- callback();
3146
- }
3147
- }
3148
-
3149
- class UnpackrStream extends stream.Transform {
3150
- constructor(options) {
3151
- if (!options)
3152
- options = {};
3153
- options.objectMode = true;
3154
- super(options);
3155
- options.structures = [];
3156
- this.unpackr = options.unpackr || new Unpackr(options);
3157
- }
3158
- _transform(chunk, encoding, callback) {
3159
- if (this.incompleteBuffer) {
3160
- chunk = Buffer.concat([this.incompleteBuffer, chunk]);
3161
- this.incompleteBuffer = null;
3162
- }
3163
- let values;
3164
- try {
3165
- values = this.unpackr.unpackMultiple(chunk);
3166
- } catch(error) {
3167
- if (error.incomplete) {
3168
- this.incompleteBuffer = chunk.slice(error.lastPosition);
3169
- values = error.values;
3170
- }
3171
- else
3172
- throw error
3173
- } finally {
3174
- for (let value of values || []) {
3175
- if (value === null)
3176
- value = this.getNullValue();
3177
- this.push(value);
3178
- }
3179
- }
3180
- if (callback) callback();
3181
- }
3182
- getNullValue() {
3183
- return Symbol.for(null)
3184
- }
3185
- }
3186
-
3187
- /**
3188
- * Given an Iterable first argument, returns an Iterable where each value is packed as a Buffer
3189
- * If the argument is only Async Iterable, the return value will be an Async Iterable.
3190
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
3191
- * @param {options} [options] - msgpackr pack options
3192
- * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
3193
- */
3194
- function packIter (objectIterator, options = {}) {
3195
- if (!objectIterator || typeof objectIterator !== 'object') {
3196
- throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
3197
- } else if (typeof objectIterator[Symbol.iterator] === 'function') {
3198
- return packIterSync(objectIterator, options)
3199
- } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
3200
- return packIterAsync(objectIterator, options)
3201
- } else {
3202
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
3203
- }
3204
- }
3205
-
3206
- function * packIterSync (objectIterator, options) {
3207
- const packr = new Packr(options);
3208
- for (const value of objectIterator) {
3209
- yield packr.pack(value);
3210
- }
3211
- }
3212
-
3213
- async function * packIterAsync (objectIterator, options) {
3214
- const packr = new Packr(options);
3215
- for await (const value of objectIterator) {
3216
- yield packr.pack(value);
3217
- }
3218
- }
3219
-
3220
- /**
3221
- * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
3222
- * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
3223
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
3224
- * @param {object} [options] - unpackr options
3225
- * @returns {IterableIterator|Promise.<AsyncIterableIterator}
3226
- */
3227
- function unpackIter (bufferIterator, options = {}) {
3228
- if (!bufferIterator || typeof bufferIterator !== 'object') {
3229
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
3230
- }
3231
-
3232
- const unpackr = new Unpackr(options);
3233
- let incomplete;
3234
- const parser = (chunk) => {
3235
- let yields;
3236
- // if there's incomplete data from previous chunk, concatinate and try again
3237
- if (incomplete) {
3238
- chunk = Buffer.concat([incomplete, chunk]);
3239
- incomplete = undefined;
3240
- }
3241
-
3242
- try {
3243
- yields = unpackr.unpackMultiple(chunk);
3244
- } catch (err) {
3245
- if (err.incomplete) {
3246
- incomplete = chunk.slice(err.lastPosition);
3247
- yields = err.values;
3248
- } else {
3249
- throw err
3250
- }
3251
- }
3252
- return yields
3253
- };
3254
-
3255
- if (typeof bufferIterator[Symbol.iterator] === 'function') {
3256
- return (function * iter () {
3257
- for (const value of bufferIterator) {
3258
- yield * parser(value);
3259
- }
3260
- })()
3261
- } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
3262
- return (async function * iter () {
3263
- for await (const value of bufferIterator) {
3264
- yield * parser(value);
3265
- }
3266
- })()
3267
- }
3268
- }
3269
- const decodeIter = unpackIter;
3270
- const encodeIter = packIter;
3271
-
3272
- const useRecords = false;
3273
- const mapsAsObjects = true;
3274
-
3275
- const nativeAccelerationDisabled = process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
3276
-
3277
- if (!nativeAccelerationDisabled) {
3278
- let extractor;
3279
- try {
3280
- if (typeof require == 'function')
3281
- extractor = require('msgpackr-extract');
3282
- else
3283
- extractor = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('node.cjs', document.baseURI).href)))('msgpackr-extract');
3284
- if (extractor)
3285
- setExtractor(extractor.extractStrings);
3286
- } catch (error) {
3287
- // native module is optional
3288
- }
3289
- }
3290
-
3291
- exports.ALWAYS = ALWAYS;
3292
- exports.C1 = C1;
3293
- exports.DECIMAL_FIT = DECIMAL_FIT;
3294
- exports.DECIMAL_ROUND = DECIMAL_ROUND;
3295
- exports.Decoder = Decoder;
3296
- exports.DecoderStream = UnpackrStream;
3297
- exports.Encoder = Encoder;
3298
- exports.EncoderStream = PackrStream;
3299
- exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
3300
- exports.NEVER = NEVER;
3301
- exports.Packr = Packr;
3302
- exports.PackrStream = PackrStream;
3303
- exports.RESERVE_START_SPACE = RESERVE_START_SPACE;
3304
- exports.RESET_BUFFER_MODE = RESET_BUFFER_MODE;
3305
- exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
3306
- exports.Unpackr = Unpackr;
3307
- exports.UnpackrStream = UnpackrStream;
3308
- exports.addExtension = addExtension;
3309
- exports.clearSource = clearSource;
3310
- exports.decode = decode;
3311
- exports.decodeIter = decodeIter;
3312
- exports.encode = encode;
3313
- exports.encodeIter = encodeIter;
3314
- exports.mapsAsObjects = mapsAsObjects;
3315
- exports.pack = pack;
3316
- exports.roundFloat32 = roundFloat32;
3317
- exports.unpack = unpack;
3318
- exports.unpackMultiple = unpackMultiple;
3319
- exports.useRecords = useRecords;
3320
- //# sourceMappingURL=node.cjs.map