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