webpack/lib/util/createHash.js

172 lines
4.7 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 17:08:51 +02:00
"use strict";
2019-07-17 16:02:33 +02:00
const Hash = require("./Hash");
2018-06-25 10:44:54 +02:00
2021-01-26 12:41:50 +01:00
const BULK_SIZE = 2000;
2021-01-26 12:41:50 +01: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-24 22:43:30 +02:00
class BulkUpdateDecorator extends Hash {
2019-07-10 11:42:34 +02:00
/**
2019-07-24 22:43:30 +02:00
* @param {Hash | function(): Hash} hashOrFactory function to create a hash
* @param {string=} hashKey key for caching
2019-07-10 11:42:34 +02:00
*/
2019-07-24 22:43:30 +02:00
constructor(hashOrFactory, hashKey) {
2019-07-10 11:42:34 +02:00
super();
2019-07-24 22:43:30 +02: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 11:42:34 +02: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 11:42:34 +02:00
*/
update(data, inputEncoding) {
2018-02-25 02:00:20 +01:00
if (
inputEncoding !== undefined ||
typeof data !== "string" ||
data.length > BULK_SIZE
) {
2019-07-24 22:43:30 +02:00
if (this.hash === undefined) this.hash = this.hashFactory();
2018-02-25 02:00:20 +01:00
if (this.buffer.length > 0) {
2017-11-23 11:44:56 +01:00
this.hash.update(this.buffer);
this.buffer = "";
}
this.hash.update(data, inputEncoding);
} else {
this.buffer += data;
2018-02-25 02:00:20 +01:00
if (this.buffer.length > BULK_SIZE) {
2019-07-24 22:43:30 +02:00
if (this.hash === undefined) this.hash = this.hashFactory();
2017-11-23 11:44:56 +01:00
this.hash.update(this.buffer);
this.buffer = "";
}
}
return this;
}
2019-07-10 11:42:34 +02: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 11:42:34 +02:00
* @returns {string|Buffer} digest
*/
digest(encoding) {
2021-01-26 12:41:50 +01:00
let digestCache;
2021-05-26 16:02:49 +02:00
const buffer = this.buffer;
2019-07-24 22:43:30 +02:00
if (this.hash === undefined) {
// short data for hash, we can use caching
2021-01-26 12:41:50 +01:00
const cacheKey = `${this.hashKey}-${encoding}`;
digestCache = digestCaches[cacheKey];
if (digestCache === undefined) {
digestCache = digestCaches[cacheKey] = new Map();
}
2021-05-26 16:02:49 +02:00
const cacheEntry = digestCache.get(buffer);
2019-07-24 22:43:30 +02:00
if (cacheEntry !== undefined) return cacheEntry;
this.hash = this.hashFactory();
}
2021-05-26 16:02:49 +02:00
if (buffer.length > 0) {
this.hash.update(buffer);
2017-11-23 11:44:56 +01:00
}
2019-07-24 22:43:30 +02:00
const digestResult = this.hash.digest(encoding);
const result =
typeof digestResult === "string" ? digestResult : digestResult.toString();
2021-01-26 12:41:50 +01:00
if (digestCache !== undefined) {
2021-05-26 16:02:49 +02:00
digestCache.set(buffer, result);
2019-07-24 22:43:30 +02:00
}
return result;
}
}
2020-04-17 10:53:30 +02:00
/* istanbul ignore next */
class DebugHash extends Hash {
constructor() {
2019-07-10 11:42:34 +02:00
super();
2018-01-20 11:28:45 +01:00
this.string = "";
}
2019-07-10 11:42:34 +02: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 11:42:34 +02:00
*/
update(data, inputEncoding) {
2018-02-25 02:00:20 +01:00
if (typeof data !== "string") data = data.toString("utf-8");
2018-10-18 09:22:47 +02:00
if (data.startsWith("debug-digest-")) {
data = Buffer.from(data.slice("debug-digest-".length), "hex").toString();
}
2019-01-09 21:35:18 +01:00
this.string += `[${data}](${new Error().stack.split("\n", 3)[2]})\n`;
return this;
}
2019-07-10 11:42:34 +02: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 11:42:34 +02:00
* @returns {string|Buffer} digest
*/
digest(encoding) {
2018-10-18 09:22:47 +02:00
return "debug-digest-" + Buffer.from(this.string).toString("hex");
}
}
2018-12-20 16:30:00 +01:00
let crypto = undefined;
let createXXHash64 = undefined;
let createMd4 = undefined;
2021-09-23 23:32:40 +02:00
let BatchedHash = undefined;
2018-12-20 16:30:00 +01:00
2018-06-25 10:44:54 +02:00
/**
* Creates a hash by name or function
2019-07-17 16:02:33 +02:00
* @param {string | typeof Hash} algorithm the algorithm name or a constructor creating a hash
2018-06-25 10:44:54 +02:00
* @returns {Hash} the hash
*/
module.exports = algorithm => {
2018-02-25 02:00:20 +01:00
if (typeof algorithm === "function") {
2019-07-24 22:43:30 +02:00
return new BulkUpdateDecorator(() => new algorithm());
}
2018-02-25 02:00:20 +01:00
switch (algorithm) {
// TODO add non-cryptographic algorithm here
case "debug":
return new DebugHash();
case "xxhash64":
2021-09-23 23:32:40 +02:00
if (createXXHash64 === undefined) {
createXXHash64 = require("./hash/xxhash64");
2021-09-23 23:32:40 +02:00
if (BatchedHash === undefined) {
BatchedHash = require("./hash/BatchedHash");
}
}
return new BatchedHash(createXXHash64());
case "md4":
if (createMd4 === undefined) {
createMd4 = require("./hash/md4");
if (BatchedHash === undefined) {
BatchedHash = require("./hash/BatchedHash");
}
}
return new BatchedHash(createMd4());
case "native-md4":
if (crypto === undefined) crypto = require("crypto");
return new BulkUpdateDecorator(() => crypto.createHash("md4"), "md4");
default:
2018-12-20 16:30:00 +01:00
if (crypto === undefined) crypto = require("crypto");
2019-07-24 22:43:30 +02:00
return new BulkUpdateDecorator(
() => crypto.createHash(algorithm),
algorithm
);
}
};