Files
klz-cables.com/.pnpm-store/v10/files/64/f9f3957dd63ef64d121bab49350b518f92687293ede9079edd3a326b209f4abb019612052b46eb03aa43dc79534ede3191cec98d9fd3d88a1d42d9289d65a9
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

92 lines
2.4 KiB
Plaintext

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { DEFAULTS } = require("../config/defaults");
const createHash = require("../util/createHash");
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {typeof import("../util/Hash")} HashConstructor */
/** @typedef {import("../util/Hash").HashFunction} HashFunction */
/**
* @typedef {object} HashableObject
* @property {(hash: Hash) => void} updateHash
*/
class LazyHashedEtag {
/**
* @param {HashableObject} obj object with updateHash method
* @param {HashFunction} hashFunction the hash function to use
*/
constructor(obj, hashFunction = DEFAULTS.HASH_FUNCTION) {
/** @type {HashableObject} */
this._obj = obj;
/** @type {undefined | string} */
this._hash = undefined;
/** @type {HashFunction} */
this._hashFunction = hashFunction;
}
/**
* @returns {string} hash of object
*/
toString() {
if (this._hash === undefined) {
const hash = createHash(this._hashFunction);
this._obj.updateHash(hash);
this._hash = hash.digest("base64");
}
return this._hash;
}
}
/** @typedef {WeakMap<HashableObject, LazyHashedEtag>} InnerCache */
/** @type {Map<HashFunction, InnerCache>} */
const mapStrings = new Map();
/** @type {WeakMap<HashConstructor, InnerCache>} */
const mapObjects = new WeakMap();
/**
* @param {HashableObject} obj object with updateHash method
* @param {HashFunction=} hashFunction the hash function to use
* @returns {LazyHashedEtag} etag
*/
const getter = (obj, hashFunction = DEFAULTS.HASH_FUNCTION) => {
/** @type {undefined | InnerCache} */
let innerMap;
if (typeof hashFunction === "string") {
innerMap = mapStrings.get(hashFunction);
if (innerMap === undefined) {
const newHash = new LazyHashedEtag(obj, hashFunction);
/** @type {InnerCache} */
innerMap = new WeakMap();
innerMap.set(obj, newHash);
mapStrings.set(hashFunction, innerMap);
return newHash;
}
} else {
innerMap = mapObjects.get(hashFunction);
if (innerMap === undefined) {
const newHash = new LazyHashedEtag(obj, hashFunction);
/** @type {InnerCache} */
innerMap = new WeakMap();
innerMap.set(obj, newHash);
mapObjects.set(hashFunction, innerMap);
return newHash;
}
}
const hash = innerMap.get(obj);
if (hash !== undefined) return hash;
const newHash = new LazyHashedEtag(obj, hashFunction);
innerMap.set(obj, newHash);
return newHash;
};
module.exports = getter;