From 520d1340aed19f41b149cf414e3a6f06f8355ba7 Mon Sep 17 00:00:00 2001 From: Damian Kaczmarek Date: Sat, 20 Oct 2018 01:21:27 -0500 Subject: [PATCH] Lazy-load file hashes --- index.js | 34 +++++++++-------- package-lock.json | 97 ++++++++++++++++++++++++++++++++++++++++++++++- package.json | 1 + 3 files changed, 115 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index afbdb9d..a634c0c 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const path = require('path'); const url = require('url'); const ignoredDirectories = require('ignore-by-default').directories(); +const memoizee = require('memoizee'); const send = require('send'); const staticify = (root, options) => { @@ -28,6 +29,18 @@ const staticify = (root, options) => { const opts = setOptions(options); + const cachedMakeHash = memoizee(filePath => { + const fileStr = fs.readFileSync(filePath, 'utf8'); + let hash = crypto.createHash('md5') + .update(fileStr, 'utf8') + .digest('hex'); + + if (opts.shortHash) { + hash = hash.slice(0, 7); + } + return hash; + }); + // Walks the directory tree, finding files, generating a version hash const buildVersionHash = (directory, root, vers) => { root = root || directory; @@ -40,22 +53,13 @@ const staticify = (root, options) => { const files = fs.readdirSync(directory); files.forEach(file => { - const filePath = path.posix.join(directory, file); - const stat = fs.statSync(filePath); + const absFilePath = path.posix.join(directory, file); + const stat = fs.statSync(absFilePath); if (stat.isDirectory()) { - buildVersionHash(filePath, root, vers); // Whee! + buildVersionHash(absFilePath, root, vers); // Whee! } else if (stat.isFile()) { - const fileStr = fs.readFileSync(filePath, 'utf8'); - let hash = crypto.createHash('md5') - .update(fileStr, 'utf8') - .digest('hex'); - - if (opts.shortHash) { - hash = hash.slice(0, 7); - } - - vers[`/${path.posix.relative(root, filePath)}`] = hash; + vers[`/${path.posix.relative(root, absFilePath)}`] = {absFilePath}; } }); @@ -72,8 +76,8 @@ const staticify = (root, options) => { const fileName = path.basename(p); const fileNameParts = fileName.split('.'); - - fileNameParts.push(versions[p], fileNameParts.pop()); + const {absFilePath} = versions[p]; + fileNameParts.push(cachedMakeHash(absFilePath), fileNameParts.pop()); return path.posix.join(opts.pathPrefix, path.dirname(p), fileNameParts.join('.')); }; diff --git a/package-lock.json b/package-lock.json index c56a9aa..eef7059 100644 --- a/package-lock.json +++ b/package-lock.json @@ -573,6 +573,14 @@ "array-find-index": "^1.0.1" } }, + "d": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/d/-/d-1.0.0.tgz", + "integrity": "sha1-dUu1v+VUUdpppYuU1F9MWwRi1Y8=", + "requires": { + "es5-ext": "^0.10.9" + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -811,6 +819,46 @@ "is-arrayish": "^0.2.1" } }, + "es5-ext": { + "version": "0.10.46", + "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.46.tgz", + "integrity": "sha512-24XxRvJXNFwEMpJb3nOkiRJKRoupmjYmOPVlI65Qy2SrtxwOTB+g6ODjBKOtwEHbYrhWRty9xxOWLNdClT2djw==", + "requires": { + "es6-iterator": "~2.0.3", + "es6-symbol": "~3.1.1", + "next-tick": "1" + } + }, + "es6-iterator": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", + "integrity": "sha1-p96IkUGgWpSwhUQDstCg+/qY87c=", + "requires": { + "d": "1", + "es5-ext": "^0.10.35", + "es6-symbol": "^3.1.1" + } + }, + "es6-symbol": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.1.tgz", + "integrity": "sha1-vwDvT9q2uhtG7Le2KbTH7VcVzHc=", + "requires": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, + "es6-weak-map": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/es6-weak-map/-/es6-weak-map-2.0.2.tgz", + "integrity": "sha1-XjqzIlH/0VOKH45f+hNXdy+S2W8=", + "requires": { + "d": "1", + "es5-ext": "^0.10.14", + "es6-iterator": "^2.0.1", + "es6-symbol": "^3.1.1" + } + }, "escape-html": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", @@ -1155,6 +1203,15 @@ "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" }, + "event-emitter": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", + "integrity": "sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=", + "requires": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, "execa": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/execa/-/execa-0.7.0.tgz", @@ -2003,8 +2060,7 @@ "is-promise": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.1.0.tgz", - "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=", - "dev": true + "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=" }, "is-proto-prop": { "version": "1.0.1", @@ -2258,6 +2314,14 @@ "yallist": "^2.1.2" } }, + "lru-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/lru-queue/-/lru-queue-0.1.0.tgz", + "integrity": "sha1-Jzi9nw089PhEkMVzbEhpmsYyzaM=", + "requires": { + "es5-ext": "~0.10.2" + } + }, "make-dir": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.3.0.tgz", @@ -2296,6 +2360,21 @@ "object-visit": "^1.0.0" } }, + "memoizee": { + "version": "0.4.14", + "resolved": "https://registry.npmjs.org/memoizee/-/memoizee-0.4.14.tgz", + "integrity": "sha512-/SWFvWegAIYAO4NQMpcX+gcra0yEZu4OntmUdrBaWrJncxOqAziGFlHxc7yjKVK2uu3lpPW27P27wkR82wA8mg==", + "requires": { + "d": "1", + "es5-ext": "^0.10.45", + "es6-weak-map": "^2.0.2", + "event-emitter": "^0.3.5", + "is-promise": "^2.1", + "lru-queue": "0.1", + "next-tick": "1", + "timers-ext": "^0.1.5" + } + }, "meow": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/meow/-/meow-5.0.0.tgz", @@ -2544,6 +2623,11 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, + "next-tick": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", + "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=" + }, "nice-try": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", @@ -3656,6 +3740,15 @@ "integrity": "sha1-8y6srFoXW+ol1/q1Zas+2HQe9W8=", "dev": true }, + "timers-ext": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/timers-ext/-/timers-ext-0.1.7.tgz", + "integrity": "sha512-b85NUNzTSdodShTIbky6ZF02e8STtVVfD+fu4aXXShEELpozH+bCpJLYMPZbsABN2wDH7fJpqIoXxJpzbf0NqQ==", + "requires": { + "es5-ext": "~0.10.46", + "next-tick": "1" + } + }, "tmp": { "version": "0.0.33", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", diff --git a/package.json b/package.json index 8671930..346ae2c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ ], "dependencies": { "ignore-by-default": "^1.0.1", + "memoizee": "^0.4.14", "send": "^0.16.2" }, "devDependencies": {