Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): start the storage export only once its file can be written
Yudhi Armyndharisd2ae70238851
diff --git a/src/modules/infra/infra-storage.controller.spec.ts b/src/modules/infra/infra-storage.controller.spec.tsindex 152d09905..8501e80eb 100644--- a/src/modules/infra/infra-storage.controller.spec.ts+++ b/src/modules/infra/infra-storage.controller.spec.ts@@ -484,3 +484,44 @@ describe('InfraStorageController.importStorage reports an import that wrote nothawait expect(importWith({ imported: 0, failed: 0 })).resolves.toMatchObject({ imported: true, count: 0 });});});++describe('InfraStorageController export setup failure', () => {+ it('prepares the directory before starting archive work', async () => {+ const filesystem = jest.requireMock<typeof import('fs')>('fs');+ const mkdir = jest.spyOn(filesystem, 'mkdirSync').mockImplementation(() => {+ throw new Error('directory unavailable');+ });+ const source = Readable.from(['archive']);+ const create = jest.fn().mockResolvedValue(source);+ (fs.existsSync as jest.Mock).mockReturnValueOnce(false);+ const controller = new InfraStorageController({+ getCurrentStorageType: () => 'local',+ createExportStream: create,+ } as never);+ try {+ await expect(controller.exportStorage()).rejects.toThrow('directory unavailable');+ expect(create).not.toHaveBeenCalled();+ } finally {+ mkdir.mockRestore();+ source.destroy();+ }+ });++ it('closes the archive if constructing the destination throws', async () => {+ const source = Readable.from(['archive']);+ (fs.existsSync as jest.Mock).mockReturnValueOnce(true);+ (fs.createWriteStream as jest.Mock).mockImplementationOnce(() => {+ throw new Error('destination unavailable');+ });+ const controller = new InfraStorageController({+ getCurrentStorageType: () => 'local',+ createExportStream: jest.fn().mockResolvedValue(source),+ } as never);+ try {+ await expect(controller.exportStorage()).rejects.toThrow('destination unavailable');+ expect(source.destroyed).toBe(true);+ } finally {+ source.destroy();+ }+ });+});diff --git a/src/modules/infra/infra-storage.controller.ts b/src/modules/infra/infra-storage.controller.tsindex 0a17b50ae..14dea6934 100644--- a/src/modules/infra/infra-storage.controller.ts+++ b/src/modules/infra/infra-storage.controller.ts@@ -184,9 +184,6 @@ export class InfraStorageController implements OnApplicationBootstrap {await this.assertStorageBackendReady();// Note: In production, this would return a StreamableFile// For simplicity, we'll save to a temp file and return the path- const stream = await this.storageService- .createExportStream()- .catch((error: unknown) => InfraStorageController.refusalAsConflict(error));// Keep the export INSIDE data/ (under data/exports/): the import handler only accepts paths under// data/, and the documented backend-migration flow re-imports this file AFTER a container restart,// so it must live on the persistent volume — the OS temp dir is wiped on restart. The original@@ -198,15 +195,19 @@ export class InfraStorageController implements OnApplicationBootstrap {}const exportPath = path.join(exportDir, `storage-export-${Date.now()}-${randomUUID()}.tar.gz`);- const writeStream = fs.createWriteStream(exportPath);- stream.pipe(writeStream);-+ // Created only once the destination can be prepared: the archive starts reading files at once, and one+ // nothing consumes would hold its open file and buffered bytes until the process exits.+ const stream = await this.storageService+ .createExportStream()+ .catch((error: unknown) => InfraStorageController.refusalAsConflict(error));+ let writeStream: fs.WriteStream | undefined;try {+ const sink = (writeStream = fs.createWriteStream(exportPath));await new Promise<void>((resolve, reject) => {- writeStream.on('finish', resolve);+ sink.on('finish', resolve);// The archive is filled while it is written: destroying the source stops the export from// opening further files once the sink has failed.- writeStream.on('error', (err: Error) => {+ sink.on('error', (err: Error) => {stream.destroy();reject(err);});@@ -214,15 +215,21 @@ export class InfraStorageController implements OnApplicationBootstrap {// the source stream, which without a listener crashes the process. Fail the request instead and// tear down the sink so its fd isn't held open waiting for a 'finish' that never comes.stream.on('error', (err: Error) => {- writeStream.destroy();+ sink.destroy();reject(err);});+ stream.pipe(sink);});} catch (error) {// The TTL sweep below only covers a finished archive; a failed one would otherwise stay on the// data volume until a restart a day later. Wait for the sink to close so the unlink cannot race// an open that is still pending.- if (!writeStream.closed) await new Promise<void>(resolve => writeStream.once('close', () => resolve()));+ stream.destroy();+ if (writeStream) {+ const sink = writeStream;+ sink.destroy();+ if (!sink.closed) await new Promise<void>(resolve => sink.once('close', () => resolve()));+ }await fs.promises.unlink(exportPath).catch(() => undefined);throw error;}