| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const { DEFAULTS } = require("../config/defaults");
|
|---|
| 8 | const memoize = require("./memoize");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("../serialization/BinaryMiddleware").MEASURE_END_OPERATION_TYPE} MEASURE_END_OPERATION */
|
|---|
| 11 | /** @typedef {import("../serialization/BinaryMiddleware").MEASURE_START_OPERATION_TYPE} MEASURE_START_OPERATION */
|
|---|
| 12 | /** @typedef {import("../util/Hash").HashFunction} HashFunction */
|
|---|
| 13 | /** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Defines the serializer type used by this module.
|
|---|
| 17 | * @template D, S, C
|
|---|
| 18 | * @typedef {import("../serialization/Serializer")<D, S, C>} Serializer
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | const getBinaryMiddleware = memoize(() =>
|
|---|
| 22 | require("../serialization/BinaryMiddleware")
|
|---|
| 23 | );
|
|---|
| 24 | const getObjectMiddleware = memoize(() =>
|
|---|
| 25 | require("../serialization/ObjectMiddleware")
|
|---|
| 26 | );
|
|---|
| 27 | const getSingleItemMiddleware = memoize(() =>
|
|---|
| 28 | require("../serialization/SingleItemMiddleware")
|
|---|
| 29 | );
|
|---|
| 30 | const getSerializer = memoize(() => require("../serialization/Serializer"));
|
|---|
| 31 | const getSerializerMiddleware = memoize(() =>
|
|---|
| 32 | require("../serialization/SerializerMiddleware")
|
|---|
| 33 | );
|
|---|
| 34 |
|
|---|
| 35 | const getBinaryMiddlewareInstance = memoize(
|
|---|
| 36 | () => new (getBinaryMiddleware())()
|
|---|
| 37 | );
|
|---|
| 38 |
|
|---|
| 39 | const registerSerializers = memoize(() => {
|
|---|
| 40 | require("./registerExternalSerializer");
|
|---|
| 41 |
|
|---|
| 42 | // Load internal paths with a relative require
|
|---|
| 43 | // This allows bundling all internal serializers
|
|---|
| 44 | const internalSerializables = require("./internalSerializables");
|
|---|
| 45 |
|
|---|
| 46 | getObjectMiddleware().registerLoader(/^webpack\/lib\//, (req) => {
|
|---|
| 47 | const loader =
|
|---|
| 48 | internalSerializables[
|
|---|
| 49 | /** @type {keyof import("./internalSerializables")} */
|
|---|
| 50 | (req.slice("webpack/lib/".length))
|
|---|
| 51 | ];
|
|---|
| 52 | if (loader) {
|
|---|
| 53 | loader();
|
|---|
| 54 | } else {
|
|---|
| 55 | // eslint-disable-next-line no-console
|
|---|
| 56 | console.warn(`${req} not found in internalSerializables`);
|
|---|
| 57 | }
|
|---|
| 58 | return true;
|
|---|
| 59 | });
|
|---|
| 60 | });
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * @type {Serializer<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
|
|---|
| 64 | */
|
|---|
| 65 | let buffersSerializer;
|
|---|
| 66 |
|
|---|
| 67 | // Expose serialization API
|
|---|
| 68 | module.exports = {
|
|---|
| 69 | get register() {
|
|---|
| 70 | return getObjectMiddleware().register;
|
|---|
| 71 | },
|
|---|
| 72 | get registerLoader() {
|
|---|
| 73 | return getObjectMiddleware().registerLoader;
|
|---|
| 74 | },
|
|---|
| 75 | get registerNotSerializable() {
|
|---|
| 76 | return getObjectMiddleware().registerNotSerializable;
|
|---|
| 77 | },
|
|---|
| 78 | get NOT_SERIALIZABLE() {
|
|---|
| 79 | return getObjectMiddleware().NOT_SERIALIZABLE;
|
|---|
| 80 | },
|
|---|
| 81 | /** @type {MEASURE_START_OPERATION} */
|
|---|
| 82 | get MEASURE_START_OPERATION() {
|
|---|
| 83 | return getBinaryMiddleware().MEASURE_START_OPERATION;
|
|---|
| 84 | },
|
|---|
| 85 | /** @type {MEASURE_END_OPERATION} */
|
|---|
| 86 | get MEASURE_END_OPERATION() {
|
|---|
| 87 | return getBinaryMiddleware().MEASURE_END_OPERATION;
|
|---|
| 88 | },
|
|---|
| 89 | get buffersSerializer() {
|
|---|
| 90 | if (buffersSerializer !== undefined) return buffersSerializer;
|
|---|
| 91 | registerSerializers();
|
|---|
| 92 | const Serializer = getSerializer();
|
|---|
| 93 | const binaryMiddleware = getBinaryMiddlewareInstance();
|
|---|
| 94 | const SerializerMiddleware = getSerializerMiddleware();
|
|---|
| 95 | const SingleItemMiddleware = getSingleItemMiddleware();
|
|---|
| 96 | return /** @type {Serializer<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} */ (
|
|---|
| 97 | buffersSerializer = new Serializer([
|
|---|
| 98 | new SingleItemMiddleware(),
|
|---|
| 99 | new (getObjectMiddleware())((context) => {
|
|---|
| 100 | if ("write" in context) {
|
|---|
| 101 | context.writeLazy = (value) => {
|
|---|
| 102 | context.write(
|
|---|
| 103 | SerializerMiddleware.createLazy(value, binaryMiddleware)
|
|---|
| 104 | );
|
|---|
| 105 | };
|
|---|
| 106 | }
|
|---|
| 107 | }, DEFAULTS.HASH_FUNCTION),
|
|---|
| 108 | binaryMiddleware
|
|---|
| 109 | ])
|
|---|
| 110 | );
|
|---|
| 111 | },
|
|---|
| 112 | /**
|
|---|
| 113 | * Creates a file serializer.
|
|---|
| 114 | * @template D, S, C
|
|---|
| 115 | * @param {IntermediateFileSystem} fs filesystem
|
|---|
| 116 | * @param {HashFunction} hashFunction hash function to use
|
|---|
| 117 | * @returns {Serializer<D, S, C>} file serializer
|
|---|
| 118 | */
|
|---|
| 119 | createFileSerializer: (fs, hashFunction) => {
|
|---|
| 120 | registerSerializers();
|
|---|
| 121 | const Serializer = getSerializer();
|
|---|
| 122 |
|
|---|
| 123 | const FileMiddleware = require("../serialization/FileMiddleware");
|
|---|
| 124 |
|
|---|
| 125 | const fileMiddleware = new FileMiddleware(fs, hashFunction);
|
|---|
| 126 | const binaryMiddleware = getBinaryMiddlewareInstance();
|
|---|
| 127 | const SerializerMiddleware = getSerializerMiddleware();
|
|---|
| 128 | const SingleItemMiddleware = getSingleItemMiddleware();
|
|---|
| 129 | return /** @type {Serializer<D, S, C>} */ (
|
|---|
| 130 | new Serializer([
|
|---|
| 131 | new SingleItemMiddleware(),
|
|---|
| 132 | new (getObjectMiddleware())((context) => {
|
|---|
| 133 | if ("write" in context) {
|
|---|
| 134 | context.writeLazy = (value) => {
|
|---|
| 135 | context.write(
|
|---|
| 136 | SerializerMiddleware.createLazy(value, binaryMiddleware)
|
|---|
| 137 | );
|
|---|
| 138 | };
|
|---|
| 139 | context.writeSeparate = (value, options) => {
|
|---|
| 140 | const lazy = SerializerMiddleware.createLazy(
|
|---|
| 141 | value,
|
|---|
| 142 | fileMiddleware,
|
|---|
| 143 | options
|
|---|
| 144 | );
|
|---|
| 145 | context.write(lazy);
|
|---|
| 146 | return lazy;
|
|---|
| 147 | };
|
|---|
| 148 | }
|
|---|
| 149 | }, hashFunction),
|
|---|
| 150 | binaryMiddleware,
|
|---|
| 151 | fileMiddleware
|
|---|
| 152 | ])
|
|---|
| 153 | );
|
|---|
| 154 | }
|
|---|
| 155 | };
|
|---|