From 24c50ce14afa786fa26fc56aa49d24badc2d8913 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Tue, 21 Apr 2020 20:11:57 -0500 Subject: [PATCH 01/10] chore(ws): switch default transportMode to ws --- lib/utils/getSocketServerImplementation.js | 2 +- lib/utils/normalizeOptions.js | 8 +++--- test/server/transportMode-option.test.js | 18 ++++++------- .../normalizeOptions.test.js.snap | 26 +++++++++---------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/utils/getSocketServerImplementation.js b/lib/utils/getSocketServerImplementation.js index 4ff26ff042..36613b988f 100644 --- a/lib/utils/getSocketServerImplementation.js +++ b/lib/utils/getSocketServerImplementation.js @@ -30,7 +30,7 @@ function getSocketServerImplementation(options) { if (!serverImplFound) { throw new Error( - "transportMode.server must be a string denoting a default implementation (e.g. 'sockjs', 'ws'), a full path to " + + "transportMode.server must be a string denoting a default implementation (e.g. 'ws', 'sockjs'), a full path to " + 'a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer) ' + 'via require.resolve(...), or the class itself which extends BaseServer' ); diff --git a/lib/utils/normalizeOptions.js b/lib/utils/normalizeOptions.js index 960d491416..0bf51ca3c2 100644 --- a/lib/utils/normalizeOptions.js +++ b/lib/utils/normalizeOptions.js @@ -15,8 +15,8 @@ function normalizeOptions(compiler, options) { // normalize transportMode option if (options.transportMode === undefined) { options.transportMode = { - server: 'sockjs', - client: 'sockjs', + server: 'ws', + client: 'ws', }; } else { switch (typeof options.transportMode) { @@ -28,8 +28,8 @@ function normalizeOptions(compiler, options) { break; // if not a string, it is an object default: - options.transportMode.server = options.transportMode.server || 'sockjs'; - options.transportMode.client = options.transportMode.client || 'sockjs'; + options.transportMode.server = options.transportMode.server || 'ws'; + options.transportMode.client = options.transportMode.client || 'ws'; } } diff --git a/test/server/transportMode-option.test.js b/test/server/transportMode-option.test.js index f3be738a2b..def5a7105b 100644 --- a/test/server/transportMode-option.test.js +++ b/test/server/transportMode-option.test.js @@ -439,11 +439,11 @@ describe('transportMode', () => { }); describe('server', () => { - let MockSockJSServer; + let MockWebsocketServer; beforeEach((done) => { - jest.mock('../../lib/servers/SockJSServer'); + jest.mock('../../lib/servers/WebsocketServer'); mockedTestServer = require('../helpers/test-server'); - MockSockJSServer = require('../../lib/servers/SockJSServer'); + MockWebsocketServer = require('../../lib/servers/WebsocketServer'); server = mockedTestServer.start( config, @@ -463,7 +463,7 @@ describe('transportMode', () => { }); it('should use server implementation correctly', () => { - const mockServerInstance = MockSockJSServer.mock.instances[0]; + const mockServerInstance = MockWebsocketServer.mock.instances[0]; const connectionObj = { foo: 'bar', @@ -483,8 +483,8 @@ describe('transportMode', () => { expect(server.sockets.length).toEqual(0); // check that the dev server was passed to the socket server implementation constructor - expect(MockSockJSServer.mock.calls[0].length).toEqual(1); - expect(MockSockJSServer.mock.calls[0][0].options.port).toEqual(port); + expect(MockWebsocketServer.mock.calls[0].length).toEqual(1); + expect(MockWebsocketServer.mock.calls[0][0].options.port).toEqual(port); expect(mockServerInstance.onConnection.mock.calls).toMatchSnapshot(); expect(mockServerInstance.send.mock.calls.length).toEqual(4); @@ -504,7 +504,7 @@ describe('transportMode', () => { }); it('should close client with bad headers', () => { - const mockServerInstance = MockSockJSServer.mock.instances[0]; + const mockServerInstance = MockWebsocketServer.mock.instances[0]; // this simulates a client connecting to the server mockServerInstance.onConnection.mock.calls[0][0]( @@ -516,8 +516,8 @@ describe('transportMode', () => { } ); expect(server.sockets.length).toEqual(0); - expect(MockSockJSServer.mock.calls[0].length).toEqual(1); - expect(MockSockJSServer.mock.calls[0][0].options.port).toEqual(port); + expect(MockWebsocketServer.mock.calls[0].length).toEqual(1); + expect(MockWebsocketServer.mock.calls[0][0].options.port).toEqual(port); expect(mockServerInstance.onConnection.mock.calls).toMatchSnapshot(); // the only call to send() here should be an invalid header message expect(mockServerInstance.send.mock.calls).toMatchSnapshot(); diff --git a/test/server/utils/__snapshots__/normalizeOptions.test.js.snap b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap index 00b8226a16..d0d7755f81 100644 --- a/test/server/utils/__snapshots__/normalizeOptions.test.js.snap +++ b/test/server/utils/__snapshots__/normalizeOptions.test.js.snap @@ -8,8 +8,8 @@ Object { ], "contentBasePublicPath": "/", "transportMode": Object { - "client": "sockjs", - "server": "sockjs", + "client": "ws", + "server": "ws", }, "watchOptions": Object {}, } @@ -20,8 +20,8 @@ Object { "contentBase": "/path/to/dist", "contentBasePublicPath": "/", "transportMode": Object { - "client": "sockjs", - "server": "sockjs", + "client": "ws", + "server": "ws", }, "watchOptions": Object {}, } @@ -31,8 +31,8 @@ exports[`normalizeOptions contentBasePublicPath string should set correct option Object { "contentBasePublicPath": "/content-base-public-path", "transportMode": Object { - "client": "sockjs", - "server": "sockjs", + "client": "ws", + "server": "ws", }, "watchOptions": Object {}, } @@ -42,8 +42,8 @@ exports[`normalizeOptions no options should set correct options 1`] = ` Object { "contentBasePublicPath": "/", "transportMode": Object { - "client": "sockjs", - "server": "sockjs", + "client": "ws", + "server": "ws", }, "watchOptions": Object {}, } @@ -54,7 +54,7 @@ Object { "contentBasePublicPath": "/", "transportMode": Object { "client": "/path/to/custom/client/", - "server": "sockjs", + "server": "ws", }, "watchOptions": Object {}, } @@ -64,7 +64,7 @@ exports[`normalizeOptions transportMode custom server class should set correct o Object { "contentBasePublicPath": "/", "transportMode": Object { - "client": "sockjs", + "client": "ws", "server": [Function], }, "watchOptions": Object {}, @@ -75,7 +75,7 @@ exports[`normalizeOptions transportMode custom server path should set correct op Object { "contentBasePublicPath": "/", "transportMode": Object { - "client": "sockjs", + "client": "ws", "server": "/path/to/custom/server/", }, "watchOptions": Object {}, @@ -119,8 +119,8 @@ exports[`normalizeOptions watchOptions should set correct options 1`] = ` Object { "contentBasePublicPath": "/", "transportMode": Object { - "client": "sockjs", - "server": "sockjs", + "client": "ws", + "server": "ws", }, "watchOptions": Object { "poll": true, From a2ecaa4f85f9ba589b68ff0594e55f325c9994be Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Fri, 1 May 2020 23:44:53 -0500 Subject: [PATCH 02/10] chore(tests): fix provide plugin test --- client-src/default/socket.js | 11 +++-------- test/e2e/Client.test.js | 6 +++--- test/e2e/ProvidePlugin.test.js | 16 ++++++++-------- .../foo.js | 0 .../webpack.config.js | 0 5 files changed, 14 insertions(+), 19 deletions(-) rename test/fixtures/{provide-plugin-config => provide-plugin-sockjs-config}/foo.js (100%) rename test/fixtures/{provide-plugin-config => provide-plugin-sockjs-config}/webpack.config.js (100%) diff --git a/client-src/default/socket.js b/client-src/default/socket.js index 7752ffea8a..cf94428055 100644 --- a/client-src/default/socket.js +++ b/client-src/default/socket.js @@ -5,18 +5,13 @@ camelcase */ -// this SockJSClient is here as a default fallback, in case inline mode -// is off or the client is not injected. This will be switched to -// WebsocketClient when it becomes the default - -// important: the path to SockJSClient here is made to work in the 'client' -// directory, but is updated via the webpack compilation when compiled from -// the 'client-src' directory +// this WebsocketClient is here as a default fallback, +// in case the client is not injected const Client = typeof __webpack_dev_server_client__ !== 'undefined' ? __webpack_dev_server_client__ : // eslint-disable-next-line import/no-unresolved - require('./clients/SockJSClient'); + require('./clients/WebsocketClient'); let retries = 0; let client = null; diff --git a/test/e2e/Client.test.js b/test/e2e/Client.test.js index dfb2b2b1a0..a629e22562 100644 --- a/test/e2e/Client.test.js +++ b/test/e2e/Client.test.js @@ -23,13 +23,13 @@ const cssFilePath = resolve(__dirname, '../fixtures/reload-config/main.css'); describe('reload', () => { const modes = [ { - title: 'hot with default transportMode.client (sockjs)', + title: 'hot with default transportMode.client (ws)', shouldRefresh: false, }, { - title: 'hot with transportMode.client ws', + title: 'hot with transportMode.client sockjs', options: { - transportMode: 'ws', + transportMode: 'sockjs', }, shouldRefresh: false, }, diff --git a/test/e2e/ProvidePlugin.test.js b/test/e2e/ProvidePlugin.test.js index 53d0277e20..267ffa0ba2 100644 --- a/test/e2e/ProvidePlugin.test.js +++ b/test/e2e/ProvidePlugin.test.js @@ -1,14 +1,14 @@ 'use strict'; const testServer = require('../helpers/test-server'); -const config = require('../fixtures/provide-plugin-config/webpack.config'); +const sockjsConfig = require('../fixtures/provide-plugin-sockjs-config/webpack.config'); const wsConfig = require('../fixtures/provide-plugin-ws-config/webpack.config'); const runBrowser = require('../helpers/run-browser'); const port = require('../ports-map').ProvidePlugin; const { beforeBrowserCloseDelay } = require('../helpers/puppeteer-constants'); describe('ProvidePlugin', () => { - describe('default transportMode.client (sockjs)', () => { + describe('default transportMode.client (ws)', () => { beforeAll((done) => { const options = { port, @@ -17,13 +17,13 @@ describe('ProvidePlugin', () => { poll: true, }, }; - testServer.startAwaitingCompilation(config, options, done); + testServer.startAwaitingCompilation(wsConfig, options, done); }); afterAll(testServer.close); describe('on browser client', () => { - it('should inject SockJS client implementation', (done) => { + it('should inject ws client implementation', (done) => { runBrowser().then(({ page, browser }) => { page.waitForNavigation({ waitUntil: 'load' }).then(() => { page.waitFor(beforeBrowserCloseDelay).then(() => { @@ -45,23 +45,23 @@ describe('ProvidePlugin', () => { }); }); - describe('with transportMode.client ws', () => { + describe('with transportMode.client sockjs', () => { beforeAll((done) => { const options = { port, host: '0.0.0.0', - transportMode: 'ws', + transportMode: 'sockjs', watchOptions: { poll: true, }, }; - testServer.startAwaitingCompilation(wsConfig, options, done); + testServer.startAwaitingCompilation(sockjsConfig, options, done); }); afterAll(testServer.close); describe('on browser client', () => { - it('should inject ws client implementation', (done) => { + it('should inject sockjs client implementation', (done) => { runBrowser().then(({ page, browser }) => { page.waitForNavigation({ waitUntil: 'load' }).then(() => { page.waitFor(beforeBrowserCloseDelay).then(() => { diff --git a/test/fixtures/provide-plugin-config/foo.js b/test/fixtures/provide-plugin-sockjs-config/foo.js similarity index 100% rename from test/fixtures/provide-plugin-config/foo.js rename to test/fixtures/provide-plugin-sockjs-config/foo.js diff --git a/test/fixtures/provide-plugin-config/webpack.config.js b/test/fixtures/provide-plugin-sockjs-config/webpack.config.js similarity index 100% rename from test/fixtures/provide-plugin-config/webpack.config.js rename to test/fixtures/provide-plugin-sockjs-config/webpack.config.js From 5fd7bc331e55f9413ae67ed464dfd4ac2788d894 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 00:26:21 -0500 Subject: [PATCH 03/10] chore(ws): fix path replacement of ws import --- client-src/default/webpack.config.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client-src/default/webpack.config.js b/client-src/default/webpack.config.js index 3968b43bd9..8f9d5a4c41 100644 --- a/client-src/default/webpack.config.js +++ b/client-src/default/webpack.config.js @@ -19,12 +19,12 @@ module.exports = { }, plugins: [ new webpack.NormalModuleReplacementPlugin( - /^\.\/clients\/SockJSClient$/, + /^\.\/clients\/WebsocketClient$/, (resource) => { if (resource.context.startsWith(process.cwd())) { resource.request = resource.request.replace( - /^\.\/clients\/SockJSClient$/, - '../clients/SockJSClient' + /^\.\/clients\/WebsocketClient$/, + '../clients/WebsocketClient' ); } } From bfce66cdedb3e97fae1a2580a06660bc90c8afe0 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 00:47:26 -0500 Subject: [PATCH 04/10] chore(tests): fix universal compiler test --- test/integration/UniversalCompiler.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/UniversalCompiler.test.js b/test/integration/UniversalCompiler.test.js index cd00af83e7..fa7ec7de66 100644 --- a/test/integration/UniversalCompiler.test.js +++ b/test/integration/UniversalCompiler.test.js @@ -26,7 +26,7 @@ describe('universal compiler', () => { return done(err); } expect(res.text).toContain('Hello from the client'); - expect(res.text).toContain('sockjs-client'); + expect(res.text).toContain('ws'); done(); }); }); @@ -43,7 +43,7 @@ describe('universal compiler', () => { return done(err); } expect(res.text).toContain('Hello from the server'); - expect(res.text).not.toContain('sockjs-client'); + expect(res.text).not.toContain('ws'); done(); }); }); From b0429a72e4ddc963d2afe8ea341c20ea22110f53 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 01:04:02 -0500 Subject: [PATCH 05/10] chore(tests): fix sockPath option test --- test/server/sockPath-option.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/server/sockPath-option.test.js b/test/server/sockPath-option.test.js index b6840228b5..68db81e797 100644 --- a/test/server/sockPath-option.test.js +++ b/test/server/sockPath-option.test.js @@ -17,7 +17,14 @@ describe('sockPath options', () => { describe('default behavior', () => { beforeEach((done) => { - server = testServer.start(config, { port }, done); + server = testServer.start( + config, + { + transportMode: 'sockjs', + port, + }, + done + ); req = request(`http://localhost:${port}`); }); @@ -37,6 +44,7 @@ describe('sockPath options', () => { server = testServer.start( config, { + transportMode: 'sockjs', sockPath: '/foo/test/bar/', port, }, From a891d251b3e0c0bb389e5befa6538c8f27d7c49d Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 01:17:30 -0500 Subject: [PATCH 06/10] chore(tests): fix socket helper tests to use ws --- .../__snapshots__/socket-helper.test.js.snap | 10 +++++----- test/client/socket-helper.test.js | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/client/__snapshots__/socket-helper.test.js.snap b/test/client/__snapshots__/socket-helper.test.js.snap index e971ef199f..ca1557015f 100644 --- a/test/client/__snapshots__/socket-helper.test.js.snap +++ b/test/client/__snapshots__/socket-helper.test.js.snap @@ -1,12 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 1`] = ` +exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 1`] = ` Array [ "my.url", ] `; -exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 2`] = ` +exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 2`] = ` Array [ Array [ [Function], @@ -14,7 +14,7 @@ Array [ ] `; -exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 3`] = ` +exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 3`] = ` Array [ Array [ [Function], @@ -22,7 +22,7 @@ Array [ ] `; -exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 4`] = ` +exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 4`] = ` Array [ Array [ [Function], @@ -30,7 +30,7 @@ Array [ ] `; -exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 5`] = ` +exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 5`] = ` Array [ Array [ "hello world", diff --git a/test/client/socket-helper.test.js b/test/client/socket-helper.test.js index 387da99290..d4f303cce6 100644 --- a/test/client/socket-helper.test.js +++ b/test/client/socket-helper.test.js @@ -6,17 +6,17 @@ describe('socket', () => { jest.resetModules(); }); - it('should default to SockJSClient when no __webpack_dev_server_client__ set', () => { - jest.mock('../../client/clients/SockJSClient'); + it('should default to WebsocketClient when no __webpack_dev_server_client__ set', () => { + jest.mock('../../client/clients/WebsocketClient'); const socket = require('../../client/socket'); - const SockJSClient = require('../../client/clients/SockJSClient'); + const WebsocketClient = require('../../client/clients/WebsocketClient'); const mockHandler = jest.fn(); socket('my.url', { example: mockHandler, }); - const mockClientInstance = SockJSClient.mock.instances[0]; + const mockClientInstance = WebsocketClient.mock.instances[0]; // this simulates receiving a message from the server and passing it // along to the callback of onMessage @@ -27,7 +27,7 @@ describe('socket', () => { }) ); - expect(SockJSClient.mock.calls[0]).toMatchSnapshot(); + expect(WebsocketClient.mock.calls[0]).toMatchSnapshot(); expect(mockClientInstance.onOpen.mock.calls).toMatchSnapshot(); expect(mockClientInstance.onClose.mock.calls).toMatchSnapshot(); expect(mockClientInstance.onMessage.mock.calls).toMatchSnapshot(); @@ -35,9 +35,9 @@ describe('socket', () => { }); it('should use __webpack_dev_server_client__ when set', () => { - jest.mock('../../client/clients/SockJSClient'); + jest.mock('../../client/clients/WebsocketClient'); const socket = require('../../client/socket'); - global.__webpack_dev_server_client__ = require('../../client/clients/SockJSClient'); + global.__webpack_dev_server_client__ = require('../../client/clients/WebsocketClient'); const mockHandler = jest.fn(); socket('my.url', { From 142e1086ef2fe13f9312d3e83aaf63e167df7756 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 01:37:21 -0500 Subject: [PATCH 07/10] chore(tests): fix ClientOptions tests to use sockjs --- test/e2e/ClientOptions.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/e2e/ClientOptions.test.js b/test/e2e/ClientOptions.test.js index 2dfc0b442a..bd324bb212 100644 --- a/test/e2e/ClientOptions.test.js +++ b/test/e2e/ClientOptions.test.js @@ -25,6 +25,7 @@ describe('Client code', () => { beforeAll((done) => { const options = { + transportMode: 'sockjs', compress: true, port: port1, host: '0.0.0.0', @@ -87,6 +88,7 @@ describe('Client code', () => { describe('Client complex script path', () => { beforeAll((done) => { const options = { + transportMode: 'sockjs', port: port2, host: '0.0.0.0', watchOptions: { @@ -127,6 +129,7 @@ describe('Client complex script path', () => { describe('Client complex script path with sockPort', () => { beforeAll((done) => { const options = { + transportMode: 'sockjs', port: port2, host: '0.0.0.0', watchOptions: { @@ -171,6 +174,7 @@ describe('Client complex script path with sockPort', () => { describe('Client complex script path with sockPort, no sockPath', () => { beforeAll((done) => { const options = { + transportMode: 'sockjs', port: port2, host: '0.0.0.0', watchOptions: { @@ -208,6 +212,7 @@ describe('Client complex script path with sockPort, no sockPath', () => { describe('Client complex script path with sockHost', () => { beforeAll((done) => { const options = { + transportMode: 'sockjs', port: port2, host: '0.0.0.0', watchOptions: { From 036bba6a802b6f0f92c647a4d068820994565b00 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 12:22:54 -0500 Subject: [PATCH 08/10] chore(tests): fix ClientOptions test and add ws tests --- test/e2e/ClientOptions.test.js | 131 ++++++++++++++++++++++++++++++--- 1 file changed, 121 insertions(+), 10 deletions(-) diff --git a/test/e2e/ClientOptions.test.js b/test/e2e/ClientOptions.test.js index bd324bb212..fe97cbf3e2 100644 --- a/test/e2e/ClientOptions.test.js +++ b/test/e2e/ClientOptions.test.js @@ -9,7 +9,7 @@ const runBrowser = require('../helpers/run-browser'); const [port1, port2, port3] = require('../ports-map').ClientOptions; const { beforeBrowserCloseDelay } = require('../helpers/puppeteer-constants'); -describe('Client code', () => { +describe('sockjs client proxy', () => { function startProxy(port, cb) { const proxy = express(); proxy.use( @@ -66,7 +66,7 @@ describe('Client code', () => { }); it('requests websocket through the proxy with proper port number', (done) => { - runBrowser().then(({ page, browser }) => { + runBrowser().then(async ({ page, browser }) => { page .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) .then((requestObj) => { @@ -85,7 +85,75 @@ describe('Client code', () => { }); }); -describe('Client complex script path', () => { +describe('ws client proxy', () => { + function startProxy(port, cb) { + const proxy = express(); + proxy.use( + '/', + createProxyMiddleware({ + target: `http://localhost:${port1}`, + ws: true, + changeOrigin: true, + }) + ); + return proxy.listen(port, cb); + } + + beforeAll((done) => { + const options = { + transportMode: 'ws', + compress: true, + port: port1, + host: '0.0.0.0', + disableHostCheck: true, + hot: true, + watchOptions: { + poll: true, + }, + quiet: true, + public: 'myhost.test', + }; + testServer.startAwaitingCompilation(config, options, done); + }); + + afterAll(testServer.close); + + // [HPM] Proxy created: / -> http://localhost:{port1} + describe('behind a proxy', () => { + let proxy; + + beforeAll((done) => { + proxy = startProxy(port2, done); + }); + + afterAll((done) => { + proxy.close(() => { + done(); + }); + }); + + // TODO: listen for websocket requestType via puppeteer when added + // to puppeteer: https://github.com/puppeteer/puppeteer/issues/2974 + it('requests websocket through the proxy with proper port number', (done) => { + runBrowser().then(({ page, browser }) => { + page.on('console', (msg) => { + const text = msg.text(); + if (msg.type() === 'error' && text.includes('WebSocket connection')) { + page.waitFor(beforeBrowserCloseDelay).then(() => { + browser.close().then(() => { + expect(text).toContain(`ws://myhost.test:${port2}/sockjs-node`); + done(); + }); + }); + } + }); + page.goto(`http://localhost:${port2}/main`); + }); + }); + }); +}); + +describe('sockjs public and sockPath', () => { beforeAll((done) => { const options = { transportMode: 'sockjs', @@ -104,7 +172,7 @@ describe('Client complex script path', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct public hostname and sockPath', (done) => { + it('uses the correct public hostname and path', (done) => { runBrowser().then(({ page, browser }) => { page .waitForRequest((requestObj) => @@ -126,7 +194,7 @@ describe('Client complex script path', () => { }); }); -describe('Client complex script path with sockPort', () => { +describe('sockjs sockPath and sockPort', () => { beforeAll((done) => { const options = { transportMode: 'sockjs', @@ -145,7 +213,7 @@ describe('Client complex script path with sockPort', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct sockPort', (done) => { + it('uses correct port and path', (done) => { runBrowser().then(({ page, browser }) => { page .waitForRequest((requestObj) => @@ -171,7 +239,7 @@ describe('Client complex script path with sockPort', () => { // previously, using sockPort without sockPath had the ability // to alter the sockPath (based on a bug in client-src/default/index.js) // so we need to make sure sockPath is not altered in this case -describe('Client complex script path with sockPort, no sockPath', () => { +describe('sockjs sockPort, no sockPath', () => { beforeAll((done) => { const options = { transportMode: 'sockjs', @@ -189,7 +257,7 @@ describe('Client complex script path with sockPort, no sockPath', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct sockPort and sockPath', (done) => { + it('uses correct port and path', (done) => { runBrowser().then(({ page, browser }) => { page .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) @@ -209,7 +277,7 @@ describe('Client complex script path with sockPort, no sockPath', () => { }); }); -describe('Client complex script path with sockHost', () => { +describe('sockjs sockHost', () => { beforeAll((done) => { const options = { transportMode: 'sockjs', @@ -227,7 +295,7 @@ describe('Client complex script path with sockHost', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct sockHost', (done) => { + it('uses correct host', (done) => { runBrowser().then(({ page, browser }) => { page .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) @@ -247,6 +315,49 @@ describe('Client complex script path with sockHost', () => { }); }); +describe('ws client sockHost, sockPort, and sockPath', () => { + beforeAll((done) => { + const options = { + transportMode: 'ws', + port: port2, + host: '0.0.0.0', + watchOptions: { + poll: true, + }, + sockHost: 'myhost.test', + sockPort: port3, + sockPath: '/foo/test/bar/', + quiet: true, + }; + testServer.startAwaitingCompilation(config, options, done); + }); + + afterAll(testServer.close); + + describe('browser client', () => { + // TODO: listen for websocket requestType via puppeteer when added + // to puppeteer: https://github.com/puppeteer/puppeteer/issues/2974 + it('uses correct host, port, and path', (done) => { + runBrowser().then(({ page, browser }) => { + page.on('console', (msg) => { + const text = msg.text(); + if (msg.type() === 'error' && text.includes('WebSocket connection')) { + page.waitFor(beforeBrowserCloseDelay).then(() => { + browser.close().then(() => { + expect(text).toContain( + `ws://myhost.test:${port3}/foo/test/bar/` + ); + done(); + }); + }); + } + }); + page.goto(`http://localhost:${port2}/main`); + }); + }); + }); +}); + describe('Client console.log', () => { const baseOptions = { port: port2, From d6aa83ed0d1ae2580ea4b4118acaa8c75aae3443 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 12:29:48 -0500 Subject: [PATCH 09/10] chore(tests): fix universal compiler test --- test/integration/UniversalCompiler.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/UniversalCompiler.test.js b/test/integration/UniversalCompiler.test.js index fa7ec7de66..b8855d55f9 100644 --- a/test/integration/UniversalCompiler.test.js +++ b/test/integration/UniversalCompiler.test.js @@ -26,7 +26,7 @@ describe('universal compiler', () => { return done(err); } expect(res.text).toContain('Hello from the client'); - expect(res.text).toContain('ws'); + expect(res.text).toContain('WebsocketClient'); done(); }); }); @@ -43,7 +43,7 @@ describe('universal compiler', () => { return done(err); } expect(res.text).toContain('Hello from the server'); - expect(res.text).not.toContain('ws'); + expect(res.text).not.toContain('WebsocketClient'); done(); }); }); From 96a53bef06040190bed6e9cae0c72e10f7db5e06 Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Sat, 2 May 2020 12:38:53 -0500 Subject: [PATCH 10/10] chore(ws): change sockjs-node path to ws by default --- client-src/default/utils/createSocketUrl.js | 2 +- examples/cli/public-protocol/README.md | 2 +- lib/Server.js | 2 +- test/client/clients/SockJSClient.test.js | 4 +- .../createSocketUrl.test.js.snap | 40 ++++----- test/client/utils/createSocketUrl.test.js | 82 ++++--------------- test/e2e/ClientOptions.test.js | 18 ++-- test/server/servers/SockJSServer.test.js | 6 +- test/server/sockPath-option.test.js | 2 +- test/server/transportMode-option.test.js | 10 +-- 10 files changed, 60 insertions(+), 108 deletions(-) diff --git a/client-src/default/utils/createSocketUrl.js b/client-src/default/utils/createSocketUrl.js index 4f5ebdd592..971ceaba99 100644 --- a/client-src/default/utils/createSocketUrl.js +++ b/client-src/default/utils/createSocketUrl.js @@ -75,7 +75,7 @@ function getSocketUrl(urlParts, loc) { // resourceQuery, so we need to fall back to the default if // they are not provided const sockHost = query.sockHost || hostname; - const sockPath = query.sockPath || '/sockjs-node'; + const sockPath = query.sockPath || '/ws'; let sockPort = query.sockPort || port; if (sockPort === 'location') { diff --git a/examples/cli/public-protocol/README.md b/examples/cli/public-protocol/README.md index 98c6cc5701..47b2b65853 100644 --- a/examples/cli/public-protocol/README.md +++ b/examples/cli/public-protocol/README.md @@ -13,6 +13,6 @@ You're now able to explicitly define the protocol used with the `public` option The script should open `http://localhost:8080/` in your default browser. -You should see a failed attempt to establish a connection to `/sockjs-node` +You should see a failed attempt to establish a connection to `/ws` via the explicitly defined `https://localhost:8080`. This fails of course since we're not hosting https. diff --git a/lib/Server.js b/lib/Server.js index 72523c185d..bc3883dc55 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -95,7 +95,7 @@ class Server { this.sockPath = `/${ this.options.sockPath ? this.options.sockPath.replace(/^\/|\/$/g, '') - : 'sockjs-node' + : 'ws' }`; if (this.progress) { diff --git a/test/client/clients/SockJSClient.test.js b/test/client/clients/SockJSClient.test.js index d6c1ec4745..bbfefd9a35 100644 --- a/test/client/clients/SockJSClient.test.js +++ b/test/client/clients/SockJSClient.test.js @@ -21,7 +21,7 @@ describe('SockJSClient', () => { listeningApp.listen(port, 'localhost', () => { socketServer = sockjs.createServer(); socketServer.installHandlers(listeningApp, { - prefix: '/sockjs-node', + prefix: '/ws', }); done(); }); @@ -41,7 +41,7 @@ describe('SockJSClient', () => { }, 1000); }); - const client = new SockJSClient(`http://localhost:${port}/sockjs-node`); + const client = new SockJSClient(`http://localhost:${port}/ws`); const data = []; client.onOpen(() => { diff --git a/test/client/utils/__snapshots__/createSocketUrl.test.js.snap b/test/client/utils/__snapshots__/createSocketUrl.test.js.snap index bc0ba1af21..eacb9b6271 100644 --- a/test/client/utils/__snapshots__/createSocketUrl.test.js.snap +++ b/test/client/utils/__snapshots__/createSocketUrl.test.js.snap @@ -1,41 +1,41 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`createSocketUrl should return the url when __resourceQuery is ?test 1`] = `"/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is ?test 1`] = `"/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is http://0.0.0.0 1`] = `"http://localhost/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is http://0.0.0.0 1`] = `"http://localhost/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is http://127.0.0.1 1`] = `"http://127.0.0.1/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is http://127.0.0.1 1`] = `"http://127.0.0.1/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is http://user:password@localhost/ 1`] = `"http://user:password@localhost/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is http://user:password@localhost/ 1`] = `"http://user:password@localhost/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is https://example.com 1`] = `"https://example.com/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is https://example.com 1`] = `"https://example.com/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path 1`] = `"https://example.com/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path 1`] = `"https://example.com/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path/foo.js 1`] = `"https://example.com/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path/foo.js 1`] = `"https://example.com/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is https://localhost:123 1`] = `"https://localhost:123/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is https://localhost:123 1`] = `"https://localhost:123/ws"`; -exports[`createSocketUrl should return the url when __resourceQuery is undefined 1`] = `"/sockjs-node"`; +exports[`createSocketUrl should return the url when __resourceQuery is undefined 1`] = `"/ws"`; -exports[`createSocketUrl should return the url when the current script source is ?test 1`] = `"/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is ?test 1`] = `"/ws"`; -exports[`createSocketUrl should return the url when the current script source is http://0.0.0.0 1`] = `"http://localhost/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is http://0.0.0.0 1`] = `"http://localhost/ws"`; -exports[`createSocketUrl should return the url when the current script source is http://127.0.0.1 1`] = `"http://127.0.0.1/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is http://127.0.0.1 1`] = `"http://127.0.0.1/ws"`; -exports[`createSocketUrl should return the url when the current script source is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/ws"`; -exports[`createSocketUrl should return the url when the current script source is http://user:password@localhost/ 1`] = `"http://user:password@localhost/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is http://user:password@localhost/ 1`] = `"http://user:password@localhost/ws"`; -exports[`createSocketUrl should return the url when the current script source is https://example.com 1`] = `"https://example.com/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is https://example.com 1`] = `"https://example.com/ws"`; -exports[`createSocketUrl should return the url when the current script source is https://example.com/path 1`] = `"https://example.com/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is https://example.com/path 1`] = `"https://example.com/ws"`; -exports[`createSocketUrl should return the url when the current script source is https://example.com/path/foo.js 1`] = `"https://example.com/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is https://example.com/path/foo.js 1`] = `"https://example.com/ws"`; -exports[`createSocketUrl should return the url when the current script source is https://localhost:123 1`] = `"https://localhost:123/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is https://localhost:123 1`] = `"https://localhost:123/ws"`; -exports[`createSocketUrl should return the url when the current script source is undefined 1`] = `"/sockjs-node"`; +exports[`createSocketUrl should return the url when the current script source is undefined 1`] = `"/ws"`; diff --git a/test/client/utils/createSocketUrl.test.js b/test/client/utils/createSocketUrl.test.js index ab42ac67d7..59b565ef11 100644 --- a/test/client/utils/createSocketUrl.test.js +++ b/test/client/utils/createSocketUrl.test.js @@ -41,36 +41,12 @@ describe('createSocketUrl', () => { const samples2 = [ // script source, location, output socket URL - [ - 'http://example.com', - 'https://something.com', - 'https://example.com/sockjs-node', - ], - [ - 'http://127.0.0.1', - 'https://something.com', - 'http://127.0.0.1/sockjs-node', - ], - [ - 'http://0.0.0.0', - 'https://something.com', - 'https://something.com/sockjs-node', - ], - [ - 'http://0.0.0.0', - 'http://something.com', - 'http://something.com/sockjs-node', - ], - [ - 'http://example.com', - 'http://something.com', - 'http://example.com/sockjs-node', - ], - [ - 'https://example.com', - 'http://something.com', - 'https://example.com/sockjs-node', - ], + ['http://example.com', 'https://something.com', 'https://example.com/ws'], + ['http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'], + ['http://0.0.0.0', 'https://something.com', 'https://something.com/ws'], + ['http://0.0.0.0', 'http://something.com', 'http://something.com/ws'], + ['http://example.com', 'http://something.com', 'http://example.com/ws'], + ['https://example.com', 'http://something.com', 'https://example.com/ws'], ]; samples2.forEach(([scriptSrc, loc, expected]) => { @@ -91,45 +67,21 @@ describe('createSocketUrl', () => { const samples3 = [ // script source, location, output socket URL - [ - '?http://example.com', - 'https://something.com', - 'https://example.com/sockjs-node', - ], - [ - '?http://127.0.0.1', - 'https://something.com', - 'http://127.0.0.1/sockjs-node', - ], - [ - '?http://0.0.0.0', - 'https://something.com', - 'https://something.com/sockjs-node', - ], - [ - '?http://0.0.0.0', - 'http://something.com', - 'http://something.com/sockjs-node', - ], - [ - '?http://example.com', - 'http://something.com', - 'http://example.com/sockjs-node', - ], - [ - '?https://example.com', - 'http://something.com', - 'https://example.com/sockjs-node', - ], + ['?http://example.com', 'https://something.com', 'https://example.com/ws'], + ['?http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'], + ['?http://0.0.0.0', 'https://something.com', 'https://something.com/ws'], + ['?http://0.0.0.0', 'http://something.com', 'http://something.com/ws'], + ['?http://example.com', 'http://something.com', 'http://example.com/ws'], + ['?https://example.com', 'http://something.com', 'https://example.com/ws'], [ '?https://example.com?sockHost=asdf', 'http://something.com', - 'https://asdf/sockjs-node', + 'https://asdf/ws', ], [ '?https://example.com?sockPort=34', 'http://something.com', - 'https://example.com:34/sockjs-node', + 'https://example.com:34/ws', ], [ '?https://example.com?sockPath=xxx', @@ -139,17 +91,17 @@ describe('createSocketUrl', () => { [ '?http://0.0.0.0:8096&sockPort=8097', 'http://localhost', - 'http://localhost:8097/sockjs-node', + 'http://localhost:8097/ws', ], [ '?http://example.com:8096&sockPort=location', 'http://something.com', - 'http://example.com/sockjs-node', + 'http://example.com/ws', ], [ '?http://0.0.0.0:8096&sockPort=location', 'http://localhost:3000', - 'http://localhost:3000/sockjs-node', + 'http://localhost:3000/ws', ], ]; samples3.forEach(([scriptSrc, loc, expected]) => { diff --git a/test/e2e/ClientOptions.test.js b/test/e2e/ClientOptions.test.js index fe97cbf3e2..2a34d49069 100644 --- a/test/e2e/ClientOptions.test.js +++ b/test/e2e/ClientOptions.test.js @@ -57,23 +57,23 @@ describe('sockjs client proxy', () => { it('responds with a 200 on proxy port', (done) => { const req = request(`http://localhost:${port2}`); - req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done); + req.get('/ws').expect(200, 'Welcome to SockJS!\n', done); }); it('responds with a 200 on non-proxy port', (done) => { const req = request(`http://localhost:${port1}`); - req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done); + req.get('/ws').expect(200, 'Welcome to SockJS!\n', done); }); it('requests websocket through the proxy with proper port number', (done) => { runBrowser().then(async ({ page, browser }) => { page - .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) + .waitForRequest((requestObj) => requestObj.url().match(/ws/)) .then((requestObj) => { page.waitFor(beforeBrowserCloseDelay).then(() => { browser.close().then(() => { expect(requestObj.url()).toContain( - `http://localhost:${port1}/sockjs-node` + `http://localhost:${port1}/ws` ); done(); }); @@ -141,7 +141,7 @@ describe('ws client proxy', () => { if (msg.type() === 'error' && text.includes('WebSocket connection')) { page.waitFor(beforeBrowserCloseDelay).then(() => { browser.close().then(() => { - expect(text).toContain(`ws://myhost.test:${port2}/sockjs-node`); + expect(text).toContain(`ws://myhost.test:${port2}/ws`); done(); }); }); @@ -260,12 +260,12 @@ describe('sockjs sockPort, no sockPath', () => { it('uses correct port and path', (done) => { runBrowser().then(({ page, browser }) => { page - .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) + .waitForRequest((requestObj) => requestObj.url().match(/ws/)) .then((requestObj) => { page.waitFor(beforeBrowserCloseDelay).then(() => { browser.close().then(() => { expect(requestObj.url()).toContain( - `http://localhost:${port3}/sockjs-node` + `http://localhost:${port3}/ws` ); done(); }); @@ -298,12 +298,12 @@ describe('sockjs sockHost', () => { it('uses correct host', (done) => { runBrowser().then(({ page, browser }) => { page - .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) + .waitForRequest((requestObj) => requestObj.url().match(/ws/)) .then((requestObj) => { page.waitFor(beforeBrowserCloseDelay).then(() => { browser.close().then(() => { expect(requestObj.url()).toContain( - `http://myhost.test:${port2}/sockjs-node` + `http://myhost.test:${port2}/ws` ); done(); }); diff --git a/test/server/servers/SockJSServer.test.js b/test/server/servers/SockJSServer.test.js index d381be41fc..12a48fff8b 100644 --- a/test/server/servers/SockJSServer.test.js +++ b/test/server/servers/SockJSServer.test.js @@ -21,7 +21,7 @@ describe('SockJSServer', () => { error: () => {}, debug: () => {}, }, - sockPath: '/sockjs-node', + sockPath: '/ws', listeningApp, }; @@ -46,7 +46,7 @@ describe('SockJSServer', () => { }, 1000); }); - const client = new SockJS(`http://localhost:${port}/sockjs-node`); + const client = new SockJS(`http://localhost:${port}/ws`); client.onmessage = (e) => { data.push(e.data); @@ -72,7 +72,7 @@ describe('SockJSServer', () => { }); // eslint-disable-next-line new-cap - const client = new SockJS(`http://localhost:${port}/sockjs-node`); + const client = new SockJS(`http://localhost:${port}/ws`); setTimeout(() => { // the client closes itself, the server does not close it diff --git a/test/server/sockPath-option.test.js b/test/server/sockPath-option.test.js index 68db81e797..c3b1a6fda1 100644 --- a/test/server/sockPath-option.test.js +++ b/test/server/sockPath-option.test.js @@ -33,7 +33,7 @@ describe('sockPath options', () => { }); it('responds with a 200', (done) => { - req.get('/sockjs-node').expect(200, done); + req.get('/ws').expect(200, done); }); }); diff --git a/test/server/transportMode-option.test.js b/test/server/transportMode-option.test.js index def5a7105b..ee8cc57700 100644 --- a/test/server/transportMode-option.test.js +++ b/test/server/transportMode-option.test.js @@ -144,7 +144,7 @@ describe('transportMode', () => { }); it('sockjs path responds with a 200', (done) => { - req.get('/sockjs-node').expect(200, done); + req.get('/ws').expect(200, done); }); }); @@ -164,7 +164,7 @@ describe('transportMode', () => { }); it('sockjs path responds with a 200', (done) => { - req.get('/sockjs-node').expect(200, done); + req.get('/ws').expect(200, done); }); }); @@ -184,7 +184,7 @@ describe('transportMode', () => { }); it('sockjs path responds with a 200', (done) => { - req.get('/sockjs-node').expect(200, done); + req.get('/ws').expect(200, done); }); }); @@ -324,7 +324,7 @@ describe('transportMode', () => { it('results in an error', (done) => { const data = []; - const client = new SockJS(`http://localhost:${port}/sockjs-node`); + const client = new SockJS(`http://localhost:${port}/ws`); client.onopen = () => { data.push('open'); @@ -416,7 +416,7 @@ describe('transportMode', () => { it('results in an error', (done) => { const data = []; - const client = new SockJS(`http://localhost:${port}/sockjs-node`); + const client = new SockJS(`http://localhost:${port}/ws`); client.onopen = () => { data.push('open');