wileys 0.4.2 → 0.4.4
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.
- package/lib/Socket/messages-send.js +0 -1
- package/lib/Socket/socket.js +30 -23
- package/package.json +1 -1
package/lib/Socket/socket.js
CHANGED
|
@@ -89,14 +89,6 @@ const makeSocket = (config) => {
|
|
|
89
89
|
// ignore
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
// gently back off when encountering rate limits (429)
|
|
93
|
-
if (message.includes('429') || message.includes('rate limit')) {
|
|
94
|
-
const wait = Math.min(30000, (config.backoffDelayMs || 5000));
|
|
95
|
-
logger.info({ wait }, 'backing off due to rate limit');
|
|
96
|
-
setTimeout(() => {
|
|
97
|
-
// intentionally empty; wait to delay further sends
|
|
98
|
-
}, wait);
|
|
99
|
-
}
|
|
100
92
|
};
|
|
101
93
|
/** await the next incoming message */
|
|
102
94
|
const awaitNextMessage = async (sendMsg) => {
|
|
@@ -134,20 +126,33 @@ const makeSocket = (config) => {
|
|
|
134
126
|
let onErr;
|
|
135
127
|
try {
|
|
136
128
|
const result = await (0, Utils_1.promiseTimeout)(timeoutMs, (resolve, reject) => {
|
|
137
|
-
onRecv =
|
|
129
|
+
onRecv = data => {
|
|
130
|
+
resolve(data);
|
|
131
|
+
};
|
|
138
132
|
onErr = err => {
|
|
139
133
|
reject(err || new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed }));
|
|
140
134
|
};
|
|
141
135
|
ws.on(`TAG:${msgId}`, onRecv);
|
|
142
136
|
ws.on('close', onErr); // if the socket closes, you'll never receive the message
|
|
143
|
-
ws.
|
|
137
|
+
ws.on('error', onErr);
|
|
138
|
+
return () => reject(new boom_1.Boom('Query Cancelled'));
|
|
144
139
|
});
|
|
145
140
|
return result;
|
|
146
141
|
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if ((error === null || error === void 0 ? void 0 : error.isBoom) && ((error === null || error === void 0 ? void 0 : error.output) === null || (error === null || error === void 0 ? void 0 : error.output) === void 0 ? void 0 : error.output.statusCode) === Types_1.DisconnectReason.timedOut) {
|
|
144
|
+
logger === null || logger === void 0 ? void 0 : logger.warn({ msgId }, 'timed out waiting for message');
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
147
149
|
finally {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
150
|
+
if (onRecv)
|
|
151
|
+
ws.off(`TAG:${msgId}`, onRecv);
|
|
152
|
+
if (onErr) {
|
|
153
|
+
ws.off('close', onErr); // if the socket closes, you'll never receive the message
|
|
154
|
+
ws.off('error', onErr);
|
|
155
|
+
}
|
|
151
156
|
}
|
|
152
157
|
};
|
|
153
158
|
/** send a query, and wait for its response. auto-generates message ID if not provided */
|
|
@@ -156,11 +161,13 @@ const makeSocket = (config) => {
|
|
|
156
161
|
node.attrs.id = generateMessageTag();
|
|
157
162
|
}
|
|
158
163
|
const msgId = node.attrs.id;
|
|
159
|
-
const
|
|
160
|
-
waitForMessage(msgId, timeoutMs)
|
|
164
|
+
const result = await (0, Utils_1.promiseTimeout)(timeoutMs, async (resolve, reject) => {
|
|
165
|
+
const wait = waitForMessage(msgId, timeoutMs).catch(reject);
|
|
161
166
|
sendNode(node)
|
|
162
|
-
|
|
163
|
-
|
|
167
|
+
.then(async () => resolve(await wait))
|
|
168
|
+
.catch(reject);
|
|
169
|
+
});
|
|
170
|
+
if (result && 'tag' in result) {
|
|
164
171
|
(0, WABinary_1.assertNodeErrorFree)(result);
|
|
165
172
|
}
|
|
166
173
|
return result;
|
|
@@ -381,13 +388,13 @@ const makeSocket = (config) => {
|
|
|
381
388
|
}
|
|
382
389
|
end(new boom_1.Boom(msg || 'Intentional Logout', { statusCode: Types_1.DisconnectReason.loggedOut }));
|
|
383
390
|
};
|
|
384
|
-
const requestPairingCode = async (phoneNumber, pairKey
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
authState.creds.pairingCode = (0, Utils_1.bytesToCrockford)((0, crypto_1.randomBytes)(5));
|
|
391
|
+
const requestPairingCode = async (phoneNumber, pairKey) => {
|
|
392
|
+
await waitForSocketOpen();
|
|
393
|
+
const pairingCode = pairKey || (0, Utils_1.bytesToCrockford)((0, crypto_1.randomBytes)(5));
|
|
394
|
+
if (pairKey && pairKey.length !== 8) {
|
|
395
|
+
throw new Error('Custom pairing code must be exactly 8 chars');
|
|
390
396
|
}
|
|
397
|
+
authState.creds.pairingCode = pairingCode.toUpperCase();
|
|
391
398
|
authState.creds.me = {
|
|
392
399
|
id: (0, WABinary_1.jidEncode)(phoneNumber, 's.whatsapp.net'),
|
|
393
400
|
name: '~'
|