Get support for random-access-storage/random-access-network

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

random-access-storage/random-access-network

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.

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2