@ubedsockets/baileys 1.6.6 → 1.6.8

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.
Files changed (87) hide show
  1. package/WAProto/GenerateStatics.sh +4 -3
  2. package/WAProto/WAProto.proto +511 -1215
  3. package/WAProto/index.js +131318 -59331
  4. package/lib/Defaults/index.js +92 -76
  5. package/lib/Signal/Group/ciphertext-message.js +5 -2
  6. package/lib/Signal/Group/group-session-builder.js +41 -7
  7. package/lib/Signal/Group/group_cipher.js +51 -37
  8. package/lib/Signal/Group/index.js +57 -12
  9. package/lib/Signal/Group/keyhelper.js +44 -7
  10. package/lib/Signal/Group/sender-chain-key.js +15 -7
  11. package/lib/Signal/Group/sender-key-distribution-message.js +11 -8
  12. package/lib/Signal/Group/sender-key-message.js +12 -9
  13. package/lib/Signal/Group/sender-key-name.js +5 -2
  14. package/lib/Signal/Group/sender-key-record.js +21 -9
  15. package/lib/Signal/Group/sender-key-state.js +42 -27
  16. package/lib/Signal/Group/sender-message-key.js +7 -4
  17. package/lib/Signal/libsignal.js +90 -347
  18. package/lib/Socket/Client/index.js +18 -3
  19. package/lib/Socket/Client/types.js +6 -4
  20. package/lib/Socket/Client/websocket.js +78 -21
  21. package/lib/Socket/business.js +43 -162
  22. package/lib/Socket/chats.js +249 -571
  23. package/lib/Socket/groups.js +99 -114
  24. package/lib/Socket/index.js +9 -11
  25. package/lib/Socket/messages-recv.js +559 -1277
  26. package/lib/Socket/messages-send.js +669 -674
  27. package/lib/Socket/newsletter.js +286 -149
  28. package/lib/Socket/socket.js +235 -548
  29. package/lib/Types/Auth.js +2 -2
  30. package/lib/Types/Call.js +2 -2
  31. package/lib/Types/Chat.js +4 -8
  32. package/lib/Types/Contact.js +2 -2
  33. package/lib/Types/Events.js +2 -2
  34. package/lib/Types/GroupMetadata.js +2 -2
  35. package/lib/Types/Label.js +5 -3
  36. package/lib/Types/LabelAssociation.js +5 -3
  37. package/lib/Types/Message.js +7 -11
  38. package/lib/Types/Product.js +2 -2
  39. package/lib/Types/Signal.js +2 -2
  40. package/lib/Types/Socket.js +2 -3
  41. package/lib/Types/State.js +2 -56
  42. package/lib/Types/USync.js +2 -2
  43. package/lib/Types/index.js +31 -15
  44. package/lib/Utils/auth-utils.js +136 -239
  45. package/lib/Utils/business.js +69 -66
  46. package/lib/Utils/chat-utils.js +238 -380
  47. package/lib/Utils/crypto.js +127 -56
  48. package/lib/Utils/decode-wa-message.js +63 -173
  49. package/lib/Utils/event-buffer.js +70 -174
  50. package/lib/Utils/generics.js +207 -159
  51. package/lib/Utils/history.js +53 -93
  52. package/lib/Utils/index.js +33 -22
  53. package/lib/Utils/link-preview.js +58 -17
  54. package/lib/Utils/logger.js +7 -3
  55. package/lib/Utils/lt-hash.js +46 -3
  56. package/lib/Utils/make-mutex.js +34 -24
  57. package/lib/Utils/messages-media.js +744 -483
  58. package/lib/Utils/messages.js +621 -411
  59. package/lib/Utils/noise-handler.js +99 -149
  60. package/lib/Utils/process-message.js +150 -459
  61. package/lib/Utils/signal.js +73 -112
  62. package/lib/Utils/use-multi-file-auth-state.js +27 -23
  63. package/lib/Utils/validate-connection.js +104 -74
  64. package/lib/WABinary/constants.js +11 -9
  65. package/lib/WABinary/decode.js +52 -49
  66. package/lib/WABinary/encode.js +49 -19
  67. package/lib/WABinary/generic-utils.js +43 -46
  68. package/lib/WABinary/index.js +21 -6
  69. package/lib/WABinary/jid-utils.js +45 -72
  70. package/lib/WABinary/types.js +2 -2
  71. package/lib/WAM/BinaryInfo.js +5 -2
  72. package/lib/WAM/constants.js +11958 -19461
  73. package/lib/WAM/encode.js +22 -17
  74. package/lib/WAM/index.js +19 -4
  75. package/lib/WAUSync/Protocols/USyncContactProtocol.js +13 -33
  76. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +14 -11
  77. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +11 -8
  78. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +13 -9
  79. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +22 -20
  80. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +8 -13
  81. package/lib/WAUSync/Protocols/index.js +20 -6
  82. package/lib/WAUSync/USyncQuery.js +35 -44
  83. package/lib/WAUSync/USyncUser.js +5 -10
  84. package/lib/WAUSync/index.js +19 -4
  85. package/lib/index.js +70 -11
  86. package/package.json +60 -100
  87. package/engine-requirements.js +0 -10
@@ -1,54 +1,111 @@
1
- import WebSocket from 'ws';
2
- import { DEFAULT_ORIGIN } from '../../Defaults/index.js';
3
- import { AbstractSocketClient } from './types.js';
4
- export class WebSocketClient extends AbstractSocketClient {
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.WebSocketClient = void 0;
7
+ const ws_1 = __importDefault(require("ws"));
8
+ const Defaults_1 = require("../../Defaults");
9
+ const types_1 = require("./types");
10
+ class WebSocketClient extends types_1.AbstractSocketClient {
5
11
  constructor() {
6
12
  super(...arguments);
7
13
  this.socket = null;
14
+ // queue & dispatch variables for throttling outgoing messages to avoid rate limits
15
+ this._queue = [];
16
+ this._isDispatching = false;
17
+ this._lastDispatch = 0;
18
+ this._minSendIntervalMs = (Defaults_1.DEFAULT_CONNECTION_CONFIG && Defaults_1.DEFAULT_CONNECTION_CONFIG.minSendIntervalMs) || 50;
8
19
  }
9
20
  get isOpen() {
10
- return this.socket?.readyState === WebSocket.OPEN;
21
+ var _a;
22
+ return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN;
11
23
  }
12
24
  get isClosed() {
13
- return this.socket === null || this.socket?.readyState === WebSocket.CLOSED;
25
+ var _a;
26
+ return this.socket === null || ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.CLOSED;
14
27
  }
15
28
  get isClosing() {
16
- return this.socket === null || this.socket?.readyState === WebSocket.CLOSING;
29
+ var _a;
30
+ return this.socket === null || ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.CLOSING;
17
31
  }
18
32
  get isConnecting() {
19
- return this.socket?.readyState === WebSocket.CONNECTING;
33
+ var _a;
34
+ return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.CONNECTING;
20
35
  }
21
- connect() {
36
+ async connect() {
37
+ var _a, _b;
22
38
  if (this.socket) {
23
39
  return;
24
40
  }
25
- this.socket = new WebSocket(this.url, {
26
- origin: DEFAULT_ORIGIN,
27
- headers: this.config.options?.headers,
41
+ this.socket = new ws_1.default(this.url, {
42
+ origin: Defaults_1.DEFAULT_ORIGIN,
43
+ headers: (_a = this.config.options) === null || _a === void 0 ? void 0 : _a.headers,
28
44
  handshakeTimeout: this.config.connectTimeoutMs,
29
45
  timeout: this.config.connectTimeoutMs,
30
- agent: this.config.agent
46
+ agent: this.config.agent,
31
47
  });
32
48
  this.socket.setMaxListeners(0);
33
49
  const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response'];
34
50
  for (const event of events) {
35
- this.socket?.on(event, (...args) => this.emit(event, ...args));
51
+ (_b = this.socket) === null || _b === void 0 ? void 0 : _b.on(event, (...args) => this.emit(event, ...args));
36
52
  }
37
53
  }
38
54
  async close() {
39
55
  if (!this.socket) {
40
56
  return;
41
57
  }
42
- const closePromise = new Promise(resolve => {
43
- this.socket?.once('close', resolve);
44
- });
45
58
  this.socket.close();
46
- await closePromise;
47
59
  this.socket = null;
48
60
  }
61
+ async restart() {
62
+ if (this.socket) {
63
+ await new Promise(resolve => {
64
+ this.socket.once('close', resolve);
65
+ this.socket.terminate();
66
+ });
67
+ this.socket = null;
68
+ }
69
+ await this.connect();
70
+ }
49
71
  send(str, cb) {
50
- this.socket?.send(str, cb);
51
- return Boolean(this.socket);
72
+ // throttle sends to reduce rate-limit likelihood
73
+ const doSend = () => {
74
+ var _a;
75
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(str, cb);
76
+ return Boolean(this.socket);
77
+ };
78
+ this._queue.push(doSend);
79
+ this._dispatch();
80
+ return true;
81
+ }
82
+
83
+ _dispatch() {
84
+ if (this._isDispatching) {
85
+ return;
86
+ }
87
+ this._isDispatching = true;
88
+ const tick = () => {
89
+ const now = Date.now();
90
+ if (this._queue.length === 0) {
91
+ this._isDispatching = false;
92
+ return;
93
+ }
94
+ const delta = now - this._lastDispatch;
95
+ const wait = Math.max(0, this._minSendIntervalMs - delta);
96
+ setTimeout(() => {
97
+ const fn = this._queue.shift();
98
+ this._lastDispatch = Date.now();
99
+ try {
100
+ fn && fn();
101
+ }
102
+ catch (_err) {
103
+ // ignore send errors here; they'll surface elsewhere
104
+ }
105
+ tick();
106
+ }, wait);
107
+ };
108
+ tick();
52
109
  }
53
110
  }
54
- //# sourceMappingURL=websocket.js.map
111
+ exports.WebSocketClient = WebSocketClient;
@@ -1,134 +1,17 @@
1
- import { getRawMediaUploadData } from '../Utils/index.js';
2
- import { parseCatalogNode, parseCollectionsNode, parseOrderDetailsNode, parseProductNode, toProductNode, uploadingNecessaryImagesOfProduct } from '../Utils/business.js';
3
- import { jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary/index.js';
4
- import { getBinaryNodeChild } from '../WABinary/generic-utils.js';
5
- import { makeMessagesRecvSocket } from './messages-recv.js';
6
- export const makeBusinessSocket = (config) => {
7
- const sock = makeMessagesRecvSocket(config);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeBusinessSocket = void 0;
4
+ const business_1 = require("../Utils/business");
5
+ const WABinary_1 = require("../WABinary");
6
+ const generic_utils_1 = require("../WABinary/generic-utils");
7
+ const messages_recv_1 = require("./messages-recv");
8
+ const makeBusinessSocket = (config) => {
9
+ const sock = (0, messages_recv_1.makeMessagesRecvSocket)(config);
8
10
  const { authState, query, waUploadToServer } = sock;
9
- const updateBussinesProfile = async (args) => {
10
- const node = [];
11
- const simpleFields = ['address', 'email', 'description'];
12
- node.push(...simpleFields
13
- .filter(key => args[key] !== undefined && args[key] !== null)
14
- .map(key => ({
15
- tag: key,
16
- attrs: {},
17
- content: args[key]
18
- })));
19
- if (args.websites !== undefined) {
20
- node.push(...args.websites.map(website => ({
21
- tag: 'website',
22
- attrs: {},
23
- content: website
24
- })));
25
- }
26
- if (args.hours !== undefined) {
27
- node.push({
28
- tag: 'business_hours',
29
- attrs: { timezone: args.hours.timezone },
30
- content: args.hours.days.map(dayConfig => {
31
- const base = {
32
- tag: 'business_hours_config',
33
- attrs: {
34
- day_of_week: dayConfig.day,
35
- mode: dayConfig.mode
36
- }
37
- };
38
- if (dayConfig.mode === 'specific_hours') {
39
- return {
40
- ...base,
41
- attrs: {
42
- ...base.attrs,
43
- open_time: dayConfig.openTimeInMinutes,
44
- close_time: dayConfig.closeTimeInMinutes
45
- }
46
- };
47
- }
48
- return base;
49
- })
50
- });
51
- }
52
- const result = await query({
53
- tag: 'iq',
54
- attrs: {
55
- to: S_WHATSAPP_NET,
56
- type: 'set',
57
- xmlns: 'w:biz'
58
- },
59
- content: [
60
- {
61
- tag: 'business_profile',
62
- attrs: {
63
- v: '3',
64
- mutation_type: 'delta'
65
- },
66
- content: node
67
- }
68
- ]
69
- });
70
- return result;
71
- };
72
- const updateCoverPhoto = async (photo) => {
73
- const { fileSha256, filePath } = await getRawMediaUploadData(photo, 'biz-cover-photo');
74
- const fileSha256B64 = fileSha256.toString('base64');
75
- const { meta_hmac, fbid, ts } = await waUploadToServer(filePath, {
76
- fileEncSha256B64: fileSha256B64,
77
- mediaType: 'biz-cover-photo'
78
- });
79
- await query({
80
- tag: 'iq',
81
- attrs: {
82
- to: S_WHATSAPP_NET,
83
- type: 'set',
84
- xmlns: 'w:biz'
85
- },
86
- content: [
87
- {
88
- tag: 'business_profile',
89
- attrs: {
90
- v: '3',
91
- mutation_type: 'delta'
92
- },
93
- content: [
94
- {
95
- tag: 'cover_photo',
96
- attrs: { id: String(fbid), op: 'update', token: meta_hmac, ts: String(ts) }
97
- }
98
- ]
99
- }
100
- ]
101
- });
102
- return fbid;
103
- };
104
- const removeCoverPhoto = async (id) => {
105
- return await query({
106
- tag: 'iq',
107
- attrs: {
108
- to: S_WHATSAPP_NET,
109
- type: 'set',
110
- xmlns: 'w:biz'
111
- },
112
- content: [
113
- {
114
- tag: 'business_profile',
115
- attrs: {
116
- v: '3',
117
- mutation_type: 'delta'
118
- },
119
- content: [
120
- {
121
- tag: 'cover_photo',
122
- attrs: { op: 'delete', id }
123
- }
124
- ]
125
- }
126
- ]
127
- });
128
- };
129
11
  const getCatalog = async ({ jid, limit, cursor }) => {
130
- jid = jid || authState.creds.me?.id;
131
- jid = jidNormalizedUser(jid);
12
+ var _a;
13
+ jid = jid || ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);
14
+ jid = (0, WABinary_1.jidNormalizedUser)(jid);
132
15
  const queryParamNodes = [
133
16
  {
134
17
  tag: 'limit',
@@ -144,7 +27,7 @@ export const makeBusinessSocket = (config) => {
144
27
  tag: 'height',
145
28
  attrs: {},
146
29
  content: Buffer.from('100')
147
- }
30
+ },
148
31
  ];
149
32
  if (cursor) {
150
33
  queryParamNodes.push({
@@ -156,7 +39,7 @@ export const makeBusinessSocket = (config) => {
156
39
  const result = await query({
157
40
  tag: 'iq',
158
41
  attrs: {
159
- to: S_WHATSAPP_NET,
42
+ to: WABinary_1.S_WHATSAPP_NET,
160
43
  type: 'get',
161
44
  xmlns: 'w:biz:catalog'
162
45
  },
@@ -165,30 +48,31 @@ export const makeBusinessSocket = (config) => {
165
48
  tag: 'product_catalog',
166
49
  attrs: {
167
50
  jid,
168
- allow_shop_source: 'true'
51
+ 'allow_shop_source': 'true'
169
52
  },
170
53
  content: queryParamNodes
171
54
  }
172
55
  ]
173
56
  });
174
- return parseCatalogNode(result);
57
+ return (0, business_1.parseCatalogNode)(result);
175
58
  };
176
59
  const getCollections = async (jid, limit = 51) => {
177
- jid = jid || authState.creds.me?.id;
178
- jid = jidNormalizedUser(jid);
60
+ var _a;
61
+ jid = jid || ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);
62
+ jid = (0, WABinary_1.jidNormalizedUser)(jid);
179
63
  const result = await query({
180
64
  tag: 'iq',
181
65
  attrs: {
182
- to: S_WHATSAPP_NET,
66
+ to: WABinary_1.S_WHATSAPP_NET,
183
67
  type: 'get',
184
68
  xmlns: 'w:biz:catalog',
185
- smax_id: '35'
69
+ 'smax_id': '35'
186
70
  },
187
71
  content: [
188
72
  {
189
73
  tag: 'collections',
190
74
  attrs: {
191
- biz_jid: jid
75
+ 'biz_jid': jid,
192
76
  },
193
77
  content: [
194
78
  {
@@ -215,16 +99,16 @@ export const makeBusinessSocket = (config) => {
215
99
  }
216
100
  ]
217
101
  });
218
- return parseCollectionsNode(result);
102
+ return (0, business_1.parseCollectionsNode)(result);
219
103
  };
220
104
  const getOrderDetails = async (orderId, tokenBase64) => {
221
105
  const result = await query({
222
106
  tag: 'iq',
223
107
  attrs: {
224
- to: S_WHATSAPP_NET,
108
+ to: WABinary_1.S_WHATSAPP_NET,
225
109
  type: 'get',
226
110
  xmlns: 'fb:thrift_iq',
227
- smax_id: '5'
111
+ 'smax_id': '5'
228
112
  },
229
113
  content: [
230
114
  {
@@ -259,15 +143,15 @@ export const makeBusinessSocket = (config) => {
259
143
  }
260
144
  ]
261
145
  });
262
- return parseOrderDetailsNode(result);
146
+ return (0, business_1.parseOrderDetailsNode)(result);
263
147
  };
264
148
  const productUpdate = async (productId, update) => {
265
- update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer);
266
- const editNode = toProductNode(productId, update);
149
+ update = await (0, business_1.uploadingNecessaryImagesOfProduct)(update, waUploadToServer);
150
+ const editNode = (0, business_1.toProductNode)(productId, update);
267
151
  const result = await query({
268
152
  tag: 'iq',
269
153
  attrs: {
270
- to: S_WHATSAPP_NET,
154
+ to: WABinary_1.S_WHATSAPP_NET,
271
155
  type: 'set',
272
156
  xmlns: 'w:biz:catalog'
273
157
  },
@@ -291,19 +175,19 @@ export const makeBusinessSocket = (config) => {
291
175
  }
292
176
  ]
293
177
  });
294
- const productCatalogEditNode = getBinaryNodeChild(result, 'product_catalog_edit');
295
- const productNode = getBinaryNodeChild(productCatalogEditNode, 'product');
296
- return parseProductNode(productNode);
178
+ const productCatalogEditNode = (0, generic_utils_1.getBinaryNodeChild)(result, 'product_catalog_edit');
179
+ const productNode = (0, generic_utils_1.getBinaryNodeChild)(productCatalogEditNode, 'product');
180
+ return (0, business_1.parseProductNode)(productNode);
297
181
  };
298
182
  const productCreate = async (create) => {
299
183
  // ensure isHidden is defined
300
184
  create.isHidden = !!create.isHidden;
301
- create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer);
302
- const createNode = toProductNode(undefined, create);
185
+ create = await (0, business_1.uploadingNecessaryImagesOfProduct)(create, waUploadToServer);
186
+ const createNode = (0, business_1.toProductNode)(undefined, create);
303
187
  const result = await query({
304
188
  tag: 'iq',
305
189
  attrs: {
306
- to: S_WHATSAPP_NET,
190
+ to: WABinary_1.S_WHATSAPP_NET,
307
191
  type: 'set',
308
192
  xmlns: 'w:biz:catalog'
309
193
  },
@@ -327,15 +211,15 @@ export const makeBusinessSocket = (config) => {
327
211
  }
328
212
  ]
329
213
  });
330
- const productCatalogAddNode = getBinaryNodeChild(result, 'product_catalog_add');
331
- const productNode = getBinaryNodeChild(productCatalogAddNode, 'product');
332
- return parseProductNode(productNode);
214
+ const productCatalogAddNode = (0, generic_utils_1.getBinaryNodeChild)(result, 'product_catalog_add');
215
+ const productNode = (0, generic_utils_1.getBinaryNodeChild)(productCatalogAddNode, 'product');
216
+ return (0, business_1.parseProductNode)(productNode);
333
217
  };
334
218
  const productDelete = async (productIds) => {
335
219
  const result = await query({
336
220
  tag: 'iq',
337
221
  attrs: {
338
- to: S_WHATSAPP_NET,
222
+ to: WABinary_1.S_WHATSAPP_NET,
339
223
  type: 'set',
340
224
  xmlns: 'w:biz:catalog'
341
225
  },
@@ -357,9 +241,9 @@ export const makeBusinessSocket = (config) => {
357
241
  }
358
242
  ]
359
243
  });
360
- const productCatalogDelNode = getBinaryNodeChild(result, 'product_catalog_delete');
244
+ const productCatalogDelNode = (0, generic_utils_1.getBinaryNodeChild)(result, 'product_catalog_delete');
361
245
  return {
362
- deleted: +(productCatalogDelNode?.attrs.deleted_count || 0)
246
+ deleted: +((productCatalogDelNode === null || productCatalogDelNode === void 0 ? void 0 : productCatalogDelNode.attrs.deleted_count) || 0)
363
247
  };
364
248
  };
365
249
  return {
@@ -370,10 +254,7 @@ export const makeBusinessSocket = (config) => {
370
254
  getCollections,
371
255
  productCreate,
372
256
  productDelete,
373
- productUpdate,
374
- updateBussinesProfile,
375
- updateCoverPhoto,
376
- removeCoverPhoto
257
+ productUpdate
377
258
  };
378
259
  };
379
- //# sourceMappingURL=business.js.map
260
+ exports.makeBusinessSocket = makeBusinessSocket;