From 442fe1e86fbeeb6334376c65f1b78f535f401d70 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 5 Sep 2020 17:00:26 -0400 Subject: [PATCH] pluginfw: Always include the function name in `hook_fn_name` Plugin authors are allowed to omit the function name in the `ep.json` parts definition. For example: ``` { "parts": [ { "name": "ep_example", "hooks": { "authenticate": "ep_example", "authFailure": "ep_example" } } ] } ``` If omitted, the function name is assumed to be the same as the hook name. Before this change, `hook_fn_name` for the example hooks would both be `/opt/etherpad-lite/node_modules/ep_example`. Now they are suffixed with `:authenticate` and `:authFailure`. This improves logging, and it makes it possible to use `hook_fn_name` to uniquely identify a particular hook function. --- src/static/js/pluginfw/plugins.js | 8 ++++++-- src/static/js/pluginfw/shared.js | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/static/js/pluginfw/plugins.js b/src/static/js/pluginfw/plugins.js index ebb1a0034..eb0f2d1ff 100644 --- a/src/static/js/pluginfw/plugins.js +++ b/src/static/js/pluginfw/plugins.js @@ -72,8 +72,12 @@ exports.callInit = function () { return Promise.all(p); } -exports.pathNormalization = function (part, hook_fn_name) { - return path.normalize(path.join(path.dirname(exports.plugins[part.plugin].package.path), hook_fn_name)); +exports.pathNormalization = function (part, hook_fn_name, hook_name) { + const parts = hook_fn_name.split(':'); + const functionName = (parts.length > 1) ? parts.pop() : hook_name; + const packageDir = path.dirname(exports.plugins[part.plugin].package.path); + const fileName = path.normalize(path.join(packageDir, parts.join(':'))); + return fileName + ((functionName == null) ? '' : (':' + functionName)); } exports.update = async function () { diff --git a/src/static/js/pluginfw/shared.js b/src/static/js/pluginfw/shared.js index 4df71aee8..a9971e939 100644 --- a/src/static/js/pluginfw/shared.js +++ b/src/static/js/pluginfw/shared.js @@ -39,7 +39,7 @@ function extractHooks(parts, hook_set_name, normalizer) { * a dependency of another plugin! Bah, pesky little details of * npm... */ if (normalizer) { - hook_fn_name = normalizer(part, hook_fn_name); + hook_fn_name = normalizer(part, hook_fn_name, hook_name); } try {