UNPKG

4.82 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Hash = require("./Hash");
9
10const BULK_SIZE = 2000;
11
12// We are using an object instead of a Map as this will stay static during the runtime
13// so access to it can be optimized by v8
14const digestCaches = {};
15
16class BulkUpdateDecorator extends Hash {
17 /**
18 * @param {Hash | function(): Hash} hashOrFactory function to create a hash
19 * @param {string=} hashKey key for caching
20 */
21 constructor(hashOrFactory, hashKey) {
22 super();
23 this.hashKey = hashKey;
24 if (typeof hashOrFactory === "function") {
25 this.hashFactory = hashOrFactory;
26 this.hash = undefined;
27 } else {
28 this.hashFactory = undefined;
29 this.hash = hashOrFactory;
30 }
31 this.buffer = "";
32 }
33
34 /**
35 * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
36 * @param {string|Buffer} data data
37 * @param {string=} inputEncoding data encoding
38 * @returns {this} updated hash
39 */
40 update(data, inputEncoding) {
41 if (
42 inputEncoding !== undefined ||
43 typeof data !== "string" ||
44 data.length > BULK_SIZE
45 ) {
46 if (this.hash === undefined) this.hash = this.hashFactory();
47 if (this.buffer.length > 0) {
48 this.hash.update(this.buffer);
49 this.buffer = "";
50 }
51 this.hash.update(data, inputEncoding);
52 } else {
53 this.buffer += data;
54 if (this.buffer.length > BULK_SIZE) {
55 if (this.hash === undefined) this.hash = this.hashFactory();
56 this.hash.update(this.buffer);
57 this.buffer = "";
58 }
59 }
60 return this;
61 }
62
63 /**
64 * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
65 * @param {string=} encoding encoding of the return value
66 * @returns {string|Buffer} digest
67 */
68 digest(encoding) {
69 let digestCache;
70 const buffer = this.buffer;
71 if (this.hash === undefined) {
72 // short data for hash, we can use caching
73 const cacheKey = `${this.hashKey}-${encoding}`;
74 digestCache = digestCaches[cacheKey];
75 if (digestCache === undefined) {
76 digestCache = digestCaches[cacheKey] = new Map();
77 }
78 const cacheEntry = digestCache.get(buffer);
79 if (cacheEntry !== undefined) return cacheEntry;
80 this.hash = this.hashFactory();
81 }
82 if (buffer.length > 0) {
83 this.hash.update(buffer);
84 }
85 const digestResult = this.hash.digest(encoding);
86 const result =
87 typeof digestResult === "string" ? digestResult : digestResult.toString();
88 if (digestCache !== undefined) {
89 digestCache.set(buffer, result);
90 }
91 return result;
92 }
93}
94
95/* istanbul ignore next */
96class DebugHash extends Hash {
97 constructor() {
98 super();
99 this.string = "";
100 }
101
102 /**
103 * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
104 * @param {string|Buffer} data data
105 * @param {string=} inputEncoding data encoding
106 * @returns {this} updated hash
107 */
108 update(data, inputEncoding) {
109 if (typeof data !== "string") data = data.toString("utf-8");
110 if (data.startsWith("debug-digest-")) {
111 data = Buffer.from(data.slice("debug-digest-".length), "hex").toString();
112 }
113 this.string += `[${data}](${new Error().stack.split("\n", 3)[2]})\n`;
114 return this;
115 }
116
117 /**
118 * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
119 * @param {string=} encoding encoding of the return value
120 * @returns {string|Buffer} digest
121 */
122 digest(encoding) {
123 return "debug-digest-" + Buffer.from(this.string).toString("hex");
124 }
125}
126
127let crypto = undefined;
128let createXXHash64 = undefined;
129let createMd4 = undefined;
130let BatchedHash = undefined;
131
132/**
133 * Creates a hash by name or function
134 * @param {string | typeof Hash} algorithm the algorithm name or a constructor creating a hash
135 * @returns {Hash} the hash
136 */
137module.exports = algorithm => {
138 if (typeof algorithm === "function") {
139 return new BulkUpdateDecorator(() => new algorithm());
140 }
141 switch (algorithm) {
142 // TODO add non-cryptographic algorithm here
143 case "debug":
144 return new DebugHash();
145 case "xxhash64":
146 if (createXXHash64 === undefined) {
147 createXXHash64 = require("./hash/xxhash64");
148 if (BatchedHash === undefined) {
149 BatchedHash = require("./hash/BatchedHash");
150 }
151 }
152 return new BatchedHash(createXXHash64());
153 case "md4":
154 if (createMd4 === undefined) {
155 createMd4 = require("./hash/md4");
156 if (BatchedHash === undefined) {
157 BatchedHash = require("./hash/BatchedHash");
158 }
159 }
160 return new BatchedHash(createMd4());
161 case "native-md4":
162 if (crypto === undefined) crypto = require("crypto");
163 return new BulkUpdateDecorator(() => crypto.createHash("md4"), "md4");
164 default:
165 if (crypto === undefined) crypto = require("crypto");
166 return new BulkUpdateDecorator(
167 () => crypto.createHash(algorithm),
168 algorithm
169 );
170 }
171};