Skip to content

Commit 905df83

Browse files
Merge branch 'master' into url-opts
2 parents e29b6b7 + b2202b6 commit 905df83

17 files changed

+2300
-3093
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [4.7.3](https://github.com/webpack/webpack-dev-server/compare/v4.7.2...v4.7.3) (2022-01-11)
6+
7+
### Security
8+
9+
* update `selfsigned` to `2.0.0` version
10+
511
### [4.7.2](https://github.com/webpack/webpack-dev-server/compare/v4.7.1...v4.7.2) (2021-12-29)
612

713

lib/Server.js

Lines changed: 121 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,22 @@ const schema = require("./options.json");
110110
*/
111111

112112
/**
113-
* @typedef {{ [url: string]: string | HttpProxyMiddlewareOptions }} ProxyConfigMap
113+
* @callback ByPass
114+
* @param {Request} req
115+
* @param {Response} res
116+
* @param {ProxyConfigArrayItem} proxyConfig
114117
*/
115118

116119
/**
117-
* @typedef {HttpProxyMiddlewareOptions[]} ProxyArray
120+
* @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & { bypass?: ByPass } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
118121
*/
119122

120123
/**
121-
* @callback ByPass
122-
* @param {Request} req
123-
* @param {Response} res
124-
* @param {ProxyConfigArray} proxyConfig
124+
* @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[]} ProxyConfigArray
125125
*/
126126

127127
/**
128-
* @typedef {{ path?: string | string[] | undefined, context?: string | string[] | HttpProxyMiddlewareOptionsFilter | undefined } & HttpProxyMiddlewareOptions & ByPass} ProxyConfigArray
128+
* @typedef {{ [url: string]: string | ProxyConfigArrayItem }} ProxyConfigMap
129129
*/
130130

131131
/**
@@ -194,7 +194,7 @@ const schema = require("./options.json");
194194
* @property {boolean} [http2]
195195
* @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
196196
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
197-
* @property {ProxyConfigMap | ProxyConfigArray | ProxyArray} [proxy]
197+
* @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
198198
* @property {boolean | string | Open | Array<string | Open>} [open]
199199
* @property {boolean} [setupExitSignals]
200200
* @property {boolean | ClientConfiguration} [client]
@@ -1380,10 +1380,10 @@ class Server {
13801380
Object.prototype.hasOwnProperty.call(options.proxy, "target") ||
13811381
Object.prototype.hasOwnProperty.call(options.proxy, "router")
13821382
) {
1383-
/** @type {ProxyArray} */
1383+
/** @type {ProxyConfigArray} */
13841384
(options.proxy) = [/** @type {ProxyConfigMap} */ (options.proxy)];
13851385
} else {
1386-
/** @type {ProxyArray} */
1386+
/** @type {ProxyConfigArray} */
13871387
(options.proxy) = Object.keys(options.proxy).map(
13881388
/**
13891389
* @param {string} context
@@ -1421,50 +1421,48 @@ class Server {
14211421
}
14221422
}
14231423

1424-
/** @type {ProxyArray} */
1424+
/** @type {ProxyConfigArray} */
14251425
(options.proxy) =
1426-
/** @type {ProxyArray} */
1427-
(options.proxy).map(
1426+
/** @type {ProxyConfigArray} */
1427+
(options.proxy).map((item) => {
1428+
if (typeof item === "function") {
1429+
return item;
1430+
}
1431+
14281432
/**
1429-
* @param {HttpProxyMiddlewareOptions} item
1430-
* @returns {HttpProxyMiddlewareOptions}
1433+
* @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
1434+
* @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
14311435
*/
1432-
(item) => {
1433-
/**
1434-
* @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
1435-
* @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
1436-
*/
1437-
const getLogLevelForProxy = (level) => {
1438-
if (level === "none") {
1439-
return "silent";
1440-
}
1441-
1442-
if (level === "log") {
1443-
return "info";
1444-
}
1445-
1446-
if (level === "verbose") {
1447-
return "debug";
1448-
}
1449-
1450-
return level;
1451-
};
1436+
const getLogLevelForProxy = (level) => {
1437+
if (level === "none") {
1438+
return "silent";
1439+
}
14521440

1453-
if (typeof item.logLevel === "undefined") {
1454-
item.logLevel = getLogLevelForProxy(
1455-
compilerOptions.infrastructureLogging
1456-
? compilerOptions.infrastructureLogging.level
1457-
: "info"
1458-
);
1441+
if (level === "log") {
1442+
return "info";
14591443
}
14601444

1461-
if (typeof item.logProvider === "undefined") {
1462-
item.logProvider = () => this.logger;
1445+
if (level === "verbose") {
1446+
return "debug";
14631447
}
14641448

1465-
return item;
1449+
return level;
1450+
};
1451+
1452+
if (typeof item.logLevel === "undefined") {
1453+
item.logLevel = getLogLevelForProxy(
1454+
compilerOptions.infrastructureLogging
1455+
? compilerOptions.infrastructureLogging.level
1456+
: "info"
1457+
);
14661458
}
1467-
);
1459+
1460+
if (typeof item.logProvider === "undefined") {
1461+
item.logProvider = () => this.logger;
1462+
}
1463+
1464+
return item;
1465+
});
14681466
}
14691467

14701468
if (typeof options.setupExitSignals === "undefined") {
@@ -1478,13 +1476,7 @@ class Server {
14781476
} else if (typeof options.static === "string") {
14791477
options.static = [getStaticItem(options.static)];
14801478
} else if (Array.isArray(options.static)) {
1481-
options.static = options.static.map((item) => {
1482-
if (typeof item === "string") {
1483-
return getStaticItem(item);
1484-
}
1485-
1486-
return getStaticItem(item);
1487-
});
1479+
options.static = options.static.map((item) => getStaticItem(item));
14881480
} else {
14891481
options.static = [getStaticItem(options.static)];
14901482
}
@@ -2130,7 +2122,7 @@ class Server {
21302122
const { createProxyMiddleware } = require("http-proxy-middleware");
21312123

21322124
/**
2133-
* @param {ProxyConfigArray} proxyConfig
2125+
* @param {ProxyConfigArrayItem} proxyConfig
21342126
* @returns {RequestHandler | undefined}
21352127
*/
21362128
const getProxyMiddleware = (proxyConfig) => {
@@ -2166,93 +2158,91 @@ class Server {
21662158
* }
21672159
* ]
21682160
*/
2169-
/** @type {ProxyArray} */
2170-
(this.options.proxy).forEach(
2161+
/** @type {ProxyConfigArray} */
2162+
(this.options.proxy).forEach((proxyConfigOrCallback) => {
21712163
/**
2172-
* @param {any} proxyConfigOrCallback
2164+
* @type {RequestHandler}
21732165
*/
2174-
(proxyConfigOrCallback) => {
2175-
/**
2176-
* @type {RequestHandler}
2177-
*/
2178-
let proxyMiddleware;
2166+
let proxyMiddleware;
21792167

2180-
let proxyConfig =
2181-
typeof proxyConfigOrCallback === "function"
2182-
? proxyConfigOrCallback()
2183-
: proxyConfigOrCallback;
2168+
let proxyConfig =
2169+
typeof proxyConfigOrCallback === "function"
2170+
? proxyConfigOrCallback()
2171+
: proxyConfigOrCallback;
21842172

2185-
proxyMiddleware =
2186-
/** @type {RequestHandler} */
2187-
(getProxyMiddleware(proxyConfig));
2173+
proxyMiddleware =
2174+
/** @type {RequestHandler} */
2175+
(getProxyMiddleware(proxyConfig));
21882176

2189-
if (proxyConfig.ws) {
2190-
this.webSocketProxies.push(proxyMiddleware);
2191-
}
2177+
if (proxyConfig.ws) {
2178+
this.webSocketProxies.push(proxyMiddleware);
2179+
}
21922180

2193-
/**
2194-
* @param {Request} req
2195-
* @param {Response} res
2196-
* @param {NextFunction} next
2197-
* @returns {Promise<void>}
2198-
*/
2199-
const handler = async (req, res, next) => {
2200-
if (typeof proxyConfigOrCallback === "function") {
2201-
const newProxyConfig = proxyConfigOrCallback(req, res, next);
2202-
2203-
if (newProxyConfig !== proxyConfig) {
2204-
proxyConfig = newProxyConfig;
2205-
proxyMiddleware =
2206-
/** @type {RequestHandler} */
2207-
(getProxyMiddleware(proxyConfig));
2208-
}
2181+
/**
2182+
* @param {Request} req
2183+
* @param {Response} res
2184+
* @param {NextFunction} next
2185+
* @returns {Promise<void>}
2186+
*/
2187+
const handler = async (req, res, next) => {
2188+
if (typeof proxyConfigOrCallback === "function") {
2189+
const newProxyConfig = proxyConfigOrCallback(req, res, next);
2190+
2191+
if (newProxyConfig !== proxyConfig) {
2192+
proxyConfig = newProxyConfig;
2193+
proxyMiddleware =
2194+
/** @type {RequestHandler} */
2195+
(getProxyMiddleware(proxyConfig));
22092196
}
2197+
}
22102198

2211-
// - Check if we have a bypass function defined
2212-
// - In case the bypass function is defined we'll retrieve the
2213-
// bypassUrl from it otherwise bypassUrl would be null
2214-
// TODO remove in the next major in favor `context` and `router` options
2215-
const isByPassFuncDefined =
2216-
typeof proxyConfig.bypass === "function";
2217-
const bypassUrl = isByPassFuncDefined
2218-
? await proxyConfig.bypass(req, res, proxyConfig)
2219-
: null;
2220-
2221-
if (typeof bypassUrl === "boolean") {
2222-
// skip the proxy
2223-
// @ts-ignore
2224-
req.url = null;
2225-
next();
2226-
} else if (typeof bypassUrl === "string") {
2227-
// byPass to that url
2228-
req.url = bypassUrl;
2229-
next();
2230-
} else if (proxyMiddleware) {
2231-
return proxyMiddleware(req, res, next);
2232-
} else {
2233-
next();
2234-
}
2235-
};
2199+
// - Check if we have a bypass function defined
2200+
// - In case the bypass function is defined we'll retrieve the
2201+
// bypassUrl from it otherwise bypassUrl would be null
2202+
// TODO remove in the next major in favor `context` and `router` options
2203+
const isByPassFuncDefined = typeof proxyConfig.bypass === "function";
2204+
const bypassUrl = isByPassFuncDefined
2205+
? await /** @type {ByPass} */ (proxyConfig.bypass)(
2206+
req,
2207+
res,
2208+
proxyConfig
2209+
)
2210+
: null;
2211+
2212+
if (typeof bypassUrl === "boolean") {
2213+
// skip the proxy
2214+
// @ts-ignore
2215+
req.url = null;
2216+
next();
2217+
} else if (typeof bypassUrl === "string") {
2218+
// byPass to that url
2219+
req.url = bypassUrl;
2220+
next();
2221+
} else if (proxyMiddleware) {
2222+
return proxyMiddleware(req, res, next);
2223+
} else {
2224+
next();
2225+
}
2226+
};
22362227

2237-
middlewares.push({
2238-
name: "http-proxy-middleware",
2239-
middleware: handler,
2240-
});
2241-
// Also forward error requests to the proxy so it can handle them.
2242-
middlewares.push({
2243-
name: "http-proxy-middleware-error-handler",
2244-
middleware:
2245-
/**
2246-
* @param {Error} error
2247-
* @param {Request} req
2248-
* @param {Response} res
2249-
* @param {NextFunction} next
2250-
* @returns {any}
2251-
*/
2252-
(error, req, res, next) => handler(req, res, next),
2253-
});
2254-
}
2255-
);
2228+
middlewares.push({
2229+
name: "http-proxy-middleware",
2230+
middleware: handler,
2231+
});
2232+
// Also forward error requests to the proxy so it can handle them.
2233+
middlewares.push({
2234+
name: "http-proxy-middleware-error-handler",
2235+
middleware:
2236+
/**
2237+
* @param {Error} error
2238+
* @param {Request} req
2239+
* @param {Response} res
2240+
* @param {NextFunction} next
2241+
* @returns {any}
2242+
*/
2243+
(error, req, res, next) => handler(req, res, next),
2244+
});
2245+
});
22562246

22572247
middlewares.push({
22582248
name: "webpack-dev-middleware",

0 commit comments

Comments
 (0)