feat: add custom host names

This commit is contained in:
Adhiraj Singh
2021-12-11 11:32:12 +05:30
parent 2b33a5202c
commit 792c4bf0a4
3 changed files with 8 additions and 4 deletions

View File

@@ -28,6 +28,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
printQRInTerminal: false, printQRInTerminal: false,
emitOwnEvents: true, emitOwnEvents: true,
defaultQueryTimeoutMs: 60_000, defaultQueryTimeoutMs: 60_000,
customUploadHosts: [],
getMessage: async() => undefined getMessage: async() => undefined
} }

View File

@@ -383,9 +383,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
let uploadInfo = await refreshMediaConn(false) let uploadInfo = await refreshMediaConn(false)
let mediaUrl: string 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 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 { try {
const {body: responseText} = await got.post( const {body: responseText} = await got.post(
@@ -411,8 +412,8 @@ export const makeMessagesSocket = (config: SocketConfig) => {
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`) throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
} }
} catch (error) { } catch (error) {
const isLast = host.hostname === uploadInfo.hosts[uploadInfo.hosts.length-1].hostname const isLast = hostname === hosts[uploadInfo.hosts.length-1]
logger.debug(`Error in uploading to ${host.hostname} (${error}) ${isLast ? '' : ', retrying...'}`) logger.debug(`Error in uploading to ${hostname} (${error}) ${isLast ? '' : ', retrying...'}`)
} }
} }
if (!mediaUrl) { if (!mediaUrl) {

View File

@@ -55,6 +55,8 @@ export type SocketConfig = {
mediaCache?: NodeCache mediaCache?: NodeCache
/** map to store the retry counts for failed messages */ /** map to store the retry counts for failed messages */
msgRetryCounterMap?: { [msgId: string]: number } msgRetryCounterMap?: { [msgId: string]: number }
/** custom domains to push media via */
customUploadHosts: string[]
/** /**
* fetch a message from your store * 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 * implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried