npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@synonymdev/slashtags-rpc

v1.0.0-alpha.6

Published

helper module for creating RPCs using Slashtags nodes

Downloads

2,978

Readme

rpc

helper module for creating RPCs using Slashtags nodes

Installation

npm install @synonymdev/slashtags-rpc

Usage

import SlashtagsRPC from '@synonymdev/slashtags-rpc';
import Slashtag from '@synonymdev/slashtag';
import c from 'compact-encoding';

class Foo extends SlashtagsRPC {
  get id() {
    return 'foo';
  }

  get valueEncoding() {
    return c.string;
  }

  get handshakeEncoding() {
    return c.string;
  }

  handshake(socket) {
    return this.id + '-handshake:for:' + socket.remotePublicKey.toString('hex');
  }

  onopen(handshake, socket) {
    this.emit('handshake', handshake, socket.reomtePublicKey);
  }

  get methods() {
    const self = this;
    return [
      {
        name: 'echo',
        handler: self._onEcho.bind(self)
      }
    ]
  }

  _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) => { console.log(req) })

const bob = new Slashtag();
const bobFoo = new Foo(bob);

const response = await bobFoo.echo(alice.key, 'hello world');

API

const rpc = new SlashtagsRPC({swarm})

Create a new instance of SlashtagsRPC using a Hyperswarm instance.

id

String identifies your RPC protocol.

valueEncoding

Default compact encoding for all requests and responses.

handshakeEncoding

Default compact encoding for handshake message, sent first thing on opening the channel.

handshake(stream) => any

A callback function that should returns the handshake value. stream is a SecretStream.

onopen(handshake, stream)

A callback on opening the rpc. handshake is the value sent on opening the channel, stream is a SecretStream.

methods

An array of methods objects that should be as following:

{
    name: string,
    options?: {
        // Optional encoding for both the request and response
        valueEncoding: Encoding,
        // Optional encoding for requests
        requestEncoding: Encoding,
        // Optional encoding for responses
        responseEncoding: Encoding
    },
    // Handler that takes the decoded request and returns a response value
    handler: (req) => any
}

rpc(key)

Returns a ProtomuxRPC instance after connecting to a Slashtag by its key, id or url. If connection is already opened, the same RPC instance is returned.

this is an idempotent operation, meaning if there is already an existing opened connection, it won't create new ones.