Skip to content

Commit

Permalink
fix: should allow nullable param for res.end (#28694)
Browse files Browse the repository at this point in the history
## Bug
Fixes #28693

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
huozhi committed Sep 1, 2021
1 parent 9a6542b commit ed9b886
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/next/server/api-utils.ts
Expand Up @@ -69,12 +69,12 @@ export async function apiResolver(
const writeData = apiRes.write
const endResponse = apiRes.end
apiRes.write = (...args: any[2]) => {
contentLength += Buffer.byteLength(args[0])
contentLength += Buffer.byteLength(args[0] || '')
return writeData.apply(apiRes, args)
}
apiRes.end = (...args: any[2]) => {
if (args.length && typeof args[0] !== 'function') {
contentLength += Buffer.byteLength(args[0])
contentLength += Buffer.byteLength(args[0] || '')
}

if (contentLength >= 4 * 1024 * 1024) {
Expand Down
4 changes: 4 additions & 0 deletions test/integration/api-support/pages/api/nullable-payload.js
@@ -0,0 +1,4 @@
export default (req, res) => {
res.write('')
res.end()
}
5 changes: 5 additions & 0 deletions test/integration/api-support/test/index.test.js
Expand Up @@ -442,6 +442,11 @@ function runTests(dev = false) {
expect(data).toBe('hi')
})

it('should work with nullable payload', async () => {
const data = await renderViaHTTP(appPort, '/api/nullable-payload')
expect(data).toBe('')
})

it('should warn if response body is larger than 4MB', async () => {
let res = await fetchViaHTTP(appPort, '/api/large-response')
expect(res.ok).toBeTruthy()
Expand Down

0 comments on commit ed9b886

Please sign in to comment.