@baileys-md/baileys 10.1.0 → 11.0.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 (122) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1303 -2
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +4633 -0
  5. package/WAProto/fix-imports.js +29 -0
  6. package/WAProto/index.js +13516 -4182
  7. package/lib/Defaults/baileys-version.js +1 -0
  8. package/lib/Defaults/index.js +51 -72
  9. package/lib/Signal/Group/ciphertext-message.js +12 -0
  10. package/lib/Signal/Group/group-session-builder.js +30 -0
  11. package/lib/Signal/Group/group_cipher.js +94 -0
  12. package/lib/Signal/Group/index.js +12 -0
  13. package/lib/Signal/Group/keyhelper.js +19 -0
  14. package/lib/Signal/Group/queue-job.js +54 -0
  15. package/lib/Signal/Group/sender-chain-key.js +32 -0
  16. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  17. package/lib/Signal/Group/sender-key-message.js +67 -0
  18. package/lib/Signal/Group/sender-key-name.js +48 -0
  19. package/lib/Signal/Group/sender-key-record.js +50 -0
  20. package/lib/Signal/Group/sender-key-state.js +96 -0
  21. package/{WASignalGroup/sender_message_key.js → lib/Signal/Group/sender-message-key.js} +4 -16
  22. package/lib/Signal/libsignal.js +41 -61
  23. package/lib/Socket/Client/index.js +3 -19
  24. package/lib/Socket/Client/types.js +11 -0
  25. package/lib/Socket/Client/websocket.js +50 -0
  26. package/lib/Socket/business.js +37 -42
  27. package/lib/Socket/chats.js +194 -187
  28. package/lib/Socket/communities.js +351 -0
  29. package/lib/Socket/groups.js +87 -90
  30. package/lib/Socket/index.js +7 -8
  31. package/lib/Socket/messages-recv.js +360 -335
  32. package/lib/Socket/messages-send.js +156 -279
  33. package/lib/Socket/mex.js +42 -0
  34. package/lib/Socket/newsletter.js +144 -213
  35. package/lib/Socket/socket.js +128 -161
  36. package/lib/Socket/usync.js +19 -26
  37. package/lib/Types/Auth.js +2 -2
  38. package/lib/Types/Call.js +2 -2
  39. package/lib/Types/Chat.js +8 -4
  40. package/lib/Types/Contact.js +2 -2
  41. package/lib/Types/Events.js +2 -2
  42. package/lib/Types/GroupMetadata.js +2 -2
  43. package/lib/Types/Label.js +3 -5
  44. package/lib/Types/LabelAssociation.js +3 -5
  45. package/lib/Types/Message.js +7 -7
  46. package/lib/Types/Newsletter.js +30 -17
  47. package/lib/Types/Product.js +2 -2
  48. package/lib/Types/Signal.js +2 -2
  49. package/lib/Types/Socket.js +3 -2
  50. package/lib/Types/State.js +2 -2
  51. package/lib/Types/USync.js +2 -2
  52. package/lib/Types/index.js +15 -31
  53. package/lib/Utils/auth-utils.js +31 -47
  54. package/lib/Utils/baileys-event-stream.js +15 -22
  55. package/lib/Utils/business.js +66 -69
  56. package/lib/Utils/chat-utils.js +200 -195
  57. package/lib/Utils/crypto.js +70 -85
  58. package/lib/Utils/decode-wa-message.js +47 -51
  59. package/lib/Utils/event-buffer.js +36 -46
  60. package/lib/Utils/generics.js +116 -188
  61. package/lib/Utils/history.js +37 -46
  62. package/lib/Utils/index.js +19 -33
  63. package/lib/Utils/link-preview.js +14 -55
  64. package/lib/Utils/logger.js +3 -7
  65. package/lib/Utils/lt-hash.js +23 -26
  66. package/lib/{Store → Utils}/make-in-memory-store.js +19 -27
  67. package/lib/Utils/make-mutex.js +7 -10
  68. package/lib/{Store → Utils}/make-ordered-dictionary.js +1 -3
  69. package/lib/Utils/messages-media.js +236 -368
  70. package/lib/Utils/messages.js +278 -510
  71. package/lib/Utils/noise-handler.js +22 -31
  72. package/lib/{Store → Utils}/object-repository.js +1 -4
  73. package/lib/Utils/process-message.js +144 -148
  74. package/lib/Utils/signal.js +71 -64
  75. package/lib/Utils/use-multi-file-auth-state.js +112 -84
  76. package/lib/Utils/validate-connection.js +72 -115
  77. package/lib/WABinary/constants.js +1281 -20
  78. package/lib/WABinary/decode.js +15 -52
  79. package/lib/WABinary/encode.js +14 -48
  80. package/lib/WABinary/generic-utils.js +31 -39
  81. package/lib/WABinary/index.js +6 -21
  82. package/lib/WABinary/jid-utils.js +23 -40
  83. package/lib/WABinary/types.js +2 -2
  84. package/lib/WAM/BinaryInfo.js +2 -5
  85. package/lib/WAM/constants.js +2257 -2366
  86. package/lib/WAM/encode.js +17 -21
  87. package/lib/WAM/index.js +4 -19
  88. package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -11
  89. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
  90. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
  91. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
  92. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
  93. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +3 -6
  94. package/lib/WAUSync/Protocols/index.js +5 -20
  95. package/lib/WAUSync/USyncQuery.js +34 -32
  96. package/lib/WAUSync/USyncUser.js +2 -5
  97. package/lib/WAUSync/index.js +4 -19
  98. package/lib/index.js +11 -33
  99. package/package.json +25 -54
  100. package/WASignalGroup/GroupProtocol.js +0 -1697
  101. package/WASignalGroup/ciphertext_message.js +0 -16
  102. package/WASignalGroup/group_cipher.js +0 -120
  103. package/WASignalGroup/group_session_builder.js +0 -46
  104. package/WASignalGroup/index.js +0 -5
  105. package/WASignalGroup/keyhelper.js +0 -21
  106. package/WASignalGroup/protobufs.js +0 -3
  107. package/WASignalGroup/queue_job.js +0 -69
  108. package/WASignalGroup/sender_chain_key.js +0 -50
  109. package/WASignalGroup/sender_key_distribution_message.js +0 -78
  110. package/WASignalGroup/sender_key_message.js +0 -92
  111. package/WASignalGroup/sender_key_name.js +0 -70
  112. package/WASignalGroup/sender_key_record.js +0 -56
  113. package/WASignalGroup/sender_key_state.js +0 -129
  114. package/lib/Defaults/baileys-version.json +0 -3
  115. package/lib/Defaults/phonenumber-mcc.json +0 -223
  116. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  117. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  118. package/lib/Socket/Client/web-socket-client.js +0 -62
  119. package/lib/Socket/registration.js +0 -166
  120. package/lib/Store/index.js +0 -8
  121. package/lib/Store/make-cache-manager-store.js +0 -83
  122. package/lib/Store/make-mongo-store.js +0 -567
@@ -0,0 +1,96 @@
1
+ import { SenderChainKey } from './sender-chain-key.js';
2
+ import { SenderMessageKey } from './sender-message-key.js';
3
+ export class SenderKeyState {
4
+ constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
5
+ this.MAX_MESSAGE_KEYS = 2000;
6
+ if (senderKeyStateStructure) {
7
+ this.senderKeyStateStructure = senderKeyStateStructure;
8
+ }
9
+ else {
10
+ if (signatureKeyPair) {
11
+ signatureKeyPublic = signatureKeyPair.public;
12
+ signatureKeyPrivate = signatureKeyPair.private;
13
+ }
14
+ chainKey = typeof chainKey === 'string' ? Buffer.from(chainKey, 'base64') : chainKey;
15
+ const senderChainKeyStructure = {
16
+ iteration: iteration || 0,
17
+ seed: chainKey || Buffer.alloc(0)
18
+ };
19
+ const signingKeyStructure = {
20
+ public: typeof signatureKeyPublic === 'string'
21
+ ? Buffer.from(signatureKeyPublic, 'base64')
22
+ : signatureKeyPublic || Buffer.alloc(0)
23
+ };
24
+ if (signatureKeyPrivate) {
25
+ signingKeyStructure.private =
26
+ typeof signatureKeyPrivate === 'string' ? Buffer.from(signatureKeyPrivate, 'base64') : signatureKeyPrivate;
27
+ }
28
+ this.senderKeyStateStructure = {
29
+ senderKeyId: id || 0,
30
+ senderChainKey: senderChainKeyStructure,
31
+ senderSigningKey: signingKeyStructure,
32
+ senderMessageKeys: []
33
+ };
34
+ }
35
+ }
36
+ getKeyId() {
37
+ return this.senderKeyStateStructure.senderKeyId;
38
+ }
39
+ getSenderChainKey() {
40
+ return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
41
+ }
42
+ setSenderChainKey(chainKey) {
43
+ this.senderKeyStateStructure.senderChainKey = {
44
+ iteration: chainKey.getIteration(),
45
+ seed: chainKey.getSeed()
46
+ };
47
+ }
48
+ getSigningKeyPublic() {
49
+ const publicKey = this.senderKeyStateStructure.senderSigningKey.public;
50
+ if (publicKey instanceof Buffer) {
51
+ return publicKey;
52
+ }
53
+ else if (typeof publicKey === 'string') {
54
+ return Buffer.from(publicKey, 'base64');
55
+ }
56
+ return Buffer.from(publicKey || []);
57
+ }
58
+ getSigningKeyPrivate() {
59
+ const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
60
+ if (!privateKey) {
61
+ return undefined;
62
+ }
63
+ if (privateKey instanceof Buffer) {
64
+ return privateKey;
65
+ }
66
+ else if (typeof privateKey === 'string') {
67
+ return Buffer.from(privateKey, 'base64');
68
+ }
69
+ return Buffer.from(privateKey || []);
70
+ }
71
+ hasSenderMessageKey(iteration) {
72
+ return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
73
+ }
74
+ addSenderMessageKey(senderMessageKey) {
75
+ this.senderKeyStateStructure.senderMessageKeys.push({
76
+ iteration: senderMessageKey.getIteration(),
77
+ seed: senderMessageKey.getSeed()
78
+ });
79
+ if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
80
+ this.senderKeyStateStructure.senderMessageKeys.shift();
81
+ }
82
+ }
83
+ removeSenderMessageKey(iteration) {
84
+ const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration);
85
+ if (index !== -1) {
86
+ const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
87
+ this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
88
+ return new SenderMessageKey(messageKey.iteration, messageKey.seed);
89
+ }
90
+ return null;
91
+ }
92
+ getStructure() {
93
+ return this.senderKeyStateStructure;
94
+ }
95
+ }
96
+ //# sourceMappingURL=sender-key-state.js.map
@@ -1,13 +1,6 @@
1
- const { deriveSecrets } = require('libsignal/src/crypto');
2
- class SenderMessageKey {
3
- iteration = 0;
4
-
5
- iv = Buffer.alloc(0);
6
-
7
- cipherKey = Buffer.alloc(0);
8
-
9
- seed = Buffer.alloc(0);
10
-
1
+ /* @ts-ignore */
2
+ import { deriveSecrets } from 'libsignal/src/crypto.js';
3
+ export class SenderMessageKey {
11
4
  constructor(iteration, seed) {
12
5
  const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from('WhisperGroup'));
13
6
  const keys = new Uint8Array(32);
@@ -15,25 +8,20 @@ class SenderMessageKey {
15
8
  keys.set(new Uint8Array(derivative[1].slice(0, 16)), 16);
16
9
  this.iv = Buffer.from(derivative[0].slice(0, 16));
17
10
  this.cipherKey = Buffer.from(keys.buffer);
18
-
19
11
  this.iteration = iteration;
20
12
  this.seed = seed;
21
13
  }
22
-
23
14
  getIteration() {
24
15
  return this.iteration;
25
16
  }
26
-
27
17
  getIv() {
28
18
  return this.iv;
29
19
  }
30
-
31
20
  getCipherKey() {
32
21
  return this.cipherKey;
33
22
  }
34
-
35
23
  getSeed() {
36
24
  return this.seed;
37
25
  }
38
26
  }
39
- module.exports = SenderMessageKey;
27
+ //# sourceMappingURL=sender-message-key.js.map
@@ -1,58 +1,29 @@
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.makeLibSignalRepository = makeLibSignalRepository;
37
- const libsignal = __importStar(require("libsignal"));
38
- const WASignalGroup_1 = require("../../WASignalGroup");
39
- const Utils_1 = require("../Utils");
40
- const WABinary_1 = require("../WABinary");
41
- function makeLibSignalRepository(auth) {
1
+ /* @ts-ignore */
2
+ import * as libsignal from 'libsignal';
3
+ import { generateSignalPubKey } from '../Utils/index.js';
4
+ import { jidDecode } from '../WABinary/index.js';
5
+ import { SenderKeyName } from './Group/sender-key-name.js';
6
+ import { SenderKeyRecord } from './Group/sender-key-record.js';
7
+ import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage } from './Group/index.js';
8
+ export function makeLibSignalRepository(auth) {
42
9
  const storage = signalStorage(auth);
43
10
  return {
44
11
  decryptGroupMessage({ group, authorJid, msg }) {
45
12
  const senderName = jidToSignalSenderKeyName(group, authorJid);
46
- const cipher = new WASignalGroup_1.GroupCipher(storage, senderName);
13
+ const cipher = new GroupCipher(storage, senderName);
47
14
  return cipher.decrypt(msg);
48
15
  },
49
16
  async processSenderKeyDistributionMessage({ item, authorJid }) {
50
- const builder = new WASignalGroup_1.GroupSessionBuilder(storage);
17
+ const builder = new GroupSessionBuilder(storage);
18
+ if (!item.groupId) {
19
+ throw new Error('Group ID is required for sender key distribution message');
20
+ }
51
21
  const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
52
- const senderMsg = new WASignalGroup_1.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
53
- const { [senderName]: senderKey } = await auth.keys.get('sender-key', [senderName]);
22
+ const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
23
+ const senderNameStr = senderName.toString();
24
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
54
25
  if (!senderKey) {
55
- await storage.storeSenderKey(senderName, new WASignalGroup_1.SenderKeyRecord());
26
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
56
27
  }
57
28
  await builder.process(senderName, senderMsg);
58
29
  },
@@ -67,6 +38,8 @@ function makeLibSignalRepository(auth) {
67
38
  case 'msg':
68
39
  result = await session.decryptWhisperMessage(ciphertext);
69
40
  break;
41
+ default:
42
+ throw new Error(`Unknown message type: ${type}`);
70
43
  }
71
44
  return result;
72
45
  },
@@ -79,17 +52,18 @@ function makeLibSignalRepository(auth) {
79
52
  },
80
53
  async encryptGroupMessage({ group, meId, data }) {
81
54
  const senderName = jidToSignalSenderKeyName(group, meId);
82
- const builder = new WASignalGroup_1.GroupSessionBuilder(storage);
83
- const { [senderName]: senderKey } = await auth.keys.get('sender-key', [senderName]);
55
+ const builder = new GroupSessionBuilder(storage);
56
+ const senderNameStr = senderName.toString();
57
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
84
58
  if (!senderKey) {
85
- await storage.storeSenderKey(senderName, new WASignalGroup_1.SenderKeyRecord());
59
+ await storage.storeSenderKey(senderName, new SenderKeyRecord());
86
60
  }
87
61
  const senderKeyDistributionMessage = await builder.create(senderName);
88
- const session = new WASignalGroup_1.GroupCipher(storage, senderName);
62
+ const session = new GroupCipher(storage, senderName);
89
63
  const ciphertext = await session.encrypt(data);
90
64
  return {
91
65
  ciphertext,
92
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize(),
66
+ senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
93
67
  };
94
68
  },
95
69
  async injectE2ESession({ jid, session }) {
@@ -98,15 +72,15 @@ function makeLibSignalRepository(auth) {
98
72
  },
99
73
  jidToSignalProtocolAddress(jid) {
100
74
  return jidToSignalProtocolAddress(jid).toString();
101
- },
75
+ }
102
76
  };
103
77
  }
104
78
  const jidToSignalProtocolAddress = (jid) => {
105
- const { user, device } = (0, WABinary_1.jidDecode)(jid);
79
+ const { user, device } = jidDecode(jid);
106
80
  return new libsignal.ProtocolAddress(user, device || 0);
107
81
  };
108
82
  const jidToSignalSenderKeyName = (group, user) => {
109
- return new WASignalGroup_1.SenderKeyName(group, jidToSignalProtocolAddress(user)).toString();
83
+ return new SenderKeyName(group, jidToSignalProtocolAddress(user));
110
84
  };
111
85
  function signalStorage({ creds, keys }) {
112
86
  return {
@@ -116,8 +90,9 @@ function signalStorage({ creds, keys }) {
116
90
  return libsignal.SessionRecord.deserialize(sess);
117
91
  }
118
92
  },
93
+ // TODO: Replace with libsignal.SessionRecord when type exports are added to libsignal
119
94
  storeSession: async (id, session) => {
120
- await keys.set({ 'session': { [id]: session.serialize() } });
95
+ await keys.set({ session: { [id]: session.serialize() } });
121
96
  },
122
97
  isTrustedIdentity: () => {
123
98
  return true;
@@ -140,22 +115,27 @@ function signalStorage({ creds, keys }) {
140
115
  pubKey: Buffer.from(key.keyPair.public)
141
116
  };
142
117
  },
143
- loadSenderKey: async (keyId) => {
118
+ loadSenderKey: async (senderKeyName) => {
119
+ const keyId = senderKeyName.toString();
144
120
  const { [keyId]: key } = await keys.get('sender-key', [keyId]);
145
121
  if (key) {
146
- return new WASignalGroup_1.SenderKeyRecord(key);
122
+ return SenderKeyRecord.deserialize(key);
147
123
  }
124
+ return new SenderKeyRecord();
148
125
  },
149
- storeSenderKey: async (keyId, key) => {
150
- await keys.set({ 'sender-key': { [keyId]: key.serialize() } });
126
+ storeSenderKey: async (senderKeyName, key) => {
127
+ const keyId = senderKeyName.toString();
128
+ const serialized = JSON.stringify(key.serialize());
129
+ await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
151
130
  },
152
- getOurRegistrationId: () => (creds.registrationId),
131
+ getOurRegistrationId: () => creds.registrationId,
153
132
  getOurIdentity: () => {
154
133
  const { signedIdentityKey } = creds;
155
134
  return {
156
135
  privKey: Buffer.from(signedIdentityKey.private),
157
- pubKey: (0, Utils_1.generateSignalPubKey)(signedIdentityKey.public),
136
+ pubKey: generateSignalPubKey(signedIdentityKey.public)
158
137
  };
159
138
  }
160
139
  };
161
140
  }
141
+ //# sourceMappingURL=libsignal.js.map
@@ -1,19 +1,3 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./abstract-socket-client"), exports);
18
- __exportStar(require("./mobile-socket-client"), exports);
19
- __exportStar(require("./web-socket-client"), exports);
1
+ export * from './types.js';
2
+ export * from './websocket.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,11 @@
1
+ import { EventEmitter } from 'events';
2
+ import { URL } from 'url';
3
+ export class AbstractSocketClient extends EventEmitter {
4
+ constructor(url, config) {
5
+ super();
6
+ this.url = url;
7
+ this.config = config;
8
+ this.setMaxListeners(0);
9
+ }
10
+ }
11
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,50 @@
1
+ import WebSocket from 'ws';
2
+ import { DEFAULT_ORIGIN } from '../../Defaults/index.js';
3
+ import { AbstractSocketClient } from './types.js';
4
+ export class WebSocketClient extends AbstractSocketClient {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.socket = null;
8
+ }
9
+ get isOpen() {
10
+ return this.socket?.readyState === WebSocket.OPEN;
11
+ }
12
+ get isClosed() {
13
+ return this.socket === null || this.socket?.readyState === WebSocket.CLOSED;
14
+ }
15
+ get isClosing() {
16
+ return this.socket === null || this.socket?.readyState === WebSocket.CLOSING;
17
+ }
18
+ get isConnecting() {
19
+ return this.socket?.readyState === WebSocket.CONNECTING;
20
+ }
21
+ async connect() {
22
+ if (this.socket) {
23
+ return;
24
+ }
25
+ this.socket = new WebSocket(this.url, {
26
+ origin: DEFAULT_ORIGIN,
27
+ headers: this.config.options?.headers,
28
+ handshakeTimeout: this.config.connectTimeoutMs,
29
+ timeout: this.config.connectTimeoutMs,
30
+ agent: this.config.agent
31
+ });
32
+ this.socket.setMaxListeners(0);
33
+ const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response'];
34
+ for (const event of events) {
35
+ this.socket?.on(event, (...args) => this.emit(event, ...args));
36
+ }
37
+ }
38
+ async close() {
39
+ if (!this.socket) {
40
+ return;
41
+ }
42
+ this.socket.close();
43
+ this.socket = null;
44
+ }
45
+ send(str, cb) {
46
+ this.socket?.send(str, cb);
47
+ return Boolean(this.socket);
48
+ }
49
+ }
50
+ //# sourceMappingURL=websocket.js.map
@@ -1,17 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeBusinessSocket = void 0;
4
- const business_1 = require("../Utils/business");
5
- const WABinary_1 = require("../WABinary");
6
- const generic_utils_1 = require("../WABinary/generic-utils");
7
- const messages_recv_1 = require("./messages-recv");
8
- const makeBusinessSocket = (config) => {
9
- const sock = (0, messages_recv_1.makeMessagesRecvSocket)(config);
1
+ import { parseCatalogNode, parseCollectionsNode, parseOrderDetailsNode, parseProductNode, toProductNode, uploadingNecessaryImagesOfProduct } from '../Utils/business.js';
2
+ import { jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary/index.js';
3
+ import { getBinaryNodeChild } from '../WABinary/generic-utils.js';
4
+ import { makeMessagesRecvSocket } from './messages-recv.js';
5
+ export const makeBusinessSocket = (config) => {
6
+ const sock = makeMessagesRecvSocket(config);
10
7
  const { authState, query, waUploadToServer } = sock;
11
8
  const getCatalog = async ({ jid, limit, cursor }) => {
12
- var _a;
13
- jid = jid || ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);
14
- jid = (0, WABinary_1.jidNormalizedUser)(jid);
9
+ jid = jid || authState.creds.me?.id;
10
+ jid = jidNormalizedUser(jid);
15
11
  const queryParamNodes = [
16
12
  {
17
13
  tag: 'limit',
@@ -27,7 +23,7 @@ const makeBusinessSocket = (config) => {
27
23
  tag: 'height',
28
24
  attrs: {},
29
25
  content: Buffer.from('100')
30
- },
26
+ }
31
27
  ];
32
28
  if (cursor) {
33
29
  queryParamNodes.push({
@@ -39,7 +35,7 @@ const makeBusinessSocket = (config) => {
39
35
  const result = await query({
40
36
  tag: 'iq',
41
37
  attrs: {
42
- to: WABinary_1.S_WHATSAPP_NET,
38
+ to: S_WHATSAPP_NET,
43
39
  type: 'get',
44
40
  xmlns: 'w:biz:catalog'
45
41
  },
@@ -48,31 +44,30 @@ const makeBusinessSocket = (config) => {
48
44
  tag: 'product_catalog',
49
45
  attrs: {
50
46
  jid,
51
- 'allow_shop_source': 'true'
47
+ allow_shop_source: 'true'
52
48
  },
53
49
  content: queryParamNodes
54
50
  }
55
51
  ]
56
52
  });
57
- return (0, business_1.parseCatalogNode)(result);
53
+ return parseCatalogNode(result);
58
54
  };
59
55
  const getCollections = async (jid, limit = 51) => {
60
- var _a;
61
- jid = jid || ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);
62
- jid = (0, WABinary_1.jidNormalizedUser)(jid);
56
+ jid = jid || authState.creds.me?.id;
57
+ jid = jidNormalizedUser(jid);
63
58
  const result = await query({
64
59
  tag: 'iq',
65
60
  attrs: {
66
- to: WABinary_1.S_WHATSAPP_NET,
61
+ to: S_WHATSAPP_NET,
67
62
  type: 'get',
68
63
  xmlns: 'w:biz:catalog',
69
- 'smax_id': '35'
64
+ smax_id: '35'
70
65
  },
71
66
  content: [
72
67
  {
73
68
  tag: 'collections',
74
69
  attrs: {
75
- 'biz_jid': jid,
70
+ biz_jid: jid
76
71
  },
77
72
  content: [
78
73
  {
@@ -99,16 +94,16 @@ const makeBusinessSocket = (config) => {
99
94
  }
100
95
  ]
101
96
  });
102
- return (0, business_1.parseCollectionsNode)(result);
97
+ return parseCollectionsNode(result);
103
98
  };
104
99
  const getOrderDetails = async (orderId, tokenBase64) => {
105
100
  const result = await query({
106
101
  tag: 'iq',
107
102
  attrs: {
108
- to: WABinary_1.S_WHATSAPP_NET,
103
+ to: S_WHATSAPP_NET,
109
104
  type: 'get',
110
105
  xmlns: 'fb:thrift_iq',
111
- 'smax_id': '5'
106
+ smax_id: '5'
112
107
  },
113
108
  content: [
114
109
  {
@@ -143,15 +138,15 @@ const makeBusinessSocket = (config) => {
143
138
  }
144
139
  ]
145
140
  });
146
- return (0, business_1.parseOrderDetailsNode)(result);
141
+ return parseOrderDetailsNode(result);
147
142
  };
148
143
  const productUpdate = async (productId, update) => {
149
- update = await (0, business_1.uploadingNecessaryImagesOfProduct)(update, waUploadToServer);
150
- const editNode = (0, business_1.toProductNode)(productId, update);
144
+ update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer);
145
+ const editNode = toProductNode(productId, update);
151
146
  const result = await query({
152
147
  tag: 'iq',
153
148
  attrs: {
154
- to: WABinary_1.S_WHATSAPP_NET,
149
+ to: S_WHATSAPP_NET,
155
150
  type: 'set',
156
151
  xmlns: 'w:biz:catalog'
157
152
  },
@@ -175,19 +170,19 @@ const makeBusinessSocket = (config) => {
175
170
  }
176
171
  ]
177
172
  });
178
- const productCatalogEditNode = (0, generic_utils_1.getBinaryNodeChild)(result, 'product_catalog_edit');
179
- const productNode = (0, generic_utils_1.getBinaryNodeChild)(productCatalogEditNode, 'product');
180
- return (0, business_1.parseProductNode)(productNode);
173
+ const productCatalogEditNode = getBinaryNodeChild(result, 'product_catalog_edit');
174
+ const productNode = getBinaryNodeChild(productCatalogEditNode, 'product');
175
+ return parseProductNode(productNode);
181
176
  };
182
177
  const productCreate = async (create) => {
183
178
  // ensure isHidden is defined
184
179
  create.isHidden = !!create.isHidden;
185
- create = await (0, business_1.uploadingNecessaryImagesOfProduct)(create, waUploadToServer);
186
- const createNode = (0, business_1.toProductNode)(undefined, create);
180
+ create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer);
181
+ const createNode = toProductNode(undefined, create);
187
182
  const result = await query({
188
183
  tag: 'iq',
189
184
  attrs: {
190
- to: WABinary_1.S_WHATSAPP_NET,
185
+ to: S_WHATSAPP_NET,
191
186
  type: 'set',
192
187
  xmlns: 'w:biz:catalog'
193
188
  },
@@ -211,15 +206,15 @@ const makeBusinessSocket = (config) => {
211
206
  }
212
207
  ]
213
208
  });
214
- const productCatalogAddNode = (0, generic_utils_1.getBinaryNodeChild)(result, 'product_catalog_add');
215
- const productNode = (0, generic_utils_1.getBinaryNodeChild)(productCatalogAddNode, 'product');
216
- return (0, business_1.parseProductNode)(productNode);
209
+ const productCatalogAddNode = getBinaryNodeChild(result, 'product_catalog_add');
210
+ const productNode = getBinaryNodeChild(productCatalogAddNode, 'product');
211
+ return parseProductNode(productNode);
217
212
  };
218
213
  const productDelete = async (productIds) => {
219
214
  const result = await query({
220
215
  tag: 'iq',
221
216
  attrs: {
222
- to: WABinary_1.S_WHATSAPP_NET,
217
+ to: S_WHATSAPP_NET,
223
218
  type: 'set',
224
219
  xmlns: 'w:biz:catalog'
225
220
  },
@@ -241,9 +236,9 @@ const makeBusinessSocket = (config) => {
241
236
  }
242
237
  ]
243
238
  });
244
- const productCatalogDelNode = (0, generic_utils_1.getBinaryNodeChild)(result, 'product_catalog_delete');
239
+ const productCatalogDelNode = getBinaryNodeChild(result, 'product_catalog_delete');
245
240
  return {
246
- deleted: +((productCatalogDelNode === null || productCatalogDelNode === void 0 ? void 0 : productCatalogDelNode.attrs.deleted_count) || 0)
241
+ deleted: +(productCatalogDelNode?.attrs.deleted_count || 0)
247
242
  };
248
243
  };
249
244
  return {
@@ -257,4 +252,4 @@ const makeBusinessSocket = (config) => {
257
252
  productUpdate
258
253
  };
259
254
  };
260
- exports.makeBusinessSocket = makeBusinessSocket;
255
+ //# sourceMappingURL=business.js.map