Skip to content

Commit

Permalink
Merge pull request #154 from idexio/feature/subscriptions-multiwallet…
Browse files Browse the repository at this point in the history
…-update

fix(websocket): remove token manager and no websocket auth token re-use
  • Loading branch information
jurosh committed Sep 15, 2020
2 parents 770965e + e53bbf2 commit 6437744
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 122 deletions.
40 changes: 17 additions & 23 deletions src/client/webSocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { isNode } from '../../utils';
import { isWebSocketAuthenticatedSubscription } from '../../types';

import { transformMessage } from './transform';
import WebsocketTokenManager, {
removeWalletFromSdkSubscription,
} from './tokenManager';
import { removeWalletFromSdkSubscription } from './utils';

export type WebSocketListenerConnect = () => unknown;

Expand Down Expand Up @@ -90,7 +88,7 @@ export class WebSocketClient {

private webSocket: null | WebSocket = null;

private webSocketTokenManager?: WebsocketTokenManager;
private websocketAuthTokenFetch?: WebSocketClientOptions['websocketAuthTokenFetch'];

private pathSubscription: string | null = null;

Expand Down Expand Up @@ -131,11 +129,7 @@ export class WebSocketClient {
this.connectTimeout = options.connectTimeout;
}

if (options.websocketAuthTokenFetch) {
this.webSocketTokenManager = new WebsocketTokenManager(
options.websocketAuthTokenFetch,
);
}
this.websocketAuthTokenFetch = options.websocketAuthTokenFetch;
}

/* Connection management */
Expand Down Expand Up @@ -302,10 +296,10 @@ export class WebSocketClient {
});
}

const { webSocketTokenManager } = this;
const { websocketAuthTokenFetch } = this;

// For authenticated, we do require token manager
if (!webSocketTokenManager) {
if (!websocketAuthTokenFetch) {
throw new Error(
'WebSocket: `websocketAuthTokenFetch` is required for authenticated subscriptions',
);
Expand All @@ -326,19 +320,14 @@ export class WebSocketClient {
);
}

// Prepare (fetch) all authentication tokens for subscriptions
await Promise.all(
uniqueWallets.map((wallet) => webSocketTokenManager.getToken(wallet)),
);

// For single wallet, send all subscriptions at once (also unauthenticated)
if (uniqueWallets.length === 1) {
return this.sendMessage({
cid,
method: 'subscribe',
markets,
subscriptions: subscriptions.map(removeWalletFromSdkSubscription),
token: webSocketTokenManager.getLastCachedToken(uniqueWallets[0]),
token: await websocketAuthTokenFetch(uniqueWallets[0]),
});
}

Expand All @@ -356,16 +345,21 @@ export class WebSocketClient {
});
}

// Send multiple wallets subscriptions
authSubscriptions.forEach((authSubscription) => {
// Now prepare all auth tokens, so we can subscribe all authenticated at "once"
const preparedTokensByWalletIndex = await Promise.all(
uniqueWallets.map((wallet) => websocketAuthTokenFetch(wallet)),
);

// Send multiple wallets subscriptions grouped by wallet
uniqueWallets.forEach((wallet, walletIndex) => {
this.sendMessage({
cid,
method: 'subscribe',
markets,
subscriptions: [removeWalletFromSdkSubscription(authSubscription)],
token: webSocketTokenManager.getLastCachedToken(
authSubscription.wallet,
),
subscriptions: authSubscriptions
.filter((item) => item.wallet === wallet)
.map(removeWalletFromSdkSubscription),
token: preparedTokensByWalletIndex[walletIndex],
});
});

Expand Down
99 changes: 0 additions & 99 deletions src/client/webSocket/tokenManager.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/client/webSocket/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
WebSocketRequestSubscription,
WebSocketRequestUnauthenticatedSubscription,
} from '../../types';

/*
* Wallet is used only to generate user's wallet auth token
* After we got token, we don't want to send wallet to the server
*/
export const removeWalletFromSdkSubscription = (
subscription:
| WebSocketRequestUnauthenticatedSubscription['name']
| (WebSocketRequestSubscription & { wallet?: string }),
):
| WebSocketRequestUnauthenticatedSubscription['name']
| WebSocketRequestSubscription => {
if (typeof subscription === 'string') {
return subscription;
}
const subscriptionWithoutWallet = { ...subscription };
if (subscriptionWithoutWallet.wallet) {
delete subscriptionWithoutWallet.wallet;
}
return subscriptionWithoutWallet;
};

0 comments on commit 6437744

Please sign in to comment.