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

feat: support async for proxy.bypass #18940

Merged
merged 7 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function proxyMiddleware(
socket.end('HTTP/1.1 404 Not Found\r\n\r\n', '')
return
}
} catch (e) {
} catch (err) {
config.logger.error(
`${colors.red(`ws proxy bypass error:`)}\n${err.stack}`,
{
Expand Down
13 changes: 12 additions & 1 deletion playground/proxy-bypass/__tests__/proxy-bypass.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { expect, test, vi } from 'vitest'
import { browserLogs } from '~utils'
import { browserLogs, isServe, page, serverLogs } from '~utils'

test('proxy-bypass', async () => {
await vi.waitFor(() => {
expect(browserLogs.join('\n')).toContain('status of 404 (Not Found)')
})
})

test('async-proxy-bypass', async () => {
const content = await page.frame('async-response').content()
expect(content).toContain('Hello after 4 ms (async timeout)')
})

test.runIf(isServe)('async-proxy-bypass-with-error', async () => {
await vi.waitFor(() => {
expect(serverLogs.join('\n')).toContain('bypass error')
})
})
2 changes: 2 additions & 0 deletions playground/proxy-bypass/index.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
root app<br />
<iframe src="/nonExistentApp" style="border: 0"></iframe>
<iframe src="/asyncResponse" name="async-response"></iframe>
<iframe src="/asyncThrowingError"></iframe>
18 changes: 18 additions & 0 deletions playground/proxy-bypass/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { defineConfig } from 'vite'

const timeout = (ms) => new Promise((r) => setTimeout(r, ms))

export default defineConfig({
server: {
port: 9606,
Expand All @@ -10,6 +12,22 @@ export default defineConfig({
return false
},
},
'/asyncResponse': {
bypass: async (_, res) => {
await timeout(4)
res.writeHead(200, {
'Content-Type': 'text/plain',
})
res.end('Hello after 4 ms (async timeout)')
return '/asyncResponse'
Copy link

@johncrim johncrim Dec 12, 2024

Choose a reason for hiding this comment

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

I think it's odd to have to return the path of the proxy as the return value here, after the request for that path has been handled. I think user's would expect either returning true or void (no return) should work.

But otherwise this completely handles the cases I care about - thank you!

Choose a reason for hiding this comment

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

@sapphi-red - please let us know whether you still think returning the path that was just handled makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine.

},
},
'/asyncThrowingError': {
bypass: async () => {
await timeout(4)
throw new Error('bypass error')
},
},
},
},
})
Loading