source: frontend/node_modules/loader-utils/lib/hash/BatchedHash.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[9af201e]1const MAX_SHORT_STRING = require('./wasm-hash').MAX_SHORT_STRING;
2
3class BatchedHash {
4 constructor(hash) {
5 this.string = undefined;
6 this.encoding = undefined;
7 this.hash = hash;
8 }
9
10 /**
11 * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
12 * @param {string|Buffer} data data
13 * @param {string=} inputEncoding data encoding
14 * @returns {this} updated hash
15 */
16 update(data, inputEncoding) {
17 if (this.string !== undefined) {
18 if (
19 typeof data === 'string' &&
20 inputEncoding === this.encoding &&
21 this.string.length + data.length < MAX_SHORT_STRING
22 ) {
23 this.string += data;
24
25 return this;
26 }
27
28 this.hash.update(this.string, this.encoding);
29 this.string = undefined;
30 }
31
32 if (typeof data === 'string') {
33 if (
34 data.length < MAX_SHORT_STRING &&
35 // base64 encoding is not valid since it may contain padding chars
36 (!inputEncoding || !inputEncoding.startsWith('ba'))
37 ) {
38 this.string = data;
39 this.encoding = inputEncoding;
40 } else {
41 this.hash.update(data, inputEncoding);
42 }
43 } else {
44 this.hash.update(data);
45 }
46
47 return this;
48 }
49
50 /**
51 * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
52 * @param {string=} encoding encoding of the return value
53 * @returns {string|Buffer} digest
54 */
55 digest(encoding) {
56 if (this.string !== undefined) {
57 this.hash.update(this.string, this.encoding);
58 }
59
60 return this.hash.digest(encoding);
61 }
62}
63
64module.exports = BatchedHash;
Note: See TracBrowser for help on using the repository browser.