webpack 5.10.0 → 5.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (40) hide show
  1. package/bin/webpack.js +0 -0
  2. package/lib/Compilation.js +5 -21
  3. package/lib/Compiler.js +58 -13
  4. package/lib/FlagAllModulesAsUsedPlugin.js +4 -0
  5. package/lib/MainTemplate.js +2 -2
  6. package/lib/Module.js +1 -1
  7. package/lib/ModuleInfoHeaderPlugin.js +1 -1
  8. package/lib/NormalModule.js +4 -2
  9. package/lib/RuntimeGlobals.js +11 -1
  10. package/lib/RuntimeModule.js +20 -0
  11. package/lib/RuntimeTemplate.js +4 -0
  12. package/lib/cache/PackFileCacheStrategy.js +4 -2
  13. package/lib/hmr/HotModuleReplacementRuntimeModule.js +1 -1
  14. package/lib/index.js +5 -0
  15. package/lib/javascript/JavascriptModulesPlugin.js +82 -26
  16. package/lib/node/NodeTargetPlugin.js +4 -0
  17. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +1 -1
  18. package/lib/node/RequireChunkLoadingRuntimeModule.js +1 -1
  19. package/lib/optimize/SideEffectsFlagPlugin.js +9 -13
  20. package/lib/prefetch/ChunkPrefetchFunctionRuntimeModule.js +1 -1
  21. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +1 -1
  22. package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +1 -1
  23. package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +1 -1
  24. package/lib/rules/RuleSetCompiler.js +4 -2
  25. package/lib/runtime/AutoPublicPathRuntimeModule.js +1 -1
  26. package/lib/runtime/CompatRuntimeModule.js +1 -1
  27. package/lib/runtime/PublicPathRuntimeModule.js +1 -1
  28. package/lib/serialization/BinaryMiddleware.js +467 -136
  29. package/lib/serialization/FileMiddleware.js +227 -37
  30. package/lib/serialization/ObjectMiddleware.js +89 -5
  31. package/lib/sharing/ConsumeSharedRuntimeModule.js +1 -1
  32. package/lib/util/fs.js +4 -0
  33. package/lib/util/source.js +61 -0
  34. package/lib/validateSchema.js +84 -73
  35. package/lib/wasm-async/AsyncWasmChunkLoadingRuntimeModule.js +1 -1
  36. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +1 -1
  37. package/lib/web/JsonpChunkLoadingRuntimeModule.js +54 -55
  38. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +1 -1
  39. package/package.json +8 -8
  40. package/types.d.ts +1170 -578
@@ -10,31 +10,36 @@ const SerializerMiddleware = require("./SerializerMiddleware");
10
10
  /** @typedef {import("./types").BufferSerializableType} BufferSerializableType */
11
11
  /** @typedef {import("./types").PrimitiveSerializableType} PrimitiveSerializableType */
12
12
 
13
+ /* eslint-disable no-loop-func */
14
+
13
15
  /*
14
16
  Format:
15
17
 
16
18
  File -> Section*
17
19
 
18
20
  Section -> NullsSection |
21
+ BooleansSection |
19
22
  F64NumbersSection |
20
23
  I32NumbersSection |
21
24
  I8NumbersSection |
22
25
  ShortStringSection |
23
26
  StringSection |
24
27
  BufferSection |
25
- BooleanSection |
26
28
  NopSection
27
29
 
28
30
 
29
31
 
30
- NullsSection -> NullsSectionHeaderByte
32
+ NullsSection ->
33
+ NullHeaderByte | Null2HeaderByte | Null3HeaderByte |
34
+ Nulls8HeaderByte 0xnn (n:count - 4) |
35
+ Nulls32HeaderByte n:ui32 (n:count - 260) |
36
+ BooleansSection -> TrueHeaderByte | FalseHeaderByte | BooleansSectionHeaderByte BooleansCountAndBitsByte
31
37
  F64NumbersSection -> F64NumbersSectionHeaderByte f64*
32
38
  I32NumbersSection -> I32NumbersSectionHeaderByte i32*
33
39
  I8NumbersSection -> I8NumbersSectionHeaderByte i8*
34
- ShortStringSection -> ShortStringSectionHeaderByte utf8-byte*
40
+ ShortStringSection -> ShortStringSectionHeaderByte ascii-byte*
35
41
  StringSection -> StringSectionHeaderByte i32:length utf8-byte*
36
42
  BufferSection -> BufferSectionHeaderByte i32:length byte*
37
- BooleanSection -> TrueHeaderByte | FalseHeaderByte
38
43
  NopSection --> NopSectionHeaderByte
39
44
 
40
45
  ShortStringSectionHeaderByte -> 0b1nnn_nnnn (n:length)
@@ -44,6 +49,13 @@ I32NumbersSectionHeaderByte -> 0b010n_nnnn (n:count - 1)
44
49
  I8NumbersSectionHeaderByte -> 0b011n_nnnn (n:count - 1)
45
50
 
46
51
  NullsSectionHeaderByte -> 0b0001_nnnn (n:count - 1)
52
+ BooleansCountAndBitsByte ->
53
+ 0b0000_1xxx (count = 3) |
54
+ 0b0001_xxxx (count = 4) |
55
+ 0b001x_xxxx (count = 5) |
56
+ 0b01xx_xxxx (count = 6) |
57
+ 0b1nnn_nnnn (n:count - 7, 7 <= count <= 133)
58
+ 0xff n:ui32 (n:count, 134 <= count < 2^32)
47
59
 
48
60
  StringSectionHeaderByte -> 0b0000_1110
49
61
  BufferSectionHeaderByte -> 0b0000_1111
@@ -58,20 +70,26 @@ RawNumber -> n (n <= 10)
58
70
  const LAZY_HEADER = 0x0b;
59
71
  const TRUE_HEADER = 0x0c;
60
72
  const FALSE_HEADER = 0x0d;
61
- const STRING_HEADER = 0x0e;
62
- const BUFFER_HEADER = 0x0f;
63
- const NULLS_HEADER = 0x10;
73
+ const BOOLEANS_HEADER = 0x0e;
74
+ const NULL_HEADER = 0x10;
75
+ const NULL2_HEADER = 0x11;
76
+ const NULL3_HEADER = 0x12;
77
+ const NULLS8_HEADER = 0x13;
78
+ const NULLS32_HEADER = 0x14;
79
+ const NULL_AND_I8_HEADER = 0x15;
80
+ const NULL_AND_I32_HEADER = 0x16;
81
+ const NULL_AND_TRUE_HEADER = 0x17;
82
+ const NULL_AND_FALSE_HEADER = 0x18;
83
+ const STRING_HEADER = 0x1e;
84
+ const BUFFER_HEADER = 0x1f;
64
85
  const I8_HEADER = 0x60;
65
86
  const I32_HEADER = 0x40;
66
87
  const F64_HEADER = 0x20;
67
88
  const SHORT_STRING_HEADER = 0x80;
68
89
 
69
- /** Uplift high-order bits */
70
- const NULLS_HEADER_MASK = 0xf0;
71
90
  /** Uplift high-order bits */
72
91
  const NUMBERS_HEADER_MASK = 0xe0;
73
92
  const NUMBERS_COUNT_MASK = 0x1f; // 0b0001_1111
74
- const NULLS_COUNT_MASK = 0x0f; // 0b0000_1111
75
93
  const SHORT_STRING_LENGTH_MASK = 0x7f; // 0b0111_1111
76
94
 
77
95
  const HEADER_SIZE = 1;
@@ -198,51 +216,73 @@ class BinaryMiddleware extends SerializerMiddleware {
198
216
  case "function": {
199
217
  if (!SerializerMiddleware.isLazy(thing))
200
218
  throw new Error("Unexpected function " + thing);
201
- /** @type {SerializedType[0]} */
202
- const serializedData = SerializerMiddleware.getLazySerializedValue(
219
+ /** @type {SerializedType | (() => SerializedType)} */
220
+ let serializedData = SerializerMiddleware.getLazySerializedValue(
203
221
  thing
204
222
  );
205
- if (serializedData !== undefined) {
206
- if (typeof serializedData === "function") {
207
- flush();
208
- buffers.push(serializedData);
223
+ if (serializedData === undefined) {
224
+ if (SerializerMiddleware.isLazy(thing, this)) {
225
+ const data = this._serialize(thing(), context);
226
+ SerializerMiddleware.setLazySerializedValue(thing, data);
227
+ serializedData = data;
209
228
  } else {
210
- serializeData(serializedData);
211
- allocate(5);
212
- writeU8(LAZY_HEADER);
213
- writeU32(serializedData.length);
229
+ serializedData = SerializerMiddleware.serializeLazy(
230
+ thing,
231
+ data => this._serialize(data, context)
232
+ );
214
233
  }
215
- } else if (SerializerMiddleware.isLazy(thing, this)) {
216
- /** @type {SerializedType} */
217
- const data = BinaryMiddleware.optimizeSerializedData(
218
- this._serialize(thing(), context)
219
- );
220
- SerializerMiddleware.setLazySerializedValue(thing, data);
221
- serializeData(data);
222
- allocate(5);
223
- writeU8(LAZY_HEADER);
224
- writeU32(data.length);
225
- } else {
234
+ }
235
+ if (typeof serializedData === "function") {
226
236
  flush();
227
- buffers.push(
228
- SerializerMiddleware.serializeLazy(thing, data =>
229
- this._serialize(data, context)
230
- )
231
- );
237
+ buffers.push(serializedData);
238
+ } else {
239
+ const lengths = [];
240
+ for (const item of serializedData) {
241
+ let last;
242
+ if (typeof item === "function") {
243
+ lengths.push(0);
244
+ } else if (item.length === 0) {
245
+ // ignore
246
+ } else if (
247
+ lengths.length > 0 &&
248
+ (last = lengths[lengths.length - 1]) !== 0
249
+ ) {
250
+ const remaining = 0xffffffff - last;
251
+ if (remaining >= item.length) {
252
+ lengths[lengths.length - 1] += item.length;
253
+ } else {
254
+ lengths.push(item.length - remaining);
255
+ lengths[lengths.length - 2] = 0xffffffff;
256
+ }
257
+ } else {
258
+ lengths.push(item.length);
259
+ }
260
+ }
261
+ allocate(5 + lengths.length * 4);
262
+ writeU8(LAZY_HEADER);
263
+ writeU32(lengths.length);
264
+ for (const l of lengths) {
265
+ writeU32(l);
266
+ }
267
+ for (const item of serializedData) {
268
+ flush();
269
+ buffers.push(item);
270
+ }
232
271
  }
233
272
  break;
234
273
  }
235
274
  case "string": {
236
275
  const len = Buffer.byteLength(thing);
237
- if (len >= 128) {
276
+ if (len >= 128 || len !== thing.length) {
238
277
  allocate(len + HEADER_SIZE + I32_SIZE);
239
278
  writeU8(STRING_HEADER);
240
279
  writeU32(len);
280
+ currentBuffer.write(thing, currentPosition);
241
281
  } else {
242
282
  allocate(len + HEADER_SIZE);
243
283
  writeU8(SHORT_STRING_HEADER | len);
284
+ currentBuffer.write(thing, currentPosition, "latin1");
244
285
  }
245
- currentBuffer.write(thing, currentPosition);
246
286
  currentPosition += len;
247
287
  break;
248
288
  }
@@ -309,20 +349,111 @@ class BinaryMiddleware extends SerializerMiddleware {
309
349
  i--;
310
350
  break;
311
351
  }
312
- case "boolean":
313
- allocate(HEADER_SIZE);
314
- writeU8(thing === true ? TRUE_HEADER : FALSE_HEADER);
352
+ case "boolean": {
353
+ let lastByte = thing === true ? 1 : 0;
354
+ const bytes = [];
355
+ let count = 1;
356
+ let n;
357
+ for (n = 1; n < 0xffffffff && i + n < data.length; n++) {
358
+ const item = data[i + n];
359
+ if (typeof item !== "boolean") break;
360
+ const pos = count & 0x7;
361
+ if (pos === 0) {
362
+ bytes.push(lastByte);
363
+ lastByte = item === true ? 1 : 0;
364
+ } else if (item === true) {
365
+ lastByte |= 1 << pos;
366
+ }
367
+ count++;
368
+ }
369
+ i += count - 1;
370
+ if (count === 1) {
371
+ allocate(HEADER_SIZE);
372
+ writeU8(lastByte === 1 ? TRUE_HEADER : FALSE_HEADER);
373
+ } else if (count === 2) {
374
+ allocate(HEADER_SIZE * 2);
375
+ writeU8(lastByte & 1 ? TRUE_HEADER : FALSE_HEADER);
376
+ writeU8(lastByte & 2 ? TRUE_HEADER : FALSE_HEADER);
377
+ } else if (count <= 6) {
378
+ allocate(HEADER_SIZE + I8_SIZE);
379
+ writeU8(BOOLEANS_HEADER);
380
+ writeU8((1 << count) | lastByte);
381
+ } else if (count <= 133) {
382
+ allocate(HEADER_SIZE + I8_SIZE * bytes.length + I8_SIZE);
383
+ writeU8(BOOLEANS_HEADER);
384
+ writeU8(0x80 | (count - 7));
385
+ for (const byte of bytes) writeU8(byte);
386
+ writeU8(lastByte);
387
+ } else {
388
+ allocate(HEADER_SIZE + I8_SIZE * bytes.length + I8_SIZE);
389
+ writeU8(BOOLEANS_HEADER);
390
+ writeU8(0xff);
391
+ writeU32(count);
392
+ for (const byte of bytes) writeU8(byte);
393
+ writeU8(lastByte);
394
+ }
315
395
  break;
396
+ }
316
397
  case "object": {
317
398
  if (thing === null) {
318
399
  let n;
319
- for (n = 1; n < 16 && i + n < data.length; n++) {
400
+ for (n = 1; n < 0x100000104 && i + n < data.length; n++) {
320
401
  const item = data[i + n];
321
402
  if (item !== null) break;
322
403
  }
323
- allocate(HEADER_SIZE);
324
- writeU8(NULLS_HEADER | (n - 1));
325
404
  i += n - 1;
405
+ if (n === 1) {
406
+ if (i + 1 < data.length) {
407
+ const next = data[i + 1];
408
+ if (next === true) {
409
+ allocate(HEADER_SIZE);
410
+ writeU8(NULL_AND_TRUE_HEADER);
411
+ i++;
412
+ } else if (next === false) {
413
+ allocate(HEADER_SIZE);
414
+ writeU8(NULL_AND_FALSE_HEADER);
415
+ i++;
416
+ } else if (typeof next === "number") {
417
+ const type = identifyNumber(next);
418
+ if (type === 0) {
419
+ allocate(HEADER_SIZE + I8_SIZE);
420
+ writeU8(NULL_AND_I8_HEADER);
421
+ currentBuffer.writeInt8(next, currentPosition);
422
+ currentPosition += I8_SIZE;
423
+ i++;
424
+ } else if (type === 1) {
425
+ allocate(HEADER_SIZE + I32_SIZE);
426
+ writeU8(NULL_AND_I32_HEADER);
427
+ currentBuffer.writeInt32LE(next, currentPosition);
428
+ currentPosition += I32_SIZE;
429
+ i++;
430
+ } else {
431
+ allocate(HEADER_SIZE);
432
+ writeU8(NULL_HEADER);
433
+ }
434
+ } else {
435
+ allocate(HEADER_SIZE);
436
+ writeU8(NULL_HEADER);
437
+ }
438
+ } else {
439
+ allocate(HEADER_SIZE);
440
+ writeU8(NULL_HEADER);
441
+ }
442
+ } else if (n === 2) {
443
+ allocate(HEADER_SIZE);
444
+ writeU8(NULL2_HEADER);
445
+ } else if (n === 3) {
446
+ allocate(HEADER_SIZE);
447
+ writeU8(NULL3_HEADER);
448
+ } else if (n < 260) {
449
+ allocate(HEADER_SIZE + I8_SIZE);
450
+ writeU8(NULLS8_HEADER);
451
+ writeU8(n - 4);
452
+ } else {
453
+ allocate(HEADER_SIZE + I32_SIZE);
454
+ writeU8(NULLS32_HEADER);
455
+ writeU32(n - 260);
456
+ }
326
457
  } else if (Buffer.isBuffer(thing)) {
327
458
  allocate(HEADER_SIZE + I32_SIZE, true);
328
459
  writeU8(BUFFER_HEADER);
@@ -409,6 +540,31 @@ class BinaryMiddleware extends SerializerMiddleware {
409
540
  checkOverflow();
410
541
  return res;
411
542
  };
543
+ /**
544
+ * Reads up to n bytes
545
+ * @param {number} n amount of bytes to read
546
+ * @returns {Buffer} buffer with bytes
547
+ */
548
+ const readUpTo = n => {
549
+ if (!currentIsBuffer) {
550
+ throw new Error(
551
+ currentBuffer === null
552
+ ? "Unexpected end of stream"
553
+ : "Unexpected lazy element in stream"
554
+ );
555
+ }
556
+ const rem = currentBuffer.length - currentPosition;
557
+ if (rem < n) {
558
+ n = rem;
559
+ }
560
+ const res = /** @type {Buffer} */ (currentBuffer).slice(
561
+ currentPosition,
562
+ currentPosition + n
563
+ );
564
+ currentPosition += n;
565
+ checkOverflow();
566
+ return res;
567
+ };
412
568
  const readU8 = () => {
413
569
  if (!currentIsBuffer) {
414
570
  throw new Error(
@@ -431,117 +587,292 @@ class BinaryMiddleware extends SerializerMiddleware {
431
587
  const readU32 = () => {
432
588
  return read(I32_SIZE).readUInt32LE(0);
433
589
  };
434
-
435
- /** @type {DeserializedType} */
436
- const result = [];
437
- while (currentBuffer !== null) {
438
- if (typeof currentBuffer === "function") {
439
- result.push(
440
- SerializerMiddleware.deserializeLazy(currentBuffer, data =>
441
- this._deserialize(data, context)
442
- )
443
- );
444
- currentDataItem++;
445
- currentBuffer =
446
- currentDataItem < data.length ? data[currentDataItem] : null;
447
- currentIsBuffer = Buffer.isBuffer(currentBuffer);
448
- continue;
590
+ const readBits = (data, n) => {
591
+ let mask = 1;
592
+ while (n !== 0) {
593
+ result.push((data & mask) !== 0);
594
+ mask = mask << 1;
595
+ n--;
449
596
  }
450
- const header = readU8();
597
+ };
598
+ const dispatchTable = Array.from({ length: 256 }).map((_, header) => {
451
599
  switch (header) {
452
- case LAZY_HEADER: {
453
- const count = readU32();
454
- const start = result.length - count;
455
- const data = /** @type {SerializedType} */ (result.slice(start));
456
- result.length = start;
457
- result.push(
458
- SerializerMiddleware.createLazy(
459
- memorize(() => this._deserialize(data, context)),
460
- this,
461
- undefined,
462
- data
463
- )
464
- );
465
- break;
466
- }
467
- case BUFFER_HEADER: {
468
- const len = readU32();
469
- result.push(read(len));
470
- break;
471
- }
600
+ case LAZY_HEADER:
601
+ return () => {
602
+ const count = readU32();
603
+ const lengths = Array.from({ length: count }).map(() => readU32());
604
+ const content = [];
605
+ for (let l of lengths) {
606
+ if (l === 0) {
607
+ if (typeof currentBuffer !== "function") {
608
+ throw new Error("Unexpected non-lazy element in stream");
609
+ }
610
+ content.push(currentBuffer);
611
+ currentDataItem++;
612
+ currentBuffer =
613
+ currentDataItem < data.length ? data[currentDataItem] : null;
614
+ currentIsBuffer = Buffer.isBuffer(currentBuffer);
615
+ } else {
616
+ do {
617
+ const buf = readUpTo(l);
618
+ l -= buf.length;
619
+ content.push(buf);
620
+ } while (l > 0);
621
+ }
622
+ }
623
+ result.push(
624
+ SerializerMiddleware.createLazy(
625
+ memorize(() => this._deserialize(content, context)),
626
+ this,
627
+ undefined,
628
+ content
629
+ )
630
+ );
631
+ };
632
+ case BUFFER_HEADER:
633
+ return () => {
634
+ const len = readU32();
635
+ result.push(read(len));
636
+ };
472
637
  case TRUE_HEADER:
473
- result.push(true);
474
- break;
638
+ return () => result.push(true);
475
639
  case FALSE_HEADER:
476
- result.push(false);
477
- break;
478
- case STRING_HEADER: {
479
- const len = readU32();
480
- const buf = read(len);
481
- result.push(buf.toString());
482
- break;
483
- }
640
+ return () => result.push(false);
641
+ case NULL3_HEADER:
642
+ return () => result.push(null, null, null);
643
+ case NULL2_HEADER:
644
+ return () => result.push(null, null);
645
+ case NULL_HEADER:
646
+ return () => result.push(null);
647
+ case NULL_AND_TRUE_HEADER:
648
+ return () => result.push(null, true);
649
+ case NULL_AND_FALSE_HEADER:
650
+ return () => result.push(null, false);
651
+ case NULL_AND_I8_HEADER:
652
+ return () => {
653
+ if (currentIsBuffer) {
654
+ result.push(
655
+ null,
656
+ /** @type {Buffer} */ (currentBuffer).readInt8(currentPosition)
657
+ );
658
+ currentPosition += I8_SIZE;
659
+ checkOverflow();
660
+ } else {
661
+ result.push(null, read(I8_SIZE).readInt8(0));
662
+ }
663
+ };
664
+ case NULL_AND_I32_HEADER:
665
+ return () => {
666
+ result.push(null);
667
+ if (isInCurrentBuffer(I32_SIZE)) {
668
+ result.push(
669
+ /** @type {Buffer} */ (currentBuffer).readInt32LE(
670
+ currentPosition
671
+ )
672
+ );
673
+ currentPosition += I32_SIZE;
674
+ checkOverflow();
675
+ } else {
676
+ result.push(read(I32_SIZE).readInt32LE(0));
677
+ }
678
+ };
679
+ case NULLS8_HEADER:
680
+ return () => {
681
+ const len = readU8() + 4;
682
+ for (let i = 0; i < len; i++) {
683
+ result.push(null);
684
+ }
685
+ };
686
+ case NULLS32_HEADER:
687
+ return () => {
688
+ const len = readU32() + 260;
689
+ for (let i = 0; i < len; i++) {
690
+ result.push(null);
691
+ }
692
+ };
693
+ case BOOLEANS_HEADER:
694
+ return () => {
695
+ const innerHeader = readU8();
696
+ if ((innerHeader & 0xf0) === 0) {
697
+ readBits(innerHeader, 3);
698
+ } else if ((innerHeader & 0xe0) === 0) {
699
+ readBits(innerHeader, 4);
700
+ } else if ((innerHeader & 0xc0) === 0) {
701
+ readBits(innerHeader, 5);
702
+ } else if ((innerHeader & 0x80) === 0) {
703
+ readBits(innerHeader, 6);
704
+ } else if (innerHeader !== 0xff) {
705
+ let count = (innerHeader & 0x7f) + 7;
706
+ while (count > 8) {
707
+ readBits(readU8(), 8);
708
+ count -= 8;
709
+ }
710
+ readBits(readU8(), count);
711
+ } else {
712
+ let count = readU32();
713
+ while (count > 8) {
714
+ readBits(readU8(), 8);
715
+ count -= 8;
716
+ }
717
+ readBits(readU8(), count);
718
+ }
719
+ };
720
+ case STRING_HEADER:
721
+ return () => {
722
+ const len = readU32();
723
+ if (isInCurrentBuffer(len)) {
724
+ result.push(
725
+ currentBuffer.toString(
726
+ undefined,
727
+ currentPosition,
728
+ currentPosition + len
729
+ )
730
+ );
731
+ currentPosition += len;
732
+ checkOverflow();
733
+ } else {
734
+ result.push(read(len).toString());
735
+ }
736
+ };
737
+ case SHORT_STRING_HEADER:
738
+ return () => result.push("");
739
+ case SHORT_STRING_HEADER | 1:
740
+ return () => {
741
+ if (currentIsBuffer) {
742
+ result.push(
743
+ currentBuffer.toString(
744
+ "latin1",
745
+ currentPosition,
746
+ currentPosition + 1
747
+ )
748
+ );
749
+ currentPosition++;
750
+ checkOverflow();
751
+ } else {
752
+ result.push(read(1).toString("latin1"));
753
+ }
754
+ };
755
+ case I8_HEADER:
756
+ return () => {
757
+ if (currentIsBuffer) {
758
+ result.push(
759
+ /** @type {Buffer} */ (currentBuffer).readInt8(currentPosition)
760
+ );
761
+ currentPosition++;
762
+ checkOverflow();
763
+ } else {
764
+ result.push(read(1).readInt8(0));
765
+ }
766
+ };
484
767
  default:
485
768
  if (header <= 10) {
486
- result.push(header);
769
+ return () => result.push(header);
487
770
  } else if ((header & SHORT_STRING_HEADER) === SHORT_STRING_HEADER) {
488
771
  const len = header & SHORT_STRING_LENGTH_MASK;
489
- const buf = read(len);
490
- result.push(buf.toString());
772
+ return () => {
773
+ if (isInCurrentBuffer(len)) {
774
+ result.push(
775
+ currentBuffer.toString(
776
+ "latin1",
777
+ currentPosition,
778
+ currentPosition + len
779
+ )
780
+ );
781
+ currentPosition += len;
782
+ checkOverflow();
783
+ } else {
784
+ result.push(read(len).toString("latin1"));
785
+ }
786
+ };
491
787
  } else if ((header & NUMBERS_HEADER_MASK) === F64_HEADER) {
492
788
  const len = (header & NUMBERS_COUNT_MASK) + 1;
493
- const need = F64_SIZE * len;
494
- if (isInCurrentBuffer(need)) {
495
- for (let i = 0; i < len; i++) {
496
- result.push(currentBuffer.readDoubleLE(currentPosition));
497
- currentPosition += F64_SIZE;
498
- }
499
- checkOverflow();
500
- } else {
501
- const buf = read(need);
502
- for (let i = 0; i < len; i++) {
503
- result.push(buf.readDoubleLE(i * F64_SIZE));
789
+ return () => {
790
+ const need = F64_SIZE * len;
791
+ if (isInCurrentBuffer(need)) {
792
+ for (let i = 0; i < len; i++) {
793
+ result.push(
794
+ /** @type {Buffer} */ (currentBuffer).readDoubleLE(
795
+ currentPosition
796
+ )
797
+ );
798
+ currentPosition += F64_SIZE;
799
+ }
800
+ checkOverflow();
801
+ } else {
802
+ const buf = read(need);
803
+ for (let i = 0; i < len; i++) {
804
+ result.push(buf.readDoubleLE(i * F64_SIZE));
805
+ }
504
806
  }
505
- }
807
+ };
506
808
  } else if ((header & NUMBERS_HEADER_MASK) === I32_HEADER) {
507
809
  const len = (header & NUMBERS_COUNT_MASK) + 1;
508
- const need = I32_SIZE * len;
509
- if (isInCurrentBuffer(need)) {
510
- for (let i = 0; i < len; i++) {
511
- result.push(currentBuffer.readInt32LE(currentPosition));
512
- currentPosition += I32_SIZE;
513
- }
514
- checkOverflow();
515
- } else {
516
- const buf = read(need);
517
- for (let i = 0; i < len; i++) {
518
- result.push(buf.readInt32LE(i * I32_SIZE));
810
+ return () => {
811
+ const need = I32_SIZE * len;
812
+ if (isInCurrentBuffer(need)) {
813
+ for (let i = 0; i < len; i++) {
814
+ result.push(
815
+ /** @type {Buffer} */ (currentBuffer).readInt32LE(
816
+ currentPosition
817
+ )
818
+ );
819
+ currentPosition += I32_SIZE;
820
+ }
821
+ checkOverflow();
822
+ } else {
823
+ const buf = read(need);
824
+ for (let i = 0; i < len; i++) {
825
+ result.push(buf.readInt32LE(i * I32_SIZE));
826
+ }
519
827
  }
520
- }
828
+ };
521
829
  } else if ((header & NUMBERS_HEADER_MASK) === I8_HEADER) {
522
830
  const len = (header & NUMBERS_COUNT_MASK) + 1;
523
- const need = I8_SIZE * len;
524
- if (isInCurrentBuffer(need)) {
525
- for (let i = 0; i < len; i++) {
526
- result.push(currentBuffer.readInt8(currentPosition));
527
- currentPosition += I8_SIZE;
528
- }
529
- checkOverflow();
530
- } else {
531
- const buf = read(need);
532
- for (let i = 0; i < len; i++) {
533
- result.push(buf.readInt8(i * I8_SIZE));
831
+ return () => {
832
+ const need = I8_SIZE * len;
833
+ if (isInCurrentBuffer(need)) {
834
+ for (let i = 0; i < len; i++) {
835
+ result.push(
836
+ /** @type {Buffer} */ (currentBuffer).readInt8(
837
+ currentPosition
838
+ )
839
+ );
840
+ currentPosition += I8_SIZE;
841
+ }
842
+ checkOverflow();
843
+ } else {
844
+ const buf = read(need);
845
+ for (let i = 0; i < len; i++) {
846
+ result.push(buf.readInt8(i * I8_SIZE));
847
+ }
534
848
  }
535
- }
536
- } else if ((header & NULLS_HEADER_MASK) === NULLS_HEADER) {
537
- const len = (header & NULLS_COUNT_MASK) + 1;
538
- for (let i = 0; i < len; i++) {
539
- result.push(null);
540
- }
849
+ };
541
850
  } else {
542
- throw new Error(`Unexpected header byte 0x${header.toString(16)}`);
851
+ return () => {
852
+ throw new Error(
853
+ `Unexpected header byte 0x${header.toString(16)}`
854
+ );
855
+ };
543
856
  }
544
- break;
857
+ }
858
+ });
859
+
860
+ /** @type {DeserializedType} */
861
+ const result = [];
862
+ while (currentBuffer !== null) {
863
+ if (typeof currentBuffer === "function") {
864
+ result.push(
865
+ SerializerMiddleware.deserializeLazy(currentBuffer, data =>
866
+ this._deserialize(data, context)
867
+ )
868
+ );
869
+ currentDataItem++;
870
+ currentBuffer =
871
+ currentDataItem < data.length ? data[currentDataItem] : null;
872
+ currentIsBuffer = Buffer.isBuffer(currentBuffer);
873
+ } else {
874
+ const header = readU8();
875
+ dispatchTable[header]();
545
876
  }
546
877
  }
547
878
  return result;