@d0v3riz/baileys 6.7.12 → 6.7.17

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 (161) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +35 -1267
  3. package/WAProto/index.d.ts +16106 -2739
  4. package/WAProto/index.js +128593 -73944
  5. package/engine-requirements.js +10 -0
  6. package/lib/Defaults/baileys-version.json +3 -3
  7. package/lib/Defaults/index.d.ts +51 -53
  8. package/lib/Defaults/index.js +105 -106
  9. package/lib/Signal/libsignal.d.ts +3 -3
  10. package/lib/Signal/libsignal.js +161 -152
  11. package/lib/Socket/Client/index.d.ts +2 -2
  12. package/lib/Socket/Client/index.js +18 -18
  13. package/lib/Socket/Client/types.d.ts +15 -17
  14. package/lib/Socket/Client/types.js +13 -13
  15. package/lib/Socket/Client/websocket.d.ts +12 -12
  16. package/lib/Socket/Client/websocket.js +62 -62
  17. package/lib/Socket/business.d.ts +146 -148
  18. package/lib/Socket/business.js +260 -260
  19. package/lib/Socket/chats.d.ts +85 -84
  20. package/lib/Socket/chats.js +880 -846
  21. package/lib/Socket/groups.d.ts +124 -123
  22. package/lib/Socket/groups.js +314 -314
  23. package/lib/Socket/index.d.ts +146 -148
  24. package/lib/Socket/index.js +10 -10
  25. package/lib/Socket/messages-recv.d.ts +135 -135
  26. package/lib/Socket/messages-recv.js +920 -913
  27. package/lib/Socket/messages-send.d.ts +130 -129
  28. package/lib/Socket/messages-send.js +683 -673
  29. package/lib/Socket/socket.d.ts +42 -45
  30. package/lib/Socket/socket.js +622 -622
  31. package/lib/Socket/usync.d.ts +36 -38
  32. package/lib/Socket/usync.js +70 -70
  33. package/lib/Types/Auth.d.ts +103 -105
  34. package/lib/Types/Auth.js +2 -2
  35. package/lib/Types/Call.d.ts +13 -13
  36. package/lib/Types/Call.js +2 -2
  37. package/lib/Types/Chat.d.ts +112 -107
  38. package/lib/Types/Chat.js +4 -4
  39. package/lib/Types/Contact.d.ts +19 -19
  40. package/lib/Types/Contact.js +2 -2
  41. package/lib/Types/Events.d.ts +172 -172
  42. package/lib/Types/Events.js +2 -2
  43. package/lib/Types/GroupMetadata.d.ts +56 -56
  44. package/lib/Types/GroupMetadata.js +2 -2
  45. package/lib/Types/Label.d.ts +46 -46
  46. package/lib/Types/Label.js +27 -27
  47. package/lib/Types/LabelAssociation.d.ts +29 -29
  48. package/lib/Types/LabelAssociation.js +9 -9
  49. package/lib/Types/Message.d.ts +269 -273
  50. package/lib/Types/Message.js +7 -9
  51. package/lib/Types/Product.d.ts +78 -78
  52. package/lib/Types/Product.js +2 -2
  53. package/lib/Types/Signal.d.ts +57 -57
  54. package/lib/Types/Signal.js +2 -2
  55. package/lib/Types/Socket.d.ts +121 -118
  56. package/lib/Types/Socket.js +2 -2
  57. package/lib/Types/State.d.ts +27 -27
  58. package/lib/Types/State.js +2 -2
  59. package/lib/Types/USync.d.ts +25 -25
  60. package/lib/Types/USync.js +2 -2
  61. package/lib/Types/index.d.ts +63 -63
  62. package/lib/Types/index.js +41 -41
  63. package/lib/Utils/auth-utils.d.ts +18 -18
  64. package/lib/Utils/auth-utils.js +200 -200
  65. package/lib/Utils/baileys-event-stream.d.ts +16 -16
  66. package/lib/Utils/baileys-event-stream.js +63 -63
  67. package/lib/Utils/business.d.ts +22 -22
  68. package/lib/Utils/business.js +234 -234
  69. package/lib/Utils/chat-utils.d.ts +70 -72
  70. package/lib/Utils/chat-utils.js +745 -745
  71. package/lib/Utils/crypto.d.ts +40 -42
  72. package/lib/Utils/crypto.js +193 -153
  73. package/lib/Utils/decode-wa-message.d.ts +36 -36
  74. package/lib/Utils/decode-wa-message.js +205 -205
  75. package/lib/Utils/event-buffer.d.ts +35 -35
  76. package/lib/Utils/event-buffer.js +520 -520
  77. package/lib/Utils/generics.d.ts +90 -94
  78. package/lib/Utils/generics.js +398 -421
  79. package/lib/Utils/history.d.ts +19 -19
  80. package/lib/Utils/history.js +94 -94
  81. package/lib/Utils/index.d.ts +17 -17
  82. package/lib/Utils/index.js +33 -33
  83. package/lib/Utils/link-preview.d.ts +21 -21
  84. package/lib/Utils/link-preview.js +126 -116
  85. package/lib/Utils/logger.d.ts +11 -4
  86. package/lib/Utils/logger.js +7 -7
  87. package/lib/Utils/lt-hash.d.ts +12 -12
  88. package/lib/Utils/lt-hash.js +51 -51
  89. package/lib/Utils/make-mutex.d.ts +7 -7
  90. package/lib/Utils/make-mutex.js +44 -44
  91. package/lib/Utils/messages-media.d.ts +104 -108
  92. package/lib/Utils/messages-media.js +677 -667
  93. package/lib/Utils/messages.d.ts +75 -78
  94. package/lib/Utils/messages.js +765 -762
  95. package/lib/Utils/noise-handler.d.ts +19 -21
  96. package/lib/Utils/noise-handler.js +150 -150
  97. package/lib/Utils/process-message.d.ts +41 -42
  98. package/lib/Utils/process-message.js +382 -371
  99. package/lib/Utils/signal.d.ts +33 -33
  100. package/lib/Utils/signal.js +153 -153
  101. package/lib/Utils/use-multi-file-auth-state.d.ts +12 -12
  102. package/lib/Utils/use-multi-file-auth-state.js +125 -94
  103. package/lib/Utils/validate-connection.d.ts +10 -10
  104. package/lib/Utils/validate-connection.js +175 -175
  105. package/lib/WABinary/constants.d.ts +27 -27
  106. package/lib/WABinary/constants.js +40 -40
  107. package/lib/WABinary/decode.d.ts +6 -8
  108. package/lib/WABinary/decode.js +264 -254
  109. package/lib/WABinary/encode.d.ts +2 -4
  110. package/lib/WABinary/encode.js +252 -234
  111. package/lib/WABinary/generic-utils.d.ts +14 -16
  112. package/lib/WABinary/generic-utils.js +110 -110
  113. package/lib/WABinary/index.d.ts +5 -5
  114. package/lib/WABinary/index.js +21 -21
  115. package/lib/WABinary/jid-utils.d.ts +33 -31
  116. package/lib/WABinary/jid-utils.js +66 -62
  117. package/lib/WABinary/types.d.ts +18 -18
  118. package/lib/WABinary/types.js +2 -2
  119. package/lib/WAM/BinaryInfo.d.ts +8 -18
  120. package/lib/WAM/BinaryInfo.js +13 -13
  121. package/lib/WAM/constants.d.ts +39 -39
  122. package/lib/WAM/constants.js +15350 -15350
  123. package/lib/WAM/encode.d.ts +2 -4
  124. package/lib/WAM/encode.js +155 -155
  125. package/lib/WAM/index.d.ts +3 -3
  126. package/lib/WAM/index.js +19 -19
  127. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -9
  128. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -32
  129. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -22
  130. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -57
  131. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -12
  132. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -30
  133. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -12
  134. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -42
  135. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
  136. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  137. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
  138. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  139. package/lib/WAUSync/Protocols/index.d.ts +4 -4
  140. package/lib/WAUSync/Protocols/index.js +20 -20
  141. package/lib/WAUSync/USyncQuery.d.ts +28 -26
  142. package/lib/WAUSync/USyncQuery.js +89 -79
  143. package/lib/WAUSync/USyncUser.d.ts +12 -10
  144. package/lib/WAUSync/USyncUser.js +26 -22
  145. package/lib/WAUSync/index.d.ts +3 -3
  146. package/lib/WAUSync/index.js +19 -19
  147. package/lib/index.d.ts +11 -12
  148. package/lib/index.js +30 -31
  149. package/package.json +27 -35
  150. package/WAProto/GenerateStatics.sh +0 -4
  151. package/WAProto/WAProto.proto +0 -3344
  152. package/lib/Store/index.d.ts +0 -3
  153. package/lib/Store/index.js +0 -10
  154. package/lib/Store/make-cache-manager-store.d.ts +0 -14
  155. package/lib/Store/make-cache-manager-store.js +0 -83
  156. package/lib/Store/make-in-memory-store.d.ts +0 -118
  157. package/lib/Store/make-in-memory-store.js +0 -420
  158. package/lib/Store/make-ordered-dictionary.d.ts +0 -13
  159. package/lib/Store/make-ordered-dictionary.js +0 -81
  160. package/lib/Store/object-repository.d.ts +0 -10
  161. package/lib/Store/object-repository.js +0 -27
@@ -1,234 +1,252 @@
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.encodeBinaryNode = void 0;
27
- const constants = __importStar(require("./constants"));
28
- const jid_utils_1 = require("./jid-utils");
29
- const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
30
- const encoded = encodeBinaryNodeInner(node, opts, buffer);
31
- return Buffer.from(encoded);
32
- };
33
- exports.encodeBinaryNode = encodeBinaryNode;
34
- const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
35
- const { TAGS, TOKEN_MAP } = opts;
36
- const pushByte = (value) => buffer.push(value & 0xff);
37
- const pushInt = (value, n, littleEndian = false) => {
38
- for (let i = 0; i < n; i++) {
39
- const curShift = littleEndian ? i : n - 1 - i;
40
- buffer.push((value >> (curShift * 8)) & 0xff);
41
- }
42
- };
43
- const pushBytes = (bytes) => {
44
- for (const b of bytes) {
45
- buffer.push(b);
46
- }
47
- };
48
- const pushInt16 = (value) => {
49
- pushBytes([(value >> 8) & 0xff, value & 0xff]);
50
- };
51
- const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]));
52
- const writeByteLength = (length) => {
53
- if (length >= 4294967296) {
54
- throw new Error('string too large to encode: ' + length);
55
- }
56
- if (length >= 1 << 20) {
57
- pushByte(TAGS.BINARY_32);
58
- pushInt(length, 4); // 32 bit integer
59
- }
60
- else if (length >= 256) {
61
- pushByte(TAGS.BINARY_20);
62
- pushInt20(length);
63
- }
64
- else {
65
- pushByte(TAGS.BINARY_8);
66
- pushByte(length);
67
- }
68
- };
69
- const writeStringRaw = (str) => {
70
- const bytes = Buffer.from(str, 'utf-8');
71
- writeByteLength(bytes.length);
72
- pushBytes(bytes);
73
- };
74
- const writeJid = ({ domainType, device, user, server }) => {
75
- if (typeof device !== 'undefined') {
76
- pushByte(TAGS.AD_JID);
77
- pushByte(domainType || 0);
78
- pushByte(device || 0);
79
- writeString(user);
80
- }
81
- else {
82
- pushByte(TAGS.JID_PAIR);
83
- if (user.length) {
84
- writeString(user);
85
- }
86
- else {
87
- pushByte(TAGS.LIST_EMPTY);
88
- }
89
- writeString(server);
90
- }
91
- };
92
- const packNibble = (char) => {
93
- switch (char) {
94
- case '-':
95
- return 10;
96
- case '.':
97
- return 11;
98
- case '\0':
99
- return 15;
100
- default:
101
- if (char >= '0' && char <= '9') {
102
- return char.charCodeAt(0) - '0'.charCodeAt(0);
103
- }
104
- throw new Error(`invalid byte for nibble "${char}"`);
105
- }
106
- };
107
- const packHex = (char) => {
108
- if (char >= '0' && char <= '9') {
109
- return char.charCodeAt(0) - '0'.charCodeAt(0);
110
- }
111
- if (char >= 'A' && char <= 'F') {
112
- return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
113
- }
114
- if (char >= 'a' && char <= 'f') {
115
- return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
116
- }
117
- if (char === '\0') {
118
- return 15;
119
- }
120
- throw new Error(`Invalid hex char "${char}"`);
121
- };
122
- const writePackedBytes = (str, type) => {
123
- if (str.length > TAGS.PACKED_MAX) {
124
- throw new Error('Too many bytes to pack');
125
- }
126
- pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
127
- let roundedLength = Math.ceil(str.length / 2.0);
128
- if (str.length % 2 !== 0) {
129
- roundedLength |= 128;
130
- }
131
- pushByte(roundedLength);
132
- const packFunction = type === 'nibble' ? packNibble : packHex;
133
- const packBytePair = (v1, v2) => {
134
- const result = (packFunction(v1) << 4) | packFunction(v2);
135
- return result;
136
- };
137
- const strLengthHalf = Math.floor(str.length / 2);
138
- for (let i = 0; i < strLengthHalf; i++) {
139
- pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
140
- }
141
- if (str.length % 2 !== 0) {
142
- pushByte(packBytePair(str[str.length - 1], '\x00'));
143
- }
144
- };
145
- const isNibble = (str) => {
146
- if (str.length > TAGS.PACKED_MAX) {
147
- return false;
148
- }
149
- for (const char of str) {
150
- const isInNibbleRange = char >= '0' && char <= '9';
151
- if (!isInNibbleRange && char !== '-' && char !== '.') {
152
- return false;
153
- }
154
- }
155
- return true;
156
- };
157
- const isHex = (str) => {
158
- if (str.length > TAGS.PACKED_MAX) {
159
- return false;
160
- }
161
- for (const char of str) {
162
- const isInNibbleRange = char >= '0' && char <= '9';
163
- if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
164
- return false;
165
- }
166
- }
167
- return true;
168
- };
169
- const writeString = (str) => {
170
- const tokenIndex = TOKEN_MAP[str];
171
- if (tokenIndex) {
172
- if (typeof tokenIndex.dict === 'number') {
173
- pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
174
- }
175
- pushByte(tokenIndex.index);
176
- }
177
- else if (isNibble(str)) {
178
- writePackedBytes(str, 'nibble');
179
- }
180
- else if (isHex(str)) {
181
- writePackedBytes(str, 'hex');
182
- }
183
- else if (str) {
184
- const decodedJid = (0, jid_utils_1.jidDecode)(str);
185
- if (decodedJid) {
186
- writeJid(decodedJid);
187
- }
188
- else {
189
- writeStringRaw(str);
190
- }
191
- }
192
- };
193
- const writeListStart = (listSize) => {
194
- if (listSize === 0) {
195
- pushByte(TAGS.LIST_EMPTY);
196
- }
197
- else if (listSize < 256) {
198
- pushBytes([TAGS.LIST_8, listSize]);
199
- }
200
- else {
201
- pushByte(TAGS.LIST_16);
202
- pushInt16(listSize);
203
- }
204
- };
205
- const validAttributes = Object.keys(attrs).filter(k => (typeof attrs[k] !== 'undefined' && attrs[k] !== null));
206
- writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
207
- writeString(tag);
208
- for (const key of validAttributes) {
209
- if (typeof attrs[key] === 'string') {
210
- writeString(key);
211
- writeString(attrs[key]);
212
- }
213
- }
214
- if (typeof content === 'string') {
215
- writeString(content);
216
- }
217
- else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
218
- writeByteLength(content.length);
219
- pushBytes(content);
220
- }
221
- else if (Array.isArray(content)) {
222
- writeListStart(content.length);
223
- for (const item of content) {
224
- encodeBinaryNodeInner(item, opts, buffer);
225
- }
226
- }
227
- else if (typeof content === 'undefined') {
228
- // do nothing
229
- }
230
- else {
231
- throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
232
- }
233
- return buffer;
234
- };
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.encodeBinaryNode = void 0;
37
+ const constants = __importStar(require("./constants"));
38
+ const jid_utils_1 = require("./jid-utils");
39
+ const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
40
+ const encoded = encodeBinaryNodeInner(node, opts, buffer);
41
+ return Buffer.from(encoded);
42
+ };
43
+ exports.encodeBinaryNode = encodeBinaryNode;
44
+ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
45
+ const { TAGS, TOKEN_MAP } = opts;
46
+ const pushByte = (value) => buffer.push(value & 0xff);
47
+ const pushInt = (value, n, littleEndian = false) => {
48
+ for (let i = 0; i < n; i++) {
49
+ const curShift = littleEndian ? i : n - 1 - i;
50
+ buffer.push((value >> (curShift * 8)) & 0xff);
51
+ }
52
+ };
53
+ const pushBytes = (bytes) => {
54
+ for (const b of bytes) {
55
+ buffer.push(b);
56
+ }
57
+ };
58
+ const pushInt16 = (value) => {
59
+ pushBytes([(value >> 8) & 0xff, value & 0xff]);
60
+ };
61
+ const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]));
62
+ const writeByteLength = (length) => {
63
+ if (length >= 4294967296) {
64
+ throw new Error('string too large to encode: ' + length);
65
+ }
66
+ if (length >= 1 << 20) {
67
+ pushByte(TAGS.BINARY_32);
68
+ pushInt(length, 4); // 32 bit integer
69
+ }
70
+ else if (length >= 256) {
71
+ pushByte(TAGS.BINARY_20);
72
+ pushInt20(length);
73
+ }
74
+ else {
75
+ pushByte(TAGS.BINARY_8);
76
+ pushByte(length);
77
+ }
78
+ };
79
+ const writeStringRaw = (str) => {
80
+ const bytes = Buffer.from(str, 'utf-8');
81
+ writeByteLength(bytes.length);
82
+ pushBytes(bytes);
83
+ };
84
+ const writeJid = ({ domainType, device, user, server }) => {
85
+ if (typeof device !== 'undefined') {
86
+ pushByte(TAGS.AD_JID);
87
+ pushByte(domainType || 0);
88
+ pushByte(device || 0);
89
+ writeString(user);
90
+ }
91
+ else {
92
+ pushByte(TAGS.JID_PAIR);
93
+ if (user.length) {
94
+ writeString(user);
95
+ }
96
+ else {
97
+ pushByte(TAGS.LIST_EMPTY);
98
+ }
99
+ writeString(server);
100
+ }
101
+ };
102
+ const packNibble = (char) => {
103
+ switch (char) {
104
+ case '-':
105
+ return 10;
106
+ case '.':
107
+ return 11;
108
+ case '\0':
109
+ return 15;
110
+ default:
111
+ if (char >= '0' && char <= '9') {
112
+ return char.charCodeAt(0) - '0'.charCodeAt(0);
113
+ }
114
+ throw new Error(`invalid byte for nibble "${char}"`);
115
+ }
116
+ };
117
+ const packHex = (char) => {
118
+ if (char >= '0' && char <= '9') {
119
+ return char.charCodeAt(0) - '0'.charCodeAt(0);
120
+ }
121
+ if (char >= 'A' && char <= 'F') {
122
+ return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
123
+ }
124
+ if (char >= 'a' && char <= 'f') {
125
+ return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
126
+ }
127
+ if (char === '\0') {
128
+ return 15;
129
+ }
130
+ throw new Error(`Invalid hex char "${char}"`);
131
+ };
132
+ const writePackedBytes = (str, type) => {
133
+ if (str.length > TAGS.PACKED_MAX) {
134
+ throw new Error('Too many bytes to pack');
135
+ }
136
+ pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
137
+ let roundedLength = Math.ceil(str.length / 2.0);
138
+ if (str.length % 2 !== 0) {
139
+ roundedLength |= 128;
140
+ }
141
+ pushByte(roundedLength);
142
+ const packFunction = type === 'nibble' ? packNibble : packHex;
143
+ const packBytePair = (v1, v2) => {
144
+ const result = (packFunction(v1) << 4) | packFunction(v2);
145
+ return result;
146
+ };
147
+ const strLengthHalf = Math.floor(str.length / 2);
148
+ for (let i = 0; i < strLengthHalf; i++) {
149
+ pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
150
+ }
151
+ if (str.length % 2 !== 0) {
152
+ pushByte(packBytePair(str[str.length - 1], '\x00'));
153
+ }
154
+ };
155
+ const isNibble = (str) => {
156
+ if (!str || str.length > TAGS.PACKED_MAX) {
157
+ return false;
158
+ }
159
+ for (const char of str) {
160
+ const isInNibbleRange = char >= '0' && char <= '9';
161
+ if (!isInNibbleRange && char !== '-' && char !== '.') {
162
+ return false;
163
+ }
164
+ }
165
+ return true;
166
+ };
167
+ const isHex = (str) => {
168
+ if (!str || str.length > TAGS.PACKED_MAX) {
169
+ return false;
170
+ }
171
+ for (const char of str) {
172
+ const isInNibbleRange = char >= '0' && char <= '9';
173
+ if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
174
+ return false;
175
+ }
176
+ }
177
+ return true;
178
+ };
179
+ const writeString = (str) => {
180
+ if (str === undefined || str === null) {
181
+ pushByte(TAGS.LIST_EMPTY);
182
+ return;
183
+ }
184
+ const tokenIndex = TOKEN_MAP[str];
185
+ if (tokenIndex) {
186
+ if (typeof tokenIndex.dict === 'number') {
187
+ pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
188
+ }
189
+ pushByte(tokenIndex.index);
190
+ }
191
+ else if (isNibble(str)) {
192
+ writePackedBytes(str, 'nibble');
193
+ }
194
+ else if (isHex(str)) {
195
+ writePackedBytes(str, 'hex');
196
+ }
197
+ else if (str) {
198
+ const decodedJid = (0, jid_utils_1.jidDecode)(str);
199
+ if (decodedJid) {
200
+ writeJid(decodedJid);
201
+ }
202
+ else {
203
+ writeStringRaw(str);
204
+ }
205
+ }
206
+ };
207
+ const writeListStart = (listSize) => {
208
+ if (listSize === 0) {
209
+ pushByte(TAGS.LIST_EMPTY);
210
+ }
211
+ else if (listSize < 256) {
212
+ pushBytes([TAGS.LIST_8, listSize]);
213
+ }
214
+ else {
215
+ pushByte(TAGS.LIST_16);
216
+ pushInt16(listSize);
217
+ }
218
+ };
219
+ if (!tag) {
220
+ throw new Error('Invalid node: tag cannot be undefined');
221
+ }
222
+ const validAttributes = Object.keys(attrs || {}).filter(k => (typeof attrs[k] !== 'undefined' && attrs[k] !== null));
223
+ writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
224
+ writeString(tag);
225
+ for (const key of validAttributes) {
226
+ if (typeof attrs[key] === 'string') {
227
+ writeString(key);
228
+ writeString(attrs[key]);
229
+ }
230
+ }
231
+ if (typeof content === 'string') {
232
+ writeString(content);
233
+ }
234
+ else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
235
+ writeByteLength(content.length);
236
+ pushBytes(content);
237
+ }
238
+ else if (Array.isArray(content)) {
239
+ const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
240
+ writeListStart(validContent.length);
241
+ for (const item of validContent) {
242
+ encodeBinaryNodeInner(item, opts, buffer);
243
+ }
244
+ }
245
+ else if (typeof content === 'undefined') {
246
+ // do nothing
247
+ }
248
+ else {
249
+ throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
250
+ }
251
+ return buffer;
252
+ };
@@ -1,16 +1,14 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { proto } from '../../WAProto';
4
- import { BinaryNode } from './types';
5
- export declare const getBinaryNodeChildren: (node: BinaryNode | undefined, childTag: string) => BinaryNode[];
6
- export declare const getAllBinaryNodeChildren: ({ content }: BinaryNode) => BinaryNode[];
7
- export declare const getBinaryNodeChild: (node: BinaryNode | undefined, childTag: string) => BinaryNode | undefined;
8
- export declare const getBinaryNodeChildBuffer: (node: BinaryNode | undefined, childTag: string) => Uint8Array | Buffer | undefined;
9
- export declare const getBinaryNodeChildString: (node: BinaryNode | undefined, childTag: string) => string | undefined;
10
- export declare const getBinaryNodeChildUInt: (node: BinaryNode, childTag: string, length: number) => number | undefined;
11
- export declare const assertNodeErrorFree: (node: BinaryNode) => void;
12
- export declare const reduceBinaryNodeToDictionary: (node: BinaryNode, tag: string) => {
13
- [_: string]: string;
14
- };
15
- export declare const getBinaryNodeMessages: ({ content }: BinaryNode) => proto.WebMessageInfo[];
16
- export declare function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i?: number): any;
1
+ import { proto } from '../../WAProto';
2
+ import { BinaryNode } from './types';
3
+ export declare const getBinaryNodeChildren: (node: BinaryNode | undefined, childTag: string) => BinaryNode[];
4
+ export declare const getAllBinaryNodeChildren: ({ content }: BinaryNode) => BinaryNode[];
5
+ export declare const getBinaryNodeChild: (node: BinaryNode | undefined, childTag: string) => BinaryNode | undefined;
6
+ export declare const getBinaryNodeChildBuffer: (node: BinaryNode | undefined, childTag: string) => Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike> | undefined;
7
+ export declare const getBinaryNodeChildString: (node: BinaryNode | undefined, childTag: string) => string | undefined;
8
+ export declare const getBinaryNodeChildUInt: (node: BinaryNode, childTag: string, length: number) => number | undefined;
9
+ export declare const assertNodeErrorFree: (node: BinaryNode) => void;
10
+ export declare const reduceBinaryNodeToDictionary: (node: BinaryNode, tag: string) => {
11
+ [_: string]: string;
12
+ };
13
+ export declare const getBinaryNodeMessages: ({ content }: BinaryNode) => proto.WebMessageInfo[];
14
+ export declare function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i?: number): any;