From acd709a2b657c065f9c5189a5a2623b2ac88c4a6 Mon Sep 17 00:00:00 2001 From: Ivo Georgiev Date: Mon, 22 May 2017 02:05:58 +0300 Subject: [PATCH] basic validation of add-on results (aimed to aid development) --- server.js | 9 ++++++++- validate.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 validate.js diff --git a/server.js b/server.js index 89206fd..a9acb8a 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ var url = require("url"); var rpc = require("./rpc"); +var validate = require("./validate"); // simply console-log warnings in case of wrong args; aimed to aid development var extend = require("extend"); var async = require("async"); @@ -10,6 +11,8 @@ var CACHE_TTL = 2.5 * 60 * 60; // seconds to live for the cache var CENTRAL = "https://api9.strem.io"; +var IS_DEVEL = process.env.NODE_ENV !== "production"; + function Server(methods, options, manifest) { var self = this; @@ -66,7 +69,11 @@ function Server(methods, options, manifest) var auth = params[0], // AUTH is obsolete args = params[1] || { }; - return methods[method](args, cb, { stremioget: true }); // everything is allowed without auth in stremioget mode + return methods[method](args, function(err, res) { + if (err) return cb(err); + if (IS_DEVEL) validate(method, res); // This would simply console-log warnings in case of wrong args; aimed to aid development + cb(null, res); + }, { stremioget: true }); // everything is allowed without auth in stremioget mode }; // HTTP middleware diff --git a/validate.js b/validate.js new file mode 100644 index 0000000..4bd2e7a --- /dev/null +++ b/validate.js @@ -0,0 +1,30 @@ +// simply console-log warnings in case of wrong args; aimed to aid development + +module.exports = function(method, res) { + if (method === "meta.find" || method === "stream.find") { + if (! Array.isArray(res)) warning('result from '+method+' is not an array, but should be, but is ',res) + else res.forEach(function(o) { + if (method === "meta.find") validateMetadata(o) + if (method === "stream.find") validateStream(o) + }) + } + + if (method === "meta.get") { + if (res) validateMetadata(res) + } +} + +function warning(msg) { + console.log.apply(console, ['stremio-addons warning:'].concat(Array.prototype.slice.call(arguments))) +} + +function validateMetadata(o) { + if (! o) warning('empty metadata object') + else if (! o.id) warning('metadata object does not contain id') + else if (! o.type) warning('metadata object with id:'+o.id+' does not contain type') +} + +function validateStream(o) { + if (! o) warning('empty stream object') + else if (! (o.url || o.yt_id || o.infoHash)) warning('stream object does not contain any stremable property') +} \ No newline at end of file