-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
cloudcmd.js
262 lines (197 loc) · 5.7 KB
/
cloudcmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict';
const DIR = __dirname + '/';
const DIR_ROOT = DIR + '../';
const DIR_COMMON = DIR + '../common/';
const path = require('path');
const fs = require('fs');
const cloudfunc = require(DIR_COMMON + 'cloudfunc');
const authentication = require(DIR + 'auth');
const config = require(DIR + 'config');
const modulas = require(DIR + 'modulas');
const rest = require(DIR + 'rest');
const route = require(DIR + 'route');
const validate = require(DIR + 'validate');
const prefixer = require(DIR + 'prefixer');
const pluginer = require(DIR + 'plugins');
const terminal = require(DIR + 'terminal');
const distribute = require(DIR + 'distribute');
const currify = require('currify');
const apart = require('apart');
const ponse = require('ponse');
const restafary = require('restafary');
const konsole = require('console-io');
const edward = require('edward');
const dword = require('dword');
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 root = () => config('root');
const notEmpty = (a) => a;
const clean = (a) => a.filter(notEmpty);
module.exports = (params) => {
const p = params || {};
const options = p.config || {};
const {
modules,
plugins,
} = p;
const keys = Object.keys(options);
checkPlugins(plugins);
keys.forEach((name) => {
const value = options[name];
if (/root|editor|packer|columns/.test(name))
validate[name](value);
config(name, value);
});
config('console', defaultValue('console', options));
config('configDialog', defaultValue('configDialog', options));
const prefix = prefixer(options.prefix);
if (p.socket)
listen(prefix, p.socket);
return cloudcmd(prefix, plugins, modules);
};
module.exports._getIndexPath = getIndexPath;
function defaultValue(name, options) {
const value = options[name];
const previous = config(name);
if (typeof value === 'undefined')
return previous;
return value;
}
module.exports._getPrefix = getPrefix;
function getPrefix(prefix) {
if (typeof prefix === 'function')
return prefix() || '';
return prefix || '';
}
module.exports._auth = _auth;
function _auth(accept, reject, username, password) {
if (!config('auth'))
return accept();
const isName = username === config('username');
const isPass = password === config('password');
if (isName && isPass)
return accept();
reject();
}
function listen(prefix, socket) {
prefix = getPrefix(prefix);
config.listen(socket, auth);
edward.listen(socket, {
root,
auth,
prefix: prefix + '/edward',
});
dword.listen(socket, {
root,
auth,
prefix: prefix + '/dword',
});
deepword.listen(socket, {
root,
auth,
prefix: prefix + '/deepword',
});
fileop.listen(socket, {
root,
auth,
prefix: prefix + '/fileop',
});
config('console') && konsole.listen(socket, {
auth,
prefix: prefix + '/console',
});
config('terminal') && terminal().listen(socket, {
auth,
prefix: prefix + '/gritty',
command: config('terminalCommand'),
autoRestart: config('terminalAutoRestart'),
});
distribute.export(socket);
}
function cloudcmd(prefix, plugins, modules) {
const online = apart(config, 'online');
const cache = false;
const diff = apart(config, 'diff');
const zip = apart(config, 'zip');
const dir = DIR_ROOT;
const ponseStatic = ponse.static(dir, {cache});
const funcs = clean([
config('console') && konsole({
online,
}),
config('terminal') && terminal({
}),
edward({
online,
diff,
zip,
}),
dword({
online,
diff,
zip,
}),
deepword({
online,
diff,
zip,
}),
fileop({
}),
nomine({
}),
setUrl,
setSW,
logout,
authentication(),
config.middle,
modules && modulas(modules),
restafary({
prefix: cloudfunc.apiURL + '/fs',
root
}),
rest,
route({
html: defaultHtml
}),
pluginer(plugins),
ponseStatic
]);
return funcs;
}
function logout(req, res, next) {
if (req.url !== '/logout')
return next();
res.sendStatus(401);
}
module.exports._replaceDist = replaceDist;
function replaceDist(url) {
if (!isDev)
return url;
return url.replace(/^\/dist\//, '/dist-dev/');
}
function setUrl(req, res, next) {
if (/^\/cloudcmd\.js(\.map)?$/.test(req.url))
req.url = `/dist${req.url}`;
req.url = replaceDist(req.url);
next();
}
function setSW(req, res, next) {
const {url} = req;
const isSW = /^\/sw\.js(\.map)?$/.test(url);
if (isSW)
req.url = replaceDist(`/dist${url}`);
next();
}
function checkPlugins(plugins) {
if (typeof plugins === 'undefined')
return;
if (!Array.isArray(plugins))
throw Error('plugins should be an array!');
}