Skip to content

Commit d265ba9

Browse files
authored
fix: added default headers
1 parent 2e0445c commit d265ba9

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

src/EventSource.js

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,30 @@ class EventSource {
3131
this.timeout = options.timeout ?? 0;
3232
this.timeoutBeforeConnection = options.timeoutBeforeConnection ?? 500;
3333
this.withCredentials = options.withCredentials || false;
34-
this.headers = options.headers || {};
3534
this.body = options.body || undefined;
3635
this.debug = options.debug || false;
3736
this.interval = options.pollingInterval ?? 5000;
3837
this.lineEndingCharacter = options.lineEndingCharacter || null;
3938

39+
const defaultHeaders = {
40+
Accept: 'text/event-stream',
41+
'Cache-Control': 'no-cache',
42+
'X-Requested-With': 'XMLHttpRequest',
43+
};
44+
45+
this.headers = {
46+
...defaultHeaders,
47+
...options.headers
48+
};
49+
4050
this._xhr = null;
4151
this._pollTimer = null;
4252
this._lastIndexProcessed = 0;
4353

44-
if (!url || (typeof url !== 'string' && typeof url.toString !== 'function')) {
54+
if (
55+
!url ||
56+
(typeof url !== 'string' && typeof url.toString !== 'function')
57+
) {
4558
throw new SyntaxError('[EventSource] Invalid URL argument.');
4659
}
4760

@@ -76,12 +89,8 @@ class EventSource {
7689
this._xhr.withCredentials = true;
7790
}
7891

79-
this._xhr.setRequestHeader('Accept', 'text/event-stream');
80-
81-
if (this.headers) {
82-
for (const [key, value] of Object.entries(this.headers)) {
83-
this._xhr.setRequestHeader(key, value);
84-
}
92+
for (const [key, value] of Object.entries(this.headers)) {
93+
if(value) this._xhr.setRequestHeader(key, value);
8594
}
8695

8796
if (this.lastEventId !== null) {
@@ -97,23 +106,34 @@ class EventSource {
97106

98107
const xhr = this._xhr;
99108

100-
this._logDebug(`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown'}(${xhr.readyState}), status: ${xhr.status}`);
109+
this._logDebug(
110+
`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown'
111+
}(${xhr.readyState}), status: ${xhr.status}`
112+
);
101113

102-
if (![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes(xhr.readyState)) {
114+
if (
115+
![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes(
116+
xhr.readyState
117+
)
118+
) {
103119
return;
104120
}
105121

106122
if (xhr.status >= 200 && xhr.status < 400) {
107123
if (this.status === this.CONNECTING) {
108124
this.status = this.OPEN;
109125
this.dispatch('open', { type: 'open' });
110-
this._logDebug('[EventSource][onreadystatechange][OPEN] Connection opened.');
126+
this._logDebug(
127+
'[EventSource][onreadystatechange][OPEN] Connection opened.'
128+
);
111129
}
112130

113131
this._handleEvent(xhr.responseText || '');
114132

115133
if (xhr.readyState === XMLHttpRequest.DONE) {
116-
this._logDebug('[EventSource][onreadystatechange][DONE] Operation done.');
134+
this._logDebug(
135+
'[EventSource][onreadystatechange][DONE] Operation done.'
136+
);
117137
this._pollAgain(this.interval, false);
118138
}
119139
} else if (xhr.status !== 0) {
@@ -126,7 +146,9 @@ class EventSource {
126146
});
127147

128148
if (xhr.readyState === XMLHttpRequest.DONE) {
129-
this._logDebug('[EventSource][onreadystatechange][ERROR] Response status error.');
149+
this._logDebug(
150+
'[EventSource][onreadystatechange][ERROR] Response status error.'
151+
);
130152
this._pollAgain(this.interval, false);
131153
}
132154
}
@@ -180,10 +202,16 @@ class EventSource {
180202
if (this.lineEndingCharacter === null) {
181203
const detectedNewlineChar = this._detectNewlineChar(response);
182204
if (detectedNewlineChar !== null) {
183-
this._logDebug(`[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify(detectedNewlineChar).slice(1, -1)}`);
205+
this._logDebug(
206+
`[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify(
207+
detectedNewlineChar
208+
).slice(1, -1)}`
209+
);
184210
this.lineEndingCharacter = detectedNewlineChar;
185211
} else {
186-
console.warn("[EventSource] Unable to identify the line ending character. Ensure your server delivers a standard line ending character: \\r\\n, \\n, \\r, or specify your custom character using the 'lineEndingCharacter' option.");
212+
console.warn(
213+
'[EventSource] Unable to identify the line ending character. Ensure your server delivers a standard line ending character: \\r\\n, \\n, \\r, or specify your custom character using the 'lineEndingCharacter' option.'
214+
);
187215
return;
188216
}
189217
}
@@ -193,7 +221,10 @@ class EventSource {
193221
return;
194222
}
195223

196-
const parts = response.substring(this._lastIndexProcessed, indexOfDoubleNewline).split(this.lineEndingCharacter);
224+
const parts = response
225+
.substring(this._lastIndexProcessed, indexOfDoubleNewline)
226+
.split(this.lineEndingCharacter);
227+
197228
this._lastIndexProcessed = indexOfDoubleNewline;
198229

199230
let type = undefined;
@@ -250,7 +281,8 @@ class EventSource {
250281
}
251282

252283
_getLastDoubleNewlineIndex(response) {
253-
const doubleLineEndingCharacter = this.lineEndingCharacter + this.lineEndingCharacter;
284+
const doubleLineEndingCharacter =
285+
this.lineEndingCharacter + this.lineEndingCharacter;
254286
const lastIndex = response.lastIndexOf(doubleLineEndingCharacter);
255287
if (lastIndex === -1) {
256288
return -1;
@@ -269,7 +301,9 @@ class EventSource {
269301

270302
removeEventListener(type, listener) {
271303
if (this.eventHandlers[type] !== undefined) {
272-
this.eventHandlers[type] = this.eventHandlers[type].filter((handler) => handler !== listener);
304+
this.eventHandlers[type] = this.eventHandlers[type].filter(
305+
(handler) => handler !== listener
306+
);
273307
}
274308
}
275309

@@ -282,7 +316,9 @@ class EventSource {
282316
}
283317
} else {
284318
if (!availableTypes.includes(type)) {
285-
throw Error(`[EventSource] '${type}' type is not supported event type.`);
319+
throw Error(
320+
`[EventSource] '${type}' type is not supported event type.`
321+
);
286322
}
287323

288324
this.eventHandlers[type] = [];

0 commit comments

Comments
 (0)