diff --git a/packages/connect/src/connect-s3.ts b/packages/connect/src/connect-s3.ts index 62113b32..482c88ac 100644 --- a/packages/connect/src/connect-s3.ts +++ b/packages/connect/src/connect-s3.ts @@ -1,55 +1,80 @@ -import { DownloadMetaFnParams, DownloadDataFnParams, UploadMetaFnParams, UploadDataFnParams } from './types' -import { Connection } from './connection' -import fetch from 'cross-fetch' -import { Base64 } from 'js-base64' +import { + DownloadMetaFnParams, + DownloadDataFnParams, + UploadMetaFnParams, + UploadDataFnParams, +} from "./types"; +import { Connection } from "./connection"; +import fetch from "cross-fetch"; +import { Base64 } from "js-base64"; export class ConnectS3 extends Connection { - uploadUrl: URL - downloadUrl: URL + uploadUrl: URL; + downloadUrl: URL; constructor(upload: string, download: string) { - super() - this.uploadUrl = new URL(upload) - this.downloadUrl = new URL(download) + super(); + this.uploadUrl = new URL(upload); + this.downloadUrl = new URL(download); } async dataUpload(bytes: Uint8Array, params: UploadDataFnParams) { // console.log('s3 dataUpload', params.car.toString()) - const fetchUploadUrl = new URL(`?${new URLSearchParams({ cache: Math.random().toString(), ...params }).toString()}`, this.uploadUrl) - const response = await fetch(fetchUploadUrl) + const fetchUploadUrl = new URL( + `?${new URLSearchParams({ cache: Math.random().toString(), ...params }).toString()}`, + this.uploadUrl + ); + const response = await fetch(fetchUploadUrl); if (!response.ok) { // console.log('failed to get upload url for data', params, response) - throw new Error('failed to get upload url for data ' + new Date().toISOString() + ' ' + response.statusText) + throw new Error( + "failed to get upload url for data " + + new Date().toISOString() + + " " + + response.statusText + ); } - const { uploadURL } = await response.json() as { uploadURL: string } - const done = await fetch(uploadURL, { method: 'PUT', body: bytes }) + const { uploadURL } = (await response.json()) as { uploadURL: string }; + const done = await fetch(uploadURL, { method: "PUT", body: bytes }); // console.log('s3 dataUpload done', params.car.toString(), done) - if (!done.ok) throw new Error('failed to upload data ' + done.statusText) + if (!done.ok) throw new Error("failed to upload data " + done.statusText); } async metaUpload(bytes: Uint8Array, params: UploadMetaFnParams) { - const event = await this.createEventBlock(bytes) - const base64String = Base64.fromUint8Array(bytes) + const event = await this.createEventBlock(bytes); + const base64String = Base64.fromUint8Array(bytes); const crdtEntry = { cid: event.cid.toString(), data: base64String, - parents: this.parents.map(p => p.toString()) - } - const fetchUploadUrl = new URL(`?${new URLSearchParams({ type: 'meta', ...params }).toString()}`, this.uploadUrl) - const done = await fetch(fetchUploadUrl, { method: 'PUT', body: JSON.stringify(crdtEntry) }) - const result=await done.json(); - if (result.status!=201) throw new Error('failed to upload data ' + JSON.parse(result.body).message) - this.parents = [event.cid] - return null + parents: this.parents.map((p) => p.toString()), + }; + const fetchUploadUrl = new URL( + `?${new URLSearchParams({ type: "meta", ...params }).toString()}`, + this.uploadUrl + ); + const done = await fetch(fetchUploadUrl, { + method: "PUT", + body: JSON.stringify(crdtEntry), + }); + const result = await done.json(); + if (result.status != 201) + throw new Error( + "failed to upload data " + JSON.parse(result.body).message + ); + this.parents = [event.cid]; + return null; } async dataDownload(params: DownloadDataFnParams) { - const { type, name, car } = params - const fetchFromUrl = new URL(`${type}/${name}/${car}.car`, this.downloadUrl) - const response = await fetch(fetchFromUrl) - if (!response.ok) return null // throw new Error('failed to download data ' + response.statusText) - const bytes = new Uint8Array(await response.arrayBuffer()) - return bytes + const { type, name, car } = params; + const fetchFromUrl = new URL( + `${type}/${name}/${car}.car`, + this.downloadUrl + ); + const response = await fetch(fetchFromUrl); + if (!response.ok) return null; // throw new Error('failed to download data ' + response.statusText) + const bytes = new Uint8Array(await response.arrayBuffer()); + return bytes; } /** @@ -91,21 +116,26 @@ export class ConnectS3 extends Connection { * @returns - Returns the metadata bytes as a Uint8Array or null if the fetch is unsuccessful. */ async metaDownload(params: DownloadMetaFnParams) { - const { name, branch } = params - const fetchUploadUrl = new URL(`?${new URLSearchParams({ type: 'meta', ...params }).toString()}`, this.uploadUrl) - const data=await fetch(fetchUploadUrl) - let response=await data.json(); - if(response.status!=200) throw new Error("Failed to download data"); - response=JSON.parse(response.body).items - console.log("This is the response",response) - const events=await Promise.all( - response.map(async (element: any)=> - { - const base64String = element.data - const bytes = Base64.toUint8Array(base64String) - return {cid: element.cid, bytes} - }) - ) - return events.map(e => e.bytes) + const { name, branch } = params; + const fetchUploadUrl = new URL( + `?${new URLSearchParams({ type: "meta", ...params }).toString()}`, + this.uploadUrl + ); + const data = await fetch(fetchUploadUrl); + let response = await data.json(); + if (response.status != 200) throw new Error("Failed to download data"); + response = JSON.parse(response.body).items; + console.log("This is the response", response); + const events = await Promise.all( + response.map(async (element: any) => { + const base64String = element.data; + const bytes = Base64.toUint8Array(base64String); + return { cid: element.cid, bytes }; + }) + ); + const cids = events.map(e => e.cid) + const uniqueParentsMap = new Map([...this.parents, ...cids].map(p => [p.toString(), p])) + this.parents = Array.from(uniqueParentsMap.values()) + return events.map((e) => e.bytes); } }