Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manually create http.IncomingMessage stream?

I want to manually create http.IncomingMessage stream with some header & body. As it is a readable stream, I can't write/pipe data into it. Is it safe to extend it?

Also, I am not sure how to store headers when it's a stream which doesn't operate on ObjectMode.

EDIT:

According to Node.js documentation, extending http.IncomingMessage is possible.

IncomingMessage <http.IncomingMessage> Specifies the IncomingMessage class to be used. Useful for extending the original IncomingMessage. Default: IncomingMessage.

How can I extend it? Its constructor asks for Socket object.

like image 786
user311413 Avatar asked Aug 14 '26 12:08

user311413


1 Answers

I mocked it like this in my tests:

import { IncomingMessage } from "http";
import { Socket } from "net";

const msg = new IncomingMessage(new Socket());
msg.method = 'POST';
msg.url = "whateverurl"

Hope this helps.

like image 180
Aquazi Avatar answered Aug 16 '26 04:08

Aquazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!