feat: pass axios config to all axios instances

This commit is contained in:
Adhiraj Singh
2022-09-19 17:46:43 +05:30
parent 116b30dff0
commit ae3ac78dc3
5 changed files with 50 additions and 17 deletions

View File

@@ -1,4 +1,5 @@
import { Boom } from '@hapi/boom'
import { AxiosRequestConfig } from 'axios'
import type { Logger } from 'pino'
import { proto } from '../../WAProto'
import { BaileysEventEmitter, ChatModification, ChatMutation, Contact, InitialAppStateSyncOptions, LastMessageList, LTHashState, WAPatchCreate, WAPatchName } from '../Types'
@@ -273,7 +274,10 @@ export const decodeSyncdPatch = async(
return result
}
export const extractSyncdPatches = async(result: BinaryNode) => {
export const extractSyncdPatches = async(
result: BinaryNode,
options: AxiosRequestConfig<any>
) => {
const syncNode = getBinaryNodeChild(result, 'sync')
const collectionNodes = getBinaryNodeChildren(syncNode, 'collection')
@@ -300,7 +304,7 @@ export const extractSyncdPatches = async(result: BinaryNode) => {
const blobRef = proto.ExternalBlobReference.decode(
snapshotNode.content! as Buffer
)
const data = await downloadExternalBlob(blobRef)
const data = await downloadExternalBlob(blobRef, options)
snapshot = proto.SyncdSnapshot.decode(data)
}
@@ -328,8 +332,11 @@ export const extractSyncdPatches = async(result: BinaryNode) => {
}
export const downloadExternalBlob = async(blob: proto.IExternalBlobReference) => {
const stream = await downloadContentFromMessage(blob, 'md-app-state')
export const downloadExternalBlob = async(
blob: proto.IExternalBlobReference,
options: AxiosRequestConfig<any>
) => {
const stream = await downloadContentFromMessage(blob, 'md-app-state', { options })
const bufferArray: Buffer[] = []
for await (const chunk of stream) {
bufferArray.push(chunk)
@@ -338,8 +345,11 @@ export const downloadExternalBlob = async(blob: proto.IExternalBlobReference) =>
return Buffer.concat(bufferArray)
}
export const downloadExternalPatch = async(blob: proto.IExternalBlobReference) => {
const buffer = await downloadExternalBlob(blob)
export const downloadExternalPatch = async(
blob: proto.IExternalBlobReference,
options: AxiosRequestConfig<any>
) => {
const buffer = await downloadExternalBlob(blob, options)
const syncData = proto.SyncdMutations.decode(buffer)
return syncData
}
@@ -399,6 +409,7 @@ export const decodePatches = async(
initial: LTHashState,
getAppStateSyncKey: FetchAppStateSyncKey,
onMutation: (mut: ChatMutation) => void,
options: AxiosRequestConfig<any>,
minimumVersionNumber?: number,
logger?: Logger,
validateMacs: boolean = true
@@ -416,7 +427,7 @@ export const decodePatches = async(
const { version, keyId, snapshotMac } = syncd
if(syncd.externalMutations) {
logger?.trace({ name, version }, 'downloading external patch')
const ref = await downloadExternalPatch(syncd.externalMutations)
const ref = await downloadExternalPatch(syncd.externalMutations, options)
logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch')
syncd.mutations?.push(...ref.mutations)
}

View File

@@ -1,3 +1,4 @@
import { AxiosRequestConfig } from 'axios'
import { promisify } from 'util'
import { inflate } from 'zlib'
import { proto } from '../../WAProto'
@@ -9,8 +10,11 @@ import { downloadContentFromMessage } from './messages-media'
const inflatePromise = promisify(inflate)
export const downloadHistory = async(msg: proto.Message.IHistorySyncNotification) => {
const stream = await downloadContentFromMessage(msg, 'md-msg-hist')
export const downloadHistory = async(
msg: proto.Message.IHistorySyncNotification,
options: AxiosRequestConfig<any>
) => {
const stream = await downloadContentFromMessage(msg, 'md-msg-hist', { options })
const bufferArray: Buffer[] = []
for await (const chunk of stream) {
bufferArray.push(chunk)
@@ -112,9 +116,10 @@ export const processHistoryMessage = (
export const downloadAndProcessHistorySyncNotification = async(
msg: proto.Message.IHistorySyncNotification,
historyCache: Set<string>,
recvChats: InitialReceivedChatsState
recvChats: InitialReceivedChatsState,
options: AxiosRequestConfig<any>
) => {
const historyMsg = await downloadHistory(msg)
const historyMsg = await downloadHistory(msg, options)
return processHistoryMessage(historyMsg, historyCache, recvChats)
}

View File

@@ -1,3 +1,4 @@
import { AxiosRequestConfig } from 'axios'
import type { Logger } from 'pino'
import { proto } from '../../WAProto'
import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, InitialReceivedChatsState, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
@@ -12,6 +13,7 @@ type ProcessMessageContext = {
keyStore: SignalKeyStoreWithTransaction
ev: BaileysEventEmitter
logger?: Logger
options: AxiosRequestConfig<any>
}
const MSG_MISSED_CALL_TYPES = new Set([
@@ -64,7 +66,7 @@ export const shouldIncrementChatUnread = (message: proto.IWebMessageInfo) => (
const processMessage = async(
message: proto.IWebMessageInfo,
{ downloadHistory, ev, historyCache, recvChats, creds, keyStore, logger }: ProcessMessageContext
{ downloadHistory, ev, historyCache, recvChats, creds, keyStore, logger, options }: ProcessMessageContext
) => {
const meId = creds.me!.id
const { accountSettings } = creds
@@ -95,7 +97,12 @@ const processMessage = async(
if(downloadHistory) {
const isLatest = !creds.processedHistoryMessages?.length
const { chats, contacts, messages, didProcess } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache, recvChats)
const { chats, contacts, messages, didProcess } = await downloadAndProcessHistorySyncNotification(
histNotification,
historyCache,
recvChats,
options
)
if(chats.length) {
ev.emit('chats.set', { chats, isLatest })