webpack/lib/util/createHash.js

149 lines
4.0 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
const digestCaches = {};
2019-07-25 04:43:30 +08:00
class BulkUpdateDecorator extends Hash {
2019-07-10 17:42:34 +08:00
/**
2019-07-25 04:43:30 +08:00
* @param {Hash | function(): Hash} hashOrFactory function to create a hash
* @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
) {
2019-07-25 04:43:30 +08:00
if (this.hash === undefined) this.hash = 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) {
2019-07-25 04:43:30 +08:00
if (this.hash === undefined) this.hash = 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;
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();
}
const cacheEntry = digestCache.get(this.buffer);
2019-07-25 04:43:30 +08:00
if (cacheEntry !== undefined) return cacheEntry;
this.hash = 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);
}
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) {
digestCache.set(this.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");
2018-10-18 15:22:47 +08:00
if (data.startsWith("debug-digest-")) {
data = Buffer.from(data.slice("debug-digest-".length), "hex").toString();
}
2019-01-10 04:35:18 +08:00
this.string += `[${data}](${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) {
2018-10-18 15:22:47 +08:00
return "debug-digest-" + Buffer.from(this.string).toString("hex");
}
}
2018-12-20 23:30:00 +08:00
let crypto = undefined;
2018-06-25 16:44:54 +08:00
/**
* Creates a hash by name or function
2019-07-17 22:02:33 +08:00
* @param {string | typeof Hash} 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") {
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();
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(
() => crypto.createHash(algorithm),
algorithm
);
}
};