Skip to content

Commit ab5bb40

Browse files
authored
fix(types)!: expose httpServer with Http2SecureServer union (#14834)
1 parent 997f2d5 commit ab5bb40

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

packages/vite/src/node/http.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import fsp from 'node:fs/promises'
22
import path from 'node:path'
3-
import type {
4-
Server as HttpServer,
5-
OutgoingHttpHeaders as HttpServerHeaders,
6-
} from 'node:http'
3+
import type { OutgoingHttpHeaders as HttpServerHeaders } from 'node:http'
74
import type { ServerOptions as HttpsServerOptions } from 'node:https'
85
import type { Connect } from 'dep-types/connect'
96
import colors from 'picocolors'
107
import type { ProxyOptions } from './server/middlewares/proxy'
118
import type { Logger } from './logger'
9+
import type { HttpServer } from './server'
1210

1311
export interface CommonServerOptions {
1412
/**
@@ -115,7 +113,7 @@ export async function resolveHttpServer(
115113
},
116114
// @ts-expect-error TODO: is this correct?
117115
app,
118-
) as unknown as HttpServer
116+
)
119117
}
120118
}
121119

packages/vite/src/node/preview.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3-
import type * as http from 'node:http'
43
import sirv from 'sirv'
54
import connect from 'connect'
65
import type { Connect } from 'dep-types/connect'
76
import corsMiddleware from 'cors'
8-
import type { ResolvedServerOptions, ResolvedServerUrls } from './server'
7+
import type {
8+
HttpServer,
9+
ResolvedServerOptions,
10+
ResolvedServerUrls,
11+
} from './server'
912
import type { CommonServerOptions } from './http'
1013
import {
1114
httpServerStart,
@@ -67,7 +70,7 @@ export interface PreviewServer {
6770
/**
6871
* native Node http server instance
6972
*/
70-
httpServer: http.Server
73+
httpServer: HttpServer
7174
/**
7275
* The resolved urls Vite prints on the CLI.
7376
* null before server is listening.

packages/vite/src/node/server/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type * as net from 'node:net'
33
import { get as httpGet } from 'node:http'
44
import type * as http from 'node:http'
55
import { performance } from 'node:perf_hooks'
6+
import type { Http2SecureServer } from 'node:http2'
67
import connect from 'connect'
78
import corsMiddleware from 'cors'
89
import colors from 'picocolors'
@@ -182,6 +183,8 @@ export type ServerHook = (
182183
server: ViteDevServer,
183184
) => (() => void) | void | Promise<(() => void) | void>
184185

186+
export type HttpServer = http.Server | Http2SecureServer
187+
185188
export interface ViteDevServer extends AsyncDisposable {
186189
/**
187190
* The resolved vite config object
@@ -200,7 +203,7 @@ export interface ViteDevServer extends AsyncDisposable {
200203
* native Node http server instance
201204
* will be null in middleware mode
202205
*/
203-
httpServer: http.Server | null
206+
httpServer: HttpServer | null
204207
/**
205208
* chokidar watcher instance
206209
* https://github.com/paulmillr/chokidar#api
@@ -797,7 +800,7 @@ async function startServer(
797800
})
798801
}
799802

800-
function createServerCloseFn(server: http.Server | null) {
803+
function createServerCloseFn(server: HttpServer | null) {
801804
if (!server) {
802805
return () => {}
803806
}

packages/vite/src/node/server/middlewares/proxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { HttpProxy } from 'dep-types/http-proxy'
66
import colors from 'picocolors'
77
import { createDebugger } from '../../utils'
88
import type { CommonServerOptions, ResolvedConfig } from '../..'
9+
import type { HttpServer } from '..'
910

1011
const debug = createDebugger('vite:proxy')
1112

@@ -29,7 +30,7 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
2930
}
3031

3132
export function proxyMiddleware(
32-
httpServer: http.Server | null,
33+
httpServer: HttpServer | null,
3334
options: NonNullable<CommonServerOptions['proxy']>,
3435
config: ResolvedConfig,
3536
): Connect.NextHandleFunction {

packages/vite/src/node/server/ws.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { InferCustomEventPayload } from 'types/customEvent'
1414
import { ASYNC_DISPOSE } from '../constants'
1515
import type { ResolvedConfig } from '..'
1616
import { isObject } from '../utils'
17+
import type { HttpServer } from '.'
1718

1819
/* In Bun, the `ws` module is overridden to hook into the native code. Using the bundled `js` version
1920
* of `ws` will not work as Bun's req.socket does not allow reading/writing to the underlying socket.
@@ -93,7 +94,7 @@ const wsServerEvents = [
9394
]
9495

9596
export function createWebSocketServer(
96-
server: Server | null,
97+
server: HttpServer | null,
9798
config: ResolvedConfig,
9899
httpsOptions?: HttpsServerOptions,
99100
): WebSocketServer {

0 commit comments

Comments
 (0)