Skip to content

Commit

Permalink
fix(types)!: expose httpServer with Http2SecureServer union (#14834)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzt1008 authored Nov 2, 2023
1 parent 997f2d5 commit ab5bb40
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
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

0 comments on commit ab5bb40

Please sign in to comment.