plugins: Default the module name to the plugin name

pull/4492/head
Richard Hansen 2020-11-13 14:33:44 -05:00 committed by John McLear
parent afb025030c
commit cedd27e4fe
2 changed files with 25 additions and 11 deletions

View File

@ -90,22 +90,34 @@ name of a function exported by the named module. See
for how to export a function. for how to export a function.
For the module name you can omit the `.js` suffix, and if the file is `index.js` For the module name you can omit the `.js` suffix, and if the file is `index.js`
you can use just the directory name. you can use just the directory name. You can also omit the module name entirely,
in which case it defaults to the plugin name (e.g., `ep_example`).
You can also omit the function name and separating colon. If you do, Etherpad You can also omit the function name. If you do, Etherpad will look for an
will look for an exported function whose name matches the name of the hook exported function whose name matches the name of the hook (e.g.,
(e.g., `authenticate`). You cannot omit the function name if the module name `authenticate`).
contains a colon.
For example, all of the following will cause the `authorize` hook to call the If either the module name or the function name is omitted (or both), the colon
`exports.authorize` function in `index.js` from the `ep_example` plugin: may also be omitted unless the provided module name contains a colon. (So if the
module name is `C:\foo.js` then the hook function specification with the
function name omitted would be `"C:\\foo.js:"`.)
Examples: Suppose the plugin name is `ep_example`. All of the following are
equivalent, and will cause the `authorize` hook to call the `exports.authorize`
function in `index.js` from the `ep_example` plugin:
* `"authorize": "ep_example/index.js:authorize"` * `"authorize": "ep_example/index.js:authorize"`
* `"authorize": "ep_example/index.js:"`
* `"authorize": "ep_example/index.js"` * `"authorize": "ep_example/index.js"`
* `"authorize": "ep_example/index:authorize"` * `"authorize": "ep_example/index:authorize"`
* `"authorize": "ep_example/index:"`
* `"authorize": "ep_example/index"` * `"authorize": "ep_example/index"`
* `"authorize": "ep_example:authorize"` * `"authorize": "ep_example:authorize"`
* `"authorize": "ep_example:"`
* `"authorize": "ep_example"` * `"authorize": "ep_example"`
* `"authorize": ":authorize"`
* `"authorize": ":"`
* `"authorize": ""`
### Client hooks and server hooks ### Client hooks and server hooks

View File

@ -58,11 +58,13 @@ const callInit = async () => {
} }
exports.pathNormalization = function (part, hook_fn_name, hook_name) { exports.pathNormalization = function (part, hook_fn_name, hook_name) {
const parts = hook_fn_name.split(':'); const tmp = hook_fn_name.split(':'); // hook_fn_name might be something like 'C:\\foo.js:myFunc'.
const functionName = (parts.length > 1) ? parts.pop() : hook_name; // If there is a single colon assume it's 'filename:funcname' not 'C:\\filename'.
const functionName = (tmp.length > 1 ? tmp.pop() : null) || hook_name;
const moduleName = tmp.join(':') || part.plugin;
const packageDir = path.dirname(defs.plugins[part.plugin].package.path); const packageDir = path.dirname(defs.plugins[part.plugin].package.path);
const fileName = path.normalize(path.join(packageDir, parts.join(':'))); const fileName = path.normalize(path.join(packageDir, moduleName));
return fileName + ((functionName == null) ? '' : (':' + functionName)); return `${fileName}:${functionName}`;
} }
exports.update = async function () { exports.update = async function () {