feature(cloudcmd) add ability to set prefix for web sockets connections with --prefix-socket (#200)

This commit is contained in:
coderaiser 2018-10-25 13:02:00 +03:00
parent 63987a0585
commit 359a0344f8
12 changed files with 58 additions and 27 deletions

View file

@ -81,6 +81,7 @@ Cloud Commander supports command line parameters:
| `--packer` | set packer: "tar" or "zip"
| `--root` | set root directory
| `--prefix` | set url prefix
| `--prefix-socket` | set prefix for url connection
| `--port` | set port number
| `--progress` | show progress of file operations
| `--confirm-copy` | confirm copy
@ -392,6 +393,7 @@ Here is description of options:
"ip" : null, /* ip or null(default) */
"root" : "/", /* root directory */
"prefix" : "", /* url prefix */
"prefixSocket" : "", /* prefix for socket connection */
"progress" : true, /* show progress of file operations */
"confirmCopy" : true, /* confirm copy */
"confirmMove" : true, /* confirm move */
@ -577,6 +579,7 @@ const app = require('express')();
const port = 1337;
const prefix = '/cloudcmd';
const socketPrefix = '/cloudcmd-sockets';
const server = http.createServer(app);
const socket = io.listen(server, {
@ -584,7 +587,8 @@ const socket = io.listen(server, {
});
const config = {
prefix // base URL or function which returns base URL (optional)
prefix, // base URL or function which returns base URL (optional)
socketPrefix, // prefix for socket connection
};
const plugins = [

View file

@ -36,6 +36,7 @@ const args = require('minimist')(argv.slice(2), {
'packer',
'root',
'prefix',
'prefix-socket',
'terminal-path',
'terminal-command',
'columns',
@ -97,6 +98,7 @@ const args = require('minimist')(argv.slice(2), {
import : choose(env.bool('import'), config('import')),
export : choose(env.bool('export'), config('export')),
'prefix-socket': config('prefixSocket'),
'show-file-name': choose(env.bool('show_file_name'), config('showFileName')),
'sync-console-path': choose(env.bool('sync_console_path'), config('syncConsolePath')),
'config-dialog': choose(env.bool('config_dialog'), config('configDialog')),
@ -156,6 +158,8 @@ function main() {
config('terminalAutoRestart', args['terminal-auto-restart']);
config('editor', args.editor);
config('prefix', prefixer(args.prefix));
// MAJOR: remove condition on v12
config('prefixSocket', prefixer(args['prefix-socket']) || config('prefix'));
config('root', args.root);
config('vim', args.vim);
config('columns', args.columns);
@ -176,10 +180,11 @@ function main() {
readConfig(args.config);
const options = {
root: args.root || '/', /* --no-root */
root: args.root || '/', // --no-root
editor: args.editor,
packer: args.packer,
prefix: args.prefix || '', /* --no-prefix */
prefix: args.prefix,
prefixSocket: args['prefix-socket'] || args.prefix, // MAJOR: remove condition on v12
columns: args.columns,
};

View file

@ -59,6 +59,7 @@ function CloudCmdProto(DOM) {
this.log = log;
this.PREFIX = '';
this.prefixSocket = '';
this.PREFIX_URL = '';
this.DIRCLIENT = '/dist/';
this.DIRCLIENT_MODULES = this.DIRCLIENT + 'modules/';
@ -143,6 +144,7 @@ function CloudCmdProto(DOM) {
CloudCmd.PREFIX = prefix;
CloudCmd.PREFIX_URL = prefix + apiURL;
CloudCmd.prefixSocket = config.prefixSocket;
CloudCmd.config = (key) => config[key];
CloudCmd.config.if = currify((key, fn, a) => config[key] && fn(a));

View file

@ -73,13 +73,17 @@ function getHost() {
function initSocket() {
const href = getHost();
const prefix = CloudCmd.PREFIX;
const {
prefixSocket,
PREFIX,
} = CloudCmd;
const ONE_MINUTE = 60 * 1000;
const socket = io.connect(href + prefix + '/config', {
const socket = io.connect(href + prefixSocket + '/config', {
reconnectionAttempts: Infinity,
reconnectionDelay: ONE_MINUTE,
path: prefix + '/socket.io'
path: PREFIX + '/socket.io'
});
const save = (data) => {

View file

@ -104,6 +104,7 @@ module.exports.hide = () => {
const loadFiles = async (element) => {
const socketPath = CloudCmd.PREFIX;
const {prefixSocket} = CloudCmd;
const prefix = socketPath + '/' + EditorName;
const url = prefix + '/' + EditorName + '.js';
@ -112,13 +113,12 @@ const loadFiles = async (element) => {
await loadJS(url);
const word = promisify(window[EditorName]);
const options = {
const [ed] = await tryToCatch(word, element, {
maxSize,
prefix,
prefixSocket,
socketPath,
};
const [ed] = await tryToCatch(word, element, options);
});
timeEnd(Name + ' load');
editor = ed;

View file

@ -56,6 +56,10 @@ function getPrefix() {
return CloudCmd.PREFIX + '/console';
}
function getPrefixSocket() {
return CloudCmd.prefixSocket + '/console';
}
function getEnv() {
return {
ACTIVE_DIR: DOM.getCurrentDirPath.bind(DOM),
@ -86,6 +90,7 @@ const create = async () => {
cwd: getDirPath(),
env: getEnv(),
prefix: getPrefix(),
prefixSocket: getPrefixSocket(),
socketPath: CloudCmd.PREFIX,
};

View file

@ -67,6 +67,10 @@ function getPrefix() {
return CloudCmd.PREFIX + '/gritty';
}
function getPrefixSocket() {
return CloudCmd.prefixSocket + '/gritty';
}
function getEnv() {
return {
ACTIVE_DIR: DOM.getCurrentDirPath,
@ -79,7 +83,7 @@ function getEnv() {
function create() {
const options = {
env: getEnv(),
prefix: getPrefix(),
prefix: getPrefixSocket(),
socketPath: CloudCmd.PREFIX,
fontFamily: 'Droid Sans Mono',
};

View file

@ -17,6 +17,7 @@
"ip": null,
"root": "/",
"prefix": "",
"prefixSocket": "",
"progress": true,
"contact": true,
"confirmCopy": true,

View file

@ -13,6 +13,7 @@
"--packer ": "set packer: \"tar\" or \"zip\"",
"--root ": "set root directory",
"--prefix ": "set url prefix",
"--prefix-socket ": "set prefix for socket connection",
"--port ": "set port number",
"--progress ": "show progress of file operations",
"--confirm-copy ": "confirm copy",

View file

@ -36,6 +36,7 @@ programs in browser from any computer, mobile or tablet device.
--packer set packer: "tar" or "zip"
--root set root directory
--prefix set url prefix
--prefix-socket set prefix for socket connection
--port set port number
--progress show progress of file operations
--confirm-copy confirm copy

View file

@ -55,10 +55,13 @@ module.exports = (params) => {
checkPlugins(plugins);
keys.forEach((name) => {
const value = options[name];
let value = options[name];
if (/root|editor|packer|columns/.test(name))
validate[name](value);
if (/prefix/.test(name))
value = prefixer(value);
config(name, value);
});
@ -66,10 +69,11 @@ module.exports = (params) => {
config('console', defaultValue('console', options));
config('configDialog', defaultValue('configDialog', options));
const prefix = prefixer(options.prefix);
const {prefix} = prefixer(options.prefix);
const prefixSocket = prefixer(options.prefixSocket) || prefix; // MAJOR: remove condition on v12
if (p.socket)
listen(prefix, p.socket);
listen(prefixSocket, p.socket);
return cloudcmd(prefix, plugins, modules);
};
@ -108,43 +112,43 @@ function _auth(accept, reject, username, password) {
reject();
}
function listen(prefix, socket) {
prefix = getPrefix(prefix);
function listen(prefixSocket, socket) {
prefixSocket = getPrefix(prefixSocket);
config.listen(socket, auth);
edward.listen(socket, {
root,
auth,
prefix: prefix + '/edward',
prefixSocket: prefixSocket + '/edward',
});
dword.listen(socket, {
root,
auth,
prefix: prefix + '/dword',
prefixSocket: prefixSocket + '/dword',
});
deepword.listen(socket, {
root,
auth,
prefix: prefix + '/deepword',
prefixSocket: prefixSocket + '/deepword',
});
config('console') && konsole.listen(socket, {
auth,
prefixSocket: prefixSocket + '/console',
});
fileop.listen(socket, {
root,
auth,
prefix: prefix + '/fileop',
});
config('console') && konsole.listen(socket, {
auth,
prefix: prefix + '/console',
prefix: prefixSocket + '/fileop',
});
config('terminal') && terminal().listen(socket, {
auth,
prefix: prefix + '/gritty',
prefix: prefixSocket + '/gritty',
command: config('terminalCommand'),
autoRestart: config('terminalAutoRestart'),
});

View file

@ -102,7 +102,7 @@ function _save(callback) {
}
function listen(sock, auth) {
const prefix = manage('prefix');
const prefix = manage('prefixSocket');
sock.of(prefix + '/config')
.on('connection', (socket) => {