Skip to content

Commit

Permalink
Fix Javascript warnings and type definition gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetazzoni committed Feb 4, 2024
1 parent 57b6e4c commit 4cf1158
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions lib/sse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2023 Maxime Petazzoni <maxime.petazzoni@bulix.org>.
* Copyright (C) 2016-2024 Maxime Petazzoni <maxime.petazzoni@bulix.org>.
* All rights reserved.
*/

Expand Down Expand Up @@ -37,7 +37,7 @@ var SSE = function (url, options) {
/** @type {string} */
this.FIELD_SEPARATOR = ':';

/** @type { {[key: string]: EventListener} } */
/** @type { {[key: string]: [EventListener]} } */
this.listeners = {};

/** @type {XMLHttpRequest} */
Expand Down Expand Up @@ -70,7 +70,7 @@ var SSE = function (url, options) {
return;
}

var filtered = [];
const filtered = [];
this.listeners[type].forEach(function(element) {
if (element !== listener) {
filtered.push(element);
Expand All @@ -97,7 +97,7 @@ var SSE = function (url, options) {

e.source = this;

var onHandler = 'on' + e.type;
const onHandler = 'on' + e.type;
if (this.hasOwnProperty(onHandler)) {
this[onHandler].call(this, e);
if (e.defaultPrevented) {
Expand All @@ -117,20 +117,20 @@ var SSE = function (url, options) {

/** @private */
this._setReadyState = function(state) {
var event = new CustomEvent('readystatechange');
const event = new CustomEvent('readystatechange');
event.readyState = state;
this.readyState = state;
this.dispatchEvent(event);
};

this._onStreamFailure = function(e) {
var event = new CustomEvent('error');
const event = new CustomEvent('error');
event.data = e.currentTarget.response;
this.dispatchEvent(event);
this.close();
}

this._onStreamAbort = function(e) {
this._onStreamAbort = function() {
this.dispatchEvent(new CustomEvent('abort'));
this.close();
}
Expand All @@ -146,19 +146,21 @@ var SSE = function (url, options) {
return;
}

if (this.readyState == this.CONNECTING) {
if (this.readyState === this.CONNECTING) {
this.dispatchEvent(new CustomEvent('open'));
this._setReadyState(this.OPEN);
}

var data = this.xhr.responseText.substring(this.progress);
const data = this.xhr.responseText.substring(this.progress);

this.progress += data.length;
var parts = (this.chunk + data).split(/(\r\n\r\n|\r\r|\n\n)/g);
const parts = (this.chunk + data).split(/(\r\n\r\n|\r\r|\n\n)/g);

// we assume that the last chunk can be incomplete because of buffering or other network effects
// so we always save the last part to merge it with the next incoming packet
var lastPart = parts.pop();
/*
* We assume that the last chunk can be incomplete because of buffering or other network effects,
* so we always save the last part to merge it with the next incoming packet
*/
const lastPart = parts.pop();
parts.forEach(function(part) {
if (part.trim().length > 0) {
this.dispatchEvent(this._parseEventChunk(part));
Expand Down Expand Up @@ -190,13 +192,13 @@ var SSE = function (url, options) {
console.debug(chunk);
}

var e = {'id': null, 'retry': null, 'data': null, 'event': null};
const e = {'id': null, 'retry': null, 'data': null, 'event': null};
chunk.split(/\n|\r\n|\r/).forEach(function(line) {
var index = line.indexOf(this.FIELD_SEPARATOR);
var field, value;
const index = line.indexOf(this.FIELD_SEPARATOR);
let field, value;
if (index > 0) {
// only first whitespace should be trimmed
var skip = (line[index+1] === ' ') ? 2 : 1;
const skip = (line[index + 1] === ' ') ? 2 : 1;
field = line.substring(0, index);
value = line.substring(index + skip);
} else if (index < 0) {
Expand Down Expand Up @@ -256,7 +258,7 @@ var SSE = function (url, options) {
this.xhr.addEventListener('error', this._onStreamFailure.bind(this));
this.xhr.addEventListener('abort', this._onStreamAbort.bind(this));
this.xhr.open(this.method, this.url);
for (var header in this.headers) {
for (let header in this.headers) {
this.xhr.setRequestHeader(header, this.headers[header]);
}
this.xhr.withCredentials = this.withCredentials;
Expand Down Expand Up @@ -300,6 +302,7 @@ export { SSE };
* @property {string} [payload] - payload as a string
* @property {string} [method] - HTTP Method
* @property {boolean} [withCredentials] - flag, if credentials needed
* @property {boolean} [start] - flag, if streaming should start automatically
* @property {boolean} [debug] - debugging flag
*/
/**
Expand Down

0 comments on commit 4cf1158

Please sign in to comment.