Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): move to util, more tests
- Loading branch information
1 parent
680d73f
commit 907f378
Showing
4 changed files
with
97 additions
and
35 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,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 (eg. '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