OptimalBits / cabinet

A lightweight fast static file server for node loaded with useful features
55 stars 6 forks source link

Cabinet BuildStatus

A fast static file server loaded with useful features.

Works as a drop-in replacement for connect's static module (also works on express).

The code is based on the original work by TJ Holowaychuck, but with several improvements and additions:

Since files are cached and always fresh, compiled coffee script, gzip, less and minification do not have any impact on the server performance.

Follow optimalbits for news and updates regarding this library.

Install:

npm install cabinet

Tests:

npm test

Example for using within an express application:

var cabinet = require('cabinet');

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  app.use(cabinet(__dirname + '/static', {
    ignore: ['.git', '*.txt', 'node_modules'],
    files: {'index.html': 'static/index.html' },
    coffee:true,
    typescript: {
      module: 'amd',
      concatenate: false
    },
    gzip:true,
    // Set LESS CSS options
    less:{
      // Specify search paths for @import directives
      paths: ['.',__dirname + '/static/stylesheets'], 
    },
      // Activates in-memory cache
    cache:{ 
      maxSize: 16384, // 16Kb pero object 
      maxObjects:256
    }
  }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
  app.use(cabinet(__dirname + '/static', {
    ignore: ['.git', '*.txt', 'node_modules'],
    coffee:true,
    gzip:true,
        // Minimize javascript files.
        minjs: true, 
    less:{
      paths: ['.',__dirname + '/static/stylesheets'],
    },
        // Activates in-memory cache
      cache:{
        maxSize: 16384, // 16Kb per object
        maxObjects:256
      }
  }));
});

Virtuals

It is possible to define virtual files in cabinet. A virtual file is the result of some processing. For example, an application manifest file is a virtual file that is generated on the fly with the given files and with an always and up-to-date timestamp (the timestamp is re-generated when any of the files in the manifest changes). Virtual files are also cached as normal files and therefore they are served very fast.

Example of a file server for serving an application cache manifest:

app.use(cabinet(__dirname + '/static', 
{
  gzip:true,
  minjs:true,
  cache:{
    maxSize: 1024, 
    maxObjects:256
  }
},
{
  '/example.appcache':cabinet.virtuals.manifest(['foo.txt', 'bar.txt'], ['*'], ['/ /fallback'])
}));

Accessing /example.appcache will generate the following manifest:

CACHE MANIFEST
#465398.1347735807000
CACHE:
foo.txt
bar.txt
NETWORK:
*
FALLBACK:
/ /fallback    

The timestamp (#465398.1347735807000) will be automatically updated if foo.txt or bar.txt changes, so that the browser will invalidate its application cache when needed.

A virtual is just an asynchronous function without parameters with the following callback signature:

function(err, data, files)

Arguments

err   {Error} Some error object describing an error. 
data  {String|Buffer} The data that is going to be served when the virtual file is accessed.
files {Array} Array of files to listen for changes. This is used when enabling the cache. If any
of the files changes, the cache will invalidate the entry containing the virtual.

Example of a (very) dummy virtual:

function dummy(name){
  return function(cb){
    cb(null, 'My name is '+name, []);
  }
}

Directory Watcher

Cabinet includes a directory watcher that is used to keep the cache always fresh as well as to compute new ETAG values for files and virtual files based on their dependencies. So files depending on other files will automatically get new ETAGS based on all their dependencies.

In some platforms such as Mac OSX, there is a known error due to limits in the amount of allowed open files:

  Error: watch EMFILE

To avoid this error use ulimit, for example to allow 4096 open files:

ulimit -n 4096

Typescript

Cabinet support on-the-fly compilation and optimization of typescript source files. The file watcher will listen for changes in the source files and their dependencies, therefore always delivering the latest code. Compilation is expensive, so in a production server the cache should be enabled for optimal performance.

Reference

cabinet(root, [options, virtuals])

Arguments

root     {String} Path to the root directory for the served files.
options  {Object} options object (see options)
virtuals {Object} Object mapping paths to virtuals.

Options

Most options are directly inherited from connect's options. Besides those we have the options related to the provided filters:

Roadmap

License

(The MIT License)

Copyright (c) 2012 Optimal Bits Sweden AB manuel@optimalbits.com Copyright (c) 2011 TJ Holowaychuk Copyright (c) 2010 Sencha Inc.

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.