From 4e99372e6fb9a72075b91389a9a96dc65c8e5d4a Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 11 May 2018 13:03:58 +0300 Subject: [PATCH] feature(cloudcmd) move out dist from route --- client/listeners/index.js | 2 +- html/index.html | 2 +- server/cloudcmd.js | 16 +++- server/route.js | 153 +++++++++++++++----------------------- test/server/cloudcmd.js | 19 +++++ test/server/route.js | 65 ---------------- 6 files changed, 94 insertions(+), 163 deletions(-) diff --git a/client/listeners/index.js b/client/listeners/index.js index 4408d4ca..7011e78b 100644 --- a/client/listeners/index.js +++ b/client/listeners/index.js @@ -172,7 +172,7 @@ function isNoCurrent(panel) { const namePanel = panel.getAttribute('data-name'); const nameInfoPanel = infoPanel.getAttribute('data-name'); - return namePanel !== nameInfoPanel + return namePanel !== nameInfoPanel; } function decodePath(path){ diff --git a/html/index.html b/html/index.html index 758a4627..77bf3061 100644 --- a/html/index.html +++ b/html/index.html @@ -30,7 +30,7 @@ - + diff --git a/server/cloudcmd.js b/server/cloudcmd.js index 4b88cd64..7e82132e 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -5,6 +5,8 @@ const DIR_ROOT = DIR + '../'; const DIR_COMMON = DIR + '../common/'; const util = require('util'); +const path = require('path'); +const fs = require('fs'); const cloudfunc = require(DIR_COMMON + 'cloudfunc'); const authentication = require(DIR + 'auth'); @@ -29,6 +31,12 @@ const deepword = require('deepword'); const nomine = require('nomine'); const fileop = require('@cloudcmd/fileop'); +const isDev = process.env.NODE_ENV === 'development'; +const getDist = (isDev) => isDev ? 'dist-dev' : 'dist'; + +const getIndexPath = (isDev) => path.join(DIR, '..', `${getDist(isDev)}/index.html`); +const defaultHtml = fs.readFileSync(getIndexPath(isDev), 'utf8'); + const auth = currify(_auth); const setUrl = currify(_setUrl); @@ -37,8 +45,6 @@ const root = () => config('root'); const notEmpty = (a) => a; const clean = (a) => a.filter(notEmpty); -const isDev = process.env.NODE_ENV === 'development'; - const deprecateOnePanelMode = (value) => { const noop = () => {}; @@ -81,6 +87,8 @@ module.exports = (params) => { return cloudcmd(prefix, plugins, modules); }; +module.exports._getIndexPath = getIndexPath; + function defaultValue(name, options) { const value = options[name]; const previous = config(name); @@ -214,7 +222,9 @@ function cloudcmd(prefix, plugins, modules) { }), rest, - route, + route({ + html: defaultHtml + }), join({ dir, diff --git a/server/route.js b/server/route.js index 81ae7c1e..8ecda5db 100644 --- a/server/route.js +++ b/server/route.js @@ -1,11 +1,9 @@ 'use strict'; -const DIR = __dirname + '/../'; const DIR_SERVER = './'; const DIR_COMMON = '../common/'; const fs = require('fs'); -const path = require('path'); const flop = require('flop'); const ponse = require('ponse'); @@ -13,6 +11,7 @@ const rendy = require('rendy'); const format = require('format-io'); const squad = require('squad/legacy'); const apart = require('apart'); +const currify = require('currify/legacy'); const config = require(DIR_SERVER + 'config'); const root = require(DIR_SERVER + 'root'); @@ -20,22 +19,64 @@ const prefixer = require(DIR_SERVER + 'prefixer'); const CloudFunc = require(DIR_COMMON + 'cloudfunc'); const prefix = squad(prefixer, apart(config, 'prefix')); -const isDev = process.env.NODE_ENV === 'development'; -const getDist = (isDev) => isDev ? 'dist-dev' : 'dist'; -module.exports._getIndexPath = getIndexPath; -function getIndexPath(isDev) { - const dist = getDist(isDev); - return path.join(DIR, `${dist}/index.html`); -} +const sendIndex = (params, data) => { + const ponseParams = Object.assign({}, params, { + name: 'index.html' + }); + + ponse.send(data, ponseParams); +}; const FS = CloudFunc.FS; const Columns = require('./columns'); const Template = require('./template'); -module.exports = route; -module.exports._getIndexPath = getIndexPath; +/** + * routing of server queries + */ +module.exports = currify((options, request, response, callback) => { + const html = options.html; + + let name = ponse.getPathName(request); + + const isFS = RegExp('^/$|^' + FS).test(name); + const gzip = true; + const p = { + request, + response, + gzip, + name, + }; + + if (!isFS) + return callback(); + + name = name.replace(CloudFunc.FS, '') || '/'; + const fullPath = root(name); + + 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); + }); + }); +}); /** * additional processing of index file @@ -103,79 +144,16 @@ function indexProcessing(options) { return data; } -const sendIndex = (params) => (error, data) => { - const ponseParams = Object.assign({}, params, { - name: getIndexPath(isDev) +function buildIndex(html, json) { + const panel = CloudFunc.buildFromJSON({ + data: json, + prefix: prefix(), + template: Template, }); - if (error) - return ponse.sendError(error, ponseParams); - - ponse.send(data, ponseParams); -}; - -/** - * routing of server queries - */ -function route(request, response, callback) { - check(request, response, callback); - - let name = ponse.getPathName(request); - - const isFS = RegExp('^/$|^' + FS).test(name); - const gzip = true; - const p = { - request, - response, - gzip, - name, - }; - - if (!isFS) - return callback(); - - name = name.replace(CloudFunc.FS, '') || '/'; - const fullPath = root(name); - - flop.read(fullPath, (error, dir) => { - if (dir) - dir.path = format.addSlashToEnd(name); - - if (!error) - return buildIndex(dir, sendIndex(p)); - - 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); - }); - }); -} - -function buildIndex(json, callback) { - fs.readFile(getIndexPath(isDev), 'utf8', (error, template) => { - if (error) - return callback(error); - - const panel = CloudFunc.buildFromJSON({ - data: json, - prefix: prefix(), - template: Template, - }); - - const data = indexProcessing({ - panel, - data: template, - }); - - callback(null, data); + return indexProcessing({ + panel, + data: html, }); } @@ -195,14 +173,3 @@ function hideKeysPanel(html) { return html.replace(RegExp(from), to); } -function check(req, res, next) { - if (!req) - throw Error('req could not be empty!'); - - if (!res) - throw Error('res could not be empty!'); - - if (typeof next !== 'function') - throw Error('next should be function!'); -} - diff --git a/test/server/cloudcmd.js b/test/server/cloudcmd.js index 93b2000a..852c2b93 100644 --- a/test/server/cloudcmd.js +++ b/test/server/cloudcmd.js @@ -1,5 +1,7 @@ 'use strict'; +const path = require('path'); + const test = require('tape'); const diff = require('sinon-called-with-diff'); const sinon = diff(require('sinon')); @@ -201,6 +203,23 @@ function credentials() { return set(reset); } +test('cloudcmd: getIndexPath: production', (t) => { + const isDev = false; + const name = path.join(__dirname, '..', '..', 'dist', 'index.html'); + + t.equal(cloudcmd._getIndexPath(isDev), name); + t.end(); +}); + +test('cloudcmd: getIndexPath: development', (t) => { + const isDev = true; + const name = path.join(__dirname, '..', '..', 'dist-dev', 'index.html'); + + t.equal(cloudcmd._getIndexPath(isDev), name); + t.end(); +}); + + function cleanNodeEnv() { const {NODE_ENV} = process.env; process.env.NODE_ENV = ''; diff --git a/test/server/route.js b/test/server/route.js index 6bdb0fe0..00323e2d 100644 --- a/test/server/route.js +++ b/test/server/route.js @@ -16,11 +16,6 @@ const routePath = `${rootDir}/server/route`; const cloudcmdPath = `${rootDir}/server/cloudcmd`; const beforePath = path.join(__dirname, '../before'); -const { - _getIndexPath, -} = require(routePath); - -const route = require(routePath); const {connect} = require(beforePath); const warp = (fn, ...a) => (...b) => fn(...b, ...a); @@ -36,25 +31,6 @@ const getStr = (url) => { .catch(console.log); }; -test('cloudcmd: route: no args', (t) => { - t.throws(route, /req could not be empty!/, 'should throw when no args'); - t.end(); -}); - -test('cloudcmd: route: no res', (t) => { - const fn = () => route({}); - - t.throws(fn, /res could not be empty!/, 'should throw when no res'); - t.end(); -}); - -test('cloudcmd: route: no next', (t) => { - const fn = () => route({}, {}); - - t.throws(fn, /next should be function!/, 'should throw when no next'); - t.end(); -}); - test('cloudcmd: route: buttons: no console', async (t) => { const config = { console: false @@ -167,22 +143,6 @@ test('cloudcmd: route: keys panel', async (t) => { done(); }); -test('cloudcmd: route: getIndexPath: production', (t) => { - const isDev = false; - const name = path.join(__dirname, '..', '..', 'dist', 'index.html'); - - t.equal(route._getIndexPath(isDev), name); - t.end(); -}); - -test('cloudcmd: route: getIndexPath: development', (t) => { - const isDev = true; - const name = path.join(__dirname, '..', '..', 'dist-dev', 'index.html'); - - t.equal(route._getIndexPath(isDev), name); - t.end(); -}); - test('cloudcmd: route: file: fs', async (t) => { const root = path.join(__dirname, '..', 'fixture', 'empty-file'); const config = { @@ -257,31 +217,6 @@ test('cloudcmd: route: realpath: error', async (t) => { done(); }); -test('cloudcmd: route: sendIndex: error', async (t) => { - const error = Error('index path error'); - const {readFile} = fs; - const isDev = true; - const indexPath = _getIndexPath(isDev); - - fs.readFile = (name, options, fn = options) => { - if (name === indexPath) { - fn(error); - fs.readFile = readFile; - return; - } - - return readFile(name, options, fn); - }; - - const {port, done} = await connect(); - const data = await getStr(`http://localhost:${port}`); - - t.equal(data, error.message, 'should return error'); - - done(); - t.end(); -}); - test('cloudcmd: route: sendIndex: encode', async (t) => { const name = '">'; const nameEncoded = '"><svg onload=alert(3);>';