Repository navigation
|
I want to proxy a vite project, I try like this, but it go some url not found export const frontendPlugin = new Elysia()
.all('/api/*', async ({ request }) => {
const url = new URL(request.url)
const target = `http://localhost:5173${url.pathname}${url.search}`
const headers = new Headers(request.headers)
headers.set('host', 'localhost:5173')
headers.delete('origin')
const response = await fetch(target, {
...request,
headers: headers,
})
return response
})
.ws('/api/*', {
async open(ws) {
const rawUrl =
(ws as any).raw?.request?.url ?? (ws as any).raw?.url ?? (ws as any).request?.url
const url = new URL(String(rawUrl))
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
url.port = '5173'
url.hostname = 'localhost'
const client = new WebSocket(url.toString())
client.binaryType = 'arraybuffer'
;(ws as any).data.client = client
client.onopen = () => {
// connected to vite websocket
}
client.onmessage = (event) => {
if (typeof event.data === 'string') {
ws.sendText(event.data)
} else if (event.data instanceof ArrayBuffer) {
ws.sendBinary(new Uint8Array(event.data))
} else if (event.data instanceof Blob) {
event.data.arrayBuffer().then((buffer) => ws.sendBinary(new Uint8Array(buffer)))
}
}
client.onclose = (event) => {
if (ws.readyState === 1) {
ws.close(event.code, event.reason)
}
}
client.onerror = (error) => {
if (ws.readyState === 1) {
ws.close(1011, error instanceof Error ? error.message : String(error))
}
}
},
message(ws, message) {
const client = (ws as any).data?.client as WebSocket | undefined
if (!client || client.readyState !== WebSocket.OPEN) {
return
}
if (typeof message === 'string') {
client.send(message)
} else if (message instanceof ArrayBuffer || ArrayBuffer.isView(message)) {
client.send(message as BufferSource)
} else {
client.send(JSON.stringify(message))
}
},
close(ws, code, reason) {
const client = (ws.data as any)?.client as WebSocket | undefined
if (client && client.readyState === WebSocket.OPEN) {
client.close(code, reason)
}
},
}) |
Answered by
seepine
Jul 4, 2026
Replies: 2 comments 1 reply
|
O problema pode ser o Header Upgrade "Websocket" que não funciona bem no handler do Elysia. Transformar para "websocket" .onRequest(({ request }) => { |
1 reply
|
After removing ws, vite hmr can work... Perhaps this can be used as an answer, although the browser print some import { Elysia } from 'elysia'
export const frontendPlugin = new Elysia()
.all('/*', async ({ request }) => {
const url = new URL(request.url)
const target = `http://localhost:5173${url.pathname}${url.search}`
const headers = new Headers(request.headers)
headers.set('host', 'localhost:5173')
headers.delete('origin')
headers.delete('connection')
return await fetch(target, {
method: request.method,
headers,
body: request.body,
redirect: 'manual',
})
}) |
0 replies
Answer selected by
seepine
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After removing ws, vite hmr can work...
Perhaps this can be used as an answer, although the browser print some
WS connection errorlog, at least it can be used for normal development work.