base project
This commit is contained in:
96
node_modules/union/examples/simple/middleware/favicon.js
generated
vendored
Normal file
96
node_modules/union/examples/simple/middleware/favicon.js
generated
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
/*!
|
||||
* Connect - favicon
|
||||
* Copyright(c) 2010 Sencha Inc.
|
||||
* Copyright(c) 2011 TJ Holowaychuk
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var crypto = require('crypto')
|
||||
, fs = require('fs');
|
||||
|
||||
/**
|
||||
* Favicon cache.
|
||||
*/
|
||||
|
||||
var icon;
|
||||
|
||||
/**
|
||||
* Return md5 hash of the given string and optional encoding,
|
||||
* defaulting to hex.
|
||||
*
|
||||
* utils.md5('wahoo');
|
||||
* // => "e493298061761236c96b02ea6aa8a2ad"
|
||||
*
|
||||
* @param {String} str
|
||||
* @param {String} encoding
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.md5 = function (str, encoding) {
|
||||
return crypto
|
||||
.createHash('md5')
|
||||
.update(str)
|
||||
.digest(encoding || 'hex');
|
||||
};
|
||||
|
||||
/**
|
||||
* By default serves the connect favicon, or the favicon
|
||||
* located by the given `path`.
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* - `maxAge` cache-control max-age directive, defaulting to 1 day
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* connect.createServer(
|
||||
* connect.favicon()
|
||||
* );
|
||||
*
|
||||
* connect.createServer(
|
||||
* connect.favicon(__dirname + '/public/favicon.ico')
|
||||
* );
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Object} options
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
module.exports = function favicon(path, options) {
|
||||
var options = options || {}
|
||||
, path = path || __dirname + '/../public/favicon.ico'
|
||||
, maxAge = options.maxAge || 86400000;
|
||||
|
||||
return function favicon(req, res, next) {
|
||||
if ('/favicon.ico' == req.url) {
|
||||
if (icon) {
|
||||
res.writeHead(200, icon.headers);
|
||||
res.end(icon.body);
|
||||
} else {
|
||||
fs.readFile(path, function (err, buf) {
|
||||
if (err) return next(err);
|
||||
icon = {
|
||||
headers: {
|
||||
'Content-Type': 'image/x-icon'
|
||||
, 'Content-Length': buf.length
|
||||
, 'ETag': '"' + exports.md5(buf) + '"'
|
||||
, 'Cache-Control': 'public, max-age=' + (maxAge / 1000)
|
||||
},
|
||||
body: buf
|
||||
};
|
||||
res.writeHead(200, icon.headers);
|
||||
res.end(icon.body);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
};
|
26
node_modules/union/examples/simple/middleware/gzip-decode.js
generated
vendored
Normal file
26
node_modules/union/examples/simple/middleware/gzip-decode.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
var spawn = require('child_process').spawn,
|
||||
util = require('util'),
|
||||
RequestStream = require('../../lib').RequestStream;
|
||||
|
||||
var GzipDecode = module.exports = function GzipDecoder(options) {
|
||||
RequestStream.call(this, options);
|
||||
|
||||
this.on('pipe', this.decode);
|
||||
}
|
||||
|
||||
util.inherits(GzipDecode, RequestStream);
|
||||
|
||||
GzipDecode.prototype.decode = function (source) {
|
||||
this.decoder = spawn('gunzip');
|
||||
this.decoder.stdout.on('data', this._onGunzipData.bind(this));
|
||||
this.decoder.stdout.on('end', this._onGunzipEnd.bind(this));
|
||||
source.pipe(this.decoder);
|
||||
}
|
||||
|
||||
GzipDecoderStack.prototype._onGunzipData = function (chunk) {
|
||||
this.emit('data', chunk);
|
||||
}
|
||||
|
||||
GzipDecoderStack.prototype._onGunzipEnd = function () {
|
||||
this.emit('end');
|
||||
}
|
40
node_modules/union/examples/simple/middleware/gzip-encode.js
generated
vendored
Normal file
40
node_modules/union/examples/simple/middleware/gzip-encode.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
var spawn = require('child_process').spawn,
|
||||
util = require('util'),
|
||||
ResponseStream = require('../../lib').ResponseStream;
|
||||
|
||||
/**
|
||||
* Accepts a writable stream, i.e. fs.WriteStream, and returns a StreamStack
|
||||
* whose 'write()' calls are transparently sent to a 'gzip' process before
|
||||
* being written to the target stream.
|
||||
*/
|
||||
var GzipEncode = module.exports = function GzipEncode(options) {
|
||||
ResponseStream.call(this, options);
|
||||
|
||||
if (compression) {
|
||||
process.assert(compression >= 1 && compression <= 9);
|
||||
this.compression = compression;
|
||||
}
|
||||
|
||||
this.on('pipe', this.encode);
|
||||
}
|
||||
|
||||
util.inherits(GzipEncode, ResponseStream);
|
||||
|
||||
GzipEncode.prototype.encode = function (source) {
|
||||
this.source = source;
|
||||
};
|
||||
|
||||
GzipEncode.prototype.pipe = function (dest) {
|
||||
if (!this.source) {
|
||||
throw new Error('GzipEncode is only pipeable once it has been piped to');
|
||||
}
|
||||
|
||||
this.encoder = spawn('gzip', ['-'+this.compression]);
|
||||
this.encoder.stdout.pipe(dest);
|
||||
this.encoder.stdin.pipe(this.source);
|
||||
};
|
||||
|
||||
inherits(GzipEncoderStack, StreamStack);
|
||||
exports.GzipEncoderStack = GzipEncoderStack;
|
||||
|
||||
GzipEncoderStack.prototype.compression = 6;
|
Reference in New Issue
Block a user