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

New Hooks API #3874

Open
ronag opened this issue Nov 23, 2024 · 2 comments
Open

New Hooks API #3874

ronag opened this issue Nov 23, 2024 · 2 comments

Comments

@ronag
Copy link
Member

ronag commented Nov 23, 2024

I am doing another iteration on this. The primary goal is to support pre-parsed headers in handlers. However, just doing that seems a bit wasteful and I'm hoping to take the interface one step forward:

I am thinking of something the following lines:

interface Controller {
  pause () : void;
  resume () : void;
  abort(err: Error) : void;
  get aborted () : boolean;
  get reason() : Error;
}

type Headers = Record<string, string | string[]>;
type Trailers = Record<string, string | string[]>;

interface Handler {
  onRequestStart(controller: Controller): void;
  // onRequestHeaders(controller: Controller, headers: Headers): Headers;
  // onRequestData(controller: Controller: chunk: Buffer): Buffer;
  // onRequestTrailers(controller: Controller: chunk, trailers: Trailers): Trailers;
  // onRequestEnd(controller: Controller): void;
  // onRequestError(controller: Controller, error: Error): void;
  onResponseStart(controller: Controller, statusCode: number, statusMessage?: string) : void;
  onResponseHeaders(controller: Controller, headers: Headers): void;
  onResponseData(controller: Controller, chunk: Buffer): void;
  onResponseTrailers(controller: Controller, trailers: Trailers): void;
  onResponseEnd(controller: Controller): void;
  onResponseError(controller: Controller, err: Error): void;
}
@ronag
Copy link
Member Author

ronag commented Nov 23, 2024

@metcoder95
Copy link
Member

Wouldn't be enough to pass the controller in a similar manner as we pass abort on the onConnect hook?
Or are we planning to made most of these hooks optional?

What use-cases do you have in mind for most of the onRequest hooks (excepting start one)?
and, for the request hooks, we are planning to add them before or after the lifecycle happened? (this can maybe open the door for mutating the pieces before actually doing any step of the request).

For the Response ones, lgtm

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

No branches or pull requests

2 participants