mirror of
https://github.com/sstent/node.git
synced 2026-01-26 23:22:28 +00:00
updated app
This commit is contained in:
139
first-project/node_modules/gzippo/lib/compress.js
generated
vendored
Normal file
139
first-project/node_modules/gzippo/lib/compress.js
generated
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
|
||||
/*!
|
||||
* Connect - compress
|
||||
* Copyright(c) 2010 Sencha Inc.
|
||||
* Copyright(c) 2011 TJ Holowaychuk
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var zlib = require('zlib');
|
||||
|
||||
/**
|
||||
* Supported content-encoding methods.
|
||||
*/
|
||||
|
||||
exports.methods = {
|
||||
gzip: zlib.createGzip,
|
||||
deflate: zlib.createDeflate
|
||||
};
|
||||
|
||||
/**
|
||||
* Default filter function.
|
||||
*/
|
||||
|
||||
exports.filter = function(req, res){
|
||||
var type = res.getHeader('Content-Type') || '';
|
||||
return type.match(/json|text|javascript/);
|
||||
};
|
||||
|
||||
/**
|
||||
* Compress response data with gzip/deflate.
|
||||
*
|
||||
* Filter:
|
||||
*
|
||||
* A `filter` callback function may be passed to
|
||||
* replace the default logic of:
|
||||
*
|
||||
* exports.filter = function(req, res){
|
||||
* var type = res.getHeader('Content-Type') || '';
|
||||
* return type.match(/json|text|javascript/);
|
||||
* };
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* All remaining options are passed to the gzip/deflate
|
||||
* creation functions. Consult node's docs for additional details.
|
||||
*
|
||||
* - `chunkSize` (default: 16*1024)
|
||||
* - `windowBits`
|
||||
* - `level`: 0-9 where 0 is no compression, and 9 is slow but best compression
|
||||
* - `memLevel`: 1-9 low is slower but uses less memory, high is fast but uses more
|
||||
* - `strategy`: compression strategy
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function compress(options) {
|
||||
var options = options || {}
|
||||
, names = Object.keys(exports.methods)
|
||||
, filter = options.filter || exports.filter;
|
||||
|
||||
return function(req, res, next){
|
||||
var accept = req.headers['accept-encoding']
|
||||
, write = res.write
|
||||
, end = res.end
|
||||
, stream
|
||||
, method;
|
||||
|
||||
// vary
|
||||
res.setHeader('Vary', 'Accept-Encoding');
|
||||
|
||||
// proxy
|
||||
|
||||
res.write = function(chunk, encoding){
|
||||
if (!this.headerSent) this._implicitHeader();
|
||||
return stream
|
||||
? stream.write(chunk, encoding)
|
||||
: write.call(res, chunk, encoding);
|
||||
};
|
||||
|
||||
res.end = function(chunk, encoding){
|
||||
if (chunk) this.write(chunk, encoding);
|
||||
return stream
|
||||
? stream.end()
|
||||
: end.call(res);
|
||||
};
|
||||
|
||||
res.on('header', function(){
|
||||
// default request filter
|
||||
if (!filter(req, res)) return;
|
||||
|
||||
// SHOULD use identity
|
||||
if (!accept) return;
|
||||
|
||||
// head
|
||||
if ('HEAD' == req.method) return;
|
||||
|
||||
// default to gzip
|
||||
if ('*' == accept.trim()) method = 'gzip';
|
||||
|
||||
// compression method
|
||||
if (!method) {
|
||||
for (var i = 0, len = names.length; i < len; ++i) {
|
||||
if (~accept.indexOf(names[i])) {
|
||||
method = names[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// compression method
|
||||
if (!method) return;
|
||||
|
||||
// compression stream
|
||||
stream = exports.methods[method](options);
|
||||
|
||||
// header fields
|
||||
res.setHeader('Content-Encoding', method);
|
||||
res.removeHeader('Content-Length');
|
||||
|
||||
// compression
|
||||
|
||||
stream.on('data', function(chunk){
|
||||
write.call(res, chunk);
|
||||
});
|
||||
|
||||
stream.on('end', function(){
|
||||
end.call(res);
|
||||
});
|
||||
});
|
||||
|
||||
next();
|
||||
};
|
||||
}
|
||||
201
first-project/node_modules/gzippo/lib/staticGzip.js
generated
vendored
Normal file
201
first-project/node_modules/gzippo/lib/staticGzip.js
generated
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
/*!
|
||||
* Tom Gallacher
|
||||
*
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var fs = require('fs'),
|
||||
parse = require('url').parse,
|
||||
path = require('path'),
|
||||
mime = require('mime'),
|
||||
zlib = require('zlib'),
|
||||
staticSend;
|
||||
try {
|
||||
staticSend = require('connect').static.send;
|
||||
} catch (e) {
|
||||
staticSend = require('express').static.send;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip `Content-*` headers from `res`.
|
||||
*
|
||||
* @param {ServerResponse} res
|
||||
* @api public
|
||||
*/
|
||||
|
||||
var removeContentHeaders = function(res){
|
||||
Object.keys(res._headers).forEach(function(field){
|
||||
if (0 === field.indexOf('content')) {
|
||||
res.removeHeader(field);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* gzipped cache.
|
||||
*/
|
||||
|
||||
var gzippoCache = {};
|
||||
|
||||
/**
|
||||
* gzip file.
|
||||
*/
|
||||
|
||||
var gzippo = function(filename, charset, callback) {
|
||||
|
||||
fs.readFile(filename, function (err, data) {
|
||||
if (err) throw err;
|
||||
zlib.gzip(data, function(err, result) {
|
||||
callback(result);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* By default gzip's static's that match the given regular expression /text|javascript|json/
|
||||
* and then serves them with Connects static provider, denoted by the given `dirPath`.
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* - `maxAge` how long gzippo should cache gziped assets, defaulting to 1 day
|
||||
* - `clientMaxAge` client cache-control max-age directive, defaulting to 1 week
|
||||
* - `contentTypeMatch` - A regular expression tested against the Content-Type header to determine whether the response
|
||||
* should be gzipped or not. The default value is `/text|javascript|json/`.
|
||||
* - `prefix` - A url prefix. If you want all your static content in a root path such as /resource/. Any url paths not matching will be ignored
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* connect.createServer(
|
||||
* connect.staticGzip(__dirname + '/public/');
|
||||
* );
|
||||
*
|
||||
* connect.createServer(
|
||||
* connect.staticGzip(__dirname + '/public/', {maxAge: 86400000});
|
||||
* );
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Object} options
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports = module.exports = function staticGzip(dirPath, options){
|
||||
options = options || {};
|
||||
var
|
||||
maxAge = options.maxAge || 86400000,
|
||||
contentTypeMatch = options.contentTypeMatch || /text|javascript|json/,
|
||||
clientMaxAge = options.clientMaxAge || 604800000,
|
||||
prefix = options.prefix || '';
|
||||
|
||||
if (!dirPath) throw new Error('You need to provide the directory to your static content.');
|
||||
if (!contentTypeMatch.test) throw new Error('contentTypeMatch: must be a regular expression.');
|
||||
|
||||
return function staticGzip(req, res, next){
|
||||
var url, filename, contentType, acceptEncoding, charset;
|
||||
|
||||
function pass(name) {
|
||||
var o = Object.create(options);
|
||||
o.path = name;
|
||||
o.maxAge = clientMaxAge;
|
||||
staticSend(req, res, next, o);
|
||||
}
|
||||
|
||||
function setHeaders(cacheObj) {
|
||||
res.setHeader('Content-Type', contentType);
|
||||
res.setHeader('Content-Encoding', 'gzip');
|
||||
res.setHeader('Vary', 'Accept-Encoding');
|
||||
res.setHeader('Content-Length', cacheObj.content.length);
|
||||
res.setHeader('Last-Modified', cacheObj.mtime.toUTCString());
|
||||
res.setHeader('Date', new Date().toUTCString());
|
||||
res.setHeader('Expires', new Date(Date.now() + clientMaxAge).toUTCString());
|
||||
res.setHeader('Cache-Control', 'public, max-age=' + (clientMaxAge / 1000));
|
||||
res.setHeader('ETag', '"' + cacheObj.content.length + '-' + Number(cacheObj.mtime) + '"');
|
||||
}
|
||||
|
||||
function sendGzipped(cacheObj) {
|
||||
setHeaders(cacheObj);
|
||||
res.end(cacheObj.content, 'binary');
|
||||
}
|
||||
|
||||
function gzipAndSend(filename, gzipName, mtime) {
|
||||
gzippo(filename, charset, function(gzippedData) {
|
||||
gzippoCache[gzipName] = {
|
||||
'ctime': Date.now(),
|
||||
'mtime': mtime,
|
||||
'content': gzippedData
|
||||
};
|
||||
sendGzipped(gzippoCache[gzipName]);
|
||||
});
|
||||
}
|
||||
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
return next();
|
||||
}
|
||||
|
||||
url = parse(req.url);
|
||||
|
||||
// Allow a url path prefix
|
||||
if (url.pathname.substring(0, prefix.length) !== prefix) {
|
||||
return next();
|
||||
}
|
||||
|
||||
filename = path.join(dirPath, url.pathname.substring(prefix.length));
|
||||
|
||||
contentType = mime.lookup(filename);
|
||||
charset = mime.charsets.lookup(contentType, 'UTF-8');
|
||||
contentType = contentType + (charset ? '; charset=' + charset : '');
|
||||
acceptEncoding = req.headers['accept-encoding'] || '';
|
||||
|
||||
//This is storing in memory for the moment, need to think what the best way to do this.
|
||||
//Check file is not a directory
|
||||
|
||||
fs.stat(decodeURI(filename), function(err, stat) {
|
||||
if (err) {
|
||||
return pass(filename);
|
||||
}
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
return pass(req.url);
|
||||
}
|
||||
|
||||
if (!contentTypeMatch.test(contentType)) {
|
||||
return pass(filename);
|
||||
}
|
||||
|
||||
if (!~acceptEncoding.indexOf('gzip')) {
|
||||
return pass(filename);
|
||||
}
|
||||
|
||||
var base = path.basename(filename),
|
||||
dir = path.dirname(filename),
|
||||
gzipName = path.join(dir, base + '.gz');
|
||||
|
||||
if (req.headers['if-modified-since'] &&
|
||||
gzippoCache[gzipName] &&
|
||||
+stat.mtime <= new Date(req.headers['if-modified-since']).getTime()) {
|
||||
setHeaders(gzippoCache[gzipName]);
|
||||
removeContentHeaders(res);
|
||||
res.statusCode = 304;
|
||||
return res.end();
|
||||
}
|
||||
|
||||
//TODO: check for pre-compressed file
|
||||
if (typeof gzippoCache[gzipName] === 'undefined') {
|
||||
gzipAndSend(filename, gzipName, stat.mtime);
|
||||
} else {
|
||||
if ((gzippoCache[gzipName].mtime < stat.mtime) ||
|
||||
((gzippoCache[gzipName].ctime + maxAge) < Date.now())) {
|
||||
gzipAndSend(filename, gzipName, stat.mtime);
|
||||
} else {
|
||||
sendGzipped(gzippoCache[gzipName]);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user