Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
feat(lib): add pubsub proof-of-concept
Browse files Browse the repository at this point in the history
  • Loading branch information
0x77dev committed Jan 25, 2022
1 parent e408f75 commit 9c722bb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
36 changes: 23 additions & 13 deletions packages/ipfs/project.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
{
"projectType": "library",
"root": "packages/ipfs",
"sourceRoot": "packages/ipfs/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nrwl/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/ipfs",
"assets": [
"packages/ipfs/*.md"
],
"main": "packages/ipfs/src/index.ts",
"tsConfig": "packages/ipfs/tsconfig.lib.json",
"assets": ["packages/ipfs/*.md"]
}
"outputPath": "dist/packages/ipfs",
"tsConfig": "packages/ipfs/tsconfig.lib.json"
},
"outputs": [
"{options.outputPath}"
]
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/ipfs/**/*.ts"]
}
"lintFilePatterns": [
"packages/ipfs/**/*.ts"
]
},
"outputs": [
"{options.outputFile}"
]
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/ipfs"],
"options": {
"jestConfig": "packages/ipfs/jest.config.js",
"passWithNoTests": true
}
},
"outputs": [
"coverage/packages/ipfs"
]
}
},
"tags": []
}
}
2 changes: 1 addition & 1 deletion packages/ipfs/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": []
"types": [],
},
"include": [
"**/*.ts"
Expand Down
42 changes: 42 additions & 0 deletions packages/lib/src/pubsub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { IPFS } from "ipfs-core"
import { Buffer } from "buffer"

export interface Message<T> {
from: string
data: T
}

export class PubSub<T = unknown> {
constructor(public ipfs: IPFS, public name: string) {

}

private getTopic(topic: string): string {
return `${this.name}/${topic}`
}

private encode(data: T): string {
return JSON.stringify(data)
}

private decode(data: string): T {
return JSON.parse(data)
}

public async subscribe(topic: string, listener: (msg: Message<T>) => void): Promise<void> {
await this.ipfs.pubsub.subscribe(this.getTopic(topic), (message) => {
listener({
from: message.from,
data: this.decode(Buffer.from(message.data).toString())
})
})
}

public async publish(topic: string, data: T): Promise<void> {
await this.ipfs.pubsub.publish(this.getTopic(topic), Buffer.from(this.encode(data)))
}

public async unsubscribe(topic: string): Promise<void> {
await this.ipfs.pubsub.unsubscribe(this.getTopic(topic))
}
}
3 changes: 3 additions & 0 deletions packages/lib/src/stack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { IPFS } from "ipfs-core"
import { PubSub } from "./pubsub"
import { Store } from "./store"

export class Stack {
public store: Store
public pubsub: PubSub

private constructor(public name: string, ipfs: IPFS) {
this.store = new Store(ipfs, name)
this.pubsub = new PubSub(ipfs, name)
}

public static async create(name: string, ipfs: IPFS) {
Expand Down
9 changes: 9 additions & 0 deletions scripts/link
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# This script links built packages using `yarn link`

for package in dist/packages/* ; do
echo "Linking $package"
bash -c "mkdir -p /tmp/$package && cp -Rf $package /tmp/$package && cd /tmp/$package && yarn install && yarn link --force" > /dev/null 2>&1 &
done

wait

0 comments on commit 9c722bb

Please sign in to comment.