Skip to content

Commit

Permalink
Merge branch 'master' into update-ws
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Aug 16, 2021
2 parents 17d6760 + 2adfd01 commit 986a76a
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 35 deletions.
77 changes: 45 additions & 32 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,12 @@ class Server {
* }
*/
if (typeof options.proxy !== "undefined") {
// TODO remove in the next major release, only accept `Array`
if (!Array.isArray(options.proxy)) {
if (Object.prototype.hasOwnProperty.call(options.proxy, "target")) {
if (
Object.prototype.hasOwnProperty.call(options.proxy, "target") ||
Object.prototype.hasOwnProperty.call(options.proxy, "router")
) {
options.proxy = [options.proxy];
} else {
options.proxy = Object.keys(options.proxy).map((context) => {
Expand All @@ -513,38 +517,42 @@ class Server {
proxyOptions.context = correctedContext;
}

const getLogLevelForProxy = (level) => {
if (level === "none") {
return "silent";
}
return proxyOptions;
});
}
}

if (level === "log") {
return "info";
}
options.proxy = options.proxy.map((item) => {
const getLogLevelForProxy = (level) => {
if (level === "none") {
return "silent";
}

if (level === "verbose") {
return "debug";
}
if (level === "log") {
return "info";
}

return level;
};
if (level === "verbose") {
return "debug";
}

if (typeof proxyOptions.logLevel === "undefined") {
proxyOptions.logLevel = getLogLevelForProxy(
compilerOptions.infrastructureLogging
? compilerOptions.infrastructureLogging.level
: "info"
);
}
return level;
};

if (typeof proxyOptions.logProvider === "undefined") {
proxyOptions.logProvider = () => this.logger;
}
if (typeof item.logLevel === "undefined") {
item.logLevel = getLogLevelForProxy(
compilerOptions.infrastructureLogging
? compilerOptions.infrastructureLogging.level
: "info"
);
}

return proxyOptions;
});
if (typeof item.logProvider === "undefined") {
item.logProvider = () => this.logger;
}
}

return item;
});
}

if (typeof options.setupExitSignals === "undefined") {
Expand Down Expand Up @@ -875,22 +883,24 @@ class Server {

// It is possible to use the `bypass` method without a `target`.
// However, the proxy middleware has no use in this case, and will fail to instantiate.
if (proxyConfig.target) {
if (context) {
return createProxyMiddleware(context, proxyConfig);
}

return createProxyMiddleware(proxyConfig);
};
/**
* Assume a proxy configuration specified as:
* proxy: [
* {
* context: ...,
* ...options...
* context: "value",
* ...options,
* },
* // or:
* function() {
* return {
* context: ...,
* ...options...
* context: "context",
* ...options,
* };
* }
* ]
Expand All @@ -903,7 +913,9 @@ class Server {
? proxyConfigOrCallback()
: proxyConfigOrCallback;

proxyMiddleware = getProxyMiddleware(proxyConfig);
if (!proxyConfig.bypass) {
proxyMiddleware = getProxyMiddleware(proxyConfig);
}

if (proxyConfig.ws) {
this.webSocketProxies.push(proxyMiddleware);
Expand All @@ -922,6 +934,7 @@ class Server {
// - Check if we have a bypass function defined
// - In case the bypass function is defined we'll retrieve the
// bypassUrl from it otherwise bypassUrl would be null
// TODO remove in the next major in favor `context` and `router` options
const isByPassFuncDefined = typeof proxyConfig.bypass === "function";
const bypassUrl = isByPassFuncDefined
? await proxyConfig.bypass(req, res, proxyConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@ Array [

exports[`web socket communication should work and close web socket client connection when web socket server closed ("ws"): page errors 1`] = `Array []`;

exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App hot update...",
"[HMR] Checking for updates on the server...",
"Failed to load resource: the server responded with a status of 404 (Not Found)",
"[HMR] Cannot find update. Need to do a full reload!",
"[HMR] (Probably because of restarting the webpack-dev-server)",
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket communication should work and reconnect when the connection is lost ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App hot update...",
"[HMR] Checking for updates on the server...",
"Failed to load resource: the server responded with a status of 404 (Not Found)",
"[HMR] Cannot find update. Need to do a full reload!",
"[HMR] (Probably because of restarting the webpack-dev-server)",
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket communication should work and reconnect when the connection is lost ("ws"): page errors 1`] = `Array []`;

exports[`web socket communication should work and terminate client that is not alive ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@ Array [

exports[`web socket communication should work and close web socket client connection when web socket server closed ("ws"): page errors 1`] = `Array []`;

exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App hot update...",
"[HMR] Checking for updates on the server...",
"Failed to load resource: the server responded with a status of 404 (Not Found)",
"[HMR] Cannot find update. Need to do a full reload!",
"[HMR] (Probably because of restarting the webpack-dev-server)",
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket communication should work and reconnect when the connection is lost ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] Disconnected!",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
"[webpack-dev-server] App hot update...",
"[HMR] Checking for updates on the server...",
"Failed to load resource: the server responded with a status of 404 (Not Found)",
"[HMR] Cannot find update. Need to do a full reload!",
"[HMR] (Probably because of restarting the webpack-dev-server)",
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket communication should work and reconnect when the connection is lost ("ws"): page errors 1`] = `Array []`;

exports[`web socket communication should work and terminate client that is not alive ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/web-socket-communication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ describe("web socket communication", () => {
await server.stop();
});

// TODO uncomment after fix regression in webpack
it.skip(`should work and reconnect when the connection is lost ("${websocketServer}")`, async () => {
it(`should work and reconnect when the connection is lost ("${websocketServer}")`, async () => {
WebsocketServer.heartbeatInterval = 100;

const compiler = webpack(config);
Expand Down
Loading

0 comments on commit 986a76a

Please sign in to comment.