BatchedHash.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. const { digest, update } = require("./hash-digest");
  8. /** @type {number} */
  9. const MAX_SHORT_STRING = require("./wasm-hash").MAX_SHORT_STRING;
  10. /** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
  11. class BatchedHash extends Hash {
  12. /**
  13. * Creates an instance of BatchedHash.
  14. * @param {Hash} hash hash
  15. */
  16. constructor(hash) {
  17. super();
  18. /** @type {undefined | string} */
  19. this.string = undefined;
  20. /** @type {undefined | Encoding} */
  21. this.encoding = undefined;
  22. /** @type {Hash} */
  23. this.hash = hash;
  24. }
  25. /**
  26. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  27. * @overload
  28. * @param {string | Buffer} data data
  29. * @returns {Hash} updated hash
  30. */
  31. /**
  32. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  33. * @overload
  34. * @param {string} data data
  35. * @param {Encoding} inputEncoding data encoding
  36. * @returns {Hash} updated hash
  37. */
  38. /**
  39. * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
  40. * @param {string | Buffer} data data
  41. * @param {Encoding=} inputEncoding data encoding
  42. * @returns {Hash} updated hash
  43. */
  44. update(data, inputEncoding) {
  45. if (this.string !== undefined) {
  46. if (
  47. typeof data === "string" &&
  48. inputEncoding === this.encoding &&
  49. this.string.length + data.length < MAX_SHORT_STRING
  50. ) {
  51. this.string += data;
  52. return this;
  53. }
  54. if (this.encoding) {
  55. update(this.hash, this.string, this.encoding);
  56. } else {
  57. update(this.hash, this.string);
  58. }
  59. this.string = undefined;
  60. }
  61. if (typeof data === "string") {
  62. if (
  63. data.length < MAX_SHORT_STRING &&
  64. // base64 encoding is not valid since it may contain padding chars
  65. (!inputEncoding || !inputEncoding.startsWith("ba"))
  66. ) {
  67. this.string = data;
  68. this.encoding = inputEncoding;
  69. } else if (inputEncoding) {
  70. update(this.hash, data, inputEncoding);
  71. } else {
  72. update(this.hash, data);
  73. }
  74. } else {
  75. update(this.hash, data);
  76. }
  77. return this;
  78. }
  79. /**
  80. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  81. * @overload
  82. * @returns {Buffer} digest
  83. */
  84. /**
  85. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  86. * @overload
  87. * @param {Encoding} encoding encoding of the return value
  88. * @returns {string} digest
  89. */
  90. /**
  91. * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
  92. * @param {Encoding=} encoding encoding of the return value
  93. * @returns {string | Buffer} digest
  94. */
  95. digest(encoding) {
  96. if (this.string !== undefined) {
  97. if (this.encoding) {
  98. update(this.hash, this.string, this.encoding);
  99. } else {
  100. update(this.hash, this.string);
  101. }
  102. }
  103. if (!encoding) {
  104. return digest(this.hash);
  105. }
  106. return digest(this.hash, encoding);
  107. }
  108. }
  109. module.exports = BatchedHash;