diff --git a/available_plugins/pluginomatic_fintest/otherpart.js b/available_plugins/pluginomatic_fintest/otherpart.js index f2a94d35..9710fa4e 100644 --- a/available_plugins/pluginomatic_fintest/otherpart.js +++ b/available_plugins/pluginomatic_fintest/otherpart.js @@ -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("Abra cadabra"); }); diff --git a/available_plugins/pluginomatic_fintest/package.json b/available_plugins/pluginomatic_fintest/package.json index d5b0531b..8ee3fd36 100644 --- a/available_plugins/pluginomatic_fintest/package.json +++ b/available_plugins/pluginomatic_fintest/package.json @@ -4,6 +4,6 @@ "version": "0.0.1", "author": "RedHog (Egil Moeller) ", "contributors": [], - "dependencies": {}, + "dependencies": {"etherpad-lite": "1"}, "engines": { "node": ">= 0.4.1 < 0.7.0" } } diff --git a/available_plugins/pluginomatic_fintest/pluginomatic.json b/available_plugins/pluginomatic_fintest/pluginomatic.json index 3b34c7ce..0e94dc17 100644 --- a/available_plugins/pluginomatic_fintest/pluginomatic.json +++ b/available_plugins/pluginomatic_fintest/pluginomatic.json @@ -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" } } ] diff --git a/available_plugins/pluginomatic_fintest/static/js/test.js b/available_plugins/pluginomatic_fintest/static/js/test.js new file mode 100644 index 00000000..7dc8e61a --- /dev/null +++ b/available_plugins/pluginomatic_fintest/static/js/test.js @@ -0,0 +1 @@ +exports.foo = 42; diff --git a/node/hooks/express/static.js b/node/hooks/express/static.js index 937fb26e..a160423d 100644 --- a/node/hooks/express/static.js +++ b/node/hooks/express/static.js @@ -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]); diff --git a/node/pluginfw/require.js b/node/pluginfw/require.js new file mode 100644 index 00000000..887c5549 --- /dev/null +++ b/node/pluginfw/require.js @@ -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); +} diff --git a/node/server.js b/node/server.js index 7bd3f90b..ff7aba96 100644 --- a/node/server.js +++ b/node/server.js @@ -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");