mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor: remove redundant stream:error handling
This commit is contained in:
@@ -5,8 +5,8 @@ import WebSocket from 'ws'
|
||||
import { proto } from '../../WAProto'
|
||||
import { DEF_CALLBACK_PREFIX, DEF_TAG_PREFIX, DEFAULT_ORIGIN, INITIAL_PREKEY_COUNT, MIN_PREKEY_COUNT } from '../Defaults'
|
||||
import { AuthenticationCreds, BaileysEventEmitter, BaileysEventMap, DisconnectReason, SocketConfig } from '../Types'
|
||||
import { addTransactionCapability, bindWaitForConnectionUpdate, configureSuccessfulPairing, Curve, generateLoginNode, generateMdTagPrefix, generateRegistrationNode, getErrorCodeFromStreamErrorReason, getNextPreKeysNode, makeNoiseHandler, printQRIfNecessaryListener, promiseTimeout, useSingleFileAuthState } from '../Utils'
|
||||
import { assertNodeErrorFree, BinaryNode, encodeBinaryNode, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } from '../WABinary'
|
||||
import { addTransactionCapability, bindWaitForConnectionUpdate, configureSuccessfulPairing, Curve, generateLoginNode, generateMdTagPrefix, generateRegistrationNode, getErrorCodeFromStreamError, getNextPreKeysNode, makeNoiseHandler, printQRIfNecessaryListener, promiseTimeout, useSingleFileAuthState } from '../Utils'
|
||||
import { assertNodeErrorFree, BinaryNode, encodeBinaryNode, getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } from '../WABinary'
|
||||
|
||||
/**
|
||||
* Connects to WA servers and performs:
|
||||
@@ -478,30 +478,15 @@ export const makeSocket = ({
|
||||
try {
|
||||
const { reply, creds: updatedCreds } = configureSuccessfulPairing(stanza, creds)
|
||||
|
||||
logger.debug('pairing configured successfully')
|
||||
|
||||
const waiting = awaitNextMessage()
|
||||
await sendNode(reply)
|
||||
|
||||
const value = (await waiting) as BinaryNode
|
||||
if(value.tag === 'stream:error') {
|
||||
if(value.attrs?.code !== '515') {
|
||||
throw new Boom('Authentication failed', { statusCode: +(value.attrs.code || 500) })
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(
|
||||
{ me: updatedCreds.me, platform: updatedCreds.platform },
|
||||
'registered connection, restart server'
|
||||
'pairing configured successfully, expect to restart the connection...'
|
||||
)
|
||||
|
||||
ev.emit('creds.update', updatedCreds)
|
||||
ev.emit('connection.update', { isNewLogin: true, qr: undefined })
|
||||
|
||||
end(new Boom('Restart Required', { statusCode: DisconnectReason.restartRequired }))
|
||||
|
||||
logger.warn('If your process stalls here, make sure to implement the reconnect logic as shown in ' +
|
||||
'https://github.com/adiwajshing/Baileys/blob/master/Example/example.ts#:~:text=reconnect')
|
||||
await sendNode(reply)
|
||||
} catch(error) {
|
||||
logger.info({ trace: error.stack }, 'error in pairing')
|
||||
end(error)
|
||||
@@ -530,12 +515,8 @@ export const makeSocket = ({
|
||||
ws.on('CB:stream:error', (node: BinaryNode) => {
|
||||
logger.error({ node }, 'stream errored out')
|
||||
|
||||
const [reasonNode] = getAllBinaryNodeChildren(node)
|
||||
const reason = reasonNode?.tag || 'unknown'
|
||||
const statusCode = +(
|
||||
node.attrs.code
|
||||
|| getErrorCodeFromStreamErrorReason(reason)
|
||||
)
|
||||
const { reason, statusCode } = getErrorCodeFromStreamError(node)
|
||||
|
||||
end(new Boom(`Stream Errored (${reason})`, { statusCode, data: node }))
|
||||
})
|
||||
// stream fail, possible logout
|
||||
|
||||
@@ -35,7 +35,7 @@ export enum DisconnectReason {
|
||||
timedOut = 408,
|
||||
loggedOut = 401,
|
||||
badSession = 500,
|
||||
restartRequired = 410,
|
||||
restartRequired = 515,
|
||||
multideviceMismatch = 411
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Logger } from 'pino'
|
||||
import { proto } from '../../WAProto'
|
||||
import { version as baileysVersion } from '../Defaults/baileys-version.json'
|
||||
import { CommonBaileysEventEmitter, ConnectionState, DisconnectReason, WAVersion } from '../Types'
|
||||
import { BinaryNode, getAllBinaryNodeChildren } from '../WABinary'
|
||||
|
||||
const PLATFORM_MAP = {
|
||||
'aix': 'AIX',
|
||||
@@ -273,6 +274,17 @@ const CODE_MAP: { [_: string]: DisconnectReason } = {
|
||||
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
||||
* @param reason the string reason given, eg. "conflict"
|
||||
*/
|
||||
export const getErrorCodeFromStreamErrorReason = (reason: string) => {
|
||||
return CODE_MAP[reason] || DisconnectReason.badSession
|
||||
export const getErrorCodeFromStreamError = (node: BinaryNode) => {
|
||||
const [reasonNode] = getAllBinaryNodeChildren(node)
|
||||
let reason = reasonNode?.tag || 'unknown'
|
||||
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession)
|
||||
|
||||
if(statusCode === DisconnectReason.restartRequired) {
|
||||
reason = 'restart required'
|
||||
}
|
||||
|
||||
return {
|
||||
reason,
|
||||
statusCode
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user