This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39eb561
commit 3b75ca3
Showing
6 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.git* | ||
.idea/* | ||
.DS_Store | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2011 christos "constantology" constandinou http://muigui.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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# inertia | ||
|
||
inertia is a VERY simple static file server for nodejs. so simple, it's retarded. | ||
|
||
## Installation | ||
|
||
``` | ||
npm install inertia | ||
``` | ||
|
||
## Requiring | ||
|
||
```javascript | ||
|
||
var inertia = require( 'inertia' ); | ||
|
||
``` | ||
|
||
## Usage | ||
|
||
```javascript | ||
|
||
var static = inertia.createHandler(); | ||
|
||
static.encoding = 'iso-8859-1'; // set character encoding | ||
static.useCache = false|true; // turn off/ on in-memory caching | ||
static.useCompression = false|true; // turn off/ on compression | ||
|
||
static.addFileHandler( /^static\..*\.(gif|jpe?g|png)$/i ) // regexp file handler | ||
.addFileHandler( 'foo', 'application/foo' ) // add a custom file type with a custom mime type | ||
.addFileHandler( 'html' ); // add a standard file type, common mime types are handled internally | ||
|
||
static.addDirHandler( './lib' ); // serve all files from a specific directory | ||
|
||
static.compress( 'css', 'html', 'js', 'json', 'txt' ); // compress (using deflate) files with specific extensions | ||
|
||
static.maxAge( 'css', 'js' 60 * 60 * 24 ) // add max-age Cache-Control headers for specific extensions | ||
.maxAge( 'html', 60 * 60 ) | ||
|
||
http.createServer( function( req, res ) { | ||
if ( static.serve( req, res ) ) return; | ||
|
||
// otherwise, do something else... | ||
} ).listen( '8080' ); | ||
|
||
``` | ||
|
||
## License | ||
|
||
(The MIT License) | ||
|
||
Copyright © 2011 christos "constantology" constandinou http://muigui.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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
//#!/usr/bin/env node | ||
var fs = require( 'fs' ), | ||
path = require( 'path' ), | ||
url = require( 'url' ), | ||
util = require( 'util' ), | ||
zlib = require( 'zlib' ), | ||
|
||
mime = require( './mime' ), | ||
server_info = 'inertia/0.0.1', | ||
slice = Array.prototype.slice; | ||
|
||
module.exports = { | ||
mime : mime, | ||
createHandler : function() { | ||
var SH = new StaticHandler; | ||
slice.call( arguments ).forEach( SH.addFileHandler, SH ); | ||
return SH; | ||
} | ||
}; | ||
|
||
function cache( SH, url, response ) { | ||
SH.__cache[url.path] || ( SH.__cache[url.path] = prepare( SH, url, response ) ); | ||
|
||
return SH.__cache[url.path]; | ||
} | ||
|
||
function compress( SH, o, response ) { | ||
if ( SH.useCache ) { | ||
if ( o.wait === true ) { | ||
o.fns.push( createCallback( response, o ) ); | ||
return o; | ||
} | ||
if ( o.file_cmp ) return o; | ||
} | ||
if ( SH.__compress[o.ext] ) { | ||
o.fns.push( createCallback( response, o ) ); | ||
o.headers['Content-Encoding'] = 'deflate'; | ||
o.wait = true; | ||
|
||
zlib.deflate( o.file_buf, function( err, compressed ) { | ||
delete o.wait; | ||
if ( err ) throw err; | ||
o.file_cmp = compressed; | ||
o.headers['Content-Length'] = o.file_cmp.length; | ||
finish( o ); | ||
} ); | ||
|
||
return o; | ||
} | ||
else return o; | ||
} | ||
|
||
function copy( destination, source ) { | ||
Object.keys( source ).forEach( function( key ) { destination[key] = source[key]; } ); | ||
return destination; | ||
} | ||
|
||
function createCallback( response, o ) { | ||
return function() { respond( response, o ); }; | ||
} | ||
|
||
function finish( o ) { | ||
if ( o.fns.length ) { | ||
o.fns.forEach( function( fn ) { fn(); } ); | ||
delete o.fns; | ||
} | ||
} | ||
|
||
function prepare( SH, url, response ) { | ||
var o, p = path.normalize( process.cwd() + url.path ), | ||
ext = path.extname( p ).substring( 1 ), | ||
stat = fs.statSync( p ), | ||
headers = { | ||
'Content-Length' : stat.size, | ||
'Date' : new Date().toUTCString(), | ||
'Last-Modified' : new Date( stat.mtime ).toUTCString(), | ||
'Server' : server_info | ||
}; | ||
|
||
headers['Content-Type'] = mime.types[ext] || 'application/octet-stream'; | ||
|
||
if ( stat.isFile() ) o = { | ||
ext : ext, | ||
fns : [], | ||
file_buf : fs.readFileSync( p ), | ||
headers : headers, | ||
status : 200, | ||
success : true, | ||
url : url.path | ||
}; | ||
else { | ||
o = { | ||
ext : 'txt', | ||
fns : [], | ||
file_buf : util.format( '404 %s not found.', url.path ), | ||
headers : headers, | ||
status : 404, | ||
success : false, | ||
url : url.path | ||
}; | ||
o.headers['Content-Length'] = o.file_buf.length; | ||
o.headers['Content-Type'] = mime.types.txt; | ||
} | ||
|
||
typeof SH.__maxAge[ext] != 'number' | ||
|| ( o.headers['Cache-Control'] = util.format( 'max-age=%d', SH.__maxAge[ext] ) ); | ||
|
||
return SH.useCompression ? compress( SH, o, response ) : o; | ||
} | ||
|
||
function respond( response, o ) { | ||
response.writeHead( o.status, o.headers ); | ||
response.end( o.file_cmp || o.file_buf ); | ||
return o.success; | ||
} | ||
|
||
function send( SH, url, response ) { | ||
var o = !SH.useCache ? prepare( SH, url, response ) : cache( SH, url, response ); | ||
|
||
if ( o.wait === true ) { | ||
o.fns.push( createCallback( response, o ) ); | ||
return true; | ||
} | ||
else respond( response, o ); | ||
|
||
return o.success; | ||
} | ||
|
||
function StaticHandler( config ) { | ||
copy( this, config || {} ); | ||
|
||
this.__cache = {}; | ||
this.__compress = { txt : true }; | ||
this.__files = []; | ||
this.__directories = []; | ||
this.__maxAge = {}; | ||
} | ||
|
||
StaticHandler.prototype = { | ||
encoding : 'utf-8', | ||
maxAge : 3600, | ||
useCache : true, | ||
useCompression : true, | ||
|
||
addFileHandler : function( ext, type ) { | ||
if ( typeof type == 'string' && typeof ext == 'string' && !( ext in mime.types ) ) | ||
mime.types[ext] = type.toLowerCase(); | ||
|
||
this.__files.push( typeof ext == 'string' ? new RegExp( '\\.' + ext + '$', 'i' ) : ext ); | ||
|
||
return this; | ||
}, | ||
addDirHandler : function( dir ) { | ||
this.__directories.push( typeof dir == 'string' ? new RegExp( '\\/' + dir + '|' + dir + '\\/', 'i' ) : dir ); | ||
|
||
return this; | ||
}, | ||
compress : function() { | ||
slice.call( arguments ).forEach( function( extension ) { | ||
this.__compress[String( extension ).toLowerCase()] = true; | ||
}, this ); | ||
|
||
return this; | ||
}, | ||
maxAge : function() { | ||
var a = slice.call( arguments ), | ||
max_age = typeof a[a.length - 1] == 'number' ? a.pop() : this.maxAge; | ||
|
||
a.forEach( function( ext ) { this[ext] = max_age; }, this.__maxAge ); | ||
|
||
return this; | ||
}, | ||
serve : function( request, response ) { | ||
var urlp = url.parse( request.url ); | ||
|
||
return !!this.__files.some( function( file ) { | ||
if ( file.test( urlp.path ) ) | ||
return send( this, urlp, response ); | ||
}, this ) | ||
|| !!this.__directories.some( function( directory ) { | ||
if ( directory.test( urlp.path ) ) | ||
return send( this, urlp, response ); | ||
}, this ); | ||
} | ||
}; |
Oops, something went wrong.