Merge branch 'persistent-bs' into naive-follows
This commit is contained in:
commit
a19d78a37c
common/src
frontend
server/src
third-party/src
@ -2,26 +2,17 @@ import level from 'level'
|
||||
import { CID } from 'multiformats/cid'
|
||||
import { BlockstoreI } from './types'
|
||||
|
||||
let globalBlockstore: Blockstore | null = null
|
||||
|
||||
export class Blockstore implements BlockstoreI {
|
||||
|
||||
store: level.LevelDB
|
||||
|
||||
constructor(name = 'blockstore') {
|
||||
this.store = level(name, {
|
||||
constructor(location = 'blockstore') {
|
||||
this.store = level(location, {
|
||||
valueEncoding: 'binary',
|
||||
compression: false
|
||||
})
|
||||
}
|
||||
|
||||
static getGlobal(): Blockstore {
|
||||
if (globalBlockstore === null) {
|
||||
globalBlockstore = new Blockstore()
|
||||
}
|
||||
return globalBlockstore
|
||||
}
|
||||
|
||||
async get(cid: CID): Promise<Uint8Array | null> {
|
||||
return this.store.get(cid.toString())
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import { CID } from 'multiformats/cid'
|
||||
import { BlockstoreI } from './types'
|
||||
import { streamToArray } from './util'
|
||||
|
||||
let globalDB: MemoryBlockstore | null = null
|
||||
export class MemoryBlockstore implements BlockstoreI {
|
||||
|
||||
map: Map<string, any>
|
||||
@ -13,13 +12,6 @@ export class MemoryBlockstore implements BlockstoreI {
|
||||
this.map = new Map()
|
||||
}
|
||||
|
||||
static getGlobal(): MemoryBlockstore {
|
||||
if (globalDB === null) {
|
||||
globalDB = new MemoryBlockstore()
|
||||
}
|
||||
return globalDB
|
||||
}
|
||||
|
||||
async get(k: CID): Promise<Uint8Array> {
|
||||
return this.map.get(k.toString())
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import { service, LocalUser, UserStore, Blockstore } from '@bluesky-demo/common'
|
||||
import * as ucan from 'ucans'
|
||||
|
||||
interface Props {
|
||||
blockstore: Blockstore
|
||||
onRegister: (user: LocalUser) => void
|
||||
}
|
||||
|
||||
@ -26,7 +27,7 @@ function Register(props: Props) {
|
||||
audience: blueskyDid,
|
||||
issuer: keypair
|
||||
})
|
||||
const userStore = await UserStore.create(username, Blockstore.getGlobal(), keypair)
|
||||
const userStore = await UserStore.create(username, props.blockstore, keypair)
|
||||
await service.register(await userStore.getCarFile(), ucan.encode(token))
|
||||
|
||||
// LOCAL STORAGE IS NOT SAFE. DO NOT DO THIS IN PRODUCTION
|
||||
|
@ -16,6 +16,7 @@ function Home(props: {}) {
|
||||
const [loading, setLoading] = React.useState<boolean>(true);
|
||||
const [users, setAllUsers] = React.useState<string[]>([]);
|
||||
const [showUsers, setShowUsers] = React.useState<boolean>(false);
|
||||
const [blockstore, setBlockstore] = React.useState<Blockstore | null>(null);
|
||||
|
||||
const addPost = async (post: Post) => {
|
||||
await store.addPost(post)
|
||||
@ -38,7 +39,7 @@ function Home(props: {}) {
|
||||
let userStore: UserStore
|
||||
try {
|
||||
const car = await service.fetchUser(localUser.keypair.did())
|
||||
userStore = await UserStore.fromCarFile(car, Blockstore.getGlobal(), localUser.keypair)
|
||||
userStore = await UserStore.fromCarFile(car, getBlockstore(), localUser.keypair)
|
||||
} catch (_err) {
|
||||
// @@TODO: show error instead of an empty store
|
||||
console.log("Could not load user store: ", _err)
|
||||
@ -49,7 +50,7 @@ function Home(props: {}) {
|
||||
}
|
||||
|
||||
const createNewStore = async (): Promise<UserStore> => {
|
||||
const userStore = await UserStore.create(localUser.username, Blockstore.getGlobal(), localUser.keypair)
|
||||
const userStore = await UserStore.create(localUser.username, getBlockstore(), localUser.keypair)
|
||||
|
||||
if(userStore.posts.length === 0) {
|
||||
const testPost = {
|
||||
@ -62,6 +63,15 @@ function Home(props: {}) {
|
||||
return userStore
|
||||
}
|
||||
|
||||
const getBlockstore = (): Blockstore => {
|
||||
if (blockstore !== null) {
|
||||
return blockstore
|
||||
}
|
||||
const bs = new Blockstore()
|
||||
setBlockstore(bs)
|
||||
return bs
|
||||
}
|
||||
|
||||
const thirdPartyPost = async () => {
|
||||
const audience = await service.getThirdPartyDid()
|
||||
const token = await ucan.build({
|
||||
@ -137,7 +147,7 @@ function Home(props: {}) {
|
||||
if (localUser === null) {
|
||||
return (
|
||||
<App>
|
||||
<Register onRegister={setLocalUser} />
|
||||
<Register blockstore={getBlockstore()} onRegister={setLocalUser} />
|
||||
</App>
|
||||
)
|
||||
}
|
||||
|
@ -1,11 +1,19 @@
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import Routes from './routes'
|
||||
import { Blockstore } from '@bluesky-demo/common'
|
||||
|
||||
const app = express()
|
||||
app.use(express.json())
|
||||
app.use(cors())
|
||||
|
||||
// attach blockstore instance
|
||||
const blockstore = new Blockstore()
|
||||
app.use((req, res, next) => {
|
||||
res.locals.blockstore = blockstore
|
||||
next()
|
||||
})
|
||||
|
||||
app.use('/', Routes)
|
||||
|
||||
const PORT = 2583
|
||||
|
@ -25,7 +25,7 @@ router.post('/register', async (req, res) => {
|
||||
let userStore: UserStore
|
||||
try {
|
||||
const bytes = await readReqBytes(req)
|
||||
userStore = await UserStore.fromCarFile(bytes, Blockstore.getGlobal(), SERVER_KEYPAIR)
|
||||
userStore = await UserStore.fromCarFile(bytes, res.locals.blockstore, SERVER_KEYPAIR)
|
||||
}catch(err) {
|
||||
return res.status(400).send("Could not parse UserStore from CAR File")
|
||||
}
|
||||
@ -48,7 +48,7 @@ router.post('/update', async (req, res) => {
|
||||
let userStore: UserStore
|
||||
try {
|
||||
const bytes = await readReqBytes(req)
|
||||
userStore = await UserStore.fromCarFile(bytes, Blockstore.getGlobal(), SERVER_KEYPAIR)
|
||||
userStore = await UserStore.fromCarFile(bytes, res.locals.blockstore, SERVER_KEYPAIR)
|
||||
}catch(err) {
|
||||
return res.status(400).send("Could not parse UserStore from CAR File")
|
||||
}
|
||||
@ -85,7 +85,7 @@ router.get('/:id', async (req, res) => {
|
||||
return res.status(404).send("User not found")
|
||||
}
|
||||
|
||||
const userStore = await UserStore.get(userRoot, Blockstore.getGlobal(), SERVER_KEYPAIR)
|
||||
const userStore = await UserStore.get(userRoot, res.locals.blockstore, SERVER_KEYPAIR)
|
||||
|
||||
const bytes = await userStore.getCarFile()
|
||||
return res.status(200).send(Buffer.from(bytes))
|
||||
|
@ -10,8 +10,11 @@ export const get = async (username: string): Promise<string | null> => {
|
||||
try {
|
||||
const got = await store.get(username)
|
||||
return got || null
|
||||
} catch(_) {
|
||||
return null
|
||||
} catch(err) {
|
||||
if (err.notFound) {
|
||||
return null
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,10 @@ export const get = async (did: string): Promise<CID | null> => {
|
||||
try {
|
||||
const got = await store.get(did)
|
||||
return CID.parse(got) || null
|
||||
} catch(_) {
|
||||
return null
|
||||
} catch(err) {
|
||||
if (err.notFound) {
|
||||
return null
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
9
third-party/src/index.ts
vendored
9
third-party/src/index.ts
vendored
@ -12,6 +12,13 @@ const app = express()
|
||||
app.use(express.json())
|
||||
app.use(cors())
|
||||
|
||||
// attach blockstore instance
|
||||
const blockstore = new Blockstore()
|
||||
app.use((req, res, next) => {
|
||||
res.locals.blockstore = blockstore
|
||||
next()
|
||||
})
|
||||
|
||||
// Return the server's did
|
||||
app.get('/.well-known/did.json', (_req, res) => {
|
||||
res.send({ id: SERVER_DID })
|
||||
@ -53,7 +60,7 @@ app.post('/post', async (req, res) => {
|
||||
const encoded = ucan.encode(extendUcan)
|
||||
|
||||
const car = await service.fetchUser(userDid)
|
||||
const userStore = await UserStore.fromCarFile(car, Blockstore.getGlobal(), SERVER_KEYPAIR)
|
||||
const userStore = await UserStore.fromCarFile(car, res.locals.blockstore, SERVER_KEYPAIR)
|
||||
await userStore.addPost({
|
||||
user: username,
|
||||
text: `Hey there! I'm posting on ${username}'s behalf`
|
||||
|
Loading…
x
Reference in New Issue
Block a user