Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(engine): opaque per-engine config — factory passes neutral fields, plugin reads context.config

rmyndharis36e469c0a159
diff --git a/src/engine/engine.factory.spec.ts b/src/engine/engine.factory.spec.ts
index 589c6a4fd..6eefa8752 100644
--- a/src/engine/engine.factory.spec.ts
+++ b/src/engine/engine.factory.spec.ts
@@ -3,6 +3,11 @@ import { ConfigService } from '@nestjs/config';
import { PluginLoaderService, PluginType } from '../core/plugins';
describe('EngineFactory', () => {
+ const engineBlob = {
+ type: 'whatsapp-web.js',
+ sessionDataPath: '/var/data/sessions',
+ puppeteer: { headless: true, args: ['--no-sandbox'], executablePath: '/usr/bin/chromium-browser' },
+ };
const buildConfigService = (overrides: Record<string, unknown> = {}): ConfigService => {
const values: Record<string, unknown> = {
'engine.type': 'whatsapp-web.js',
@@ -10,12 +15,13 @@ describe('EngineFactory', () => {
'engine.puppeteer.headless': true,
'engine.puppeteer.args': ['--no-sandbox'],
'engine.puppeteer.executablePath': '/usr/bin/chromium-browser',
+ engine: engineBlob,
...overrides,
};
return { get: jest.fn((key: string) => values[key]) } as unknown as ConfigService;
};
- it('threads the resolved puppeteer config (executablePath/sessionDataPath) to the plugin — #219', () => {
+ it('passes ONLY engine-neutral fields to createEngine (no Puppeteer leak)', () => {
const createEngine = jest.fn().mockReturnValue({});
const pluginInstance = { type: PluginType.ENGINE, createEngine };
const pluginLoader = {
@@ -23,31 +29,39 @@ describe('EngineFactory', () => {
} as unknown as PluginLoaderService;
const factory = new EngineFactory(buildConfigService(), pluginLoader);
- factory.create({ sessionId: 'sess-1' });
-
- // Finding 2: the built-in plugin registers with an empty context config, so the
- // factory must resolve and pass these through — otherwise sessionDataPath and the
- // executable path silently fall back to relative-path defaults.
- expect(createEngine).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: 'sess-1',
- sessionDataPath: '/var/data/sessions',
- headless: true,
- puppeteerArgs: ['--no-sandbox'],
- executablePath: '/usr/bin/chromium-browser',
- }),
+ factory.create({ sessionId: 'sess-1', proxyUrl: 'http://p', proxyType: 'http' });
+
+ expect(createEngine).toHaveBeenCalledWith({ sessionId: 'sess-1', proxyUrl: 'http://p', proxyType: 'http' });
+ const passed = createEngine.mock.calls[0][0] as Record<string, unknown>;
+ for (const k of ['headless', 'puppeteerArgs', 'executablePath', 'sessionDataPath']) {
+ expect(passed).not.toHaveProperty(k);
+ }
+ });
+
+ it('registers the built-in engine with the opaque engine config blob (#219 guarantee moves to context.config)', async () => {
+ const registerBuiltInPlugin = jest.fn();
+ const pluginLoader = {
+ registerBuiltInPlugin,
+ enablePlugin: jest.fn().mockResolvedValue(undefined),
+ getPlugin: jest.fn(),
+ } as unknown as PluginLoaderService;
+
+ const factory = new EngineFactory(buildConfigService(), pluginLoader);
+ await factory.onModuleInit();
+
+ expect(registerBuiltInPlugin).toHaveBeenCalledWith(
+ expect.objectContaining({ id: 'whatsapp-web.js', type: PluginType.ENGINE }),
+ expect.anything(),
+ engineBlob,
);
});
- it('falls back to the direct adapter (with executablePath) when no engine plugin is available', () => {
+ it('falls back to the direct adapter when no engine plugin is available', () => {
const pluginLoader = {
getPlugin: jest.fn().mockReturnValue(undefined),
} as unknown as PluginLoaderService;
const factory = new EngineFactory(buildConfigService(), pluginLoader);
-
- // The fallback path must not throw and must produce an engine instance even when
- // the plugin registry is empty (legacy support).
expect(() => factory.create({ sessionId: 'sess-2' })).not.toThrow();
});
});
diff --git a/src/engine/engine.factory.ts b/src/engine/engine.factory.ts
index 0548d2c07..84c7bec39 100644
--- a/src/engine/engine.factory.ts
+++ b/src/engine/engine.factory.ts
@@ -42,7 +42,9 @@ export class EngineFactory implements OnModuleInit {
};
const wwjsPlugin = new WhatsAppWebJsPlugin();
- this.pluginLoader.registerBuiltInPlugin(wwjsManifest, wwjsPlugin);
+ // Supply the engine config sub-tree (engine.* from configuration.ts) as an opaque blob;
+ // the plugin reads its own namespace (puppeteer.*, sessionDataPath) from context.config.
+ this.pluginLoader.registerBuiltInPlugin(wwjsManifest, wwjsPlugin, this.configService.get('engine') ?? {});
// Auto-enable the configured engine
try {
@@ -65,20 +67,13 @@ export class EngineFactory implements OnModuleInit {
const enginePlugin = this.pluginLoader.getPlugin(this.engineType);
if (enginePlugin?.instance && this.isEnginePlugin(enginePlugin.instance)) {
+ // Engine-neutral per-call config only. Engine-specific config (e.g. Puppeteer for
+ // whatsapp-web.js) is supplied to the plugin as an opaque blob via context.config at
+ // registration, so the factory never assembles browser-shaped fields.
return enginePlugin.instance.createEngine({
sessionId: options.sessionId,
proxyUrl: options.proxyUrl,
proxyType: options.proxyType,
- // Resolve engine runtime config here (the built-in plugin registers with an
- // empty context config) so sessionDataPath / puppeteer settings honour the
- // environment instead of falling back to relative-path defaults.
- sessionDataPath: this.configService.get<string>('engine.sessionDataPath') ?? './data/sessions',
- headless: this.configService.get<boolean>('engine.puppeteer.headless') ?? true,
- puppeteerArgs: this.configService.get<string[]>('engine.puppeteer.args') ?? [
- '--no-sandbox',
- '--disable-setuid-sandbox',
- ],
- executablePath: this.configService.get<string>('engine.puppeteer.executablePath'),
}) as IWhatsAppEngine;
}
diff --git a/src/plugins/engines/whatsapp-web-js/index.spec.ts b/src/plugins/engines/whatsapp-web-js/index.spec.ts
index d49f3a147..5dd732c58 100644
--- a/src/plugins/engines/whatsapp-web-js/index.spec.ts
+++ b/src/plugins/engines/whatsapp-web-js/index.spec.ts
@@ -4,33 +4,38 @@ jest.mock('../../../engine/adapters/whatsapp-web-js.adapter', () => ({
import { WhatsAppWebJsPlugin } from './index';
import { WhatsAppWebJsAdapter } from '../../../engine/adapters/whatsapp-web-js.adapter';
+import { PluginContext } from '../../../core/plugins';
-describe('WhatsAppWebJsPlugin.createEngine (#219)', () => {
+describe('WhatsAppWebJsPlugin.createEngine (opaque config)', () => {
beforeEach(() => {
(WhatsAppWebJsAdapter as unknown as jest.Mock).mockClear();
});
- it('prefers per-call config over context/defaults and threads executablePath', () => {
- const plugin = new WhatsAppWebJsPlugin();
+ function withContext(plugin: WhatsAppWebJsPlugin, config: Record<string, unknown>): void {
+ // onLoad sets this.context synchronously; the returned promise can be ignored here.
+ void plugin.onLoad({ config, logger: { log: jest.fn() } } as unknown as PluginContext);
+ }
- plugin.createEngine({
- sessionId: 'sess-1',
+ it('reads browser config from context.config (the opaque engine blob), not per-call', () => {
+ const plugin = new WhatsAppWebJsPlugin();
+ withContext(plugin, {
sessionDataPath: '/data/sessions',
- headless: false,
- puppeteerArgs: ['--single-process'],
- executablePath: '/usr/bin/chromium',
+ puppeteer: { headless: false, args: ['--single-process'], executablePath: '/usr/bin/chromium' },
});
+ plugin.createEngine({ sessionId: 'sess-1', proxyUrl: 'http://p', proxyType: 'http' });
+
expect(WhatsAppWebJsAdapter).toHaveBeenCalledWith(
expect.objectContaining({
sessionId: 'sess-1',
sessionDataPath: '/data/sessions',
puppeteer: { headless: false, args: ['--single-process'], executablePath: '/usr/bin/chromium' },
+ proxy: { url: 'http://p', type: 'http' },
}),
);
});
- it('falls back to safe defaults when no config is supplied', () => {
+ it('falls back to safe defaults when context has no config', () => {
const plugin = new WhatsAppWebJsPlugin();
plugin.createEngine({ sessionId: 'sess-2' });
diff --git a/src/plugins/engines/whatsapp-web-js/index.ts b/src/plugins/engines/whatsapp-web-js/index.ts
index b8f35bcfb..d77a2cd2b 100644
--- a/src/plugins/engines/whatsapp-web-js/index.ts
+++ b/src/plugins/engines/whatsapp-web-js/index.ts
@@ -29,25 +29,22 @@ export class WhatsAppWebJsPlugin implements IEnginePlugin {
createEngine(config: Record<string, unknown>): IWhatsAppEngine {
const sessionId = config.sessionId as string;
- // Prefer the per-call config resolved by EngineFactory (from ConfigService);
- // fall back to any plugin-level context config, then to safe defaults. The
- // built-in plugin registers with an empty context config, so without the
- // per-call values sessionDataPath/headless/executablePath would silently
- // fall back to relative-path defaults and ignore the environment.
- const sessionDataPath =
- (config.sessionDataPath as string | undefined) ??
- (this.context?.config.sessionDataPath as string | undefined) ??
- './data/sessions';
- const headless =
- (config.headless as boolean | undefined) ?? (this.context?.config.headless as boolean | undefined) ?? true;
- const puppeteerArgs = (config.puppeteerArgs as string[] | undefined) ??
- (this.context?.config.puppeteerArgs as string[] | undefined) ?? ['--no-sandbox', '--disable-setuid-sandbox'];
- const executablePath =
- (config.executablePath as string | undefined) ?? (this.context?.config.executablePath as string | undefined);
-
const proxyUrl = config.proxyUrl as string | undefined;
const proxyType = config.proxyType as 'http' | 'https' | 'socks4' | 'socks5' | undefined;
+ // Browser config is this engine's OWN namespace, read from the opaque per-engine blob the
+ // factory supplies via context.config (the `engine` sub-tree in configuration.ts). The
+ // per-call config carries only engine-neutral fields (sessionId, proxy).
+ const engineConfig = (this.context?.config ?? {}) as {
+ sessionDataPath?: string;
+ puppeteer?: { headless?: boolean; args?: string[]; executablePath?: string };
+ };
+ const puppeteer = engineConfig.puppeteer ?? {};
+ const sessionDataPath = engineConfig.sessionDataPath ?? './data/sessions';
+ const headless = puppeteer.headless ?? true;
+ const puppeteerArgs = puppeteer.args ?? ['--no-sandbox', '--disable-setuid-sandbox'];
+ const executablePath = puppeteer.executablePath;
+
return new WhatsAppWebJsAdapter({
sessionId,
sessionDataPath,