1.0.5 • Published 6 years ago

random-access-network v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

Random Access Network

Build Status

A Random Access Storage implementation that goes through a Transport.

Installation

npm install random-access-network --save

Usage

const {RAN, StreamTransport} = require('random-access-network')
const stream = connectSomehwereAndGetDuplexStream("test")
const storage = RAN("test", new StreamTransport("test", stream))

storage.read(4, 20, function(err, buffer) {})

Advanced example with a WebSocket transport:

const WebSocket = require('ws')
const RAN = require('random-acces-network')
// see below
const WssTransport = require('./transport')

const sock = new WebSocket('ws://localhost:8080')
const transport = new WssTransport('test', sock)
const file = RAN('test', transport)

sock.on('open', function() {
  file.write(0, Buffer.from('hello'), function (err) {
    file.read(0, 5, function (err, buffer) {
      console.log(buffer.toString())
      file.close(function() {
        console.log('file closed')
        transport.close()
      })
    })
  })
})

Random access network bridge

random-access-network provides a bridge utility to transform a request to a random-access-storage call:

const raf = require('random-access-file')
const {RasBridge} = require('random-access-network')
const ras = RasBridge(function getRas(name) {
  return raf(name)
})

Usage example in the websocket case:

const WebSocket = require('ws')
const raf = require('random-access-file')
const {RasBridge} = require('random-access-network')

const ras = RasBridge((name) => raf(name))
const wss = new WebSocket.Server({ port: 8080 })

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    ras(message, function (callback) {
      ws.send(callback)
    })
  })
})

Transport

A transport is a class that sends/receive data to/from an interface (network, IPC etc.) and handles encoding/decoding.

The NoopTransport handles the correct propagation of request/callbacks. When creating your own transport you should implement the methods send, onmessage and optionally close:

  • send gets a buffer to be send to the network of your choice
  • onmessage should call this._next(buffer) once you've transformed the received data in a buffer
  • close if you want to close the network interface

For example a websocket transport:

const {NoopTransport} = require('random-access-network')

function WssTransport (name, socket) {
  NoopTransport.call(this, name)
  this._sock = socket
  this._sock.on('message', (response) => this.onmessage(response))
}

WssTransport.prototype = Object.create(NoopTransport.prototype)

WssTransport.prototype.send = function (data) {
  this._sock.send(data)
}

WssTransport.prototype.onmessage = function (data) {
  this._next(data)
}

WssTransport.prototype.close = function () {
  this._sock.close()
}

If you're using a duplex stream you can use the provided StreamTransport:

const {StreamTransport} = require('random-access-network')

See also the native messaging implementation.