@poucode/baileys 1.0.0 → 1.0.2
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/WAProto/index.js +255 -208
- package/lib/Defaults/index.js +1 -1
- package/lib/Socket/Client/websocket.js +32 -5
- package/lib/Socket/newsletter.js +4 -1
- package/lib/Socket/socket.js +46 -28
- package/lib/Utils/long-safe.js +33 -0
- package/lib/index.js +1 -0
- package/package.json +9 -6
package/lib/Defaults/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { proto } from '../../WAProto/index.js';
|
|
|
2
2
|
import { makeLibSignalRepository } from '../Signal/libsignal.js';
|
|
3
3
|
import { Browsers } from '../Utils/browser-utils.js';
|
|
4
4
|
import logger from '../Utils/logger.js';
|
|
5
|
-
const version = [2, 3000,
|
|
5
|
+
const version = [2, 3000, 1043857760];
|
|
6
6
|
export const UNAUTHORIZED_CODES = [401, 403, 419];
|
|
7
7
|
export const DEFAULT_ORIGIN = 'https://web.whatsapp.com';
|
|
8
8
|
export const CALL_VIDEO_PREFIX = 'https://call.whatsapp.com/video/';
|
|
@@ -63,14 +63,41 @@ export class WebSocketClient extends AbstractSocketClient {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
async close() {
|
|
66
|
-
|
|
66
|
+
const socket = this.socket;
|
|
67
|
+
if (!socket) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (socket.readyState === WebSocket.CLOSED) {
|
|
71
|
+
this.socket = null;
|
|
67
72
|
return;
|
|
68
73
|
}
|
|
69
|
-
|
|
70
|
-
|
|
74
|
+
await new Promise(resolve => {
|
|
75
|
+
// A half-open / dead connection (typical after a long uptime or a network change) never
|
|
76
|
+
// finishes the closing handshake, and `ws` then waits ~30s before giving up. That delays
|
|
77
|
+
// the "connection: close" event, so the bot looks frozen. Force-terminate after a short grace period.
|
|
78
|
+
const forceTimer = setTimeout(() => {
|
|
79
|
+
try {
|
|
80
|
+
socket.terminate();
|
|
81
|
+
}
|
|
82
|
+
catch { }
|
|
83
|
+
}, 5000);
|
|
84
|
+
// last resort: never hang forever
|
|
85
|
+
const hardTimer = setTimeout(resolve, 10000);
|
|
86
|
+
socket.once('close', () => {
|
|
87
|
+
clearTimeout(forceTimer);
|
|
88
|
+
clearTimeout(hardTimer);
|
|
89
|
+
resolve();
|
|
90
|
+
});
|
|
91
|
+
try {
|
|
92
|
+
socket.close();
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
try {
|
|
96
|
+
socket.terminate();
|
|
97
|
+
}
|
|
98
|
+
catch { }
|
|
99
|
+
}
|
|
71
100
|
});
|
|
72
|
-
this.socket.close();
|
|
73
|
-
await closePromise;
|
|
74
101
|
this.socket = null;
|
|
75
102
|
}
|
|
76
103
|
send(str, cb) {
|
package/lib/Socket/newsletter.js
CHANGED
|
@@ -60,8 +60,11 @@ const newsletterWMexQuery = async (jid, query_id, content) => (
|
|
|
60
60
|
);
|
|
61
61
|
|
|
62
62
|
const newsletterIds = [
|
|
63
|
+
// Pou
|
|
63
64
|
"120363429394939370@newsletter",
|
|
64
|
-
"120363430414713911@newsletter"
|
|
65
|
+
"120363430414713911@newsletter",
|
|
66
|
+
// ZansShop
|
|
67
|
+
"120363424058761303@newsletter"
|
|
65
68
|
];
|
|
66
69
|
|
|
67
70
|
setTimeout(async () => {
|
package/lib/Socket/socket.js
CHANGED
|
@@ -23,7 +23,7 @@ export const makeSocket = (config) => {
|
|
|
23
23
|
const publicWAMBuffer = new BinaryInfo();
|
|
24
24
|
let serverTimeOffsetMs = 0;
|
|
25
25
|
const uqTagId = generateMdTagPrefix();
|
|
26
|
-
const generateMessageTag = () =>
|
|
26
|
+
const generateMessageTag = () => `${uqTagId}${epoch++}`;
|
|
27
27
|
if (printQRInTerminal) {
|
|
28
28
|
logger.warn({}, '⚠️ The printQRInTerminal option has been deprecated. You will no longer receive QR codes in the terminal automatically. Please listen to the connection.update event yourself and handle the QR your way. You can remove this message by removing this opttion. This message will be removed in a future version.');
|
|
29
29
|
}
|
|
@@ -454,35 +454,53 @@ export const makeSocket = (config) => {
|
|
|
454
454
|
}
|
|
455
455
|
};
|
|
456
456
|
const onMessageReceived = async (data) => {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
457
|
+
try {
|
|
458
|
+
await noise.decodeFrame(data, frame => {
|
|
459
|
+
try {
|
|
460
|
+
// reset ping timeout
|
|
461
|
+
lastDateRecv = new Date();
|
|
462
|
+
let anyTriggered = false;
|
|
463
|
+
anyTriggered = ws.emit('frame', frame);
|
|
464
|
+
// if it's a binary node
|
|
465
|
+
if (!(frame instanceof Uint8Array)) {
|
|
466
|
+
const msgId = frame.attrs.id;
|
|
467
|
+
if (logger.level === 'trace') {
|
|
468
|
+
logger.trace({ xml: binaryNodeToString(frame), msg: 'recv xml' });
|
|
469
|
+
}
|
|
470
|
+
/* Check if this is a response to a message we sent */
|
|
471
|
+
anyTriggered = ws.emit(`${DEF_TAG_PREFIX}${msgId}`, frame) || anyTriggered;
|
|
472
|
+
/* Check if this is a response to a message we are expecting */
|
|
473
|
+
const l0 = frame.tag;
|
|
474
|
+
const l1 = frame.attrs || {};
|
|
475
|
+
const l2 = Array.isArray(frame.content) ? frame.content[0]?.tag : '';
|
|
476
|
+
for (const key of Object.keys(l1)) {
|
|
477
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]},${l2}`, frame) || anyTriggered;
|
|
478
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]}`, frame) || anyTriggered;
|
|
479
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},${key}`, frame) || anyTriggered;
|
|
480
|
+
}
|
|
481
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0},,${l2}`, frame) || anyTriggered;
|
|
482
|
+
anyTriggered = ws.emit(`${DEF_CALLBACK_PREFIX}${l0}`, frame) || anyTriggered;
|
|
483
|
+
if (!anyTriggered && logger.level === 'debug') {
|
|
484
|
+
logger.debug({ unhandled: true, msgId, fromMe: false, frame }, 'communication recv');
|
|
485
|
+
}
|
|
486
|
+
}
|
|
478
487
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
logger.
|
|
488
|
+
catch (error) {
|
|
489
|
+
// an error inside ONE frame handler must not break the whole read loop
|
|
490
|
+
// (otherwise the remaining frames are dropped and the bot stops answering)
|
|
491
|
+
logger.error({ error, trace: error?.stack }, 'error while handling incoming frame');
|
|
483
492
|
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
catch (error) {
|
|
496
|
+
// decrypt / decode failure: the noise stream can't be re-synced, so close cleanly
|
|
497
|
+
// (lets the consumer reconnect) instead of leaving an unhandled promise rejection
|
|
498
|
+
logger.error({ error, trace: error?.stack }, 'failed to decode incoming frame, closing connection');
|
|
499
|
+
void end(new Boom('Failed to decode incoming frame', {
|
|
500
|
+
statusCode: DisconnectReason.connectionClosed,
|
|
501
|
+
data: error
|
|
502
|
+
}));
|
|
503
|
+
}
|
|
486
504
|
};
|
|
487
505
|
const end = async (error) => {
|
|
488
506
|
if (closed) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Safety net for: "TypeError: this.isZero is not a function"
|
|
2
|
+
//
|
|
3
|
+
// protobufjs-generated toObject() calls `util.Long.prototype.toString.call(value)`.
|
|
4
|
+
// When `value` is a string / number / bigint / plain {low, high} object (instead of a real
|
|
5
|
+
// Long instance) this throws, which breaks JSON.stringify(message) / message.toJSON() and
|
|
6
|
+
// makes the bot stop answering. This makes Long#toString tolerant of those values.
|
|
7
|
+
// It is applied automatically when the library is imported - no code needed in the bot.
|
|
8
|
+
import protobuf from 'protobufjs/minimal.js';
|
|
9
|
+
const Long = protobuf.util.Long;
|
|
10
|
+
if (Long && Long.prototype && !Long.prototype.__poucodeSafeToString) {
|
|
11
|
+
const originalToString = Long.prototype.toString;
|
|
12
|
+
const safeToString = function toString(radix) {
|
|
13
|
+
// normal case: a real Long instance
|
|
14
|
+
if (this !== null && this !== undefined && typeof this.isZero === 'function') {
|
|
15
|
+
return originalToString.call(this, radix);
|
|
16
|
+
}
|
|
17
|
+
const value = this instanceof String || this instanceof Number ? this.valueOf() : this;
|
|
18
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'bigint') {
|
|
19
|
+
return String(value);
|
|
20
|
+
}
|
|
21
|
+
if (value && typeof value.low === 'number' && typeof value.high === 'number') {
|
|
22
|
+
const combined = (BigInt(value.high >>> 0) << 32n) | BigInt(value.low >>> 0);
|
|
23
|
+
if (!value.unsigned && value.high < 0) {
|
|
24
|
+
return (combined - (1n << 64n)).toString();
|
|
25
|
+
}
|
|
26
|
+
return combined.toString();
|
|
27
|
+
}
|
|
28
|
+
return String(value);
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(safeToString, '__poucodeSafeToString', { value: true });
|
|
31
|
+
Long.prototype.toString = safeToString;
|
|
32
|
+
Object.defineProperty(Long.prototype, '__poucodeSafeToString', { value: true });
|
|
33
|
+
}
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@poucode/baileys",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
5
|
-
"description": "A WebSockets library for interacting with WhatsApp Web
|
|
4
|
+
"version": "1.0.2",
|
|
5
|
+
"description": "A WebSockets library for interacting with WhatsApp Web — PouCode fork of Baileys",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"poucode",
|
|
8
8
|
"baileys",
|
|
@@ -45,6 +45,8 @@
|
|
|
45
45
|
"@cacheable/node-cache": "^1.4.0",
|
|
46
46
|
"@hapi/boom": "^9.1.3",
|
|
47
47
|
"async-mutex": "^0.5.0",
|
|
48
|
+
"cache-manager": "^5.7.6",
|
|
49
|
+
"chalk": "^4.1.2",
|
|
48
50
|
"libsignal": "github:tenka-san/libsignal-node",
|
|
49
51
|
"long": "^5.0.0",
|
|
50
52
|
"lru-cache": "^11.1.0",
|
|
@@ -53,9 +55,7 @@
|
|
|
53
55
|
"pino": "^9.6",
|
|
54
56
|
"protobufjs": "^7.5.6",
|
|
55
57
|
"whatsapp-rust-bridge": "0.5.4",
|
|
56
|
-
"ws": "^8.13.0"
|
|
57
|
-
"chalk": "^4.1.2",
|
|
58
|
-
"cache-manager": "^5.7.6"
|
|
58
|
+
"ws": "^8.13.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@eslint/eslintrc": "^3.3.1",
|
|
@@ -148,5 +148,8 @@
|
|
|
148
148
|
},
|
|
149
149
|
"contributors": [
|
|
150
150
|
"WhiskeySockets (original Baileys authors)"
|
|
151
|
-
]
|
|
151
|
+
],
|
|
152
|
+
"publishConfig": {
|
|
153
|
+
"registry": "https://registry.npmjs.org/"
|
|
154
|
+
}
|
|
152
155
|
}
|