msgpackr 1.7.0-alpha2 → 1.7.0-alpha3

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/struct.js CHANGED
@@ -1,174 +1,476 @@
1
- // first four bits
2
- // 0000 - unsigned int
3
- // 0010 - float32
4
- // 0011 - float32
5
- // 0100 - float32
6
- // 0101 - float32
7
- // 0110 - latin string reference
8
- // 0111 - plain reference
9
- // 1000 - structure reference
10
- // 1001 - random access structure reference
11
- // 1010 - float32
12
- // 1011 - float32
13
- // 1100 - float32
14
- // 1101 - float32
15
- // 1110 - constants and 3-byte strings
16
- // 1111 - negative int
17
1
 
18
- // first three bits
19
- // 000 - unsigned int
20
- // 001 - float32
21
- // 010 - float32
22
- // 011 - latin string reference
23
- // 100 - reference
24
- // 101 - float32
25
- // 110 - float32
26
- // 111 - constants and 3-byte strings
2
+ /*
27
3
 
28
- import { setWriteStructSlots, RECORD_SYMBOL } from './pack.js'
29
- import { setReadStruct, unpack, mult10 } from './unpack.js';
30
- const hasNonLatin = /[\u0080-\uFFFF]/;
31
- const float32Headers = [false, true, true, false, false, true, true, false]
32
- setWriteStructSlots(writeStruct);
33
- function writeStruct(object, target, position, structures, makeRoom, pack) {
34
- let transition = structures.transitions || false
35
- let newTransitions = 0
36
- let keyCount = 0;
37
- let start = position;
38
- position += 4;
39
- let queuedReferences = [];
40
- let uint32 = target.uint32 || (target.uint32 = new Uint32Array(target.buffer));
4
+ For "any-data":
5
+ 32-55 - record with record ids (-32)
6
+ 56 - 8-bit record ids
7
+ 57 - 16-bit record ids
8
+ 58 - 24-bit record ids
9
+ 59 - 32-bit record ids
10
+ 250-255 - followed by typed fixed width values
11
+ 64-250 msgpackr/cbor/paired data
12
+ arrays and strings within arrays are handled by paired encoding
13
+
14
+ Structure encoding:
15
+ (type - string (using paired encoding))+
16
+
17
+ Type encoding
18
+ encoding byte - fixed width byte - next reference+
19
+
20
+ Encoding byte:
21
+ first bit:
22
+ 0 - inline
23
+ 1 - reference
24
+ second bit:
25
+ 0 - data or number
26
+ 1 - string
27
+
28
+ remaining bits:
29
+ character encoding - ISO-8859-x
30
+
31
+
32
+ null (0xff)+ 0xf6
33
+ null (0xff)+ 0xf7
34
+
35
+ */
36
+
37
+
38
+ import {setWriteStructSlots, RECORD_SYMBOL, addExtension} from './pack.js'
39
+ import {setReadStruct, mult10, readString} from './unpack.js';
40
+ 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)
41
+ const NUMBER = 0;
42
+ const UTF8 = 2;
43
+ const OBJECT_DATA = 1;
44
+ const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
45
+ const float32Headers = [false, true, true, false, false, true, true, false];
46
+ let updatedPosition;
47
+ const hasNodeBuffer = typeof Buffer !== 'undefined'
48
+ let textEncoder
49
+ try {
50
+ textEncoder = new TextEncoder()
51
+ } catch (error) {}
52
+ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
53
+ return target.utf8Write(string, position, 0xffffffff)
54
+ } : (textEncoder && textEncoder.encodeInto) ?
55
+ function(target, string, position) {
56
+ return textEncoder.encodeInto(string, target.subarray(position)).written
57
+ } : false
58
+
59
+ const TYPE = Symbol('type');
60
+ const PARENT = Symbol('parent');
61
+ setWriteStructSlots(writeStruct, prepareStructures);
62
+ function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
63
+ let typedStructs = packr.typedStructs || (packr.typedStructs = []);
64
+ // note that we rely on pack.js to load stored structures before we get to this point
65
+ packr.lastTypedStructuresLength = typedStructs.length;
41
66
  let targetView = target.dataView;
42
- let encoded;
43
- let stringData = '';
67
+ let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
44
68
  let safeEnd = target.length - 10;
69
+ let start = position;
70
+ if (position > safeEnd) {
71
+ let lastStart = start;
72
+ target = makeRoom(position);
73
+ targetView = target.dataView;
74
+ position -= lastStart;
75
+ refsStartPosition -= lastStart;
76
+ start = 0;
77
+ safeEnd = target.length - 10
78
+ }
79
+
80
+ let refOffset, refPosition = refsStartPosition;
81
+
82
+ let transition = typedStructs.transitions || (typedStructs.transitions = Object.create(null));
83
+ let nextId = typedStructs.nextId || typedStructs.length;
84
+ let headerSize =
85
+ nextId < 0xf ? 1 :
86
+ nextId < 0xf0 ? 2 :
87
+ nextId < 0xf000 ? 3 :
88
+ nextId < 0xf00000 ? 4 : 0;
89
+ if (headerSize === 0)
90
+ return 0;
91
+ position += headerSize;
92
+ let queuedReferences = [];
93
+ let usedLatin0;
94
+ let keyIndex = 0;
45
95
  for (let key in object) {
46
- let nextTransition = transition[key]
96
+ let value = object[key];
97
+ let nextTransition = transition[key];
47
98
  if (!nextTransition) {
48
- return 0; // bail
49
- //nextTransition = transition[key] = Object.create(null)
50
- //newTransitions++
99
+ transition[key] = nextTransition = {
100
+ key,
101
+ parent: transition,
102
+ enumerationOffset: 0,
103
+ ascii0: null,
104
+ ascii8: null,
105
+ num8: null,
106
+ string16: null,
107
+ object16: null,
108
+ num32: null,
109
+ float64: null
110
+ };
51
111
  }
52
112
  if (position > safeEnd) {
53
- let newPosition = position - start;
54
- target = makeRoom(position)
55
- position = newPosition;
56
- start = 0
113
+ let lastStart = start;
114
+ target = makeRoom(position);
115
+ targetView = target.dataView;
116
+ position -= lastStart;
117
+ refsStartPosition -= lastStart;
118
+ refPosition -= lastStart;
119
+ start = 0;
57
120
  safeEnd = target.length - 10
58
121
  }
59
- transition = nextTransition
60
- let value = object[key];
61
122
  switch (typeof value) {
62
123
  case 'number':
63
- if (value >>> 0 === value && value < 0x20000000) {
64
- encoded = value;
124
+ let number = value;
125
+ if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
126
+ if (number < 0xf6 && number >= 0 && (nextTransition.num8 || number < 0x20 && !nextTransition.num32)) {
127
+ transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
128
+ target[position++] = number;
129
+ } else {
130
+ transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
131
+ targetView.setUint32(position, number, true);
132
+ position += 4;
133
+ }
65
134
  break;
66
- } else if (value < 0x100000000 && value >= -0x80000000) {
67
- targetView.setFloat32(position, value, true)
135
+ } else if (number < 0x100000000 && number >= -0x80000000) {
136
+ targetView.setFloat32(position, number, true);
68
137
  if (float32Headers[target[position + 3] >>> 5]) {
69
138
  let xShifted
70
139
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
71
- if (((xShifted = value * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
140
+ if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
141
+ transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
72
142
  position += 4;
73
- continue;
143
+ break;
74
144
  }
75
145
  }
76
146
  }
77
- // fall back to msgpack encoding
78
- queuedReferences.push(value, position - start);
79
- position += 4;
80
- continue;
147
+ transition = nextTransition.num64 || createTypeTransition(nextTransition, NUMBER, 8);
148
+ targetView.setFloat64(position, number, true);
149
+ position += 8;
150
+ break;
81
151
  case 'string':
82
- if (hasNonLatin.test(value)) {
83
- queuedReferences.push(value, position - start);
84
- position += 4;
85
- continue;
152
+ let strLength = value.length;
153
+ refOffset = refPosition - refsStartPosition;
154
+ if ((strLength << 2) + position > safeEnd) {
155
+ let lastStart = start;
156
+ target = makeRoom(refPosition);
157
+ targetView = target.dataView;
158
+ position -= lastStart;
159
+ refsStartPosition -= lastStart;
160
+ refPosition -= lastStart;
161
+ start = 0;
162
+ safeEnd = target.length - 10
163
+ }
164
+ if (strLength > ((0xff00 + refOffset) >> 2)) {
165
+ queuedReferences.push(key, value, position - start);
166
+ break;
86
167
  }
87
- if (value.length < 4) { // we can inline really small strings
88
- encoded = 0xf8000000 + (value.length << 24) + (value.charCodeAt(0) << 16) + (value.charCodeAt(1) << 8) + (value.charCodeAt(2) || 0)
89
- // TODO: determining remaining and make max value be a ratio of that (probably 1/256th)
90
- } else if (value.length < 256 && stringData.length < 61440) {
91
- // bundle these strings
92
- encoded = 0x60000000 | (value.length << 16) | stringData.length;
93
- stringData += value;
94
- } else { // else queue it
95
- queuedReferences.push(value, position - start);
96
- position += 4;
97
- continue;
168
+ let isNotAscii
169
+ let strStart = refPosition;
170
+ if (strLength < 0x40) {
171
+ let i, c1, c2;
172
+ for (i = 0; i < strLength; i++) {
173
+ c1 = value.charCodeAt(i)
174
+ if (c1 < 0x80) {
175
+ target[refPosition++] = c1
176
+ } else if (c1 < 0x800) {
177
+ isNotAscii = true;
178
+ target[refPosition++] = c1 >> 6 | 0xc0
179
+ target[refPosition++] = c1 & 0x3f | 0x80
180
+ } else if (
181
+ (c1 & 0xfc00) === 0xd800 &&
182
+ ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
183
+ ) {
184
+ isNotAscii = true;
185
+ c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff)
186
+ i++
187
+ target[refPosition++] = c1 >> 18 | 0xf0
188
+ target[refPosition++] = c1 >> 12 & 0x3f | 0x80
189
+ target[refPosition++] = c1 >> 6 & 0x3f | 0x80
190
+ target[refPosition++] = c1 & 0x3f | 0x80
191
+ } else {
192
+ isNotAscii = true;
193
+ target[refPosition++] = c1 >> 12 | 0xe0
194
+ target[refPosition++] = c1 >> 6 & 0x3f | 0x80
195
+ target[refPosition++] = c1 & 0x3f | 0x80
196
+ }
197
+ }
198
+ } else {
199
+ refPosition += encodeUtf8(target, value, refPosition);
200
+ isNotAscii = refPosition - strStart > strLength;
201
+ }
202
+ if (refOffset < 0x100) {
203
+ if (isNotAscii)
204
+ transition = nextTransition.string8 || createTypeTransition(nextTransition, UTF8, 1);
205
+ else
206
+ transition = nextTransition.ascii8 || createTypeTransition(nextTransition, ASCII, 1);
207
+ target[position++] = refOffset;
208
+ } else {
209
+ if (isNotAscii)
210
+ transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
211
+ else
212
+ transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
213
+ targetView.setUint16(position, refOffset, true);
214
+ position += 2;
98
215
  }
99
216
  break;
100
217
  case 'object':
101
218
  if (value) {
102
- queuedReferences.push(value, position - start);
103
- position += 4;
104
- continue;
219
+ //transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
220
+ queuedReferences.push(key, value, keyIndex);
221
+ break;
105
222
  } else { // null
106
- encoded = 0xe0000000;
223
+ nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
224
+ if (nextTransition) {
225
+ transition = nextTransition;
226
+ position = updatedPosition;
227
+ } else queuedReferences.push(key, value, keyIndex);
107
228
  }
108
229
  break;
109
230
  case 'boolean':
110
- encoded = value ? 0xe3000000 : 0xe2000000;
231
+ transition = nextTransition.num8 || nextTransition.ascii8 || createTypeTransition(nextTransition, NUMBER, 1);
232
+ target[position++] = value ? 0xf9 : 0xf8; // match CBOR with these
111
233
  break;
112
234
  case 'undefined':
113
- encoded = 0xe1000000;
235
+ nextTransition = anyType(nextTransition, position, targetView, -9); // match CBOR with this
236
+ if (nextTransition) {
237
+ transition = nextTransition;
238
+ position = updatedPosition;
239
+ } else queuedReferences.push(key, value, keyIndex);
114
240
  break;
115
241
  }
116
- targetView.setUint32(position, encoded, true);
117
- position += 4;
242
+ keyIndex++;
118
243
  }
119
- let recordId = transition[RECORD_SYMBOL]
120
- if (!(recordId < 1024)) {
121
- // for now just punt and go back to writeObject
122
- return 0;
123
- // newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions, true)
124
- }
125
- let stringLength = stringData.length;
126
- if (stringData) {
127
- if (position + stringLength > safeEnd) {
128
- target = makeRoom(position + stringLength);
129
- }
130
- position += target.latin1Write(stringData, position, 0xffffffff);
131
- }
132
- target[start] = recordId >> 8;
133
- target[start + 1] = recordId & 0xff;
134
- target[start + 2] = stringLength >> 8;
135
- target[start + 3] = stringLength & 0xff;
136
- let queued32BitReferences;
244
+
137
245
  for (let i = 0, l = queuedReferences.length; i < l;) {
246
+ let key = queuedReferences[i++];
138
247
  let value = queuedReferences[i++];
139
- let slotOffset = queuedReferences[i++] + start;
140
- let offset = position - slotOffset;
141
- if (offset < 0x1f000000) {
142
- targetView.setUint32(slotOffset, 0x80000000 | (offset), true);
248
+ let propertyIndex = queuedReferences[i++];
249
+ let nextTransition = transition[key];
250
+ if (!nextTransition) {
251
+ transition[key] = nextTransition = {
252
+ key,
253
+ parent: transition,
254
+ enumerationOffset: propertyIndex - keyIndex,
255
+ ascii0: null,
256
+ ascii8: null,
257
+ num8: null,
258
+ string16: null,
259
+ object16: null,
260
+ num32: null,
261
+ float64: null
262
+ };
263
+ }
264
+ let newPosition;
265
+ if (value) {
266
+ /*if (typeof value === 'string') { // TODO: we could re-enable long strings
267
+ if (position + value.length * 3 > safeEnd) {
268
+ target = makeRoom(position + value.length * 3);
269
+ position -= start;
270
+ targetView = target.dataView;
271
+ start = 0;
272
+ }
273
+ newPosition = position + target.utf8Write(value, position, 0xffffffff);
274
+ } else { */
275
+ let size;
276
+ refOffset = refPosition - refsStartPosition;
277
+ if (refOffset < 0xff00) {
278
+ transition = nextTransition.object16;
279
+ if (transition)
280
+ size = 2;
281
+ else if ((transition = nextTransition.object32))
282
+ size = 4;
283
+ else {
284
+ transition = createTypeTransition(nextTransition, OBJECT_DATA, 2);
285
+ size = 2;
286
+ }
287
+ } else {
288
+ transition = nextTransition.object32 || createTypeTransition(nextTransition, OBJECT_DATA, 4);
289
+ size = 4;
290
+ }
291
+ newPosition = pack(value, refPosition);
292
+ //}
293
+ if (typeof newPosition === 'object') {
294
+ // re-allocated
295
+ refPosition = newPosition.position;
296
+ targetView = newPosition.targetView;
297
+ target = newPosition.target;
298
+ refsStartPosition -= start;
299
+ position -= start;
300
+ start = 0;
301
+ } else
302
+ refPosition = newPosition;
303
+ if (size === 2) {
304
+ targetView.setUint16(position, refOffset, true);
305
+ position += 2;
306
+ } else {
307
+ targetView.setUint32(position, refOffset, true);
308
+ position += 4;
309
+ }
143
310
  } else {
144
- if (!queued32BitReferences)
145
- queued32BitReferences = [];
146
- queued32BitReferences.push({slotOffset, offset: position - start});
311
+ transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
312
+ targetView.setInt16(position, value === null ? -10 : -9, true);
313
+ position += 2;
147
314
  }
148
- let newPosition = pack(value, position);
149
- if (typeof newPosition === 'object') {
150
- // re-allocated
151
- position = newPosition.position;
152
- targetView = newPosition.targetView;
153
- start = 0;
154
- } else
155
- position = newPosition;
315
+ keyIndex++;
156
316
  }
157
- if (queued32BitReferences) {
158
- // TODO: makeRoom
159
- for (let i = 0, l = queued32BitReferences.length; i < l; i++) {
160
- let ref = queued32BitReferences[i];
161
- targetView.setUint32(ref.slotOffset, 0xa0000000 - ((l - i) << 2), true);
162
- targetView.setUint32(position, ref.offset, true);
163
- position += 4;
317
+
318
+
319
+ let recordId = transition[RECORD_SYMBOL];
320
+ if (recordId == null) {
321
+ recordId = packr.typedStructs.length;
322
+ let structure = [];
323
+ let nextTransition = transition;
324
+ let key, type;
325
+ while ((type = nextTransition.__type) !== undefined) {
326
+ let size = nextTransition.__size;
327
+ nextTransition = nextTransition.__parent;
328
+ key = nextTransition.key;
329
+ let property = [type, size, key];
330
+ if (nextTransition.enumerationOffset)
331
+ property.push(nextTransition.enumerationOffset);
332
+ structure.push(property);
333
+ nextTransition = nextTransition.parent;
164
334
  }
335
+ structure.reverse();
336
+ transition[RECORD_SYMBOL] = recordId;
337
+ packr.typedStructs[recordId] = structure;
338
+ pack(null, 0, true); // special call to notify that structures have been updated
339
+ }
340
+
341
+
342
+ switch (headerSize) {
343
+ case 1:
344
+ if (recordId >= 0x10) return 0;
345
+ target[start] = recordId + 0x20;
346
+ break;
347
+ case 2:
348
+ if (recordId >= 0x100) return 0;
349
+ target[start] = 0x38;
350
+ target[start + 1] = recordId;
351
+ break;
352
+ case 3:
353
+ if (recordId >= 0x10000) return 0;
354
+ target[start] = 0x39;
355
+ target.setUint16(start + 1, recordId, true);
356
+ break;
357
+ case 4:
358
+ if (recordId >= 0x1000000) return 0;
359
+ target.setUint32(start, (recordId << 8) + 0x3a, true);
360
+ break;
165
361
  }
166
362
 
167
- return position;
363
+ if (position < refsStartPosition) {
364
+ if (refsStartPosition === refPosition)
365
+ return position; // no refs
366
+ // adjust positioning
367
+ target.copyWithin(position, refsStartPosition, refPosition);
368
+ refPosition += position - refsStartPosition;
369
+ typedStructs.lastStringStart = position - start;
370
+ } else if (position > refsStartPosition) {
371
+ if (refsStartPosition === refPosition)
372
+ return position; // no refs
373
+ typedStructs.lastStringStart = position - start;
374
+ return writeStruct(object, target, start, structures, makeRoom, pack, packr);
375
+ }
376
+ return refPosition;
377
+ }
378
+ function anyType(transition, position, targetView, value) {
379
+ let nextTransition;
380
+ if ((nextTransition = transition.ascii8 || transition.num8)) {
381
+ targetView.setInt8(position, value, true);
382
+ updatedPosition = position + 1;
383
+ return nextTransition;
384
+ }
385
+ if ((nextTransition = transition.string16 || transition.object16)) {
386
+ targetView.setInt16(position, value, true);
387
+ updatedPosition = position + 2;
388
+ return nextTransition;
389
+ }
390
+ if (nextTransition = transition.num32) {
391
+ targetView.setUint32(position, 0xe0000100 + value, true);
392
+ updatedPosition = position + 4;
393
+ return nextTransition;
394
+ }
395
+ // transition.float64
396
+ if (nextTransition = transition.num64) {
397
+ targetView.setFloat64(position, NaN, true);
398
+ targetView.setInt8(position, value);
399
+ updatedPosition = position + 8;
400
+ return nextTransition;
401
+ }
402
+ updatedPosition = position;
403
+ // TODO: can we do an "any" type where we defer the decision?
404
+ return;
405
+ }
406
+ function createTypeTransition(transition, type, size) {
407
+ let typeName = TYPE_NAMES[type] + (size << 3);
408
+ let newTransition = transition[typeName] || (transition[typeName] = Object.create(null));
409
+ newTransition.__type = type;
410
+ newTransition.__size = size;
411
+ newTransition.__parent = transition;
412
+ return newTransition;
413
+ }
414
+ function onLoadedStructures(sharedData) {
415
+ if (!(sharedData instanceof Map))
416
+ return sharedData;
417
+ let typed = sharedData.get('typed') || [];
418
+ let named = sharedData.get('named');
419
+ let transitions = Object.create(null);
420
+ for (let i = 0, l = typed.length; i < l; i++) {
421
+ let structure = typed[i];
422
+ let transition = transitions;
423
+ for (let [type, size, key] of structure) {
424
+ let nextTransition = transition[key];
425
+ if (!nextTransition) {
426
+ transition[key] = nextTransition = {
427
+ key,
428
+ parent: transition,
429
+ enumerationOffset: 0,
430
+ ascii0: null,
431
+ ascii8: null,
432
+ num8: null,
433
+ string16: null,
434
+ object16: null,
435
+ num32: null,
436
+ float64: null
437
+ };
438
+ }
439
+ transition = createTypeTransition(nextTransition, type, size);
440
+ }
441
+ transition[RECORD_SYMBOL] = i;
442
+ }
443
+ typed.transitions = transitions;
444
+ this.typedStructs = typed;
445
+ return named;
168
446
  }
169
447
  var sourceSymbol = Symbol('source')
170
- function readStruct(src, position, srcEnd, structure, unpackr) {
171
- var stringLength = (src[position++] << 8) | src[position++];
448
+ function readStruct(src, position, srcEnd, unpackr) {
449
+ // var stringLength = (src[position++] << 8) | src[position++];
450
+ let recordId = src[position++] - 0x20;
451
+ if (recordId >= 24) {
452
+ switch(recordId) {
453
+ case 24: recordId = src[position++]; break;
454
+ // little endian:
455
+ case 25: recordId = src[position++] + (src[position++] << 8); break;
456
+ case 26: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16); break;
457
+ case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
458
+ }
459
+ }
460
+ let structure = unpackr.typedStructs?.[recordId];
461
+ if (!structure) {
462
+ // copy src buffer because getStructures will override it
463
+ src = Uint8Array.prototype.slice.call(src, position, srcEnd);
464
+ srcEnd -= position;
465
+ position = 0;
466
+ unpackr._mergeStructures(unpackr.getStructures());
467
+ if (!unpackr.typedStructs)
468
+ throw new Error('Could not find any shared typed structures');
469
+ unpackr.lastTypedStructuresLength = unpackr.typedStructs.length;
470
+ structure = unpackr.typedStructs[recordId];
471
+ if (!structure)
472
+ throw new Error('Could not find typed structure ' + recordId);
473
+ }
172
474
  var construct = structure.construct;
173
475
  if (!construct) {
174
476
  construct = structure.construct = function() {
@@ -186,77 +488,224 @@ function readStruct(src, position, srcEnd, structure, unpackr) {
186
488
  },
187
489
  // not enumerable or anything
188
490
  });
491
+ let currentOffset = 0;
492
+ let lastRefProperty;
493
+ let properties = [];
189
494
  for (let i = 0, l = structure.length; i < l; i++) {
190
- let key = structure[i];
191
- Object.defineProperty(prototype, key, {
192
- get() {
193
- let source = this[sourceSymbol];
194
- let src = source.src;
195
- //let uint32 = src.uint32 || (src.uint32 = new Uint32Array(src.buffer, src.byteOffset, src.byteLength));
196
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
197
- let position = source.position + (i << 2);
198
- let value = dataView.getUint32(position, true);
199
- let start;
200
- switch (value >>> 29) {
201
- case 0:
202
- return value;
203
- case 3:
204
- if (value & 0x10000000) {
205
- start = (value & 0xffff) + position;
206
- return src.toString('utf8', start, start + ((value >> 16) & 0x7ff));
207
- } else {
208
- if (!source.srcString) {
209
- start = source.position + (l << 2);
210
- source.srcString = src.toString('latin1', start, start + source.stringLength);
211
- }
212
- start = value & 0xffff;
213
- return source.srcString.slice(start, start + ((value >> 16) & 0x7ff));
214
- }
215
- case 4:
216
- start = (0x1fffffff & value) + position;
217
- let end = source.srcEnd;
218
- for (let next = i + 1; next < l; next++) {
219
- position = source.position + (next << 2);
220
- let nextValue = dataView.getUint32(position, true);;
221
- if ((nextValue & 0xe0000000) == -0x80000000) {
222
- end = (0x1fffffff & nextValue) + position;
495
+ let definition = structure[i];
496
+ let [ type, size, key, enumerationOffset ] = definition;
497
+ let property = {
498
+ key,
499
+ offset: currentOffset,
500
+ }
501
+ if (enumerationOffset)
502
+ properties.splice(i + enumerationOffset, 0, property);
503
+ else
504
+ properties.push(property);
505
+ let getRef;
506
+ switch(size) { // TODO: Move into a separate function
507
+ case 0: getRef = () => 0; break;
508
+ case 1:
509
+ getRef = (source, position) => {
510
+ let ref = source.src[position + property.offset];
511
+ return ref >= 0xf6 ? toConstant(ref) : ref;
512
+ };
513
+ break;
514
+ case 2:
515
+ getRef = (source, position) => {
516
+ let src = source.src;
517
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
518
+ let ref = dataView.getUint16(position + property.offset, true);
519
+ return ref >= 0xff00 ? toConstant(ref & 0xff) : ref;
520
+ };
521
+ break;
522
+ case 4:
523
+ getRef = (source, position) => {
524
+ let src = source.src;
525
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
526
+ let ref = dataView.getUint32(position + property.offset, true);
527
+ return ref >= 0xffffff00 ? toConstant(ref & 0xff) : ref;
528
+ };
529
+ break;
530
+ }
531
+ property.getRef = getRef;
532
+ currentOffset += size;
533
+ let get;
534
+ switch(type) {
535
+ case ASCII:
536
+ if (lastRefProperty && !lastRefProperty.next)
537
+ lastRefProperty.next = property;
538
+ lastRefProperty = property;
539
+ property.multiGetCount = 0;
540
+ get = function() {
541
+ let source = this[sourceSymbol];
542
+ let src = source.src;
543
+ let position = source.position;
544
+ let refStart = currentOffset + position;
545
+ let ref = getRef(source, position);
546
+ if (typeof ref !== 'number') return ref;
547
+
548
+ let end, next = property.next;
549
+ while(next) {
550
+ end = next.getRef(source, position);
551
+ if (typeof end === 'number')
552
+ break;
553
+ else
554
+ end = null;
555
+ next = next.next;
556
+ }
557
+ if (end == null)
558
+ end = srcEnd - refStart;
559
+ if (source.srcString) {
560
+ return source.srcString.slice(ref, end);
561
+ }
562
+ /*if (property.multiGetCount > 0) {
563
+ let asciiEnd;
564
+ next = firstRefProperty;
565
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
566
+ do {
567
+ asciiEnd = dataView.getUint16(source.position + next.offset, true);
568
+ if (asciiEnd < 0xff00)
223
569
  break;
570
+ else
571
+ asciiEnd = null;
572
+ } while((next = next.next));
573
+ if (asciiEnd == null)
574
+ asciiEnd = srcEnd - refStart
575
+ source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
576
+ return source.srcString.slice(ref, end);
577
+ }
578
+ if (source.prevStringGet) {
579
+ source.prevStringGet.multiGetCount += 2;
580
+ } else {
581
+ source.prevStringGet = property;
582
+ property.multiGetCount--;
583
+ }*/
584
+ return readString(src, ref + refStart, end - ref);
585
+ //return src.toString('latin1', ref + refStart, end + refStart);
586
+ };
587
+ break;
588
+ case UTF8: case OBJECT_DATA:
589
+ if (lastRefProperty && !lastRefProperty.next)
590
+ lastRefProperty.next = property;
591
+ lastRefProperty = property;
592
+ get = function() {
593
+ let source = this[sourceSymbol];
594
+ let position = source.position;
595
+ let refStart = currentOffset + position;
596
+ let ref = getRef(source, position);
597
+ if (typeof ref !== 'number') return ref;
598
+ let src = source.src;
599
+ let end, next = property.next;
600
+ while(next) {
601
+ end = next.getRef(source, position);
602
+ if (typeof end === 'number')
603
+ break;
604
+ else
605
+ end = null;
606
+ next = next.next;
607
+ }
608
+ if (end == null)
609
+ end = srcEnd - refStart;
610
+ if (type === UTF8) {
611
+ return src.toString('utf8', ref + refStart, end + refStart);
612
+ } else {
613
+ return unpackr.unpack(src, { start: ref + refStart, end: end + refStart }); // could reuse this object
614
+ }
615
+ };
616
+ break;
617
+ case NUMBER:
618
+ switch(size) {
619
+ case 4:
620
+ get = function () {
621
+ let source = this[sourceSymbol];
622
+ let src = source.src;
623
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
624
+ let position = source.position + property.offset;
625
+ let value = dataView.getInt32(position, true)
626
+ if (value < 0x20000000) {
627
+ if (value > -0x1f000000)
628
+ return value;
629
+ if (value > -0x20000000)
630
+ return toConstant(value & 0xff);
224
631
  }
225
- }
226
- return unpackr.unpack(src.slice(start, end));
227
- case 1: case 2: case 5: case 6:
228
- let fValue = dataView.getFloat32(position, true);
229
- // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
230
- let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)]
231
- return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
232
- case 7:
233
- switch((value >> 24) & 0x1f) {
234
- case 0: return null;
235
- case 1: return undefined;
236
- case 2: return false;
237
- case 3: return true;
238
- case 8: return dataView.getFloat64(position + (value & 0x3ffffff), true);
239
- case 0x18: return '';
240
- case 0x19: return String.fromCharCode((value >> 16) & 0xff);
241
- case 0x1a: return String.fromCharCode((value >> 16) & 0xff, (value >> 8) & 0xff);
242
- case 0x1b: return String.fromCharCode((value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff);
243
- default: throw new Error('Unknown constant');
244
- }
632
+ let fValue = dataView.getFloat32(position, true);
633
+ // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
634
+ let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)]
635
+ return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
636
+ };
637
+ break;
638
+ case 8:
639
+ get = function () {
640
+ let source = this[sourceSymbol];
641
+ let src = source.src;
642
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
643
+ let value = dataView.getFloat64(source.position + property.offset, true);
644
+ if (isNaN(value)) {
645
+ let byte = src[source.position + property.offset];
646
+ if (byte >= 0xf6)
647
+ return toConstant(byte);
648
+ }
649
+ return value;
650
+ };
651
+ break;
652
+ case 1:
653
+ get = function () {
654
+ let source = this[sourceSymbol];
655
+ let src = source.src;
656
+ let value = src[source.position + property.offset];
657
+ return value < 0xf6 ? value : toConstant(value);
658
+ };
659
+ break;
245
660
  }
246
- },
247
- enumerable: true,
248
- });
661
+ }
662
+ property.get = get;
249
663
  }
664
+ for (let property of properties) // assign in enumeration order
665
+ Object.defineProperty(prototype, property.key, { get: property.get, enumerable: true });
250
666
  }
251
667
  var instance = new construct();
252
668
  instance[sourceSymbol] = {
253
669
  src,
254
- uint32: src.uint32,
255
670
  position,
256
671
  srcString: '',
257
- srcEnd,
258
- stringLength
672
+ srcEnd
259
673
  }
260
674
  return instance;
261
675
  }
262
- setReadStruct(readStruct)
676
+ function toConstant(code) {
677
+ switch(code) {
678
+ case 0xf6: return null;
679
+ case 0xf7: return undefined;
680
+ case 0xf8: return false;
681
+ case 0xf9: return true;
682
+ }
683
+ throw new Error('Unknown constant');
684
+ }
685
+ function prepareStructures(structures, packr) {
686
+ if (!packr.typedStructs)
687
+ return structures;
688
+ let structMap = new Map();
689
+ structMap.set('named', structures);
690
+ structMap.set('typed', packr.typedStructs);
691
+ let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
692
+ structMap.isCompatible = existing => {
693
+ if (existing instanceof Map) {
694
+ let named = existing.get('named') || [];
695
+ if (named.length !== (packr.lastNamedStructuresLength || 0))
696
+ return false;
697
+ let typed = existing.get('typed') || [];
698
+ if (typed.length !== lastTypedStructuresLength)
699
+ return false;
700
+ } else if (existing instanceof Array) {
701
+ if (existing.length !== (packr.lastNamedStructuresLength || 0))
702
+ return false;
703
+ }
704
+ return true;
705
+ };
706
+ packr.lastTypedStructuresLength = packr.typedStructs?.length;
707
+ return structMap;
708
+ }
709
+
710
+ setReadStruct(readStruct, onLoadedStructures);
711
+