StringXor.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../util/Hash")} Hash */
  7. /**
  8. * StringXor class provides methods for performing
  9. * [XOR operations](https://en.wikipedia.org/wiki/Exclusive_or) on strings. In this context
  10. * we operating on the character codes of two strings, which are represented as
  11. * [Buffer](https://nodejs.org/api/buffer.html) objects.
  12. *
  13. * We use [StringXor in webpack](https://github.com/webpack/webpack/commit/41a8e2ea483a544c4ccd3e6217bdfb80daffca39)
  14. * to create a hash of the current state of the compilation. By XOR'ing the Module hashes, it
  15. * doesn't matter if the Module hashes are sorted or not. This is useful because it allows us to avoid sorting the
  16. * Module hashes.
  17. * @example
  18. * ```js
  19. * const xor = new StringXor();
  20. * xor.add('hello');
  21. * xor.add('world');
  22. * console.log(xor.toString());
  23. * ```
  24. * @example
  25. * ```js
  26. * const xor = new StringXor();
  27. * xor.add('foo');
  28. * xor.add('bar');
  29. * const hash = createHash('sha256');
  30. * hash.update(xor.toString());
  31. * console.log(hash.digest('hex'));
  32. * ```
  33. */
  34. class StringXor {
  35. constructor() {
  36. /** @type {Buffer|undefined} */
  37. this._value = undefined;
  38. }
  39. /**
  40. * Adds a string to the current StringXor object.
  41. * @param {string} str string
  42. * @returns {void}
  43. */
  44. add(str) {
  45. const len = str.length;
  46. const value = this._value;
  47. if (value === undefined) {
  48. /**
  49. * We are choosing to use Buffer.allocUnsafe() because it is often faster than Buffer.alloc() because
  50. * it allocates a new buffer of the specified size without initializing the memory.
  51. */
  52. const newValue = (this._value = Buffer.allocUnsafe(len));
  53. for (let i = 0; i < len; i++) {
  54. newValue[i] = str.charCodeAt(i);
  55. }
  56. return;
  57. }
  58. const valueLen = value.length;
  59. if (valueLen < len) {
  60. const newValue = (this._value = Buffer.allocUnsafe(len));
  61. let i;
  62. for (i = 0; i < valueLen; i++) {
  63. newValue[i] = value[i] ^ str.charCodeAt(i);
  64. }
  65. for (; i < len; i++) {
  66. newValue[i] = str.charCodeAt(i);
  67. }
  68. } else {
  69. for (let i = 0; i < len; i++) {
  70. value[i] = value[i] ^ str.charCodeAt(i);
  71. }
  72. }
  73. }
  74. /**
  75. * Returns a string that represents the current state of the StringXor object. We chose to use "latin1" encoding
  76. * here because "latin1" encoding is a single-byte encoding that can represent all characters in the
  77. * [ISO-8859-1 character set](https://en.wikipedia.org/wiki/ISO/IEC_8859-1). This is useful when working
  78. * with binary data that needs to be represented as a string.
  79. * @returns {string} Returns a string that represents the current state of the StringXor object.
  80. */
  81. toString() {
  82. const value = this._value;
  83. return value === undefined ? "" : value.toString("latin1");
  84. }
  85. /**
  86. * Updates the hash with the current state of the StringXor object.
  87. * @param {Hash} hash Hash instance
  88. */
  89. updateHash(hash) {
  90. const value = this._value;
  91. if (value !== undefined) hash.update(value);
  92. }
  93. }
  94. module.exports = StringXor;