@rizzxdc/baileys 8.0.10 → 8.1.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 (220) hide show
  1. package/LICENSE +4 -2
  2. package/README.md +107 -245
  3. package/WAProto/index.d.ts +15795 -0
  4. package/WAProto/index.js +25587 -4867
  5. package/lib/Defaults/index.d.ts +147 -0
  6. package/lib/Defaults/index.js +134 -104
  7. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  8. package/lib/Signal/Group/ciphertext-message.js +9 -11
  9. package/lib/Signal/Group/group-session-builder.d.ts +8 -0
  10. package/lib/Signal/Group/group-session-builder.js +5 -8
  11. package/lib/Signal/Group/group_cipher.d.ts +11 -0
  12. package/lib/Signal/Group/group_cipher.js +72 -75
  13. package/lib/Signal/Group/index.d.ts +12 -0
  14. package/lib/Signal/Group/index.js +11 -13
  15. package/lib/Signal/Group/keyhelper.d.ts +7 -0
  16. package/lib/Signal/Group/keyhelper.js +11 -14
  17. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  18. package/lib/Signal/Group/sender-chain-key.js +23 -26
  19. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  20. package/lib/Signal/Group/sender-key-distribution-message.js +58 -61
  21. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  22. package/lib/Signal/Group/sender-key-message.js +61 -64
  23. package/lib/Signal/Group/sender-key-name.d.ts +12 -0
  24. package/lib/Signal/Group/sender-key-name.js +39 -44
  25. package/lib/Signal/Group/sender-key-record.d.ts +13 -0
  26. package/lib/Signal/Group/sender-key-record.js +35 -38
  27. package/lib/Signal/Group/sender-key-state.d.ts +17 -0
  28. package/lib/Signal/Group/sender-key-state.js +76 -79
  29. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  30. package/lib/Signal/Group/sender-message-key.js +23 -26
  31. package/lib/Signal/libsignal.d.ts +55 -0
  32. package/lib/Signal/libsignal.js +414 -308
  33. package/lib/Signal/lid-mapping.d.ts +21 -0
  34. package/lib/Signal/lid-mapping.js +270 -149
  35. package/lib/Socket/Client/index.d.ts +3 -0
  36. package/lib/Socket/Client/index.js +2 -4
  37. package/lib/Socket/Client/types.d.ts +6 -0
  38. package/lib/Socket/Client/types.js +8 -11
  39. package/lib/Socket/Client/websocket.d.ts +13 -0
  40. package/lib/Socket/Client/websocket.js +51 -50
  41. package/lib/Socket/business.d.ts +395 -0
  42. package/lib/Socket/business.js +357 -355
  43. package/lib/Socket/chats.d.ts +137 -0
  44. package/lib/Socket/chats.js +1195 -867
  45. package/lib/Socket/communities.d.ts +562 -0
  46. package/lib/Socket/communities.js +416 -398
  47. package/lib/Socket/dugong.js +8 -1
  48. package/lib/Socket/groups.d.ts +286 -0
  49. package/lib/Socket/groups.js +336 -301
  50. package/lib/Socket/index.d.ts +530 -0
  51. package/lib/Socket/index.js +8 -13
  52. package/lib/Socket/messages-recv.d.ts +334 -0
  53. package/lib/Socket/messages-recv.js +1729 -1121
  54. package/lib/Socket/messages-send.d.ts +330 -0
  55. package/lib/Socket/messages-send.js +684 -462
  56. package/lib/Socket/mex.d.ts +2 -0
  57. package/lib/Socket/mex.js +36 -40
  58. package/lib/Socket/newsletter.d.ts +284 -0
  59. package/lib/Socket/newsletter.js +247 -237
  60. package/lib/Socket/socket.d.ts +61 -0
  61. package/lib/Socket/socket.js +968 -790
  62. package/lib/Store/index.d.ts +4 -0
  63. package/lib/Store/index.js +4 -6
  64. package/lib/Store/make-cache-manager-store.js +2 -2
  65. package/lib/Store/make-in-memory-store.d.ts +63 -0
  66. package/lib/Store/make-in-memory-store.js +414 -284
  67. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  68. package/lib/Store/make-ordered-dictionary.js +75 -76
  69. package/lib/Store/object-repository.d.ts +11 -0
  70. package/lib/Store/object-repository.js +21 -23
  71. package/lib/Types/Auth.d.ts +2 -0
  72. package/lib/Types/Auth.js +0 -2
  73. package/lib/Types/Bussines.d.ts +2 -0
  74. package/lib/Types/Bussines.js +0 -2
  75. package/lib/Types/Call.d.ts +2 -0
  76. package/lib/Types/Call.js +0 -2
  77. package/lib/Types/Chat.d.ts +2 -0
  78. package/lib/Types/Chat.js +5 -7
  79. package/lib/Types/Contact.d.ts +2 -0
  80. package/lib/Types/Contact.js +0 -2
  81. package/lib/Types/Events.d.ts +2 -0
  82. package/lib/Types/Events.js +1 -3
  83. package/lib/Types/GroupMetadata.d.ts +2 -0
  84. package/lib/Types/GroupMetadata.js +0 -2
  85. package/lib/Types/Label.d.ts +3 -0
  86. package/lib/Types/Label.js +21 -22
  87. package/lib/Types/LabelAssociation.d.ts +3 -0
  88. package/lib/Types/LabelAssociation.js +3 -4
  89. package/lib/Types/Message.d.ts +12 -0
  90. package/lib/Types/Message.js +11 -6
  91. package/lib/Types/Mex.d.ts +3 -0
  92. package/lib/Types/Mex.js +38 -0
  93. package/lib/Types/Product.d.ts +2 -0
  94. package/lib/Types/Product.js +0 -2
  95. package/lib/Types/RichType.d.ts +3 -0
  96. package/lib/Types/RichType.js +22 -0
  97. package/lib/Types/Signal.d.ts +2 -0
  98. package/lib/Types/Signal.js +1 -3
  99. package/lib/Types/Socket.d.ts +2 -0
  100. package/lib/Types/Socket.js +1 -4
  101. package/lib/Types/State.d.ts +6 -0
  102. package/lib/Types/State.js +52 -8
  103. package/lib/Types/USync.d.ts +2 -0
  104. package/lib/Types/USync.js +1 -3
  105. package/lib/Types/index.d.ts +14 -0
  106. package/lib/Types/index.js +22 -25
  107. package/lib/Utils/auth-utils.d.ts +58 -0
  108. package/lib/Utils/auth-utils.js +285 -203
  109. package/lib/Utils/browser-utils.d.ts +9 -0
  110. package/lib/Utils/browser-utils.js +21 -19
  111. package/lib/Utils/business.d.ts +50 -0
  112. package/lib/Utils/business.js +206 -209
  113. package/lib/Utils/chat-utils.d.ts +409 -0
  114. package/lib/Utils/chat-utils.js +825 -706
  115. package/lib/Utils/companion-reg-client-utils.d.ts +5 -0
  116. package/lib/Utils/companion-reg-client-utils.js +39 -0
  117. package/lib/Utils/crypto.d.ts +40 -0
  118. package/lib/Utils/crypto.js +88 -101
  119. package/lib/Utils/decode-wa-message.d.ts +84 -0
  120. package/lib/Utils/decode-wa-message.js +287 -238
  121. package/lib/Utils/event-buffer.d.ts +13 -0
  122. package/lib/Utils/event-buffer.js +602 -509
  123. package/lib/Utils/generics.d.ts +64 -0
  124. package/lib/Utils/generics.js +334 -295
  125. package/lib/Utils/history.d.ts +76 -0
  126. package/lib/Utils/history.js +125 -79
  127. package/lib/Utils/identity-change-handler.d.ts +14 -0
  128. package/lib/Utils/identity-change-handler.js +49 -0
  129. package/lib/Utils/index.d.ts +25 -0
  130. package/lib/Utils/index.js +25 -21
  131. package/lib/Utils/link-preview.d.ts +13 -0
  132. package/lib/Utils/link-preview.js +73 -70
  133. package/lib/Utils/logger.d.ts +3 -0
  134. package/lib/Utils/logger.js +1 -4
  135. package/lib/Utils/lt-hash.d.ts +7 -0
  136. package/lib/Utils/lt-hash.js +7 -45
  137. package/lib/Utils/make-mutex.d.ts +7 -0
  138. package/lib/Utils/make-mutex.js +27 -31
  139. package/lib/Utils/message-retry-manager.d.ts +80 -0
  140. package/lib/Utils/message-retry-manager.js +165 -14
  141. package/lib/Utils/messages-media.d.ts +137 -0
  142. package/lib/Utils/messages-media.js +768 -572
  143. package/lib/Utils/messages.d.ts +46 -0
  144. package/lib/Utils/messages.js +1082 -130
  145. package/lib/Utils/noise-handler.d.ts +21 -0
  146. package/lib/Utils/noise-handler.js +191 -135
  147. package/lib/Utils/offline-node-processor.d.ts +10 -0
  148. package/lib/Utils/offline-node-processor.js +39 -0
  149. package/lib/Utils/pre-key-manager.d.ts +26 -0
  150. package/lib/Utils/pre-key-manager.js +96 -76
  151. package/lib/Utils/process-message.d.ts +84 -0
  152. package/lib/Utils/process-message.js +718 -311
  153. package/lib/Utils/reporting-utils.d.ts +13 -0
  154. package/lib/Utils/reporting-utils.js +257 -0
  155. package/lib/Utils/rich-message-utils.d.ts +49 -0
  156. package/lib/Utils/rich-message-utils.js +257 -0
  157. package/lib/Utils/signal.d.ts +83 -0
  158. package/lib/Utils/signal.js +174 -135
  159. package/lib/Utils/stanza-ack.d.ts +17 -0
  160. package/lib/Utils/stanza-ack.js +37 -0
  161. package/lib/Utils/sync-action-utils.d.ts +3 -0
  162. package/lib/Utils/sync-action-utils.js +48 -0
  163. package/lib/Utils/tc-token-utils.d.ts +30 -0
  164. package/lib/Utils/tc-token-utils.js +162 -0
  165. package/lib/Utils/use-multi-file-auth-state.d.ts +11 -0
  166. package/lib/Utils/use-multi-file-auth-state.js +104 -95
  167. package/lib/Utils/use-single-file-auth-state.d.ts +11 -0
  168. package/lib/Utils/use-single-file-auth-state.js +108 -0
  169. package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  170. package/lib/Utils/use-sqlite-auth-state.js +108 -0
  171. package/lib/Utils/validate-connection.d.ts +44 -0
  172. package/lib/Utils/validate-connection.js +182 -180
  173. package/lib/WABinary/constants.d.ts +62 -0
  174. package/lib/WABinary/constants.js +1455 -1292
  175. package/lib/WABinary/decode.d.ts +10 -0
  176. package/lib/WABinary/decode.js +249 -228
  177. package/lib/WABinary/encode.d.ts +3 -0
  178. package/lib/WABinary/encode.js +207 -206
  179. package/lib/WABinary/generic-utils.d.ts +77 -0
  180. package/lib/WABinary/generic-utils.js +135 -99
  181. package/lib/WABinary/index.d.ts +6 -0
  182. package/lib/WABinary/index.js +5 -7
  183. package/lib/WABinary/jid-utils.d.ts +29 -0
  184. package/lib/WABinary/jid-utils.js +80 -78
  185. package/lib/WABinary/types.d.ts +2 -0
  186. package/lib/WABinary/types.js +1 -3
  187. package/lib/WAM/BinaryInfo.d.ts +8 -0
  188. package/lib/WAM/BinaryInfo.js +7 -9
  189. package/lib/WAM/constants.d.ts +34927 -0
  190. package/lib/WAM/constants.js +22847 -22848
  191. package/lib/WAM/encode.d.ts +2 -0
  192. package/lib/WAM/encode.js +129 -134
  193. package/lib/WAM/index.d.ts +4 -0
  194. package/lib/WAM/index.js +3 -5
  195. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +37 -0
  196. package/lib/WAUSync/Protocols/USyncContactProtocol.js +48 -27
  197. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  198. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -48
  199. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  200. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -26
  201. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  202. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -35
  203. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  204. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  205. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +39 -0
  206. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +46 -49
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +15 -0
  208. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +23 -25
  209. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  210. package/lib/WAUSync/Protocols/index.js +5 -6
  211. package/lib/WAUSync/USyncQuery.d.ts +21 -0
  212. package/lib/WAUSync/USyncQuery.js +92 -85
  213. package/lib/WAUSync/USyncUser.d.ts +17 -0
  214. package/lib/WAUSync/USyncUser.js +28 -22
  215. package/lib/WAUSync/index.d.ts +4 -0
  216. package/lib/WAUSync/index.js +3 -5
  217. package/lib/index.d.ts +12 -0
  218. package/lib/index.js +31 -19
  219. package/package.json +12 -6
  220. package/lib/Types/Newsletter.js +0 -33
@@ -0,0 +1,2 @@
1
+ export function encodeWAM(binaryInfo: any): any;
2
+ //# sourceMappingURL=encode.d.ts.map
package/lib/WAM/encode.js CHANGED
@@ -1,154 +1,149 @@
1
- //=======================================================//
2
- import { FLAG_BYTE, FLAG_EVENT, FLAG_EXTENDED, FLAG_FIELD, FLAG_GLOBAL, WEB_EVENTS, WEB_GLOBALS } from "./constants.js";
3
- import { BinaryInfo } from "./BinaryInfo.js";
4
- //=======================================================//
1
+ import { BinaryInfo } from './BinaryInfo.js';
2
+ import { FLAG_BYTE, FLAG_EVENT, FLAG_EXTENDED, FLAG_FIELD, FLAG_GLOBAL, WEB_EVENTS, WEB_GLOBALS } from './constants.js';
5
3
  const getHeaderBitLength = (key) => (key < 256 ? 2 : 3);
6
4
  export const encodeWAM = (binaryInfo) => {
7
- binaryInfo.buffer = [];
8
- encodeWAMHeader(binaryInfo);
9
- encodeEvents(binaryInfo);
10
- const totalSize = binaryInfo.buffer.map(a => a.length).reduce((a, b) => a + b);
11
- const buffer = Buffer.alloc(totalSize);
12
- let offset = 0;
13
- for (const buffer_ of binaryInfo.buffer) {
14
- buffer_.copy(buffer, offset);
15
- offset += buffer_.length;
16
- }
17
- return buffer;
5
+ binaryInfo.buffer = [];
6
+ encodeWAMHeader(binaryInfo);
7
+ encodeEvents(binaryInfo);
8
+ const totalSize = binaryInfo.buffer.map(a => a.length).reduce((a, b) => a + b);
9
+ const buffer = Buffer.alloc(totalSize);
10
+ let offset = 0;
11
+ for (const buffer_ of binaryInfo.buffer) {
12
+ buffer_.copy(buffer, offset);
13
+ offset += buffer_.length;
14
+ }
15
+ return buffer;
18
16
  };
19
- //=======================================================//
20
17
  function encodeWAMHeader(binaryInfo) {
21
- const headerBuffer = Buffer.alloc(8);
22
- headerBuffer.write("WAM", 0, "utf8");
23
- headerBuffer.writeUInt8(binaryInfo.protocolVersion, 3);
24
- headerBuffer.writeUInt8(1, 4);
25
- headerBuffer.writeUInt16BE(binaryInfo.sequence, 5);
26
- headerBuffer.writeUInt8(0, 7);
27
- binaryInfo.buffer.push(headerBuffer);
18
+ const headerBuffer = Buffer.alloc(8); // starting buffer
19
+ headerBuffer.write('WAM', 0, 'utf8');
20
+ headerBuffer.writeUInt8(binaryInfo.protocolVersion, 3);
21
+ headerBuffer.writeUInt8(1, 4); //random flag
22
+ headerBuffer.writeUInt16BE(binaryInfo.sequence, 5);
23
+ headerBuffer.writeUInt8(0, 7); // regular channel
24
+ binaryInfo.buffer.push(headerBuffer);
28
25
  }
29
- //=======================================================//
30
26
  function encodeGlobalAttributes(binaryInfo, globals) {
31
- for (const [key, _value] of Object.entries(globals)) {
32
- const id = WEB_GLOBALS.find(a => a?.name === key).id;
33
- let value = _value;
34
- if (typeof value === "boolean") {
35
- value = value ? 1 : 0;
27
+ for (const [key, _value] of Object.entries(globals)) {
28
+ const id = WEB_GLOBALS.find(a => a?.name === key).id;
29
+ let value = _value;
30
+ if (typeof value === 'boolean') {
31
+ value = value ? 1 : 0;
32
+ }
33
+ binaryInfo.buffer.push(serializeData(id, value, FLAG_GLOBAL));
36
34
  }
37
- binaryInfo.buffer.push(serializeData(id, value, FLAG_GLOBAL));
38
- }
39
35
  }
40
- //=======================================================//
41
36
  function encodeEvents(binaryInfo) {
42
- for (const [name, { props, globals }] of binaryInfo.events.map(a => Object.entries(a)[0])) {
43
- encodeGlobalAttributes(binaryInfo, globals);
44
- const event = WEB_EVENTS.find(a => a.name === name);
45
- const props_ = Object.entries(props);
46
- let extended = false;
47
- for (const [, value] of props_) {
48
- extended || (extended = value !== null);
37
+ for (const [name, { props, globals }] of binaryInfo.events.map(a => Object.entries(a)[0])) {
38
+ encodeGlobalAttributes(binaryInfo, globals);
39
+ const event = WEB_EVENTS.find(a => a.name === name);
40
+ const props_ = Object.entries(props);
41
+ let extended = false;
42
+ for (const [, value] of props_) {
43
+ extended || (extended = value !== null);
44
+ }
45
+ const eventFlag = extended ? FLAG_EVENT : FLAG_EVENT | FLAG_EXTENDED;
46
+ binaryInfo.buffer.push(serializeData(event.id, -event.weight, eventFlag));
47
+ for (let i = 0; i < props_.length; i++) {
48
+ const [key, _value] = props_[i];
49
+ const id = event.props[key]?.[0];
50
+ extended = i < props_.length - 1;
51
+ let value = _value;
52
+ if (typeof value === 'boolean') {
53
+ value = value ? 1 : 0;
54
+ }
55
+ const fieldFlag = extended ? FLAG_EVENT : FLAG_FIELD | FLAG_EXTENDED;
56
+ binaryInfo.buffer.push(serializeData(id, value, fieldFlag));
57
+ }
49
58
  }
50
- const eventFlag = extended ? FLAG_EVENT : FLAG_EVENT | FLAG_EXTENDED;
51
- binaryInfo.buffer.push(serializeData(event.id, -event.weight, eventFlag));
52
- for (let i = 0; i < props_.length; i++) {
53
- const [key, _value] = props_[i];
54
- const id = event.props[key]?.[0];
55
- extended = i < props_.length - 1;
56
- let value = _value;
57
- if (typeof value === "boolean") {
58
- value = value ? 1 : 0;
59
- }
60
- const fieldFlag = extended ? FLAG_EVENT : FLAG_FIELD | FLAG_EXTENDED;
61
- binaryInfo.buffer.push(serializeData(id, value, fieldFlag));
62
- }
63
- }
64
59
  }
65
- //=======================================================//
66
60
  function serializeData(key, value, flag) {
67
- const bufferLength = getHeaderBitLength(key);
68
- let buffer;
69
- let offset = 0;
70
- if (value === null) {
71
- if (flag === FLAG_GLOBAL) {
72
- buffer = Buffer.alloc(bufferLength);
73
- offset = serializeHeader(buffer, offset, key, flag);
74
- return buffer;
75
- }
76
- }
77
- else if (typeof value === "number" && Number.isInteger(value)) {
78
- if (value === 0 || value === 1) {
79
- buffer = Buffer.alloc(bufferLength);
80
- offset = serializeHeader(buffer, offset, key, flag | ((value + 1) << 4));
81
- return buffer;
61
+ const bufferLength = getHeaderBitLength(key);
62
+ let buffer;
63
+ let offset = 0;
64
+ if (value === null) {
65
+ if (flag === FLAG_GLOBAL) {
66
+ buffer = Buffer.alloc(bufferLength);
67
+ offset = serializeHeader(buffer, offset, key, flag);
68
+ return buffer;
69
+ }
82
70
  }
83
- else if (-128 <= value && value < 128) {
84
- buffer = Buffer.alloc(bufferLength + 1);
85
- offset = serializeHeader(buffer, offset, key, flag | (3 << 4));
86
- buffer.writeInt8(value, offset);
87
- return buffer;
71
+ else if (typeof value === 'number' && Number.isInteger(value)) {
72
+ // is number
73
+ if (value === 0 || value === 1) {
74
+ buffer = Buffer.alloc(bufferLength);
75
+ offset = serializeHeader(buffer, offset, key, flag | ((value + 1) << 4));
76
+ return buffer;
77
+ }
78
+ else if (-128 <= value && value < 128) {
79
+ buffer = Buffer.alloc(bufferLength + 1);
80
+ offset = serializeHeader(buffer, offset, key, flag | (3 << 4));
81
+ buffer.writeInt8(value, offset);
82
+ return buffer;
83
+ }
84
+ else if (-32768 <= value && value < 32768) {
85
+ buffer = Buffer.alloc(bufferLength + 2);
86
+ offset = serializeHeader(buffer, offset, key, flag | (4 << 4));
87
+ buffer.writeInt16LE(value, offset);
88
+ return buffer;
89
+ }
90
+ else if (-2147483648 <= value && value < 2147483648) {
91
+ buffer = Buffer.alloc(bufferLength + 4);
92
+ offset = serializeHeader(buffer, offset, key, flag | (5 << 4));
93
+ buffer.writeInt32LE(value, offset);
94
+ return buffer;
95
+ }
96
+ else {
97
+ buffer = Buffer.alloc(bufferLength + 8);
98
+ offset = serializeHeader(buffer, offset, key, flag | (7 << 4));
99
+ buffer.writeDoubleLE(value, offset);
100
+ return buffer;
101
+ }
88
102
  }
89
- else if (-32768 <= value && value < 32768) {
90
- buffer = Buffer.alloc(bufferLength + 2);
91
- offset = serializeHeader(buffer, offset, key, flag | (4 << 4));
92
- buffer.writeInt16LE(value, offset);
93
- return buffer;
103
+ else if (typeof value === 'number') {
104
+ // is float
105
+ buffer = Buffer.alloc(bufferLength + 8);
106
+ offset = serializeHeader(buffer, offset, key, flag | (7 << 4));
107
+ buffer.writeDoubleLE(value, offset);
108
+ return buffer;
94
109
  }
95
- else if (-2147483648 <= value && value < 2147483648) {
96
- buffer = Buffer.alloc(bufferLength + 4);
97
- offset = serializeHeader(buffer, offset, key, flag | (5 << 4));
98
- buffer.writeInt32LE(value, offset);
99
- return buffer;
110
+ else if (typeof value === 'string') {
111
+ // is string
112
+ const utf8Bytes = Buffer.byteLength(value, 'utf8');
113
+ if (utf8Bytes < 256) {
114
+ buffer = Buffer.alloc(bufferLength + 1 + utf8Bytes);
115
+ offset = serializeHeader(buffer, offset, key, flag | (8 << 4));
116
+ buffer.writeUint8(utf8Bytes, offset++);
117
+ }
118
+ else if (utf8Bytes < 65536) {
119
+ buffer = Buffer.alloc(bufferLength + 2 + utf8Bytes);
120
+ offset = serializeHeader(buffer, offset, key, flag | (9 << 4));
121
+ buffer.writeUInt16LE(utf8Bytes, offset);
122
+ offset += 2;
123
+ }
124
+ else {
125
+ buffer = Buffer.alloc(bufferLength + 4 + utf8Bytes);
126
+ offset = serializeHeader(buffer, offset, key, flag | (10 << 4));
127
+ buffer.writeUInt32LE(utf8Bytes, offset);
128
+ offset += 4;
129
+ }
130
+ buffer.write(value, offset, 'utf8');
131
+ return buffer;
100
132
  }
101
- else {
102
- buffer = Buffer.alloc(bufferLength + 8);
103
- offset = serializeHeader(buffer, offset, key, flag | (7 << 4));
104
- buffer.writeDoubleLE(value, offset);
105
- return buffer;
106
- }
107
- }
108
- else if (typeof value === "number") {
109
- buffer = Buffer.alloc(bufferLength + 8);
110
- offset = serializeHeader(buffer, offset, key, flag | (7 << 4));
111
- buffer.writeDoubleLE(value, offset);
112
- return buffer;
113
- }
114
- else if (typeof value === "string") {
115
- const utf8Bytes = Buffer.byteLength(value, "utf8");
116
- if (utf8Bytes < 256) {
117
- buffer = Buffer.alloc(bufferLength + 1 + utf8Bytes);
118
- offset = serializeHeader(buffer, offset, key, flag | (8 << 4));
119
- buffer.writeUint8(utf8Bytes, offset++);
120
- }
121
- else if (utf8Bytes < 65536) {
122
- buffer = Buffer.alloc(bufferLength + 2 + utf8Bytes);
123
- offset = serializeHeader(buffer, offset, key, flag | (9 << 4));
124
- buffer.writeUInt16LE(utf8Bytes, offset);
125
- offset += 2;
133
+ throw 'missing';
134
+ }
135
+ function serializeHeader(buffer, offset, key, flag) {
136
+ if (key < 256) {
137
+ buffer.writeUInt8(flag, offset);
138
+ offset += 1;
139
+ buffer.writeUInt8(key, offset);
140
+ offset += 1;
126
141
  }
127
142
  else {
128
- buffer = Buffer.alloc(bufferLength + 4 + utf8Bytes);
129
- offset = serializeHeader(buffer, offset, key, flag | (10 << 4));
130
- buffer.writeUInt32LE(utf8Bytes, offset);
131
- offset += 4;
143
+ buffer.writeUInt8(flag | FLAG_BYTE, offset);
144
+ offset += 1;
145
+ buffer.writeUInt16LE(key, offset);
146
+ offset += 2;
132
147
  }
133
- buffer.write(value, offset, "utf8");
134
- return buffer;
135
- }
136
- throw "missing";
137
- }
138
- //=======================================================//
139
- function serializeHeader(buffer, offset, key, flag) {
140
- if (key < 256) {
141
- buffer.writeUInt8(flag, offset);
142
- offset += 1;
143
- buffer.writeUInt8(key, offset);
144
- offset += 1;
145
- }
146
- else {
147
- buffer.writeUInt8(flag | FLAG_BYTE, offset);
148
- offset += 1;
149
- buffer.writeUInt16LE(key, offset);
150
- offset += 2;
151
- }
152
- return offset;
148
+ return offset;
153
149
  }
154
- //=======================================================//
@@ -0,0 +1,4 @@
1
+ export * from "./constants.js";
2
+ export * from "./encode.js";
3
+ export * from "./BinaryInfo.js";
4
+ //# sourceMappingURL=index.d.ts.map
package/lib/WAM/index.js CHANGED
@@ -1,5 +1,3 @@
1
- //=======================================================//
2
- export * from "./BinaryInfo.js";
3
- export * from "./constants.js";
4
- export * from "./encode.js";
5
- //=======================================================//
1
+ export * from './constants.js';
2
+ export * from './encode.js';
3
+ export * from './BinaryInfo.js';
@@ -0,0 +1,37 @@
1
+ export class USyncContactProtocol {
2
+ name: string;
3
+ getQueryElement(): {
4
+ tag: string;
5
+ attrs: {};
6
+ };
7
+ getUserElement(user: any): {
8
+ tag: string;
9
+ attrs: {
10
+ type?: undefined;
11
+ };
12
+ content: any;
13
+ } | {
14
+ tag: string;
15
+ attrs: {
16
+ lid?: any;
17
+ pin?: any;
18
+ username: any;
19
+ type?: undefined;
20
+ };
21
+ content?: undefined;
22
+ } | {
23
+ tag: string;
24
+ attrs: {
25
+ type: any;
26
+ };
27
+ content?: undefined;
28
+ } | {
29
+ tag: string;
30
+ attrs: {
31
+ type?: undefined;
32
+ };
33
+ content?: undefined;
34
+ };
35
+ parser(node: any): boolean;
36
+ }
37
+ //# sourceMappingURL=USyncContactProtocol.d.ts.map
@@ -1,30 +1,51 @@
1
- //=======================================================//
2
- import { assertNodeErrorFree } from "../../WABinary/index.js";
3
- import { USyncUser } from "../USyncUser.js";
4
- //=======================================================//
1
+ import { assertNodeErrorFree } from '../../WABinary/index.js';
2
+ import { USyncUser } from '../USyncUser.js';
5
3
  export class USyncContactProtocol {
6
- constructor() {
7
- this.name = "contact";
8
- }
9
- getQueryElement() {
10
- return {
11
- tag: "contact",
12
- attrs: {}
13
- };
14
- }
15
- getUserElement(user) {
16
- return {
17
- tag: "contact",
18
- attrs: {},
19
- content: user.phone
20
- };
21
- }
22
- parser(node) {
23
- if (node.tag === "contact") {
24
- assertNodeErrorFree(node);
25
- return node?.attrs?.type === "in";
4
+ constructor() {
5
+ this.name = 'contact';
6
+ }
7
+ getQueryElement() {
8
+ return {
9
+ tag: 'contact',
10
+ attrs: {}
11
+ };
12
+ }
13
+ getUserElement(user) {
14
+ if (user.phone) {
15
+ return {
16
+ tag: 'contact',
17
+ attrs: {},
18
+ content: user.phone
19
+ };
20
+ }
21
+ if (user.username) {
22
+ return {
23
+ tag: 'contact',
24
+ attrs: {
25
+ username: user.username,
26
+ ...(user.usernameKey ? { pin: user.usernameKey } : {}),
27
+ ...(user.lid ? { lid: user.lid } : {})
28
+ }
29
+ };
30
+ }
31
+ if (user.type) {
32
+ return {
33
+ tag: 'contact',
34
+ attrs: {
35
+ type: user.type
36
+ }
37
+ };
38
+ }
39
+ return {
40
+ tag: 'contact',
41
+ attrs: {}
42
+ };
43
+ }
44
+ parser(node) {
45
+ if (node.tag === 'contact') {
46
+ assertNodeErrorFree(node);
47
+ return node?.attrs?.type === 'in';
48
+ }
49
+ return false;
26
50
  }
27
- return false;
28
- }
29
51
  }
30
- //=======================================================//
@@ -0,0 +1,23 @@
1
+ export class USyncDeviceProtocol {
2
+ name: string;
3
+ getQueryElement(): {
4
+ tag: string;
5
+ attrs: {
6
+ version: string;
7
+ };
8
+ };
9
+ getUserElement(): null;
10
+ parser(node: any): {
11
+ deviceList: {
12
+ id: number;
13
+ keyIndex: number;
14
+ isHosted: boolean;
15
+ }[];
16
+ keyIndex: {
17
+ timestamp: number;
18
+ signedKeyIndex: any;
19
+ expectedTimestamp: number | undefined;
20
+ } | undefined;
21
+ };
22
+ }
23
+ //# sourceMappingURL=USyncDeviceProtocol.d.ts.map
@@ -1,53 +1,53 @@
1
- //=======================================================//
2
- import { assertNodeErrorFree, getBinaryNodeChild } from "../../WABinary/index.js";
3
- //=======================================================//
1
+ import { assertNodeErrorFree, getBinaryNodeChild } from '../../WABinary/index.js';
4
2
  export class USyncDeviceProtocol {
5
- constructor() {
6
- this.name = "devices";
7
- }
8
- getQueryElement() {
9
- return {
10
- tag: "devices",
11
- attrs: {
12
- version: "2"
13
- }
14
- };
15
- }
16
- getUserElement() {
17
- return null;
18
- }
19
- parser(node) {
20
- const deviceList = [];
21
- let keyIndex = undefined;
22
- if (node.tag === "devices") {
23
- assertNodeErrorFree(node);
24
- const deviceListNode = getBinaryNodeChild(node, "device-list");
25
- const keyIndexNode = getBinaryNodeChild(node, "key-index-list");
26
- if (Array.isArray(deviceListNode?.content)) {
27
- for (const { tag, attrs } of deviceListNode.content) {
28
- const id = +attrs.id;
29
- const keyIndex = +attrs["key-index"];
30
- if (tag === "device") {
31
- deviceList.push({
32
- id,
33
- keyIndex,
34
- isHosted: !!(attrs["is_hosted"] && attrs["is_hosted"] === "true")
35
- });
36
- }
3
+ constructor() {
4
+ this.name = 'devices';
5
+ }
6
+ getQueryElement() {
7
+ return {
8
+ tag: 'devices',
9
+ attrs: {
10
+ version: '2'
11
+ }
12
+ };
13
+ }
14
+ getUserElement( /* user: USyncUser */) {
15
+ //TODO: Implement device phashing, ts and expectedTs
16
+ //TODO: if all are not present, return null <- current behavior
17
+ //TODO: otherwise return a node w tag 'devices' w those as attrs
18
+ return null;
19
+ }
20
+ parser(node) {
21
+ const deviceList = [];
22
+ let keyIndex = undefined;
23
+ if (node.tag === 'devices') {
24
+ assertNodeErrorFree(node);
25
+ const deviceListNode = getBinaryNodeChild(node, 'device-list');
26
+ const keyIndexNode = getBinaryNodeChild(node, 'key-index-list');
27
+ if (Array.isArray(deviceListNode?.content)) {
28
+ for (const { tag, attrs } of deviceListNode.content) {
29
+ const id = +attrs.id;
30
+ const keyIndex = +attrs['key-index'];
31
+ if (tag === 'device') {
32
+ deviceList.push({
33
+ id,
34
+ keyIndex,
35
+ isHosted: !!(attrs['is_hosted'] && attrs['is_hosted'] === 'true')
36
+ });
37
+ }
38
+ }
39
+ }
40
+ if (keyIndexNode?.tag === 'key-index-list') {
41
+ keyIndex = {
42
+ timestamp: +keyIndexNode.attrs['ts'],
43
+ signedKeyIndex: keyIndexNode?.content,
44
+ expectedTimestamp: keyIndexNode.attrs['expected_ts'] ? +keyIndexNode.attrs['expected_ts'] : undefined
45
+ };
46
+ }
37
47
  }
38
- }
39
- if (keyIndexNode?.tag === "key-index-list") {
40
- keyIndex = {
41
- timestamp: +keyIndexNode.attrs["ts"],
42
- signedKeyIndex: keyIndexNode?.content,
43
- expectedTimestamp: keyIndexNode.attrs["expected_ts"] ? +keyIndexNode.attrs["expected_ts"] : undefined
48
+ return {
49
+ deviceList,
50
+ keyIndex
44
51
  };
45
- }
46
52
  }
47
- return {
48
- deviceList,
49
- keyIndex
50
- };
51
- }
52
53
  }
53
- //=======================================================//
@@ -0,0 +1,13 @@
1
+ export class USyncDisappearingModeProtocol {
2
+ name: string;
3
+ getQueryElement(): {
4
+ tag: string;
5
+ attrs: {};
6
+ };
7
+ getUserElement(): null;
8
+ parser(node: any): {
9
+ duration: number;
10
+ setAt: Date;
11
+ } | undefined;
12
+ }
13
+ //# sourceMappingURL=USyncDisappearingModeProtocol.d.ts.map
@@ -1,29 +1,26 @@
1
- //=======================================================//
2
- import { assertNodeErrorFree } from "../../WABinary/index.js";
3
- //=======================================================//
1
+ import { assertNodeErrorFree } from '../../WABinary/index.js';
4
2
  export class USyncDisappearingModeProtocol {
5
- constructor() {
6
- this.name = "disappearing_mode";
7
- }
8
- getQueryElement() {
9
- return {
10
- tag: "disappearing_mode",
11
- attrs: {}
12
- };
13
- }
14
- getUserElement() {
15
- return null;
16
- }
17
- parser(node) {
18
- if (node.tag === "disappearing_mode") {
19
- assertNodeErrorFree(node);
20
- const duration = +node?.attrs.duration;
21
- const setAt = new Date(+(node?.attrs.t || 0) * 1000);
22
- return {
23
- duration,
24
- setAt
25
- };
3
+ constructor() {
4
+ this.name = 'disappearing_mode';
5
+ }
6
+ getQueryElement() {
7
+ return {
8
+ tag: 'disappearing_mode',
9
+ attrs: {}
10
+ };
11
+ }
12
+ getUserElement() {
13
+ return null;
14
+ }
15
+ parser(node) {
16
+ if (node.tag === 'disappearing_mode') {
17
+ assertNodeErrorFree(node);
18
+ const duration = +node?.attrs.duration;
19
+ const setAt = new Date(+(node?.attrs.t || 0) * 1000);
20
+ return {
21
+ duration,
22
+ setAt
23
+ };
24
+ }
26
25
  }
27
- }
28
26
  }
29
- //=======================================================//
@@ -0,0 +1,13 @@
1
+ export class USyncStatusProtocol {
2
+ name: string;
3
+ getQueryElement(): {
4
+ tag: string;
5
+ attrs: {};
6
+ };
7
+ getUserElement(): null;
8
+ parser(node: any): {
9
+ status: any;
10
+ setAt: Date;
11
+ } | undefined;
12
+ }
13
+ //# sourceMappingURL=USyncStatusProtocol.d.ts.map