Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/web/eventsource/eventsource-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ class EventSourceStream extends Transform {
if (isFieldName(line, fieldLength, EVENT)) {
const value = line.toString('utf8', valueStart)

if (value.length > 0) {
event.event = value
}
event.event = value
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/eventsource/eventsource-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ describe('EventSource - message', () => {
})
})

test('Should use the default message type when an empty event field replaces a custom type', async (t) => {
t.plan(3)

const server = http.createServer({ joinDuplicateHeaders: true }, (_req, res) => {
res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
res.end('event: custom\nevent:\ndata: payload\n\n')
})

await once(server.listen(0), 'listening')
const port = server.address().port
const eventSourceInstance = new EventSource(`http://localhost:${port}`)

t.after(async () => {
eventSourceInstance.close()
await new Promise(resolve => server.close(resolve))
})

const received = await Promise.race([
once(eventSourceInstance, 'message').then(([event]) => ({ listener: 'message', event })),
once(eventSourceInstance, 'custom').then(([event]) => ({ listener: 'custom', event }))
])

t.assert.strictEqual(received.listener, 'message')
t.assert.strictEqual(received.event.type, 'message')
t.assert.strictEqual(received.event.data, 'payload')
})

test('Should emit a message event if data is provided', (t, done) => {
t.plan(1)

Expand Down
16 changes: 6 additions & 10 deletions test/eventsource/eventsource-stream-parse-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,19 @@ describe('EventSourceStream - parseLine', () => {
t.assert.strictEqual(event.retry, undefined)
})

test('empty event', (t) => {
test('empty event replaces previous event type', (t) => {
const stream = new EventSourceStream({
eventSourceSettings: {
...defaultEventSourceSettings
}
})

const event = {}
'event: \ndata:data'.split('\n').forEach((line) => {
stream.parseLine(Buffer.from(line, 'utf8'), event)
})

t.assert.strictEqual(typeof event, 'object')
t.assert.strictEqual(Object.keys(event).length, 1)
t.assert.strictEqual(event.data, 'data')
t.assert.strictEqual(event.id, undefined)
t.assert.strictEqual(event.event, undefined)
t.assert.strictEqual(event.retry, undefined)
stream.parseLine(Buffer.from('event: custom', 'utf8'), event)
t.assert.strictEqual(event.event, 'custom')

stream.parseLine(Buffer.from('event: ', 'utf8'), event)
t.assert.strictEqual(event.event, '')
})
})
Loading