faye 1.4.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,15 +18,15 @@ var constants = require('../util/constants'),
18
18
  StaticServer = require('./static_server');
19
19
 
20
20
  var NodeAdapter = Class({ className: 'NodeAdapter',
21
- URL_BASE: 'http://localhost',
22
- DEFAULT_ENDPOINT: '/bayeux',
23
- SCRIPT_PATH: 'faye-browser-min.js',
21
+ URL_BASE: 'http://localhost',
22
+ DEFAULT_ENDPOINT: '/bayeux',
23
+ SCRIPT_PATH: 'faye-browser-min.js',
24
+ DEFAULT_MAX_LENGTH: 0x3ffffff, // 64 MB
24
25
 
25
26
  VALID_JSONP_CALLBACK: /^[a-z_\$][a-z0-9_\$]*(\.[a-z_\$][a-z0-9_\$]*)*$/i,
26
27
 
27
28
  initialize: function(options) {
28
- this._options = options || {};
29
- validateOptions(this._options, ['engine', 'mount', 'ping', 'timeout', 'extensions', 'websocketExtensions']);
29
+ this._options = validateOptions(options, ['engine', 'mount', 'maxLength', 'ping', 'timeout', 'extensions', 'websocketExtensions']);
30
30
 
31
31
  this._extensions = [];
32
32
  this._endpoint = this._options.mount || this.DEFAULT_ENDPOINT;
@@ -97,12 +97,16 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
97
97
  },
98
98
 
99
99
  check: function(request) {
100
- var path = URI.parse(request.url, this.URL_BASE).pathname;
101
- return !!this._endpointRe.test(path);
100
+ try {
101
+ var path = URI.parse(this.URL_BASE + request.url).pathname;
102
+ return !!this._endpointRe.test(path);
103
+ } catch (error) {
104
+ return false;
105
+ }
102
106
  },
103
107
 
104
108
  handle: function(request, response) {
105
- var requestUrl = URI.parse(request.url, this.URL_BASE),
109
+ var requestUrl = URI.parse(this.URL_BASE + request.url),
106
110
  requestMethod = request.method,
107
111
  self = this;
108
112
 
@@ -129,7 +133,10 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
129
133
  }
130
134
 
131
135
  if (requestMethod === 'POST') {
132
- return this._concatStream(request, function(data) {
136
+ return this._concatStream(request, function(error, data) {
137
+ if (error)
138
+ return this._returnResponse(response, 413, contenttypes.TYPE_TEXT, error.message);
139
+
133
140
  var type = (request.headers['content-type'] || '').split(';')[0],
134
141
  params = (type === 'application/json')
135
142
  ? new URLSearchParams({ message: data })
@@ -181,8 +188,7 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
181
188
  headers['Content-Length'] = Buffer.from(body, 'utf8').length.toString();
182
189
 
183
190
  this.debug('HTTP response: ?', body);
184
- response.writeHead(200, headers);
185
- response.end(body);
191
+ this._returnResponse(response, 200, headers, body);
186
192
  }, this);
187
193
  } catch (error) {
188
194
  this._returnError(response, error);
@@ -193,8 +199,12 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
193
199
  return json.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
194
200
  },
195
201
 
202
+ _getMaxLength: function() {
203
+ return this._options.maxLength || this.DEFAULT_MAX_LENGTH;
204
+ },
205
+
196
206
  handleUpgrade: function(request, socket, head) {
197
- var options = { extensions: this._extensions, ping: this._options.ping },
207
+ var options = { maxLength: this._getMaxLength(), extensions: this._extensions, ping: this._options.ping },
198
208
  ws = new WebSocket(request, socket, head, [], options),
199
209
  clientId = null,
200
210
  self = this;
@@ -251,20 +261,34 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
251
261
  'Access-Control-Max-Age': '86400'
252
262
  };
253
263
 
254
- response.writeHead(200, headers);
255
- response.end('');
264
+ this._returnResponse(response, 200, headers, '');
256
265
  },
257
266
 
258
- _concatStream: function(stream, callback, context) {
259
- var chunks = [],
260
- length = 0;
267
+ _concatStream: function(request, callback, context) {
268
+ var chunks = [],
269
+ length = 0,
270
+ maxLength = this._getMaxLength(),
271
+ failed = false;
272
+
273
+ var contentLength = parseInt(request.headers['content-length'] || '0', 10);
274
+ if (contentLength > maxLength)
275
+ return callback.call(context, new Error('Content too large'));
276
+
277
+ request.on('data', function(chunk) {
278
+ if (failed) return;
261
279
 
262
- stream.on('data', function(chunk) {
263
280
  chunks.push(chunk);
264
281
  length += chunk.length;
282
+
283
+ if (length > maxLength) {
284
+ failed = true;
285
+ callback.call(context, new Error('Content too large'));
286
+ }
265
287
  });
266
288
 
267
- stream.on('end', function() {
289
+ request.on('end', function() {
290
+ if (failed) return;
291
+
268
292
  var buffer = Buffer.alloc(length),
269
293
  offset = 0;
270
294
 
@@ -272,7 +296,7 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
272
296
  chunk.copy(buffer, offset);
273
297
  offset += chunk.length;
274
298
  }
275
- callback.call(context, buffer.toString('utf8'));
299
+ callback.call(context, null, buffer.toString('utf8'));
276
300
  });
277
301
  },
278
302
 
@@ -294,15 +318,19 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
294
318
  return string;
295
319
  },
296
320
 
321
+ _returnResponse: function(response, status, headers, body) {
322
+ if (!response || response.writableEnded) return;
323
+
324
+ response.writeHead(status, headers);
325
+ response.end(body);
326
+ },
327
+
297
328
  _returnError: function(response, error) {
298
329
  var message = error.message;
299
330
  if (error.stack) message += '\nBacktrace:\n' + error.stack;
300
331
  this.error(message);
301
332
 
302
- if (!response) return;
303
-
304
- response.writeHead(400, contenttypes.TYPE_TEXT);
305
- response.end('Bad request');
333
+ this._returnResponse(response, 400, contenttypes.TYPE_TEXT, 'Bad request');
306
334
  }
307
335
  });
308
336
 
@@ -16,6 +16,10 @@ var StaticServer = Class({
16
16
  this._pathRegex = pathRegex;
17
17
  this._pathMap = new Map();
18
18
  this._index = new Map();
19
+
20
+ try {
21
+ this._fillCache();
22
+ } catch (error) {}
19
23
  },
20
24
 
21
25
  map: function(requestPath, filename) {
@@ -27,26 +31,19 @@ var StaticServer = Class({
27
31
  },
28
32
 
29
33
  call: function(request, response) {
30
- var pathname = URI.parse(request.url, this.URL_BASE).pathname,
34
+ var pathname = URI.parse(this.URL_BASE + request.url).pathname,
31
35
  filename = path.basename(pathname);
32
36
 
33
37
  filename = this._pathMap.get(filename) || filename;
34
- this._index.set(filename, this._index.get(filename) || {});
35
-
36
- var cache = this._index.get(filename),
37
- fullpath = path.join(this._directory, filename);
38
38
 
39
- try {
40
- cache.content = cache.content || fs.readFileSync(fullpath);
41
- cache.digest = cache.digest || crypto.createHash('sha1').update(cache.content).digest('hex');
42
- cache.mtime = cache.mtime || fs.statSync(fullpath).mtime;
43
- } catch (error) {
39
+ if (!this._index.has(filename)) {
44
40
  response.writeHead(404, {});
45
41
  return response.end();
46
42
  }
47
43
 
48
- var type = /\.js$/.test(pathname) ? 'TYPE_SCRIPT' : 'TYPE_JSON',
49
- ims = request.headers['if-modified-since'];
44
+ var cache = this._index.get(filename),
45
+ type = /\.js$/.test(filename) ? 'TYPE_SCRIPT' : 'TYPE_JSON',
46
+ ims = request.headers['if-modified-since'];
50
47
 
51
48
  var headers = {
52
49
  'ETag': cache.digest,
@@ -67,6 +64,23 @@ var StaticServer = Class({
67
64
  response.writeHead(200, headers);
68
65
  response.end(cache.content);
69
66
  }
67
+ },
68
+
69
+ _fillCache: function() {
70
+ var entries = fs.readdirSync(this._directory);
71
+
72
+ for (let entry of entries) {
73
+ var fullpath = path.join(this._directory, entry);
74
+ var content = fs.readFileSync(fullpath);
75
+ var digest = crypto.createHash('sha1').update(content).digest('hex');
76
+ var mtime = fs.statSync(fullpath).mtime;
77
+
78
+ this._index.set(entry, {
79
+ content: content,
80
+ digest: digest,
81
+ mtime: mtime
82
+ });
83
+ }
70
84
  }
71
85
  });
72
86
 
@@ -23,6 +23,7 @@ var Connection = Class({
23
23
  options = options || {};
24
24
  var timeout = (options.timeout !== undefined) ? options.timeout / 1000 : this._engine.timeout;
25
25
 
26
+ this.setDeferredStatus('succeeded', []);
26
27
  this.setDeferredStatus('unknown');
27
28
  this.callback(callback, context);
28
29
 
@@ -3,6 +3,7 @@
3
3
  module.exports = {
4
4
  then: function(callback, errback) {
5
5
  var self = this;
6
+ var noop = function() {};
6
7
 
7
8
  if (!this._promise) {
8
9
  this._promise = new Promise(function(resolve, reject) {
@@ -11,21 +12,20 @@ module.exports = {
11
12
  });
12
13
  }
13
14
 
14
- if (arguments.length === 0) {
15
- return this._promise;
16
- } else {
17
- return this._promise.then(callback, errback);
18
- }
15
+ return this._promise.then(
16
+ callback || noop,
17
+ errback || noop
18
+ );
19
19
  },
20
20
 
21
21
  callback: function(callback, context) {
22
22
  if (!callback) return;
23
- return this.then(function(value) { callback.call(context, value) });
23
+ this.then(function(value) { callback.call(context, value) });
24
24
  },
25
25
 
26
26
  errback: function(callback, context) {
27
27
  if (!callback) return;
28
- return this.then(null, function(reason) { callback.call(context, reason) });
28
+ this.then(null, function(reason) { callback.call(context, reason) });
29
29
  },
30
30
 
31
31
  timeout: function(seconds, message) {
@@ -32,9 +32,8 @@ var Client = Class({ className: 'Client',
32
32
 
33
33
  initialize: function(endpoint, options) {
34
34
  this.info('New client created for ?', endpoint);
35
- options = options || {};
36
35
 
37
- validateOptions(options, ['interval', 'timeout', 'endpoints', 'proxy', 'retry', 'scheduler', 'websocketExtensions', 'tls', 'ca']);
36
+ options = validateOptions(options, ['interval', 'timeout', 'endpoints', 'proxy', 'retry', 'scheduler', 'websocketExtensions', 'tls', 'ca']);
38
37
 
39
38
  this._channels = new Channel.Set();
40
39
  this._dispatcher = Dispatcher.create(this, endpoint || this.DEFAULT_ENDPOINT, options);
@@ -294,7 +293,7 @@ var Client = Class({ className: 'Client',
294
293
  // * id * error
295
294
  // * ext * ext
296
295
  publish: function(channel, data, options) {
297
- validateOptions(options || {}, ['attempts', 'deadline']);
296
+ options = validateOptions(options, ['attempts', 'deadline']);
298
297
  var publication = new Publication();
299
298
 
300
299
  this.connect(function() {
@@ -1,5 +1,5 @@
1
1
  module.exports = {
2
- VERSION: '1.4.2',
2
+ VERSION: '1.5.0',
3
3
 
4
4
  BAYEUX_VERSION: '1.0',
5
5
  ID_LENGTH: 160,
package/src/util/uri.js CHANGED
@@ -11,14 +11,9 @@ module.exports = {
11
11
  uri.port === location.port;
12
12
  },
13
13
 
14
- parse: function(url, base) {
14
+ parse: function(url) {
15
15
  if (typeof url !== 'string') return url;
16
-
17
- if (typeof location === 'undefined') {
18
- return new URL(url, base);
19
- } else {
20
- return new URL(url, base || location.href);
21
- }
16
+ return new URL(url);
22
17
  },
23
18
 
24
19
  stringify: function(uri) {
@@ -3,9 +3,13 @@
3
3
  var array = require('./array');
4
4
 
5
5
  module.exports = function(options, validKeys) {
6
+ options = options || {};
7
+
6
8
  for (let key of Object.keys(options)) {
7
9
  if (array.indexOf(validKeys, key) < 0) {
8
10
  throw new Error('Unrecognized option: ' + key);
9
11
  }
10
12
  }
13
+
14
+ return options;
11
15
  };