Skip to content

Commit

Permalink
merge connect to encrypted-blockstore
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv Soni authored and Dhruv Soni committed Apr 10, 2024
1 parent a7391cc commit 251bbca
Show file tree
Hide file tree
Showing 50 changed files with 929 additions and 1,407 deletions.
38 changes: 38 additions & 0 deletions packages/connect-aws/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// eslint-disable-next-line no-undef
module.exports = {
env: {
mocha: true
},
plugins: [
'mocha'
],
ignorePatterns: ['dist/'],
parser: '@typescript-eslint/parser',
extends: [
'standard',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:mocha/recommended'
],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
project: './tsconfig.json'
},
rules: {
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'@typescript-eslint/no-use-before-define': ['error', { functions: false, classes: false, variables: true, typedefs: false }],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/type-annotation-spacing': 'error',
'no-use-before-define': 'off',
'no-void': 'off',
'space-before-function-paren': ['error', {
anonymous: 'always',
named: 'never',
asyncArrow: 'always'
}],
semi: ['error', 'never']
}
}
3 changes: 3 additions & 0 deletions packages/connect-aws/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
./README.md
dist/
stats.html
59 changes: 59 additions & 0 deletions packages/connect-aws/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# `@fireproof/aws`

[Fireproof](https://use-fireproof.com) is an embedded JavaScript document database that runs in the browser (or anywhere with JavaScript) and **[connects to any cloud](https://www.npmjs.com/package/@fireproof/connect)**.

This module, `@fireproof/aws`, allows you to connect your Fireproof database to AWS S3 and DynamoDB via pre defined Lambda functions, enabling you to sync your data across multiple users in real-time.

## Get started

We assume you already have an app that uses Fireproof in the browser, and you want to setup collaboration among multiple users via the cloud. To write your first Fireproof app, see the [Fireproof quickstart](https://use-fireproof.com/docs/react-tutorial), otherwise read on.

### 1. Install

In your existing Fireproof app install the connector:

```sh
npm install @fireproof/aws
```

### 2. Connect

You're all done on the server, and ready to develop locally and then deploy with no further changes. Now you just need to connect to the AWS in your client code. Fireproof has an already deployed SAM template and to use the provisioned resources without the websocket based live syncing (backwards compatibility with older fireproof versions) as well as with websocket connections you can simply use the s3Free and awsFree functions respectively. However, if one wants to deploy their own resources they can do so by deploying our sam template and adding the neccassary urls to connect's aws function:

```js
// you already have this in your app
import { useFireproof } from "use-fireproof";
// add this line
import { connect } from "@fireproof/aws";
```

Now later in your app connect to the party (be sure to do this a component that runs on every render, like your root component or layout):

```js
const { database } = useFireproof("my-app-database-name");
const connection = connect.awsFree(database);
```

OR

```js
const { database } = useFireproof("my-app-database-name");
const connection = connect.s3Free(database);
```

OR

```js
const { database } = useFireproof("my-app-database-name");
const connection = connect.aws(database, {
uploadUrl,
downloadUrl,
websocketUrl,
});
```

All the functions are idempotent and designed to be safe to call on every render.

### 3. Collaborate

Now you can use Fireproof as you normally would, and it will sync in realtime with other users. Any existing apps you have that use the [live query](https://use-fireproof.com/docs/react-hooks/use-live-query) or [subscription](https://use-fireproof.com/docs/database-api/database#subscribe) APIs will automatically render multi-user updates.
52 changes: 52 additions & 0 deletions packages/connect-aws/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@fireproof/aws",
"version": "1.0.0",
"description": "",
"main": "./dist/browser/index.cjs",
"module": "./dist/browser/index.esm.js",
"exports": {
".": {
"import": "./dist/browser/index.esm.js",
"require": "./dist/browser/index.cjs",
"types": "./dist/types/index.d.ts",
"script": "./dist/browser/index.iife.js"
},
"./node": {
"import": "./dist/node/index.esm.js",
"require": "./dist/node/index.cjs",
"types": "./dist/types/index.d.ts",
"script": "./dist/browser/index.iife.js",
"default": "./dist/node/index.esm.js"
}
},
"browser": "./dist/index.browser.iife.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/node",
"dist/browser",
"dist/types"
],
"type": "module",
"scripts": {
"prepublishOnly": "npm run build",
"build:clean": "rm -rf dist",
"build:tsc": "npm run build:clean && tsc && mkdir dist/tsc && mv dist/*.js dist/tsc/ && node ../encrypted-blockstore/scripts/types.js",
"build:script": "node ../encrypted-blockstore/scripts/build.js",
"build": "npm run build:tsc && npm run build:script && cp dist/browser/index.iife.js ../fireproof/test/www/connect.iife.js",
"clean": "rm -rf node_modules",
"test": "node ../encrypted-blockstore/scripts/test.js",
"browser-test": "node ../connectors-helper/scripts/browser-test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@fireproof/encrypted-blockstore": "workspace:^",
"cross-fetch": "^4.0.0",
"js-base64": "^3.7.5"
},
"devDependencies": {
"ts-loader": "^9.4.4",
"typescript": "^5.4.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DownloadMetaFnParams, DownloadDataFnParams, UploadMetaFnParams, UploadDataFnParams } from './types'
import { Connection } from "./connection"
import { Connection } from '@fireproof/encrypted-blockstore'
import fetch from "cross-fetch"
import { Base64 } from "js-base64"

Expand Down
60 changes: 60 additions & 0 deletions packages/connect-aws/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ConnectS3 } from './connect-s3'
import { Connection,Connectable } from '@fireproof/encrypted-blockstore'
import type { AnyLink } from '@fireproof/encrypted-blockstore'
export type { AnyLink }
import {
UploadDataFnParams,
UploadMetaFnParams,
DownloadDataFnParams,
DownloadMetaFnParams
} from './types'


export const connect = {
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, name }: 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 = `wss://v7eax67rm6.execute-api.us-east-2.amazonaws.com/Prod?database=${name}`
const connection = new ConnectS3(upload, download, websocket)
connection.connect(blockstore)
return connection
},
aws: (
{ blockstore, name }: Connectable,
{ upload, download, websocket }: { upload: string; download: string; websocket: string }
) => {
const updatedwebsocket = `${websocket}?database=${name}`
const connection = new ConnectS3(upload, download, updatedwebsocket)
connection.connect(blockstore)
return connection
},
}

export function validateDataParams(params: DownloadDataFnParams | UploadDataFnParams) {
const { type, name, car } = params
if (!name) throw new Error('name is required')
if (!car) {
throw new Error('car is required')
}
if (type !== 'file' && type !== 'data') {
throw new Error('type must be file or data')
}
}

export function validateMetaParams(params: DownloadMetaFnParams | UploadMetaFnParams) {
const { name, branch } = params
if (!name) throw new Error('name is required')
if (!branch) {
throw new Error('branch is required')
}
}

export { ConnectS3 }
File renamed without changes.
20 changes: 20 additions & 0 deletions packages/connect-aws/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"include": ["src", "test", ".eslintrc.cjs", "scripts"],
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"strict": true,
"moduleResolution": "node",
"jsx": "react",
"skipLibCheck": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"emitDeclarationOnly": false,
"declarationDir": "dist/types",
"declaration": true,
"outDir": "dist",
"rootDir": "src",
"forceConsistentCasingInFileNames": true
}
}
9 changes: 6 additions & 3 deletions packages/connect-ipfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
"scripts": {
"prepublishOnly": "npm run build",
"build:clean": "rm -rf dist",
"build:tsc": "npm run build:clean && tsc && mkdir dist/tsc && mv dist/*.js dist/tsc/ && node ./scripts/types.js",
"build:script": "node ./scripts/build.js",
"build:tsc": "npm run build:clean && tsc && mkdir dist/tsc && mv dist/*.js dist/tsc/ && node ../encrypted-blockstore/scripts/types.js",
"build:script": "node ../encrypted-blockstore/scripts/build.js",
"build": "npm run build:tsc && npm run build:script && cp dist/browser/index.iife.js ../fireproof/test/www/ipfs.iife.js",
"clean": "rm -rf node_modules"
"clean": "rm -rf node_modules",
"test": "node ../encrypted-blockstore/scripts/test.js",
"browser-test": "node ../encrypted-blockstore/scripts/browser-test.js"
},
"keywords": [],
"author": "",
Expand All @@ -50,6 +52,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@fireproof/aws": "workspace:^",
"@fireproof/connect": "workspace:^",
"@fireproof/core": "workspace:^",
"@fireproof/encrypted-blockstore": "workspace:^",
Expand Down
55 changes: 0 additions & 55 deletions packages/connect-ipfs/scripts/browser-test.js

This file was deleted.

20 changes: 0 additions & 20 deletions packages/connect-ipfs/scripts/build.js

This file was deleted.

Loading

0 comments on commit 251bbca

Please sign in to comment.