Skip to content

Commit

Permalink
Leave libs as is
Browse files Browse the repository at this point in the history
  • Loading branch information
theoilie authored and michellebrier committed Oct 24, 2023
1 parent 96276a5 commit 366d91c
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions packages/libs/src/services/creatorNode/CreatorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ export type PlaylistMetadata = {
export type ProgressCB = (
progress:
| {
art: {
upload?: { loaded: number; total: number }
transcode?: { decimal: number }
resize?: undefined
art: {
upload?: { loaded: number; total: number }
transcode?: { decimal: number }
resize?: undefined
}
}
}
| {
audio: {
upload?: { loaded: number; total: number }
transcode?: { decimal: number }
resize?: undefined
audio: {
upload?: { loaded: number; total: number }
transcode?: { decimal: number }
resize?: undefined
}
}
}
) => void

export type CreatorNodeConfig = {
Expand Down Expand Up @@ -236,15 +236,15 @@ export class CreatorNode {
return await this.uploadFileV2(file, onProgress, 'audio', options)
}

async uploadTrackCoverArtV2(file: File, onProgress: ProgressCB = () => { }) {
async uploadTrackCoverArtV2(file: File, onProgress: ProgressCB = () => {}) {
return await this.uploadFileV2(file, onProgress, 'img_square')
}

async uploadProfilePictureV2(file: File, onProgress: ProgressCB = () => { }) {
async uploadProfilePictureV2(file: File, onProgress: ProgressCB = () => {}) {
return await this.uploadFileV2(file, onProgress, 'img_square')
}

async uploadCoverPhotoV2(file: File, onProgress: ProgressCB = () => { }) {
async uploadCoverPhotoV2(file: File, onProgress: ProgressCB = () => {}) {
return await this.uploadFileV2(file, onProgress, 'img_backdrop')
}

Expand Down Expand Up @@ -392,23 +392,34 @@ export class CreatorNode {
/* ------- INTERNAL FUNCTIONS ------- */

/**
* Makes an axios request to each storage node sequentially until 1 succeeds or all fail
* Makes an axios request to this.creatorNodeEndpoint
* @return response body
*/
async _makeRequestV2(axiosRequestObj: AxiosRequestConfig) {
let lastErr
for (let selectedNode = await this.storageNodeSelector.getSelectedNode(); this.storageNodeSelector.triedSelectingAllNodes(); selectedNode = await this.storageNodeSelector.getSelectedNode(true)) {
axiosRequestObj.baseURL = selectedNode!
try {
return await axios(axiosRequestObj)
} catch (e: any) {
lastErr = e // keep trying other nodes
// TODO: This might want to have other error handling, request UUIDs, etc...
// But I didn't want to pull in all the chaos and incompatiblity of the old _makeRequest
axiosRequestObj.baseURL = this.creatorNodeEndpoint
try {
return await axios(axiosRequestObj)
} catch (e: any) {
const wallet = this.userStateManager.getCurrentUser()?.wallet
// storageNodeSelector is not always defined (not always passed in to the constructor)
const storageNodes = this.storageNodeSelector.getNodes(wallet ?? '')

for (const storageNode of storageNodes) {
try {
axiosRequestObj.baseURL = storageNode
return await axios(axiosRequestObj)
} catch (e) {
// continue
}
}

const requestId = axiosRequestObj.headers['X-Request-ID']
const msg = `Error sending storagev2 request for X-Request-ID=${requestId}, tried all storage nodes: ${e}`
console.error(msg)
throw new Error(msg)
}
const requestId = axiosRequestObj.headers['X-Request-ID']
const msg = `Error sending storagev2 request for X-Request-ID=${requestId}, tried all healthy storage nodes. Last error: ${lastErr}`
console.error(msg)
throw new Error(msg)
}

/**
Expand Down Expand Up @@ -447,7 +458,7 @@ export class CreatorNode {
/**
* Calls fn and then retries once after 500ms, again after 1500ms, and again after 4000ms
*/
async _retry3(fn: () => Promise<any>, onRetry = (_err: any) => { }) {
async _retry3(fn: () => Promise<any>, onRetry = (_err: any) => {}) {
return await retry(fn, {
minTimeout: 500,
maxTimeout: 4000,
Expand Down

0 comments on commit 366d91c

Please sign in to comment.