From 97ea501e4fd33e095ca9833d2daccc60e030230d Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Sat, 12 Sep 2026 18:06:53 -0700 Subject: [PATCH] perf: skip decoding discarded diagnostic error bodies (#20276) Co-authored-by: Orca Worker --- .../observability/diagnostic-upload-http.test.ts | 16 ++++++++++++++++ src/main/observability/diagnostic-upload-http.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/observability/diagnostic-upload-http.test.ts b/src/main/observability/diagnostic-upload-http.test.ts index 6ad6f2dcf9d..d7b73ba63ad 100644 --- a/src/main/observability/diagnostic-upload-http.test.ts +++ b/src/main/observability/diagnostic-upload-http.test.ts @@ -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() diff --git a/src/main/observability/diagnostic-upload-http.ts b/src/main/observability/diagnostic-upload-http.ts index 17e6b186085..7373d7e590e 100644 --- a/src/main/observability/diagnostic-upload-http.ts +++ b/src/main/observability/diagnostic-upload-http.ts @@ -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 {