Skip to content

Commit 26d9872

Browse files
authored
refactor: use http-proxy-3 (#20402)
1 parent f19c4d7 commit 26d9872

File tree

13 files changed

+73
-450
lines changed

13 files changed

+73
-450
lines changed

docs/config/preview-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Automatically open the app in the browser on server start. When the value is a s
8080

8181
Configure custom proxy rules for the preview server. Expects an object of `{ key: options }` pairs. If the key starts with `^`, it will be interpreted as a `RegExp`. The `configure` option can be used to access the proxy instance.
8282

83-
Uses [`http-proxy`](https://github.com/http-party/node-http-proxy). Full options [here](https://github.com/http-party/node-http-proxy#options).
83+
Uses [`http-proxy-3`](https://github.com/sagemathinc/http-proxy-3). Full options [here](https://github.com/sagemathinc/http-proxy-3#options).
8484

8585
## preview.cors
8686

docs/config/server-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Configure custom proxy rules for the dev server. Expects an object of `{ key: op
122122

123123
Note that if you are using non-relative [`base`](/config/shared-options.md#base), you must prefix each key with that `base`.
124124

125-
Extends [`http-proxy`](https://github.com/http-party/node-http-proxy#options). Additional options are [here](https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/proxy.ts#L13).
125+
Extends [`http-proxy-3`](https://github.com/sagemathinc/http-proxy-3#options). Additional options are [here](https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/proxy.ts#L13).
126126

127127
In some cases, you might also want to configure the underlying dev server (e.g. to add custom middlewares to the internal [connect](https://github.com/senchalabs/connect) app). In order to do that, you need to write your own [plugin](/guide/using-plugins.html) and use [configureServer](/guide/api-plugin.html#configureserver) function.
128128

packages/vite/LICENSE.md

Lines changed: 21 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,35 +1059,6 @@ Repository: jshttp/etag
10591059
10601060
---------------------------------------
10611061

1062-
## eventemitter3
1063-
License: MIT
1064-
By: Arnout Kazemier
1065-
Repository: git://github.com/primus/eventemitter3.git
1066-
1067-
> The MIT License (MIT)
1068-
>
1069-
> Copyright (c) 2014 Arnout Kazemier
1070-
>
1071-
> Permission is hereby granted, free of charge, to any person obtaining a copy
1072-
> of this software and associated documentation files (the "Software"), to deal
1073-
> in the Software without restriction, including without limitation the rights
1074-
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1075-
> copies of the Software, and to permit persons to whom the Software is
1076-
> furnished to do so, subject to the following conditions:
1077-
>
1078-
> The above copyright notice and this permission notice shall be included in all
1079-
> copies or substantial portions of the Software.
1080-
>
1081-
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1082-
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1083-
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1084-
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1085-
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1086-
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1087-
> SOFTWARE.
1088-
1089-
---------------------------------------
1090-
10911062
## finalhandler
10921063
License: MIT
10931064
By: Douglas Christopher Wilson
@@ -1225,33 +1196,33 @@ Repository: git+https://github.com/sapphi-red/host-validation-middleware.git
12251196
12261197
---------------------------------------
12271198

1228-
## http-proxy
1199+
## http-proxy-3
12291200
License: MIT
1230-
By: Charlie Robbins, jcrugzz <jcrugzz@gmail.com>
1231-
Repository: https://github.com/http-party/node-http-proxy.git
1201+
By: William Stein, Charlie Robbins, Jimb Esser, jcrugzz
1202+
Repository: https://github.com/sagemathinc/http-proxy-3.git
12321203

1233-
> node-http-proxy
1204+
> node-http-3
12341205
>
1235-
> Copyright (c) 2010-2016 Charlie Robbins, Jarrett Cruger & the Contributors.
1206+
> Copyright (c) 2010-2025 William Stein, Charlie Robbins, Jarrett Cruger & the Contributors.
12361207
>
1237-
> Permission is hereby granted, free of charge, to any person obtaining
1238-
> a copy of this software and associated documentation files (the
1239-
> "Software"), to deal in the Software without restriction, including
1240-
> without limitation the rights to use, copy, modify, merge, publish,
1241-
> distribute, sublicense, and/or sell copies of the Software, and to
1242-
> permit persons to whom the Software is furnished to do so, subject to
1243-
> the following conditions:
1208+
> Permission is hereby granted, free of charge, to any person obtaining
1209+
> a copy of this software and associated documentation files (the
1210+
> "Software"), to deal in the Software without restriction, including
1211+
> without limitation the rights to use, copy, modify, merge, publish,
1212+
> distribute, sublicense, and/or sell copies of the Software, and to
1213+
> permit persons to whom the Software is furnished to do so, subject to
1214+
> the following conditions:
12441215
>
1245-
> The above copyright notice and this permission notice shall be
1246-
> included in all copies or substantial portions of the Software.
1216+
> The above copyright notice and this permission notice shall be
1217+
> included in all copies or substantial portions of the Software.
12471218
>
1248-
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1249-
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1250-
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1251-
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1252-
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1253-
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1254-
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1219+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1220+
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1221+
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1222+
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1223+
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1224+
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1225+
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12551226
12561227
---------------------------------------
12571228

@@ -2089,35 +2060,6 @@ Repository: git://github.com/paulmillr/readdirp.git
20892060
20902061
---------------------------------------
20912062

2092-
## requires-port
2093-
License: MIT
2094-
By: Arnout Kazemier
2095-
Repository: https://github.com/unshiftio/requires-port
2096-
2097-
> The MIT License (MIT)
2098-
>
2099-
> Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors.
2100-
>
2101-
> Permission is hereby granted, free of charge, to any person obtaining a copy
2102-
> of this software and associated documentation files (the "Software"), to deal
2103-
> in the Software without restriction, including without limitation the rights
2104-
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2105-
> copies of the Software, and to permit persons to whom the Software is
2106-
> furnished to do so, subject to the following conditions:
2107-
>
2108-
> The above copyright notice and this permission notice shall be included in all
2109-
> copies or substantial portions of the Software.
2110-
>
2111-
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2112-
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2113-
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2114-
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2115-
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2116-
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2117-
> SOFTWARE.
2118-
2119-
---------------------------------------
2120-
21212063
## resolve.exports, totalist
21222064
License: MIT
21232065
By: Luke Edwards

packages/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"estree-walker": "^3.0.3",
122122
"etag": "^1.8.1",
123123
"host-validation-middleware": "^0.1.1",
124-
"http-proxy": "^1.18.1",
124+
"http-proxy-3": "^1.20.10",
125125
"launch-editor-middleware": "^2.10.0",
126126
"lightningcss": "^1.30.1",
127127
"magic-string": "^0.30.17",

packages/vite/rolldown.dts.config.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFileSync } from 'node:fs'
22
import { fileURLToPath } from 'node:url'
3+
import { builtinModules } from 'node:module'
34
import { defineConfig } from 'rolldown'
45
import type {
56
OutputChunk,
@@ -30,7 +31,6 @@ const external = [
3031
'rollup/parseAst',
3132
...Object.keys(pkg.dependencies),
3233
...Object.keys(pkg.peerDependencies),
33-
...Object.keys(pkg.devDependencies),
3434
]
3535

3636
export default defineConfig({
@@ -48,7 +48,12 @@ export default defineConfig({
4848
external,
4949
plugins: [
5050
patchTypes(),
51-
dts({ tsconfig: './src/node/tsconfig.build.json', emitDtsOnly: true }),
51+
addNodePrefix(),
52+
dts({
53+
tsconfig: './src/node/tsconfig.build.json',
54+
emitDtsOnly: true,
55+
resolve: true,
56+
}),
5257
],
5358
})
5459

@@ -80,6 +85,9 @@ const identifierReplacements: Record<string, Record<string, string>> = {
8085
Server$2: 'HttpsServer',
8186
ServerOptions$2: 'HttpsServerOptions',
8287
},
88+
'node:url': {
89+
URL$1: 'url_URL',
90+
},
8391
'vite/module-runner': {
8492
FetchResult$1: 'moduleRunner_FetchResult',
8593
},
@@ -100,6 +108,7 @@ const ignoreConfusingTypeNames = [
100108
'Plugin$1',
101109
'MinimalPluginContext$1',
102110
'ServerOptions$1',
111+
'ServerOptions$3',
103112
]
104113

105114
/**
@@ -412,3 +421,18 @@ function escapeRegex(str: string): string {
412421
function unique<T>(arr: T[]): T[] {
413422
return Array.from(new Set(arr))
414423
}
424+
425+
function addNodePrefix(): Plugin {
426+
return {
427+
name: 'add-node-prefix',
428+
resolveId: {
429+
order: 'pre',
430+
filter: {
431+
id: new RegExp(`^(?:${builtinModules.join('|')})$`),
432+
},
433+
handler(id) {
434+
return { id: `node:${id}`, external: true }
435+
},
436+
},
437+
}
438+
}

packages/vite/src/node/http.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export interface CommonServerOptions {
4848
/**
4949
* Configure custom proxy rules for the dev server. Expects an object
5050
* of `{ key: options }` pairs.
51-
* Uses [`http-proxy`](https://github.com/http-party/node-http-proxy).
52-
* Full options [here](https://github.com/http-party/node-http-proxy#options).
51+
* Uses [`http-proxy-3`](https://github.com/sagemathinc/http-proxy-3).
52+
* Full options [here](https://github.com/sagemathinc/http-proxy-3#options).
5353
*
5454
* Example `vite.config.js`:
5555
* ``` js

packages/vite/src/node/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export type {
251251
} from 'dep-types/alias'
252252
export type { Connect } from 'dep-types/connect'
253253
export type { WebSocket, WebSocketAlias } from 'dep-types/ws'
254-
export type { HttpProxy } from 'dep-types/http-proxy'
254+
export type * as HttpProxy from 'http-proxy-3'
255255
export type { FSWatcher, WatchOptions } from 'dep-types/chokidar'
256256
export type { Terser } from 'types/internal/terserOptions'
257257
export type { RollupCommonJSOptions } from 'dep-types/commonjs'

packages/vite/src/node/server/middlewares/proxy.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import type * as http from 'node:http'
2-
import type * as net from 'node:net'
3-
import httpProxy from 'http-proxy'
2+
import * as httpProxy from 'http-proxy-3'
43
import type { Connect } from 'dep-types/connect'
5-
import type { HttpProxy } from 'dep-types/http-proxy'
64
import colors from 'picocolors'
75
import { createDebugger } from '../../utils'
86
import type { CommonServerOptions, ResolvedConfig } from '../..'
97
import type { HttpServer } from '..'
108

119
const debug = createDebugger('vite:proxy')
1210

13-
export interface ProxyOptions extends HttpProxy.ServerOptions {
11+
export interface ProxyOptions extends httpProxy.ServerOptions {
1412
/**
1513
* rewrite path
1614
*/
1715
rewrite?: (path: string) => string
1816
/**
1917
* configure the proxy server (e.g. listen to events)
2018
*/
21-
configure?: (proxy: HttpProxy.Server, options: ProxyOptions) => void
19+
configure?: (proxy: httpProxy.ProxyServer, options: ProxyOptions) => void
2220
/**
2321
* webpack-dev-server style bypass function
2422
*/
@@ -66,7 +64,7 @@ const rewriteOriginHeader = (
6664
if (proxyReq.getHeader('origin') && target) {
6765
const changedOrigin =
6866
typeof target === 'object'
69-
? `${target.protocol}//${target.host}`
67+
? `${target.protocol ?? 'http:'}//${target.host}`
7068
: target
7169

7270
proxyReq.setHeader('origin', changedOrigin)
@@ -80,39 +78,27 @@ export function proxyMiddleware(
8078
config: ResolvedConfig,
8179
): Connect.NextHandleFunction {
8280
// lazy require only when proxy is used
83-
const proxies: Record<string, [HttpProxy.Server, ProxyOptions]> = {}
81+
const proxies: Record<string, [httpProxy.ProxyServer, ProxyOptions]> = {}
8482

8583
Object.keys(options).forEach((context) => {
8684
let opts = options[context]
8785
if (!opts) {
8886
return
8987
}
9088
if (typeof opts === 'string') {
91-
opts = { target: opts, changeOrigin: true } as ProxyOptions
89+
opts = { target: opts, changeOrigin: true }
9290
}
93-
const proxy = httpProxy.createProxyServer(opts) as HttpProxy.Server
91+
const proxy = httpProxy.createProxyServer(opts)
9492

9593
if (opts.configure) {
9694
opts.configure(proxy, opts)
9795
}
9896

99-
proxy.on('error', (err, _req, originalRes) => {
97+
proxy.on('error', (err, _req, res) => {
10098
// When it is ws proxy, res is net.Socket
101-
// originalRes can be falsy if the proxy itself errored
102-
const res = originalRes as http.ServerResponse | net.Socket | undefined
103-
if (!res) {
99+
if ('req' in res) {
104100
config.logger.error(
105-
`${colors.red(`http proxy error: ${err.message}`)}\n${err.stack}`,
106-
{
107-
timestamp: true,
108-
error: err,
109-
},
110-
)
111-
} else if ('req' in res) {
112-
config.logger.error(
113-
`${colors.red(`http proxy error: ${originalRes.req.url}`)}\n${
114-
err.stack
115-
}`,
101+
`${colors.red(`http proxy error: ${res.req.url}`)}\n${err.stack}`,
116102
{
117103
timestamp: true,
118104
error: err,
@@ -148,17 +134,6 @@ export function proxyMiddleware(
148134
})
149135
})
150136

151-
// https://github.com/http-party/node-http-proxy/issues/1520#issue-877626125
152-
// https://github.com/chimurai/http-proxy-middleware/blob/cd58f962aec22c925b7df5140502978da8f87d5f/src/plugins/default/debug-proxy-errors-plugin.ts#L25-L37
153-
proxy.on('proxyRes', (proxyRes, _req, res) => {
154-
res.on('close', () => {
155-
if (!res.writableEnded) {
156-
debug?.('destroying proxyRes in proxyRes close event')
157-
proxyRes.destroy()
158-
}
159-
})
160-
})
161-
162137
// clone before saving because http-proxy mutates the options
163138
proxies[context] = [proxy, { ...opts }]
164139
})
@@ -217,7 +192,7 @@ export function proxyMiddleware(
217192
for (const context in proxies) {
218193
if (doesProxyContextMatchUrl(context, url)) {
219194
const [proxy, opts] = proxies[context]
220-
const options: HttpProxy.ServerOptions = {}
195+
const options: httpProxy.ServerOptions = {}
221196

222197
if (opts.bypass) {
223198
try {

0 commit comments

Comments
 (0)