Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): fail a best-effort patch after a partial write

Yudhi Armyndharis9739eb172ae1
diff --git a/scripts/patch-baileys-appstate.js b/scripts/patch-baileys-appstate.js
index 1a1a84ce8..8a716ae4b 100644
--- a/scripts/patch-baileys-appstate.js
+++ b/scripts/patch-baileys-appstate.js
@@ -66,7 +66,13 @@ function applyAppStatePatch(baileysDir = DEFAULT_BAILEYS) {
'the upstream shape changed; re-check whether the loop is still unbounded before removing this patcher',
);
}
- fs.writeFileSync(file, source.replace(LOOP_FIND, LOOP_REPLACE), 'utf8');
+ try {
+ fs.writeFileSync(file, source.replace(LOOP_FIND, LOOP_REPLACE), 'utf8');
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
return { note: 'bounded the app-state resync loop on an empty decode' };
}
@@ -76,7 +82,7 @@ function run() {
const result = applyAppStatePatch();
console.log(`patch-baileys-appstate: ${result.skipped ? `skipped — ${result.reason}` : result.note}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-baileys-appstate: skipped — ${error.message}`);
return;
}
diff --git a/scripts/patch-baileys-newsletter-create.js b/scripts/patch-baileys-newsletter-create.js
index 1f8eb5bdd..bf51fba3e 100644
--- a/scripts/patch-baileys-newsletter-create.js
+++ b/scripts/patch-baileys-newsletter-create.js
@@ -95,7 +95,13 @@ function applyNewsletterCreatePatch(baileysDir = DEFAULT_BAILEYS) {
'before removing this patcher',
);
}
- fs.writeFileSync(file, source.replace(PARSE_FIND, PARSE_REPLACE), 'utf8');
+ try {
+ fs.writeFileSync(file, source.replace(PARSE_FIND, PARSE_REPLACE), 'utf8');
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
return { note: 'made the newsletter-create parse tolerate an absent picture, keeping the new channel id' };
}
@@ -105,7 +111,7 @@ function run() {
const result = applyNewsletterCreatePatch();
console.log(`patch-baileys-newsletter-create: ${result.skipped ? `skipped — ${result.reason}` : result.note}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-baileys-newsletter-create: skipped — ${error.message}`);
return;
}
diff --git a/scripts/patch-write-failure.spec.js b/scripts/patch-write-failure.spec.js
new file mode 100644
index 000000000..afa71178d
--- /dev/null
+++ b/scripts/patch-write-failure.spec.js
@@ -0,0 +1,112 @@
+'use strict';
+
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const os = require('node:os');
+const path = require('node:path');
+const { spawnSync } = require('node:child_process');
+
+/**
+ * `--best-effort` lets a source install carry on without a patch it cannot place. A write that fails
+ * part-way is not that case: fs.writeFileSync has already truncated the target, so the dependency is
+ * left broken and no later run can tell. Each patcher must exit non-zero then, and only then.
+ */
+
+// realpath: os.tmpdir() is a symlink on macOS and the patcher resolves its own location through it,
+// so the fault below would never match the target path.
+const WORK = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'patch-write-failure-')));
+test.after(() => fs.rmSync(WORK, { recursive: true, force: true }));
+
+const PREFIX_BYTES = 64;
+const MARKER = 'FIXTURE_WRITE_FAULT';
+const PRELOAD = path.join(WORK, 'write-fault.cjs');
+fs.writeFileSync(
+ PRELOAD,
+ `'use strict';
+const fs = require('node:fs');
+const path = require('node:path');
+const write = fs.writeFileSync;
+const target = path.resolve(process.env.FIXTURE_WRITE_TARGET);
+fs.writeFileSync = function (file, data, ...args) {
+ if (typeof file !== 'number' && path.resolve(String(file)) === target) {
+ write.call(fs, file, Buffer.from(String(data)).subarray(0, ${PREFIX_BYTES}));
+ process.stderr.write('${MARKER}\\n');
+ throw Object.assign(new Error('fixture write failure'), { code: 'ENOSPC' });
+ }
+ return write.call(fs, file, data, ...args);
+};
+`,
+);
+
+const finds = edits => edits.map(edit => edit.find).join('\n');
+const PATCHERS = [
+ ['patch-baileys-appstate.js', '@whiskeysockets/baileys/lib/Socket/chats.js', m => m.LOOP_FIND],
+ ['patch-baileys-newsletter-create.js', '@whiskeysockets/baileys/lib/Socket/newsletter.js', m => m.PARSE_FIND],
+ [
+ 'patch-wwebjs-block.js',
+ 'whatsapp-web.js/src/structures/Contact.js',
+ m => m.GROUPS.map(group => group.find).join('\n'),
+ ],
+ ['patch-wwebjs-group-description.js', 'whatsapp-web.js/src/structures/GroupChat.js', m => m.POSITIONAL_CALL],
+ ['patch-wwebjs-media-id.js', 'whatsapp-web.js/src/util/Injected/Utils.js', m => m.ANCHOR],
+ ['patch-wwebjs-newsletter-preview.js', 'whatsapp-web.js/src/util/Injected/Utils.js', m => m.LEGACY_CALL],
+ [
+ 'patch-wwebjs-participant-arity.js',
+ 'whatsapp-web.js/src/structures/GroupChat.js',
+ m => finds(m.GROUPS.flatMap(group => group.edits)),
+ ],
+ ['patch-wwebjs-ready-sync.js', 'whatsapp-web.js/src/Client.js', m => finds(m.EDITS)],
+ ['patch-wwebjs-send-error.js', 'whatsapp-web.js/src/Client.js', m => m.ANCHOR],
+ [
+ 'patch-wwebjs-status.js',
+ 'whatsapp-web.js/src/util/Injected/Utils.js',
+ m => finds(m.GROUPS.flatMap(group => group.edits)),
+ ],
+];
+
+function fixture(name, relative, content) {
+ const root = fs.mkdtempSync(path.join(WORK, 'tree-'));
+ const script = path.join(root, 'scripts', name);
+ const target = path.join(root, 'node_modules', relative);
+ fs.mkdirSync(path.dirname(script), { recursive: true });
+ fs.mkdirSync(path.dirname(target), { recursive: true });
+ fs.copyFileSync(path.join(__dirname, name), script);
+ fs.writeFileSync(target, content);
+ return { root, script, target };
+}
+
+function patch({ root, script, target }, fault) {
+ const result = spawnSync(process.execPath, [...(fault ? ['--require', PRELOAD] : []), script, '--best-effort'], {
+ cwd: root,
+ env: { PATH: process.env.PATH, FIXTURE_WRITE_TARGET: target },
+ encoding: 'utf8',
+ timeout: 10000,
+ });
+ assert.equal(result.error, undefined);
+ return result;
+}
+
+for (const [name, relative, shapeOf] of PATCHERS) {
+ const shape = `// fixture\n${shapeOf(require(`./${name}`))}\n`;
+
+ test(`${name} patches a recognised tree`, () => {
+ const tree = fixture(name, relative, shape);
+ assert.equal(patch(tree).status, 0);
+ assert.notEqual(fs.readFileSync(tree.target, 'utf8'), shape);
+ });
+
+ test(`${name} skips an unrecognised tree under --best-effort`, () => {
+ const tree = fixture(name, relative, 'unknown source shape\n');
+ assert.equal(patch(tree).status, 0);
+ assert.equal(fs.readFileSync(tree.target, 'utf8'), 'unknown source shape\n');
+ });
+
+ test(`${name} fails when the write breaks part-way, even under --best-effort`, () => {
+ const tree = fixture(name, relative, shape);
+ const result = patch(tree, true);
+ assert.match(result.stderr, new RegExp(MARKER), 'the injected fault did not fire');
+ assert.equal(fs.statSync(tree.target).size, PREFIX_BYTES, 'the target keeps only the written prefix');
+ assert.notEqual(result.status, 0);
+ });
+}
diff --git a/scripts/patch-wwebjs-block.js b/scripts/patch-wwebjs-block.js
index 3b9e2e481..e85f2d417 100644
--- a/scripts/patch-wwebjs-block.js
+++ b/scripts/patch-wwebjs-block.js
@@ -181,7 +181,13 @@ function applyBlockPatches({ wwjsDir = DEFAULT_WWJS } = {}) {
}
if (applied.length > 0) {
- fs.writeFileSync(contactFile, source);
+ try {
+ fs.writeFileSync(contactFile, source);
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
}
return { applied, skipped };
}
@@ -196,7 +202,7 @@ function run() {
].join('; ');
console.log(`patch-wwebjs-block: ${report}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-block: skipped, ${error.message}`);
return;
}
diff --git a/scripts/patch-wwebjs-group-description.js b/scripts/patch-wwebjs-group-description.js
index e79518bc1..300d98fd9 100644
--- a/scripts/patch-wwebjs-group-description.js
+++ b/scripts/patch-wwebjs-group-description.js
@@ -97,7 +97,13 @@ function applyGroupDescriptionFix(wwjsDir = DEFAULT_WWJS) {
);
}
- fs.writeFileSync(groupChatFile, source.replace(POSITIONAL_CALL, OPTIONS_CALL));
+ try {
+ fs.writeFileSync(groupChatFile, source.replace(POSITIONAL_CALL, OPTIONS_CALL));
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
return { skipped: false, note: 'group description job called with an options object' };
}
@@ -107,7 +113,7 @@ function run() {
const result = applyGroupDescriptionFix();
console.log(`patch-wwebjs-group-description: ${result.skipped ? `skipped — ${result.reason}` : result.note}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-group-description: skipped — ${error.message}`);
return;
}
diff --git a/scripts/patch-wwebjs-media-id.js b/scripts/patch-wwebjs-media-id.js
index 0d378f850..335bd7c8c 100644
--- a/scripts/patch-wwebjs-media-id.js
+++ b/scripts/patch-wwebjs-media-id.js
@@ -51,7 +51,13 @@ function applyBackport(wwjsDir = DEFAULT_WWJS) {
);
}
- fs.writeFileSync(utilsFile, source.replace(ANCHOR, FIX + ANCHOR));
+ try {
+ fs.writeFileSync(utilsFile, source.replace(ANCHOR, FIX + ANCHOR));
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
return { skipped: false, note: 'media model id stripped from outgoing messages' };
}
@@ -74,7 +80,7 @@ function run() {
const result = applyBackport();
console.log(`patch-wwebjs-media-id: ${result.skipped ? `skipped: ${result.reason}` : result.note}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-media-id: skipped: ${error.message}`);
return;
}
diff --git a/scripts/patch-wwebjs-newsletter-preview.js b/scripts/patch-wwebjs-newsletter-preview.js
index 734ad20e5..8b916fc92 100644
--- a/scripts/patch-wwebjs-newsletter-preview.js
+++ b/scripts/patch-wwebjs-newsletter-preview.js
@@ -47,7 +47,13 @@ function applyBackport(wwjsDir = DEFAULT_WWJS) {
);
}
- fs.writeFileSync(utilsFile, source.replace(LEGACY_CALL, CONTEXT_CALL));
+ try {
+ fs.writeFileSync(utilsFile, source.replace(LEGACY_CALL, CONTEXT_CALL));
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
return { skipped: false, note: 'destination chat context added to link previews' };
}
@@ -72,7 +78,7 @@ function run() {
`patch-wwebjs-newsletter-preview: ${result.skipped ? `skipped — ${result.reason}` : result.note}`,
);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-newsletter-preview: skipped — ${error.message}`);
return;
}
diff --git a/scripts/patch-wwebjs-participant-arity.js b/scripts/patch-wwebjs-participant-arity.js
index d74890be2..630dfc8fd 100644
--- a/scripts/patch-wwebjs-participant-arity.js
+++ b/scripts/patch-wwebjs-participant-arity.js
@@ -176,7 +176,13 @@ function applyParticipantArityPatches(wwjsDir = DEFAULT_WWJS) {
}
if (applied.length) {
- fs.writeFileSync(groupChatFile, source);
+ try {
+ fs.writeFileSync(groupChatFile, source);
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
}
return { applied, skipped };
}
@@ -191,7 +197,7 @@ function run() {
].join('; ');
console.log(`patch-wwebjs-participant-arity: ${report}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-participant-arity: skipped — ${error.message}`);
return;
}
diff --git a/scripts/patch-wwebjs-ready-sync.js b/scripts/patch-wwebjs-ready-sync.js
index 9badbd672..00cba03cf 100644
--- a/scripts/patch-wwebjs-ready-sync.js
+++ b/scripts/patch-wwebjs-ready-sync.js
@@ -115,7 +115,13 @@ function applyReadySyncPatch(wwjsDir = DEFAULT_WWJS) {
for (const edit of EDITS) {
source = source.replace(edit.find, edit.replace);
}
- fs.writeFileSync(clientFile, source);
+ try {
+ fs.writeFileSync(clientFile, source);
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
return { skipped: false, note: 'readiness marker and hasSynced level-check applied' };
}
throw new Error(
@@ -130,7 +136,7 @@ function run() {
const result = applyReadySyncPatch();
console.log(`patch-wwebjs-ready-sync: ${result.skipped ? `skipped — ${result.reason}` : result.note}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-ready-sync: skipped — ${error.message}`);
return;
}
diff --git a/scripts/patch-wwebjs-send-error.js b/scripts/patch-wwebjs-send-error.js
index 45b17bfe9..122e3a7ab 100644
--- a/scripts/patch-wwebjs-send-error.js
+++ b/scripts/patch-wwebjs-send-error.js
@@ -157,7 +157,13 @@ function applyBackport(wwjsDir = DEFAULT_WWJS) {
);
}
- fs.writeFileSync(clientFile, source.replace(ANCHOR, FIX));
+ try {
+ fs.writeFileSync(clientFile, source.replace(ANCHOR, FIX));
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
return { skipped: false, note: 'page-side send errors now carry what the page threw' };
}
@@ -180,7 +186,7 @@ function run() {
const result = applyBackport();
console.log(`patch-wwebjs-send-error: ${result.skipped ? `skipped: ${result.reason}` : result.note}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-send-error: skipped: ${error.message}`);
return;
}
diff --git a/scripts/patch-wwebjs-status.js b/scripts/patch-wwebjs-status.js
index b12236997..53b172660 100644
--- a/scripts/patch-wwebjs-status.js
+++ b/scripts/patch-wwebjs-status.js
@@ -170,7 +170,13 @@ function applyStatusPatches(wwjsDir = DEFAULT_WWJS) {
}
if (applied.length) {
- fs.writeFileSync(utilsFile, source);
+ try {
+ fs.writeFileSync(utilsFile, source);
+ } catch (error) {
+ // The write may have truncated the file: a partial tree is never skipped, even under --best-effort.
+ error.partial = true;
+ throw error;
+ }
}
return { applied, skipped };
}
@@ -185,7 +191,7 @@ function run() {
].join('; ');
console.log(`patch-wwebjs-status: ${report}`);
} catch (error) {
- if (bestEffort) {
+ if (bestEffort && !error.partial) {
console.warn(`patch-wwebjs-status: skipped — ${error.message}`);
return;
}