11import { afterEach , beforeEach , describe , expect , test } from "bun:test"
22import { createServer , request as httpRequest , type Server } from "node:http"
3- import net from "node:net"
4- import { PassThrough } from "node:stream"
3+ import { WebSocketServer , WebSocket } from "ws"
54import { DshWebColdReload } from "@wopal/ellamaka-cordis/runtime/web-cold-reload"
65import {
76 spawnWebChildContainer ,
@@ -26,43 +25,6 @@ import {
2625let fakeChild : Server
2726let fakePort = 0
2827
29- /**
30- * Minimal duplex socket pair standing in for the dispatcher boundary: writing
31- * to one end is readable from the other, which is all `handleUpgrade` needs.
32- * (Bun has no `net.createPair`; a real loopback socket pair would need an
33- * extra listener and adds no contract value here.)
34- */
35- function socketPair ( ) : [ net . Socket , net . Socket ] {
36- const make = ( ) : { sock : net . Socket ; feed : ( chunk : Buffer ) => void } => {
37- const source = new PassThrough ( )
38- const sink = new PassThrough ( )
39- const sock = source as unknown as net . Socket
40- // Cross-wire: writes to `sock` surface on the peer's readable side.
41- const peer = { feed : ( chunk : Buffer ) => source . write ( chunk ) }
42- void sink
43- sock . write = ( ( chunk : Buffer , _enc ?: unknown , cb ?: ( ) => void ) => {
44- peer . feed ( chunk )
45- cb ?.( )
46- return true
47- } ) as net . Socket [ "write" ]
48- return { sock, feed : ( chunk ) => source . push ( chunk ) }
49- }
50- const a = make ( )
51- const b = make ( )
52- // a.write → readable on b; b.write → readable on a
53- a . sock . write = ( ( chunk : Buffer , _enc ?: unknown , cb ?: ( ) => void ) => {
54- b . feed ( chunk )
55- cb ?.( )
56- return true
57- } ) as net . Socket [ "write" ]
58- b . sock . write = ( ( chunk : Buffer , _enc ?: unknown , cb ?: ( ) => void ) => {
59- a . feed ( chunk )
60- cb ?.( )
61- return true
62- } ) as net . Socket [ "write" ]
63- return [ a . sock , b . sock ]
64- }
65-
6628beforeEach ( async ( ) => {
6729 fakeChild = createServer ( ( req , res ) => {
6830 res . writeHead ( 200 , { "content-type" : "text/plain" } )
@@ -98,71 +60,57 @@ describe("dsh:web child host builder", () => {
9860 expect ( ( ) => container . handleRequest ( { } , { writeHead ( ) { } , end ( ) { } } ) ) . not . toThrow ( )
9961 } )
10062
101- test ( "webChildProxyContainer forwards WebSocket upgrades to the child (101 handshake completes)" , async ( ) => {
102- // Regression (Bun runtime): the previous proxyUpgrade relied on
103- // node:http's `upgrade` event, which Bun does not emit — every browser
104- // WebSocket through the parent hung until the client gave up ("连接中断"
105- // retry loop). The contract is now end-to-end: a real upgrade request
106- // through `handleUpgrade` must reach the child and complete the 101
107- // handshake within a bounded time.
108- // The fake child is a RAW TCP server, matching the real child's upgrade
109- // path (cordis VirtualWebServer answers upgrades on the raw socket; Bun's
110- // node:http `upgrade` event fires but swallows socket bytes, so a plain
111- // createServer() cannot stand in for the child here).
112- const upgradeServer = net . createServer ( ( socket ) => {
113- socket . once ( "data" , ( ) => {
114- socket . write ( "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n" )
115- socket . end ( )
116- } )
117- } )
118- await new Promise < void > ( ( resolve ) => upgradeServer . listen ( 0 , "127.0.0.1" , resolve ) )
119- const address : unknown = upgradeServer . address ( )
63+ test ( "webChildProxyContainer bridges WebSocket upgrades to the child (101 handshake + message round-trip)" , async ( ) => {
64+ // Regression (Bun runtime): Bun swallows every socket.write() on a
65+ // node:http upgrade socket (oven-sh/bun#9882, #35325), so raw-byte tunnels
66+ // can never answer a browser's 101. The proxy must terminate the upgrade
67+ // with the `ws` noServer path (Bun's ws shim uses the native handoff) and
68+ // bridge frames to a native WebSocket dialed at the child. The contract is
69+ // end-to-end: handshake completes AND one frame round-trips.
70+ const childHttp = createServer ( )
71+ const childWss = new WebSocketServer ( { noServer : true } )
72+ childHttp . on ( "upgrade" , ( req , sock , head ) =>
73+ childWss . handleUpgrade ( req , sock , head , ( ws ) => {
74+ ws . on ( "message" , ( m ) => ws . send ( `echo:${ String ( m ) } ` ) )
75+ } ) ,
76+ )
77+ await new Promise < void > ( ( resolve ) => childHttp . listen ( 0 , "127.0.0.1" , resolve ) )
78+ const address : unknown = childHttp . address ( )
12079 expect ( address ) . toHaveProperty ( "port" )
12180 // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- expect guard above pins the shape
122- const upgradePort = ( address as { port : number } ) . port
81+ const childPort = ( address as { port : number } ) . port
12382
124- const container = webChildProxyContainer ( { port : upgradePort } )
125- // A client-side socket pair whose "server side" IS the proxy: we feed the
126- // raw upgrade request into `handleUpgrade` (the dispatcher's entry) and
127- // read the child's answer back from the paired client — the exact parent
128- // path without needing a real listening socket in between.
129- const [ client , serverSide ] = socketPair ( )
130- const raw = client
131- container . handleUpgrade (
132- {
133- method : "GET" ,
134- url : "/api/remote.mux" ,
135- headers : {
136- host : "127.0.0.1" ,
137- upgrade : "websocket" ,
138- connection : "Upgrade" ,
139- "sec-websocket-key" : "dGhlIHNhbXBsZSBub25jZQ==" ,
140- "sec-websocket-version" : "13" ,
141- } ,
142- } ,
143- serverSide ,
144- Buffer . alloc ( 0 ) ,
145- )
146- // The proxy must answer with the child's 101 within 5s; a hang is the
147- // exact Bun regression this test pins.
148- const response = await new Promise < string > ( ( resolve ) => {
149- let data = ""
150- const timer = setTimeout ( ( ) => resolve ( "TIMEOUT" ) , 5000 )
151- raw . on ( "data" , ( chunk ) => {
152- data += chunk . toString ( "latin1" )
153- if ( data . includes ( "\r\n\r\n" ) ) {
154- clearTimeout ( timer )
155- resolve ( data )
156- }
83+ const container = webChildProxyContainer ( { port : childPort } )
84+ // The dispatcher boundary: feed an upgrade request into handleUpgrade with
85+ // a fake duplex pair, then use a native WebSocket against a loopback
86+ // listener that forwards into the container's socket side.
87+ const relay = createServer ( ( req : unknown , res : { writeHead ( n : number ) : unknown ; end ( ) : unknown } ) => {
88+ res . writeHead ( 500 )
89+ res . end ( )
90+ } )
91+ relay . on ( "upgrade" , ( req , sock , head ) => container . handleUpgrade ( req , sock , head ) )
92+ await new Promise < void > ( ( resolve ) => relay . listen ( 0 , "127.0.0.1" , resolve ) )
93+ const relayAddress : unknown = relay . address ( )
94+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- expect guard pins shape
95+ const relayPort = ( relayAddress as { port : number } ) . port
96+
97+ const probe = new WebSocket ( `ws://127.0.0.1:${ relayPort } /api/remote.mux` )
98+ const roundTrip = new Promise < string > ( ( resolve , reject ) => {
99+ const timer = setTimeout ( ( ) => reject ( new Error ( "ws round-trip timed out" ) ) , 5000 )
100+ probe . on ( "open" , ( ) => probe . send ( "ping" ) )
101+ probe . on ( "message" , ( m ) => {
102+ clearTimeout ( timer )
103+ resolve ( String ( m ) )
157104 } )
158- raw . on ( "error" , ( ) => {
105+ probe . on ( "error" , ( e ) => {
159106 clearTimeout ( timer )
160- resolve ( "ERROR" )
107+ reject ( e )
161108 } )
162109 } )
163- expect ( response ) . toContain ( "101 Switching Protocols" )
164- raw . destroy ( )
165- await new Promise < void > ( ( resolve ) => upgradeServer . close ( ( ) => resolve ( ) ) )
110+ expect ( await roundTrip ) . toBe ( "echo:ping" )
111+ probe . close ( )
112+ await new Promise < void > ( ( resolve ) => relay . close ( ( ) => resolve ( ) ) )
113+ await new Promise < void > ( ( resolve ) => childHttp . close ( ( ) => resolve ( ) ) )
166114 } )
167115
168116 test ( "spawnWebChildContainer resolves after the ready line and proxies HTTP; dispose kills the child" , async ( ) => {
0 commit comments