Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): pass the factory's auth-dir bases to the engine plugin
Yudhi Armyndharis61af65b961a1
diff --git a/src/engine/engine.factory.spec.ts b/src/engine/engine.factory.spec.tsindex e3847bc37..80cc34379 100644--- a/src/engine/engine.factory.spec.ts+++ b/src/engine/engine.factory.spec.ts@@ -83,15 +83,54 @@ describe('EngineFactory', () => {factory.create({ sessionId: 'sess-1', dbSessionId: 'db-1', proxyUrl: 'http://p', proxyType: 'http' });// Plain-object (not objectContaining) assertion: any browser key (headless/puppeteerArgs/- // executablePath/sessionDataPath) leaking into the per-call config would fail this exact match.+ // executablePath) leaking into the per-call config would fail this exact match. The auth-dir+ // bases are the deliberate exception, pinned below.expect(createEngine).toHaveBeenCalledWith({sessionId: 'sess-1',dbSessionId: 'db-1',proxyUrl: 'http://p',proxyType: 'http',+ sessionDataPath: '/var/data/sessions',+ authDir: './data/baileys',});});+ // The adapters used to read these bases from the plugin config, which a PUT /api/plugins/:id/config+ // override can move, while this factory hardens and purges the dirs under the env-derived bases.+ // Credentials then landed in a directory never made owner-only and never removed on delete.+ it('hands the engine the same auth-dir bases it hardens and purges', () => {+ const createEngine = jest.fn().mockReturnValue({});+ const pluginLoader = {+ getPlugin: jest.fn().mockReturnValue({+ instance: { type: PluginType.ENGINE, createEngine },+ config: { sessionDataPath: '/data/other', baileys: { authDir: '/data/other' } },+ }),+ } as unknown as PluginLoaderService;+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'openwa-factory-bases-'));+ try {+ const factory = new EngineFactory(+ buildConfigService({+ 'engine.sessionDataPath': path.join(tmp, 'sessions'),+ 'engine.baileys.authDir': path.join(tmp, 'baileys'),+ }),+ pluginLoader,+ buildMessageStore(),+ buildLidStore(),+ buildChatStateStore(),+ );++ factory.create({ sessionId: SESSION_ID, dbSessionId: SESSION_ID });++ const passed = (createEngine.mock.calls[0] as [{ sessionDataPath: string; authDir: string }])[0];+ expect(wwjsAuthDir(passed.sessionDataPath, SESSION_ID)).toBe(wwjsAuthDir(path.join(tmp, 'sessions'), SESSION_ID));+ expect(baileysAuthDir(passed.authDir, SESSION_ID)).toBe(baileysAuthDir(path.join(tmp, 'baileys'), SESSION_ID));+ expect(fs.existsSync(wwjsAuthDir(passed.sessionDataPath, SESSION_ID))).toBe(true);+ expect(fs.existsSync(baileysAuthDir(passed.authDir, SESSION_ID))).toBe(true);+ } finally {+ fs.rmSync(tmp, { recursive: true, force: true });+ }+ });+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 = {diff --git a/src/engine/engine.factory.ts b/src/engine/engine.factory.tsindex de3377f0c..b08bf1b78 100644--- a/src/engine/engine.factory.ts+++ b/src/engine/engine.factory.ts@@ -122,14 +122,19 @@ 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.+ // Engine-neutral per-call config, plus the two auth-dir bases. 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. The bases are the+ // exception: this factory hardens and purges the credential dirs under them, and the boot+ // migration renames into them, so the engine must write there too. context.config can carry a+ // persisted plugin-config override that none of those would follow.return enginePlugin.instance.createEngine({sessionId: options.sessionId,dbSessionId: options.dbSessionId,proxyUrl: options.proxyUrl,proxyType: options.proxyType,+ sessionDataPath: this.sessionDataPath(),+ authDir: this.baileysAuthBase(),}) as IWhatsAppEngine;}@@ -201,14 +206,24 @@ export class EngineFactory implements OnModuleInit {}}+ /** The whatsapp-web.js sessionDataPath from config. */+ private sessionDataPath(): string {+ return this.configService.get<string>('engine.sessionDataPath') ?? './data/sessions';+ }++ /** The baileys authDir from config. */+ private baileysAuthBase(): string {+ return this.configService.get<string>('engine.baileys.authDir') ?? './data/baileys';+ }+/** The whatsapp-web.js LocalAuth profile dir for `sessionId`, with sessionDataPath from config. */private wwjsAuthDir(sessionId: string): string {- return wwjsAuthDir(this.configService.get<string>('engine.sessionDataPath') ?? './data/sessions', sessionId);+ return wwjsAuthDir(this.sessionDataPath(), sessionId);}/** The baileys multi-file auth dir for `sessionId`, with authDir from config. */private baileysAuthDir(sessionId: string): string {- return baileysAuthDir(this.configService.get<string>('engine.baileys.authDir') ?? './data/baileys', sessionId);+ return baileysAuthDir(this.baileysAuthBase(), sessionId);}/** True when `dir`'s base directory holds an entry with exactly that name (see readAuthDirEntries). */