Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(storage): fail the export when the S3 bucket itself is gone
Yudhi Armyndharisabacc2c7bb58
diff --git a/src/common/storage/storage-transfer.spec.ts b/src/common/storage/storage-transfer.spec.tsindex 7480025ac..65235eaee 100644--- a/src/common/storage/storage-transfer.spec.ts+++ b/src/common/storage/storage-transfer.spec.ts@@ -59,11 +59,18 @@ const contentFor = (name: string): Buffer => {};/** An openFile that counts the file streams open at once and the files ever opened. */-function trackingOpener(failing: Record<string, 'gone' | 'open' | 'read'> = {}, withSize = true) {+function trackingOpener(failing: Record<string, 'gone' | 'bucket' | 'open' | 'read'> = {}, withSize = true) {const stats = { opened: 0, open: 0, maxOpen: 0 };const openFile = (name: string): Promise<ExportFileSource> => {if (failing[name] === 'gone')return Promise.reject(Object.assign(new Error(`ENOENT: ${name}`), { code: 'ENOENT' }));+ if (failing[name] === 'bucket')+ return Promise.reject(+ Object.assign(new Error('The specified bucket does not exist'), {+ name: 'NoSuchBucket',+ $metadata: { httpStatusCode: 404 },+ }),+ );if (failing[name] === 'open') return Promise.reject(new Error(`SlowDown: ${name}`));const data = contentFor(name);stats.opened++;@@ -142,6 +149,22 @@ describe('createExportStream streams one file at a time', () => {expect(stats.opened).toBe(2);});+ it('fails the output when the bucket itself is gone, not only one object', async () => {+ const { stats, openFile } = trackingOpener({ 'media/file-c.bin': 'bucket' });+ const logger = makeLogger();+ const output = await createExportStream(() => Promise.resolve(files), openFile, logger as never);++ const error = await new Promise<Error>(resolve => {+ output.on('error', resolve);+ output.resume();+ });+ await settle(20);++ expect(error.name).toBe('NoSuchBucket');+ expect(stats.opened).toBe(2);+ expect(logger.warn).not.toHaveBeenCalledWith('Failed to export file: media/file-c.bin', expect.anything());+ });+it('still exports a file whose size the backend did not report', async () => {const { openFile } = trackingOpener({}, false);const logger = makeLogger();diff --git a/src/common/storage/storage-transfer.ts b/src/common/storage/storage-transfer.tsindex 2e79c3210..7a62d7aa6 100644--- a/src/common/storage/storage-transfer.ts+++ b/src/common/storage/storage-transfer.ts@@ -119,7 +119,11 @@ async function appendEntries(try {source = await openFile(file);} catch (error) {- if (!isMissingObjectError(error)) throw error;+ // Only a per-object miss is skippable: ENOENT locally, NoSuchKey from S3 (openS3File returns it+ // once the local fallback also misses). isMissingObjectError also counts any 404, which includes+ // a bucket that is gone, and skipping that would report a partial archive as a finished export.+ const { code, name } = error as { code?: string; name?: string };+ if (code !== 'ENOENT' && name !== 'NoSuchKey') throw error;logger.warn(`Failed to export file: ${file}`, { error: String(error) });continue;}