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

@consento/api

v0.5.0

Published

JavasScript API for building things with consento.

Downloads

105

Readme

@consento/api

@consento/api is the main API for build consento systems.

Under heavy development.

Setup

The consento API has a few configuration points.

const { setup } = require('@consento/api')
const api = setup({
  core: require('@consento/crypto/core/sodium') // or '@consento/crypto/core/friends' depending on environment
  notificationTransport // Implementation of notification transport
})

Crypto

@consento/crypto is the foundation upon which Consento is built.

The API exposes the crypto primitives through api.crypto. e.g.:

const { Sender } = api.crypto

Notifications

The Consento API comes with an end-to-end encrypted notification system.

const { notifications } = api

Any ISender instance is able to submit notifications:

notifications.send(sender, 'Hello World')

For another device/instance to receive the notification, the device needs to first register the matching IReceiver

notifications.subscribe(sender.newReceiver())

All messages are received through a single handler:

import { isSuccessNotification, isErrorNotification } from '@consento/api'

notifications.processor.add((message) => {
  // Handle the message result.
  if (isSuccessNotification(message)) {
    message.body // body of the message
    message.receiver // receiver for the message
    message.receiverIdBase64 // base64 for the receiver
  }
  if (isErrorNotification(message)) {
    message.code // code for the error
    message.error // error object (if available)
    message.receiverIdBase64 // id for the receiver
  }
})

Of course it is possible to unsubscribe from receiving messages:

notifications.unsubscribe(receiver)

If the transport receives a method it needs to call

notifications.handle(idBase64, encryptedMessage)

For simple one-time reading of a request you can also subscribe, receive and unsubscribe from a channel.

const { promise, cancel } = notifications.receive(receiver)

const response = await promise // To receive the next notification
await cancel() // To cancel the receiving of a notification

You can also send a message before receiving with the sendAndReceive helper:

const message = 'Hello World'
const { promise, cancel } = notifications.sendAndReceive({ sender, receiver }, message)

In extension it is possible to verify the body message by using a filter:

import { IEncodable } from '@consento/api'

const isStringLen32 = (body: IEncodable): body is string => typeof body === 'string' && body.length === 32
const { promise } = notifications.receive(receiver, isStringLen32)

const response: string = await promise // only resolves if a 32 character string has been sent received on the channel

... and furthermore it is possible to add a timeout to receiving a message:

const { promise } = notifications.receive(receiver, null, 1000)

try {
  const data = await promise
} catch (err) {
  err.code === 'timeout'
  err.timeout === 1000
}

(You can also pass a filter & timeout to sendAndReceive)

License

MIT