webpack 5.48.0 → 5.51.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.

Potentially problematic release.


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

Files changed (50) hide show
  1. package/README.md +4 -16
  2. package/hot/only-dev-server.js +1 -1
  3. package/hot/poll.js +1 -1
  4. package/hot/signal.js +1 -1
  5. package/lib/CompatibilityPlugin.js +21 -4
  6. package/lib/Compilation.js +8 -3
  7. package/lib/EvalSourceMapDevToolPlugin.js +2 -2
  8. package/lib/ExternalModuleFactoryPlugin.js +1 -1
  9. package/lib/FileSystemInfo.js +665 -193
  10. package/lib/HotModuleReplacementPlugin.js +4 -4
  11. package/lib/Module.js +1 -0
  12. package/lib/MultiCompiler.js +0 -2
  13. package/lib/NormalModule.js +51 -20
  14. package/lib/NormalModuleFactory.js +137 -74
  15. package/lib/Parser.js +1 -0
  16. package/lib/RuntimeGlobals.js +5 -0
  17. package/lib/SourceMapDevToolPlugin.js +2 -2
  18. package/lib/WebpackOptionsApply.js +8 -0
  19. package/lib/asset/AssetModulesPlugin.js +0 -1
  20. package/lib/config/defaults.js +27 -6
  21. package/lib/config/normalization.js +6 -1
  22. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +10 -1
  23. package/lib/hmr/HotModuleReplacement.runtime.js +5 -1
  24. package/lib/index.js +0 -3
  25. package/lib/javascript/JavascriptParser.js +2 -0
  26. package/lib/library/ModuleLibraryPlugin.js +4 -0
  27. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +7 -1
  28. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +2 -2
  29. package/lib/node/ReadFileCompileWasmPlugin.js +2 -1
  30. package/lib/node/RequireChunkLoadingRuntimeModule.js +7 -1
  31. package/lib/optimize/ConcatenatedModule.js +3 -3
  32. package/lib/optimize/SplitChunksPlugin.js +4 -4
  33. package/lib/schemes/HttpUriPlugin.js +942 -25
  34. package/lib/serialization/BinaryMiddleware.js +293 -267
  35. package/lib/util/fs.js +40 -0
  36. package/lib/util/identifier.js +26 -8
  37. package/lib/wasm-async/{AsyncWasmChunkLoadingRuntimeModule.js → AsyncWasmLoadingRuntimeModule.js} +3 -3
  38. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +18 -2
  39. package/lib/web/FetchCompileAsyncWasmPlugin.js +2 -2
  40. package/lib/web/FetchCompileWasmPlugin.js +2 -1
  41. package/lib/web/JsonpChunkLoadingRuntimeModule.js +21 -8
  42. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +7 -1
  43. package/package.json +1 -1
  44. package/schemas/WebpackOptions.check.js +1 -1
  45. package/schemas/WebpackOptions.json +43 -0
  46. package/schemas/plugins/schemes/HttpUriPlugin.check.d.ts +7 -0
  47. package/schemas/plugins/schemes/HttpUriPlugin.check.js +6 -0
  48. package/schemas/plugins/schemes/HttpUriPlugin.json +42 -0
  49. package/types.d.ts +110 -14
  50. package/lib/schemes/HttpsUriPlugin.js +0 -63
@@ -10,8 +10,6 @@ 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
-
15
13
  /*
16
14
  Format:
17
15
 
@@ -135,17 +133,29 @@ class BinaryMiddleware extends SerializerMiddleware {
135
133
  /**
136
134
  * @param {DeserializedType} data data
137
135
  * @param {Object} context context object
136
+ * @param {{ leftOverBuffer: Buffer | null, allocationSize: number, increaseCounter: number }} allocationScope allocation scope
138
137
  * @returns {SerializedType} serialized data
139
138
  */
140
- _serialize(data, context) {
141
- /** @type {Buffer} */
142
- let currentBuffer = null;
139
+ _serialize(
140
+ data,
141
+ context,
142
+ allocationScope = {
143
+ allocationSize: 1024,
144
+ increaseCounter: 0,
145
+ leftOverBuffer: null
146
+ }
147
+ ) {
143
148
  /** @type {Buffer} */
144
149
  let leftOverBuffer = null;
145
- let currentPosition = 0;
146
150
  /** @type {BufferSerializableType[]} */
147
151
  let buffers = [];
148
- let buffersTotalLength = 0;
152
+ /** @type {Buffer} */
153
+ let currentBuffer = allocationScope ? allocationScope.leftOverBuffer : null;
154
+ allocationScope.leftOverBuffer = null;
155
+ let currentPosition = 0;
156
+ if (currentBuffer === null) {
157
+ currentBuffer = Buffer.allocUnsafe(allocationScope.allocationSize);
158
+ }
149
159
  const allocate = bytesNeeded => {
150
160
  if (currentBuffer !== null) {
151
161
  if (currentBuffer.length - currentPosition >= bytesNeeded) return;
@@ -156,22 +166,28 @@ class BinaryMiddleware extends SerializerMiddleware {
156
166
  leftOverBuffer = null;
157
167
  } else {
158
168
  currentBuffer = Buffer.allocUnsafe(
159
- Math.max(
160
- bytesNeeded,
161
- Math.min(Math.max(buffersTotalLength, 1024), 16384)
162
- )
169
+ Math.max(bytesNeeded, allocationScope.allocationSize)
163
170
  );
171
+ if (
172
+ !(allocationScope.increaseCounter =
173
+ (allocationScope.increaseCounter + 1) % 4) &&
174
+ allocationScope.allocationSize < 16777216
175
+ ) {
176
+ allocationScope.allocationSize = allocationScope.allocationSize << 1;
177
+ }
164
178
  }
165
179
  };
166
180
  const flush = () => {
167
181
  if (currentBuffer !== null) {
168
- buffers.push(
169
- Buffer.from(
170
- currentBuffer.buffer,
171
- currentBuffer.byteOffset,
172
- currentPosition
173
- )
174
- );
182
+ if (currentPosition > 0) {
183
+ buffers.push(
184
+ Buffer.from(
185
+ currentBuffer.buffer,
186
+ currentBuffer.byteOffset,
187
+ currentPosition
188
+ )
189
+ );
190
+ }
175
191
  if (
176
192
  !leftOverBuffer ||
177
193
  leftOverBuffer.length < currentBuffer.length - currentPosition
@@ -182,8 +198,8 @@ class BinaryMiddleware extends SerializerMiddleware {
182
198
  currentBuffer.byteLength - currentPosition
183
199
  );
184
200
  }
201
+
185
202
  currentBuffer = null;
186
- buffersTotalLength += currentPosition;
187
203
  currentPosition = 0;
188
204
  }
189
205
  };
@@ -207,232 +223,237 @@ class BinaryMiddleware extends SerializerMiddleware {
207
223
  }
208
224
  return size;
209
225
  };
210
- const serializeData = data => {
211
- for (let i = 0; i < data.length; i++) {
212
- const thing = data[i];
213
- switch (typeof thing) {
214
- case "function": {
215
- if (!SerializerMiddleware.isLazy(thing))
216
- throw new Error("Unexpected function " + thing);
217
- /** @type {SerializedType | (() => SerializedType)} */
218
- let serializedData =
219
- SerializerMiddleware.getLazySerializedValue(thing);
220
- if (serializedData === undefined) {
221
- if (SerializerMiddleware.isLazy(thing, this)) {
222
- const data = this._serialize(thing(), context);
223
- SerializerMiddleware.setLazySerializedValue(thing, data);
224
- serializedData = data;
225
- } else {
226
- serializedData = this._serializeLazy(thing, context);
227
- }
226
+ for (let i = 0; i < data.length; i++) {
227
+ const thing = data[i];
228
+ switch (typeof thing) {
229
+ case "function": {
230
+ if (!SerializerMiddleware.isLazy(thing))
231
+ throw new Error("Unexpected function " + thing);
232
+ /** @type {SerializedType | (() => SerializedType)} */
233
+ let serializedData =
234
+ SerializerMiddleware.getLazySerializedValue(thing);
235
+ if (serializedData === undefined) {
236
+ if (SerializerMiddleware.isLazy(thing, this)) {
237
+ flush();
238
+ allocationScope.leftOverBuffer = leftOverBuffer;
239
+ const result =
240
+ /** @type {(Exclude<PrimitiveSerializableType, Promise<PrimitiveSerializableType>>)[]} */ (
241
+ thing()
242
+ );
243
+ const data = this._serialize(result, context, allocationScope);
244
+ leftOverBuffer = allocationScope.leftOverBuffer;
245
+ allocationScope.leftOverBuffer = null;
246
+ SerializerMiddleware.setLazySerializedValue(thing, data);
247
+ serializedData = data;
248
+ } else {
249
+ serializedData = this._serializeLazy(thing, context);
250
+ flush();
251
+ buffers.push(serializedData);
252
+ break;
228
253
  }
254
+ } else {
229
255
  if (typeof serializedData === "function") {
230
256
  flush();
231
257
  buffers.push(serializedData);
232
- } else {
233
- const lengths = [];
234
- for (const item of serializedData) {
235
- let last;
236
- if (typeof item === "function") {
237
- lengths.push(0);
238
- } else if (item.length === 0) {
239
- // ignore
240
- } else if (
241
- lengths.length > 0 &&
242
- (last = lengths[lengths.length - 1]) !== 0
243
- ) {
244
- const remaining = 0xffffffff - last;
245
- if (remaining >= item.length) {
246
- lengths[lengths.length - 1] += item.length;
247
- } else {
248
- lengths.push(item.length - remaining);
249
- lengths[lengths.length - 2] = 0xffffffff;
250
- }
251
- } else {
252
- lengths.push(item.length);
253
- }
254
- }
255
- allocate(5 + lengths.length * 4);
256
- writeU8(LAZY_HEADER);
257
- writeU32(lengths.length);
258
- for (const l of lengths) {
259
- writeU32(l);
260
- }
261
- flush();
262
- for (const item of serializedData) {
263
- buffers.push(item);
264
- }
258
+ break;
265
259
  }
266
- break;
267
260
  }
268
- case "string": {
269
- const len = Buffer.byteLength(thing);
270
- if (len >= 128 || len !== thing.length) {
271
- allocate(len + HEADER_SIZE + I32_SIZE);
272
- writeU8(STRING_HEADER);
273
- writeU32(len);
274
- currentBuffer.write(thing, currentPosition);
261
+ const lengths = [];
262
+ for (const item of serializedData) {
263
+ let last;
264
+ if (typeof item === "function") {
265
+ lengths.push(0);
266
+ } else if (item.length === 0) {
267
+ // ignore
268
+ } else if (
269
+ lengths.length > 0 &&
270
+ (last = lengths[lengths.length - 1]) !== 0
271
+ ) {
272
+ const remaining = 0xffffffff - last;
273
+ if (remaining >= item.length) {
274
+ lengths[lengths.length - 1] += item.length;
275
+ } else {
276
+ lengths.push(item.length - remaining);
277
+ lengths[lengths.length - 2] = 0xffffffff;
278
+ }
275
279
  } else {
276
- allocate(len + HEADER_SIZE);
277
- writeU8(SHORT_STRING_HEADER | len);
278
- currentBuffer.write(thing, currentPosition, "latin1");
280
+ lengths.push(item.length);
279
281
  }
280
- currentPosition += len;
282
+ }
283
+ allocate(5 + lengths.length * 4);
284
+ writeU8(LAZY_HEADER);
285
+ writeU32(lengths.length);
286
+ for (const l of lengths) {
287
+ writeU32(l);
288
+ }
289
+ flush();
290
+ for (const item of serializedData) {
291
+ buffers.push(item);
292
+ }
293
+ break;
294
+ }
295
+ case "string": {
296
+ const len = Buffer.byteLength(thing);
297
+ if (len >= 128 || len !== thing.length) {
298
+ allocate(len + HEADER_SIZE + I32_SIZE);
299
+ writeU8(STRING_HEADER);
300
+ writeU32(len);
301
+ currentBuffer.write(thing, currentPosition);
302
+ } else {
303
+ allocate(len + HEADER_SIZE);
304
+ writeU8(SHORT_STRING_HEADER | len);
305
+ currentBuffer.write(thing, currentPosition, "latin1");
306
+ }
307
+ currentPosition += len;
308
+ break;
309
+ }
310
+ case "number": {
311
+ const type = identifyNumber(thing);
312
+ if (type === 0 && thing >= 0 && thing <= 10) {
313
+ // shortcut for very small numbers
314
+ allocate(I8_SIZE);
315
+ writeU8(thing);
281
316
  break;
282
317
  }
283
- case "number": {
284
- const type = identifyNumber(thing);
285
- if (type === 0 && thing >= 0 && thing <= 10) {
286
- // shortcut for very small numbers
287
- allocate(I8_SIZE);
288
- writeU8(thing);
318
+ /**
319
+ * amount of numbers to write
320
+ * @type {number}
321
+ */
322
+ let n = 1;
323
+ for (; n < 32 && i + n < data.length; n++) {
324
+ const item = data[i + n];
325
+ if (typeof item !== "number") break;
326
+ if (identifyNumber(item) !== type) break;
327
+ }
328
+ switch (type) {
329
+ case 0:
330
+ allocate(HEADER_SIZE + I8_SIZE * n);
331
+ writeU8(I8_HEADER | (n - 1));
332
+ while (n > 0) {
333
+ currentBuffer.writeInt8(
334
+ /** @type {number} */ (data[i]),
335
+ currentPosition
336
+ );
337
+ currentPosition += I8_SIZE;
338
+ n--;
339
+ i++;
340
+ }
289
341
  break;
290
- }
291
- /**
292
- * amount of numbers to write
293
- * @type {number}
294
- */
295
- let n = 1;
296
- for (; n < 32 && i + n < data.length; n++) {
297
- const item = data[i + n];
298
- if (typeof item !== "number") break;
299
- if (identifyNumber(item) !== type) break;
300
- }
301
- switch (type) {
302
- case 0:
303
- allocate(HEADER_SIZE + I8_SIZE * n);
304
- writeU8(I8_HEADER | (n - 1));
305
- while (n > 0) {
306
- currentBuffer.writeInt8(
307
- /** @type {number} */ (data[i]),
308
- currentPosition
309
- );
310
- currentPosition += I8_SIZE;
311
- n--;
312
- i++;
313
- }
314
- break;
315
- case 1:
316
- allocate(HEADER_SIZE + I32_SIZE * n);
317
- writeU8(I32_HEADER | (n - 1));
318
- while (n > 0) {
319
- currentBuffer.writeInt32LE(
320
- /** @type {number} */ (data[i]),
321
- currentPosition
322
- );
323
- currentPosition += I32_SIZE;
324
- n--;
325
- i++;
326
- }
327
- break;
328
- case 2:
329
- allocate(HEADER_SIZE + F64_SIZE * n);
330
- writeU8(F64_HEADER | (n - 1));
331
- while (n > 0) {
332
- currentBuffer.writeDoubleLE(
333
- /** @type {number} */ (data[i]),
334
- currentPosition
335
- );
336
- currentPosition += F64_SIZE;
337
- n--;
338
- i++;
339
- }
340
- break;
341
- }
342
+ case 1:
343
+ allocate(HEADER_SIZE + I32_SIZE * n);
344
+ writeU8(I32_HEADER | (n - 1));
345
+ while (n > 0) {
346
+ currentBuffer.writeInt32LE(
347
+ /** @type {number} */ (data[i]),
348
+ currentPosition
349
+ );
350
+ currentPosition += I32_SIZE;
351
+ n--;
352
+ i++;
353
+ }
354
+ break;
355
+ case 2:
356
+ allocate(HEADER_SIZE + F64_SIZE * n);
357
+ writeU8(F64_HEADER | (n - 1));
358
+ while (n > 0) {
359
+ currentBuffer.writeDoubleLE(
360
+ /** @type {number} */ (data[i]),
361
+ currentPosition
362
+ );
363
+ currentPosition += F64_SIZE;
364
+ n--;
365
+ i++;
366
+ }
367
+ break;
368
+ }
342
369
 
343
- i--;
344
- break;
370
+ i--;
371
+ break;
372
+ }
373
+ case "boolean": {
374
+ let lastByte = thing === true ? 1 : 0;
375
+ const bytes = [];
376
+ let count = 1;
377
+ let n;
378
+ for (n = 1; n < 0xffffffff && i + n < data.length; n++) {
379
+ const item = data[i + n];
380
+ if (typeof item !== "boolean") break;
381
+ const pos = count & 0x7;
382
+ if (pos === 0) {
383
+ bytes.push(lastByte);
384
+ lastByte = item === true ? 1 : 0;
385
+ } else if (item === true) {
386
+ lastByte |= 1 << pos;
387
+ }
388
+ count++;
345
389
  }
346
- case "boolean": {
347
- let lastByte = thing === true ? 1 : 0;
348
- const bytes = [];
349
- let count = 1;
390
+ i += count - 1;
391
+ if (count === 1) {
392
+ allocate(HEADER_SIZE);
393
+ writeU8(lastByte === 1 ? TRUE_HEADER : FALSE_HEADER);
394
+ } else if (count === 2) {
395
+ allocate(HEADER_SIZE * 2);
396
+ writeU8(lastByte & 1 ? TRUE_HEADER : FALSE_HEADER);
397
+ writeU8(lastByte & 2 ? TRUE_HEADER : FALSE_HEADER);
398
+ } else if (count <= 6) {
399
+ allocate(HEADER_SIZE + I8_SIZE);
400
+ writeU8(BOOLEANS_HEADER);
401
+ writeU8((1 << count) | lastByte);
402
+ } else if (count <= 133) {
403
+ allocate(HEADER_SIZE + I8_SIZE + I8_SIZE * bytes.length + I8_SIZE);
404
+ writeU8(BOOLEANS_HEADER);
405
+ writeU8(0x80 | (count - 7));
406
+ for (const byte of bytes) writeU8(byte);
407
+ writeU8(lastByte);
408
+ } else {
409
+ allocate(
410
+ HEADER_SIZE +
411
+ I8_SIZE +
412
+ I32_SIZE +
413
+ I8_SIZE * bytes.length +
414
+ I8_SIZE
415
+ );
416
+ writeU8(BOOLEANS_HEADER);
417
+ writeU8(0xff);
418
+ writeU32(count);
419
+ for (const byte of bytes) writeU8(byte);
420
+ writeU8(lastByte);
421
+ }
422
+ break;
423
+ }
424
+ case "object": {
425
+ if (thing === null) {
350
426
  let n;
351
- for (n = 1; n < 0xffffffff && i + n < data.length; n++) {
427
+ for (n = 1; n < 0x100000104 && i + n < data.length; n++) {
352
428
  const item = data[i + n];
353
- if (typeof item !== "boolean") break;
354
- const pos = count & 0x7;
355
- if (pos === 0) {
356
- bytes.push(lastByte);
357
- lastByte = item === true ? 1 : 0;
358
- } else if (item === true) {
359
- lastByte |= 1 << pos;
360
- }
361
- count++;
429
+ if (item !== null) break;
362
430
  }
363
- i += count - 1;
364
- if (count === 1) {
365
- allocate(HEADER_SIZE);
366
- writeU8(lastByte === 1 ? TRUE_HEADER : FALSE_HEADER);
367
- } else if (count === 2) {
368
- allocate(HEADER_SIZE * 2);
369
- writeU8(lastByte & 1 ? TRUE_HEADER : FALSE_HEADER);
370
- writeU8(lastByte & 2 ? TRUE_HEADER : FALSE_HEADER);
371
- } else if (count <= 6) {
372
- allocate(HEADER_SIZE + I8_SIZE);
373
- writeU8(BOOLEANS_HEADER);
374
- writeU8((1 << count) | lastByte);
375
- } else if (count <= 133) {
376
- allocate(
377
- HEADER_SIZE + I8_SIZE + I8_SIZE * bytes.length + I8_SIZE
378
- );
379
- writeU8(BOOLEANS_HEADER);
380
- writeU8(0x80 | (count - 7));
381
- for (const byte of bytes) writeU8(byte);
382
- writeU8(lastByte);
383
- } else {
384
- allocate(
385
- HEADER_SIZE +
386
- I8_SIZE +
387
- I32_SIZE +
388
- I8_SIZE * bytes.length +
389
- I8_SIZE
390
- );
391
- writeU8(BOOLEANS_HEADER);
392
- writeU8(0xff);
393
- writeU32(count);
394
- for (const byte of bytes) writeU8(byte);
395
- writeU8(lastByte);
396
- }
397
- break;
398
- }
399
- case "object": {
400
- if (thing === null) {
401
- let n;
402
- for (n = 1; n < 0x100000104 && i + n < data.length; n++) {
403
- const item = data[i + n];
404
- if (item !== null) break;
405
- }
406
- i += n - 1;
407
- if (n === 1) {
408
- if (i + 1 < data.length) {
409
- const next = data[i + 1];
410
- if (next === true) {
411
- allocate(HEADER_SIZE);
412
- writeU8(NULL_AND_TRUE_HEADER);
431
+ i += n - 1;
432
+ if (n === 1) {
433
+ if (i + 1 < data.length) {
434
+ const next = data[i + 1];
435
+ if (next === true) {
436
+ allocate(HEADER_SIZE);
437
+ writeU8(NULL_AND_TRUE_HEADER);
438
+ i++;
439
+ } else if (next === false) {
440
+ allocate(HEADER_SIZE);
441
+ writeU8(NULL_AND_FALSE_HEADER);
442
+ i++;
443
+ } else if (typeof next === "number") {
444
+ const type = identifyNumber(next);
445
+ if (type === 0) {
446
+ allocate(HEADER_SIZE + I8_SIZE);
447
+ writeU8(NULL_AND_I8_HEADER);
448
+ currentBuffer.writeInt8(next, currentPosition);
449
+ currentPosition += I8_SIZE;
413
450
  i++;
414
- } else if (next === false) {
415
- allocate(HEADER_SIZE);
416
- writeU8(NULL_AND_FALSE_HEADER);
451
+ } else if (type === 1) {
452
+ allocate(HEADER_SIZE + I32_SIZE);
453
+ writeU8(NULL_AND_I32_HEADER);
454
+ currentBuffer.writeInt32LE(next, currentPosition);
455
+ currentPosition += I32_SIZE;
417
456
  i++;
418
- } else if (typeof next === "number") {
419
- const type = identifyNumber(next);
420
- if (type === 0) {
421
- allocate(HEADER_SIZE + I8_SIZE);
422
- writeU8(NULL_AND_I8_HEADER);
423
- currentBuffer.writeInt8(next, currentPosition);
424
- currentPosition += I8_SIZE;
425
- i++;
426
- } else if (type === 1) {
427
- allocate(HEADER_SIZE + I32_SIZE);
428
- writeU8(NULL_AND_I32_HEADER);
429
- currentBuffer.writeInt32LE(next, currentPosition);
430
- currentPosition += I32_SIZE;
431
- i++;
432
- } else {
433
- allocate(HEADER_SIZE);
434
- writeU8(NULL_HEADER);
435
- }
436
457
  } else {
437
458
  allocate(HEADER_SIZE);
438
459
  writeU8(NULL_HEADER);
@@ -441,59 +462,64 @@ class BinaryMiddleware extends SerializerMiddleware {
441
462
  allocate(HEADER_SIZE);
442
463
  writeU8(NULL_HEADER);
443
464
  }
444
- } else if (n === 2) {
445
- allocate(HEADER_SIZE);
446
- writeU8(NULL2_HEADER);
447
- } else if (n === 3) {
448
- allocate(HEADER_SIZE);
449
- writeU8(NULL3_HEADER);
450
- } else if (n < 260) {
451
- allocate(HEADER_SIZE + I8_SIZE);
452
- writeU8(NULLS8_HEADER);
453
- writeU8(n - 4);
454
- } else {
455
- allocate(HEADER_SIZE + I32_SIZE);
456
- writeU8(NULLS32_HEADER);
457
- writeU32(n - 260);
458
- }
459
- } else if (Buffer.isBuffer(thing)) {
460
- if (thing.length < 8192) {
461
- allocate(HEADER_SIZE + I32_SIZE + thing.length);
462
- writeU8(BUFFER_HEADER);
463
- writeU32(thing.length);
464
- thing.copy(currentBuffer, currentPosition);
465
- currentPosition += thing.length;
466
465
  } else {
467
- allocate(HEADER_SIZE + I32_SIZE);
468
- writeU8(BUFFER_HEADER);
469
- writeU32(thing.length);
470
- flush();
471
- buffers.push(thing);
466
+ allocate(HEADER_SIZE);
467
+ writeU8(NULL_HEADER);
472
468
  }
469
+ } else if (n === 2) {
470
+ allocate(HEADER_SIZE);
471
+ writeU8(NULL2_HEADER);
472
+ } else if (n === 3) {
473
+ allocate(HEADER_SIZE);
474
+ writeU8(NULL3_HEADER);
475
+ } else if (n < 260) {
476
+ allocate(HEADER_SIZE + I8_SIZE);
477
+ writeU8(NULLS8_HEADER);
478
+ writeU8(n - 4);
479
+ } else {
480
+ allocate(HEADER_SIZE + I32_SIZE);
481
+ writeU8(NULLS32_HEADER);
482
+ writeU32(n - 260);
473
483
  }
474
- break;
475
- }
476
- case "symbol": {
477
- if (thing === MEASURE_START_OPERATION) {
478
- measureStart();
479
- } else if (thing === MEASURE_END_OPERATION) {
480
- const size = measureEnd();
484
+ } else if (Buffer.isBuffer(thing)) {
485
+ if (thing.length < 8192) {
486
+ allocate(HEADER_SIZE + I32_SIZE + thing.length);
487
+ writeU8(BUFFER_HEADER);
488
+ writeU32(thing.length);
489
+ thing.copy(currentBuffer, currentPosition);
490
+ currentPosition += thing.length;
491
+ } else {
481
492
  allocate(HEADER_SIZE + I32_SIZE);
482
- writeU8(I32_HEADER);
483
- currentBuffer.writeInt32LE(size, currentPosition);
484
- currentPosition += I32_SIZE;
493
+ writeU8(BUFFER_HEADER);
494
+ writeU32(thing.length);
495
+ flush();
496
+ buffers.push(thing);
485
497
  }
486
- break;
487
498
  }
499
+ break;
500
+ }
501
+ case "symbol": {
502
+ if (thing === MEASURE_START_OPERATION) {
503
+ measureStart();
504
+ } else if (thing === MEASURE_END_OPERATION) {
505
+ const size = measureEnd();
506
+ allocate(HEADER_SIZE + I32_SIZE);
507
+ writeU8(I32_HEADER);
508
+ currentBuffer.writeInt32LE(size, currentPosition);
509
+ currentPosition += I32_SIZE;
510
+ }
511
+ break;
488
512
  }
489
513
  }
490
- };
491
- serializeData(data);
514
+ }
492
515
  flush();
493
516
 
517
+ allocationScope.leftOverBuffer = leftOverBuffer;
518
+
494
519
  // avoid leaking memory
495
520
  currentBuffer = null;
496
521
  leftOverBuffer = null;
522
+ allocationScope = undefined;
497
523
  const _buffers = buffers;
498
524
  buffers = undefined;
499
525
  return _buffers;