UNPKG

1.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.cache = void 0;
4exports.sha1 = sha1;
5var crypto_1 = require("crypto");
6/**
7 * @internal
8 */
9// stores hashes made out of only one argument being a string
10exports.cache = Object.create(null);
11/**
12 * @internal
13 */
14function sha1() {
15 var data = [];
16 for (var _i = 0; _i < arguments.length; _i++) {
17 data[_i] = arguments[_i];
18 }
19 var canCache = data.length === 1 && typeof data[0] === 'string';
20 // caching
21 var cacheKey;
22 if (canCache) {
23 cacheKey = data[0];
24 if (cacheKey in exports.cache) {
25 return exports.cache[cacheKey];
26 }
27 }
28 // we use SHA1 because it's the fastest provided by node
29 // and we are not concerned about security here
30 var hash = (0, crypto_1.createHash)('sha1');
31 data.forEach(function (item) {
32 if (typeof item === 'string')
33 hash.update(item, 'utf8');
34 else
35 hash.update(item);
36 });
37 var res = hash.digest('hex').toString();
38 if (canCache) {
39 exports.cache[cacheKey] = res;
40 }
41 return res;
42}