Validate hook names when they are added

This commit is contained in:
Nick O'Leary
2020-09-29 16:28:52 +01:00
parent 460e1f5563
commit 605177dcf0
2 changed files with 107 additions and 52 deletions

View File

@@ -1,5 +1,17 @@
const Log = require("@node-red/util").log;
const VALID_HOOKS = [
// Message Routing Path
"onSend",
"preRoute",
"preDeliver",
"postDeliver",
"onReceive",
"postReceive",
"onComplete"
]
// Flags for what hooks have handlers registered
let states = { }
@@ -34,6 +46,9 @@ let labelledHooks = { }
*/
function add(hookId, callback) {
let [id, label] = hookId.split(".");
if (VALID_HOOKS.indexOf(id) === -1) {
throw new Error(`Invalid hook '${id}'`);
}
if (label) {
if (labelledHooks[label] && labelledHooks[label][id]) {
throw new Error("Hook "+hookId+" already registered")
@@ -149,8 +164,17 @@ function clear() {
labelledHooks = {}
states = {}
}
function has(hookId) {
let [id, label] = hookId.split(".");
if (label) {
return !!(labelledHooks[label] && labelledHooks[label][id])
}
return !!states[id]
}
module.exports = {
get states() { return states },
has,
clear,
add,
remove,