createHash.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Hash = require("./Hash");
  7. /** @typedef {import("../../declarations/WebpackOptions").HashFunction} HashFunction */
  8. const BULK_SIZE = 2000;
  9. // We are using an object instead of a Map as this will stay static during the runtime
  10. // so access to it can be optimized by v8
  11. /** @type {{[key: string]: Map<string, string>}} */
  12. const digestCaches = {};
  13. /** @typedef {() => Hash} HashFactory */
  14. class BulkUpdateDecorator extends Hash {
  15. /**
  16. * @param {Hash | HashFactory} hashOrFactory function to create a hash
  17. * @param {string=} hashKey key for caching
  18. */
  19. constructor(hashOrFactory, hashKey) {
  20. super();
  21. this.hashKey = hashKey;
  22. if (typeof hashOrFactory === "function") {
  23. this.hashFactory = hashOrFactory;
  24. this.hash = undefined;
  25. } else {
  26. this.hashFactory = undefined;
  27. this.hash = hashOrFactory;
  28. }
  29. this.buffer = "";
  30. }
  31. /**
  32. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  33. * @param {string|Buffer} data data
  34. * @param {string=} inputEncoding data encoding
  35. * @returns {this} updated hash
  36. */
  37. update(data, inputEncoding) {
  38. if (
  39. inputEncoding !== undefined ||
  40. typeof data !== "string" ||
  41. data.length > BULK_SIZE
  42. ) {
  43. if (this.hash === undefined) {
  44. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  45. }
  46. if (this.buffer.length > 0) {
  47. this.hash.update(this.buffer);
  48. this.buffer = "";
  49. }
  50. this.hash.update(data, inputEncoding);
  51. } else {
  52. this.buffer += data;
  53. if (this.buffer.length > BULK_SIZE) {
  54. if (this.hash === undefined) {
  55. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  56. }
  57. this.hash.update(this.buffer);
  58. this.buffer = "";
  59. }
  60. }
  61. return this;
  62. }
  63. /**
  64. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  65. * @param {string=} encoding encoding of the return value
  66. * @returns {string|Buffer} digest
  67. */
  68. digest(encoding) {
  69. let digestCache;
  70. const buffer = this.buffer;
  71. if (this.hash === undefined) {
  72. // short data for hash, we can use caching
  73. const cacheKey = `${this.hashKey}-${encoding}`;
  74. digestCache = digestCaches[cacheKey];
  75. if (digestCache === undefined) {
  76. digestCache = digestCaches[cacheKey] = new Map();
  77. }
  78. const cacheEntry = digestCache.get(buffer);
  79. if (cacheEntry !== undefined) return cacheEntry;
  80. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  81. }
  82. if (buffer.length > 0) {
  83. this.hash.update(buffer);
  84. }
  85. const digestResult = this.hash.digest(encoding);
  86. const result =
  87. typeof digestResult === "string" ? digestResult : digestResult.toString();
  88. if (digestCache !== undefined) {
  89. digestCache.set(buffer, result);
  90. }
  91. return result;
  92. }
  93. }
  94. /* istanbul ignore next */
  95. class DebugHash extends Hash {
  96. constructor() {
  97. super();
  98. this.string = "";
  99. }
  100. /**
  101. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  102. * @param {string|Buffer} data data
  103. * @param {string=} inputEncoding data encoding
  104. * @returns {this} updated hash
  105. */
  106. update(data, inputEncoding) {
  107. if (typeof data !== "string") data = data.toString("utf8");
  108. const prefix = Buffer.from("@webpack-debug-digest@").toString("hex");
  109. if (data.startsWith(prefix)) {
  110. data = Buffer.from(data.slice(prefix.length), "hex").toString();
  111. }
  112. this.string += `[${data}](${
  113. /** @type {string} */
  114. (
  115. // eslint-disable-next-line unicorn/error-message
  116. new Error().stack
  117. ).split("\n", 3)[2]
  118. })\n`;
  119. return this;
  120. }
  121. /**
  122. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  123. * @param {string=} encoding encoding of the return value
  124. * @returns {string|Buffer} digest
  125. */
  126. digest(encoding) {
  127. return Buffer.from(`@webpack-debug-digest@${this.string}`).toString("hex");
  128. }
  129. }
  130. /** @type {typeof import("crypto") | undefined} */
  131. let crypto;
  132. /** @type {typeof import("./hash/xxhash64") | undefined} */
  133. let createXXHash64;
  134. /** @type {typeof import("./hash/md4") | undefined} */
  135. let createMd4;
  136. /** @type {typeof import("./hash/BatchedHash") | undefined} */
  137. let BatchedHash;
  138. /**
  139. * Creates a hash by name or function
  140. * @param {HashFunction} algorithm the algorithm name or a constructor creating a hash
  141. * @returns {Hash} the hash
  142. */
  143. module.exports = algorithm => {
  144. if (typeof algorithm === "function") {
  145. // eslint-disable-next-line new-cap
  146. return new BulkUpdateDecorator(() => new algorithm());
  147. }
  148. switch (algorithm) {
  149. // TODO add non-cryptographic algorithm here
  150. case "debug":
  151. return new DebugHash();
  152. case "xxhash64":
  153. if (createXXHash64 === undefined) {
  154. createXXHash64 = require("./hash/xxhash64");
  155. if (BatchedHash === undefined) {
  156. BatchedHash = require("./hash/BatchedHash");
  157. }
  158. }
  159. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  160. BatchedHash
  161. )(createXXHash64());
  162. case "md4":
  163. if (createMd4 === undefined) {
  164. createMd4 = require("./hash/md4");
  165. if (BatchedHash === undefined) {
  166. BatchedHash = require("./hash/BatchedHash");
  167. }
  168. }
  169. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  170. BatchedHash
  171. )(createMd4());
  172. case "native-md4":
  173. if (crypto === undefined) crypto = require("crypto");
  174. return new BulkUpdateDecorator(
  175. () => /** @type {typeof import("crypto")} */ (crypto).createHash("md4"),
  176. "md4"
  177. );
  178. default:
  179. if (crypto === undefined) crypto = require("crypto");
  180. return new BulkUpdateDecorator(
  181. () =>
  182. /** @type {typeof import("crypto")} */ (crypto).createHash(algorithm),
  183. algorithm
  184. );
  185. }
  186. };