Skip to content

Commit f204f0c

Browse files
authored
refactor(server)!: convert some public methods to private (#149)
1 parent fd5c256 commit f204f0c

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

src/server.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class Server<
391391
return path.resolve(dir, 'node_modules/.cache/rspack-dev-server');
392392
}
393393

394-
addAdditionalEntries(compiler: Compiler) {
394+
#addAdditionalEntries(compiler: Compiler) {
395395
const additionalEntries: string[] = [];
396396
const isWebTarget = Boolean(compiler.platform.web);
397397

@@ -560,7 +560,7 @@ class Server<
560560
* @private
561561
* @returns {Compiler["options"]} compiler options
562562
*/
563-
getCompilerOptions() {
563+
#getCompilerOptions() {
564564
if (typeof (this.compiler as MultiCompiler).compilers !== 'undefined') {
565565
if ((this.compiler as MultiCompiler).compilers.length === 1) {
566566
return (this.compiler as MultiCompiler).compilers[0].options;
@@ -591,15 +591,15 @@ class Server<
591591
return (this.compiler as Compiler).options;
592592
}
593593

594-
shouldLogInfrastructureInfo() {
595-
const compilerOptions = this.getCompilerOptions();
594+
#shouldLogInfrastructureInfo() {
595+
const compilerOptions = this.#getCompilerOptions();
596596
const { level = 'info' } = compilerOptions.infrastructureLogging || {};
597597
return level === 'info' || level === 'log' || level === 'verbose';
598598
}
599599

600-
async normalizeOptions() {
600+
async #normalizeOptions() {
601601
const { options } = this;
602-
const compilerOptions = this.getCompilerOptions();
602+
const compilerOptions = this.#getCompilerOptions();
603603
const compilerWatchOptions = compilerOptions.watchOptions;
604604
const getWatchOptions = (
605605
watchOptions: WatchOptions & {
@@ -1217,7 +1217,7 @@ class Server<
12171217
* @private
12181218
* @returns {string} client transport
12191219
*/
1220-
getClientTransport() {
1220+
#getClientTransport() {
12211221
let clientImplementation: string | undefined;
12221222
let clientImplementationFound = true;
12231223

@@ -1285,7 +1285,7 @@ class Server<
12851285
return clientImplementation as string;
12861286
}
12871287

1288-
getServerTransport() {
1288+
#getServerTransport() {
12891289
let implementation: unknown;
12901290
let implementationFound = true;
12911291

@@ -1352,7 +1352,7 @@ class Server<
13521352
}
13531353
}
13541354

1355-
setupProgressPlugin(): void {
1355+
#setupProgressPlugin(): void {
13561356
const { ProgressPlugin } = (this.compiler as MultiCompiler).compilers
13571357
? (this.compiler as MultiCompiler).compilers[0].webpack
13581358
: (this.compiler as Compiler).webpack;
@@ -1388,7 +1388,7 @@ class Server<
13881388
* @private
13891389
* @returns {Promise<void>}
13901390
*/
1391-
async initialize() {
1391+
async #initialize() {
13921392
const compilers = isMultiCompiler(this.compiler)
13931393
? this.compiler.compilers
13941394
: [this.compiler];
@@ -1411,10 +1411,10 @@ class Server<
14111411
}
14121412
}
14131413

1414-
this.setupHooks();
1414+
this.#setupHooks();
14151415

1416-
await this.setupApp();
1417-
await this.createServer();
1416+
await this.#setupApp();
1417+
await this.#createServer();
14181418

14191419
if (this.options.webSocketServer) {
14201420
const compilers =
@@ -1426,12 +1426,12 @@ class Server<
14261426
continue;
14271427
}
14281428

1429-
this.addAdditionalEntries(compiler);
1429+
this.#addAdditionalEntries(compiler);
14301430

14311431
const { ProvidePlugin, HotModuleReplacementPlugin } = compiler.rspack;
14321432

14331433
new ProvidePlugin({
1434-
__rspack_dev_server_client__: this.getClientTransport() as
1434+
__rspack_dev_server_client__: this.#getClientTransport() as
14351435
| string
14361436
| string[],
14371437
}).apply(compiler);
@@ -1459,13 +1459,13 @@ class Server<
14591459
this.options.client &&
14601460
(this.options.client as ClientConfiguration).progress
14611461
) {
1462-
this.setupProgressPlugin();
1462+
this.#setupProgressPlugin();
14631463
}
14641464
}
14651465

1466-
this.setupWatchFiles();
1467-
this.setupWatchStaticFiles();
1468-
await this.setupMiddlewares();
1466+
this.#setupWatchFiles();
1467+
this.#setupWatchStaticFiles();
1468+
await this.#setupMiddlewares();
14691469

14701470
if (this.options.setupExitSignals) {
14711471
const signals = ['SIGINT', 'SIGTERM'];
@@ -1521,17 +1521,17 @@ class Server<
15211521
}
15221522
}
15231523

1524-
async setupApp(): Promise<void> {
1524+
async #setupApp(): Promise<void> {
15251525
this.app = (
15261526
typeof this.options.app === 'function'
15271527
? await this.options.app()
15281528
: (await getConnect())()
15291529
) as A;
15301530
}
15311531

1532-
getStats(statsObj: Stats | MultiStats): StatsCompilation {
1532+
#getStats(statsObj: Stats | MultiStats): StatsCompilation {
15331533
const stats = Server.DEFAULT_STATS;
1534-
const compilerOptions = this.getCompilerOptions();
1534+
const compilerOptions = this.#getCompilerOptions();
15351535

15361536
if (
15371537
compilerOptions.stats &&
@@ -1546,7 +1546,7 @@ class Server<
15461546
return statsObj.toJson(stats);
15471547
}
15481548

1549-
setupHooks(): void {
1549+
#setupHooks(): void {
15501550
this.compiler.hooks.invalid.tap('rspack-dev-server', () => {
15511551
if (this.webSocketServer) {
15521552
this.sendMessage(this.webSocketServer.clients, 'invalid');
@@ -1556,14 +1556,14 @@ class Server<
15561556
'rspack-dev-server',
15571557
(stats: Stats | MultiStats): void => {
15581558
if (this.webSocketServer) {
1559-
this.sendStats(this.webSocketServer.clients, this.getStats(stats));
1559+
this.#sendStats(this.webSocketServer.clients, this.#getStats(stats));
15601560
}
15611561
this.stats = stats;
15621562
},
15631563
);
15641564
}
15651565

1566-
setupWatchStaticFiles(): void {
1566+
#setupWatchStaticFiles(): void {
15671567
const watchFiles = this.options.static as NormalizedStatic[];
15681568

15691569
if (watchFiles.length > 0) {
@@ -1575,7 +1575,7 @@ class Server<
15751575
}
15761576
}
15771577

1578-
setupWatchFiles(): void {
1578+
#setupWatchFiles(): void {
15791579
const watchFiles = this.options.watchFiles as WatchFiles[];
15801580

15811581
if (watchFiles.length > 0) {
@@ -1585,7 +1585,7 @@ class Server<
15851585
}
15861586
}
15871587

1588-
async setupMiddlewares(): Promise<void> {
1588+
async #setupMiddlewares(): Promise<void> {
15891589
let middlewares: Middleware[] = [];
15901590

15911591
// Register setup host header check for security
@@ -1661,7 +1661,7 @@ class Server<
16611661
if (typeof this.options.headers !== 'undefined') {
16621662
middlewares.push({
16631663
name: 'set-headers',
1664-
middleware: this.setHeaders.bind(this),
1664+
middleware: this.#setHeaders.bind(this),
16651665
});
16661666
}
16671667

@@ -2056,7 +2056,7 @@ class Server<
20562056
* @private
20572057
* @returns {Promise<void>}
20582058
*/
2059-
async createServer() {
2059+
async #createServer() {
20602060
const { type, options } = this.options.server as ServerConfiguration<A, S>;
20612061

20622062
if (typeof type === 'function') {
@@ -2092,8 +2092,8 @@ class Server<
20922092
});
20932093
}
20942094

2095-
createWebSocketServer() {
2096-
this.webSocketServer = new (this.getServerTransport() as EXPECTED_ANY)(
2095+
#createWebSocketServer() {
2096+
this.webSocketServer = new (this.#getServerTransport() as EXPECTED_ANY)(
20972097
this,
20982098
);
20992099

@@ -2115,7 +2115,7 @@ class Server<
21152115
!headers ||
21162116
!this.isValidHost(headers, 'host') ||
21172117
!this.isValidHost(headers, 'origin') ||
2118-
!this.isSameOrigin(headers)
2118+
!this.#isSameOrigin(headers)
21192119
) {
21202120
this.sendMessage([client], 'error', 'Invalid Host/Origin header');
21212121

@@ -2187,12 +2187,12 @@ class Server<
21872187
return;
21882188
}
21892189

2190-
this.sendStats([client], this.getStats(this.stats), true);
2190+
this.#sendStats([client], this.#getStats(this.stats), true);
21912191
},
21922192
);
21932193
}
21942194

2195-
async openBrowser(defaultOpenTarget: string): Promise<void> {
2195+
async #openBrowser(defaultOpenTarget: string): Promise<void> {
21962196
const { default: open } = await import(
21972197
/* webpackChunkName: "open" */ 'open'
21982198
);
@@ -2229,7 +2229,7 @@ class Server<
22292229
);
22302230
}
22312231

2232-
async logStatus() {
2232+
async #logStatus() {
22332233
const server = this.server as S;
22342234

22352235
if (this.options.ipc) {
@@ -2307,7 +2307,7 @@ class Server<
23072307
);
23082308
}
23092309

2310-
if (urlLogs.length && this.shouldLogInfrastructureInfo()) {
2310+
if (urlLogs.length && this.#shouldLogInfrastructureInfo()) {
23112311
console.log(`${urlLogs.join('\n')}\n`);
23122312
}
23132313

@@ -2320,12 +2320,12 @@ class Server<
23202320
: this.options.host,
23212321
);
23222322

2323-
await this.openBrowser(openTarget);
2323+
await this.#openBrowser(openTarget);
23242324
}
23252325
}
23262326
}
23272327

2328-
setHeaders(req: Request, res: Response, next: NextFunction) {
2328+
#setHeaders(req: Request, res: Response, next: NextFunction) {
23292329
let { headers } = this.options;
23302330

23312331
if (headers) {
@@ -2359,7 +2359,7 @@ class Server<
23592359
next();
23602360
}
23612361

2362-
isHostAllowed(value: string): boolean {
2362+
#isHostAllowed(value: string): boolean {
23632363
const { allowedHosts } = this.options;
23642364

23652365
// allow user to opt out of this security check, at their own risk
@@ -2431,7 +2431,7 @@ class Server<
24312431
return false;
24322432
}
24332433

2434-
if (this.isHostAllowed(hostname)) {
2434+
if (this.#isHostAllowed(hostname)) {
24352435
return true;
24362436
}
24372437

@@ -2455,7 +2455,7 @@ class Server<
24552455
return isValidHostname;
24562456
}
24572457

2458-
isSameOrigin(headers: Record<string, string | undefined>): boolean {
2458+
#isSameOrigin(headers: Record<string, string | undefined>): boolean {
24592459
if (this.options.allowedHosts === 'all') {
24602460
return true;
24612461
}
@@ -2476,7 +2476,7 @@ class Server<
24762476
return false;
24772477
}
24782478

2479-
if (this.isHostAllowed(origin)) {
2479+
if (this.#isHostAllowed(origin)) {
24802480
return true;
24812481
}
24822482

@@ -2496,7 +2496,7 @@ class Server<
24962496
return false;
24972497
}
24982498

2499-
if (this.isHostAllowed(host)) {
2499+
if (this.#isHostAllowed(host)) {
25002500
return true;
25012501
}
25022502

@@ -2518,7 +2518,7 @@ class Server<
25182518
}
25192519

25202520
// Send stats to a socket or multiple sockets
2521-
sendStats(
2521+
#sendStats(
25222522
clients: ClientConnection[],
25232523
stats: StatsCompilation,
25242524
force?: boolean,
@@ -2605,7 +2605,7 @@ class Server<
26052605
}
26062606

26072607
async start(): Promise<void> {
2608-
await this.normalizeOptions();
2608+
await this.#normalizeOptions();
26092609

26102610
if (this.options.ipc) {
26112611
await new Promise<void>((resolve, reject) => {
@@ -2643,7 +2643,7 @@ class Server<
26432643
);
26442644
}
26452645

2646-
await this.initialize();
2646+
await this.#initialize();
26472647

26482648
const listenOptions = this.options.ipc
26492649
? { path: this.options.ipc }
@@ -2663,10 +2663,10 @@ class Server<
26632663
}
26642664

26652665
if (this.options.webSocketServer) {
2666-
this.createWebSocketServer();
2666+
this.#createWebSocketServer();
26672667
}
26682668

2669-
await this.logStatus();
2669+
await this.#logStatus();
26702670

26712671
if (typeof this.options.onListening === 'function') {
26722672
this.options.onListening(this);

0 commit comments

Comments
 (0)