@based/client
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -14,12 +14,15 @@ "use strict"; | ||
]; | ||
for (const hubDiscoveryUrl of discoveryUrls) { | ||
const dUrl = `${hubDiscoveryUrl}/status?hub=${opts.name || '@based/env-hub'}&key=${opts.key}`; | ||
const res = await (0, cross_fetch_1.default)(dUrl); | ||
if (res.ok) { | ||
const hubs = (await res.json()); | ||
const [hubUrl] = hubs[~~(Math.random() * hubs.length)]; | ||
return `ws://${hubUrl}`; | ||
while (true) { | ||
for (const hubDiscoveryUrl of discoveryUrls) { | ||
const dUrl = `${hubDiscoveryUrl}/status?hub=${opts.name || '@based/env-hub'}&key=${opts.key}`; | ||
const res = await (0, cross_fetch_1.default)(dUrl).catch(() => ({ ok: false })); | ||
if (res.ok) { | ||
const hubs = (await res.json()); | ||
const [hubUrl] = hubs[~~(Math.random() * hubs.length)]; | ||
return hubUrl; | ||
} | ||
} | ||
await new Promise((resolve) => setTimeout(resolve, 5e3 + ~~(Math.random() * 1e3))); | ||
} | ||
}; | ||
//# sourceMappingURL=getUrlFromOpts.js.map |
@@ -10,2 +10,3 @@ import { BasedOpts, AuthState, FunctionResponseListeners, Settings, FunctionQueue, ObserveState, ObserveQueue, Cache, GetObserveQueue } from './types'; | ||
export * from './authState/parseAuthState'; | ||
export * from './types/error'; | ||
export { AuthState, BasedQuery }; | ||
@@ -22,3 +23,3 @@ export declare class BasedClient extends Emitter { | ||
connection: Connection; | ||
url: string | (() => Promise<string>); | ||
url: () => Promise<string>; | ||
outgoingStreams: Map<string, { | ||
@@ -25,0 +26,0 @@ stream: any; |
@@ -23,3 +23,2 @@ "use strict"; | ||
const Emitter_1 = __importDefault(require("./Emitter")); | ||
const getUrlFromOpts_1 = __importDefault(require("./getUrlFromOpts")); | ||
const outgoing_1 = require("./outgoing"); | ||
@@ -33,3 +32,5 @@ const incoming_1 = require("./incoming"); | ||
const hash_1 = require("@saulx/hash"); | ||
const opts_1 = __importDefault(require("@based/opts")); | ||
__exportStar(require("./authState/parseAuthState"), exports); | ||
__exportStar(require("./types/error"), exports); | ||
class BasedClient extends Emitter_1.default { | ||
@@ -145,3 +146,3 @@ constructor(opts, settings) { | ||
this.opts = opts; | ||
this.url = await (0, getUrlFromOpts_1.default)(opts); | ||
this.url = () => (0, opts_1.default)(opts); | ||
} | ||
@@ -148,0 +149,0 @@ if (!this.opts) { |
@@ -8,4 +8,4 @@ "use strict"; | ||
const cross_fetch_1 = __importDefault(require("cross-fetch")); | ||
const getUrlFromOpts_1 = __importDefault(require("../getUrlFromOpts")); | ||
const utils_1 = require("@saulx/utils"); | ||
const opts_1 = __importDefault(require("@based/opts")); | ||
exports.default = async (client, name, options) => { | ||
@@ -15,7 +15,3 @@ if (!client.connected) { | ||
} | ||
// key is something special | ||
let url = await (0, getUrlFromOpts_1.default)(client.opts); | ||
if (typeof url === 'function') { | ||
url = await url(); | ||
} | ||
const url = await (0, opts_1.default)(client.opts, true); | ||
const headers = { | ||
@@ -32,3 +28,3 @@ 'Content-Type': options.mimeType || 'text/plain', | ||
} | ||
const result = await (0, cross_fetch_1.default)(url.replace(/^ws/, 'http') + '/' + name + q, { | ||
const result = await (0, cross_fetch_1.default)(url + '/' + name + q, { | ||
method: 'POST', | ||
@@ -35,0 +31,0 @@ cache: 'no-cache', |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { Readable } from 'stream'; | ||
@@ -3,0 +2,0 @@ import { StreamFunctionPath, StreamFunctionStream } from './types'; |
@@ -13,3 +13,3 @@ "use strict"; | ||
const __1 = require(".."); | ||
const getUrlFromOpts_1 = __importDefault(require("../getUrlFromOpts")); | ||
const opts_1 = __importDefault(require("@based/opts")); | ||
const error_1 = require("../types/error"); | ||
@@ -96,6 +96,3 @@ const utils_1 = require("@saulx/utils"); | ||
// key is something special | ||
let url = await (0, getUrlFromOpts_1.default)(client.opts); | ||
if (typeof url === 'function') { | ||
url = await url(); | ||
} | ||
const url = await (0, opts_1.default)(client.opts, true); | ||
const headers = { | ||
@@ -102,0 +99,0 @@ 'Content-Length': String(options.size), |
@@ -7,13 +7,6 @@ "use strict"; | ||
const __1 = require(".."); | ||
const getUrlFromOpts_1 = __importDefault(require("../getUrlFromOpts")); | ||
const opts_1 = __importDefault(require("@based/opts")); | ||
const error_1 = require("../types/error"); | ||
const inProgress = {}; | ||
const queue = {}; | ||
const getUrl = async (client) => { | ||
let url = await (0, getUrlFromOpts_1.default)(client.opts); | ||
if (typeof url === 'function') { | ||
url = await url(); | ||
} | ||
return url.replace(/^ws/, 'http'); | ||
}; | ||
const reject = (err, q) => { | ||
@@ -29,3 +22,3 @@ q.forEach((item) => { | ||
inProgress[functionName] = false; | ||
const url = await getUrl(client); | ||
const url = await (0, opts_1.default)(client.opts, true); | ||
const q = queue[functionName]; | ||
@@ -32,0 +25,0 @@ queue[functionName] = []; |
@@ -21,3 +21,4 @@ export declare enum BasedErrorCode { | ||
RateLimit = 40029, | ||
MissingAuthStateProtocolHeader = 40030 | ||
MissingAuthStateProtocolHeader = 40030, | ||
IncorrectAccessKey = 40031 | ||
} | ||
@@ -24,0 +25,0 @@ export type BasedErrorData = { |
@@ -26,2 +26,3 @@ "use strict"; | ||
BasedErrorCode[BasedErrorCode["MissingAuthStateProtocolHeader"] = 40030] = "MissingAuthStateProtocolHeader"; | ||
BasedErrorCode[BasedErrorCode["IncorrectAccessKey"] = 40031] = "IncorrectAccessKey"; | ||
})(BasedErrorCode = exports.BasedErrorCode || (exports.BasedErrorCode = {})); | ||
@@ -28,0 +29,0 @@ class BasedError extends Error { |
export type GenericObject = { | ||
[key: string]: any; | ||
}; | ||
export type BasedUrlFn = () => Promise<string>; | ||
export type BasedOpts = { | ||
@@ -11,3 +12,4 @@ env?: string; | ||
key?: string; | ||
url?: string | (() => Promise<string>); | ||
optionalKey?: boolean; | ||
url?: string | BasedUrlFn; | ||
discoveryUrls?: string[]; | ||
@@ -14,0 +16,0 @@ params?: { |
@@ -57,4 +57,7 @@ "use strict"; | ||
])); | ||
ws.onerror = () => { | ||
// console.error() | ||
let isError = false; | ||
ws.onerror = (err) => { | ||
if (err.message && err.message.includes('401')) { | ||
isError = true; | ||
} | ||
}; | ||
@@ -84,3 +87,5 @@ ws.onmessage = (d) => { | ||
// relatively low backoff but will make it faster if multiple servers are down | ||
Math.min(2000, Math.min(time + ~~(Math.random() * 500) + 100)), true); | ||
isError | ||
? 10e3 | ||
: Math.min(2000, Math.min(time + ~~(Math.random() * 500) + 100)), true); | ||
} | ||
@@ -87,0 +92,0 @@ }; |
{ | ||
"name": "@based/client", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"license": "MIT", | ||
@@ -27,2 +27,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@based/opts": "0.0.5", | ||
"@saulx/diff": "^1.1.4", | ||
@@ -29,0 +30,0 @@ "@saulx/hash": "^2.0.0", |
@@ -44,12 +44,12 @@ # @based/client | ||
const unsubscribe = client | ||
.query('db', { $id: 'fwe2233', title: true }, { persistent: true}) | ||
.query('db', { $id: 'fwe2233', title: true }, { persistent: true }) | ||
.subscribe((data) => console.log(data)) | ||
// Channels are stateless streams | ||
const unsubscribeChannel = client.channel('events', { type: 'page-view }) | ||
const unsubscribeChannel = client.channel('events', { type: 'page-view' }) | ||
.subscribe(event => console.log(event)) | ||
client | ||
.channel('events', { type: 'page-view }) | ||
.channel('events', { type: 'page-view' }) | ||
.publish({ id: 'mypage' }) | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
194819
2
8
2950
+ Added@based/opts@0.0.5
+ Added@based/opts@0.0.5(transitive)
+ Added@saulx/utils@3.2.1(transitive)