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 | * Adds a string to the current StringXor object.
|
---|
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 | let i;
|
---|
66 | for (i = 0; i < valueLen; i++) {
|
---|
67 | newValue[i] = value[i] ^ str.charCodeAt(i);
|
---|
68 | }
|
---|
69 | for (; i < len; i++) {
|
---|
70 | newValue[i] = str.charCodeAt(i);
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | for (let i = 0; i < len; i++) {
|
---|
74 | value[i] = value[i] ^ str.charCodeAt(i);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Returns a string that represents the current state of the StringXor object. We chose to use "latin1" encoding
|
---|
81 | * here because "latin1" encoding is a single-byte encoding that can represent all characters in the
|
---|
82 | * [ISO-8859-1 character set](https://en.wikipedia.org/wiki/ISO/IEC_8859-1). This is useful when working
|
---|
83 | * with binary data that needs to be represented as a string.
|
---|
84 | * @returns {string} Returns a string that represents the current state of the StringXor object.
|
---|
85 | */
|
---|
86 | toString() {
|
---|
87 | const value = this._value;
|
---|
88 | return value === undefined ? "" : value.toString("latin1");
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Updates the hash with the current state of the StringXor object.
|
---|
93 | * @param {Hash} hash Hash instance
|
---|
94 | */
|
---|
95 | updateHash(hash) {
|
---|
96 | const value = this._value;
|
---|
97 | if (value !== undefined) hash.update(value);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | module.exports = StringXor;
|
---|