diff --git a/client/sw/register.js b/client/sw/register.js index b8480592..0f295891 100644 --- a/client/sw/register.js +++ b/client/sw/register.js @@ -6,8 +6,6 @@ module.exports.unregisterSW = unregisterSW; const noop = () => {}; async function registerSW(prefix) { - prefix = prefix ? `${prefix}/` : `/`; - if (!navigator.serviceWorker) return; @@ -19,7 +17,7 @@ async function registerSW(prefix) { addEventListener: noop, }; - return navigator.serviceWorker.register(`${prefix}sw.js`); + return navigator.serviceWorker.register(`${prefix}/sw.js`); } async function unregisterSW() { diff --git a/client/sw/register.spec.js b/client/sw/register.spec.js index 909e0d6a..5b42ad0a 100644 --- a/client/sw/register.spec.js +++ b/client/sw/register.spec.js @@ -99,12 +99,12 @@ test('sw: register: registerSW', async (t) => { registerSW, } = mock.reRequire('./register'); - await registerSW('hello'); + await registerSW('/hello'); global.location = location; global.navigator = navigator; - t.ok(register.calledWith('hello/sw.js'), 'should call register'); + t.ok(register.calledWith('/hello/sw.js'), 'should call register'); t.end(); }); diff --git a/package.json b/package.json index 324066fa..bfa3bd09 100644 --- a/package.json +++ b/package.json @@ -115,7 +115,7 @@ "@cloudcmd/read-files-sync": "^2.0.0", "apart": "^2.0.0", "chalk": "^2.0.1", - "console-io": "^9.0.0", + "console-io": "^9.1.0", "copymitter": "^4.0.2", "criton": "^1.0.0", "currify": "^3.0.0", diff --git a/server/cloudcmd.js b/server/cloudcmd.js index db4bce37..9e4ab1a0 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -37,9 +37,6 @@ const getIndexPath = (isDev) => path.join(DIR, '..', `${getDist(isDev)}/index.ht const defaultHtml = fs.readFileSync(getIndexPath(isDev), 'utf8'); const auth = currify(_auth); -const setUrl = currify(_setUrl); -const setSW = currify(_setSW); - const root = () => config('root'); const notEmpty = (a) => a; @@ -166,45 +163,38 @@ function cloudcmd(prefix, plugins, modules) { const funcs = clean([ config('console') && konsole({ - prefix: prefix + '/console', online, }), config('terminal') && terminal({ - prefix: prefix + '/gritty', }), edward({ - prefix: prefix + '/edward', online, diff, zip, }), dword({ - prefix: prefix + '/dword', online, diff, zip, }), deepword({ - prefix: prefix + '/deepword', online, diff, zip, }), fileop({ - prefix: prefix + '/fileop', }), nomine({ - prefix: prefix + '/rename', }), - setUrl(prefix), - setSW(prefix), + setUrl, + setSW, logout, authentication(), config.middle, @@ -235,11 +225,6 @@ function logout(req, res, next) { res.sendStatus(401); } -module.exports._replacePrefix = replacePrefix; -function replacePrefix(url, prefix) { - return url.replace(prefix, '') || '/'; -} - module.exports._replaceDist = replaceDist; function replaceDist(url) { if (!isDev) @@ -248,15 +233,7 @@ function replaceDist(url) { return url.replace(/^\/dist\//, '/dist-dev/'); } -function _setUrl(pref, req, res, next) { - const prefix = getPrefix(pref); - const is = !req.url.indexOf(prefix); - - if (!is) - return next(); - - req.url = replacePrefix(req.url, prefix); - +function setUrl(req, res, next) { if (/^\/cloudcmd\.js(\.map)?$/.test(req.url)) req.url = `/dist${req.url}`; @@ -265,10 +242,8 @@ function _setUrl(pref, req, res, next) { next(); } -function _setSW(pref, req, res, next) { - const prefix = getPrefix(pref); - - const url = replacePrefix(req.url, prefix); +function setSW(req, res, next) { + const {url} = req; const isSW = /^\/sw\.js(\.map)?$/.test(url); if (isSW) diff --git a/server/route.js b/server/route.js index 818c309c..85a271b8 100644 --- a/server/route.js +++ b/server/route.js @@ -65,6 +65,8 @@ async function route(options, request, response) { name, }; + config('prefix', prefixer(request.baseUrl)); + const rootName = name.replace(CloudFunc.FS, '') || '/'; const fullPath = root(rootName); diff --git a/server/server.js b/server/server.js index cbe2b88a..061cd3d6 100644 --- a/server/server.js +++ b/server/server.js @@ -40,7 +40,10 @@ module.exports = async (options) => { if (logger) app.use(logger('dev')); - app.use(cloudcmd({ + if (prefix) + app.get('/', (req, res) => res.redirect(prefix + '/')); + + app.use(prefix, cloudcmd({ config: options, socket: io(server, { path: `${prefix}/socket.io`, diff --git a/test/server/cloudcmd.js b/test/server/cloudcmd.js index 832abbe3..96c97a96 100644 --- a/test/server/cloudcmd.js +++ b/test/server/cloudcmd.js @@ -20,7 +20,6 @@ const {connect} = require(beforePath); const { _getPrefix, _auth, - _replacePrefix, } = cloudcmd; const get = promisify(request); @@ -90,15 +89,6 @@ test('cloudcmd: getPrefix: function: empty', (t) => { t.end(); }); -test('cloudcmd: replacePrefix', (t) => { - const url = '/hello'; - const prefix = url; - const result = _replacePrefix(url, prefix); - - t.equal(result, '/', 'should equal'); - t.end(); -}); - test('cloudcmd: replaceDist', (t) => { const {NODE_ENV} = process.env; process.env.NODE_ENV = 'development';