kage-baileys 3.0.6 → 3.2.3
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/Example/connection.cjs +4 -0
- package/README.MD +4726 -48
- package/WAProto/WAProto.proto +3395 -415
- package/WAProto/compat.cjs +12 -0
- package/WAProto/index.d.ts +47104 -19888
- package/WAProto/index.js +131979 -57361
- package/lib/Addon/http-error.js +43 -0
- package/lib/Addon/index.js +32 -0
- package/lib/Addon/keyed-db.js +250 -0
- package/lib/Addon/link-preview.js +82 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +110 -0
- package/lib/Defaults/constants.js +156 -0
- package/lib/Defaults/history.js +46 -0
- package/lib/Defaults/index.js +230 -0
- package/lib/Defaults/media.js +104 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +44 -0
- package/lib/Extensions/business-advanced.js +382 -0
- package/lib/Extensions/business.js +159 -0
- package/lib/Extensions/chats-advanced.js +1106 -0
- package/lib/Extensions/chats.js +349 -0
- package/lib/Extensions/communities-advanced.js +430 -0
- package/lib/Extensions/community.js +94 -0
- package/lib/Extensions/groups-advanced.js +343 -0
- package/lib/Extensions/index.js +30 -0
- package/lib/Extensions/messages-recv-advanced.js +1690 -0
- package/lib/Extensions/messages-send-advanced.js +622 -0
- package/lib/Extensions/messages-send.js +1380 -0
- package/lib/Extensions/newsletter-advanced.js +228 -0
- package/lib/Extensions/newsletter.js +72 -0
- package/lib/Extensions/socket.js +389 -0
- package/lib/Modded/message_builder.js +2084 -4
- package/lib/Signal/Group/ciphertext-message.js +54 -0
- package/lib/Signal/Group/group-session-builder.js +124 -0
- package/lib/Signal/Group/group_cipher.js +216 -0
- package/lib/Signal/Group/index.js +124 -0
- package/lib/Signal/Group/keyhelper.js +108 -0
- package/lib/Signal/Group/sender-chain-key.js +83 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +157 -0
- package/lib/Signal/Group/sender-key-message.js +166 -0
- package/lib/Signal/Group/sender-key-name.js +130 -0
- package/lib/Signal/Group/sender-key-record.js +113 -0
- package/lib/Signal/Group/sender-key-state.js +208 -0
- package/lib/Signal/Group/sender-message-key.js +82 -0
- package/lib/Signal/libsignal.js +935 -0
- package/lib/Signal/lid-mapping.js +545 -1
- package/lib/Socket/Client/index.js +60 -0
- package/lib/Socket/Client/types.js +51 -0
- package/lib/Socket/Client/websocket.js +152 -0
- package/lib/Socket/business.js +831 -0
- package/lib/Socket/chats.js +2535 -49
- package/lib/Socket/communities.js +450 -1
- package/lib/Socket/community.js +1022 -0
- package/lib/Socket/dugong.js +1 -1
- package/lib/Socket/groups.js +747 -0
- package/lib/Socket/index.js +60 -2
- package/lib/Socket/messages-recv.js +3450 -5
- package/lib/Socket/messages-send.js +3123 -0
- package/lib/Socket/mex.js +126 -1
- package/lib/Socket/newsletter.js +465 -0
- package/lib/Socket/socket.js +2217 -1
- package/lib/Store/index.js +79 -0
- package/lib/Store/make-cache-manager-store.js +186 -0
- package/lib/Store/make-in-memory-store.js +878 -0
- package/lib/Store/make-ordered-dictionary.js +187 -0
- package/lib/Store/object-repository.js +78 -0
- package/lib/Types/Auth.js +25 -0
- package/lib/Types/Bussines.js +25 -1
- package/lib/Types/Call.js +25 -0
- package/lib/Types/Chat.js +39 -0
- package/lib/Types/Contact.js +25 -0
- package/lib/Types/Events.js +25 -0
- package/lib/Types/GroupMetadata.js +25 -0
- package/lib/Types/Label.js +76 -0
- package/lib/Types/LabelAssociation.js +40 -0
- package/lib/Types/Message.js +57 -0
- package/lib/Types/Mex.js +56 -1
- package/lib/Types/MexUpdates.js +46 -0
- package/lib/Types/Newsletter.js +56 -0
- package/lib/Types/Product.js +25 -0
- package/lib/Types/RichType.js +23 -0
- package/lib/Types/Signal.js +25 -0
- package/lib/Types/Socket.js +25 -0
- package/lib/Types/State.js +92 -0
- package/lib/Types/USync.js +25 -0
- package/lib/Types/index.js +133 -0
- package/lib/Utils/auth-utils.js +703 -0
- package/lib/Utils/automation-tools.js +51 -16
- package/lib/Utils/baileys-event-stream.js +146 -0
- package/lib/Utils/browser-utils.js +95 -0
- package/lib/Utils/business.js +525 -0
- package/lib/Utils/chat-utils.js +1861 -0
- package/lib/Utils/companion-reg-client-utils.js +55 -1
- package/lib/Utils/crypto.js +355 -0
- package/lib/Utils/decode-wa-message.js +709 -0
- package/lib/Utils/event-buffer.js +1323 -8
- package/lib/Utils/generics.js +945 -1
- package/lib/Utils/history.js +266 -0
- package/lib/Utils/identity-change-handler.js +62 -1
- package/lib/Utils/index.js +114 -0
- package/lib/Utils/link-preview.js +179 -0
- package/lib/Utils/logger.js +42 -0
- package/lib/Utils/lt-hash.js +73 -0
- package/lib/Utils/make-mutex.js +104 -0
- package/lib/Utils/message-inspector.js +110 -0
- package/lib/Utils/message-retry-manager.js +507 -2
- package/lib/Utils/messages-media.js +1931 -1
- package/lib/Utils/messages.js +3748 -0
- package/lib/Utils/noise-handler.js +423 -0
- package/lib/Utils/offline-node-processor.js +53 -1
- package/lib/Utils/pre-key-manager.js +255 -1
- package/lib/Utils/process-message.js +1592 -0
- package/lib/Utils/reporting-utils.js +272 -1
- package/lib/Utils/rich-message-utils.js +377 -0
- package/lib/Utils/rich-messages.js +1 -1
- package/lib/Utils/signal.js +437 -0
- package/lib/Utils/stanza-ack.js +51 -1
- package/lib/Utils/status-refresh.js +37 -0
- package/lib/Utils/status-tools.js +4 -2
- package/lib/Utils/sync-action-utils.js +63 -1
- package/lib/Utils/tc-token-utils.js +185 -1
- package/lib/Utils/terminal-banner.js +31 -0
- package/lib/Utils/use-multi-file-auth-state.js +271 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +142 -0
- package/lib/Utils/validate-connection.js +497 -1
- package/lib/Utils/view-once-compat.js +13 -5
- package/lib/VoIP/audio-feeder.js +150 -0
- package/lib/VoIP/index.js +289 -0
- package/lib/VoIP/relay-transport.js +608 -0
- package/lib/VoIP/signaling.js +583 -0
- package/lib/VoIP/types.js +30 -0
- package/lib/VoIP/wasm-engine.js +1261 -2
- package/lib/VoIP/worker-bootstrap.js +1090 -2
- package/lib/WABinary/constants.js +2805 -0
- package/lib/WABinary/decode.js +649 -2
- package/lib/WABinary/encode.js +564 -2
- package/lib/WABinary/generic-utils.js +401 -0
- package/lib/WABinary/index.js +82 -0
- package/lib/WABinary/jid-utils.js +290 -0
- package/lib/WABinary/types.js +25 -0
- package/lib/WAM/BinaryInfo.js +50 -0
- package/lib/WAM/constants.js +45747 -0
- package/lib/WAM/encode.js +393 -0
- package/lib/WAM/index.js +78 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +135 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +110 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +142 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +85 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +69 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +107 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +38 -1
- package/lib/WAUSync/Protocols/index.js +83 -0
- package/lib/WAUSync/USyncQuery.js +241 -2
- package/lib/WAUSync/USyncUser.js +86 -0
- package/lib/WAUSync/index.js +78 -0
- package/lib/assets/wasm/loader.js +1875 -1
- package/lib/assets/wasm/worker-modules.js +11047 -1
- package/lib/index.d.ts +126 -0
- package/lib/index.js +53 -21
- package/lib/index.mjs +437 -0
- package/licenses/upstream-MIT.txt +22 -0
- package/maintenance/api-coverage.json +1642 -0
- package/maintenance/archivos-sha256.json +207 -0
- package/maintenance/consumer-validation.log +3 -0
- package/maintenance/dependency-audit.json +4 -4
- package/maintenance/extension-inventory.json +57 -0
- package/maintenance/protocol-update.json +1101 -0
- package/maintenance/protocol-wire-audit.json +4 -0
- package/maintenance/reference-socket-surface.json +209 -0
- package/maintenance/resumen-validacion.json +50 -0
- package/maintenance/validation.log +2725 -0
- package/maintenance/verification.json +566 -0
- package/package.json +107 -9
- package/scripts/check-imports.cjs +10 -0
- package/scripts/check-package.cjs +40 -0
- package/scripts/check-syntax.cjs +1 -1
- package/scripts/generate-protobuf.cjs +6 -1
- package/scripts/generate-public-types.cjs +9 -0
- package/scripts/kage-base64-fix.cjs +45 -0
- package/scripts/maintenance.cjs +10 -0
- package/test/advanced.test.cjs +24 -0
- package/test/base64-fix.test.cjs +49 -0
- package/test/maintenance.test.cjs +1 -1
- package/test/npm-publication.test.cjs +141 -0
- package/test/recepcion-reparada.test.cjs +98 -0
- package/test/resilience-binary.test.cjs +69 -0
- package/test/resilience-transport.test.cjs +34 -0
- package/test/types.test.ts +5 -0
- package/test/upgrade.test.cjs +104 -0
- package/tsconfig.json +20 -0
- package/WAProto/p.html +0 -1
- package/lib/KageExtra/Modded/message_builder.js +0 -2330
- package/lib/KageExtra/VoIP/audio-feeder.js +0 -136
- package/lib/KageExtra/VoIP/index.js +0 -282
- package/lib/KageExtra/VoIP/relay-transport.js +0 -559
- package/lib/KageExtra/VoIP/signaling.js +0 -603
- package/lib/KageExtra/VoIP/types.js +0 -17
- package/lib/KageExtra/VoIP/wasm-engine.js +0 -1214
- package/lib/KageExtra/VoIP/worker-bootstrap.js +0 -1042
- package/lib/KageExtra/assets/wasm/loader.js +0 -5
- package/lib/KageExtra/assets/wasm/whatsapp.wasm +0 -0
- package/lib/KageExtra/assets/wasm/worker-modules.js +0 -273
- package/lib/KageExtra/index.js +0 -3
- package/lib/KageExtra/package.json +0 -1
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -53
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -24
- package/maintenance/revision-full-check.txt +0 -1097
- package/maintenance/revision-library-tests.tap +0 -1088
- package/maintenance/revision-protobuf.txt +0 -5
- package/maintenance/revision-syntax.txt +0 -5
- package/maintenance/schema-coverage.json +0 -23
- package/maintenance/schema-merge.json +0 -30
- package/maintenance/validation-node20.txt +0 -191
- package/maintenance/validation-node22.txt +0 -237
|
@@ -1,136 +0,0 @@
|
|
|
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("error", (err) => {
|
|
72
|
-
process.stderr.write(`[AudioFeeder] ffmpeg process error: ${err.message}\n`);
|
|
73
|
-
this.#proc = null;
|
|
74
|
-
});
|
|
75
|
-
this.#proc.on("exit", (code) => {
|
|
76
|
-
if (code !== 0 && code !== null) {
|
|
77
|
-
process.stderr.write(`[AudioFeeder] ffmpeg exited with code=${code}\n`);
|
|
78
|
-
}
|
|
79
|
-
this.#proc = null;
|
|
80
|
-
});
|
|
81
|
-
this.#nextEmitAtMs = 0;
|
|
82
|
-
this.#warmupUntilMs = Date.now() + DEFAULT_WARMUP_MS;
|
|
83
|
-
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
84
|
-
};
|
|
85
|
-
stop = () => {
|
|
86
|
-
if (this.#emitTimer) {
|
|
87
|
-
clearTimeout(this.#emitTimer);
|
|
88
|
-
this.#emitTimer = null;
|
|
89
|
-
}
|
|
90
|
-
this.#proc?.kill("SIGTERM");
|
|
91
|
-
this.#proc = null;
|
|
92
|
-
this.#pending = Buffer.alloc(0);
|
|
93
|
-
this.#queue = [];
|
|
94
|
-
this.#warmupUntilMs = 0;
|
|
95
|
-
};
|
|
96
|
-
#resolveInputArgs = () => {
|
|
97
|
-
if (!this.source || this.source === "silence") {
|
|
98
|
-
return ["-f", "lavfi", "-i", `aevalsrc=0:d=3600:s=${this.sampleRate}`];
|
|
99
|
-
}
|
|
100
|
-
if (this.source.startsWith("lavfi:")) {
|
|
101
|
-
return ["-f", "lavfi", "-i", this.source.slice("lavfi:".length)];
|
|
102
|
-
}
|
|
103
|
-
return ["-i", this.source];
|
|
104
|
-
};
|
|
105
|
-
#scheduleNext = (chunkSamples, chunkIntervalMs) => {
|
|
106
|
-
if (!this.#proc)
|
|
107
|
-
return;
|
|
108
|
-
const now = Date.now();
|
|
109
|
-
if (this.#nextEmitAtMs === 0)
|
|
110
|
-
this.#nextEmitAtMs = now;
|
|
111
|
-
const delayMs = Math.max(0, this.#nextEmitAtMs - now);
|
|
112
|
-
this.#emitTimer = setTimeout(() => {
|
|
113
|
-
this.#emitTimer = null;
|
|
114
|
-
if (this.#queue.length < LOW_WATERMARK_CHUNKS && Date.now() < this.#warmupUntilMs) {
|
|
115
|
-
this.#nextEmitAtMs = Date.now() + 10;
|
|
116
|
-
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
this.#flushOne(chunkSamples);
|
|
120
|
-
this.#nextEmitAtMs += chunkIntervalMs;
|
|
121
|
-
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
122
|
-
}, delayMs);
|
|
123
|
-
};
|
|
124
|
-
#flushOne = (chunkSamples) => {
|
|
125
|
-
let nextChunk = this.#queue.shift();
|
|
126
|
-
if (!nextChunk) {
|
|
127
|
-
nextChunk = new Float32Array(chunkSamples);
|
|
128
|
-
this.underflowChunks += 1;
|
|
129
|
-
}
|
|
130
|
-
this.chunksEmitted += 1;
|
|
131
|
-
this.onChunk(nextChunk);
|
|
132
|
-
if (this.#proc?.stdout.isPaused() && this.#queue.length <= MAX_QUEUED_CHUNKS / 4) {
|
|
133
|
-
this.#proc.stdout.resume();
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
}
|
|
@@ -1,282 +0,0 @@
|
|
|
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
|
-
#hasBeenActive = false;
|
|
57
|
-
_audioSource = "silence";
|
|
58
|
-
|
|
59
|
-
constructor(callId, engine, durationMs) {
|
|
60
|
-
super();
|
|
61
|
-
this.callId = callId;
|
|
62
|
-
this.engine = engine;
|
|
63
|
-
this.#endPromise = new Promise((res) => { this.#endResolver = res; });
|
|
64
|
-
if (durationMs > 0) {
|
|
65
|
-
this.#endTimer = setTimeout(() => this.end(), durationMs);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
get state() { return this.#state; }
|
|
70
|
-
|
|
71
|
-
end = () => {
|
|
72
|
-
if (this.#ended) return;
|
|
73
|
-
this.#ended = true;
|
|
74
|
-
if (this.#endTimer) { clearTimeout(this.#endTimer); this.#endTimer = null; }
|
|
75
|
-
try { this.engine.endCall(0, true); } catch {}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
mute = (muted) => {
|
|
79
|
-
try { this.engine.setMute(muted); } catch {}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
waitForEnd = () => this.#endPromise;
|
|
83
|
-
|
|
84
|
-
_updateState = (state) => {
|
|
85
|
-
this.#state = state;
|
|
86
|
-
if (state === CallState.Calling || state === CallState.PreacceptReceived || state === CallState.Active) {
|
|
87
|
-
this.#hasBeenActive = true;
|
|
88
|
-
}
|
|
89
|
-
if (state === CallState.PreacceptReceived) this.emit("ringing");
|
|
90
|
-
else if (state === CallState.Active) this.emit("connected");
|
|
91
|
-
else if (this.#hasBeenActive && (state === CallState.Idle || state === CallState.Ending)) {
|
|
92
|
-
this._forceEnd("ended");
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
_emitAudio = (pcm) => { this.emit("audio", pcm); };
|
|
97
|
-
|
|
98
|
-
_forceEnd = (reason) => {
|
|
99
|
-
if (this.#ended) return;
|
|
100
|
-
this.#ended = true;
|
|
101
|
-
if (this.#endTimer) { clearTimeout(this.#endTimer); this.#endTimer = null; }
|
|
102
|
-
this.emit("ended", reason);
|
|
103
|
-
this.#endResolver(reason);
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export class VoipClient {
|
|
108
|
-
#config;
|
|
109
|
-
#engine = null;
|
|
110
|
-
#relay = null;
|
|
111
|
-
#signaling = null;
|
|
112
|
-
#sock = null;
|
|
113
|
-
#activeCall = null;
|
|
114
|
-
#capturePtr = 0;
|
|
115
|
-
#captureChunkBytes = 0;
|
|
116
|
-
#captureSampleRate = 16000;
|
|
117
|
-
#captureChannels = 1;
|
|
118
|
-
#captureFramesPerChunk = 320;
|
|
119
|
-
#feeder = null;
|
|
120
|
-
|
|
121
|
-
constructor(config = {}) {
|
|
122
|
-
this.#config = config;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
connectWithSocket = async (existingSock) => {
|
|
126
|
-
this.#sock = existingSock;
|
|
127
|
-
await this.#initVoipStack();
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
#initVoipStack = async () => {
|
|
131
|
-
this.#signaling = new SignalingBridge({ sock: this.#sock });
|
|
132
|
-
await this.#signaling.init();
|
|
133
|
-
|
|
134
|
-
this.#relay = new RelayRtcTransport({
|
|
135
|
-
onTransportMessage: (data, ip, port) => this.#engine?.handleOnTransportMessage(data, ip, port),
|
|
136
|
-
onIceRtt: (rttMs, ip, port) => this.#engine?.updateIceRtt(rttMs, ip, port),
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
this.#engine = new WasmEngine({
|
|
140
|
-
resourcesPath: this.#config.resourcesPath,
|
|
141
|
-
callbacks: {
|
|
142
|
-
onSignalingXmpp: (peerJid, callId, xmlPayload) =>
|
|
143
|
-
this.#signaling.sendSignaling(peerJid, callId, xmlPayload),
|
|
144
|
-
onCallEvent: (eventType, eventData) => this.#handleCallEvent(eventType, eventData),
|
|
145
|
-
sendDataToRelay: (data, ip, port) => this.#relay.send(data, ip, port),
|
|
146
|
-
onAudioCaptureInit: (config) => this.#handleAudioCaptureInit(config),
|
|
147
|
-
onAudioCaptureStart: () => this.#handleAudioCaptureStart(),
|
|
148
|
-
onAudioCaptureStop: () => this.#handleAudioCaptureStop(),
|
|
149
|
-
onAudioPlaybackData: (audioData) => this.#activeCall?._emitAudio(audioData),
|
|
150
|
-
cryptoHkdf: computeHkdf,
|
|
151
|
-
hmacSha256: computeHmacSha256,
|
|
152
|
-
},
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
await this.#engine.initialize();
|
|
156
|
-
this.#signaling.attachEngine(this.#engine);
|
|
157
|
-
|
|
158
|
-
const selfPnJid = this.#sock.authState.creds.me?.id;
|
|
159
|
-
const selfLidJid = this.#sock.authState.creds.me?.lid;
|
|
160
|
-
this.#engine.initVoipStack(selfPnJid, toBareJid(selfPnJid), selfLidJid);
|
|
161
|
-
await this.#engine.waitForVoipStackReady();
|
|
162
|
-
try { this.#engine.updateNetworkMedium(2, 0); } catch {}
|
|
163
|
-
|
|
164
|
-
this.#sock.ws.on("CB:call", (node) => {
|
|
165
|
-
this.#signaling.processIncomingCall(node, this.#engine, this.#activeCall?.callId ?? "");
|
|
166
|
-
});
|
|
167
|
-
this.#sock.ws.on("CB:receipt", (node) => {
|
|
168
|
-
if (!isCallReceiptNode(node)) return;
|
|
169
|
-
this.#signaling.processIncomingReceipt(node, this.#engine, this.#activeCall?.callId ?? "");
|
|
170
|
-
});
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
call = async (phoneNumber, opts = {}) => {
|
|
174
|
-
if (!this.#engine || !this.#signaling) throw new Error("Not connected. Call connectWithSocket() first.");
|
|
175
|
-
if (this.#activeCall) throw new Error("A call is already active.");
|
|
176
|
-
|
|
177
|
-
const targetNumber = phoneNumber.replace(/\D/g, "");
|
|
178
|
-
const targetPnJid = `${targetNumber}@s.whatsapp.net`;
|
|
179
|
-
const durationMs = opts.durationMs ?? 120_000;
|
|
180
|
-
const audioSource = opts.audioSource ?? "silence";
|
|
181
|
-
|
|
182
|
-
const peerLid = await this.#signaling.resolveLid(targetPnJid);
|
|
183
|
-
if (!peerLid) throw new Error(`Could not resolve LID for ${targetPnJid}`);
|
|
184
|
-
|
|
185
|
-
for (const jid of [targetPnJid, peerLid]) {
|
|
186
|
-
try { await this.#sock.presenceSubscribe(jid); } catch {}
|
|
187
|
-
}
|
|
188
|
-
await new Promise((r) => setTimeout(r, 750));
|
|
189
|
-
|
|
190
|
-
const peerDeviceJids = await this.#signaling.discoverPeerDevices(peerLid);
|
|
191
|
-
const deviceList = peerDeviceJids.length ? peerDeviceJids : [toBareJid(peerLid)];
|
|
192
|
-
|
|
193
|
-
const allSessionTargets = [...new Set([...deviceList, peerLid, toBareJid(peerLid), targetPnJid, toBareJid(targetPnJid)])];
|
|
194
|
-
await this.#signaling.ensureSessionsForPeers(allSessionTargets);
|
|
195
|
-
|
|
196
|
-
await new Promise((r) => setTimeout(r, 500));
|
|
197
|
-
await this.#signaling.issueTcToken(peerLid);
|
|
198
|
-
const tcToken = await this.#signaling.ensureTcToken(peerLid, targetPnJid);
|
|
199
|
-
|
|
200
|
-
const callId = ("00" + randomBytes(16).toString("hex").slice(2)).toUpperCase();
|
|
201
|
-
|
|
202
|
-
const call = new ActiveCall(callId, this.#engine, durationMs);
|
|
203
|
-
call._audioSource = audioSource;
|
|
204
|
-
this.#activeCall = call;
|
|
205
|
-
|
|
206
|
-
this.#engine.startCall({
|
|
207
|
-
peerJid: peerLid,
|
|
208
|
-
peerPn: targetPnJid,
|
|
209
|
-
peerList: deviceList,
|
|
210
|
-
callId,
|
|
211
|
-
isVideo: false,
|
|
212
|
-
isLidCall: true,
|
|
213
|
-
isFromDialer: false,
|
|
214
|
-
extraData: tcToken,
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
return call;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
disconnect = () => {
|
|
221
|
-
this.#activeCall?._forceEnd("disconnect");
|
|
222
|
-
this.#activeCall = null;
|
|
223
|
-
this.#relay?.closeAll();
|
|
224
|
-
this.#engine?.destroy();
|
|
225
|
-
this.#engine = null;
|
|
226
|
-
this.#relay = null;
|
|
227
|
-
this.#signaling = null;
|
|
228
|
-
this.#sock = null;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
#handleCallEvent = (eventType, eventData) => {
|
|
232
|
-
if (eventType === 16 && eventData) {
|
|
233
|
-
try {
|
|
234
|
-
const parsed = JSON.parse(eventData);
|
|
235
|
-
const info = parsed.call_info ?? parsed.callInfo ?? {};
|
|
236
|
-
const callState = Number(info.call_state ?? info.callState ?? 0);
|
|
237
|
-
this.#activeCall?._updateState(callState);
|
|
238
|
-
} catch {}
|
|
239
|
-
} else if (eventType === 156 && eventData) {
|
|
240
|
-
try {
|
|
241
|
-
const update = JSON.parse(eventData);
|
|
242
|
-
this.#relay?.updateRelayList(update);
|
|
243
|
-
} catch {}
|
|
244
|
-
} else if (eventType === 2) {
|
|
245
|
-
this.#activeCall?._forceEnd("remote_end");
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
#handleAudioCaptureInit = (config) => {
|
|
250
|
-
if (!this.#engine) return;
|
|
251
|
-
this.#captureSampleRate = config.sampleRate || 16000;
|
|
252
|
-
this.#captureChannels = config.channels || 1;
|
|
253
|
-
this.#captureFramesPerChunk = config.framesPerChunk || 320;
|
|
254
|
-
const chunkSamples = this.#captureFramesPerChunk * this.#captureChannels;
|
|
255
|
-
this.#captureChunkBytes = chunkSamples * Float32Array.BYTES_PER_ELEMENT;
|
|
256
|
-
this.#capturePtr = this.#engine.malloc(this.#captureChunkBytes);
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
#handleAudioCaptureStart = () => {
|
|
260
|
-
if (!this.#engine || !this.#capturePtr) return;
|
|
261
|
-
const audioSource = this.#activeCall?._audioSource ?? "silence";
|
|
262
|
-
this.#feeder = new AudioFeeder(
|
|
263
|
-
this.#captureSampleRate,
|
|
264
|
-
this.#captureChannels,
|
|
265
|
-
this.#captureFramesPerChunk,
|
|
266
|
-
(chunk) => {
|
|
267
|
-
if (this.#engine && this.#capturePtr) this.#engine.sendAudioData(chunk, this.#capturePtr);
|
|
268
|
-
},
|
|
269
|
-
audioSource,
|
|
270
|
-
);
|
|
271
|
-
this.#feeder.start();
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
#handleAudioCaptureStop = () => {
|
|
275
|
-
this.#feeder?.stop();
|
|
276
|
-
this.#feeder = null;
|
|
277
|
-
if (this.#engine && this.#capturePtr) {
|
|
278
|
-
try { this.#engine.free(this.#capturePtr); } catch {}
|
|
279
|
-
this.#capturePtr = 0;
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
}
|