createHash.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. if (this.buffer.length > 0) {
  46. this.hash.update(this.buffer);
  47. this.buffer = "";
  48. }
  49. this.hash.update(data, inputEncoding);
  50. } else {
  51. this.buffer += data;
  52. if (this.buffer.length > BULK_SIZE) {
  53. if (this.hash === undefined)
  54. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  55. this.hash.update(this.buffer);
  56. this.buffer = "";
  57. }
  58. }
  59. return this;
  60. }
  61. /**
  62. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  63. * @param {string=} encoding encoding of the return value
  64. * @returns {string|Buffer} digest
  65. */
  66. digest(encoding) {
  67. let digestCache;
  68. const buffer = this.buffer;
  69. if (this.hash === undefined) {
  70. // short data for hash, we can use caching
  71. const cacheKey = `${this.hashKey}-${encoding}`;
  72. digestCache = digestCaches[cacheKey];
  73. if (digestCache === undefined) {
  74. digestCache = digestCaches[cacheKey] = new Map();
  75. }
  76. const cacheEntry = digestCache.get(buffer);
  77. if (cacheEntry !== undefined) return cacheEntry;
  78. this.hash = /** @type {HashFactory} */ (this.hashFactory)();
  79. }
  80. if (buffer.length > 0) {
  81. this.hash.update(buffer);
  82. }
  83. const digestResult = this.hash.digest(encoding);
  84. const result =
  85. typeof digestResult === "string" ? digestResult : digestResult.toString();
  86. if (digestCache !== undefined) {
  87. digestCache.set(buffer, result);
  88. }
  89. return result;
  90. }
  91. }
  92. /* istanbul ignore next */
  93. class DebugHash extends Hash {
  94. constructor() {
  95. super();
  96. this.string = "";
  97. }
  98. /**
  99. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  100. * @param {string|Buffer} data data
  101. * @param {string=} inputEncoding data encoding
  102. * @returns {this} updated hash
  103. */
  104. update(data, inputEncoding) {
  105. if (typeof data !== "string") data = data.toString("utf-8");
  106. const prefix = Buffer.from("@webpack-debug-digest@").toString("hex");
  107. if (data.startsWith(prefix)) {
  108. data = Buffer.from(data.slice(prefix.length), "hex").toString();
  109. }
  110. this.string += `[${data}](${
  111. /** @type {string} */ (new Error().stack).split("\n", 3)[2]
  112. })\n`;
  113. return this;
  114. }
  115. /**
  116. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  117. * @param {string=} encoding encoding of the return value
  118. * @returns {string|Buffer} digest
  119. */
  120. digest(encoding) {
  121. return Buffer.from(`@webpack-debug-digest@${this.string}`).toString("hex");
  122. }
  123. }
  124. /** @type {typeof import("crypto") | undefined} */
  125. let crypto;
  126. /** @type {typeof import("./hash/xxhash64") | undefined} */
  127. let createXXHash64;
  128. /** @type {typeof import("./hash/md4") | undefined} */
  129. let createMd4;
  130. /** @type {typeof import("./hash/BatchedHash") | undefined} */
  131. let BatchedHash;
  132. /**
  133. * Creates a hash by name or function
  134. * @param {HashFunction} algorithm the algorithm name or a constructor creating a hash
  135. * @returns {Hash} the hash
  136. */
  137. module.exports = algorithm => {
  138. if (typeof algorithm === "function") {
  139. // eslint-disable-next-line new-cap
  140. return new BulkUpdateDecorator(() => new algorithm());
  141. }
  142. switch (algorithm) {
  143. // TODO add non-cryptographic algorithm here
  144. case "debug":
  145. return new DebugHash();
  146. case "xxhash64":
  147. if (createXXHash64 === undefined) {
  148. createXXHash64 = require("./hash/xxhash64");
  149. if (BatchedHash === undefined) {
  150. BatchedHash = require("./hash/BatchedHash");
  151. }
  152. }
  153. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  154. BatchedHash
  155. )(createXXHash64());
  156. case "md4":
  157. if (createMd4 === undefined) {
  158. createMd4 = require("./hash/md4");
  159. if (BatchedHash === undefined) {
  160. BatchedHash = require("./hash/BatchedHash");
  161. }
  162. }
  163. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  164. BatchedHash
  165. )(createMd4());
  166. case "native-md4":
  167. if (crypto === undefined) crypto = require("crypto");
  168. return new BulkUpdateDecorator(
  169. () => /** @type {typeof import("crypto")} */ (crypto).createHash("md4"),
  170. "md4"
  171. );
  172. default:
  173. if (crypto === undefined) crypto = require("crypto");
  174. return new BulkUpdateDecorator(
  175. () =>
  176. /** @type {typeof import("crypto")} */ (crypto).createHash(algorithm),
  177. algorithm
  178. );
  179. }
  180. };