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

Catch when nodejs request is aborted #141

Merged
merged 1 commit into from
Feb 9, 2024

Conversation

M4RC3L05
Copy link
Contributor

@M4RC3L05 M4RC3L05 commented Feb 6, 2024

When we receive a request, we start listening to the close event. When fired, we check if the request was aborted using incoming.aborted, if it was, we dispatch an abort event to the existing request abort signal.

Without this, when the client aborts the request, the signal on the request was not being called with the abort event.

Closes: #107

src/listener.ts Outdated
@@ -143,6 +143,13 @@ export const getRequestListener = (
// so generate a pseudo Request object with only the minimum required information.
const req = newRequest(incoming)

// Detect if request was aborted.
incoming.socket.on('close', () => {
if (incoming.aborted) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the nodejs docs it appears that the aborted property is deprecated, and as a replacement, we should check the destroyed (https://nodejs.org/dist/latest-v20.x/docs/api/http.html#messageaborted).

For some reason if i use it, it makes most of the tests fail, do not know if this is something related with supertest...

Copy link
Contributor Author

@M4RC3L05 M4RC3L05 Feb 6, 2024

Choose a reason for hiding this comment

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

Resolved, had to listen to the close event on the response and check the incoming.destroyed

@@ -89,3 +89,42 @@ describe('Error handling - async fetchCallback', () => {
expect(res.text).toBe('error handler did not return a response')
})
})

describe('Abort request', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any more tests that i should include?

@M4RC3L05 M4RC3L05 force-pushed the enhancement/nodejs-request-abort branch 2 times, most recently from 16ef93d to 2a75974 Compare February 6, 2024 23:48
@yusukebe
Copy link
Member

yusukebe commented Feb 8, 2024

Hi @M4RC3L05

Thank you for the PR. Looks good to me.

@usualoma Could you too review this?

@usualoma
Copy link
Member

usualoma commented Feb 8, 2024

I'm preparing an improvement plan now, so wait a minute.

@usualoma
Copy link
Member

usualoma commented Feb 8, 2024

Hi @M4RC3L05
Thank you. The implementation policy makes sense to me!

I hope to achieve the following two points as well.

As an example, I would like to have something like the following PR, can you incorporate it into your PR?
#142

@M4RC3L05
Copy link
Contributor Author

M4RC3L05 commented Feb 8, 2024

Hi, sure, I can work on making does changes

@M4RC3L05 M4RC3L05 force-pushed the enhancement/nodejs-request-abort branch from 2a75974 to c9fa9ef Compare February 8, 2024 22:24
@M4RC3L05
Copy link
Contributor Author

M4RC3L05 commented Feb 8, 2024

Updated with the improvements pointed out on #142 (thx btw 🙏).

Also, added some more tests, please @usualoma @yusukebe let me know if they are relevant or not.

@@ -40,6 +40,34 @@ describe('Request', () => {
expect(req).toBeInstanceOf(global.Request)
expect(req.url).toBe('http://localhost/foo.txt')
})

it('should generate only one `AbortController` per `Request` object created', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a sanity check to make sure we do not create a new AbortController every time we access it, or any other property since it calls getRequestCache.

@usualoma
Copy link
Member

usualoma commented Feb 8, 2024

@M4RC3L05 Thank you. Looks good!

@yusukebe
Copy link
Member

yusukebe commented Feb 9, 2024

@M4RC3L05 @usualoma Thanks both!

I'd like to merge, as @usualoma as co-author.

@M4RC3L05
One thing, could run yarn lint:fix && yarn format:fix to fix the CI error?

When we receive a request, we start listening to the close event on the
response.
When fired, we check if the request was aborted using `incoming.destroyed`,
if it was, we dispatch an abort event to the existing request abort
signal.

Without this, whe the client abortes the request, the signal on the
request was not being called with the abort event.

Signed-off-by: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com>
@M4RC3L05 M4RC3L05 force-pushed the enhancement/nodejs-request-abort branch from c9fa9ef to 55e0250 Compare February 9, 2024 08:20
@M4RC3L05
Copy link
Contributor Author

M4RC3L05 commented Feb 9, 2024

Done

@yusukebe
Copy link
Member

yusukebe commented Feb 9, 2024

@M4RC3L05 Thanks!!

@yusukebe yusukebe merged commit 3d9a0e8 into honojs:main Feb 9, 2024
3 checks passed
nicolewhite referenced this pull request in autoblocksai/cli Mar 20, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@hono/node-server](https://togithub.com/honojs/node-server) |
[`1.7.0` ->
`1.8.2`](https://renovatebot.com/diffs/npm/@hono%2fnode-server/1.7.0/1.8.2)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@hono%2fnode-server/1.8.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@hono%2fnode-server/1.8.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@hono%2fnode-server/1.7.0/1.8.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@hono%2fnode-server/1.7.0/1.8.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>honojs/node-server (@&#8203;hono/node-server)</summary>

###
[`v1.8.2`](https://togithub.com/honojs/node-server/releases/tag/v1.8.2)

[Compare
Source](https://togithub.com/honojs/node-server/compare/v1.8.1...v1.8.2)

#### What's Changed

- fix: duplex only has a getter by
[@&#8203;wenerme](https://togithub.com/wenerme) in
[https://github.com/honojs/node-server/pull/149](https://togithub.com/honojs/node-server/pull/149)

#### New Contributors

- [@&#8203;wenerme](https://togithub.com/wenerme) made their first
contribution in
[https://github.com/honojs/node-server/pull/149](https://togithub.com/honojs/node-server/pull/149)

**Full Changelog**:
honojs/node-server@v1.8.1...v1.8.2

###
[`v1.8.1`](https://togithub.com/honojs/node-server/releases/tag/v1.8.1)

[Compare
Source](https://togithub.com/honojs/node-server/compare/v1.8.0...v1.8.1)

This release includes a `feat` change, but it's minor, so release this
as a patch-release.

#### What's Changed

- feat: support keepalive property for Request object by
[@&#8203;usualoma](https://togithub.com/usualoma) in
[https://github.com/honojs/node-server/pull/148](https://togithub.com/honojs/node-server/pull/148)

**Full Changelog**:
honojs/node-server@v1.8.0...v1.8.1

###
[`v1.8.0`](https://togithub.com/honojs/node-server/releases/tag/v1.8.0)

[Compare
Source](https://togithub.com/honojs/node-server/compare/v1.7.0...v1.8.0)

#### What's Changed

- feat: bump hono to v3.12.8, add customer err tests for compress middl…
by [@&#8203;ariskemper](https://togithub.com/ariskemper) in
[https://github.com/honojs/node-server/pull/130](https://togithub.com/honojs/node-server/pull/130)
- refactor(test): replace features to be removed in v4 by
[@&#8203;ryuapp](https://togithub.com/ryuapp) in
[https://github.com/honojs/node-server/pull/136](https://togithub.com/honojs/node-server/pull/136)
- Catch when nodejs request is aborted by
[@&#8203;M4RC3L05](https://togithub.com/M4RC3L05) in
[https://github.com/honojs/node-server/pull/141](https://togithub.com/honojs/node-server/pull/141)
- chore: bump hono to v4 by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/node-server/pull/143](https://togithub.com/honojs/node-server/pull/143)
- feat: use internal body if available for returning the response in its
original form as much as possible by
[@&#8203;usualoma](https://togithub.com/usualoma) in
[https://github.com/honojs/node-server/pull/145](https://togithub.com/honojs/node-server/pull/145)

#### New Contributors

- [@&#8203;ariskemper](https://togithub.com/ariskemper) made their first
contribution in
[https://github.com/honojs/node-server/pull/130](https://togithub.com/honojs/node-server/pull/130)
- [@&#8203;ryuapp](https://togithub.com/ryuapp) made their first
contribution in
[https://github.com/honojs/node-server/pull/136](https://togithub.com/honojs/node-server/pull/136)
- [@&#8203;M4RC3L05](https://togithub.com/M4RC3L05) made their first
contribution in
[https://github.com/honojs/node-server/pull/141](https://togithub.com/honojs/node-server/pull/141)

**Full Changelog**:
honojs/node-server@v1.7.0...v1.8.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on Monday" in timezone
America/Chicago, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/autoblocksai/cli).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yNDUuMCIsInVwZGF0ZWRJblZlciI6IjM3LjI0NS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.

Abort is not working.
3 participants