@x4r1s3/baileys 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +1245 -2
- package/SECURITY.md +47 -0
- package/WAProto/V +1 -0
- package/WAProto/WAProto.proto +8892 -0
- package/WAProto/index.d.ts +104128 -0
- package/WAProto/index.js +334920 -0
- package/assets/rolling-circle.svg +133 -0
- package/assets/x4r1s3-banner.svg +32 -0
- package/engine-requirements.js +27 -0
- package/lib/Builders/A2UI.js +245 -0
- package/lib/Builders/AIRich.js +179 -0
- package/lib/Builders/Button.js +108 -0
- package/lib/Builders/CFC.js +284 -0
- package/lib/Builders/Carousel.js +81 -0
- package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
- package/lib/Call/astralena-v3.mjs +96 -0
- package/lib/Call/audio-feeder.mjs +201 -0
- package/lib/Call/audio-recorder.mjs +250 -0
- package/lib/Call/fetch-wasm-resources.mjs +104 -0
- package/lib/Call/index.mjs +707 -0
- package/lib/Call/relay-transport.mjs +563 -0
- package/lib/Call/signaling.mjs +660 -0
- package/lib/Call/types.mjs +17 -0
- package/lib/Call/video-feeder.mjs +94 -0
- package/lib/Call/voip-resources.mjs +259 -0
- package/lib/Call/wasm-engine.mjs +1372 -0
- package/lib/Call/worker-bootstrap.mjs +1042 -0
- package/lib/Defaults/ban-checker.js +56 -0
- package/lib/Defaults/index.js +170 -0
- package/lib/Signal/Group/ciphertext-message.js +23 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +93 -0
- package/lib/Signal/Group/index.js +23 -0
- package/lib/Signal/Group/keyhelper.js +29 -0
- package/lib/Signal/Group/sender-chain-key.js +37 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
- package/lib/Signal/Group/sender-key-message.js +77 -0
- package/lib/Signal/Group/sender-key-name.js +59 -0
- package/lib/Signal/Group/sender-key-record.js +52 -0
- package/lib/Signal/Group/sender-key-state.js +95 -0
- package/lib/Signal/Group/sender-message-key.js +37 -0
- package/lib/Signal/libsignal.js +440 -0
- package/lib/Signal/lid-mapping.js +292 -0
- package/lib/Socket/Client/index.js +14 -0
- package/lib/Socket/Client/types.js +22 -0
- package/lib/Socket/Client/websocket.js +65 -0
- package/lib/Socket/aigroups.js +327 -0
- package/lib/Socket/ban-checker.js +165 -0
- package/lib/Socket/business.js +419 -0
- package/lib/Socket/chats.js +1921 -0
- package/lib/Socket/communities.js +443 -0
- package/lib/Socket/graphql.js +911 -0
- package/lib/Socket/groups.js +561 -0
- package/lib/Socket/index.js +49 -0
- package/lib/Socket/interactive-handler.js +612 -0
- package/lib/Socket/interop.js +575 -0
- package/lib/Socket/managed-account.js +220 -0
- package/lib/Socket/messages-recv.js +2193 -0
- package/lib/Socket/messages-send.js +4141 -0
- package/lib/Socket/mex.js +53 -0
- package/lib/Socket/newsletter.js +291 -0
- package/lib/Socket/privacy.js +458 -0
- package/lib/Socket/registration.js +440 -0
- package/lib/Socket/socket.js +1042 -0
- package/lib/Socket/status-actions.js +287 -0
- package/lib/Socket/username.js +241 -0
- package/lib/Store/index.js +16 -0
- package/lib/Store/make-cache-manager-store.js +101 -0
- package/lib/Store/make-in-memory-store.js +432 -0
- package/lib/Store/make-ordered-dictionary.js +90 -0
- package/lib/Store/object-repository.js +35 -0
- package/lib/Types/Auth.js +13 -0
- package/lib/Types/Bussines.js +13 -0
- package/lib/Types/Call.js +13 -0
- package/lib/Types/Chat.js +19 -0
- package/lib/Types/Contact.js +13 -0
- package/lib/Types/Events.js +13 -0
- package/lib/Types/GroupMetadata.js +13 -0
- package/lib/Types/Label.js +36 -0
- package/lib/Types/LabelAssociation.js +18 -0
- package/lib/Types/Message.js +30 -0
- package/lib/Types/Mex.js +53 -0
- package/lib/Types/Product.js +13 -0
- package/lib/Types/RichType.js +34 -0
- package/lib/Types/Signal.js +13 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +60 -0
- package/lib/Types/USync.js +13 -0
- package/lib/Types/index.js +37 -0
- package/lib/Utils/auth-utils.js +313 -0
- package/lib/Utils/ban-checker.js +48 -0
- package/lib/Utils/bot-capabilities.js +96 -0
- package/lib/Utils/bot-planning-replay.js +177 -0
- package/lib/Utils/browser-utils.js +40 -0
- package/lib/Utils/business.js +242 -0
- package/lib/Utils/c2582.js +147 -0
- package/lib/Utils/chat-utils.js +905 -0
- package/lib/Utils/companion-reg-client-utils.js +104 -0
- package/lib/Utils/consumer-application.js +114 -0
- package/lib/Utils/crypto.js +129 -0
- package/lib/Utils/decode-wa-message.js +496 -0
- package/lib/Utils/direct-chat-theme.js +164 -0
- package/lib/Utils/event-buffer.js +601 -0
- package/lib/Utils/experimental-proto.js +206 -0
- package/lib/Utils/games/miniapp.js +17 -0
- package/lib/Utils/games/slot-machine.js +270 -0
- package/lib/Utils/games/website-preview.js +306 -0
- package/lib/Utils/generics.js +402 -0
- package/lib/Utils/group-history.js +68 -0
- package/lib/Utils/group-wallpaper.js +168 -0
- package/lib/Utils/history.js +162 -0
- package/lib/Utils/identity-change-handler.js +60 -0
- package/lib/Utils/index.js +61 -0
- package/lib/Utils/jid-display-normalization.js +221 -0
- package/lib/Utils/link-preview.js +96 -0
- package/lib/Utils/logger.js +14 -0
- package/lib/Utils/lt-hash.js +19 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/message-composer.js +709 -0
- package/lib/Utils/message-inspect.js +79 -0
- package/lib/Utils/message-retry-manager.js +253 -0
- package/lib/Utils/message-security.js +167 -0
- package/lib/Utils/messages-media.js +1027 -0
- package/lib/Utils/messages.js +2467 -0
- package/lib/Utils/meta-ai-msmsg.js +215 -0
- package/lib/Utils/meta-compositing.js +239 -0
- package/lib/Utils/native-flow.js +158 -0
- package/lib/Utils/newsletter-live-update-utils.js +74 -0
- package/lib/Utils/newsletter-status-utils.js +152 -0
- package/lib/Utils/noise-handler.js +212 -0
- package/lib/Utils/offline-node-processor.js +51 -0
- package/lib/Utils/poll-manager.js +206 -0
- package/lib/Utils/portable-compat.js +12 -0
- package/lib/Utils/pre-key-manager.js +117 -0
- package/lib/Utils/process-message.js +610 -0
- package/lib/Utils/reporting-utils.js +269 -0
- package/lib/Utils/rich-message-utils.js +2841 -0
- package/lib/Utils/scheduled-message.js +107 -0
- package/lib/Utils/signal.js +212 -0
- package/lib/Utils/source-grep.js +57 -0
- package/lib/Utils/stanza-ack.js +49 -0
- package/lib/Utils/sticker-annotation.js +87 -0
- package/lib/Utils/sync-action-utils.js +59 -0
- package/lib/Utils/tc-token-utils.js +174 -0
- package/lib/Utils/use-multi-file-auth-state.js +132 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +120 -0
- package/lib/Utils/validate-connection.js +221 -0
- package/lib/Utils/view-once-utils.js +218 -0
- package/lib/Utils/voip-rekey.js +32 -0
- package/lib/Utils/website-preview.js +27 -0
- package/lib/Utils/welcome-flow.js +228 -0
- package/lib/Utils/x4r1s3.js +144 -0
- package/lib/WABinary/constants.js +1475 -0
- package/lib/WABinary/decode.js +273 -0
- package/lib/WABinary/encode.js +231 -0
- package/lib/WABinary/generic-utils.js +242 -0
- package/lib/WABinary/index.js +17 -0
- package/lib/WABinary/jid-utils.js +107 -0
- package/lib/WABinary/types.js +13 -0
- package/lib/WAM/BinaryInfo.js +21 -0
- package/lib/WAM/constants.js +22864 -0
- package/lib/WAM/encode.js +161 -0
- package/lib/WAM/index.js +15 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
- package/lib/WAUSync/Protocols/index.js +24 -0
- package/lib/WAUSync/USyncQuery.js +213 -0
- package/lib/WAUSync/USyncUser.js +66 -0
- package/lib/WAUSync/index.js +15 -0
- package/lib/antiban.cjs +4726 -0
- package/lib/index.d.ts +1895 -0
- package/lib/index.js +41 -0
- package/package.json +125 -4
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const toBytes = (value) => {
|
|
2
|
+
if (value instanceof Uint8Array)
|
|
3
|
+
return value;
|
|
4
|
+
if (Buffer.isBuffer(value))
|
|
5
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
6
|
+
if (value instanceof ArrayBuffer)
|
|
7
|
+
return new Uint8Array(value);
|
|
8
|
+
if (ArrayBuffer.isView(value))
|
|
9
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
10
|
+
throw new TypeError("Video frame data must be a Uint8Array, Buffer, ArrayBuffer, or typed-array view.");
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Feeds raw NV12 frames supplied by an async iterable into the WASM video driver. */
|
|
14
|
+
export class VideoFeeder {
|
|
15
|
+
#source;
|
|
16
|
+
#sendFrame;
|
|
17
|
+
#onError;
|
|
18
|
+
#running = false;
|
|
19
|
+
#iterator = null;
|
|
20
|
+
|
|
21
|
+
constructor(source, sendFrame, onError = () => {}) {
|
|
22
|
+
if (!source || typeof source[Symbol.asyncIterator] !== "function") {
|
|
23
|
+
throw new TypeError("videoSource must be an async iterable of raw NV12 frame objects.");
|
|
24
|
+
}
|
|
25
|
+
if (typeof sendFrame !== "function")
|
|
26
|
+
throw new TypeError("sendFrame must be a function.");
|
|
27
|
+
this.#source = source;
|
|
28
|
+
this.#sendFrame = sendFrame;
|
|
29
|
+
this.#onError = onError;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
start() {
|
|
33
|
+
if (this.#running)
|
|
34
|
+
return;
|
|
35
|
+
this.#running = true;
|
|
36
|
+
void this.#consume();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
stop() {
|
|
40
|
+
this.#running = false;
|
|
41
|
+
try {
|
|
42
|
+
void this.#iterator?.return?.();
|
|
43
|
+
}
|
|
44
|
+
catch { }
|
|
45
|
+
this.#iterator = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async #consume() {
|
|
49
|
+
try {
|
|
50
|
+
this.#iterator = this.#source[Symbol.asyncIterator]();
|
|
51
|
+
while (this.#running) {
|
|
52
|
+
const { value, done } = await this.#iterator.next();
|
|
53
|
+
if (done || !this.#running)
|
|
54
|
+
break;
|
|
55
|
+
this.#sendFrame(VideoFeeder.validateFrame(value));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (this.#running)
|
|
60
|
+
this.#onError(error instanceof Error ? error : new Error(String(error)));
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
this.#running = false;
|
|
64
|
+
this.#iterator = null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static validateFrame(frame) {
|
|
69
|
+
if (!frame || typeof frame !== "object")
|
|
70
|
+
throw new TypeError("Each video frame must be an object.");
|
|
71
|
+
const width = Math.trunc(Number(frame.width));
|
|
72
|
+
const height = Math.trunc(Number(frame.height));
|
|
73
|
+
if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 2 || height < 2 || width % 2 || height % 2) {
|
|
74
|
+
throw new RangeError("NV12 frame width and height must be positive even integers.");
|
|
75
|
+
}
|
|
76
|
+
const data = toBytes(frame.data);
|
|
77
|
+
const expectedBytes = width * height * 3 / 2;
|
|
78
|
+
if (!Number.isSafeInteger(expectedBytes) || data.byteLength !== expectedBytes) {
|
|
79
|
+
throw new RangeError(`NV12 frame has ${data.byteLength} bytes; ${expectedBytes} required for ${width}x${height}.`);
|
|
80
|
+
}
|
|
81
|
+
const fps = Number(frame.fps ?? 30);
|
|
82
|
+
if (!Number.isFinite(fps) || fps < 1 || fps > 60)
|
|
83
|
+
throw new RangeError("Video frame fps must be between 1 and 60.");
|
|
84
|
+
return {
|
|
85
|
+
data,
|
|
86
|
+
width,
|
|
87
|
+
height,
|
|
88
|
+
fps: Math.round(fps),
|
|
89
|
+
format: Number.isInteger(frame.format) ? frame.format : 0,
|
|
90
|
+
orientation: Number.isInteger(frame.orientation) ? frame.orientation : 0,
|
|
91
|
+
timestamp: Number.isFinite(Number(frame.timestamp)) ? Math.trunc(Number(frame.timestamp)) : Date.now() * 1000,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { randomBytes } from 'node:crypto';
|
|
4
|
+
import { WebSocket } from 'ws';
|
|
5
|
+
|
|
6
|
+
export const VOIP_RESOURCE_FILES = Object.freeze({
|
|
7
|
+
wasm: 'assets/wasm/whatsapp.wasm',
|
|
8
|
+
worker: 'assets/wasm/worker-modules.js',
|
|
9
|
+
loader: 'assets/wasm/loader.js',
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export class VoipResourceError extends Error {
|
|
13
|
+
constructor(message, options = {}) {
|
|
14
|
+
super(message, options);
|
|
15
|
+
this.name = 'VoipResourceError';
|
|
16
|
+
this.code = options.code ?? 'VOIP_RESOURCES_UNAVAILABLE';
|
|
17
|
+
this.capabilities = options.capabilities;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const pathFor = (resourcesPath, relative) => join(resolve(resourcesPath), relative);
|
|
22
|
+
const present = file => existsSync(file) && statSync(file).size > 0;
|
|
23
|
+
|
|
24
|
+
export const inspectVoipResources = (resourcesPath = 'call-resources') => {
|
|
25
|
+
const root = resolve(resourcesPath);
|
|
26
|
+
const files = {
|
|
27
|
+
wasm: pathFor(root, VOIP_RESOURCE_FILES.wasm),
|
|
28
|
+
worker: pathFor(root, VOIP_RESOURCE_FILES.worker),
|
|
29
|
+
loader: pathFor(root, VOIP_RESOURCE_FILES.loader),
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
resourcesPath: root,
|
|
33
|
+
wasm: present(files.wasm),
|
|
34
|
+
worker: present(files.worker),
|
|
35
|
+
loader: present(files.loader),
|
|
36
|
+
complete: present(files.wasm) && present(files.worker),
|
|
37
|
+
files,
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Detect whether a connected Baileys socket exposes a future/session-side
|
|
43
|
+
* resource manifest. Current Baileys sockets do not expose one; this helper
|
|
44
|
+
* makes that capability explicit for callers and future protocol adapters.
|
|
45
|
+
*/
|
|
46
|
+
export const inspectSessionVoipResources = sock => {
|
|
47
|
+
if (!sock) return { supported: false, reason: 'No Baileys socket was supplied.' };
|
|
48
|
+
if (typeof sock.fetchVoipResources === 'function') {
|
|
49
|
+
return { supported: true, method: 'fetchVoipResources' };
|
|
50
|
+
}
|
|
51
|
+
if (sock.voipResources && typeof sock.voipResources === 'object') {
|
|
52
|
+
return { supported: true, method: 'voipResources' };
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
supported: false,
|
|
56
|
+
reason: 'The current Baileys socket does not expose a VOIP resource manifest. Authentication/session state and browser runtime assets are separate.',
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const toBytes = value => {
|
|
61
|
+
if (value == null) return undefined;
|
|
62
|
+
if (Buffer.isBuffer(value)) return value;
|
|
63
|
+
if (value instanceof Uint8Array) return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
64
|
+
if (value instanceof ArrayBuffer) return Buffer.from(value);
|
|
65
|
+
if (ArrayBuffer.isView(value)) return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
66
|
+
return value;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const normalizeManifest = value => {
|
|
70
|
+
if (!value || typeof value !== 'object') return null;
|
|
71
|
+
const wasmUrl = value.wasmUrl ?? value.wasm?.url;
|
|
72
|
+
const workerUrl = value.workerUrl ?? value.worker?.url;
|
|
73
|
+
const loaderUrl = value.loaderUrl ?? value.loader?.url;
|
|
74
|
+
const wasmBinary = toBytes(value.wasmBinary ?? value.wasm?.bytes);
|
|
75
|
+
const workerModulesCode = value.workerModulesCode ?? value.worker?.code;
|
|
76
|
+
const loaderCode = value.loaderCode ?? value.loader?.code;
|
|
77
|
+
if (!wasmUrl && !workerUrl && !wasmBinary && !workerModulesCode) return null;
|
|
78
|
+
return { wasmUrl, workerUrl, loaderUrl, wasmBinary, workerModulesCode, loaderCode };
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const atomicWrite = (file, data) => {
|
|
82
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
83
|
+
const temporary = `${file}.tmp-${process.pid}-${randomBytes(5).toString('hex')}`;
|
|
84
|
+
writeFileSync(temporary, data);
|
|
85
|
+
renameSync(temporary, file);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const fetchChecked = async url => {
|
|
89
|
+
const response = await fetch(url);
|
|
90
|
+
if (!response.ok) throw new VoipResourceError(`HTTP ${response.status} fetching VOIP resource ${url}`, { code: 'VOIP_RESOURCE_HTTP_ERROR' });
|
|
91
|
+
return response;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const downloadManifest = async manifest => {
|
|
95
|
+
const normalized = normalizeManifest(manifest);
|
|
96
|
+
if (!normalized) return null;
|
|
97
|
+
const result = { ...normalized };
|
|
98
|
+
if (!result.wasmBinary && result.wasmUrl) result.wasmBinary = Buffer.from(await (await fetchChecked(result.wasmUrl)).arrayBuffer());
|
|
99
|
+
if (!result.workerModulesCode && result.workerUrl) result.workerModulesCode = await (await fetchChecked(result.workerUrl)).text();
|
|
100
|
+
if (!result.loaderCode && result.loaderUrl) result.loaderCode = await (await fetchChecked(result.loaderUrl)).text();
|
|
101
|
+
if (!result.wasmBinary || !result.workerModulesCode) {
|
|
102
|
+
throw new VoipResourceError('VOIP resource manifest must provide wasm bytes/URL and worker code/URL.', { code: 'VOIP_RESOURCE_MANIFEST_INCOMPLETE' });
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const cdpRequest = (ws, method, params, pending, nextId) => new Promise((resolveResult, reject) => {
|
|
108
|
+
const id = nextId.value++;
|
|
109
|
+
const timeout = setTimeout(() => {
|
|
110
|
+
pending.delete(id);
|
|
111
|
+
reject(new VoipResourceError(`Timed out waiting for Chrome DevTools method ${method}`, { code: 'VOIP_RESOURCE_CDP_TIMEOUT' }));
|
|
112
|
+
}, 15000);
|
|
113
|
+
pending.set(id, {
|
|
114
|
+
resolve: value => { clearTimeout(timeout); resolveResult(value); },
|
|
115
|
+
reject: error => { clearTimeout(timeout); reject(error); },
|
|
116
|
+
});
|
|
117
|
+
ws.send(JSON.stringify({ id, method, params }));
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/** Fetch compatible resources from an already-open WhatsApp Web browser tab. */
|
|
121
|
+
export const fetchVoipResourcesFromBrowser = async ({
|
|
122
|
+
debuggerUrl = process.env.CALL_CHROME_DEBUGGER_JSON_URL || 'http://127.0.0.1:9222/json/list',
|
|
123
|
+
wasmId = process.env.CALL_BROWSER_WASM_ID || '32180',
|
|
124
|
+
} = {}) => {
|
|
125
|
+
const targets = await (await fetchChecked(debuggerUrl)).json();
|
|
126
|
+
const page = targets.find(target => target.type === 'page' && target.url?.includes('web.whatsapp.com'));
|
|
127
|
+
if (!page?.webSocketDebuggerUrl) {
|
|
128
|
+
throw new VoipResourceError('No WhatsApp Web page found through the configured browser debugger. The current Baileys socket does not provide browser runtime resources.', { code: 'VOIP_BROWSER_PAGE_NOT_FOUND' });
|
|
129
|
+
}
|
|
130
|
+
const ws = new WebSocket(page.webSocketDebuggerUrl);
|
|
131
|
+
const pending = new Map();
|
|
132
|
+
const nextId = { value: 1 };
|
|
133
|
+
const rejectPending = error => {
|
|
134
|
+
for (const request of pending.values()) request.reject(error);
|
|
135
|
+
pending.clear();
|
|
136
|
+
};
|
|
137
|
+
ws.on('error', error => rejectPending(new VoipResourceError(`Chrome DevTools connection failed: ${error.message}`, { code: 'VOIP_BROWSER_CONNECTION_FAILED', cause: error })));
|
|
138
|
+
ws.on('close', () => rejectPending(new VoipResourceError('Chrome DevTools connection closed while fetching VOIP resources.', { code: 'VOIP_BROWSER_CONNECTION_CLOSED' })));
|
|
139
|
+
ws.on('message', raw => {
|
|
140
|
+
let message;
|
|
141
|
+
try { message = JSON.parse(raw.toString()); } catch { return; }
|
|
142
|
+
if (!message.id || !pending.has(message.id)) return;
|
|
143
|
+
const request = pending.get(message.id);
|
|
144
|
+
pending.delete(message.id);
|
|
145
|
+
if (message.error) request.reject(new Error(message.error.message));
|
|
146
|
+
else request.resolve(message.result);
|
|
147
|
+
});
|
|
148
|
+
try {
|
|
149
|
+
await new Promise((resolveOpen, rejectOpen) => {
|
|
150
|
+
ws.once('open', resolveOpen);
|
|
151
|
+
ws.once('error', rejectOpen);
|
|
152
|
+
});
|
|
153
|
+
const expression = `(() => {
|
|
154
|
+
try {
|
|
155
|
+
const resource = require("WAWebVoipWebWasmWorkerResource");
|
|
156
|
+
const id = ${JSON.stringify(wasmId)};
|
|
157
|
+
return {
|
|
158
|
+
workerUrl: resource?.url || null,
|
|
159
|
+
wasmUrl: resource?.hsdp?.bxData?.[id]?.uri || null,
|
|
160
|
+
availableIds: Object.keys(resource?.hsdp?.bxData || {}),
|
|
161
|
+
};
|
|
162
|
+
} catch (error) { return { error: error.message }; }
|
|
163
|
+
})()`;
|
|
164
|
+
const evaluation = await cdpRequest(ws, 'Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }, pending, nextId);
|
|
165
|
+
if (evaluation.exceptionDetails) throw new VoipResourceError(evaluation.exceptionDetails.text || 'Could not inspect WhatsApp Web resources', { code: 'VOIP_BROWSER_EVALUATION_FAILED' });
|
|
166
|
+
const info = evaluation.result?.value;
|
|
167
|
+
if (info?.error) throw new VoipResourceError(`Browser evaluation failed: ${info.error}`, { code: 'VOIP_BROWSER_EVALUATION_FAILED' });
|
|
168
|
+
if (!info?.workerUrl || !info?.wasmUrl) throw new VoipResourceError(`No resources found for WASM id ${wasmId}; available IDs: ${(info?.availableIds || []).join(', ')}`, { code: 'VOIP_BROWSER_RESOURCES_NOT_FOUND' });
|
|
169
|
+
return downloadManifest(info);
|
|
170
|
+
} finally {
|
|
171
|
+
ws.removeAllListeners();
|
|
172
|
+
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) ws.close();
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/** Fetch resources from caller-supplied URLs. URLs are intentionally never used by default. */
|
|
177
|
+
export const fetchVoipResourcesFromUrls = async ({ wasmUrl, workerUrl, loaderUrl } = {}) => {
|
|
178
|
+
if (!wasmUrl || !workerUrl) {
|
|
179
|
+
throw new VoipResourceError('Remote VOIP resources require both wasmUrl and workerUrl.', { code: 'VOIP_REMOTE_URLS_INCOMPLETE' });
|
|
180
|
+
}
|
|
181
|
+
return downloadManifest({ wasmUrl, workerUrl, loaderUrl });
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/** Load bytes/code from a trusted runtime asset package exposing a manifest. */
|
|
185
|
+
export const loadVoipResourcesFromPackage = async packageName => {
|
|
186
|
+
if (!packageName) throw new TypeError('A VOIP runtime package name is required.');
|
|
187
|
+
const module = await import(packageName);
|
|
188
|
+
const manifest = module.voipResources ?? module.default?.voipResources ?? module.default;
|
|
189
|
+
const result = await downloadManifest(manifest);
|
|
190
|
+
if (!result) throw new VoipResourceError(`Runtime package ${packageName} does not export a VOIP resource manifest.`, { code: 'VOIP_RUNTIME_PACKAGE_INVALID' });
|
|
191
|
+
return result;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const writeManifest = (resourcesPath, manifest, force = false) => {
|
|
195
|
+
const status = inspectVoipResources(resourcesPath);
|
|
196
|
+
if (!status.complete || force) {
|
|
197
|
+
atomicWrite(status.files.worker, Buffer.from(manifest.workerModulesCode));
|
|
198
|
+
atomicWrite(status.files.wasm, Buffer.from(manifest.wasmBinary));
|
|
199
|
+
if (manifest.loaderCode) atomicWrite(status.files.loader, Buffer.from(manifest.loaderCode));
|
|
200
|
+
}
|
|
201
|
+
return inspectVoipResources(resourcesPath);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Ensure local VOIP resources exist. Resolution order:
|
|
206
|
+
* 1. Existing cache.
|
|
207
|
+
* 2. Explicit provider or a future socket-side manifest.
|
|
208
|
+
* 3. Trusted runtime asset package.
|
|
209
|
+
* 4. Optional WhatsApp Web browser debugger.
|
|
210
|
+
*
|
|
211
|
+
* A normal Baileys socket is intentionally not treated as a resource source
|
|
212
|
+
* unless it exposes an explicit fetchVoipResources()/voipResources capability.
|
|
213
|
+
*/
|
|
214
|
+
export const ensureVoipResources = async ({
|
|
215
|
+
resourcesPath = process.env.X4R1S3_VOIP_RESOURCES || process.env.PLOGME_VOIP_RESOURCES || 'call-resources',
|
|
216
|
+
sock,
|
|
217
|
+
resourceProvider,
|
|
218
|
+
runtimePackage,
|
|
219
|
+
resourceUrls,
|
|
220
|
+
debuggerUrl,
|
|
221
|
+
wasmId,
|
|
222
|
+
force = false,
|
|
223
|
+
} = {}) => {
|
|
224
|
+
const existing = inspectVoipResources(resourcesPath);
|
|
225
|
+
if (existing.complete && !force) return { ...existing, source: 'cache' };
|
|
226
|
+
|
|
227
|
+
let manifest = null;
|
|
228
|
+
let source = null;
|
|
229
|
+
if (typeof resourceProvider === 'function') {
|
|
230
|
+
manifest = await resourceProvider({ sock });
|
|
231
|
+
source = 'provider';
|
|
232
|
+
} else if (sock?.fetchVoipResources) {
|
|
233
|
+
manifest = await sock.fetchVoipResources();
|
|
234
|
+
source = 'session';
|
|
235
|
+
} else if (sock?.voipResources) {
|
|
236
|
+
manifest = sock.voipResources;
|
|
237
|
+
source = 'session';
|
|
238
|
+
} else if (runtimePackage) {
|
|
239
|
+
manifest = await loadVoipResourcesFromPackage(runtimePackage);
|
|
240
|
+
source = 'runtime-package';
|
|
241
|
+
} else if (resourceUrls) {
|
|
242
|
+
manifest = await fetchVoipResourcesFromUrls(resourceUrls);
|
|
243
|
+
source = 'remote-url';
|
|
244
|
+
} else if (debuggerUrl || process.env.CALL_CHROME_DEBUGGER_JSON_URL) {
|
|
245
|
+
manifest = await fetchVoipResourcesFromBrowser({ debuggerUrl, wasmId });
|
|
246
|
+
source = 'browser';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!manifest) {
|
|
250
|
+
const capabilities = inspectSessionVoipResources(sock);
|
|
251
|
+
throw new VoipResourceError(
|
|
252
|
+
'VOIP resources are missing. The current Baileys session has no resource manifest; provide a trusted runtime package, resource provider, or browser debugger endpoint.',
|
|
253
|
+
{ code: 'VOIP_RESOURCES_UNAVAILABLE', capabilities },
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
const downloaded = await downloadManifest(manifest);
|
|
257
|
+
const result = writeManifest(resourcesPath, downloaded, force);
|
|
258
|
+
return { ...result, source };
|
|
259
|
+
};
|