@periskope/baileys 6.7.15-alpha.6 → 6.7.16-alpha.1

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 (149) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +35 -1267
  3. package/WAProto/WAProto.proto +1299 -286
  4. package/WAProto/index.d.ts +16106 -2739
  5. package/WAProto/index.js +128593 -73944
  6. package/engine-requirements.js +10 -0
  7. package/lib/Defaults/baileys-version.json +3 -3
  8. package/lib/Defaults/index.d.ts +51 -53
  9. package/lib/Defaults/index.js +106 -107
  10. package/lib/Signal/libsignal.d.ts +3 -3
  11. package/lib/Signal/libsignal.js +161 -152
  12. package/lib/Socket/Client/index.d.ts +2 -2
  13. package/lib/Socket/Client/index.js +18 -18
  14. package/lib/Socket/Client/types.d.ts +15 -17
  15. package/lib/Socket/Client/types.js +13 -13
  16. package/lib/Socket/Client/websocket.d.ts +12 -12
  17. package/lib/Socket/Client/websocket.js +62 -62
  18. package/lib/Socket/business.d.ts +145 -149
  19. package/lib/Socket/business.js +260 -260
  20. package/lib/Socket/chats.d.ts +84 -85
  21. package/lib/Socket/chats.js +879 -850
  22. package/lib/Socket/groups.d.ts +123 -124
  23. package/lib/Socket/groups.js +314 -314
  24. package/lib/Socket/index.d.ts +145 -149
  25. package/lib/Socket/index.js +10 -10
  26. package/lib/Socket/messages-recv.d.ts +134 -136
  27. package/lib/Socket/messages-recv.js +925 -917
  28. package/lib/Socket/messages-send.d.ts +129 -130
  29. package/lib/Socket/messages-send.js +683 -673
  30. package/lib/Socket/socket.d.ts +43 -45
  31. package/lib/Socket/socket.js +622 -622
  32. package/lib/Socket/usync.d.ts +36 -38
  33. package/lib/Socket/usync.js +70 -70
  34. package/lib/Types/Auth.d.ts +103 -105
  35. package/lib/Types/Auth.js +2 -2
  36. package/lib/Types/Call.d.ts +13 -13
  37. package/lib/Types/Call.js +2 -2
  38. package/lib/Types/Chat.d.ts +112 -108
  39. package/lib/Types/Chat.js +4 -4
  40. package/lib/Types/Contact.d.ts +19 -19
  41. package/lib/Types/Contact.js +2 -2
  42. package/lib/Types/Events.d.ts +175 -175
  43. package/lib/Types/Events.js +2 -2
  44. package/lib/Types/GroupMetadata.d.ts +56 -56
  45. package/lib/Types/GroupMetadata.js +2 -2
  46. package/lib/Types/Label.d.ts +46 -46
  47. package/lib/Types/Label.js +27 -27
  48. package/lib/Types/LabelAssociation.d.ts +29 -29
  49. package/lib/Types/LabelAssociation.js +9 -9
  50. package/lib/Types/Message.d.ts +267 -271
  51. package/lib/Types/Message.js +9 -9
  52. package/lib/Types/Product.d.ts +78 -78
  53. package/lib/Types/Product.js +2 -2
  54. package/lib/Types/Signal.d.ts +57 -57
  55. package/lib/Types/Signal.js +2 -2
  56. package/lib/Types/Socket.d.ts +121 -118
  57. package/lib/Types/Socket.js +2 -2
  58. package/lib/Types/State.d.ts +27 -27
  59. package/lib/Types/State.js +2 -2
  60. package/lib/Types/USync.d.ts +25 -25
  61. package/lib/Types/USync.js +2 -2
  62. package/lib/Types/index.d.ts +63 -63
  63. package/lib/Types/index.js +41 -41
  64. package/lib/Utils/auth-utils.d.ts +18 -18
  65. package/lib/Utils/auth-utils.js +200 -200
  66. package/lib/Utils/baileys-event-stream.d.ts +16 -16
  67. package/lib/Utils/baileys-event-stream.js +63 -63
  68. package/lib/Utils/business.d.ts +22 -22
  69. package/lib/Utils/business.js +234 -234
  70. package/lib/Utils/chat-utils.d.ts +70 -72
  71. package/lib/Utils/chat-utils.js +745 -745
  72. package/lib/Utils/crypto.d.ts +40 -42
  73. package/lib/Utils/crypto.js +193 -181
  74. package/lib/Utils/decode-wa-message.d.ts +36 -36
  75. package/lib/Utils/decode-wa-message.js +205 -205
  76. package/lib/Utils/event-buffer.d.ts +35 -35
  77. package/lib/Utils/event-buffer.js +520 -520
  78. package/lib/Utils/generics.d.ts +90 -94
  79. package/lib/Utils/generics.js +398 -433
  80. package/lib/Utils/history.d.ts +19 -19
  81. package/lib/Utils/history.js +94 -94
  82. package/lib/Utils/index.d.ts +17 -17
  83. package/lib/Utils/index.js +33 -33
  84. package/lib/Utils/link-preview.d.ts +21 -21
  85. package/lib/Utils/link-preview.js +129 -119
  86. package/lib/Utils/logger.d.ts +11 -11
  87. package/lib/Utils/logger.js +7 -7
  88. package/lib/Utils/lt-hash.d.ts +12 -12
  89. package/lib/Utils/lt-hash.js +51 -51
  90. package/lib/Utils/make-mutex.d.ts +7 -7
  91. package/lib/Utils/make-mutex.js +44 -44
  92. package/lib/Utils/messages-media.d.ts +105 -109
  93. package/lib/Utils/messages-media.js +704 -694
  94. package/lib/Utils/messages.d.ts +75 -78
  95. package/lib/Utils/messages.js +761 -762
  96. package/lib/Utils/noise-handler.d.ts +19 -21
  97. package/lib/Utils/noise-handler.js +150 -150
  98. package/lib/Utils/process-message.d.ts +41 -42
  99. package/lib/Utils/process-message.js +385 -376
  100. package/lib/Utils/signal.d.ts +33 -33
  101. package/lib/Utils/signal.js +153 -153
  102. package/lib/Utils/use-multi-file-auth-state.d.ts +12 -12
  103. package/lib/Utils/use-multi-file-auth-state.js +125 -94
  104. package/lib/Utils/validate-connection.d.ts +10 -10
  105. package/lib/Utils/validate-connection.js +175 -175
  106. package/lib/WABinary/constants.d.ts +27 -27
  107. package/lib/WABinary/constants.js +40 -40
  108. package/lib/WABinary/decode.d.ts +6 -8
  109. package/lib/WABinary/decode.js +264 -254
  110. package/lib/WABinary/encode.d.ts +2 -4
  111. package/lib/WABinary/encode.js +244 -234
  112. package/lib/WABinary/generic-utils.d.ts +14 -16
  113. package/lib/WABinary/generic-utils.js +110 -110
  114. package/lib/WABinary/index.d.ts +5 -5
  115. package/lib/WABinary/index.js +21 -21
  116. package/lib/WABinary/jid-utils.d.ts +33 -31
  117. package/lib/WABinary/jid-utils.js +66 -62
  118. package/lib/WABinary/types.d.ts +18 -18
  119. package/lib/WABinary/types.js +2 -2
  120. package/lib/WAM/BinaryInfo.d.ts +8 -18
  121. package/lib/WAM/BinaryInfo.js +13 -13
  122. package/lib/WAM/constants.d.ts +39 -39
  123. package/lib/WAM/constants.js +15350 -15350
  124. package/lib/WAM/encode.d.ts +2 -4
  125. package/lib/WAM/encode.js +155 -155
  126. package/lib/WAM/index.d.ts +3 -3
  127. package/lib/WAM/index.js +19 -19
  128. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -9
  129. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -32
  130. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -22
  131. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -57
  132. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -12
  133. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -30
  134. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -12
  135. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -42
  136. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
  137. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  138. package/lib/WAUSync/Protocols/index.d.ts +4 -4
  139. package/lib/WAUSync/Protocols/index.js +20 -20
  140. package/lib/WAUSync/USyncQuery.d.ts +26 -26
  141. package/lib/WAUSync/USyncQuery.js +79 -79
  142. package/lib/WAUSync/USyncUser.d.ts +12 -10
  143. package/lib/WAUSync/USyncUser.js +26 -22
  144. package/lib/WAUSync/index.d.ts +3 -3
  145. package/lib/WAUSync/index.js +19 -19
  146. package/lib/index.d.ts +11 -12
  147. package/lib/index.js +30 -31
  148. package/package.json +20 -26
  149. package/WAProto/GenerateStatics.sh +0 -4
@@ -1,254 +1,264 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.decodeBinaryNode = exports.decodeDecompressedBinaryNode = exports.decompressingIfRequired = void 0;
27
- const util_1 = require("util");
28
- const zlib_1 = require("zlib");
29
- const constants = __importStar(require("./constants"));
30
- const jid_utils_1 = require("./jid-utils");
31
- const inflatePromise = (0, util_1.promisify)(zlib_1.inflate);
32
- const decompressingIfRequired = async (buffer) => {
33
- if (2 & buffer.readUInt8()) {
34
- buffer = await inflatePromise(buffer.slice(1));
35
- }
36
- else { // nodes with no compression have a 0x00 prefix, we remove that
37
- buffer = buffer.slice(1);
38
- }
39
- return buffer;
40
- };
41
- exports.decompressingIfRequired = decompressingIfRequired;
42
- const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
43
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
44
- const checkEOS = (length) => {
45
- if (indexRef.index + length > buffer.length) {
46
- throw new Error('end of stream');
47
- }
48
- };
49
- const next = () => {
50
- const value = buffer[indexRef.index];
51
- indexRef.index += 1;
52
- return value;
53
- };
54
- const readByte = () => {
55
- checkEOS(1);
56
- return next();
57
- };
58
- const readBytes = (n) => {
59
- checkEOS(n);
60
- const value = buffer.slice(indexRef.index, indexRef.index + n);
61
- indexRef.index += n;
62
- return value;
63
- };
64
- const readStringFromChars = (length) => {
65
- return readBytes(length).toString('utf-8');
66
- };
67
- const readInt = (n, littleEndian = false) => {
68
- checkEOS(n);
69
- let val = 0;
70
- for (let i = 0; i < n; i++) {
71
- const shift = littleEndian ? i : n - 1 - i;
72
- val |= next() << (shift * 8);
73
- }
74
- return val;
75
- };
76
- const readInt20 = () => {
77
- checkEOS(3);
78
- return ((next() & 15) << 16) + (next() << 8) + next();
79
- };
80
- const unpackHex = (value) => {
81
- if (value >= 0 && value < 16) {
82
- return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;
83
- }
84
- throw new Error('invalid hex: ' + value);
85
- };
86
- const unpackNibble = (value) => {
87
- if (value >= 0 && value <= 9) {
88
- return '0'.charCodeAt(0) + value;
89
- }
90
- switch (value) {
91
- case 10:
92
- return '-'.charCodeAt(0);
93
- case 11:
94
- return '.'.charCodeAt(0);
95
- case 15:
96
- return '\0'.charCodeAt(0);
97
- default:
98
- throw new Error('invalid nibble: ' + value);
99
- }
100
- };
101
- const unpackByte = (tag, value) => {
102
- if (tag === TAGS.NIBBLE_8) {
103
- return unpackNibble(value);
104
- }
105
- else if (tag === TAGS.HEX_8) {
106
- return unpackHex(value);
107
- }
108
- else {
109
- throw new Error('unknown tag: ' + tag);
110
- }
111
- };
112
- const readPacked8 = (tag) => {
113
- const startByte = readByte();
114
- let value = '';
115
- for (let i = 0; i < (startByte & 127); i++) {
116
- const curByte = readByte();
117
- value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
118
- value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
119
- }
120
- if (startByte >> 7 !== 0) {
121
- value = value.slice(0, -1);
122
- }
123
- return value;
124
- };
125
- const isListTag = (tag) => {
126
- return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
127
- };
128
- const readListSize = (tag) => {
129
- switch (tag) {
130
- case TAGS.LIST_EMPTY:
131
- return 0;
132
- case TAGS.LIST_8:
133
- return readByte();
134
- case TAGS.LIST_16:
135
- return readInt(2);
136
- default:
137
- throw new Error('invalid tag for list size: ' + tag);
138
- }
139
- };
140
- const readJidPair = () => {
141
- const i = readString(readByte());
142
- const j = readString(readByte());
143
- if (j) {
144
- return (i || '') + '@' + j;
145
- }
146
- throw new Error('invalid jid pair: ' + i + ', ' + j);
147
- };
148
- const readAdJid = () => {
149
- const agent = readByte();
150
- const device = readByte();
151
- const user = readString(readByte());
152
- return (0, jid_utils_1.jidEncode)(user, agent === 0 ? 's.whatsapp.net' : 'lid', device);
153
- };
154
- const readString = (tag) => {
155
- if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
156
- return SINGLE_BYTE_TOKENS[tag] || '';
157
- }
158
- switch (tag) {
159
- case TAGS.DICTIONARY_0:
160
- case TAGS.DICTIONARY_1:
161
- case TAGS.DICTIONARY_2:
162
- case TAGS.DICTIONARY_3:
163
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
164
- case TAGS.LIST_EMPTY:
165
- return '';
166
- case TAGS.BINARY_8:
167
- return readStringFromChars(readByte());
168
- case TAGS.BINARY_20:
169
- return readStringFromChars(readInt20());
170
- case TAGS.BINARY_32:
171
- return readStringFromChars(readInt(4));
172
- case TAGS.JID_PAIR:
173
- return readJidPair();
174
- case TAGS.AD_JID:
175
- return readAdJid();
176
- case TAGS.HEX_8:
177
- case TAGS.NIBBLE_8:
178
- return readPacked8(tag);
179
- default:
180
- throw new Error('invalid string with tag: ' + tag);
181
- }
182
- };
183
- const readList = (tag) => {
184
- const items = [];
185
- const size = readListSize(tag);
186
- for (let i = 0; i < size; i++) {
187
- items.push((0, exports.decodeDecompressedBinaryNode)(buffer, opts, indexRef));
188
- }
189
- return items;
190
- };
191
- const getTokenDouble = (index1, index2) => {
192
- const dict = DOUBLE_BYTE_TOKENS[index1];
193
- if (!dict) {
194
- throw new Error(`Invalid double token dict (${index1})`);
195
- }
196
- const value = dict[index2];
197
- if (typeof value === 'undefined') {
198
- throw new Error(`Invalid double token (${index2})`);
199
- }
200
- return value;
201
- };
202
- const listSize = readListSize(readByte());
203
- const header = readString(readByte());
204
- if (!listSize || !header.length) {
205
- throw new Error('invalid node');
206
- }
207
- const attrs = {};
208
- let data;
209
- if (listSize === 0 || !header) {
210
- throw new Error('invalid node');
211
- }
212
- // read the attributes in
213
- const attributesLength = (listSize - 1) >> 1;
214
- for (let i = 0; i < attributesLength; i++) {
215
- const key = readString(readByte());
216
- const value = readString(readByte());
217
- attrs[key] = value;
218
- }
219
- if (listSize % 2 === 0) {
220
- const tag = readByte();
221
- if (isListTag(tag)) {
222
- data = readList(tag);
223
- }
224
- else {
225
- let decoded;
226
- switch (tag) {
227
- case TAGS.BINARY_8:
228
- decoded = readBytes(readByte());
229
- break;
230
- case TAGS.BINARY_20:
231
- decoded = readBytes(readInt20());
232
- break;
233
- case TAGS.BINARY_32:
234
- decoded = readBytes(readInt(4));
235
- break;
236
- default:
237
- decoded = readString(tag);
238
- break;
239
- }
240
- data = decoded;
241
- }
242
- }
243
- return {
244
- tag: header,
245
- attrs,
246
- content: data
247
- };
248
- };
249
- exports.decodeDecompressedBinaryNode = decodeDecompressedBinaryNode;
250
- const decodeBinaryNode = async (buff) => {
251
- const decompBuff = await (0, exports.decompressingIfRequired)(buff);
252
- return (0, exports.decodeDecompressedBinaryNode)(decompBuff, constants);
253
- };
254
- exports.decodeBinaryNode = decodeBinaryNode;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.decodeBinaryNode = exports.decodeDecompressedBinaryNode = exports.decompressingIfRequired = void 0;
37
+ const util_1 = require("util");
38
+ const zlib_1 = require("zlib");
39
+ const constants = __importStar(require("./constants"));
40
+ const jid_utils_1 = require("./jid-utils");
41
+ const inflatePromise = (0, util_1.promisify)(zlib_1.inflate);
42
+ const decompressingIfRequired = async (buffer) => {
43
+ if (2 & buffer.readUInt8()) {
44
+ buffer = await inflatePromise(buffer.slice(1));
45
+ }
46
+ else { // nodes with no compression have a 0x00 prefix, we remove that
47
+ buffer = buffer.slice(1);
48
+ }
49
+ return buffer;
50
+ };
51
+ exports.decompressingIfRequired = decompressingIfRequired;
52
+ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
53
+ const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
54
+ const checkEOS = (length) => {
55
+ if (indexRef.index + length > buffer.length) {
56
+ throw new Error('end of stream');
57
+ }
58
+ };
59
+ const next = () => {
60
+ const value = buffer[indexRef.index];
61
+ indexRef.index += 1;
62
+ return value;
63
+ };
64
+ const readByte = () => {
65
+ checkEOS(1);
66
+ return next();
67
+ };
68
+ const readBytes = (n) => {
69
+ checkEOS(n);
70
+ const value = buffer.slice(indexRef.index, indexRef.index + n);
71
+ indexRef.index += n;
72
+ return value;
73
+ };
74
+ const readStringFromChars = (length) => {
75
+ return readBytes(length).toString('utf-8');
76
+ };
77
+ const readInt = (n, littleEndian = false) => {
78
+ checkEOS(n);
79
+ let val = 0;
80
+ for (let i = 0; i < n; i++) {
81
+ const shift = littleEndian ? i : n - 1 - i;
82
+ val |= next() << (shift * 8);
83
+ }
84
+ return val;
85
+ };
86
+ const readInt20 = () => {
87
+ checkEOS(3);
88
+ return ((next() & 15) << 16) + (next() << 8) + next();
89
+ };
90
+ const unpackHex = (value) => {
91
+ if (value >= 0 && value < 16) {
92
+ return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;
93
+ }
94
+ throw new Error('invalid hex: ' + value);
95
+ };
96
+ const unpackNibble = (value) => {
97
+ if (value >= 0 && value <= 9) {
98
+ return '0'.charCodeAt(0) + value;
99
+ }
100
+ switch (value) {
101
+ case 10:
102
+ return '-'.charCodeAt(0);
103
+ case 11:
104
+ return '.'.charCodeAt(0);
105
+ case 15:
106
+ return '\0'.charCodeAt(0);
107
+ default:
108
+ throw new Error('invalid nibble: ' + value);
109
+ }
110
+ };
111
+ const unpackByte = (tag, value) => {
112
+ if (tag === TAGS.NIBBLE_8) {
113
+ return unpackNibble(value);
114
+ }
115
+ else if (tag === TAGS.HEX_8) {
116
+ return unpackHex(value);
117
+ }
118
+ else {
119
+ throw new Error('unknown tag: ' + tag);
120
+ }
121
+ };
122
+ const readPacked8 = (tag) => {
123
+ const startByte = readByte();
124
+ let value = '';
125
+ for (let i = 0; i < (startByte & 127); i++) {
126
+ const curByte = readByte();
127
+ value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
128
+ value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
129
+ }
130
+ if (startByte >> 7 !== 0) {
131
+ value = value.slice(0, -1);
132
+ }
133
+ return value;
134
+ };
135
+ const isListTag = (tag) => {
136
+ return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
137
+ };
138
+ const readListSize = (tag) => {
139
+ switch (tag) {
140
+ case TAGS.LIST_EMPTY:
141
+ return 0;
142
+ case TAGS.LIST_8:
143
+ return readByte();
144
+ case TAGS.LIST_16:
145
+ return readInt(2);
146
+ default:
147
+ throw new Error('invalid tag for list size: ' + tag);
148
+ }
149
+ };
150
+ const readJidPair = () => {
151
+ const i = readString(readByte());
152
+ const j = readString(readByte());
153
+ if (j) {
154
+ return (i || '') + '@' + j;
155
+ }
156
+ throw new Error('invalid jid pair: ' + i + ', ' + j);
157
+ };
158
+ const readAdJid = () => {
159
+ const agent = readByte();
160
+ const device = readByte();
161
+ const user = readString(readByte());
162
+ return (0, jid_utils_1.jidEncode)(user, agent === 0 ? 's.whatsapp.net' : 'lid', device);
163
+ };
164
+ const readString = (tag) => {
165
+ if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
166
+ return SINGLE_BYTE_TOKENS[tag] || '';
167
+ }
168
+ switch (tag) {
169
+ case TAGS.DICTIONARY_0:
170
+ case TAGS.DICTIONARY_1:
171
+ case TAGS.DICTIONARY_2:
172
+ case TAGS.DICTIONARY_3:
173
+ return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
174
+ case TAGS.LIST_EMPTY:
175
+ return '';
176
+ case TAGS.BINARY_8:
177
+ return readStringFromChars(readByte());
178
+ case TAGS.BINARY_20:
179
+ return readStringFromChars(readInt20());
180
+ case TAGS.BINARY_32:
181
+ return readStringFromChars(readInt(4));
182
+ case TAGS.JID_PAIR:
183
+ return readJidPair();
184
+ case TAGS.AD_JID:
185
+ return readAdJid();
186
+ case TAGS.HEX_8:
187
+ case TAGS.NIBBLE_8:
188
+ return readPacked8(tag);
189
+ default:
190
+ throw new Error('invalid string with tag: ' + tag);
191
+ }
192
+ };
193
+ const readList = (tag) => {
194
+ const items = [];
195
+ const size = readListSize(tag);
196
+ for (let i = 0; i < size; i++) {
197
+ items.push((0, exports.decodeDecompressedBinaryNode)(buffer, opts, indexRef));
198
+ }
199
+ return items;
200
+ };
201
+ const getTokenDouble = (index1, index2) => {
202
+ const dict = DOUBLE_BYTE_TOKENS[index1];
203
+ if (!dict) {
204
+ throw new Error(`Invalid double token dict (${index1})`);
205
+ }
206
+ const value = dict[index2];
207
+ if (typeof value === 'undefined') {
208
+ throw new Error(`Invalid double token (${index2})`);
209
+ }
210
+ return value;
211
+ };
212
+ const listSize = readListSize(readByte());
213
+ const header = readString(readByte());
214
+ if (!listSize || !header.length) {
215
+ throw new Error('invalid node');
216
+ }
217
+ const attrs = {};
218
+ let data;
219
+ if (listSize === 0 || !header) {
220
+ throw new Error('invalid node');
221
+ }
222
+ // read the attributes in
223
+ const attributesLength = (listSize - 1) >> 1;
224
+ for (let i = 0; i < attributesLength; i++) {
225
+ const key = readString(readByte());
226
+ const value = readString(readByte());
227
+ attrs[key] = value;
228
+ }
229
+ if (listSize % 2 === 0) {
230
+ const tag = readByte();
231
+ if (isListTag(tag)) {
232
+ data = readList(tag);
233
+ }
234
+ else {
235
+ let decoded;
236
+ switch (tag) {
237
+ case TAGS.BINARY_8:
238
+ decoded = readBytes(readByte());
239
+ break;
240
+ case TAGS.BINARY_20:
241
+ decoded = readBytes(readInt20());
242
+ break;
243
+ case TAGS.BINARY_32:
244
+ decoded = readBytes(readInt(4));
245
+ break;
246
+ default:
247
+ decoded = readString(tag);
248
+ break;
249
+ }
250
+ data = decoded;
251
+ }
252
+ }
253
+ return {
254
+ tag: header,
255
+ attrs,
256
+ content: data
257
+ };
258
+ };
259
+ exports.decodeDecompressedBinaryNode = decodeDecompressedBinaryNode;
260
+ const decodeBinaryNode = async (buff) => {
261
+ const decompBuff = await (0, exports.decompressingIfRequired)(buff);
262
+ return (0, exports.decodeDecompressedBinaryNode)(decompBuff, constants);
263
+ };
264
+ exports.decodeBinaryNode = decodeBinaryNode;
@@ -1,4 +1,2 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import type { BinaryNode, BinaryNodeCodingOptions } from './types';
4
- export declare const encodeBinaryNode: (node: BinaryNode, opts?: Pick<BinaryNodeCodingOptions, 'TAGS' | 'TOKEN_MAP'>, buffer?: number[]) => Buffer;
1
+ import type { BinaryNode, BinaryNodeCodingOptions } from './types';
2
+ export declare const encodeBinaryNode: (node: BinaryNode, opts?: Pick<BinaryNodeCodingOptions, "TAGS" | "TOKEN_MAP">, buffer?: number[]) => Buffer;