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(middleware/body-limit): set default duplex option for readable stream #2837

Merged
merged 3 commits into from
May 28, 2024

Conversation

fzn0x
Copy link
Contributor

@fzn0x fzn0x commented May 28, 2024

Fixes #2627

In this case we do not need globalThis.Request anymore.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code

@fzn0x
Copy link
Contributor Author

fzn0x commented May 28, 2024

Tested without buildRequestInit default duplex is working!

@usualoma
Copy link
Member

Hi @fzn0x
Very good! It works fine on all runtimes.

By the way, it seems that ec04a03 alone would solve the problem—do we need a734bf2? I don't see the need to add code to src/hono-base.ts for testing.

@fzn0x
Copy link
Contributor Author

fzn0x commented May 28, 2024

Hi @fzn0x Very good! It works fine on all runtimes.

By the way, it seems that ec04a03 alone would solve the problem—do we need a734bf2? I don't see the need to add code to src/hono-base.ts for testing.

Looks like we need to always add duplex by default from now though it's only one available value half for now, this approach was used by ky and some other famous Request wrapper libraries.

@fzn0x
Copy link
Contributor Author

fzn0x commented May 28, 2024

-- To avoid user manually adds duplex for their ReadableStream requests.

@usualoma
Copy link
Member

@fzn0x Thanks for the answer!

Yes, I know how you feel.

I just think "ky" is a library for sending HTTP requests, which is different from a web application framework like "hono". I think it's great that "ky" makes it easy for users to send POST requests.

However, as far as "hono" is concerned, I think it is better to use the RequestInit as it is passed, and I don't think that what the duplex value of the Request object is is a value that "hono" should take care of. For duplex, it is better if the user explicitly specifies 'half' (or in the future 'full').

As a matter of fact, "node-server" does the same thing, but in hindsight I think this was also overkill.

https://github.com/usualoma/node-server/blob/a24d3ec1e45634d74b0e24bf215eb80e7606ad52/src/request.ts#L34-L38

That's my opinion, but it's not mandatory, so we might as well listen to @yusukebe's opinion too.

@usualoma
Copy link
Member

Avoid @ts-expect-error

Rather than using @ts-expect-error, I prefer to avoid errors in the type definition.

diff --git a/src/middleware/body-limit/index.test.ts b/src/middleware/body-limit/index.test.ts
index 3da88445..60fae554 100644
--- a/src/middleware/body-limit/index.test.ts
+++ b/src/middleware/body-limit/index.test.ts
@@ -1,7 +1,7 @@
 import { Hono } from '../../hono'
 import { bodyLimit } from '.'
 
-const buildRequestInit = (init: RequestInit = {}): RequestInit => {
+const buildRequestInit = (init: RequestInit = {}): RequestInit & { duplex: 'half' } => {
   const headers: Record<string, string> = {
     'Content-Type': 'text/plain',
   }
@@ -13,7 +13,6 @@ const buildRequestInit = (init: RequestInit = {}): RequestInit => {
     headers,
     body: null,
     ...init,
-    // @ts-expect-error - duplex do not have any type yet
     duplex: 'half',
   }
 }
diff --git a/src/middleware/body-limit/index.ts b/src/middleware/body-limit/index.ts
index 282f4d61..7f636830 100644
--- a/src/middleware/body-limit/index.ts
+++ b/src/middleware/body-limit/index.ts
@@ -103,8 +103,8 @@ export const bodyLimit = (options: BodyLimitOptions): MiddlewareHandler => {
       },
     })
 
-    // @ts-expect-error - duplex do not have any type yet
-    c.req.raw = new Request(c.req.raw, { body: reader, duplex: 'half' })
+    const requestInit: RequestInit & { duplex: 'half' } = { body: reader, duplex: 'half' }
+    c.req.raw = new Request(c.req.raw, requestInit as RequestInit)
 
     await next()
 

@fzn0x
Copy link
Contributor Author

fzn0x commented May 28, 2024

For duplex, it is better if the user explicitly specifies 'half' (or in the future 'full').

Since the Request throws duplex errors once users sent ReadableStream requests without specifying it, I think this is makes sense.

Revert and types fix is applied!

@usualoma
Copy link
Member

@fzn0x Thank you, LGTM 👍

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Member

@fzn0x @usualoma Thank you both! It's an interesting discussion. I fully agree. Looks good merging.

@yusukebe yusukebe merged commit 0c7fb9a into honojs:main May 28, 2024
10 checks passed
nicolewhite referenced this pull request in autoblocksai/cli Jun 10, 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](https://hono.dev/) ([source](https://togithub.com/honojs/hono))
| [`4.4.0` ->
`4.4.4`](https://renovatebot.com/diffs/npm/hono/4.4.0/4.4.4) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/hono/4.4.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/hono/4.4.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/hono/4.4.0/4.4.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/hono/4.4.0/4.4.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>honojs/hono (hono)</summary>

### [`v4.4.4`](https://togithub.com/honojs/hono/releases/tag/v4.4.4)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.3...v4.4.4)

##### What's Changed

- fix(typo): Fix typo in request.test.ts by
[@&#8203;yasuaki640](https://togithub.com/yasuaki640) in
[https://github.com/honojs/hono/pull/2899](https://togithub.com/honojs/hono/pull/2899)
- feat(hono-base): skip import HTTPException by using HTTPResponseError
by [@&#8203;usualoma](https://togithub.com/usualoma) in
[https://github.com/honojs/hono/pull/2898](https://togithub.com/honojs/hono/pull/2898)
- chore: improve unfinalized response error by
[@&#8203;Cherry](https://togithub.com/Cherry) in
[https://github.com/honojs/hono/pull/2902](https://togithub.com/honojs/hono/pull/2902)
- chore: create .gitpod.yml by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2868](https://togithub.com/honojs/hono/pull/2868)
- fix(cloudflare-workers): export getConnInfo() by
[@&#8203;ryuapp](https://togithub.com/ryuapp) in
[https://github.com/honojs/hono/pull/2906](https://togithub.com/honojs/hono/pull/2906)
- fix(hono-base): return 404 if lacking response in a single sync
handler by [@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2909](https://togithub.com/honojs/hono/pull/2909)
- refactor: remove `Prettify` as duplicated with `Simplify` by
[@&#8203;NamesMT](https://togithub.com/NamesMT) in
[https://github.com/honojs/hono/pull/2914](https://togithub.com/honojs/hono/pull/2914)
- fix(types):
[#&#8203;2912](https://togithub.com/honojs/hono/issues/2912): interfaces
array's respond typed as `never` by
[@&#8203;NamesMT](https://togithub.com/NamesMT) in
[https://github.com/honojs/hono/pull/2915](https://togithub.com/honojs/hono/pull/2915)
- feat(context): `c.redirect()` supports `TypedResponse` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2908](https://togithub.com/honojs/hono/pull/2908)
- feat(jsx): support htmlfor attribute alias by
[@&#8203;akira-tsuno](https://togithub.com/akira-tsuno) in
[https://github.com/honojs/hono/pull/2916](https://togithub.com/honojs/hono/pull/2916)
- fix(filepath): allow suffix includes `-` and `_` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2910](https://togithub.com/honojs/hono/pull/2910)
- fix(types): add `_` prefix to `TypedResponse` properties by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2917](https://togithub.com/honojs/hono/pull/2917)
- fix(types): `SimplifyDeepArray` should now actually be "deep" by
[@&#8203;NamesMT](https://togithub.com/NamesMT) in
[https://github.com/honojs/hono/pull/2920](https://togithub.com/honojs/hono/pull/2920)
- refactor(middleware/serve-static): call getContent only once if the
file does not exist by [@&#8203;usualoma](https://togithub.com/usualoma)
in
[https://github.com/honojs/hono/pull/2922](https://togithub.com/honojs/hono/pull/2922)
- chore: add `text` and `html` for coverage reporter by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2923](https://togithub.com/honojs/hono/pull/2923)
- refactor(conninfo): create `types.ts` for type definitions by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2924](https://togithub.com/honojs/hono/pull/2924)

##### New Contributors

- [@&#8203;yasuaki640](https://togithub.com/yasuaki640) made their first
contribution in
[https://github.com/honojs/hono/pull/2899](https://togithub.com/honojs/hono/pull/2899)
- [@&#8203;Cherry](https://togithub.com/Cherry) made their first
contribution in
[https://github.com/honojs/hono/pull/2902](https://togithub.com/honojs/hono/pull/2902)
- [@&#8203;akira-tsuno](https://togithub.com/akira-tsuno) made their
first contribution in
[https://github.com/honojs/hono/pull/2916](https://togithub.com/honojs/hono/pull/2916)

**Full Changelog**:
honojs/hono@v4.4.3...v4.4.4

### [`v4.4.3`](https://togithub.com/honojs/hono/releases/tag/v4.4.3)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.2...v4.4.3)

##### What's Changed

- ci: Update workflow name of release.yml by
[@&#8203;siguici](https://togithub.com/siguici) in
[https://github.com/honojs/hono/pull/2874](https://togithub.com/honojs/hono/pull/2874)
- refactor: removed unnecessary line by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2869](https://togithub.com/honojs/hono/pull/2869)
- ci: change name of workflow jobs by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2875](https://togithub.com/honojs/hono/pull/2875)
- docs(jsdoc): add jsdoc of some modules by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2836](https://togithub.com/honojs/hono/pull/2836)
- ci: Report coverage with CodeCov by
[@&#8203;exoego](https://togithub.com/exoego) in
[https://github.com/honojs/hono/pull/2862](https://togithub.com/honojs/hono/pull/2862)
- docs: update readme and migrate guide for migrating `deno.land/x` to
JSR by [@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2879](https://togithub.com/honojs/hono/pull/2879)
- chore: add coverage badge to README by
[@&#8203;exoego](https://togithub.com/exoego) in
[https://github.com/honojs/hono/pull/2881](https://togithub.com/honojs/hono/pull/2881)
- fix(websocket): the onopen event cannot be triggered during delayed
operations in deno by [@&#8203;JetLua](https://togithub.com/JetLua) in
[https://github.com/honojs/hono/pull/2864](https://togithub.com/honojs/hono/pull/2864)
- fix(cloudflare-workers): Update websocket.ts to return 101 status code
by [@&#8203;ronkeiser](https://togithub.com/ronkeiser) in
[https://github.com/honojs/hono/pull/2886](https://togithub.com/honojs/hono/pull/2886)
- test(workerd): rename the runtime test `wrangler` to `workerd` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2888](https://togithub.com/honojs/hono/pull/2888)
- test(workerd): add tests for WebSocket by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2891](https://togithub.com/honojs/hono/pull/2891)
- refactor(aws-lambda): merge custom-context into types by
[@&#8203;exoego](https://togithub.com/exoego) in
[https://github.com/honojs/hono/pull/2889](https://togithub.com/honojs/hono/pull/2889)
- chore: Exclude type-only files from coverage by
[@&#8203;exoego](https://togithub.com/exoego) in
[https://github.com/honojs/hono/pull/2890](https://togithub.com/honojs/hono/pull/2890)
- test(presets): add tests for `hono/quick` and `hono/tiny` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2892](https://togithub.com/honojs/hono/pull/2892)
- fix(types): fix typo for unofficial status code type by
[@&#8203;ryuapp](https://togithub.com/ryuapp) in
[https://github.com/honojs/hono/pull/2894](https://togithub.com/honojs/hono/pull/2894)
- feat(hono-base): add `replaceRequest` option for `app.mount` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/2852](https://togithub.com/honojs/hono/pull/2852)

##### New Contributors

- [@&#8203;siguici](https://togithub.com/siguici) made their first
contribution in
[https://github.com/honojs/hono/pull/2874](https://togithub.com/honojs/hono/pull/2874)
- [@&#8203;JetLua](https://togithub.com/JetLua) made their first
contribution in
[https://github.com/honojs/hono/pull/2864](https://togithub.com/honojs/hono/pull/2864)
- [@&#8203;ronkeiser](https://togithub.com/ronkeiser) made their first
contribution in
[https://github.com/honojs/hono/pull/2886](https://togithub.com/honojs/hono/pull/2886)

**Full Changelog**:
honojs/hono@v4.4.2...v4.4.3

### [`v4.4.2`](https://togithub.com/honojs/hono/releases/tag/v4.4.2)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.1...v4.4.2)

#### What's Changed

- fix: add return types of void function by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2870](https://togithub.com/honojs/hono/pull/2870)

**Full Changelog**:
honojs/hono@v4.4.1...v4.4.2

### [`v4.4.1`](https://togithub.com/honojs/hono/releases/tag/v4.4.1)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.0...v4.4.1)

#### What's Changed

- refactor(pretty-json): remove useless condition by
[@&#8203;6km](https://togithub.com/6km) in
[https://github.com/honojs/hono/pull/2815](https://togithub.com/honojs/hono/pull/2815)
- fix(aws-lambda): Update handler.ts getQueryString by
[@&#8203;qualipsolutions](https://togithub.com/qualipsolutions) in
[https://github.com/honojs/hono/pull/2782](https://togithub.com/honojs/hono/pull/2782)
- refactor(aws-lambda): Object.hasOwn is recommended by
[@&#8203;exoego](https://togithub.com/exoego) in
[https://github.com/honojs/hono/pull/2831](https://togithub.com/honojs/hono/pull/2831)
- fix(client): allow multiple files on the same key in form by
[@&#8203;olivier-drieux](https://togithub.com/olivier-drieux) in
[https://github.com/honojs/hono/pull/2791](https://togithub.com/honojs/hono/pull/2791)
- fix(helper/ssg): fix bug of joinPaths by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2809](https://togithub.com/honojs/hono/pull/2809)
- chore: Author should include JSDoc by
[@&#8203;fzn0x](https://togithub.com/fzn0x) in
[https://github.com/honojs/hono/pull/2840](https://togithub.com/honojs/hono/pull/2840)
- fix(middleware/body-limit): set default duplex option for readable
stream by [@&#8203;fzn0x](https://togithub.com/fzn0x) in
[https://github.com/honojs/hono/pull/2837](https://togithub.com/honojs/hono/pull/2837)
- refactor(websocket): remove unused condition by
[@&#8203;fzn0x](https://togithub.com/fzn0x) in
[https://github.com/honojs/hono/pull/2839](https://togithub.com/honojs/hono/pull/2839)
- feat(bun): WebSocket helper supports that env be `{ server: server }`
by [@&#8203;nakasyou](https://togithub.com/nakasyou) in
[https://github.com/honojs/hono/pull/2812](https://togithub.com/honojs/hono/pull/2812)
- refactor: added paths-ignore for ignore files of dont need tests by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2850](https://togithub.com/honojs/hono/pull/2850)
- ci: include node22 tests by
[@&#8203;Jayllyz](https://togithub.com/Jayllyz) in
[https://github.com/honojs/hono/pull/2851](https://togithub.com/honojs/hono/pull/2851)
- docs(src/\*): Add TSDoc comments to improve code documentation by
[@&#8203;fzn0x](https://togithub.com/fzn0x) in
[https://github.com/honojs/hono/pull/2841](https://togithub.com/honojs/hono/pull/2841)
- fix(types): allow blank env by
[@&#8203;fzn0x](https://togithub.com/fzn0x) in
[https://github.com/honojs/hono/pull/2834](https://togithub.com/honojs/hono/pull/2834)
- refactor: removed v4 branch in actions. by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2849](https://togithub.com/honojs/hono/pull/2849)
- refactor(helper/adapter): improve runtime detection by
[@&#8203;6km](https://togithub.com/6km) in
[https://github.com/honojs/hono/pull/2846](https://togithub.com/honojs/hono/pull/2846)
- fix(jsx/dom): Fixed to not add "px" for certain properties, even if
numeric value is given by
[@&#8203;usualoma](https://togithub.com/usualoma) in
[https://github.com/honojs/hono/pull/2845](https://togithub.com/honojs/hono/pull/2845)
- refactor(aws-lambda): remove unused setHeadersToResult by
[@&#8203;exoego](https://togithub.com/exoego) in
[https://github.com/honojs/hono/pull/2828](https://togithub.com/honojs/hono/pull/2828)
- fix(validator): support json api header by
[@&#8203;dil-borosz](https://togithub.com/dil-borosz) in
[https://github.com/honojs/hono/pull/2855](https://togithub.com/honojs/hono/pull/2855)
- refactor(helper/testing): remove some any of helper/testing by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2833](https://togithub.com/honojs/hono/pull/2833)
- docs: change to shell highlight by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2848](https://togithub.com/honojs/hono/pull/2848)
- refactor(compose): Remove type definition of ComposeContext that was
defined twice by [@&#8203;chimame](https://togithub.com/chimame) in
[https://github.com/honojs/hono/pull/2858](https://togithub.com/honojs/hono/pull/2858)

#### New Contributors

- [@&#8203;6km](https://togithub.com/6km) made their first contribution
in
[https://github.com/honojs/hono/pull/2815](https://togithub.com/honojs/hono/pull/2815)
- [@&#8203;qualipsolutions](https://togithub.com/qualipsolutions) made
their first contribution in
[https://github.com/honojs/hono/pull/2782](https://togithub.com/honojs/hono/pull/2782)
- [@&#8203;olivier-drieux](https://togithub.com/olivier-drieux) made
their first contribution in
[https://github.com/honojs/hono/pull/2791](https://togithub.com/honojs/hono/pull/2791)
- [@&#8203;Jayllyz](https://togithub.com/Jayllyz) made their first
contribution in
[https://github.com/honojs/hono/pull/2851](https://togithub.com/honojs/hono/pull/2851)
- [@&#8203;dil-borosz](https://togithub.com/dil-borosz) made their first
contribution in
[https://github.com/honojs/hono/pull/2855](https://togithub.com/honojs/hono/pull/2855)
- [@&#8203;chimame](https://togithub.com/chimame) made their first
contribution in
[https://github.com/honojs/hono/pull/2858](https://togithub.com/honojs/hono/pull/2858)

**Full Changelog**:
honojs/hono@v4.4.0...v4.4.1

</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:eyJjcmVhdGVkSW5WZXIiOiIzNy4zOTMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjM5My4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

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.

hono.request method does not work in combination with hono/body-limit
3 participants