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(proxy): include original request signal in proxy request #3925

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading