@nexustechpro/baileys 2.0.2 → 2.0.5

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 (102) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +924 -1299
  3. package/lib/Defaults/baileys-version.json +6 -2
  4. package/lib/Defaults/index.js +172 -172
  5. package/lib/Signal/libsignal.js +380 -292
  6. package/lib/Signal/lid-mapping.js +264 -171
  7. package/lib/Socket/Client/index.js +2 -2
  8. package/lib/Socket/Client/types.js +10 -10
  9. package/lib/Socket/Client/websocket.js +45 -310
  10. package/lib/Socket/business.js +375 -375
  11. package/lib/Socket/chats.js +909 -963
  12. package/lib/Socket/communities.js +430 -430
  13. package/lib/Socket/groups.js +342 -342
  14. package/lib/Socket/index.js +22 -22
  15. package/lib/Socket/messages-recv.js +777 -743
  16. package/lib/Socket/messages-send.js +295 -305
  17. package/lib/Socket/mex.js +50 -50
  18. package/lib/Socket/newsletter.js +148 -148
  19. package/lib/Socket/nexus-handler.js +75 -261
  20. package/lib/Socket/socket.js +709 -1201
  21. package/lib/Store/index.js +5 -5
  22. package/lib/Store/make-cache-manager-store.js +81 -81
  23. package/lib/Store/make-in-memory-store.js +416 -416
  24. package/lib/Store/make-ordered-dictionary.js +81 -81
  25. package/lib/Store/object-repository.js +30 -30
  26. package/lib/Types/Auth.js +1 -1
  27. package/lib/Types/Bussines.js +1 -1
  28. package/lib/Types/Call.js +1 -1
  29. package/lib/Types/Chat.js +7 -7
  30. package/lib/Types/Contact.js +1 -1
  31. package/lib/Types/Events.js +1 -1
  32. package/lib/Types/GroupMetadata.js +1 -1
  33. package/lib/Types/Label.js +24 -24
  34. package/lib/Types/LabelAssociation.js +6 -6
  35. package/lib/Types/Message.js +10 -10
  36. package/lib/Types/Newsletter.js +28 -28
  37. package/lib/Types/Product.js +1 -1
  38. package/lib/Types/Signal.js +1 -1
  39. package/lib/Types/Socket.js +2 -2
  40. package/lib/Types/State.js +12 -12
  41. package/lib/Types/USync.js +1 -1
  42. package/lib/Types/index.js +25 -25
  43. package/lib/Utils/auth-utils.js +264 -256
  44. package/lib/Utils/baileys-event-stream.js +55 -55
  45. package/lib/Utils/browser-utils.js +27 -27
  46. package/lib/Utils/business.js +228 -230
  47. package/lib/Utils/chat-utils.js +694 -764
  48. package/lib/Utils/crypto.js +109 -135
  49. package/lib/Utils/decode-wa-message.js +310 -314
  50. package/lib/Utils/event-buffer.js +547 -547
  51. package/lib/Utils/generics.js +297 -297
  52. package/lib/Utils/history.js +91 -83
  53. package/lib/Utils/index.js +21 -20
  54. package/lib/Utils/key-store.js +17 -0
  55. package/lib/Utils/link-preview.js +97 -98
  56. package/lib/Utils/logger.js +2 -2
  57. package/lib/Utils/lt-hash.js +47 -47
  58. package/lib/Utils/make-mutex.js +39 -39
  59. package/lib/Utils/message-retry-manager.js +148 -148
  60. package/lib/Utils/messages-media.js +534 -534
  61. package/lib/Utils/messages.js +705 -705
  62. package/lib/Utils/noise-handler.js +255 -255
  63. package/lib/Utils/pre-key-manager.js +105 -105
  64. package/lib/Utils/process-message.js +412 -412
  65. package/lib/Utils/signal.js +160 -158
  66. package/lib/Utils/use-multi-file-auth-state.js +120 -120
  67. package/lib/Utils/validate-connection.js +194 -194
  68. package/lib/WABinary/constants.js +1300 -1300
  69. package/lib/WABinary/decode.js +237 -237
  70. package/lib/WABinary/encode.js +232 -232
  71. package/lib/WABinary/generic-utils.js +252 -211
  72. package/lib/WABinary/index.js +5 -5
  73. package/lib/WABinary/jid-utils.js +279 -95
  74. package/lib/WABinary/types.js +1 -1
  75. package/lib/WAM/BinaryInfo.js +9 -9
  76. package/lib/WAM/constants.js +22852 -22852
  77. package/lib/WAM/encode.js +149 -149
  78. package/lib/WAM/index.js +3 -3
  79. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
  80. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
  81. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
  82. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
  83. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  84. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
  85. package/lib/WAUSync/Protocols/index.js +4 -4
  86. package/lib/WAUSync/USyncQuery.js +93 -93
  87. package/lib/WAUSync/USyncUser.js +22 -22
  88. package/lib/WAUSync/index.js +3 -3
  89. package/lib/index.js +66 -66
  90. package/package.json +171 -144
  91. package/lib/Signal/Group/ciphertext-message.js +0 -12
  92. package/lib/Signal/Group/group-session-builder.js +0 -30
  93. package/lib/Signal/Group/group_cipher.js +0 -100
  94. package/lib/Signal/Group/index.js +0 -12
  95. package/lib/Signal/Group/keyhelper.js +0 -18
  96. package/lib/Signal/Group/sender-chain-key.js +0 -26
  97. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  98. package/lib/Signal/Group/sender-key-message.js +0 -66
  99. package/lib/Signal/Group/sender-key-name.js +0 -48
  100. package/lib/Signal/Group/sender-key-record.js +0 -41
  101. package/lib/Signal/Group/sender-key-state.js +0 -84
  102. package/lib/Signal/Group/sender-message-key.js +0 -26
@@ -1,233 +1,233 @@
1
- import * as constants from './constants.js';
2
- import { jidDecode } from './jid-utils.js';
3
- export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
4
- const encoded = encodeBinaryNodeInner(node, opts, buffer);
5
- return Buffer.from(encoded);
6
- };
7
- const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
8
- const { TAGS, TOKEN_MAP } = opts;
9
- const pushByte = (value) => buffer.push(value & 0xff);
10
- const pushInt = (value, n, littleEndian = false) => {
11
- for (let i = 0; i < n; i++) {
12
- const curShift = littleEndian ? i : n - 1 - i;
13
- buffer.push((value >> (curShift * 8)) & 0xff);
14
- }
15
- };
16
- const pushBytes = (bytes) => {
17
- // For large buffers, use spread operator more efficiently
18
- if (bytes.length > 1000) {
19
- // For very large buffers, push in chunks to avoid stack overflow
20
- for (let i = 0; i < bytes.length; i++) {
21
- buffer.push(bytes[i]);
22
- }
23
- } else {
24
- // For smaller buffers, use spread for efficiency
25
- buffer.push(...bytes);
26
- }
27
- };
28
- const pushInt16 = (value) => {
29
- pushBytes([(value >> 8) & 0xff, value & 0xff]);
30
- };
31
- const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]);
32
- const writeByteLength = (length) => {
33
- if (length >= 4294967296) {
34
- throw new Error('string too large to encode: ' + length);
35
- }
36
- if (length >= 1 << 20) {
37
- pushByte(TAGS.BINARY_32);
38
- pushInt(length, 4); // 32 bit integer
39
- }
40
- else if (length >= 256) {
41
- pushByte(TAGS.BINARY_20);
42
- pushInt20(length);
43
- }
44
- else {
45
- pushByte(TAGS.BINARY_8);
46
- pushByte(length);
47
- }
48
- };
49
- const writeStringRaw = (str) => {
50
- const bytes = Buffer.from(str, 'utf-8');
51
- writeByteLength(bytes.length);
52
- pushBytes(bytes);
53
- };
54
- const writeJid = ({ domainType, device, user, server }) => {
55
- if (typeof device !== 'undefined') {
56
- pushByte(TAGS.AD_JID);
57
- pushByte(domainType || 0);
58
- pushByte(device || 0);
59
- writeString(user);
60
- }
61
- else {
62
- pushByte(TAGS.JID_PAIR);
63
- if (user.length) {
64
- writeString(user);
65
- }
66
- else {
67
- pushByte(TAGS.LIST_EMPTY);
68
- }
69
- writeString(server);
70
- }
71
- };
72
- const packNibble = (char) => {
73
- switch (char) {
74
- case '-':
75
- return 10;
76
- case '.':
77
- return 11;
78
- case '\0':
79
- return 15;
80
- default:
81
- if (char >= '0' && char <= '9') {
82
- return char.charCodeAt(0) - '0'.charCodeAt(0);
83
- }
84
- throw new Error(`invalid byte for nibble "${char}"`);
85
- }
86
- };
87
- const packHex = (char) => {
88
- if (char >= '0' && char <= '9') {
89
- return char.charCodeAt(0) - '0'.charCodeAt(0);
90
- }
91
- if (char >= 'A' && char <= 'F') {
92
- return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
93
- }
94
- if (char >= 'a' && char <= 'f') {
95
- return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
96
- }
97
- if (char === '\0') {
98
- return 15;
99
- }
100
- throw new Error(`Invalid hex char "${char}"`);
101
- };
102
- const writePackedBytes = (str, type) => {
103
- if (str.length > TAGS.PACKED_MAX) {
104
- throw new Error('Too many bytes to pack');
105
- }
106
- pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
107
- let roundedLength = Math.ceil(str.length / 2.0);
108
- if (str.length % 2 !== 0) {
109
- roundedLength |= 128;
110
- }
111
- pushByte(roundedLength);
112
- const packFunction = type === 'nibble' ? packNibble : packHex;
113
- const packBytePair = (v1, v2) => {
114
- const result = (packFunction(v1) << 4) | packFunction(v2);
115
- return result;
116
- };
117
- const strLengthHalf = Math.floor(str.length / 2);
118
- for (let i = 0; i < strLengthHalf; i++) {
119
- pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
120
- }
121
- if (str.length % 2 !== 0) {
122
- pushByte(packBytePair(str[str.length - 1], '\x00'));
123
- }
124
- };
125
- const isNibble = (str) => {
126
- if (!str || str.length > TAGS.PACKED_MAX) {
127
- return false;
128
- }
129
- for (const char of str) {
130
- const isInNibbleRange = char >= '0' && char <= '9';
131
- if (!isInNibbleRange && char !== '-' && char !== '.') {
132
- return false;
133
- }
134
- }
135
- return true;
136
- };
137
- const isHex = (str) => {
138
- if (!str || str.length > TAGS.PACKED_MAX) {
139
- return false;
140
- }
141
- for (const char of str) {
142
- const isInNibbleRange = char >= '0' && char <= '9';
143
- if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
144
- return false;
145
- }
146
- }
147
- return true;
148
- };
149
- const writeString = (str) => {
150
- if (str === undefined || str === null) {
151
- pushByte(TAGS.LIST_EMPTY);
152
- return;
153
- }
154
- const tokenIndex = TOKEN_MAP[str];
155
- if (tokenIndex) {
156
- if (typeof tokenIndex.dict === 'number') {
157
- pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
158
- }
159
- pushByte(tokenIndex.index);
160
- }
161
- else if (isNibble(str)) {
162
- writePackedBytes(str, 'nibble');
163
- }
164
- else if (isHex(str)) {
165
- writePackedBytes(str, 'hex');
166
- }
167
- else if (str) {
168
- const decodedJid = jidDecode(str);
169
- if (decodedJid) {
170
- writeJid(decodedJid);
171
- }
172
- else {
173
- writeStringRaw(str);
174
- }
175
- }
176
- };
177
- const writeListStart = (listSize) => {
178
- if (listSize === 0) {
179
- pushByte(TAGS.LIST_EMPTY);
180
- }
181
- else if (listSize < 256) {
182
- pushBytes([TAGS.LIST_8, listSize]);
183
- }
184
- else {
185
- pushByte(TAGS.LIST_16);
186
- pushInt16(listSize);
187
- }
188
- };
189
- if (!tag) {
190
- throw new Error('Invalid node: tag cannot be undefined');
191
- }
192
- const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
193
- writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
194
- writeString(tag);
195
- for (const key of validAttributes) {
196
- if (typeof attrs[key] === 'string') {
197
- writeString(key);
198
- writeString(attrs[key]);
199
- }
200
- }
201
- if (typeof content === 'string') {
202
- writeString(content);
203
- }
204
- else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
205
- writeByteLength(content.length);
206
- // For very large buffers, handle differently to avoid array size limits
207
- if (content.length > 10000000) {
208
- // Push length header first
209
- pushBytes([...content.slice(0, Math.min(1000, content.length))]);
210
- // Process rest in chunks if needed
211
- for (let i = 1000; i < content.length; i++) {
212
- buffer.push(content[i]);
213
- }
214
- } else {
215
- pushBytes(content);
216
- }
217
- }
218
- else if (Array.isArray(content)) {
219
- const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
220
- writeListStart(validContent.length);
221
- for (const item of validContent) {
222
- encodeBinaryNodeInner(item, opts, buffer);
223
- }
224
- }
225
- else if (typeof content === 'undefined') {
226
- // do nothing
227
- }
228
- else {
229
- throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
230
- }
231
- return buffer;
232
- };
1
+ import * as constants from './constants.js';
2
+ import { jidDecode } from './jid-utils.js';
3
+ export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
4
+ const encoded = encodeBinaryNodeInner(node, opts, buffer);
5
+ return Buffer.from(encoded);
6
+ };
7
+ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
8
+ const { TAGS, TOKEN_MAP } = opts;
9
+ const pushByte = (value) => buffer.push(value & 0xff);
10
+ const pushInt = (value, n, littleEndian = false) => {
11
+ for (let i = 0; i < n; i++) {
12
+ const curShift = littleEndian ? i : n - 1 - i;
13
+ buffer.push((value >> (curShift * 8)) & 0xff);
14
+ }
15
+ };
16
+ const pushBytes = (bytes) => {
17
+ // For large buffers, use spread operator more efficiently
18
+ if (bytes.length > 1000) {
19
+ // For very large buffers, push in chunks to avoid stack overflow
20
+ for (let i = 0; i < bytes.length; i++) {
21
+ buffer.push(bytes[i]);
22
+ }
23
+ } else {
24
+ // For smaller buffers, use spread for efficiency
25
+ buffer.push(...bytes);
26
+ }
27
+ };
28
+ const pushInt16 = (value) => {
29
+ pushBytes([(value >> 8) & 0xff, value & 0xff]);
30
+ };
31
+ const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]);
32
+ const writeByteLength = (length) => {
33
+ if (length >= 4294967296) {
34
+ throw new Error('string too large to encode: ' + length);
35
+ }
36
+ if (length >= 1 << 20) {
37
+ pushByte(TAGS.BINARY_32);
38
+ pushInt(length, 4); // 32 bit integer
39
+ }
40
+ else if (length >= 256) {
41
+ pushByte(TAGS.BINARY_20);
42
+ pushInt20(length);
43
+ }
44
+ else {
45
+ pushByte(TAGS.BINARY_8);
46
+ pushByte(length);
47
+ }
48
+ };
49
+ const writeStringRaw = (str) => {
50
+ const bytes = Buffer.from(str, 'utf-8');
51
+ writeByteLength(bytes.length);
52
+ pushBytes(bytes);
53
+ };
54
+ const writeJid = ({ domainType, device, user, server }) => {
55
+ if (typeof device !== 'undefined') {
56
+ pushByte(TAGS.AD_JID);
57
+ pushByte(domainType || 0);
58
+ pushByte(device || 0);
59
+ writeString(user);
60
+ }
61
+ else {
62
+ pushByte(TAGS.JID_PAIR);
63
+ if (user.length) {
64
+ writeString(user);
65
+ }
66
+ else {
67
+ pushByte(TAGS.LIST_EMPTY);
68
+ }
69
+ writeString(server);
70
+ }
71
+ };
72
+ const packNibble = (char) => {
73
+ switch (char) {
74
+ case '-':
75
+ return 10;
76
+ case '.':
77
+ return 11;
78
+ case '\0':
79
+ return 15;
80
+ default:
81
+ if (char >= '0' && char <= '9') {
82
+ return char.charCodeAt(0) - '0'.charCodeAt(0);
83
+ }
84
+ throw new Error(`invalid byte for nibble "${char}"`);
85
+ }
86
+ };
87
+ const packHex = (char) => {
88
+ if (char >= '0' && char <= '9') {
89
+ return char.charCodeAt(0) - '0'.charCodeAt(0);
90
+ }
91
+ if (char >= 'A' && char <= 'F') {
92
+ return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
93
+ }
94
+ if (char >= 'a' && char <= 'f') {
95
+ return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
96
+ }
97
+ if (char === '\0') {
98
+ return 15;
99
+ }
100
+ throw new Error(`Invalid hex char "${char}"`);
101
+ };
102
+ const writePackedBytes = (str, type) => {
103
+ if (str.length > TAGS.PACKED_MAX) {
104
+ throw new Error('Too many bytes to pack');
105
+ }
106
+ pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
107
+ let roundedLength = Math.ceil(str.length / 2.0);
108
+ if (str.length % 2 !== 0) {
109
+ roundedLength |= 128;
110
+ }
111
+ pushByte(roundedLength);
112
+ const packFunction = type === 'nibble' ? packNibble : packHex;
113
+ const packBytePair = (v1, v2) => {
114
+ const result = (packFunction(v1) << 4) | packFunction(v2);
115
+ return result;
116
+ };
117
+ const strLengthHalf = Math.floor(str.length / 2);
118
+ for (let i = 0; i < strLengthHalf; i++) {
119
+ pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
120
+ }
121
+ if (str.length % 2 !== 0) {
122
+ pushByte(packBytePair(str[str.length - 1], '\x00'));
123
+ }
124
+ };
125
+ const isNibble = (str) => {
126
+ if (!str || str.length > TAGS.PACKED_MAX) {
127
+ return false;
128
+ }
129
+ for (const char of str) {
130
+ const isInNibbleRange = char >= '0' && char <= '9';
131
+ if (!isInNibbleRange && char !== '-' && char !== '.') {
132
+ return false;
133
+ }
134
+ }
135
+ return true;
136
+ };
137
+ const isHex = (str) => {
138
+ if (!str || str.length > TAGS.PACKED_MAX) {
139
+ return false;
140
+ }
141
+ for (const char of str) {
142
+ const isInNibbleRange = char >= '0' && char <= '9';
143
+ if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
144
+ return false;
145
+ }
146
+ }
147
+ return true;
148
+ };
149
+ const writeString = (str) => {
150
+ if (str === undefined || str === null) {
151
+ pushByte(TAGS.LIST_EMPTY);
152
+ return;
153
+ }
154
+ const tokenIndex = TOKEN_MAP[str];
155
+ if (tokenIndex) {
156
+ if (typeof tokenIndex.dict === 'number') {
157
+ pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
158
+ }
159
+ pushByte(tokenIndex.index);
160
+ }
161
+ else if (isNibble(str)) {
162
+ writePackedBytes(str, 'nibble');
163
+ }
164
+ else if (isHex(str)) {
165
+ writePackedBytes(str, 'hex');
166
+ }
167
+ else if (str) {
168
+ const decodedJid = jidDecode(str);
169
+ if (decodedJid) {
170
+ writeJid(decodedJid);
171
+ }
172
+ else {
173
+ writeStringRaw(str);
174
+ }
175
+ }
176
+ };
177
+ const writeListStart = (listSize) => {
178
+ if (listSize === 0) {
179
+ pushByte(TAGS.LIST_EMPTY);
180
+ }
181
+ else if (listSize < 256) {
182
+ pushBytes([TAGS.LIST_8, listSize]);
183
+ }
184
+ else {
185
+ pushByte(TAGS.LIST_16);
186
+ pushInt16(listSize);
187
+ }
188
+ };
189
+ if (!tag) {
190
+ throw new Error('Invalid node: tag cannot be undefined');
191
+ }
192
+ const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
193
+ writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
194
+ writeString(tag);
195
+ for (const key of validAttributes) {
196
+ if (typeof attrs[key] === 'string') {
197
+ writeString(key);
198
+ writeString(attrs[key]);
199
+ }
200
+ }
201
+ if (typeof content === 'string') {
202
+ writeString(content);
203
+ }
204
+ else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
205
+ writeByteLength(content.length);
206
+ // For very large buffers, handle differently to avoid array size limits
207
+ if (content.length > 10000000) {
208
+ // Push length header first
209
+ pushBytes([...content.slice(0, Math.min(1000, content.length))]);
210
+ // Process rest in chunks if needed
211
+ for (let i = 1000; i < content.length; i++) {
212
+ buffer.push(content[i]);
213
+ }
214
+ } else {
215
+ pushBytes(content);
216
+ }
217
+ }
218
+ else if (Array.isArray(content)) {
219
+ const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
220
+ writeListStart(validContent.length);
221
+ for (const item of validContent) {
222
+ encodeBinaryNodeInner(item, opts, buffer);
223
+ }
224
+ }
225
+ else if (typeof content === 'undefined') {
226
+ // do nothing
227
+ }
228
+ else {
229
+ throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
230
+ }
231
+ return buffer;
232
+ };
233
233
  //# sourceMappingURL=encode.js.map