@synonymdev/slashtags-rpc
Advanced tools
Comparing version 1.0.0-alpha.1 to 1.0.0-alpha.2
37
index.js
import ProtomuxRPC from 'protomux-rpc' | ||
import EventEmitter from 'events' | ||
import b4a from 'b4a' | ||
@@ -7,7 +8,7 @@ const RPCS_SYMBOL = Symbol.for('slashtags-rpcs') | ||
export class SlashtagsRPC extends EventEmitter { | ||
/** @param {Slashtag} slashtag */ | ||
/** @param {Slashtag} [slashtag] */ | ||
constructor (slashtag) { | ||
super() | ||
this.slashtag = slashtag | ||
this.slashtag.on('connection', this.setup.bind(this)) | ||
this.slashtag?.on('connection', this.setup.bind(this)) | ||
} | ||
@@ -55,26 +56,28 @@ | ||
* Create a new RPC instance on the stream if doesn't already exist | ||
* @param {SecretStream} socket | ||
* @param {SecretStream} stream | ||
* @returns {ProtomuxRPC} | ||
*/ | ||
setup (socket) { | ||
// @ts-ignore Deduplicate ProtomuxRPC instances on the same socket | ||
if (socket[RPCS_SYMBOL]?.get(this.id)) return | ||
setup (stream) { | ||
// @ts-ignore | ||
if (!socket[RPCS_SYMBOL]) socket[RPCS_SYMBOL] = new Map() | ||
const map = stream[RPCS_SYMBOL] = stream[RPCS_SYMBOL] || new Map() | ||
const existing = map.get(this.id) | ||
if (existing) return existing | ||
const options = { | ||
id: Buffer.from(this.id), | ||
id: b4a.from(this.id), | ||
valueEncoding: this.valueEncoding, | ||
handshakeEncoding: this.handshakeEncoding, | ||
handshake: this.handshake(socket) | ||
handshake: this.handshake(stream) | ||
} | ||
const rpc = new ProtomuxRPC(socket, options) | ||
const rpc = new ProtomuxRPC(stream, options) | ||
// @ts-ignore | ||
socket[RPCS_SYMBOL].set(this.id, rpc) | ||
map.set(this.id, rpc) | ||
rpc.on('open', (handshake) => this.onopen.bind(this)(handshake, socket)) | ||
rpc.on('open', (handshake) => this.onopen.bind(this)(handshake, stream)) | ||
this.methods.forEach(m => | ||
rpc.respond(m.name, m.options || {}, req => m.handler(req, socket)) | ||
rpc.respond(m.name, m.options || {}, req => m.handler(req, stream)) | ||
) | ||
return rpc | ||
} | ||
@@ -88,8 +91,6 @@ | ||
async rpc (key) { | ||
if (!this.slashtag) throw new Error('Can not call rpc() if not initialized with a Slashtag instance') | ||
const socket = this.slashtag.connect(key) | ||
await socket.opened | ||
this.setup(socket) | ||
// @ts-ignore | ||
return socket[RPCS_SYMBOL].get(this.id) | ||
return this.setup(socket) | ||
} | ||
@@ -96,0 +97,0 @@ } |
{ | ||
"name": "@synonymdev/slashtags-rpc", | ||
"version": "1.0.0-alpha.1", | ||
"version": "1.0.0-alpha.2", | ||
"description": "helper module for creating RPCs using Slashtags nodes", | ||
@@ -33,5 +33,7 @@ "type": "module", | ||
"dependencies": { | ||
"b4a": "^1.6.0", | ||
"protomux-rpc": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"@hyperswarm/dht": "^6.2.3", | ||
"@hyperswarm/testnet": "^3.1.0", | ||
@@ -46,2 +48,2 @@ "@synonymdev/slashtag": "^1.0.0-alpha.10", | ||
} | ||
} | ||
} |
@@ -30,7 +30,7 @@ # rpc | ||
handshake(socket) { | ||
handshake(stream) { | ||
return this.id + '-handshake:for:' + socket.remotePublicKey.toString('hex'); | ||
} | ||
onopen(handshake, socket) { | ||
onopen(handshake, stream) { | ||
this.emit('handshake', handshake, socket.reomtePublicKey); | ||
@@ -44,17 +44,56 @@ } | ||
name: 'echo', | ||
handler: (req) => req, | ||
}, | ||
]; | ||
handler: self._onEcho.bind(self) | ||
} | ||
] | ||
} | ||
async echo(key, message) { | ||
const rpc = await this.rpc(key); | ||
return rpc?.request('echo', message); | ||
_onEcho (req) { | ||
this.emit('echo', req) | ||
return req | ||
} | ||
async echo (key, message) { | ||
const rpc = await this.rpc(key) | ||
return rpc?.request('echo', message) | ||
} | ||
} | ||
const alice = new Slashtag(); | ||
await alice.listen(); | ||
const aliceFoo = new Foo(alice); | ||
aliceFoo.on('echo', (req) => { // req should equal 'hello world' }) | ||
const bob = new Slashtag(); | ||
const bobFoo = new Foo(bob); | ||
const response = await bobFoo.echo(alice.key, 'hello world'); | ||
// response should equal 'hello world' | ||
``` | ||
#### Use without Slashtag | ||
If the RPC doesn't reuqire Slashtag for anything other than establish a connection, you can pass your own stream: | ||
```js | ||
const alice = new DHT(); | ||
const server = alice.createServer() | ||
await server.listen(); | ||
const aliceFoo = new Foo(); | ||
server.on('connection', (stream) => aliceFoo.setup(stream)) | ||
aliceFoo.on('echo', (req) => { | ||
// req = 'hello world' | ||
}) | ||
const bob = new DHT(); | ||
const bobFoo = new Foo(); | ||
const stream = bob.connect(server.address().publicKey) | ||
await stream.opened | ||
const rpc = bobFoo.setup(stream) | ||
const response = await rpc?.request('echo', 'hello world') | ||
// response = 'hello world' | ||
``` | ||
## API | ||
@@ -106,6 +145,6 @@ | ||
Internally, it uses `Slashtags.connect(key)` then sets up the RPC instance using `setup(socket)` | ||
Internally, it uses `Slashtags.connect(key)` then sets up the RPC instance using `setup(stream)` | ||
#### `setup(socket)` | ||
#### `setup(stream)` | ||
Create a new [ProtomuxRPC](https://www.npmjs.com/package/protomux-rpc) instance on any stream if doesn't already exist. | ||
Create a new [ProtomuxRPC](https://www.npmjs.com/package/protomux-rpc) instance on any stream if doesn't already exist, otherwise it will return the existing RPC. |
/// <reference path="../../../declarations.d.ts" /> | ||
/// <reference types="node" /> | ||
export class SlashtagsRPC extends EventEmitter { | ||
/** @param {Slashtag} slashtag */ | ||
constructor(slashtag: Slashtag); | ||
slashtag: import("@synonymdev/slashtag").Slashtag; | ||
/** @param {Slashtag} [slashtag] */ | ||
constructor(slashtag?: import("@synonymdev/slashtag").Slashtag | undefined); | ||
slashtag: import("@synonymdev/slashtag").Slashtag | undefined; | ||
/** | ||
@@ -41,5 +41,6 @@ * RPC Identifier | ||
* Create a new RPC instance on the stream if doesn't already exist | ||
* @param {SecretStream} socket | ||
* @param {SecretStream} stream | ||
* @returns {ProtomuxRPC} | ||
*/ | ||
setup(socket: SecretStream): void; | ||
setup(stream: SecretStream): ProtomuxRPC; | ||
/** | ||
@@ -46,0 +47,0 @@ * Connect to a peer if not already connected, and return Protomux RPC instance. |
Sorry, the diff of this file is not supported yet
10502
7
152
148
2
9
+ Addedb4a@^1.6.0