Skip to content

Commit

Permalink
Disable mustache render where there is nothing to render
Browse files Browse the repository at this point in the history
Converting the entire HTML to a mustache template where there is no data passed uses a huge amount of memory to do a no-op.

Skip the mustache rendering if no data is passed.
  • Loading branch information
lennym committed Apr 15, 2020
1 parent e918b41 commit 0f429f3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions middleware/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module.exports = (req, res, next) => {
req.log('debug', 'Rendering HTML');
const template = req.body.template;
const data = req.body.data;
if (!data || req.body.noRender) {
res.locals.html = template;
return next();
}
try {
debug('Rendering %s', template);
const rendered = mustache.render(template, data);
Expand Down
3 changes: 3 additions & 0 deletions middleware/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const debug = require('debug')('pdf:middleware:validate');
const ValidationError = require('../lib/validation-error');

module.exports = (req, res, next) => {
if (req.body.noRender) {
return next();
}
req.log('debug', 'Validating request');
const template = req.body.template;
let missing;
Expand Down
4 changes: 2 additions & 2 deletions test/integration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ describe('POSTing to /convert', () => {

describe('with a valid html string', () => {

it('renders the html', () => {
it('renders the html without calling a mustache render', () => {
return supertest(App)
.post('/convert')
.send({template: template})
.expect(201)
.expect('Content-type', /octet-stream/)
.expect(() => assert(mustache.render.calledOnce));
.expect(() => assert(!mustache.render.called));
});

it('returns a 201 and a PDF', () => {
Expand Down

0 comments on commit 0f429f3

Please sign in to comment.