eventsource
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -0,1 +1,6 @@ | ||
# [Git master HEAD](https://github.com/aslakhellesoy/eventsource-node/compare/v0.1.3...master) | ||
* Bugfix: Added missing origin property. ([#39](https://github.com/aslakhellesoy/eventsource-node/pull/39), [#38](https://github.com/aslakhellesoy/eventsource-node/issues/38) Arnout Kazemier) | ||
* Expose `status` property on `error` events. ([#40](https://github.com/aslakhellesoy/eventsource-node/pull/40) Adriano Raiano) | ||
# [0.1.3](https://github.com/aslakhellesoy/eventsource-node/compare/v0.1.2...v0.1.3) | ||
@@ -2,0 +7,0 @@ |
@@ -1,5 +0,7 @@ | ||
var http = require('http') | ||
var original = require('original') | ||
, parse = require('url').parse | ||
, events = require('events') | ||
, https = require('https') | ||
, util = require('util') | ||
, events = require('events'); | ||
, http = require('http') | ||
, util = require('util'); | ||
@@ -71,3 +73,3 @@ function isPlainObject(obj) { | ||
var options = require('url').parse(url); | ||
var options = parse(url); | ||
var isSecure = options.protocol == 'https:'; | ||
@@ -90,3 +92,3 @@ options.headers = { 'Cache-Control': 'no-cache', 'Accept': 'text/event-stream' }; | ||
// Server sent redirect response without Location header. | ||
_emit('error', new Event('error')); | ||
_emit('error', new Event('error', {status: res.statusCode})); | ||
return; | ||
@@ -102,5 +104,5 @@ } | ||
if (res.statusCode == 403) { | ||
if (res.statusCode == 403 || res.statusCode == 401) { | ||
// 'Access denied' | ||
_emit('error', new Event('error')); | ||
_emit('error', new Event('error', {status: res.statusCode})); | ||
return self.close(); | ||
@@ -190,3 +192,4 @@ } | ||
data: data.slice(0, -1), // remove trailing newline | ||
lastEventId: lastEventId | ||
lastEventId: lastEventId, | ||
origin: original(url) | ||
})); | ||
@@ -290,4 +293,11 @@ data = ''; | ||
*/ | ||
function Event(type) { | ||
Object.defineProperty(this, 'type', { writable: false, value: type }); | ||
function Event(type, optionalProperties) { | ||
Object.defineProperty(this, 'type', { writable: false, value: type, enumerable: true }); | ||
if (optionalProperties) { | ||
for (var f in optionalProperties) { | ||
if (optionalProperties.hasOwnProperty(f)) { | ||
Object.defineProperty(this, f, { writable: false, value: optionalProperties[f], enumerable: true }); | ||
} | ||
} | ||
} | ||
} | ||
@@ -309,2 +319,1 @@ | ||
} | ||
{ | ||
"name": "eventsource", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "W3C compliant EventSource client for Node.js", | ||
@@ -13,15 +13,2 @@ "keywords": [ | ||
"author": "Aslak Hellesøy <aslak.hellesoy@gmail.com>", | ||
"contributors": [ | ||
"Aslak Hellesøy <aslak.hellesoy@gmail.com>", | ||
"Einar Otto Stangvik <einaros+gh@gmail.com>", | ||
"Dan North <tastapod@gmail.com>", | ||
"Scott Moak <scott.moak@mybrainoncode.com>", | ||
"William Wicks", | ||
"Devon Adkisson", | ||
"FrozenCow <frozencow@gmail.com>", | ||
"mbieser <29erpilot@gmail.com>", | ||
"qqueue <queue@hakase.org>", | ||
"Romain Gauthier", | ||
"Lesterpig <loick.bt@gmail.com>" | ||
], | ||
"repository": { | ||
@@ -48,7 +35,11 @@ "type": "git", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha --reporter spec" | ||
"test": "mocha --reporter spec", | ||
"postpublish": "git push && git push --tags" | ||
}, | ||
"engines": { | ||
"node": ">=0.6.0" | ||
}, | ||
"dependencies": { | ||
"original": "^0.0.5" | ||
} | ||
} |
@@ -58,1 +58,15 @@ # EventSource [![Build Status](https://secure.travis-ci.org/aslakhellesoy/eventsource-node.png)](http://travis-ci.org/aslakhellesoy/eventsource-node) [![Dependencies](https://david-dm.org/aslakhellesoy/eventsource-node.png)](https://david-dm.org/aslakhellesoy/eventsource-node) [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/aslakhellesoy/eventsource-node/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
Note that for Node.js < v0.10.x this option has no effect - unauthorized HTTPS requests are *always* allowed. | ||
### HTTP status code on error events | ||
Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the `status` property in the error event. | ||
``` | ||
es.onerror = function (err) { | ||
if (err) { | ||
if (err.status === 401 || err.status === 403) { | ||
console.log('not authorized'); | ||
} | ||
} | ||
}; | ||
``` |
@@ -362,4 +362,5 @@ var EventSource = require('../lib/eventsource') | ||
var es = new EventSource(url); | ||
es.onerror = function () { | ||
es.onerror = function (err) { | ||
assert.ok(true, 'got error'); | ||
assert.equal(err.status, 403); | ||
es.close(); | ||
@@ -375,2 +376,20 @@ close(done); | ||
it('causes error event when response is 401', function (done) { | ||
createServer(["id: 1\ndata: hello world\n\n"], | ||
function (port, close) { | ||
var url = 'http://localhost:' + port; | ||
var es = new EventSource(url); | ||
es.onerror = function (err) { | ||
assert.ok(true, 'got error'); | ||
assert.equal(err.status, 401); | ||
es.close(); | ||
close(done); | ||
}; | ||
}, | ||
function (req, res) { | ||
res.writeHead(401, {'Content-Type': 'text/html'}); | ||
res.end(); | ||
}); | ||
}); | ||
it('causes error event when response is 301 with missing location', function (done) { | ||
@@ -381,3 +400,4 @@ createServer([], | ||
var es = new EventSource(url); | ||
es.onerror = function () { | ||
es.onerror = function (err) { | ||
assert.equal(err.status, 301); | ||
es.close(); | ||
@@ -430,3 +450,4 @@ close(done); | ||
var es = new EventSource(url); | ||
es.onerror = function () { | ||
es.onerror = function (err) { | ||
assert.equal(err.status, 307); | ||
es.close(); | ||
@@ -837,2 +858,13 @@ close(done); | ||
it('supplies the correct origin', function (done) { | ||
createServer(["id: 123\ndata: sample_data\n\n"], function (port, close) { | ||
var es = new EventSource('http://localhost:' + port); | ||
es.onmessage = function (event) { | ||
assert.equal(event.origin, 'http://localhost:'+ port); | ||
es.close(); | ||
close(done); | ||
} | ||
}); | ||
}); | ||
it('emits open event when connection is established', function (done) { | ||
@@ -839,0 +871,0 @@ createServer([], function (port, close) { |
48209
12
1123
72
1
+ Addedoriginal@^0.0.5
+ Addedoriginal@0.0.5(transitive)
+ Addedurl-parse@0.0.4(transitive)