webpack/lib/util/createHash.js

195 lines
5.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2019-07-17 22:02:33 +08:00
const Hash = require("./Hash");
2018-06-25 16:44:54 +08:00
2021-01-26 19:41:50 +08:00
const BULK_SIZE = 2000;
2021-01-26 19:41:50 +08:00
// We are using an object instead of a Map as this will stay static during the runtime
// so access to it can be optimized by v8
2024-06-11 21:09:50 +08:00
/** @type {{[key: string]: Map<string, string>}} */
2021-01-26 19:41:50 +08:00
const digestCaches = {};
2019-07-25 04:43:30 +08:00
/** @typedef {() => Hash} HashFactory */
2023-06-12 22:21:21 +08:00
class BulkUpdateDecorator extends Hash {
2019-07-10 17:42:34 +08:00
/**
2023-06-12 22:21:21 +08:00
* @param {Hash | HashFactory} hashOrFactory function to create a hash
2025-04-16 22:04:11 +08:00
* @param {string=} hashKey key for caching
2019-07-10 17:42:34 +08:00
*/
2019-07-25 04:43:30 +08:00
constructor(hashOrFactory, hashKey) {
2019-07-10 17:42:34 +08:00
super();
2019-07-25 04:43:30 +08:00
this.hashKey = hashKey;
if (typeof hashOrFactory === "function") {
this.hashFactory = hashOrFactory;
this.hash = undefined;
} else {
this.hashFactory = undefined;
this.hash = hashOrFactory;
}
this.buffer = "";
}
2019-07-10 17:42:34 +08:00
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
2019-07-10 17:42:34 +08:00
*/
update(data, inputEncoding) {
2018-02-25 09:00:20 +08:00
if (
inputEncoding !== undefined ||
typeof data !== "string" ||
data.length > BULK_SIZE
) {
2023-06-12 22:21:21 +08:00
if (this.hash === undefined)
this.hash = /** @type {HashFactory} */ (this.hashFactory)();
2018-02-25 09:00:20 +08:00
if (this.buffer.length > 0) {
2017-11-23 18:44:56 +08:00
this.hash.update(this.buffer);
this.buffer = "";
}
this.hash.update(data, inputEncoding);
} else {
this.buffer += data;
2018-02-25 09:00:20 +08:00
if (this.buffer.length > BULK_SIZE) {
2023-06-12 22:21:21 +08:00
if (this.hash === undefined)
this.hash = /** @type {HashFactory} */ (this.hashFactory)();
2017-11-23 18:44:56 +08:00
this.hash.update(this.buffer);
this.buffer = "";
}
}
return this;
}
2019-07-10 17:42:34 +08:00
/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string=} encoding encoding of the return value
2019-07-10 17:42:34 +08:00
* @returns {string|Buffer} digest
*/
digest(encoding) {
2021-01-26 19:41:50 +08:00
let digestCache;
2021-05-26 22:02:49 +08:00
const buffer = this.buffer;
2019-07-25 04:43:30 +08:00
if (this.hash === undefined) {
// short data for hash, we can use caching
2021-01-26 19:41:50 +08:00
const cacheKey = `${this.hashKey}-${encoding}`;
digestCache = digestCaches[cacheKey];
if (digestCache === undefined) {
digestCache = digestCaches[cacheKey] = new Map();
}
2021-05-26 22:02:49 +08:00
const cacheEntry = digestCache.get(buffer);
2019-07-25 04:43:30 +08:00
if (cacheEntry !== undefined) return cacheEntry;
2023-06-12 22:21:21 +08:00
this.hash = /** @type {HashFactory} */ (this.hashFactory)();
2019-07-25 04:43:30 +08:00
}
2021-05-26 22:02:49 +08:00
if (buffer.length > 0) {
this.hash.update(buffer);
2017-11-23 18:44:56 +08:00
}
2019-07-25 04:43:30 +08:00
const digestResult = this.hash.digest(encoding);
const result =
typeof digestResult === "string" ? digestResult : digestResult.toString();
2021-01-26 19:41:50 +08:00
if (digestCache !== undefined) {
2021-05-26 22:02:49 +08:00
digestCache.set(buffer, result);
2019-07-25 04:43:30 +08:00
}
return result;
}
}
2020-04-17 16:53:30 +08:00
/* istanbul ignore next */
class DebugHash extends Hash {
constructor() {
2019-07-10 17:42:34 +08:00
super();
2018-01-20 18:28:45 +08:00
this.string = "";
}
2019-07-10 17:42:34 +08:00
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
2019-07-10 17:42:34 +08:00
*/
update(data, inputEncoding) {
2018-02-25 09:00:20 +08:00
if (typeof data !== "string") data = data.toString("utf-8");
2023-04-10 20:13:07 +08:00
const prefix = Buffer.from("@webpack-debug-digest@").toString("hex");
if (data.startsWith(prefix)) {
data = Buffer.from(data.slice(prefix.length), "hex").toString();
2018-10-18 15:22:47 +08:00
}
2023-06-12 22:21:21 +08:00
this.string += `[${data}](${
/** @type {string} */ (new Error().stack).split("\n", 3)[2]
})\n`;
return this;
}
2019-07-10 17:42:34 +08:00
/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string=} encoding encoding of the return value
2019-07-10 17:42:34 +08:00
* @returns {string|Buffer} digest
*/
digest(encoding) {
2024-07-31 10:39:30 +08:00
return Buffer.from(`@webpack-debug-digest@${this.string}`).toString("hex");
}
}
2023-06-12 22:21:21 +08:00
/** @type {typeof import("crypto") | undefined} */
2024-07-31 06:15:03 +08:00
let crypto;
2023-06-12 22:21:21 +08:00
/** @type {typeof import("./hash/xxhash64") | undefined} */
2024-07-31 06:15:03 +08:00
let createXXHash64;
2023-06-12 22:21:21 +08:00
/** @type {typeof import("./hash/md4") | undefined} */
2024-07-31 06:15:03 +08:00
let createMd4;
2023-06-12 22:21:21 +08:00
/** @type {typeof import("./hash/BatchedHash") | undefined} */
2024-07-31 06:15:03 +08:00
let BatchedHash;
2018-12-20 23:30:00 +08:00
2024-08-09 01:03:17 +08:00
/** @typedef {string | typeof Hash} Algorithm */
2018-06-25 16:44:54 +08:00
/**
* Creates a hash by name or function
2024-08-09 01:03:17 +08:00
* @param {Algorithm} algorithm the algorithm name or a constructor creating a hash
2018-06-25 16:44:54 +08:00
* @returns {Hash} the hash
*/
module.exports = algorithm => {
2018-02-25 09:00:20 +08:00
if (typeof algorithm === "function") {
2024-07-31 11:50:02 +08:00
// eslint-disable-next-line new-cap
2019-07-25 04:43:30 +08:00
return new BulkUpdateDecorator(() => new algorithm());
}
2018-02-25 09:00:20 +08:00
switch (algorithm) {
// TODO add non-cryptographic algorithm here
case "debug":
return new DebugHash();
case "xxhash64":
2021-09-24 05:32:40 +08:00
if (createXXHash64 === undefined) {
createXXHash64 = require("./hash/xxhash64");
2021-09-24 05:32:40 +08:00
if (BatchedHash === undefined) {
BatchedHash = require("./hash/BatchedHash");
}
}
2023-06-12 22:21:21 +08:00
return new /** @type {typeof import("./hash/BatchedHash")} */ (
BatchedHash
)(createXXHash64());
case "md4":
if (createMd4 === undefined) {
createMd4 = require("./hash/md4");
if (BatchedHash === undefined) {
BatchedHash = require("./hash/BatchedHash");
}
}
2023-06-12 22:21:21 +08:00
return new /** @type {typeof import("./hash/BatchedHash")} */ (
BatchedHash
)(createMd4());
case "native-md4":
if (crypto === undefined) crypto = require("crypto");
2023-06-12 22:21:21 +08:00
return new BulkUpdateDecorator(
() => /** @type {typeof import("crypto")} */ (crypto).createHash("md4"),
"md4"
);
default:
2018-12-20 23:30:00 +08:00
if (crypto === undefined) crypto = require("crypto");
2019-07-25 04:43:30 +08:00
return new BulkUpdateDecorator(
2023-06-12 22:21:21 +08:00
() =>
2024-03-21 20:09:45 +08:00
/** @type {typeof import("crypto")} */ (crypto).createHash(algorithm),
2019-07-25 04:43:30 +08:00
algorithm
);
}
};