comparison modules/mod_http.js @ 41:bc717575e66a

Much better handling of modules.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 20 Feb 2011 15:51:12 +0100
parents 62cbb1c49bc5
children 023f767662d3
comparison
equal deleted inserted replaced
40:741110cdcfcf 41:bc717575e66a
1 var config = require('../configuration'); 1 var sites = {};
2
2 var http = require('http'); 3 var http = require('http');
3 4
4 var files = require('fs').readdirSync('modules/http'); 5 var fs = require('fs');
6 var dir = require('./configuration').pluginsDir;
7 var files = fs.readdirSync(dir + '/http');
5 var regex = /^mod_.*\.js/; 8 var regex = /^mod_.*\.js/;
6 9
7 var sites = {}; 10 var Script = process.binding('evals').Script;
11 var sandbox = {
12 config: require('./configuration'),
13 xmpp: require('xmpp'),
14 errors: require('./errors'),
15 forms: require('./forms'),
16 makeError: require('./errors').makeError,
17 notifs: require('./notifs'),
18 NS: require('./namespaces'),
19 require: require,
20 storage: require('./storage'),
21 toBareJID: require('./fdsq').toBareJID,
22 exports: {}
23 };
8 24
9 for (var i in files) { 25 for (var i in files) {
10 var file = files[i]; 26 var file = files[i];
11 if (!regex.test(file)) 27 if (!regex.test(file))
12 continue; 28 continue;
13 29
14 var module = require('./http/' + files[i]); 30 fs.readFile(dir + '/http/' + file, function(err, data) {
15 for (var j in module) { 31 if (err) {
16 if (typeof sites[j] != 'undefined') 32 console.log('ERROR: loading http module “'+file+'”.'); // FIXME: file is always the last file of the list…
17 console.log('WARNING: module '+j+' already loaded.'); 33 return;
34 }
18 35
19 sites[j] = module[j]; 36 Script.runInNewContext(data, sandbox, dir + '/http/' + file); // FIXME: the same.
20 } 37 var module = sandbox.exports;
38 sandbox.exports = {};
39
40 for (var j in module) {
41 if (typeof sites[j] != 'undefined')
42 console.log('WARNING: module http/'+j+' already loaded.');
43
44 sites[j] = module[j];
45 }
46 });
21 } 47 }
22 48
23 http.createServer(function (req, res) { 49 http.createServer(function (req, res) {
24 var sent = false; 50 var sent = false;
25 for (var i in sites) { 51 for (var i in sites) {