@mrlegendbot/baileys 1.2.3 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/README.upstream.md +1312 -0
  2. package/WAProto/WAProto.json +1 -0
  3. package/WAProto/WAProto.proto +6275 -0
  4. package/WAProto/compiler.js +550 -0
  5. package/WAProto/index.js +2 -97691
  6. package/lib/Defaults/index.js +89 -92
  7. package/lib/Signal/Group/ciphertext-message.js +9 -10
  8. package/lib/Signal/Group/group-session-builder.js +21 -22
  9. package/lib/Signal/Group/group_cipher.js +75 -69
  10. package/lib/Signal/Group/index.js +0 -1
  11. package/lib/Signal/Group/keyhelper.js +9 -10
  12. package/lib/Signal/Group/sender-chain-key.js +21 -22
  13. package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
  14. package/lib/Signal/Group/sender-key-message.js +56 -62
  15. package/lib/Signal/Group/sender-key-name.js +38 -40
  16. package/lib/Signal/Group/sender-key-record.js +33 -34
  17. package/lib/Signal/Group/sender-key-state.js +72 -76
  18. package/lib/Signal/Group/sender-message-key.js +22 -23
  19. package/lib/Signal/libsignal.js +506 -405
  20. package/lib/Signal/lid-mapping.js +276 -258
  21. package/lib/Socket/Client/index.js +0 -1
  22. package/lib/Socket/Client/types.js +6 -7
  23. package/lib/Socket/Client/websocket.js +48 -49
  24. package/lib/Socket/business.js +349 -369
  25. package/lib/Socket/chats.js +1172 -1148
  26. package/lib/Socket/communities.js +473 -413
  27. package/lib/Socket/groups.js +387 -331
  28. package/lib/Socket/index.js +6 -8
  29. package/lib/Socket/messages-recv.js +1782 -1719
  30. package/lib/Socket/messages-send.js +1554 -2450
  31. package/lib/Socket/mex.js +40 -35
  32. package/lib/Socket/newsletter.js +239 -191
  33. package/lib/Socket/socket.js +992 -923
  34. package/lib/Store/index.js +8 -0
  35. package/lib/Store/keyed-db.js +108 -0
  36. package/lib/Store/make-cache-manager-store.js +70 -0
  37. package/lib/Store/make-in-memory-store.js +417 -0
  38. package/lib/Store/make-ordered-dictionary.js +69 -0
  39. package/lib/Store/object-repository.js +27 -0
  40. package/lib/Types/Auth.js +0 -1
  41. package/lib/Types/Bussines.js +0 -1
  42. package/lib/Types/Call.js +0 -1
  43. package/lib/Types/Chat.js +1 -8
  44. package/lib/Types/Contact.js +0 -1
  45. package/lib/Types/Events.js +1 -2
  46. package/lib/Types/GroupMetadata.js +0 -1
  47. package/lib/Types/Label.js +20 -22
  48. package/lib/Types/LabelAssociation.js +2 -4
  49. package/lib/Types/Message.js +3 -5
  50. package/lib/Types/Mex.js +26 -31
  51. package/lib/Types/Product.js +0 -1
  52. package/lib/Types/Signal.js +1 -2
  53. package/lib/Types/Socket.js +2 -3
  54. package/lib/Types/State.js +4 -52
  55. package/lib/Types/USync.js +0 -1
  56. package/lib/Types/index.js +10 -11
  57. package/lib/Utils/auth-utils.js +240 -277
  58. package/lib/Utils/browser-utils.js +20 -23
  59. package/lib/Utils/business.js +200 -204
  60. package/lib/Utils/chat-utils.js +836 -810
  61. package/lib/Utils/companion-reg-client-utils.js +23 -25
  62. package/lib/Utils/crypto.js +70 -86
  63. package/lib/Utils/decode-wa-message.js +277 -284
  64. package/lib/Utils/event-buffer.js +592 -595
  65. package/lib/Utils/generics.js +312 -335
  66. package/lib/Utils/history.js +123 -115
  67. package/lib/Utils/index.js +1 -2
  68. package/lib/Utils/link-preview.js +74 -73
  69. package/lib/Utils/logger.js +3 -2
  70. package/lib/Utils/lt-hash.js +0 -6
  71. package/lib/Utils/make-mutex.js +27 -27
  72. package/lib/Utils/message-retry-manager.js +213 -254
  73. package/lib/Utils/messages-media.js +741 -715
  74. package/lib/Utils/messages.js +791 -827
  75. package/lib/Utils/noise-handler.js +215 -187
  76. package/lib/Utils/pre-key-manager.js +69 -98
  77. package/lib/Utils/process-message.js +617 -586
  78. package/lib/Utils/reporting-utils.js +553 -234
  79. package/lib/Utils/signal.js +217 -170
  80. package/lib/Utils/stanza-ack.js +27 -35
  81. package/lib/Utils/sync-action-utils.js +37 -41
  82. package/lib/Utils/tc-token-utils.js +120 -141
  83. package/lib/Utils/use-multi-file-auth-state.js +87 -105
  84. package/lib/Utils/use-sqlite-auth-state.js +162 -0
  85. package/lib/Utils/validate-connection.js +190 -188
  86. package/lib/WABinary/constants.js +28 -1294
  87. package/lib/WABinary/decode.js +245 -249
  88. package/lib/WABinary/encode.js +201 -206
  89. package/lib/WABinary/generic-utils.js +86 -89
  90. package/lib/WABinary/index.js +0 -1
  91. package/lib/WABinary/jid-utils.js +68 -77
  92. package/lib/WABinary/types.js +0 -1
  93. package/lib/WAM/BinaryInfo.js +7 -8
  94. package/lib/WAM/constants.js +19892 -22853
  95. package/lib/WAM/encode.js +121 -132
  96. package/lib/WAM/index.js +0 -1
  97. package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
  98. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
  99. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
  101. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
  102. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
  103. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  104. package/lib/WAUSync/Protocols/index.js +0 -1
  105. package/lib/WAUSync/USyncQuery.js +79 -88
  106. package/lib/WAUSync/USyncUser.js +28 -29
  107. package/lib/WAUSync/index.js +0 -1
  108. package/lib/index.js +2 -2
  109. package/package.json +22 -86
  110. package/WAProto/GenerateStatics.sh +0 -3
  111. package/WAProto/fix-imports.js +0 -85
  112. package/WAProto/index.d.ts +0 -14017
  113. package/lib/Defaults/index.d.ts +0 -75
  114. package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
  115. package/lib/Signal/Group/group-session-builder.d.ts +0 -15
  116. package/lib/Signal/Group/group_cipher.d.ts +0 -17
  117. package/lib/Signal/Group/index.d.ts +0 -12
  118. package/lib/Signal/Group/keyhelper.d.ts +0 -11
  119. package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
  120. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
  121. package/lib/Signal/Group/sender-key-message.d.ts +0 -19
  122. package/lib/Signal/Group/sender-key-name.d.ts +0 -18
  123. package/lib/Signal/Group/sender-key-record.d.ts +0 -31
  124. package/lib/Signal/Group/sender-key-state.d.ts +0 -39
  125. package/lib/Signal/Group/sender-message-key.d.ts +0 -12
  126. package/lib/Signal/libsignal.d.ts +0 -5
  127. package/lib/Signal/lid-mapping.d.ts +0 -23
  128. package/lib/Socket/Client/index.d.ts +0 -3
  129. package/lib/Socket/Client/types.d.ts +0 -16
  130. package/lib/Socket/Client/websocket.d.ts +0 -13
  131. package/lib/Socket/business.d.ts +0 -217
  132. package/lib/Socket/chats.d.ts +0 -125
  133. package/lib/Socket/communities.d.ts +0 -273
  134. package/lib/Socket/groups.d.ts +0 -161
  135. package/lib/Socket/index.d.ts +0 -260
  136. package/lib/Socket/messages-recv.d.ts +0 -203
  137. package/lib/Socket/messages-send.d.ts +0 -199
  138. package/lib/Socket/mex.d.ts +0 -3
  139. package/lib/Socket/newsletter.d.ts +0 -170
  140. package/lib/Socket/socket.d.ts +0 -59
  141. package/lib/Types/Auth.d.ts +0 -117
  142. package/lib/Types/Bussines.d.ts +0 -25
  143. package/lib/Types/Call.d.ts +0 -15
  144. package/lib/Types/Chat.d.ts +0 -124
  145. package/lib/Types/Contact.d.ts +0 -26
  146. package/lib/Types/Events.d.ts +0 -256
  147. package/lib/Types/GroupMetadata.d.ts +0 -71
  148. package/lib/Types/Label.d.ts +0 -47
  149. package/lib/Types/LabelAssociation.d.ts +0 -30
  150. package/lib/Types/Message.d.ts +0 -320
  151. package/lib/Types/Mex.d.ts +0 -141
  152. package/lib/Types/Product.d.ts +0 -79
  153. package/lib/Types/Signal.d.ts +0 -87
  154. package/lib/Types/Socket.d.ts +0 -136
  155. package/lib/Types/State.d.ts +0 -97
  156. package/lib/Types/USync.d.ts +0 -26
  157. package/lib/Types/index.d.ts +0 -66
  158. package/lib/Utils/auth-utils.d.ts +0 -24
  159. package/lib/Utils/browser-utils.d.ts +0 -4
  160. package/lib/Utils/business.d.ts +0 -23
  161. package/lib/Utils/chat-utils.d.ts +0 -100
  162. package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
  163. package/lib/Utils/crypto.d.ts +0 -37
  164. package/lib/Utils/decode-wa-message.d.ts +0 -66
  165. package/lib/Utils/event-buffer.d.ts +0 -36
  166. package/lib/Utils/generics.d.ts +0 -92
  167. package/lib/Utils/history.d.ts +0 -24
  168. package/lib/Utils/identity-change-handler.d.ts +0 -44
  169. package/lib/Utils/identity-change-handler.js +0 -50
  170. package/lib/Utils/index.d.ts +0 -22
  171. package/lib/Utils/link-preview.d.ts +0 -21
  172. package/lib/Utils/logger.d.ts +0 -12
  173. package/lib/Utils/lt-hash.d.ts +0 -8
  174. package/lib/Utils/make-mutex.d.ts +0 -9
  175. package/lib/Utils/message-retry-manager.d.ts +0 -115
  176. package/lib/Utils/messages-media.d.ts +0 -133
  177. package/lib/Utils/messages.d.ts +0 -91
  178. package/lib/Utils/noise-handler.d.ts +0 -20
  179. package/lib/Utils/offline-node-processor.d.ts +0 -17
  180. package/lib/Utils/offline-node-processor.js +0 -40
  181. package/lib/Utils/pre-key-manager.d.ts +0 -28
  182. package/lib/Utils/process-message.d.ts +0 -60
  183. package/lib/Utils/reporting-utils.d.ts +0 -11
  184. package/lib/Utils/signal.d.ts +0 -47
  185. package/lib/Utils/stanza-ack.d.ts +0 -11
  186. package/lib/Utils/sync-action-utils.d.ts +0 -19
  187. package/lib/Utils/tc-token-utils.d.ts +0 -37
  188. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  189. package/lib/Utils/validate-connection.d.ts +0 -11
  190. package/lib/WABinary/constants.d.ts +0 -28
  191. package/lib/WABinary/decode.d.ts +0 -7
  192. package/lib/WABinary/encode.d.ts +0 -3
  193. package/lib/WABinary/generic-utils.d.ts +0 -15
  194. package/lib/WABinary/index.d.ts +0 -6
  195. package/lib/WABinary/jid-utils.d.ts +0 -48
  196. package/lib/WABinary/types.d.ts +0 -19
  197. package/lib/WAM/BinaryInfo.d.ts +0 -9
  198. package/lib/WAM/constants.d.ts +0 -40
  199. package/lib/WAM/encode.d.ts +0 -3
  200. package/lib/WAM/index.d.ts +0 -4
  201. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
  202. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
  203. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
  204. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
  205. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
  206. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
  208. package/lib/WAUSync/Protocols/index.d.ts +0 -6
  209. package/lib/WAUSync/USyncQuery.d.ts +0 -30
  210. package/lib/WAUSync/USyncUser.d.ts +0 -17
  211. package/lib/WAUSync/index.d.ts +0 -4
  212. package/lib/index.d.ts +0 -12
package/lib/WAM/encode.js CHANGED
@@ -1,150 +1,139 @@
1
1
  import { BinaryInfo } from './BinaryInfo.js';
2
2
  import { FLAG_BYTE, FLAG_EVENT, FLAG_EXTENDED, FLAG_FIELD, FLAG_GLOBAL, WEB_EVENTS, WEB_GLOBALS } from './constants.js';
3
- const getHeaderBitLength = (key) => (key < 256 ? 2 : 3);
4
- export const encodeWAM = (binaryInfo) => {
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;
3
+ const getHeaderBitLength = key => key < 256 ? 2 : 3;
4
+ export const encodeWAM = binaryInfo => {
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;
16
16
  };
17
17
  function encodeWAMHeader(binaryInfo) {
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);
18
+ const headerBuffer = Buffer.alloc(8);
19
+ headerBuffer.write('WAM', 0, 'utf8');
20
+ headerBuffer.writeUInt8(binaryInfo.protocolVersion, 3);
21
+ headerBuffer.writeUInt8(1, 4);
22
+ headerBuffer.writeUInt16BE(binaryInfo.sequence, 5);
23
+ headerBuffer.writeUInt8(0, 7);
24
+ binaryInfo.buffer.push(headerBuffer);
25
25
  }
26
26
  function encodeGlobalAttributes(binaryInfo, globals) {
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));
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;
34
32
  }
33
+ binaryInfo.buffer.push(serializeData(id, value, FLAG_GLOBAL));
34
+ }
35
35
  }
36
36
  function encodeEvents(binaryInfo) {
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
- }
37
+ for (const [name, {
38
+ props,
39
+ globals
40
+ }] of binaryInfo.events.map(a => Object.entries(a)[0])) {
41
+ encodeGlobalAttributes(binaryInfo, globals);
42
+ const event = WEB_EVENTS.find(a => a.name === name);
43
+ const props_ = Object.entries(props);
44
+ let extended = false;
45
+ for (const [, value] of props_) {
46
+ extended || (extended = value !== null);
58
47
  }
48
+ const eventFlag = extended ? FLAG_EVENT : FLAG_EVENT | FLAG_EXTENDED;
49
+ binaryInfo.buffer.push(serializeData(event.id, -event.weight, eventFlag));
50
+ for (let i = 0; i < props_.length; i++) {
51
+ const [key, _value] = props_[i];
52
+ const id = event.props[key]?.[0];
53
+ extended = i < props_.length - 1;
54
+ let value = _value;
55
+ if (typeof value === 'boolean') {
56
+ value = value ? 1 : 0;
57
+ }
58
+ const fieldFlag = extended ? FLAG_EVENT : FLAG_FIELD | FLAG_EXTENDED;
59
+ binaryInfo.buffer.push(serializeData(id, value, fieldFlag));
60
+ }
61
+ }
59
62
  }
60
63
  function serializeData(key, value, flag) {
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
- }
70
- }
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
- }
64
+ const bufferLength = getHeaderBitLength(key);
65
+ let buffer;
66
+ let offset = 0;
67
+ if (value === null) {
68
+ if (flag === FLAG_GLOBAL) {
69
+ buffer = Buffer.alloc(bufferLength);
70
+ offset = serializeHeader(buffer, offset, key, flag);
71
+ return buffer;
102
72
  }
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;
73
+ } else if (typeof value === 'number' && Number.isInteger(value)) {
74
+ if (value === 0 || value === 1) {
75
+ buffer = Buffer.alloc(bufferLength);
76
+ offset = serializeHeader(buffer, offset, key, flag | value + 1 << 4);
77
+ return buffer;
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
+ } else if (-32768 <= value && value < 32768) {
84
+ buffer = Buffer.alloc(bufferLength + 2);
85
+ offset = serializeHeader(buffer, offset, key, flag | 4 << 4);
86
+ buffer.writeInt16LE(value, offset);
87
+ return buffer;
88
+ } else if (-2147483648 <= value && value < 2147483648) {
89
+ buffer = Buffer.alloc(bufferLength + 4);
90
+ offset = serializeHeader(buffer, offset, key, flag | 5 << 4);
91
+ buffer.writeInt32LE(value, offset);
92
+ return buffer;
93
+ } else {
94
+ buffer = Buffer.alloc(bufferLength + 8);
95
+ offset = serializeHeader(buffer, offset, key, flag | 7 << 4);
96
+ buffer.writeDoubleLE(value, offset);
97
+ return buffer;
109
98
  }
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;
99
+ } else if (typeof value === 'number') {
100
+ buffer = Buffer.alloc(bufferLength + 8);
101
+ offset = serializeHeader(buffer, offset, key, flag | 7 << 4);
102
+ buffer.writeDoubleLE(value, offset);
103
+ return buffer;
104
+ } else if (typeof value === 'string') {
105
+ const utf8Bytes = Buffer.byteLength(value, 'utf8');
106
+ if (utf8Bytes < 256) {
107
+ buffer = Buffer.alloc(bufferLength + 1 + utf8Bytes);
108
+ offset = serializeHeader(buffer, offset, key, flag | 8 << 4);
109
+ buffer.writeUint8(utf8Bytes, offset++);
110
+ } else if (utf8Bytes < 65536) {
111
+ buffer = Buffer.alloc(bufferLength + 2 + utf8Bytes);
112
+ offset = serializeHeader(buffer, offset, key, flag | 9 << 4);
113
+ buffer.writeUInt16LE(utf8Bytes, offset);
114
+ offset += 2;
115
+ } else {
116
+ buffer = Buffer.alloc(bufferLength + 4 + utf8Bytes);
117
+ offset = serializeHeader(buffer, offset, key, flag | 10 << 4);
118
+ buffer.writeUInt32LE(utf8Bytes, offset);
119
+ offset += 4;
132
120
  }
133
- throw 'missing';
121
+ buffer.write(value, offset, 'utf8');
122
+ return buffer;
123
+ }
124
+ throw 'missing';
134
125
  }
135
126
  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;
141
- }
142
- else {
143
- buffer.writeUInt8(flag | FLAG_BYTE, offset);
144
- offset += 1;
145
- buffer.writeUInt16LE(key, offset);
146
- offset += 2;
147
- }
148
- return offset;
127
+ if (key < 256) {
128
+ buffer.writeUInt8(flag, offset);
129
+ offset += 1;
130
+ buffer.writeUInt8(key, offset);
131
+ offset += 1;
132
+ } else {
133
+ buffer.writeUInt8(flag | FLAG_BYTE, offset);
134
+ offset += 1;
135
+ buffer.writeUInt16LE(key, offset);
136
+ offset += 2;
137
+ }
138
+ return offset;
149
139
  }
150
- //# sourceMappingURL=encode.js.map
package/lib/WAM/index.js CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from './constants.js';
2
2
  export * from './encode.js';
3
3
  export * from './BinaryInfo.js';
4
- //# sourceMappingURL=index.js.map
@@ -1,52 +1,55 @@
1
1
  import { assertNodeErrorFree } from '../../WABinary/index.js';
2
2
  import { USyncUser } from '../USyncUser.js';
3
3
  export class USyncContactProtocol {
4
- constructor() {
5
- this.name = 'contact';
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
+ };
6
20
  }
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
- };
21
+ if (user.username) {
22
+ return {
23
+ tag: 'contact',
24
+ attrs: {
25
+ username: user.username,
26
+ ...(user.usernameKey ? {
27
+ pin: user.usernameKey
28
+ } : {}),
29
+ ...(user.lid ? {
30
+ lid: user.lid
31
+ } : {})
30
32
  }
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
- };
33
+ };
43
34
  }
44
- parser(node) {
45
- if (node.tag === 'contact') {
46
- assertNodeErrorFree(node);
47
- return node?.attrs?.type === 'in';
35
+ if (user.type) {
36
+ return {
37
+ tag: 'contact',
38
+ attrs: {
39
+ type: user.type
48
40
  }
49
- return false;
41
+ };
42
+ }
43
+ return {
44
+ tag: 'contact',
45
+ attrs: {}
46
+ };
47
+ }
48
+ parser(node) {
49
+ if (node.tag === 'contact') {
50
+ assertNodeErrorFree(node);
51
+ return node?.attrs?.type === 'in';
50
52
  }
53
+ return false;
54
+ }
51
55
  }
52
- //# sourceMappingURL=USyncContactProtocol.js.map
@@ -1,54 +1,53 @@
1
1
  import { assertNodeErrorFree, getBinaryNodeChild } from '../../WABinary/index.js';
2
2
  export class USyncDeviceProtocol {
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
- }
3
+ constructor() {
4
+ this.name = 'devices';
5
+ }
6
+ getQueryElement() {
7
+ return {
8
+ tag: 'devices',
9
+ attrs: {
10
+ version: '2'
11
+ }
12
+ };
13
+ }
14
+ getUserElement() {
15
+ return null;
16
+ }
17
+ parser(node) {
18
+ const deviceList = [];
19
+ let keyIndex = undefined;
20
+ if (node.tag === 'devices') {
21
+ assertNodeErrorFree(node);
22
+ const deviceListNode = getBinaryNodeChild(node, 'device-list');
23
+ const keyIndexNode = getBinaryNodeChild(node, 'key-index-list');
24
+ if (Array.isArray(deviceListNode?.content)) {
25
+ for (const {
26
+ tag,
27
+ attrs
28
+ } 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
+ }
47
38
  }
48
- return {
49
- deviceList,
50
- keyIndex
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
51
45
  };
46
+ }
52
47
  }
48
+ return {
49
+ deviceList,
50
+ keyIndex
51
+ };
52
+ }
53
53
  }
54
- //# sourceMappingURL=USyncDeviceProtocol.js.map
@@ -1,27 +1,26 @@
1
1
  import { assertNodeErrorFree } from '../../WABinary/index.js';
2
2
  export class USyncDisappearingModeProtocol {
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
- }
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
+ };
25
24
  }
25
+ }
26
26
  }
27
- //# sourceMappingURL=USyncDisappearingModeProtocol.js.map
@@ -1,38 +1,35 @@
1
1
  import { assertNodeErrorFree } from '../../WABinary/index.js';
2
2
  export class USyncStatusProtocol {
3
- constructor() {
4
- this.name = 'status';
5
- }
6
- getQueryElement() {
7
- return {
8
- tag: 'status',
9
- attrs: {}
10
- };
11
- }
12
- getUserElement() {
13
- return null;
14
- }
15
- parser(node) {
16
- if (node.tag === 'status') {
17
- assertNodeErrorFree(node);
18
- let status = node?.content?.toString() ?? null;
19
- const setAt = new Date(+(node?.attrs.t || 0) * 1000);
20
- if (!status) {
21
- if (node.attrs?.code && +node.attrs.code === 401) {
22
- status = '';
23
- }
24
- else {
25
- status = null;
26
- }
27
- }
28
- else if (typeof status === 'string' && status.length === 0) {
29
- status = null;
30
- }
31
- return {
32
- status,
33
- setAt
34
- };
3
+ constructor() {
4
+ this.name = 'status';
5
+ }
6
+ getQueryElement() {
7
+ return {
8
+ tag: 'status',
9
+ attrs: {}
10
+ };
11
+ }
12
+ getUserElement() {
13
+ return null;
14
+ }
15
+ parser(node) {
16
+ if (node.tag === 'status') {
17
+ assertNodeErrorFree(node);
18
+ let status = node?.content?.toString() ?? null;
19
+ const setAt = new Date(+(node?.attrs.t || 0) * 1000);
20
+ if (!status) {
21
+ if (node.attrs?.code && +node.attrs.code === 401) {
22
+ status = '';
23
+ } else {
24
+ status = null;
35
25
  }
26
+ } else if (typeof status === 'string' && status.length === 0) {
27
+ status = null;
28
+ }
29
+ return {
30
+ status,
31
+ setAt
32
+ };
36
33
  }
34
+ }
37
35
  }
38
- //# sourceMappingURL=USyncStatusProtocol.js.map
@@ -1,25 +1,24 @@
1
1
  import { assertNodeErrorFree } from '../../WABinary/index.js';
2
2
  import { USyncUser } from '../USyncUser.js';
3
3
  export class USyncUsernameProtocol {
4
- constructor() {
5
- this.name = 'username';
6
- }
7
- getQueryElement() {
8
- return {
9
- tag: 'username',
10
- attrs: {}
11
- };
12
- }
13
- getUserElement(user) {
14
- void user;
15
- return null;
16
- }
17
- parser(node) {
18
- if (node.tag === 'username') {
19
- assertNodeErrorFree(node);
20
- return typeof node.content === 'string' ? node.content : null;
21
- }
22
- return null;
4
+ constructor() {
5
+ this.name = 'username';
6
+ }
7
+ getQueryElement() {
8
+ return {
9
+ tag: 'username',
10
+ attrs: {}
11
+ };
12
+ }
13
+ getUserElement(user) {
14
+ void user;
15
+ return null;
16
+ }
17
+ parser(node) {
18
+ if (node.tag === 'username') {
19
+ assertNodeErrorFree(node);
20
+ return typeof node.content === 'string' ? node.content : null;
23
21
  }
22
+ return null;
23
+ }
24
24
  }
25
- //# sourceMappingURL=USyncUsernameProtocol.js.map