serialization.js 4.3 KB

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