Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

level-serve

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

level-serve

Streaming static file server based on [LevelDB](https://github.com/rvagg/node-levelup).

Source
npmnpm
Version
0.0.0
Version published
Weekly downloads
10
400%
Maintainers
1
Weekly downloads
 
Created
Source

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');

// initialize db and server
var db = level(__dirname + '/.server-db', { valueEncoding: 'binary' });
var server = Server(db);

// store cat
var ws = server.createWriteStream('cat.png');
fs.createReadStream(__dirname + '/cat.png').pipe(ws);

// serve cat
http.createServer(server.serve.bind(server)).listen(8000);
console.log('go to http://localhost:8000' + server.url('cat.png'));

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.

var Server = require('level-serve');
var level = require('level');
var http = require('http');
var SubLevel = require('level-sublevel');
var fs = require('fs');

// initialize the db and give it sublevels
var db = level(__dirname + '/.sublevel-db', { valueEncoding: 'binary' });
SubLevel(db);

// initialize server for sublevel
var server = Server(db.sublevel('cool').sublevel('cats'));

// cat from sublevel "cool"."cats" will be served at /files/cool/cats/white.png
var ws = server.createWriteStream('white.png');
fs.createReadStream(__dirname + '/cat.png').pipe(ws);

// serve cat
var dbServer = Server(db);
http.createServer(dbServer.serve.bind(dbServer)).listen(8000);
console.log('or to http://localhost:8000' + server.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#serve(req, res[, next])

HTTP request handler. Pass this to http.createServer() or express for example.

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#write(file-id, data[, cb])

Store data as file-id. Convenience method that exposes the write stream in a 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.

FAQs

Package last updated on 17 Jun 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts