junexbaileys 1.1.3 → 1.1.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/mex.js +58 -6
- package/package.json +2 -2
package/lib/Socket/mex.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import nodeFs from 'node:fs';
|
|
2
|
+
import nodePath from 'node:path';
|
|
1
3
|
import { Boom } from '@hapi/boom';
|
|
2
4
|
import { getBinaryNodeChild, S_WHATSAPP_NET } from '../WABinary/index.js';
|
|
3
5
|
const wMexQuery = (variables, queryId, query, generateMessageTag) => {
|
|
@@ -19,18 +21,22 @@ const wMexQuery = (variables, queryId, query, generateMessageTag) => {
|
|
|
19
21
|
});
|
|
20
22
|
};
|
|
21
23
|
/**
|
|
22
|
-
* WhatsApp
|
|
23
|
-
* or another invisible control character
|
|
24
|
+
* WhatsApp occasionally returns the w:mex <result> body with a stray byte-order
|
|
25
|
+
* mark, a NUL or another invisible control character. A bare JSON.parse then
|
|
26
|
+
* throws
|
|
27
|
+
*
|
|
24
28
|
* Unexpected token '', ", ":12036340"... is not valid JSON
|
|
25
|
-
*
|
|
26
|
-
*
|
|
29
|
+
*
|
|
30
|
+
* where the offending token looks empty on screen. Retry against a cleaned copy
|
|
31
|
+
* before giving up, and when we do give up, surface the raw payload (text + hex)
|
|
32
|
+
* so the caller can report the real reason instead of a parser error.
|
|
27
33
|
*/
|
|
28
34
|
const STRIP_INVISIBLE_RE = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u200B-\u200D\uFEFF]/g;
|
|
29
35
|
const tryJSON = (text) => {
|
|
30
36
|
try {
|
|
31
37
|
return { ok: true, data: JSON.parse(text) };
|
|
32
38
|
}
|
|
33
|
-
catch
|
|
39
|
+
catch {
|
|
34
40
|
return { ok: false };
|
|
35
41
|
}
|
|
36
42
|
};
|
|
@@ -38,10 +44,12 @@ export const parseWMexContent = (raw) => {
|
|
|
38
44
|
const strict = tryJSON(raw);
|
|
39
45
|
if (strict.ok)
|
|
40
46
|
return strict.data;
|
|
47
|
+
// 1. invisible bytes (BOM, NUL, zero-width, C0 controls) removed
|
|
41
48
|
const cleaned = raw.replace(STRIP_INVISIBLE_RE, '');
|
|
42
49
|
const stripped = tryJSON(cleaned);
|
|
43
50
|
if (stripped.ok)
|
|
44
51
|
return stripped.data;
|
|
52
|
+
// 2. keep only the outermost JSON region, then retry
|
|
45
53
|
const first = cleaned.search(/[{[]/);
|
|
46
54
|
const last = Math.max(cleaned.lastIndexOf('}'), cleaned.lastIndexOf(']'));
|
|
47
55
|
if (first !== -1 && last > first) {
|
|
@@ -51,11 +59,55 @@ export const parseWMexContent = (raw) => {
|
|
|
51
59
|
}
|
|
52
60
|
throw new Boom(`w:mex non-JSON payload (${Buffer.byteLength(raw, 'utf8')} bytes, hex ${Buffer.from(raw, 'utf8').subarray(0, 48).toString('hex')}): ${raw.slice(0, 200)}`, { statusCode: 400 });
|
|
53
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* WhatsApp answers some newsletter joins with a short BINARY acknowledgement
|
|
64
|
+
* instead of the JSON GraphQL body. Observed shape (45 bytes):
|
|
65
|
+
*
|
|
66
|
+
* 04 3a 31 32 ... "\u0004:120363405182019728@newsletter" + NUL padding
|
|
67
|
+
*
|
|
68
|
+
* The join itself succeeds - only the body shape differs, so a JSON-only reader
|
|
69
|
+
* reports a failure that never happened. Accept it for join/leave and keep the
|
|
70
|
+
* raw bytes on disk so the shape can be inspected after the fact.
|
|
71
|
+
*/
|
|
72
|
+
const BINARY_ACK_RE = /xwa2_newsletter_(join|leave)_v2/;
|
|
73
|
+
const reportedBinaryAcks = new Set();
|
|
74
|
+
const reportBinaryAck = (dataPath, raw) => {
|
|
75
|
+
try {
|
|
76
|
+
const file = nodePath.join(process.env.JUNE_ROOT || process.cwd(), 'w-mex-report.txt');
|
|
77
|
+
const bytes = Buffer.from(raw, 'utf8');
|
|
78
|
+
nodeFs.appendFileSync(file, [
|
|
79
|
+
`[${new Date().toISOString()}] binary-ack ${dataPath}`,
|
|
80
|
+
` bytes: ${bytes.length}`,
|
|
81
|
+
` hex : ${bytes.toString('hex')}`,
|
|
82
|
+
` utf8 : ${JSON.stringify(bytes.toString('utf8'))}`,
|
|
83
|
+
''
|
|
84
|
+
].join('\n'));
|
|
85
|
+
if (nodeFs.statSync(file).size > 256 * 1024)
|
|
86
|
+
nodeFs.writeFileSync(file, '');
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// diagnostics must never break the caller
|
|
90
|
+
}
|
|
91
|
+
if (!reportedBinaryAcks.has(dataPath)) {
|
|
92
|
+
reportedBinaryAcks.add(dataPath);
|
|
93
|
+
console.warn(`[ w:mex ] ${dataPath} answered with a binary ack - treating as accepted (raw bytes in w-mex-report.txt)`);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
54
96
|
export const executeWMexQuery = async (variables, queryId, dataPath, query, generateMessageTag) => {
|
|
55
97
|
const result = await wMexQuery(variables, queryId, query, generateMessageTag);
|
|
56
98
|
const child = getBinaryNodeChild(result, 'result');
|
|
57
99
|
if (child?.content) {
|
|
58
|
-
|
|
100
|
+
let data;
|
|
101
|
+
try {
|
|
102
|
+
data = parseWMexContent(child.content.toString());
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
if (BINARY_ACK_RE.test(dataPath || '')) {
|
|
106
|
+
reportBinaryAck(dataPath, child.content.toString());
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
59
111
|
if (data.errors && data.errors.length > 0) {
|
|
60
112
|
const errorMessages = data.errors.map((err) => err.message || 'Unknown error').join(', ');
|
|
61
113
|
const firstError = data.errors[0];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "junexbaileys",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.4",
|
|
5
5
|
"description": "A WebSockets library for interacting with WhatsApp Web",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"whatsapp",
|
|
@@ -123,4 +123,4 @@
|
|
|
123
123
|
"bugs": {
|
|
124
124
|
"url": "https://github.com/supreme-Lord2/JuneX-baileys/issues"
|
|
125
125
|
}
|
|
126
|
-
}
|
|
126
|
+
}
|