Przeglądaj źródła

companion-client: remove the use of window.location

Ifedapo Olarewaju 6 lat temu
rodzic
commit
62305be8b9

+ 2 - 2
packages/@uppy/companion-client/src/Provider.js

@@ -83,8 +83,8 @@ module.exports = class Provider extends RequestClient {
       plugin.opts.serverPattern = pattern
       plugin.opts.serverPattern = pattern
     } else {
     } else {
       // does not start with https://
       // 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 {
       } else {
         plugin.opts.serverPattern = opts.serverUrl
         plugin.opts.serverPattern = opts.serverUrl
       }
       }

+ 2 - 2
packages/@uppy/utils/src/getSocketHost.js

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

+ 13 - 1
packages/@uppy/utils/src/getSocketHost.test.js

@@ -4,6 +4,18 @@ describe('getSocketHost', () => {
   it('should get the host from the specified url', () => {
   it('should get the host from the specified url', () => {
     expect(
     expect(
         getSocketHost('https://foo.bar/a/b/cd?e=fghi&l=k&m=n')
         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')
   })
   })
 })
 })