WXL
3 天以前 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
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Alexander Akait @alexander-akait
*/
 
"use strict";
 
const Hash = require("../Hash");
 
/** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
 
/* istanbul ignore next */
class DebugHash extends Hash {
    constructor() {
        super();
        this.string = "";
    }
 
    /**
     * 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 (typeof data !== "string") data = data.toString("utf8");
        const prefix = Buffer.from("@webpack-debug-digest@").toString("hex");
        if (data.startsWith(prefix)) {
            data = Buffer.from(data.slice(prefix.length), "hex").toString();
        }
        this.string += `[${data}](${
            /** @type {string} */
            (
                // eslint-disable-next-line unicorn/error-message
                new Error().stack
            ).split("\n", 3)[2]
        })\n`;
        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) {
        return Buffer.from(`@webpack-debug-digest@${this.string}`).toString("hex");
    }
}
 
module.exports = DebugHash;