@based/opts
Advanced tools
Comparing version 0.0.1 to 0.0.2
import { BasedConnectOpts } from './types'; | ||
declare const discoverService: (opts: BasedConnectOpts, tries?: number) => Promise<string>; | ||
declare const discoverService: (opts: BasedConnectOpts, http?: boolean, tries?: number) => Promise<string>; | ||
export default discoverService; | ||
//# sourceMappingURL=getUrlFromOpts.d.ts.map |
@@ -21,3 +21,3 @@ "use strict"; | ||
}; | ||
const request = async (discoveryUrl, opts) => { | ||
const request = async (discoveryUrl, opts, http) => { | ||
try { | ||
@@ -31,4 +31,10 @@ const res = await (0, cross_fetch_1.default)(generateDiscoverUrl(discoveryUrl, opts)); | ||
const [hubUrl, accessKey] = hubs[~~(Math.random() * hubs.length)]; | ||
const protocol = /^https/.test(discoveryUrl) ? 'wss' : 'ws'; | ||
return `${protocol}://${hubUrl}/${accessKey}`; | ||
if (http) { | ||
const protocol = /^https/.test(discoveryUrl) ? 'https' : 'http'; | ||
return `${protocol}://${hubUrl}`; | ||
} | ||
else { | ||
const protocol = /^https/.test(discoveryUrl) ? 'wss' : 'ws'; | ||
return `${protocol}://${hubUrl}/${accessKey}`; | ||
} | ||
} | ||
@@ -44,3 +50,4 @@ else { | ||
}; | ||
const discoverService = async (opts, tries = 0) => { | ||
const isWsRe = /^ws/; | ||
const discoverService = async (opts, http = false, tries = 0) => { | ||
if (opts.url) { | ||
@@ -50,2 +57,5 @@ if (typeof opts.url === 'function') { | ||
} | ||
if (http && isWsRe.test(opts.url)) { | ||
return opts.url.replace(isWsRe, 'http'); | ||
} | ||
return opts.url; | ||
@@ -61,6 +71,9 @@ } | ||
const discoveryUrl = discoveryUrls[i]; | ||
const url = await Promise.race([request(discoveryUrl, opts), (0, utils_1.wait)(3e3)]); | ||
const url = await Promise.race([ | ||
request(discoveryUrl, opts, http), | ||
(0, utils_1.wait)(3e3), | ||
]); | ||
if (url === 1 || (!url && i === discoveryUrls.length - 1)) { | ||
await (0, utils_1.wait)(Math.min(tries * tries * 50 + 500, 5e3)); | ||
return discoverService(opts, ++tries); | ||
return discoverService(opts, http, ++tries); | ||
} | ||
@@ -67,0 +80,0 @@ else if (url) { |
{ | ||
"name": "@based/opts", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "public": true, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
9438
128