Skip to content

Commit e114e77

Browse files
authored
align EventSource with spec (#5418)
1 parent 6df53c5 commit e114e77

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

‎lib/web/eventsource/eventsource.js‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
const { pipeline } = require('node:stream')
44
const { fetching } = require('../fetch')
5-
const { makeRequest } = require('../fetch/request')
65
const { webidl } = require('../webidl')
76
const { EventSourceStream } = require('./eventsource-stream')
87
const { parseMIMEType } = require('../fetch/data-url')
98
const { createFastMessageEvent } = require('../websocket/events')
109
const { isNetworkError } = require('../fetch/response')
1110
const { kEnumerableProperty } = require('../../core/util')
1211
const { environmentSettingsObject } = require('../fetch/util')
12+
const { createPotentialCORSRequest } = require('./util')
1313

1414
let experimentalWarned = false
1515

@@ -160,33 +160,22 @@ class EventSource extends EventTarget {
160160

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

174165
// 9. Set request's client to settings.
175-
initRequest.client = environmentSettingsObject.settingsObject
166+
request.client = environmentSettingsObject.settingsObject
176167

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

180171
// 11. Set request's cache mode to "no-store".
181-
initRequest.cache = 'no-store'
172+
request.cache = 'no-store'
182173

183174
// 12. Set request's initiator type to "other".
184-
initRequest.initiator = 'other'
185-
186-
initRequest.urlList = [new URL(this.#url)]
175+
request.initiator = 'other'
187176

188177
// 13. Set ev's request to request.
189-
this.#request = makeRequest(initRequest)
178+
this.#request = request
190179

191180
this.#connect()
192181
}

‎lib/web/eventsource/util.js‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { makeRequest } = require('../fetch/request')
4+
35
/**
46
* Checks if the given value is a valid LastEventId.
57
* @param {string} value
@@ -23,7 +25,36 @@ function isASCIINumber (value) {
2325
return true
2426
}
2527

28+
function createPotentialCORSRequest (url, destination, corsAttributeState, sameOriginFallback) {
29+
// 1. Let mode be "no-cors" if corsAttributeState is No CORS, and "cors" otherwise.
30+
let mode = corsAttributeState === 'no cors' ? 'no-cors' : 'cors'
31+
32+
// 2. If same-origin fallback flag is set and mode is "no-cors", set mode to "same-origin".
33+
if (sameOriginFallback && mode === 'no-cors') {
34+
mode = 'same-origin'
35+
}
36+
37+
// 3. Let credentialsMode be "include".
38+
let credentialsMode = 'include'
39+
40+
// 4. If corsAttributeState is Anonymous, set credentialsMode to "same-origin".
41+
if (corsAttributeState === 'anonymous') {
42+
credentialsMode = 'same-origin'
43+
}
44+
45+
// 5. Return a new request whose URL is url, destination is destination, mode is mode,
46+
// credentials mode is credentialsMode, and whose use-URL-credentials flag is set.
47+
return makeRequest({
48+
urlList: [url],
49+
destination,
50+
mode,
51+
credentials: credentialsMode,
52+
useCredentials: true
53+
})
54+
}
55+
2656
module.exports = {
2757
isValidLastEventId,
28-
isASCIINumber
58+
isASCIINumber,
59+
createPotentialCORSRequest
2960
}

‎lib/web/fetch/request.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ function makeRequest (init) {
930930
referrerPolicy: init.referrerPolicy ?? '',
931931
mode: init.mode ?? 'no-cors',
932932
useCORSPreflightFlag: init.useCORSPreflightFlag ?? false,
933+
// TODO: is this credentials mode? https://fetch.spec.whatwg.org/#concept-request-credentials-mode
933934
credentials: init.credentials ?? 'same-origin',
934935
useCredentials: init.useCredentials ?? false,
935936
cache: init.cache ?? 'default',

‎test/web-platform-tests/expectation.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43499,11 +43499,11 @@
4349943499
"success": true
4350043500
},
4350143501
{
43502-
"name": "EventSource: lastEventId persists",
43502+
"name": "EventSource: lastEventId resets (id without colon)",
4350343503
"success": true
4350443504
},
4350543505
{
43506-
"name": "EventSource: lastEventId resets (id without colon)",
43506+
"name": "EventSource: lastEventId persists",
4350743507
"success": true
4350843508
}
4350943509
]

0 commit comments

Comments
 (0)