automatically try port 8080 if 80 won't do

Massimo Melina committed Nov 12, 2024 at 23:51 UTC 9aeac280ff4182cf195e0d8a078e14bb7290c680
2 files changed +7 -5
src/listen.ts
+7 -4
@@ -45,8 +45,13 @@ const considerHttp = debounceAsync(async () => {
45 await readyToListen
46 void stopServer(httpSrv)
47 httpSrv = Object.assign(http.createServer(commonServerOptions, app.callback()), { name: 'http' }, commonServerAssign)
48 - const port = await startServer(httpSrv, { port: portCfg.get(), host: listenInterface.get() })
49 - if (!port) return
48 + const host = listenInterface.get()
49 + const port = portCfg.get()
50 + if (!await startServer(httpSrv, { port, host }))
51 + if (port !== 80)
52 + return console.log(` >> try specifying a different port, enter this command: config ${portCfg.key()} 1080`)
53 + else if (!await startServer(httpSrv, { port: 8080, host }))
54 + return
55 httpSrv.on('connection', newConnection)
56 printUrls(httpSrv.name)
57 if (openBrowserAtStart.get() && !argv.updated)
@@ -230,8 +235,6 @@ export function startServer(srv: typeof httpSrv, { port, host }: StartServer) {
235 srv.error = `port ${port} busy: ${await srv.busy || "unknown process"}`
236 }
237 console.error(srv.name, srv.error)
233 - const k = (srv === httpSrv? portCfg : httpsPortCfg).key()
234 - console.log(` >> try specifying a different port, enter this command: config ${k} 1080`)
238 resolve(0)
239 }
240 })
src/util-http.ts
-1
@@ -5,7 +5,6 @@ import http, { IncomingMessage } from 'node:http'
5 import { Readable } from 'node:stream'
6 import _ from 'lodash'
7 import { text as stream2string, buffer } from 'node:stream/consumers'
8 -import { stringAfter } from './cross'
8 export { stream2string }
9
10 export async function httpString(url: string, options?: XRequestOptions): Promise<string> {