Skip to content

Commit

Permalink
Update to not show API not ended warning when response is piped to (#…
Browse files Browse the repository at this point in the history
…10342)

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

* Update to use res.once

Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
ijjk and Timer committed Feb 3, 2020
1 parent c01534c commit 65de050
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/next/next-server/server/api-utils.ts
Expand Up @@ -55,9 +55,17 @@ export async function apiResolver(
apiRes.json = data => sendJson(apiRes, data)

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)

if (process.env.NODE_ENV !== 'production' && !isResSent(res)) {
if (process.env.NODE_ENV !== 'production' && !isResSent(res) && !wasPiped) {
console.warn(
`API resolved without sending a response for ${req.url}, this may result in stalled requests.`
)
Expand Down
10 changes: 10 additions & 0 deletions test/integration/api-support/pages/api/test-res-pipe.js
@@ -0,0 +1,10 @@
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)
}
8 changes: 8 additions & 0 deletions test/integration/api-support/test/index.test.js
Expand Up @@ -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.`
)
})

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 {
it('should build api routes', async () => {
const pagesManifest = JSON.parse(
Expand Down

0 comments on commit 65de050

Please sign in to comment.