Skip to content

Commit

Permalink
Merge 449bf3e into adaf32c
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Oct 29, 2019
2 parents adaf32c + 449bf3e commit 0554048
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 107 deletions.
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const path = require('path');
const url = require('url');

const ignoredDirectories = require('ignore-by-default').directories();
const memoizee = require('memoizee');
const send = require('send');

const MAX_AGE = 1000 * 60 * 60 * 24 * 365; // 1 year in milliseconds
Expand All @@ -16,6 +15,8 @@ const LONG_LEN = 32;
const staticify = (root, options) => {
let sendOptsNonVersioned;

const hashCache = new Set();

const setOptions = (opts = {}) => {
let defaultOptions = {
includeAll: opts.includeAll || false,
Expand All @@ -38,7 +39,11 @@ const staticify = (root, options) => {

const opts = setOptions(options);

const cachedMakeHash = memoizee(filePath => {
const getHash = filePath => {
if (hashCache.has(filePath)) {
return;
}

const fileStr = fs.readFileSync(filePath, 'utf8');
let hash = crypto.createHash('md5')
.update(fileStr, 'utf8')
Expand All @@ -49,7 +54,9 @@ const staticify = (root, options) => {
}

return hash;
});
};

hashCache.add(getHash);

// Walks the directory tree, finding files, generating a version hash
const buildVersionHash = (directory, root = directory, vers = {}) => {
Expand Down Expand Up @@ -84,7 +91,7 @@ const staticify = (root, options) => {
const fileName = path.basename(p);
const fileNameParts = fileName.split('.');
const {absFilePath} = versions[p];
fileNameParts.push(cachedMakeHash(absFilePath), fileNameParts.pop());
fileNameParts.push(getHash(absFilePath), fileNameParts.pop());

return path.posix.join(opts.pathPrefix, path.dirname(p), fileNameParts.join('.'));
};
Expand Down Expand Up @@ -146,7 +153,7 @@ const staticify = (root, options) => {
};

const refresh = () => {
cachedMakeHash.clear();
hashCache.clear();
versions = buildVersionHash(root);
};

Expand Down
103 changes: 2 additions & 101 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
],
"dependencies": {
"ignore-by-default": "^1.0.1",
"memoizee": "^0.4.14",
"send": "^0.17.1"
},
"devDependencies": {
Expand Down

0 comments on commit 0554048

Please sign in to comment.