From 2e0445c91e692fdeb6b17faa4bd72afb72b9a5eb Mon Sep 17 00:00:00 2001 From: Arpit Malik Date: Fri, 3 May 2024 16:35:08 +0530 Subject: [PATCH 1/4] fix: Disable cache control and x formatted by header --- src/EventSource.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/EventSource.js b/src/EventSource.js index 6a7b146..d2b78cf 100644 --- a/src/EventSource.js +++ b/src/EventSource.js @@ -77,8 +77,6 @@ class EventSource { } this._xhr.setRequestHeader('Accept', 'text/event-stream'); - this._xhr.setRequestHeader('Cache-Control', 'no-cache'); - this._xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); if (this.headers) { for (const [key, value] of Object.entries(this.headers)) { From 4af39af21313109e8a119bca89f649ca34e5070c Mon Sep 17 00:00:00 2001 From: Arpit Malik Date: Mon, 3 Jun 2024 12:47:32 +0530 Subject: [PATCH 2/4] fix: added default headers --- src/EventSource.js | 74 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/src/EventSource.js b/src/EventSource.js index d2b78cf..5e96aea 100644 --- a/src/EventSource.js +++ b/src/EventSource.js @@ -31,17 +31,30 @@ class EventSource { this.timeout = options.timeout ?? 0; this.timeoutBeforeConnection = options.timeoutBeforeConnection ?? 500; this.withCredentials = options.withCredentials || false; - this.headers = options.headers || {}; this.body = options.body || undefined; this.debug = options.debug || false; this.interval = options.pollingInterval ?? 5000; this.lineEndingCharacter = options.lineEndingCharacter || null; + const defaultHeaders = { + Accept: 'text/event-stream', + 'Cache-Control': 'no-cache', + 'X-Requested-With': 'XMLHttpRequest', + }; + + this.headers = { + ...defaultHeaders, + ...options.headers + }; + this._xhr = null; this._pollTimer = null; this._lastIndexProcessed = 0; - if (!url || (typeof url !== 'string' && typeof url.toString !== 'function')) { + if ( + !url || + (typeof url !== 'string' && typeof url.toString !== 'function') + ) { throw new SyntaxError('[EventSource] Invalid URL argument.'); } @@ -76,12 +89,8 @@ class EventSource { this._xhr.withCredentials = true; } - this._xhr.setRequestHeader('Accept', 'text/event-stream'); - - if (this.headers) { - for (const [key, value] of Object.entries(this.headers)) { - this._xhr.setRequestHeader(key, value); - } + for (const [key, value] of Object.entries(this.headers)) { + if(value) this._xhr.setRequestHeader(key, value); } if (this.lastEventId !== null) { @@ -97,9 +106,16 @@ class EventSource { const xhr = this._xhr; - this._logDebug(`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown'}(${xhr.readyState}), status: ${xhr.status}`); + this._logDebug( + `[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown' + }(${xhr.readyState}), status: ${xhr.status}` + ); - if (![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes(xhr.readyState)) { + if ( + ![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes( + xhr.readyState + ) + ) { return; } @@ -107,13 +123,17 @@ class EventSource { if (this.status === this.CONNECTING) { this.status = this.OPEN; this.dispatch('open', { type: 'open' }); - this._logDebug('[EventSource][onreadystatechange][OPEN] Connection opened.'); + this._logDebug( + '[EventSource][onreadystatechange][OPEN] Connection opened.' + ); } this._handleEvent(xhr.responseText || ''); if (xhr.readyState === XMLHttpRequest.DONE) { - this._logDebug('[EventSource][onreadystatechange][DONE] Operation done.'); + this._logDebug( + '[EventSource][onreadystatechange][DONE] Operation done.' + ); this._pollAgain(this.interval, false); } } else if (xhr.status !== 0) { @@ -126,7 +146,9 @@ class EventSource { }); if (xhr.readyState === XMLHttpRequest.DONE) { - this._logDebug('[EventSource][onreadystatechange][ERROR] Response status error.'); + this._logDebug( + '[EventSource][onreadystatechange][ERROR] Response status error.' + ); this._pollAgain(this.interval, false); } } @@ -180,10 +202,16 @@ class EventSource { if (this.lineEndingCharacter === null) { const detectedNewlineChar = this._detectNewlineChar(response); if (detectedNewlineChar !== null) { - this._logDebug(`[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify(detectedNewlineChar).slice(1, -1)}`); + this._logDebug( + `[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify( + detectedNewlineChar + ).slice(1, -1)}` + ); this.lineEndingCharacter = detectedNewlineChar; } else { - 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."); + 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.' + ); return; } } @@ -193,7 +221,10 @@ class EventSource { return; } - const parts = response.substring(this._lastIndexProcessed, indexOfDoubleNewline).split(this.lineEndingCharacter); + const parts = response + .substring(this._lastIndexProcessed, indexOfDoubleNewline) + .split(this.lineEndingCharacter); + this._lastIndexProcessed = indexOfDoubleNewline; let type = undefined; @@ -250,7 +281,8 @@ class EventSource { } _getLastDoubleNewlineIndex(response) { - const doubleLineEndingCharacter = this.lineEndingCharacter + this.lineEndingCharacter; + const doubleLineEndingCharacter = + this.lineEndingCharacter + this.lineEndingCharacter; const lastIndex = response.lastIndexOf(doubleLineEndingCharacter); if (lastIndex === -1) { return -1; @@ -269,7 +301,9 @@ class EventSource { removeEventListener(type, listener) { if (this.eventHandlers[type] !== undefined) { - this.eventHandlers[type] = this.eventHandlers[type].filter((handler) => handler !== listener); + this.eventHandlers[type] = this.eventHandlers[type].filter( + (handler) => handler !== listener + ); } } @@ -282,7 +316,9 @@ class EventSource { } } else { if (!availableTypes.includes(type)) { - throw Error(`[EventSource] '${type}' type is not supported event type.`); + throw Error( + `[EventSource] '${type}' type is not supported event type.` + ); } this.eventHandlers[type] = []; From d265ba918162196de1e6f645b83c573d0db9ce4c Mon Sep 17 00:00:00 2001 From: Arpit Malik Date: Mon, 3 Jun 2024 12:58:11 +0530 Subject: [PATCH 3/4] fix: added default headers --- src/EventSource.js | 74 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/src/EventSource.js b/src/EventSource.js index d2b78cf..5e96aea 100644 --- a/src/EventSource.js +++ b/src/EventSource.js @@ -31,17 +31,30 @@ class EventSource { this.timeout = options.timeout ?? 0; this.timeoutBeforeConnection = options.timeoutBeforeConnection ?? 500; this.withCredentials = options.withCredentials || false; - this.headers = options.headers || {}; this.body = options.body || undefined; this.debug = options.debug || false; this.interval = options.pollingInterval ?? 5000; this.lineEndingCharacter = options.lineEndingCharacter || null; + const defaultHeaders = { + Accept: 'text/event-stream', + 'Cache-Control': 'no-cache', + 'X-Requested-With': 'XMLHttpRequest', + }; + + this.headers = { + ...defaultHeaders, + ...options.headers + }; + this._xhr = null; this._pollTimer = null; this._lastIndexProcessed = 0; - if (!url || (typeof url !== 'string' && typeof url.toString !== 'function')) { + if ( + !url || + (typeof url !== 'string' && typeof url.toString !== 'function') + ) { throw new SyntaxError('[EventSource] Invalid URL argument.'); } @@ -76,12 +89,8 @@ class EventSource { this._xhr.withCredentials = true; } - this._xhr.setRequestHeader('Accept', 'text/event-stream'); - - if (this.headers) { - for (const [key, value] of Object.entries(this.headers)) { - this._xhr.setRequestHeader(key, value); - } + for (const [key, value] of Object.entries(this.headers)) { + if(value) this._xhr.setRequestHeader(key, value); } if (this.lastEventId !== null) { @@ -97,9 +106,16 @@ class EventSource { const xhr = this._xhr; - this._logDebug(`[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown'}(${xhr.readyState}), status: ${xhr.status}`); + this._logDebug( + `[EventSource][onreadystatechange] ReadyState: ${XMLReadyStateMap[xhr.readyState] || 'Unknown' + }(${xhr.readyState}), status: ${xhr.status}` + ); - if (![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes(xhr.readyState)) { + if ( + ![XMLHttpRequest.DONE, XMLHttpRequest.LOADING].includes( + xhr.readyState + ) + ) { return; } @@ -107,13 +123,17 @@ class EventSource { if (this.status === this.CONNECTING) { this.status = this.OPEN; this.dispatch('open', { type: 'open' }); - this._logDebug('[EventSource][onreadystatechange][OPEN] Connection opened.'); + this._logDebug( + '[EventSource][onreadystatechange][OPEN] Connection opened.' + ); } this._handleEvent(xhr.responseText || ''); if (xhr.readyState === XMLHttpRequest.DONE) { - this._logDebug('[EventSource][onreadystatechange][DONE] Operation done.'); + this._logDebug( + '[EventSource][onreadystatechange][DONE] Operation done.' + ); this._pollAgain(this.interval, false); } } else if (xhr.status !== 0) { @@ -126,7 +146,9 @@ class EventSource { }); if (xhr.readyState === XMLHttpRequest.DONE) { - this._logDebug('[EventSource][onreadystatechange][ERROR] Response status error.'); + this._logDebug( + '[EventSource][onreadystatechange][ERROR] Response status error.' + ); this._pollAgain(this.interval, false); } } @@ -180,10 +202,16 @@ class EventSource { if (this.lineEndingCharacter === null) { const detectedNewlineChar = this._detectNewlineChar(response); if (detectedNewlineChar !== null) { - this._logDebug(`[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify(detectedNewlineChar).slice(1, -1)}`); + this._logDebug( + `[EventSource] Automatically detected lineEndingCharacter: ${JSON.stringify( + detectedNewlineChar + ).slice(1, -1)}` + ); this.lineEndingCharacter = detectedNewlineChar; } else { - 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."); + 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.' + ); return; } } @@ -193,7 +221,10 @@ class EventSource { return; } - const parts = response.substring(this._lastIndexProcessed, indexOfDoubleNewline).split(this.lineEndingCharacter); + const parts = response + .substring(this._lastIndexProcessed, indexOfDoubleNewline) + .split(this.lineEndingCharacter); + this._lastIndexProcessed = indexOfDoubleNewline; let type = undefined; @@ -250,7 +281,8 @@ class EventSource { } _getLastDoubleNewlineIndex(response) { - const doubleLineEndingCharacter = this.lineEndingCharacter + this.lineEndingCharacter; + const doubleLineEndingCharacter = + this.lineEndingCharacter + this.lineEndingCharacter; const lastIndex = response.lastIndexOf(doubleLineEndingCharacter); if (lastIndex === -1) { return -1; @@ -269,7 +301,9 @@ class EventSource { removeEventListener(type, listener) { if (this.eventHandlers[type] !== undefined) { - this.eventHandlers[type] = this.eventHandlers[type].filter((handler) => handler !== listener); + this.eventHandlers[type] = this.eventHandlers[type].filter( + (handler) => handler !== listener + ); } } @@ -282,7 +316,9 @@ class EventSource { } } else { if (!availableTypes.includes(type)) { - throw Error(`[EventSource] '${type}' type is not supported event type.`); + throw Error( + `[EventSource] '${type}' type is not supported event type.` + ); } this.eventHandlers[type] = []; From f3c88f2fe855d92117d620d57f940418646ba672 Mon Sep 17 00:00:00 2001 From: Arpit Malik Date: Mon, 3 Jun 2024 16:36:09 +0530 Subject: [PATCH 4/4] Update EventSource.js --- src/EventSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSource.js b/src/EventSource.js index 5e96aea..2a1724f 100644 --- a/src/EventSource.js +++ b/src/EventSource.js @@ -210,7 +210,7 @@ class EventSource { this.lineEndingCharacter = detectedNewlineChar; } else { 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.' + "[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." ); return; }