Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): import sharp lazily so it cannot fail the whole boot

Yudhi Armyndharisa2cb08982396
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 3086fa48a..dd9ef465e 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -1,4 +1,3 @@
-import sharp from 'sharp';
import type * as BaileysLib from '@whiskeysockets/baileys';
import type { AnyMessageContent, MiscMessageGenerationOptions, WAMessage, WASocket } from '@whiskeysockets/baileys';
import { generateSafeLinkPreview } from './safe-link-preview';
@@ -99,6 +98,12 @@ async function toWebpSticker(data: Buffer, mimetype: string): Promise<Buffer> {
);
}
try {
+ // Imported lazily so an unusable `sharp` (a native binary that will not build or load on an
+ // older CPU, a stripped image) degrades ONLY this one Baileys sticker route instead of killing
+ // the whole gateway at boot. `sharp` sits at the top of a module the built-in engine loads
+ // unconditionally, so an eager import made a single optional capability a hard boot requirement
+ // on both engines. Same deferral the adapters already use for the engine libraries themselves.
+ const { default: sharp } = await import('sharp');
return await sharp(data, { animated: true })
.resize(512, 512, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
.webp()
diff --git a/src/engine/adapters/baileys-sharp-lazy.spec.ts b/src/engine/adapters/baileys-sharp-lazy.spec.ts
new file mode 100644
index 000000000..16ebd9bb3
--- /dev/null
+++ b/src/engine/adapters/baileys-sharp-lazy.spec.ts
@@ -0,0 +1,31 @@
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
+
+/**
+ * `sharp` must never be imported eagerly here.
+ *
+ * It is a native module that fails to build or load on some CPUs and stripped images, and it backs
+ * exactly one Baileys route: sticker WebP conversion. This file is pulled in by the built-in engine
+ * chain that boots unconditionally on BOTH engines, so a top-level `import sharp from 'sharp'` made
+ * one optional capability a hard boot requirement for the whole gateway. An operator whose sharp
+ * cannot load lost everything, not just sticker sends.
+ *
+ * The import is now deferred into the one function that uses it, so an unusable sharp degrades that
+ * route with a clear error and the gateway still boots. This test locks that: a future edit that
+ * re-adds an eager import would silently reinstate the boot dependency.
+ */
+describe('sharp is imported lazily, not at module load', () => {
+ const source = readFileSync(join(__dirname, 'baileys-messaging.ts'), 'utf8');
+
+ // Strip line and block comments so a `sharp` mentioned in prose does not trip the scan.
+ const code = source.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '');
+
+ it('has no top-level static import of sharp', () => {
+ const eager = /^\s*import\s+[^;]*\bfrom\s+['"]sharp['"]/m.test(code);
+ expect(eager).toBe(false);
+ });
+
+ it('defers sharp behind a dynamic import', () => {
+ expect(code).toMatch(/await import\(['"]sharp['"]\)/);
+ });
+});