Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: insert client implementation from within Dev Server API (#1933
- Loading branch information
1 parent
6973e4c
commit eecc1e4
Showing
9 changed files
with
226 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable | ||
no-unused-vars | ||
*/ | ||
module.exports = class BaseClient { | ||
static getClientPath(options) { | ||
throw new Error('Client needs implementation'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable | ||
no-unused-vars | ||
*/ | ||
const SockJS = require('sockjs-client/dist/sockjs'); | ||
const BaseClient = require('./BaseClient'); | ||
|
||
module.exports = class SockJSClient extends BaseClient { | ||
constructor(url) { | ||
super(); | ||
this.sock = new SockJS(url); | ||
} | ||
|
||
static getClientPath(options) { | ||
return require.resolve('./SockJSClient'); | ||
} | ||
|
||
onOpen(f) { | ||
this.sock.onopen = f; | ||
} | ||
|
||
onClose(f) { | ||
this.sock.onclose = f; | ||
} | ||
|
||
// call f with the message string as the first argument | ||
onMessage(f) { | ||
this.sock.onmessage = (e) => { | ||
f(e.data); | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
const BaseClient = require('./BaseClient'); | ||
|
||
module.exports = class WebsocketClient extends BaseClient {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const http = require('http'); | ||
const express = require('express'); | ||
const sockjs = require('sockjs'); | ||
const SockJSClient = require('../lib/clients/SockJSClient'); | ||
|
||
describe('SockJSClient', () => { | ||
let socketServer; | ||
let listeningApp; | ||
|
||
beforeAll((done) => { | ||
// eslint-disable-next-line new-cap | ||
const app = new express(); | ||
listeningApp = http.createServer(app); | ||
listeningApp.listen(8080, 'localhost', () => { | ||
socketServer = sockjs.createServer(); | ||
socketServer.installHandlers(listeningApp, { | ||
prefix: '/sockjs-node', | ||
}); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('client', () => { | ||
it('should open, recieve message, and close', (done) => { | ||
socketServer.on('connection', (connection) => { | ||
connection.write('hello world'); | ||
setTimeout(() => { | ||
connection.close(); | ||
}, 1000); | ||
}); | ||
const client = new SockJSClient('http://localhost:8080/sockjs-node'); | ||
const data = []; | ||
client.onOpen(() => { | ||
data.push('open'); | ||
}); | ||
client.onClose(() => { | ||
data.push('close'); | ||
}); | ||
client.onMessage((msg) => { | ||
data.push(msg); | ||
}); | ||
setTimeout(() => { | ||
expect(data.length).toEqual(3); | ||
expect(data[0]).toEqual('open'); | ||
expect(data[1]).toEqual('hello world'); | ||
expect(data[2]).toEqual('close'); | ||
done(); | ||
}, 3000); | ||
}); | ||
}); | ||
|
||
afterAll((done) => { | ||
listeningApp.close(() => { | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const testServer = require('../helpers/test-server'); | ||
const config = require('../fixtures/provide-plugin-config/webpack.config'); | ||
const runBrowser = require('../helpers/run-browser'); | ||
|
||
describe('ProvidePlugin', () => { | ||
describe('inline', () => { | ||
beforeAll((done) => { | ||
const options = { | ||
port: 9000, | ||
host: '0.0.0.0', | ||
inline: true, | ||
watchOptions: { | ||
poll: true, | ||
}, | ||
}; | ||
testServer.startAwaitingCompilation(config, options, done); | ||
}); | ||
|
||
afterAll(testServer.close); | ||
|
||
describe('on browser client', () => { | ||
jest.setTimeout(30000); | ||
|
||
it('should inject SockJS client implementation', (done) => { | ||
runBrowser().then(({ page, browser }) => { | ||
page.waitForNavigation({ waitUntil: 'load' }).then(() => { | ||
page | ||
.evaluate(() => { | ||
return window.injectedClient === window.expectedClient; | ||
}) | ||
.then((isCorrectClient) => { | ||
expect(isCorrectClient).toBeTruthy(); | ||
browser.close().then(done); | ||
}); | ||
}); | ||
page.goto('http://localhost:9000/main'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('not inline', () => { | ||
beforeAll((done) => { | ||
const options = { | ||
port: 9000, | ||
host: '0.0.0.0', | ||
inline: false, | ||
watchOptions: { | ||
poll: true, | ||
}, | ||
}; | ||
testServer.startAwaitingCompilation(config, options, done); | ||
}); | ||
|
||
afterAll(testServer.close); | ||
|
||
describe('on browser client', () => { | ||
jest.setTimeout(30000); | ||
|
||
it('should not inject client implementation', (done) => { | ||
runBrowser().then(({ page, browser }) => { | ||
page.waitForNavigation({ waitUntil: 'load' }).then(() => { | ||
page | ||
.evaluate(() => { | ||
// eslint-disable-next-line no-undefined | ||
return window.injectedClient === undefined; | ||
}) | ||
.then((isCorrectClient) => { | ||
expect(isCorrectClient).toBeTruthy(); | ||
browser.close().then(done); | ||
}); | ||
}); | ||
page.goto('http://localhost:9000/main'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const SockJSClient = require('../../../lib/clients/SockJSClient'); | ||
|
||
window.expectedClient = SockJSClient; | ||
// eslint-disable-next-line camelcase, no-undef | ||
window.injectedClient = __webpack_dev_server_client__; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
mode: 'development', | ||
context: __dirname, | ||
entry: './foo.js', | ||
output: { | ||
path: '/', | ||
}, | ||
node: false, | ||
}; |