fix(whatsapp): support Baileys rc10 postinstall patch

This commit is contained in:
Peter Steinberger
2026-05-10 02:10:25 +01:00
parent 0496063264
commit 6d1a7169da
2 changed files with 127 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import {
applyBaileysEncryptedStreamFinishHotfix,
collectLegacyPluginRuntimeDepsStateRoots,
isSourceCheckoutRoot,
isDirectPostinstallInvocation,
@@ -58,6 +59,21 @@ async function writePluginPackage(
}
}
async function writeBaileysMediaFile(packageRoot: string, text: string) {
const mediaFile = path.join(
packageRoot,
"node_modules",
"@whiskeysockets",
"baileys",
"lib",
"Utils",
"messages-media.js",
);
await fs.mkdir(path.dirname(mediaFile), { recursive: true });
await fs.writeFile(mediaFile, text);
return mediaFile;
}
describe("bundled plugin postinstall", () => {
function existsSyncWithoutGlobalCompileCache(value: string) {
if (path.resolve(value) === path.join(tmpdir(), "node-compile-cache")) {
@@ -206,6 +222,83 @@ describe("bundled plugin postinstall", () => {
expect(warn).not.toHaveBeenCalled();
});
it("patches the Baileys rc10 upload helper dispatcher guard", async () => {
const packageRoot = await createTempDirAsync("openclaw-baileys-postinstall-");
const mediaFile = await writeBaileysMediaFile(
packageRoot,
[
"import { once } from 'events';",
"const encryptedStream = async () => {",
" encFileWriteStream.write(mac);",
" const encFinishPromise = once(encFileWriteStream, 'finish');",
" const originalFinishPromise = originalFileStream ? once(originalFileStream, 'finish') : Promise.resolve();",
" encFileWriteStream.end();",
" originalFileStream?.end?.();",
" stream.destroy();",
" await encFinishPromise;",
" await originalFinishPromise;",
" logger?.debug('encrypted data successfully');",
"};",
"const uploadWithFetch = async ({ url, filePath, headers, timeoutMs, agent }) => {",
" const nodeStream = createReadStream(filePath);",
" const webStream = Readable.toWeb(nodeStream);",
" const response = await fetch(url, {",
" dispatcher: agent,",
" method: 'POST',",
" body: webStream,",
" headers,",
" duplex: 'half',",
" signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined",
" });",
"};",
"",
].join("\n"),
);
expect(applyBaileysEncryptedStreamFinishHotfix({ packageRoot })).toMatchObject({
applied: true,
reason: "patched",
});
const patchedText = await fs.readFile(mediaFile, "utf8");
expect(patchedText).toContain(
"...(typeof agent?.dispatch === 'function' ? { dispatcher: agent } : {}),",
);
expect(patchedText).not.toContain(" dispatcher: agent,");
});
it("recognizes already patched Baileys rc10 upload helpers", async () => {
const packageRoot = await createTempDirAsync("openclaw-baileys-postinstall-");
await writeBaileysMediaFile(
packageRoot,
[
"import { once } from 'events';",
"const encryptedStream = async () => {",
" encFileWriteStream.write(mac);",
" const encFinishPromise = once(encFileWriteStream, 'finish');",
" const originalFinishPromise = originalFileStream ? once(originalFileStream, 'finish') : Promise.resolve();",
" encFileWriteStream.end();",
" originalFileStream?.end?.();",
" stream.destroy();",
" await encFinishPromise;",
" await originalFinishPromise;",
" logger?.debug('encrypted data successfully');",
"};",
"const uploadWithFetch = async ({ url, filePath, headers, timeoutMs, agent }) => {",
" const response = await fetch(url, {",
" ...(typeof agent?.dispatch === 'function' ? { dispatcher: agent } : {}),",
" method: 'POST',",
" });",
"};",
"",
].join("\n"),
);
expect(applyBaileysEncryptedStreamFinishHotfix({ packageRoot })).toMatchObject({
applied: false,
reason: "already_patched",
});
});
it("does not classify published packages with source files as source checkouts", () => {
const packageRoot = "/pkg";
const existingPaths = new Set([