From 635e268422aa885e1bffda28b65f532c72dde10d Mon Sep 17 00:00:00 2001 From: Chandra Penke Date: Fri, 2 Jun 2023 15:25:48 -0700 Subject: [PATCH] fix: error handling for streaming upload + fix streaming upload interface --- index.d.ts | 2 +- utils/stream-upload.js | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/index.d.ts b/index.d.ts index e49113e..0a6bcf4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,7 @@ export interface streamUploadInput { apiKey: string, apiSecret: string, - data: any, + stream: any, key: string, bucket?: string, } diff --git a/utils/stream-upload.js b/utils/stream-upload.js index fdb6e0c..116c310 100644 --- a/utils/stream-upload.js +++ b/utils/stream-upload.js @@ -14,24 +14,29 @@ const streamUploadFile = (s3, params) => (new Promise((resolve, reject) => { if (err) { reject(err); } - const { ETag } = data; - const hash = ETag.replace(/^"|"$/g, ''); + try { + const { ETag } = data; - const cidObj = CID.parse(hash); - - let cidv0; - - const cidv1 = cidObj.toV1().toString(); + const hash = ETag.replace(/^"|"$/g, ''); - try { - cidv0 = cidObj.toV0().toString(); - } catch (e) { - // fallback when cbor is used - cidv0 = cidv1; - } + const cidObj = CID.parse(hash); + + let cidv0; - resolve ({ hash: cidv1, hashV0: cidv0 }); + const cidv1 = cidObj.toV1().toString(); + + try { + cidv0 = cidObj.toV0().toString(); + } catch (e) { + // fallback when cbor is used + cidv0 = cidv1; + } + + resolve({ hash: cidv1, hashV0: cidv0 }); + } catch (err) { + reject(err); + } }); }));