mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Validate hook names when they are added
This commit is contained in:
@@ -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,
|
||||
|
Reference in New Issue
Block a user