Skip to content
Open
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
12 changes: 12 additions & 0 deletions ext/node/polyfills/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,12 @@ function _addAbortSignalOption(server: ServerImpl, options: ListenOptions) {
}
}

export class DenoRequestEvent {
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 wanted to use extends Event there so that users would use typical e.preventDefault() pattern, but it seems strange to mix node's EventEmitted with browser events.

It would also require both setting pendingResponse and cancelling the event, instead of just setting the response field

pendingResponse?: Promise<Response> | Response;

constructor(public req: Request) {}
}

export class ServerImpl extends EventEmitter {
#addr: Deno.NetAddr | null = null;
#hasClosed = false;
Expand Down Expand Up @@ -2077,6 +2083,12 @@ export class ServerImpl extends EventEmitter {
_serve() {
const ac = new AbortController();
const handler = (request: Request, info: Deno.ServeHandlerInfo) => {
const denoEv = new DenoRequestEvent(request);
this.emit("deno:request", denoEv);
if (denoEv.pendingResponse) {
return denoEv.pendingResponse;
}

const socket = new FakeSocket({
remoteAddress: info.remoteAddr.hostname,
remotePort: info.remoteAddr.port,
Expand Down
Loading