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

feat: simulate body events #7

wants to merge 1 commit into from

Conversation

farnabaz
Copy link
Contributor

@farnabaz farnabaz commented Aug 4, 2021

This PR create a simple simulation for body streaming based on https://github.com/unjs/h3/issues/29#issuecomment-891951452

resolves unjs/node-mock-http#8

@farnabaz farnabaz requested a review from pi0 August 4, 2021 08:46
@@ -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)

@pi0
Copy link
Member

pi0 commented Nov 24, 2021

(closing since timeout is not a good solution. since having workaround, let's redo it later and track in linked issue)

@pi0 pi0 closed this Nov 24, 2021
@pi0 pi0 deleted the feat/simulate-data branch November 24, 2021 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useBody does not work on unenv direct call
3 participants