Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(storage): carry partial counts on an aborted import

Yudhi Armyndharisee46e62d08b1
diff --git a/src/common/storage/storage-transfer.spec.ts b/src/common/storage/storage-transfer.spec.ts
index 65235eaee..502f0376b 100644
--- a/src/common/storage/storage-transfer.spec.ts
+++ b/src/common/storage/storage-transfer.spec.ts
@@ -245,3 +245,32 @@ describe('importFromStream writes regular files only', () => {
expect(result).toEqual({ imported: 1, failed: 0 });
});
});
+
+describe('importFromStream reports partial counts on an abort', () => {
+ it('carries the entries already written on an entry-cap rejection', async () => {
+ const prev = process.env.STORAGE_IMPORT_MAX_ENTRIES;
+ process.env.STORAGE_IMPORT_MAX_ENTRIES = '2';
+ const pack = tar.pack();
+ for (const name of ['a.jpg', 'b.jpg', 'c.jpg']) pack.entry({ name }, Buffer.from(name));
+ pack.finalize();
+ const written: string[] = [];
+ try {
+ const rejection = await importFromStream(
+ pack.pipe(createGzip()),
+ name => {
+ written.push(name);
+ return Promise.resolve();
+ },
+ makeLogger() as never,
+ ).catch((err: unknown) => err);
+
+ expect(rejection).toBeInstanceOf(Error);
+ expect((rejection as Error).message).toMatch(/2-entry limit/);
+ expect(rejection).toMatchObject({ imported: 2, failed: 0 });
+ expect(written).toEqual(['a.jpg', 'b.jpg']);
+ } finally {
+ if (prev === undefined) delete process.env.STORAGE_IMPORT_MAX_ENTRIES;
+ else process.env.STORAGE_IMPORT_MAX_ENTRIES = prev;
+ }
+ });
+});
diff --git a/src/common/storage/storage-transfer.ts b/src/common/storage/storage-transfer.ts
index 7a62d7aa6..c9a3dbabf 100644
--- a/src/common/storage/storage-transfer.ts
+++ b/src/common/storage/storage-transfer.ts
@@ -205,7 +205,9 @@ export async function importFromStream(
gunzip.destroy();
// Destroying the input mid-pipe stops the source; without an error arg it emits no 'error'.
inputStream.destroy();
- reject(err);
+ // The counts reached so far ride on the rejection for the caller's audit row. A lower bound: a
+ // putFile already in flight when a gzip or input error lands can still complete afterwards.
+ reject(Object.assign(err, { imported: importedCount, failed: failedCount }));
};
// Every stream in the pipeline needs an 'error' listener: an EventEmitter with none CRASHES the
// process on error. pipe() does not forward errors, so a corrupt gzip (zlib error on gunzip) or