From c95d79f03611de317078e7b40c09b60a3b406b19 Mon Sep 17 00:00:00 2001 From: maxlath Date: Fri, 30 Dec 2016 23:21:51 +0100 Subject: [PATCH 1/2] parallelizing file existance check using promises --- routes/misc.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/routes/misc.js b/routes/misc.js index 8a5c3a3f..1bf64a07 100644 --- a/routes/misc.js +++ b/routes/misc.js @@ -3,6 +3,10 @@ var router = require('express').Router() var renderer = require('../lib/renderer') var fs = require('fs') var models = require('../lib/models') +var Promiser = require('bluebird') +// using fs.access instead of the deprecated fs.exists: +// https://nodejs.org/api/fs.html#fs_fs_exists_path_callback +var exists = Promiser.promisify(fs.access) models.use(Git) @@ -22,24 +26,25 @@ function _postPreview (req, res) { function _getExistence (req, res) { if (!req.query.data) { - res.send(JSON.stringify({data: []})) + res.json({data: []}) return } var result = [] - var page - var n = req.query.data.length - - req.query.data.forEach(function (pageName, idx) { - (function (name, index) { - page = new models.Page(name) - if (!fs.existsSync(page.pathname)) { - result.push(name) - } - if (index === (n - 1)) { - res.send(JSON.stringify({data: result})) + + Promiser.all(req.query.data.map(function (pageName) { + var page = new models.Page(pageName) + return exists(page.pathname) + .catch(function (err) { + if (err.code === 'ENOENT') { + result.push(pageName) + } else { + console.error(err) } - }(pageName, idx)) + }) + })) + .then(function () { + res.json({data: result}) }) } From 4d5495f84dcf179282d88a93ca9092447a853c9f Mon Sep 17 00:00:00 2001 From: maxlath Date: Sun, 8 Jan 2017 16:52:47 +0100 Subject: [PATCH 2/2] replacing all fs.exists by function from lib/file_exists based on fs.access --- jingo | 3 ++- lib/components.js | 3 ++- lib/file_exists.js | 30 ++++++++++++++++++++++++++++++ lib/models.js | 3 ++- routes/misc.js | 13 ++++--------- 5 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 lib/file_exists.js diff --git a/jingo b/jingo index 1f594f15..f27d2006 100755 --- a/jingo +++ b/jingo @@ -15,6 +15,7 @@ var fs = require('fs') var os = require('os') var semver = require('semver') var pkg = require('./package') +var fileExists = require('./lib/file_exists') global.Git = require('./lib/gitmech') @@ -35,7 +36,7 @@ if (program.hashString) { process.exit(0) } -if (!program.config || !fs.existsSync(program.config)) { +if (!program.config || !fileExists.sync(program.config)) { program.help() process.exit(-1) } diff --git a/lib/components.js b/lib/components.js index bd62d6b8..e982c795 100644 --- a/lib/components.js +++ b/lib/components.js @@ -4,6 +4,7 @@ var models = require('./models') var Promiser = require('bluebird') var renderer = require('./renderer') var Configurable = require('./configurable') +var fileExists = require('./file_exists') models.use(Git) @@ -34,7 +35,7 @@ Component.prototype.exists = function () { return this._exists } - this._exists = fs.existsSync(Git.absPath(this.file)) + this._exists = fileExists.sync(Git.absPath(this.file)) if (!this._exists) { this.cache = null diff --git a/lib/file_exists.js b/lib/file_exists.js new file mode 100644 index 00000000..a1d70943 --- /dev/null +++ b/lib/file_exists.js @@ -0,0 +1,30 @@ +var fs = require('fs') +var Promiser = require('bluebird') +// using fs.access instead of the deprecated fs.exists: +// https://nodejs.org/api/fs.html#fs_fs_exists_path_callback +var exists = Promiser.promisify(fs.access) + +module.exports = { + sync: function (file) { + try { + fs.accessSync(file) + // If it didn't throw, the file exists + return true + } catch (err) { + return catchEnoent(err) + } + }, + async: function (file) { + return exists(file) + .then(function (res) { return true }) + .catch(catchEnoent) + } +} + +function catchEnoent (err) { + if (err.code === 'ENOENT') { + return false + } else { + throw err + } +} diff --git a/lib/models.js b/lib/models.js index 7907ddc3..84e3a3a1 100644 --- a/lib/models.js +++ b/lib/models.js @@ -4,6 +4,7 @@ var namer = require('./namer') var fs = require('fs') var Configurable = require('./configurable') var locker = require('./locker') +var fileExists = require('./file_exists') var gitmech @@ -61,7 +62,7 @@ Page.prototype.renameTo = function (newName) { return new Promiserr(function (resolve, reject) { // Cannot rename if the file already exists - if (fs.existsSync(gitmech.absPath(newFilename))) { + if (fileExists.sync(gitmech.absPath(newFilename))) { reject() return } diff --git a/routes/misc.js b/routes/misc.js index 1bf64a07..535b573f 100644 --- a/routes/misc.js +++ b/routes/misc.js @@ -1,12 +1,9 @@ /* global Git */ var router = require('express').Router() var renderer = require('../lib/renderer') -var fs = require('fs') var models = require('../lib/models') var Promiser = require('bluebird') -// using fs.access instead of the deprecated fs.exists: -// https://nodejs.org/api/fs.html#fs_fs_exists_path_callback -var exists = Promiser.promisify(fs.access) +var fileExists = require('../lib/file_exists') models.use(Git) @@ -34,12 +31,10 @@ function _getExistence (req, res) { Promiser.all(req.query.data.map(function (pageName) { var page = new models.Page(pageName) - return exists(page.pathname) - .catch(function (err) { - if (err.code === 'ENOENT') { + return fileExists.async(page.pathname) + .then(function (exists) { + if (!exists) { result.push(pageName) - } else { - console.error(err) } }) }))