1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const crypto_1 = require("crypto");
|
4 | const fs = require("fs-extra");
|
5 | const path = require("path");
|
6 | const xxh = require("xxhashjs");
|
7 | const HEXBASE = 16;
|
8 | const defaultHashOptions = {
|
9 | method: 'xxhash32',
|
10 | shrink: 8,
|
11 | append: false
|
12 | };
|
13 | const getxxhash = (content, options) => {
|
14 | const hashFunc = options.method === 'xxhash32' ? xxh.h32 : xxh.h64;
|
15 | const seed = 0;
|
16 | return hashFunc(seed)
|
17 | .update(content)
|
18 | .digest()
|
19 | .toString(HEXBASE);
|
20 | };
|
21 | const getHash = (content, options) => {
|
22 | if (options.method && typeof options.method === 'function') {
|
23 | return options.method(content);
|
24 | }
|
25 | if (options.method && options.method.indexOf('xxhash') === 0) {
|
26 | return getxxhash(content, options);
|
27 | }
|
28 | try {
|
29 | const hashFunc = crypto_1.default.createHash(options.method);
|
30 | return hashFunc.update(content).digest('hex');
|
31 | }
|
32 | catch (e) {
|
33 | return null;
|
34 | }
|
35 | };
|
36 | function hash(content, options) {
|
37 | options = options || defaultHashOptions;
|
38 | let hash = getHash(content, options);
|
39 | if (hash == null) {
|
40 |
|
41 |
|
42 | hash = getHash(content, defaultHashOptions);
|
43 | }
|
44 | return options.shrink ? hash.substr(0, options.shrink) : hash;
|
45 | }
|
46 | exports.default = (file, options) => {
|
47 | const content = fs.readFileSync(file);
|
48 | const ext = path.extname(file);
|
49 | return hash(content, options) + ext;
|
50 | };
|
51 |
|
\ | No newline at end of file |