perf: skip decoding discarded diagnostic error bodies (#20276)

Co-authored-by: Orca Worker <orca-worker@localhost>
This commit is contained in:
OrcaWin
2026-09-12 18:06:53 -07:00
committed by GitHub
co-authored by Orca Worker
parent ebfedc3e22
commit 97ea501e4f
2 changed files with 17 additions and 1 deletions
@@ -23,6 +23,22 @@ class FakeResponse extends EventEmitter {
}
describe('diagnostic upload HTTP', () => {
it('reports only the status for an error response with an invalid JSON body', async () => {
const request = new FakeRequest()
const response = new FakeResponse()
response.statusCode = 503
httpRequestMock.mockImplementationOnce((_options, callback) => {
callback(response)
return request
})
const result = postJsonForJson('http://diagnostics.example/upload', {}, 1000)
response.emit('data', Buffer.from('private backend details: not JSON'))
response.emit('end')
await expect(result).rejects.toThrow(/^HTTP 503$/)
expect(response.listenerCount('data')).toBe(0)
expect(request.listenerCount('error')).toBe(0)
})
it('removes request and response listeners after a successful response', async () => {
const request = new FakeRequest()
const response = new FakeResponse()
@@ -89,8 +89,8 @@ function postRaw(
}
function onResponseEnd(): void {
const status = res?.statusCode ?? 0
const text = Buffer.concat(chunks).toString('utf8')
if (status >= 200 && status < 300) {
const text = Buffer.concat(chunks).toString('utf8')
try {
resolveOnce(text.length > 0 ? JSON.parse(text) : {})
} catch {