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 all 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 @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -797,7 +800,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 @@ -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.
Expand Down Expand Up @@ -93,7 +94,7 @@ const wsServerEvents = [
]

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