| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const Hash = require("../Hash");
|
|---|
| 9 | const { digest, update } = require("./hash-digest");
|
|---|
| 10 | /** @type {number} */
|
|---|
| 11 | const MAX_SHORT_STRING = require("./wasm-hash").MAX_SHORT_STRING;
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
|
|---|
| 14 |
|
|---|
| 15 | class BatchedHash extends Hash {
|
|---|
| 16 | /**
|
|---|
| 17 | * Creates an instance of BatchedHash.
|
|---|
| 18 | * @param {Hash} hash hash
|
|---|
| 19 | */
|
|---|
| 20 | constructor(hash) {
|
|---|
| 21 | super();
|
|---|
| 22 | /** @type {undefined | string} */
|
|---|
| 23 | this.string = undefined;
|
|---|
| 24 | /** @type {undefined | Encoding} */
|
|---|
| 25 | this.encoding = undefined;
|
|---|
| 26 | /** @type {Hash} */
|
|---|
| 27 | this.hash = hash;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
|
|---|
| 32 | * @overload
|
|---|
| 33 | * @param {string | Buffer} data data
|
|---|
| 34 | * @returns {Hash} updated hash
|
|---|
| 35 | */
|
|---|
| 36 | /**
|
|---|
| 37 | * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
|
|---|
| 38 | * @overload
|
|---|
| 39 | * @param {string} data data
|
|---|
| 40 | * @param {Encoding} inputEncoding data encoding
|
|---|
| 41 | * @returns {Hash} updated hash
|
|---|
| 42 | */
|
|---|
| 43 | /**
|
|---|
| 44 | * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
|
|---|
| 45 | * @param {string | Buffer} data data
|
|---|
| 46 | * @param {Encoding=} inputEncoding data encoding
|
|---|
| 47 | * @returns {Hash} updated hash
|
|---|
| 48 | */
|
|---|
| 49 | update(data, inputEncoding) {
|
|---|
| 50 | if (this.string !== undefined) {
|
|---|
| 51 | if (
|
|---|
| 52 | typeof data === "string" &&
|
|---|
| 53 | inputEncoding === this.encoding &&
|
|---|
| 54 | this.string.length + data.length < MAX_SHORT_STRING
|
|---|
| 55 | ) {
|
|---|
| 56 | this.string += data;
|
|---|
| 57 | return this;
|
|---|
| 58 | }
|
|---|
| 59 | if (this.encoding) {
|
|---|
| 60 | update(this.hash, this.string, this.encoding);
|
|---|
| 61 | } else {
|
|---|
| 62 | update(this.hash, this.string);
|
|---|
| 63 | }
|
|---|
| 64 | this.string = undefined;
|
|---|
| 65 | }
|
|---|
| 66 | if (typeof data === "string") {
|
|---|
| 67 | if (
|
|---|
| 68 | data.length < MAX_SHORT_STRING &&
|
|---|
| 69 | // base64 encoding is not valid since it may contain padding chars
|
|---|
| 70 | (!inputEncoding || !inputEncoding.startsWith("ba"))
|
|---|
| 71 | ) {
|
|---|
| 72 | this.string = data;
|
|---|
| 73 | this.encoding = inputEncoding;
|
|---|
| 74 | } else if (inputEncoding) {
|
|---|
| 75 | update(this.hash, data, inputEncoding);
|
|---|
| 76 | } else {
|
|---|
| 77 | update(this.hash, data);
|
|---|
| 78 | }
|
|---|
| 79 | } else {
|
|---|
| 80 | update(this.hash, data);
|
|---|
| 81 | }
|
|---|
| 82 | return this;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
|
|---|
| 87 | * @overload
|
|---|
| 88 | * @returns {Buffer} digest
|
|---|
| 89 | */
|
|---|
| 90 | /**
|
|---|
| 91 | * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
|
|---|
| 92 | * @overload
|
|---|
| 93 | * @param {Encoding} encoding encoding of the return value
|
|---|
| 94 | * @returns {string} digest
|
|---|
| 95 | */
|
|---|
| 96 | /**
|
|---|
| 97 | * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
|
|---|
| 98 | * @param {Encoding=} encoding encoding of the return value
|
|---|
| 99 | * @returns {string | Buffer} digest
|
|---|
| 100 | */
|
|---|
| 101 | digest(encoding) {
|
|---|
| 102 | if (this.string !== undefined) {
|
|---|
| 103 | if (this.encoding) {
|
|---|
| 104 | update(this.hash, this.string, this.encoding);
|
|---|
| 105 | } else {
|
|---|
| 106 | update(this.hash, this.string);
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | if (!encoding) {
|
|---|
| 110 | return digest(this.hash);
|
|---|
| 111 | }
|
|---|
| 112 | return digest(this.hash, encoding);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | module.exports = BatchedHash;
|
|---|