Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simulate body events #7

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/runtime/fetch/call.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IncomingMessage } from '../node/http/request'
import { ServerResponse } from '../node/http/response'
import { EventEmitter } from 'events'

export type Handle = (req: IncomingMessage, res: ServerResponse) => Promise <any>

Expand Down Expand Up @@ -30,6 +31,16 @@ export function createCall (handle: Handle) {
// @ts-ignore
req.body = context.body || null

if (context.body) {
// @ts-ignore
process.nextTick(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.nextTick is important here, this is necessary to postpone emit call and make sure listeners are set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work in a non-Node environment, even with the unenv process polyfill. Is there another way to implement this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think createCall is deeply depends on node environment, because IncomingMessage extends Readable and it extends Node's EventEmitter import { EventEmitter } from 'events'

As for process.nextTick, we need a way to ensure listeners are set, as a solution we can implement EventEmitter logic and create a delayed emit.

Copy link
Member

@pi0 pi0 Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably use defer polyfill but seems actual readable implementation waits for first listener to drain data: https://github.com/nodejs/node/blob/master/lib/internal/streams/readable.js#L311

Copy link
Member

@pi0 pi0 Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielroe While not related, i'm curious why polyfill not working? It seems has polyfill for nextTick too (https://github.com/defunctzombie/node-process/blob/77caa43cdaee4ea710aa14d11cea1705293c0ef3/browser.js#L134)

const data = new TextEncoder().encode(context.body);
(req as EventEmitter).emit('data', data)
(req as EventEmitter).emit('end')
})
}


return handle(req, res).then(() => {
const r = {
body: res._data.toString(),
Expand Down