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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
/** @typedef {import("./Hash")} Hash */
/** @typedef {import("../../declarations/WebpackOptions").HashFunction} HashFunction */
 
/** @type {typeof import("crypto") | undefined} */
let crypto;
/** @type {typeof import("./hash/xxhash64") | undefined} */
let createXXHash64;
/** @type {typeof import("./hash/md4") | undefined} */
let createMd4;
/** @type {typeof import("./hash/DebugHash") | undefined} */
let DebugHash;
/** @type {typeof import("./hash/BatchedHash") | undefined} */
let BatchedHash;
/** @type {typeof import("./hash/BulkUpdateHash") | undefined} */
let BulkUpdateHash;
 
/**
 * Creates a hash by name or function
 * @param {HashFunction} algorithm the algorithm name or a constructor creating a hash
 * @returns {Hash} the hash
 */
module.exports = (algorithm) => {
    if (typeof algorithm === "function") {
        if (BulkUpdateHash === undefined) {
            BulkUpdateHash = require("./hash/BulkUpdateHash");
        }
        // eslint-disable-next-line new-cap
        return new BulkUpdateHash(() => new algorithm());
    }
    switch (algorithm) {
        // TODO add non-cryptographic algorithm here
        case "debug":
            if (DebugHash === undefined) {
                DebugHash = require("./hash/DebugHash");
            }
            return new DebugHash();
        case "xxhash64":
            if (createXXHash64 === undefined) {
                createXXHash64 = require("./hash/xxhash64");
                if (BatchedHash === undefined) {
                    BatchedHash = require("./hash/BatchedHash");
                }
            }
            return new /** @type {typeof import("./hash/BatchedHash")} */ (
                BatchedHash
            )(createXXHash64());
        case "md4":
            if (createMd4 === undefined) {
                createMd4 = require("./hash/md4");
                if (BatchedHash === undefined) {
                    BatchedHash = require("./hash/BatchedHash");
                }
            }
            return new /** @type {typeof import("./hash/BatchedHash")} */ (
                BatchedHash
            )(createMd4());
        case "native-md4":
            if (crypto === undefined) crypto = require("crypto");
            if (BulkUpdateHash === undefined) {
                BulkUpdateHash = require("./hash/BulkUpdateHash");
            }
            return new BulkUpdateHash(
                () =>
                    /** @type {Hash} */ (
                        /** @type {typeof import("crypto")} */
                        (crypto).createHash("md4")
                    ),
                "md4"
            );
        default:
            if (crypto === undefined) crypto = require("crypto");
            if (BulkUpdateHash === undefined) {
                BulkUpdateHash = require("./hash/BulkUpdateHash");
            }
            return new BulkUpdateHash(
                () =>
                    /** @type {Hash} */ (
                        /** @type {typeof import("crypto")} */
                        (crypto).createHash(algorithm)
                    ),
                algorithm
            );
    }
};