Unverified Commit 65de0500 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to not show API not ended warning when response is piped to (#10342)

* Update to not show API not ended warning when response is piped to

* Update to use res.once
Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
parent c01534c9
...@@ -55,9 +55,17 @@ export async function apiResolver( ...@@ -55,9 +55,17 @@ export async function apiResolver(
apiRes.json = data => sendJson(apiRes, data) apiRes.json = data => sendJson(apiRes, data)
const resolver = interopDefault(resolverModule) const resolver = interopDefault(resolverModule)
let wasPiped = false
if (process.env.NODE_ENV !== 'production') {
// listen for pipe event and don't show resolve warning
res.once('pipe', () => (wasPiped = true))
}
// Call API route method
await resolver(req, res) await resolver(req, res)
if (process.env.NODE_ENV !== 'production' && !isResSent(res)) { if (process.env.NODE_ENV !== 'production' && !isResSent(res) && !wasPiped) {
console.warn( console.warn(
`API resolved without sending a response for ${req.url}, this may result in stalled requests.` `API resolved without sending a response for ${req.url}, this may result in stalled requests.`
) )
......
import fetch from 'node-fetch'
export default async (req, res) => {
const dataRes = await fetch(
`http://localhost:${req.query.port}/api/query?hello=from-pipe`
)
res.status(dataRes.status)
dataRes.body.pipe(res)
}
...@@ -363,6 +363,14 @@ function runTests(dev = false) { ...@@ -363,6 +363,14 @@ function runTests(dev = false) {
`API resolved without sending a response for /api/test-no-end, this may result in stalled requests.` `API resolved without sending a response for /api/test-no-end, this may result in stalled requests.`
) )
}) })
it('should not show warning when the API resolves and the response is piped', async () => {
const startIdx = stderr.length > 0 ? stderr.length - 1 : stderr.length
await fetchViaHTTP(appPort, `/api/test-res-pipe`, { port: appPort })
expect(stderr.substr(startIdx)).not.toContain(
`API resolved without sending a response for /api/test-res-pipe`
)
})
} else { } else {
it('should build api routes', async () => { it('should build api routes', async () => {
const pagesManifest = JSON.parse( const pagesManifest = JSON.parse(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment