Skip to content

Commit f328f61

Browse files
authored
fix: expose server as Http2SecureServer type (#10196)
1 parent 27e2832 commit f328f61

File tree

7 files changed

+17
-12
lines changed

7 files changed

+17
-12
lines changed

docs/guide/api-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ interface ViteDevServer {
7272
* Native Node http server instance.
7373
* Will be null in middleware mode.
7474
*/
75-
httpServer: http.Server | null
75+
httpServer: http.Server | Http2SecureServer | null
7676
/**
7777
* Chokidar watcher instance.
7878
* https://github.com/paulmillr/chokidar#api

docs/guide/api-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
307307

308308
### `configurePreviewServer`
309309

310-
- **Type:** `(server: { middlewares: Connect.Server, httpServer: http.Server }) => (() => void) | void | Promise<(() => void) | void>`
310+
- **Type:** `(server: { middlewares: Connect.Server, httpServer: http.Server | Http2SecureServer }) => (() => void) | void | Promise<(() => void) | void>`
311311
- **Kind:** `async`, `sequential`
312312

313313
Same as [`configureServer`](/guide/api-plugin.html#configureserver) but for the preview server. It provides the [connect](https://github.com/senchalabs/connect) server and its underlying [http server](https://nodejs.org/api/http.html). Similarly to `configureServer`, the `configurePreviewServer` hook is called before other middlewares are installed. If you want to inject a middleware **after** other middlewares, you can return a function from `configurePreviewServer`, which will be called after internal middlewares are installed:

packages/vite/src/node/http.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
OutgoingHttpHeaders as HttpServerHeaders
66
} from 'node:http'
77
import type { ServerOptions as HttpsServerOptions } from 'node:https'
8+
import type { Http2SecureServer } from 'node:http2'
89
import type { Connect } from 'dep-types/connect'
910
import colors from 'picocolors'
1011
import { isObject } from './utils'
@@ -94,7 +95,7 @@ export async function resolveHttpServer(
9495
{ proxy }: CommonServerOptions,
9596
app: Connect.Server,
9697
httpsOptions?: HttpsServerOptions
97-
): Promise<HttpServer> {
98+
): Promise<HttpServer | Http2SecureServer> {
9899
if (!httpsOptions) {
99100
const { createServer } = await import('node:http')
100101
return createServer(app)
@@ -116,7 +117,7 @@ export async function resolveHttpServer(
116117
},
117118
// @ts-expect-error TODO: is this correct?
118119
app
119-
) as unknown as HttpServer
120+
)
120121
}
121122
}
122123

@@ -149,7 +150,7 @@ function readFileIfExists(value?: string | Buffer | any[]) {
149150
}
150151

151152
export async function httpServerStart(
152-
httpServer: HttpServer,
153+
httpServer: HttpServer | Http2SecureServer,
153154
serverOptions: {
154155
port: number
155156
strictPort: boolean | undefined
@@ -185,7 +186,7 @@ export async function httpServerStart(
185186
}
186187

187188
export function setClientErrorHandler(
188-
server: HttpServer,
189+
server: HttpServer | Http2SecureServer,
189190
logger: Logger
190191
): void {
191192
server.on('clientError', (err, socket) => {

packages/vite/src/node/preview.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import type * as http from 'node:http'
3+
import type { Http2SecureServer } from 'node:http2'
34
import sirv from 'sirv'
45
import connect from 'connect'
56
import type { Connect } from 'dep-types/connect'
@@ -51,7 +52,7 @@ export interface PreviewServer {
5152
/**
5253
* native Node http server instance
5354
*/
54-
httpServer: http.Server
55+
httpServer: http.Server | Http2SecureServer
5556
/**
5657
* The resolved urls Vite prints on the CLI
5758
*/
@@ -66,7 +67,7 @@ export type PreviewServerHook = (
6667
this: void,
6768
server: {
6869
middlewares: Connect.Server
69-
httpServer: http.Server
70+
httpServer: http.Server | Http2SecureServer
7071
}
7172
) => (() => void) | void | Promise<(() => void) | void>
7273

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import type * as net from 'node:net'
44
import type * as http from 'node:http'
5+
import type { Http2SecureServer } from 'node:http2'
56
import { performance } from 'node:perf_hooks'
67
import connect from 'connect'
78
import corsMiddleware from 'cors'
@@ -182,7 +183,7 @@ export interface ViteDevServer {
182183
* native Node http server instance
183184
* will be null in middleware mode
184185
*/
185-
httpServer: http.Server | null
186+
httpServer: http.Server | Http2SecureServer | null
186187
/**
187188
* chokidar watcher instance
188189
* https://github.com/paulmillr/chokidar#api
@@ -692,7 +693,7 @@ async function startServer(
692693
}
693694
}
694695

695-
function createServerCloseFn(server: http.Server | null) {
696+
function createServerCloseFn(server: http.Server | Http2SecureServer | null) {
696697
if (!server) {
697698
return () => {}
698699
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type * as http from 'node:http'
2+
import type { Http2SecureServer } from 'node:http2'
23
import type * as net from 'node:net'
34
import httpProxy from 'http-proxy'
45
import type { Connect } from 'dep-types/connect'
@@ -30,7 +31,7 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
3031
}
3132

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Server } from 'node:http'
22
import { STATUS_CODES } from 'node:http'
33
import type { ServerOptions as HttpsServerOptions } from 'node:https'
44
import { createServer as createHttpsServer } from 'node:https'
5+
import type { Http2SecureServer } from 'node:http2'
56
import type { Socket } from 'node:net'
67
import colors from 'picocolors'
78
import type { ServerOptions, WebSocket as WebSocketRaw } from 'ws'
@@ -78,7 +79,7 @@ const wsServerEvents = [
7879
]
7980

8081
export function createWebSocketServer(
81-
server: Server | null,
82+
server: Server | Http2SecureServer | null,
8283
config: ResolvedConfig,
8384
httpsOptions?: HttpsServerOptions
8485
): WebSocketServer {

0 commit comments

Comments
 (0)