Skip to content

Commit

Permalink
Drop Node.js 6, add support for filenamePrefix callback (#13)
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
coreyfarrell authored May 9, 2019
1 parent ff41105 commit 9b685fe
Show file tree
Hide file tree
Showing 6 changed files with 1,303 additions and 1,581 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ node_js:
- "node"
- 10
- 8
- 6
after_script:
- 'cat ./coverage/lcov.info | ./node_modules/.bin/coveralls'
35 changes: 18 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ function wrap(opts) {
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} = opts;
const {factory, cacheDir, shouldTransform, disableCache, hashData, onHash, filenamePrefix, ext, salt} = opts;
const cacheDirCreated = opts.createCacheDir === false;
let created = transformFn && cacheDirCreated;
const ext = opts.ext || '';
const salt = opts.salt || '';
const encoding = opts.encoding === 'buffer' ? undefined : opts.encoding || 'utf8';

function transform(input, metadata, hash) {
Expand Down Expand Up @@ -54,22 +61,16 @@ function wrap(opts) {
return transform(input, metadata);
}

let data = [ownHash || getOwnHash(), input];

if (salt) {
data.push(salt);
}

if (hashData) {
data = data.concat(hashData(input, metadata));
}

const data = [
ownHash || getOwnHash(),
input,
salt,
...[].concat(hashData(input, metadata))
];
const hash = hasha(data, {algorithm: 'sha256'});
const cachedPath = path.join(cacheDir, hash + ext);
const cachedPath = path.join(cacheDir, filenamePrefix(metadata) + hash + ext);

if (onHash) {
onHash(input, metadata, hash);
}
onHash(input, metadata, hash);

try {
return fs.readFileSync(cachedPath, encoding);
Expand Down
Loading

0 comments on commit 9b685fe

Please sign in to comment.