Skip to content

Commit bd15231

Browse files
committed
don't await hydro for /events POST response
1 parent dbf5f90 commit bd15231

2 files changed

Lines changed: 25 additions & 36 deletions

File tree

middleware/events.js

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,32 @@ router.post('/', async function postEvents (req, res, next) {
1919
return res.status(400).json({})
2020
}
2121

22-
// Don't depend on Hydro on local development
23-
if (isDev && !req.hydro.maySend()) {
24-
return res.status(200).json({})
25-
}
26-
27-
try {
28-
const hydroRes = await req.hydro.publish(
22+
if (req.hydro.maySend()) {
23+
// intentionally don't await this async request
24+
// so that the http response afterwards is sent immediately
25+
req.hydro.publish(
2926
req.hydro.schemas[fields.type],
3027
omit(fields, OMIT_FIELDS)
31-
)
32-
33-
if (!hydroRes.ok) {
34-
const err = new Error('Hydro request failed')
35-
err.status = hydroRes.status
36-
err.path = fields.path
37-
38-
await FailBot.report(err, {
39-
path: fields.path,
40-
hydroStatus: hydroRes.status,
41-
hydroText: await hydroRes.text()
42-
})
43-
44-
throw err
45-
}
46-
47-
if (res.headersSent) {
48-
throw new Error('Cannot send http response: Hydro publish succeeded, but took too long.')
49-
}
50-
51-
return res.status(201).json(fields)
52-
} catch (err) {
53-
if (isDev) console.error(err)
54-
55-
if (!res.headersSent) {
56-
return res.status(502).json({})
57-
}
28+
).then(async (hydroRes) => {
29+
if (!hydroRes.ok) {
30+
const err = new Error('Hydro request failed')
31+
err.status = hydroRes.status
32+
err.path = fields.path
33+
34+
await FailBot.report(err, {
35+
path: fields.path,
36+
hydroStatus: hydroRes.status,
37+
hydroText: await hydroRes.text()
38+
})
39+
40+
throw err
41+
}
42+
}).catch((e) => {
43+
if (isDev) console.error(e)
44+
});
5845
}
46+
47+
return res.status(201).json(fields)
5948
})
6049

6150
module.exports = router

tests/rendering/events.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('POST /events', () => {
2020
csrfToken = $('meta[name="csrf-token"]').attr('content')
2121
nock('http://example.com')
2222
.post('/hydro')
23-
.reply(200, {})
23+
.reply(201, {})
2424
})
2525

2626
afterEach(() => {
@@ -201,7 +201,7 @@ describe('POST /events', () => {
201201
}, 400)
202202
)
203203

204-
it('should a valid os option', () =>
204+
it('should os a valid os option', () =>
205205
checkEvent({
206206
...pageExample,
207207
context: {

0 commit comments

Comments
 (0)