Skip to content
This repository has been archived by the owner on Feb 16, 2019. It is now read-only.

Commit

Permalink
basic validation of add-on results (aimed to aid development)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivshti committed May 21, 2017
1 parent 528a60b commit acd709a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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");

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions 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')
}

0 comments on commit acd709a

Please sign in to comment.