-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server): add serverMode option #1937
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aa6f615
feat(server): added serverMode option
knagaitsev 680d73f
feat(server): removed BaseServer inheritance requirement
knagaitsev 907f378
feat(server): move to util, more tests
knagaitsev 07d1d58
fix(server): fix error message on bad implementation
knagaitsev f835079
feat(server): renamed to SocketServerImplementation
knagaitsev 537e0b2
feat(server): uncapitalized socketServerImplementation
knagaitsev 54a8067
Merge branch 'master' into serverMode-option
evilebottnawi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,41 @@ | ||
'use strict'; | ||
|
||
function getSocketServerImplementation(options) { | ||
let ServerImplementation; | ||
let serverImplFound = true; | ||
switch (typeof options.serverMode) { | ||
case 'string': | ||
// could be 'sockjs', in the future 'ws', or a path that should be required | ||
if (options.serverMode === 'sockjs') { | ||
// eslint-disable-next-line global-require | ||
ServerImplementation = require('../servers/SockJSServer'); | ||
} else { | ||
try { | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
ServerImplementation = require(options.serverMode); | ||
} catch (e) { | ||
serverImplFound = false; | ||
} | ||
} | ||
break; | ||
case 'function': | ||
// potentially do more checks here to confirm that the user implemented this properlly | ||
// since errors could be difficult to understand | ||
ServerImplementation = options.serverMode; | ||
break; | ||
default: | ||
serverImplFound = false; | ||
} | ||
|
||
if (!serverImplFound) { | ||
throw new Error( | ||
"serverMode must be a string denoting a default implementation (e.g. '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' | ||
); | ||
} | ||
|
||
return ServerImplementation; | ||
} | ||
|
||
module.exports = getSocketServerImplementation; |
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,50 @@ | ||
'use strict'; | ||
|
||
const getSocketServerImplementation = require('../lib/utils/getSocketServerImplementation'); | ||
const SockJSServer = require('../lib/servers/SockJSServer'); | ||
|
||
describe('getSocketServerImplementation', () => { | ||
it("should work with serverMode: 'sockjs'", () => { | ||
let result; | ||
|
||
expect(() => { | ||
result = getSocketServerImplementation({ | ||
serverMode: 'sockjs', | ||
}); | ||
}).not.toThrow(); | ||
|
||
expect(result).toEqual(SockJSServer); | ||
}); | ||
|
||
it('should work with serverMode: SockJSServer class', () => { | ||
let result; | ||
|
||
expect(() => { | ||
result = getSocketServerImplementation({ | ||
serverMode: SockJSServer, | ||
}); | ||
}).not.toThrow(); | ||
|
||
expect(result).toEqual(SockJSServer); | ||
}); | ||
|
||
it('should work with serverMode: SockJSServer full path', () => { | ||
let result; | ||
|
||
expect(() => { | ||
result = getSocketServerImplementation({ | ||
serverMode: require.resolve('../lib/servers/SockJSServer'), | ||
}); | ||
}).not.toThrow(); | ||
|
||
expect(result).toEqual(SockJSServer); | ||
}); | ||
|
||
it('should throw with serverMode: bad path', () => { | ||
expect(() => { | ||
getSocketServerImplementation({ | ||
serverMode: '/bad/path/to/implementation', | ||
}); | ||
}).toThrow(/serverMode must be a string/); | ||
}); | ||
}); |
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,133 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable | ||
class-methods-use-this | ||
*/ | ||
const request = require('supertest'); | ||
const sockjs = require('sockjs'); | ||
const SockJSServer = require('../lib/servers/SockJSServer'); | ||
const config = require('./fixtures/simple-config/webpack.config'); | ||
const testServer = require('./helpers/test-server'); | ||
const BaseServer = require('./../lib/servers/BaseServer'); | ||
|
||
describe('serverMode', () => { | ||
let server; | ||
let req; | ||
|
||
afterEach((done) => { | ||
testServer.close(done); | ||
req = null; | ||
server = null; | ||
}); | ||
describe("supplying 'sockjs' as a string", () => { | ||
beforeEach((done) => { | ||
server = testServer.start( | ||
config, | ||
{ | ||
serverMode: 'sockjs', | ||
}, | ||
done | ||
); | ||
req = request('http://localhost:8080'); | ||
}); | ||
|
||
it('sockjs path responds with a 200', (done) => { | ||
req.get('/sockjs-node').expect(200, done); | ||
}); | ||
}); | ||
|
||
describe('supplying path to sockjs implementation', () => { | ||
beforeEach((done) => { | ||
server = testServer.start( | ||
config, | ||
{ | ||
serverMode: require.resolve('../lib/servers/SockJSServer'), | ||
}, | ||
done | ||
); | ||
req = request('http://localhost:8080'); | ||
}); | ||
|
||
it('sockjs path responds with a 200', (done) => { | ||
req.get('/sockjs-node').expect(200, done); | ||
}); | ||
}); | ||
|
||
describe('supplying sockjs implementation class', () => { | ||
beforeEach((done) => { | ||
server = testServer.start( | ||
config, | ||
{ | ||
serverMode: SockJSServer, | ||
}, | ||
done | ||
); | ||
req = request('http://localhost:8080'); | ||
}); | ||
|
||
it('sockjs path responds with a 200', (done) => { | ||
req.get('/sockjs-node').expect(200, done); | ||
}); | ||
}); | ||
|
||
describe('custom sockjs implementation', () => { | ||
it('uses supplied server implementation', (done) => { | ||
server = testServer.start( | ||
config, | ||
{ | ||
sockPath: '/foo/test/bar/', | ||
serverMode: class MySockJSServer extends BaseServer { | ||
constructor(serv) { | ||
super(serv); | ||
this.socket = sockjs.createServer({ | ||
// Use provided up-to-date sockjs-client | ||
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js', | ||
// Limit useless logs | ||
log: (severity, line) => { | ||
if (severity === 'error') { | ||
this.server.log.error(line); | ||
} else { | ||
this.server.log.debug(line); | ||
} | ||
}, | ||
}); | ||
|
||
this.socket.installHandlers(this.server.listeningApp, { | ||
prefix: this.server.sockPath, | ||
}); | ||
|
||
expect(server.options.sockPath).toEqual('/foo/test/bar/'); | ||
} | ||
|
||
send(connection, message) { | ||
connection.write(message); | ||
} | ||
|
||
close(connection) { | ||
connection.close(); | ||
} | ||
|
||
onConnection(f) { | ||
this.socket.on('connection', f); | ||
} | ||
}, | ||
}, | ||
done | ||
); | ||
}); | ||
}); | ||
|
||
describe('supplying nonexistent path', () => { | ||
it('should throw an error', () => { | ||
expect(() => { | ||
server = testServer.start( | ||
config, | ||
{ | ||
serverMode: '/bad/path/to/implementation', | ||
}, | ||
() => {} | ||
); | ||
}).toThrow(/serverMode must be a string/); | ||
}); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evilebottnawi being able to pass in class also makes testing easier