Skip to content

Commit

Permalink
Accepts empty data to render correctly a full text with new lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Loick Bonniot committed Jul 31, 2014
1 parent 54f7bf6 commit 36895e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ function EventSource(url, eventSourceInitDict) {
, value = buf.slice(pos, pos + valueLength);

if (field === 'data') {
if (value.length > 0) {
data += value + '\n';
}
data += value + '\n';
} else if (field === 'event') {
eventName = value;
} else if (field === 'id') {
Expand Down
20 changes: 14 additions & 6 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,24 @@ describe('Parser', function () {
});
});

it('causes entire event to be ignored for empty data fields', function (done) {
createServer(["data:\n\ndata: Hello\n\n"], function (port, close) {
it('does not ignore multilines strings', function (done) {
createServer(["data: line one\ndata:\ndata: line two\n\n"], function (port, close) {
var es = new EventSource('http://localhost:' + port);
var originalEmit = es.emit;
es.emit = function (event) {
assert.ok(event === 'message' || event === 'newListener');
return originalEmit.apply(this, arguments);
es.onmessage = function (m) {
assert.equal('line one\n\nline two', m.data);
es.close();
close(done);
};
});
});

it('does not ignore multilines strings even in data beginning', function (done) {
createServer(["data:\ndata:line one\ndata: line two\n\n"], function (port, close) {
var es = new EventSource('http://localhost:' + port);
var originalEmit = es.emit;
es.onmessage = function (m) {
assert.equal('Hello', m.data);
assert.equal('\nline one\nline two', m.data);
es.close();
close(done);
};
Expand Down

0 comments on commit 36895e6

Please sign in to comment.