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