eugef / node-mocks-http

Mock 'http' objects for testing Express,js, Next.js and Koa routing functions
Other
755 stars 134 forks source link

Any plans to add async iterator support to IncomingMessage? #277

Closed hbgl closed 1 year ago

hbgl commented 1 year ago

Node's http.IncomingMessage inherits from stream.Readable which implements the async iterator protocol in recent versions of Node. It would be appropriate for the mock to also have support for it. A use case would be converting an http.IncomingMessage to a web Request:

import { IncomingMessage } from 'http';

function incomingMessageToRequest(incomingMessage: IncomingMessage) {
    return new Request('...', {
        method: '...',
        body: incomingMessage, // Body supports any async iterable
        duplex: 'half',
    });
}

It would help me fix this issue in Astro: https://github.com/withastro/astro/issues/7525#issuecomment-1622597735

eugef commented 1 year ago

Hi @hbgl, there are no plans to implement this feature.

So I'd like encourage you to create a PR wow add a missing functionality.

hbgl commented 1 year ago

Hello @eugef, thanks for the reply. I will create a PR for this new feature once I am done. It will require Node version >= 10 because that is when async iterator support was added.

As an aside, I would not give up on properly extending from ReadableStream (old PR) which already comes with async iterator support baked in among other things. One could even think about extending IncomingMessage directly. In any case, it would probably be a breaking change.

eugef commented 1 year ago

We are currently supporting Node.js 14 and above, so you can safely use async iterators

hbgl commented 1 year ago

We are currently supporting Node.js 14 and above, so you can safely use async iterators

I saw this line in the package.json https://github.com/eugef/node-mocks-http/blob/5d97424e1fc98cd2c4be768aea93deb73110a885/package.json#L40-L42

So I was unsure if adding async iterators would be a problem. Thanks for clearing that up.

eugef commented 1 year ago

Thanks for pointing out, I will fix it straight away