source: frontend/node_modules/webpack/lib/util/hash/BulkUpdateHash.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Alexander Akait @alexander-akait
4*/
5
6"use strict";
7
8const Hash = require("../Hash");
9const { digest, update } = require("./hash-digest");
10
11/** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
12/** @typedef {() => Hash} HashFactory */
13
14const BULK_SIZE = 3;
15
16// We are using an object instead of a Map as this will stay static during the runtime
17// so access to it can be optimized by v8
18/** @type {{ [key: string]: Map<string, string> }} */
19const digestCaches = {};
20
21class BulkUpdateHash extends Hash {
22 /**
23 * Creates an instance of BulkUpdateHash.
24 * @param {Hash | HashFactory} hashOrFactory function to create a hash
25 * @param {string=} hashKey key for caching
26 */
27 constructor(hashOrFactory, hashKey) {
28 super();
29 /** @type {undefined | string} */
30 this.hashKey = hashKey;
31 if (typeof hashOrFactory === "function") {
32 /** @type {undefined | HashFactory} */
33 this.hashFactory = hashOrFactory;
34 /** @type {undefined | Hash} */
35 this.hash = undefined;
36 } else {
37 /** @type {undefined | HashFactory} */
38 this.hashFactory = undefined;
39 /** @type {undefined | Hash} */
40 this.hash = hashOrFactory;
41 }
42 /** @type {string} */
43 this.buffer = "";
44 }
45
46 /**
47 * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
48 * @overload
49 * @param {string | Buffer} data data
50 * @returns {Hash} updated hash
51 */
52 /**
53 * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
54 * @overload
55 * @param {string} data data
56 * @param {Encoding} inputEncoding data encoding
57 * @returns {Hash} updated hash
58 */
59 /**
60 * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
61 * @param {string | Buffer} data data
62 * @param {Encoding=} inputEncoding data encoding
63 * @returns {Hash} updated hash
64 */
65 update(data, inputEncoding) {
66 if (
67 inputEncoding !== undefined ||
68 typeof data !== "string" ||
69 data.length > BULK_SIZE
70 ) {
71 if (this.hash === undefined) {
72 this.hash = /** @type {HashFactory} */ (this.hashFactory)();
73 }
74 if (this.buffer.length > 0) {
75 update(this.hash, this.buffer);
76 this.buffer = "";
77 }
78 if (typeof data === "string" && inputEncoding) {
79 update(this.hash, data, inputEncoding);
80 } else {
81 update(this.hash, data);
82 }
83 } else {
84 this.buffer += data;
85 if (this.buffer.length > BULK_SIZE) {
86 if (this.hash === undefined) {
87 this.hash = /** @type {HashFactory} */ (this.hashFactory)();
88 }
89 update(this.hash, this.buffer);
90 this.buffer = "";
91 }
92 }
93 return this;
94 }
95
96 /**
97 * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
98 * @overload
99 * @returns {Buffer} digest
100 */
101 /**
102 * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
103 * @overload
104 * @param {Encoding} encoding encoding of the return value
105 * @returns {string} digest
106 */
107 /**
108 * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
109 * @param {Encoding=} encoding encoding of the return value
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
146module.exports = BulkUpdateHash;
Note: See TracBrowser for help on using the repository browser.