Skip to content

Commit

Permalink
Should use a real function instead of a string with the Function cons…
Browse files Browse the repository at this point in the history
…tructor or eval
  • Loading branch information
chris-l committed Sep 26, 2015
1 parent f23434d commit a6943df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions lib/get_show.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*jslint node: true, indent: 2, nomen: true, evil: true */
/*jslint node: true, indent: 2, nomen: true */
'use strict';
var R = require('ramda');

Expand All @@ -7,7 +7,7 @@ module.exports = function (self) {
* GET method used to show a show
*/
return function (req, res) {
var db, designdoc, doc, fn, fn_body, img, key, name, output, show_fn;
var db, designdoc, doc, img, key, name, output, show_fn;

doc = null;
db = self.databases[req.params.db];
Expand All @@ -33,9 +33,7 @@ module.exports = function (self) {
}

try {
fn_body = /\{([\s\S]+)\}$/.exec(show_fn)[1];
fn = new Function('doc', 'req', fn_body);
output = fn(doc, req);
output = show_fn(doc, req);
} catch (error) {
return res.send(500, {error: 'render_error', reason: 'function raised error: ' + error.message});
}
Expand Down Expand Up @@ -65,4 +63,4 @@ module.exports = function (self) {
output: output
});
};
};
};
8 changes: 4 additions & 4 deletions test/shows.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ describe('shows', function () {
},
'_design/designer': {
shows: {
docName: 'function(doc, req){\nreturn doc.name;\n}',
general: 'function(doc, req){\nreturn \"It works!\";\n}',
headers: 'function(doc, req){\nreturn{\nheaders:{\n\'X-My-Own-Header\':\'you can set your own headers\'\n},\nbody: \'SimpleText\'\n};}',
nullDoc: 'function(doc, req){\nreturn doc.name;\n}'
docName: function (doc) { return doc.name; },
general: function () { return "It works!"; },
headers: function () { return { headers : { 'X-My-Own-Header': 'you can set your own headers' }, body: 'SimpleText'}; },
nullDoc: function (doc) { return doc.name; }
}
}
})
Expand Down

0 comments on commit a6943df

Please sign in to comment.