dart-lang / webdev

A CLI for Dart web development.
https://pub.dev/packages/webdev
212 stars 75 forks source link

Vite proxied `flutter run -d chrome` does not respond with RunRequest #2425

Open eighty4 opened 6 months ago

eighty4 commented 6 months ago

I'm proxying flutter run -d chrome --web-port 5710 through a Vite proxy and it doesn't respond with a RunRequest over the dwds websocket. When flutter.js, proxied through vite to run embedded in a Svelte app, creates the connection to the WebSocket, RunRequest never gets returned from the WebScoket.

Here is the code I can manually run in the console to get a RunRequest (I copied this by going to http:localhost:5710 and copying ConnectRequest from the Network tab):

const ws = new WebSocket('ws://localhost:5173/$dwdsSseHandler')
ws.send('["ConnectRequest","appId","Im4Su93feIfeEOgiTPFjmA==","instanceId","006dcb10-0d68-11ef-907d-716f5f111410","entrypointPath","main_module.bootstrap.js"]')

RunRequest is returned in response to the ConnectRequest through the WS proxy, so I've verified it's not the WebSocket proxy or a hostname origin issue. Do you have any ideas why this wouldn't respond with this event?

Here is the vite config:

import {sveltekit} from '@sveltejs/kit/vite'
import {defineConfig} from 'vite'

export default defineConfig({
    server: {
        proxy: {
            '/dev/ui': {
                rewrite: (path) => path.replace(/^\/dev\/ui\//, '/'),
                target: 'http://localhost:5710',
                ws: true,
            },
            '/$dwdsSseHandler': {
                target: 'http://localhost:5710',
                ws: true,
            },
        },
    },
    plugins: [sveltekit()],
})
eighty4 commented 6 months ago

Trying to debug/read code (to no avail) and was curious to see that neither appId nor instanceId match when proxying through Vite vs connecting to flutter run's dwds ws directly. Why would that be the case?

["ConnectRequest","appId","Im4Su93feIfeEOgiTPFjmA==","instanceId","13994f00-0d6f-11ef-ba4b-ff8af6980f25","entrypointPath","main_module.bootstrap.js"]
["ConnectRequest","appId","f9Hzqrgk3bDXbjTJIfpzDg==","instanceId","0f3c5d80-0d6f-11ef-8b2d-13db2147535e","entrypointPath","main_module.bootstrap.js"]
bkonyi commented 1 week ago

Hi @eighty4,

Were you able to figure this out? It looks like you're assuming that /$dwdsSseHandler is a websocket endpoint but that's actually the SSE (server sent event) handler.

eighty4 commented 1 day ago

I never resolved the issue nor could workaround it. I also haven't tried recreating it since reporting a few Flutter versions ago.

Maybe removing ws: true will fix it. I'll give that a try, but seems weird that Vite's proxy would make a WS request on an HTTP GET.

Hopefully during the holidays I'll have time to revisit the hobby project this occurred in.