feat: add legacy connection

This commit is contained in:
Adhiraj Singh
2021-12-17 19:27:04 +05:30
parent 13b49e658d
commit 19a9980492
23 changed files with 2402 additions and 103 deletions

View File

@@ -1,6 +1,5 @@
import type { Contact } from "./Contact"
import type { proto } from "../../WAProto"
import type { WAPatchName } from "./Chat"
export type KeyPair = { public: Uint8Array, private: Uint8Array }
export type SignedKeyPair = { keyPair: KeyPair, signature: Uint8Array, keyId: number }

View File

@@ -38,7 +38,7 @@ export type ChatModification =
mute: number | null
} |
{
clear: 'all' | { message: {id: string, fromMe?: boolean} }
clear: 'all' | { messages: {id: string, fromMe?: boolean}[] }
} |
{
star: {

56
src/Types/Events.ts Normal file
View File

@@ -0,0 +1,56 @@
import type EventEmitter from "events"
import { AuthenticationCreds } from './Auth'
import { Chat, PresenceData } from './Chat'
import { Contact } from './Contact'
import { ConnectionState } from './State'
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
import { MessageInfoUpdate, MessageUpdateType, WAMessage, WAMessageUpdate, WAMessageKey } from './Message'
export type BaileysEventMap<T> = {
/** connection state has been updated -- WS closed, opened, connecting etc. */
'connection.update': Partial<ConnectionState>
/** credentials updated -- some metadata, keys or something */
'creds.update': Partial<T>
/** set chats (history sync), messages are reverse chronologically sorted */
'chats.set': { chats: Chat[], messages: WAMessage[] }
/** upsert chats */
'chats.upsert': Chat[]
/** update the given chats */
'chats.update': Partial<Chat>[]
/** delete chats with given ID */
'chats.delete': string[]
/** presence of contact in a chat updated */
'presence.update': { id: string, presences: { [participant: string]: PresenceData } }
'contacts.upsert': Contact[]
'contacts.update': Partial<Contact>[]
'messages.delete': { keys: WAMessageKey[] } | { jid: string, all: true }
'messages.update': WAMessageUpdate[]
/**
* add/update the given messages. If they were received while the connection was online,
* the update will have type: "notify"
* */
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
'message-info.update': MessageInfoUpdate[]
'groups.upsert': GroupMetadata[]
'groups.update': Partial<GroupMetadata>[]
/** apply an action to participants in a group */
'group-participants.update': { id: string, participants: string[], action: ParticipantAction }
'blocklist.set': { blocklist: string[] }
'blocklist.update': { blocklist: string[], type: 'add' | 'remove' }
}
export interface CommonBaileysEventEmitter<Creds> extends EventEmitter {
on<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): this
off<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): this
removeAllListeners<T extends keyof BaileysEventMap<Creds>>(event: T): this
emit<T extends keyof BaileysEventMap<Creds>>(event: T, arg: BaileysEventMap<Creds>[T]): boolean
}
export type BaileysEventEmitter = CommonBaileysEventEmitter<AuthenticationCreds>

83
src/Types/Legacy.ts Normal file
View File

@@ -0,0 +1,83 @@
import { CommonSocketConfig } from "./Socket"
import { CommonBaileysEventEmitter } from "./Events"
import { BinaryNode } from "../WABinary"
export interface LegacyAuthenticationCreds {
clientID: string
serverToken: string
clientToken: string
encKey: Buffer
macKey: Buffer
}
/** used for binary messages */
export enum WAMetric {
debugLog = 1,
queryResume = 2,
liveLocation = 3,
queryMedia = 4,
queryChat = 5,
queryContact = 6,
queryMessages = 7,
presence = 8,
presenceSubscribe = 9,
group = 10,
read = 11,
chat = 12,
received = 13,
picture = 14,
status = 15,
message = 16,
queryActions = 17,
block = 18,
queryGroup = 19,
queryPreview = 20,
queryEmoji = 21,
queryRead = 22,
queryVCard = 29,
queryStatus = 30,
queryStatusUpdate = 31,
queryLiveLocation = 33,
queryLabel = 36,
queryQuickReply = 39
}
/** used for binary messages */
export enum WAFlag {
available = 160,
other = 136, // don't know this one
ignore = 1 << 7,
acknowledge = 1 << 6,
unavailable = 1 << 4,
expires = 1 << 3,
composing = 1 << 2,
recording = 1 << 2,
paused = 1 << 2
}
/** Tag used with binary queries */
export type WATag = [WAMetric, WAFlag]
export type SocketSendMessageOptions = {
json: BinaryNode | any[]
binaryTag?: WATag
tag?: string
longTag?: boolean
}
export type SocketQueryOptions = SocketSendMessageOptions & {
timeoutMs?: number
expect200?: boolean
requiresPhoneConnection?: boolean
}
export type LegacySocketConfig = CommonSocketConfig<LegacyAuthenticationCreds> & {
/** max time for the phone to respond to a connectivity test */
phoneResponseTimeMs: number
/** max time for WA server to respond before error with 422 */
expectResponseTimeout: number
pendingRequestTimeoutMs: number
}
export type LegacyBaileysEventEmitter = CommonBaileysEventEmitter<LegacyAuthenticationCreds>

View File

@@ -155,7 +155,7 @@ export type MessageContentGenerationOptions = MediaGenerationOptions & {
}
export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent
export type MessageUpdateType = 'append' | 'notify' | 'prepend'
export type MessageUpdateType = 'append' | 'notify' | 'prepend' | 'last'
export type MessageInfoEventMap = { [jid: string]: Date }
export interface MessageInfo {

39
src/Types/Socket.ts Normal file
View File

@@ -0,0 +1,39 @@
import type { Agent } from "https"
import type { Logger } from "pino"
import type { URL } from "url"
import type NodeCache from 'node-cache'
export type WAVersion = [number, number, number]
export type WABrowserDescription = [string, string, string]
export type CommonSocketConfig<T> = {
/** provide an auth state object to maintain the auth state */
auth?: T
/** the WS url to connect to WA */
waWebSocketUrl: string | URL
/** Fails the connection if the socket times out in this interval */
connectTimeoutMs: number
/** Default timeout for queries, undefined for no timeout */
defaultQueryTimeoutMs: number | undefined
/** ping-pong interval for WS connection */
keepAliveIntervalMs: number
/** proxy agent */
agent?: Agent
/** pino logger */
logger: Logger
/** version to connect with */
version: WAVersion
/** override browser config */
browser: WABrowserDescription
/** agent used for fetch requests -- uploading/downloading media */
fetchAgent?: Agent
/** should the QR be printed in the terminal */
printQRInTerminal: boolean
/** should events be emitted for actions done by this socket connection */
emitOwnEvents: boolean
/** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: NodeCache
customUploadHosts: string[]
}

View File

@@ -1,3 +1,5 @@
import { Contact } from "./Contact"
export type WAConnectionState = 'open' | 'connecting' | 'close'
export type ConnectionState = {
@@ -13,5 +15,11 @@ export type ConnectionState = {
/** the current QR code */
qr?: string
/** has the device received all pending notifications while it was offline */
receivedPendingNotifications?: boolean
receivedPendingNotifications?: boolean
/** legacy connection options */
legacy?: {
phoneConnected: boolean
user?: Contact
}
}

View File

@@ -4,11 +4,11 @@ export * from './Chat'
export * from './Contact'
export * from './State'
export * from './Message'
export * from './Legacy'
export * from './Socket'
export * from './Events'
import type EventEmitter from "events"
import type { Agent } from "https"
import type { Logger } from "pino"
import type { URL } from "url"
import type NodeCache from 'node-cache'
import { AuthenticationState, AuthenticationCreds } from './Auth'
@@ -19,40 +19,11 @@ import { ConnectionState } from './State'
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
import { MessageInfoUpdate, MessageUpdateType, WAMessage, WAMessageUpdate, WAMessageKey } from './Message'
import { proto } from '../../WAProto'
import { CommonSocketConfig } from './Socket'
export type WAVersion = [number, number, number]
export type WABrowserDescription = [string, string, string]
export type ReconnectMode = 'no-reconnects' | 'on-any-error' | 'on-connection-error'
export type SocketConfig = {
/** provide an auth state object to maintain the auth state */
auth?: AuthenticationState
/** the WS url to connect to WA */
waWebSocketUrl: string | URL
/** Fails the connection if the socket times out in this interval */
connectTimeoutMs: number
/** Default timeout for queries, undefined for no timeout */
defaultQueryTimeoutMs: number | undefined
/** ping-pong interval for WS connection */
keepAliveIntervalMs: number
/** proxy agent */
agent?: Agent
/** pino logger */
logger: Logger
/** version to connect with */
version: WAVersion
/** override browser config */
browser: WABrowserDescription
/** agent used for fetch requests -- uploading/downloading media */
fetchAgent?: Agent
/** should the QR be printed in the terminal */
printQRInTerminal: boolean
/** should events be emitted for actions done by this socket connection */
emitOwnEvents: boolean
export type SocketConfig = CommonSocketConfig<AuthenticationState> & {
/** provide a cache to store a user's device list */
userDevicesCache?: NodeCache
/** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: NodeCache
/** map to store the retry counts for failed messages */
msgRetryCounterMap?: { [msgId: string]: number }
/** custom domains to push media via */
@@ -67,11 +38,12 @@ export type SocketConfig = {
export enum DisconnectReason {
connectionClosed = 428,
connectionLost = 408,
connectionReplaced = 440,
timedOut = 408,
loggedOut = 401,
badSession = 500,
restartRequired = 410,
notJoinedBeta = 403
multideviceMismatch = 403
}
export type WAInitResponse = {
@@ -104,11 +76,11 @@ export type WABusinessProfile = {
export type CurveKeyPair = { private: Uint8Array; public: Uint8Array }
export type BaileysEventMap = {
export type BaileysEventMap<T> = {
/** connection state has been updated -- WS closed, opened, connecting etc. */
'connection.update': Partial<ConnectionState>
/** credentials updated -- some metadata, keys or something */
'creds.update': Partial<AuthenticationCreds>
'creds.update': Partial<T>
/** set chats (history sync), messages are reverse chronologically sorted */
'chats.set': { chats: Chat[], messages: WAMessage[] }
/** upsert chats */
@@ -141,9 +113,12 @@ export type BaileysEventMap = {
'blocklist.set': { blocklist: string[] }
'blocklist.update': { blocklist: string[], type: 'add' | 'remove' }
}
export interface BaileysEventEmitter extends EventEmitter {
on<T extends keyof BaileysEventMap>(event: T, listener: (arg: BaileysEventMap[T]) => void): this
off<T extends keyof BaileysEventMap>(event: T, listener: (arg: BaileysEventMap[T]) => void): this
removeAllListeners<T extends keyof BaileysEventMap>(event: T): this
emit<T extends keyof BaileysEventMap>(event: T, arg: BaileysEventMap[T]): boolean
}
export interface CommonBaileysEventEmitter<Creds> extends EventEmitter {
on<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): this
off<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): this
removeAllListeners<T extends keyof BaileysEventMap<Creds>>(event: T): this
emit<T extends keyof BaileysEventMap<Creds>>(event: T, arg: BaileysEventMap<Creds>[T]): boolean
}
export type BaileysEventEmitter = CommonBaileysEventEmitter<AuthenticationCreds>