diff --git a/src/__tests__/common.ts b/src/__tests__/common.ts index 0cd99e3c..b1db34b1 100644 --- a/src/__tests__/common.ts +++ b/src/__tests__/common.ts @@ -8,10 +8,10 @@ export async function initInstance(email: string, password: string): Promise { +export const vaultCreate = async (akord: Akord, cacheOnly = true) => { const name = faker.random.words(); const termsOfAccess = faker.lorem.sentences(); - const { vaultId, membershipId } = await akord.vault.create(name, { termsOfAccess, cacheOnly: true }); + const { vaultId, membershipId } = await akord.vault.create(name, { termsOfAccess, cacheOnly: cacheOnly }); const membership = await akord.membership.get(membershipId); expect(membership.status).toEqual("ACCEPTED"); diff --git a/src/__tests__/manifest.test.ts b/src/__tests__/manifest.test.ts index 05703b85..2a94300c 100644 --- a/src/__tests__/manifest.test.ts +++ b/src/__tests__/manifest.test.ts @@ -44,7 +44,7 @@ describe("Testing manifest functions", () => { beforeAll(async () => { akord = await initInstance(email, password); - vaultId = (await vaultCreate(akord)).vaultId; + vaultId = (await vaultCreate(akord, false)).vaultId; // upload html file const htmlSrc = "

Hello World

"; diff --git a/src/api/akord-api.ts b/src/api/akord-api.ts index 236289f5..1f0be3b9 100644 --- a/src/api/akord-api.ts +++ b/src/api/akord-api.ts @@ -212,12 +212,10 @@ export default class AkordApi extends Api { }; public async getNodeState(stateId: string): Promise { - const { response } = await new ApiClient() + return await new ApiClient() .env(this.config) .resourceId(stateId) .downloadState() - - return response.data }; public async getNotifications(): Promise> { diff --git a/src/core/batch.ts b/src/core/batch.ts index bf33b526..8c92f4c1 100644 --- a/src/core/batch.ts +++ b/src/core/batch.ts @@ -361,7 +361,7 @@ class BatchService extends Service { service.setObjectId(item.id); service.setObjectType(item.type); service.arweaveTags = await service.getTxTags(); - const { id, object } = await this.api.postContractTransaction(this.vaultId, item.input, service.arweaveTags); + const { id, object } = await this.api.postContractTransaction(service.vaultId, item.input, service.arweaveTags); const processedObject = item.type === objectType.MEMBERSHIP ? await (service).processMembership(object as Membership, !batchService.isPublic, batchService.keys) : await (>service).processNode(object as any, !batchService.isPublic, batchService.keys) as any; diff --git a/src/core/file.ts b/src/core/file.ts index 77ed82e6..0b597ae1 100644 --- a/src/core/file.ts +++ b/src/core/file.ts @@ -15,99 +15,12 @@ import { InternalError } from "../errors/internal-error"; const DEFAULT_FILE_TYPE = "text/plain"; const DEFAULT_CHUNK_SIZE_IN_BYTES = 10000000; //10MB export const IV_LENGTH_IN_BYTES = 16; -const DEFAULT_CHUNK_SIZE_WITH_IV_IN_BYTES = DEFAULT_CHUNK_SIZE_IN_BYTES + IV_LENGTH_IN_BYTES; class FileService extends Service { //asyncUploadTreshold = 209715200; asyncUploadTreshold = 20000000; contentType = null as string; - /** - * Returns file as ArrayBuffer. Puts the whole file into memory. - * For downloading without putting whole file to memory use FileService#download() - * @param {string} id file resource url - * @param {string} vaultId - * @param {DownloadOptions} [options] - * @returns Promise with file buffer - */ - // public async get(id: string, vaultId: string, options: DownloadOptions = {}): Promise { - // const service = new FileService(this.wallet, this.api); - // await service.setVaultContext(vaultId); - // const downloadOptions = options as FileDownloadOptions; - // downloadOptions.public = service.isPublic; - // let fileBinary: ArrayBuffer; - // if (options.isChunked) { - // const chunkSize: number = options.chunkSize || (downloadOptions.public ? DEFAULT_CHUNK_SIZE_IN_BYTES : DEFAULT_CHUNK_SIZE_WITH_IV_IN_BYTES); - // let currentChunk = 0; - // while (currentChunk < options.numberOfChunks) { - // const url = `${id}_${currentChunk}`; - // downloadOptions.loadedSize = currentChunk * chunkSize - // const chunkBinary = await service.getBinary(url, downloadOptions); - // fileBinary = service.appendBuffer(fileBinary, chunkBinary); - // currentChunk++; - // } - // } else { - // const { fileData, metadata } = await this.api.downloadFile(id, downloadOptions); - // fileBinary = await service.processReadRaw(fileData, metadata) - // } - // return fileBinary; - // } - - /** - * Downloads the file keeping memory consumed (RAM) under defiend level: this#chunkSize. - * In browser, streaming of the binary requires self hosting of mitm.html and sw.js - * See: https://github.com/jimmywarting/StreamSaver.js#configuration - * @param {string} id file resource url - * @param {string} vaultId - * @param {DownloadOptions} [options] - * @returns Promise with file buffer - */ - // public async download(id: string, vaultId: string, options: DownloadOptions = {}): Promise { - // const service = new FileService(this.wallet, this.api); - // await service.setVaultContext(vaultId); - // const downloadOptions = options as FileDownloadOptions; - // downloadOptions.public = service.isPublic; - - // if (typeof window !== 'undefined') { - // if (!service.isPublic) { - // if (!navigator.serviceWorker?.controller) { - // throw new InternalError("Decryption service worker is not running") - // } - // navigator.serviceWorker.controller.postMessage({ - // keys: [], - // id: 'test' - // }); - // } - // } - - // const writer = await service.stream(options.name, options.resourceSize); - // if (options.isChunked) { - // let currentChunk = 0; - // try { - // while (currentChunk < options.numberOfChunks) { - // const url = `${id}_${currentChunk}`; - // // downloadOptions.loadedSize = currentChunk * service.chunkSize; - // const fileBinary = await service.getBinary(url, downloadOptions); - // if (writer instanceof WritableStreamDefaultWriter) { - // await writer.ready - // } - // await writer.write(new Uint8Array(fileBinary)); - // currentChunk++; - // } - // } catch (err) { - // throw new Error(err); - // } finally { - // if (writer instanceof WritableStreamDefaultWriter) { - // await writer.ready - // } - // await writer.close(); - // } - // } else { - // const fileBinary = await service.getBinary(id, downloadOptions); - // await writer.write(new Uint8Array(fileBinary)); - // await writer.close(); - // } - //} public async create( file: FileLike, @@ -146,28 +59,6 @@ class FileService extends Service { return { file, resourceUri }; } - // public async stream(path: string, size?: number): Promise { - // if (typeof window === 'undefined') { - // const fs = (await import("fs")).default; - // return fs.createWriteStream(path); - // } - // else { - // const streamSaver = (await import('streamsaver')).default; - // if (!streamSaver.WritableStream) { - // const pony = await import('web-streams-polyfill/ponyfill'); - // streamSaver.WritableStream = pony.WritableStream as unknown as typeof streamSaver.WritableStream; - // } - // if (window.location.protocol === 'https:' - // || window.location.protocol === 'chrome-extension:' - // || window.location.hostname === 'localhost') { - // streamSaver.mitm = '/streamsaver/mitm.html'; - // } - - // const fileStream = streamSaver.createWriteStream(path, { size: size, writableStrategy: new ByteLengthQueuingStrategy({ highWaterMark: 3 * this.chunkSize }) }); - // return fileStream.getWriter(); - // } - // } - public async newVersion(file: FileLike, uploadResult: FileUploadResult): Promise { const version = new FileVersion({ owner: await this.wallet.getAddress(), @@ -304,30 +195,6 @@ class FileService extends Service { }; } - private appendBuffer(buffer1: ArrayBuffer, buffer2: ArrayBuffer): ArrayBufferLike { - if (!buffer1 && !buffer2) return; - if (!buffer1) return buffer2; - if (!buffer2) return buffer1; - var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength); - tmp.set(new Uint8Array(buffer1), 0); - tmp.set(new Uint8Array(buffer2), buffer1.byteLength); - return tmp.buffer; - } - - // private async getBinary(id: string, options: FileDownloadOptions) { - // try { - // options.public = this.isPublic; - // const { fileData, metadata } = await this.api.downloadFile(id, options); - // return await this.processReadRaw(fileData, metadata); - // } catch (e) { - // Logger.log(e); - // throw new Error( - // "Failed to download. Please check your network connection." + - // " Please upload the file again if problem persists and/or contact Akord support." - // ); - // } - // } - private getFileTags(file: FileLike, options: FileUploadOptions = {}): Tags { const tags = [] as Tags; if (this.isPublic) { diff --git a/src/core/membership.ts b/src/core/membership.ts index 5149a613..ebd41735 100644 --- a/src/core/membership.ts +++ b/src/core/membership.ts @@ -419,6 +419,8 @@ class MembershipService extends Service { protected async setVaultContextFromMembershipId(membershipId: string, vaultId?: string) { const membership = await this.api.getMembership(membershipId, vaultId); + const vault = await this.api.getVault(membership.vaultId); + this.setVault(vault); this.setVaultId(membership.vaultId); this.setIsPublic(membership.__public__); await this.setMembershipKeys(membership); diff --git a/src/core/node.ts b/src/core/node.ts index 1c658326..fc435a70 100644 --- a/src/core/node.ts +++ b/src/core/node.ts @@ -213,6 +213,8 @@ class NodeService extends Service { protected async setVaultContextFromNodeId(nodeId: string, type: NodeType, vaultId?: string) { const object = await this.api.getNode(nodeId, type, vaultId); + const vault = await this.api.getVault(object.vaultId); + this.setVault(vault); this.setVaultId(object.vaultId); this.setIsPublic(object.__public__); await this.setMembershipKeys(object);