From 04884bd0ce1df4b48197a93c0aa2052df5beb968 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 2 Jan 2024 21:55:00 +0100 Subject: [PATCH] fix(handlers): validate handlers before adding them Fixes #912 Signed-off-by: Max --- src/services/Viewer.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/services/Viewer.js b/src/services/Viewer.js index c1035a391..2678b263c 100644 --- a/src/services/Viewer.js +++ b/src/services/Viewer.js @@ -99,6 +99,12 @@ export default class Viewer { * @param {Handler} handler a new unregistered handler */ registerHandler(handler) { + const err = this.validateHandler(handler) + if (err) { + console.error(err, handler) + return + } + this._state.handlers.push(handler) const handledMimes = [ ...handler.mimes, @@ -110,6 +116,28 @@ export default class Viewer { } } + validateHandler({ id, mimes, mimesAliases, component }) { + // checking valid handler id + if (!id || id.trim() === '' || typeof id !== 'string') { + return 'The handler doesn\'t have a valid id' + } + + // checking if handler is not already registered + if (this._state.handlers.find(h => h.id === id)) { + return 'The handler is already registered' + } + + // Nothing available to process! Failure + if (!(mimes && Array.isArray(mimes)) && !mimesAliases) { + return 'Handler needs a valid mime array or mimesAliases' + } + + // checking valid handler component data + if ((!component || (typeof component !== 'object' && typeof component !== 'function'))) { + return 'The handler doesn\'t have a valid component' + } + } + /** * Get the current opened file *