|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const os = require("os"); |
| 4 | +const net = require("net"); |
| 5 | +const path = require("path"); |
| 6 | +const http = require("http"); |
| 7 | +const webpack = require("webpack"); |
| 8 | +const { test } = require("@playwright/test"); |
| 9 | +const { expect } = require("@playwright/test"); |
| 10 | +const { describe } = require("@playwright/test"); |
| 11 | +const httpProxy = require("http-proxy"); |
| 12 | +const Server = require("../../lib/Server"); |
| 13 | +const config = require("../fixtures/client-config/webpack.config"); |
| 14 | +const sessionSubscribe = require("../helpers/session-subscribe"); |
| 15 | +const port1 = require("../ports-map").ipc; |
| 16 | + |
| 17 | +const webSocketServers = ["ws", "sockjs"]; |
| 18 | + |
| 19 | +describe("web socket server URL", () => { |
| 20 | + for (const webSocketServer of webSocketServers) { |
| 21 | + const websocketURLProtocol = webSocketServer === "ws" ? "ws" : "http"; |
| 22 | + |
| 23 | + test(`should work with the "ipc" option using "true" value ("${webSocketServer}")`, async ({ |
| 24 | + page, |
| 25 | + }) => { |
| 26 | + const devServerHost = "127.0.0.1"; |
| 27 | + const proxyHost = devServerHost; |
| 28 | + const proxyPort = port1; |
| 29 | + |
| 30 | + const compiler = webpack(config); |
| 31 | + const devServerOptions = { |
| 32 | + webSocketServer, |
| 33 | + ipc: true, |
| 34 | + }; |
| 35 | + const server = new Server(devServerOptions, compiler); |
| 36 | + |
| 37 | + await server.start(); |
| 38 | + |
| 39 | + function startProxy(callback) { |
| 40 | + const proxy = httpProxy.createProxyServer({ |
| 41 | + target: { socketPath: server.options.ipc }, |
| 42 | + }); |
| 43 | + |
| 44 | + const proxyServer = http.createServer((request, response) => { |
| 45 | + // You can define here your custom logic to handle the request |
| 46 | + // and then proxy the request. |
| 47 | + proxy.web(request, response); |
| 48 | + }); |
| 49 | + |
| 50 | + proxyServer.on("upgrade", (request, socket, head) => { |
| 51 | + proxy.ws(request, socket, head); |
| 52 | + }); |
| 53 | + |
| 54 | + return proxyServer.listen(proxyPort, proxyHost, callback); |
| 55 | + } |
| 56 | + |
| 57 | + const proxy = await new Promise((resolve) => { |
| 58 | + const proxyCreated = startProxy(() => { |
| 59 | + resolve(proxyCreated); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + try { |
| 64 | + const pageErrors = []; |
| 65 | + const consoleMessages = []; |
| 66 | + |
| 67 | + page |
| 68 | + .on("console", (message) => { |
| 69 | + consoleMessages.push(message); |
| 70 | + }) |
| 71 | + .on("pageerror", (error) => { |
| 72 | + pageErrors.push(error); |
| 73 | + }); |
| 74 | + |
| 75 | + const webSocketRequests = []; |
| 76 | + |
| 77 | + if (webSocketServer === "ws") { |
| 78 | + const session = await page.context().newCDPSession(page); |
| 79 | + |
| 80 | + session.on("Network.webSocketCreated", (payload) => { |
| 81 | + webSocketRequests.push(payload); |
| 82 | + }); |
| 83 | + |
| 84 | + await session.send("Target.setAutoAttach", { |
| 85 | + autoAttach: true, |
| 86 | + flatten: true, |
| 87 | + waitForDebuggerOnStart: true, |
| 88 | + }); |
| 89 | + |
| 90 | + sessionSubscribe(session); |
| 91 | + } else { |
| 92 | + page.on("request", (request) => { |
| 93 | + if (/\/ws\//.test(request.url())) { |
| 94 | + webSocketRequests.push({ url: request.url() }); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + await page.goto(`http://${proxyHost}:${proxyPort}/`, { |
| 100 | + waitUntil: "networkidle0", |
| 101 | + }); |
| 102 | + |
| 103 | + const webSocketRequest = webSocketRequests[0]; |
| 104 | + |
| 105 | + expect(webSocketRequest.url).toContain( |
| 106 | + `${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`, |
| 107 | + ); |
| 108 | + expect( |
| 109 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 110 | + ).toMatchSnapshot(); |
| 111 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 112 | + } catch (error) { |
| 113 | + throw error; |
| 114 | + } finally { |
| 115 | + proxy.close(); |
| 116 | + |
| 117 | + await server.stop(); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + test(`should work with the "ipc" option using "string" value ("${webSocketServer}")`, async ({ |
| 122 | + page, |
| 123 | + }) => { |
| 124 | + const isWindows = process.platform === "win32"; |
| 125 | + const pipePrefix = isWindows ? "\\\\.\\pipe\\" : os.tmpdir(); |
| 126 | + const pipeName = `webpack-dev-server.${process.pid}-1.sock`; |
| 127 | + const ipc = path.join(pipePrefix, pipeName); |
| 128 | + |
| 129 | + const devServerHost = "127.0.0.1"; |
| 130 | + const proxyHost = devServerHost; |
| 131 | + const proxyPort = port1; |
| 132 | + |
| 133 | + const compiler = webpack(config); |
| 134 | + const devServerOptions = { |
| 135 | + webSocketServer, |
| 136 | + ipc, |
| 137 | + }; |
| 138 | + const server = new Server(devServerOptions, compiler); |
| 139 | + |
| 140 | + await server.start(); |
| 141 | + |
| 142 | + function startProxy(callback) { |
| 143 | + const proxy = httpProxy.createProxyServer({ |
| 144 | + target: { socketPath: ipc }, |
| 145 | + }); |
| 146 | + |
| 147 | + const proxyServer = http.createServer((request, response) => { |
| 148 | + // You can define here your custom logic to handle the request |
| 149 | + // and then proxy the request. |
| 150 | + proxy.web(request, response); |
| 151 | + }); |
| 152 | + |
| 153 | + proxyServer.on("upgrade", (request, socket, head) => { |
| 154 | + proxy.ws(request, socket, head); |
| 155 | + }); |
| 156 | + |
| 157 | + return proxyServer.listen(proxyPort, proxyHost, callback); |
| 158 | + } |
| 159 | + |
| 160 | + const proxy = await new Promise((resolve) => { |
| 161 | + const proxyCreated = startProxy(() => { |
| 162 | + resolve(proxyCreated); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + try { |
| 167 | + const pageErrors = []; |
| 168 | + const consoleMessages = []; |
| 169 | + |
| 170 | + page |
| 171 | + .on("console", (message) => { |
| 172 | + consoleMessages.push(message); |
| 173 | + }) |
| 174 | + .on("pageerror", (error) => { |
| 175 | + pageErrors.push(error); |
| 176 | + }); |
| 177 | + |
| 178 | + const webSocketRequests = []; |
| 179 | + |
| 180 | + if (webSocketServer === "ws") { |
| 181 | + const session = await page.context().newCDPSession(page); |
| 182 | + |
| 183 | + session.on("Network.webSocketCreated", (payload) => { |
| 184 | + webSocketRequests.push(payload); |
| 185 | + }); |
| 186 | + |
| 187 | + await session.send("Target.setAutoAttach", { |
| 188 | + autoAttach: true, |
| 189 | + flatten: true, |
| 190 | + waitForDebuggerOnStart: true, |
| 191 | + }); |
| 192 | + |
| 193 | + sessionSubscribe(session); |
| 194 | + } else { |
| 195 | + page.on("request", (request) => { |
| 196 | + if (/\/ws\//.test(request.url())) { |
| 197 | + webSocketRequests.push({ url: request.url() }); |
| 198 | + } |
| 199 | + }); |
| 200 | + } |
| 201 | + |
| 202 | + await page.goto(`http://${proxyHost}:${proxyPort}/`, { |
| 203 | + waitUntil: "networkidle0", |
| 204 | + }); |
| 205 | + |
| 206 | + const webSocketRequest = webSocketRequests[0]; |
| 207 | + |
| 208 | + expect(webSocketRequest.url).toContain( |
| 209 | + `${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`, |
| 210 | + ); |
| 211 | + expect( |
| 212 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 213 | + ).toMatchSnapshot(); |
| 214 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 215 | + } catch (error) { |
| 216 | + throw error; |
| 217 | + } finally { |
| 218 | + proxy.close(); |
| 219 | + |
| 220 | + await server.stop(); |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + // TODO un skip after implement new API |
| 225 | + test.skip(`should work with the "ipc" option using "string" value and remove old ("${webSocketServer}")`, async ({ |
| 226 | + page, |
| 227 | + }) => { |
| 228 | + const isWindows = process.platform === "win32"; |
| 229 | + const localRelative = path.relative(process.cwd(), `${os.tmpdir()}/`); |
| 230 | + const pipePrefix = isWindows ? "\\\\.\\pipe\\" : localRelative; |
| 231 | + const pipeName = `webpack-dev-server.${process.pid}-2.sock`; |
| 232 | + const ipc = path.join(pipePrefix, pipeName); |
| 233 | + |
| 234 | + const ipcServer = await new Promise((resolve, reject) => { |
| 235 | + const server = net.Server(); |
| 236 | + |
| 237 | + server.on("error", (error) => { |
| 238 | + reject(error); |
| 239 | + }); |
| 240 | + |
| 241 | + return server.listen(ipc, () => { |
| 242 | + resolve(); |
| 243 | + }); |
| 244 | + }); |
| 245 | + |
| 246 | + const devServerHost = "127.0.0.1"; |
| 247 | + const proxyHost = devServerHost; |
| 248 | + const proxyPort = port1; |
| 249 | + |
| 250 | + const compiler = webpack(config); |
| 251 | + const devServerOptions = { |
| 252 | + webSocketServer, |
| 253 | + host: devServerHost, |
| 254 | + ipc, |
| 255 | + }; |
| 256 | + const server = new Server(devServerOptions, compiler); |
| 257 | + |
| 258 | + await server.start(); |
| 259 | + |
| 260 | + function startProxy(callback) { |
| 261 | + const proxy = httpProxy.createProxyServer({ |
| 262 | + target: { socketPath: ipc }, |
| 263 | + }); |
| 264 | + |
| 265 | + const proxyServer = http.createServer((request, response) => { |
| 266 | + // You can define here your custom logic to handle the request |
| 267 | + // and then proxy the request. |
| 268 | + proxy.web(request, response); |
| 269 | + }); |
| 270 | + |
| 271 | + proxyServer.on("upgrade", (request, socket, head) => { |
| 272 | + proxy.ws(request, socket, head); |
| 273 | + }); |
| 274 | + |
| 275 | + return proxyServer.listen(proxyPort, proxyHost, callback); |
| 276 | + } |
| 277 | + |
| 278 | + const proxy = await new Promise((resolve) => { |
| 279 | + const proxyCreated = startProxy(() => { |
| 280 | + resolve(proxyCreated); |
| 281 | + }); |
| 282 | + }); |
| 283 | + |
| 284 | + try { |
| 285 | + const pageErrors = []; |
| 286 | + const consoleMessages = []; |
| 287 | + |
| 288 | + page |
| 289 | + .on("console", (message) => { |
| 290 | + consoleMessages.push(message); |
| 291 | + }) |
| 292 | + .on("pageerror", (error) => { |
| 293 | + pageErrors.push(error); |
| 294 | + }); |
| 295 | + |
| 296 | + const webSocketRequests = []; |
| 297 | + |
| 298 | + if (webSocketServer === "ws") { |
| 299 | + const session = await page.target().createCDPSession(); |
| 300 | + |
| 301 | + session.on("Network.webSocketCreated", (payload) => { |
| 302 | + webSocketRequests.push(payload); |
| 303 | + }); |
| 304 | + |
| 305 | + await session.send("Target.setAutoAttach", { |
| 306 | + autoAttach: true, |
| 307 | + flatten: true, |
| 308 | + waitForDebuggerOnStart: true, |
| 309 | + }); |
| 310 | + |
| 311 | + sessionSubscribe(session); |
| 312 | + } else { |
| 313 | + page.on("request", (request) => { |
| 314 | + if (/\/ws\//.test(request.url())) { |
| 315 | + webSocketRequests.push({ url: request.url() }); |
| 316 | + } |
| 317 | + }); |
| 318 | + } |
| 319 | + |
| 320 | + await page.goto(`http://${proxyHost}:${proxyPort}/`, { |
| 321 | + waitUntil: "networkidle0", |
| 322 | + }); |
| 323 | + |
| 324 | + const webSocketRequest = webSocketRequests[0]; |
| 325 | + |
| 326 | + expect(webSocketRequest.url).toContain( |
| 327 | + `${websocketURLProtocol}://${devServerHost}:${proxyPort}/ws`, |
| 328 | + ); |
| 329 | + expect( |
| 330 | + JSON.stringify(consoleMessages.map((message) => message.text())), |
| 331 | + ).toMatchSnapshot(); |
| 332 | + expect(JSON.stringify(pageErrors)).toMatchSnapshot(); |
| 333 | + } catch (error) { |
| 334 | + throw error; |
| 335 | + } finally { |
| 336 | + proxy.close(); |
| 337 | + |
| 338 | + await new Promise((resolve, reject) => { |
| 339 | + ipcServer.close((error) => { |
| 340 | + if (error) { |
| 341 | + reject(error); |
| 342 | + |
| 343 | + return; |
| 344 | + } |
| 345 | + |
| 346 | + resolve(); |
| 347 | + }); |
| 348 | + }); |
| 349 | + await server.stop(); |
| 350 | + } |
| 351 | + }); |
| 352 | + } |
| 353 | +}); |
0 commit comments