Skip to content

Commit

Permalink
improved version
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 c96fd36 commit afcd914
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
20 changes: 15 additions & 5 deletions packages/connect/src/connect-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import { Base64 } from "js-base64"
export class ConnectS3 extends Connection {
uploadUrl: URL
downloadUrl: URL
ws: WebSocket
ws: WebSocket | undefined
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)
if(websocket.length!=0)
{
this.ws = new WebSocket(websocket)
}
else{
this.ws=undefined
}
this.messagePromise = new Promise<Uint8Array[]>((resolve, reject) => {
this.messageResolve = resolve;
})
Expand Down Expand Up @@ -48,7 +54,8 @@ export class ConnectS3 extends Connection {
body: JSON.stringify(crdtEntry),
})
const result = await done.json()
if (result.status != 201) {
if (result.status != 201)
{
throw new Error("failed to upload data " + JSON.parse(result.body).message)
}
this.parents = [event.cid]
Expand All @@ -66,15 +73,18 @@ export class ConnectS3 extends Connection {


async onConnect() {
console.log("Is the onconnect function being called?")
if (!this.loader || !this.taskManager) {
throw new Error("loader and taskManager must be set")
}

if(this.ws==undefined)
{
return;
}
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
const eventBlock = await this.createEventBlock(uint8ArrayBuffer)
await this.taskManager!.handleEvent(eventBlock)
Expand Down
12 changes: 9 additions & 3 deletions packages/connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ class ConnectRaw extends Connection {
}

export const connect = {
s3free: ({ blockstore }: Connectable, databasename: string) => {
// const upload = 'https://udvtu5wy39.execute-api.us-east-2.amazonaws.com/uploads'
s3free: ({ blockstore }: Connectable) => {
const upload = 'https://udvtu5wy39.execute-api.us-east-2.amazonaws.com/uploads'
const download = 'https://crdt-s3uploadbucket-dcjyurxwxmba.s3.us-east-2.amazonaws.com'
const websocket=""
const connection = new ConnectS3(upload, download, websocket)
connection.connect(blockstore)
return connection
},
awsfree: ({ blockstore }: Connectable, databasename: string) => {
const upload = 'https://aq0pbyfywg.execute-api.us-east-1.amazonaws.com/uploads'
// const download = 'https://crdt-s3uploadbucket-dcjyurxwxmba.s3.us-east-2.amazonaws.com'
const download = 'https://fireproof-aws-connector-s3uploadbucket-yll7d1l9zlyh.s3.amazonaws.com'
const websocket = `wss://fhpo61crph.execute-api.us-east-1.amazonaws.com/Prod?database=${databasename}`
const connection = new ConnectS3(upload, download, websocket)
Expand Down
1 change: 1 addition & 0 deletions packages/fireproof/test/www/todo-aws.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
dbName = name
db = fireproof(name, { autoCompact: 100 })
cx = connect.s3(db,{ upload: 'https://aq0pbyfywg.execute-api.us-east-1.amazonaws.com/uploads', download:'https://fireproof-aws-connector-s3uploadbucket-yll7d1l9zlyh.s3.amazonaws.com', websocket:'wss://fhpo61crph.execute-api.us-east-1.amazonaws.com/Prod',databasename: dbName })
// cx=connect.s3free(db);
cx.ready.then(async () => {
const span = document.querySelector('#cxInfo')
span.innerText = `📡`
Expand Down

0 comments on commit afcd914

Please sign in to comment.