Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"libsignal": "git+https://github.com/whiskeysockets/libsignal-node",
"lru-cache": "^11.1.0",
"music-metadata": "^11.7.0",
"node-fetch": "^2.7.0",
"p-queue": "^9.0.0",
"pino": "^9.6",
"protobufjs": "^7.2.4",
Expand All @@ -56,6 +57,7 @@
"@eslint/js": "^9.31.0",
"@types/jest": "^30.0.0",
"@types/node": "^20.9.0",
"@types/node-fetch": "^2.6.11",
"@types/ws": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^8",
"@typescript-eslint/parser": "^8",
Expand Down
2 changes: 1 addition & 1 deletion src/Types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export type MediaGenerationOptions = {

mediaUploadTimeoutMs?: number

options?: RequestInit
options?: FetchRequestInit

backgroundColor?: string

Expand Down
2 changes: 1 addition & 1 deletion src/Types/Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export type SocketConfig = {
}

/** options for HTTP fetch requests */
options: RequestInit
options: FetchRequestInit
/**
* fetch a message from your store
* implement this so that messages failed to send
Expand Down
8 changes: 6 additions & 2 deletions src/Types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/// <reference types="node-fetch" />
import type { RequestInit } from 'node-fetch'
declare global {
interface RequestInit {
// FetchRequestInit extends RequestInit from node-fetch
type FetchRequestInit = RequestInit & {
dispatcher?: any
duplex?: 'half' | 'full'
}
// Export node-fetch's HeadersInit as a global type
type FetchHeadersInit = import('node-fetch').HeadersInit
}

export {}
8 changes: 4 additions & 4 deletions src/Utils/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export const decodeSyncdPatch = async (
return result
}

export const extractSyncdPatches = async (result: BinaryNode, options: RequestInit) => {
export const extractSyncdPatches = async (result: BinaryNode, options: FetchRequestInit) => {
const syncNode = getBinaryNodeChild(result, 'sync')
const collectionNodes = getBinaryNodeChildren(syncNode, 'collection')

Expand Down Expand Up @@ -353,7 +353,7 @@ export const extractSyncdPatches = async (result: BinaryNode, options: RequestIn
return final
}

export const downloadExternalBlob = async (blob: proto.IExternalBlobReference, options: RequestInit) => {
export const downloadExternalBlob = async (blob: proto.IExternalBlobReference, options: FetchRequestInit) => {
const stream = await downloadContentFromMessage(blob, 'md-app-state', { options })
const bufferArray: Buffer[] = []
for await (const chunk of stream) {
Expand All @@ -363,7 +363,7 @@ export const downloadExternalBlob = async (blob: proto.IExternalBlobReference, o
return Buffer.concat(bufferArray)
}

export const downloadExternalPatch = async (blob: proto.IExternalBlobReference, options: RequestInit) => {
export const downloadExternalPatch = async (blob: proto.IExternalBlobReference, options: FetchRequestInit) => {
const buffer = await downloadExternalBlob(blob, options)
const syncData = proto.SyncdMutations.decode(buffer)
return syncData
Expand Down Expand Up @@ -422,7 +422,7 @@ export const decodePatches = async (
syncds: proto.ISyncdPatch[],
initial: LTHashState,
getAppStateSyncKey: FetchAppStateSyncKey,
options: RequestInit,
options: FetchRequestInit,
minimumVersionNumber?: number,
logger?: ILogger,
validateMacs = true
Expand Down
7 changes: 4 additions & 3 deletions src/Utils/generics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Boom } from '@hapi/boom'
import { createHash, randomBytes } from 'crypto'
import fetch from 'node-fetch'
import { proto } from '../../WAProto/index.js'
const baileysVersion = [2, 3000, 1027934701]
import type {
Expand Down Expand Up @@ -231,11 +232,11 @@ export const bindWaitForConnectionUpdate = (ev: BaileysEventEmitter) => bindWait
* utility that fetches latest baileys version from the master branch.
* Use to ensure your WA connection is always on the latest version
*/
export const fetchLatestBaileysVersion = async (options: RequestInit = {}) => {
export const fetchLatestBaileysVersion = async (options: FetchRequestInit = {}) => {
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts'
try {
const response = await fetch(URL, {
dispatcher: options.dispatcher,
agent: options.dispatcher,
method: 'GET',
headers: options.headers
})
Expand Down Expand Up @@ -272,7 +273,7 @@ export const fetchLatestBaileysVersion = async (options: RequestInit = {}) => {
* A utility that fetches the latest web version of whatsapp.
* Use to ensure your WA connection is always on the latest version
*/
export const fetchLatestWaWebVersion = async (options: RequestInit = {}) => {
export const fetchLatestWaWebVersion = async (options: FetchRequestInit = {}) => {
try {
// Absolute minimal headers required to bypass anti-bot detection
const defaultHeaders = {
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { downloadContentFromMessage } from './messages-media'

const inflatePromise = promisify(inflate)

export const downloadHistory = async (msg: proto.Message.IHistorySyncNotification, options: RequestInit) => {
export const downloadHistory = async (msg: proto.Message.IHistorySyncNotification, options: FetchRequestInit) => {
const stream = await downloadContentFromMessage(msg, 'md-msg-hist', { options })
const bufferArray: Buffer[] = []
for await (const chunk of stream) {
Expand Down Expand Up @@ -94,7 +94,7 @@ export const processHistoryMessage = (item: proto.IHistorySync) => {

export const downloadAndProcessHistorySyncNotification = async (
msg: proto.Message.IHistorySyncNotification,
options: RequestInit
options: FetchRequestInit
) => {
const historyMsg = await downloadHistory(msg, options)
return processHistoryMessage(historyMsg)
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/link-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type URLGenerationOptions = {
/** Timeout in ms */
timeout: number
proxyUrl?: string
headers?: HeadersInit
headers?: FetchHeadersInit
}
uploadImage?: WAMediaUploadFunction
logger?: ILogger
Expand Down
16 changes: 8 additions & 8 deletions src/Utils/messages-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { tmpdir } from 'os'
import { join } from 'path'
import { Readable, Transform } from 'stream'
import { URL } from 'url'
import fetch from 'node-fetch'
import { proto } from '../../WAProto/index.js'
import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP, type MediaType } from '../Defaults'
import type {
Expand Down Expand Up @@ -300,7 +301,7 @@ export const toBuffer = async (stream: Readable) => {
return Buffer.concat(chunks)
}

export const getStream = async (item: WAMediaUpload, opts?: RequestInit & { maxContentLength?: number }) => {
export const getStream = async (item: WAMediaUpload, opts?: FetchRequestInit & { maxContentLength?: number }) => {
if (Buffer.isBuffer(item)) {
return { stream: toReadable(item), type: 'buffer' } as const
}
Expand Down Expand Up @@ -361,11 +362,11 @@ export async function generateThumbnail(
}
}

export const getHttpStream = async (url: string | URL, options: RequestInit & { isStream?: true } = {}) => {
export const getHttpStream = async (url: string | URL, options: FetchRequestInit & { isStream?: true } = {}) => {
const response = await fetch(url.toString(), {
dispatcher: options.dispatcher,
agent: options.dispatcher,
method: 'GET',
headers: options.headers as HeadersInit
headers: options.headers
})
if (!response.ok) {
throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } })
Expand All @@ -378,7 +379,7 @@ export const getHttpStream = async (url: string | URL, options: RequestInit & {
type EncryptedStreamOptions = {
saveOriginalFileIfRequired?: boolean
logger?: ILogger
opts?: RequestInit
opts?: FetchRequestInit
}

export const encryptedStream = async (
Expand Down Expand Up @@ -498,7 +499,7 @@ const toSmallestChunkSize = (num: number) => {
export type MediaDownloadOptions = {
startByte?: number
endByte?: number
options?: RequestInit
options?: FetchRequestInit
}

export const getUrlFromDirectPath = (directPath: string) => `https://${DEF_HOST}${directPath}`
Expand Down Expand Up @@ -662,7 +663,7 @@ export const getWAUploadToServer = (
try {
const stream = createReadStream(filePath)
const response = await fetch(url, {
dispatcher: fetchAgent,
agent: fetchAgent,
method: 'POST',
body: stream as any,
headers: {
Expand All @@ -674,7 +675,6 @@ export const getWAUploadToServer = (
'Content-Type': 'application/octet-stream',
Origin: DEFAULT_ORIGIN
},
duplex: 'half',
// Note: custom agents/proxy require undici Agent; omitted here.
signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined
})
Expand Down
6 changes: 5 additions & 1 deletion src/Utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Boom } from '@hapi/boom'
import { randomBytes } from 'crypto'
import { promises as fs } from 'fs'
import { type Transform } from 'stream'
import fetch from 'node-fetch'
import { proto } from '../../WAProto/index.js'
import {
CALL_AUDIO_PREFIX,
Expand Down Expand Up @@ -454,7 +455,10 @@ export const generateWAMessageContent = async (
if (options.getProfilePicUrl) {
const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview')
if (pfpUrl) {
const resp = await fetch(pfpUrl, { method: 'GET', dispatcher: options?.options?.dispatcher })
const resp = await fetch(pfpUrl, {
method: 'GET',
agent: options?.options?.dispatcher
})
if (resp.ok) {
const buf = Buffer.from(await resp.arrayBuffer())
m.groupInviteMessage.jpegThumbnail = buf
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/process-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ProcessMessageContext = {
keyStore: SignalKeyStoreWithTransaction
ev: BaileysEventEmitter
logger?: ILogger
options: RequestInit
options: FetchRequestInit
signalRepository: SignalRepositoryWithLIDStore
}

Expand Down
Loading
Loading