From 33fa0b50b7acb18759297707b2e0d434b7d6ed33 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Tue, 30 Aug 2022 13:55:55 +0100 Subject: [PATCH] Promisify stream --- packages/server/src/integrations/s3.ts | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/server/src/integrations/s3.ts b/packages/server/src/integrations/s3.ts index ddae25eee8..43aa04de6a 100644 --- a/packages/server/src/integrations/s3.ts +++ b/packages/server/src/integrations/s3.ts @@ -123,29 +123,22 @@ module S3Module { } async readCsv(query: { bucket: string; key: string }) { - let streamErr: string | undefined = undefined const stream = this.client .getObject({ Bucket: query.bucket, Key: query.key, }) .createReadStream() - .on("error", (err: string) => { - //stream.destroy() - // - console.log("err ", err) - streamErr = "ERROR" - }) - if (streamErr) { - throw new Error("ERROR") - } - - try { - return await csv().fromStream(stream) - } catch (err) { - throw new Error("Failed to read CSV") - } + return new Promise((resolve, reject) => { + stream + .on("error", (err: Error) => { + reject(err) + }) + .on("finish", async () => { + resolve(csv().fromStream(stream)) + }) + }) } }