[6a3a178] | 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 |
|
---|
| 15 | const getBinaryMiddleware = memoize(() =>
|
---|
| 16 | require("../serialization/BinaryMiddleware")
|
---|
| 17 | );
|
---|
| 18 | const getObjectMiddleware = memoize(() =>
|
---|
| 19 | require("../serialization/ObjectMiddleware")
|
---|
| 20 | );
|
---|
| 21 | const getSingleItemMiddleware = memoize(() =>
|
---|
| 22 | require("../serialization/SingleItemMiddleware")
|
---|
| 23 | );
|
---|
| 24 | const getSerializer = memoize(() => require("../serialization/Serializer"));
|
---|
| 25 | const getSerializerMiddleware = memoize(() =>
|
---|
| 26 | require("../serialization/SerializerMiddleware")
|
---|
| 27 | );
|
---|
| 28 |
|
---|
| 29 | const getBinaryMiddlewareInstance = memoize(
|
---|
| 30 | () => new (getBinaryMiddleware())()
|
---|
| 31 | );
|
---|
| 32 |
|
---|
| 33 | const registerSerializers = memoize(() => {
|
---|
| 34 | require("./registerExternalSerializer");
|
---|
| 35 |
|
---|
| 36 | // Load internal paths with a relative require
|
---|
| 37 | // This allows bundling all internal serializers
|
---|
| 38 | const internalSerializables = require("./internalSerializables");
|
---|
| 39 | getObjectMiddleware().registerLoader(/^webpack\/lib\//, req => {
|
---|
| 40 | const loader = internalSerializables[req.slice("webpack/lib/".length)];
|
---|
| 41 | if (loader) {
|
---|
| 42 | loader();
|
---|
| 43 | } else {
|
---|
| 44 | console.warn(`${req} not found in internalSerializables`);
|
---|
| 45 | }
|
---|
| 46 | return true;
|
---|
| 47 | });
|
---|
| 48 | });
|
---|
| 49 |
|
---|
| 50 | /** @type {Serializer} */
|
---|
| 51 | let buffersSerializer;
|
---|
| 52 |
|
---|
| 53 | // Expose serialization API
|
---|
| 54 | module.exports = {
|
---|
| 55 | get register() {
|
---|
| 56 | return getObjectMiddleware().register;
|
---|
| 57 | },
|
---|
| 58 | get registerLoader() {
|
---|
| 59 | return getObjectMiddleware().registerLoader;
|
---|
| 60 | },
|
---|
| 61 | get registerNotSerializable() {
|
---|
| 62 | return getObjectMiddleware().registerNotSerializable;
|
---|
| 63 | },
|
---|
| 64 | get NOT_SERIALIZABLE() {
|
---|
| 65 | return getObjectMiddleware().NOT_SERIALIZABLE;
|
---|
| 66 | },
|
---|
| 67 | /** @type {MEASURE_START_OPERATION} */
|
---|
| 68 | get MEASURE_START_OPERATION() {
|
---|
| 69 | return getBinaryMiddleware().MEASURE_START_OPERATION;
|
---|
| 70 | },
|
---|
| 71 | /** @type {MEASURE_END_OPERATION} */
|
---|
| 72 | get MEASURE_END_OPERATION() {
|
---|
| 73 | return getBinaryMiddleware().MEASURE_END_OPERATION;
|
---|
| 74 | },
|
---|
| 75 | get buffersSerializer() {
|
---|
| 76 | if (buffersSerializer !== undefined) return buffersSerializer;
|
---|
| 77 | registerSerializers();
|
---|
| 78 | const Serializer = getSerializer();
|
---|
| 79 | const binaryMiddleware = getBinaryMiddlewareInstance();
|
---|
| 80 | const SerializerMiddleware = getSerializerMiddleware();
|
---|
| 81 | const SingleItemMiddleware = getSingleItemMiddleware();
|
---|
| 82 | return (buffersSerializer = new Serializer([
|
---|
| 83 | new SingleItemMiddleware(),
|
---|
| 84 | new (getObjectMiddleware())(context => {
|
---|
| 85 | if (context.write) {
|
---|
| 86 | context.writeLazy = value => {
|
---|
| 87 | context.write(
|
---|
| 88 | SerializerMiddleware.createLazy(value, binaryMiddleware)
|
---|
| 89 | );
|
---|
| 90 | };
|
---|
| 91 | }
|
---|
| 92 | }),
|
---|
| 93 | binaryMiddleware
|
---|
| 94 | ]));
|
---|
| 95 | },
|
---|
| 96 | createFileSerializer: fs => {
|
---|
| 97 | registerSerializers();
|
---|
| 98 | const Serializer = getSerializer();
|
---|
| 99 | const FileMiddleware = require("../serialization/FileMiddleware");
|
---|
| 100 | const fileMiddleware = new FileMiddleware(fs);
|
---|
| 101 | const binaryMiddleware = getBinaryMiddlewareInstance();
|
---|
| 102 | const SerializerMiddleware = getSerializerMiddleware();
|
---|
| 103 | const SingleItemMiddleware = getSingleItemMiddleware();
|
---|
| 104 | return new Serializer([
|
---|
| 105 | new SingleItemMiddleware(),
|
---|
| 106 | new (getObjectMiddleware())(context => {
|
---|
| 107 | if (context.write) {
|
---|
| 108 | context.writeLazy = value => {
|
---|
| 109 | context.write(
|
---|
| 110 | SerializerMiddleware.createLazy(value, binaryMiddleware)
|
---|
| 111 | );
|
---|
| 112 | };
|
---|
| 113 | context.writeSeparate = (value, options) => {
|
---|
| 114 | context.write(
|
---|
| 115 | SerializerMiddleware.createLazy(value, fileMiddleware, options)
|
---|
| 116 | );
|
---|
| 117 | };
|
---|
| 118 | }
|
---|
| 119 | }),
|
---|
| 120 | binaryMiddleware,
|
---|
| 121 | fileMiddleware
|
---|
| 122 | ]);
|
---|
| 123 | }
|
---|
| 124 | };
|
---|