Skip to content
Merged
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
45 changes: 22 additions & 23 deletions lib/web/eventsource/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const { EventSourceStream } = require('./eventsource-stream')
const { parseMIMEType } = require('../fetch/data-url')
const { createFastMessageEvent } = require('../websocket/events')
const { isNetworkError } = require('../fetch/response')
const { delay } = require('./util')
const { kEnumerableProperty } = require('../../core/util')
const { environmentSettingsObject } = require('../fetch/util')

Expand Down Expand Up @@ -318,9 +317,9 @@ class EventSource extends EventTarget {

/**
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html#sse-processing-model
* @returns {Promise<void>}
* @returns {void}
*/
async #reconnect () {
#reconnect () {
// When a user agent is to reestablish the connection, the user agent must
// run the following steps. These steps are run in parallel, not as part of
// a task. (The tasks that it queues, of course, are run like normal tasks
Expand All @@ -338,27 +337,27 @@ class EventSource extends EventTarget {
this.dispatchEvent(new Event('error'))

// 2. Wait a delay equal to the reconnection time of the event source.
await delay(this.#state.reconnectionTime)

// 5. Queue a task to run the following steps:

// 1. If the EventSource object's readyState attribute is not set to
// CONNECTING, then return.
if (this.#readyState !== CONNECTING) return

// 2. Let request be the EventSource object's request.
// 3. If the EventSource object's last event ID string is not the empty
// string, then:
// 1. Let lastEventIDValue be the EventSource object's last event ID
// string, encoded as UTF-8.
// 2. Set (`Last-Event-ID`, lastEventIDValue) in request's header
// list.
if (this.#state.lastEventId.length) {
this.#request.headersList.set('last-event-id', this.#state.lastEventId, true)
}
setTimeout(() => {
// 5. Queue a task to run the following steps:

// 1. If the EventSource object's readyState attribute is not set to
// CONNECTING, then return.
if (this.#readyState !== CONNECTING) return

// 2. Let request be the EventSource object's request.
// 3. If the EventSource object's last event ID string is not the empty
// string, then:
// 1. Let lastEventIDValue be the EventSource object's last event ID
// string, encoded as UTF-8.
// 2. Set (`Last-Event-ID`, lastEventIDValue) in request's header
// list.
if (this.#state.lastEventId.length) {
this.#request.headersList.set('last-event-id', this.#state.lastEventId, true)
}

// 4. Fetch request and process the response obtained in this fashion, if any, as described earlier in this section.
this.#connect()
// 4. Fetch request and process the response obtained in this fashion, if any, as described earlier in this section.
this.#connect()
}, this.#state.reconnectionTime)?.unref()
}

/**
Expand Down
10 changes: 1 addition & 9 deletions lib/web/eventsource/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ function isASCIINumber (value) {
return true
}

// https://github.com/nodejs/undici/issues/2664
function delay (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

module.exports = {
isValidLastEventId,
isASCIINumber,
delay
isASCIINumber
}