11import * as path from "node:path" ;
22import * as stream from "node:stream" ;
3+ import * as http from "node:http" ;
34import fs from "fs-extra" ;
45import prettyMs from "pretty-ms" ;
56import execa from "execa" ;
@@ -44,18 +45,10 @@ export let serve = async (
4445 httpScheme : string ;
4546 httpHost : string ;
4647 httpPort : number ;
47- webSocketPort : number ;
4848 restart : boolean ;
4949 }
5050) => {
5151 await loadEnv ( initialConfig . rootDirectory ) ;
52- let websocket = Socket . serve ( { port : options . webSocketPort } ) ;
53- let httpOrigin : Origin = {
54- scheme : options . httpScheme ,
55- host : options . httpHost ,
56- port : options . httpPort ,
57- } ;
58-
5952 let state : {
6053 appServer ?: execa . ExecaChildProcess ;
6154 manifest ?: Manifest ;
@@ -65,6 +58,30 @@ export let serve = async (
6558 prevLoaderHashes ?: Record < string , string > ;
6659 } = { } ;
6760
61+ let app = express ( )
62+ // handle `broadcastDevReady` messages
63+ . use ( express . json ( ) )
64+ . post ( "/ping" , ( req , res ) => {
65+ let { buildHash } = req . body ;
66+ if ( typeof buildHash !== "string" ) {
67+ console . warn ( `Unrecognized payload: ${ req . body } ` ) ;
68+ res . sendStatus ( 400 ) ;
69+ }
70+ if ( buildHash === state . manifest ?. version ) {
71+ state . appReady ?. ok ( ) ;
72+ }
73+ res . sendStatus ( 200 ) ;
74+ } ) ;
75+
76+ let server = http . createServer ( app ) ;
77+ let websocket = Socket . serve ( server ) ;
78+
79+ let httpOrigin : Origin = {
80+ scheme : options . httpScheme ,
81+ host : options . httpHost ,
82+ port : options . httpPort ,
83+ } ;
84+
6885 let bin = await detectBin ( ) ;
6986 let startAppServer = ( command : string ) => {
7087 console . log ( `> ${ command } ` ) ;
@@ -119,7 +136,7 @@ export let serve = async (
119136 sourcemap : true ,
120137 onWarning : warnOnce ,
121138 devHttpOrigin : httpOrigin ,
122- devWebSocketPort : options . webSocketPort ,
139+ devWebSocketPort : options . httpPort ,
123140 } ,
124141 } ,
125142 {
@@ -198,28 +215,14 @@ export let serve = async (
198215 }
199216 ) ;
200217
201- let httpServer = express ( )
202- // handle `broadcastDevReady` messages
203- . use ( express . json ( ) )
204- . post ( "/ping" , ( req , res ) => {
205- let { buildHash } = req . body ;
206- if ( typeof buildHash !== "string" ) {
207- console . warn ( `Unrecognized payload: ${ req . body } ` ) ;
208- res . sendStatus ( 400 ) ;
209- }
210- if ( buildHash === state . manifest ?. version ) {
211- state . appReady ?. ok ( ) ;
212- }
213- res . sendStatus ( 200 ) ;
214- } )
215- . listen ( httpOrigin . port , ( ) => {
216- console . log ( "Remix dev server ready" ) ;
217- } ) ;
218+ server . listen ( httpOrigin . port , ( ) => {
219+ console . log ( "Remix dev server ready" ) ;
220+ } ) ;
218221
219222 return new Promise ( ( ) => { } ) . finally ( async ( ) => {
220223 await kill ( state . appServer ) ;
221224 websocket . close ( ) ;
222- httpServer . close ( ) ;
225+ server . close ( ) ;
223226 await dispose ( ) ;
224227 } ) ;
225228} ;
0 commit comments