-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add storage server proxy for downloading and uploading data fro…
…m relay server
- Loading branch information
Showing
7 changed files
with
163 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,7 @@ | |
"dotenv": "8.2.0", | ||
"events": "^3.2.0", | ||
"express": "^4.17.1", | ||
"fetch-retry": "^6.0.0", | ||
"get-port": "^5.1.1", | ||
"go-ipfs": "npm:[email protected]", | ||
"http-server": "^0.12.3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/backend/src/nest/storageServerProxy/storageServerProxy.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common' | ||
import { ServerProxyService } from './storageServerProxy.service' | ||
|
||
@Module({ | ||
providers: [ServerProxyService], | ||
exports: [ServerProxyService], | ||
}) | ||
export class ServerProxyServiceModule {} |
97 changes: 97 additions & 0 deletions
97
packages/backend/src/nest/storageServerProxy/storageServerProxy.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Injectable, OnModuleInit } from '@nestjs/common' | ||
import EventEmitter from 'events' | ||
import { ServerStoredCommunityMetadata } from './storageServerProxy.types' | ||
import fetchRetry from 'fetch-retry' | ||
import { SocketActionTypes } from '@quiet/types' | ||
const fetch = fetchRetry(global.fetch) | ||
// TODO: handle errors | ||
// TODO: handle retries | ||
@Injectable() | ||
export class ServerProxyService extends EventEmitter implements OnModuleInit { | ||
serverAddress: string | ||
|
||
constructor() { | ||
super() | ||
} | ||
|
||
onModuleInit() { | ||
this.on(SocketActionTypes.SET_STORAGE_SERVER_ADDRESS, (payload: { serverAddress: string }) => | ||
this.setServerAddress(payload.serverAddress) | ||
) | ||
this.on( | ||
SocketActionTypes.DOWNLOAD_STORAGE_SERVER_DATA, | ||
async (payload: { cid: string }, callback: (downloadedData: ServerStoredCommunityMetadata) => void) => { | ||
callback(await this.downloadData(payload.cid)) | ||
} | ||
) | ||
this.on( | ||
SocketActionTypes.UPLOAD_STORAGE_SERVER_DATA, | ||
async (payload: { cid: string; data: ServerStoredCommunityMetadata }, callback: (cid: string) => void) => { | ||
callback(await this.uploadData(payload.cid, payload.data)) | ||
} | ||
) | ||
} | ||
|
||
setServerAddress = (serverAddress: string) => { | ||
this.serverAddress = serverAddress | ||
} | ||
|
||
get authUrl() { | ||
const authUrl = new URL('auth', this.serverAddress) | ||
return authUrl.href | ||
} | ||
|
||
getInviteUrl = (cid: string) => { | ||
const invitationUrl = new URL('invite', this.serverAddress) | ||
invitationUrl.searchParams.append('CID', cid) | ||
return invitationUrl.href | ||
} | ||
|
||
getAuthorizationHeader = (token: string) => { | ||
return `Bearer ${token}` | ||
} | ||
|
||
auth = async () => { | ||
console.log('Authenticating') | ||
const authResponse = await fetch(this.authUrl, { | ||
method: 'POST', | ||
}) | ||
console.log('Auth response status', authResponse.status) | ||
const authResponseData = await authResponse.json() | ||
console.log('Auth response data', authResponseData) | ||
return authResponseData['access_token'] | ||
} | ||
|
||
public downloadData = async (cid: string): Promise<ServerStoredCommunityMetadata> => { | ||
console.log('Downloading data', cid) | ||
const accessToken = await this.auth() | ||
const dataResponse = await fetch(this.getInviteUrl(cid), { | ||
method: 'GET', | ||
headers: { Authorization: this.getAuthorizationHeader(accessToken) }, | ||
}) | ||
const data = await dataResponse.json() | ||
console.log('Downloaded data', data) | ||
// this.emit('storageServerDataDownloaded', data) | ||
return data | ||
} | ||
|
||
public uploadData = async (cid: string, data: ServerStoredCommunityMetadata) => { | ||
console.log('Uploading data', cid, data) | ||
const accessToken = await this.auth() | ||
const putBody = JSON.stringify(data) | ||
console.log('putBody', putBody) | ||
const dataResponsePost = await fetch(this.getInviteUrl(cid), { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: this.getAuthorizationHeader(accessToken), | ||
}, | ||
body: putBody, | ||
}) | ||
console.log('Upload data response status', dataResponsePost) | ||
// if (dataResponsePost.status === 200) { | ||
// this.emit('storageServerDataUploaded', { cid }) | ||
// } | ||
return cid | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/backend/src/nest/storageServerProxy/storageServerProxy.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type ServerStoredCommunityMetadata = { | ||
id: string | ||
ownerCertificate: string | ||
rootCa: string | ||
ownerOrbitDbIdentity: string | ||
peerList: string[] | ||
psk: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters