From d98eec9d5ecb5b0513fe422fcef03f186060b1c9 Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Fri, 3 May 2024 16:26:01 +1000 Subject: [PATCH 1/3] Add check for request closed --- src/body-parser.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/body-parser.ts b/src/body-parser.ts index 454978a..5feb363 100644 --- a/src/body-parser.ts +++ b/src/body-parser.ts @@ -95,6 +95,10 @@ export function bodyParserWrapper(opts: BodyParserOptions = {}) { return next(); } + if (ctx.req.closed) { + return ctx.throw(499, 'Request already closed'); + } + try { const response = await parseBody(ctx); // patch node From 32537d5f95bc0c9b767acba9c52cd07c8bce0b87 Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Tue, 14 May 2024 16:44:25 +1000 Subject: [PATCH 2/3] Remove `ctx.throw` --- src/body-parser.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/body-parser.ts b/src/body-parser.ts index 5feb363..3092846 100644 --- a/src/body-parser.ts +++ b/src/body-parser.ts @@ -96,7 +96,9 @@ export function bodyParserWrapper(opts: BodyParserOptions = {}) { } if (ctx.req.closed) { - return ctx.throw(499, 'Request already closed'); + ctx.status = 499; + ctx.body = 'Request already closed'; + return; } try { From 20a7bd6a6023e5177e4a5d25bf8bcafce6ed57d8 Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Tue, 14 May 2024 16:44:32 +1000 Subject: [PATCH 3/3] Add test --- test/middleware.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/middleware.test.ts b/test/middleware.test.ts index 9b7f1eb..00af3ce 100644 --- a/test/middleware.test.ts +++ b/test/middleware.test.ts @@ -558,4 +558,19 @@ describe("test/body-parser.test.ts", () => { .expect({ foo: "bar" }); }); }); + + describe("request closed", () => { + it("should return 499 on request closed", async () => { + const app = new Koa(); + + app.use(async (ctx, next) => { + Object.defineProperty(ctx.req, "closed", { value: true }); + await next(); + }); + app.use(bodyParser()); + server = app.listen(); + + await request(server).post("/").send({ foo: "bar" }).expect(499); + }); + }); });