Skip to content

Commit

Permalink
feat: fallback to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrvis committed Sep 21, 2023
1 parent 28e0297 commit a9260ee
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/core/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ class StackService extends NodeService<Stack> {
await version.decrypt();
}
await service.setVaultContext(stack.vaultId);

const file = await this.api.downloadFile(version.getUri(StorageType.S3), { responseType: 'stream' });
let stream: ReadableStream<Uint8Array>
if (service.isPublic) {
stream = file.fileData as ReadableStream<Uint8Array>
} else {
const encryptedKey = version.encryptedKey || file.metadata.encryptedKey;
const iv = version.iv || file.metadata.iv?.split(',');
const streamChunkSize = version.chunkSize || version.size + IV_LENGTH_IN_BYTES;
stream = await service.dataEncrypter.decryptStream(file.fileData as ReadableStream, version.encryptedKey, version.iv, streamChunkSize);
stream = await service.dataEncrypter.decryptStream(file.fileData as ReadableStream, encryptedKey, iv, streamChunkSize);
}

let data: ReadableStream<Uint8Array> | ArrayBuffer;
Expand All @@ -144,7 +147,7 @@ class StackService extends NodeService<Stack> {
}

/**
* Get stack version by index, return the latest version by default
* Download stack version by index, return the latest version by default
* @param {string} stackId
* @param {number} [index] stack version index
* @returns Promise with version name & data buffer
Expand Down Expand Up @@ -242,7 +245,7 @@ class StackService extends NodeService<Stack> {
if (typeof window === 'undefined') {
const fs = (await import("fs")).default;
return new Promise((resolve, reject) =>
(stream as NodeJS.ReadableStream).pipe(fs.createWriteStream(path))
stream.pipe(fs.createWriteStream(path))
.on('error', error => reject(error))
.on('finish', () => resolve())
);
Expand Down

0 comments on commit a9260ee

Please sign in to comment.