Skip to content

Commit

Permalink
fix: nodejs download stream
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrvis committed Sep 21, 2023
1 parent d47a6f3 commit fc7e7e7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ lib/*
npm-debug.log*
yarn-debug.log*
yarn-error.log*

src/__tests__/data/out
1 change: 1 addition & 0 deletions src/__tests__/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ export const noteCreate = async (akord: Akord, vaultId: string) => {
}

export const testDataPath = "./src/__tests__/data/";
export const testDataOutPath = "./src/__tests__/data/out";
14 changes: 13 additions & 1 deletion src/__tests__/stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Akord } from "../index";
import faker from '@faker-js/faker';
import { initInstance, testDataPath, vaultCreate } from './common';
import { initInstance, testDataPath, testDataOutPath, vaultCreate } from './common';
import { email, password } from './data/test-credentials';
import { NodeJs } from "../types/file-like";
import { getTxData } from "../arweave";
Expand All @@ -27,6 +27,10 @@ describe("Testing stack functions", () => {
stackId = await stackCreate(vaultId);
});

it("should download the stack", async () => {
await stackDownload(stackId);
});

it("should upload new revision", async () => {
await stackUploadRevision(stackId);
});
Expand Down Expand Up @@ -66,6 +70,14 @@ export const stackCreate = async (vaultId: string) => {
return stackId;
}

export const stackDownload = async (stackId: string) => {
const { version, data } = await akord.stack.getVersion(stackId, { responseType: 'arraybuffer' });
await akord.stack.download(stackId, { path: testDataOutPath });
const file = await NodeJs.File.fromPath(`${testDataOutPath}/${version.name}`);

expect(data).toEqual(await file.arrayBuffer());
}

export const stackUploadRevision = async (stackId: string) => {
const file = await NodeJs.File.fromPath(testDataPath + secondFileName);

Expand Down
7 changes: 4 additions & 3 deletions src/core/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ 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) {
Expand Down Expand Up @@ -156,7 +156,7 @@ class StackService extends NodeService<Stack> {
let downloadPromise: Promise<void>
if (typeof window === 'undefined' || !navigator.serviceWorker?.controller) {
const { version, data } = await this.getVersion(stackId, { ...options, responseType: 'stream' });
const path = options.path ? options.path : version.name;
const path = `${options.path}/${version.name}`;
const contentType = version.type;
downloadPromise = this.saveFile(path, contentType, data);
} else {
Expand Down Expand Up @@ -244,8 +244,9 @@ class StackService extends NodeService<Stack> {
private async saveFile(path: string, type: string, stream: any): Promise<void> {
if (typeof window === 'undefined') {
const fs = (await import("fs")).default;
const Readable = (await import("stream")).Readable;
return new Promise((resolve, reject) =>
stream.pipe(fs.createWriteStream(path))
Readable.from(stream).pipe(fs.createWriteStream(path))
.on('error', error => reject(error))
.on('finish', () => resolve())
);
Expand Down

0 comments on commit fc7e7e7

Please sign in to comment.