diff 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
line wrap: on
line diff
--- a/modules/mod_http.js
+++ b/modules/mod_http.js
@@ -1,23 +1,49 @@
-var config = require('../configuration');
+var sites = {};
+
 var http = require('http');
 
-var files = require('fs').readdirSync('modules/http');
+var fs = require('fs');
+var dir = require('./configuration').pluginsDir;
+var files = fs.readdirSync(dir + '/http');
 var regex = /^mod_.*\.js/;
 
-var sites = {};
+var Script = process.binding('evals').Script;
+var sandbox = {
+	config: require('./configuration'),
+	xmpp: require('xmpp'),
+	errors: require('./errors'),
+	forms: require('./forms'),
+	makeError: require('./errors').makeError,
+	notifs: require('./notifs'),
+	NS: require('./namespaces'),
+	require: require,
+	storage: require('./storage'),
+	toBareJID: require('./fdsq').toBareJID,
+	exports: {}
+};
 
 for (var i in files) {
 	var file = files[i];
 	if (!regex.test(file))
 		continue;
 
-	var module = require('./http/' + files[i]);
-	for (var j in module) {
-		if (typeof sites[j] != 'undefined')
-			console.log('WARNING: module '+j+' already loaded.');
+	fs.readFile(dir + '/http/' + file, function(err, data) {
+		if (err) {
+			console.log('ERROR: loading http module “'+file+'”.'); // FIXME: file is always the last file of the list…
+			return;
+		}
 
-		sites[j] = module[j];
-	}
+		Script.runInNewContext(data, sandbox, dir + '/http/' + file); // FIXME: the same.
+		var module = sandbox.exports;
+		sandbox.exports = {};
+
+		for (var j in module) {
+			if (typeof sites[j] != 'undefined')
+				console.log('WARNING: module http/'+j+' already loaded.');
+
+			sites[j] = module[j];
+		}
+	});
 }
 
 http.createServer(function (req, res) {