level-serve
Streaming static file server based on
LevelDB.

Usage
Store and serve a nice cat picture at
/files/cat.png.
var Server = require('level-serve');
var level = require('level');
var http = require('http');
var fs = require('fs');
var db = level(__dirname + '/.server-db', { valueEncoding: 'binary' });
var server = Server(db);
var ws = server.createWriteStream('cat.png');
fs.createReadStream(__dirname + '/cat.png').pipe(ws);
http.createServer(server.handle.bind(server)).listen(8000);
console.log('go to http://localhost:8000' + server.url('cat.png'));
Caching
For now resources are cached for 1d on HTTP level. Proper caching is to be implemented.
Sublevels
With sublevels you can e.g.
give a plugin or a user only access to a part of the database so they can't
do anything harmful. The location in the sublevel tree is reflected in the
resulting public url.
This concept involves two instances of level-serve.
- One is read only and serves the whole database over http.
- The other one is given to clients / plugins and they can write whatever they
want, however only see their own section of the db, their sublevel.
var Server = require('level-serve');
var level = require('level');
var http = require('http');
var SubLevel = require('level-sublevel');
var fs = require('fs');
var db = level(__dirname + '/.sublevel-db', { valueEncoding: 'binary' });
SubLevel(db);
var server = Server(db.sublevel('cool').sublevel('cats'));
var ws = server.createWriteStream('white.png');
fs.createReadStream(__dirname + '/cat.png').pipe(ws);
var dbServer = Server(db);
http.createServer(dbServer.serve.bind(dbServer)).listen(8000);
console.log('or to http://localhost:8000' + dbServer.url('white.png'));
URLs
/files/ (sublevels /) file-id
Generate URLs using Server#url.
API
Server(db)
Make sure that db has been opened with valueEncoding: 'binary' if you want
to serve binary files.
Server#handle(req, res[, next])
HTTP request handler. Pass this to http.createServer() or express for
example.
If next is passed it will be called when errors happen or a requested file
cannot be found.
Server#createWriteStream(file-id)
Store a file under file-id. If you give file-id an extension it will be
served with the correct mime type.
Server#store(file-id, data[, cb])
Store data as file-id. Convenience method that exposes the write stream
in an async api.
Server#url(file-id)
Get the url of file-id, respecting sublevels.
Installation
With npm do
$ npm install level-serve
License
(MIT)
Copyright (c) 2013 Wayla <data@wayla.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.