darkmode toggle
This commit is contained in:
1
node_modules/union/.gitattributes
generated
vendored
Normal file
1
node_modules/union/.gitattributes
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
package-lock.json binary
|
12
node_modules/union/.travis.yml
generated
vendored
Normal file
12
node_modules/union/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.10"
|
||||
- "0.12"
|
||||
- "4"
|
||||
- "6"
|
||||
|
||||
notifications:
|
||||
email:
|
||||
- travis@nodejitsu.com
|
||||
irc: "irc.freenode.org#nodejitsu"
|
||||
|
7
node_modules/union/CHANGELOG.md
generated
vendored
Normal file
7
node_modules/union/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
0.3.4 / 2012-07-24
|
||||
==================
|
||||
|
||||
* Added SPDY support
|
||||
* Added http redirect utility function
|
||||
|
19
node_modules/union/LICENSE
generated
vendored
Normal file
19
node_modules/union/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2010 Charlie Robbins & the Contributors.
|
||||
|
||||
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.
|
323
node_modules/union/README.md
generated
vendored
Normal file
323
node_modules/union/README.md
generated
vendored
Normal file
@@ -0,0 +1,323 @@
|
||||
|
||||
<img src="https://github.com/flatiron/union/raw/master/union.png" />
|
||||
|
||||
# Synopsis
|
||||
A hybrid streaming middleware kernel backwards compatible with connect.
|
||||
|
||||
# Motivation
|
||||
The advantage to streaming middlewares is that they do not require buffering the entire stream in order to execute their function.
|
||||
|
||||
# Status
|
||||
|
||||
[](http://travis-ci.org/flatiron/union)
|
||||
|
||||
# Installation
|
||||
There are a few ways to use `union`. Install the library using npm. You can add it to your `package.json` file as a dependancy
|
||||
|
||||
```bash
|
||||
$ [sudo] npm install union
|
||||
```
|
||||
|
||||
## Usage
|
||||
Union's request handling is [connect](https://github.com/senchalabs/connect)-compatible, meaning that all existing connect middlewares should work out-of-the-box with union.
|
||||
|
||||
**(Union 0.3.x is compatible with connect >= 2.1.0)**
|
||||
|
||||
In addition, the response object passed to middlewares listens for a "next" event, which is equivalent to calling `next()`. Flatiron middlewares are written in this manner, meaning they are not reverse-compatible with connect.
|
||||
|
||||
### A simple case
|
||||
|
||||
``` js
|
||||
var fs = require('fs'),
|
||||
union = require('../lib'),
|
||||
director = require('director');
|
||||
|
||||
var router = new director.http.Router();
|
||||
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
var found = router.dispatch(req, res);
|
||||
if (!found) {
|
||||
res.emit('next');
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
router.get(/foo/, function () {
|
||||
this.res.writeHead(200, { 'Content-Type': 'text/plain' })
|
||||
this.res.end('hello world\n');
|
||||
});
|
||||
|
||||
router.post(/foo/, { stream: true }, function () {
|
||||
var req = this.req,
|
||||
res = this.res,
|
||||
writeStream;
|
||||
|
||||
writeStream = fs.createWriteStream(Date.now() + '-foo.txt');
|
||||
req.pipe(writeStream);
|
||||
|
||||
writeStream.on('close', function () {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('wrote to a stream!');
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(9090);
|
||||
console.log('union with director running on 9090');
|
||||
```
|
||||
|
||||
To demonstrate the code, we use [director](https://github.com/flatiron/director). A light-weight, Client AND Server side URL-Router for Node.js and Single Page Apps!
|
||||
|
||||
### A case with connect
|
||||
|
||||
Code based on connect
|
||||
|
||||
```js
|
||||
var connect = require('connect')
|
||||
, http = require('http');
|
||||
|
||||
var app = connect()
|
||||
.use(connect.favicon())
|
||||
.use(connect.logger('dev'))
|
||||
.use(connect.static('public'))
|
||||
.use(connect.directory('public'))
|
||||
.use(connect.cookieParser('my secret here'))
|
||||
.use(connect.session())
|
||||
.use(function (req, res) {
|
||||
res.end('Hello from Connect!\n');
|
||||
});
|
||||
|
||||
http.createServer(app).listen(3000);
|
||||
```
|
||||
|
||||
Code based on union
|
||||
|
||||
```js
|
||||
var connect = require('connect')
|
||||
, union = require('union');
|
||||
|
||||
var server = union.createServer({
|
||||
buffer: false,
|
||||
before: [
|
||||
connect.favicon(),
|
||||
connect.logger('dev'),
|
||||
connect.static('public'),
|
||||
connect.directory('public'),
|
||||
connect.cookieParser('my secret here'),
|
||||
connect.session(),
|
||||
function (req, res) {
|
||||
res.end('Hello from Connect!\n');
|
||||
},
|
||||
]
|
||||
}).listen(3000);
|
||||
```
|
||||
|
||||
### SPDY enabled server example
|
||||
|
||||
# API
|
||||
|
||||
## union Static Members
|
||||
|
||||
### createServer(options)
|
||||
The `options` object is required. Options include:
|
||||
|
||||
Specification
|
||||
|
||||
```
|
||||
function createServer(options)
|
||||
|
||||
@param options {Object}
|
||||
An object literal that represents the configuration for the server.
|
||||
|
||||
@option before {Array}
|
||||
The `before` value is an array of middlewares, which are used to route and serve incoming
|
||||
requests. For instance, in the example, `favicon` is a middleware which handles requests
|
||||
for `/favicon.ico`.
|
||||
|
||||
@option after {Array}
|
||||
The `after` value is an array of functions that return stream filters,
|
||||
which are applied after the request handlers in `options.before`.
|
||||
Stream filters inherit from `union.ResponseStream`, which implements the
|
||||
Node.js core streams api with a bunch of other goodies.
|
||||
|
||||
@option limit {Object}
|
||||
(optional) A value, passed to internal instantiations of `union.BufferedStream`.
|
||||
|
||||
@option https {Object}
|
||||
(optional) A value that specifies the certificate and key necessary to create an instance of
|
||||
`https.Server`.
|
||||
|
||||
@option spdy {Object}
|
||||
(optional) A value that specifies the certificate and key necessary to create an instance of
|
||||
`spdy.Server`.
|
||||
|
||||
@option headers {Object}
|
||||
(optional) An object representing a set of headers to set in every outgoing response
|
||||
```
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
favicon('./favicon.png'),
|
||||
function (req, res) {
|
||||
var found = router.dispatch(req, res);
|
||||
if (!found) {
|
||||
res.emit('next');
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
An example of the `https` or `spdy` option.
|
||||
|
||||
``` js
|
||||
{
|
||||
cert: 'path/to/cert.pem',
|
||||
key: 'path/to/key.pem',
|
||||
ca: 'path/to/ca.pem'
|
||||
}
|
||||
```
|
||||
|
||||
An example of the `headers` option.
|
||||
|
||||
``` js
|
||||
{
|
||||
'x-powered-by': 'your-sweet-application v10.9.8'
|
||||
}
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
Error handler is similiar to middlware but takes an extra argument for error at the beginning.
|
||||
|
||||
```js
|
||||
var handle = function (err, req, res) {
|
||||
res.statusCode = err.status;
|
||||
res.end(req.headers);
|
||||
};
|
||||
|
||||
var server = union.createServer({
|
||||
onError: handle,
|
||||
before: [
|
||||
favicon('./favicon.png'),
|
||||
function (req, res) {
|
||||
var found = router.dispatch(req, res);
|
||||
if (!found) {
|
||||
res.emit('next');
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
## BufferedStream Constructor
|
||||
This constructor inherits from `Stream` and can buffer data up to `limit` bytes. It also implements `pause` and `resume` methods.
|
||||
|
||||
Specification
|
||||
|
||||
```
|
||||
function BufferedStream(limit)
|
||||
|
||||
@param limit {Number}
|
||||
the limit for which the stream can be buffered
|
||||
```
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
var bs = union.BufferedStream(n);
|
||||
```
|
||||
|
||||
## HttpStream Constructor
|
||||
This constructor inherits from `union.BufferedStream` and returns a stream with these extra properties:
|
||||
|
||||
Specification
|
||||
|
||||
```
|
||||
function HttpStream()
|
||||
```
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
var hs = union.HttpStream();
|
||||
```
|
||||
|
||||
## HttpStream Instance Members
|
||||
|
||||
### url
|
||||
The url from the request.
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
httpStream.url = '';
|
||||
```
|
||||
|
||||
### headers
|
||||
The HTTP headers associated with the stream.
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
httpStream.headers = '';
|
||||
```
|
||||
|
||||
### method
|
||||
The HTTP method ("GET", "POST", etc).
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
httpStream.method = 'POST';
|
||||
```
|
||||
|
||||
### query
|
||||
The querystring associated with the stream (if applicable).
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
httpStream.query = '';
|
||||
```
|
||||
|
||||
## ResponseStream Constructor
|
||||
This constructor inherits from `union.HttpStream`, and is additionally writeable. Union supplies this constructor as a basic response stream middleware from which to inherit.
|
||||
|
||||
Specification
|
||||
|
||||
```
|
||||
function ResponseStream()
|
||||
```
|
||||
|
||||
Example
|
||||
|
||||
```js
|
||||
var rs = union.ResponseStream();
|
||||
```
|
||||
|
||||
# Tests
|
||||
|
||||
All tests are written with [vows][0] and should be run with [npm][1]:
|
||||
|
||||
``` bash
|
||||
$ npm test
|
||||
```
|
||||
|
||||
# Licence
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2010-2012 Charlie Robbins & the Contributors
|
||||
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.
|
||||
|
||||
[0]: http://vowsjs.org
|
||||
[1]: http://npmjs.org
|
26
node_modules/union/examples/after/index.js
generated
vendored
Normal file
26
node_modules/union/examples/after/index.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
union = require('../../lib');
|
||||
|
||||
var server = union.createServer({
|
||||
before: [ function (req,res) {
|
||||
if (req.url === "/foo") {
|
||||
res.text(201, "foo");
|
||||
}
|
||||
} ],
|
||||
after: [
|
||||
function LoggerStream() {
|
||||
var stream = new union.ResponseStream();
|
||||
|
||||
stream.once("pipe", function (req) {
|
||||
console.log({res: this.res.statusCode, method: this.req.method});
|
||||
});
|
||||
|
||||
return stream;
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
server.listen(9080);
|
||||
console.log('union running on 9080');
|
||||
|
BIN
node_modules/union/examples/simple/favicon.png
generated
vendored
Normal file
BIN
node_modules/union/examples/simple/favicon.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 545 B |
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;
|
60
node_modules/union/examples/simple/simple.js
generated
vendored
Normal file
60
node_modules/union/examples/simple/simple.js
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
union = require('../../lib'),
|
||||
director = require('director'),
|
||||
favicon = require('./middleware/favicon');
|
||||
|
||||
var router = new director.http.Router();
|
||||
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
favicon(path.join(__dirname, 'favicon.png')),
|
||||
function (req, res) {
|
||||
var found = router.dispatch(req, res);
|
||||
if (!found) {
|
||||
res.emit('next');
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
router.get('/foo', function () {
|
||||
this.res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
this.res.end('hello world\n');
|
||||
});
|
||||
|
||||
router.post('/foo', { stream: true }, function () {
|
||||
var req = this.req,
|
||||
res = this.res,
|
||||
writeStream;
|
||||
|
||||
writeStream = fs.createWriteStream(__dirname + '/' + Date.now() + '-foo.txt');
|
||||
req.pipe(writeStream);
|
||||
|
||||
writeStream.on('close', function () {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('wrote to a stream!');
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/redirect', function () {
|
||||
this.res.redirect('http://www.google.com');
|
||||
});
|
||||
|
||||
router.get('/custom_redirect', function () {
|
||||
this.res.redirect('/foo', 301);
|
||||
});
|
||||
|
||||
router.get('/async', function () {
|
||||
var self = this;
|
||||
process.nextTick(function () {
|
||||
self.req.on('end', function () {
|
||||
self.res.end();
|
||||
})
|
||||
self.req.buffer = false;
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(9090);
|
||||
console.log('union with director running on 9090');
|
||||
|
30
node_modules/union/examples/simple/spdy.js
generated
vendored
Normal file
30
node_modules/union/examples/simple/spdy.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// In order to run this example you need to
|
||||
// generate local ssl certificate
|
||||
var union = require('../../lib'),
|
||||
director = require('director');
|
||||
|
||||
var router = new director.http.Router();
|
||||
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
var found = router.dispatch(req, res);
|
||||
if (!found) {
|
||||
res.emit('next');
|
||||
}
|
||||
}
|
||||
],
|
||||
spdy :{
|
||||
key: './certs/privatekey.pem',
|
||||
cert: './certs/certificate.pem'
|
||||
}
|
||||
});
|
||||
|
||||
router.get(/foo/, function () {
|
||||
this.res.writeHead(200, { 'Content-Type': 'text/plain' })
|
||||
this.res.end('hello world\n');
|
||||
});
|
||||
|
||||
server.listen(9090, function () {
|
||||
console.log('union with director running on 9090 with SPDY');
|
||||
});
|
13
node_modules/union/examples/socketio/README
generated
vendored
Normal file
13
node_modules/union/examples/socketio/README
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
This folder contains an example of how to use Union with Socket.io.
|
||||
|
||||
First, you'll want to install both Union and Socket.io. Run this
|
||||
command in the folder you placed these two files:
|
||||
|
||||
npm install union socket.io
|
||||
|
||||
You can run the server like so:
|
||||
|
||||
node server.js
|
||||
|
||||
Now open up your web browser to http://localhost and see the results
|
||||
in the console!
|
8
node_modules/union/examples/socketio/index.html
generated
vendored
Normal file
8
node_modules/union/examples/socketio/index.html
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
var socket = io.connect('http://localhost');
|
||||
socket.on('news', function (data) {
|
||||
console.log(data);
|
||||
socket.emit('my other event', { my: 'data' });
|
||||
});
|
||||
</script>
|
30
node_modules/union/examples/socketio/server.js
generated
vendored
Normal file
30
node_modules/union/examples/socketio/server.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
var fs = require('fs'),
|
||||
union = require('union');
|
||||
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
fs.readFile(__dirname + '/index.html',
|
||||
function (err, data) {
|
||||
if (err) {
|
||||
res.writeHead(500);
|
||||
return res.end('Error loading index.html');
|
||||
}
|
||||
|
||||
res.writeHead(200);
|
||||
res.end(data);
|
||||
});
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
server.listen(9090);
|
||||
|
||||
var io = require('socket.io').listen(server);
|
||||
|
||||
io.sockets.on('connection', function (socket) {
|
||||
socket.emit('news', {hello: 'world'});
|
||||
socket.on('my other event', function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
});
|
141
node_modules/union/lib/buffered-stream.js
generated
vendored
Normal file
141
node_modules/union/lib/buffered-stream.js
generated
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* buffered-stream.js: A simple(r) Stream which is partially buffered into memory.
|
||||
*
|
||||
* (C) 2010, Mikeal Rogers
|
||||
*
|
||||
* Adapted for Flatiron
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var events = require('events'),
|
||||
fs = require('fs'),
|
||||
stream = require('stream'),
|
||||
util = require('util');
|
||||
|
||||
//
|
||||
// ### function BufferedStream (limit)
|
||||
// #### @limit {number} **Optional** Size of the buffer to limit
|
||||
// Constructor function for the BufferedStream object responsible for
|
||||
// maintaining a stream interface which can also persist to memory
|
||||
// temporarily.
|
||||
//
|
||||
|
||||
var BufferedStream = module.exports = function (limit) {
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
if (typeof limit === 'undefined') {
|
||||
limit = Infinity;
|
||||
}
|
||||
|
||||
this.limit = limit;
|
||||
this.size = 0;
|
||||
this.chunks = [];
|
||||
this.writable = true;
|
||||
this.readable = true;
|
||||
this._buffer = true;
|
||||
};
|
||||
|
||||
util.inherits(BufferedStream, stream.Stream);
|
||||
|
||||
Object.defineProperty(BufferedStream.prototype, 'buffer', {
|
||||
get: function () {
|
||||
return this._buffer;
|
||||
},
|
||||
set: function (value) {
|
||||
if (!value && this.chunks) {
|
||||
var self = this;
|
||||
this.chunks.forEach(function (c) { self.emit('data', c) });
|
||||
if (this.ended) this.emit('end');
|
||||
this.size = 0;
|
||||
delete this.chunks;
|
||||
}
|
||||
|
||||
this._buffer = value;
|
||||
}
|
||||
});
|
||||
|
||||
BufferedStream.prototype.pipe = function () {
|
||||
var self = this,
|
||||
dest;
|
||||
|
||||
if (self.resume) {
|
||||
self.resume();
|
||||
}
|
||||
|
||||
dest = stream.Stream.prototype.pipe.apply(self, arguments);
|
||||
|
||||
//
|
||||
// just incase you are piping to two streams, do not emit data twice.
|
||||
// note: you can pipe twice, but you need to pipe both streams in the same tick.
|
||||
// (this is normal for streams)
|
||||
//
|
||||
if (this.piped) {
|
||||
return dest;
|
||||
}
|
||||
|
||||
process.nextTick(function () {
|
||||
if (self.chunks) {
|
||||
self.chunks.forEach(function (c) { self.emit('data', c) });
|
||||
self.size = 0;
|
||||
delete self.chunks;
|
||||
}
|
||||
|
||||
if (!self.readable) {
|
||||
if (self.ended) {
|
||||
self.emit('end');
|
||||
}
|
||||
else if (self.closed) {
|
||||
self.emit('close');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.piped = true;
|
||||
|
||||
return dest;
|
||||
};
|
||||
|
||||
BufferedStream.prototype.write = function (chunk) {
|
||||
if (!this.chunks || this.piped) {
|
||||
this.emit('data', chunk);
|
||||
return;
|
||||
}
|
||||
|
||||
this.chunks.push(chunk);
|
||||
this.size += chunk.length;
|
||||
if (this.limit < this.size) {
|
||||
this.pause();
|
||||
}
|
||||
};
|
||||
|
||||
BufferedStream.prototype.end = function () {
|
||||
this.readable = false;
|
||||
this.ended = true;
|
||||
this.emit('end');
|
||||
};
|
||||
|
||||
BufferedStream.prototype.destroy = function () {
|
||||
this.readable = false;
|
||||
this.writable = false;
|
||||
delete this.chunks;
|
||||
};
|
||||
|
||||
BufferedStream.prototype.close = function () {
|
||||
this.readable = false;
|
||||
this.closed = true;
|
||||
};
|
||||
|
||||
if (!stream.Stream.prototype.pause) {
|
||||
BufferedStream.prototype.pause = function () {
|
||||
this.emit('pause');
|
||||
};
|
||||
}
|
||||
|
||||
if (!stream.Stream.prototype.resume) {
|
||||
BufferedStream.prototype.resume = function () {
|
||||
this.emit('resume');
|
||||
};
|
||||
}
|
||||
|
108
node_modules/union/lib/core.js
generated
vendored
Normal file
108
node_modules/union/lib/core.js
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* core.js: Core functionality for the Flatiron HTTP (with SPDY support) plugin.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var http = require('http'),
|
||||
https = require('https'),
|
||||
fs = require('fs'),
|
||||
stream = require('stream'),
|
||||
HttpStream = require('./http-stream'),
|
||||
RoutingStream = require('./routing-stream');
|
||||
|
||||
var core = exports;
|
||||
|
||||
core.createServer = function (options) {
|
||||
var isArray = Array.isArray(options.after),
|
||||
credentials;
|
||||
|
||||
if (!options) {
|
||||
throw new Error('options is required to create a server');
|
||||
}
|
||||
|
||||
function requestHandler(req, res) {
|
||||
var routingStream = new RoutingStream({
|
||||
before: options.before,
|
||||
buffer: options.buffer,
|
||||
//
|
||||
// Remark: without new after is a huge memory leak that
|
||||
// pipes to every single open connection
|
||||
//
|
||||
after: isArray && options.after.map(function (After) {
|
||||
return new After;
|
||||
}),
|
||||
request: req,
|
||||
response: res,
|
||||
limit: options.limit,
|
||||
headers: options.headers
|
||||
});
|
||||
|
||||
routingStream.on('error', function (err) {
|
||||
var fn = options.onError || core.errorHandler;
|
||||
fn(err, routingStream, routingStream.target, function () {
|
||||
routingStream.target.emit('next');
|
||||
});
|
||||
});
|
||||
|
||||
req.pipe(routingStream);
|
||||
}
|
||||
|
||||
//
|
||||
// both https and spdy requires same params
|
||||
//
|
||||
if (options.https || options.spdy) {
|
||||
if (options.https && options.spdy) {
|
||||
throw new Error('You shouldn\'t be using https and spdy simultaneously.');
|
||||
}
|
||||
|
||||
var serverOptions,
|
||||
credentials,
|
||||
key = !options.spdy
|
||||
? 'https'
|
||||
: 'spdy';
|
||||
|
||||
serverOptions = options[key];
|
||||
if (!serverOptions.key || !serverOptions.cert) {
|
||||
throw new Error('Both options.' + key + '.`key` and options.' + key + '.`cert` are required.');
|
||||
}
|
||||
|
||||
credentials = {
|
||||
key: fs.readFileSync(serverOptions.key),
|
||||
cert: fs.readFileSync(serverOptions.cert)
|
||||
};
|
||||
|
||||
if (serverOptions.ca) {
|
||||
serverOptions.ca = !Array.isArray(serverOptions.ca)
|
||||
? [serverOptions.ca]
|
||||
: serverOptions.ca
|
||||
|
||||
credentials.ca = serverOptions.ca.map(function (ca) {
|
||||
return fs.readFileSync(ca);
|
||||
});
|
||||
}
|
||||
|
||||
if (options.spdy) {
|
||||
// spdy is optional so we require module here rather than on top
|
||||
var spdy = require('spdy');
|
||||
return spdy.createServer(credentials, requestHandler);
|
||||
}
|
||||
|
||||
return https.createServer(credentials, requestHandler);
|
||||
}
|
||||
|
||||
return http.createServer(requestHandler);
|
||||
};
|
||||
|
||||
core.errorHandler = function error(err, req, res) {
|
||||
if (err) {
|
||||
(this.res || res).writeHead(err.status || 500, err.headers || { "Content-Type": "text/plain" });
|
||||
(this.res || res).end(err.message + "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
(this.res || res).writeHead(404, {"Content-Type": "text/plain"});
|
||||
(this.res || res).end("Not Found\n");
|
||||
};
|
52
node_modules/union/lib/http-stream.js
generated
vendored
Normal file
52
node_modules/union/lib/http-stream.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* http-stream.js: Idomatic buffered stream which pipes additional HTTP information.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var url = require('url'),
|
||||
util = require('util'),
|
||||
qs = require('qs'),
|
||||
BufferedStream = require('./buffered-stream');
|
||||
|
||||
var HttpStream = module.exports = function (options) {
|
||||
options = options || {};
|
||||
BufferedStream.call(this, options.limit);
|
||||
|
||||
if (options.buffer === false) {
|
||||
this.buffer = false;
|
||||
}
|
||||
|
||||
this.on('pipe', this.pipeState);
|
||||
};
|
||||
|
||||
util.inherits(HttpStream, BufferedStream);
|
||||
|
||||
//
|
||||
// ### function pipeState (source)
|
||||
// #### @source {ServerRequest|HttpStream} Source stream piping to this instance
|
||||
// Pipes additional HTTP metadata from the `source` HTTP stream (either concrete or
|
||||
// abstract) to this instance. e.g. url, headers, query, etc.
|
||||
//
|
||||
// Remark: Is there anything else we wish to pipe?
|
||||
//
|
||||
HttpStream.prototype.pipeState = function (source) {
|
||||
this.headers = source.headers;
|
||||
this.trailers = source.trailers;
|
||||
this.method = source.method;
|
||||
|
||||
if (source.url) {
|
||||
this.url = this.originalUrl = source.url;
|
||||
}
|
||||
|
||||
if (source.query) {
|
||||
this.query = source.query;
|
||||
}
|
||||
else if (source.url) {
|
||||
this.query = ~source.url.indexOf('?')
|
||||
? qs.parse(url.parse(source.url).query)
|
||||
: {};
|
||||
}
|
||||
};
|
24
node_modules/union/lib/index.js
generated
vendored
Normal file
24
node_modules/union/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* index.js: Top-level plugin exposing HTTP features in flatiron
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var union = exports;
|
||||
|
||||
//
|
||||
// Expose version information
|
||||
//
|
||||
exports.version = require('../package.json').version;
|
||||
|
||||
//
|
||||
// Expose core union components
|
||||
//
|
||||
union.BufferedStream = require('./buffered-stream');
|
||||
union.HttpStream = require('./http-stream');
|
||||
union.ResponseStream = require('./response-stream');
|
||||
union.RoutingStream = require('./routing-stream');
|
||||
union.createServer = require('./core').createServer;
|
||||
union.errorHandler = require('./core').errorHandler;
|
58
node_modules/union/lib/request-stream.js
generated
vendored
Normal file
58
node_modules/union/lib/request-stream.js
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* http-stream.js: Idomatic buffered stream which pipes additional HTTP information.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var url = require('url'),
|
||||
util = require('util'),
|
||||
qs = require('qs'),
|
||||
HttpStream = require('./http-stream');
|
||||
|
||||
var RequestStream = module.exports = function (options) {
|
||||
options = options || {};
|
||||
HttpStream.call(this, options);
|
||||
|
||||
this.on('pipe', this.pipeRequest);
|
||||
this.request = options.request;
|
||||
};
|
||||
|
||||
util.inherits(RequestStream, HttpStream);
|
||||
|
||||
//
|
||||
// ### function pipeRequest (source)
|
||||
// #### @source {ServerRequest|HttpStream} Source stream piping to this instance
|
||||
// Pipes additional HTTP request metadata from the `source` HTTP stream (either concrete or
|
||||
// abstract) to this instance. e.g. url, headers, query, etc.
|
||||
//
|
||||
// Remark: Is there anything else we wish to pipe?
|
||||
//
|
||||
RequestStream.prototype.pipeRequest = function (source) {
|
||||
this.url = this.originalUrl = source.url;
|
||||
this.method = source.method;
|
||||
this.httpVersion = source.httpVersion;
|
||||
this.httpVersionMajor = source.httpVersionMajor;
|
||||
this.httpVersionMinor = source.httpVersionMinor;
|
||||
this.setEncoding = source.setEncoding;
|
||||
this.connection = source.connection;
|
||||
this.socket = source.socket;
|
||||
|
||||
if (source.query) {
|
||||
this.query = source.query;
|
||||
}
|
||||
else {
|
||||
this.query = ~source.url.indexOf('?')
|
||||
? qs.parse(url.parse(source.url).query)
|
||||
: {};
|
||||
}
|
||||
};
|
||||
|
||||
// http.serverRequest methods
|
||||
['setEncoding'].forEach(function (method) {
|
||||
RequestStream.prototype[method] = function () {
|
||||
return this.request[method].apply(this.request, arguments);
|
||||
};
|
||||
});
|
||||
|
203
node_modules/union/lib/response-stream.js
generated
vendored
Normal file
203
node_modules/union/lib/response-stream.js
generated
vendored
Normal file
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
* response-stream.js: A Stream focused on writing any relevant information to
|
||||
* a raw http.ServerResponse object.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var util = require('util'),
|
||||
HttpStream = require('./http-stream');
|
||||
|
||||
var STATUS_CODES = require('http').STATUS_CODES;
|
||||
|
||||
//
|
||||
// ### function ResponseStream (options)
|
||||
//
|
||||
//
|
||||
var ResponseStream = module.exports = function (options) {
|
||||
var self = this,
|
||||
key;
|
||||
|
||||
options = options || {};
|
||||
HttpStream.call(this, options);
|
||||
|
||||
this.writeable = true;
|
||||
this.response = options.response;
|
||||
|
||||
if (options.headers) {
|
||||
for (key in options.headers) {
|
||||
this.response.setHeader(key, options.headers[key]);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Proxy `statusCode` changes to the actual `response.statusCode`.
|
||||
//
|
||||
Object.defineProperty(this, 'statusCode', {
|
||||
get: function () {
|
||||
return self.response.statusCode;
|
||||
},
|
||||
set: function (value) {
|
||||
self.response.statusCode = value;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
if (this.response) {
|
||||
this._headers = this.response._headers = this.response._headers || {};
|
||||
|
||||
// Patch to node core
|
||||
this.response._headerNames = this.response._headerNames || {};
|
||||
|
||||
//
|
||||
// Proxy to emit "header" event
|
||||
//
|
||||
this._renderHeaders = this.response._renderHeaders;
|
||||
this.response._renderHeaders = function () {
|
||||
if (!self._emittedHeader) {
|
||||
self._emittedHeader = true;
|
||||
self.headerSent = true;
|
||||
self._header = true;
|
||||
self.emit('header');
|
||||
}
|
||||
|
||||
return self._renderHeaders.call(self.response);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
util.inherits(ResponseStream, HttpStream);
|
||||
|
||||
ResponseStream.prototype.writeHead = function (statusCode, statusMessage, headers) {
|
||||
if (typeof statusMessage === 'string') {
|
||||
this.response.statusMessage = statusMessage;
|
||||
} else {
|
||||
this.response.statusMessage = this.response.statusMessage
|
||||
|| STATUS_CODES[statusCode] || 'unknown';
|
||||
headers = statusMessage;
|
||||
}
|
||||
|
||||
this.response.statusCode = statusCode;
|
||||
|
||||
if (headers) {
|
||||
var keys = Object.keys(headers);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var k = keys[i];
|
||||
if (k) this.response.setHeader(k, headers[k]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Create pass-thru for the necessary
|
||||
// `http.ServerResponse` methods.
|
||||
//
|
||||
['setHeader', 'getHeader', 'removeHeader', '_implicitHeader', 'addTrailers'].forEach(function (method) {
|
||||
ResponseStream.prototype[method] = function () {
|
||||
return this.response[method].apply(this.response, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
ResponseStream.prototype.json = function (obj) {
|
||||
if (!this.response.writable) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof obj === 'number') {
|
||||
this.response.statusCode = obj;
|
||||
obj = arguments[1];
|
||||
}
|
||||
|
||||
this.modified = true;
|
||||
|
||||
if (!this.response._header && this.response.getHeader('content-type') !== 'application/json') {
|
||||
this.response.setHeader('content-type', 'application/json');
|
||||
}
|
||||
|
||||
this.end(obj ? JSON.stringify(obj) : '');
|
||||
};
|
||||
|
||||
ResponseStream.prototype.html = function (str) {
|
||||
if (!this.response.writable) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof str === 'number') {
|
||||
this.response.statusCode = str;
|
||||
str = arguments[1];
|
||||
}
|
||||
|
||||
this.modified = true;
|
||||
|
||||
if (!this.response._header && this.response.getHeader('content-type') !== 'text/html') {
|
||||
this.response.setHeader('content-type', 'text/html');
|
||||
}
|
||||
|
||||
this.end(str ? str: '');
|
||||
};
|
||||
|
||||
ResponseStream.prototype.text = function (str) {
|
||||
if (!this.response.writable) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof str === 'number') {
|
||||
this.response.statusCode = str;
|
||||
str = arguments[1];
|
||||
}
|
||||
|
||||
this.modified = true;
|
||||
|
||||
if (!this.response._header && this.response.getHeader('content-type') !== 'text/plain') {
|
||||
this.response.setHeader('content-type', 'text/plain');
|
||||
}
|
||||
|
||||
this.end(str ? str: '');
|
||||
};
|
||||
|
||||
ResponseStream.prototype.end = function (data) {
|
||||
if (data && this.writable) {
|
||||
this.emit('data', data);
|
||||
}
|
||||
|
||||
this.modified = true;
|
||||
this.emit('end');
|
||||
};
|
||||
|
||||
ResponseStream.prototype.pipe = function () {
|
||||
var self = this,
|
||||
dest;
|
||||
|
||||
self.dest = dest = HttpStream.prototype.pipe.apply(self, arguments);
|
||||
|
||||
dest.on('drain', function() {
|
||||
self.emit('drain')
|
||||
})
|
||||
return dest;
|
||||
};
|
||||
|
||||
ResponseStream.prototype.write = function (data) {
|
||||
this.modified = true;
|
||||
|
||||
if (this.writable) {
|
||||
return this.dest.write(data);
|
||||
}
|
||||
};
|
||||
|
||||
ResponseStream.prototype.redirect = function (path, status) {
|
||||
var url = '';
|
||||
|
||||
if (~path.indexOf('://')) {
|
||||
url = path;
|
||||
} else {
|
||||
url += this.req.connection.encrypted ? 'https://' : 'http://';
|
||||
url += this.req.headers.host;
|
||||
url += (path[0] === '/') ? path : '/' + path;
|
||||
}
|
||||
|
||||
this.res.writeHead(status || 302, { 'Location': url });
|
||||
this.end();
|
||||
};
|
126
node_modules/union/lib/routing-stream.js
generated
vendored
Normal file
126
node_modules/union/lib/routing-stream.js
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* routing-stream.js: A Stream focused on connecting an arbitrary RequestStream and
|
||||
* ResponseStream through a given Router.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var util = require('util'),
|
||||
union = require('./index'),
|
||||
RequestStream = require('./request-stream'),
|
||||
ResponseStream = require('./response-stream');
|
||||
|
||||
//
|
||||
// ### function RoutingStream (options)
|
||||
//
|
||||
//
|
||||
var RoutingStream = module.exports = function (options) {
|
||||
options = options || {};
|
||||
RequestStream.call(this, options);
|
||||
|
||||
this.before = options.before || [];
|
||||
this.after = options.after || [];
|
||||
this.response = options.response || options.res;
|
||||
this.headers = options.headers || {
|
||||
'x-powered-by': 'union ' + union.version
|
||||
};
|
||||
|
||||
this.target = new ResponseStream({
|
||||
response: this.response,
|
||||
headers: this.headers
|
||||
});
|
||||
|
||||
this.once('pipe', this.route);
|
||||
};
|
||||
|
||||
util.inherits(RoutingStream, RequestStream);
|
||||
|
||||
//
|
||||
// Called when this instance is piped to **by another stream**
|
||||
//
|
||||
RoutingStream.prototype.route = function (req) {
|
||||
//
|
||||
// When a `RoutingStream` is piped to:
|
||||
//
|
||||
// 1. Setup the pipe-chain between the `after` middleware, the abstract response
|
||||
// and the concrete response.
|
||||
// 2. Attempt to dispatch to the `before` middleware, which represent things such as
|
||||
// favicon, static files, application routing.
|
||||
// 3. If no match is found then pipe to the 404Stream
|
||||
//
|
||||
var self = this,
|
||||
after,
|
||||
error,
|
||||
i;
|
||||
|
||||
//
|
||||
// Don't allow `this.target` to be writable on HEAD requests
|
||||
//
|
||||
this.target.writable = req.method !== 'HEAD';
|
||||
|
||||
//
|
||||
// 1. Setup the pipe-chain between the `after` middleware, the abstract response
|
||||
// and the concrete response.
|
||||
//
|
||||
after = [this.target].concat(this.after, this.response);
|
||||
for (i = 0; i < after.length - 1; i++) {
|
||||
//
|
||||
// attach req and res to all streams
|
||||
//
|
||||
after[i].req = req;
|
||||
after[i + 1].req = req;
|
||||
after[i].res = this.response;
|
||||
after[i + 1].res = this.response;
|
||||
after[i].pipe(after[i + 1]);
|
||||
|
||||
//
|
||||
// prevent multiple responses and memory leaks
|
||||
//
|
||||
after[i].on('error', this.onError);
|
||||
}
|
||||
|
||||
//
|
||||
// Helper function for dispatching to the 404 stream.
|
||||
//
|
||||
function notFound() {
|
||||
error = new Error('Not found');
|
||||
error.status = 404;
|
||||
self.onError(error);
|
||||
}
|
||||
|
||||
//
|
||||
// 2. Attempt to dispatch to the `before` middleware, which represent things such as
|
||||
// favicon, static files, application routing.
|
||||
//
|
||||
(function dispatch(i) {
|
||||
if (self.target.modified) {
|
||||
return;
|
||||
}
|
||||
else if (++i === self.before.length) {
|
||||
//
|
||||
// 3. If no match is found then pipe to the 404Stream
|
||||
//
|
||||
return notFound();
|
||||
}
|
||||
|
||||
self.target.once('next', dispatch.bind(null, i));
|
||||
if (self.before[i].length === 3) {
|
||||
self.before[i](self, self.target, function (err) {
|
||||
if (err) {
|
||||
self.onError(err);
|
||||
} else {
|
||||
self.target.emit('next');
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
self.before[i](self, self.target);
|
||||
}
|
||||
})(-1);
|
||||
};
|
||||
|
||||
RoutingStream.prototype.onError = function (err) {
|
||||
this.emit('error', err);
|
||||
};
|
30
node_modules/union/package.json
generated
vendored
Normal file
30
node_modules/union/package.json
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "union",
|
||||
"description": "A hybrid buffered / streaming middleware kernel backwards compatible with connect.",
|
||||
"version": "0.5.0",
|
||||
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
|
||||
"maintainers": [
|
||||
"dscape <nuno@nodejitsu.com>"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/flatiron/union.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"qs": "^6.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ecstatic": "0.5.x",
|
||||
"director": "1.x.x",
|
||||
"request": "2.29.x",
|
||||
"vows": "0.8.0",
|
||||
"connect": "2.22.x"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "vows test/*-test.js --spec -i"
|
||||
},
|
||||
"main": "./lib",
|
||||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
}
|
||||
}
|
37
node_modules/union/test/after-test.js
generated
vendored
Normal file
37
node_modules/union/test/after-test.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
var assert = require('assert'),
|
||||
vows = require('vows'),
|
||||
request = require('request'),
|
||||
union = require('../');
|
||||
|
||||
function stream_callback(cb) {
|
||||
return function () {
|
||||
var stream = new union.ResponseStream();
|
||||
|
||||
stream.once("pipe", function (req) {
|
||||
return cb ? cb(null,req) : undefined;
|
||||
});
|
||||
|
||||
return stream;
|
||||
};
|
||||
}
|
||||
|
||||
vows.describe('union/after').addBatch({
|
||||
'When using `union`': {
|
||||
'a union server with after middleware': {
|
||||
topic: function () {
|
||||
var self = this;
|
||||
|
||||
union.createServer({
|
||||
after: [ stream_callback(), stream_callback(self.callback) ]
|
||||
}).listen(9000, function () {
|
||||
request.get('http://localhost:9000');
|
||||
});
|
||||
},
|
||||
'should preserve the request until the last call': function (req) {
|
||||
assert.equal(req.req.httpVersion, '1.1');
|
||||
assert.equal(req.req.url, '/');
|
||||
assert.equal(req.req.method, 'GET');
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
50
node_modules/union/test/body-parser-test.js
generated
vendored
Normal file
50
node_modules/union/test/body-parser-test.js
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* simple-test.js: Simple tests for basic streaming and non-streaming HTTP requests with union.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var assert = require('assert'),
|
||||
connect = require('connect'),
|
||||
request = require('request'),
|
||||
vows = require('vows'),
|
||||
union = require('../');
|
||||
|
||||
vows.describe('union/body-parser').addBatch({
|
||||
"When using union with connect body parsing via urlencoded() or json()": {
|
||||
topic: function () {
|
||||
union.createServer({
|
||||
buffer: false,
|
||||
before: [
|
||||
connect.urlencoded(),
|
||||
connect.json(),
|
||||
function (req, res) {
|
||||
res.end(JSON.stringify(req.body, true, 2));
|
||||
}
|
||||
]
|
||||
}).listen(8082, this.callback);
|
||||
},
|
||||
"a request to /": {
|
||||
topic: function () {
|
||||
request.post({
|
||||
uri: 'http://localhost:8082/',
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ a: "foo", b: "bar" })
|
||||
}, this.callback);
|
||||
},
|
||||
"should respond with a body-decoded object": function (err, res, body) {
|
||||
assert.isNull(err);
|
||||
assert.equal(res.statusCode, 200);
|
||||
assert.deepEqual(
|
||||
JSON.parse(body),
|
||||
{ a: 'foo', b: 'bar' }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
|
62
node_modules/union/test/double-write-test.js
generated
vendored
Normal file
62
node_modules/union/test/double-write-test.js
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* simple-test.js: Simple tests for basic streaming and non-streaming HTTP requests with union.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var assert = require('assert'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
request = require('request'),
|
||||
vows = require('vows'),
|
||||
union = require('../lib/index'),
|
||||
macros = require('./helpers/macros');
|
||||
|
||||
var doubleWrite = false,
|
||||
server;
|
||||
|
||||
server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
res.json(200, { 'hello': 'world' });
|
||||
res.emit('next');
|
||||
},
|
||||
function (req, res) {
|
||||
doubleWrite = true;
|
||||
res.json(200, { 'hello': 'world' });
|
||||
res.emit('next');
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
|
||||
vows.describe('union/double-write').addBatch({
|
||||
"When using union": {
|
||||
"an http server which attempts to write to the response twice": {
|
||||
topic: function () {
|
||||
server.listen(9091, this.callback);
|
||||
},
|
||||
"a GET request to `/foo`": {
|
||||
topic: function () {
|
||||
request({ uri: 'http://localhost:9091/foo' }, this.callback);
|
||||
},
|
||||
"it should respond with `{ 'hello': 'world' }`": function (err, res, body) {
|
||||
macros.assertValidResponse(err, res);
|
||||
assert.deepEqual(JSON.parse(body), { 'hello': 'world' });
|
||||
},
|
||||
"it should not write to the response twice": function () {
|
||||
assert.isFalse(doubleWrite);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).addBatch({
|
||||
"When the tests are over": {
|
||||
"the server should close": function () {
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
|
44
node_modules/union/test/ecstatic-test.js
generated
vendored
Normal file
44
node_modules/union/test/ecstatic-test.js
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* simple-test.js: Simple tests for basic streaming and non-streaming HTTP requests with union.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var assert = require('assert'),
|
||||
ecstatic = require('ecstatic')(__dirname + '/fixtures/static'),
|
||||
request = require('request'),
|
||||
vows = require('vows'),
|
||||
union = require('../');
|
||||
|
||||
vows.describe('union/ecstatic').addBatch({
|
||||
"When using union with ecstatic": {
|
||||
topic: function () {
|
||||
union.createServer({
|
||||
before: [
|
||||
ecstatic
|
||||
]
|
||||
}).listen(18082, this.callback);
|
||||
},
|
||||
"a request to /some-file.txt": {
|
||||
topic: function () {
|
||||
request({ uri: 'http://localhost:18082/some-file.txt' }, this.callback);
|
||||
},
|
||||
"should respond with `hello world`": function (err, res, body) {
|
||||
assert.isNull(err);
|
||||
assert.equal(body, 'hello world\n');
|
||||
}
|
||||
},
|
||||
"a request to /404.txt (which does not exist)": {
|
||||
topic: function () {
|
||||
request({ uri: 'http://localhost:18082/404.txt' }, this.callback);
|
||||
},
|
||||
"should respond with 404 status code": function (err, res, body) {
|
||||
assert.isNull(err);
|
||||
assert.equal(res.statusCode, 404);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
|
0
node_modules/union/test/fixtures/index.js
generated
vendored
Normal file
0
node_modules/union/test/fixtures/index.js
generated
vendored
Normal file
1
node_modules/union/test/fixtures/static/some-file.txt
generated
vendored
Normal file
1
node_modules/union/test/fixtures/static/some-file.txt
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
hello world
|
36
node_modules/union/test/header-test.js
generated
vendored
Normal file
36
node_modules/union/test/header-test.js
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
// var assert = require('assert'),
|
||||
// request = require('request'),
|
||||
// vows = require('vows'),
|
||||
// union = require('../');
|
||||
|
||||
// vows.describe('union/header').addBatch({
|
||||
// 'When using `union`': {
|
||||
// 'with a server that responds with a header': {
|
||||
// topic: function () {
|
||||
// var callback = this.callback;
|
||||
// var server = union.createServer({
|
||||
// before: [
|
||||
// function (req, res) {
|
||||
// res.on('header', function () {
|
||||
// callback(null, res);
|
||||
// });
|
||||
// res.writeHead(200, { 'content-type': 'text' });
|
||||
// res.end();
|
||||
// }
|
||||
// ]
|
||||
// });
|
||||
// server.listen(9092, function () {
|
||||
// request('http://localhost:9092/');
|
||||
// });
|
||||
// },
|
||||
// 'it should have proper `headerSent` set': function (err, res) {
|
||||
// assert.isNull(err);
|
||||
// assert.isTrue(res.headerSent);
|
||||
// },
|
||||
// 'it should have proper `_emittedHeader` set': function (err, res) {
|
||||
// assert.isNull(err);
|
||||
// assert.isTrue(res._emittedHeader);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }).export(module);
|
0
node_modules/union/test/helpers/index.js
generated
vendored
Normal file
0
node_modules/union/test/helpers/index.js
generated
vendored
Normal file
17
node_modules/union/test/helpers/macros.js
generated
vendored
Normal file
17
node_modules/union/test/helpers/macros.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* macros.js: Simple test macros
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var assert = require('assert');
|
||||
|
||||
var macros = exports;
|
||||
|
||||
macros.assertValidResponse = function (err, res) {
|
||||
assert.isTrue(!err);
|
||||
assert.equal(res.statusCode, 200);
|
||||
};
|
||||
|
45
node_modules/union/test/prop-test.js
generated
vendored
Normal file
45
node_modules/union/test/prop-test.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
var assert = require('assert'),
|
||||
request = require('request'),
|
||||
vows = require('vows'),
|
||||
union = require('../');
|
||||
|
||||
vows.describe('union/properties').addBatch({
|
||||
'When using `union`': {
|
||||
'with a server that responds to requests': {
|
||||
topic: function () {
|
||||
var callback = this.callback;
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
callback(null, req, res);
|
||||
|
||||
res.writeHead(200, { 'content-type': 'text' });
|
||||
res.end();
|
||||
}
|
||||
]
|
||||
});
|
||||
server.listen(9092, function () {
|
||||
request('http://localhost:9092/');
|
||||
});
|
||||
},
|
||||
'the `req` should have a proper `httpVersion` set': function (err, req) {
|
||||
assert.isNull(err);
|
||||
assert.equal(req.httpVersion, '1.1');
|
||||
},
|
||||
'the `req` should have a proper `httpVersionMajor` set': function (err, req) {
|
||||
assert.isNull(err);
|
||||
assert.equal(req.httpVersionMajor, 1);
|
||||
},
|
||||
'the `req` should have a proper `httpVersionMinor` set': function (err, req) {
|
||||
assert.isNull(err);
|
||||
assert.equal(req.httpVersionMinor, 1);
|
||||
},
|
||||
'the `req` should have proper `socket` reference set': function (err, req) {
|
||||
var net = require('net');
|
||||
|
||||
assert.isNull(err);
|
||||
assert.isTrue(req.socket instanceof net.Socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
97
node_modules/union/test/simple-test.js
generated
vendored
Normal file
97
node_modules/union/test/simple-test.js
generated
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* simple-test.js: Simple tests for basic streaming and non-streaming HTTP requests with union.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins & the Contributors
|
||||
* MIT LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
var assert = require('assert'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
spawn = require('child_process').spawn,
|
||||
request = require('request'),
|
||||
vows = require('vows'),
|
||||
macros = require('./helpers/macros');
|
||||
|
||||
var examplesDir = path.join(__dirname, '..', 'examples', 'simple'),
|
||||
simpleScript = path.join(examplesDir, 'simple.js'),
|
||||
pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8')),
|
||||
fooURI = 'http://localhost:9090/foo',
|
||||
server;
|
||||
|
||||
vows.describe('union/simple').addBatch({
|
||||
"When using union": {
|
||||
"a simple http server": {
|
||||
topic: function () {
|
||||
server = spawn(process.argv[0], [simpleScript]);
|
||||
server.stdout.on('data', this.callback.bind(this, null));
|
||||
},
|
||||
"a GET request to `/foo`": {
|
||||
topic: function () {
|
||||
request({ uri: fooURI }, this.callback);
|
||||
},
|
||||
"it should respond with `hello world`": function (err, res, body) {
|
||||
macros.assertValidResponse(err, res);
|
||||
assert.equal(body, 'hello world\n');
|
||||
},
|
||||
"it should respond with 'x-powered-by': 'union <version>'": function (err, res, body) {
|
||||
assert.isNull(err);
|
||||
assert.equal(res.headers['x-powered-by'], 'union ' + pkg.version);
|
||||
}
|
||||
},
|
||||
"a POST request to `/foo`": {
|
||||
topic: function () {
|
||||
request.post({ uri: fooURI }, this.callback);
|
||||
},
|
||||
"it should respond with `wrote to a stream!`": function (err, res, body) {
|
||||
macros.assertValidResponse(err, res);
|
||||
assert.equal(body, 'wrote to a stream!');
|
||||
}
|
||||
},
|
||||
"a GET request to `/redirect`": {
|
||||
topic: function () {
|
||||
request.get({
|
||||
url: 'http://localhost:9090/redirect',
|
||||
followRedirect: false
|
||||
}, this.callback);
|
||||
},
|
||||
"it should redirect to `http://www.google.com`": function (err, res, body) {
|
||||
assert.equal(res.statusCode, 302);
|
||||
assert.equal(res.headers.location, "http://www.google.com");
|
||||
}
|
||||
},
|
||||
"a GET request to `/custom_redirect`": {
|
||||
topic: function () {
|
||||
request.get({
|
||||
url: 'http://localhost:9090/custom_redirect',
|
||||
followRedirect: false
|
||||
}, this.callback);
|
||||
},
|
||||
"it should redirect to `/foo`": function (err, res, body) {
|
||||
assert.equal(res.statusCode, 301);
|
||||
assert.equal(res.headers.location, "http://localhost:9090/foo");
|
||||
}
|
||||
},
|
||||
"a GET request to `/async`": {
|
||||
topic: function () {
|
||||
request.get({
|
||||
url: 'http://localhost:9090/async',
|
||||
timeout: 500
|
||||
}, this.callback);
|
||||
},
|
||||
"it should not timeout": function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 200);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).addBatch({
|
||||
"When the tests are over": {
|
||||
"the server should close": function () {
|
||||
server.kill();
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
|
31
node_modules/union/test/status-code-test.js
generated
vendored
Normal file
31
node_modules/union/test/status-code-test.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
var assert = require('assert'),
|
||||
request = require('request'),
|
||||
vows = require('vows'),
|
||||
union = require('../');
|
||||
|
||||
vows.describe('union/status-code').addBatch({
|
||||
'When using `union`': {
|
||||
'with a server setting `res.statusCode`': {
|
||||
topic: function () {
|
||||
var server = union.createServer({
|
||||
before: [
|
||||
function (req, res) {
|
||||
res.statusCode = 404;
|
||||
res.end();
|
||||
}
|
||||
]
|
||||
});
|
||||
server.listen(9091, this.callback);
|
||||
},
|
||||
'and sending a request': {
|
||||
topic: function () {
|
||||
request('http://localhost:9091/', this.callback);
|
||||
},
|
||||
'it should have proper `statusCode` set': function (err, res, body) {
|
||||
assert.isTrue(!err);
|
||||
assert.equal(res.statusCode, 404);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
68
node_modules/union/test/streaming-test.js
generated
vendored
Normal file
68
node_modules/union/test/streaming-test.js
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
var assert = require('assert'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
request = require('request'),
|
||||
vows = require('vows'),
|
||||
union = require('../');
|
||||
|
||||
vows.describe('union/streaming').addBatch({
|
||||
'When using `union`': {
|
||||
'a simple union server': {
|
||||
topic: function () {
|
||||
var self = this;
|
||||
|
||||
union.createServer({
|
||||
buffer: false,
|
||||
before: [
|
||||
function (req, res, next) {
|
||||
var chunks = '';
|
||||
|
||||
req.on('data', function (chunk) {
|
||||
chunks += chunk;
|
||||
});
|
||||
|
||||
req.on('end', function () {
|
||||
self.callback(null, chunks);
|
||||
});
|
||||
}
|
||||
]
|
||||
}).listen(9000, function () {
|
||||
request.post('http://localhost:9000').write('hello world');
|
||||
});
|
||||
},
|
||||
'should receive complete POST data': function (chunks) {
|
||||
assert.equal(chunks, 'hello world');
|
||||
}
|
||||
},
|
||||
"a simple pipe to a file": {
|
||||
topic: function () {
|
||||
var self = this;
|
||||
|
||||
union.createServer({
|
||||
before: [
|
||||
function (req, res, next) {
|
||||
var filename = path.join(__dirname, 'fixtures', 'pipe-write-test.txt'),
|
||||
writeStream = fs.createWriteStream(filename);
|
||||
|
||||
req.pipe(writeStream);
|
||||
writeStream.on('close', function () {
|
||||
res.writeHead(200);
|
||||
fs.createReadStream(filename).pipe(res);
|
||||
});
|
||||
}
|
||||
]
|
||||
}).listen(9044, function () {
|
||||
request({
|
||||
method: 'POST',
|
||||
uri: 'http://localhost:9044',
|
||||
body: 'hello world'
|
||||
}, self.callback);
|
||||
});
|
||||
},
|
||||
'should receive complete POST data': function (err, res, body) {
|
||||
assert.equal(body, 'hello world');
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
|
BIN
node_modules/union/union.png
generated
vendored
Normal file
BIN
node_modules/union/union.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
Reference in New Issue
Block a user