DebugHash.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const Hash = require("../Hash");
  7. /** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
  8. /* istanbul ignore next */
  9. class DebugHash extends Hash {
  10. constructor() {
  11. super();
  12. this.string = "";
  13. }
  14. /**
  15. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  16. * @overload
  17. * @param {string | Buffer} data data
  18. * @returns {Hash} updated hash
  19. */
  20. /**
  21. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  22. * @overload
  23. * @param {string} data data
  24. * @param {Encoding} inputEncoding data encoding
  25. * @returns {Hash} updated hash
  26. */
  27. /**
  28. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  29. * @param {string | Buffer} data data
  30. * @param {Encoding=} inputEncoding data encoding
  31. * @returns {Hash} updated hash
  32. */
  33. update(data, inputEncoding) {
  34. if (typeof data !== "string") data = data.toString("utf8");
  35. const prefix = Buffer.from("@webpack-debug-digest@").toString("hex");
  36. if (data.startsWith(prefix)) {
  37. data = Buffer.from(data.slice(prefix.length), "hex").toString();
  38. }
  39. this.string += `[${data}](${
  40. /** @type {string} */
  41. (
  42. // eslint-disable-next-line unicorn/error-message
  43. new Error().stack
  44. ).split("\n", 3)[2]
  45. })\n`;
  46. return this;
  47. }
  48. /**
  49. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  50. * @overload
  51. * @returns {Buffer} digest
  52. */
  53. /**
  54. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  55. * @overload
  56. * @param {Encoding} encoding encoding of the return value
  57. * @returns {string} digest
  58. */
  59. /**
  60. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  61. * @param {Encoding=} encoding encoding of the return value
  62. * @returns {string | Buffer} digest
  63. */
  64. digest(encoding) {
  65. return Buffer.from(`@webpack-debug-digest@${this.string}`).toString("hex");
  66. }
  67. }
  68. module.exports = DebugHash;