Skip to content

Commit

Permalink
Improve performance for large messages across many chunks (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
trentmwillis authored and rexxars committed Jan 27, 2020
1 parent 4ad734b commit dad588f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/eventsource.js
Expand Up @@ -181,6 +181,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 @@ -200,10 +202,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 @@ -218,7 +220,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
20 changes: 20 additions & 0 deletions test/eventsource_test.js
Expand Up @@ -467,6 +467,26 @@ 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 longMessageContent = new Array(100000).join('a')
var longMessage = 'data: ' + longMessageContent + '\n\n'
var longMessageChunks = longMessage.match(/[\s\S]{1,10}/g) // Split the message into chunks of 10 characters
server.on('request', writeEvents(longMessageChunks))

var es = new EventSource(server.url)

es.onmessage = function (m) {
assert.equal(longMessageContent, m.data)
server.close(done)
}
})
})
})

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

0 comments on commit dad588f

Please sign in to comment.