Closed K-Mistele closed 1 month ago
unfortunately Bun does not use the options
parameter in WebSocket
so any arguments like finishRequest
or agent
will not be used
I guess we can rename this ticket to something like Support the options parameter for ws.WebSocket
also, it should probably not override the WebSocket
in the ws
module when it's being explicitly imported and used.
also, it should probably not override the
WebSocket
in thews
module when it's being explicitly imported and used.
It's fine since Bun aims to have everything go faster when they provide same API as WS module it just makes all those packages way faster
Is this released in v1.1.30? https://bun.sh/blog/bun-v1.1.30
Using bun v1.1.33, still hangs on client.connect()
What version of Bun is running?
bun --revision 1.1.17+bb66bba1b
What platform is your computer?
Darwin 23.6.0 arm64 arm
What steps can reproduce the bug?
install the
openai/openai-realtime-api-beta
package. The package is designed to run both on the server and in the browser.Run the following code with
bun run voice.ts
- it will hang atnew RealtimeClient
. If you add the argument toRealtimeClient
to allow setting the API key in the browser (because the package tries to execute the client code as described below), then it hangs atawait client.connnect()
.What is the expected behavior?
Bun breaks a few things with this package:
globalThis.WebSocket
to detect if it should run the client websockets or server websockets with thews
module; which Bun implements even though it is not a browser. Probably, the package should be patched to useglobalThis.window
for browser detection. This is why it is necessary to setdangerouslyAllowAPIKeyInBrowser
in the example above, since the code thinks it's browser code. While this isn't technically a bug in Bun, it's undefined behavior caused by Bun deviating from Node's API, and the failure modes are non-obvious for reasons described below.If you patch the package in
node_modules/@openai/realtime-api-beta/lib/api.js
to useglobalThis.window
for browser detection, a couple of things happen:The package correctly executes the Node portion of code (starting at line
109
) to set up the websocket (below code copy/pasted from the package). But, while the package tries to importWebSocket
from thews
module, the type of thewsModule.default
and ofWebSocket
are stillBunWebSocket
. Bun "hijacking" thews
module is definitely unexpected, and in this case, undesired behavior.The reason that the hang occurs, is that the package relies on the
ws
module'sfinishRequest
method to finish adding the headers to the websocket request before it is self. Bun seems not to implement this. Running the code above usingbun run voice.ts
does not result in execution entering thefinishRequest
function below, or aconsole.log()
line that I tried placing in it being executed. This results in theawait client.connect()
hang that's described above - because the client is set up, but when the connection request is sent without the proper authorization headers, it causes the connection to be closed with abad_request_error
since theAuthorization
header is missing.What do you see instead?
ws
module'sWebSocket
implementation & exports, when they are specifically requested.finishRequest
in Bun'sWebSocket
Additional information
Twitter thread where I originally debugged this is here: https://x.com/0xblacklight/status/1842035646719811803