-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
102 lines (86 loc) · 2.39 KB
/
index.js
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
92
93
94
95
96
97
98
99
100
101
102
'use strict';
const fs = require('fs');
const path = require('path');
const hasha = require('hasha');
const makeDir = require('make-dir');
const writeFileAtomic = require('write-file-atomic');
const packageHash = require('package-hash');
let ownHash = '';
function getOwnHash() {
ownHash = packageHash.sync(path.join(__dirname, 'package.json'));
return ownHash;
}
function wrap(opts) {
if (!(opts.factory || opts.transform) || (opts.factory && opts.transform)) {
throw new Error('Specify factory or transform but not both');
}
if (typeof opts.cacheDir !== 'string' && !opts.disableCache) {
throw new Error('cacheDir must be a string');
}
opts = {
ext: '',
salt: '',
hashData: () => [],
filenamePrefix: () => '',
onHash: () => {},
...opts
};
let transformFn = opts.transform;
const {factory, cacheDir, shouldTransform, disableCache, hashData, onHash, filenamePrefix, ext, salt} = opts;
const cacheDirCreated = opts.createCacheDir === false;
let created = transformFn && cacheDirCreated;
const encoding = opts.encoding === 'buffer' ? undefined : opts.encoding || 'utf8';
function transform(input, metadata, hash) {
if (!created) {
if (!cacheDirCreated && !disableCache) {
makeDir.sync(cacheDir);
}
if (!transformFn) {
transformFn = factory(cacheDir);
}
created = true;
}
return transformFn(input, metadata, hash);
}
return function (input, metadata) {
if (shouldTransform && !shouldTransform(input, metadata)) {
return input;
}
if (disableCache) {
return transform(input, metadata);
}
const data = [
ownHash || getOwnHash(),
input,
salt,
...[].concat(hashData(input, metadata))
];
const hash = hasha(data, {algorithm: 'sha256'});
const cachedPath = path.join(cacheDir, filenamePrefix(metadata) + hash + ext);
onHash(input, metadata, hash);
let result;
let retry = 0;
/* eslint-disable-next-line no-constant-condition */
while (true) {
try {
return fs.readFileSync(cachedPath, encoding);
} catch (_) {
if (!result) {
result = transform(input, metadata, hash);
}
try {
writeFileAtomic.sync(cachedPath, result, {encoding});
return result;
} catch (error) {
/* Likely https://github.com/npm/write-file-atomic/issues/28
* Make up to 3 attempts to read or write the cache. */
retry++;
if (retry > 3) {
throw error;
}
}
}
}
};
}
module.exports = wrap;