@vanzxy/baileys 2.0.0
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/NOTICE.md +111 -0
- package/README.md +830 -0
- package/WAProto/index.d.ts +9 -0
- package/WAProto/index.js +142036 -0
- package/engine-requirements.js +13 -0
- package/lib/Defaults/index.d.ts +152 -0
- package/lib/Defaults/index.js +179 -0
- package/lib/Framework/Bot.js +209 -0
- package/lib/Framework/Context.js +90 -0
- package/lib/Framework/MediaManager.js +152 -0
- package/lib/Framework/SessionManager.js +34 -0
- package/lib/Framework/StatsManager.js +120 -0
- package/lib/Framework/Store/SQLiteStore.js +74 -0
- package/lib/Framework/index.js +11 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.d.ts +7 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.d.ts +10 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.d.ts +6 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-name.d.ts +11 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.d.ts +12 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-state.d.ts +16 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/libsignal.d.ts +54 -0
- package/lib/Signal/libsignal.js +431 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +277 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/types.d.ts +5 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +54 -0
- package/lib/Socket/business.d.ts +395 -0
- package/lib/Socket/business.js +380 -0
- package/lib/Socket/chats.d.ts +136 -0
- package/lib/Socket/chats.js +1328 -0
- package/lib/Socket/communities.d.ts +561 -0
- package/lib/Socket/communities.js +434 -0
- package/lib/Socket/dugong.d.ts +117 -0
- package/lib/Socket/dugong.js +799 -0
- package/lib/Socket/groups.d.ts +285 -0
- package/lib/Socket/groups.js +355 -0
- package/lib/Socket/index.d.ts +531 -0
- package/lib/Socket/index.js +34 -0
- package/lib/Socket/messages-recv.d.ts +333 -0
- package/lib/Socket/messages-recv.js +1782 -0
- package/lib/Socket/messages-send.d.ts +329 -0
- package/lib/Socket/messages-send.js +1383 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.d.ts +281 -0
- package/lib/Socket/newsletter.js +321 -0
- package/lib/Socket/socket.d.ts +60 -0
- package/lib/Socket/socket.js +1001 -0
- package/lib/Socket/username.js +153 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-in-memory-store.d.ts +62 -0
- package/lib/Store/make-in-memory-store.js +429 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +24 -0
- package/lib/Types/Auth.d.ts +117 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +124 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.d.ts +26 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +256 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +71 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.d.ts +2 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.d.ts +11 -0
- package/lib/Types/Message.js +18 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +39 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/RichType.d.ts +2 -0
- package/lib/Types/RichType.js +23 -0
- package/lib/Types/Signal.d.ts +87 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +136 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +5 -0
- package/lib/Types/State.js +56 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +66 -0
- package/lib/Types/index.js +26 -0
- package/lib/Utils/A2UI.js +277 -0
- package/lib/Utils/MessageBuilder.js +4405 -0
- package/lib/Utils/MessageBuilder_d.ts +411 -0
- package/lib/Utils/PersistentStore.js +592 -0
- package/lib/Utils/PersistentStore_d.ts +60 -0
- package/lib/Utils/anti-delete.d.ts +74 -0
- package/lib/Utils/anti-delete.js +222 -0
- package/lib/Utils/auth-utils.d.ts +57 -0
- package/lib/Utils/auth-utils.js +307 -0
- package/lib/Utils/auto-reply.d.ts +47 -0
- package/lib/Utils/auto-reply.js +158 -0
- package/lib/Utils/baileys-event-stream.d.ts +8 -0
- package/lib/Utils/baileys-event-stream.js +62 -0
- package/lib/Utils/browser-utils.d.ts +8 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.d.ts +49 -0
- package/lib/Utils/business.js +240 -0
- package/lib/Utils/button-helper-utils.js +314 -0
- package/lib/Utils/button-sender.js +824 -0
- package/lib/Utils/chat-control.d.ts +159 -0
- package/lib/Utils/chat-control.js +232 -0
- package/lib/Utils/chat-history-helpers.d.ts +21 -0
- package/lib/Utils/chat-history-helpers.js +71 -0
- package/lib/Utils/chat-utils.d.ts +407 -0
- package/lib/Utils/chat-utils.js +886 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/companion-reg-client-utils.js +40 -0
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +148 -0
- package/lib/Utils/decode-wa-message.d.ts +83 -0
- package/lib/Utils/decode-wa-message.js +317 -0
- package/lib/Utils/event-buffer.d.ts +12 -0
- package/lib/Utils/event-buffer.js +636 -0
- package/lib/Utils/generics.d.ts +69 -0
- package/lib/Utils/generics.js +439 -0
- package/lib/Utils/history.d.ts +90 -0
- package/lib/Utils/history.js +147 -0
- package/lib/Utils/identity-change-handler.d.ts +13 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/index.d.ts +39 -0
- package/lib/Utils/index.js +46 -0
- package/lib/Utils/link-preview.d.ts +12 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/logger.d.ts +2 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +27 -0
- package/lib/Utils/make-mutex.d.ts +6 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/media-messages.d.ts +18 -0
- package/lib/Utils/media-messages.js +71 -0
- package/lib/Utils/media-set.d.ts +13 -0
- package/lib/Utils/media-set.js +172 -0
- package/lib/Utils/message-kind.js +139 -0
- package/lib/Utils/message-retry-manager.d.ts +79 -0
- package/lib/Utils/message-retry-manager.js +265 -0
- package/lib/Utils/message-search.d.ts +44 -0
- package/lib/Utils/message-search.js +189 -0
- package/lib/Utils/messages-media.d.ts +135 -0
- package/lib/Utils/messages-media.js +869 -0
- package/lib/Utils/messages.d.ts +44 -0
- package/lib/Utils/messages.js +2155 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +201 -0
- package/lib/Utils/offline-node-processor.d.ts +9 -0
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/past-participants.d.ts +14 -0
- package/lib/Utils/past-participants.js +40 -0
- package/lib/Utils/pre-key-manager.d.ts +25 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/process-message.d.ts +82 -0
- package/lib/Utils/process-message.js +809 -0
- package/lib/Utils/reporting-utils.d.ts +12 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/rich-message-utils.d.ts +48 -0
- package/lib/Utils/rich-message-utils.js +445 -0
- package/lib/Utils/scheduling.d.ts +42 -0
- package/lib/Utils/scheduling.js +140 -0
- package/lib/Utils/signal.d.ts +82 -0
- package/lib/Utils/signal.js +201 -0
- package/lib/Utils/stanza-ack.d.ts +16 -0
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/status.d.ts +50 -0
- package/lib/Utils/status.js +108 -0
- package/lib/Utils/stickerpack.d.ts +51 -0
- package/lib/Utils/stickerpack.js +275 -0
- package/lib/Utils/sync-action-utils.d.ts +2 -0
- package/lib/Utils/sync-action-utils.js +49 -0
- package/lib/Utils/tc-token-utils.d.ts +29 -0
- package/lib/Utils/tc-token-utils.js +163 -0
- package/lib/Utils/templates.d.ts +76 -0
- package/lib/Utils/templates.js +151 -0
- package/lib/Utils/use-cache-manager-auth-state.d.ts +13 -0
- package/lib/Utils/use-cache-manager-auth-state.js +82 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-multi-file-auth-state.js +167 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-single-file-auth-state.js +114 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/use-sqlite-auth-state.js +170 -0
- package/lib/Utils/validate-connection.d.ts +42 -0
- package/lib/Utils/validate-connection.js +199 -0
- package/lib/Utils/vcard.d.ts +58 -0
- package/lib/Utils/vcard.js +105 -0
- package/lib/VoIP/audio-feeder.d.ts +15 -0
- package/lib/VoIP/audio-feeder.js +132 -0
- package/lib/VoIP/index.js +277 -0
- package/lib/VoIP/relay-transport.d.ts +43 -0
- package/lib/VoIP/relay-transport.js +559 -0
- package/lib/VoIP/signaling.js +624 -0
- package/lib/VoIP/types.d.ts +69 -0
- package/lib/VoIP/types.js +17 -0
- package/lib/VoIP/wasm-engine.d.ts +103 -0
- package/lib/VoIP/wasm-engine.js +1214 -0
- package/lib/VoIP/worker-bootstrap.js +1042 -0
- package/lib/WABinary/constants.d.ts +61 -0
- package/lib/WABinary/constants.js +1467 -0
- package/lib/WABinary/decode.d.ts +9 -0
- package/lib/WABinary/decode.js +262 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +220 -0
- package/lib/WABinary/generic-utils.d.ts +74 -0
- package/lib/WABinary/generic-utils.js +277 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.d.ts +28 -0
- package/lib/WABinary/jid-utils.js +96 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +7 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.d.ts +34926 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.d.ts +1 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +63 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/lib/WAUSync/Protocols/index.d.ts +7 -0
- package/lib/WAUSync/Protocols/index.js +8 -0
- package/lib/WAUSync/USyncQuery.d.ts +20 -0
- package/lib/WAUSync/USyncQuery.js +122 -0
- package/lib/WAUSync/USyncUser.d.ts +16 -0
- package/lib/WAUSync/USyncUser.js +31 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +4 -0
- package/lib/assets/wasm/loader.js +5 -0
- package/lib/assets/wasm/whatsapp.wasm +0 -0
- package/lib/assets/wasm/worker-modules.js +273 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +20 -0
- package/package.json +175 -0
- package/postinstall-banner.js +56 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audio feeder.
|
|
3
|
+
*
|
|
4
|
+
* Spawns ffmpeg to decode `source` into f32le PCM at the requested rate, then
|
|
5
|
+
* meters frames out at chunk-cadence to the WASM uplink.
|
|
6
|
+
*
|
|
7
|
+
* @author ShellTear
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
const LOW_WATERMARK_CHUNKS = 16;
|
|
11
|
+
const MAX_QUEUED_CHUNKS = 1024;
|
|
12
|
+
const DEFAULT_WARMUP_MS = 500;
|
|
13
|
+
export class AudioFeeder {
|
|
14
|
+
sampleRate;
|
|
15
|
+
channels;
|
|
16
|
+
framesPerChunk;
|
|
17
|
+
onChunk;
|
|
18
|
+
source;
|
|
19
|
+
#proc = null;
|
|
20
|
+
#pending = Buffer.alloc(0);
|
|
21
|
+
#queue = [];
|
|
22
|
+
#emitTimer = null;
|
|
23
|
+
#nextEmitAtMs = 0;
|
|
24
|
+
#warmupUntilMs = 0;
|
|
25
|
+
droppedChunks = 0;
|
|
26
|
+
underflowChunks = 0;
|
|
27
|
+
bytesProduced = 0;
|
|
28
|
+
chunksEmitted = 0;
|
|
29
|
+
constructor(sampleRate, channels, framesPerChunk, onChunk, source = "silence") {
|
|
30
|
+
this.sampleRate = sampleRate;
|
|
31
|
+
this.channels = channels;
|
|
32
|
+
this.framesPerChunk = framesPerChunk;
|
|
33
|
+
this.onChunk = onChunk;
|
|
34
|
+
this.source = source;
|
|
35
|
+
}
|
|
36
|
+
start = () => {
|
|
37
|
+
if (this.#proc)
|
|
38
|
+
return;
|
|
39
|
+
const chunkSamples = this.framesPerChunk * this.channels;
|
|
40
|
+
const chunkBytes = chunkSamples * Float32Array.BYTES_PER_ELEMENT;
|
|
41
|
+
const chunkIntervalMs = (this.framesPerChunk / this.sampleRate) * 1000;
|
|
42
|
+
const inputArgs = this.#resolveInputArgs();
|
|
43
|
+
this.#proc = spawn("ffmpeg", [
|
|
44
|
+
"-hide_banner",
|
|
45
|
+
"-loglevel", "error",
|
|
46
|
+
"-thread_queue_size", "512",
|
|
47
|
+
...inputArgs,
|
|
48
|
+
"-f", "f32le",
|
|
49
|
+
"-ac", String(this.channels),
|
|
50
|
+
"-ar", String(this.sampleRate),
|
|
51
|
+
"pipe:1",
|
|
52
|
+
]);
|
|
53
|
+
this.#proc.stdout.on("data", (chunk) => {
|
|
54
|
+
this.#pending = Buffer.concat([this.#pending, chunk]);
|
|
55
|
+
while (this.#pending.length >= chunkBytes) {
|
|
56
|
+
if (this.#queue.length >= MAX_QUEUED_CHUNKS) {
|
|
57
|
+
this.#proc?.stdout.pause();
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
const frame = this.#pending.subarray(0, chunkBytes);
|
|
61
|
+
this.#pending = this.#pending.subarray(chunkBytes);
|
|
62
|
+
const out = new Float32Array(chunkSamples);
|
|
63
|
+
out.set(new Float32Array(frame.buffer, frame.byteOffset, chunkSamples));
|
|
64
|
+
this.bytesProduced += chunkBytes;
|
|
65
|
+
this.#queue.push(out);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
this.#proc.stderr.on("data", (chunk) => {
|
|
69
|
+
process.stderr.write(`[AudioFeeder] ${chunk.toString().trim()}\n`);
|
|
70
|
+
});
|
|
71
|
+
this.#proc.on("exit", (code) => {
|
|
72
|
+
if (code !== 0 && code !== null) {
|
|
73
|
+
process.stderr.write(`[AudioFeeder] ffmpeg exited with code=${code}\n`);
|
|
74
|
+
}
|
|
75
|
+
this.#proc = null;
|
|
76
|
+
});
|
|
77
|
+
this.#nextEmitAtMs = 0;
|
|
78
|
+
this.#warmupUntilMs = Date.now() + DEFAULT_WARMUP_MS;
|
|
79
|
+
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
80
|
+
};
|
|
81
|
+
stop = () => {
|
|
82
|
+
if (this.#emitTimer) {
|
|
83
|
+
clearTimeout(this.#emitTimer);
|
|
84
|
+
this.#emitTimer = null;
|
|
85
|
+
}
|
|
86
|
+
this.#proc?.kill("SIGTERM");
|
|
87
|
+
this.#proc = null;
|
|
88
|
+
this.#pending = Buffer.alloc(0);
|
|
89
|
+
this.#queue = [];
|
|
90
|
+
this.#warmupUntilMs = 0;
|
|
91
|
+
};
|
|
92
|
+
#resolveInputArgs = () => {
|
|
93
|
+
if (!this.source || this.source === "silence") {
|
|
94
|
+
return ["-f", "lavfi", "-i", `aevalsrc=0:d=3600:s=${this.sampleRate}`];
|
|
95
|
+
}
|
|
96
|
+
if (this.source.startsWith("lavfi:")) {
|
|
97
|
+
return ["-f", "lavfi", "-i", this.source.slice("lavfi:".length)];
|
|
98
|
+
}
|
|
99
|
+
return ["-i", this.source];
|
|
100
|
+
};
|
|
101
|
+
#scheduleNext = (chunkSamples, chunkIntervalMs) => {
|
|
102
|
+
if (!this.#proc)
|
|
103
|
+
return;
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
if (this.#nextEmitAtMs === 0)
|
|
106
|
+
this.#nextEmitAtMs = now;
|
|
107
|
+
const delayMs = Math.max(0, this.#nextEmitAtMs - now);
|
|
108
|
+
this.#emitTimer = setTimeout(() => {
|
|
109
|
+
this.#emitTimer = null;
|
|
110
|
+
if (this.#queue.length < LOW_WATERMARK_CHUNKS && Date.now() < this.#warmupUntilMs) {
|
|
111
|
+
this.#nextEmitAtMs = Date.now() + 10;
|
|
112
|
+
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.#flushOne(chunkSamples);
|
|
116
|
+
this.#nextEmitAtMs += chunkIntervalMs;
|
|
117
|
+
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
118
|
+
}, delayMs);
|
|
119
|
+
};
|
|
120
|
+
#flushOne = (chunkSamples) => {
|
|
121
|
+
let nextChunk = this.#queue.shift();
|
|
122
|
+
if (!nextChunk) {
|
|
123
|
+
nextChunk = new Float32Array(chunkSamples);
|
|
124
|
+
this.underflowChunks += 1;
|
|
125
|
+
}
|
|
126
|
+
this.chunksEmitted += 1;
|
|
127
|
+
this.onChunk(nextChunk);
|
|
128
|
+
if (this.#proc?.stdout.isPaused() && this.#queue.length <= MAX_QUEUED_CHUNKS / 4) {
|
|
129
|
+
this.#proc.stdout.resume();
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import { randomBytes, createHmac } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
import { WasmEngine } from "./wasm-engine.js";
|
|
5
|
+
import { RelayRtcTransport } from "./relay-transport.js";
|
|
6
|
+
import { SignalingBridge } from "./signaling.js";
|
|
7
|
+
import { AudioFeeder } from "./audio-feeder.js";
|
|
8
|
+
import { CallState } from "./types.js";
|
|
9
|
+
|
|
10
|
+
export { CallState } from "./types.js";
|
|
11
|
+
|
|
12
|
+
const SHA256_LEN = 32;
|
|
13
|
+
|
|
14
|
+
const toBareJid = (jid) => {
|
|
15
|
+
if (!jid) return jid;
|
|
16
|
+
const at = jid.indexOf("@");
|
|
17
|
+
if (at < 0) return jid;
|
|
18
|
+
const user = jid.slice(0, at).split(":")[0];
|
|
19
|
+
return `${user}@${jid.slice(at + 1)}`;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const computeHkdf = (key, salt, info, length) => {
|
|
23
|
+
const effectiveSalt = salt && salt.length > 0 ? Buffer.from(salt) : Buffer.alloc(SHA256_LEN, 0);
|
|
24
|
+
const prk = createHmac("sha256", effectiveSalt).update(key).digest();
|
|
25
|
+
const blocks = Math.ceil(length / SHA256_LEN);
|
|
26
|
+
const okm = Buffer.alloc(blocks * SHA256_LEN);
|
|
27
|
+
let prev = Buffer.alloc(0);
|
|
28
|
+
for (let i = 1; i <= blocks; i += 1) {
|
|
29
|
+
prev = createHmac("sha256", prk)
|
|
30
|
+
.update(prev)
|
|
31
|
+
.update(info)
|
|
32
|
+
.update(Buffer.from([i]))
|
|
33
|
+
.digest();
|
|
34
|
+
prev.copy(okm, (i - 1) * SHA256_LEN);
|
|
35
|
+
}
|
|
36
|
+
return new Uint8Array(okm.buffer, okm.byteOffset, length);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const computeHmacSha256 = (data, key) => {
|
|
40
|
+
const result = createHmac("sha256", Buffer.from(key)).update(data).digest();
|
|
41
|
+
return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const isCallReceiptNode = (node) => {
|
|
45
|
+
if (node?.tag !== "receipt") return false;
|
|
46
|
+
const child = Array.isArray(node.content) ? node.content[0] : null;
|
|
47
|
+
return !!(child?.attrs?.["call-id"] || child?.attrs?.call_id);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export class ActiveCall extends EventEmitter {
|
|
51
|
+
#state = CallState.Idle;
|
|
52
|
+
#endResolver;
|
|
53
|
+
#endPromise;
|
|
54
|
+
#endTimer = null;
|
|
55
|
+
#ended = false;
|
|
56
|
+
_audioSource = "silence";
|
|
57
|
+
|
|
58
|
+
constructor(callId, engine, durationMs) {
|
|
59
|
+
super();
|
|
60
|
+
this.callId = callId;
|
|
61
|
+
this.engine = engine;
|
|
62
|
+
this.#endPromise = new Promise((res) => { this.#endResolver = res; });
|
|
63
|
+
if (durationMs > 0) {
|
|
64
|
+
this.#endTimer = setTimeout(() => this.end(), durationMs);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get state() { return this.#state; }
|
|
69
|
+
|
|
70
|
+
end = () => {
|
|
71
|
+
if (this.#ended) return;
|
|
72
|
+
this.#ended = true;
|
|
73
|
+
if (this.#endTimer) { clearTimeout(this.#endTimer); this.#endTimer = null; }
|
|
74
|
+
try { this.engine.endCall(0, true); } catch {}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
mute = (muted) => {
|
|
78
|
+
try { this.engine.setMute(muted); } catch {}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
waitForEnd = () => this.#endPromise;
|
|
82
|
+
|
|
83
|
+
_updateState = (state) => {
|
|
84
|
+
this.#state = state;
|
|
85
|
+
if (state === CallState.PreacceptReceived) this.emit("ringing");
|
|
86
|
+
else if (state === CallState.Active) this.emit("connected");
|
|
87
|
+
else if (state === CallState.Idle || state === CallState.Ending) {
|
|
88
|
+
this._forceEnd("ended");
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
_emitAudio = (pcm) => { this.emit("audio", pcm); };
|
|
93
|
+
|
|
94
|
+
_forceEnd = (reason) => {
|
|
95
|
+
if (this.#ended) return;
|
|
96
|
+
this.#ended = true;
|
|
97
|
+
if (this.#endTimer) { clearTimeout(this.#endTimer); this.#endTimer = null; }
|
|
98
|
+
this.emit("ended", reason);
|
|
99
|
+
this.#endResolver(reason);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class VoipClient {
|
|
104
|
+
#config;
|
|
105
|
+
#engine = null;
|
|
106
|
+
#relay = null;
|
|
107
|
+
#signaling = null;
|
|
108
|
+
#sock = null;
|
|
109
|
+
#activeCall = null;
|
|
110
|
+
#capturePtr = 0;
|
|
111
|
+
#captureChunkBytes = 0;
|
|
112
|
+
#captureSampleRate = 16000;
|
|
113
|
+
#captureChannels = 1;
|
|
114
|
+
#captureFramesPerChunk = 320;
|
|
115
|
+
#feeder = null;
|
|
116
|
+
|
|
117
|
+
constructor(config = {}) {
|
|
118
|
+
this.#config = config;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
connectWithSocket = async (existingSock) => {
|
|
122
|
+
this.#sock = existingSock;
|
|
123
|
+
await this.#initVoipStack();
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
#initVoipStack = async () => {
|
|
127
|
+
this.#signaling = new SignalingBridge({ sock: this.#sock });
|
|
128
|
+
await this.#signaling.init();
|
|
129
|
+
|
|
130
|
+
this.#relay = new RelayRtcTransport({
|
|
131
|
+
onTransportMessage: (data, ip, port) => this.#engine?.handleOnTransportMessage(data, ip, port),
|
|
132
|
+
onIceRtt: (rttMs, ip, port) => this.#engine?.updateIceRtt(rttMs, ip, port),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
this.#engine = new WasmEngine({
|
|
136
|
+
resourcesPath: this.#config.resourcesPath,
|
|
137
|
+
callbacks: {
|
|
138
|
+
onSignalingXmpp: (peerJid, callId, xmlPayload) =>
|
|
139
|
+
this.#signaling.sendSignaling(peerJid, callId, xmlPayload),
|
|
140
|
+
onCallEvent: (eventType, eventData) => this.#handleCallEvent(eventType, eventData),
|
|
141
|
+
sendDataToRelay: (data, ip, port) => this.#relay.send(data, ip, port),
|
|
142
|
+
onAudioCaptureInit: (config) => this.#handleAudioCaptureInit(config),
|
|
143
|
+
onAudioCaptureStart: () => this.#handleAudioCaptureStart(),
|
|
144
|
+
onAudioCaptureStop: () => this.#handleAudioCaptureStop(),
|
|
145
|
+
onAudioPlaybackData: (audioData) => this.#activeCall?._emitAudio(audioData),
|
|
146
|
+
cryptoHkdf: computeHkdf,
|
|
147
|
+
hmacSha256: computeHmacSha256,
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await this.#engine.initialize();
|
|
152
|
+
this.#signaling.attachEngine(this.#engine);
|
|
153
|
+
|
|
154
|
+
const selfPnJid = this.#sock.authState.creds.me?.id;
|
|
155
|
+
const selfLidJid = this.#sock.authState.creds.me?.lid;
|
|
156
|
+
this.#engine.initVoipStack(selfPnJid, toBareJid(selfPnJid), selfLidJid);
|
|
157
|
+
await this.#engine.waitForVoipStackReady();
|
|
158
|
+
try { this.#engine.updateNetworkMedium(2, 0); } catch {}
|
|
159
|
+
|
|
160
|
+
this.#sock.ws.on("CB:call", (node) => {
|
|
161
|
+
this.#signaling.processIncomingCall(node, this.#engine, this.#activeCall?.callId ?? "");
|
|
162
|
+
});
|
|
163
|
+
this.#sock.ws.on("CB:receipt", (node) => {
|
|
164
|
+
if (!isCallReceiptNode(node)) return;
|
|
165
|
+
this.#signaling.processIncomingReceipt(node, this.#engine, this.#activeCall?.callId ?? "");
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
call = async (phoneNumber, opts = {}) => {
|
|
170
|
+
if (!this.#engine || !this.#signaling) throw new Error("Not connected. Call connectWithSocket() first.");
|
|
171
|
+
if (this.#activeCall) throw new Error("A call is already active.");
|
|
172
|
+
|
|
173
|
+
const targetNumber = phoneNumber.replace(/\D/g, "");
|
|
174
|
+
const targetPnJid = `${targetNumber}@s.whatsapp.net`;
|
|
175
|
+
const durationMs = opts.durationMs ?? 120_000;
|
|
176
|
+
const audioSource = opts.audioSource ?? "silence";
|
|
177
|
+
|
|
178
|
+
const peerLid = await this.#signaling.resolveLid(targetPnJid);
|
|
179
|
+
if (!peerLid) throw new Error(`Could not resolve LID for ${targetPnJid}`);
|
|
180
|
+
|
|
181
|
+
for (const jid of [targetPnJid, peerLid]) {
|
|
182
|
+
try { await this.#sock.presenceSubscribe(jid); } catch {}
|
|
183
|
+
}
|
|
184
|
+
await new Promise((r) => setTimeout(r, 750));
|
|
185
|
+
|
|
186
|
+
const peerDeviceJids = await this.#signaling.discoverPeerDevices(peerLid);
|
|
187
|
+
const deviceList = peerDeviceJids.length ? peerDeviceJids : [toBareJid(peerLid)];
|
|
188
|
+
|
|
189
|
+
await this.#signaling.ensureSessionsForPeers(deviceList);
|
|
190
|
+
|
|
191
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
192
|
+
await this.#signaling.issueTcToken(peerLid);
|
|
193
|
+
const tcToken = await this.#signaling.ensureTcToken(peerLid, targetPnJid);
|
|
194
|
+
|
|
195
|
+
const callId = ("00" + randomBytes(16).toString("hex").slice(2)).toUpperCase();
|
|
196
|
+
|
|
197
|
+
const call = new ActiveCall(callId, this.#engine, durationMs);
|
|
198
|
+
call._audioSource = audioSource;
|
|
199
|
+
this.#activeCall = call;
|
|
200
|
+
|
|
201
|
+
this.#engine.startCall({
|
|
202
|
+
peerJid: peerLid,
|
|
203
|
+
peerPn: targetPnJid,
|
|
204
|
+
peerList: deviceList,
|
|
205
|
+
callId,
|
|
206
|
+
isVideo: false,
|
|
207
|
+
isLidCall: true,
|
|
208
|
+
isFromDialer: false,
|
|
209
|
+
extraData: tcToken,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
return call;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
disconnect = () => {
|
|
216
|
+
this.#activeCall?._forceEnd("disconnect");
|
|
217
|
+
this.#activeCall = null;
|
|
218
|
+
this.#relay?.closeAll();
|
|
219
|
+
this.#engine?.destroy();
|
|
220
|
+
this.#engine = null;
|
|
221
|
+
this.#relay = null;
|
|
222
|
+
this.#signaling = null;
|
|
223
|
+
this.#sock = null;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
#handleCallEvent = (eventType, eventData) => {
|
|
227
|
+
if (eventType === 16 && eventData) {
|
|
228
|
+
try {
|
|
229
|
+
const parsed = JSON.parse(eventData);
|
|
230
|
+
const info = parsed.call_info ?? parsed.callInfo ?? {};
|
|
231
|
+
const callState = Number(info.call_state ?? info.callState ?? 0);
|
|
232
|
+
this.#activeCall?._updateState(callState);
|
|
233
|
+
} catch {}
|
|
234
|
+
} else if (eventType === 156 && eventData) {
|
|
235
|
+
try {
|
|
236
|
+
const update = JSON.parse(eventData);
|
|
237
|
+
this.#relay?.updateRelayList(update);
|
|
238
|
+
} catch {}
|
|
239
|
+
} else if (eventType === 2) {
|
|
240
|
+
this.#activeCall?._forceEnd("remote_end");
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
#handleAudioCaptureInit = (config) => {
|
|
245
|
+
if (!this.#engine) return;
|
|
246
|
+
this.#captureSampleRate = config.sampleRate || 16000;
|
|
247
|
+
this.#captureChannels = config.channels || 1;
|
|
248
|
+
this.#captureFramesPerChunk = config.framesPerChunk || 320;
|
|
249
|
+
const chunkSamples = this.#captureFramesPerChunk * this.#captureChannels;
|
|
250
|
+
this.#captureChunkBytes = chunkSamples * Float32Array.BYTES_PER_ELEMENT;
|
|
251
|
+
this.#capturePtr = this.#engine.malloc(this.#captureChunkBytes);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
#handleAudioCaptureStart = () => {
|
|
255
|
+
if (!this.#engine || !this.#capturePtr) return;
|
|
256
|
+
const audioSource = this.#activeCall?._audioSource ?? "silence";
|
|
257
|
+
this.#feeder = new AudioFeeder(
|
|
258
|
+
this.#captureSampleRate,
|
|
259
|
+
this.#captureChannels,
|
|
260
|
+
this.#captureFramesPerChunk,
|
|
261
|
+
(chunk) => {
|
|
262
|
+
if (this.#engine && this.#capturePtr) this.#engine.sendAudioData(chunk, this.#capturePtr);
|
|
263
|
+
},
|
|
264
|
+
audioSource,
|
|
265
|
+
);
|
|
266
|
+
this.#feeder.start();
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
#handleAudioCaptureStop = () => {
|
|
270
|
+
this.#feeder?.stop();
|
|
271
|
+
this.#feeder = null;
|
|
272
|
+
if (this.#engine && this.#capturePtr) {
|
|
273
|
+
try { this.#engine.free(this.#capturePtr); } catch {}
|
|
274
|
+
this.#capturePtr = 0;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
type RelayAddress = {
|
|
2
|
+
protocol: number;
|
|
3
|
+
ipv4?: string;
|
|
4
|
+
ipv6?: string;
|
|
5
|
+
port?: number;
|
|
6
|
+
port_v6?: number;
|
|
7
|
+
};
|
|
8
|
+
type RelayDescriptor = {
|
|
9
|
+
relay_id: number;
|
|
10
|
+
relay_name: string;
|
|
11
|
+
token_id: number;
|
|
12
|
+
auth_token_id?: number;
|
|
13
|
+
addresses: RelayAddress[];
|
|
14
|
+
};
|
|
15
|
+
export type RelayListUpdatePayload = {
|
|
16
|
+
relay_key: string;
|
|
17
|
+
relay_tokens: string[];
|
|
18
|
+
auth_tokens?: string[];
|
|
19
|
+
enable_edgeray_dtls_active_mode?: boolean;
|
|
20
|
+
relays: RelayDescriptor[];
|
|
21
|
+
};
|
|
22
|
+
export type RelayTransportStats = {
|
|
23
|
+
sentPackets: number;
|
|
24
|
+
receivedPackets: number;
|
|
25
|
+
sentBytes: number;
|
|
26
|
+
receivedBytes: number;
|
|
27
|
+
droppedPackets: number;
|
|
28
|
+
openConnections: number;
|
|
29
|
+
};
|
|
30
|
+
export type RelayTransportConfig = {
|
|
31
|
+
onTransportMessage: (data: Uint8Array, ip: string, port: number) => void;
|
|
32
|
+
onIceRtt?: (rttMs: number, ip: string, port: number) => void;
|
|
33
|
+
};
|
|
34
|
+
export declare class RelayRtcTransport {
|
|
35
|
+
#private;
|
|
36
|
+
private readonly config;
|
|
37
|
+
constructor(config: RelayTransportConfig);
|
|
38
|
+
updateRelayList: (update: RelayListUpdatePayload) => void;
|
|
39
|
+
send: (packet: Uint8Array | Buffer, ip: string, port: number) => number;
|
|
40
|
+
getStats: () => RelayTransportStats;
|
|
41
|
+
closeAll: () => Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
export {};
|