webpack 5.99.5 → 5.99.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/README.md +1 -1
  2. package/lib/APIPlugin.js +2 -2
  3. package/lib/Cache.js +1 -1
  4. package/lib/Chunk.js +12 -9
  5. package/lib/Compilation.js +36 -27
  6. package/lib/Compiler.js +4 -3
  7. package/lib/ContextModule.js +2 -1
  8. package/lib/HotModuleReplacementPlugin.js +3 -3
  9. package/lib/Module.js +10 -2
  10. package/lib/ModuleFilenameHelpers.js +1 -1
  11. package/lib/ModuleGraph.js +15 -10
  12. package/lib/NormalModule.js +1 -1
  13. package/lib/PrefetchPlugin.js +1 -1
  14. package/lib/RecordIdsPlugin.js +1 -1
  15. package/lib/SourceMapDevToolPlugin.js +1 -1
  16. package/lib/Template.js +1 -1
  17. package/lib/WebpackOptionsApply.js +17 -2
  18. package/lib/buildChunkGraph.js +7 -2
  19. package/lib/cache/IdleFileCachePlugin.js +1 -1
  20. package/lib/cache/MemoryCachePlugin.js +2 -1
  21. package/lib/cache/MemoryWithGcCachePlugin.js +3 -2
  22. package/lib/cache/PackFileCacheStrategy.js +13 -21
  23. package/lib/cache/ResolverCachePlugin.js +14 -16
  24. package/lib/cli.js +3 -3
  25. package/lib/config/defaults.js +2 -2
  26. package/lib/config/target.js +6 -6
  27. package/lib/css/CssParser.js +1 -1
  28. package/lib/debug/ProfilingPlugin.js +1 -1
  29. package/lib/dependencies/ContextDependencyHelpers.js +1 -1
  30. package/lib/dependencies/CssIcssImportDependency.js +3 -2
  31. package/lib/dependencies/HarmonyExportExpressionDependency.js +1 -1
  32. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +2 -1
  33. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +1 -1
  34. package/lib/dependencies/JsonExportsDependency.js +1 -1
  35. package/lib/dependencies/LocalModulesHelpers.js +1 -1
  36. package/lib/dependencies/RequireResolveDependency.js +1 -1
  37. package/lib/dependencies/WorkerPlugin.js +2 -2
  38. package/lib/esm/ModuleChunkLoadingPlugin.js +20 -0
  39. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +11 -9
  40. package/lib/ids/DeterministicChunkIdsPlugin.js +1 -1
  41. package/lib/ids/DeterministicModuleIdsPlugin.js +1 -1
  42. package/lib/ids/NamedChunkIdsPlugin.js +2 -2
  43. package/lib/ids/NamedModuleIdsPlugin.js +2 -2
  44. package/lib/index.js +1 -1
  45. package/lib/javascript/BasicEvaluatedExpression.js +2 -2
  46. package/lib/javascript/JavascriptParser.js +41 -37
  47. package/lib/library/EnableLibraryPlugin.js +15 -2
  48. package/lib/library/ModuleLibraryPlugin.js +6 -1
  49. package/lib/logging/createConsoleLogger.js +0 -1
  50. package/lib/node/CommonJsChunkLoadingPlugin.js +2 -2
  51. package/lib/node/NodeTemplatePlugin.js +2 -2
  52. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +2 -2
  53. package/lib/node/ReadFileCompileWasmPlugin.js +3 -3
  54. package/lib/optimize/AggressiveMergingPlugin.js +1 -1
  55. package/lib/optimize/SplitChunksPlugin.js +2 -1
  56. package/lib/runtime/GetChunkFilenameRuntimeModule.js +3 -4
  57. package/lib/serialization/BinaryMiddleware.js +22 -38
  58. package/lib/serialization/FileMiddleware.js +29 -33
  59. package/lib/serialization/ObjectMiddleware.js +29 -30
  60. package/lib/serialization/Serializer.js +29 -18
  61. package/lib/serialization/SerializerMiddleware.js +105 -72
  62. package/lib/serialization/SingleItemMiddleware.js +4 -5
  63. package/lib/stats/DefaultStatsFactoryPlugin.js +39 -24
  64. package/lib/stats/DefaultStatsPrinterPlugin.js +267 -80
  65. package/lib/stats/StatsFactory.js +47 -10
  66. package/lib/stats/StatsPrinter.js +52 -31
  67. package/lib/util/ArrayQueue.js +1 -1
  68. package/lib/util/AsyncQueue.js +1 -1
  69. package/lib/util/TupleQueue.js +9 -7
  70. package/lib/util/TupleSet.js +37 -18
  71. package/lib/util/WeakTupleMap.js +50 -37
  72. package/lib/util/cleverMerge.js +2 -2
  73. package/lib/util/comparators.js +1 -1
  74. package/lib/util/createHash.js +1 -1
  75. package/lib/util/fs.js +1 -1
  76. package/lib/util/makeSerializable.js +1 -1
  77. package/lib/util/runtime.js +1 -0
  78. package/lib/util/serialization.js +50 -42
  79. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +1 -1
  80. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +1 -1
  81. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +2 -2
  82. package/lib/wasm-sync/WebAssemblyGenerator.js +1 -1
  83. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +1 -1
  84. package/lib/web/FetchCompileWasmPlugin.js +2 -2
  85. package/lib/webpack.js +1 -1
  86. package/package.json +1 -1
  87. package/types.d.ts +125 -73
@@ -280,10 +280,13 @@ class VariableInfo {
280
280
  /**
281
281
  * @typedef {object} TagInfo
282
282
  * @property {Tag} tag
283
- * @property {TagData} [data]
283
+ * @property {TagData=} data
284
284
  * @property {TagInfo | undefined} next
285
285
  */
286
286
 
287
+ const SCOPE_INFO_TERMINATED_RETURN = 1;
288
+ const SCOPE_INFO_TERMINATED_THROW = 2;
289
+
287
290
  /**
288
291
  * @typedef {object} ScopeInfo
289
292
  * @property {StackedMap<string, VariableInfo | ScopeInfo>} definitions
@@ -293,8 +296,7 @@ class VariableInfo {
293
296
  * @property {boolean} inTry
294
297
  * @property {boolean} isStrict
295
298
  * @property {boolean} isAsmJs
296
- * @property {boolean} inExecutedPath false for unknown state
297
- * @property {undefined|"return"|"throw"} terminated
299
+ * @property {undefined | 1 | 2} terminated
298
300
  */
299
301
 
300
302
  /** @typedef {[number, number]} Range */
@@ -310,9 +312,9 @@ class VariableInfo {
310
312
  * Helper function for joining two ranges into a single range. This is useful
311
313
  * when working with AST nodes, as it allows you to combine the ranges of child nodes
312
314
  * to create the range of the _parent node_.
313
- * @param {[number, number]} startRange start range to join
314
- * @param {[number, number]} endRange end range to join
315
- * @returns {[number, number]} joined range
315
+ * @param {Range} startRange start range to join
316
+ * @param {Range} endRange end range to join
317
+ * @returns {Range} joined range
316
318
  * @example
317
319
  * ```js
318
320
  * const startRange = [0, 5];
@@ -827,8 +829,8 @@ class JavascriptParser extends Parser {
827
829
  if (!right.isCompileTimeValue()) return;
828
830
 
829
831
  const result = operandHandler(
830
- left.asCompileTimeValue(),
831
- right.asCompileTimeValue()
832
+ /** @type {T} */ (left.asCompileTimeValue()),
833
+ /** @type {T} */ (right.asCompileTimeValue())
832
834
  );
833
835
  return valueAsExpression(
834
836
  result,
@@ -1231,7 +1233,9 @@ class JavascriptParser extends Parser {
1231
1233
  const handleConstOperation = operandHandler => {
1232
1234
  const argument = this.evaluateExpression(expr.argument);
1233
1235
  if (!argument.isCompileTimeValue()) return;
1234
- const result = operandHandler(argument.asCompileTimeValue());
1236
+ const result = operandHandler(
1237
+ /** @type {T} */ (argument.asCompileTimeValue())
1238
+ );
1235
1239
  return valueAsExpression(
1236
1240
  result,
1237
1241
  expr,
@@ -2171,7 +2175,7 @@ class JavascriptParser extends Parser {
2171
2175
  this.blockPreWalkStatements(body);
2172
2176
  this.prevStatement = prev;
2173
2177
  this.walkStatements(body);
2174
- }, this.scope.inExecutedPath);
2178
+ }, true);
2175
2179
  }
2176
2180
 
2177
2181
  /**
@@ -2211,20 +2215,12 @@ class JavascriptParser extends Parser {
2211
2215
 
2212
2216
  this.scope.terminated =
2213
2217
  consequentTerminated && alternateTerminated
2214
- ? this.scope.terminated
2218
+ ? alternateTerminated
2215
2219
  : undefined;
2216
- } else {
2217
- const oldState = this.scope.inExecutedPath;
2218
-
2219
- this.scope.inExecutedPath = true;
2220
-
2221
- if (result) {
2222
- this.walkNestedStatement(statement.consequent);
2223
- } else if (statement.alternate) {
2224
- this.walkNestedStatement(statement.alternate);
2225
- }
2226
-
2227
- this.scope.inExecutedPath = oldState;
2220
+ } else if (result) {
2221
+ this.walkNestedStatement(statement.consequent);
2222
+ } else if (statement.alternate) {
2223
+ this.walkNestedStatement(statement.alternate);
2228
2224
  }
2229
2225
  }
2230
2226
 
@@ -2290,7 +2286,9 @@ class JavascriptParser extends Parser {
2290
2286
  if (this.scope.topLevelScope === true) return;
2291
2287
  if (this.hooks.terminate.call(statement)) {
2292
2288
  this.scope.terminated =
2293
- statement.type === "ReturnStatement" ? "return" : "throw";
2289
+ statement.type === "ReturnStatement"
2290
+ ? SCOPE_INFO_TERMINATED_RETURN
2291
+ : SCOPE_INFO_TERMINATED_THROW;
2294
2292
  }
2295
2293
  }
2296
2294
 
@@ -2337,14 +2335,20 @@ class JavascriptParser extends Parser {
2337
2335
  const handlerTerminated = this.scope.terminated;
2338
2336
  this.scope.terminated = undefined;
2339
2337
 
2340
- if (statement.finalizer) this.walkStatement(statement.finalizer);
2338
+ if (statement.finalizer) {
2339
+ this.walkStatement(statement.finalizer);
2340
+ }
2341
2341
 
2342
- if (
2343
- !this.scope.terminated &&
2342
+ const finalizerTerminated = this.scope.terminated;
2343
+ this.scope.terminated = undefined;
2344
+
2345
+ if (finalizerTerminated) {
2346
+ this.scope.terminated = finalizerTerminated;
2347
+ } else if (
2344
2348
  tryTerminated &&
2345
2349
  (statement.handler ? handlerTerminated : true)
2346
2350
  ) {
2347
- this.scope.terminated = tryTerminated;
2351
+ this.scope.terminated = handlerTerminated || tryTerminated;
2348
2352
  }
2349
2353
  }
2350
2354
 
@@ -2412,7 +2416,9 @@ class JavascriptParser extends Parser {
2412
2416
  if (statement.update) {
2413
2417
  this.walkExpression(statement.update);
2414
2418
  }
2419
+
2415
2420
  const body = statement.body;
2421
+
2416
2422
  if (body.type === "BlockStatement") {
2417
2423
  // no need to add additional scope
2418
2424
  const prev = this.prevStatement;
@@ -2446,8 +2452,11 @@ class JavascriptParser extends Parser {
2446
2452
  } else {
2447
2453
  this.walkPattern(statement.left);
2448
2454
  }
2455
+
2449
2456
  this.walkExpression(statement.right);
2457
+
2450
2458
  const body = statement.body;
2459
+
2451
2460
  if (body.type === "BlockStatement") {
2452
2461
  // no need to add additional scope
2453
2462
  const prev = this.prevStatement;
@@ -2484,8 +2493,11 @@ class JavascriptParser extends Parser {
2484
2493
  } else {
2485
2494
  this.walkPattern(statement.left);
2486
2495
  }
2496
+
2487
2497
  this.walkExpression(statement.right);
2498
+
2488
2499
  const body = statement.body;
2500
+
2489
2501
  if (body.type === "BlockStatement") {
2490
2502
  // no need to add additional scope
2491
2503
  const prev = this.prevStatement;
@@ -4074,7 +4086,6 @@ class JavascriptParser extends Parser {
4074
4086
  inTaggedTemplateTag: false,
4075
4087
  isStrict: oldScope.isStrict,
4076
4088
  isAsmJs: oldScope.isAsmJs,
4077
- inExecutedPath: false,
4078
4089
  terminated: undefined,
4079
4090
  definitions: oldScope.definitions.createChild()
4080
4091
  };
@@ -4103,7 +4114,6 @@ class JavascriptParser extends Parser {
4103
4114
  inTry: false,
4104
4115
  inShorthand: false,
4105
4116
  inTaggedTemplateTag: false,
4106
- inExecutedPath: true,
4107
4117
  isStrict: oldScope.isStrict,
4108
4118
  isAsmJs: oldScope.isAsmJs,
4109
4119
  terminated: undefined,
@@ -4136,7 +4146,6 @@ class JavascriptParser extends Parser {
4136
4146
  inTry: false,
4137
4147
  inShorthand: false,
4138
4148
  inTaggedTemplateTag: false,
4139
- inExecutedPath: true,
4140
4149
  isStrict: oldScope.isStrict,
4141
4150
  isAsmJs: oldScope.isAsmJs,
4142
4151
  terminated: undefined,
@@ -4168,7 +4177,6 @@ class JavascriptParser extends Parser {
4168
4177
  inTry: oldScope.inTry,
4169
4178
  inShorthand: false,
4170
4179
  inTaggedTemplateTag: false,
4171
- inExecutedPath,
4172
4180
  isStrict: oldScope.isStrict,
4173
4181
  isAsmJs: oldScope.isAsmJs,
4174
4182
  terminated: oldScope.terminated,
@@ -4179,10 +4187,7 @@ class JavascriptParser extends Parser {
4179
4187
 
4180
4188
  const terminated = this.scope.terminated;
4181
4189
 
4182
- if (
4183
- inExecutedPath &&
4184
- ((this.scope.inTry && terminated === "throw") || terminated === "return")
4185
- ) {
4190
+ if (inExecutedPath && terminated) {
4186
4191
  oldScope.terminated = terminated;
4187
4192
  }
4188
4193
 
@@ -4502,7 +4507,6 @@ class JavascriptParser extends Parser {
4502
4507
  inTry: false,
4503
4508
  inShorthand: false,
4504
4509
  inTaggedTemplateTag: false,
4505
- inExecutedPath: false,
4506
4510
  isStrict: false,
4507
4511
  isAsmJs: false,
4508
4512
  terminated: undefined,
@@ -12,6 +12,11 @@
12
12
  /** @type {WeakMap<Compiler, Set<LibraryType>>} */
13
13
  const enabledTypes = new WeakMap();
14
14
 
15
+ /**
16
+ * @typedef {object} EnableLibraryPluginOptions
17
+ * @property {() => void=} additionalApply function that runs when applying the current plugin.
18
+ */
19
+
15
20
  /**
16
21
  * @param {Compiler} compiler the compiler instance
17
22
  * @returns {Set<LibraryType>} enabled types
@@ -28,9 +33,13 @@ const getEnabledTypes = compiler => {
28
33
  class EnableLibraryPlugin {
29
34
  /**
30
35
  * @param {LibraryType} type library type that should be available
36
+ * @param {EnableLibraryPluginOptions} options options of EnableLibraryPlugin
31
37
  */
32
- constructor(type) {
38
+ constructor(type, options = {}) {
39
+ /** @type {LibraryType} */
33
40
  this.type = type;
41
+ /** @type {EnableLibraryPluginOptions} */
42
+ this.options = options;
34
43
  }
35
44
 
36
45
  /**
@@ -67,13 +76,17 @@ class EnableLibraryPlugin {
67
76
  * @returns {void}
68
77
  */
69
78
  apply(compiler) {
70
- const { type } = this;
79
+ const { type, options } = this;
71
80
 
72
81
  // Only enable once
73
82
  const enabled = getEnabledTypes(compiler);
74
83
  if (enabled.has(type)) return;
75
84
  enabled.add(type);
76
85
 
86
+ if (typeof options.additionalApply === "function") {
87
+ options.additionalApply();
88
+ }
89
+
77
90
  if (typeof type === "string") {
78
91
  const enableExportProperty = () => {
79
92
  const ExportPropertyTemplatePlugin = require("./ExportPropertyLibraryPlugin");
@@ -187,7 +187,12 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
187
187
  );
188
188
  }
189
189
 
190
- if (finalName && (finalName.includes(".") || finalName.includes("["))) {
190
+ if (
191
+ finalName &&
192
+ (finalName.includes(".") ||
193
+ finalName.includes("[") ||
194
+ finalName.includes("("))
195
+ ) {
191
196
  exports.push([exportInfo.name, finalName]);
192
197
  } else {
193
198
  shortHandedExports.push(
@@ -89,7 +89,6 @@ module.exports = ({ level = "info", debug = false, console }) => {
89
89
  .concat(debug)
90
90
  .map(filterToFunction)
91
91
  );
92
- /** @type {number} */
93
92
  const loglevel = LogLevel[`${level}`] || 0;
94
93
 
95
94
  /**
@@ -13,12 +13,12 @@ const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenc
13
13
 
14
14
  /**
15
15
  * @typedef {object} CommonJsChunkLoadingPluginOptions
16
- * @property {boolean} [asyncChunkLoading] enable async chunk loading
16
+ * @property {boolean=} asyncChunkLoading enable async chunk loading
17
17
  */
18
18
 
19
19
  class CommonJsChunkLoadingPlugin {
20
20
  /**
21
- * @param {CommonJsChunkLoadingPluginOptions} [options] options
21
+ * @param {CommonJsChunkLoadingPluginOptions=} options options
22
22
  */
23
23
  constructor(options = {}) {
24
24
  this._asyncChunkLoading = options.asyncChunkLoading;
@@ -12,12 +12,12 @@ const EnableChunkLoadingPlugin = require("../javascript/EnableChunkLoadingPlugin
12
12
 
13
13
  /**
14
14
  * @typedef {object} NodeTemplatePluginOptions
15
- * @property {boolean} [asyncChunkLoading] enable async chunk loading
15
+ * @property {boolean=} asyncChunkLoading enable async chunk loading
16
16
  */
17
17
 
18
18
  class NodeTemplatePlugin {
19
19
  /**
20
- * @param {NodeTemplatePluginOptions} [options] options object
20
+ * @param {NodeTemplatePluginOptions=} options options object
21
21
  */
22
22
  constructor(options = {}) {
23
23
  this._options = options;
@@ -15,14 +15,14 @@ const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRun
15
15
 
16
16
  /**
17
17
  * @typedef {object} ReadFileCompileAsyncWasmPluginOptions
18
- * @property {boolean} [import] use import?
18
+ * @property {boolean=} import use import?
19
19
  */
20
20
 
21
21
  const PLUGIN_NAME = "ReadFileCompileAsyncWasmPlugin";
22
22
 
23
23
  class ReadFileCompileAsyncWasmPlugin {
24
24
  /**
25
- * @param {ReadFileCompileAsyncWasmPluginOptions} [options] options object
25
+ * @param {ReadFileCompileAsyncWasmPluginOptions=} options options object
26
26
  */
27
27
  constructor({ import: useImport = false } = {}) {
28
28
  this._import = useImport;
@@ -15,8 +15,8 @@ const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRunt
15
15
 
16
16
  /**
17
17
  * @typedef {object} ReadFileCompileWasmPluginOptions
18
- * @property {boolean} [mangleImports] mangle imports
19
- * @property {boolean} [import] use import?
18
+ * @property {boolean=} mangleImports mangle imports
19
+ * @property {boolean=} import use import?
20
20
  */
21
21
 
22
22
  // TODO webpack 6 remove
@@ -25,7 +25,7 @@ const PLUGIN_NAME = "ReadFileCompileWasmPlugin";
25
25
 
26
26
  class ReadFileCompileWasmPlugin {
27
27
  /**
28
- * @param {ReadFileCompileWasmPluginOptions} [options] options object
28
+ * @param {ReadFileCompileWasmPluginOptions=} options options object
29
29
  */
30
30
  constructor(options = {}) {
31
31
  this.options = options;
@@ -17,7 +17,7 @@ const { STAGE_ADVANCED } = require("../OptimizationStages");
17
17
 
18
18
  class AggressiveMergingPlugin {
19
19
  /**
20
- * @param {AggressiveMergingPluginOptions=} [options] options object
20
+ * @param {AggressiveMergingPluginOptions=} options options object
21
21
  */
22
22
  constructor(options) {
23
23
  if (
@@ -26,6 +26,7 @@ const MinMaxSizeWarning = require("./MinMaxSizeWarning");
26
26
  /** @typedef {import("../../declarations/WebpackOptions").OptimizationSplitChunksOptions} OptimizationSplitChunksOptions */
27
27
  /** @typedef {import("../../declarations/WebpackOptions").OptimizationSplitChunksSizes} OptimizationSplitChunksSizes */
28
28
  /** @typedef {import("../../declarations/WebpackOptions").Output} OutputOptions */
29
+ /** @typedef {import("../Chunk").ChunkName} ChunkName */
29
30
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
30
31
  /** @typedef {import("../ChunkGroup")} ChunkGroup */
31
32
  /** @typedef {import("../Compiler")} Compiler */
@@ -1387,7 +1388,7 @@ module.exports = class SplitChunksPlugin {
1387
1388
  const item = /** @type {ChunksInfoItem} */ (bestEntry);
1388
1389
  chunksInfoMap.delete(/** @type {string} */ (bestEntryKey));
1389
1390
 
1390
- /** @type {Chunk["name"] | undefined} */
1391
+ /** @type {ChunkName | undefined} */
1391
1392
  let chunkName = item.name;
1392
1393
  // Variable for the new chunk (lazy created)
1393
1394
  /** @type {Chunk | undefined} */
@@ -10,6 +10,7 @@ const Template = require("../Template");
10
10
  const { first } = require("../util/SetHelpers");
11
11
 
12
12
  /** @typedef {import("../Chunk")} Chunk */
13
+ /** @typedef {import("../Chunk").ChunkId} ChunkId */
13
14
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
14
15
  /** @typedef {import("../Compilation")} Compilation */
15
16
  /** @typedef {import("../Compilation").AssetInfo} AssetInfo */
@@ -156,14 +157,12 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
156
157
  hashWithLength: length =>
157
158
  `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
158
159
  chunk: {
159
- id: unquotedStringify(/** @type {number | string} */ (c.id)),
160
+ id: unquotedStringify(/** @type {ChunkId} */ (c.id)),
160
161
  hash: unquotedStringify(/** @type {string} */ (c.renderedHash)),
161
162
  hashWithLength: unquotedStringifyWithLength(
162
163
  /** @type {string} */ (c.renderedHash)
163
164
  ),
164
- name: unquotedStringify(
165
- c.name || /** @type {number | string} */ (c.id)
166
- ),
165
+ name: unquotedStringify(c.name || /** @type {ChunkId} */ (c.id)),
167
166
  contentHash: {
168
167
  [contentType]: unquotedStringify(c.contentHash[contentType])
169
168
  },
@@ -7,15 +7,9 @@
7
7
  const memoize = require("../util/memoize");
8
8
  const SerializerMiddleware = require("./SerializerMiddleware");
9
9
 
10
- /** @typedef {import("./SerializerMiddleware").Context} Context */
11
10
  /** @typedef {import("./types").BufferSerializableType} BufferSerializableType */
12
11
  /** @typedef {import("./types").PrimitiveSerializableType} PrimitiveSerializableType */
13
12
 
14
- /**
15
- * @template LAZY_RESULT
16
- * @typedef {import("./SerializerMiddleware").LazyFunction<LAZY_RESULT>} LazyFunction
17
- */
18
-
19
13
  /*
20
14
  Format:
21
15
 
@@ -141,10 +135,18 @@ const identifyBigInt = n => {
141
135
  return 2;
142
136
  };
143
137
 
138
+ /** @typedef {PrimitiveSerializableType[]} DeserializedType */
139
+ /** @typedef {BufferSerializableType[]} SerializedType} */
140
+ /** @typedef {{ retainedBuffer?: (x: Buffer) => Buffer }} Context} */
141
+
144
142
  /**
145
- * @typedef {PrimitiveSerializableType[]} DeserializedType
146
- * @typedef {BufferSerializableType[]} SerializedType
147
- * @extends {SerializerMiddleware<DeserializedType, SerializedType>}
143
+ * @template LazyInputValue
144
+ * @template LazyOutputValue
145
+ * @typedef {import("./SerializerMiddleware").LazyFunction<LazyInputValue, LazyOutputValue, BinaryMiddleware, undefined>} LazyFunction
146
+ */
147
+
148
+ /**
149
+ * @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
148
150
  */
149
151
  class BinaryMiddleware extends SerializerMiddleware {
150
152
  /**
@@ -157,9 +159,9 @@ class BinaryMiddleware extends SerializerMiddleware {
157
159
  }
158
160
 
159
161
  /**
160
- * @param {LazyFunction<DeserializedType>} fn lazy function
162
+ * @param {LazyFunction<DeserializedType, SerializedType>} fn lazy function
161
163
  * @param {Context} context serialize function
162
- * @returns {LazyFunction<SerializedType>} new lazy
164
+ * @returns {LazyFunction<SerializedType, DeserializedType>} new lazy
163
165
  */
164
166
  _serializeLazy(fn, context) {
165
167
  return SerializerMiddleware.serializeLazy(fn, data =>
@@ -281,7 +283,7 @@ class BinaryMiddleware extends SerializerMiddleware {
281
283
  case "function": {
282
284
  if (!SerializerMiddleware.isLazy(thing))
283
285
  throw new Error(`Unexpected function ${thing}`);
284
- /** @type {SerializedType | LazyFunction<SerializedType>} */
286
+ /** @type {SerializedType | LazyFunction<SerializedType, DeserializedType> | undefined} */
285
287
  let serializedData =
286
288
  SerializerMiddleware.getLazySerializedValue(thing);
287
289
  if (serializedData === undefined) {
@@ -294,18 +296,10 @@ class BinaryMiddleware extends SerializerMiddleware {
294
296
  const data = this._serialize(result, context, allocationScope);
295
297
  leftOverBuffer = allocationScope.leftOverBuffer;
296
298
  allocationScope.leftOverBuffer = null;
297
- SerializerMiddleware.setLazySerializedValue(
298
- /** @type {LazyFunction<DeserializedType>} */
299
- (thing),
300
- data
301
- );
299
+ SerializerMiddleware.setLazySerializedValue(thing, data);
302
300
  serializedData = data;
303
301
  } else {
304
- serializedData = this._serializeLazy(
305
- /** @type {LazyFunction<DeserializedType>} */
306
- (thing),
307
- context
308
- );
302
+ serializedData = this._serializeLazy(thing, context);
309
303
  flush();
310
304
  buffers.push(serializedData);
311
305
  break;
@@ -666,7 +660,7 @@ class BinaryMiddleware extends SerializerMiddleware {
666
660
  * @private
667
661
  * @param {SerializedType} content content
668
662
  * @param {Context} context context object
669
- * @returns {LazyFunction<DeserializedType>} lazy function
663
+ * @returns {LazyFunction<DeserializedType, SerializedType>} lazy function
670
664
  */
671
665
  _createLazyDeserialized(content, context) {
672
666
  return SerializerMiddleware.createLazy(
@@ -679,9 +673,9 @@ class BinaryMiddleware extends SerializerMiddleware {
679
673
 
680
674
  /**
681
675
  * @private
682
- * @param {LazyFunction<SerializedType>} fn lazy function
676
+ * @param {LazyFunction<SerializedType, DeserializedType>} fn lazy function
683
677
  * @param {Context} context context object
684
- * @returns {LazyFunction<DeserializedType>} new lazy
678
+ * @returns {LazyFunction<DeserializedType, SerializedType>} new lazy
685
679
  */
686
680
  _deserializeLazy(fn, context) {
687
681
  return SerializerMiddleware.deserializeLazy(fn, data =>
@@ -701,7 +695,6 @@ class BinaryMiddleware extends SerializerMiddleware {
701
695
  let currentIsBuffer = Buffer.isBuffer(currentBuffer);
702
696
  let currentPosition = 0;
703
697
 
704
- /** @type {(x: Buffer) => Buffer} */
705
698
  const retainedBuffer = context.retainedBuffer || (x => x);
706
699
 
707
700
  const checkOverflow = () => {
@@ -818,17 +811,14 @@ class BinaryMiddleware extends SerializerMiddleware {
818
811
  return () => {
819
812
  const count = readU32();
820
813
  const lengths = Array.from({ length: count }).map(() => readU32());
821
- /** @type {(Buffer | LazyFunction<BufferSerializableType[]>)[]} */
814
+ /** @type {(Buffer | LazyFunction<SerializedType, DeserializedType>)[]} */
822
815
  const content = [];
823
816
  for (let l of lengths) {
824
817
  if (l === 0) {
825
818
  if (typeof currentBuffer !== "function") {
826
819
  throw new Error("Unexpected non-lazy element in stream");
827
820
  }
828
- content.push(
829
- /** @type {LazyFunction<BufferSerializableType[]>} */
830
- (currentBuffer)
831
- );
821
+ content.push(currentBuffer);
832
822
  currentDataItem++;
833
823
  currentBuffer =
834
824
  currentDataItem < data.length ? data[currentDataItem] : null;
@@ -1144,13 +1134,7 @@ class BinaryMiddleware extends SerializerMiddleware {
1144
1134
  let result = [];
1145
1135
  while (currentBuffer !== null) {
1146
1136
  if (typeof currentBuffer === "function") {
1147
- result.push(
1148
- this._deserializeLazy(
1149
- /** @type {LazyFunction<SerializedType>} */
1150
- (currentBuffer),
1151
- context
1152
- )
1153
- );
1137
+ result.push(this._deserializeLazy(currentBuffer, context));
1154
1138
  currentDataItem++;
1155
1139
  currentBuffer =
1156
1140
  currentDataItem < data.length ? data[currentDataItem] : null;
@@ -22,14 +22,8 @@ const SerializerMiddleware = require("./SerializerMiddleware");
22
22
  /** @typedef {typeof import("../util/Hash")} Hash */
23
23
  /** @typedef {import("../util/fs").IStats} IStats */
24
24
  /** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
25
- /** @typedef {import("./SerializerMiddleware").Context} Context */
26
25
  /** @typedef {import("./types").BufferSerializableType} BufferSerializableType */
27
26
 
28
- /**
29
- * @template LAZY_RESULT
30
- * @typedef {import("./SerializerMiddleware").LazyFunction<LAZY_RESULT>} LazyFunction
31
- */
32
-
33
27
  /*
34
28
  Format:
35
29
 
@@ -95,9 +89,14 @@ const readUInt64LE = Buffer.prototype.readBigUInt64LE
95
89
  * @property {BackgroundJob=} backgroundJob
96
90
  */
97
91
 
92
+ /** @typedef {{ name: string, size: number }} LazyOptions */
93
+ /**
94
+ * @typedef {import("./SerializerMiddleware").LazyFunction<BufferSerializableType[], Buffer, FileMiddleware, LazyOptions>} LazyFunction
95
+ */
96
+
98
97
  /**
99
98
  * @param {FileMiddleware} middleware this
100
- * @param {BufferSerializableType[] | Promise<BufferSerializableType[]>} data data to be serialized
99
+ * @param {(BufferSerializableType | LazyFunction)[]} data data to be serialized
101
100
  * @param {string | boolean} name file base name
102
101
  * @param {(name: string | false, buffers: Buffer[], size: number) => Promise<void>} writeFile writes a file
103
102
  * @param {string | Hash} hashFunction hash function to use
@@ -112,7 +111,7 @@ const serialize = async (
112
111
  ) => {
113
112
  /** @type {(Buffer[] | Buffer | Promise<SerializeResult>)[]} */
114
113
  const processedData = [];
115
- /** @type {WeakMap<SerializeResult, LazyFunction<BufferSerializableType[]>>} */
114
+ /** @type {WeakMap<SerializeResult, LazyFunction>} */
116
115
  const resultToLazy = new WeakMap();
117
116
  /** @type {Buffer[] | undefined} */
118
117
  let lastBuffers;
@@ -138,25 +137,19 @@ const serialize = async (
138
137
  } else {
139
138
  const content = item();
140
139
  if (content) {
141
- const options = SerializerMiddleware.getLazyOptions(
142
- /** @type {LazyFunction<Buffer[]>} */
143
- (item)
144
- );
140
+ const options = SerializerMiddleware.getLazyOptions(item);
145
141
  processedData.push(
146
142
  serialize(
147
143
  middleware,
148
- content,
144
+ /** @type {BufferSerializableType[]} */
145
+ (content),
149
146
  (options && options.name) || true,
150
147
  writeFile,
151
148
  hashFunction
152
149
  ).then(result => {
153
- /** @type {LazyFunction<Buffer[]>} */
154
- (item).options.size = result.size;
155
- resultToLazy.set(
156
- result,
157
- /** @type {LazyFunction<Buffer[]>} */
158
- (item)
159
- );
150
+ /** @type {LazyOptions} */
151
+ (item.options).size = result.size;
152
+ resultToLazy.set(result, item);
160
153
  return result;
161
154
  })
162
155
  );
@@ -193,7 +186,7 @@ const serialize = async (
193
186
  writeUInt64LE(buf, item.size, 0);
194
187
  nameBuffer.copy(buf, 8, 0);
195
188
  const lazy =
196
- /** @type {LazyFunction<BufferSerializableType[]>} */
189
+ /** @type {LazyFunction} */
197
190
  (resultToLazy.get(item));
198
191
  SerializerMiddleware.setLazySerializedValue(lazy, buf);
199
192
  return buf;
@@ -351,7 +344,7 @@ const deserialize = async (middleware, name, readFile) => {
351
344
  lastLengthPositive = valuePositive;
352
345
  }
353
346
  }
354
- /** @type {(Buffer | LazyFunction<BufferSerializableType[]>)[]} */
347
+ /** @type {BufferSerializableType[]} */
355
348
  const result = [];
356
349
  for (let length of lengths) {
357
350
  if (length < 0) {
@@ -359,13 +352,16 @@ const deserialize = async (middleware, name, readFile) => {
359
352
  const size = Number(readUInt64LE(slice, 0));
360
353
  const nameBuffer = slice.slice(8);
361
354
  const name = nameBuffer.toString();
362
- /** @type {LazyFunction<BufferSerializableType[]>} */
363
- const lazy = SerializerMiddleware.createLazy(
364
- memoize(() => deserialize(middleware, name, readFile)),
365
- middleware,
366
- { name, size },
367
- slice
368
- );
355
+ const lazy =
356
+ /** @type {LazyFunction} */
357
+ (
358
+ SerializerMiddleware.createLazy(
359
+ memoize(() => deserialize(middleware, name, readFile)),
360
+ middleware,
361
+ { name, size },
362
+ slice
363
+ )
364
+ );
369
365
  result.push(lazy);
370
366
  } else {
371
367
  if (contentPosition === contentItemLength) {
@@ -423,12 +419,12 @@ const deserialize = async (middleware, name, readFile) => {
423
419
  return result;
424
420
  };
425
421
 
426
- /** @typedef {{ filename: string, extension?: string }} FileMiddlewareContext */
422
+ /** @typedef {BufferSerializableType[]} DeserializedType */
423
+ /** @typedef {true} SerializedType */
424
+ /** @typedef {{ filename: string, extension?: string }} Context */
427
425
 
428
426
  /**
429
- * @typedef {BufferSerializableType[]} DeserializedType
430
- * @typedef {true} SerializedType
431
- * @extends {SerializerMiddleware<DeserializedType, SerializedType>}
427
+ * @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
432
428
  */
433
429
  class FileMiddleware extends SerializerMiddleware {
434
430
  /**