multilevel

Expose a leveldb over the network.

multilevel

Expose a levelDB over the network, to be used by multiple processes, with levelUp's API.

Expose a db on the server:

var multilevel = require('multilevel');
var net = require('net');
var level = require('level');
 
var db = level('/my/db');
 
net.createServer(function (con) {
  con.pipe(multilevel.server(db)).pipe(con);
}).listen(3000);

And connect to it from the client:

var multilevel = require('multilevel');
var net = require('net');
 
var db = multilevel.client();
var con = net.connect(3000);
con.pipe(db.createRpcStream()).pipe(con);
 
// asynchronous methods 
db.get('foo', function () { /* */ });
 
// streams 
db.createReadStream().on('data', function () { /* */ });

multilevel works in the browser too - via browserify - and has full support for binary data. For getting a connection between browser and server I recommend either websocket-stream, which treats binary data well, or engine.io-stream, which has websocket fallbacks.

When using a binary capable transport, require multilevel like this:

var multilevel = require('multilevel/msgpack');

This way it uses msgpack-stream instead of json-buffer which uses way less bandwidth - but needs a binary capable transport.

You can also expose custom methods and sublevels with multilevel!

When using plugins, you must generate a manifest and require it in the client.

Here's an example:

// server.js 
// create `db` 
var level = require('level');
var multilevel = require('multilevel');
var db = level(PATH);
 
// extend `db` with a foo(cb) method 
db.methods = db.methods || {};
db.methods['foo'] = { type: 'async' };
db.foo = function (cb) {
  cb(null, 'bar');
};
 
// now write the manifest to a file 
multilevel.writeManifest(db, __dirname + '/manifest.json');
 
// then expose `db` via shoe or any other streaming transport. 
var shoe = require('shoe');
var sock = shoe(function (stream) {
  stream.pipe(multilevel.server(db)).pipe(stream);
});
sock.install(http.createServer(/* ... */), '/websocket');

Manifests are generated using level-manifest, which doesn't only support async functions but e.g. streams as well. For more, check its README.

Then require the manifest on the client when bundling with browserify or in any other nodejs compatible environment.

// client.js 
// instantiate a multilevel client with the `manifest.json` we just generated 
var multilevel = require('multilevel');
var manifest = require('./manifest.json');
var db = multilevel.client(manifest);
 
// now pipe the db to the server 
var stream = shoe('/websocket');
stream.pipe(db.createRpcStream()).pipe(stream);
 
// and you can call the custom `foo` method! 
db.foo(function (errres) {
  console.log(res); // => "bar" 
});

You do not want to expose every database feature to every user, you might e.g. only want to provide read-only access to some users.

Auth controls may be injected when creating the server stream.

In this example, allow read only access, unless logged in as root.

//server.js 
var db = require('./setup-db'); //all your database customizations 
var multilevel = require('multilevel');
 
//write out manifest 
multilevel.writeManifest(db, __dirname + '/manifest.json');
 
shoe(function (stream) {
  stream.pipe(multilevel.server(db, {
    authfunction (usercb) {
      if (user.name == 'root' && user.pass == 'toor') {
        //the data returned will be attached to the mulilevel stream 
        //and passed to `access` 
        cb(null, {name: 'root'});
      } else {
        cb(new Error('not authorized');
      }
    },
    accessfunction (userdbmethodargs) {
      //`user` is the {name: 'root'} object that `auth` 
      //returned.  
 
      //if not a privliged user... 
      if (!user || user.name !== 'root') {
        //do not allow any write access 
        if (/^put|^del|^batch|write/i.test(method)) {
          throw new Error('read-only access');
        }
      }        
    })
  })).pipe(stream);
});
 
// ... 

The client authorizes by calling the auth method.

var multilevel = require('multilevel');
var shoe = require('shoe');
 
var stream = shoe();
var db = multilevel.client();
stream.pipe(db.createRpcStream()).pipe(stream);
 
db.auth({ name: 'root', pass: 'toor' }, function (errdata) {
  if (err) throw err
  //later, they can sign out, too. 
 
  db.deauth(function (err) {
    //signed out! 
  });
});

The exposed DB has the exact same API as levelUp, except

  • db#close() closes the connection, instead of the database.
  • the synchronous versions of db#isOpen() and db#isClose() tell you if you currently have a connection to the remote db.
  • events, like db.on("put", ...) are not emitted. If you need updates, you can use level-live-stream.

Returns a server-stream that exposes db, an instance of levelUp. authOpts is optional and should be of this form:

var authOpts = {
  authfunction (userDatacb) {
    //call back an error, if the user is not authorized. 
  },
  accessfunction (userDatadbmethodargs) {
    //throw if this user is not authorized for this action. 
  }
}

It is necessary that you create one server stream per client.

Synchronoulsy write db's manifest to path.

Return a new client db. manifest may optionally be provided, which will grant the client access to extensions and sublevels.

Pipe this into a server stream.

Listen for the error event on this stream to handle / swallow reconnect events.

Authorize with the server.

Deauthorize with the server.

On my macbook pro one multilevel server handles ~15k ops/s over a local tcp socket.

 ∴  bench (master) : node index.js 
 
writing "1234567890abcdef" 100 times
 
 native             : 2ms (50000 ops/s)
 multilevel direct  : 21ms (4762 ops/s)
 multilevel network : 14ms (7143 ops/s)
 
writing "1234567890abcdef" 1000 times
 
 native             : 12ms (83333 ops/s)
 multilevel direct  : 71ms (14085 ops/s)
 multilevel network : 77ms (12987 ops/s)
 
writing "1234567890abcdef" 10000 times
 
 native             : 88ms (113636 ops/s)
 multilevel direct  : 594ms (16835 ops/s)
 multilevel network : 590ms (16949 ops/s)
 
writing "1234567890abcdef" 100000 times
 
 native             : 927ms (107875 ops/s)
 multilevel direct  : 10925ms (9153 ops/s)
 multilevel network : 9839ms (10164 ops/s)

With npm do:

$ npm install multilevel
$ npm install
$ npm test
$ npm run test-browser

(MIT)

Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.