Promisify stream

This commit is contained in:
Mel O'Hagan 2022-08-30 13:55:55 +01:00
parent 36ebb30825
commit 33fa0b50b7
1 changed files with 9 additions and 16 deletions

View File

@ -123,29 +123,22 @@ module S3Module {
} }
async readCsv(query: { bucket: string; key: string }) { async readCsv(query: { bucket: string; key: string }) {
let streamErr: string | undefined = undefined
const stream = this.client const stream = this.client
.getObject({ .getObject({
Bucket: query.bucket, Bucket: query.bucket,
Key: query.key, Key: query.key,
}) })
.createReadStream() .createReadStream()
.on("error", (err: string) => {
//stream.destroy() return new Promise((resolve, reject) => {
// stream
console.log("err ", err) .on("error", (err: Error) => {
streamErr = "ERROR" reject(err)
})
.on("finish", async () => {
resolve(csv().fromStream(stream))
})
}) })
if (streamErr) {
throw new Error("ERROR")
}
try {
return await csv().fromStream(stream)
} catch (err) {
throw new Error("Failed to read CSV")
}
} }
} }