serialization.js 3.9 KB

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