Skip to content

Commit c3fbe03

Browse files
committed
fix: Refactor XMLReadyStateMap declaration and improve header setting logic
1 parent f4507e6 commit c3fbe03

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"tabWidth": 2,
3+
"useTabs": false,
4+
"printWidth": 120,
5+
"singleQuote": true,
6+
"trailingComma": "all",
7+
"semi": true
8+
}

src/EventSource.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
const XMLReadyStateMap = [
2-
'UNSENT',
3-
'OPENED',
4-
'HEADERS_RECEIVED',
5-
'LOADING',
6-
'DONE',
7-
];
1+
const XMLReadyStateMap = ['UNSENT', 'OPENED', 'HEADERS_RECEIVED', 'LOADING', 'DONE'];
82

93
class EventSource {
104
ERROR = -1;
@@ -44,7 +38,7 @@ class EventSource {
4438

4539
this.headers = {
4640
...defaultHeaders,
47-
...options.headers
41+
...options.headers,
4842
};
4943

5044
this._xhr = null;
@@ -87,7 +81,9 @@ class EventSource {
8781
}
8882

8983
for (const [key, value] of Object.entries(this.headers)) {
90-
if(value) this._xhr.setRequestHeader(key, value);
84+
if (value !== undefined && value !== null) {
85+
this._xhr.setRequestHeader(key, value);
86+
}
9187
}
9288

9389
if (this.lastEventId !== null) {
@@ -103,7 +99,11 @@ class EventSource {
10399

104100
const xhr = this._xhr;
105101

106-
this._logDebug(`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown'}(${xhr.readyState}), status: ${xhr.status}`);
102+
this._logDebug(
103+
`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown'}(${
104+
xhr.readyState
105+
}), status: ${xhr.status}`,
106+
);
107107

108108
if (![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes(xhr.readyState)) {
109109
return;
@@ -186,10 +186,17 @@ class EventSource {
186186
if (this.lineEndingCharacter === null) {
187187
const detectedNewlineChar = this._detectNewlineChar(response);
188188
if (detectedNewlineChar !== null) {
189-
this._logDebug(`[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify(detectedNewlineChar).slice(1, -1)}`);
189+
this._logDebug(
190+
`[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify(detectedNewlineChar).slice(
191+
1,
192+
-1,
193+
)}`,
194+
);
190195
this.lineEndingCharacter = detectedNewlineChar;
191196
} else {
192-
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.");
197+
console.warn(
198+
"[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.",
199+
);
193200
return;
194201
}
195202
}

0 commit comments

Comments
 (0)