Skip to content

Commit

Permalink
Minimal code changes (TS stuff)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Feb 14, 2025
1 parent 88d6b8f commit d75f6e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* global process */

import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import https from 'https';
import https from 'node:https';
import WebSocket from 'isomorphic-ws';

import { Channel } from './channel';
Expand Down Expand Up @@ -1472,10 +1472,10 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
return await this.wsConnection.connect(
this.options.enableWSFallback ? this.defaultWSTimeoutWithFallback : this.defaultWSTimeout,
);
} catch (err) {
} catch (error: any) {
// run fallback only if it's WS/Network error and not a normal API error
// make sure browser is online before even trying the longpoll
if (this.options.enableWSFallback && isWSFailure(err) && isOnline()) {
if (this.options.enableWSFallback && isWSFailure(error) && isOnline()) {
this.logger('info', 'client:connect() - WS failed, fallback to longpoll', { tags: ['connection', 'client'] });
this.dispatchEvent({ type: 'transport.changed', mode: 'longpoll' });

Expand All @@ -1487,7 +1487,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
return await this.wsFallback.connect();
}

throw err;
throw error;
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
this.consecutiveFailures = 0;

this._log(`connect() - Established ws connection with healthcheck: ${healthCheck}`);
} catch (error) {
} catch (error: any) {
this.isHealthy = false;
this.consecutiveFailures += 1;

Expand Down Expand Up @@ -148,7 +148,7 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
for (let i = 0; i <= timeout; i += interval) {
try {
return await this.connectionOpen;
} catch (error) {
} catch (error: any) {
if (i === timeout) {
throw new Error(
JSON.stringify({
Expand Down Expand Up @@ -298,17 +298,20 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
}
return response;
}
} catch (err) {
} catch (error: any) {
this.isConnecting = false;
this._log(`_connect() - Error - `, err);
this._log(`_connect() - Error - `, error);
if (this.client.options.enableInsights) {
this.client.insightMetrics.wsConsecutiveFailures++;
this.client.insightMetrics.wsTotalFailures++;

const insights = buildWsFatalInsight((this as unknown) as StableWSConnection, convertErrorToJson(err as Error));
const insights = buildWsFatalInsight(
(this as unknown) as StableWSConnection,
convertErrorToJson(error as Error),
);
postInsights?.('ws_fatal', insights);
}
throw err;
throw error;
}
}

Expand Down Expand Up @@ -366,7 +369,7 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
this._log('_reconnect() - Finished recoverCallBack');

this.consecutiveFailures = 0;
} catch (error) {
} catch (error: any) {
this.isHealthy = false;
this.consecutiveFailures += 1;
if (error.code === chatCodes.TOKEN_EXPIRED && !this.client.tokenManager.isStatic()) {
Expand Down
14 changes: 7 additions & 7 deletions src/connection_fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ export class WSConnectionFallback<StreamChatGenerics extends ExtendableGenerics

this.consecutiveFailures = 0; // always reset in case of no error
return res;
} catch (err) {
} catch (error: any) {
this.consecutiveFailures += 1;

if (retry && isErrorRetryable(err)) {
if (retry && isErrorRetryable(error)) {
this._log(`_req() - Retryable error, retrying request`);
await sleep(retryInterval(this.consecutiveFailures));
return this._req<T>(params, config, retry);
}

throw err;
throw error;
}
};

Expand All @@ -107,22 +107,22 @@ export class WSConnectionFallback<StreamChatGenerics extends ExtendableGenerics
this.client.dispatchEvent(data.events[i]);
}
}
} catch (err) {
if (axios.isCancel(err)) {
} catch (error: any) {
if (axios.isCancel(error)) {
this._log(`_poll() - axios canceled request`);
return;
}

/** client.doAxiosRequest will take care of TOKEN_EXPIRED error */

if (isConnectionIDError(err)) {
if (isConnectionIDError(error)) {
this._log(`_poll() - ConnectionID error, connecting without ID...`);
this._setState(ConnectionState.Disconnected);
this.connect(true);
return;
}

if (isAPIError(err) && !isErrorRetryable(err)) {
if (isAPIError(error) && !isErrorRetryable(error)) {
this._setState(ConnectionState.Closed);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/signing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jwt, { Secret, SignOptions } from 'jsonwebtoken';
import crypto from 'crypto';
import crypto from 'node:crypto';
import { encodeBase64, decodeBase64 } from './base64';
import { UR } from './types';

Expand Down

0 comments on commit d75f6e6

Please sign in to comment.