Skip to content

Commit

Permalink
fix(proxy): include original request signal in proxy request (#3925)
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryThePenguin authored Feb 17, 2025
1 parent 0c405b9 commit 6ae0213
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/helper/proxy/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ describe('Proxy Middleware', () => {
global.fetch = vi.fn().mockImplementation(async (req) => {
if (req.url === 'https://example.com/ok') {
return Promise.resolve(new Response('ok'))
} else if (req.url === 'https://example.com/disconnect') {
const reader = req.body.getReader()
let response

req.signal.addEventListener('abort', () => {
response = req.signal.reason
reader.cancel()
})

await reader.read()

return Promise.resolve(new Response(response))
} else if (req.url === 'https://example.com/compressed') {
return Promise.resolve(
new Response('ok', {
Expand Down Expand Up @@ -200,5 +212,19 @@ describe('Proxy Middleware', () => {
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.headers.get('Authorization')).toBeNull()
})

it('client disconnect', async () => {
const app = new Hono()
const controller = new AbortController()
app.post('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req))
const resPromise = app.request('/proxy/disconnect', {
method: 'POST',
body: 'test',
signal: controller.signal,
})
controller.abort('client disconnect')
const res = await resPromise
expect(await res.text()).toBe('client disconnect')
})
})
})
1 change: 1 addition & 0 deletions src/helper/proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const buildRequestInitFromRequest = (
body: request.body,
duplex: request.body ? 'half' : undefined,
headers,
signal: request.signal,
}
}

Expand Down

0 comments on commit 6ae0213

Please sign in to comment.