@nexustechpro/baileys 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +306 -261
- package/WAProto/fix-imports.js +71 -19
- package/WAProto/index.js +65468 -137440
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.js +145 -114
- package/lib/Socket/Client/websocket.js +35 -131
- package/lib/Socket/communities.js +1 -1
- package/lib/Socket/messages-recv.js +364 -1003
- package/lib/Socket/messages-send.js +179 -576
- package/lib/Socket/newsletter.js +141 -293
- package/lib/Socket/nexus-handler.js +29 -7
- package/lib/Socket/socket.js +162 -644
- package/lib/Store/index.js +5 -5
- package/lib/Store/make-in-memory-store.js +2 -2
- package/lib/Types/Newsletter.js +29 -31
- package/lib/Utils/chat-utils.js +1 -1
- package/lib/Utils/crypto.js +21 -47
- package/lib/Utils/generics.js +269 -352
- package/lib/Utils/identity-chnage-handler.js +55 -0
- package/lib/Utils/index.js +1 -0
- package/lib/Utils/lt-hash.js +2 -2
- package/lib/Utils/messages-media.js +200 -15
- package/lib/Utils/messages.js +354 -287
- package/lib/Utils/noise-handler.js +200 -166
- package/lib/WABinary/generic-utils.js +94 -0
- package/lib/index.js +2 -2
- package/package.json +137 -120
package/lib/Utils/generics.js
CHANGED
|
@@ -1,381 +1,298 @@
|
|
|
1
|
-
import { Boom } from '@hapi/boom'
|
|
2
|
-
import { createHash, randomBytes } from 'crypto'
|
|
3
|
-
import { proto } from '../../WAProto/index.js'
|
|
4
|
-
const baileysVersion = [2, 3000,
|
|
5
|
-
import { DisconnectReason } from '../Types/index.js'
|
|
6
|
-
import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js'
|
|
7
|
-
import { sha256 } from './crypto.js'
|
|
1
|
+
import { Boom } from '@hapi/boom'
|
|
2
|
+
import { createHash, randomBytes } from 'crypto'
|
|
3
|
+
import { proto } from '../../WAProto/index.js'
|
|
4
|
+
const baileysVersion = [2, 3000, 1033105955]
|
|
5
|
+
import { DisconnectReason } from '../Types/index.js'
|
|
6
|
+
import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js'
|
|
7
|
+
import { sha256 } from './crypto.js'
|
|
8
|
+
|
|
8
9
|
export const BufferJSON = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (values.every(v => typeof v === 'number')) {
|
|
26
|
-
return Buffer.from(values);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
10
|
+
replacer: (k, value) => {
|
|
11
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
12
|
+
return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') }
|
|
13
|
+
}
|
|
14
|
+
return value
|
|
15
|
+
},
|
|
16
|
+
reviver: (_, value) => {
|
|
17
|
+
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
18
|
+
return Buffer.from(value.data, 'base64')
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
21
|
+
const keys = Object.keys(value)
|
|
22
|
+
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
23
|
+
const values = Object.values(value)
|
|
24
|
+
if (values.every(v => typeof v === 'number')) {
|
|
25
|
+
return Buffer.from(values)
|
|
29
26
|
}
|
|
30
|
-
|
|
27
|
+
}
|
|
31
28
|
}
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
return value
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || ''
|
|
34
|
+
|
|
35
|
+
export const isStringNullOrEmpty = (value) => value == null || value === ''
|
|
36
|
+
|
|
34
37
|
export const writeRandomPadMax16 = (msg) => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
38
|
+
const pad = randomBytes(1)
|
|
39
|
+
const padLength = (pad[0] & 0x0f) + 1
|
|
40
|
+
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)])
|
|
41
|
+
}
|
|
42
|
+
|
|
39
43
|
export const unpadRandomMax16 = (e) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
const t = new Uint8Array(e)
|
|
45
|
+
if (0 === t.length) throw new Error('unpadPkcs7 given empty bytes')
|
|
46
|
+
var r = t[t.length - 1]
|
|
47
|
+
if (r > t.length) throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`)
|
|
48
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const generateParticipantHashV2 = (participants) => {
|
|
52
|
+
participants.sort()
|
|
53
|
+
const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64')
|
|
54
|
+
return '2:' + sha256Hash.slice(0, 6)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const encodeWAMessage = (message) => {
|
|
58
|
+
try {
|
|
59
|
+
if (message && typeof message === 'object' && message.$type === proto.Message) {
|
|
60
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
43
61
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
if (message && typeof message === 'object') {
|
|
63
|
+
const protoMessage = proto.Message.fromObject(message)
|
|
64
|
+
return writeRandomPadMax16(proto.Message.encode(protoMessage).finish())
|
|
47
65
|
}
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
};
|
|
56
|
-
export const encodeWAMessage = (message) => writeRandomPadMax16(proto.Message.encode(message).finish());
|
|
57
|
-
export const generateRegistrationId = () => {
|
|
58
|
-
return Uint16Array.from(randomBytes(2))[0] & 16383;
|
|
59
|
-
};
|
|
60
|
-
export const encodeBigEndian = (e, t = 4) => {
|
|
61
|
-
let r = e;
|
|
62
|
-
const a = new Uint8Array(t);
|
|
63
|
-
for (let i = t - 1; i >= 0; i--) {
|
|
64
|
-
a[i] = 255 & r;
|
|
65
|
-
r >>>= 8;
|
|
66
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
67
|
+
} catch (error) {
|
|
68
|
+
try {
|
|
69
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
70
|
+
} catch (e) {
|
|
71
|
+
console.warn('Message encoding failed:', error.message, e.message)
|
|
72
|
+
throw new Error(`Failed to encode message: ${error.message}`)
|
|
66
73
|
}
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const generateRegistrationId = () => Uint16Array.from(randomBytes(2))[0] & 16383
|
|
78
|
+
|
|
79
|
+
export const encodeBigEndian = (e, t = 4) => {
|
|
80
|
+
let r = e
|
|
81
|
+
const a = new Uint8Array(t)
|
|
82
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
83
|
+
a[i] = 255 & r
|
|
84
|
+
r >>>= 8
|
|
85
|
+
}
|
|
86
|
+
return a
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0
|
|
90
|
+
|
|
91
|
+
export const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000)
|
|
92
|
+
|
|
72
93
|
export const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
94
|
+
let timeout
|
|
95
|
+
return {
|
|
96
|
+
start: (newIntervalMs, newTask) => {
|
|
97
|
+
task = newTask || task
|
|
98
|
+
intervalMs = newIntervalMs || intervalMs
|
|
99
|
+
timeout && clearTimeout(timeout)
|
|
100
|
+
timeout = setTimeout(() => task?.(), intervalMs)
|
|
101
|
+
},
|
|
102
|
+
cancel: () => {
|
|
103
|
+
timeout && clearTimeout(timeout)
|
|
104
|
+
timeout = undefined
|
|
105
|
+
},
|
|
106
|
+
setTask: (newTask) => (task = newTask),
|
|
107
|
+
setInterval: (newInterval) => (intervalMs = newInterval)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const delay = (ms) => delayCancellable(ms).delay
|
|
112
|
+
|
|
90
113
|
export const delayCancellable = (ms) => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}));
|
|
106
|
-
};
|
|
107
|
-
return { delay, cancel };
|
|
108
|
-
};
|
|
114
|
+
const stack = new Error().stack
|
|
115
|
+
let timeout
|
|
116
|
+
let reject
|
|
117
|
+
const delay = new Promise((resolve, _reject) => {
|
|
118
|
+
timeout = setTimeout(resolve, ms)
|
|
119
|
+
reject = _reject
|
|
120
|
+
})
|
|
121
|
+
const cancel = () => {
|
|
122
|
+
clearTimeout(timeout)
|
|
123
|
+
reject(new Boom('Cancelled', { statusCode: 500, data: { stack } }))
|
|
124
|
+
}
|
|
125
|
+
return { delay, cancel }
|
|
126
|
+
}
|
|
127
|
+
|
|
109
128
|
export async function promiseTimeout(ms, promise) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
.then(() => reject(new Boom('Timed Out', {
|
|
119
|
-
statusCode: DisconnectReason.timedOut,
|
|
120
|
-
data: {
|
|
121
|
-
stack
|
|
122
|
-
}
|
|
123
|
-
})))
|
|
124
|
-
.catch(err => reject(err));
|
|
125
|
-
promise(resolve, reject);
|
|
126
|
-
}).finally(cancel);
|
|
127
|
-
return p;
|
|
129
|
+
if (!ms) return new Promise(promise)
|
|
130
|
+
const stack = new Error().stack
|
|
131
|
+
const { delay, cancel } = delayCancellable(ms)
|
|
132
|
+
const p = new Promise((resolve, reject) => {
|
|
133
|
+
delay.then(() => reject(new Boom('Timed Out', { statusCode: DisconnectReason.timedOut, data: { stack } }))).catch(err => reject(err))
|
|
134
|
+
promise(resolve, reject)
|
|
135
|
+
}).finally(cancel)
|
|
136
|
+
return p
|
|
128
137
|
}
|
|
129
|
-
|
|
130
|
-
// https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
|
|
138
|
+
|
|
131
139
|
export const generateMessageIDV2 = (userId) => {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
+
const data = Buffer.alloc(8 + 20 + 16)
|
|
141
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)))
|
|
142
|
+
if (userId) {
|
|
143
|
+
const id = jidDecode(userId)
|
|
144
|
+
if (id?.user) {
|
|
145
|
+
data.write(id.user, 8)
|
|
146
|
+
data.write('@c.us', 8 + id.user.length)
|
|
140
147
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
+
}
|
|
149
|
+
const random = randomBytes(16)
|
|
150
|
+
random.copy(data, 28)
|
|
151
|
+
const hash = createHash('sha256').update(data).digest()
|
|
152
|
+
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase()
|
|
156
|
+
|
|
148
157
|
export function bindWaitForEvent(ev, event) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
ev.off(event, listener);
|
|
167
|
-
ev.off('connection.update', closeListener);
|
|
168
|
-
});
|
|
169
|
-
};
|
|
158
|
+
return async (check, timeoutMs) => {
|
|
159
|
+
let listener
|
|
160
|
+
let closeListener
|
|
161
|
+
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
162
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
|
163
|
+
if (connection === 'close') reject(lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }))
|
|
164
|
+
}
|
|
165
|
+
ev.on('connection.update', closeListener)
|
|
166
|
+
listener = async (update) => {
|
|
167
|
+
if (await check(update)) resolve()
|
|
168
|
+
}
|
|
169
|
+
ev.on(event, listener)
|
|
170
|
+
}).finally(() => {
|
|
171
|
+
ev.off(event, listener)
|
|
172
|
+
ev.off('connection.update', closeListener)
|
|
173
|
+
})
|
|
174
|
+
}
|
|
170
175
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
* Use to ensure your WA connection is always on the latest version
|
|
175
|
-
*/
|
|
176
|
+
|
|
177
|
+
export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update')
|
|
178
|
+
|
|
176
179
|
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
return {
|
|
195
|
-
version,
|
|
196
|
-
isLatest: true
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
throw new Error('Could not parse version from Defaults/index.ts');
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
catch (error) {
|
|
204
|
-
return {
|
|
205
|
-
version: baileysVersion,
|
|
206
|
-
isLatest: false,
|
|
207
|
-
error
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* A utility that fetches the latest web version of whatsapp.
|
|
213
|
-
* Use to ensure your WA connection is always on the latest version
|
|
214
|
-
*/
|
|
180
|
+
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts'
|
|
181
|
+
try {
|
|
182
|
+
const response = await fetch(URL, { dispatcher: options.dispatcher, method: 'GET', headers: options.headers })
|
|
183
|
+
if (!response.ok) throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status })
|
|
184
|
+
const text = await response.text()
|
|
185
|
+
const lines = text.split('\n')
|
|
186
|
+
const versionLine = lines[6]
|
|
187
|
+
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/)
|
|
188
|
+
if (versionMatch) {
|
|
189
|
+
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])]
|
|
190
|
+
return { version, isLatest: true }
|
|
191
|
+
} else throw new Error('Could not parse version from Defaults/index.ts')
|
|
192
|
+
} catch (error) {
|
|
193
|
+
return { version: baileysVersion, isLatest: false, error }
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
215
197
|
export const fetchLatestWaWebVersion = async (options = {}) => {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
233
|
-
const match = data.match(regex);
|
|
234
|
-
if (!match?.[1]) {
|
|
235
|
-
return {
|
|
236
|
-
version: baileysVersion,
|
|
237
|
-
isLatest: false,
|
|
238
|
-
error: {
|
|
239
|
-
message: 'Could not find client revision in the fetched content'
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
const clientRevision = match[1];
|
|
244
|
-
return {
|
|
245
|
-
version: [2, 3000, +clientRevision],
|
|
246
|
-
isLatest: true
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
catch (error) {
|
|
250
|
-
return {
|
|
251
|
-
version: baileysVersion,
|
|
252
|
-
isLatest: false,
|
|
253
|
-
error
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
/** unique message tag prefix for MD clients */
|
|
198
|
+
try {
|
|
199
|
+
const defaultHeaders = { 'sec-fetch-site': 'none', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' }
|
|
200
|
+
const headers = { ...defaultHeaders, ...options.headers }
|
|
201
|
+
const response = await fetch('https://web.whatsapp.com/sw.js', { ...options, method: 'GET', headers })
|
|
202
|
+
if (!response.ok) throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status })
|
|
203
|
+
const data = await response.text()
|
|
204
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/
|
|
205
|
+
const match = data.match(regex)
|
|
206
|
+
if (!match?.[1]) return { version: baileysVersion, isLatest: false, error: { message: 'Could not find client revision in the fetched content' } }
|
|
207
|
+
const clientRevision = match[1]
|
|
208
|
+
return { version: [2, 3000, +clientRevision], isLatest: true }
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return { version: baileysVersion, isLatest: false, error }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
258
214
|
export const generateMdTagPrefix = () => {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
read: proto.WebMessageInfo.Status.READ,
|
|
266
|
-
'read-self': proto.WebMessageInfo.Status.READ
|
|
267
|
-
};
|
|
268
|
-
/**
|
|
269
|
-
* Given a type of receipt, returns what the new status of the message should be
|
|
270
|
-
* @param type type from receipt
|
|
271
|
-
*/
|
|
215
|
+
const bytes = randomBytes(4)
|
|
216
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const STATUS_MAP = { sender: proto.WebMessageInfo.Status.SERVER_ACK, played: proto.WebMessageInfo.Status.PLAYED, read: proto.WebMessageInfo.Status.READ, 'read-self': proto.WebMessageInfo.Status.READ }
|
|
220
|
+
|
|
272
221
|
export const getStatusFromReceiptType = (type) => {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
conflict: DisconnectReason.connectionReplaced
|
|
281
|
-
};
|
|
282
|
-
/**
|
|
283
|
-
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
|
284
|
-
* @param reason the string reason given, eg. "conflict"
|
|
285
|
-
*/
|
|
222
|
+
const status = STATUS_MAP[type]
|
|
223
|
+
if (typeof type === 'undefined') return proto.WebMessageInfo.Status.DELIVERY_ACK
|
|
224
|
+
return status
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const CODE_MAP = { conflict: DisconnectReason.connectionReplaced }
|
|
228
|
+
|
|
286
229
|
export const getErrorCodeFromStreamError = (node) => {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
reason,
|
|
295
|
-
statusCode
|
|
296
|
-
};
|
|
297
|
-
};
|
|
230
|
+
const [reasonNode] = getAllBinaryNodeChildren(node)
|
|
231
|
+
let reason = reasonNode?.tag || 'unknown'
|
|
232
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession)
|
|
233
|
+
if (statusCode === DisconnectReason.restartRequired) reason = 'restart required'
|
|
234
|
+
return { reason, statusCode }
|
|
235
|
+
}
|
|
236
|
+
|
|
298
237
|
export const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
return status;
|
|
325
|
-
};
|
|
326
|
-
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
|
|
238
|
+
let status
|
|
239
|
+
switch (tag) {
|
|
240
|
+
case 'offer':
|
|
241
|
+
case 'offer_notice':
|
|
242
|
+
status = 'offer'
|
|
243
|
+
break
|
|
244
|
+
case 'terminate':
|
|
245
|
+
status = attrs.reason === 'timeout' ? 'timeout' : 'terminate'
|
|
246
|
+
break
|
|
247
|
+
case 'reject':
|
|
248
|
+
status = 'reject'
|
|
249
|
+
break
|
|
250
|
+
case 'accept':
|
|
251
|
+
status = 'accept'
|
|
252
|
+
break
|
|
253
|
+
default:
|
|
254
|
+
status = 'ringing'
|
|
255
|
+
break
|
|
256
|
+
}
|
|
257
|
+
return status
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: '
|
|
261
|
+
|
|
327
262
|
export const getCodeFromWSError = (error) => {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
error?.message?.includes('timed out')) {
|
|
339
|
-
// handle ETIMEOUT, ENOTFOUND etc
|
|
340
|
-
statusCode = 408;
|
|
341
|
-
}
|
|
342
|
-
return statusCode;
|
|
343
|
-
};
|
|
344
|
-
/**
|
|
345
|
-
* Is the given platform WA business
|
|
346
|
-
* @param platform AuthenticationCreds.platform
|
|
347
|
-
*/
|
|
348
|
-
export const isWABusinessPlatform = (platform) => {
|
|
349
|
-
return platform === 'smbi' || platform === 'smba';
|
|
350
|
-
};
|
|
351
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
263
|
+
let statusCode = 500
|
|
264
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
265
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length)
|
|
266
|
+
if (!Number.isNaN(code) && code >= 400) statusCode = code
|
|
267
|
+
} else if (error?.code?.startsWith('E') || error?.message?.includes('timed out')) statusCode = 408
|
|
268
|
+
return statusCode
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export const isWABusinessPlatform = (platform) => platform === 'smbi' || platform === 'smba'
|
|
272
|
+
|
|
352
273
|
export function trimUndefined(obj) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
delete obj[key];
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
return obj;
|
|
274
|
+
for (const key in obj) if (typeof obj[key] === 'undefined') delete obj[key]
|
|
275
|
+
return obj
|
|
359
276
|
}
|
|
360
|
-
|
|
277
|
+
|
|
278
|
+
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ'
|
|
279
|
+
|
|
361
280
|
export function bytesToCrockford(buffer) {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
if (bitCount > 0) {
|
|
374
|
-
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
|
281
|
+
let value = 0
|
|
282
|
+
let bitCount = 0
|
|
283
|
+
const crockford = []
|
|
284
|
+
for (const element of buffer) {
|
|
285
|
+
value = (value << 8) | (element & 0xff)
|
|
286
|
+
bitCount += 8
|
|
287
|
+
while (bitCount >= 5) {
|
|
288
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31))
|
|
289
|
+
bitCount -= 5
|
|
375
290
|
}
|
|
376
|
-
|
|
291
|
+
}
|
|
292
|
+
if (bitCount > 0) crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31))
|
|
293
|
+
return crockford.join('')
|
|
377
294
|
}
|
|
295
|
+
|
|
378
296
|
export function encodeNewsletterMessage(message) {
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
//# sourceMappingURL=generics.js.map
|
|
297
|
+
return proto.Message.encode(message).finish()
|
|
298
|
+
}
|