Merge pull request #1393 from transloadit/remove-window-location

companion-client: remove the use of window.location
This commit is contained in:
Artur Paikin 2019-04-02 11:57:31 +03:00 committed by GitHub
commit d3a6be786d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 5 deletions

View file

@ -83,8 +83,8 @@ module.exports = class Provider extends RequestClient {
plugin.opts.serverPattern = pattern
} else {
// does not start with https://
if (/^(?!https?:\/\/).*$/.test(opts.serverUrl)) {
plugin.opts.serverPattern = `${location.protocol}//${opts.serverUrl.replace(/^\/\//, '')}`
if (/^(?!https?:\/\/).*$/i.test(opts.serverUrl)) {
plugin.opts.serverPattern = `https://${opts.serverUrl.replace(/^\/\//, '')}`
} else {
plugin.opts.serverPattern = opts.serverUrl
}

View file

@ -1,8 +1,8 @@
module.exports = function getSocketHost (url) {
// get the host domain
var regex = /^(?:https?:\/\/|\/\/)?(?:[^@\n]+@)?(?:www\.)?([^\n]+)/
var regex = /^(?:https?:\/\/|\/\/)?(?:[^@\n]+@)?(?:www\.)?([^\n]+)/i
var host = regex.exec(url)[1]
var socketProtocol = location.protocol === 'https:' ? 'wss' : 'ws'
var socketProtocol = /^http:\/\//i.test(url) ? 'ws' : 'wss'
return `${socketProtocol}://${host}`
}

View file

@ -4,6 +4,18 @@ describe('getSocketHost', () => {
it('should get the host from the specified url', () => {
expect(
getSocketHost('https://foo.bar/a/b/cd?e=fghi&l=k&m=n')
).toEqual('ws://foo.bar/a/b/cd?e=fghi&l=k&m=n')
).toEqual('wss://foo.bar/a/b/cd?e=fghi&l=k&m=n')
expect(
getSocketHost('Https://foo.bar/a/b/cd?e=fghi&l=k&m=n')
).toEqual('wss://foo.bar/a/b/cd?e=fghi&l=k&m=n')
expect(
getSocketHost('foo.bar/a/b/cd?e=fghi&l=k&m=n')
).toEqual('wss://foo.bar/a/b/cd?e=fghi&l=k&m=n')
expect(
getSocketHost('http://foo.bar/a/b/cd?e=fghi&l=k&m=n')
).toEqual('ws://foo.bar/a/b/cd?e=fghi&l=k&m=n')
})
})