Giter Club home page Giter Club logo

simple-ddatabase-protocol's Introduction

@ddatabase/simple-protocol

dDatabase protocol state machine

npm install @ddatabase/simple-protocol

Includes a Noise handshake, and is not backwards compatible with dDatabase <= 7

Usage

const Protocol = require('@ddatabase/simple-protocol')

const a = new Protocol(true, {
  send (data) { // send hook should send data
    b.recv(data)
  }
})

const b = new Protocol(false, {
  onrequest (channel, message) {
    console.log('got request message', message, 'on channel', channel)
  },
  send (data) {
    a.recv(data)
  }
})

// send a request message on channel 10
a.request(10, {
  index: 42
})

API

This is still a work in progress, so that messages supported might change. See the schema.proto file for the schema for each message.

p = new Protocol(isInitator, handlers)

Create a new protocol state machine.

  • isInitator is a boolean indicating if you are a client or server
  • handlers is a series of functions handling incoming messages

Everytime a binary message should be sent to another peer, handlers.send(data) is invoked.

If there is a critical error, handlers.destroy(err) is called.

After the initial handshake transport encryption is enabled, to ensure your stream is private.

To disable transport encryption set handlers.encrypted = false.

To disable the NOISE handshake set handlers.noise = false (works only when encrypted is also set to false).

p.recv(data)

Call this with incoming data.

buf = p.remoteCapability(key)

Create a remote capability for a key. Use this to verify if a remote indeed had a key when you get an open message.

buf = p.capability(key)

Create a local capability.

p.destroy(err)

Destroy the protocol state machine.

p.publicKey

The local public key used for authentication.

p.remotePublicKey

The remotes public key.

p.handshakeHash

The noise handshake hash which uniquely identifies the noise session.

http://noiseprotocol.org/noise.html#channel-binding

handlers.onauthenticate(remotePublicKey, done)

Called when you should authenticate a remote public key.

handlers.onhandshake()

Called when the initial protocol handshake has finished.

p.open(channel, message)

Send an open message on a channel.

Note that if you message.key the protocol, will turn that into a capability that is sent instead of the key.

Receiving an open message triggers handlers.onopen(channel, message)

p.options(channel, message)

Send a options message on a channel.

Receiving a handshake message triggers handlers.onoptions(channel, message)

p.status(channel, message)

Send a status message on a channel.

Receiving a info message triggers handlers.onstatus(channel, message)

p.have(channel, message)

Send a have message on a channel.

Receiving a have message triggers handlers.onhave(channel, message)

p.unhave(channel, message)

Send an unhave message on a channel.

Receiving an unhave message triggers handlers.onunhave(channel, message)

p.want(channel, message)

Send a want message on a channel.

Receiving a want message triggers handlers.onwant(channel, message)

p.unwant(channel, message)

Send an unwant message on a channel.

Receiving an unwant message triggers handlers.onunwant(channel, message)

p.request(channel, message)

Send a request message on a channel.

Receiving a request message triggers handlers.onrequest(channel, message)

p.cancel(channel, message)

Send a cancel message on a channel.

Receiving a cancel message triggers handlers.oncancel(channel, message)

p.data(channel, message)

Send a data message on a channel.

Receiving a data message triggers handlers.ondata(channel, message)

keyPair = Protocol.keyPair([seed])

Static function to generate a Noise key pair, optionally from a seed.

License

MIT

simple-ddatabase-protocol's People

Contributors

erkrujjawal786 avatar frando avatar freedomhunter avatar mafintosh avatar shikhars371 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.