createHash.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Hash")} Hash */
  7. /** @typedef {import("../../declarations/WebpackOptions").HashFunction} HashFunction */
  8. /** @type {typeof import("crypto") | undefined} */
  9. let crypto;
  10. /** @type {typeof import("./hash/xxhash64") | undefined} */
  11. let createXXHash64;
  12. /** @type {typeof import("./hash/md4") | undefined} */
  13. let createMd4;
  14. /** @type {typeof import("./hash/DebugHash") | undefined} */
  15. let DebugHash;
  16. /** @type {typeof import("./hash/BatchedHash") | undefined} */
  17. let BatchedHash;
  18. /** @type {typeof import("./hash/BulkUpdateHash") | undefined} */
  19. let BulkUpdateHash;
  20. /**
  21. * Creates a hash by name or function
  22. * @param {HashFunction} algorithm the algorithm name or a constructor creating a hash
  23. * @returns {Hash} the hash
  24. */
  25. module.exports = (algorithm) => {
  26. if (typeof algorithm === "function") {
  27. if (BulkUpdateHash === undefined) {
  28. BulkUpdateHash = require("./hash/BulkUpdateHash");
  29. }
  30. // eslint-disable-next-line new-cap
  31. return new BulkUpdateHash(() => new algorithm());
  32. }
  33. switch (algorithm) {
  34. // TODO add non-cryptographic algorithm here
  35. case "debug":
  36. if (DebugHash === undefined) {
  37. DebugHash = require("./hash/DebugHash");
  38. }
  39. return new DebugHash();
  40. case "xxhash64":
  41. if (createXXHash64 === undefined) {
  42. createXXHash64 = require("./hash/xxhash64");
  43. if (BatchedHash === undefined) {
  44. BatchedHash = require("./hash/BatchedHash");
  45. }
  46. }
  47. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  48. BatchedHash
  49. )(createXXHash64());
  50. case "md4":
  51. if (createMd4 === undefined) {
  52. createMd4 = require("./hash/md4");
  53. if (BatchedHash === undefined) {
  54. BatchedHash = require("./hash/BatchedHash");
  55. }
  56. }
  57. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  58. BatchedHash
  59. )(createMd4());
  60. case "native-md4":
  61. if (crypto === undefined) crypto = require("crypto");
  62. if (BulkUpdateHash === undefined) {
  63. BulkUpdateHash = require("./hash/BulkUpdateHash");
  64. }
  65. return new BulkUpdateHash(
  66. () =>
  67. /** @type {Hash} */ (
  68. /** @type {typeof import("crypto")} */
  69. (crypto).createHash("md4")
  70. ),
  71. "md4"
  72. );
  73. default:
  74. if (crypto === undefined) crypto = require("crypto");
  75. if (BulkUpdateHash === undefined) {
  76. BulkUpdateHash = require("./hash/BulkUpdateHash");
  77. }
  78. return new BulkUpdateHash(
  79. () =>
  80. /** @type {Hash} */ (
  81. /** @type {typeof import("crypto")} */
  82. (crypto).createHash(algorithm)
  83. ),
  84. algorithm
  85. );
  86. }
  87. };