Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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