@vinzsocket/baileys 2.8.9 → 2.8.20

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 (83) hide show
  1. package/lib/Defaults/index.js +75 -73
  2. package/lib/Signal/Group/ciphertext-message.js +9 -9
  3. package/lib/Signal/Group/group-session-builder.js +21 -21
  4. package/lib/Signal/Group/group_cipher.js +79 -81
  5. package/lib/Signal/Group/index.js +2 -2
  6. package/lib/Signal/Group/keyhelper.js +9 -9
  7. package/lib/Signal/Group/queue-job.js +43 -46
  8. package/lib/Signal/Group/sender-chain-key.js +24 -25
  9. package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
  10. package/lib/Signal/Group/sender-key-message.js +55 -60
  11. package/lib/Signal/Group/sender-key-name.js +38 -39
  12. package/lib/Signal/Group/sender-key-record.js +39 -41
  13. package/lib/Signal/Group/sender-key-state.js +85 -89
  14. package/lib/Signal/Group/sender-message-key.js +22 -22
  15. package/lib/Signal/libsignal.js +387 -292
  16. package/lib/Signal/lid-mapping.js +98 -93
  17. package/lib/Socket/Client/types.js +6 -6
  18. package/lib/Socket/Client/websocket.js +44 -44
  19. package/lib/Socket/business.js +350 -365
  20. package/lib/Socket/chats.js +1019 -914
  21. package/lib/Socket/communities.js +454 -376
  22. package/lib/Socket/groups.js +378 -310
  23. package/lib/Socket/index.js +11 -11
  24. package/lib/Socket/messages-recv.js +1355 -1182
  25. package/lib/Socket/messages-send.js +1224 -1081
  26. package/lib/Socket/mex.js +40 -34
  27. package/lib/Socket/newsletter.js +212 -169
  28. package/lib/Socket/socket.js +896 -757
  29. package/lib/Store/index.js +1 -1
  30. package/lib/Store/make-cache-manager-store.js +75 -79
  31. package/lib/Store/make-in-memory-store.js +450 -389
  32. package/lib/Store/make-ordered-dictionary.js +73 -76
  33. package/lib/Store/object-repository.js +24 -22
  34. package/lib/Types/Chat.js +1 -7
  35. package/lib/Types/Label.js +20 -20
  36. package/lib/Types/LabelAssociation.js +2 -2
  37. package/lib/Types/Message.js +2 -2
  38. package/lib/Types/Newsletter.js +24 -24
  39. package/lib/Types/Socket.js +1 -1
  40. package/lib/Types/State.js +8 -8
  41. package/lib/Types/index.js +10 -10
  42. package/lib/Utils/auth-utils.js +408 -401
  43. package/lib/Utils/baileys-event-stream.js +44 -35
  44. package/lib/Utils/business.js +200 -197
  45. package/lib/Utils/chat-utils.js +775 -708
  46. package/lib/Utils/crypto.js +97 -85
  47. package/lib/Utils/decode-wa-message.js +240 -227
  48. package/lib/Utils/event-buffer.js +547 -518
  49. package/lib/Utils/extended-messages.js +250 -203
  50. package/lib/Utils/generics.js +300 -289
  51. package/lib/Utils/history.js +76 -69
  52. package/lib/Utils/lid-resolver.js +47 -42
  53. package/lib/Utils/link-preview.js +75 -65
  54. package/lib/Utils/logger.js +3 -1
  55. package/lib/Utils/lt-hash.js +38 -34
  56. package/lib/Utils/make-mutex.js +32 -34
  57. package/lib/Utils/mention.js +79 -86
  58. package/lib/Utils/message-retry-manager.js +148 -136
  59. package/lib/Utils/messages-media.js +652 -562
  60. package/lib/Utils/messages.js +753 -707
  61. package/lib/Utils/noise-handler.js +156 -135
  62. package/lib/Utils/process-message.js +404 -341
  63. package/lib/Utils/signal.js +180 -118
  64. package/lib/Utils/use-multi-file-auth-state.js +87 -91
  65. package/lib/Utils/validate-connection.js +167 -142
  66. package/lib/WABinary/constants.js +28 -1293
  67. package/lib/WABinary/decode.js +220 -218
  68. package/lib/WABinary/encode.js +199 -201
  69. package/lib/WABinary/generic-utils.js +80 -77
  70. package/lib/WABinary/jid-utils.js +41 -35
  71. package/lib/WAM/BinaryInfo.js +7 -7
  72. package/lib/WAM/constants.js +1 -15241
  73. package/lib/WAM/encode.js +124 -131
  74. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  75. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
  76. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
  77. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
  78. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
  79. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
  80. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
  81. package/lib/WAUSync/USyncQuery.js +82 -84
  82. package/lib/WAUSync/USyncUser.js +20 -20
  83. package/package.json +4 -1
@@ -3,226 +3,228 @@ import { inflate } from 'zlib';
3
3
  import * as constants from './constants.js';
4
4
  import { jidEncode } from './jid-utils.js';
5
5
  const inflatePromise = promisify(inflate);
6
- export const decompressingIfRequired = async (buffer) => {
7
- if (2 & buffer.readUInt8()) {
8
- buffer = await inflatePromise(buffer.slice(1));
6
+ export const decompressingIfRequired = async buffer => {
7
+ if (2 & buffer.readUInt8()) {
8
+ buffer = await inflatePromise(buffer.slice(1));
9
+ } else {
10
+ // nodes with no compression have a 0x00 prefix, we remove that
11
+ buffer = buffer.slice(1);
12
+ }
13
+ return buffer;
14
+ };
15
+ export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = {
16
+ index: 0
17
+ }) => {
18
+ const {
19
+ DOUBLE_BYTE_TOKENS,
20
+ SINGLE_BYTE_TOKENS,
21
+ TAGS
22
+ } = opts;
23
+ const checkEOS = length => {
24
+ if (indexRef.index + length > buffer.length) {
25
+ throw new Error('end of stream');
9
26
  }
10
- else {
11
- // nodes with no compression have a 0x00 prefix, we remove that
12
- buffer = buffer.slice(1);
27
+ };
28
+ const next = () => {
29
+ const value = buffer[indexRef.index];
30
+ indexRef.index += 1;
31
+ return value;
32
+ };
33
+ const readByte = () => {
34
+ checkEOS(1);
35
+ return next();
36
+ };
37
+ const readBytes = n => {
38
+ checkEOS(n);
39
+ const value = buffer.slice(indexRef.index, indexRef.index + n);
40
+ indexRef.index += n;
41
+ return value;
42
+ };
43
+ const readStringFromChars = length => {
44
+ return readBytes(length).toString('utf-8');
45
+ };
46
+ const readInt = (n, littleEndian = false) => {
47
+ checkEOS(n);
48
+ let val = 0;
49
+ for (let i = 0; i < n; i++) {
50
+ const shift = littleEndian ? i : n - 1 - i;
51
+ val |= next() << shift * 8;
13
52
  }
14
- return buffer;
15
- };
16
- export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
17
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
18
- const checkEOS = (length) => {
19
- if (indexRef.index + length > buffer.length) {
20
- throw new Error('end of stream');
21
- }
22
- };
23
- const next = () => {
24
- const value = buffer[indexRef.index];
25
- indexRef.index += 1;
26
- return value;
27
- };
28
- const readByte = () => {
29
- checkEOS(1);
30
- return next();
31
- };
32
- const readBytes = (n) => {
33
- checkEOS(n);
34
- const value = buffer.slice(indexRef.index, indexRef.index + n);
35
- indexRef.index += n;
36
- return value;
37
- };
38
- const readStringFromChars = (length) => {
39
- return readBytes(length).toString('utf-8');
40
- };
41
- const readInt = (n, littleEndian = false) => {
42
- checkEOS(n);
43
- let val = 0;
44
- for (let i = 0; i < n; i++) {
45
- const shift = littleEndian ? i : n - 1 - i;
46
- val |= next() << (shift * 8);
47
- }
48
- return val;
49
- };
50
- const readInt20 = () => {
51
- checkEOS(3);
52
- return ((next() & 15) << 16) + (next() << 8) + next();
53
- };
54
- const unpackHex = (value) => {
55
- if (value >= 0 && value < 16) {
56
- return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;
57
- }
58
- throw new Error('invalid hex: ' + value);
59
- };
60
- const unpackNibble = (value) => {
61
- if (value >= 0 && value <= 9) {
62
- return '0'.charCodeAt(0) + value;
63
- }
64
- switch (value) {
65
- case 10:
66
- return '-'.charCodeAt(0);
67
- case 11:
68
- return '.'.charCodeAt(0);
69
- case 15:
70
- return '\0'.charCodeAt(0);
71
- default:
72
- throw new Error('invalid nibble: ' + value);
73
- }
74
- };
75
- const unpackByte = (tag, value) => {
76
- if (tag === TAGS.NIBBLE_8) {
77
- return unpackNibble(value);
78
- }
79
- else if (tag === TAGS.HEX_8) {
80
- return unpackHex(value);
81
- }
82
- else {
83
- throw new Error('unknown tag: ' + tag);
84
- }
85
- };
86
- const readPacked8 = (tag) => {
87
- const startByte = readByte();
88
- let value = '';
89
- for (let i = 0; i < (startByte & 127); i++) {
90
- const curByte = readByte();
91
- value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
92
- value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
93
- }
94
- if (startByte >> 7 !== 0) {
95
- value = value.slice(0, -1);
96
- }
97
- return value;
98
- };
99
- const isListTag = (tag) => {
100
- return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
101
- };
102
- const readListSize = (tag) => {
103
- switch (tag) {
104
- case TAGS.LIST_EMPTY:
105
- return 0;
106
- case TAGS.LIST_8:
107
- return readByte();
108
- case TAGS.LIST_16:
109
- return readInt(2);
110
- default:
111
- throw new Error('invalid tag for list size: ' + tag);
112
- }
113
- };
114
- const readJidPair = () => {
115
- const i = readString(readByte());
116
- const j = readString(readByte());
117
- if (j) {
118
- return (i || '') + '@' + j;
119
- }
120
- throw new Error('invalid jid pair: ' + i + ', ' + j);
121
- };
122
- const readAdJid = () => {
123
- const rawDomainType = readByte();
124
- const domainType = Number(rawDomainType);
125
- const device = readByte();
126
- const user = readString(readByte());
127
- return jidEncode(user, domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid', device);
128
- };
129
- const readString = (tag) => {
130
- if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
131
- return SINGLE_BYTE_TOKENS[tag] || '';
132
- }
133
- switch (tag) {
134
- case TAGS.DICTIONARY_0:
135
- case TAGS.DICTIONARY_1:
136
- case TAGS.DICTIONARY_2:
137
- case TAGS.DICTIONARY_3:
138
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
139
- case TAGS.LIST_EMPTY:
140
- return '';
141
- case TAGS.BINARY_8:
142
- return readStringFromChars(readByte());
143
- case TAGS.BINARY_20:
144
- return readStringFromChars(readInt20());
145
- case TAGS.BINARY_32:
146
- return readStringFromChars(readInt(4));
147
- case TAGS.JID_PAIR:
148
- return readJidPair();
149
- case TAGS.AD_JID:
150
- return readAdJid();
151
- case TAGS.HEX_8:
152
- case TAGS.NIBBLE_8:
153
- return readPacked8(tag);
154
- default:
155
- throw new Error('invalid string with tag: ' + tag);
156
- }
157
- };
158
- const readList = (tag) => {
159
- const items = [];
160
- const size = readListSize(tag);
161
- for (let i = 0; i < size; i++) {
162
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
163
- }
164
- return items;
165
- };
166
- const getTokenDouble = (index1, index2) => {
167
- const dict = DOUBLE_BYTE_TOKENS[index1];
168
- if (!dict) {
169
- throw new Error(`Invalid double token dict (${index1})`);
170
- }
171
- const value = dict[index2];
172
- if (typeof value === 'undefined') {
173
- throw new Error(`Invalid double token (${index2})`);
174
- }
175
- return value;
176
- };
177
- const listSize = readListSize(readByte());
178
- const header = readString(readByte());
179
- if (!listSize || !header.length) {
180
- throw new Error('invalid node');
181
- }
182
- const attrs = {};
183
- let data;
184
- if (listSize === 0 || !header) {
185
- throw new Error('invalid node');
186
- }
187
- // read the attributes in
188
- const attributesLength = (listSize - 1) >> 1;
189
- for (let i = 0; i < attributesLength; i++) {
190
- const key = readString(readByte());
191
- const value = readString(readByte());
192
- attrs[key] = value;
193
- }
194
- if (listSize % 2 === 0) {
195
- const tag = readByte();
196
- if (isListTag(tag)) {
197
- data = readList(tag);
198
- }
199
- else {
200
- let decoded;
201
- switch (tag) {
202
- case TAGS.BINARY_8:
203
- decoded = readBytes(readByte());
204
- break;
205
- case TAGS.BINARY_20:
206
- decoded = readBytes(readInt20());
207
- break;
208
- case TAGS.BINARY_32:
209
- decoded = readBytes(readInt(4));
210
- break;
211
- default:
212
- decoded = readString(tag);
213
- break;
214
- }
215
- data = decoded;
216
- }
217
- }
218
- return {
219
- tag: header,
220
- attrs,
221
- content: data
222
- };
53
+ return val;
54
+ };
55
+ const readInt20 = () => {
56
+ checkEOS(3);
57
+ return ((next() & 15) << 16) + (next() << 8) + next();
58
+ };
59
+ const unpackHex = value => {
60
+ if (value >= 0 && value < 16) {
61
+ return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;
62
+ }
63
+ throw new Error('invalid hex: ' + value);
64
+ };
65
+ const unpackNibble = value => {
66
+ if (value >= 0 && value <= 9) {
67
+ return '0'.charCodeAt(0) + value;
68
+ }
69
+ switch (value) {
70
+ case 10:
71
+ return '-'.charCodeAt(0);
72
+ case 11:
73
+ return '.'.charCodeAt(0);
74
+ case 15:
75
+ return '\0'.charCodeAt(0);
76
+ default:
77
+ throw new Error('invalid nibble: ' + value);
78
+ }
79
+ };
80
+ const unpackByte = (tag, value) => {
81
+ if (tag === TAGS.NIBBLE_8) {
82
+ return unpackNibble(value);
83
+ } else if (tag === TAGS.HEX_8) {
84
+ return unpackHex(value);
85
+ } else {
86
+ throw new Error('unknown tag: ' + tag);
87
+ }
88
+ };
89
+ const readPacked8 = tag => {
90
+ const startByte = readByte();
91
+ let value = '';
92
+ for (let i = 0; i < (startByte & 127); i++) {
93
+ const curByte = readByte();
94
+ value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
95
+ value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
96
+ }
97
+ if (startByte >> 7 !== 0) {
98
+ value = value.slice(0, -1);
99
+ }
100
+ return value;
101
+ };
102
+ const isListTag = tag => {
103
+ return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
104
+ };
105
+ const readListSize = tag => {
106
+ switch (tag) {
107
+ case TAGS.LIST_EMPTY:
108
+ return 0;
109
+ case TAGS.LIST_8:
110
+ return readByte();
111
+ case TAGS.LIST_16:
112
+ return readInt(2);
113
+ default:
114
+ throw new Error('invalid tag for list size: ' + tag);
115
+ }
116
+ };
117
+ const readJidPair = () => {
118
+ const i = readString(readByte());
119
+ const j = readString(readByte());
120
+ if (j) {
121
+ return (i || '') + '@' + j;
122
+ }
123
+ throw new Error('invalid jid pair: ' + i + ', ' + j);
124
+ };
125
+ const readAdJid = () => {
126
+ const rawDomainType = readByte();
127
+ const domainType = Number(rawDomainType);
128
+ const device = readByte();
129
+ const user = readString(readByte());
130
+ return jidEncode(user, domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid', device);
131
+ };
132
+ const readString = tag => {
133
+ if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
134
+ return SINGLE_BYTE_TOKENS[tag] || '';
135
+ }
136
+ switch (tag) {
137
+ case TAGS.DICTIONARY_0:
138
+ case TAGS.DICTIONARY_1:
139
+ case TAGS.DICTIONARY_2:
140
+ case TAGS.DICTIONARY_3:
141
+ return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
142
+ case TAGS.LIST_EMPTY:
143
+ return '';
144
+ case TAGS.BINARY_8:
145
+ return readStringFromChars(readByte());
146
+ case TAGS.BINARY_20:
147
+ return readStringFromChars(readInt20());
148
+ case TAGS.BINARY_32:
149
+ return readStringFromChars(readInt(4));
150
+ case TAGS.JID_PAIR:
151
+ return readJidPair();
152
+ case TAGS.AD_JID:
153
+ return readAdJid();
154
+ case TAGS.HEX_8:
155
+ case TAGS.NIBBLE_8:
156
+ return readPacked8(tag);
157
+ default:
158
+ throw new Error('invalid string with tag: ' + tag);
159
+ }
160
+ };
161
+ const readList = tag => {
162
+ const items = [];
163
+ const size = readListSize(tag);
164
+ for (let i = 0; i < size; i++) {
165
+ items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
166
+ }
167
+ return items;
168
+ };
169
+ const getTokenDouble = (index1, index2) => {
170
+ const dict = DOUBLE_BYTE_TOKENS[index1];
171
+ if (!dict) {
172
+ throw new Error(`Invalid double token dict (${index1})`);
173
+ }
174
+ const value = dict[index2];
175
+ if (typeof value === 'undefined') {
176
+ throw new Error(`Invalid double token (${index2})`);
177
+ }
178
+ return value;
179
+ };
180
+ const listSize = readListSize(readByte());
181
+ const header = readString(readByte());
182
+ if (!listSize || !header.length) {
183
+ throw new Error('invalid node');
184
+ }
185
+ const attrs = {};
186
+ let data;
187
+ if (listSize === 0 || !header) {
188
+ throw new Error('invalid node');
189
+ }
190
+ // read the attributes in
191
+ const attributesLength = listSize - 1 >> 1;
192
+ for (let i = 0; i < attributesLength; i++) {
193
+ const key = readString(readByte());
194
+ const value = readString(readByte());
195
+ attrs[key] = value;
196
+ }
197
+ if (listSize % 2 === 0) {
198
+ const tag = readByte();
199
+ if (isListTag(tag)) {
200
+ data = readList(tag);
201
+ } else {
202
+ let decoded;
203
+ switch (tag) {
204
+ case TAGS.BINARY_8:
205
+ decoded = readBytes(readByte());
206
+ break;
207
+ case TAGS.BINARY_20:
208
+ decoded = readBytes(readInt20());
209
+ break;
210
+ case TAGS.BINARY_32:
211
+ decoded = readBytes(readInt(4));
212
+ break;
213
+ default:
214
+ decoded = readString(tag);
215
+ break;
216
+ }
217
+ data = decoded;
218
+ }
219
+ }
220
+ return {
221
+ tag: header,
222
+ attrs,
223
+ content: data
224
+ };
223
225
  };
224
- export const decodeBinaryNode = async (buff) => {
225
- const decompBuff = await decompressingIfRequired(buff);
226
- return decodeDecompressedBinaryNode(decompBuff, constants);
226
+ export const decodeBinaryNode = async buff => {
227
+ const decompBuff = await decompressingIfRequired(buff);
228
+ return decodeDecompressedBinaryNode(decompBuff, constants);
227
229
  };
228
230
  //# sourceMappingURL=decode.js.map