@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,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audio feeder adapted from baileys-caller (ShellTear, MIT).
|
|
3
|
+
* Supports FFmpeg file/lavfi inputs and live Float32 PCM Readable streams.
|
|
4
|
+
*/
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
|
|
7
|
+
const LOW_WATERMARK_CHUNKS = 16;
|
|
8
|
+
const MAX_QUEUED_CHUNKS = 1024;
|
|
9
|
+
const DEFAULT_WARMUP_MS = 100;
|
|
10
|
+
|
|
11
|
+
export class AudioFeeder {
|
|
12
|
+
sampleRate;
|
|
13
|
+
channels;
|
|
14
|
+
framesPerChunk;
|
|
15
|
+
onChunk;
|
|
16
|
+
source;
|
|
17
|
+
#proc = null;
|
|
18
|
+
#sourceStream = null;
|
|
19
|
+
#streamListeners = null;
|
|
20
|
+
#pending = Buffer.alloc(0);
|
|
21
|
+
#queue = [];
|
|
22
|
+
#emitTimer = null;
|
|
23
|
+
#nextEmitAtMs = 0;
|
|
24
|
+
#chunkIntervalMs = 0;
|
|
25
|
+
#warmupUntilMs = 0;
|
|
26
|
+
#started = false;
|
|
27
|
+
#chunkSamples = 0;
|
|
28
|
+
#chunkBytes = 0;
|
|
29
|
+
|
|
30
|
+
droppedChunks = 0;
|
|
31
|
+
underflowChunks = 0;
|
|
32
|
+
bytesProduced = 0;
|
|
33
|
+
chunksEmitted = 0;
|
|
34
|
+
|
|
35
|
+
constructor(sampleRate, channels, framesPerChunk, onChunk, source = "silence") {
|
|
36
|
+
this.sampleRate = sampleRate;
|
|
37
|
+
this.channels = channels;
|
|
38
|
+
this.framesPerChunk = framesPerChunk;
|
|
39
|
+
this.onChunk = onChunk;
|
|
40
|
+
this.source = source;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
start = () => {
|
|
44
|
+
if (this.#started) return;
|
|
45
|
+
this.#started = true;
|
|
46
|
+
this.#chunkSamples = this.framesPerChunk * this.channels;
|
|
47
|
+
this.#chunkBytes = this.#chunkSamples * Float32Array.BYTES_PER_ELEMENT;
|
|
48
|
+
const chunkIntervalMs = (this.framesPerChunk / this.sampleRate) * 1000;
|
|
49
|
+
this.#chunkIntervalMs = chunkIntervalMs;
|
|
50
|
+
|
|
51
|
+
if (this.source && typeof this.source !== "string") {
|
|
52
|
+
if (typeof this.source.on !== "function") {
|
|
53
|
+
throw new TypeError("audioSource must be a file/lavfi string or a Readable stream of Float32 PCM");
|
|
54
|
+
}
|
|
55
|
+
this.#sourceStream = this.source;
|
|
56
|
+
const onData = (chunk) => this.#ingest(chunk);
|
|
57
|
+
const onError = (error) => {
|
|
58
|
+
process.stderr.write(`[AudioFeeder] PCM input stream error: ${error.message}\n`);
|
|
59
|
+
this.#sourceStream = null;
|
|
60
|
+
};
|
|
61
|
+
const onEnd = () => { this.#sourceStream = null; };
|
|
62
|
+
this.#streamListeners = { stream: this.source, onData, onError, onEnd };
|
|
63
|
+
this.source.on("data", onData);
|
|
64
|
+
this.source.on("error", onError);
|
|
65
|
+
this.source.on("end", onEnd);
|
|
66
|
+
this.#warmupUntilMs = Date.now() + DEFAULT_WARMUP_MS;
|
|
67
|
+
this.#scheduleNext(chunkIntervalMs);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const inputArgs = this.#resolveInputArgs();
|
|
72
|
+
this.#proc = spawn(process.env.X4R1S3_FFMPEG_PATH || "ffmpeg", [
|
|
73
|
+
"-hide_banner", "-loglevel", "error", "-thread_queue_size", "512",
|
|
74
|
+
...inputArgs,
|
|
75
|
+
"-f", "f32le", "-ac", String(this.channels), "-ar", String(this.sampleRate), "pipe:1",
|
|
76
|
+
]);
|
|
77
|
+
this.#proc.on("error", (error) => {
|
|
78
|
+
process.stderr.write(`[AudioFeeder] Unable to start FFmpeg: ${error.message}\n`);
|
|
79
|
+
this.#proc = null;
|
|
80
|
+
});
|
|
81
|
+
this.#proc.stdout.on("data", (chunk) => this.#ingest(chunk));
|
|
82
|
+
this.#proc.stderr.on("data", (chunk) => {
|
|
83
|
+
const message = chunk.toString().trim();
|
|
84
|
+
if (message) process.stderr.write(`[AudioFeeder] ${message}\n`);
|
|
85
|
+
});
|
|
86
|
+
this.#proc.on("exit", (code) => {
|
|
87
|
+
if (code !== 0 && code !== null) {
|
|
88
|
+
process.stderr.write(`[AudioFeeder] ffmpeg exited with code=${code}\n`);
|
|
89
|
+
}
|
|
90
|
+
this.#proc = null;
|
|
91
|
+
});
|
|
92
|
+
this.#warmupUntilMs = Date.now() + DEFAULT_WARMUP_MS;
|
|
93
|
+
this.#scheduleNext(chunkIntervalMs);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Queue interleaved Float32 PCM for an already-running call. */
|
|
97
|
+
enqueuePcm = (pcm) => {
|
|
98
|
+
if (!this.#started) this.start();
|
|
99
|
+
if (!(pcm instanceof Float32Array)) {
|
|
100
|
+
if (ArrayBuffer.isView(pcm)) {
|
|
101
|
+
pcm = new Float32Array(pcm.buffer, pcm.byteOffset, Math.floor(pcm.byteLength / Float32Array.BYTES_PER_ELEMENT));
|
|
102
|
+
} else if (pcm instanceof ArrayBuffer) {
|
|
103
|
+
pcm = new Float32Array(pcm);
|
|
104
|
+
} else {
|
|
105
|
+
throw new TypeError("PCM must be a Float32Array or an ArrayBuffer view.");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (pcm.length === 0) return;
|
|
109
|
+
this.#ingest(Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength));
|
|
110
|
+
if (!this.#emitTimer && this.#queue.length > 0) {
|
|
111
|
+
this.#nextEmitAtMs = Date.now();
|
|
112
|
+
this.#scheduleNext(this.#chunkIntervalMs);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
stop = () => {
|
|
117
|
+
if (this.#emitTimer) {
|
|
118
|
+
clearTimeout(this.#emitTimer);
|
|
119
|
+
this.#emitTimer = null;
|
|
120
|
+
}
|
|
121
|
+
this.#proc?.kill("SIGTERM");
|
|
122
|
+
this.#proc = null;
|
|
123
|
+
if (this.#streamListeners) {
|
|
124
|
+
const { stream, onData, onError, onEnd } = this.#streamListeners;
|
|
125
|
+
stream.off?.("data", onData) ?? stream.removeListener?.("data", onData);
|
|
126
|
+
stream.off?.("error", onError) ?? stream.removeListener?.("error", onError);
|
|
127
|
+
stream.off?.("end", onEnd) ?? stream.removeListener?.("end", onEnd);
|
|
128
|
+
stream.pause?.();
|
|
129
|
+
}
|
|
130
|
+
this.#streamListeners = null;
|
|
131
|
+
this.#sourceStream = null;
|
|
132
|
+
this.#pending = Buffer.alloc(0);
|
|
133
|
+
this.#queue = [];
|
|
134
|
+
this.#warmupUntilMs = 0;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
#ingest = (chunk) => {
|
|
138
|
+
const bytes = Buffer.isBuffer(chunk)
|
|
139
|
+
? chunk
|
|
140
|
+
: chunk instanceof Uint8Array
|
|
141
|
+
? Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength)
|
|
142
|
+
: Buffer.from(chunk);
|
|
143
|
+
this.#pending = Buffer.concat([this.#pending, bytes]);
|
|
144
|
+
while (this.#pending.length >= this.#chunkBytes) {
|
|
145
|
+
if (this.#queue.length >= MAX_QUEUED_CHUNKS) {
|
|
146
|
+
this.#proc?.stdout.pause();
|
|
147
|
+
this.#sourceStream?.pause?.();
|
|
148
|
+
this.droppedChunks += 1;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
const frame = this.#pending.subarray(0, this.#chunkBytes);
|
|
152
|
+
this.#pending = this.#pending.subarray(this.#chunkBytes);
|
|
153
|
+
const out = new Float32Array(this.#chunkSamples);
|
|
154
|
+
out.set(new Float32Array(frame.buffer, frame.byteOffset, this.#chunkSamples));
|
|
155
|
+
this.bytesProduced += this.#chunkBytes;
|
|
156
|
+
this.#queue.push(out);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
#resolveInputArgs = () => {
|
|
161
|
+
if (!this.source || this.source === "silence") {
|
|
162
|
+
return ["-f", "lavfi", "-i", `aevalsrc=0:d=3600:s=${this.sampleRate}`];
|
|
163
|
+
}
|
|
164
|
+
if (this.source.startsWith("lavfi:")) {
|
|
165
|
+
return ["-f", "lavfi", "-i", this.source.slice("lavfi:".length)];
|
|
166
|
+
}
|
|
167
|
+
return ["-i", this.source];
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
#scheduleNext = (chunkIntervalMs) => {
|
|
171
|
+
if (!this.#proc && !this.#sourceStream && this.#queue.length === 0) return;
|
|
172
|
+
const now = Date.now();
|
|
173
|
+
if (this.#nextEmitAtMs === 0) this.#nextEmitAtMs = now;
|
|
174
|
+
const delayMs = Math.max(0, this.#nextEmitAtMs - now);
|
|
175
|
+
this.#emitTimer = setTimeout(() => {
|
|
176
|
+
this.#emitTimer = null;
|
|
177
|
+
if (this.#queue.length < LOW_WATERMARK_CHUNKS && Date.now() < this.#warmupUntilMs) {
|
|
178
|
+
this.#nextEmitAtMs = Date.now() + 10;
|
|
179
|
+
this.#scheduleNext(chunkIntervalMs);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.#flushOne();
|
|
183
|
+
this.#nextEmitAtMs += chunkIntervalMs;
|
|
184
|
+
this.#scheduleNext(chunkIntervalMs);
|
|
185
|
+
}, delayMs);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
#flushOne = () => {
|
|
189
|
+
let nextChunk = this.#queue.shift();
|
|
190
|
+
if (!nextChunk) {
|
|
191
|
+
nextChunk = new Float32Array(this.#chunkSamples);
|
|
192
|
+
this.underflowChunks += 1;
|
|
193
|
+
}
|
|
194
|
+
this.chunksEmitted += 1;
|
|
195
|
+
this.onChunk(nextChunk);
|
|
196
|
+
if (this.#queue.length <= MAX_QUEUED_CHUNKS / 4) {
|
|
197
|
+
if (this.#proc?.stdout.isPaused()) this.#proc.stdout.resume();
|
|
198
|
+
this.#sourceStream?.resume?.();
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, open, rename, rm } from "node:fs/promises";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
const WAV_MAX_DATA_BYTES = 0xFFFFFFFF - 36;
|
|
6
|
+
const DEFAULT_MAX_QUEUED_BYTES = 2 * 1024 * 1024;
|
|
7
|
+
|
|
8
|
+
const makeHeader = (sampleRate, channels, dataBytes) => {
|
|
9
|
+
const header = Buffer.alloc(44);
|
|
10
|
+
header.write("RIFF", 0);
|
|
11
|
+
header.writeUInt32LE(36 + dataBytes, 4);
|
|
12
|
+
header.write("WAVE", 8);
|
|
13
|
+
header.write("fmt ", 12);
|
|
14
|
+
header.writeUInt32LE(16, 16);
|
|
15
|
+
header.writeUInt16LE(1, 20);
|
|
16
|
+
header.writeUInt16LE(channels, 22);
|
|
17
|
+
header.writeUInt32LE(sampleRate, 24);
|
|
18
|
+
header.writeUInt32LE(sampleRate * channels * 2, 28);
|
|
19
|
+
header.writeUInt16LE(channels * 2, 32);
|
|
20
|
+
header.writeUInt16LE(16, 34);
|
|
21
|
+
header.write("data", 36);
|
|
22
|
+
header.writeUInt32LE(dataBytes, 40);
|
|
23
|
+
return header;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const writeFully = async (handle, buffer, position) => {
|
|
27
|
+
let offset = 0;
|
|
28
|
+
while (offset < buffer.length) {
|
|
29
|
+
const { bytesWritten } = await handle.write(buffer, offset, buffer.length - offset, position + offset);
|
|
30
|
+
if (bytesWritten <= 0) throw new Error("WAV writer made no forward progress");
|
|
31
|
+
offset += bytesWritten;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const toPcm16 = (samples, channels) => {
|
|
36
|
+
const usableSamples = Math.floor(samples.length / channels) * channels;
|
|
37
|
+
const output = Buffer.allocUnsafe(usableSamples * 2);
|
|
38
|
+
for (let i = 0; i < usableSamples; i += 1) {
|
|
39
|
+
const sample = Number(samples[i]);
|
|
40
|
+
const finite = Number.isFinite(sample) ? sample : 0;
|
|
41
|
+
const clamped = Math.max(-1, Math.min(1, finite));
|
|
42
|
+
output.writeInt16LE(clamped < 0 ? Math.round(clamped * 0x8000) : Math.round(clamped * 0x7FFF), i * 2);
|
|
43
|
+
}
|
|
44
|
+
return output;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
class WavTrackWriter {
|
|
48
|
+
#direction;
|
|
49
|
+
#path;
|
|
50
|
+
#partialPath;
|
|
51
|
+
#handle;
|
|
52
|
+
#sampleRate;
|
|
53
|
+
#channels;
|
|
54
|
+
#maxQueuedBytes;
|
|
55
|
+
#onError;
|
|
56
|
+
#writeQueue = Promise.resolve();
|
|
57
|
+
#queuedBytes = 0;
|
|
58
|
+
#reservedBytes = 0;
|
|
59
|
+
#writtenBytes = 0;
|
|
60
|
+
#error = null;
|
|
61
|
+
#closed = false;
|
|
62
|
+
#closePromise = null;
|
|
63
|
+
|
|
64
|
+
constructor({ direction, path, partialPath, handle, sampleRate, channels, maxQueuedBytes, onError }) {
|
|
65
|
+
this.#direction = direction;
|
|
66
|
+
this.#path = path;
|
|
67
|
+
this.#partialPath = partialPath;
|
|
68
|
+
this.#handle = handle;
|
|
69
|
+
this.#sampleRate = sampleRate;
|
|
70
|
+
this.#channels = channels;
|
|
71
|
+
this.#maxQueuedBytes = maxQueuedBytes;
|
|
72
|
+
this.#onError = onError;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static async create(options) {
|
|
76
|
+
const partialPath = `${options.path}.part`;
|
|
77
|
+
await mkdir(dirname(options.path), { recursive: true });
|
|
78
|
+
const handle = await open(partialPath, "wx+");
|
|
79
|
+
try {
|
|
80
|
+
await writeFully(handle, makeHeader(options.sampleRate, options.channels, 0), 0);
|
|
81
|
+
return new WavTrackWriter({ ...options, partialPath, handle });
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
await handle.close().catch(() => { });
|
|
85
|
+
await rm(partialPath, { force: true }).catch(() => { });
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get path() {
|
|
91
|
+
return this.#path;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
write = (samples) => {
|
|
95
|
+
if (this.#closed || this.#error || !samples || typeof samples.length !== "number") return false;
|
|
96
|
+
const pcm = toPcm16(samples, this.#channels);
|
|
97
|
+
if (pcm.length === 0) return true;
|
|
98
|
+
if (this.#reservedBytes + pcm.length > WAV_MAX_DATA_BYTES) {
|
|
99
|
+
this.#fail(new Error("Recording reached the RIFF/WAV 4 GiB limit"));
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (this.#queuedBytes + pcm.length > this.#maxQueuedBytes) {
|
|
103
|
+
this.#fail(new Error(`Recording ${this.#direction} writer exceeded its ${this.#maxQueuedBytes}-byte queue limit`));
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const position = 44 + this.#reservedBytes;
|
|
108
|
+
this.#reservedBytes += pcm.length;
|
|
109
|
+
this.#queuedBytes += pcm.length;
|
|
110
|
+
this.#writeQueue = this.#writeQueue
|
|
111
|
+
.then(async () => {
|
|
112
|
+
if (this.#error) return;
|
|
113
|
+
await writeFully(this.#handle, pcm, position);
|
|
114
|
+
this.#writtenBytes += pcm.length;
|
|
115
|
+
})
|
|
116
|
+
.catch((error) => this.#fail(error))
|
|
117
|
+
.finally(() => { this.#queuedBytes -= pcm.length; });
|
|
118
|
+
return true;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
#fail = (error) => {
|
|
122
|
+
if (this.#error) return;
|
|
123
|
+
this.#error = error instanceof Error ? error : new Error(String(error));
|
|
124
|
+
try { this.#onError?.(this.#direction, this.#error); }
|
|
125
|
+
catch { }
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
close = () => {
|
|
129
|
+
if (this.#closePromise) return this.#closePromise;
|
|
130
|
+
this.#closed = true;
|
|
131
|
+
this.#closePromise = this.#finalize();
|
|
132
|
+
return this.#closePromise;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
#finalize = async () => {
|
|
136
|
+
try {
|
|
137
|
+
await this.#writeQueue;
|
|
138
|
+
await writeFully(this.#handle, makeHeader(this.#sampleRate, this.#channels, this.#writtenBytes), 0);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
this.#fail(error);
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
await this.#handle.close().catch((error) => this.#fail(error));
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
await rename(this.#partialPath, this.#path);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
this.#fail(error);
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
path: this.#path,
|
|
154
|
+
bytesWritten: this.#writtenBytes,
|
|
155
|
+
complete: !this.#error,
|
|
156
|
+
error: this.#error?.message,
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
abort = async () => {
|
|
161
|
+
this.#closed = true;
|
|
162
|
+
await this.#writeQueue.catch(() => { });
|
|
163
|
+
await this.#handle.close().catch(() => { });
|
|
164
|
+
await rm(this.#partialPath, { force: true }).catch(() => { });
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Records each direction as an independent PCM16 WAV track without retaining call audio in memory. */
|
|
169
|
+
export class CallRecorder {
|
|
170
|
+
#callId;
|
|
171
|
+
#startedAt;
|
|
172
|
+
#sampleRate;
|
|
173
|
+
#channels;
|
|
174
|
+
#formats;
|
|
175
|
+
#writers;
|
|
176
|
+
#closed = false;
|
|
177
|
+
#closePromise = null;
|
|
178
|
+
|
|
179
|
+
constructor({ callId, sampleRate, channels, formats, writers }) {
|
|
180
|
+
this.#callId = callId;
|
|
181
|
+
this.#startedAt = new Date();
|
|
182
|
+
this.#sampleRate = sampleRate;
|
|
183
|
+
this.#channels = channels;
|
|
184
|
+
this.#formats = formats;
|
|
185
|
+
this.#writers = writers;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static create = async ({ directory, callId, sampleRate = 16000, channels = 1, inboundSampleRate = sampleRate, inboundChannels = channels, outboundSampleRate = sampleRate, outboundChannels = channels, maxQueuedBytes = DEFAULT_MAX_QUEUED_BYTES, onError }) => {
|
|
189
|
+
if (typeof directory !== "string" || !directory.trim()) throw new Error("Recording requires an explicit output directory");
|
|
190
|
+
const formats = {
|
|
191
|
+
inbound: { sampleRate: inboundSampleRate, channels: inboundChannels },
|
|
192
|
+
outbound: { sampleRate: outboundSampleRate, channels: outboundChannels },
|
|
193
|
+
};
|
|
194
|
+
for (const format of Object.values(formats)) {
|
|
195
|
+
if (!Number.isInteger(format.sampleRate) || format.sampleRate < 8000 || format.sampleRate > 96000) throw new Error("Recording sample rate must be an integer from 8000 to 96000 Hz");
|
|
196
|
+
if (!Number.isInteger(format.channels) || format.channels < 1 || format.channels > 2) throw new Error("Recording supports one or two PCM channels");
|
|
197
|
+
}
|
|
198
|
+
if (!Number.isInteger(maxQueuedBytes) || maxQueuedBytes < 1) throw new Error("maxQueuedBytes must be a positive integer");
|
|
199
|
+
|
|
200
|
+
const outputDirectory = resolve(directory);
|
|
201
|
+
const safeCallId = String(callId).replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64) || "call";
|
|
202
|
+
const base = `call-${new Date().toISOString().replace(/[:.]/g, "-")}-${safeCallId}-${randomUUID()}`;
|
|
203
|
+
const options = { maxQueuedBytes, onError };
|
|
204
|
+
const writers = {};
|
|
205
|
+
try {
|
|
206
|
+
writers.outbound = await WavTrackWriter.create({ ...options, ...formats.outbound, direction: "outbound", path: resolve(outputDirectory, `${base}-outbound.wav`) });
|
|
207
|
+
writers.inbound = await WavTrackWriter.create({ ...options, ...formats.inbound, direction: "inbound", path: resolve(outputDirectory, `${base}-inbound.wav`) });
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
await Promise.all(Object.values(writers).map((writer) => writer.abort()));
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
return new CallRecorder({ callId, sampleRate, channels, formats, writers });
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
get paths() {
|
|
217
|
+
return Object.fromEntries(Object.entries(this.#writers).map(([direction, writer]) => [direction, writer.path]));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
write = (direction, samples) => {
|
|
221
|
+
if (this.#closed) return false;
|
|
222
|
+
return this.#writers[direction]?.write(samples) ?? false;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
close = () => {
|
|
226
|
+
if (this.#closePromise) return this.#closePromise;
|
|
227
|
+
this.#closed = true;
|
|
228
|
+
this.#closePromise = this.#finalize();
|
|
229
|
+
return this.#closePromise;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
#finalize = async () => {
|
|
233
|
+
const tracks = await Promise.all(Object.entries(this.#writers).map(async ([direction, writer]) => [direction, await writer.close()]));
|
|
234
|
+
const files = Object.fromEntries(tracks.map(([direction, result]) => [direction, result.path]));
|
|
235
|
+
const bytesWritten = Object.fromEntries(tracks.map(([direction, result]) => [direction, result.bytesWritten]));
|
|
236
|
+
const errors = tracks.filter(([, result]) => !result.complete).map(([direction, result]) => ({ direction, error: result.error }));
|
|
237
|
+
return {
|
|
238
|
+
callId: this.#callId,
|
|
239
|
+
startedAt: this.#startedAt,
|
|
240
|
+
endedAt: new Date(),
|
|
241
|
+
sampleRate: this.#sampleRate,
|
|
242
|
+
channels: this.#channels,
|
|
243
|
+
formats: this.#formats,
|
|
244
|
+
files,
|
|
245
|
+
bytesWritten,
|
|
246
|
+
complete: errors.length === 0,
|
|
247
|
+
errors,
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Fetch the current WhatsApp Web VoIP runtime files from the user's own browser.
|
|
4
|
+
* Keep Chrome's DevTools endpoint bound to loopback; it grants browser control.
|
|
5
|
+
*/
|
|
6
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { join, resolve } from "node:path";
|
|
8
|
+
import { WebSocket } from "ws";
|
|
9
|
+
|
|
10
|
+
const debuggerUrl = process.env.CALL_CHROME_DEBUGGER_JSON_URL || "http://127.0.0.1:9222/json/list";
|
|
11
|
+
const wasmId = process.env.CALL_BROWSER_WASM_ID || "32180";
|
|
12
|
+
const resourceRoot = resolve(process.env.X4R1S3_VOIP_RESOURCES || "call-resources");
|
|
13
|
+
const outputDir = join(resourceRoot, "assets", "wasm");
|
|
14
|
+
|
|
15
|
+
const fetchChecked = async (url) => {
|
|
16
|
+
const response = await fetch(url);
|
|
17
|
+
if (!response.ok) throw new Error(`HTTP ${response.status} fetching ${url}`);
|
|
18
|
+
return response;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const main = async () => {
|
|
22
|
+
mkdirSync(outputDir, { recursive: true });
|
|
23
|
+
const targets = await (await fetchChecked(debuggerUrl)).json();
|
|
24
|
+
const page = targets.find((target) => target.type === "page" && target.url?.includes("web.whatsapp.com"));
|
|
25
|
+
if (!page?.webSocketDebuggerUrl) {
|
|
26
|
+
throw new Error("No WhatsApp Web page found. Open WhatsApp Web in Chrome with remote debugging enabled.");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ws = new WebSocket(page.webSocketDebuggerUrl);
|
|
30
|
+
let nextId = 1;
|
|
31
|
+
const pending = new Map();
|
|
32
|
+
ws.on("message", (raw) => {
|
|
33
|
+
let message;
|
|
34
|
+
try { message = JSON.parse(raw.toString()); }
|
|
35
|
+
catch { return; }
|
|
36
|
+
if (!message.id || !pending.has(message.id)) return;
|
|
37
|
+
const { resolve: resolveResult, reject } = pending.get(message.id);
|
|
38
|
+
pending.delete(message.id);
|
|
39
|
+
if (message.error) reject(new Error(message.error.message));
|
|
40
|
+
else resolveResult(message.result);
|
|
41
|
+
});
|
|
42
|
+
const send = (method, params = {}) => new Promise((resolveResult, reject) => {
|
|
43
|
+
const id = nextId++;
|
|
44
|
+
const timeout = setTimeout(() => {
|
|
45
|
+
pending.delete(id);
|
|
46
|
+
reject(new Error(`Timed out waiting for Chrome DevTools method ${method}`));
|
|
47
|
+
}, 15_000);
|
|
48
|
+
pending.set(id, {
|
|
49
|
+
resolve: (value) => { clearTimeout(timeout); resolveResult(value); },
|
|
50
|
+
reject: (error) => { clearTimeout(timeout); reject(error); },
|
|
51
|
+
});
|
|
52
|
+
ws.send(JSON.stringify({ id, method, params }));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
await new Promise((resolveOpen, rejectOpen) => {
|
|
57
|
+
ws.once("open", resolveOpen);
|
|
58
|
+
ws.once("error", rejectOpen);
|
|
59
|
+
});
|
|
60
|
+
const expression = `(() => {
|
|
61
|
+
try {
|
|
62
|
+
const resource = require("WAWebVoipWebWasmWorkerResource");
|
|
63
|
+
const id = ${JSON.stringify(wasmId)};
|
|
64
|
+
return {
|
|
65
|
+
workerUrl: resource?.url || null,
|
|
66
|
+
wasmUrl: resource?.hsdp?.bxData?.[id]?.uri || null,
|
|
67
|
+
availableIds: Object.keys(resource?.hsdp?.bxData || {}),
|
|
68
|
+
};
|
|
69
|
+
} catch (error) { return { error: error.message }; }
|
|
70
|
+
})()`;
|
|
71
|
+
const evaluation = await send("Runtime.evaluate", {
|
|
72
|
+
expression,
|
|
73
|
+
returnByValue: true,
|
|
74
|
+
awaitPromise: true,
|
|
75
|
+
});
|
|
76
|
+
if (evaluation.exceptionDetails) {
|
|
77
|
+
throw new Error(evaluation.exceptionDetails.text || "Could not inspect WhatsApp Web resources");
|
|
78
|
+
}
|
|
79
|
+
const info = evaluation.result?.value;
|
|
80
|
+
if (info?.error) throw new Error(`Browser evaluation failed: ${info.error}`);
|
|
81
|
+
if (!info?.workerUrl || !info?.wasmUrl) {
|
|
82
|
+
throw new Error(`No resources found for WASM id ${wasmId}; available IDs: ${(info?.availableIds || []).join(", ")}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
console.log(`Downloading worker bundle from ${info.workerUrl}`);
|
|
86
|
+
const workerCode = await (await fetchChecked(info.workerUrl)).text();
|
|
87
|
+
writeFileSync(join(outputDir, "worker-modules.js"), workerCode);
|
|
88
|
+
console.log(`Saved worker-modules.js (${workerCode.length} characters)`);
|
|
89
|
+
|
|
90
|
+
console.log(`Downloading WASM runtime from ${info.wasmUrl}`);
|
|
91
|
+
const wasmBytes = Buffer.from(await (await fetchChecked(info.wasmUrl)).arrayBuffer());
|
|
92
|
+
writeFileSync(join(outputDir, "whatsapp.wasm"), wasmBytes);
|
|
93
|
+
console.log(`Saved whatsapp.wasm (${wasmBytes.length} bytes) to ${outputDir}`);
|
|
94
|
+
console.log("These runtime resources are local-only; do not commit or redistribute them.");
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
ws.close();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
main().catch((error) => {
|
|
102
|
+
console.error(`VoIP resource download failed: ${error.message}`);
|
|
103
|
+
process.exitCode = 1;
|
|
104
|
+
});
|