Skip to content

Commit

Permalink
fix(middleware/body-limit): set default duplex option for readable st…
Browse files Browse the repository at this point in the history
…ream (#2837)

* fix(middleware/body-limit): set default duplex option for readable stream

* test: should return 200 response without user-defined duplex option

* fix: revert and avoid ts-expect-error
  • Loading branch information
fzn0x authored May 28, 2024
1 parent 116867a commit 0c7fb9a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
14 changes: 2 additions & 12 deletions src/middleware/body-limit/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { Hono } from '../../hono'
import { bodyLimit } from '.'

const GlobalRequest = globalThis.Request
globalThis.Request = class Request extends GlobalRequest {
constructor(input: Request | string, init: RequestInit) {
if (init) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(init as any).duplex ??= 'half'
}
super(input, init)
}
} as typeof GlobalRequest

const buildRequestInit = (init: RequestInit = {}): RequestInit => {
const buildRequestInit = (init: RequestInit = {}): RequestInit & { duplex: 'half' } => {
const headers: Record<string, string> = {
'Content-Type': 'text/plain',
}
Expand All @@ -24,6 +13,7 @@ const buildRequestInit = (init: RequestInit = {}): RequestInit => {
headers,
body: null,
...init,
duplex: 'half',
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/middleware/body-limit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export const bodyLimit = (options: BodyLimitOptions): MiddlewareHandler => {
},
})

c.req.raw = new Request(c.req.raw, { body: reader })
const requestInit: RequestInit & { duplex: 'half' } = { body: reader, duplex: 'half' }
c.req.raw = new Request(c.req.raw, requestInit as RequestInit)

await next()

Expand Down

0 comments on commit 0c7fb9a

Please sign in to comment.