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

feat(binding-http): add http server middleware #1027

Merged
merged 6 commits into from
Jul 7, 2023

Conversation

ilbertt
Copy link
Contributor

@ilbertt ilbertt commented Jun 18, 2023

Add a middleware for the http server to handle raw http requests.
An example on how to use the middleware can be found in the tests: https://github.com/Luca8991/node-wot/blob/master/packages/binding-http/test/http-server-test.ts#L53-L101

@codecov-commenter
Copy link

codecov-commenter commented Jun 22, 2023

Codecov Report

Merging #1027 (b8f3bde) into master (96a7fa4) will decrease coverage by 0.19%.
The diff coverage is 68.18%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master    #1027      +/-   ##
==========================================
- Coverage   75.79%   75.61%   -0.19%     
==========================================
  Files          72       72              
  Lines       14871    14887      +16     
  Branches     1428     1431       +3     
==========================================
- Hits        11272    11257      -15     
- Misses       3566     3597      +31     
  Partials       33       33              
Impacted Files Coverage Δ
packages/binding-http/src/http-server.ts 63.75% <63.15%> (+0.01%) ⬆️
packages/binding-http/src/http.ts 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

Copy link
Member

@relu91 relu91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggesting a small refactor.

Comment on lines 72 to 83
export default class HttpMiddleware {
public handler: MiddlewareRequestHandler;

constructor(handler: MiddlewareRequestHandler) {
this.handler = handler;
}

public handleRequest(req: http.IncomingMessage, res: http.ServerResponse, next: () => void): Promise<void> {
debug("Hit middleware");
return this.handler(req, res, next);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just get rid of this class and use directly the MiddlewareRequestHandler. I don't see the benefit of having this container class other than having a debug message. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, it would be useful to have a class that takes as input a list of MiddlewareRequestHandlers and configure the next function accordingly? (just got this requirement from my local refactoring, but feel free to add it later)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just get rid of this class and use directly the MiddlewareRequestHandler. I don't see the benefit of having this container class other than having a debug message.

The class was meant more as a base class that can a developer can extend to handle more complex logic, like keeping a "state" between on request and the other, or any other logic that comes to mind.

But I agree the MiddlewareRequestHandler function is what gets executed inside the HttpServer, so we can get rid of the class. Just let me know how to proceed.

Maybe, it would be useful to have a class that takes as input a list of MiddlewareRequestHandlers and configure the next function accordingly? (just got this requirement from my local refactoring, but feel free to add it later)

How would you configure the next function based on the MiddlewareRequestHandler? I didn't really get it 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, it would be useful to have a class that takes as input a list of MiddlewareRequestHandlers and configure the next function accordingly? (just got this requirement from my local refactoring, but feel free to add it later)

Maybe the type of the middleware parameter in the HttpServer's constructor could also be changed to HttpMiddleware | HttpMiddleware[] to cover that? Although things might indeed become a bit complicated in the array case when it comes to the next function.

Maybe an alternative could be to let users pass in functions of type

export type AlternativeMiddlewareRequestHandler = (
    req: http.IncomingMessage,
    res: http.ServerResponse,
    abort: () => void,
) => Promise<void>;

and then the server does the chaining internally. In this case, I replaced next() with an abort() function which would allow users to stop the processing – the next middleware would be called by the server instead if no abort() happened before.

Do you think this could work? Or would having a next() function instead give users more control?

Copy link
Member

@relu91 relu91 Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of something like this:

let middlewares: MiddlewareRequestHandler[] // initialized somewhere
const first = middlewares.shift()
const entries = middleware.entries()

first(req,res, () => entries.next().value[1]()) // needs to check also if we are done but removed for giving the idea

this way the middleware can control whether to pass to the next function or not, without introducing the abort. Going back to the PR itself @Luca8991 I would remove HTTPMiddleware class and discuss further improvements in a future PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@relu91 I've pushed the commit that removes the HTTPMiddleware class

Copy link
Member

@relu91 relu91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you really much for providing the requested changes! Ready to go.

Copy link
Member

@danielpeintner danielpeintner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, Thanks 👍.
I have just some minor requests.

packages/binding-http/src/http-server-middleware.ts Outdated Show resolved Hide resolved
packages/binding-http/README.md Outdated Show resolved Hide resolved
packages/binding-http/README.md Outdated Show resolved Hide resolved
@ilbertt ilbertt requested a review from danielpeintner July 6, 2023 12:47
Copy link
Member

@danielpeintner danielpeintner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@danielpeintner danielpeintner merged commit d998040 into eclipse-thingweb:master Jul 7, 2023
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

Successfully merging this pull request may close these issues.

5 participants