StringXor.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. *
  18. * @example
  19. * ```js
  20. * const xor = new StringXor();
  21. * xor.add('hello');
  22. * xor.add('world');
  23. * console.log(xor.toString());
  24. * ```
  25. *
  26. * @example
  27. * ```js
  28. * const xor = new StringXor();
  29. * xor.add('foo');
  30. * xor.add('bar');
  31. * const hash = createHash('sha256');
  32. * hash.update(xor.toString());
  33. * console.log(hash.digest('hex'));
  34. * ```
  35. */
  36. class StringXor {
  37. constructor() {
  38. /** @type {Buffer|undefined} */
  39. this._value = undefined;
  40. }
  41. /**
  42. * Adds a string to the current StringXor object.
  43. *
  44. * @param {string} str string
  45. * @returns {void}
  46. */
  47. add(str) {
  48. const len = str.length;
  49. const value = this._value;
  50. if (value === undefined) {
  51. /**
  52. * We are choosing to use Buffer.allocUnsafe() because it is often faster than Buffer.alloc() because
  53. * it allocates a new buffer of the specified size without initializing the memory.
  54. */
  55. const newValue = (this._value = Buffer.allocUnsafe(len));
  56. for (let i = 0; i < len; i++) {
  57. newValue[i] = str.charCodeAt(i);
  58. }
  59. return;
  60. }
  61. const valueLen = value.length;
  62. if (valueLen < len) {
  63. const newValue = (this._value = Buffer.allocUnsafe(len));
  64. let i;
  65. for (i = 0; i < valueLen; i++) {
  66. newValue[i] = value[i] ^ str.charCodeAt(i);
  67. }
  68. for (; i < len; i++) {
  69. newValue[i] = str.charCodeAt(i);
  70. }
  71. } else {
  72. for (let i = 0; i < len; i++) {
  73. value[i] = value[i] ^ str.charCodeAt(i);
  74. }
  75. }
  76. }
  77. /**
  78. * Returns a string that represents the current state of the StringXor object. We chose to use "latin1" encoding
  79. * here because "latin1" encoding is a single-byte encoding that can represent all characters in the
  80. * [ISO-8859-1 character set](https://en.wikipedia.org/wiki/ISO/IEC_8859-1). This is useful when working
  81. * with binary data that needs to be represented as a string.
  82. *
  83. * @returns {string} Returns a string that represents the current state of the StringXor object.
  84. */
  85. toString() {
  86. const value = this._value;
  87. return value === undefined ? "" : value.toString("latin1");
  88. }
  89. /**
  90. * Updates the hash with the current state of the StringXor object.
  91. *
  92. * @param {Hash} hash Hash instance
  93. */
  94. updateHash(hash) {
  95. const value = this._value;
  96. if (value !== undefined) hash.update(value);
  97. }
  98. }
  99. module.exports = StringXor;