diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index 9a542e7..844449c 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -28,6 +28,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { printQRInTerminal: false, emitOwnEvents: true, defaultQueryTimeoutMs: 60_000, + customUploadHosts: [], getMessage: async() => undefined } diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 19befed..db63873 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -383,9 +383,10 @@ export const makeMessagesSocket = (config: SocketConfig) => { let uploadInfo = await refreshMediaConn(false) let mediaUrl: string - for (let host of uploadInfo.hosts) { + const hosts = [ ...config.customUploadHosts, ...uploadInfo.hosts.map(h => h.hostname) ] + for (let hostname of hosts) { const auth = encodeURIComponent(uploadInfo.auth) // the auth token - const url = `https://${host.hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}` + const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}` try { const {body: responseText} = await got.post( @@ -411,8 +412,8 @@ export const makeMessagesSocket = (config: SocketConfig) => { throw new Error(`upload failed, reason: ${JSON.stringify(result)}`) } } catch (error) { - const isLast = host.hostname === uploadInfo.hosts[uploadInfo.hosts.length-1].hostname - logger.debug(`Error in uploading to ${host.hostname} (${error}) ${isLast ? '' : ', retrying...'}`) + const isLast = hostname === hosts[uploadInfo.hosts.length-1] + logger.debug(`Error in uploading to ${hostname} (${error}) ${isLast ? '' : ', retrying...'}`) } } if (!mediaUrl) { diff --git a/src/Types/index.ts b/src/Types/index.ts index 113a92d..680190f 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -55,6 +55,8 @@ export type SocketConfig = { mediaCache?: NodeCache /** map to store the retry counts for failed messages */ msgRetryCounterMap?: { [msgId: string]: number } + /** custom domains to push media via */ + customUploadHosts: string[] /** * fetch a message from your store * implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried