From 9430023a6f5454b14b9ee63e8e90191934ca8b09 Mon Sep 17 00:00:00 2001 From: "Daniel A.C. Martin" Date: Mon, 27 Oct 2025 16:11:24 +0000 Subject: [PATCH 1/5] Add test for transfer-encoding header removal --- test/format-response.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/format-response.js b/test/format-response.js index 03df4ae..b00ed8c 100644 --- a/test/format-response.js +++ b/test/format-response.js @@ -167,6 +167,17 @@ describe('format-response', function () { expect(formatResponse(v1Event, response, {}).body).to.eql('CombineThisText'); }); + it("adapts headers on chunked responses on v2Event", () => { + const chunkedBody = '7\r\nCombine\r\n4\r\nThis\r\n4\r\nText\r\n0\r\n\r\n'; + const response = Response.from({ + body: chunkedBody, + headers: { 'transfer-encoding': 'chunked'}, + statusCode: 200 + }) + response.chunkedEncoding = true; + expect(formatResponse(v2Event, response, {}).headers['transfer-encoding']).to.be.undefined; + }); + it("v2Event: return object contains cookies", () => { const response = new Response({}); response.headers['set-cookie'] = ['foo=bar', 'hail=hydra']; From 775c2342fa957bc8ea7e4084b4d0f8d04bd1ee67 Mon Sep 17 00:00:00 2001 From: "Daniel A.C. Martin" Date: Mon, 27 Oct 2025 16:12:36 +0000 Subject: [PATCH 2/5] Delete transfer-encoding header --- lib/provider/aws/format-response.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/provider/aws/format-response.js b/lib/provider/aws/format-response.js index 1596619..953e913 100644 --- a/lib/provider/aws/format-response.js +++ b/lib/provider/aws/format-response.js @@ -32,6 +32,8 @@ module.exports = (event, response, options) => { let body = Response.body(response).toString(encoding); if (headers['transfer-encoding'] === 'chunked' || response.chunkedEncoding) { + delete headers['transfer-encoding']; + const raw = Response.body(response).toString().split('\r\n'); const parsed = []; for (let i = 0; i < raw.length; i +=2) { From 3eba83939a838135c89e72bd6379300dce3753f7 Mon Sep 17 00:00:00 2001 From: "Daniel A.C. Martin" Date: Mon, 27 Oct 2025 16:13:30 +0000 Subject: [PATCH 3/5] Fix bug in existing test (v1 -> v2) --- test/format-response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/format-response.js b/test/format-response.js index b00ed8c..f60756e 100644 --- a/test/format-response.js +++ b/test/format-response.js @@ -164,7 +164,7 @@ describe('format-response', function () { statusCode: 200 }) response.chunkedEncoding = true; - expect(formatResponse(v1Event, response, {}).body).to.eql('CombineThisText'); + expect(formatResponse(v2Event, response, {}).body).to.eql('CombineThisText'); }); it("adapts headers on chunked responses on v2Event", () => { From ed1c9025a8c91f468381d68cf4112ff859ceb6ef Mon Sep 17 00:00:00 2001 From: "Daniel A.C. Martin" Date: Mon, 27 Oct 2025 16:14:31 +0000 Subject: [PATCH 4/5] Add test for failing, 'already combined' scenario --- test/format-response.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/format-response.js b/test/format-response.js index f60756e..20e3f0d 100644 --- a/test/format-response.js +++ b/test/format-response.js @@ -146,6 +146,16 @@ describe('format-response', function () { }); + it("handles non-chunked body on res.headers['transfer-encoding'] === 'chunked' on v1Event", () => { + const nonChunkedBody = 'AlreadyCombined'; + const response = Response.from({ + body: nonChunkedBody, + headers: { 'transfer-encoding': 'chunked'}, + statusCode: 200 + }) + expect(formatResponse(v1Event, response, {}).body).to.eql('AlreadyCombined'); + }); + it("parses chunked body on transfer-encoding on v2Event", () => { const chunkedBody = '7\r\nCombine\r\n4\r\nThis\r\n4\r\nText\r\n0\r\n\r\n'; const response = Response.from({ @@ -167,6 +177,16 @@ describe('format-response', function () { expect(formatResponse(v2Event, response, {}).body).to.eql('CombineThisText'); }); + it("handles non-chunked body on res.headers['transfer-encoding'] === 'chunked' on v2Event", () => { + const nonChunkedBody = 'AlreadyCombined'; + const response = Response.from({ + body: nonChunkedBody, + headers: { 'transfer-encoding': 'chunked'}, + statusCode: 200 + }) + expect(formatResponse(v2Event, response, {}).body).to.eql('AlreadyCombined'); + }); + it("adapts headers on chunked responses on v2Event", () => { const chunkedBody = '7\r\nCombine\r\n4\r\nThis\r\n4\r\nText\r\n0\r\n\r\n'; const response = Response.from({ From 00f40e15c09c18afc40a4e96bf22663037639cec Mon Sep 17 00:00:00 2001 From: "Daniel A.C. Martin" Date: Mon, 27 Oct 2025 16:15:24 +0000 Subject: [PATCH 5/5] Only attempt to parse chunks when data is found --- lib/provider/aws/format-response.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/provider/aws/format-response.js b/lib/provider/aws/format-response.js index 953e913..0e68ab2 100644 --- a/lib/provider/aws/format-response.js +++ b/lib/provider/aws/format-response.js @@ -35,15 +35,18 @@ module.exports = (event, response, options) => { delete headers['transfer-encoding']; const raw = Response.body(response).toString().split('\r\n'); - const parsed = []; - for (let i = 0; i < raw.length; i +=2) { - const size = parseInt(raw[i], 16); - const value = raw[i + 1]; - if (value) { - parsed.push(value.substring(0, size)); + + if (raw.length > 1) { + const parsed = []; + for (let i = 0; i < raw.length; i +=2) { + const size = parseInt(raw[i], 16); + const value = raw[i + 1]; + if (value) { + parsed.push(value.substring(0, size)); + } } + body = parsed.join(''); } - body = parsed.join('') } if (eventType === LAMBDA_EVENT_TYPES.ALB) {