Skip to content

Commit

Permalink
use new body stream
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 24, 2023
2 parents 0d6346b + 3211109 commit 28aff66
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
45 changes: 33 additions & 12 deletions src/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class H3Event implements Pick<FetchEvent, "respondWith"> {
_headers: Headers | undefined;
_path: string | undefined;
_url: URL | undefined;
_body: BodyInit | undefined;

// Response
_handled = false;
Expand Down Expand Up @@ -77,29 +78,49 @@ export class H3Event implements Pick<FetchEvent, "respondWith"> {
return this._headers;
}

/** @deprecated Please use `event.node.req` instead. **/
get req() {
return this.node.req;
}

/** @deprecated Please use `event.node.res` instead. **/
get res() {
return this.node.res;
}

get body() {
if (!this._body) {
this._body = new ReadableStream({
start: (controller) => {
this.node.req.on("data", (chunk) => {
controller.enqueue(chunk);
});
this.node.req.on("end", () => {
controller.close();
});
this.node.req.on("error", (err) => {
controller.error(err);
});
},
});
}
return this._body;
}

/** @experimental */
get request(): Request {
if (!this._request) {
this._request = new Request(this.url, {
// @ts-ignore Undici option
duplex: "half",
method: this.method,
headers: this.headers,
// TODO: Use readable stream
body: (this.node.req as any)[RawBodySymbol],
body: this.body,
});
}
return this._request;
}

/** @deprecated Please use `event.node.req` instead. **/
get req() {
return this.node.req;
}

/** @deprecated Please use `event.node.res` instead. **/
get res() {
return this.node.res;
}

// Implementation of FetchEvent
respondWith(r: H3Response | PromiseLike<H3Response>): void {
Promise.resolve(r).then((_response) => {
Expand Down
32 changes: 30 additions & 2 deletions test/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,44 @@ describe("Event", () => {
expect(result.text).toMatch(/http:\/\/127.0.0.1:\d+\/hello/);
});

it("can read request body", async () => {
app.use(
"/",
eventHandler(async (event) => {
const bodyStream = event.body as unknown as NodeJS.ReadableStream;
let bytes = 0;
for await (const chunk of bodyStream) {
bytes += chunk.length;
}
return {
bytes,
};
})
);

const result = await request.post("/hello").send(Buffer.from([1, 2, 3]));

expect(result.body).toMatchObject({ bytes: 3 });
});

it("can convert to a web request", async () => {
app.use(
"/",
eventHandler((event) => {
eventHandler(async (event) => {
expect(event.request.method).toBe("POST");
expect(event.request.headers.get("x-test")).toBe("123");
expect(await event.request.text()).toMatchObject(
JSON.stringify({ hello: "world" })
);
return "200";
})
);
const result = await request.post("/hello").set("x-test", "123");
const result = await request
.post("/hello")
.set("x-test", "123")
.set("content-type", "application/json")
.send(JSON.stringify({ hello: "world" }));

expect(result.text).toBe("200");
});
});

0 comments on commit 28aff66

Please sign in to comment.