webpack 5.98.0 → 5.99.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/lib/AsyncDependenciesBlock.js +3 -1
- package/lib/BannerPlugin.js +1 -1
- package/lib/Cache.js +9 -7
- package/lib/CacheFacade.js +5 -5
- package/lib/Chunk.js +2 -2
- package/lib/ChunkGraph.js +21 -16
- package/lib/ChunkTemplate.js +6 -6
- package/lib/CleanPlugin.js +10 -10
- package/lib/CodeGenerationResults.js +4 -3
- package/lib/CompatibilityPlugin.js +4 -1
- package/lib/Compilation.js +326 -152
- package/lib/Compiler.js +13 -18
- package/lib/ConditionalInitFragment.js +1 -1
- package/lib/ConstPlugin.js +5 -3
- package/lib/ContextModule.js +4 -2
- package/lib/ContextModuleFactory.js +3 -3
- package/lib/ContextReplacementPlugin.js +43 -16
- package/lib/DefinePlugin.js +25 -24
- package/lib/DelegatedModule.js +4 -2
- package/lib/DelegatedModuleFactoryPlugin.js +2 -1
- package/lib/Dependency.js +19 -13
- package/lib/DependencyTemplates.js +4 -3
- package/lib/DllModule.js +4 -2
- package/lib/DllModuleFactory.js +2 -2
- package/lib/DllReferencePlugin.js +2 -1
- package/lib/DynamicEntryPlugin.js +1 -1
- package/lib/EnvironmentPlugin.js +4 -2
- package/lib/ExportsInfo.js +72 -40
- package/lib/ExternalModule.js +14 -5
- package/lib/ExternalModuleFactoryPlugin.js +24 -12
- package/lib/FileSystemInfo.js +129 -94
- package/lib/FlagDependencyExportsPlugin.js +6 -4
- package/lib/FlagDependencyUsagePlugin.js +1 -1
- package/lib/Generator.js +29 -1
- package/lib/HookWebpackError.js +2 -2
- package/lib/HotModuleReplacementPlugin.js +3 -9
- package/lib/IgnoreErrorModuleFactory.js +2 -2
- package/lib/IgnorePlugin.js +0 -5
- package/lib/InitFragment.js +1 -1
- package/lib/LoaderOptionsPlugin.js +8 -5
- package/lib/MainTemplate.js +7 -7
- package/lib/Module.js +40 -17
- package/lib/ModuleBuildError.js +3 -1
- package/lib/ModuleDependencyError.js +4 -3
- package/lib/ModuleDependencyWarning.js +4 -3
- package/lib/ModuleFactory.js +9 -3
- package/lib/ModuleFilenameHelpers.js +13 -13
- package/lib/ModuleGraph.js +20 -14
- package/lib/ModuleGraphConnection.js +7 -13
- package/lib/ModuleNotFoundError.js +1 -1
- package/lib/ModuleParseError.js +2 -1
- package/lib/ModuleSourceTypesConstants.js +11 -0
- package/lib/ModuleTemplate.js +5 -5
- package/lib/ModuleTypeConstants.js +15 -0
- package/lib/MultiCompiler.js +4 -4
- package/lib/MultiStats.js +1 -1
- package/lib/NormalModule.js +101 -54
- package/lib/NormalModuleFactory.js +38 -33
- package/lib/NormalModuleReplacementPlugin.js +3 -2
- package/lib/NullFactory.js +2 -2
- package/lib/Parser.js +4 -3
- package/lib/ProgressPlugin.js +1 -2
- package/lib/RawModule.js +4 -2
- package/lib/RecordIdsPlugin.js +6 -2
- package/lib/RequestShortener.js +3 -1
- package/lib/ResolverFactory.js +12 -9
- package/lib/RuntimeModule.js +4 -2
- package/lib/RuntimeTemplate.js +2 -1
- package/lib/SelfModuleFactory.js +2 -2
- package/lib/SourceMapDevToolPlugin.js +0 -8
- package/lib/Template.js +6 -5
- package/lib/TemplatedPathPlugin.js +15 -12
- package/lib/WebpackOptionsApply.js +1 -1
- package/lib/asset/AssetGenerator.js +237 -170
- package/lib/asset/AssetModulesPlugin.js +50 -8
- package/lib/asset/AssetSourceGenerator.js +18 -0
- package/lib/asset/RawDataUrlModule.js +4 -2
- package/lib/buildChunkGraph.js +14 -4
- package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
- package/lib/cache/PackFileCacheStrategy.js +137 -121
- package/lib/cache/ResolverCachePlugin.js +15 -7
- package/lib/cache/getLazyHashedEtag.js +4 -3
- package/lib/cli.js +23 -15
- package/lib/config/defaults.js +93 -26
- package/lib/config/normalization.js +14 -13
- package/lib/config/target.js +8 -8
- package/lib/container/ContainerEntryModule.js +4 -2
- package/lib/container/ContainerEntryModuleFactory.js +2 -2
- package/lib/container/FallbackModule.js +4 -2
- package/lib/container/FallbackModuleFactory.js +2 -2
- package/lib/container/RemoteModule.js +4 -2
- package/lib/container/options.js +5 -5
- package/lib/css/CssGenerator.js +71 -9
- package/lib/css/CssModulesPlugin.js +30 -5
- package/lib/css/CssParser.js +37 -17
- package/lib/css/walkCssTokens.js +17 -17
- package/lib/debug/ProfilingPlugin.js +98 -38
- package/lib/dependencies/AMDDefineDependencyParserPlugin.js +10 -4
- package/lib/dependencies/AMDPlugin.js +5 -2
- package/lib/dependencies/AMDRequireArrayDependency.js +4 -3
- package/lib/dependencies/AMDRequireContextDependency.js +2 -1
- package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +15 -7
- package/lib/dependencies/AMDRuntimeModules.js +3 -1
- package/lib/dependencies/CommonJsExportRequireDependency.js +4 -5
- package/lib/dependencies/CommonJsExportsParserPlugin.js +3 -3
- package/lib/dependencies/CommonJsImportsParserPlugin.js +8 -7
- package/lib/dependencies/CommonJsRequireContextDependency.js +2 -1
- package/lib/dependencies/ContextDependencyHelpers.js +13 -6
- package/lib/dependencies/CssIcssExportDependency.js +15 -12
- package/lib/dependencies/CssIcssImportDependency.js +4 -1
- package/lib/dependencies/CssLocalIdentifierDependency.js +17 -14
- package/lib/dependencies/ExportsInfoDependency.js +6 -1
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +5 -5
- package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +22 -15
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +23 -8
- package/lib/dependencies/HarmonyExportSpecifierDependency.js +2 -2
- package/lib/dependencies/HarmonyImportDependency.js +8 -6
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +19 -14
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +5 -5
- package/lib/dependencies/ImportContextDependency.js +2 -1
- package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +8 -4
- package/lib/dependencies/JsonExportsDependency.js +24 -8
- package/lib/dependencies/LoaderPlugin.js +4 -14
- package/lib/dependencies/RequireContextDependency.js +2 -1
- package/lib/dependencies/RequireContextDependencyParserPlugin.js +6 -3
- package/lib/dependencies/RequireEnsureDependenciesBlock.js +3 -2
- package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +11 -5
- package/lib/dependencies/RequireResolveContextDependency.js +1 -1
- package/lib/dependencies/WebAssemblyExportImportedDependency.js +1 -1
- package/lib/dependencies/WorkerDependency.js +6 -3
- package/lib/dependencies/WorkerPlugin.js +100 -41
- package/lib/esm/ModuleChunkFormatPlugin.js +5 -0
- package/lib/hmr/HotModuleReplacement.runtime.js +1 -0
- package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +1 -0
- package/lib/hmr/LazyCompilationPlugin.js +32 -24
- package/lib/hmr/lazyCompilationBackend.js +1 -1
- package/lib/ids/DeterministicModuleIdsPlugin.js +1 -1
- package/lib/ids/HashedModuleIdsPlugin.js +2 -1
- package/lib/ids/IdHelpers.js +15 -14
- package/lib/ids/SyncModuleIdsPlugin.js +9 -5
- package/lib/index.js +5 -5
- package/lib/javascript/BasicEvaluatedExpression.js +6 -6
- package/lib/javascript/JavascriptGenerator.js +11 -1
- package/lib/javascript/JavascriptModulesPlugin.js +51 -31
- package/lib/javascript/JavascriptParser.js +287 -188
- package/lib/javascript/JavascriptParserHelpers.js +10 -9
- package/lib/javascript/StartupHelpers.js +4 -1
- package/lib/json/JsonData.js +4 -4
- package/lib/json/JsonGenerator.js +54 -22
- package/lib/json/JsonModulesPlugin.js +16 -2
- package/lib/json/JsonParser.js +8 -4
- package/lib/library/AbstractLibraryPlugin.js +7 -3
- package/lib/library/AssignLibraryPlugin.js +29 -1
- package/lib/library/EnableLibraryPlugin.js +7 -10
- package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
- package/lib/library/ModuleLibraryPlugin.js +121 -15
- package/lib/logging/Logger.js +2 -2
- package/lib/logging/createConsoleLogger.js +4 -4
- package/lib/node/NodeEnvironmentPlugin.js +6 -2
- package/lib/node/ReadFileCompileAsyncWasmPlugin.js +1 -2
- package/lib/node/ReadFileCompileWasmPlugin.js +1 -2
- package/lib/node/nodeConsole.js +3 -1
- package/lib/optimize/AggressiveSplittingPlugin.js +1 -1
- package/lib/optimize/ConcatenatedModule.js +19 -12
- package/lib/optimize/InnerGraph.js +3 -2
- package/lib/optimize/InnerGraphPlugin.js +13 -7
- package/lib/optimize/LimitChunkCountPlugin.js +20 -0
- package/lib/optimize/ModuleConcatenationPlugin.js +10 -7
- package/lib/optimize/RealContentHashPlugin.js +8 -4
- package/lib/optimize/SideEffectsFlagPlugin.js +4 -2
- package/lib/optimize/SplitChunksPlugin.js +87 -65
- package/lib/rules/BasicEffectRulePlugin.js +9 -1
- package/lib/rules/BasicMatcherRulePlugin.js +15 -4
- package/lib/rules/ObjectMatcherRulePlugin.js +12 -3
- package/lib/rules/RuleSetCompiler.js +25 -14
- package/lib/rules/UseEffectRulePlugin.js +47 -17
- package/lib/runtime/GetChunkFilenameRuntimeModule.js +6 -6
- package/lib/schemes/HttpUriPlugin.js +38 -17
- package/lib/serialization/BinaryMiddleware.js +52 -19
- package/lib/serialization/FileMiddleware.js +78 -48
- package/lib/serialization/ObjectMiddleware.js +78 -29
- package/lib/serialization/PlainObjectSerializer.js +1 -1
- package/lib/serialization/Serializer.js +15 -10
- package/lib/serialization/SerializerMiddleware.js +80 -41
- package/lib/serialization/SingleItemMiddleware.js +10 -7
- package/lib/serialization/types.js +1 -1
- package/lib/sharing/ConsumeSharedModule.js +4 -2
- package/lib/sharing/ProvideSharedModule.js +4 -2
- package/lib/sharing/ProvideSharedModuleFactory.js +5 -3
- package/lib/sharing/utils.js +2 -2
- package/lib/stats/DefaultStatsFactoryPlugin.js +80 -78
- package/lib/stats/DefaultStatsPresetPlugin.js +43 -23
- package/lib/stats/DefaultStatsPrinterPlugin.js +85 -43
- package/lib/stats/StatsFactory.js +11 -11
- package/lib/stats/StatsPrinter.js +7 -7
- package/lib/util/ArrayHelpers.js +2 -4
- package/lib/util/ArrayQueue.js +1 -1
- package/lib/util/AsyncQueue.js +4 -4
- package/lib/util/IterableHelpers.js +1 -1
- package/lib/util/LazyBucketSortedSet.js +41 -23
- package/lib/util/LazySet.js +3 -2
- package/lib/util/MapHelpers.js +1 -1
- package/lib/util/ParallelismFactorCalculator.js +1 -1
- package/lib/util/Semaphore.js +3 -3
- package/lib/util/SetHelpers.js +1 -1
- package/lib/util/SortableSet.js +9 -7
- package/lib/util/TupleQueue.js +9 -8
- package/lib/util/TupleSet.js +2 -2
- package/lib/util/WeakTupleMap.js +12 -11
- package/lib/util/binarySearchBounds.js +2 -1
- package/lib/util/cleverMerge.js +84 -54
- package/lib/util/comparators.js +22 -21
- package/lib/util/compileBooleanMatcher.js +3 -3
- package/lib/util/concatenate.js +6 -4
- package/lib/util/create-schema-validation.js +4 -4
- package/lib/util/createHash.js +2 -2
- package/lib/util/deprecation.js +35 -33
- package/lib/util/deterministicGrouping.js +6 -6
- package/lib/util/findGraphRoots.js +1 -1
- package/lib/util/fs.js +39 -39
- package/lib/util/hash/wasm-hash.js +2 -2
- package/lib/util/identifier.js +15 -18
- package/lib/util/makeSerializable.js +1 -1
- package/lib/util/memoize.js +4 -1
- package/lib/util/objectToMap.js +3 -2
- package/lib/util/processAsyncTree.js +2 -2
- package/lib/util/propertyName.js +0 -1
- package/lib/util/registerExternalSerializer.js +15 -18
- package/lib/util/removeBOM.js +25 -0
- package/lib/util/runtime.js +34 -27
- package/lib/util/serialization.js +5 -16
- package/lib/util/smartGrouping.js +3 -3
- package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +3 -3
- package/lib/wasm-async/AsyncWebAssemblyGenerator.js +11 -0
- package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +10 -0
- package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +4 -0
- package/lib/wasm-sync/WebAssemblyGenerator.js +28 -12
- package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +10 -0
- package/lib/wasm-sync/WebAssemblyParser.js +9 -4
- package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -0
- package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +30 -25
- package/package.json +19 -17
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +48 -13
- package/schemas/plugins/JsonModulesPluginGenerator.check.d.ts +7 -0
- package/schemas/plugins/JsonModulesPluginGenerator.check.js +6 -0
- package/schemas/plugins/JsonModulesPluginGenerator.json +11 -0
- package/schemas/plugins/SourceMapDevToolPlugin.json +2 -2
- package/types.d.ts +918 -615
- package/lib/library/ModernModuleLibraryPlugin.js +0 -144
@@ -13,6 +13,7 @@ const {
|
|
13
13
|
createGunzip,
|
14
14
|
constants: zConstants
|
15
15
|
} = require("zlib");
|
16
|
+
const { DEFAULTS } = require("../config/defaults");
|
16
17
|
const createHash = require("../util/createHash");
|
17
18
|
const { dirname, join, mkdirp } = require("../util/fs");
|
18
19
|
const memoize = require("../util/memoize");
|
@@ -21,8 +22,14 @@ const SerializerMiddleware = require("./SerializerMiddleware");
|
|
21
22
|
/** @typedef {typeof import("../util/Hash")} Hash */
|
22
23
|
/** @typedef {import("../util/fs").IStats} IStats */
|
23
24
|
/** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
|
25
|
+
/** @typedef {import("./SerializerMiddleware").Context} Context */
|
24
26
|
/** @typedef {import("./types").BufferSerializableType} BufferSerializableType */
|
25
27
|
|
28
|
+
/**
|
29
|
+
* @template LAZY_RESULT
|
30
|
+
* @typedef {import("./SerializerMiddleware").LazyFunction<LAZY_RESULT>} LazyFunction
|
31
|
+
*/
|
32
|
+
|
26
33
|
/*
|
27
34
|
Format:
|
28
35
|
|
@@ -58,7 +65,7 @@ const hashForName = (buffers, hashFunction) => {
|
|
58
65
|
const COMPRESSION_CHUNK_SIZE = 100 * 1024 * 1024;
|
59
66
|
const DECOMPRESSION_CHUNK_SIZE = 100 * 1024 * 1024;
|
60
67
|
|
61
|
-
/** @type {
|
68
|
+
/** @type {(buffer: Buffer, value: number, offset: number) => void} */
|
62
69
|
const writeUInt64LE = Buffer.prototype.writeBigUInt64LE
|
63
70
|
? (buf, value, offset) => {
|
64
71
|
buf.writeBigUInt64LE(BigInt(value), offset);
|
@@ -70,7 +77,7 @@ const writeUInt64LE = Buffer.prototype.writeBigUInt64LE
|
|
70
77
|
buf.writeUInt32LE(high, offset + 4);
|
71
78
|
};
|
72
79
|
|
73
|
-
/** @type {
|
80
|
+
/** @type {(buffer: Buffer, offset: number) => void} */
|
74
81
|
const readUInt64LE = Buffer.prototype.readBigUInt64LE
|
75
82
|
? (buf, offset) => Number(buf.readBigUInt64LE(offset))
|
76
83
|
: (buf, offset) => {
|
@@ -79,18 +86,20 @@ const readUInt64LE = Buffer.prototype.readBigUInt64LE
|
|
79
86
|
return high * 0x100000000 + low;
|
80
87
|
};
|
81
88
|
|
89
|
+
/** @typedef {Promise<void | void[]>} BackgroundJob */
|
90
|
+
|
82
91
|
/**
|
83
92
|
* @typedef {object} SerializeResult
|
84
93
|
* @property {string | false} name
|
85
94
|
* @property {number} size
|
86
|
-
* @property {
|
95
|
+
* @property {BackgroundJob=} backgroundJob
|
87
96
|
*/
|
88
97
|
|
89
98
|
/**
|
90
99
|
* @param {FileMiddleware} middleware this
|
91
100
|
* @param {BufferSerializableType[] | Promise<BufferSerializableType[]>} data data to be serialized
|
92
101
|
* @param {string | boolean} name file base name
|
93
|
-
* @param {
|
102
|
+
* @param {(name: string | false, buffers: Buffer[], size: number) => Promise<void>} writeFile writes a file
|
94
103
|
* @param {string | Hash} hashFunction hash function to use
|
95
104
|
* @returns {Promise<SerializeResult>} resulting file pointer and promise
|
96
105
|
*/
|
@@ -99,11 +108,11 @@ const serialize = async (
|
|
99
108
|
data,
|
100
109
|
name,
|
101
110
|
writeFile,
|
102
|
-
hashFunction =
|
111
|
+
hashFunction = DEFAULTS.HASH_FUNCTION
|
103
112
|
) => {
|
104
|
-
/** @type {(Buffer[] | Buffer |
|
113
|
+
/** @type {(Buffer[] | Buffer | Promise<SerializeResult>)[]} */
|
105
114
|
const processedData = [];
|
106
|
-
/** @type {WeakMap<SerializeResult,
|
115
|
+
/** @type {WeakMap<SerializeResult, LazyFunction<BufferSerializableType[]>>} */
|
107
116
|
const resultToLazy = new WeakMap();
|
108
117
|
/** @type {Buffer[] | undefined} */
|
109
118
|
let lastBuffers;
|
@@ -129,7 +138,10 @@ const serialize = async (
|
|
129
138
|
} else {
|
130
139
|
const content = item();
|
131
140
|
if (content) {
|
132
|
-
const options = SerializerMiddleware.getLazyOptions(
|
141
|
+
const options = SerializerMiddleware.getLazyOptions(
|
142
|
+
/** @type {LazyFunction<Buffer[]>} */
|
143
|
+
(item)
|
144
|
+
);
|
133
145
|
processedData.push(
|
134
146
|
serialize(
|
135
147
|
middleware,
|
@@ -138,8 +150,13 @@ const serialize = async (
|
|
138
150
|
writeFile,
|
139
151
|
hashFunction
|
140
152
|
).then(result => {
|
141
|
-
/** @type {
|
142
|
-
|
153
|
+
/** @type {LazyFunction<Buffer[]>} */
|
154
|
+
(item).options.size = result.size;
|
155
|
+
resultToLazy.set(
|
156
|
+
result,
|
157
|
+
/** @type {LazyFunction<Buffer[]>} */
|
158
|
+
(item)
|
159
|
+
);
|
143
160
|
return result;
|
144
161
|
})
|
145
162
|
);
|
@@ -160,24 +177,24 @@ const serialize = async (
|
|
160
177
|
throw new Error("Unexpected falsy value in items array");
|
161
178
|
}
|
162
179
|
}
|
163
|
-
/** @type {
|
180
|
+
/** @type {BackgroundJob[]} */
|
164
181
|
const backgroundJobs = [];
|
165
|
-
const resolvedData = (
|
166
|
-
await Promise.all(
|
167
|
-
/** @type {Promise<Buffer[] | Buffer | SerializeResult>[]} */
|
168
|
-
(processedData)
|
169
|
-
)
|
170
|
-
).map(item => {
|
182
|
+
const resolvedData = (await Promise.all(processedData)).map(item => {
|
171
183
|
if (Array.isArray(item) || Buffer.isBuffer(item)) return item;
|
172
184
|
|
173
|
-
backgroundJobs.push(
|
185
|
+
backgroundJobs.push(
|
186
|
+
/** @type {BackgroundJob} */
|
187
|
+
(item.backgroundJob)
|
188
|
+
);
|
174
189
|
// create pointer buffer from size and name
|
175
190
|
const name = /** @type {string} */ (item.name);
|
176
191
|
const nameBuffer = Buffer.from(name);
|
177
192
|
const buf = Buffer.allocUnsafe(8 + nameBuffer.length);
|
178
193
|
writeUInt64LE(buf, item.size, 0);
|
179
194
|
nameBuffer.copy(buf, 8, 0);
|
180
|
-
const lazy =
|
195
|
+
const lazy =
|
196
|
+
/** @type {LazyFunction<BufferSerializableType[]>} */
|
197
|
+
(resultToLazy.get(item));
|
181
198
|
SerializerMiddleware.setLazySerializedValue(lazy, buf);
|
182
199
|
return buf;
|
183
200
|
});
|
@@ -225,14 +242,14 @@ const serialize = async (
|
|
225
242
|
backgroundJob:
|
226
243
|
backgroundJobs.length === 1
|
227
244
|
? backgroundJobs[0]
|
228
|
-
: Promise.all(backgroundJobs)
|
245
|
+
: /** @type {BackgroundJob} */ (Promise.all(backgroundJobs))
|
229
246
|
};
|
230
247
|
};
|
231
248
|
|
232
249
|
/**
|
233
250
|
* @param {FileMiddleware} middleware this
|
234
251
|
* @param {string | false} name filename
|
235
|
-
* @param {
|
252
|
+
* @param {(name: string | false) => Promise<Buffer[]>} readFile read content of a file
|
236
253
|
* @returns {Promise<BufferSerializableType[]>} deserialized data
|
237
254
|
*/
|
238
255
|
const deserialize = async (middleware, name, readFile) => {
|
@@ -334,6 +351,7 @@ const deserialize = async (middleware, name, readFile) => {
|
|
334
351
|
lastLengthPositive = valuePositive;
|
335
352
|
}
|
336
353
|
}
|
354
|
+
/** @type {(Buffer | LazyFunction<BufferSerializableType[]>)[]} */
|
337
355
|
const result = [];
|
338
356
|
for (let length of lengths) {
|
339
357
|
if (length < 0) {
|
@@ -341,17 +359,14 @@ const deserialize = async (middleware, name, readFile) => {
|
|
341
359
|
const size = Number(readUInt64LE(slice, 0));
|
342
360
|
const nameBuffer = slice.slice(8);
|
343
361
|
const name = nameBuffer.toString();
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
size
|
351
|
-
},
|
352
|
-
slice
|
353
|
-
)
|
362
|
+
/** @type {LazyFunction<BufferSerializableType[]>} */
|
363
|
+
const lazy = SerializerMiddleware.createLazy(
|
364
|
+
memoize(() => deserialize(middleware, name, readFile)),
|
365
|
+
middleware,
|
366
|
+
{ name, size },
|
367
|
+
slice
|
354
368
|
);
|
369
|
+
result.push(lazy);
|
355
370
|
} else {
|
356
371
|
if (contentPosition === contentItemLength) {
|
357
372
|
nextContent();
|
@@ -420,7 +435,7 @@ class FileMiddleware extends SerializerMiddleware {
|
|
420
435
|
* @param {IntermediateFileSystem} fs filesystem
|
421
436
|
* @param {string | Hash} hashFunction hash function to use
|
422
437
|
*/
|
423
|
-
constructor(fs, hashFunction =
|
438
|
+
constructor(fs, hashFunction = DEFAULTS.HASH_FUNCTION) {
|
424
439
|
super();
|
425
440
|
this.fs = fs;
|
426
441
|
this._hashFunction = hashFunction;
|
@@ -428,8 +443,8 @@ class FileMiddleware extends SerializerMiddleware {
|
|
428
443
|
|
429
444
|
/**
|
430
445
|
* @param {DeserializedType} data data
|
431
|
-
* @param {
|
432
|
-
* @returns {SerializedType|Promise<SerializedType>} serialized data
|
446
|
+
* @param {Context} context context object
|
447
|
+
* @returns {SerializedType | Promise<SerializedType> | null} serialized data
|
433
448
|
*/
|
434
449
|
serialize(data, context) {
|
435
450
|
const { filename, extension = "" } = context;
|
@@ -483,7 +498,7 @@ class FileMiddleware extends SerializerMiddleware {
|
|
483
498
|
stream.on("finish", () => resolve());
|
484
499
|
}
|
485
500
|
// split into chunks for WRITE_LIMIT_CHUNK size
|
486
|
-
/** @type {
|
501
|
+
/** @type {Buffer[]} */
|
487
502
|
const chunks = [];
|
488
503
|
for (const b of content) {
|
489
504
|
if (b.length < WRITE_LIMIT_CHUNK) {
|
@@ -590,14 +605,14 @@ class FileMiddleware extends SerializerMiddleware {
|
|
590
605
|
|
591
606
|
/**
|
592
607
|
* @param {SerializedType} data data
|
593
|
-
* @param {
|
594
|
-
* @returns {DeserializedType|Promise<DeserializedType>} deserialized data
|
608
|
+
* @param {Context} context context object
|
609
|
+
* @returns {DeserializedType | Promise<DeserializedType>} deserialized data
|
595
610
|
*/
|
596
611
|
deserialize(data, context) {
|
597
612
|
const { filename, extension = "" } = context;
|
598
613
|
/**
|
599
614
|
* @param {string | boolean} name name
|
600
|
-
* @returns {Promise<
|
615
|
+
* @returns {Promise<Buffer[]>} result
|
601
616
|
*/
|
602
617
|
const readFile = name =>
|
603
618
|
new Promise((resolve, reject) => {
|
@@ -614,7 +629,7 @@ class FileMiddleware extends SerializerMiddleware {
|
|
614
629
|
let currentBuffer;
|
615
630
|
/** @type {number | undefined} */
|
616
631
|
let currentBufferUsed;
|
617
|
-
/** @type {
|
632
|
+
/** @type {Buffer[]} */
|
618
633
|
const buf = [];
|
619
634
|
/** @type {import("zlib").Zlib & import("stream").Transform | undefined} */
|
620
635
|
let decompression;
|
@@ -628,7 +643,12 @@ class FileMiddleware extends SerializerMiddleware {
|
|
628
643
|
});
|
629
644
|
}
|
630
645
|
if (decompression) {
|
646
|
+
/** @typedef {(value: Buffer[] | PromiseLike<Buffer[]>) => void} NewResolve */
|
647
|
+
/** @typedef {(reason?: Error) => void} NewReject */
|
648
|
+
|
649
|
+
/** @type {NewResolve | undefined} */
|
631
650
|
let newResolve;
|
651
|
+
/** @type {NewReject | undefined} */
|
632
652
|
let newReject;
|
633
653
|
resolve(
|
634
654
|
Promise.all([
|
@@ -636,15 +656,21 @@ class FileMiddleware extends SerializerMiddleware {
|
|
636
656
|
newResolve = rs;
|
637
657
|
newReject = rj;
|
638
658
|
}),
|
639
|
-
new Promise(
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
659
|
+
new Promise(
|
660
|
+
/**
|
661
|
+
* @param {(value?: undefined) => void} resolve resolve
|
662
|
+
* @param {(reason?: Error) => void} reject reject
|
663
|
+
*/
|
664
|
+
(resolve, reject) => {
|
665
|
+
decompression.on("data", chunk => buf.push(chunk));
|
666
|
+
decompression.on("end", () => resolve());
|
667
|
+
decompression.on("error", err => reject(err));
|
668
|
+
}
|
669
|
+
)
|
644
670
|
]).then(() => buf)
|
645
671
|
);
|
646
|
-
resolve = newResolve;
|
647
|
-
reject = newReject;
|
672
|
+
resolve = /** @type {NewResolve} */ (newResolve);
|
673
|
+
reject = /** @type {NewReject} */ (newReject);
|
648
674
|
}
|
649
675
|
this.fs.open(file, "r", (err, _fd) => {
|
650
676
|
if (err) {
|
@@ -694,12 +720,16 @@ class FileMiddleware extends SerializerMiddleware {
|
|
694
720
|
remaining -= bytesRead;
|
695
721
|
if (
|
696
722
|
currentBufferUsed ===
|
697
|
-
/** @type {Buffer} */
|
723
|
+
/** @type {Buffer} */
|
724
|
+
(currentBuffer).length
|
698
725
|
) {
|
699
726
|
if (decompression) {
|
700
727
|
decompression.write(currentBuffer);
|
701
728
|
} else {
|
702
|
-
buf.push(
|
729
|
+
buf.push(
|
730
|
+
/** @type {Buffer} */
|
731
|
+
(currentBuffer)
|
732
|
+
);
|
703
733
|
}
|
704
734
|
currentBuffer = undefined;
|
705
735
|
if (remaining === 0) {
|
@@ -4,6 +4,7 @@
|
|
4
4
|
|
5
5
|
"use strict";
|
6
6
|
|
7
|
+
const { DEFAULTS } = require("../config/defaults");
|
7
8
|
const createHash = require("../util/createHash");
|
8
9
|
const ArraySerializer = require("./ArraySerializer");
|
9
10
|
const DateObjectSerializer = require("./DateObjectSerializer");
|
@@ -16,10 +17,16 @@ const SerializerMiddleware = require("./SerializerMiddleware");
|
|
16
17
|
const SetObjectSerializer = require("./SetObjectSerializer");
|
17
18
|
|
18
19
|
/** @typedef {typeof import("../util/Hash")} Hash */
|
20
|
+
/** @typedef {import("./SerializerMiddleware").Context} Context */
|
19
21
|
/** @typedef {import("./types").ComplexSerializableType} ComplexSerializableType */
|
20
22
|
/** @typedef {import("./types").PrimitiveSerializableType} PrimitiveSerializableType */
|
21
23
|
|
22
|
-
/** @typedef {new (...params:
|
24
|
+
/** @typedef {new (...params: EXPECTED_ANY[]) => EXPECTED_ANY} Constructor */
|
25
|
+
|
26
|
+
/**
|
27
|
+
* @template LAZY_RESULT
|
28
|
+
* @typedef {import("./SerializerMiddleware").LazyFunction<LAZY_RESULT>} LazyFunction
|
29
|
+
*/
|
23
30
|
|
24
31
|
/*
|
25
32
|
|
@@ -43,24 +50,36 @@ Technically any value can be used.
|
|
43
50
|
|
44
51
|
*/
|
45
52
|
|
53
|
+
/**
|
54
|
+
* @typedef {object} ObjectSerializerSnapshot
|
55
|
+
* @property {number} length
|
56
|
+
* @property {number} cycleStackSize
|
57
|
+
* @property {number} referenceableSize
|
58
|
+
* @property {number} currentPos
|
59
|
+
* @property {number} objectTypeLookupSize
|
60
|
+
* @property {number} currentPosTypeLookup
|
61
|
+
*/
|
62
|
+
|
46
63
|
/**
|
47
64
|
* @typedef {object} ObjectSerializerContext
|
48
|
-
* @property {
|
49
|
-
* @property {(
|
50
|
-
* @property {(
|
51
|
-
* @property {
|
65
|
+
* @property {(value: any) => void} write
|
66
|
+
* @property {(value: any) => void} setCircularReference
|
67
|
+
* @property {() => ObjectSerializerSnapshot} snapshot
|
68
|
+
* @property {(snapshot: ObjectSerializerSnapshot) => void} rollback
|
69
|
+
* @property {((item: any) => void)=} writeLazy
|
70
|
+
* @property {((item: any, obj?: TODO) => (() => Promise<any> | any))=} writeSeparate
|
52
71
|
*/
|
53
72
|
|
54
73
|
/**
|
55
74
|
* @typedef {object} ObjectDeserializerContext
|
56
|
-
* @property {
|
57
|
-
* @property {
|
75
|
+
* @property {() => any} read
|
76
|
+
* @property {(value: any) => void} setCircularReference
|
58
77
|
*/
|
59
78
|
|
60
79
|
/**
|
61
80
|
* @typedef {object} ObjectSerializer
|
62
|
-
* @property {
|
63
|
-
* @property {
|
81
|
+
* @property {(value: any, context: ObjectSerializerContext) => void} serialize
|
82
|
+
* @property {(context: ObjectDeserializerContext) => any} deserialize
|
64
83
|
*/
|
65
84
|
|
66
85
|
/**
|
@@ -109,7 +128,10 @@ const ESCAPE_UNDEFINED = false;
|
|
109
128
|
|
110
129
|
const CURRENT_VERSION = 2;
|
111
130
|
|
112
|
-
/** @
|
131
|
+
/** @typedef {{ request?: string, name?: string | number | null, serializer?: ObjectSerializer }} SerializerConfig */
|
132
|
+
/** @typedef {{ request?: string, name?: string | number | null, serializer: ObjectSerializer }} SerializerConfigWithSerializer */
|
133
|
+
|
134
|
+
/** @type {Map<Constructor, SerializerConfig>} */
|
113
135
|
const serializers = new Map();
|
114
136
|
/** @type {Map<string | number, ObjectSerializer>} */
|
115
137
|
const serializerInversed = new Map();
|
@@ -119,6 +141,8 @@ const loadedRequests = new Set();
|
|
119
141
|
|
120
142
|
const NOT_SERIALIZABLE = {};
|
121
143
|
|
144
|
+
/** @typedef {TODO} Item */
|
145
|
+
|
122
146
|
const jsTypes = new Map();
|
123
147
|
jsTypes.set(Object, new PlainObjectSerializer());
|
124
148
|
jsTypes.set(Array, new ArraySerializer());
|
@@ -134,14 +158,14 @@ jsTypes.set(ReferenceError, new ErrorObjectSerializer(ReferenceError));
|
|
134
158
|
jsTypes.set(SyntaxError, new ErrorObjectSerializer(SyntaxError));
|
135
159
|
jsTypes.set(TypeError, new ErrorObjectSerializer(TypeError));
|
136
160
|
|
137
|
-
// If in a sandboxed environment (e.
|
138
|
-
// real Object and Array types to. These types may occur in the wild too, e.
|
161
|
+
// If in a sandboxed environment (e.g. jest), this escapes the sandbox and registers
|
162
|
+
// real Object and Array types to. These types may occur in the wild too, e.g. when
|
139
163
|
// using Structured Clone in postMessage.
|
140
164
|
// eslint-disable-next-line n/exports-style
|
141
165
|
if (exports.constructor !== Object) {
|
142
|
-
// eslint-disable-next-line
|
143
|
-
const Obj = /** @type {
|
144
|
-
const Fn = /** @type {
|
166
|
+
// eslint-disable-next-line n/exports-style
|
167
|
+
const Obj = /** @type {ObjectConstructor} */ (exports.constructor);
|
168
|
+
const Fn = /** @type {FunctionConstructor} */ (Obj.constructor);
|
145
169
|
for (const [type, config] of Array.from(jsTypes)) {
|
146
170
|
if (type) {
|
147
171
|
const Type = new Fn(`return ${type.name};`)();
|
@@ -178,10 +202,10 @@ const loaders = new Map();
|
|
178
202
|
*/
|
179
203
|
class ObjectMiddleware extends SerializerMiddleware {
|
180
204
|
/**
|
181
|
-
* @param {
|
205
|
+
* @param {(context: ObjectSerializerContext | ObjectDeserializerContext) => void} extendContext context extensions
|
182
206
|
* @param {string | Hash} hashFunction hash function to use
|
183
207
|
*/
|
184
|
-
constructor(extendContext, hashFunction =
|
208
|
+
constructor(extendContext, hashFunction = DEFAULTS.HASH_FUNCTION) {
|
185
209
|
super();
|
186
210
|
this.extendContext = extendContext;
|
187
211
|
this._hashFunction = hashFunction;
|
@@ -189,7 +213,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
189
213
|
|
190
214
|
/**
|
191
215
|
* @param {RegExp} regExp RegExp for which the request is tested
|
192
|
-
* @param {
|
216
|
+
* @param {(request: string) => boolean} loader loader to load the request, returns true when successful
|
193
217
|
* @returns {void}
|
194
218
|
*/
|
195
219
|
static registerLoader(regExp, loader) {
|
@@ -241,6 +265,10 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
241
265
|
serializers.set(Constructor, NOT_SERIALIZABLE);
|
242
266
|
}
|
243
267
|
|
268
|
+
/**
|
269
|
+
* @param {TODO} object for serialization
|
270
|
+
* @returns {SerializerConfigWithSerializer} Serializer config
|
271
|
+
*/
|
244
272
|
static getSerializerFor(object) {
|
245
273
|
const proto = Object.getPrototypeOf(object);
|
246
274
|
let c;
|
@@ -260,12 +288,12 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
260
288
|
if (!config) throw new Error(`No serializer registered for ${c.name}`);
|
261
289
|
if (config === NOT_SERIALIZABLE) throw NOT_SERIALIZABLE;
|
262
290
|
|
263
|
-
return config;
|
291
|
+
return /** @type {SerializerConfigWithSerializer} */ (config);
|
264
292
|
}
|
265
293
|
|
266
294
|
/**
|
267
295
|
* @param {string} request request
|
268
|
-
* @param {
|
296
|
+
* @param {string} name name
|
269
297
|
* @returns {ObjectSerializer} serializer
|
270
298
|
*/
|
271
299
|
static getDeserializerFor(request, name) {
|
@@ -292,14 +320,18 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
292
320
|
|
293
321
|
/**
|
294
322
|
* @param {DeserializedType} data data
|
295
|
-
* @param {
|
296
|
-
* @returns {SerializedType|Promise<SerializedType>} serialized data
|
323
|
+
* @param {Context} context context object
|
324
|
+
* @returns {SerializedType | Promise<SerializedType> | null} serialized data
|
297
325
|
*/
|
298
326
|
serialize(data, context) {
|
299
|
-
/** @type {
|
327
|
+
/** @type {Item[]} */
|
300
328
|
let result = [CURRENT_VERSION];
|
301
329
|
let currentPos = 0;
|
330
|
+
/** @type {Map<Item, number>} */
|
302
331
|
let referenceable = new Map();
|
332
|
+
/**
|
333
|
+
* @param {Item} item referenceable item
|
334
|
+
*/
|
303
335
|
const addReferenceable = item => {
|
304
336
|
referenceable.set(item, currentPos++);
|
305
337
|
};
|
@@ -368,6 +400,10 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
368
400
|
let currentPosTypeLookup = 0;
|
369
401
|
let objectTypeLookup = new Map();
|
370
402
|
const cycleStack = new Set();
|
403
|
+
/**
|
404
|
+
* @param {Item} item item to stack
|
405
|
+
* @returns {string} stack
|
406
|
+
*/
|
371
407
|
const stackToString = item => {
|
372
408
|
const arr = Array.from(cycleStack);
|
373
409
|
arr.push(item);
|
@@ -411,15 +447,16 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
411
447
|
try {
|
412
448
|
return `${item}`;
|
413
449
|
} catch (err) {
|
414
|
-
return `(${err.message})`;
|
450
|
+
return `(${/** @type {Error} */ (err).message})`;
|
415
451
|
}
|
416
452
|
})
|
417
453
|
.join(" -> ");
|
418
454
|
};
|
419
455
|
/** @type {WeakSet<Error>} */
|
420
456
|
let hasDebugInfoAttached;
|
457
|
+
/** @type {ObjectSerializerContext} */
|
421
458
|
let ctx = {
|
422
|
-
write(value
|
459
|
+
write(value) {
|
423
460
|
try {
|
424
461
|
process(value);
|
425
462
|
} catch (err) {
|
@@ -459,6 +496,9 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
459
496
|
...context
|
460
497
|
};
|
461
498
|
this.extendContext(ctx);
|
499
|
+
/**
|
500
|
+
* @param {Item} item item to serialize
|
501
|
+
*/
|
462
502
|
const process = item => {
|
463
503
|
if (Buffer.isBuffer(item)) {
|
464
504
|
// check if we can emit a reference
|
@@ -599,8 +639,8 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
599
639
|
|
600
640
|
/**
|
601
641
|
* @param {SerializedType} data data
|
602
|
-
* @param {
|
603
|
-
* @returns {DeserializedType|Promise<DeserializedType>} deserialized data
|
642
|
+
* @param {Context} context context object
|
643
|
+
* @returns {DeserializedType | Promise<DeserializedType>} deserialized data
|
604
644
|
*/
|
605
645
|
deserialize(data, context) {
|
606
646
|
let currentDataPos = 0;
|
@@ -615,14 +655,20 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
615
655
|
throw new Error("Version mismatch, serializer changed");
|
616
656
|
|
617
657
|
let currentPos = 0;
|
658
|
+
/** @type {Item[]} */
|
618
659
|
let referenceable = [];
|
660
|
+
/**
|
661
|
+
* @param {Item} item referenceable item
|
662
|
+
*/
|
619
663
|
const addReferenceable = item => {
|
620
664
|
referenceable.push(item);
|
621
665
|
currentPos++;
|
622
666
|
};
|
623
667
|
let currentPosTypeLookup = 0;
|
668
|
+
/** @type {ObjectSerializer[]} */
|
624
669
|
let objectTypeLookup = [];
|
625
670
|
let result = [];
|
671
|
+
/** @type {ObjectDeserializerContext} */
|
626
672
|
let ctx = {
|
627
673
|
read() {
|
628
674
|
return decodeValue();
|
@@ -745,8 +791,11 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
745
791
|
return item;
|
746
792
|
} else if (typeof item === "function") {
|
747
793
|
return SerializerMiddleware.deserializeLazy(
|
748
|
-
|
749
|
-
|
794
|
+
/** @type {LazyFunction<SerializedType>} */
|
795
|
+
(item),
|
796
|
+
data =>
|
797
|
+
/** @type {[DeserializedType]} */
|
798
|
+
(this.deserialize(data, context))[0]
|
750
799
|
);
|
751
800
|
} else {
|
752
801
|
return item;
|
@@ -7,7 +7,7 @@
|
|
7
7
|
/** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
8
8
|
/** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
9
9
|
|
10
|
-
/** @typedef {
|
10
|
+
/** @typedef {EXPECTED_FUNCTION} CacheAssoc */
|
11
11
|
|
12
12
|
/**
|
13
13
|
* @template T
|
@@ -4,6 +4,8 @@
|
|
4
4
|
|
5
5
|
"use strict";
|
6
6
|
|
7
|
+
/** @typedef {import("./SerializerMiddleware").Context} Context */
|
8
|
+
|
7
9
|
/**
|
8
10
|
* @template T, K
|
9
11
|
* @typedef {import("./SerializerMiddleware")<T, K>} SerializerMiddleware
|
@@ -12,7 +14,7 @@
|
|
12
14
|
class Serializer {
|
13
15
|
/**
|
14
16
|
* @param {SerializerMiddleware<any, any>[]} middlewares serializer middlewares
|
15
|
-
* @param {
|
17
|
+
* @param {Context} [context] context
|
16
18
|
*/
|
17
19
|
constructor(middlewares, context) {
|
18
20
|
this.serializeMiddlewares = middlewares.slice();
|
@@ -21,16 +23,18 @@ class Serializer {
|
|
21
23
|
}
|
22
24
|
|
23
25
|
/**
|
24
|
-
* @param {
|
25
|
-
* @param {
|
26
|
-
* @returns {Promise<
|
26
|
+
* @param {TODO | Promise<TODO>} obj object
|
27
|
+
* @param {Context} context context object
|
28
|
+
* @returns {Promise<TODO>} result
|
27
29
|
*/
|
28
30
|
serialize(obj, context) {
|
29
31
|
const ctx = { ...context, ...this.context };
|
30
32
|
let current = obj;
|
31
33
|
for (const middleware of this.serializeMiddlewares) {
|
32
34
|
if (current && typeof current.then === "function") {
|
33
|
-
current =
|
35
|
+
current =
|
36
|
+
/** @type {Promise<TODO>} */
|
37
|
+
(current).then(data => data && middleware.serialize(data, ctx));
|
34
38
|
} else if (current) {
|
35
39
|
try {
|
36
40
|
current = middleware.serialize(current, ctx);
|
@@ -43,18 +47,19 @@ class Serializer {
|
|
43
47
|
}
|
44
48
|
|
45
49
|
/**
|
46
|
-
* @param {
|
47
|
-
* @param {
|
48
|
-
* @returns {Promise<
|
50
|
+
* @param {TODO | Promise<TODO>} value value
|
51
|
+
* @param {Context} context object
|
52
|
+
* @returns {Promise<TODO>} result
|
49
53
|
*/
|
50
54
|
deserialize(value, context) {
|
51
55
|
const ctx = { ...context, ...this.context };
|
52
|
-
/** @type {any} */
|
53
56
|
let current = value;
|
54
57
|
for (const middleware of this.deserializeMiddlewares) {
|
55
58
|
current =
|
56
59
|
current && typeof current.then === "function"
|
57
|
-
? current.then(data =>
|
60
|
+
? /** @type {Promise<TODO>} */ (current).then(data =>
|
61
|
+
middleware.deserialize(data, ctx)
|
62
|
+
)
|
58
63
|
: middleware.deserialize(current, ctx);
|
59
64
|
}
|
60
65
|
return current;
|