Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[minor] Fix whitespace

  • Loading branch information...
commit 908ff5735ef58118ab76c99adc38e2b724afed5b 1 parent 1884602
Pavan Kumar Sunkara pksunkara authored
28 lib/buffered-stream.js
@@ -2,33 +2,33 @@
2 2 * buffered-stream.js: A simple(r) Stream which is partially buffered into memory.
3 3 *
4 4 * (C) 2010, Mikeal Rogers
5   - *
  5 + *
6 6 * Adapted for Flatiron
7 7 * (C) 2011, Nodejitsu Inc.
8 8 * MIT LICENSE
9 9 *
10 10 */
11   -
  11 +
12 12 var events = require('events'),
13   - fs = require('fs'),
14   - stream = require('stream'),
  13 + fs = require('fs'),
  14 + stream = require('stream'),
15 15 util = require('util');
16 16
17 17 //
18 18 // ### function BufferedStream (limit)
19 19 // #### @limit {number} **Optional** Size of the buffer to limit
20   -// Constructor function for the BufferedStream object responsible for
21   -// maintaining a stream interface which can also persist to memory
  20 +// Constructor function for the BufferedStream object responsible for
  21 +// maintaining a stream interface which can also persist to memory
22 22 // temporarily.
23 23 //
24 24
25 25 var BufferedStream = module.exports = function (limit) {
26 26 events.EventEmitter.call(this);
27   -
  27 +
28 28 if (typeof limit === 'undefined') {
29 29 limit = Infinity;
30 30 }
31   -
  31 +
32 32 this.limit = limit;
33 33 this.size = 0;
34 34 this.chunks = [];
@@ -43,9 +43,9 @@ BufferedStream.prototype.pipe = function () {
43 43 if (self.resume) {
44 44 self.resume();
45 45 }
46   -
  46 +
47 47 var dest = stream.Stream.prototype.pipe.apply(self, arguments);
48   -
  48 +
49 49 //
50 50 // just incase you are piping to two streams, do not emit data twice.
51 51 // note: you can pipe twice, but you need to pipe both streams in the same tick.
@@ -54,7 +54,7 @@ BufferedStream.prototype.pipe = function () {
54 54 if (this.piped) {
55 55 return dest;
56 56 }
57   -
  57 +
58 58 process.nextTick(function () {
59 59 self.chunks.forEach(function (c) { self.emit('data', c) })
60 60 self.size = 0;
@@ -66,10 +66,10 @@ BufferedStream.prototype.pipe = function () {
66 66 }
67 67 else if (self.closed) {
68 68 self.emit('close');
69   - }
  69 + }
70 70 }
71 71 });
72   -
  72 +
73 73 this.piped = true;
74 74
75 75 return dest;
@@ -80,7 +80,7 @@ BufferedStream.prototype.write = function (chunk) {
80 80 this.emit('data', chunk);
81 81 return;
82 82 }
83   -
  83 +
84 84 this.chunks.push(chunk);
85 85 this.size += chunk.length;
86 86 if (this.limit < this.size) {
30 lib/core.js
... ... @@ -1,5 +1,5 @@
1 1 /*
2   - * core.js: Core functionality for the Flatiron HTTP plugin.
  2 + * core.js: Core functionality for the Flatiron HTTP plugin.
3 3 *
4 4 * (C) 2011, Nodejitsu Inc.
5 5 * MIT LICENSE
@@ -12,30 +12,30 @@ var http = require('http'),
12 12 stream = require('stream'),
13 13 HttpStream = require('./http-stream'),
14 14 RoutingStream = require('./routing-stream');
15   -
  15 +
16 16 var core = exports;
17 17
18 18 core.createServer = function (options) {
19 19 var isArray = Array.isArray(options.after);
20   -
  20 +
21 21 if (!options) {
22 22 throw new Error('options is required to create a server');
23 23 }
24   -
  24 +
25 25 function requestHandler(req, res) {
26 26 var routingStream = new RoutingStream({
27 27 before: options.before,
28 28 // without new after is a huge memory leak that pipes to every
29 29 // single open connection
30   - after: isArray &&
31   - options.after.map(function (a) {
32   - return new a;
  30 + after: isArray &&
  31 + options.after.map(function (a) {
  32 + return new a;
33 33 }),
34 34 response: res,
35 35 limit: options.limit,
36 36 headers: options.headers
37 37 });
38   -
  38 +
39 39 routingStream.on('error', function (err) {
40 40 var fn = options.onError || core.errorHandler;
41 41 fn(err, routingStream, routingStream.target, function () {
@@ -44,24 +44,24 @@ core.createServer = function (options) {
44 44 });
45 45 req.pipe(routingStream);
46 46 }
47   -
  47 +
48 48 if (options.https) {
49 49 if (!options.https.key || !options.https.cert) {
50 50 throw new Error('Both `options.https.key` and `options.https.cert` are required.');
51 51 }
52   -
  52 +
53 53 var credentials = {
54 54 key: fs.readFileSync(options.https.key),
55 55 cert: fs.readFileSync(options.https.cert)
56 56 };
57   -
  57 +
58 58 if (options.https.ca) {
59 59 credentials.ca = fs.readFileSync(options.https.ca);
60 60 }
61   -
  61 +
62 62 return https.createServer(credentials, requestHandler);
63   - }
64   -
  63 + }
  64 +
65 65 return http.createServer(requestHandler);
66 66 };
67 67
@@ -71,7 +71,7 @@ core.errorHandler = function error(err, req, res) {
71 71 (this.res || res).end(err.stack + "\n");
72 72 return;
73 73 }
74   -
  74 +
75 75 (this.res || res).writeHead(404, {"Content-Type": "text/plain"});
76 76 (this.res || res).end("Not Found\n");
77 77 };
6 lib/http-stream.js
@@ -5,16 +5,16 @@
5 5 * MIT LICENSE
6 6 *
7 7 */
8   -
  8 +
9 9 var url = require('url'),
10 10 util = require('util'),
11 11 qs = require('qs'),
12 12 BufferedStream = require('./buffered-stream');
13   -
  13 +
14 14 var HttpStream = module.exports = function (options) {
15 15 options = options || {};
16 16 BufferedStream.call(this, options.limit);
17   -
  17 +
18 18 this.on('pipe', this.pipeState);
19 19 };
20 20
2  lib/index.js
@@ -5,7 +5,7 @@
5 5 * MIT LICENSE
6 6 *
7 7 */
8   -
  8 +
9 9 var union = exports;
10 10
11 11 //
6 lib/request-stream.js
@@ -5,16 +5,16 @@
5 5 * MIT LICENSE
6 6 *
7 7 */
8   -
  8 +
9 9 var url = require('url'),
10 10 util = require('util'),
11 11 qs = require('qs'),
12 12 HttpStream = require('./http-stream');
13   -
  13 +
14 14 var RequestStream = module.exports = function (options) {
15 15 options = options || {};
16 16 HttpStream.call(this, options);
17   -
  17 +
18 18 this.on('pipe', this.pipeRequest);
19 19 };
20 20
30 lib/response-stream.js
... ... @@ -1,25 +1,25 @@
1 1 /*
2   - * response-stream.js: A Stream focused on writing any relevant information to
  2 + * response-stream.js: A Stream focused on writing any relevant information to
3 3 * a raw http.ServerResponse object.
4 4 *
5 5 * (C) 2011, Nodejitsu Inc.
6 6 * MIT LICENSE
7 7 *
8 8 */
9   -
  9 +
10 10 var util = require('util'),
11 11 HttpStream = require('./http-stream');
12 12
13 13 //
14   -// ### function RequestStream (options)
15   -//
16   -//
  14 +// ### function RequestStream (options)
  15 +//
  16 +//
17 17 var ResponseStream = module.exports = function (options) {
18 18 var self = this;
19 19
20 20 options = options || {};
21 21 HttpStream.call(this, options);
22   -
  22 +
23 23 this.writeable = true;
24 24 this.response = options.response;
25 25
@@ -71,12 +71,12 @@ ResponseStream.prototype.writeHead = function (statusCode, headers) {
71 71 this.response.setHeader(key, headers[key]);
72 72 }
73 73 }
74   -
  74 +
75 75 this.response.statusCode = statusCode;
76 76 };
77 77
78 78 //
79   -// Create pass-thru for the necessary
  79 +// Create pass-thru for the necessary
80 80 // `http.ServerResponse` methods.
81 81 //
82 82 ['setHeader', 'getHeader', 'removeHeader', '_implicitHeader', 'addTrailers'].forEach(function (method) {
@@ -89,18 +89,18 @@ ResponseStream.prototype.json = function (obj) {
89 89 if (!this.response.writable) {
90 90 return;
91 91 }
92   -
  92 +
93 93 if (typeof obj === 'number') {
94 94 this.response.statusCode = obj;
95 95 obj = arguments[1];
96   - }
97   -
  96 + }
  97 +
98 98 this.modified = true;
99   -
  99 +
100 100 if (!this.response._header && this.response.getHeader('content-type') !== 'application/json') {
101 101 this.response.setHeader('content-type', 'application/json');
102 102 }
103   -
  103 +
104 104 this.end(obj ? JSON.stringify(obj) : '');
105 105 };
106 106
@@ -146,14 +146,14 @@ ResponseStream.prototype.end = function (data) {
146 146 if (data && this.writable) {
147 147 this.emit('data', data);
148 148 }
149   -
  149 +
150 150 this.modified = true;
151 151 this.emit('end');
152 152 };
153 153
154 154 ResponseStream.prototype.write = function (data) {
155 155 this.modified = true;
156   -
  156 +
157 157 if (this.writable) {
158 158 this.emit('data', data);
159 159 }
32 lib/routing-stream.js
@@ -6,20 +6,20 @@
6 6 * MIT LICENSE
7 7 *
8 8 */
9   -
  9 +
10 10 var util = require('util'),
11 11 union = require('./index'),
12 12 RequestStream = require('./request-stream'),
13 13 ResponseStream = require('./response-stream');
14 14
15 15 //
16   -// ### function RequestStream (options)
17   -//
18   -//
  16 +// ### function RequestStream (options)
  17 +//
  18 +//
19 19 var RoutingStream = module.exports = function (options) {
20 20 options = options || {};
21 21 RequestStream.call(this, options);
22   -
  22 +
23 23 this.before = options.before || [];
24 24 this.after = options.after || [];
25 25 this.response = options.response || options.res;
@@ -46,9 +46,9 @@ RoutingStream.prototype.route = function (req) {
46 46 // When a `RoutingStream` is piped to:
47 47 //
48 48 // 1. Setup the pipe-chain between the `after` middleware, the abstract response
49   - // and the concrete response.
50   - // 2. Attempt to dispatch to the `before` middleware, which represent things such as
51   - // favicon, static files, application routing.
  49 + // and the concrete response.
  50 + // 2. Attempt to dispatch to the `before` middleware, which represent things such as
  51 + // favicon, static files, application routing.
52 52 // 3. If no match is found then pipe to the 404Stream
53 53 //
54 54 var self = this,
@@ -57,8 +57,8 @@ RoutingStream.prototype.route = function (req) {
57 57 i;
58 58
59 59 // make sure data from req is piped on the `once` first call
60   - req.on('data', function () {
61   - self.emit.apply(self, ["data"].concat([].slice.call(arguments,0)));
  60 + req.on('data', function () {
  61 + self.emit.apply(self, ["data"].concat([].slice.call(arguments,0)));
62 62 });
63 63
64 64 //
@@ -68,7 +68,7 @@ RoutingStream.prototype.route = function (req) {
68 68
69 69 //
70 70 // 1. Setup the pipe-chain between the `after` middleware, the abstract response
71   - // and the concrete response.
  71 + // and the concrete response.
72 72 //
73 73 after = [this.target].concat(this.after, this.response);
74 74 for (i = 0; i < after.length - 1; i++) {
@@ -81,7 +81,7 @@ RoutingStream.prototype.route = function (req) {
81 81 // prevent multiple responses and memory leaks
82 82 after[i].on('error', this.onError);
83 83 }
84   -
  84 +
85 85 //
86 86 // Helper function for dispatching to the 404 stream.
87 87 //
@@ -90,10 +90,10 @@ RoutingStream.prototype.route = function (req) {
90 90 error.status = 404;
91 91 self.onError(error);
92 92 }
93   -
  93 +
94 94 //
95   - // 2. Attempt to dispatch to the `before` middleware, which represent things such as
96   - // favicon, static files, application routing.
  95 + // 2. Attempt to dispatch to the `before` middleware, which represent things such as
  96 + // favicon, static files, application routing.
97 97 //
98 98 (function dispatch(i) {
99 99 if (self.target.modified) {
@@ -119,7 +119,7 @@ RoutingStream.prototype.route = function (req) {
119 119 else {
120 120 self.before[i](self, self.target);
121 121 }
122   - })(-1);
  122 + })(-1);
123 123 };
124 124
125 125 RoutingStream.prototype.onError = function (err) {

0 comments on commit 908ff57

Please sign in to comment.
Something went wrong with that request. Please try again.