serialization.js
3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
const memoize = require("./memoize");
/** @typedef {import("../serialization/BinaryMiddleware").MEASURE_END_OPERATION_TYPE} MEASURE_END_OPERATION */
/** @typedef {import("../serialization/BinaryMiddleware").MEASURE_START_OPERATION_TYPE} MEASURE_START_OPERATION */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("../serialization/Serializer")} Serializer */
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")
);
const getBinaryMiddlewareInstance = memoize(
() => new (getBinaryMiddleware())()
);
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 => {
const loader = internalSerializables[req.slice("webpack/lib/".length)];
if (loader) {
loader();
} else {
console.warn(`${req} not found in internalSerializables`);
}
return true;
});
});
/** @type {Serializer} */
let buffersSerializer;
// 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();
return (buffersSerializer = new Serializer([
new SingleItemMiddleware(),
new (getObjectMiddleware())(context => {
if (context.write) {
context.writeLazy = value => {
context.write(
SerializerMiddleware.createLazy(value, binaryMiddleware)
);
};
}
}, "md4"),
binaryMiddleware
]));
},
createFileSerializer: (fs, hashFunction) => {
registerSerializers();
const Serializer = getSerializer();
const FileMiddleware = require("../serialization/FileMiddleware");
const fileMiddleware = new FileMiddleware(fs, hashFunction);
const binaryMiddleware = getBinaryMiddlewareInstance();
const SerializerMiddleware = getSerializerMiddleware();
const SingleItemMiddleware = getSingleItemMiddleware();
return new Serializer([
new SingleItemMiddleware(),
new (getObjectMiddleware())(context => {
if (context.write) {
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
]);
}
};