* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding} * @param {Encoding=} encoding encoding of the return value * @returns {string | Buffer} digest
(encoding)
| 110 | * @returns {string | Buffer} digest |
| 111 | */ |
| 112 | digest(encoding) { |
| 113 | /** @type {undefined | Map<string, string | Buffer>} */ |
| 114 | let digestCache; |
| 115 | const buffer = this.buffer; |
| 116 | if (this.hash === undefined) { |
| 117 | // short data for hash, we can use caching |
| 118 | const cacheKey = `${this.hashKey}-${encoding}`; |
| 119 | digestCache = digestCaches[cacheKey]; |
| 120 | if (digestCache === undefined) { |
| 121 | digestCache = digestCaches[cacheKey] = new Map(); |
| 122 | } |
| 123 | const cacheEntry = digestCache.get(buffer); |
| 124 | if (cacheEntry !== undefined) return cacheEntry; |
| 125 | this.hash = /** @type {HashFactory} */ (this.hashFactory)(); |
| 126 | } |
| 127 | |
| 128 | if (buffer.length > 0) { |
| 129 | update(this.hash, buffer); |
| 130 | } |
| 131 | if (!encoding) { |
| 132 | const result = digest(this.hash, undefined, Boolean(this.hashKey)); |
| 133 | if (digestCache !== undefined) { |
| 134 | digestCache.set(buffer, result); |
| 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | const result = digest(this.hash, encoding, Boolean(this.hashKey)); |
| 139 | if (digestCache !== undefined) { |
| 140 | digestCache.set(buffer, result); |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | module.exports = BulkUpdateHash; |