wileys 0.4.3 → 0.4.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.
@@ -126,20 +126,33 @@ const makeSocket = (config) => {
126
126
  let onErr;
127
127
  try {
128
128
  const result = await (0, Utils_1.promiseTimeout)(timeoutMs, (resolve, reject) => {
129
- onRecv = resolve;
129
+ onRecv = data => {
130
+ resolve(data);
131
+ };
130
132
  onErr = err => {
131
133
  reject(err || new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed }));
132
134
  };
133
135
  ws.on(`TAG:${msgId}`, onRecv);
134
136
  ws.on('close', onErr); // if the socket closes, you'll never receive the message
135
137
  ws.on('error', onErr);
138
+ return () => reject(new boom_1.Boom('Query Cancelled'));
136
139
  });
137
140
  return result;
138
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
+ }
139
149
  finally {
140
- ws.off(`TAG:${msgId}`, onRecv);
141
- ws.off('close', onErr); // if the socket closes, you'll never receive the message
142
- ws.off('error', onErr);
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
+ }
143
156
  }
144
157
  };
145
158
  /** send a query, and wait for its response. auto-generates message ID if not provided */
@@ -375,65 +388,81 @@ const makeSocket = (config) => {
375
388
  }
376
389
  end(new boom_1.Boom(msg || 'Intentional Logout', { statusCode: Types_1.DisconnectReason.loggedOut }));
377
390
  };
378
- const requestPairingCode = async (phoneNumber, pairKey = "YUPRADEV") => {
379
- if (pairKey) {
380
- authState.creds.pairingCode = pairKey.toUpperCase();
381
- }
382
- else {
383
- authState.creds.pairingCode = (0, Utils_1.bytesToCrockford)((0, crypto_1.randomBytes)(5));
391
+ const requestPairingCode = async (phoneNumber, pairKey) => {
392
+ var _a, _b;
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');
384
396
  }
397
+ authState.creds.pairingCode = pairingCode.toUpperCase();
385
398
  authState.creds.me = {
386
399
  id: (0, WABinary_1.jidEncode)(phoneNumber, 's.whatsapp.net'),
387
400
  name: '~'
388
401
  };
389
402
  ev.emit('creds.update', authState.creds);
390
- await sendNode({
391
- tag: 'iq',
392
- attrs: {
393
- to: WABinary_1.S_WHATSAPP_NET,
394
- type: 'set',
395
- id: generateMessageTag(),
396
- xmlns: 'md'
397
- },
398
- content: [
399
- {
400
- tag: 'link_code_companion_reg',
403
+ const maxRetries = 2;
404
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
405
+ try {
406
+ await waitForSocketOpen();
407
+ await sendNode({
408
+ tag: 'iq',
401
409
  attrs: {
402
- jid: authState.creds.me.id,
403
- stage: 'companion_hello',
404
- // eslint-disable-next-line camelcase
405
- should_show_push_notification: 'true'
410
+ to: WABinary_1.S_WHATSAPP_NET,
411
+ type: 'set',
412
+ id: generateMessageTag(),
413
+ xmlns: 'md'
406
414
  },
407
415
  content: [
408
416
  {
409
- tag: 'link_code_pairing_wrapped_companion_ephemeral_pub',
410
- attrs: {},
411
- content: await generatePairingKey()
412
- },
413
- {
414
- tag: 'companion_server_auth_key_pub',
415
- attrs: {},
416
- content: authState.creds.noiseKey.public
417
- },
418
- {
419
- tag: 'companion_platform_id',
420
- attrs: {},
421
- content: (0, Utils_1.getPlatformId)(browser[1])
422
- },
423
- {
424
- tag: 'companion_platform_display',
425
- attrs: {},
426
- content: `${browser[1]} (${browser[0]})`
427
- },
428
- {
429
- tag: 'link_code_pairing_nonce',
430
- attrs: {},
431
- content: '0'
417
+ tag: 'link_code_companion_reg',
418
+ attrs: {
419
+ jid: authState.creds.me.id,
420
+ stage: 'companion_hello',
421
+ // eslint-disable-next-line camelcase
422
+ should_show_push_notification: 'true'
423
+ },
424
+ content: [
425
+ {
426
+ tag: 'link_code_pairing_wrapped_companion_ephemeral_pub',
427
+ attrs: {},
428
+ content: await generatePairingKey()
429
+ },
430
+ {
431
+ tag: 'companion_server_auth_key_pub',
432
+ attrs: {},
433
+ content: authState.creds.noiseKey.public
434
+ },
435
+ {
436
+ tag: 'companion_platform_id',
437
+ attrs: {},
438
+ content: (0, Utils_1.getPlatformId)(browser[1])
439
+ },
440
+ {
441
+ tag: 'companion_platform_display',
442
+ attrs: {},
443
+ content: `${browser[1]} (${browser[0]})`
444
+ },
445
+ {
446
+ tag: 'link_code_pairing_nonce',
447
+ attrs: {},
448
+ content: '0'
449
+ }
450
+ ]
432
451
  }
433
452
  ]
453
+ });
454
+ break;
455
+ }
456
+ catch (error) {
457
+ const statusCode = ((_b = (_a = error === null || error === void 0 ? void 0 : error.output) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.statusCode) || (error === null || error === void 0 ? void 0 : error.statusCode);
458
+ const canRetry = statusCode === 428 || statusCode === Types_1.DisconnectReason.connectionClosed || statusCode === Types_1.DisconnectReason.connectionLost;
459
+ if (!canRetry || attempt >= maxRetries) {
460
+ throw error;
434
461
  }
435
- ]
436
- });
462
+ logger.warn({ attempt, statusCode, trace: error === null || error === void 0 ? void 0 : error.stack }, 'pairing code request failed, retrying');
463
+ await (0, Utils_1.delay)(500 * attempt);
464
+ }
465
+ }
437
466
  return authState.creds.pairingCode;
438
467
  };
439
468
  async function generatePairingKey() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wileys",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "WhatsApp Web API Library",
5
5
  "keywords": [
6
6
  "wileys",