Skip to content

Commit

Permalink
postpone setting http response timeout until the req.options.timeout …
Browse files Browse the repository at this point in the history
…is parsed
  • Loading branch information
pofider committed Apr 4, 2023
1 parent d89d286 commit ddef65b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/jsreport-core/lib/main/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ class MainReporter extends Reporter {
}

req = Request(req)
options.onReqReady?.(req)

// TODO: we will probably validate in the thread
if (this.entityTypeValidator.getSchema('TemplateType') != null) {
Expand Down
5 changes: 3 additions & 2 deletions packages/jsreport-express/lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = (app, reporter, exposedOptions) => {
reporter.extensionsManager.extensions.forEach((e) => app.use('/extension/' + e.name, serveStatic(e.directory, { maxAge: oneMonth })))

function httpRender (renderRequestContent, req, res, stream, next) {
res.setTimeout((reporter.getReportTimeout(req) + reporter.options.reportTimeoutMargin) * 1.2)
res.setHeader('X-XSS-Protection', 0)

const renderRequest = typeof renderRequestContent === 'string'
Expand Down Expand Up @@ -68,7 +67,9 @@ module.exports = (app, reporter, exposedOptions) => {
abortEmitter.emit('abort')
})

reporter.render(renderRequest, { abortEmitter }).then((renderResponse) => {
const onReqReady = (req) => res.setTimeout((reporter.getReportTimeout(req) + reporter.options.reportTimeoutMargin) * 1.2)

reporter.render(renderRequest, { abortEmitter, onReqReady }).then((renderResponse) => {
abortEmitter.removeAllListeners('abort')
if (stream) {
form.append('report', renderResponse.stream, {
Expand Down
32 changes: 32 additions & 0 deletions packages/jsreport-express/test/expressTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,35 @@ describe('express with disabled cors', () => {
r.headers.should.not.have.property('access-control-allow-methods')
})
})

describe('express with low reportTimeout', () => {
let jsreport

beforeEach(() => {
jsreport = JsReport({
enableRequestReportTimeout: true,
reportTimeout: 100,
reportTimeoutMargin: 0
})
.use(require('../')())

return jsreport.init()
})

afterEach(() => jsreport && jsreport.close())

it('/api/report and res.setTimeout when options.timeout passed', () => {
return supertest(jsreport.express.app)
.post('/api/report')
.send({
template: {
content: 'hello',
engine: 'none',
helpers: 'await new Promise((resolve) => setTimeout(resolve, 110))',
recipe: 'html'
},
options: { timeout: 5000 }
})
.expect(200, 'hello')
})
})

0 comments on commit ddef65b

Please sign in to comment.