0.5.0 • Published 4 years ago

@consento/api v0.5.0

Weekly downloads
33
License
MIT
Repository
github
Last release
4 years ago

@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

0.5.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.2

4 years ago

0.3.3

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago