Skip to content

Commit c633c0f

Browse files
authored
perf: use dynamic imports for node:net (#5314)
1 parent b9947f6 commit c633c0f

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

packages/core/src/server/devServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export async function createDevServer<
202202
};
203203

204204
const protocol = https ? 'https' : 'http';
205-
const urls = getAddressUrls({ protocol, port, host });
205+
const urls = await getAddressUrls({ protocol, port, host });
206206

207207
const cliShortcutsEnabled = isCliShortcutsEnabled(devConfig);
208208

packages/core/src/server/helper.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { IncomingMessage, Server } from 'node:http';
22
import type { Http2SecureServer } from 'node:http2';
3-
import net from 'node:net';
43
import type { Socket } from 'node:net';
54
import os from 'node:os';
65
import { posix, relative, sep } from 'node:path';
@@ -265,14 +264,16 @@ export const getPort = async ({
265264
tryLimits = 1;
266265
}
267266

267+
const { createServer } = await import('node:net');
268268
const original = port;
269269

270270
let found = false;
271271
let attempts = 0;
272+
272273
while (!found && attempts <= tryLimits) {
273274
try {
274275
await new Promise((resolve, reject) => {
275-
const server = net.createServer();
276+
const server = createServer();
276277
server.unref();
277278
server.on('error', reject);
278279
server.listen({ port, host }, () => {
@@ -372,11 +373,13 @@ const isLoopbackHost = (host: string) => {
372373
return loopbackHosts.has(host);
373374
};
374375

375-
export const getHostInUrl = (host: string): string => {
376+
export const getHostInUrl = async (host: string): Promise<string> => {
376377
if (host === DEFAULT_DEV_HOST) {
377378
return 'localhost';
378379
}
379-
if (net.isIPv6(host)) {
380+
381+
const { isIPv6 } = await import('node:net');
382+
if (isIPv6(host)) {
380383
return host === '::' ? '[::1]' : `[${host}]`;
381384
}
382385
return host;
@@ -406,24 +409,25 @@ const getUrlLabel = (url: string) => {
406409

407410
type AddressUrl = { label: string; url: string };
408411

409-
export const getAddressUrls = ({
412+
export const getAddressUrls = async ({
410413
protocol = 'http',
411414
port,
412415
host,
413416
}: {
414417
protocol?: string;
415418
port: number;
416419
host?: string;
417-
}): AddressUrl[] => {
420+
}): Promise<AddressUrl[]> => {
418421
if (host && host !== DEFAULT_DEV_HOST) {
422+
const url = concatUrl({
423+
port,
424+
host: await getHostInUrl(host),
425+
protocol,
426+
});
419427
return [
420428
{
421429
label: isLoopbackHost(host) ? LOCAL_LABEL : NETWORK_LABEL,
422-
url: concatUrl({
423-
port,
424-
host: getHostInUrl(host),
425-
protocol,
426-
}),
430+
url,
427431
},
428432
];
429433
}

packages/core/src/server/open.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export async function open({
196196

197197
const urls: string[] = [];
198198
const protocol = https ? 'https' : 'http';
199-
const host = getHostInUrl(config.server.host);
199+
const host = await getHostInUrl(config.server.host);
200200
const baseUrl = `${protocol}://${host}:${port}`;
201201

202202
if (!targets.length) {

packages/core/src/server/prodServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export async function startProdServer(
226226
});
227227

228228
const protocol = https ? 'https' : 'http';
229-
const urls = getAddressUrls({ protocol, port, host });
229+
const urls = await getAddressUrls({ protocol, port, host });
230230
const cliShortcutsEnabled = isCliShortcutsEnabled(config.dev);
231231

232232
const cleanupGracefulShutdown = setupGracefulShutdown();

0 commit comments

Comments
 (0)