junexbaileys 1.1.4 → 1.1.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.
Files changed (2) hide show
  1. package/lib/Socket/mex.js +1 -86
  2. package/package.json +19 -2
package/lib/Socket/mex.js CHANGED
@@ -1,5 +1,3 @@
1
- import nodeFs from 'node:fs';
2
- import nodePath from 'node:path';
3
1
  import { Boom } from '@hapi/boom';
4
2
  import { getBinaryNodeChild, S_WHATSAPP_NET } from '../WABinary/index.js';
5
3
  const wMexQuery = (variables, queryId, query, generateMessageTag) => {
@@ -20,94 +18,11 @@ const wMexQuery = (variables, queryId, query, generateMessageTag) => {
20
18
  ]
21
19
  });
22
20
  };
23
- /**
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
- *
28
- * Unexpected token '', ", ":12036340"... is not valid JSON
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.
33
- */
34
- const STRIP_INVISIBLE_RE = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u200B-\u200D\uFEFF]/g;
35
- const tryJSON = (text) => {
36
- try {
37
- return { ok: true, data: JSON.parse(text) };
38
- }
39
- catch {
40
- return { ok: false };
41
- }
42
- };
43
- export const parseWMexContent = (raw) => {
44
- const strict = tryJSON(raw);
45
- if (strict.ok)
46
- return strict.data;
47
- // 1. invisible bytes (BOM, NUL, zero-width, C0 controls) removed
48
- const cleaned = raw.replace(STRIP_INVISIBLE_RE, '');
49
- const stripped = tryJSON(cleaned);
50
- if (stripped.ok)
51
- return stripped.data;
52
- // 2. keep only the outermost JSON region, then retry
53
- const first = cleaned.search(/[{[]/);
54
- const last = Math.max(cleaned.lastIndexOf('}'), cleaned.lastIndexOf(']'));
55
- if (first !== -1 && last > first) {
56
- const region = tryJSON(cleaned.slice(first, last + 1));
57
- if (region.ok)
58
- return region.data;
59
- }
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 });
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
- };
96
21
  export const executeWMexQuery = async (variables, queryId, dataPath, query, generateMessageTag) => {
97
22
  const result = await wMexQuery(variables, queryId, query, generateMessageTag);
98
23
  const child = getBinaryNodeChild(result, 'result');
99
24
  if (child?.content) {
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
- }
25
+ const data = JSON.parse(child.content.toString());
111
26
  if (data.errors && data.errors.length > 0) {
112
27
  const errorMessages = data.errors.map((err) => err.message || 'Unknown error').join(', ');
113
28
  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",
4
+ "version": "1.1.5",
5
5
  "description": "A WebSockets library for interacting with WhatsApp Web",
6
6
  "keywords": [
7
7
  "whatsapp",
@@ -22,7 +22,24 @@
22
22
  "engine-requirements.js"
23
23
  ],
24
24
  "scripts": {
25
- "preinstall": "node ./engine-requirements.js"
25
+ "build:all": "npm run build && npm run build:docs",
26
+ "build:docs": "typedoc",
27
+ "build": "tsc -P tsconfig.build.json && tsc-esm-fix --tsconfig=tsconfig.build.json --ext=.js",
28
+ "changelog:last": "conventional-changelog -p angular -r 2",
29
+ "changelog:preview": "conventional-changelog -p angular -u",
30
+ "changelog:update": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
31
+ "example": "tsx ./Example/example.ts",
32
+ "gen:protobuf": "sh WAProto/GenerateStatics.sh",
33
+ "format": "prettier --write \"src/**/*.{ts,js,json,md}\"",
34
+ "lint": "tsc && eslint src --ext .js,.ts",
35
+ "lint:fix": "npm run format && npm run lint -- --fix",
36
+ "prepack": "npm run build",
37
+ "prepare": "npm run build",
38
+ "preinstall": "node ./engine-requirements.js",
39
+ "release": "release-it",
40
+ "test": "node --experimental-vm-modules ./node_modules/.bin/jest --testMatch '**/*.test.ts'",
41
+ "test:e2e": "NODE_TLS_REJECT_UNAUTHORIZED=0 node --experimental-vm-modules ./node_modules/.bin/jest --runInBand --forceExit --testMatch '**/*.test-e2e.ts'",
42
+ "update:version": "tsx ./scripts/update-version.ts"
26
43
  },
27
44
  "dependencies": {
28
45
  "@cacheable/node-cache": "^1.4.0",