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