Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types)!: expose httpServer with Http2SecureServer union #14834

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -115,7 +113,7 @@ export async function resolveHttpServer(
},
// @ts-expect-error TODO: is this correct?
app,
) as unknown as HttpServer
)
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -181,6 +182,8 @@ export type ServerHook = (
server: ViteDevServer,
) => (() => void) | void | Promise<(() => void) | void>

export type HttpServer = http.Server | Http2SecureServer

export interface ViteDevServer {
/**
* The resolved vite config object
Expand All @@ -199,7 +202,7 @@ export interface ViteDevServer {
* 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
Expand Down Expand Up @@ -769,7 +772,7 @@ async function startServer(
})
}

function createServerCloseFn(server: http.Server | null) {
function createServerCloseFn(server: HttpServer | null) {
if (!server) {
return () => {}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

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

export function proxyMiddleware(
httpServer: http.Server | null,
httpServer: HttpServer | null,
options: NonNullable<CommonServerOptions['proxy']>,
config: ResolvedConfig,
): Connect.NextHandleFunction {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { CustomPayload, ErrorPayload, HMRPayload } from 'types/hmrPayload'
import type { InferCustomEventPayload } from 'types/customEvent'
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.
Expand Down Expand Up @@ -92,7 +93,7 @@ const wsServerEvents = [
]

export function createWebSocketServer(
server: Server | null,
server: HttpServer | null,
config: ResolvedConfig,
httpsOptions?: HttpsServerOptions,
): WebSocketServer {
Expand Down