Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix process.env.SOCKET_SERVER port is NO_VALIDATE #12847

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions packages/bundler-webpack/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -235,19 +235,31 @@ export async function createServer(opts: IOpts): Promise<any> {
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;
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 startPort === basePort ? undefined : startPort;
};

ws = createWebSocketServer(server, await parseSocketServerAddress());

ws.wss.on('connection', (socket) => {
if (stats) {
sendStats(getStats(stats), false, socket);
}
});

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);
Expand Down
12 changes: 9 additions & 3 deletions packages/bundler-webpack/src/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
Loading