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: expose server as Http2SecureServer type #10196

Merged
merged 8 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ interface ViteDevServer {
* Native Node http server instance.
* Will be null in middleware mode.
*/
httpServer: http.Server | null
httpServer: http.Server | Http2SecureServer | null
/**
* Chokidar watcher instance.
* https://github.com/paulmillr/chokidar#api
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo

### `configurePreviewServer`

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

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:
Expand Down
9 changes: 5 additions & 4 deletions packages/vite/src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
OutgoingHttpHeaders as HttpServerHeaders
} from 'node:http'
import type { ServerOptions as HttpsServerOptions } from 'node:https'
import type { Http2SecureServer } from 'node:http2'
import type { Connect } from 'dep-types/connect'
import colors from 'picocolors'
import { isObject } from './utils'
Expand Down Expand Up @@ -94,7 +95,7 @@ export async function resolveHttpServer(
{ proxy }: CommonServerOptions,
app: Connect.Server,
httpsOptions?: HttpsServerOptions
): Promise<HttpServer> {
): Promise<HttpServer | Http2SecureServer> {
if (!httpsOptions) {
const { createServer } = await import('node:http')
return createServer(app)
Expand All @@ -116,7 +117,7 @@ export async function resolveHttpServer(
},
// @ts-expect-error TODO: is this correct?
app
) as unknown as HttpServer
)
}
}

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

export async function httpServerStart(
httpServer: HttpServer,
httpServer: HttpServer | Http2SecureServer,
serverOptions: {
port: number
strictPort: boolean | undefined
Expand Down Expand Up @@ -185,7 +186,7 @@ export async function httpServerStart(
}

export function setClientErrorHandler(
server: HttpServer,
server: HttpServer | Http2SecureServer,
logger: Logger
): void {
server.on('clientError', (err, socket) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { cleanUrl, getHash, normalizePath } from '../utils'
import { FS_PREFIX } from '../constants'

export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g
export const publicAssetUrlRE = /__VITE_PUBLIC_ASSET__([a-z\d]{8})__/g

export const duplicateAssets = new WeakMap<
ResolvedConfig,
Expand Down Expand Up @@ -388,8 +389,6 @@ export const publicAssetUrlCache = new WeakMap<
Map<string, string>
>()

export const publicAssetUrlRE = /__VITE_PUBLIC_ASSET__([a-z\d]{8})__/g

export function publicFileToBuiltUrl(
url: string,
config: ResolvedConfig
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import type * as http from 'node:http'
import type { Http2SecureServer } from 'node:http2'
import sirv from 'sirv'
import connect from 'connect'
import type { Connect } from 'dep-types/connect'
Expand Down Expand Up @@ -51,7 +52,7 @@ export interface PreviewServer {
/**
* native Node http server instance
*/
httpServer: http.Server
httpServer: http.Server | Http2SecureServer
/**
* The resolved urls Vite prints on the CLI
*/
Expand All @@ -66,7 +67,7 @@ export type PreviewServerHook = (
this: void,
server: {
middlewares: Connect.Server
httpServer: http.Server
httpServer: http.Server | Http2SecureServer
}
) => (() => void) | void | Promise<(() => void) | void>

Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs'
import path from 'node:path'
import type * as net from 'node:net'
import type * as http from 'node:http'
import type { Http2SecureServer } from 'node:http2'
import { performance } from 'node:perf_hooks'
import connect from 'connect'
import corsMiddleware from 'cors'
Expand Down Expand Up @@ -181,7 +182,7 @@ export interface ViteDevServer {
* native Node http server instance
* will be null in middleware mode
*/
httpServer: http.Server | null
httpServer: http.Server | Http2SecureServer | null
/**
* chokidar watcher instance
* https://github.com/paulmillr/chokidar#api
Expand Down Expand Up @@ -681,7 +682,7 @@ async function startServer(
}
}

function createServerCloseFn(server: http.Server | null) {
function createServerCloseFn(server: http.Server | Http2SecureServer | null) {
if (!server) {
return () => {}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/server/middlewares/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function compression() {
let pendingListeners = []
let started = false
let size = 0
const { end, write, on, writeHead } = res
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to the PR, would you revert it?


function start() {
started = true
Expand Down Expand Up @@ -81,8 +82,6 @@ export default function compression() {
writeHead.call(res, pendingStatus || res.statusCode)
}

const { end, write, on, writeHead } = res

res.writeHead = function (status, reason, headers) {
if (typeof reason !== 'string') [headers, reason] = [reason, headers]
if (headers) for (let i in headers) res.setHeader(i, headers[i])
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as http from 'node:http'
import type { Http2SecureServer } from 'node:http2'
import type * as net from 'node:net'
import httpProxy from 'http-proxy'
import type { Connect } from 'dep-types/connect'
Expand Down Expand Up @@ -26,11 +27,11 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
req: http.IncomingMessage,
res: http.ServerResponse,
options: ProxyOptions
) => void | null | undefined | false | string
) => void | null | undefined | false | string | HttpProxy.ServerOptions
}

export function proxyMiddleware(
httpServer: http.Server | null,
httpServer: http.Server | Http2SecureServer | 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 @@ -2,6 +2,7 @@ import type { Server } from 'node:http'
import { STATUS_CODES } from 'node:http'
import type { ServerOptions as HttpsServerOptions } from 'node:https'
import { createServer as createHttpsServer } from 'node:https'
import type { Http2SecureServer } from 'node:http2'
import type { Socket } from 'node:net'
import colors from 'picocolors'
import type { ServerOptions, WebSocket as WebSocketRaw } from 'ws'
Expand Down Expand Up @@ -78,7 +79,7 @@ const wsServerEvents = [
]

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