Skip to content

Commit

Permalink
check for body existance
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 24, 2023
1 parent 28aff66 commit 97ab38e
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPMethod> = new Set([
"PATCH",
"POST",
"PUT",
"DELETE",
]);

export interface NodeEventContext {
req: NodeIncomingMessage;
Expand Down Expand Up @@ -47,6 +55,10 @@ export class H3Event implements Pick<FetchEvent, "respondWith"> {
);
}

get _hasBody() {
return PayloadMethods.has(this.method!);
}

get path() {
if (!this._path) {
this._path = this._originalPath.replace(DOUBLE_SLASH_RE, "/");
Expand Down Expand Up @@ -89,7 +101,10 @@ export class H3Event implements Pick<FetchEvent, "respondWith"> {
}

get body() {
if (!this._body) {
if (!this._hasBody) {
return undefined;
}

Check warning on line 106 in src/event/event.ts

View check run for this annotation

Codecov / codecov/patch

src/event/event.ts#L105-L106

Added lines #L105 - L106 were not covered by tests
if (this._body === undefined) {
this._body = new ReadableStream({
start: (controller) => {
this.node.req.on("data", (chunk) => {
Expand Down

0 comments on commit 97ab38e

Please sign in to comment.