From ab5bb40942c7023046fa6f6d0b49cabc105b6073 Mon Sep 17 00:00:00 2001 From: lzt1008 <75012451+lzt1008@users.noreply.github.com> Date: Thu, 2 Nov 2023 16:49:37 +0800 Subject: [PATCH] fix(types)!: expose httpServer with Http2SecureServer union (#14834) --- packages/vite/src/node/http.ts | 8 +++----- packages/vite/src/node/preview.ts | 9 ++++++--- packages/vite/src/node/server/index.ts | 7 +++++-- packages/vite/src/node/server/middlewares/proxy.ts | 3 ++- packages/vite/src/node/server/ws.ts | 3 ++- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/node/http.ts b/packages/vite/src/node/http.ts index 579b995c9ae92d..2039a6c9f75e22 100644 --- a/packages/vite/src/node/http.ts +++ b/packages/vite/src/node/http.ts @@ -1,14 +1,12 @@ import fsp from 'node:fs/promises' import path from 'node:path' -import type { - Server as HttpServer, - OutgoingHttpHeaders as HttpServerHeaders, -} from 'node:http' +import type { OutgoingHttpHeaders as HttpServerHeaders } from 'node:http' import type { ServerOptions as HttpsServerOptions } from 'node:https' import type { Connect } from 'dep-types/connect' import colors from 'picocolors' import type { ProxyOptions } from './server/middlewares/proxy' import type { Logger } from './logger' +import type { HttpServer } from './server' export interface CommonServerOptions { /** @@ -115,7 +113,7 @@ export async function resolveHttpServer( }, // @ts-expect-error TODO: is this correct? app, - ) as unknown as HttpServer + ) } } diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 3d9329e581f3d8..77b8b0687eea59 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -1,11 +1,14 @@ import fs from 'node:fs' import path from 'node:path' -import type * as http from 'node:http' import sirv from 'sirv' import connect from 'connect' import type { Connect } from 'dep-types/connect' import corsMiddleware from 'cors' -import type { ResolvedServerOptions, ResolvedServerUrls } from './server' +import type { + HttpServer, + ResolvedServerOptions, + ResolvedServerUrls, +} from './server' import type { CommonServerOptions } from './http' import { httpServerStart, @@ -67,7 +70,7 @@ export interface PreviewServer { /** * native Node http server instance */ - httpServer: http.Server + httpServer: HttpServer /** * The resolved urls Vite prints on the CLI. * null before server is listening. diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 086f4d1db51931..9fb7c201a93633 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -3,6 +3,7 @@ import type * as net from 'node:net' import { get as httpGet } from 'node:http' import type * as http from 'node:http' import { performance } from 'node:perf_hooks' +import type { Http2SecureServer } from 'node:http2' import connect from 'connect' import corsMiddleware from 'cors' import colors from 'picocolors' @@ -182,6 +183,8 @@ export type ServerHook = ( server: ViteDevServer, ) => (() => void) | void | Promise<(() => void) | void> +export type HttpServer = http.Server | Http2SecureServer + export interface ViteDevServer extends AsyncDisposable { /** * The resolved vite config object @@ -200,7 +203,7 @@ export interface ViteDevServer extends AsyncDisposable { * native Node http server instance * will be null in middleware mode */ - httpServer: http.Server | null + httpServer: HttpServer | null /** * chokidar watcher instance * https://github.com/paulmillr/chokidar#api @@ -797,7 +800,7 @@ async function startServer( }) } -function createServerCloseFn(server: http.Server | null) { +function createServerCloseFn(server: HttpServer | null) { if (!server) { return () => {} } diff --git a/packages/vite/src/node/server/middlewares/proxy.ts b/packages/vite/src/node/server/middlewares/proxy.ts index 219d15f3c81d21..52fab126b8847f 100644 --- a/packages/vite/src/node/server/middlewares/proxy.ts +++ b/packages/vite/src/node/server/middlewares/proxy.ts @@ -6,6 +6,7 @@ import type { HttpProxy } from 'dep-types/http-proxy' import colors from 'picocolors' import { createDebugger } from '../../utils' import type { CommonServerOptions, ResolvedConfig } from '../..' +import type { HttpServer } from '..' const debug = createDebugger('vite:proxy') @@ -29,7 +30,7 @@ export interface ProxyOptions extends HttpProxy.ServerOptions { } export function proxyMiddleware( - httpServer: http.Server | null, + httpServer: HttpServer | null, options: NonNullable, config: ResolvedConfig, ): Connect.NextHandleFunction { diff --git a/packages/vite/src/node/server/ws.ts b/packages/vite/src/node/server/ws.ts index 4c41b054e5cfd1..195e66d26fa8f0 100644 --- a/packages/vite/src/node/server/ws.ts +++ b/packages/vite/src/node/server/ws.ts @@ -14,6 +14,7 @@ import type { InferCustomEventPayload } from 'types/customEvent' import { ASYNC_DISPOSE } from '../constants' import type { ResolvedConfig } from '..' import { isObject } from '../utils' +import type { HttpServer } from '.' /* In Bun, the `ws` module is overridden to hook into the native code. Using the bundled `js` version * 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 = [ ] export function createWebSocketServer( - server: Server | null, + server: HttpServer | null, config: ResolvedConfig, httpsOptions?: HttpsServerOptions, ): WebSocketServer {