createHash.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @import Hash from "./Hash" */
  7. /** @import { HashFunction } from "../../declarations/WebpackOptions" */
  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. case "debug":
  35. if (DebugHash === undefined) {
  36. DebugHash = require("./hash/DebugHash");
  37. }
  38. return new DebugHash();
  39. case "xxhash64":
  40. if (createXXHash64 === undefined) {
  41. createXXHash64 = require("./hash/xxhash64");
  42. if (BatchedHash === undefined) {
  43. BatchedHash = require("./hash/BatchedHash");
  44. }
  45. }
  46. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  47. BatchedHash
  48. )(createXXHash64());
  49. case "md4":
  50. if (createMd4 === undefined) {
  51. createMd4 = require("./hash/md4");
  52. if (BatchedHash === undefined) {
  53. BatchedHash = require("./hash/BatchedHash");
  54. }
  55. }
  56. return new /** @type {typeof import("./hash/BatchedHash")} */ (
  57. BatchedHash
  58. )(createMd4());
  59. case "native-md4":
  60. if (crypto === undefined) crypto = require("crypto");
  61. if (BulkUpdateHash === undefined) {
  62. BulkUpdateHash = require("./hash/BulkUpdateHash");
  63. }
  64. return new BulkUpdateHash(
  65. () =>
  66. /** @type {Hash} */ (
  67. /** @type {typeof import("crypto")} */
  68. (crypto).createHash("md4")
  69. ),
  70. "md4"
  71. );
  72. default:
  73. if (crypto === undefined) crypto = require("crypto");
  74. if (BulkUpdateHash === undefined) {
  75. BulkUpdateHash = require("./hash/BulkUpdateHash");
  76. }
  77. return new BulkUpdateHash(
  78. () =>
  79. /** @type {Hash} */ (
  80. /** @type {typeof import("crypto")} */
  81. (crypto).createHash(algorithm)
  82. ),
  83. algorithm
  84. );
  85. }
  86. };