oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.62k stars 2.72k forks source link

npm ws package overridden by baked-in module #3613

Open PaddeK opened 1 year ago

PaddeK commented 1 year ago

What version of Bun is running?

0.6.14

What platform is your computer?

Darwin 22.5.0 x86_64 i386

What steps can reproduce the bug?

server.js:

import {WebSocketServer} from 'ws';

const wss = new WebSocketServer({port: 8080, handleProtocols: () => 'test'});

wss.on('connection', () => wss.close());

client.js:

import {WebSocket} from 'ws';

const ws = new WebSocket('ws://127.0.0.1:8080', ['custom']);

ws.on('upgrade', res => console.log(res.headers['sec-websocket-protocol']));
ws.on('error', () => ws.terminate());

Running server with node results in expected behavior. Running server with bun results in client printing custom. In both cases client runs in node. Running client in bun shows related issue with overriden WebSocket.

What is the expected behavior?

Expected is client printing test due to handleProtocols override in server.

What do you see instead?

Client is printing custom which hints at handleProtocols not getting called. Running client in bun results in [bun] Warning: ws.WebSocket 'upgrade' event is not implemented in bun.

Additional information

Possibly same issue was closed as fixed before #2955

paperdave commented 1 year ago

right now loading the real ws server doesn't work because it relies on req.socket.write and it implements the protocol that way, which would be a lot of work to get running on bun. the real issue is our ws implementation needs to be finished since it has alot of stubs (mainly on the server)

PaddeK commented 1 year ago

I agree, finishing the integrated WebSocketServer would be the prefered solution. Because the integraded solution does not support ping/pong at the moment i was searching for a fallback alternative and tried ws after uws is crashing due to a napi error. Is a finished integrated implementation planned for the 1.0.0 release?