| 
									
										
										
										
											2018-12-28 20:18:02 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 03:39:02 +08:00
										 |  |  | const { DEFAULTS } = require("../config/defaults"); | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | const memoize = require("./memoize"); | 
					
						
							| 
									
										
										
										
											2018-12-28 20:18:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | /** @typedef {import("../serialization/BinaryMiddleware").MEASURE_END_OPERATION_TYPE} MEASURE_END_OPERATION */ | 
					
						
							|  |  |  | /** @typedef {import("../serialization/BinaryMiddleware").MEASURE_START_OPERATION_TYPE} MEASURE_START_OPERATION */ | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | /** @typedef {typeof import("../util/Hash")} Hash */ | 
					
						
							|  |  |  | /** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */ | 
					
						
							| 
									
										
										
										
											2018-12-28 21:48:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template D, S, C | 
					
						
							|  |  |  |  * @typedef {import("../serialization/Serializer")<D, S, C>} Serializer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | const getBinaryMiddleware = memoize(() => | 
					
						
							|  |  |  | 	require("../serialization/BinaryMiddleware") | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | const getObjectMiddleware = memoize(() => | 
					
						
							|  |  |  | 	require("../serialization/ObjectMiddleware") | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | const getSingleItemMiddleware = memoize(() => | 
					
						
							|  |  |  | 	require("../serialization/SingleItemMiddleware") | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | const getSerializer = memoize(() => require("../serialization/Serializer")); | 
					
						
							|  |  |  | const getSerializerMiddleware = memoize(() => | 
					
						
							|  |  |  | 	require("../serialization/SerializerMiddleware") | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2018-12-28 20:18:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | const getBinaryMiddlewareInstance = memoize( | 
					
						
							|  |  |  | 	() => new (getBinaryMiddleware())() | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | const registerSerializers = memoize(() => { | 
					
						
							|  |  |  | 	require("./registerExternalSerializer"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Load internal paths with a relative require
 | 
					
						
							|  |  |  | 	// This allows bundling all internal serializers
 | 
					
						
							|  |  |  | 	const internalSerializables = require("./internalSerializables"); | 
					
						
							|  |  |  | 	getObjectMiddleware().registerLoader(/^webpack\/lib\//, req => { | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | 		const loader = | 
					
						
							|  |  |  | 			internalSerializables[ | 
					
						
							|  |  |  | 				/** @type {keyof import("./internalSerializables")} */ | 
					
						
							|  |  |  | 				(req.slice("webpack/lib/".length)) | 
					
						
							|  |  |  | 			]; | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | 		if (loader) { | 
					
						
							|  |  |  | 			loader(); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2025-03-07 23:32:55 +08:00
										 |  |  | 			// eslint-disable-next-line no-console
 | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | 			console.warn(`${req} not found in internalSerializables`); | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2018-12-28 20:54:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @type {Serializer<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | let buffersSerializer; | 
					
						
							| 
									
										
										
										
											2018-12-28 20:54:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | // Expose serialization API
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  | 	get register() { | 
					
						
							|  |  |  | 		return getObjectMiddleware().register; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	get registerLoader() { | 
					
						
							|  |  |  | 		return getObjectMiddleware().registerLoader; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	get registerNotSerializable() { | 
					
						
							|  |  |  | 		return getObjectMiddleware().registerNotSerializable; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	get NOT_SERIALIZABLE() { | 
					
						
							|  |  |  | 		return getObjectMiddleware().NOT_SERIALIZABLE; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	/** @type {MEASURE_START_OPERATION} */ | 
					
						
							|  |  |  | 	get MEASURE_START_OPERATION() { | 
					
						
							|  |  |  | 		return getBinaryMiddleware().MEASURE_START_OPERATION; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	/** @type {MEASURE_END_OPERATION} */ | 
					
						
							|  |  |  | 	get MEASURE_END_OPERATION() { | 
					
						
							|  |  |  | 		return getBinaryMiddleware().MEASURE_END_OPERATION; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	get buffersSerializer() { | 
					
						
							|  |  |  | 		if (buffersSerializer !== undefined) return buffersSerializer; | 
					
						
							|  |  |  | 		registerSerializers(); | 
					
						
							|  |  |  | 		const Serializer = getSerializer(); | 
					
						
							|  |  |  | 		const binaryMiddleware = getBinaryMiddlewareInstance(); | 
					
						
							|  |  |  | 		const SerializerMiddleware = getSerializerMiddleware(); | 
					
						
							|  |  |  | 		const SingleItemMiddleware = getSingleItemMiddleware(); | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 		return /** @type {Serializer<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} */ ( | 
					
						
							|  |  |  | 			buffersSerializer = new Serializer([ | 
					
						
							|  |  |  | 				new SingleItemMiddleware(), | 
					
						
							|  |  |  | 				new (getObjectMiddleware())(context => { | 
					
						
							|  |  |  | 					if ("write" in context) { | 
					
						
							|  |  |  | 						context.writeLazy = value => { | 
					
						
							|  |  |  | 							context.write( | 
					
						
							|  |  |  | 								SerializerMiddleware.createLazy(value, binaryMiddleware) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}, DEFAULTS.HASH_FUNCTION), | 
					
						
							|  |  |  | 				binaryMiddleware | 
					
						
							|  |  |  | 			]) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 	 * @template D, S, C | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | 	 * @param {IntermediateFileSystem} fs filesystem | 
					
						
							|  |  |  | 	 * @param {string | Hash} hashFunction hash function to use | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 	 * @returns {Serializer<D, S, C>} file serializer | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-09-22 18:12:46 +08:00
										 |  |  | 	createFileSerializer: (fs, hashFunction) => { | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | 		registerSerializers(); | 
					
						
							|  |  |  | 		const Serializer = getSerializer(); | 
					
						
							|  |  |  | 		const FileMiddleware = require("../serialization/FileMiddleware"); | 
					
						
							| 
									
										
										
										
											2021-09-22 18:12:46 +08:00
										 |  |  | 		const fileMiddleware = new FileMiddleware(fs, hashFunction); | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | 		const binaryMiddleware = getBinaryMiddlewareInstance(); | 
					
						
							|  |  |  | 		const SerializerMiddleware = getSerializerMiddleware(); | 
					
						
							|  |  |  | 		const SingleItemMiddleware = getSingleItemMiddleware(); | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 		return /** @type {Serializer<D, S, C>} */ ( | 
					
						
							|  |  |  | 			new Serializer([ | 
					
						
							|  |  |  | 				new SingleItemMiddleware(), | 
					
						
							|  |  |  | 				new (getObjectMiddleware())(context => { | 
					
						
							|  |  |  | 					if ("write" in context) { | 
					
						
							|  |  |  | 						context.writeLazy = value => { | 
					
						
							|  |  |  | 							context.write( | 
					
						
							|  |  |  | 								SerializerMiddleware.createLazy(value, binaryMiddleware) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 						context.writeSeparate = (value, options) => { | 
					
						
							|  |  |  | 							const lazy = SerializerMiddleware.createLazy( | 
					
						
							|  |  |  | 								value, | 
					
						
							|  |  |  | 								fileMiddleware, | 
					
						
							|  |  |  | 								options | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							context.write(lazy); | 
					
						
							|  |  |  | 							return lazy; | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}, hashFunction), | 
					
						
							|  |  |  | 				binaryMiddleware, | 
					
						
							|  |  |  | 				fileMiddleware | 
					
						
							|  |  |  | 			]) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2019-10-11 16:01:08 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-26 21:53:23 +08:00
										 |  |  | }; |