DebugHash.js 2.2 KB

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