@based/client
Advanced tools
Comparing version 6.4.0 to 6.5.0
@@ -36,2 +36,5 @@ import { addGetToQueue } from '../outgoing/index.js'; | ||
export const parseArrayBuffer = async (d) => { | ||
if (d instanceof Uint8Array) { | ||
return d; | ||
} | ||
// needed for CF workers which return array buffers | ||
@@ -38,0 +41,0 @@ if (d instanceof ArrayBuffer) { |
@@ -11,2 +11,3 @@ import { BasedOpts, AuthState, FunctionResponseListeners, Settings, FunctionQueue, ObserveState, ObserveQueue, Cache, GetObserveQueue, GetState, ChannelQueue, ChannelPublishQueue, ChannelState, CallOptions, QueryOptions } from './types/index.js'; | ||
constructor(opts?: BasedOpts, settings?: Settings); | ||
restFallBack?: Settings['restFallBack']; | ||
storageSize: number; | ||
@@ -13,0 +14,0 @@ maxStorageSize: number; |
@@ -11,2 +11,3 @@ import { BasedOpts, AuthState, FunctionResponseListeners, Settings, FunctionQueue, ObserveState, ObserveQueue, Cache, GetObserveQueue, GetState, ChannelQueue, ChannelPublishQueue, ChannelState, CallOptions, QueryOptions } from './types/index.js'; | ||
constructor(opts?: BasedOpts, settings?: Settings); | ||
restFallBack?: Settings['restFallBack']; | ||
storageSize: number; | ||
@@ -13,0 +14,0 @@ maxStorageSize: number; |
@@ -28,2 +28,5 @@ import connectWebsocket from './websocket/index.js'; | ||
} | ||
if (settings?.restFallBack) { | ||
this.restFallBack = settings.restFallBack; | ||
} | ||
if (opts) { | ||
@@ -33,2 +36,3 @@ this.connect(opts); | ||
} | ||
restFallBack; | ||
// --------- Persistent Storage | ||
@@ -35,0 +39,0 @@ storageSize = 0; |
@@ -22,2 +22,5 @@ export type GenericObject = { | ||
persistentStorage?: string; | ||
restFallBack?: { | ||
pollInverval?: number; | ||
}; | ||
}; |
import urlLoader from './urlLoader.js'; | ||
import { encodeAuthState } from '../authState/parseAuthState.js'; | ||
import { isStreaming } from '../stream/index.js'; | ||
import fetch from '@based/fetch'; | ||
import WebSocket from 'isomorphic-ws'; | ||
import { FakeWebsocket } from './FakeWebsocket.js'; | ||
const activityListeners = new Map(); | ||
let activeTimer; | ||
// Disconnect in the browser when a window is inactive (on the background) for 30 seconds | ||
if (typeof document !== 'undefined') { | ||
@@ -26,2 +27,3 @@ let putToOffline = false; | ||
}); | ||
// Disconnect in the browser when a window is inactive (on the background) for 30 seconds | ||
document.addEventListener('visibilitychange', function () { | ||
@@ -43,2 +45,33 @@ clearTimeout(activeTimer); | ||
} | ||
// remove the logs here | ||
const restPing = (ms = 500, realUrl, connection, fallback) => { | ||
connection.fallBackTimer = setTimeout(() => { | ||
if (!connection.disconnected) { | ||
console.warn(`Cannot connect to ws in ${ms}ms`); | ||
let d = Date.now(); | ||
connection.fallBackInProgress = true; | ||
const url = `${realUrl.replace(/^ws/, 'http')}/based:rpstatus`; | ||
fetch(url).then(async (r) => { | ||
if (connection.fallBackInProgress) { | ||
connection.fallBackInProgress = false; | ||
const t = await r.text(); | ||
if (t && t[0] === '1') { | ||
const timeEllapsed = Date.now() - d; | ||
console.warn(`Took ${timeEllapsed}ms for rest`); | ||
if (timeEllapsed < ms) { | ||
console.warn(`was able to connect to rpstatus within ${ms}ms need to fallback to rest`); | ||
fallback(t); | ||
} | ||
else { | ||
restPing(timeEllapsed + 100, realUrl, connection, fallback); | ||
} | ||
} | ||
} | ||
else { | ||
console.warn('Connected while trying RP - skip'); | ||
} | ||
}); | ||
} | ||
}, ms); | ||
}; | ||
const connect = (client, url, connection = { | ||
@@ -61,3 +94,2 @@ destroy: () => { | ||
ws.close(); | ||
// add online listener as well | ||
} | ||
@@ -88,8 +120,17 @@ else if (!active && isActive) { | ||
}); | ||
const ws = (connection.ws = new WebSocket(realUrl, [ | ||
encodeAuthState(client.authState), | ||
])); | ||
const ws = (connection.ws = connection.useFallback | ||
? new FakeWebsocket(realUrl, connection.useFallback, client) | ||
: new WebSocket(realUrl, [encodeAuthState(client.authState)])); | ||
ws.binaryType = 'blob'; | ||
let isError = false; | ||
ws.binaryType = 'blob'; | ||
if (!connection.useFallback && client.restFallBack) { | ||
restPing(300, realUrl, connection, (t) => { | ||
connection.useFallback = t; | ||
ws.close(); | ||
}); | ||
} | ||
ws.addEventListener('error', (err) => { | ||
clearTimeout(connection.fallBackTimer); | ||
// maybe this is a bad idea | ||
connection.fallBackInProgress = false; | ||
// TODO: add a websocket close number | ||
@@ -105,2 +146,4 @@ // also for rateLimit | ||
ws.addEventListener('open', () => { | ||
clearTimeout(connection.fallBackTimer); | ||
connection.fallBackInProgress = false; | ||
if (isActive) { | ||
@@ -118,2 +161,4 @@ if (connection.disconnected) { | ||
ws.addEventListener('close', () => { | ||
clearTimeout(connection.fallBackTimer); | ||
connection.fallBackInProgress = false; | ||
if (isActive) { | ||
@@ -120,0 +165,0 @@ if (connection.disconnected) { |
@@ -6,2 +6,5 @@ import WebSocket from 'isomorphic-ws'; | ||
destroy: () => void; | ||
fallBackTimer?: ReturnType<typeof setTimeout>; | ||
fallBackInProgress?: boolean; | ||
useFallback?: string; | ||
} |
@@ -5,3 +5,6 @@ export class Connection { | ||
destroy; | ||
fallBackTimer; | ||
fallBackInProgress; | ||
useFallback; | ||
} | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@based/client", | ||
"version": "6.4.0", | ||
"version": "6.5.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
Network access
Supply chain riskThis module accesses the network.
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
213252
109
3406
2