WXL
4 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Alexander Akait @alexander-akait
*/
 
"use strict";
 
const Hash = require("../Hash");
const { digest, update } = require("./hash-digest");
 
/** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
/** @typedef {() => Hash} HashFactory */
 
const BULK_SIZE = 3;
 
// We are using an object instead of a Map as this will stay static during the runtime
// so access to it can be optimized by v8
/** @type {{ [key: string]: Map<string, string> }} */
const digestCaches = {};
 
class BulkUpdateHash extends Hash {
    /**
     * Creates an instance of BulkUpdateHash.
     * @param {Hash | HashFactory} hashOrFactory function to create a hash
     * @param {string=} hashKey key for caching
     */
    constructor(hashOrFactory, hashKey) {
        super();
        /** @type {undefined | string} */
        this.hashKey = hashKey;
        if (typeof hashOrFactory === "function") {
            /** @type {undefined | HashFactory} */
            this.hashFactory = hashOrFactory;
            /** @type {undefined | Hash} */
            this.hash = undefined;
        } else {
            /** @type {undefined | HashFactory} */
            this.hashFactory = undefined;
            /** @type {undefined | Hash} */
            this.hash = hashOrFactory;
        }
        /** @type {string} */
        this.buffer = "";
    }
 
    /**
     * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
     * @overload
     * @param {string | Buffer} data data
     * @returns {Hash} updated hash
     */
    /**
     * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
     * @overload
     * @param {string} data data
     * @param {Encoding} inputEncoding data encoding
     * @returns {Hash} updated hash
     */
    /**
     * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
     * @param {string | Buffer} data data
     * @param {Encoding=} inputEncoding data encoding
     * @returns {Hash} updated hash
     */
    update(data, inputEncoding) {
        if (
            inputEncoding !== undefined ||
            typeof data !== "string" ||
            data.length > BULK_SIZE
        ) {
            if (this.hash === undefined) {
                this.hash = /** @type {HashFactory} */ (this.hashFactory)();
            }
            if (this.buffer.length > 0) {
                update(this.hash, this.buffer);
                this.buffer = "";
            }
            if (typeof data === "string" && inputEncoding) {
                update(this.hash, data, inputEncoding);
            } else {
                update(this.hash, data);
            }
        } else {
            this.buffer += data;
            if (this.buffer.length > BULK_SIZE) {
                if (this.hash === undefined) {
                    this.hash = /** @type {HashFactory} */ (this.hashFactory)();
                }
                update(this.hash, this.buffer);
                this.buffer = "";
            }
        }
        return this;
    }
 
    /**
     * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
     * @overload
     * @returns {Buffer} digest
     */
    /**
     * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
     * @overload
     * @param {Encoding} encoding encoding of the return value
     * @returns {string} digest
     */
    /**
     * 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
     */
    digest(encoding) {
        /** @type {undefined | Map<string, string | Buffer>} */
        let digestCache;
        const buffer = this.buffer;
        if (this.hash === undefined) {
            // short data for hash, we can use caching
            const cacheKey = `${this.hashKey}-${encoding}`;
            digestCache = digestCaches[cacheKey];
            if (digestCache === undefined) {
                digestCache = digestCaches[cacheKey] = new Map();
            }
            const cacheEntry = digestCache.get(buffer);
            if (cacheEntry !== undefined) return cacheEntry;
            this.hash = /** @type {HashFactory} */ (this.hashFactory)();
        }
 
        if (buffer.length > 0) {
            update(this.hash, buffer);
        }
        if (!encoding) {
            const result = digest(this.hash, undefined, Boolean(this.hashKey));
            if (digestCache !== undefined) {
                digestCache.set(buffer, result);
            }
            return result;
        }
        const result = digest(this.hash, encoding, Boolean(this.hashKey));
        if (digestCache !== undefined) {
            digestCache.set(buffer, result);
        }
        return result;
    }
}
 
module.exports = BulkUpdateHash;