Skip to content

Commit

Permalink
formatting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
valorant-dhruv committed Feb 13, 2024
1 parent e155ad0 commit 60bbff3
Showing 1 changed file with 31 additions and 55 deletions.
86 changes: 31 additions & 55 deletions packages/connect/src/connect-s3.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,56 @@
import {
DownloadMetaFnParams,
DownloadDataFnParams,
UploadMetaFnParams,
UploadDataFnParams,
} from "./types";
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);
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 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);
}

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 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();
})
const result = await done.json()
if (result.status != 201)
throw new Error(
"failed to upload data " + JSON.parse(result.body).message
);
{
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 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());
Expand Down Expand Up @@ -117,25 +97,21 @@ export class ConnectS3 extends Connection {
*/
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 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
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 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]))
)
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);
return events.map((e) => e.bytes)
}
}

0 comments on commit 60bbff3

Please sign in to comment.