WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
node_modules/webpack/lib/serialization/ObjectMiddleware.js
@@ -18,7 +18,7 @@
const SetObjectSerializer = require("./SetObjectSerializer");
/** @typedef {import("../logging/Logger").Logger} Logger */
/** @typedef {typeof import("../util/Hash")} Hash */
/** @typedef {import("../util/Hash").HashFunction} HashFunction */
/** @typedef {import("./SerializerMiddleware").LazyOptions} LazyOptions */
/** @typedef {import("./types").ComplexSerializableType} ComplexSerializableType */
/** @typedef {import("./types").PrimitiveSerializableType} PrimitiveSerializableType */
@@ -48,6 +48,7 @@
*/
/**
 * Defines the object serializer snapshot type used by this module.
 * @typedef {object} ObjectSerializerSnapshot
 * @property {number} length
 * @property {number} cycleStackSize
@@ -60,6 +61,7 @@
/** @typedef {EXPECTED_OBJECT | string} ReferenceableItem */
/**
 * Defines the object serializer context type used by this module.
 * @typedef {object} ObjectSerializerContext
 * @property {(value: EXPECTED_ANY) => void} write
 * @property {(value: ReferenceableItem) => void} setCircularReference
@@ -70,18 +72,21 @@
 */
/**
 * Defines the object deserializer context type used by this module.
 * @typedef {object} ObjectDeserializerContext
 * @property {() => EXPECTED_ANY} read
 * @property {(value: ReferenceableItem) => void} setCircularReference
 */
/**
 * Defines the object serializer type used by this module.
 * @typedef {object} ObjectSerializer
 * @property {(value: EXPECTED_ANY, context: ObjectSerializerContext) => void} serialize
 * @property {(context: ObjectDeserializerContext) => EXPECTED_ANY} deserialize
 */
/**
 * Updates set size using the provided set.
 * @template T
 * @param {Set<T>} set set
 * @param {number} size count of items to keep
@@ -96,6 +101,7 @@
};
/**
 * Updates map size using the provided map.
 * @template K, X
 * @param {Map<K, X>} map map
 * @param {number} size count of items to keep
@@ -110,8 +116,9 @@
};
/**
 * Returns hash.
 * @param {Buffer} buffer buffer
 * @param {string | Hash} hashFunction hash function to use
 * @param {HashFunction} hashFunction hash function to use
 * @returns {string} hash
 */
const toHash = (buffer, hashFunction) => {
@@ -157,11 +164,9 @@
jsTypes.set(SyntaxError, new ErrorObjectSerializer(SyntaxError));
jsTypes.set(TypeError, new ErrorObjectSerializer(TypeError));
// @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax
if (typeof AggregateError !== "undefined") {
   jsTypes.set(
      // @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
      // eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax
      AggregateError,
      new AggregateErrorSerializer()
@@ -209,21 +214,28 @@
/** @typedef {PrimitiveSerializableType[]} SerializedType */
/** @typedef {{ logger: Logger }} Context */
/** @typedef {(context: ObjectSerializerContext | ObjectDeserializerContext) => void} ExtendContext */
/**
 * Represents ObjectMiddleware.
 * @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
 */
class ObjectMiddleware extends SerializerMiddleware {
   /**
    * @param {(context: ObjectSerializerContext | ObjectDeserializerContext) => void} extendContext context extensions
    * @param {string | Hash} hashFunction hash function to use
    * Creates an instance of ObjectMiddleware.
    * @param {ExtendContext} extendContext context extensions
    * @param {HashFunction} hashFunction hash function to use
    */
   constructor(extendContext, hashFunction = DEFAULTS.HASH_FUNCTION) {
      super();
      /** @type {ExtendContext} */
      this.extendContext = extendContext;
      /** @type {HashFunction} */
      this._hashFunction = hashFunction;
   }
   /**
    * Processes the provided reg exp.
    * @param {RegExp} regExp RegExp for which the request is tested
    * @param {(request: string) => boolean} loader loader to load the request, returns true when successful
    * @returns {void}
@@ -233,6 +245,7 @@
   }
   /**
    * Processes the provided constructor.
    * @param {Constructor} Constructor the constructor
    * @param {string} request the request which will be required when deserializing
    * @param {string | null} name the name to make multiple serializer unique when sharing a request
@@ -264,6 +277,7 @@
   }
   /**
    * Register not serializable.
    * @param {Constructor} Constructor the constructor
    * @returns {void}
    */
@@ -278,11 +292,13 @@
   }
   /**
    * @param {Constructor} object for serialization
    * Gets serializer for.
    * @param {EXPECTED_ANY} object for serialization
    * @returns {SerializerConfigWithSerializer} Serializer config
    */
   static getSerializerFor(object) {
      const proto = Object.getPrototypeOf(object);
      /** @type {null | Constructor} */
      let c;
      if (proto === null) {
         // Object created with Object.create(null)
@@ -297,13 +313,18 @@
      }
      const config = serializers.get(c);
      if (!config) throw new Error(`No serializer registered for ${c.name}`);
      if (!config) {
         throw new Error(
            `No serializer registered for ${/** @type {Constructor} */ (c).name}`
         );
      }
      if (config === NOT_SERIALIZABLE) throw NOT_SERIALIZABLE;
      return /** @type {SerializerConfigWithSerializer} */ (config);
   }
   /**
    * Gets deserializer for.
    * @param {string} request request
    * @param {string} name name
    * @returns {ObjectSerializer} serializer
@@ -320,6 +341,7 @@
   }
   /**
    * Get deserializer for without error.
    * @param {string} request request
    * @param {string} name name
    * @returns {ObjectSerializer | undefined} serializer
@@ -331,6 +353,7 @@
   }
   /**
    * Serializes this instance into the provided serializer context.
    * @param {DeserializedType} data data
    * @param {Context} context context object
    * @returns {SerializedType | Promise<SerializedType> | null} serialized data
@@ -342,13 +365,16 @@
      /** @type {Map<ReferenceableItem, number>} */
      let referenceable = new Map();
      /**
       * Adds referenceable.
       * @param {ReferenceableItem} item referenceable item
       */
      const addReferenceable = (item) => {
         referenceable.set(item, currentPos++);
      };
      /** @type {Map<number, Buffer | [Buffer, Buffer] | Map<string, Buffer>>} */
      let bufferDedupeMap = new Map();
      /**
       * Returns deduped buffer.
       * @param {Buffer} buf buffer
       * @returns {Buffer} deduped buffer
       */
@@ -368,6 +394,7 @@
               return buf;
            }
            const hash = toHash(entry, this._hashFunction);
            /** @type {Map<string, Buffer>} */
            const newMap = new Map();
            newMap.set(hash, entry);
            bufferDedupeMap.set(len, newMap);
@@ -386,8 +413,10 @@
               entry.push(buf);
               return buf;
            }
            /** @type {Map<string, Buffer>} */
            const newMap = new Map();
            const hash = toHash(buf, this._hashFunction);
            /** @type {undefined | Buffer} */
            let found;
            for (const item of entry) {
               const itemHash = toHash(item, this._hashFunction);
@@ -410,9 +439,12 @@
         return buf;
      };
      let currentPosTypeLookup = 0;
      /** @type {Map<ComplexSerializableType, number>} */
      let objectTypeLookup = new Map();
      /** @type {Set<ComplexSerializableType>} */
      const cycleStack = new Set();
      /**
       * Returns stack.
       * @param {ComplexSerializableType} item item to stack
       * @returns {string} stack
       */
@@ -443,12 +475,18 @@
                     if (item.constructor === Object) {
                        return `Object { ${Object.keys(item).join(", ")} }`;
                     }
                     if (item.constructor === Map) return `Map { ${item.size} items }`;
                     if (item.constructor === Array) {
                        return `Array { ${item.length} items }`;
                     if (item.constructor === Map) {
                        return `Map { ${/** @type {Map<EXPECTED_ANY, EXPECTED_ANY>} */ (item).size} items }`;
                     }
                     if (item.constructor === Set) return `Set { ${item.size} items }`;
                     if (item.constructor === RegExp) return item.toString();
                     if (item.constructor === Array) {
                        return `Array { ${/** @type {EXPECTED_ANY[]} */ (item).length} items }`;
                     }
                     if (item.constructor === Set) {
                        return `Set { ${/** @type {Set<EXPECTED_ANY>} */ (item).size} items }`;
                     }
                     if (item.constructor === RegExp) {
                        return /** @type {RegExp} */ (item).toString();
                     }
                     return `${item.constructor.name}`;
                  }
                  return `Object [null prototype] { ${Object.keys(item).join(
@@ -466,7 +504,7 @@
            })
            .join(" -> ");
      };
      /** @type {WeakSet<Error>} */
      /** @type {undefined | WeakSet<Error>} */
      let hasDebugInfoAttached;
      /** @type {ObjectSerializerContext} */
      let ctx = {
@@ -512,6 +550,7 @@
      };
      this.extendContext(ctx);
      /**
       * Processes the provided item.
       * @param {ComplexSerializableType} item item to serialize
       */
      const process = (item) => {
@@ -662,6 +701,7 @@
   }
   /**
    * Restores this instance from the provided deserializer context.
    * @param {SerializedType} data data
    * @param {Context} context context object
    * @returns {DeserializedType | Promise<DeserializedType>} deserialized data
@@ -684,6 +724,7 @@
      /** @type {ReferenceableItem[]} */
      let referenceable = [];
      /**
       * Adds referenceable.
       * @param {ReferenceableItem} item referenceable item
       */
      const addReferenceable = (item) => {
@@ -707,6 +748,7 @@
      };
      this.extendContext(ctx);
      /**
       * Decodes the provided value.
       * @returns {ComplexSerializableType} deserialize value
       */
      const decodeValue = () => {
@@ -725,6 +767,7 @@
               );
            } else {
               const request = nextItem;
               /** @type {undefined | ObjectSerializer} */
               let serializer;
               if (typeof request === "number") {
@@ -790,6 +833,7 @@
                  // As this is only for error handling, we omit creating a Map for
                  // faster access to this information, as this would affect performance
                  // in the good case
                  /** @type {undefined | [Constructor | null, SerializerConfig]} */
                  let serializerEntry;
                  for (const entry of serializers) {
                     if (entry[1].serializer === serializer) {