From 1d78774fdf49e2bc1caf7497886f31d08b7870d8 Mon Sep 17 00:00:00 2001 From: Dylan Jeffers Date: Tue, 4 Apr 2023 19:13:30 -0700 Subject: [PATCH] [PLAT-814] Use sdk discovery-node-selection in libs (#3125) --- packages/common/package-lock.json | 6 +- packages/common/package.json | 2 +- .../audius-api-client/AudiusAPIClient.ts | 10 +- .../services/audius-backend/AudiusBackend.ts | 61 +++++++---- .../DiscoveryNodeSelectorInstance.ts | 89 ++++++++++++++++ .../services/discovery-node-selector/index.ts | 1 + packages/common/src/services/index.ts | 1 + .../services/remote-config/feature-flags.ts | 6 +- .../common/src/store/pages/chat/middleware.ts | 4 +- packages/common/src/store/storeContext.ts | 4 +- packages/mobile/package-lock.json | 100 +++++++++--------- packages/mobile/package.json | 2 +- .../services/audius-api-client/apiClient.ts | 5 +- .../src/services/audius-backend-instance.ts | 2 + packages/mobile/src/services/audius-sdk.ts | 56 ++-------- .../src/services/discovery-node-selector.ts | 9 ++ packages/web/package-lock.json | 6 +- packages/web/package.json | 2 +- .../services/audius-api-client/apiClient.ts | 4 +- .../audius-backend/audius-backend-instance.ts | 2 + .../web/src/services/audius-sdk/audiusSdk.ts | 55 +--------- .../src/services/discovery-node-selector.ts | 9 ++ 22 files changed, 242 insertions(+), 194 deletions(-) create mode 100644 packages/common/src/services/discovery-node-selector/DiscoveryNodeSelectorInstance.ts create mode 100644 packages/common/src/services/discovery-node-selector/index.ts create mode 100644 packages/mobile/src/services/discovery-node-selector.ts create mode 100644 packages/web/src/services/discovery-node-selector.ts diff --git a/packages/common/package-lock.json b/packages/common/package-lock.json index 617e3595fcb..2405d96c1fe 100644 --- a/packages/common/package-lock.json +++ b/packages/common/package-lock.json @@ -33,9 +33,9 @@ } }, "@audius/sdk": { - "version": "2.0.3-beta.4", - "resolved": "https://registry.npmjs.org/@audius/sdk/-/sdk-2.0.3-beta.4.tgz", - "integrity": "sha512-MgQKw7r+PbOjb7MdgF/XGYZUd6M19O7IUADxjUXirRUs+s90PcsrTAOxJb4roAsRN/aq5TFwk9EziJ3hWYchIg==", + "version": "2.0.3-beta.5", + "resolved": "https://registry.npmjs.org/@audius/sdk/-/sdk-2.0.3-beta.5.tgz", + "integrity": "sha512-bDD82p7Lrehd2E3Ghv2uEv6Ep11FCGxXjlvKBNT7aoMN+O0FYkvoaLbBWh5cjJBeOaKm9+7GBW1Uqaj7B9tz7w==", "requires": { "@audius/hedgehog": "2.1.0", "@babel/runtime": "7.18.3", diff --git a/packages/common/package.json b/packages/common/package.json index f4b1e953a91..2a78f6d54e5 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -28,7 +28,7 @@ "url": "https://github.com/AudiusProject/audius-client/issues" }, "dependencies": { - "@audius/sdk": "2.0.3-beta.4", + "@audius/sdk": "2.0.3-beta.5", "@fingerprintjs/fingerprintjs-pro": "3.5.6", "@metaplex-foundation/mpl-token-metadata": "2.5.2", "@optimizely/optimizely-sdk": "4.0.0", diff --git a/packages/common/src/services/audius-api-client/AudiusAPIClient.ts b/packages/common/src/services/audius-api-client/AudiusAPIClient.ts index e4042092a0f..109c37fa90b 100644 --- a/packages/common/src/services/audius-api-client/AudiusAPIClient.ts +++ b/packages/common/src/services/audius-api-client/AudiusAPIClient.ts @@ -502,6 +502,7 @@ type AudiusAPIClientConfig = { remoteConfigInstance: RemoteConfigInstance localStorage: LocalStorage env: Env + waitForLibsInit: () => Promise } export class AudiusAPIClient { @@ -516,6 +517,7 @@ export class AudiusAPIClient { localStorage: LocalStorage env: Env isReachable?: boolean = true + waitForLibsInit: () => Promise constructor({ audiusBackendInstance, @@ -523,7 +525,8 @@ export class AudiusAPIClient { overrideEndpoint, remoteConfigInstance, localStorage, - env + env, + waitForLibsInit }: AudiusAPIClientConfig) { this.audiusBackendInstance = audiusBackendInstance this.getAudiusLibs = getAudiusLibs @@ -531,6 +534,7 @@ export class AudiusAPIClient { this.remoteConfigInstance = remoteConfigInstance this.localStorage = localStorage this.env = env + this.waitForLibsInit = waitForLibsInit } setIsReachable(isReachable: boolean) { @@ -1870,9 +1874,7 @@ export class AudiusAPIClient { // Something went wrong with the request and we should wait for the libs // initialization state if needed before retrying if (this.initializationState.type === 'manual') { - // use wait for libs init from audius backend instance - // BEEP - // await waitForLibsInit() + await this.waitForLibsInit() } return this._getResponse(path, sanitizedParams, retry, pathType) } diff --git a/packages/common/src/services/audius-backend/AudiusBackend.ts b/packages/common/src/services/audius-backend/AudiusBackend.ts index 1caac064c24..f3179fa5665 100644 --- a/packages/common/src/services/audius-backend/AudiusBackend.ts +++ b/packages/common/src/services/audius-backend/AudiusBackend.ts @@ -1,3 +1,4 @@ +import type { DiscoveryNodeSelector } from '@audius/sdk' import { DiscoveryAPI } from '@audius/sdk/dist/core' import type { HedgehogConfig } from '@audius/sdk/dist/services/hedgehog' import type { LocalStorage } from '@audius/sdk/dist/utils/localStorage' @@ -66,7 +67,6 @@ import { } from '../../store' import { CIDCache } from '../../store/cache/CIDCache' import { - Nullable as totalUnviewed, getErrorMessage, uuid, Maybe, @@ -76,6 +76,7 @@ import { Nullable, removeNullable } from '../../utils' +import type { DiscoveryNodeSelectorInstance } from '../discovery-node-selector' import { MonitoringCallbacks } from './types' @@ -179,7 +180,7 @@ type TransactionReceipt = { blockHash: string; blockNumber: number } let preloadImageTimer: Timer const avoidGC: HTMLImageElement[] = [] -type DiscoveryProviderListener = (endpoint: totalUnviewed) => void +type DiscoveryProviderListener = (endpoint: Nullable) => void type AudiusBackendSolanaConfig = Partial<{ claimableTokenPda: string @@ -222,11 +223,12 @@ type AudiusBackendParams = { ethProviderUrls: Maybe ethRegistryAddress: Maybe ethTokenAddress: Maybe + discoveryNodeSelectorInstance: DiscoveryNodeSelectorInstance getFeatureEnabled: ( flag: FeatureFlags, fallbackFlag?: FeatureFlags ) => Promise | null | boolean - getHostUrl: () => totalUnviewed + getHostUrl: () => Nullable getLibs: () => Promise getWeb3Config: ( libs: any, @@ -278,6 +280,7 @@ export const audiusBackend = ({ ethProviderUrls, ethRegistryAddress, ethTokenAddress, + discoveryNodeSelectorInstance, getFeatureEnabled, getHostUrl, getLibs, @@ -327,7 +330,7 @@ export const audiusBackend = ({ }: AudiusBackendParams) => { const { getRemoteVar, waitForRemoteConfig } = remoteConfigInstance - const currentDiscoveryProvider: totalUnviewed = null + const currentDiscoveryProvider: Nullable = null const didSelectDiscoveryProviderListeners: DiscoveryProviderListener[] = [] /** @@ -355,7 +358,7 @@ export const audiusBackend = ({ } } - function getCreatorNodeIPFSGateways(endpoint: totalUnviewed) { + function getCreatorNodeIPFSGateways(endpoint: Nullable) { if (endpoint) { return endpoint .split(',') @@ -429,7 +432,7 @@ export const audiusBackend = ({ creatorNodeGateways = [] as string[], cache = true, asUrl = true, - trackId: totalUnviewed = null + trackId: Nullable = null ) { await waitForLibsInit() try { @@ -520,8 +523,8 @@ export const audiusBackend = ({ } async function getImageUrl( - cid: totalUnviewed, - size: totalUnviewed, + cid: Nullable, + size: Nullable, gateways: string[] ) { if (!cid) return '' @@ -668,7 +671,7 @@ export const audiusBackend = ({ SolanaUtils = libsModule.SolanaUtils RewardsAttester = libsModule.RewardsAttester // initialize libs - let libsError: totalUnviewed = null + let libsError: Nullable = null const { web3Config } = await getWeb3Config( AudiusLibs, registryAddress, @@ -687,6 +690,21 @@ export const audiusBackend = ({ StringKeys.DISCOVERY_NODE_BLOCK_LIST ) + const useSdkDiscoveryNodeSelector = await getFeatureEnabled( + FeatureFlags.SDK_V2 + ) + + let discoveryNodeSelector: Maybe + + if (useSdkDiscoveryNodeSelector) { + discoveryNodeSelector = + await discoveryNodeSelectorInstance.getDiscoveryNodeSelector() + + discoveryNodeSelector.addEventListener('change', (endpoint) => { + discoveryProviderSelectionCallback(endpoint, []) + }) + } + try { audiusLibs = new AudiusLibs({ localStorage, @@ -712,9 +730,10 @@ export const audiusBackend = ({ ) ? getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_SLOT_DIFF_PLAYS) : null, - unhealthyBlockDiff: getRemoteVar( - IntKeys.DISCOVERY_NODE_MAX_BLOCK_DIFF - ) + unhealthyBlockDiff: + getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_BLOCK_DIFF) ?? undefined, + + discoveryNodeSelector }, identityServiceConfig: AudiusLibs.configIdentityService(identityServiceUrl), @@ -977,8 +996,8 @@ export const audiusBackend = ({ DiscoveryAPIParams, 'sort' | 'filterDeleted' > & { - sort: totalUnviewed - filterDeleted: totalUnviewed + sort: Nullable + filterDeleted: Nullable }) { try { const tracks = await withEagerOption( @@ -1547,8 +1566,8 @@ export const audiusBackend = ({ } async function getPlaylists( - userId: totalUnviewed, - playlistIds: totalUnviewed, + userId: Nullable, + playlistIds: Nullable, withUsers = true ): Promise { try { @@ -1890,8 +1909,8 @@ export const audiusBackend = ({ coverPhoto: File } hasWallet: boolean - referrer: totalUnviewed - feePayerOverride: totalUnviewed + referrer: Nullable + feePayerOverride: Nullable }) { await waitForLibsInit() const metadata = schemas.newUserMetadata() @@ -2458,13 +2477,12 @@ export const audiusBackend = ({ }) { await waitForLibsInit() const account = audiusLibs.Account.getCurrentUser() - if (!account) { + if (!account) return { message: 'error', error: new Error('User not signed in'), isRequestError: false } - } const encodedUserId = encodeHashId(account.user_id) type DiscoveryNotificationsResponse = { @@ -2491,7 +2509,6 @@ export const audiusBackend = ({ const { unread_count, notifications } = response return { - message: 'success', totalUnviewed: unread_count, notifications: notifications.map( mapDiscoveryNotification @@ -3483,7 +3500,7 @@ export const audiusBackend = ({ endpoints: string[] AAOEndpoint: string parallelization: number - feePayerOverride: totalUnviewed + feePayerOverride: Nullable isFinalAttempt: boolean }) { await waitForLibsInit() diff --git a/packages/common/src/services/discovery-node-selector/DiscoveryNodeSelectorInstance.ts b/packages/common/src/services/discovery-node-selector/DiscoveryNodeSelectorInstance.ts new file mode 100644 index 00000000000..aa59100b4a0 --- /dev/null +++ b/packages/common/src/services/discovery-node-selector/DiscoveryNodeSelectorInstance.ts @@ -0,0 +1,89 @@ +import { + developmentConfig, + DiscoveryNodeSelector, + productionConfig, + stagingConfig +} from '@audius/sdk' + +import { Env } from '../env' +import { + BooleanKeys, + IntKeys, + RemoteConfigInstance, + StringKeys +} from '../remote-config' + +type DiscoveryNodeSelectorConfig = { + env: Env + remoteConfigInstance: RemoteConfigInstance +} + +export class DiscoveryNodeSelectorInstance { + private env: Env + private remoteConfigInstance: RemoteConfigInstance + private discoveryNodeSelectorPromise: Promise | null + + constructor(config: DiscoveryNodeSelectorConfig) { + const { env, remoteConfigInstance } = config + this.env = env + this.remoteConfigInstance = remoteConfigInstance + this.discoveryNodeSelectorPromise = null + } + + private async makeDiscoveryNodeSelector() { + const { getRemoteVar, waitForRemoteConfig } = this.remoteConfigInstance + + await waitForRemoteConfig() + + const bootstrapConfig = + this.env.ENVIRONMENT === 'development' + ? developmentConfig + : this.env.ENVIRONMENT === 'staging' + ? stagingConfig + : productionConfig + + const { minVersion, discoveryNodes } = bootstrapConfig + + const maxBlockDiff = + getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_BLOCK_DIFF) ?? undefined + const maxSlotDiffPlays = getRemoteVar( + BooleanKeys.ENABLE_DISCOVERY_NODE_MAX_SLOT_DIFF_PLAYS + ) + ? getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_SLOT_DIFF_PLAYS) + : null + + const healthCheckThresholds = { minVersion, maxBlockDiff, maxSlotDiffPlays } + + const blocklist = this.getBlockList(StringKeys.DISCOVERY_NODE_BLOCK_LIST) + + const requestTimeout = + getRemoteVar(IntKeys.DISCOVERY_PROVIDER_SELECTION_TIMEOUT_MS) ?? undefined + + return new DiscoveryNodeSelector({ + healthCheckThresholds, + blocklist, + requestTimeout, + bootstrapServices: discoveryNodes + }) + } + + private getBlockList(remoteVarKey: StringKeys) { + const list = this.remoteConfigInstance.getRemoteVar(remoteVarKey) + if (list) { + try { + return new Set(list.split(',')) + } catch (e) { + console.error(e) + return null + } + } + return null + } + + public async getDiscoveryNodeSelector() { + if (!this.discoveryNodeSelectorPromise) { + this.discoveryNodeSelectorPromise = this.makeDiscoveryNodeSelector() + } + return await this.discoveryNodeSelectorPromise + } +} diff --git a/packages/common/src/services/discovery-node-selector/index.ts b/packages/common/src/services/discovery-node-selector/index.ts new file mode 100644 index 00000000000..bd7d53ffff6 --- /dev/null +++ b/packages/common/src/services/discovery-node-selector/index.ts @@ -0,0 +1 @@ +export * from './DiscoveryNodeSelectorInstance' diff --git a/packages/common/src/services/index.ts b/packages/common/src/services/index.ts index 949b5f33edb..632116e8739 100644 --- a/packages/common/src/services/index.ts +++ b/packages/common/src/services/index.ts @@ -14,3 +14,4 @@ export * from './cognito' export * from './track-download' export * from './oauth' export * from './location' +export * from './discovery-node-selector' diff --git a/packages/common/src/services/remote-config/feature-flags.ts b/packages/common/src/services/remote-config/feature-flags.ts index 1b73dcdde0c..26fc9f3411d 100644 --- a/packages/common/src/services/remote-config/feature-flags.ts +++ b/packages/common/src/services/remote-config/feature-flags.ts @@ -46,7 +46,8 @@ export enum FeatureFlags { TRENDING_PLAYLIST_NOTIFICATIONS = 'trending_playlist_notifications', TRENDING_UNDERGROUND_NOTIFICATIONS = 'trending_underground_notifications', TASTEMAKER_NOTIFICATIONS = 'tastemaker_notifications', - STORAGE_V2 = 'storage_v2' + STORAGE_V2 = 'storage_v2', + SDK_V2 = 'sdk_v2' } type FlagDefaults = Record @@ -108,5 +109,6 @@ export const flagDefaults: FlagDefaults = { [FeatureFlags.TRENDING_PLAYLIST_NOTIFICATIONS]: false, [FeatureFlags.TRENDING_UNDERGROUND_NOTIFICATIONS]: false, [FeatureFlags.TASTEMAKER_NOTIFICATIONS]: false, - [FeatureFlags.STORAGE_V2]: false + [FeatureFlags.STORAGE_V2]: false, + [FeatureFlags.SDK_V2]: false } diff --git a/packages/common/src/store/pages/chat/middleware.ts b/packages/common/src/store/pages/chat/middleware.ts index af299b67fa8..933002c499e 100644 --- a/packages/common/src/store/pages/chat/middleware.ts +++ b/packages/common/src/store/pages/chat/middleware.ts @@ -1,4 +1,4 @@ -import { ChatEvents, type sdk } from '@audius/sdk' +import { type AudiusSdk, ChatEvents } from '@audius/sdk' import { Middleware } from 'redux' import { Status } from 'models/Status' @@ -14,7 +14,7 @@ const { } = chatActions export const chatMiddleware = - (audiusSdk: () => Promise>): Middleware => + (audiusSdk: () => Promise): Middleware => (store) => { let messageListener: ChatEvents['message'] | null = null let reactionListener: ChatEvents['reaction'] | null = null diff --git a/packages/common/src/store/storeContext.ts b/packages/common/src/store/storeContext.ts index 9732692ea72..23c46c3e78f 100644 --- a/packages/common/src/store/storeContext.ts +++ b/packages/common/src/store/storeContext.ts @@ -1,4 +1,4 @@ -import type { sdk } from '@audius/sdk' +import type { AudiusSdk } from '@audius/sdk' import { AnalyticsEvent, LineupState, Track } from '../models' import { AudioPlayer } from '../services/audio-player' @@ -59,5 +59,5 @@ export type CommonStoreContext = { instagramRedirectUrl?: string share: (url: string, message?: string) => Promise | void openSeaClient: OpenSeaClient - audiusSdk: () => Promise> + audiusSdk: () => Promise } diff --git a/packages/mobile/package-lock.json b/packages/mobile/package-lock.json index 4e727fca6a5..079487b5508 100644 --- a/packages/mobile/package-lock.json +++ b/packages/mobile/package-lock.json @@ -73,9 +73,9 @@ } }, "@audius/sdk": { - "version": "2.0.3-beta.1", - "resolved": "https://registry.npmjs.org/@audius/sdk/-/sdk-2.0.3-beta.1.tgz", - "integrity": "sha512-2PIXnhJRMsDTK2nUKncYKzvf0+wsU6sRByH0MQHnVkRBPcVXyKLwjOVH9HnxAjidc4NUZVHf962QqU67ho3goA==", + "version": "2.0.3-beta.5", + "resolved": "https://registry.npmjs.org/@audius/sdk/-/sdk-2.0.3-beta.5.tgz", + "integrity": "sha512-bDD82p7Lrehd2E3Ghv2uEv6Ep11FCGxXjlvKBNT7aoMN+O0FYkvoaLbBWh5cjJBeOaKm9+7GBW1Uqaj7B9tz7w==", "requires": { "@audius/hedgehog": "2.1.0", "@babel/runtime": "7.18.3", @@ -10282,7 +10282,7 @@ "@walletconnect/utils": "^1.8.0", "keyvaluestorage": "0.7.1", "react-native-qrcode-svg": "6.0.6", - "react-native-svg": "12.3.0", + "react-native-svg": "9.6.4", "use-deep-compare-effect": "1.6.1" }, "dependencies": { @@ -11841,11 +11841,11 @@ }, "dependencies": { "@babel/helper-module-imports": { - "version": "7.18.6", - "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.18.6.tgz", - "integrity": "sha512-0NFvs3VkuSYbFi1x2Vd6tKrywq+z/cLeYC/RJNFrIX/30Bf5aiGYbtvGXolEktzJH8o5E5KJ3tT+nkxuuZFVlA==", + "version": "7.21.4", + "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.21.4.tgz", + "integrity": "sha512-orajc5T2PsRYUN3ZryCEFeMDYwyw09c/pZeaQEZPH0MpKzSvn3e0uXsDBu3k03VI+9DBiRo+l22BfKTpKwa/Wg==", "requires": { - "@babel/types": "^7.18.6" + "@babel/types": "^7.21.4" } }, "@babel/helper-validator-identifier": { @@ -11854,9 +11854,9 @@ "integrity": "sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==" }, "@babel/types": { - "version": "7.21.3", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.21.3.tgz", - "integrity": "sha512-sBGdETxC+/M4o/zKC0sl6sjWv62WFR/uzxrJ6uYyMLZOUlPnwzw0tKgVHOXxaAd5l2g8pEDM5RZ495GPQI77kg==", + "version": "7.21.4", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.21.4.tgz", + "integrity": "sha512-rU2oY501qDxE8Pyo7i/Orqma4ziCOrby0/9mvbDUGEfvZjb279Nk9k19e2fiCxHbRRpY2ZyrgW1eq22mvmOIzA==", "requires": { "@babel/helper-string-parser": "^7.19.4", "@babel/helper-validator-identifier": "^7.19.1", @@ -27053,6 +27053,32 @@ "css-tree": "^1.0.0-alpha.39" }, "dependencies": { + "css-select": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz", + "integrity": "sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ==", + "requires": { + "boolbase": "^1.0.0", + "css-what": "^6.0.1", + "domhandler": "^4.3.1", + "domutils": "^2.8.0", + "nth-check": "^2.0.1" + } + }, + "css-tree": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-1.1.3.tgz", + "integrity": "sha512-tRpdppF7TRazZrjJ6v3stzv93qxRcSsFmW6cX0Zm2NVKpxE1WV1HblnghVv9TreireHkqI/VDEsfolRF1p6y7Q==", + "requires": { + "mdn-data": "2.0.14", + "source-map": "^0.6.1" + } + }, + "css-what": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz", + "integrity": "sha512-HTUrgRJ7r4dsZKU6GjmpfRK1O76h97Z8MfS1G0FozR+oF2kG6Vfe8JE6zwrkbxigziPHinCJ+gCPjA9EaBDtRw==" + }, "dom-serializer": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-1.4.1.tgz", @@ -27063,10 +27089,10 @@ "entities": "^2.0.0" } }, - "source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" + "domelementtype": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.3.0.tgz", + "integrity": "sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==" }, "domutils": { "version": "2.8.0", @@ -27078,36 +27104,10 @@ "domhandler": "^4.2.0" } }, - "domelementtype": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.3.0.tgz", - "integrity": "sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==" - }, - "css-select": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz", - "integrity": "sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ==", - "requires": { - "boolbase": "^1.0.0", - "css-what": "^6.0.1", - "domhandler": "^4.3.1", - "domutils": "^2.8.0", - "nth-check": "^2.0.1" - } - }, - "css-what": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz", - "integrity": "sha512-HTUrgRJ7r4dsZKU6GjmpfRK1O76h97Z8MfS1G0FozR+oF2kG6Vfe8JE6zwrkbxigziPHinCJ+gCPjA9EaBDtRw==" - }, - "css-tree": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-1.1.3.tgz", - "integrity": "sha512-tRpdppF7TRazZrjJ6v3stzv93qxRcSsFmW6cX0Zm2NVKpxE1WV1HblnghVv9TreireHkqI/VDEsfolRF1p6y7Q==", - "requires": { - "mdn-data": "2.0.14", - "source-map": "^0.6.1" - } + "mdn-data": { + "version": "2.0.14", + "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz", + "integrity": "sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==" }, "nth-check": { "version": "2.1.1", @@ -27117,10 +27117,10 @@ "boolbase": "^1.0.0" } }, - "mdn-data": { - "version": "2.0.14", - "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz", - "integrity": "sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==" + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" } } }, @@ -31755,4 +31755,4 @@ } } } -} \ No newline at end of file +} diff --git a/packages/mobile/package.json b/packages/mobile/package.json index e7845cdb8e3..94cab124f94 100644 --- a/packages/mobile/package.json +++ b/packages/mobile/package.json @@ -44,7 +44,7 @@ "dependencies": { "@amplitude/react-native": "2.6.0", "@audius/common": "1.5.18", - "@audius/sdk": "2.0.3-beta.4", + "@audius/sdk": "2.0.3-beta.5", "@fingerprintjs/fingerprintjs-pro-react-native": "2.0.0-test.2", "@gorhom/portal": "1.0.9", "@hcaptcha/react-native-hcaptcha": "1.3.4", diff --git a/packages/mobile/src/services/audius-api-client/apiClient.ts b/packages/mobile/src/services/audius-api-client/apiClient.ts index 01638d725c0..7ecc5e62e65 100644 --- a/packages/mobile/src/services/audius-api-client/apiClient.ts +++ b/packages/mobile/src/services/audius-api-client/apiClient.ts @@ -2,7 +2,7 @@ import { AudiusAPIClient } from '@audius/common' import { audiusBackendInstance } from '../audius-backend-instance' import { env } from '../env' -import { audiusLibs } from '../libs' +import { audiusLibs, waitForLibsInit } from '../libs' import { localStorage } from '../local-storage' import { remoteConfigInstance } from '../remote-config' @@ -11,5 +11,6 @@ export const apiClient = new AudiusAPIClient({ remoteConfigInstance, getAudiusLibs: () => audiusLibs, localStorage, - env + env, + waitForLibsInit }) diff --git a/packages/mobile/src/services/audius-backend-instance.ts b/packages/mobile/src/services/audius-backend-instance.ts index 60e1b2237ad..bcbb83dc8e2 100644 --- a/packages/mobile/src/services/audius-backend-instance.ts +++ b/packages/mobile/src/services/audius-backend-instance.ts @@ -8,6 +8,7 @@ import { track } from 'app/services/analytics' import { reportToSentry } from 'app/utils/reportToSentry' import { createPrivateKey } from './createPrivateKey' +import { discoveryNodeSelectorInstance } from './discovery-node-selector' import { withEagerOption } from './eagerLoadUtils' import { env } from './env' import { @@ -30,6 +31,7 @@ export const audiusBackendInstance = audiusBackend({ ethProviderUrls: (Config.ETH_PROVIDER_URL || '').split(','), ethRegistryAddress: Config.ETH_REGISTRY_ADDRESS, ethTokenAddress: Config.ETH_TOKEN_ADDRESS, + discoveryNodeSelectorInstance, getFeatureEnabled, getHostUrl: () => { return `${Config.PUBLIC_PROTOCOL}//${Config.PUBLIC_HOSTNAME}` diff --git a/packages/mobile/src/services/audius-sdk.ts b/packages/mobile/src/services/audius-sdk.ts index f44bca4fbf3..85c34863a2a 100644 --- a/packages/mobile/src/services/audius-sdk.ts +++ b/packages/mobile/src/services/audius-sdk.ts @@ -1,70 +1,26 @@ import { EventEmitter } from 'events' -import { StringKeys, IntKeys } from '@audius/common' -import { - developmentConfig, - DiscoveryNodeSelector, - productionConfig, - sdk, - stagingConfig -} from '@audius/sdk' +import type { AudiusSdk } from '@audius/sdk' +import { sdk } from '@audius/sdk' import { keccak_256 } from '@noble/hashes/sha3' import * as secp from '@noble/secp256k1' -import { env } from './env' +import { discoveryNodeSelectorInstance } from './discovery-node-selector' import { audiusLibs, waitForLibsInit } from './libs' -import { remoteConfigInstance } from './remote-config/remote-config-instance' -const { getRemoteVar, waitForRemoteConfig } = remoteConfigInstance -const getBlockList = (remoteVarKey: StringKeys) => { - const list = getRemoteVar(remoteVarKey) - if (list) { - try { - return new Set(list.split(',')) - } catch (e) { - console.error(e) - return null - } - } - return null -} let inProgress = false const SDK_LOADED_EVENT_NAME = 'AUDIUS_SDK_LOADED' const sdkEventEmitter = new EventEmitter() -let sdkInstance: ReturnType - -const bootstrapConfig = - env.ENVIRONMENT === 'development' - ? developmentConfig - : env.ENVIRONMENT === 'staging' - ? stagingConfig - : productionConfig +let sdkInstance: AudiusSdk const initSdk = async () => { inProgress = true - await waitForRemoteConfig() - const discoveryNodeBlockList = getBlockList( - StringKeys.DISCOVERY_NODE_BLOCK_LIST - ) const audiusSdk = sdk({ appName: 'audius-mobile-client', services: { - discoveryNodeSelector: new DiscoveryNodeSelector({ - healthCheckThresholds: { - minVersion: bootstrapConfig.minVersion, - maxBlockDiff: - getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_BLOCK_DIFF) ?? undefined, - maxSlotDiffPlays: - getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_SLOT_DIFF_PLAYS) ?? - undefined - }, - blocklist: discoveryNodeBlockList ?? undefined, - bootstrapServices: bootstrapConfig.discoveryNodes, - requestTimeout: - getRemoteVar(IntKeys.DISCOVERY_PROVIDER_SELECTION_TIMEOUT_MS) ?? - undefined - }), + discoveryNodeSelector: + await discoveryNodeSelectorInstance.getDiscoveryNodeSelector(), walletApi: { sign: async (data: string) => { await waitForLibsInit() diff --git a/packages/mobile/src/services/discovery-node-selector.ts b/packages/mobile/src/services/discovery-node-selector.ts new file mode 100644 index 00000000000..a996657e4ec --- /dev/null +++ b/packages/mobile/src/services/discovery-node-selector.ts @@ -0,0 +1,9 @@ +import { DiscoveryNodeSelectorInstance } from '@audius/common' + +import { env } from './env' +import { remoteConfigInstance } from './remote-config/remote-config-instance' + +export const discoveryNodeSelectorInstance = new DiscoveryNodeSelectorInstance({ + env, + remoteConfigInstance +}) diff --git a/packages/web/package-lock.json b/packages/web/package-lock.json index 74ac8ce99b4..a127cbeccda 100644 --- a/packages/web/package-lock.json +++ b/packages/web/package-lock.json @@ -121,9 +121,9 @@ } }, "@audius/sdk": { - "version": "2.0.3-beta.1", - "resolved": "https://registry.npmjs.org/@audius/sdk/-/sdk-2.0.3-beta.1.tgz", - "integrity": "sha512-2PIXnhJRMsDTK2nUKncYKzvf0+wsU6sRByH0MQHnVkRBPcVXyKLwjOVH9HnxAjidc4NUZVHf962QqU67ho3goA==", + "version": "2.0.3-beta.5", + "resolved": "https://registry.npmjs.org/@audius/sdk/-/sdk-2.0.3-beta.5.tgz", + "integrity": "sha512-bDD82p7Lrehd2E3Ghv2uEv6Ep11FCGxXjlvKBNT7aoMN+O0FYkvoaLbBWh5cjJBeOaKm9+7GBW1Uqaj7B9tz7w==", "requires": { "@audius/hedgehog": "2.1.0", "@babel/runtime": "7.18.3", diff --git a/packages/web/package.json b/packages/web/package.json index f317cb35779..82e1c6bbe47 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -52,7 +52,7 @@ }, "dependencies": { "@audius/common": "1.5.18", - "@audius/sdk": "2.0.3-beta.4", + "@audius/sdk": "2.0.3-beta.5", "@audius/stems": "1.5.17", "@coinbase/cbpay-js": "1.2.0", "@craco/craco": "7.0.0-alpha.3", diff --git a/packages/web/src/services/audius-api-client/apiClient.ts b/packages/web/src/services/audius-api-client/apiClient.ts index 475ae3e0acf..a355d6ee5af 100644 --- a/packages/web/src/services/audius-api-client/apiClient.ts +++ b/packages/web/src/services/audius-api-client/apiClient.ts @@ -1,6 +1,7 @@ import { AudiusAPIClient } from '@audius/common' import { audiusBackendInstance } from 'services/audius-backend/audius-backend-instance' +import { waitForLibsInit } from 'services/audius-backend/eagerLoadUtils' import { env } from 'services/env' import { localStorage } from 'services/local-storage' import { remoteConfigInstance } from 'services/remote-config/remote-config-instance' @@ -10,5 +11,6 @@ export const apiClient = new AudiusAPIClient({ remoteConfigInstance, getAudiusLibs: () => window.audiusLibs, localStorage, - env + env, + waitForLibsInit }) diff --git a/packages/web/src/services/audius-backend/audius-backend-instance.ts b/packages/web/src/services/audius-backend/audius-backend-instance.ts index ce52a0e235c..56628f9306f 100644 --- a/packages/web/src/services/audius-backend/audius-backend-instance.ts +++ b/packages/web/src/services/audius-backend/audius-backend-instance.ts @@ -7,6 +7,7 @@ import { waitForLibsInit, withEagerOption } from 'services/audius-backend/eagerLoadUtils' +import { discoveryNodeSelectorInstance } from 'services/discovery-node-selector' import { getFeatureEnabled } from 'services/remote-config/featureFlagHelpers' import { remoteConfigInstance } from 'services/remote-config/remote-config-instance' import { monitoringCallbacks } from 'services/serviceMonitoring' @@ -35,6 +36,7 @@ export const audiusBackendInstance = audiusBackend({ getFeatureEnabled, getHostUrl: () => window.location.origin, getLibs: () => import('@audius/sdk/dist/legacy'), + discoveryNodeSelectorInstance, getWeb3Config: async ( libs, registryAddress, diff --git a/packages/web/src/services/audius-sdk/audiusSdk.ts b/packages/web/src/services/audius-sdk/audiusSdk.ts index caf075145ee..5e945705f1e 100644 --- a/packages/web/src/services/audius-sdk/audiusSdk.ts +++ b/packages/web/src/services/audius-sdk/audiusSdk.ts @@ -1,72 +1,27 @@ -import { IntKeys, StringKeys } from '@audius/common' -import { - sdk, - DiscoveryNodeSelector, - stagingConfig, - productionConfig, - developmentConfig -} from '@audius/sdk' +import { sdk, AudiusSdk } from '@audius/sdk' import { keccak_256 } from '@noble/hashes/sha3' import * as secp from '@noble/secp256k1' import { waitForLibsInit } from 'services/audius-backend/eagerLoadUtils' -import { remoteConfigInstance } from 'services/remote-config/remote-config-instance' +import { discoveryNodeSelectorInstance } from 'services/discovery-node-selector' declare global { interface Window { audiusLibs: any - audiusSdk: ReturnType + audiusSdk: AudiusSdk } } -const { getRemoteVar, waitForRemoteConfig } = remoteConfigInstance -const getBlockList = (remoteVarKey: StringKeys) => { - const list = getRemoteVar(remoteVarKey) - if (list) { - try { - return new Set(list.split(',')) - } catch (e) { - console.error(e) - return null - } - } - return null -} let inProgress = false const SDK_LOADED_EVENT_NAME = 'AUDIUS_SDK_LOADED' -const env = process.env.REACT_APP_ENVIRONMENT -const bootstrapConfig = - env === 'development' - ? developmentConfig - : env === 'staging' - ? stagingConfig - : productionConfig - const initSdk = async () => { inProgress = true - await waitForRemoteConfig() - const discoveryNodeBlockList = getBlockList( - StringKeys.DISCOVERY_NODE_BLOCK_LIST - ) const audiusSdk = sdk({ appName: 'audius-client', services: { - discoveryNodeSelector: new DiscoveryNodeSelector({ - healthCheckThresholds: { - minVersion: bootstrapConfig.minVersion, - maxBlockDiff: - getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_BLOCK_DIFF) ?? undefined, - maxSlotDiffPlays: - getRemoteVar(IntKeys.DISCOVERY_NODE_MAX_SLOT_DIFF_PLAYS) ?? - undefined - }, - blocklist: discoveryNodeBlockList, - requestTimeout: - getRemoteVar(IntKeys.DISCOVERY_PROVIDER_SELECTION_TIMEOUT_MS) ?? - undefined, - bootstrapServices: bootstrapConfig.discoveryNodes - }), + discoveryNodeSelector: + await discoveryNodeSelectorInstance.getDiscoveryNodeSelector(), walletApi: { sign: async (data: string) => { await waitForLibsInit() diff --git a/packages/web/src/services/discovery-node-selector.ts b/packages/web/src/services/discovery-node-selector.ts new file mode 100644 index 00000000000..a996657e4ec --- /dev/null +++ b/packages/web/src/services/discovery-node-selector.ts @@ -0,0 +1,9 @@ +import { DiscoveryNodeSelectorInstance } from '@audius/common' + +import { env } from './env' +import { remoteConfigInstance } from './remote-config/remote-config-instance' + +export const discoveryNodeSelectorInstance = new DiscoveryNodeSelectorInstance({ + env, + remoteConfigInstance +})