From 22833bf13cf0271b5283441d69a159be53277615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=B9=E6=B5=B7?= Date: Thu, 12 Dec 2024 19:04:58 +0800 Subject: [PATCH 1/4] fix: Fix process.env.SOCKET_SERVER port is NO_VALIDATE --- packages/bundler-webpack/src/server/server.ts | 26 ++++++++++++++----- packages/bundler-webpack/src/server/ws.ts | 12 ++++++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/bundler-webpack/src/server/server.ts b/packages/bundler-webpack/src/server/server.ts index b6296e867bae..2bfecd9dd004 100644 --- a/packages/bundler-webpack/src/server/server.ts +++ b/packages/bundler-webpack/src/server/server.ts @@ -4,7 +4,7 @@ import type { Stats } from '@umijs/bundler-webpack/compiled/webpack'; import webpack, { Configuration, } from '@umijs/bundler-webpack/compiled/webpack'; -import { getDevBanner, lodash, logger } from '@umijs/utils'; +import { getDevBanner, lodash, logger, portfinder } from '@umijs/utils'; import cors from 'cors'; import { createReadStream, existsSync } from 'fs'; import http from 'http'; @@ -235,7 +235,22 @@ export async function createServer(opts: IOpts): Promise { return null; } - ws = createWebSocketServer(server); + const protocol = userConfig.https ? 'https:' : 'http:'; + const basePort = opts.port || 8000; + + const parseSocketServerAddress = async () => { + if (!process.env.SOCKET_SERVER) return basePort; + const {port} = new URL(process.env.SOCKET_SERVER); + const startPort = Number(port) || basePort; + if (port) { + const hmrPort = await portfinder.getPortPromise({ port: startPort, stopPort: startPort + 1 }) + // 实际可用的hmr端口和设置的hmr端口不同 + if (startPort !== hmrPort) console.log(`[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`) + } + return Number(startPort) === basePort ? undefined : (Number(startPort) || undefined); + }; + + ws = createWebSocketServer(server, await parseSocketServerAddress()); ws.wss.on('connection', (socket) => { if (stats) { @@ -243,11 +258,8 @@ export async function createServer(opts: IOpts): Promise { } }); - const protocol = userConfig.https ? 'https:' : 'http:'; - const port = opts.port || 8000; - - server.listen(port, () => { - const banner = getDevBanner(protocol, opts.host, port); + server.listen(basePort, () => { + const banner = getDevBanner(protocol, opts.host, basePort); console.log(banner.before); logger.ready(banner.main); diff --git a/packages/bundler-webpack/src/server/ws.ts b/packages/bundler-webpack/src/server/ws.ts index addf2de591c2..15265364ed2c 100644 --- a/packages/bundler-webpack/src/server/ws.ts +++ b/packages/bundler-webpack/src/server/ws.ts @@ -7,10 +7,16 @@ import WebSocket from '../../compiled/ws'; export function createWebSocketServer( server: HttpServer | HttpsServer | Http2Server | Server, + port?: number | undefined, ) { - const wss = new WebSocket.Server({ - noServer: true, - }); + let wss: WebSocket.Server; + if (port) { + wss = new WebSocket.Server({port}); + } else { + wss = new WebSocket.Server({ + noServer: true, + }); + } server.on('upgrade', (req, socket, head) => { if (req.headers['sec-websocket-protocol'] === 'webpack-hmr') { From fe99c0acfc11ba63d1972af8b80df34571af56ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=B9=E6=B5=B7?= Date: Thu, 12 Dec 2024 19:17:48 +0800 Subject: [PATCH 2/4] fix: Fix process.env.SOCKET_SERVER port is NO_VALIDATE --- packages/bundler-webpack/src/server/server.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/bundler-webpack/src/server/server.ts b/packages/bundler-webpack/src/server/server.ts index 2bfecd9dd004..9d6b2e0c5081 100644 --- a/packages/bundler-webpack/src/server/server.ts +++ b/packages/bundler-webpack/src/server/server.ts @@ -242,10 +242,10 @@ export async function createServer(opts: IOpts): Promise { if (!process.env.SOCKET_SERVER) return basePort; const {port} = new URL(process.env.SOCKET_SERVER); const startPort = Number(port) || basePort; - if (port) { - const hmrPort = await portfinder.getPortPromise({ port: startPort, stopPort: startPort + 1 }) - // 实际可用的hmr端口和设置的hmr端口不同 - if (startPort !== hmrPort) console.log(`[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`) + const hmrPort = await portfinder.getPortPromise({ port: startPort }) + if (port && startPort !== hmrPort) { + console.log(`[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`) + return undefined; } return Number(startPort) === basePort ? undefined : (Number(startPort) || undefined); }; From 78e69fe641ef3900225826301e968287aca5a11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=B9=E6=B5=B7?= Date: Thu, 12 Dec 2024 19:23:29 +0800 Subject: [PATCH 3/4] fix: remove Number --- packages/bundler-webpack/src/server/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bundler-webpack/src/server/server.ts b/packages/bundler-webpack/src/server/server.ts index 9d6b2e0c5081..42be546fa70f 100644 --- a/packages/bundler-webpack/src/server/server.ts +++ b/packages/bundler-webpack/src/server/server.ts @@ -247,7 +247,7 @@ export async function createServer(opts: IOpts): Promise { console.log(`[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`) return undefined; } - return Number(startPort) === basePort ? undefined : (Number(startPort) || undefined); + return startPort === basePort ? undefined : startPort; }; ws = createWebSocketServer(server, await parseSocketServerAddress()); From 93b69e60920b053b96560ce7e335a73a5bd858fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=B9=E6=B5=B7?= Date: Mon, 13 Jan 2025 19:17:17 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20server=20port=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/bundler-webpack/src/server/server.ts | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/packages/bundler-webpack/src/server/server.ts b/packages/bundler-webpack/src/server/server.ts index 42be546fa70f..71b1453a56ef 100644 --- a/packages/bundler-webpack/src/server/server.ts +++ b/packages/bundler-webpack/src/server/server.ts @@ -240,31 +240,42 @@ export async function createServer(opts: IOpts): Promise { const parseSocketServerAddress = async () => { if (!process.env.SOCKET_SERVER) return basePort; - const {port} = new URL(process.env.SOCKET_SERVER); + const { port } = new URL(process.env.SOCKET_SERVER); const startPort = Number(port) || basePort; - const hmrPort = await portfinder.getPortPromise({ port: startPort }) - if (port && startPort !== hmrPort) { - console.log(`[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`) + try { + const hmrPort = await portfinder.getPortPromise({ + port: startPort, + stopPort: startPort + 1, + }); + if (port && startPort !== hmrPort) { + console.log( + `[SOCKET_SERVER] hmr port changed from ${port} to ${basePort}`, + ); + return undefined; + } + return startPort === basePort ? undefined : startPort; + } catch (e) { + console.log(`[SOCKET_SERVER] hmr port not found, use ${basePort}`); return undefined; } - return startPort === basePort ? undefined : startPort; }; - ws = createWebSocketServer(server, await parseSocketServerAddress()); - - ws.wss.on('connection', (socket) => { - if (stats) { - sendStats(getStats(stats), false, socket); - } - }); + const hmrPort = await parseSocketServerAddress(); server.listen(basePort, () => { const banner = getDevBanner(protocol, opts.host, basePort); - console.log(banner.before); logger.ready(banner.main); console.log(banner.after); }); + ws = createWebSocketServer(server, hmrPort); + + ws.wss.on('connection', (socket) => { + if (stats) { + sendStats(getStats(stats), false, socket); + } + }); + return server; }