@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.
@@ -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, 1027934701];
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- reviver: (_, value) => {
18
- if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
19
- return Buffer.from(value.data, 'base64');
20
- }
21
- if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
22
- const keys = Object.keys(value);
23
- if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
24
- const values = Object.values(value);
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
- return value;
27
+ }
31
28
  }
32
- };
33
- export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participant || key?.remoteJid) || '';
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
- const pad = randomBytes(1);
36
- const padLength = (pad[0] & 0x0f) + 1;
37
- return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
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
- const t = new Uint8Array(e);
41
- if (0 === t.length) {
42
- throw new Error('unpadPkcs7 given empty bytes');
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
- var r = t[t.length - 1];
45
- if (r > t.length) {
46
- throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
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 new Uint8Array(t.buffer, t.byteOffset, t.length - r);
49
- };
50
- // code is inspired by whatsmeow
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
- 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
- return a;
68
- };
69
- export const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0;
70
- /** unix timestamp of a date in seconds */
71
- export const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
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
- let timeout;
74
- return {
75
- start: (newIntervalMs, newTask) => {
76
- task = newTask || task;
77
- intervalMs = newIntervalMs || intervalMs;
78
- timeout && clearTimeout(timeout);
79
- timeout = setTimeout(() => task?.(), intervalMs);
80
- },
81
- cancel: () => {
82
- timeout && clearTimeout(timeout);
83
- timeout = undefined;
84
- },
85
- setTask: (newTask) => (task = newTask),
86
- setInterval: (newInterval) => (intervalMs = newInterval)
87
- };
88
- };
89
- export const delay = (ms) => delayCancellable(ms).delay;
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
- const stack = new Error().stack;
92
- let timeout;
93
- let reject;
94
- const delay = new Promise((resolve, _reject) => {
95
- timeout = setTimeout(resolve, ms);
96
- reject = _reject;
97
- });
98
- const cancel = () => {
99
- clearTimeout(timeout);
100
- reject(new Boom('Cancelled', {
101
- statusCode: 500,
102
- data: {
103
- stack
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
- if (!ms) {
111
- return new Promise(promise);
112
- }
113
- const stack = new Error().stack;
114
- // Create a promise that rejects in <ms> milliseconds
115
- const { delay, cancel } = delayCancellable(ms);
116
- const p = new Promise((resolve, reject) => {
117
- delay
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
- // inspired from whatsmeow code
130
- // https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
138
+
131
139
  export const generateMessageIDV2 = (userId) => {
132
- const data = Buffer.alloc(8 + 20 + 16);
133
- data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
134
- if (userId) {
135
- const id = jidDecode(userId);
136
- if (id?.user) {
137
- data.write(id.user, 8);
138
- data.write('@c.us', 8 + id.user.length);
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
- const random = randomBytes(16);
142
- random.copy(data, 28);
143
- const hash = createHash('sha256').update(data).digest();
144
- return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18);
145
- };
146
- // generate a random ID to attach to a message
147
- export const generateMessageID = () => 'NEXUSTECHPRO-' + randomBytes(6).toString('hex').toUpperCase();
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
- return async (check, timeoutMs) => {
150
- let listener;
151
- let closeListener;
152
- await promiseTimeout(timeoutMs, (resolve, reject) => {
153
- closeListener = ({ connection, lastDisconnect }) => {
154
- if (connection === 'close') {
155
- reject(lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }));
156
- }
157
- };
158
- ev.on('connection.update', closeListener);
159
- listener = async (update) => {
160
- if (await check(update)) {
161
- resolve();
162
- }
163
- };
164
- ev.on(event, listener);
165
- }).finally(() => {
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
- export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
172
- /**
173
- * utility that fetches latest baileys version from the master branch.
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
- const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts';
178
- try {
179
- const response = await fetch(URL, {
180
- dispatcher: options.dispatcher,
181
- method: 'GET',
182
- headers: options.headers
183
- });
184
- if (!response.ok) {
185
- throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status });
186
- }
187
- const text = await response.text();
188
- // Extract version from line 7 (const version = [...])
189
- const lines = text.split('\n');
190
- const versionLine = lines[6]; // Line 7 (0-indexed)
191
- const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
192
- if (versionMatch) {
193
- const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
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
- try {
217
- // Absolute minimal headers required to bypass anti-bot detection
218
- const defaultHeaders = {
219
- 'sec-fetch-site': 'none',
220
- 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
221
- };
222
- const headers = { ...defaultHeaders, ...options.headers };
223
- const response = await fetch('https://web.whatsapp.com/sw.js', {
224
- ...options,
225
- method: 'GET',
226
- headers
227
- });
228
- if (!response.ok) {
229
- throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
230
- }
231
- const data = await response.text();
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
- const bytes = randomBytes(4);
260
- return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
261
- };
262
- const STATUS_MAP = {
263
- sender: proto.WebMessageInfo.Status.SERVER_ACK,
264
- played: proto.WebMessageInfo.Status.PLAYED,
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
- const status = STATUS_MAP[type];
274
- if (typeof type === 'undefined') {
275
- return proto.WebMessageInfo.Status.DELIVERY_ACK;
276
- }
277
- return status;
278
- };
279
- const CODE_MAP = {
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
- const [reasonNode] = getAllBinaryNodeChildren(node);
288
- let reason = reasonNode?.tag || 'unknown';
289
- const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession);
290
- if (statusCode === DisconnectReason.restartRequired) {
291
- reason = 'restart required';
292
- }
293
- return {
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
- let status;
300
- switch (tag) {
301
- case 'offer':
302
- case 'offer_notice':
303
- status = 'offer';
304
- break;
305
- case 'terminate':
306
- if (attrs.reason === 'timeout') {
307
- status = 'timeout';
308
- }
309
- else {
310
- //fired when accepted/rejected/timeout/caller hangs up
311
- status = 'terminate';
312
- }
313
- break;
314
- case 'reject':
315
- status = 'reject';
316
- break;
317
- case 'accept':
318
- status = 'accept';
319
- break;
320
- default:
321
- status = 'ringing';
322
- break;
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
- let statusCode = 500;
329
- if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
330
- const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
331
- if (!Number.isNaN(code) && code >= 400) {
332
- statusCode = code;
333
- }
334
- }
335
- else if (
336
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
337
- error?.code?.startsWith('E') ||
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
- for (const key in obj) {
354
- if (typeof obj[key] === 'undefined') {
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
- const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
277
+
278
+ const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ'
279
+
361
280
  export function bytesToCrockford(buffer) {
362
- let value = 0;
363
- let bitCount = 0;
364
- const crockford = [];
365
- for (const element of buffer) {
366
- value = (value << 8) | (element & 0xff);
367
- bitCount += 8;
368
- while (bitCount >= 5) {
369
- crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
370
- bitCount -= 5;
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
- return crockford.join('');
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
- return proto.Message.encode(message).finish();
380
- }
381
- //# sourceMappingURL=generics.js.map
297
+ return proto.Message.encode(message).finish()
298
+ }