Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv Soni authored and Dhruv Soni committed Mar 26, 2024
1 parent 1fa0a01 commit b236f43
Showing 1 changed file with 29 additions and 60 deletions.
89 changes: 29 additions & 60 deletions packages/connect/src/connect-s3.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
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 {
Expand All @@ -16,55 +11,39 @@ export class ConnectS3 extends Connection {
messagePromise: Promise<Uint8Array[]>
messageResolve?: (value: Uint8Array[] | PromiseLike<Uint8Array[]>) => void


constructor(upload: string, download: string, websocket: string) {
super()
this.uploadUrl = new URL(upload)
this.downloadUrl = new URL(download)
this.ws = new WebSocket(websocket)

this.messagePromise = new Promise<Uint8Array[]>((resolve, reject) => {
this.messageResolve = resolve;
})
}

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);
console.log("The base64 string when data was uploaded", base64String);

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),
Expand All @@ -73,21 +52,17 @@ export class ConnectS3 extends Connection {
if (result.status != 201) {
throw new Error("failed to upload data " + JSON.parse(result.body).message)
}
this.parents = [event.cid];

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
}


Expand All @@ -99,7 +74,6 @@ export class ConnectS3 extends Connection {
this.ws.addEventListener("message", async (event: any) => {
const data = JSON.parse(event.data);
const bytes = Base64.toUint8Array(data.items[0].data)

const afn = async () => {
console.log("Inside the afn");
const uint8ArrayBuffer = bytes as Uint8Array
Expand Down Expand Up @@ -167,19 +141,14 @@ export class ConnectS3 extends Connection {
response = JSON.parse(response.body).items
const events = await Promise.all(
response.map(async (element: any) => {
const base64String = element.data;
console.log("The base64 string in metadownload", base64String);
const bytes = Base64.toUint8Array(base64String);
return { cid: element.cid, bytes };
const base64String = element.data
const bytes = Base64.toUint8Array(base64String)
return { cid: element.cid, bytes }
})
);
console.log("These are the events in metaDownload", events);
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);

)
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)
}
}

0 comments on commit b236f43

Please sign in to comment.