Skip to content

Commit

Permalink
Improve performance for large messages across many chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
trentmwillis committed Oct 9, 2019
1 parent 82d38b0 commit 64eefa3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/eventsource.js
Expand Up @@ -174,6 +174,8 @@ function EventSource (url, eventSourceInitDict) {
// Source/WebCore/page/EventSource.cpp
var isFirst = true
var buf
var startingPos = 0
var startingFieldLength = -1
res.on('data', function (chunk) {
buf = buf ? Buffer.concat([buf, chunk]) : chunk
if (isFirst && hasBom(buf)) {
Expand All @@ -193,10 +195,10 @@ function EventSource (url, eventSourceInitDict) {
}

var lineLength = -1
var fieldLength = -1
var fieldLength = startingFieldLength
var c

for (var i = pos; lineLength < 0 && i < length; ++i) {
for (var i = startingPos; lineLength < 0 && i < length; ++i) {
c = buf[i]
if (c === colon) {
if (fieldLength < 0) {
Expand All @@ -211,7 +213,12 @@ function EventSource (url, eventSourceInitDict) {
}

if (lineLength < 0) {
startingPos = length - pos
startingFieldLength = fieldLength
break
} else {
startingPos = 0
startingFieldLength = -1
}

parseEventStreamLine(buf, pos, fieldLength, lineLength)
Expand Down
18 changes: 18 additions & 0 deletions test/eventsource_test.js
Expand Up @@ -466,6 +466,24 @@ describe('Parser', function () {
}
})
})

it('parses a relatively huge message across many chunks efficiently', function (done) {
this.timeout(1000)

createServer(function (err, server) {
if (err) return done(err)

var longMessage = 'data: ' + new Array(100000).join('a') + '\n\n'
var longMessageChunks = longMessage.match(/.{1,10}/gs);
server.on('request', writeEvents(longMessageChunks))

var es = new EventSource(server.url)

es.onmessage = function () {
server.close(done)
}
})
})
})

describe('HTTP Request', function () {
Expand Down

0 comments on commit 64eefa3

Please sign in to comment.