@badzz88/baileys 8.4.7 → 8.5.0

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 (144) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1055 -337
  3. package/WAProto/index.js +5 -5
  4. package/lib/Defaults/index.js +187 -127
  5. package/lib/Defaults/phonenumber-mcc.json +223 -0
  6. package/lib/Signal/Group/ciphertext-message.js +14 -11
  7. package/lib/Signal/Group/group-session-builder.js +91 -29
  8. package/lib/Signal/Group/group_cipher.js +88 -81
  9. package/lib/Signal/Group/index.js +136 -12
  10. package/lib/Signal/Group/keyhelper.js +70 -15
  11. package/lib/Signal/Group/sender-chain-key.js +31 -25
  12. package/lib/Signal/Group/sender-key-distribution-message.js +65 -62
  13. package/lib/Signal/Group/sender-key-message.js +68 -65
  14. package/lib/Signal/Group/sender-key-name.js +43 -41
  15. package/lib/Signal/Group/sender-key-record.js +43 -40
  16. package/lib/Signal/Group/sender-key-state.js +96 -83
  17. package/lib/Signal/Group/sender-message-key.js +29 -25
  18. package/lib/Signal/libsignal.js +483 -424
  19. package/lib/Signal/lid-mapping.js +261 -276
  20. package/lib/Socket/Client/index.js +30 -3
  21. package/lib/Socket/Client/types.js +12 -10
  22. package/lib/Socket/Client/websocket.js +61 -53
  23. package/lib/Socket/aigroups.js +240 -0
  24. package/lib/Socket/business.js +422 -379
  25. package/lib/Socket/chats.js +1816 -1193
  26. package/lib/Socket/communities.js +549 -431
  27. package/lib/Socket/graphql.js +656 -0
  28. package/lib/Socket/groups.js +764 -374
  29. package/lib/Socket/index.js +38 -11
  30. package/lib/Socket/interactive-handler.js +522 -0
  31. package/lib/Socket/interop.js +549 -0
  32. package/lib/Socket/luxu.js +6 -5
  33. package/lib/Socket/managed-account.js +183 -0
  34. package/lib/Socket/messages-recv.js +2439 -1915
  35. package/lib/Socket/messages-send.js +1642 -1242
  36. package/lib/Socket/mex.js +59 -40
  37. package/lib/Socket/newsletter.js +812 -249
  38. package/lib/Socket/privacy.js +449 -0
  39. package/lib/Socket/registration.js +427 -0
  40. package/lib/Socket/socket.js +1077 -965
  41. package/lib/Socket/text-router.js +83 -0
  42. package/lib/Socket/username.js +237 -140
  43. package/lib/Store/index.js +36 -10
  44. package/lib/Store/make-cache-manager-store.js +85 -80
  45. package/lib/Store/make-in-memory-store.js +591 -185
  46. package/lib/Store/make-ordered-dictionary.js +78 -72
  47. package/lib/Store/object-repository.js +25 -28
  48. package/lib/Types/Auth.js +38 -2
  49. package/lib/Types/Bussines.js +2 -2
  50. package/lib/Types/Call.js +2 -2
  51. package/lib/Types/Chat.js +4 -8
  52. package/lib/Types/Contact.js +2 -2
  53. package/lib/Types/Events.js +2 -2
  54. package/lib/Types/GroupMetadata.js +2 -2
  55. package/lib/Types/Label.js +26 -24
  56. package/lib/Types/LabelAssociation.js +8 -6
  57. package/lib/Types/Message.js +95 -11
  58. package/lib/Types/Mex.js +112 -37
  59. package/lib/Types/Newsletter.js +121 -0
  60. package/lib/Types/Product.js +2 -2
  61. package/lib/Types/Signal.js +2 -2
  62. package/lib/Types/Socket.js +2 -3
  63. package/lib/Types/State.js +70 -56
  64. package/lib/Types/USync.js +2 -2
  65. package/lib/Types/index.js +55 -26
  66. package/lib/Utils/auth-utils.js +292 -288
  67. package/lib/Utils/browser-utils.js +113 -48
  68. package/lib/Utils/business.js +240 -224
  69. package/lib/Utils/chat-utils.js +1205 -869
  70. package/lib/Utils/command-loader.d.ts +31 -0
  71. package/lib/Utils/command-loader.js +100 -0
  72. package/lib/Utils/companion-reg-client-utils.js +41 -34
  73. package/lib/Utils/consumer-application.js +107 -0
  74. package/lib/Utils/crypto.js +144 -107
  75. package/lib/Utils/curve25519-js.js +275 -0
  76. package/lib/Utils/decode-wa-message.js +518 -308
  77. package/lib/Utils/event-buffer.js +596 -611
  78. package/lib/Utils/generics.js +514 -354
  79. package/lib/Utils/group-history.js +60 -0
  80. package/lib/Utils/history.js +244 -134
  81. package/lib/Utils/identity-change-handler.js +51 -49
  82. package/lib/Utils/index.js +57 -23
  83. package/lib/Utils/jid-display-normalization.js +218 -0
  84. package/lib/Utils/link-preview.js +135 -77
  85. package/lib/Utils/logger.js +9 -3
  86. package/lib/Utils/lt-hash.js +37 -6
  87. package/lib/Utils/make-mutex.js +36 -33
  88. package/lib/Utils/message-composer.js +439 -233
  89. package/lib/Utils/message-retry-manager.js +265 -260
  90. package/lib/Utils/messages-media.js +855 -747
  91. package/lib/Utils/messages.js +2528 -884
  92. package/lib/Utils/meta-ai-msmsg.js +262 -0
  93. package/lib/Utils/native-bridge.js +82 -0
  94. package/lib/Utils/noise-handler.js +195 -200
  95. package/lib/Utils/offline-node-processor.js +35 -33
  96. package/lib/Utils/pre-key-manager.js +103 -102
  97. package/lib/Utils/process-message.js +978 -560
  98. package/lib/Utils/reporting-utils.js +257 -253
  99. package/lib/Utils/session-pool.d.ts +16 -0
  100. package/lib/Utils/session-pool.js +94 -0
  101. package/lib/Utils/signal.js +218 -195
  102. package/lib/Utils/stanza-ack.js +31 -29
  103. package/lib/Utils/sticker.d.ts +17 -0
  104. package/lib/Utils/sticker.js +145 -0
  105. package/lib/Utils/sync-action-utils.js +50 -45
  106. package/lib/Utils/tc-token-utils.js +137 -139
  107. package/lib/Utils/use-multi-file-auth-state.js +109 -109
  108. package/lib/Utils/validate-connection.js +218 -202
  109. package/lib/Utils/view-once-cache.d.ts +9 -0
  110. package/lib/Utils/view-once-cache.js +79 -0
  111. package/lib/Utils/voip-rekey.js +22 -0
  112. package/lib/WABinary/constants.js +1302 -1299
  113. package/lib/WABinary/decode.js +343 -262
  114. package/lib/WABinary/encode.js +12 -4
  115. package/lib/WABinary/generic-utils.js +223 -187
  116. package/lib/WABinary/index.js +33 -6
  117. package/lib/WABinary/jid-utils.js +351 -88
  118. package/lib/WABinary/types.js +2 -2
  119. package/lib/WAM/BinaryInfo.js +12 -9
  120. package/lib/WAM/constants.js +39486 -22853
  121. package/lib/WAM/encode.js +132 -140
  122. package/lib/WAM/index.js +31 -4
  123. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +55 -0
  124. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
  125. package/lib/WAUSync/Protocols/USyncContactProtocol.js +53 -51
  126. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +56 -53
  127. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -26
  128. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +59 -0
  129. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +31 -0
  130. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
  131. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
  132. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -37
  133. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
  134. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +26 -24
  135. package/lib/WAUSync/Protocols/index.js +39 -6
  136. package/lib/WAUSync/USyncQuery.js +132 -97
  137. package/lib/WAUSync/USyncUser.js +49 -30
  138. package/lib/WAUSync/index.js +31 -4
  139. package/lib/antiban.js +4637 -0
  140. package/lib/index.js +48 -15
  141. package/package.json +55 -92
  142. package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -263
  143. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -51
  144. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -29
@@ -1,258 +1,262 @@
1
- import { createHmac } from 'crypto';
2
- import { proto } from '../../WAProto/index.js';
3
- import { hkdf } from './crypto.js';
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.getMessageReportingToken = exports.shouldIncludeReportingToken = void 0
4
+ const Crypto_1 = require('./crypto')
5
+ const crypto_2 = require('./crypto')
4
6
  const reportingFields = [
5
- { f: 1 },
6
- {
7
- f: 3,
8
- s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
9
- },
10
- { f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
11
- { f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
12
- { f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
13
- { f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
14
- { f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
15
- { f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
16
- { f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
17
- { f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
18
- { f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
19
- { f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
20
- { f: 37, s: [{ f: 1, m: true }] },
21
- {
22
- f: 49,
23
- s: [
24
- { f: 2 },
25
- { f: 3, s: [{ f: 1 }, { f: 2 }] },
26
- { f: 5, s: [{ f: 21 }, { f: 22 }] },
27
- { f: 8, s: [{ f: 1 }, { f: 2 }] }
28
- ]
29
- },
30
- { f: 53, s: [{ f: 1, m: true }] },
31
- { f: 55, s: [{ f: 1, m: true }] },
32
- { f: 58, s: [{ f: 1, m: true }] },
33
- { f: 59, s: [{ f: 1, m: true }] },
34
- {
35
- f: 60,
36
- s: [
37
- { f: 2 },
38
- { f: 3, s: [{ f: 1 }, { f: 2 }] },
39
- { f: 5, s: [{ f: 21 }, { f: 22 }] },
40
- { f: 8, s: [{ f: 1 }, { f: 2 }] }
41
- ]
42
- },
43
- {
44
- f: 64,
45
- s: [
46
- { f: 2 },
47
- { f: 3, s: [{ f: 1 }, { f: 2 }] },
48
- { f: 5, s: [{ f: 21 }, { f: 22 }] },
49
- { f: 8, s: [{ f: 1 }, { f: 2 }] }
50
- ]
51
- },
52
- { f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
53
- { f: 74, s: [{ f: 1, m: true }] },
54
- { f: 87, s: [{ f: 1, m: true }] },
55
- { f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
56
- { f: 92, s: [{ f: 1, m: true }] },
57
- { f: 93, s: [{ f: 1, m: true }] },
58
- { f: 94, s: [{ f: 1, m: true }] }
59
- ];
60
- const compileReportingFields = (fields) => {
61
- const map = new Map();
62
- for (const f of fields) {
63
- map.set(f.f, {
64
- m: f.m,
65
- children: f.s ? compileReportingFields(f.s) : undefined
66
- });
67
- }
68
- return map;
69
- };
70
- const compiledReportingFields = compileReportingFields(reportingFields);
71
- const EMPTY_MAP = new Map();
72
- const ENC_SECRET_REPORT_TOKEN = 'Report Token';
7
+ { f: 1 },
8
+ {
9
+ f: 3,
10
+ s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
11
+ },
12
+ { f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
13
+ { f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
14
+ { f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
15
+ { f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
16
+ { f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
17
+ { f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
18
+ { f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
19
+ { f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
20
+ { f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
21
+ { f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
22
+ { f: 37, s: [{ f: 1, m: true }] },
23
+ {
24
+ f: 49,
25
+ s: [
26
+ { f: 2 },
27
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
28
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
29
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
30
+ ]
31
+ },
32
+ { f: 53, s: [{ f: 1, m: true }] },
33
+ { f: 55, s: [{ f: 1, m: true }] },
34
+ { f: 58, s: [{ f: 1, m: true }] },
35
+ { f: 59, s: [{ f: 1, m: true }] },
36
+ {
37
+ f: 60,
38
+ s: [
39
+ { f: 2 },
40
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
41
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
42
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
43
+ ]
44
+ },
45
+ {
46
+ f: 64,
47
+ s: [
48
+ { f: 2 },
49
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
50
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
51
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
52
+ ]
53
+ },
54
+ { f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
55
+ { f: 74, s: [{ f: 1, m: true }] },
56
+ { f: 87, s: [{ f: 1, m: true }] },
57
+ { f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
58
+ { f: 92, s: [{ f: 1, m: true }] },
59
+ { f: 93, s: [{ f: 1, m: true }] },
60
+ { f: 94, s: [{ f: 1, m: true }] }
61
+ ]
62
+ const compileReportingFields = fields => {
63
+ const map = new Map()
64
+ for (const f of fields) {
65
+ map.set(f.f, {
66
+ m: f.m,
67
+ children: f.s ? compileReportingFields(f.s) : undefined
68
+ })
69
+ }
70
+ return map
71
+ }
72
+ const compiledReportingFields = compileReportingFields(reportingFields)
73
+ const EMPTY_MAP = new Map()
74
+ const ENC_SECRET_REPORT_TOKEN = 'Report Token'
73
75
  const WIRE = {
74
- VARINT: 0,
75
- FIXED64: 1,
76
- BYTES: 2,
77
- FIXED32: 5
78
- };
79
- export const shouldIncludeReportingToken = (message) => !message.reactionMessage &&
80
- !message.encReactionMessage &&
81
- !message.encEventResponseMessage &&
82
- !message.pollUpdateMessage;
76
+ VARINT: 0,
77
+ FIXED64: 1,
78
+ BYTES: 2,
79
+ FIXED32: 5
80
+ }
81
+ const shouldIncludeReportingToken = message =>
82
+ !message.reactionMessage &&
83
+ !message.encReactionMessage &&
84
+ !message.encEventResponseMessage &&
85
+ !message.pollUpdateMessage
86
+ exports.shouldIncludeReportingToken = shouldIncludeReportingToken
83
87
  const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
84
- const useCaseSecret = Buffer.concat([
85
- Buffer.from(origMsgId, 'utf8'),
86
- Buffer.from(origMsgSender, 'utf8'),
87
- Buffer.from(modificationSender, 'utf8'),
88
- Buffer.from(modificationType, 'utf8')
89
- ]);
90
- return hkdf(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') });
91
- };
88
+ const useCaseSecret = Buffer.concat([
89
+ Buffer.from(origMsgId, 'utf8'),
90
+ Buffer.from(origMsgSender, 'utf8'),
91
+ Buffer.from(modificationSender, 'utf8'),
92
+ Buffer.from(modificationType, 'utf8')
93
+ ])
94
+ return (0, crypto_2.hkdf)(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') })
95
+ }
92
96
  const extractReportingTokenContent = (data, cfg) => {
93
- const out = [];
94
- let i = 0;
95
- while (i < data.length) {
96
- const tag = decodeVarint(data, i);
97
- if (!tag.ok) {
98
- return null;
99
- }
100
- const fieldNum = tag.value >> 3;
101
- const wireType = tag.value & 0x7;
102
- const fieldStart = i;
103
- i += tag.bytes;
104
- const fieldCfg = cfg.get(fieldNum);
105
- const pushSlice = (end) => {
106
- if (end > data.length) {
107
- return false;
108
- }
109
- out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) });
110
- i = end;
111
- return true;
112
- };
113
- const skip = (end) => {
114
- if (end > data.length) {
115
- return false;
116
- }
117
- i = end;
118
- return true;
119
- };
120
- if (wireType === WIRE.VARINT) {
121
- const v = decodeVarint(data, i);
122
- if (!v.ok) {
123
- return null;
124
- }
125
- const end = i + v.bytes;
126
- if (!fieldCfg) {
127
- if (!skip(end)) {
128
- return null;
129
- }
130
- continue;
131
- }
132
- if (!pushSlice(end)) {
133
- return null;
134
- }
135
- continue;
136
- }
137
- if (wireType === WIRE.FIXED64) {
138
- const end = i + 8;
139
- if (!fieldCfg) {
140
- if (!skip(end)) {
141
- return null;
142
- }
143
- continue;
144
- }
145
- if (!pushSlice(end)) {
146
- return null;
147
- }
148
- continue;
149
- }
150
- if (wireType === WIRE.FIXED32) {
151
- const end = i + 4;
152
- if (!fieldCfg) {
153
- if (!skip(end)) {
154
- return null;
155
- }
156
- continue;
157
- }
158
- if (!pushSlice(end)) {
159
- return null;
160
- }
161
- continue;
162
- }
163
- if (wireType === WIRE.BYTES) {
164
- const len = decodeVarint(data, i);
165
- if (!len.ok) {
166
- return null;
167
- }
168
- const valStart = i + len.bytes;
169
- const valEnd = valStart + len.value;
170
- if (valEnd > data.length) {
171
- return null;
172
- }
173
- if (!fieldCfg) {
174
- i = valEnd;
175
- continue;
176
- }
177
- if (fieldCfg.m || fieldCfg.children) {
178
- const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP);
179
- if (sub === null) {
180
- return null;
181
- }
182
- if (sub.length > 0) {
183
- const newTag = encodeVarint(tag.value);
184
- const newLen = encodeVarint(sub.length);
185
- out.push({
186
- num: fieldNum,
187
- bytes: Buffer.concat([newTag, newLen, sub])
188
- });
189
- }
190
- i = valEnd;
191
- continue;
192
- }
193
- out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) });
194
- i = valEnd;
195
- continue;
196
- }
197
- return null;
198
- }
199
- if (out.length === 0) {
200
- return Buffer.alloc(0);
201
- }
202
- out.sort((a, b) => a.num - b.num);
203
- return Buffer.concat(out.map(f => f.bytes));
204
- };
97
+ const out = []
98
+ let i = 0
99
+ while (i < data.length) {
100
+ const tag = decodeVarint(data, i)
101
+ if (!tag.ok) {
102
+ return null
103
+ }
104
+ const fieldNum = tag.value >> 3
105
+ const wireType = tag.value & 0x7
106
+ const fieldStart = i
107
+ i += tag.bytes
108
+ const fieldCfg = cfg.get(fieldNum)
109
+ const pushSlice = end => {
110
+ if (end > data.length) {
111
+ return false
112
+ }
113
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) })
114
+ i = end
115
+ return true
116
+ }
117
+ const skip = end => {
118
+ if (end > data.length) {
119
+ return false
120
+ }
121
+ i = end
122
+ return true
123
+ }
124
+ if (wireType === WIRE.VARINT) {
125
+ const v = decodeVarint(data, i)
126
+ if (!v.ok) {
127
+ return null
128
+ }
129
+ const end = i + v.bytes
130
+ if (!fieldCfg) {
131
+ if (!skip(end)) {
132
+ return null
133
+ }
134
+ continue
135
+ }
136
+ if (!pushSlice(end)) {
137
+ return null
138
+ }
139
+ continue
140
+ }
141
+ if (wireType === WIRE.FIXED64) {
142
+ const end = i + 8
143
+ if (!fieldCfg) {
144
+ if (!skip(end)) {
145
+ return null
146
+ }
147
+ continue
148
+ }
149
+ if (!pushSlice(end)) {
150
+ return null
151
+ }
152
+ continue
153
+ }
154
+ if (wireType === WIRE.FIXED32) {
155
+ const end = i + 4
156
+ if (!fieldCfg) {
157
+ if (!skip(end)) {
158
+ return null
159
+ }
160
+ continue
161
+ }
162
+ if (!pushSlice(end)) {
163
+ return null
164
+ }
165
+ continue
166
+ }
167
+ if (wireType === WIRE.BYTES) {
168
+ const len = decodeVarint(data, i)
169
+ if (!len.ok) {
170
+ return null
171
+ }
172
+ const valStart = i + len.bytes
173
+ const valEnd = valStart + len.value
174
+ if (valEnd > data.length) {
175
+ return null
176
+ }
177
+ if (!fieldCfg) {
178
+ i = valEnd
179
+ continue
180
+ }
181
+ if (fieldCfg.m || fieldCfg.children) {
182
+ const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP)
183
+ if (sub === null) {
184
+ return null
185
+ }
186
+ if (sub.length > 0) {
187
+ const newTag = encodeVarint(tag.value)
188
+ const newLen = encodeVarint(sub.length)
189
+ out.push({
190
+ num: fieldNum,
191
+ bytes: Buffer.concat([newTag, newLen, sub])
192
+ })
193
+ }
194
+ i = valEnd
195
+ continue
196
+ }
197
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) })
198
+ i = valEnd
199
+ continue
200
+ }
201
+ return null
202
+ }
203
+ if (out.length === 0) {
204
+ return Buffer.alloc(0)
205
+ }
206
+ out.sort((a, b) => a.num - b.num)
207
+ return Buffer.concat(out.map(f => f.bytes))
208
+ }
205
209
  const decodeVarint = (buffer, offset) => {
206
- let value = 0;
207
- let bytes = 0;
208
- let shift = 0;
209
- while (offset + bytes < buffer.length) {
210
- const current = buffer[offset + bytes];
211
- value |= (current & 0x7f) << shift;
212
- bytes++;
213
- if ((current & 0x80) === 0) {
214
- return { value, bytes, ok: true };
215
- }
216
- shift += 7;
217
- if (shift > 35) {
218
- return { value: 0, bytes: 0, ok: false };
219
- }
220
- }
221
- return { value: 0, bytes: 0, ok: false };
222
- };
223
- const encodeVarint = (value) => {
224
- const parts = [];
225
- let remaining = value >>> 0;
226
- while (remaining > 0x7f) {
227
- parts.push((remaining & 0x7f) | 0x80);
228
- remaining >>>= 7;
229
- }
230
- parts.push(remaining);
231
- return Buffer.from(parts);
232
- };
233
- export const getMessageReportingToken = async (msgProtobuf, message, key) => {
234
- const msgSecret = message.messageContextInfo?.messageSecret;
235
- if (!msgSecret || !key.id) {
236
- return null;
237
- }
238
- const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid;
239
- const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid;
240
- const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret);
241
- const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields);
242
- if (!content || content.length === 0) {
243
- return null;
244
- }
245
- const reportingToken = createHmac('sha256', reportingSecret).update(content).digest().subarray(0, 16);
246
- return {
247
- tag: 'reporting',
248
- attrs: {},
249
- content: [
250
- {
251
- tag: 'reporting_token',
252
- attrs: { v: '2' },
253
- content: reportingToken
254
- }
255
- ]
256
- };
257
- };
258
- //# sourceMappingURL=reporting-utils.js.map
210
+ let value = 0
211
+ let bytes = 0
212
+ let shift = 0
213
+ while (offset + bytes < buffer.length) {
214
+ const current = buffer[offset + bytes]
215
+ value |= (current & 0x7f) << shift
216
+ bytes++
217
+ if ((current & 0x80) === 0) {
218
+ return { value, bytes, ok: true }
219
+ }
220
+ shift += 7
221
+ if (shift > 35) {
222
+ return { value: 0, bytes: 0, ok: false }
223
+ }
224
+ }
225
+ return { value: 0, bytes: 0, ok: false }
226
+ }
227
+ const encodeVarint = value => {
228
+ const parts = []
229
+ let remaining = value >>> 0
230
+ while (remaining > 0x7f) {
231
+ parts.push((remaining & 0x7f) | 0x80)
232
+ remaining >>>= 7
233
+ }
234
+ parts.push(remaining)
235
+ return Buffer.from(parts)
236
+ }
237
+ const getMessageReportingToken = async (msgProtobuf, message, key) => {
238
+ const msgSecret = message.messageContextInfo?.messageSecret
239
+ if (!msgSecret || !key.id) {
240
+ return null
241
+ }
242
+ const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid
243
+ const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid
244
+ const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret)
245
+ const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields)
246
+ if (!content || content.length === 0) {
247
+ return null
248
+ }
249
+ const reportingToken = Buffer.from(Crypto_1.hmacSign(content, reportingSecret)).subarray(0, 16)
250
+ return {
251
+ tag: 'reporting',
252
+ attrs: {},
253
+ content: [
254
+ {
255
+ tag: 'reporting_token',
256
+ attrs: { v: '2' },
257
+ content: reportingToken
258
+ }
259
+ ]
260
+ }
261
+ }
262
+ exports.getMessageReportingToken = getMessageReportingToken
@@ -0,0 +1,16 @@
1
+ export interface SessionPoolOptions {
2
+ makeSocket: (sessionId: string) => Promise<any> | any
3
+ logger?: any
4
+ maxBackoffMs?: number
5
+ onSessionUpdate?: (sessionId: string, sock: any) => void
6
+ onLoggedOut?: (sessionId: string) => void
7
+ }
8
+
9
+ export interface SessionPool {
10
+ add: (sessionId: string) => Promise<void>
11
+ remove: (sessionId: string) => void
12
+ get: (sessionId: string) => any
13
+ list: () => string[]
14
+ }
15
+
16
+ export function createSessionPool(options: SessionPoolOptions): SessionPool
@@ -0,0 +1,94 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.createSessionPool = void 0
4
+
5
+ const DISCONNECT_REASON_LOGGED_OUT = 401
6
+
7
+ /**
8
+ * Manages multiple Baileys socket instances (one per account/session), automatically
9
+ * restarting any session that disconnects -- except when it was logged out -- using
10
+ * exponential backoff with jitter, instead of every project re-implementing its own
11
+ * reconnect loop.
12
+ *
13
+ * @param {{
14
+ * makeSocket: (sessionId: string) => Promise<any> | any,
15
+ * logger?: any,
16
+ * maxBackoffMs?: number,
17
+ * onSessionUpdate?: (sessionId: string, sock: any) => void,
18
+ * onLoggedOut?: (sessionId: string) => void
19
+ * }} options
20
+ * @returns {{
21
+ * add: (sessionId: string) => Promise<void>,
22
+ * remove: (sessionId: string) => void,
23
+ * get: (sessionId: string) => any,
24
+ * list: () => string[]
25
+ * }}
26
+ */
27
+ const createSessionPool = options => {
28
+ const logger = options.logger
29
+ const maxBackoffMs = options.maxBackoffMs ?? 60000
30
+ const sessions = new Map() // sessionId -> { sock, attempts, stopped }
31
+
32
+ const computeBackoffMs = attempts => {
33
+ const base = Math.min(1000 * 2 ** attempts, maxBackoffMs)
34
+ return base + Math.floor(Math.random() * (base * 0.2))
35
+ }
36
+
37
+ const start = async sessionId => {
38
+ const entry = sessions.get(sessionId) || { attempts: 0, stopped: false }
39
+ sessions.set(sessionId, entry)
40
+ if (entry.stopped) return
41
+ try {
42
+ const sock = await options.makeSocket(sessionId)
43
+ entry.sock = sock
44
+ options.onSessionUpdate?.(sessionId, sock)
45
+ sock.ev.on('connection.update', update => {
46
+ if (update.connection === 'open') {
47
+ entry.attempts = 0
48
+ }
49
+ if (update.connection === 'close') {
50
+ const statusCode = update.lastDisconnect?.error?.output?.statusCode
51
+ if (statusCode === DISCONNECT_REASON_LOGGED_OUT) {
52
+ logger?.info?.({ sessionId }, 'session logged out, removing from pool')
53
+ sessions.delete(sessionId)
54
+ options.onLoggedOut?.(sessionId)
55
+ return
56
+ }
57
+ if (entry.stopped) return
58
+ entry.attempts += 1
59
+ const backoffMs = computeBackoffMs(entry.attempts)
60
+ logger?.warn?.(
61
+ { sessionId, attempt: entry.attempts, backoffMs },
62
+ 'session disconnected, reconnecting with backoff'
63
+ )
64
+ setTimeout(() => start(sessionId), backoffMs)
65
+ }
66
+ })
67
+ } catch (error) {
68
+ entry.attempts += 1
69
+ const backoffMs = computeBackoffMs(entry.attempts)
70
+ logger?.error?.({ error, sessionId, backoffMs }, 'failed to start session, retrying with backoff')
71
+ if (!entry.stopped) {
72
+ setTimeout(() => start(sessionId), backoffMs)
73
+ }
74
+ }
75
+ }
76
+
77
+ const add = sessionId => start(sessionId)
78
+ const remove = sessionId => {
79
+ const entry = sessions.get(sessionId)
80
+ if (!entry) return
81
+ entry.stopped = true
82
+ if (entry.sock?.end) {
83
+ try {
84
+ entry.sock.end(undefined)
85
+ } catch {}
86
+ }
87
+ sessions.delete(sessionId)
88
+ }
89
+ const get = sessionId => sessions.get(sessionId)?.sock
90
+ const list = () => [...sessions.keys()]
91
+
92
+ return { add, remove, get, list }
93
+ }
94
+ exports.createSessionPool = createSessionPool