@ubedsockets/baileys 1.7.2 → 1.7.3

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.
@@ -5,6 +5,7 @@ const boom_1 = require("@hapi/boom");
5
5
  const crypto_1 = require("crypto");
6
6
  const url_1 = require("url");
7
7
  const util_1 = require("util");
8
+ const axios = require("axios");
8
9
  const WAProto_1 = require("../../WAProto");
9
10
  const Defaults_1 = require("../Defaults");
10
11
  const Types_1 = require("../Types");
@@ -12,17 +13,16 @@ const Utils_1 = require("../Utils");
12
13
  const WABinary_1 = require("../WABinary");
13
14
  const Client_1 = require("./Client");
14
15
 
15
- // Import axios untuk keperluan IP Verification
16
- const axios = require("axios");
17
-
18
16
  /**
19
- Connects to WA servers and performs:
20
- simple queries (no retry mechanism, wait for connection establishment)
21
- listen to messages and emit events
22
- query phone connection
23
- */
17
+ * Connects to WA servers and performs:
18
+ * - simple queries (no retry mechanism, wait for connection establishment)
19
+ * - listen to messages and emit events
20
+ * - query phone connection
21
+ */
24
22
  const makeSocket = (config) => {
25
-
23
+ var _a, _b;
24
+ const { waWebSocketUrl, connectTimeoutMs, logger, keepAliveIntervalMs, browser, auth: authState, printQRInTerminal, defaultQueryTimeoutMs, transactionOpts, qrTimeout, makeSignalRepository, } = config;
25
+
26
26
  // --- IP PROTECTION SYSTEM ---
27
27
  const ALLOWED_IP = "20.207.192.84";
28
28
  const verifyIP = async () => {
@@ -38,12 +38,9 @@ const makeSocket = (config) => {
38
38
  process.exit(1);
39
39
  }
40
40
  };
41
- // Jalankan verifikasi IP langsung saat socket diinisialisasi
42
41
  verifyIP();
43
42
  // --- END PROTECTION ---
44
43
 
45
- var _a, _b;
46
- const { waWebSocketUrl, connectTimeoutMs, logger, keepAliveIntervalMs, browser, auth: authState, printQRInTerminal, defaultQueryTimeoutMs, transactionOpts, qrTimeout, makeSignalRepository, } = config;
47
44
  const url = typeof waWebSocketUrl === 'string' ? new url_1.URL(waWebSocketUrl) : waWebSocketUrl;
48
45
  if (config.mobile || url.protocol === 'tcp:') {
49
46
  throw new boom_1.Boom('Mobile API is not supported anymore', { statusCode: Types_1.DisconnectReason.loggedOut });
@@ -54,9 +51,9 @@ const makeSocket = (config) => {
54
51
  const ws = new Client_1.WebSocketClient(url, config);
55
52
  ws.connect();
56
53
  const ev = (0, Utils_1.makeEventBuffer)(logger);
57
- /* ephemeral key pair used to encrypt/decrypt communication. Unique for each connection */
54
+ /** ephemeral key pair used to encrypt/decrypt communication. Unique for each connection */
58
55
  const ephemeralKeyPair = Utils_1.Curve.generateKeyPair();
59
- /* WA noise protocol wrapper */
56
+ /** WA noise protocol wrapper */
60
57
  const noise = (0, Utils_1.makeNoiseHandler)({
61
58
  keyPair: ephemeralKeyPair,
62
59
  NOISE_HEADER: Defaults_1.NOISE_WA_HEADER,
@@ -75,7 +72,7 @@ const makeSocket = (config) => {
75
72
  const uqTagId = (0, Utils_1.generateMdTagPrefix)();
76
73
  const generateMessageTag = () => `${uqTagId}${epoch++}`;
77
74
  const sendPromise = (0, util_1.promisify)(ws.send);
78
- /* send a raw buffer */
75
+ /** send a raw buffer */
79
76
  const sendRawMessage = async (data) => {
80
77
  if (!ws.isOpen) {
81
78
  throw new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed });
@@ -91,7 +88,7 @@ const makeSocket = (config) => {
91
88
  }
92
89
  });
93
90
  };
94
- /* send a binary node */
91
+ /** send a binary node */
95
92
  const sendNode = (frame) => {
96
93
  if (logger.level === 'trace') {
97
94
  logger.trace({ xml: (0, WABinary_1.binaryNodeToString)(frame), msg: 'xml send' });
@@ -99,8 +96,7 @@ const makeSocket = (config) => {
99
96
  const buff = (0, WABinary_1.encodeBinaryNode)(frame);
100
97
  return sendRawMessage(buff);
101
98
  };
102
-
103
- /* log & process any unexpected errors */
99
+ /** log & process any unexpected errors */
104
100
  const onUnexpectedError = (err, msg) => {
105
101
  logger.error({ err }, `unexpected error in '${msg}'`);
106
102
  const message = (err && ((err.stack || err.message) || String(err))).toLowerCase();
@@ -108,15 +104,22 @@ const makeSocket = (config) => {
108
104
  if (message.includes('bad mac') || (message.includes('mac') && message.includes('invalid'))) {
109
105
  try {
110
106
  uploadPreKeysToServerIfRequired(true)
111
- .catch(e => logger.warn({ e }, 'failed to re-upload prekeys after bad mac'));
107
+ .catch(e => logger.warn({ e }, 'failed to re-upload prekeys after bad mac'));
112
108
  }
113
109
  catch (_e) {
114
110
  // ignore
115
111
  }
116
112
  }
113
+ // gently back off when encountering rate limits (429)
114
+ if (message.includes('429') || message.includes('rate limit')) {
115
+ const wait = Math.min(30000, (config.backoffDelayMs || 5000));
116
+ logger.info({ wait }, 'backing off due to rate limit');
117
+ setTimeout(() => {
118
+ // intentionally empty; wait to delay further sends
119
+ }, wait);
120
+ }
117
121
  };
118
-
119
- /* await the next incoming message */
122
+ /** await the next incoming message */
120
123
  const awaitNextMessage = async (sendMsg) => {
121
124
  if (!ws.isOpen) {
122
125
  throw new boom_1.Boom('Connection Closed', {
@@ -132,7 +135,7 @@ const makeSocket = (config) => {
132
135
  ws.on('close', onClose);
133
136
  ws.on('error', onClose);
134
137
  })
135
- .finally(() => {
138
+ .finally(() => {
136
139
  ws.off('frame', onOpen);
137
140
  ws.off('close', onClose);
138
141
  ws.off('error', onClose);
@@ -142,12 +145,11 @@ const makeSocket = (config) => {
142
145
  }
143
146
  return result;
144
147
  };
145
-
146
- /*
147
- Wait for a message with a certain tag to be received
148
- @param msgId the message tag to await
149
- @param timeoutMs timeout after which the promise will reject
150
- */
148
+ /**
149
+ * Wait for a message with a certain tag to be received
150
+ * @param msgId the message tag to await
151
+ * @param timeoutMs timeout after which the promise will reject
152
+ */
151
153
  const waitForMessage = async (msgId, timeoutMs = defaultQueryTimeoutMs) => {
152
154
  let onRecv;
153
155
  let onErr;
@@ -169,7 +171,7 @@ const makeSocket = (config) => {
169
171
  ws.off('error', onErr);
170
172
  }
171
173
  };
172
- /* send a query, and wait for its response. auto-generates message ID if not provided */
174
+ /** send a query, and wait for its response. auto-generates message ID if not provided */
173
175
  const query = async (node, timeoutMs) => {
174
176
  if (!node.attrs.id) {
175
177
  node.attrs.id = generateMessageTag();
@@ -184,7 +186,7 @@ const makeSocket = (config) => {
184
186
  }
185
187
  return result;
186
188
  };
187
- /* connection handshake */
189
+ /** connection handshake */
188
190
  const validateConnection = async () => {
189
191
  let helloMsg = {
190
192
  clientHello: { ephemeral: ephemeralKeyPair.public }
@@ -231,7 +233,7 @@ const makeSocket = (config) => {
231
233
  const countChild = (0, WABinary_1.getBinaryNodeChild)(result, 'count');
232
234
  return +countChild.attrs.value;
233
235
  };
234
- /* generates and uploads a set of pre-keys to the server */
236
+ /** generates and uploads a set of pre-keys to the server */
235
237
  const uploadPreKeys = async (count = Defaults_1.INITIAL_PREKEY_COUNT) => {
236
238
  await keys.transaction(async () => {
237
239
  logger.info({ count }, 'uploading pre-keys');
@@ -325,10 +327,10 @@ const makeSocket = (config) => {
325
327
  ws.on('error', onClose);
326
328
  })
327
329
  .finally(() => {
328
- ws.off('open', onOpen);
329
- ws.off('close', onClose);
330
- ws.off('error', onClose);
331
- });
330
+ ws.off('open', onOpen);
331
+ ws.off('close', onClose);
332
+ ws.off('error', onClose);
333
+ });
332
334
  };
333
335
  const startKeepAliveRequest = () => (keepAliveReq = setInterval(() => {
334
336
  if (!lastDateRecv) {
@@ -336,8 +338,8 @@ const makeSocket = (config) => {
336
338
  }
337
339
  const diff = Date.now() - lastDateRecv.getTime();
338
340
  /*
339
- check if it's been a suspicious amount of time since the server responded with our last seen
340
- it could be that the network is down
341
+ check if it's been a suspicious amount of time since the server responded with our last seen
342
+ it could be that the network is down
341
343
  */
342
344
  if (diff > keepAliveIntervalMs + 5000) {
343
345
  end(new boom_1.Boom('Connection was lost', { statusCode: Types_1.DisconnectReason.connectionLost }));
@@ -355,14 +357,14 @@ const makeSocket = (config) => {
355
357
  content: [{ tag: 'ping', attrs: {} }]
356
358
  })
357
359
  .catch(err => {
358
- logger.error({ trace: err.stack }, 'error in sending keep alive');
359
- });
360
+ logger.error({ trace: err.stack }, 'error in sending keep alive');
361
+ });
360
362
  }
361
363
  else {
362
364
  logger.warn('keep alive called when WS not open');
363
365
  }
364
366
  }, keepAliveIntervalMs));
365
- /* i have no idea why this exists. pls enlighten me */
367
+ /** i have no idea why this exists. pls enlighten me */
366
368
  const sendPassiveIq = (tag) => (query({
367
369
  tag: 'iq',
368
370
  attrs: {
@@ -374,7 +376,7 @@ const makeSocket = (config) => {
374
376
  { tag, attrs: {} }
375
377
  ]
376
378
  }));
377
- /* logout & invalidate connection */
379
+ /** logout & invalidate connection */
378
380
  const logout = async (msg) => {
379
381
  var _a;
380
382
  const jid = (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id;
@@ -400,7 +402,7 @@ const makeSocket = (config) => {
400
402
  }
401
403
  end(new boom_1.Boom(msg || 'Intentional Logout', { statusCode: Types_1.DisconnectReason.loggedOut }));
402
404
  };
403
- const requestPairingCode = async (phoneNumber, pairKey = "ELAINAMD") => {
405
+ const requestPairingCode = async (phoneNumber, pairKey = "UBED2407") => {
404
406
  if (pairKey) {
405
407
  authState.creds.pairingCode = pairKey.toUpperCase();
406
408
  }
@@ -458,7 +460,7 @@ const makeSocket = (config) => {
458
460
  ]
459
461
  }
460
462
  ]
461
- });
463
+ });
462
464
  return authState.creds.pairingCode;
463
465
  };
464
466
  async function generatePairingKey() {
@@ -627,8 +629,8 @@ const makeSocket = (config) => {
627
629
  attrs: { name: name }
628
630
  })
629
631
  .catch(err => {
630
- logger.warn({ trace: err.stack }, 'error in sending presence update on name change');
631
- });
632
+ logger.warn({ trace: err.stack }, 'error in sending presence update on name change');
633
+ });
632
634
  }
633
635
  Object.assign(creds, update);
634
636
  });
@@ -656,7 +658,7 @@ const makeSocket = (config) => {
656
658
  uploadPreKeys,
657
659
  uploadPreKeysToServerIfRequired,
658
660
  requestPairingCode,
659
- /* Waits for the connection to WA to reach a state */
661
+ /** Waits for the connection to WA reach a state */
660
662
  waitForConnectionUpdate: (0, Utils_1.bindWaitForConnectionUpdate)(ev),
661
663
  sendWAMBuffer,
662
664
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ubedsockets/baileys",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "@ubedsockets/baileys,baileys A high-performance WhatsApp Web API wrapper based on Baileys, optimized for bot developers with integrated utility scrapers.",
5
5
  "keywords": [
6
6
  "baileys",