mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature(cloudcmd) add support of express mounting point (#200)
This commit is contained in:
parent
8a92c97485
commit
51ee3773f7
7 changed files with 15 additions and 47 deletions
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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`,
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue