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
25 changes: 7 additions & 18 deletions lib/web/eventsource/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const { pipeline } = require('node:stream')
const { fetching } = require('../fetch')
const { makeRequest } = require('../fetch/request')
const { webidl } = require('../webidl')
const { EventSourceStream } = require('./eventsource-stream')
const { parseMIMEType } = require('../fetch/data-url')
const { createFastMessageEvent } = require('../websocket/events')
const { isNetworkError } = require('../fetch/response')
const { kEnumerableProperty } = require('../../core/util')
const { environmentSettingsObject } = require('../fetch/util')
const { createPotentialCORSRequest } = require('./util')

let experimentalWarned = false

Expand Down Expand Up @@ -160,33 +160,22 @@ class EventSource extends EventTarget {

// 8. Let request be the result of creating a potential-CORS request given
// urlRecord, the empty string, and corsAttributeState.
const initRequest = {
redirect: 'follow',
keepalive: true,
// @see https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes
mode: 'cors',
credentials: corsAttributeState === 'anonymous'
? 'same-origin'
: 'omit',
referrer: 'no-referrer'
}
const request = createPotentialCORSRequest(urlRecord, '', corsAttributeState)

// 9. Set request's client to settings.
initRequest.client = environmentSettingsObject.settingsObject
request.client = environmentSettingsObject.settingsObject

// 10. User agents may set (`Accept`, `text/event-stream`) in request's header list.
initRequest.headersList = [['accept', { name: 'accept', value: 'text/event-stream' }]]
request.headersList.set('Accept', 'text/event-stream')

// 11. Set request's cache mode to "no-store".
initRequest.cache = 'no-store'
request.cache = 'no-store'

// 12. Set request's initiator type to "other".
initRequest.initiator = 'other'

initRequest.urlList = [new URL(this.#url)]
request.initiator = 'other'

// 13. Set ev's request to request.
this.#request = makeRequest(initRequest)
this.#request = request

this.#connect()
}
Expand Down
33 changes: 32 additions & 1 deletion lib/web/eventsource/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { makeRequest } = require('../fetch/request')

/**
* Checks if the given value is a valid LastEventId.
* @param {string} value
Expand All @@ -23,7 +25,36 @@ function isASCIINumber (value) {
return true
}

function createPotentialCORSRequest (url, destination, corsAttributeState, sameOriginFallback) {
// 1. Let mode be "no-cors" if corsAttributeState is No CORS, and "cors" otherwise.
let mode = corsAttributeState === 'no cors' ? 'no-cors' : 'cors'

// 2. If same-origin fallback flag is set and mode is "no-cors", set mode to "same-origin".
if (sameOriginFallback && mode === 'no-cors') {
mode = 'same-origin'
}

// 3. Let credentialsMode be "include".
let credentialsMode = 'include'

// 4. If corsAttributeState is Anonymous, set credentialsMode to "same-origin".
if (corsAttributeState === 'anonymous') {
credentialsMode = 'same-origin'
}

// 5. Return a new request whose URL is url, destination is destination, mode is mode,
// credentials mode is credentialsMode, and whose use-URL-credentials flag is set.
return makeRequest({
urlList: [url],
destination,
mode,
credentials: credentialsMode,
useCredentials: true
})
}

module.exports = {
isValidLastEventId,
isASCIINumber
isASCIINumber,
createPotentialCORSRequest
}
1 change: 1 addition & 0 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ function makeRequest (init) {
referrerPolicy: init.referrerPolicy ?? '',
mode: init.mode ?? 'no-cors',
useCORSPreflightFlag: init.useCORSPreflightFlag ?? false,
// TODO: is this credentials mode? https://fetch.spec.whatwg.org/#concept-request-credentials-mode
credentials: init.credentials ?? 'same-origin',
useCredentials: init.useCredentials ?? false,
cache: init.cache ?? 'default',
Expand Down
4 changes: 2 additions & 2 deletions test/web-platform-tests/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -43499,11 +43499,11 @@
"success": true
},
{
"name": "EventSource: lastEventId persists",
"name": "EventSource: lastEventId resets (id without colon)",
"success": true
},
{
"name": "EventSource: lastEventId resets (id without colon)",
"name": "EventSource: lastEventId persists",
"success": true
}
]
Expand Down
Loading