From 1a3c535e8912f1fab48a16f679a319401b6ec951 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 19 Oct 2018 14:06:14 +0300 Subject: [PATCH] chore(route) es2018-ify --- server/route.js | 70 +++++++++++--------- test/server/route.js => server/route.spec.js | 43 ++++++------ 2 files changed, 63 insertions(+), 50 deletions(-) rename test/server/route.js => server/route.spec.js (90%) diff --git a/server/route.js b/server/route.js index a03cf373..818c309c 100644 --- a/server/route.js +++ b/server/route.js @@ -4,6 +4,9 @@ const DIR_SERVER = './'; const DIR_COMMON = '../common/'; const fs = require('fs'); +const { + promisify, +} = require('util'); const flop = require('flop'); const ponse = require('ponse'); @@ -12,6 +15,7 @@ const format = require('format-io'); const squad = require('squad'); const apart = require('apart'); const currify = require('currify'); +const tryToCatch = require('try-to-catch'); const config = require(DIR_SERVER + 'config'); const root = require(DIR_SERVER + 'root'); @@ -29,20 +33,30 @@ const sendIndex = (params, data) => { ponse.send(data, ponseParams); }; -const FS = CloudFunc.FS; +const {FS} = CloudFunc; const Columns = require('./columns'); const Template = require('./template'); +const read = promisify(flop.read); +const realpath = promisify(fs.realpath); + /** * routing of server queries */ -module.exports = currify((options, request, response, callback) => { - const html = options.html; - - let name = ponse.getPathName(request); - +module.exports = currify((options, request, response, next) => { + const name = ponse.getPathName(request); const isFS = RegExp('^/$|^' + FS).test(name); + + if (!isFS) + return next(); + + route(options, request, response) + .catch(next); +}); + +async function route(options, request, response) { + const name = ponse.getPathName(request); const gzip = true; const p = { request, @@ -51,33 +65,29 @@ module.exports = currify((options, request, response, callback) => { name, }; - if (!isFS) - return callback(); + const rootName = name.replace(CloudFunc.FS, '') || '/'; + const fullPath = root(rootName); - name = name.replace(CloudFunc.FS, '') || '/'; - const fullPath = root(name); + const [error, dir] = await tryToCatch(read, fullPath); + const {html} = options; - flop.read(fullPath, (error, dir) => { - if (dir) - dir.path = format.addSlashToEnd(name); - - if (!error) - return sendIndex(p, buildIndex(html, dir)); - - if (error.code !== 'ENOTDIR') - return ponse.sendError(error, p); - - fs.realpath(fullPath, (error, pathReal) => { - if (!error) - p.name = pathReal; - else - p.name = name; - - p.gzip = false; - ponse.sendFile(p); - }); + if (!error) + return sendIndex(p, buildIndex(html, { + ...dir, + path: format.addSlashToEnd(rootName), + })); + + if (error.code !== 'ENOTDIR') + return ponse.sendError(error, p); + + const [realPathError, pathReal] = await tryToCatch(realpath, fullPath); + + ponse.sendFile({ + ...p, + name: realPathError ? name : pathReal, + gzip: false }); -}); +} /** * additional processing of index file diff --git a/test/server/route.js b/server/route.spec.js similarity index 90% rename from test/server/route.js rename to server/route.spec.js index 060d997b..5cd12303 100644 --- a/test/server/route.js +++ b/server/route.spec.js @@ -4,17 +4,19 @@ const path = require('path'); const fs = require('fs'); const test = require('tape'); -const {promisify} = require('es6-promisify'); +const {promisify} = require('util'); const pullout = require('pullout'); const request = require('request'); const mockRequire = require('mock-require'); +const {reRequire} = mockRequire; const clear = require('clear-module'); const rootDir = path.join(__dirname, '../..'); +const fixtureDir = path.join(__dirname, '..', 'test', 'fixture'); -const routePath = `${rootDir}/server/route`; -const cloudcmdPath = `${rootDir}/server/cloudcmd`; -const beforePath = path.join(__dirname, '../before'); +const routePath = './route'; +const cloudcmdPath = './cloudcmd'; +const beforePath = path.join(__dirname, '../test/before'); const {connect} = require(beforePath); @@ -28,7 +30,7 @@ const get = promisify((url, fn) => { const getStr = (url) => { return get(url) .then(warp(_pullout, 'string')) - .catch(console.log); + .catch(console.error); }; test('cloudcmd: route: buttons: no console', async (t) => { @@ -158,7 +160,7 @@ test('cloudcmd: route: keys panel', async (t) => { }); test('cloudcmd: route: file: fs', async (t) => { - const root = path.join(__dirname, '..', 'fixture', 'empty-file'); + const root = path.join(fixtureDir, 'empty-file'); const config = { root, }; @@ -173,8 +175,8 @@ test('cloudcmd: route: file: fs', async (t) => { }); test('cloudcmd: route: symlink', async (t) => { - const emptyDir = path.join(__dirname, '..', 'fixture', 'empty-dir'); - const root = path.join(__dirname, '..', 'fixture'); + const emptyDir = path.join(fixtureDir, 'empty-dir'); + const root = fixtureDir const symlink = path.join(root, 'symlink-dir'); const config = { @@ -194,7 +196,7 @@ test('cloudcmd: route: symlink', async (t) => { }); test('cloudcmd: route: not found', async (t) => { - const root = path.join(__dirname, '..', 'fixture'); + const root = fixtureDir; const config = { root, }; @@ -217,14 +219,19 @@ test('cloudcmd: route: realpath: error', async (t) => { fs.realpath = realpath; }; - const root = path.join(__dirname, '..', 'fixture'); const config = { - root, + root: fixtureDir, }; + reRequire('./route'); + reRequire('./cloudcmd'); + + const {connect} = reRequire(beforePath); const {port, done} = await connect({config}); const data = await getStr(`http://localhost:${port}/fs/empty-file`); + fs.realpath = realpath; + t.ok(/^ENOENT/.test(data), 'should return error'); t.end(); @@ -247,20 +254,16 @@ test('cloudcmd: route: sendIndex: encode', async (t) => { read }); - clear(routePath); - clear('../../server/cloudcmd'); - clear(beforePath); + reRequire(routePath); + reRequire(cloudcmdPath); - const {connect} = require(beforePath); + const {connect} = reRequire(beforePath); const {port, done} = await connect(); const data = await getStr(`http://localhost:${port}`); t.ok(data.includes(nameEncoded), 'should encode name'); mockRequire.stop('flop'); - clear(routePath); - clear('../../server/cloudcmd'); - clear(beforePath); await done(); t.end(); @@ -282,7 +285,7 @@ test('cloudcmd: route: sendIndex: encode: not encoded', async (t) => { }); clear(routePath); - clear('../../server/cloudcmd'); + clear(cloudcmdPath); clear(beforePath); const {connect} = require(beforePath); @@ -293,7 +296,7 @@ test('cloudcmd: route: sendIndex: encode: not encoded', async (t) => { mockRequire.stop('flop'); clear(routePath); - clear('../../server/cloudcmd'); + clear(cloudcmdPath); clear(beforePath); await done();