Client side / server side require

This commit is contained in:
Egil Moeller 2012-02-26 00:45:06 +01:00
parent 261bbd460f
commit 2a58a62dcc
7 changed files with 59 additions and 9 deletions

View File

@ -7,6 +7,9 @@ exports.morehook = function (hook_name, args, cb) {
}
exports.expressServer = function (hook_name, args, cb) {
test = ep_client_require("/plugins/pluginomatic_fintest/test.js");
console.log("FOOO:", test.foo);
args.app.get('/otherpart', function(req, res) {
res.send("<em>Abra cadabra</em>");
});

View File

@ -4,6 +4,6 @@
"version": "0.0.1",
"author": "RedHog (Egil Moeller) <egil.moller@freecode.no>",
"contributors": [],
"dependencies": {},
"dependencies": {"etherpad-lite": "1"},
"engines": { "node": ">= 0.4.1 < 0.7.0" }
}

View File

@ -25,7 +25,7 @@
"hooks": {
"somehookname": "pluginomatic_fintest/otherpart:somehook",
"morehook": "pluginomatic_fintest/otherpart:morehook",
"expressServer": "pluginomatic_fintest/otherpart:expressServer"
"expressCreateServer": "pluginomatic_fintest/otherpart:expressServer"
}
}
]

View File

@ -0,0 +1 @@
exports.foo = 42;

View File

@ -10,17 +10,20 @@ exports.expressCreateServer = function (hook_name, args, cb) {
res.end();
});
args.app.get('/static/plugins/*', function(req, res) {
var url = req.url.replace(/\.\./g, '').split("?")[0];
url = url.split("/");
url.splice(0, 3);
var plugin_name = url.splice(0, 1)[0];
url = url.join("/");
/* Handle paths like "/static/js/plugins/pluginomatic_myplugin/test.js"
by rewriting it to ROOT_PATH_OF_MYPLUGIN/static/js/test.js,
commonly ETHERPAD_ROOT/node_modules/pluginomatic_myplugin/static/js/test.js
*/
args.app.get(/^\/static\/([^\/]+)\/plugins\/([^\/]+)\/(.*)/, function(req, res) {
var type_dir = req.params[0].replace(/\.\./g, '').split("?")[0];
var plugin_name = req.params[1];
var url = req.params[2].replace(/\.\./g, '').split("?")[0];
var filePath = path.normalize(path.join(plugins.plugins[plugin_name].package.path, "static", url));
var filePath = path.normalize(path.join(plugins.plugins[plugin_name].package.path, "static", type_dir, url));
res.sendfile(filePath, { maxAge: exports.maxAge });
});
// Handle normal static files
args.app.get('/static/*', function(req, res) {
var filePath = path.normalize(__dirname + "/../../.." +
req.url.replace(/\.\./g, '').split("?")[0]);

42
node/pluginfw/require.js Normal file
View File

@ -0,0 +1,42 @@
var plugins = require('./plugins');
var path = require('path');
var SERVER_JS_SRC = path.normalize(path.join(__dirname, '../../'));
var CLIENT_JS_SRC = path.normalize(path.join(__dirname, '../../static/js'));
global.ep_require = function (url) {
if (url.indexOf("/plugins/") == 0) {
/* Handle paths like "/plugins/pluginomatic_myplugin/test.js"
by rewriting it to ROOT_PATH_OF_MYPLUGIN/test.js,
commonly ETHERPAD_ROOT/node_modules/pluginomatic_myplugin/test.js
*/
url = url.split("/");
url.splice(0, 1);
var plugin_name = url.splice(0, 1)[0];
url = url.join("/");
url = path.normalize(path.join(plugins.plugins[plugin_name].package.path, url));
} else if (url.indexOf("/") == 0) {
/* Handle all non-plugin paths for files in / */
url = path.normalize(path.join(SERVER_JS_SRC, url))
}
return require(url);
}
global.ep_client_require = function (url) {
if (url.indexOf("/plugins/") == 0) {
/* Handle paths like "/plugins/pluginomatic_myplugin/test.js"
by rewriting it to ROOT_PATH_OF_MYPLUGIN/static/js/test.js,
commonly ETHERPAD_ROOT/node_modules/pluginomatic_myplugin/static/js/test.js
For more information see hooks/express/static.js
*/
url = url.split("/");
url.splice(0, 2);
var plugin_name = url.splice(0, 1)[0];
url = url.join("/");
url = path.normalize(path.join(plugins.plugins[plugin_name].package.path, "static/js", url));
} else if (url.indexOf("/") == 0) {
/* Handle all non-plugin paths for files in /static */
url = path.normalize(path.join(CLIENT_JS_SRC, url))
}
return require(url);
}

View File

@ -27,6 +27,7 @@ var db = require('./db/DB');
var async = require('async');
var express = require('express');
var path = require('path');
require("./pluginfw/require"); // Load globals
var plugins = require("./pluginfw/plugins");
var hooks = require("./pluginfw/hooks");