-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
fix(ext/node): refactor http.ServerResponse into function class #26210
fix(ext/node): refactor http.ServerResponse into function class #26210
Conversation
a34d2f0
to
5240833
Compare
@nicolabovolato thanks for the PR. Could you please add a test to |
Fixes denoland#19901 Added socket, connection and _header params
5240833
to
a1d825d
Compare
@bartlomieju I added some, let me know if we should have more 👍🏼 |
@bartlomieju I think I've found abetter way to do this, without es5 classes and without rewriting the whole import { EventEmitter } from "node:events";
import { EventEmitterOptions } from "npm:@types/node";
class Base extends EventEmitter {
constructor(options?: EventEmitterOptions) {
super(options);
}
static override call(cls: any, options?: EventEmitterOptions) {
const x = new this(options);
Object.assign(cls, x);
}
}
function Class(this: any) {
Base.call(this, {});
}
Object.setPrototypeOf(Class.prototype, Base.prototype);
const x = new (Class as any)();
console.log(x);
x.on("foo", (data: any) => console.log(data));
x.emit("foo", "bar"); I'll open another pr in the following days. |
@nicolabovolato I think the current solution is fine, it's closer to what Node.js is doing, so I'd prefer to go with this approach. |
@littledivy please take a look - this is fine for me so if you don't have any objections we should land it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
While testing, I found out that light-my-request relies on `ServerResponse.connection`, which is deprecated, so I added that and `socket`, the non deprecated property. It also relies on an undocumented `_header` property, apparently for [raw header processing](https://github.com/fastify/light-my-request/blob/v6.1.0/lib/response.js#L180-L186). I added it as an empty string, feel free to provide other approaches. Fixes #19901 Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Fixes #19901
Context
light-my-request is used internally by
fastify.inject()
, which in turn is used to test routes without spinning up an HTTP server.Since it relied on
http.ServerResponse.call()
it could not work on Deno, which used an ES6 class.This PR should fix that.
Technical
While testing, I found out that light-my-request relies on
ServerResponse.connection
, which is deprecated, so I added that andsocket
, the non deprecated property.It also relies on an undocumented
_header
property, apparently for raw header processing.I added it as an empty string, feel free to provide other approaches.
Code is definitely uglier now, but there are not many options... suggestions are welcome!