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

fix(ext/http): Throwing Error if the return value of Deno.serve handler is not a Response class #21099

Merged
merged 4 commits into from
Nov 7, 2023

Conversation

codesculpture
Copy link
Contributor

@codesculpture codesculpture commented Nov 6, 2023

Fixing this #20947 and #20013
We are not checking the return value of handler passed through Deno.serve and directly passing it to toInnerResponse but thats expecting a Response class . So throwing error if handler return other than Response class. (Btw, its my first PR correct me if im wrong on anything :) )

@CLAassistant
Copy link

CLAassistant commented Nov 6, 2023

CLA assistant check
All committers have signed the CLA.

@@ -449,6 +450,11 @@ function mapToCallback(context, callback, onError) {
fromInnerRequest(innerRequest, signal, "immutable"),
new ServeHandlerInfo(innerRequest),
);

// Throwing Error if the handler return value is not a Response class
if(!(response && ObjectPrototypeIsPrototypeOf(ResponsePrototype, response))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't need a null check as ObjectPrototypeIsPrototypeOf will return false here:

Suggested change
if(!(response && ObjectPrototypeIsPrototypeOf(ResponsePrototype, response))) {
if (!ObjectPrototypeIsPrototypeOf(ResponsePrototype, response)) {

Copy link
Contributor Author

@codesculpture codesculpture Nov 6, 2023

Choose a reason for hiding this comment

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

I checked it throwed another error, isnt it? Seems ObjectPrototypeIsPrototypeOf throw error if the input is undefined instead of returning false..

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems ObjectPrototypeIsPrototypeOf throw error if the input is undefined instead of returning false..

I believe that's only if the first parameter is null or undefined. The second parameter may be null or undefined:

> WebSocket.prototype.isPrototypeOf(a)
true
> WebSocket.prototype.isPrototypeOf(null)
false
> WebSocket.prototype.isPrototypeOf(undefined)
false

ext/http/00_serve.js Outdated Show resolved Hide resolved
@mmastrac
Copy link
Contributor

mmastrac commented Nov 6, 2023

Changes look good, but we should add a test to serve_test.ts to validate it.

@mmastrac mmastrac linked an issue Nov 6, 2023 that may be closed by this pull request
@codesculpture
Copy link
Contributor Author

Changes look good, but we should add a test to serve_test.ts to validate it.

Okay its night here, I would write a test tmrw and will let u know @mmastrac. Thanks

} catch (error) {
try {
response = await onError(error);
if (!ObjectPrototypeIsPrototypeOf(ResponsePrototype, response)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added same logic to restrict to return only Response class in onError callback

@mmastrac
Copy link
Contributor

mmastrac commented Nov 7, 2023

Looks great to me. If you mark it as ready for review we can give it a run with CI!

@codesculpture
Copy link
Contributor Author

codesculpture commented Nov 7, 2023

Just doing linting and formatting (got some problem with that, now it was fixed), and am running cargo test. Then i will mark this as ready for review.

@codesculpture codesculpture changed the title fix(ext/http): Throwing Error if the return value of handler is not a Response class fix(ext/http): Throwing Error if the return value of Deno.serve handler is not a Response class Nov 7, 2023
@codesculpture
Copy link
Contributor Author

Hey @mmastrac, when i run cargo test some were failing the log is so big, but i mostly saw integration_test were failing. Is that something i need to do since i dint do any breaking change i believe

@mmastrac
Copy link
Contributor

mmastrac commented Nov 7, 2023

@codesculpture Sometimes we get test flakes -- let's see what the CI says.

@mmastrac mmastrac marked this pull request as ready for review November 7, 2023 17:43
@codesculpture
Copy link
Contributor Author

Seems it was unexpectedly failed. Isnt it?

@mmastrac
Copy link
Contributor

mmastrac commented Nov 7, 2023

I can take a look! I'll try to merge it today

EDIT: Github is acting poorly right now, so I'll just restart the builds until we get a proper result.

@mmastrac mmastrac enabled auto-merge (squash) November 7, 2023 22:39
@mmastrac
Copy link
Contributor

mmastrac commented Nov 7, 2023

/bench http[minimal,realistic]

@denobot
Copy link
Collaborator

denobot commented Nov 7, 2023

http[minimal]

rps latency bytes relative
node 89,305 110.66 µs σ=34.47 758,535 kB -71.3%
bun 311,412 32.03 µs σ=17.06 1,533,203 kB best
deno 168,027 57.84 µs σ=16.19 1,376,953 kB -46.0%
deno-baseline 165,220 58.58 µs σ=16.58 1,357,421 kB -46.9%

http[realistic]

rps latency bytes relative
node 87,376 113.54 µs σ=37.43 937,500 kB -55.9%
bun 198,111 57.61 µs σ=126.82 1,855,468 kB best
deno 139,513 70.1 µs σ=22.55 1,474,609 kB -29.6%
deno-baseline 140,288 69.64 µs σ=21.77 1,484,375 kB -29.2%

start: Tue, 07 Nov 2023 22:39:58 GMT

id: c45fd752-5508-4e1c-b49e-033368666b1f

server: fa5afee1-2891-4294-87e1-8d4bbd5d11c8

Copy link
Contributor

@mmastrac mmastrac left a comment

Choose a reason for hiding this comment

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

LGTM!

@mmastrac mmastrac merged commit e459387 into denoland:main Nov 7, 2023
@codesculpture
Copy link
Contributor Author

Thank you @mmastrac

@codesculpture codesculpture deleted the fix-serve branch November 16, 2023 05:46
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.

Deno.serve crashes if nothing is returned
4 participants