forked from fbornhofen/life_star
-
Notifications
You must be signed in to change notification settings - Fork 6
/
life_star.js
405 lines (365 loc) · 14.9 KB
/
life_star.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*global require, module*/
var lang = require('lively.lang'),
express = require('express'),
morgan = require('morgan'),
LivelyFsHandler = require('lively-davfs/request-handler'),
log4js = require('log4js'),
proxy = require('./lib/proxy'),
testing = require('./lib/testing'),
auth = require('./lib/auth'),
SubserverHandler = require('./lib/subservers').SubserverHandler,
ManifestHandler = require('./lib/manifest').ManifestHandler,
lfUtil = require('./lib/util'),
util = require('util'),
fs = require('fs'),
path = require('path'),
EventEmitter = require('events').EventEmitter
var serverSetup = module.exports = function(config, thenDo) {
config.host = config.host || "localhost";
config.port = config.port || 9001;
config.srvOptions = config.srvOptions || {node: config.fsNode || "../LivelyKernel/"};
config.logLevel = config.logLevel || "debug";
config.enableTesting = config.enableTesting;
config.sslServerKey = config.sslServerKey;
config.sslServerCert = config.sslServerCert;
config.sslCACert = config.sslCACert;
config.enableSSL = config.enableSSL && config.sslServerKey && config.sslServerCert && config.sslCACert;
config.enableSSLClientAuth = config.enableSSL && config.enableSSLClientAuth;
config.behindProxy = config.behindProxy || false;
config.subservers = config.subservers || {};
config.subserverDirectory = config.subserverDirectory || __dirname + "/subservers/";
config.useManifestCaching = config.useManifestCaching || false;
config.cors = config.hasOwnProperty("cors") ? config.cors : true;
var app = express(), server, logger;
thenDo = lang.fun.once(thenDo || function() {});
lang.fun.composeAsync(
extendServerSetupFunction,
createServer,
createNodejsLivelyInterface,
setupBehindProxy,
setupCORS,
setupStreamBuffers,
setupBodyParser,
setupCookies,
setupHTTPLogger,
setupSSLCertHandler,
setupAuthHandler,
setupProxyServer,
setupTestServer,
setupSubserverHandler,
setupManifestCacheHandler,
setupResourceDatabaseAndFileSystemHandler,
startHTTPListener
)(function(err) {
if (err) {
console.error("Error starting life_star: %s", err);
thenDo(err);
} else {
server.once('listening', function() { thenDo(err, server); });
}
});
return server;
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// server setup steps below
function extendServerSetupFunction(next) {
// make it an event emitter so that life_star users can receive server
// related events. Events are emitted in startHTTPListener()
util._extend(serverSetup, EventEmitter.prototype);
EventEmitter.call(serverSetup);
serverSetup.getServer = function() { return server; };
serverSetup.getApp = function() { return app; };
next();
}
function createNodejsLivelyInterface(next) {
// some helpers, mainly for interactive usage
if (typeof global.lively === "undefined") global.lively = {};
var lv = global.lv = global.lively;
util._extend(lv, {
server: {
dir: __dirname,
get lifeStar() { return server; },
get app() { return app; }
}
});
next();
}
function createServer(next) {
if (config.enableSSL) {
var https = require('https'),
options = {
// Specify the key and certificate file
key: fs.readFileSync(config.sslServerKey),
cert: fs.readFileSync(config.sslServerCert),
// Specify the Certificate Authority certificate
ca: fs.readFileSync(config.sslCACert),
// This is where the magic happens in Node. All previous steps simply
// setup SSL (except the CA). By requesting the client provide a
// certificate, we are essentially authenticating the user.
requestCert: config.enableSSLClientAuth,
// If specified as "true", no unauthenticated traffic will make it to
// the route specified.
rejectUnauthorized: config.enableSSLClientAuth
};
server = require('https').createServer(options, app);
} else {
server = require('http').createServer(app);
}
server.config = config;
next();
}
function setupBehindProxy(next) {
// express specifically handles the case of sitting behind a proxy, see
// http://expressjs.com/guide.html#proxies
if (config.behindProxy) app.enable('trust proxy');
next();
}
function setupCORS(next) {
if (config.cors) {
console.log('Lively server started with cross origin resource sharing (CORS) enabled.');
app.use(function cors(req, res, next) {
var allowedHeaders = req.header("Access-Control-Request-Headers"), // allow all headers by default
allowedMethods = "POST,OPTIONS,GET,HEAD,DELETE,PROPFIND,PUT,PROPPATCH,COPY,MOVE,REPORT";
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', allowedMethods);
allowedHeaders && res.header('Access-Control-Expose-Headers', allowedHeaders);
allowedHeaders && res.header('Access-Control-Allow-Headers', allowedHeaders);
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
}
next();
}
function setupStreamBuffers(next) {
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=
// dealing with request content
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=
function installStreambuffer(req) {
// to learn about the rationale of this funny hack see
// https://github.com/LivelyKernel/node-lively-davfs/blob/7f8082bceaf7851b5152d672a98ca55c399b944d/jsDAV-plugin.js#L31
function invokeCb(cb, arg) {
try { cb.call(null, arg); } catch (e) {
console.log("life_star streambuffer callback error: ", e);
}
}
function dataHandler(d) {
if (data) data = Buffer.concat([data, d]);
else data = d;
streamBufferDataHandlers.forEach(function(ea) { invokeCb(ea, d) });
}
function endHandler() {
done = true;
streamBufferEndHandlers.forEach(function(ea) { invokeCb(ea) });
streamBufferDataHandlers = [];
streamBufferEndHandlers = [];
}
var done = false, data = null,
streamBufferDataHandlers = [],
streamBufferEndHandlers = [];
if (req.streambuffer) {
var origStreambuffer = req.streambuffer;
origStreambuffer.ondata(dataHandler);
origStreambuffer.onend(endHandler);
} else {
req.on("data", dataHandler);
req.on('end', endHandler);
}
req.streambuffer = {
ondata: function(cb) {
if (done) invokeCb(cb, data);
else {
if (data) invokeCb(cb, data);
streamBufferDataHandlers.push(cb);
}
},
onend: function(cb) {
if (done) invokeCb(cb);
else streamBufferEndHandlers.push(cb);
}
}
}
app.use(function(req,res,next) {
if (req.method === 'PUT') installStreambuffer(req);
next();
});
next();
}
function setupBodyParser(next) {
app.use(express.bodyParser({limit: '150mb'}));
next();
}
function setupCookies(next) {
app.use(express.cookieParser());
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// store auth information into a cookie
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
app.use(express.cookieSession({
key: 'livelykernel-sign-on',
secret: 'foo',
proxy: config.behindProxy,
cookie: {path: '/', httpOnly: false, maxAge: null}
}));
next();
}
function setupHTTPLogger(next) {
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// set up logger, proxy and testing routes
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
logger = log4js.getLogger();
logger.level = (config.logLevel || 'OFF').toUpperCase();
morgan.token('user', function(req, res) { return (req.session && req.session["lvUserData_2013-10-12"] && req.session["lvUserData_2013-10-12"].username) || 'unknown user'; });
morgan.token('email', function(req, res) { return (req.session && req.session["lvUserData_2013-10-12"] && req.session["lvUserData_2013-10-12"].email) || ''; });
// default format:
// ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"',
morgan.lkFormat = morgan.combined.replace('":method', '":user <:email>" ":method');
app.use(morgan('lkFormat',{
skip: function (req, res) {
return req.url.indexOf("/nodejs/LogServer") === 0
|| req.url.indexOf("/nodejs/ObjectRepositoryServer/?getRecords") === 0
|| req.url.indexOf("/uvic-login?redirect=") === 0;
}
}));
next();
}
function setupSSLCertHandler(next) {
// -=-=-=-=-=-=-=-=-=-=-=-=-
// deal with authentication
// -=-=-=-=-=-=-=-=-=-=-=-=-
if (config.behindProxy) {
app.use(auth.extractApacheClientCertHeadersIntoSession);
}
next();
}
function setupAuthHandler(next) {
// -=-=-=-=-=-=-=-=-=-=-
// auth handler
// -=-=-=-=-=-=-=-=-=-=-
// FIXME: better mapping of auth conf / lively conf / server conf!!!
var authConf = {};
if (lively.Config) {
authConf.enabled = lively.Config.get('userAuthEnabled', true);
authConf.cookieField = lively.Config.get('cookieField', true);
authConf.usersDefaultWorld = lively.Config.get('usersDefaultWorld', true);
authConf.paths = lively.Config.get('authPaths', true);
authConf.requireLogin = lively.Config.get('authRequireLogin', true);
if (lively.Config.get('usersFile', true) != null)
authConf.usersFile = lively.Config.get('usersFile');
}
authConf = lang.obj.merge(config.authConf, authConf);
if (!authConf.enabled) next();
else {
// TODO: for dev purpose: do not reinstall the package every time again!
lfUtil.npmInstall("life_star-auth", __dirname, function(err) {
if (err) next(err);
else {
var AuthHandler = require('life_star-auth').HTTPHandler;
server.authHandler = new AuthHandler(authConf).registerWith(app, server);
next();
}
});
}
}
function setupProxyServer(next) {
// -=-=-=-=-=-=-
// Proxy routes
// -=-=-=-=-=-=-
var proxyHandler = proxy(logger);
function extractURLFromProxyRequest(req) {
// example: /proxy/localhost:5984/test/_all_docs?limit=3
// => http://localhost:5984/test/_all_docs?limit=3
return req.protocol + '://' + req.url.slice('/proxy/'.length);
}
app.all(/\/proxy\/(.*)/, function(req, res) {
var url = extractURLFromProxyRequest(req);
proxyHandler[req.method.toLowerCase()](url, req, res);
});
next();
}
function setupTestServer(next) {
// -=-=-=-=-=-
// test server
// -=-=-=-=-=-
if (config.enableTesting) testing(app, logger);
next();
}
function setupSubserverHandler(next) {
// -=-=-=-=-=-=-=-
// setup subserver
// -=-=-=-=-=-=-=-
new SubserverHandler({
baseURL: '/nodejs/',
subserverDirectory: config.subserverDirectory,
additionalSubservers: config.subservers
}).registerWith(app, server);
next();
}
function setupManifestCacheHandler(next) {
// -=-=-=-=-=-=-=-=-=-=-
// manifest file related
// -=-=-=-=-=-=-=-=-=-=-
if (config.useManifestCaching) {
var manifestHandler = new ManifestHandler(config);
manifestHandler.registerWith(app, server);
app.all(/.*/, function fileHandler(req, res, next) {
if (req.url.match(/\?\d+/)) {
req.url = req.url.replace(/\?.*/, ''); // only the bare file name
}
manifestHandler.addManifestRef(req, res);
next();
});
}
next();
}
function setupResourceDatabaseAndFileSystemHandler(next) {
// -=-=-=-=-=--=-=-=-=-=--=-=-=-
// set up file system connection
// -=-=-=-=-=--=-=-=-=-=--=-=-=-
var dbConf = { // defaults
enableVersioning: true,
enableRewriting: false,
// Modules necessary modules for world load, the rest is rewritten onLoad
bootstrapRewriteFiles: [ // 'core/lively/bootstrap.js', 'core/lib/lively-libs-debug.js', 'core/lib/escodegen.browser.js',
'core/lively/Migration.js', 'core/lively/JSON.js', 'core/lively/lang/LocalStorage.js',
'node_modules/lively.lang/lib/base.js', 'node_modules/lively.lang/lib/events.js', 'node_modules/lively.lang/lib/object.js',
'node_modules/lively.lang/lib/collection.js', 'node_modules/lively.lang/lib/tree.js', 'node_modules/lively.lang/lib/function.js',
'node_modules/lively.lang/lib/string.js', 'node_modules/lively.lang/lib/number.js', 'node_modules/lively.lang/lib/date.js',
'node_modules/lively.lang/lib/class.js', 'node_modules/lively.lang/lib/messenger.js', 'node_modules/lively.lang/lib/worker.js',
'core/lively/defaultconfig.js', 'core/lively/Base.js', 'core/lively/ModuleSystem.js', 'core/lively/Traits.js', 'core/lively/DOMAbstraction.js',
'core/lively/IPad.js', 'core/lively/LogHelper.js', 'core/lively/localconfig.js', // FIXME: + user configs ?
// bootstrap.js
'core/lively/lang/Closure.js',
'core/lively/bindings.js', 'core/lively/bindings/Core.js',
'core/lively/Main.js', 'core/lively/persistence/Serializer.js'
// directly neccessary for debugging BUT excluded for now:
// 'core/lively/ast/Debugging.js', 'core/lively/ast/AcornInterpreter.js', 'core/lively/ast/Rewriting.js', 'core/lively/ast/AstHelper.js',
// 'core/lively/ast/acorn.js', 'core/lively/ast/StackReification.js'
],
fs: config.srvOptions.node,
excludedDirectories: ['.svn', '.git', /node_modules\/(?!lively\.)/, '.optimized-loading-cache'],
excludedFiles: [/.*\.sqlite/, /.*\.gz/, '.DS_Store', 'combined.js'],
includedFiles: [/\.(cmd|conf|css|diff|el|html|ini|js|json|md|mdown|metainfo|patch|r|snippets|st|txt|xhtml|xml|yml)$/i],
dbFile: path.join(config.fsNode || '', "objects.sqlite"),
resetDatabase: false
};
if (config.dbConf) {
if (typeof config.dbConf === 'string')
config.dbConf = JSON.parse(config.dbConf);
util._extend(dbConf, config.dbConf);
}
var fsHandler = new LivelyFsHandler(dbConf).registerWith(app, server);
global.lively.server.repository = fsHandler.repository;
app.all(/.*/, fsHandler.handleRequest.bind(fsHandler));
next();
}
function startHTTPListener(next) {
// -=-=-=-=-
// GO GO GO
// -=-=-=-=-
server.on('listening', function() {
console.log("life_star running");
serverSetup.emit('start', server);
});
server.on('close', function() { serverSetup.emit('close'); });
server.listen(config.port, config.host);
next();
}
};