From 97ab38e8e164dd1b1f0c5673fff293d72538a092 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 24 Jul 2023 19:53:02 +0200 Subject: [PATCH] check for body existance --- src/event/event.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/event/event.ts b/src/event/event.ts index 9845be6d..58240e5a 100644 --- a/src/event/event.ts +++ b/src/event/event.ts @@ -9,8 +9,16 @@ import { } from "../utils"; import { H3Response } from "./response"; -const DOUBLE_SLASH_RE = /[/\\]{2,}/g; // TODO: Dedup from request.ts -const RawBodySymbol = Symbol.for("h3RawBody"); // TODO: Dedup from body.ts +// TODO: Dedup from request.ts +const DOUBLE_SLASH_RE = /[/\\]{2,}/g; + +// TODO: Dedup from body.ts +const PayloadMethods: Set = new Set([ + "PATCH", + "POST", + "PUT", + "DELETE", +]); export interface NodeEventContext { req: NodeIncomingMessage; @@ -47,6 +55,10 @@ export class H3Event implements Pick { ); } + get _hasBody() { + return PayloadMethods.has(this.method!); + } + get path() { if (!this._path) { this._path = this._originalPath.replace(DOUBLE_SLASH_RE, "/"); @@ -89,7 +101,10 @@ export class H3Event implements Pick { } get body() { - if (!this._body) { + if (!this._hasBody) { + return undefined; + } + if (this._body === undefined) { this._body = new ReadableStream({ start: (controller) => { this.node.req.on("data", (chunk) => {