Comparing version 2.2.0 to 2.3.0
/*! | ||
* vue-sse v2.2.0 | ||
* vue-sse v2.3.0 | ||
* (c) 2021 James Churchard | ||
@@ -8,226 +8,2 @@ * @license MIT | ||
var formatText = function (e) { return e.data; }; | ||
var formatJSON = function (e) { return JSON.parse(e.data); }; | ||
var SSEClient = function SSEClient(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
if (config.handlers) { | ||
for (var event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
}; | ||
var prototypeAccessors = { source: { configurable: true } }; | ||
prototypeAccessors.source.get = function () { | ||
return this._source; | ||
}; | ||
SSEClient.prototype.connect = function connect () { | ||
var this$1 = this; | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
this$1._source.onopen = function () { | ||
// Add event listeners that were added before we connected | ||
for (var event in this$1._listeners) { | ||
this$1._source.addEventListener(event, this$1._listeners[event]); | ||
} | ||
this$1._source.onerror = null; | ||
resolve(this$1); | ||
}; | ||
this$1._source.onerror = reject; | ||
}); | ||
}; | ||
SSEClient.prototype.disconnect = function disconnect () { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
}; | ||
SSEClient.prototype.on = function on (event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
this._handlers[event].push(handler); | ||
return this; | ||
}; | ||
SSEClient.prototype.once = function once (event, handler) { | ||
var this$1 = this; | ||
this.on(event, function (e) { | ||
this$1.off(event, handler); | ||
handler(e); | ||
}); | ||
return this; | ||
}; | ||
SSEClient.prototype.off = function off (event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
var idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
return this; | ||
}; | ||
SSEClient.prototype._create = function _create (event) { | ||
var this$1 = this; | ||
this._handlers[event] = []; | ||
this._listeners[event] = function (message) { | ||
var data; | ||
try { | ||
data = this$1._format(message); | ||
} catch (err) { | ||
if (typeof this$1._source.onerror === 'function') { | ||
this$1._source.onerror(err); | ||
} | ||
return; | ||
} | ||
this$1._handlers[event].forEach(function (handler) { return handler(data); }); | ||
}; | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
}; | ||
Object.defineProperties( SSEClient.prototype, prototypeAccessors ); | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate: function beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy: function beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach(function (c) { return c.disconnect(); }); | ||
this.$sse.$clients = []; | ||
} | ||
} | ||
}); | ||
} | ||
var SSEManager = function SSEManager(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config | ||
); | ||
this.$clients = null; | ||
}; | ||
SSEManager.prototype.create = function create (configOrURL) { | ||
var config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
var client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
return client; | ||
}; | ||
var index_cjs = { | ||
SSEManager: SSEManager, | ||
install: install, | ||
}; | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
@@ -1279,5 +1055,240 @@ | ||
var eventsource$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), eventsource, { | ||
'default': eventsource | ||
'default': eventsource | ||
})); | ||
var formatText = function (e) { return e.data; }; | ||
var formatJSON = function (e) { return JSON.parse(e.data); }; | ||
var SSEClient = function SSEClient(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
if (config.handlers) { | ||
for (var event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
this.polyfillOptions = config.polyfillOptions || {}; | ||
this.forcePolyfill = !!config.forcePolyfill; | ||
}; | ||
var prototypeAccessors = { source: { configurable: true } }; | ||
prototypeAccessors.source.get = function () { | ||
return this._source; | ||
}; | ||
SSEClient.prototype.connect = function connect () { | ||
var this$1 = this; | ||
if (this.forcePolyfill) { | ||
this._source = eventsource.EventSourcePolyfill( | ||
this.url, | ||
Object.assign({}, this.config.polyfillOptions, { | ||
withCredentials: this.withCredentials, | ||
}) | ||
); | ||
} else { | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
this$1._source.onopen = function () { | ||
// Add event listeners that were added before we connected | ||
for (var event in this$1._listeners) { | ||
this$1._source.addEventListener(event, this$1._listeners[event]); | ||
} | ||
this$1._source.onerror = null; | ||
resolve(this$1); | ||
}; | ||
this$1._source.onerror = reject; | ||
}); | ||
}; | ||
SSEClient.prototype.disconnect = function disconnect () { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
}; | ||
SSEClient.prototype.on = function on (event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
this._handlers[event].push(handler); | ||
return this; | ||
}; | ||
SSEClient.prototype.once = function once (event, handler) { | ||
var this$1 = this; | ||
this.on(event, function (e) { | ||
this$1.off(event, handler); | ||
handler(e); | ||
}); | ||
return this; | ||
}; | ||
SSEClient.prototype.off = function off (event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
var idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
return this; | ||
}; | ||
SSEClient.prototype._create = function _create (event) { | ||
var this$1 = this; | ||
this._handlers[event] = []; | ||
this._listeners[event] = function (message) { | ||
var data; | ||
try { | ||
data = this$1._format(message); | ||
} catch (err) { | ||
if (typeof this$1._source.onerror === 'function') { | ||
this$1._source.onerror(err); | ||
} | ||
return; | ||
} | ||
this$1._handlers[event].forEach(function (handler) { return handler(data); }); | ||
}; | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
}; | ||
Object.defineProperties( SSEClient.prototype, prototypeAccessors ); | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate: function beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy: function beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach(function (c) { return c.disconnect(); }); | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
}); | ||
} | ||
var SSEManager = function SSEManager(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config | ||
); | ||
this.$clients = null; | ||
}; | ||
SSEManager.prototype.create = function create (configOrURL) { | ||
var config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
var client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
return client; | ||
}; | ||
var index_cjs = { | ||
SSEManager: SSEManager, | ||
install: install, | ||
}; | ||
module.exports = index_cjs; |
/*! | ||
* vue-sse v2.2.0 | ||
* vue-sse v2.3.0 | ||
* (c) 2021 James Churchard | ||
* @license MIT | ||
*/ | ||
const formatText = (e) => e.data; | ||
const formatJSON = (e) => JSON.parse(e.data); | ||
class SSEClient { | ||
constructor(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
if (config.handlers) { | ||
for (const event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
} | ||
get source() { | ||
return this._source; | ||
} | ||
connect() { | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
return new Promise((resolve, reject) => { | ||
this._source.onopen = () => { | ||
// Add event listeners that were added before we connected | ||
for (let event in this._listeners) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
this._source.onerror = null; | ||
resolve(this); | ||
}; | ||
this._source.onerror = reject; | ||
}); | ||
} | ||
disconnect() { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
} | ||
on(event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
this._handlers[event].push(handler); | ||
return this; | ||
} | ||
once(event, handler) { | ||
this.on(event, (e) => { | ||
this.off(event, handler); | ||
handler(e); | ||
}); | ||
return this; | ||
} | ||
off(event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
const idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
return this; | ||
} | ||
_create(event) { | ||
this._handlers[event] = []; | ||
this._listeners[event] = (message) => { | ||
let data; | ||
try { | ||
data = this._format(message); | ||
} catch (err) { | ||
if (typeof this._source.onerror === 'function') { | ||
this._source.onerror(err); | ||
} | ||
return; | ||
} | ||
this._handlers[event].forEach((handler) => handler(data)); | ||
}; | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
} | ||
} | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach(c => c.disconnect()); | ||
this.$sse.$clients = []; | ||
} | ||
} | ||
}); | ||
} | ||
class SSEManager { | ||
constructor(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config, | ||
); | ||
this.$clients = null; | ||
} | ||
create(configOrURL) { | ||
let config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
const client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
return client; | ||
} | ||
} | ||
var index = { | ||
install, | ||
}; | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
@@ -1269,6 +1052,234 @@ | ||
var eventsource$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), eventsource, { | ||
'default': eventsource | ||
'default': eventsource | ||
})); | ||
const formatText = (e) => e.data; | ||
const formatJSON = (e) => JSON.parse(e.data); | ||
class SSEClient { | ||
constructor(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
if (config.handlers) { | ||
for (const event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
this.polyfillOptions = config.polyfillOptions || {}; | ||
this.forcePolyfill = !!config.forcePolyfill; | ||
} | ||
get source() { | ||
return this._source; | ||
} | ||
connect() { | ||
if (this.forcePolyfill) { | ||
this._source = eventsource.EventSourcePolyfill( | ||
this.url, | ||
Object.assign({}, this.config.polyfillOptions, { | ||
withCredentials: this.withCredentials, | ||
}), | ||
); | ||
} else { | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this._source.onopen = () => { | ||
// Add event listeners that were added before we connected | ||
for (let event in this._listeners) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
this._source.onerror = null; | ||
resolve(this); | ||
}; | ||
this._source.onerror = reject; | ||
}); | ||
} | ||
disconnect() { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
} | ||
on(event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
this._handlers[event].push(handler); | ||
return this; | ||
} | ||
once(event, handler) { | ||
this.on(event, (e) => { | ||
this.off(event, handler); | ||
handler(e); | ||
}); | ||
return this; | ||
} | ||
off(event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
const idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
return this; | ||
} | ||
_create(event) { | ||
this._handlers[event] = []; | ||
this._listeners[event] = (message) => { | ||
let data; | ||
try { | ||
data = this._format(message); | ||
} catch (err) { | ||
if (typeof this._source.onerror === 'function') { | ||
this._source.onerror(err); | ||
} | ||
return; | ||
} | ||
this._handlers[event].forEach((handler) => handler(data)); | ||
}; | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
} | ||
} | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach((c) => c.disconnect()); | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
}); | ||
} | ||
class SSEManager { | ||
constructor(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config, | ||
); | ||
this.$clients = null; | ||
} | ||
create(configOrURL) { | ||
let config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
const client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
return client; | ||
} | ||
} | ||
var index = { | ||
install, | ||
}; | ||
export default index; | ||
export { SSEManager, install }; |
/*! | ||
* vue-sse v2.2.0 | ||
* vue-sse v2.3.0 | ||
* (c) 2021 James Churchard | ||
* @license MIT | ||
*/ | ||
const e=e=>e.data,t=e=>JSON.parse(e.data);class n{constructor(n){if(this._handlers={},this._listeners={},this._source=null,n.format?"string"==typeof n.format?"plain"===n.format?this._format=e:"json"===n.format?this._format=t:this._format=e:"function"==typeof n.format?this._format=n.format:this._format=e:this._format=e,n.handlers)for(const e in n.handlers)this.on(e,n.handlers[e]);this.url=n.url,this.withCredentials=!!n.withCredentials}get source(){return this._source}connect(){return this._source=new window.EventSource(this.url,{withCredentials:this.withCredentials}),new Promise(((e,t)=>{this._source.onopen=()=>{for(let e in this._listeners)this._source.addEventListener(e,this._listeners[e]);this._source.onerror=null,e(this)},this._source.onerror=t}))}disconnect(){null!==this._source&&(this._source.close(),this._source=null)}on(e,t){return e||(e="message"),this._listeners[e]||this._create(e),this._handlers[e].push(t),this}once(e,t){return this.on(e,(n=>{this.off(e,t),t(n)})),this}off(e,t){if(!this._handlers[e])return this;const n=this._handlers[e].indexOf(t);return-1===n||(this._handlers[e].splice(n,1),0===this._handlers[e].length&&(this._source.removeEventListener(e,this._listeners[e]),delete this._handlers[e],delete this._listeners[e])),this}_create(e){this._handlers[e]=[],this._listeners[e]=t=>{let n;try{n=this._format(t)}catch(e){return void("function"==typeof this._source.onerror&&this._source.onerror(e))}this._handlers[e].forEach((e=>e(n)))},this._source&&this._source.addEventListener(e,this._listeners[e])}}function r(e,t){e.$sse=e.prototype.$sse=new o(t),t&&t.polyfill&&Promise.resolve().then((function(){return c})),e.mixin({beforeCreate(){this.$options.sse&&this.$options.sse.cleanup&&(this.$sse=new o,this.$sse.$clients=[])},beforeDestroy(){null!==this.$sse.$clients&&(this.$sse.$clients.forEach((e=>e.disconnect())),this.$sse.$clients=[])}})}class o{constructor(t){this.$defaultConfig=Object.assign({format:e,sendCredentials:!1},t),this.$clients=null}create(e){let t;t="object"==typeof e?e:"string"==typeof e?{url:e}:{};const r=new n(Object.assign({},this.$defaultConfig,t));return null!==this.$clients&&this.$clients.push(r),r}}var s={install:r},i="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{}; | ||
var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{}; | ||
/** @license | ||
@@ -12,2 +12,2 @@ * eventsource.js | ||
*/ | ||
var a,l=(function(e,t){!function(n){var r=n.setTimeout,o=n.clearTimeout,s=n.XMLHttpRequest,i=n.XDomainRequest,a=n.ActiveXObject,l=n.EventSource,c=n.document,u=n.Promise,h=n.fetch,d=n.Response,f=n.TextDecoder,p=n.TextEncoder,y=n.AbortController;if("undefined"==typeof window||"readyState"in c||null!=c.body||(c.readyState="loading",window.addEventListener("load",(function(e){c.readyState="complete"}),!1)),null==s&&null!=a&&(s=function(){return new a("Microsoft.XMLHTTP")}),null==Object.create&&(Object.create=function(e){function t(){}return t.prototype=e,new t}),Date.now||(Date.now=function(){return(new Date).getTime()}),null==y){var v=h;h=function(e,t){var n=t.signal;return v(e,{headers:t.headers,credentials:t.credentials,cache:t.cache}).then((function(e){var t=e.body.getReader();return n._reader=t,n._aborted&&n._reader.cancel(),{status:e.status,statusText:e.statusText,headers:e.headers,body:{getReader:function(){return t}}}}))},y=function(){this.signal={_reader:null,_aborted:!1},this.abort=function(){null!=this.signal._reader&&this.signal._reader.cancel(),this.signal._aborted=!0}}}function g(){this.bitsNeeded=0,this.codePoint=0}g.prototype.decode=function(e){function t(e,t,n){if(1===n)return e>=128>>t&&e<<t<=2047;if(2===n)return e>=2048>>t&&e<<t<=55295||e>=57344>>t&&e<<t<=65535;if(3===n)return e>=65536>>t&&e<<t<=1114111;throw new Error}function n(e,t){if(6===e)return t>>6>15?3:t>31?2:1;if(12===e)return t>15?3:2;if(18===e)return 3;throw new Error}for(var r=65533,o="",s=this.bitsNeeded,i=this.codePoint,a=0;a<e.length;a+=1){var l=e[a];0!==s&&(l<128||l>191||!t(i<<6|63&l,s-6,n(s,i)))&&(s=0,i=r,o+=String.fromCharCode(i)),0===s?(l>=0&&l<=127?(s=0,i=l):l>=192&&l<=223?(s=6,i=31&l):l>=224&&l<=239?(s=12,i=15&l):l>=240&&l<=247?(s=18,i=7&l):(s=0,i=r),0===s||t(i,s,n(s,i))||(s=0,i=r)):(s-=6,i=i<<6|63&l),0===s&&(i<=65535?o+=String.fromCharCode(i):(o+=String.fromCharCode(55296+(i-65535-1>>10)),o+=String.fromCharCode(56320+(i-65535-1&1023))))}return this.bitsNeeded=s,this.codePoint=i,o},null!=f&&null!=p&&function(){try{return"test"===(new f).decode((new p).encode("test"),{stream:!0})}catch(e){console.debug("TextDecoder does not support streaming option. Using polyfill instead: "+e)}return!1}()||(f=g);var _=function(){};function w(e){this.withCredentials=!1,this.readyState=0,this.status=0,this.statusText="",this.responseText="",this.onprogress=_,this.onload=_,this.onerror=_,this.onreadystatechange=_,this._contentType="",this._xhr=e,this._sendTimeout=0,this._abort=_}function b(e){return e.replace(/[A-Z]/g,(function(e){return String.fromCharCode(e.charCodeAt(0)+32)}))}function m(e){for(var t=Object.create(null),n=e.split("\r\n"),r=0;r<n.length;r+=1){var o=n[r].split(": "),s=o.shift(),i=o.join(": ");t[b(s)]=i}this._map=t}function C(){}function E(e){this._headers=e}function T(){}function S(){this._listeners=Object.create(null)}function x(e){r((function(){throw e}),0)}function O(e){this.type=e,this.target=void 0}function A(e,t){O.call(this,e),this.data=t.data,this.lastEventId=t.lastEventId}function R(e,t){O.call(this,e),this.status=t.status,this.statusText=t.statusText,this.headers=t.headers}function D(e,t){O.call(this,e),this.error=t.error}w.prototype.open=function(e,t){this._abort(!0);var n=this,i=this._xhr,a=1,l=0;this._abort=function(e){0!==n._sendTimeout&&(o(n._sendTimeout),n._sendTimeout=0),1!==a&&2!==a&&3!==a||(a=4,i.onload=_,i.onerror=_,i.onabort=_,i.onprogress=_,i.onreadystatechange=_,i.abort(),0!==l&&(o(l),l=0),e||(n.readyState=4,n.onabort(null),n.onreadystatechange())),a=0};var c=function(){if(1===a){var e=0,t="",r=void 0;if("contentType"in i)e=200,t="OK",r=i.contentType;else try{e=i.status,t=i.statusText,r=i.getResponseHeader("Content-Type")}catch(n){e=0,t="",r=void 0}0!==e&&(a=2,n.readyState=2,n.status=e,n.statusText=t,n._contentType=r,n.onreadystatechange())}},u=function(){if(c(),2===a||3===a){a=3;var e="";try{e=i.responseText}catch(e){}n.readyState=3,n.responseText=e,n.onprogress()}},h=function(e,t){if(null!=t&&null!=t.preventDefault||(t={preventDefault:_}),u(),1===a||2===a||3===a){if(a=4,0!==l&&(o(l),l=0),n.readyState=4,"load"===e)n.onload(t);else if("error"===e)n.onerror(t);else{if("abort"!==e)throw new TypeError;n.onabort(t)}n.onreadystatechange()}},d=function(){l=r((function(){d()}),500),3===i.readyState&&u()};"onload"in i&&(i.onload=function(e){h("load",e)}),"onerror"in i&&(i.onerror=function(e){h("error",e)}),"onabort"in i&&(i.onabort=function(e){h("abort",e)}),"onprogress"in i&&(i.onprogress=u),"onreadystatechange"in i&&(i.onreadystatechange=function(e){!function(e){null!=i&&(4===i.readyState?"onload"in i&&"onerror"in i&&"onabort"in i||h(""===i.responseText?"error":"load",e):3===i.readyState?"onprogress"in i||u():2===i.readyState&&c())}(e)}),!("contentType"in i)&&"ontimeout"in s.prototype||(t+=(-1===t.indexOf("?")?"?":"&")+"padding=true"),i.open(e,t,!0),"readyState"in i&&(l=r((function(){d()}),0))},w.prototype.abort=function(){this._abort(!1)},w.prototype.getResponseHeader=function(e){return this._contentType},w.prototype.setRequestHeader=function(e,t){var n=this._xhr;"setRequestHeader"in n&&n.setRequestHeader(e,t)},w.prototype.getAllResponseHeaders=function(){return null!=this._xhr.getAllResponseHeaders&&this._xhr.getAllResponseHeaders()||""},w.prototype.send=function(){if("ontimeout"in s.prototype&&("sendAsBinary"in s.prototype||"mozAnon"in s.prototype)||null==c||null==c.readyState||"complete"===c.readyState){var e=this._xhr;"withCredentials"in e&&(e.withCredentials=this.withCredentials);try{e.send(void 0)}catch(e){throw e}}else{var t=this;t._sendTimeout=r((function(){t._sendTimeout=0,t.send()}),4)}},m.prototype.get=function(e){return this._map[b(e)]},null!=s&&null==s.HEADERS_RECEIVED&&(s.HEADERS_RECEIVED=2),C.prototype.open=function(e,t,n,r,o,i,a){e.open("GET",o);var l=0;for(var c in e.onprogress=function(){var t=e.responseText.slice(l);l+=t.length,n(t)},e.onerror=function(e){e.preventDefault(),r(new Error("NetworkError"))},e.onload=function(){r(null)},e.onabort=function(){r(null)},e.onreadystatechange=function(){if(e.readyState===s.HEADERS_RECEIVED){var n=e.status,r=e.statusText,o=e.getResponseHeader("Content-Type"),i=e.getAllResponseHeaders();t(n,r,o,new m(i))}},e.withCredentials=i,a)Object.prototype.hasOwnProperty.call(a,c)&&e.setRequestHeader(c,a[c]);return e.send(),e},E.prototype.get=function(e){return this._headers.get(e)},T.prototype.open=function(e,t,n,r,o,s,i){var a=null,l=new y,c=l.signal,d=new f;return h(o,{headers:i,credentials:s?"include":"same-origin",signal:c,cache:"no-store"}).then((function(e){return a=e.body.getReader(),t(e.status,e.statusText,e.headers.get("Content-Type"),new E(e.headers)),new u((function(e,t){var r=function(){a.read().then((function(t){if(t.done)e(void 0);else{var o=d.decode(t.value,{stream:!0});n(o),r()}})).catch((function(e){t(e)}))};r()}))})).catch((function(e){return"AbortError"===e.name?void 0:e})).then((function(e){r(e)})),{abort:function(){null!=a&&a.cancel(),l.abort()}}},S.prototype.dispatchEvent=function(e){e.target=this;var t=this._listeners[e.type];if(null!=t)for(var n=t.length,r=0;r<n;r+=1){var o=t[r];try{"function"==typeof o.handleEvent?o.handleEvent(e):o.call(this,e)}catch(e){x(e)}}},S.prototype.addEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];null==r&&(r=[],n[e]=r);for(var o=!1,s=0;s<r.length;s+=1)r[s]===t&&(o=!0);o||r.push(t)},S.prototype.removeEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];if(null!=r){for(var o=[],s=0;s<r.length;s+=1)r[s]!==t&&o.push(r[s]);0===o.length?delete n[e]:n[e]=o}},A.prototype=Object.create(O.prototype),R.prototype=Object.create(O.prototype),D.prototype=Object.create(O.prototype);var j=-1,$=-1,N=/^text\/event\-stream(;.*)?$/i,H=function(e,t){var n=null==e?t:parseInt(e,10);return n!=n&&(n=t),P(n)},P=function(e){return Math.min(Math.max(e,1e3),18e6)},I=function(e,t,n){try{"function"==typeof t&&t.call(e,n)}catch(e){x(e)}};function L(e,t){S.call(this),t=t||{},this.onopen=void 0,this.onmessage=void 0,this.onerror=void 0,this.url=void 0,this.readyState=void 0,this.withCredentials=void 0,this.headers=void 0,this._close=void 0,function(e,t,n){t=String(t);var a=Boolean(n.withCredentials),l=n.lastEventIdQueryParameterName||"lastEventId",c=P(1e3),u=H(n.heartbeatTimeout,45e3),h="",d=c,f=!1,p=0,y=n.headers||{},v=n.Transport,g=M&&null==v?void 0:new w(null!=v?new v:null!=s&&"withCredentials"in s.prototype||null==i?new s:new i),_=null!=v&&"string"!=typeof v?new v:null==g?new T:new C,b=void 0,m=0,E=j,S="",x="",O="",L="",q=0,X=0,G=0,V=function(t,n,r,o){if(0===E)if(200===t&&null!=r&&N.test(r)){E=1,f=Date.now(),d=c,e.readyState=1;var s=new R("open",{status:t,statusText:n,headers:o});e.dispatchEvent(s),I(e,e.onopen,s)}else{var i="";200!==t?(n&&(n=n.replace(/\s+/g," ")),i="EventSource's response has a status "+t+" "+n+" that is not 200. Aborting the connection."):i="EventSource's response has a Content-Type specifying an unsupported type: "+(null==r?"-":r.replace(/\s+/g," "))+". Aborting the connection.",U(),s=new R("error",{status:t,statusText:n,headers:o}),e.dispatchEvent(s),I(e,e.onerror,s),console.error(i)}},z=function(t){if(1===E){for(var n=-1,s=0;s<t.length;s+=1)(l=t.charCodeAt(s))!=="\n".charCodeAt(0)&&l!=="\r".charCodeAt(0)||(n=s);var i=(-1!==n?L:"")+t.slice(0,n+1);L=(-1===n?L:"")+t.slice(n+1),""!==t&&(f=Date.now(),p+=t.length);for(var a=0;a<i.length;a+=1){var l=i.charCodeAt(a);if(q===$&&l==="\n".charCodeAt(0))q=0;else if(q===$&&(q=0),l==="\r".charCodeAt(0)||l==="\n".charCodeAt(0)){if(0!==q){1===q&&(G=a+1);var y=i.slice(X,G-1),v=i.slice(G+(G<a&&i.charCodeAt(G)===" ".charCodeAt(0)?1:0),a);"data"===y?(S+="\n",S+=v):"id"===y?x=v:"event"===y?O=v:"retry"===y?(c=H(v,c),d=c):"heartbeatTimeout"===y&&(u=H(v,u),0!==m&&(o(m),m=r((function(){k()}),u)))}if(0===q){if(""!==S){h=x,""===O&&(O="message");var g=new A(O,{data:S.slice(1),lastEventId:x});if(e.dispatchEvent(g),"open"===O?I(e,e.onopen,g):"message"===O?I(e,e.onmessage,g):"error"===O&&I(e,e.onerror,g),2===E)return}S="",O=""}q=l==="\r".charCodeAt(0)?$:0}else 0===q&&(X=a,q=1),1===q?l===":".charCodeAt(0)&&(G=a+1,q=2):2===q&&(q=3)}}},B=function(t){if(1===E||0===E){E=j,0!==m&&(o(m),m=0),m=r((function(){k()}),d),d=P(Math.min(16*c,2*d)),e.readyState=0;var n=new D("error",{error:t});e.dispatchEvent(n),I(e,e.onerror,n),null!=t&&console.error(t)}},U=function(){E=2,null!=b&&(b.abort(),b=void 0),0!==m&&(o(m),m=0),e.readyState=2},k=function(){if(m=0,E===j){f=!1,p=0,m=r((function(){k()}),u),E=0,S="",O="",x=h,L="",X=0,G=0,q=0;var n=t;"data:"!==t.slice(0,5)&&"blob:"!==t.slice(0,5)&&""!==h&&(n+=(-1===t.indexOf("?")?"?":"&")+l+"="+encodeURIComponent(h));var o=e.withCredentials,s={Accept:"text/event-stream"},i=e.headers;if(null!=i)for(var a in i)Object.prototype.hasOwnProperty.call(i,a)&&(s[a]=i[a]);try{b=_.open(g,V,z,B,n,o,s)}catch(e){throw U(),e}}else if(f||null==b){var c=Math.max((f||Date.now())+u-Date.now(),1);f=!1,m=r((function(){k()}),c)}else B(new Error("No activity within "+u+" milliseconds. "+(0===E?"No response received.":p+" chars received.")+" Reconnecting.")),null!=b&&(b.abort(),b=void 0)};e.url=t,e.readyState=0,e.withCredentials=a,e.headers=y,e._close=U,k()}(this,e,t)}var M=null!=h&&null!=d&&"body"in d.prototype;L.prototype=Object.create(S.prototype),L.prototype.CONNECTING=0,L.prototype.OPEN=1,L.prototype.CLOSED=2,L.prototype.close=function(){this._close()},L.CONNECTING=0,L.OPEN=1,L.CLOSED=2,L.prototype.withCredentials=void 0;var q,X=l;null==s||null!=l&&"withCredentials"in l.prototype||(X=L),void 0!==(q=function(e){e.EventSourcePolyfill=L,e.NativeEventSource=l,e.EventSource=X}(t))&&(e.exports=q)}("undefined"==typeof globalThis?"undefined"!=typeof window?window:"undefined"!=typeof self?self:i:globalThis)}(a={exports:{}},a.exports),a.exports),c=Object.freeze(Object.assign(Object.create(null),l,{default:l}));export default s;export{o as SSEManager,r as install}; | ||
var t,n=(function(t,n){!function(e){var r=e.setTimeout,o=e.clearTimeout,s=e.XMLHttpRequest,i=e.XDomainRequest,a=e.ActiveXObject,l=e.EventSource,c=e.document,u=e.Promise,h=e.fetch,d=e.Response,f=e.TextDecoder,p=e.TextEncoder,y=e.AbortController;if("undefined"==typeof window||"readyState"in c||null!=c.body||(c.readyState="loading",window.addEventListener("load",(function(e){c.readyState="complete"}),!1)),null==s&&null!=a&&(s=function(){return new a("Microsoft.XMLHTTP")}),null==Object.create&&(Object.create=function(e){function t(){}return t.prototype=e,new t}),Date.now||(Date.now=function(){return(new Date).getTime()}),null==y){var v=h;h=function(e,t){var n=t.signal;return v(e,{headers:t.headers,credentials:t.credentials,cache:t.cache}).then((function(e){var t=e.body.getReader();return n._reader=t,n._aborted&&n._reader.cancel(),{status:e.status,statusText:e.statusText,headers:e.headers,body:{getReader:function(){return t}}}}))},y=function(){this.signal={_reader:null,_aborted:!1},this.abort=function(){null!=this.signal._reader&&this.signal._reader.cancel(),this.signal._aborted=!0}}}function g(){this.bitsNeeded=0,this.codePoint=0}g.prototype.decode=function(e){function t(e,t,n){if(1===n)return e>=128>>t&&e<<t<=2047;if(2===n)return e>=2048>>t&&e<<t<=55295||e>=57344>>t&&e<<t<=65535;if(3===n)return e>=65536>>t&&e<<t<=1114111;throw new Error}function n(e,t){if(6===e)return t>>6>15?3:t>31?2:1;if(12===e)return t>15?3:2;if(18===e)return 3;throw new Error}for(var r=65533,o="",s=this.bitsNeeded,i=this.codePoint,a=0;a<e.length;a+=1){var l=e[a];0!==s&&(l<128||l>191||!t(i<<6|63&l,s-6,n(s,i)))&&(s=0,i=r,o+=String.fromCharCode(i)),0===s?(l>=0&&l<=127?(s=0,i=l):l>=192&&l<=223?(s=6,i=31&l):l>=224&&l<=239?(s=12,i=15&l):l>=240&&l<=247?(s=18,i=7&l):(s=0,i=r),0===s||t(i,s,n(s,i))||(s=0,i=r)):(s-=6,i=i<<6|63&l),0===s&&(i<=65535?o+=String.fromCharCode(i):(o+=String.fromCharCode(55296+(i-65535-1>>10)),o+=String.fromCharCode(56320+(i-65535-1&1023))))}return this.bitsNeeded=s,this.codePoint=i,o},null!=f&&null!=p&&function(){try{return"test"===(new f).decode((new p).encode("test"),{stream:!0})}catch(e){console.debug("TextDecoder does not support streaming option. Using polyfill instead: "+e)}return!1}()||(f=g);var _=function(){};function w(e){this.withCredentials=!1,this.readyState=0,this.status=0,this.statusText="",this.responseText="",this.onprogress=_,this.onload=_,this.onerror=_,this.onreadystatechange=_,this._contentType="",this._xhr=e,this._sendTimeout=0,this._abort=_}function b(e){return e.replace(/[A-Z]/g,(function(e){return String.fromCharCode(e.charCodeAt(0)+32)}))}function m(e){for(var t=Object.create(null),n=e.split("\r\n"),r=0;r<n.length;r+=1){var o=n[r].split(": "),s=o.shift(),i=o.join(": ");t[b(s)]=i}this._map=t}function C(){}function E(e){this._headers=e}function T(){}function S(){this._listeners=Object.create(null)}function x(e){r((function(){throw e}),0)}function O(e){this.type=e,this.target=void 0}function A(e,t){O.call(this,e),this.data=t.data,this.lastEventId=t.lastEventId}function R(e,t){O.call(this,e),this.status=t.status,this.statusText=t.statusText,this.headers=t.headers}function D(e,t){O.call(this,e),this.error=t.error}w.prototype.open=function(e,t){this._abort(!0);var n=this,i=this._xhr,a=1,l=0;this._abort=function(e){0!==n._sendTimeout&&(o(n._sendTimeout),n._sendTimeout=0),1!==a&&2!==a&&3!==a||(a=4,i.onload=_,i.onerror=_,i.onabort=_,i.onprogress=_,i.onreadystatechange=_,i.abort(),0!==l&&(o(l),l=0),e||(n.readyState=4,n.onabort(null),n.onreadystatechange())),a=0};var c=function(){if(1===a){var e=0,t="",r=void 0;if("contentType"in i)e=200,t="OK",r=i.contentType;else try{e=i.status,t=i.statusText,r=i.getResponseHeader("Content-Type")}catch(n){e=0,t="",r=void 0}0!==e&&(a=2,n.readyState=2,n.status=e,n.statusText=t,n._contentType=r,n.onreadystatechange())}},u=function(){if(c(),2===a||3===a){a=3;var e="";try{e=i.responseText}catch(e){}n.readyState=3,n.responseText=e,n.onprogress()}},h=function(e,t){if(null!=t&&null!=t.preventDefault||(t={preventDefault:_}),u(),1===a||2===a||3===a){if(a=4,0!==l&&(o(l),l=0),n.readyState=4,"load"===e)n.onload(t);else if("error"===e)n.onerror(t);else{if("abort"!==e)throw new TypeError;n.onabort(t)}n.onreadystatechange()}},d=function(){l=r((function(){d()}),500),3===i.readyState&&u()};"onload"in i&&(i.onload=function(e){h("load",e)}),"onerror"in i&&(i.onerror=function(e){h("error",e)}),"onabort"in i&&(i.onabort=function(e){h("abort",e)}),"onprogress"in i&&(i.onprogress=u),"onreadystatechange"in i&&(i.onreadystatechange=function(e){!function(e){null!=i&&(4===i.readyState?"onload"in i&&"onerror"in i&&"onabort"in i||h(""===i.responseText?"error":"load",e):3===i.readyState?"onprogress"in i||u():2===i.readyState&&c())}(e)}),!("contentType"in i)&&"ontimeout"in s.prototype||(t+=(-1===t.indexOf("?")?"?":"&")+"padding=true"),i.open(e,t,!0),"readyState"in i&&(l=r((function(){d()}),0))},w.prototype.abort=function(){this._abort(!1)},w.prototype.getResponseHeader=function(e){return this._contentType},w.prototype.setRequestHeader=function(e,t){var n=this._xhr;"setRequestHeader"in n&&n.setRequestHeader(e,t)},w.prototype.getAllResponseHeaders=function(){return null!=this._xhr.getAllResponseHeaders&&this._xhr.getAllResponseHeaders()||""},w.prototype.send=function(){if("ontimeout"in s.prototype&&("sendAsBinary"in s.prototype||"mozAnon"in s.prototype)||null==c||null==c.readyState||"complete"===c.readyState){var e=this._xhr;"withCredentials"in e&&(e.withCredentials=this.withCredentials);try{e.send(void 0)}catch(e){throw e}}else{var t=this;t._sendTimeout=r((function(){t._sendTimeout=0,t.send()}),4)}},m.prototype.get=function(e){return this._map[b(e)]},null!=s&&null==s.HEADERS_RECEIVED&&(s.HEADERS_RECEIVED=2),C.prototype.open=function(e,t,n,r,o,i,a){e.open("GET",o);var l=0;for(var c in e.onprogress=function(){var t=e.responseText.slice(l);l+=t.length,n(t)},e.onerror=function(e){e.preventDefault(),r(new Error("NetworkError"))},e.onload=function(){r(null)},e.onabort=function(){r(null)},e.onreadystatechange=function(){if(e.readyState===s.HEADERS_RECEIVED){var n=e.status,r=e.statusText,o=e.getResponseHeader("Content-Type"),i=e.getAllResponseHeaders();t(n,r,o,new m(i))}},e.withCredentials=i,a)Object.prototype.hasOwnProperty.call(a,c)&&e.setRequestHeader(c,a[c]);return e.send(),e},E.prototype.get=function(e){return this._headers.get(e)},T.prototype.open=function(e,t,n,r,o,s,i){var a=null,l=new y,c=l.signal,d=new f;return h(o,{headers:i,credentials:s?"include":"same-origin",signal:c,cache:"no-store"}).then((function(e){return a=e.body.getReader(),t(e.status,e.statusText,e.headers.get("Content-Type"),new E(e.headers)),new u((function(e,t){var r=function(){a.read().then((function(t){if(t.done)e(void 0);else{var o=d.decode(t.value,{stream:!0});n(o),r()}})).catch((function(e){t(e)}))};r()}))})).catch((function(e){return"AbortError"===e.name?void 0:e})).then((function(e){r(e)})),{abort:function(){null!=a&&a.cancel(),l.abort()}}},S.prototype.dispatchEvent=function(e){e.target=this;var t=this._listeners[e.type];if(null!=t)for(var n=t.length,r=0;r<n;r+=1){var o=t[r];try{"function"==typeof o.handleEvent?o.handleEvent(e):o.call(this,e)}catch(e){x(e)}}},S.prototype.addEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];null==r&&(r=[],n[e]=r);for(var o=!1,s=0;s<r.length;s+=1)r[s]===t&&(o=!0);o||r.push(t)},S.prototype.removeEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];if(null!=r){for(var o=[],s=0;s<r.length;s+=1)r[s]!==t&&o.push(r[s]);0===o.length?delete n[e]:n[e]=o}},A.prototype=Object.create(O.prototype),R.prototype=Object.create(O.prototype),D.prototype=Object.create(O.prototype);var j=-1,$=-1,N=/^text\/event\-stream(;.*)?$/i,P=function(e,t){var n=null==e?t:parseInt(e,10);return n!=n&&(n=t),H(n)},H=function(e){return Math.min(Math.max(e,1e3),18e6)},I=function(e,t,n){try{"function"==typeof t&&t.call(e,n)}catch(e){x(e)}};function L(e,t){S.call(this),t=t||{},this.onopen=void 0,this.onmessage=void 0,this.onerror=void 0,this.url=void 0,this.readyState=void 0,this.withCredentials=void 0,this.headers=void 0,this._close=void 0,function(e,t,n){t=String(t);var a=Boolean(n.withCredentials),l=n.lastEventIdQueryParameterName||"lastEventId",c=H(1e3),u=P(n.heartbeatTimeout,45e3),h="",d=c,f=!1,p=0,y=n.headers||{},v=n.Transport,g=M&&null==v?void 0:new w(null!=v?new v:null!=s&&"withCredentials"in s.prototype||null==i?new s:new i),_=null!=v&&"string"!=typeof v?new v:null==g?new T:new C,b=void 0,m=0,E=j,S="",x="",O="",L="",q=0,X=0,G=0,V=function(t,n,r,o){if(0===E)if(200===t&&null!=r&&N.test(r)){E=1,f=Date.now(),d=c,e.readyState=1;var s=new R("open",{status:t,statusText:n,headers:o});e.dispatchEvent(s),I(e,e.onopen,s)}else{var i="";200!==t?(n&&(n=n.replace(/\s+/g," ")),i="EventSource's response has a status "+t+" "+n+" that is not 200. Aborting the connection."):i="EventSource's response has a Content-Type specifying an unsupported type: "+(null==r?"-":r.replace(/\s+/g," "))+". Aborting the connection.",U(),s=new R("error",{status:t,statusText:n,headers:o}),e.dispatchEvent(s),I(e,e.onerror,s),console.error(i)}},z=function(t){if(1===E){for(var n=-1,s=0;s<t.length;s+=1)(l=t.charCodeAt(s))!=="\n".charCodeAt(0)&&l!=="\r".charCodeAt(0)||(n=s);var i=(-1!==n?L:"")+t.slice(0,n+1);L=(-1===n?L:"")+t.slice(n+1),""!==t&&(f=Date.now(),p+=t.length);for(var a=0;a<i.length;a+=1){var l=i.charCodeAt(a);if(q===$&&l==="\n".charCodeAt(0))q=0;else if(q===$&&(q=0),l==="\r".charCodeAt(0)||l==="\n".charCodeAt(0)){if(0!==q){1===q&&(G=a+1);var y=i.slice(X,G-1),v=i.slice(G+(G<a&&i.charCodeAt(G)===" ".charCodeAt(0)?1:0),a);"data"===y?(S+="\n",S+=v):"id"===y?x=v:"event"===y?O=v:"retry"===y?(c=P(v,c),d=c):"heartbeatTimeout"===y&&(u=P(v,u),0!==m&&(o(m),m=r((function(){k()}),u)))}if(0===q){if(""!==S){h=x,""===O&&(O="message");var g=new A(O,{data:S.slice(1),lastEventId:x});if(e.dispatchEvent(g),"open"===O?I(e,e.onopen,g):"message"===O?I(e,e.onmessage,g):"error"===O&&I(e,e.onerror,g),2===E)return}S="",O=""}q=l==="\r".charCodeAt(0)?$:0}else 0===q&&(X=a,q=1),1===q?l===":".charCodeAt(0)&&(G=a+1,q=2):2===q&&(q=3)}}},B=function(t){if(1===E||0===E){E=j,0!==m&&(o(m),m=0),m=r((function(){k()}),d),d=H(Math.min(16*c,2*d)),e.readyState=0;var n=new D("error",{error:t});e.dispatchEvent(n),I(e,e.onerror,n),null!=t&&console.error(t)}},U=function(){E=2,null!=b&&(b.abort(),b=void 0),0!==m&&(o(m),m=0),e.readyState=2},k=function(){if(m=0,E===j){f=!1,p=0,m=r((function(){k()}),u),E=0,S="",O="",x=h,L="",X=0,G=0,q=0;var n=t;"data:"!==t.slice(0,5)&&"blob:"!==t.slice(0,5)&&""!==h&&(n+=(-1===t.indexOf("?")?"?":"&")+l+"="+encodeURIComponent(h));var o=e.withCredentials,s={Accept:"text/event-stream"},i=e.headers;if(null!=i)for(var a in i)Object.prototype.hasOwnProperty.call(i,a)&&(s[a]=i[a]);try{b=_.open(g,V,z,B,n,o,s)}catch(e){throw U(),e}}else if(f||null==b){var c=Math.max((f||Date.now())+u-Date.now(),1);f=!1,m=r((function(){k()}),c)}else B(new Error("No activity within "+u+" milliseconds. "+(0===E?"No response received.":p+" chars received.")+" Reconnecting.")),null!=b&&(b.abort(),b=void 0)};e.url=t,e.readyState=0,e.withCredentials=a,e.headers=y,e._close=U,k()}(this,e,t)}var M=null!=h&&null!=d&&"body"in d.prototype;L.prototype=Object.create(S.prototype),L.prototype.CONNECTING=0,L.prototype.OPEN=1,L.prototype.CLOSED=2,L.prototype.close=function(){this._close()},L.CONNECTING=0,L.OPEN=1,L.CLOSED=2,L.prototype.withCredentials=void 0;var q,X=l;null==s||null!=l&&"withCredentials"in l.prototype||(X=L),void 0!==(q=function(e){e.EventSourcePolyfill=L,e.NativeEventSource=l,e.EventSource=X}(n))&&(t.exports=q)}("undefined"==typeof globalThis?"undefined"!=typeof window?window:"undefined"!=typeof self?self:e:globalThis)}(t={exports:{}},t.exports),t.exports),r=Object.freeze(Object.assign(Object.create(null),n,{default:n}));const o=e=>e.data,s=e=>JSON.parse(e.data);class i{constructor(e){if(this._handlers={},this._listeners={},this._source=null,e.format?"string"==typeof e.format?"plain"===e.format?this._format=o:"json"===e.format?this._format=s:this._format=o:"function"==typeof e.format?this._format=e.format:this._format=o:this._format=o,e.handlers)for(const t in e.handlers)this.on(t,e.handlers[t]);this.url=e.url,this.withCredentials=!!e.withCredentials,this.polyfillOptions=e.polyfillOptions||{},this.forcePolyfill=!!e.forcePolyfill}get source(){return this._source}connect(){return this.forcePolyfill?this._source=n.EventSourcePolyfill(this.url,Object.assign({},this.config.polyfillOptions,{withCredentials:this.withCredentials})):this._source=new window.EventSource(this.url,{withCredentials:this.withCredentials}),new Promise(((e,t)=>{this._source.onopen=()=>{for(let e in this._listeners)this._source.addEventListener(e,this._listeners[e]);this._source.onerror=null,e(this)},this._source.onerror=t}))}disconnect(){null!==this._source&&(this._source.close(),this._source=null)}on(e,t){return e||(e="message"),this._listeners[e]||this._create(e),this._handlers[e].push(t),this}once(e,t){return this.on(e,(n=>{this.off(e,t),t(n)})),this}off(e,t){if(!this._handlers[e])return this;const n=this._handlers[e].indexOf(t);return-1===n||(this._handlers[e].splice(n,1),0===this._handlers[e].length&&(this._source.removeEventListener(e,this._listeners[e]),delete this._handlers[e],delete this._listeners[e])),this}_create(e){this._handlers[e]=[],this._listeners[e]=t=>{let n;try{n=this._format(t)}catch(e){return void("function"==typeof this._source.onerror&&this._source.onerror(e))}this._handlers[e].forEach((e=>e(n)))},this._source&&this._source.addEventListener(e,this._listeners[e])}}function a(e,t){e.$sse=e.prototype.$sse=new l(t),t&&t.polyfill&&Promise.resolve().then((function(){return r})),e.mixin({beforeCreate(){this.$options.sse&&this.$options.sse.cleanup&&(this.$sse=new l,this.$sse.$clients=[])},beforeDestroy(){null!==this.$sse.$clients&&(this.$sse.$clients.forEach((e=>e.disconnect())),this.$sse.$clients=[])}})}class l{constructor(e){this.$defaultConfig=Object.assign({format:o,sendCredentials:!1},e),this.$clients=null}create(e){let t;t="object"==typeof e?e:"string"==typeof e?{url:e}:{};const n=new i(Object.assign({},this.$defaultConfig,t));return null!==this.$clients&&this.$clients.push(n),n}}var c={install:a};export default c;export{l as SSEManager,a as install}; |
/*! | ||
* vue-sse v2.2.0 | ||
* vue-sse v2.3.0 | ||
* (c) 2021 James Churchard | ||
* @license MIT | ||
*/ | ||
var formatText = function (e) { return e.data; }; | ||
var formatJSON = function (e) { return JSON.parse(e.data); }; | ||
var SSEClient = function SSEClient(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
if (config.handlers) { | ||
for (var event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
}; | ||
var prototypeAccessors = { source: { configurable: true } }; | ||
prototypeAccessors.source.get = function () { | ||
return this._source; | ||
}; | ||
SSEClient.prototype.connect = function connect () { | ||
var this$1 = this; | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
this$1._source.onopen = function () { | ||
// Add event listeners that were added before we connected | ||
for (var event in this$1._listeners) { | ||
this$1._source.addEventListener(event, this$1._listeners[event]); | ||
} | ||
this$1._source.onerror = null; | ||
resolve(this$1); | ||
}; | ||
this$1._source.onerror = reject; | ||
}); | ||
}; | ||
SSEClient.prototype.disconnect = function disconnect () { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
}; | ||
SSEClient.prototype.on = function on (event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
this._handlers[event].push(handler); | ||
return this; | ||
}; | ||
SSEClient.prototype.once = function once (event, handler) { | ||
var this$1 = this; | ||
this.on(event, function (e) { | ||
this$1.off(event, handler); | ||
handler(e); | ||
}); | ||
return this; | ||
}; | ||
SSEClient.prototype.off = function off (event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
var idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
return this; | ||
}; | ||
SSEClient.prototype._create = function _create (event) { | ||
var this$1 = this; | ||
this._handlers[event] = []; | ||
this._listeners[event] = function (message) { | ||
var data; | ||
try { | ||
data = this$1._format(message); | ||
} catch (err) { | ||
if (typeof this$1._source.onerror === 'function') { | ||
this$1._source.onerror(err); | ||
} | ||
return; | ||
} | ||
this$1._handlers[event].forEach(function (handler) { return handler(data); }); | ||
}; | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
}; | ||
Object.defineProperties( SSEClient.prototype, prototypeAccessors ); | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate: function beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy: function beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach(function (c) { return c.disconnect(); }); | ||
this.$sse.$clients = []; | ||
} | ||
} | ||
}); | ||
} | ||
var SSEManager = function SSEManager(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config | ||
); | ||
this.$clients = null; | ||
}; | ||
SSEManager.prototype.create = function create (configOrURL) { | ||
var config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
var client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
return client; | ||
}; | ||
var index = { | ||
install: install, | ||
}; | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
@@ -1275,6 +1052,240 @@ | ||
var eventsource$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), eventsource, { | ||
'default': eventsource | ||
'default': eventsource | ||
})); | ||
var formatText = function (e) { return e.data; }; | ||
var formatJSON = function (e) { return JSON.parse(e.data); }; | ||
var SSEClient = function SSEClient(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
if (config.handlers) { | ||
for (var event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
this.polyfillOptions = config.polyfillOptions || {}; | ||
this.forcePolyfill = !!config.forcePolyfill; | ||
}; | ||
var prototypeAccessors = { source: { configurable: true } }; | ||
prototypeAccessors.source.get = function () { | ||
return this._source; | ||
}; | ||
SSEClient.prototype.connect = function connect () { | ||
var this$1 = this; | ||
if (this.forcePolyfill) { | ||
this._source = eventsource.EventSourcePolyfill( | ||
this.url, | ||
Object.assign({}, this.config.polyfillOptions, { | ||
withCredentials: this.withCredentials, | ||
}) | ||
); | ||
} else { | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
this$1._source.onopen = function () { | ||
// Add event listeners that were added before we connected | ||
for (var event in this$1._listeners) { | ||
this$1._source.addEventListener(event, this$1._listeners[event]); | ||
} | ||
this$1._source.onerror = null; | ||
resolve(this$1); | ||
}; | ||
this$1._source.onerror = reject; | ||
}); | ||
}; | ||
SSEClient.prototype.disconnect = function disconnect () { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
}; | ||
SSEClient.prototype.on = function on (event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
this._handlers[event].push(handler); | ||
return this; | ||
}; | ||
SSEClient.prototype.once = function once (event, handler) { | ||
var this$1 = this; | ||
this.on(event, function (e) { | ||
this$1.off(event, handler); | ||
handler(e); | ||
}); | ||
return this; | ||
}; | ||
SSEClient.prototype.off = function off (event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
var idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
return this; | ||
}; | ||
SSEClient.prototype._create = function _create (event) { | ||
var this$1 = this; | ||
this._handlers[event] = []; | ||
this._listeners[event] = function (message) { | ||
var data; | ||
try { | ||
data = this$1._format(message); | ||
} catch (err) { | ||
if (typeof this$1._source.onerror === 'function') { | ||
this$1._source.onerror(err); | ||
} | ||
return; | ||
} | ||
this$1._handlers[event].forEach(function (handler) { return handler(data); }); | ||
}; | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
}; | ||
Object.defineProperties( SSEClient.prototype, prototypeAccessors ); | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate: function beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy: function beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach(function (c) { return c.disconnect(); }); | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
}); | ||
} | ||
var SSEManager = function SSEManager(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config | ||
); | ||
this.$clients = null; | ||
}; | ||
SSEManager.prototype.create = function create (configOrURL) { | ||
var config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
var client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
return client; | ||
}; | ||
var index = { | ||
install: install, | ||
}; | ||
export default index; | ||
export { SSEManager, install }; |
2313
dist/vue-sse.js
/*! | ||
* vue-sse v2.2.0 | ||
* vue-sse v2.3.0 | ||
* (c) 2021 James Churchard | ||
@@ -7,1282 +7,1293 @@ * @license MIT | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.VueSSE = factory()); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.VueSSE = factory()); | ||
}(this, (function () { 'use strict'; | ||
var formatText = function (e) { return e.data; }; | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
var formatJSON = function (e) { return JSON.parse(e.data); }; | ||
function createCommonjsModule(fn) { | ||
var module = { exports: {} }; | ||
return fn(module, module.exports), module.exports; | ||
} | ||
var SSEClient = function SSEClient(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
/** @license | ||
* eventsource.js | ||
* Available under MIT License (MIT) | ||
* https://github.com/Yaffle/EventSource/ | ||
*/ | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
var eventsource = createCommonjsModule(function (module, exports) { | ||
/*jslint indent: 2, vars: true, plusplus: true */ | ||
/*global setTimeout, clearTimeout */ | ||
if (config.handlers) { | ||
for (var event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
(function (global) { | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
}; | ||
var setTimeout = global.setTimeout; | ||
var clearTimeout = global.clearTimeout; | ||
var XMLHttpRequest = global.XMLHttpRequest; | ||
var XDomainRequest = global.XDomainRequest; | ||
var ActiveXObject = global.ActiveXObject; | ||
var NativeEventSource = global.EventSource; | ||
var prototypeAccessors = { source: { configurable: true } }; | ||
var document = global.document; | ||
var Promise = global.Promise; | ||
var fetch = global.fetch; | ||
var Response = global.Response; | ||
var TextDecoder = global.TextDecoder; | ||
var TextEncoder = global.TextEncoder; | ||
var AbortController = global.AbortController; | ||
prototypeAccessors.source.get = function () { | ||
return this._source; | ||
}; | ||
if (typeof window !== "undefined" && !("readyState" in document) && document.body == null) { // Firefox 2 | ||
document.readyState = "loading"; | ||
window.addEventListener("load", function (event) { | ||
document.readyState = "complete"; | ||
}, false); | ||
} | ||
SSEClient.prototype.connect = function connect () { | ||
var this$1 = this; | ||
if (XMLHttpRequest == null && ActiveXObject != null) { // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest_in_IE6 | ||
XMLHttpRequest = function () { | ||
return new ActiveXObject("Microsoft.XMLHTTP"); | ||
}; | ||
} | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
if (Object.create == undefined) { | ||
Object.create = function (C) { | ||
function F(){} | ||
F.prototype = C; | ||
return new F(); | ||
}; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
this$1._source.onopen = function () { | ||
// Add event listeners that were added before we connected | ||
for (var event in this$1._listeners) { | ||
this$1._source.addEventListener(event, this$1._listeners[event]); | ||
} | ||
if (!Date.now) { | ||
Date.now = function now() { | ||
return new Date().getTime(); | ||
}; | ||
} | ||
this$1._source.onerror = null; | ||
// see #118 (Promise#finally with polyfilled Promise) | ||
// see #123 (data URLs crash Edge) | ||
// see #125 (CSP violations) | ||
// see pull/#138 | ||
// => No way to polyfill Promise#finally | ||
resolve(this$1); | ||
}; | ||
if (AbortController == undefined) { | ||
var originalFetch2 = fetch; | ||
fetch = function (url, options) { | ||
var signal = options.signal; | ||
return originalFetch2(url, {headers: options.headers, credentials: options.credentials, cache: options.cache}).then(function (response) { | ||
var reader = response.body.getReader(); | ||
signal._reader = reader; | ||
if (signal._aborted) { | ||
signal._reader.cancel(); | ||
} | ||
return { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: response.headers, | ||
body: { | ||
getReader: function () { | ||
return reader; | ||
} | ||
} | ||
}; | ||
}); | ||
}; | ||
AbortController = function () { | ||
this.signal = { | ||
_reader: null, | ||
_aborted: false | ||
}; | ||
this.abort = function () { | ||
if (this.signal._reader != null) { | ||
this.signal._reader.cancel(); | ||
} | ||
this.signal._aborted = true; | ||
}; | ||
}; | ||
} | ||
this$1._source.onerror = reject; | ||
}); | ||
}; | ||
function TextDecoderPolyfill() { | ||
this.bitsNeeded = 0; | ||
this.codePoint = 0; | ||
} | ||
SSEClient.prototype.disconnect = function disconnect () { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
}; | ||
TextDecoderPolyfill.prototype.decode = function (octets) { | ||
function valid(codePoint, shift, octetsCount) { | ||
if (octetsCount === 1) { | ||
return codePoint >= 0x0080 >> shift && codePoint << shift <= 0x07FF; | ||
} | ||
if (octetsCount === 2) { | ||
return codePoint >= 0x0800 >> shift && codePoint << shift <= 0xD7FF || codePoint >= 0xE000 >> shift && codePoint << shift <= 0xFFFF; | ||
} | ||
if (octetsCount === 3) { | ||
return codePoint >= 0x010000 >> shift && codePoint << shift <= 0x10FFFF; | ||
} | ||
throw new Error(); | ||
} | ||
function octetsCount(bitsNeeded, codePoint) { | ||
if (bitsNeeded === 6 * 1) { | ||
return codePoint >> 6 > 15 ? 3 : codePoint > 31 ? 2 : 1; | ||
} | ||
if (bitsNeeded === 6 * 2) { | ||
return codePoint > 15 ? 3 : 2; | ||
} | ||
if (bitsNeeded === 6 * 3) { | ||
return 3; | ||
} | ||
throw new Error(); | ||
} | ||
var REPLACER = 0xFFFD; | ||
var string = ""; | ||
var bitsNeeded = this.bitsNeeded; | ||
var codePoint = this.codePoint; | ||
for (var i = 0; i < octets.length; i += 1) { | ||
var octet = octets[i]; | ||
if (bitsNeeded !== 0) { | ||
if (octet < 128 || octet > 191 || !valid(codePoint << 6 | octet & 63, bitsNeeded - 6, octetsCount(bitsNeeded, codePoint))) { | ||
bitsNeeded = 0; | ||
codePoint = REPLACER; | ||
string += String.fromCharCode(codePoint); | ||
} | ||
} | ||
if (bitsNeeded === 0) { | ||
if (octet >= 0 && octet <= 127) { | ||
bitsNeeded = 0; | ||
codePoint = octet; | ||
} else if (octet >= 192 && octet <= 223) { | ||
bitsNeeded = 6 * 1; | ||
codePoint = octet & 31; | ||
} else if (octet >= 224 && octet <= 239) { | ||
bitsNeeded = 6 * 2; | ||
codePoint = octet & 15; | ||
} else if (octet >= 240 && octet <= 247) { | ||
bitsNeeded = 6 * 3; | ||
codePoint = octet & 7; | ||
} else { | ||
bitsNeeded = 0; | ||
codePoint = REPLACER; | ||
} | ||
if (bitsNeeded !== 0 && !valid(codePoint, bitsNeeded, octetsCount(bitsNeeded, codePoint))) { | ||
bitsNeeded = 0; | ||
codePoint = REPLACER; | ||
} | ||
} else { | ||
bitsNeeded -= 6; | ||
codePoint = codePoint << 6 | octet & 63; | ||
} | ||
if (bitsNeeded === 0) { | ||
if (codePoint <= 0xFFFF) { | ||
string += String.fromCharCode(codePoint); | ||
} else { | ||
string += String.fromCharCode(0xD800 + (codePoint - 0xFFFF - 1 >> 10)); | ||
string += String.fromCharCode(0xDC00 + (codePoint - 0xFFFF - 1 & 0x3FF)); | ||
} | ||
} | ||
} | ||
this.bitsNeeded = bitsNeeded; | ||
this.codePoint = codePoint; | ||
return string; | ||
}; | ||
SSEClient.prototype.on = function on (event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
// Firefox < 38 throws an error with stream option | ||
var supportsStreamOption = function () { | ||
try { | ||
return new TextDecoder().decode(new TextEncoder().encode("test"), {stream: true}) === "test"; | ||
} catch (error) { | ||
console.debug("TextDecoder does not support streaming option. Using polyfill instead: " + error); | ||
} | ||
return false; | ||
}; | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
// IE, Edge | ||
if (TextDecoder == undefined || TextEncoder == undefined || !supportsStreamOption()) { | ||
TextDecoder = TextDecoderPolyfill; | ||
} | ||
this._handlers[event].push(handler); | ||
var k = function () { | ||
}; | ||
return this; | ||
}; | ||
function XHRWrapper(xhr) { | ||
this.withCredentials = false; | ||
this.readyState = 0; | ||
this.status = 0; | ||
this.statusText = ""; | ||
this.responseText = ""; | ||
this.onprogress = k; | ||
this.onload = k; | ||
this.onerror = k; | ||
this.onreadystatechange = k; | ||
this._contentType = ""; | ||
this._xhr = xhr; | ||
this._sendTimeout = 0; | ||
this._abort = k; | ||
} | ||
SSEClient.prototype.once = function once (event, handler) { | ||
var this$1 = this; | ||
XHRWrapper.prototype.open = function (method, url) { | ||
this._abort(true); | ||
this.on(event, function (e) { | ||
this$1.off(event, handler); | ||
var that = this; | ||
var xhr = this._xhr; | ||
var state = 1; | ||
var timeout = 0; | ||
handler(e); | ||
}); | ||
this._abort = function (silent) { | ||
if (that._sendTimeout !== 0) { | ||
clearTimeout(that._sendTimeout); | ||
that._sendTimeout = 0; | ||
} | ||
if (state === 1 || state === 2 || state === 3) { | ||
state = 4; | ||
xhr.onload = k; | ||
xhr.onerror = k; | ||
xhr.onabort = k; | ||
xhr.onprogress = k; | ||
xhr.onreadystatechange = k; | ||
// IE 8 - 9: XDomainRequest#abort() does not fire any event | ||
// Opera < 10: XMLHttpRequest#abort() does not fire any event | ||
xhr.abort(); | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
if (!silent) { | ||
that.readyState = 4; | ||
that.onabort(null); | ||
that.onreadystatechange(); | ||
} | ||
} | ||
state = 0; | ||
}; | ||
return this; | ||
}; | ||
var onStart = function () { | ||
if (state === 1) { | ||
//state = 2; | ||
var status = 0; | ||
var statusText = ""; | ||
var contentType = undefined; | ||
if (!("contentType" in xhr)) { | ||
try { | ||
status = xhr.status; | ||
statusText = xhr.statusText; | ||
contentType = xhr.getResponseHeader("Content-Type"); | ||
} catch (error) { | ||
// IE < 10 throws exception for `xhr.status` when xhr.readyState === 2 || xhr.readyState === 3 | ||
// Opera < 11 throws exception for `xhr.status` when xhr.readyState === 2 | ||
// https://bugs.webkit.org/show_bug.cgi?id=29121 | ||
status = 0; | ||
statusText = ""; | ||
contentType = undefined; | ||
// Firefox < 14, Chrome ?, Safari ? | ||
// https://bugs.webkit.org/show_bug.cgi?id=29658 | ||
// https://bugs.webkit.org/show_bug.cgi?id=77854 | ||
} | ||
} else { | ||
status = 200; | ||
statusText = "OK"; | ||
contentType = xhr.contentType; | ||
} | ||
if (status !== 0) { | ||
state = 2; | ||
that.readyState = 2; | ||
that.status = status; | ||
that.statusText = statusText; | ||
that._contentType = contentType; | ||
that.onreadystatechange(); | ||
} | ||
} | ||
}; | ||
var onProgress = function () { | ||
onStart(); | ||
if (state === 2 || state === 3) { | ||
state = 3; | ||
var responseText = ""; | ||
try { | ||
responseText = xhr.responseText; | ||
} catch (error) { | ||
// IE 8 - 9 with XMLHttpRequest | ||
} | ||
that.readyState = 3; | ||
that.responseText = responseText; | ||
that.onprogress(); | ||
} | ||
}; | ||
var onFinish = function (type, event) { | ||
if (event == null || event.preventDefault == null) { | ||
event = { | ||
preventDefault: k | ||
}; | ||
} | ||
// Firefox 52 fires "readystatechange" (xhr.readyState === 4) without final "readystatechange" (xhr.readyState === 3) | ||
// IE 8 fires "onload" without "onprogress" | ||
onProgress(); | ||
if (state === 1 || state === 2 || state === 3) { | ||
state = 4; | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
that.readyState = 4; | ||
if (type === "load") { | ||
that.onload(event); | ||
} else if (type === "error") { | ||
that.onerror(event); | ||
} else if (type === "abort") { | ||
that.onabort(event); | ||
} else { | ||
throw new TypeError(); | ||
} | ||
that.onreadystatechange(); | ||
} | ||
}; | ||
var onReadyStateChange = function (event) { | ||
if (xhr != undefined) { // Opera 12 | ||
if (xhr.readyState === 4) { | ||
if (!("onload" in xhr) || !("onerror" in xhr) || !("onabort" in xhr)) { | ||
onFinish(xhr.responseText === "" ? "error" : "load", event); | ||
} | ||
} else if (xhr.readyState === 3) { | ||
if (!("onprogress" in xhr)) { // testing XMLHttpRequest#responseText too many times is too slow in IE 11 | ||
// and in Firefox 3.6 | ||
onProgress(); | ||
} | ||
} else if (xhr.readyState === 2) { | ||
onStart(); | ||
} | ||
} | ||
}; | ||
var onTimeout = function () { | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, 500); | ||
if (xhr.readyState === 3) { | ||
onProgress(); | ||
} | ||
}; | ||
SSEClient.prototype.off = function off (event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
// XDomainRequest#abort removes onprogress, onerror, onload | ||
if ("onload" in xhr) { | ||
xhr.onload = function (event) { | ||
onFinish("load", event); | ||
}; | ||
} | ||
if ("onerror" in xhr) { | ||
xhr.onerror = function (event) { | ||
onFinish("error", event); | ||
}; | ||
} | ||
// improper fix to match Firefox behaviour, but it is better than just ignore abort | ||
// see https://bugzilla.mozilla.org/show_bug.cgi?id=768596 | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=880200 | ||
// https://code.google.com/p/chromium/issues/detail?id=153570 | ||
// IE 8 fires "onload" without "onprogress | ||
if ("onabort" in xhr) { | ||
xhr.onabort = function (event) { | ||
onFinish("abort", event); | ||
}; | ||
} | ||
var idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
if ("onprogress" in xhr) { | ||
xhr.onprogress = onProgress; | ||
} | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
// IE 8 - 9 (XMLHTTPRequest) | ||
// Opera < 12 | ||
// Firefox < 3.5 | ||
// Firefox 3.5 - 3.6 - ? < 9.0 | ||
// onprogress is not fired sometimes or delayed | ||
// see also #64 (significant lag in IE 11) | ||
if ("onreadystatechange" in xhr) { | ||
xhr.onreadystatechange = function (event) { | ||
onReadyStateChange(event); | ||
}; | ||
} | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
if ("contentType" in xhr || !("ontimeout" in XMLHttpRequest.prototype)) { | ||
url += (url.indexOf("?") === -1 ? "?" : "&") + "padding=true"; | ||
} | ||
xhr.open(method, url, true); | ||
return this; | ||
}; | ||
if ("readyState" in xhr) { | ||
// workaround for Opera 12 issue with "progress" events | ||
// #91 (XMLHttpRequest onprogress not fired for streaming response in Edge 14-15-?) | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, 0); | ||
} | ||
}; | ||
XHRWrapper.prototype.abort = function () { | ||
this._abort(false); | ||
}; | ||
XHRWrapper.prototype.getResponseHeader = function (name) { | ||
return this._contentType; | ||
}; | ||
XHRWrapper.prototype.setRequestHeader = function (name, value) { | ||
var xhr = this._xhr; | ||
if ("setRequestHeader" in xhr) { | ||
xhr.setRequestHeader(name, value); | ||
} | ||
}; | ||
XHRWrapper.prototype.getAllResponseHeaders = function () { | ||
// XMLHttpRequest#getAllResponseHeaders returns null for CORS requests in Firefox 3.6.28 | ||
return this._xhr.getAllResponseHeaders != undefined ? this._xhr.getAllResponseHeaders() || "" : ""; | ||
}; | ||
XHRWrapper.prototype.send = function () { | ||
// loading indicator in Safari < ? (6), Chrome < 14, Firefox | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=736723 | ||
if ((!("ontimeout" in XMLHttpRequest.prototype) || (!("sendAsBinary" in XMLHttpRequest.prototype) && !("mozAnon" in XMLHttpRequest.prototype))) && | ||
document != undefined && | ||
document.readyState != undefined && | ||
document.readyState !== "complete") { | ||
var that = this; | ||
that._sendTimeout = setTimeout(function () { | ||
that._sendTimeout = 0; | ||
that.send(); | ||
}, 4); | ||
return; | ||
} | ||
SSEClient.prototype._create = function _create (event) { | ||
var this$1 = this; | ||
var xhr = this._xhr; | ||
// withCredentials should be set after "open" for Safari and Chrome (< 19 ?) | ||
if ("withCredentials" in xhr) { | ||
xhr.withCredentials = this.withCredentials; | ||
} | ||
try { | ||
// xhr.send(); throws "Not enough arguments" in Firefox 3.0 | ||
xhr.send(undefined); | ||
} catch (error1) { | ||
// Safari 5.1.7, Opera 12 | ||
throw error1; | ||
} | ||
}; | ||
this._handlers[event] = []; | ||
function toLowerCase(name) { | ||
return name.replace(/[A-Z]/g, function (c) { | ||
return String.fromCharCode(c.charCodeAt(0) + 0x20); | ||
}); | ||
} | ||
this._listeners[event] = function (message) { | ||
var data; | ||
function HeadersPolyfill(all) { | ||
// Get headers: implemented according to mozilla's example code: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#Example | ||
var map = Object.create(null); | ||
var array = all.split("\r\n"); | ||
for (var i = 0; i < array.length; i += 1) { | ||
var line = array[i]; | ||
var parts = line.split(": "); | ||
var name = parts.shift(); | ||
var value = parts.join(": "); | ||
map[toLowerCase(name)] = value; | ||
} | ||
this._map = map; | ||
} | ||
HeadersPolyfill.prototype.get = function (name) { | ||
return this._map[toLowerCase(name)]; | ||
}; | ||
try { | ||
data = this$1._format(message); | ||
} catch (err) { | ||
if (typeof this$1._source.onerror === 'function') { | ||
this$1._source.onerror(err); | ||
} | ||
return; | ||
} | ||
if (XMLHttpRequest != null && XMLHttpRequest.HEADERS_RECEIVED == null) { // IE < 9, Firefox 3.6 | ||
XMLHttpRequest.HEADERS_RECEIVED = 2; | ||
} | ||
this$1._handlers[event].forEach(function (handler) { return handler(data); }); | ||
}; | ||
function XHRTransport() { | ||
} | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
}; | ||
XHRTransport.prototype.open = function (xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers) { | ||
xhr.open("GET", url); | ||
var offset = 0; | ||
xhr.onprogress = function () { | ||
var responseText = xhr.responseText; | ||
var chunk = responseText.slice(offset); | ||
offset += chunk.length; | ||
onProgressCallback(chunk); | ||
}; | ||
xhr.onerror = function (event) { | ||
event.preventDefault(); | ||
onFinishCallback(new Error("NetworkError")); | ||
}; | ||
xhr.onload = function () { | ||
onFinishCallback(null); | ||
}; | ||
xhr.onabort = function () { | ||
onFinishCallback(null); | ||
}; | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) { | ||
var status = xhr.status; | ||
var statusText = xhr.statusText; | ||
var contentType = xhr.getResponseHeader("Content-Type"); | ||
var headers = xhr.getAllResponseHeaders(); | ||
onStartCallback(status, statusText, contentType, new HeadersPolyfill(headers)); | ||
} | ||
}; | ||
xhr.withCredentials = withCredentials; | ||
for (var name in headers) { | ||
if (Object.prototype.hasOwnProperty.call(headers, name)) { | ||
xhr.setRequestHeader(name, headers[name]); | ||
} | ||
} | ||
xhr.send(); | ||
return xhr; | ||
}; | ||
Object.defineProperties( SSEClient.prototype, prototypeAccessors ); | ||
function HeadersWrapper(headers) { | ||
this._headers = headers; | ||
} | ||
HeadersWrapper.prototype.get = function (name) { | ||
return this._headers.get(name); | ||
}; | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
function FetchTransport() { | ||
} | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
FetchTransport.prototype.open = function (xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers) { | ||
var reader = null; | ||
var controller = new AbortController(); | ||
var signal = controller.signal; | ||
var textDecoder = new TextDecoder(); | ||
fetch(url, { | ||
headers: headers, | ||
credentials: withCredentials ? "include" : "same-origin", | ||
signal: signal, | ||
cache: "no-store" | ||
}).then(function (response) { | ||
reader = response.body.getReader(); | ||
onStartCallback(response.status, response.statusText, response.headers.get("Content-Type"), new HeadersWrapper(response.headers)); | ||
// see https://github.com/promises-aplus/promises-spec/issues/179 | ||
return new Promise(function (resolve, reject) { | ||
var readNextChunk = function () { | ||
reader.read().then(function (result) { | ||
if (result.done) { | ||
//Note: bytes in textDecoder are ignored | ||
resolve(undefined); | ||
} else { | ||
var chunk = textDecoder.decode(result.value, {stream: true}); | ||
onProgressCallback(chunk); | ||
readNextChunk(); | ||
} | ||
})["catch"](function (error) { | ||
reject(error); | ||
}); | ||
}; | ||
readNextChunk(); | ||
}); | ||
})["catch"](function (error) { | ||
if (error.name === "AbortError") { | ||
return undefined; | ||
} else { | ||
return error; | ||
} | ||
}).then(function (error) { | ||
onFinishCallback(error); | ||
}); | ||
return { | ||
abort: function () { | ||
if (reader != null) { | ||
reader.cancel(); // https://bugzilla.mozilla.org/show_bug.cgi?id=1583815 | ||
} | ||
controller.abort(); | ||
} | ||
}; | ||
}; | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate: function beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
function EventTarget() { | ||
this._listeners = Object.create(null); | ||
} | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy: function beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach(function (c) { return c.disconnect(); }); | ||
this.$sse.$clients = []; | ||
} | ||
} | ||
}); | ||
} | ||
function throwError(e) { | ||
setTimeout(function () { | ||
throw e; | ||
}, 0); | ||
} | ||
var SSEManager = function SSEManager(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config | ||
); | ||
EventTarget.prototype.dispatchEvent = function (event) { | ||
event.target = this; | ||
var typeListeners = this._listeners[event.type]; | ||
if (typeListeners != undefined) { | ||
var length = typeListeners.length; | ||
for (var i = 0; i < length; i += 1) { | ||
var listener = typeListeners[i]; | ||
try { | ||
if (typeof listener.handleEvent === "function") { | ||
listener.handleEvent(event); | ||
} else { | ||
listener.call(this, event); | ||
} | ||
} catch (e) { | ||
throwError(e); | ||
} | ||
} | ||
} | ||
}; | ||
EventTarget.prototype.addEventListener = function (type, listener) { | ||
type = String(type); | ||
var listeners = this._listeners; | ||
var typeListeners = listeners[type]; | ||
if (typeListeners == undefined) { | ||
typeListeners = []; | ||
listeners[type] = typeListeners; | ||
} | ||
var found = false; | ||
for (var i = 0; i < typeListeners.length; i += 1) { | ||
if (typeListeners[i] === listener) { | ||
found = true; | ||
} | ||
} | ||
if (!found) { | ||
typeListeners.push(listener); | ||
} | ||
}; | ||
EventTarget.prototype.removeEventListener = function (type, listener) { | ||
type = String(type); | ||
var listeners = this._listeners; | ||
var typeListeners = listeners[type]; | ||
if (typeListeners != undefined) { | ||
var filtered = []; | ||
for (var i = 0; i < typeListeners.length; i += 1) { | ||
if (typeListeners[i] !== listener) { | ||
filtered.push(typeListeners[i]); | ||
} | ||
} | ||
if (filtered.length === 0) { | ||
delete listeners[type]; | ||
} else { | ||
listeners[type] = filtered; | ||
} | ||
} | ||
}; | ||
this.$clients = null; | ||
}; | ||
function Event(type) { | ||
this.type = type; | ||
this.target = undefined; | ||
} | ||
SSEManager.prototype.create = function create (configOrURL) { | ||
var config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
function MessageEvent(type, options) { | ||
Event.call(this, type); | ||
this.data = options.data; | ||
this.lastEventId = options.lastEventId; | ||
} | ||
var client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
MessageEvent.prototype = Object.create(Event.prototype); | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
function ConnectionEvent(type, options) { | ||
Event.call(this, type); | ||
this.status = options.status; | ||
this.statusText = options.statusText; | ||
this.headers = options.headers; | ||
} | ||
return client; | ||
}; | ||
ConnectionEvent.prototype = Object.create(Event.prototype); | ||
var index_cjs = { | ||
SSEManager: SSEManager, | ||
install: install, | ||
}; | ||
function ErrorEvent(type, options) { | ||
Event.call(this, type); | ||
this.error = options.error; | ||
} | ||
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
ErrorEvent.prototype = Object.create(Event.prototype); | ||
function createCommonjsModule(fn) { | ||
var module = { exports: {} }; | ||
return fn(module, module.exports), module.exports; | ||
} | ||
var WAITING = -1; | ||
var CONNECTING = 0; | ||
var OPEN = 1; | ||
var CLOSED = 2; | ||
/** @license | ||
* eventsource.js | ||
* Available under MIT License (MIT) | ||
* https://github.com/Yaffle/EventSource/ | ||
*/ | ||
var AFTER_CR = -1; | ||
var FIELD_START = 0; | ||
var FIELD = 1; | ||
var VALUE_START = 2; | ||
var VALUE = 3; | ||
var eventsource = createCommonjsModule(function (module, exports) { | ||
/*jslint indent: 2, vars: true, plusplus: true */ | ||
/*global setTimeout, clearTimeout */ | ||
var contentTypeRegExp = /^text\/event\-stream(;.*)?$/i; | ||
(function (global) { | ||
var MINIMUM_DURATION = 1000; | ||
var MAXIMUM_DURATION = 18000000; | ||
var setTimeout = global.setTimeout; | ||
var clearTimeout = global.clearTimeout; | ||
var XMLHttpRequest = global.XMLHttpRequest; | ||
var XDomainRequest = global.XDomainRequest; | ||
var ActiveXObject = global.ActiveXObject; | ||
var NativeEventSource = global.EventSource; | ||
var parseDuration = function (value, def) { | ||
var n = value == null ? def : parseInt(value, 10); | ||
if (n !== n) { | ||
n = def; | ||
} | ||
return clampDuration(n); | ||
}; | ||
var clampDuration = function (n) { | ||
return Math.min(Math.max(n, MINIMUM_DURATION), MAXIMUM_DURATION); | ||
}; | ||
var document = global.document; | ||
var Promise = global.Promise; | ||
var fetch = global.fetch; | ||
var Response = global.Response; | ||
var TextDecoder = global.TextDecoder; | ||
var TextEncoder = global.TextEncoder; | ||
var AbortController = global.AbortController; | ||
var fire = function (that, f, event) { | ||
try { | ||
if (typeof f === "function") { | ||
f.call(that, event); | ||
} | ||
} catch (e) { | ||
throwError(e); | ||
} | ||
}; | ||
if (typeof window !== "undefined" && !("readyState" in document) && document.body == null) { // Firefox 2 | ||
document.readyState = "loading"; | ||
window.addEventListener("load", function (event) { | ||
document.readyState = "complete"; | ||
}, false); | ||
} | ||
function EventSourcePolyfill(url, options) { | ||
EventTarget.call(this); | ||
options = options || {}; | ||
if (XMLHttpRequest == null && ActiveXObject != null) { // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest_in_IE6 | ||
XMLHttpRequest = function () { | ||
return new ActiveXObject("Microsoft.XMLHTTP"); | ||
}; | ||
} | ||
this.onopen = undefined; | ||
this.onmessage = undefined; | ||
this.onerror = undefined; | ||
if (Object.create == undefined) { | ||
Object.create = function (C) { | ||
function F(){} | ||
F.prototype = C; | ||
return new F(); | ||
}; | ||
} | ||
this.url = undefined; | ||
this.readyState = undefined; | ||
this.withCredentials = undefined; | ||
this.headers = undefined; | ||
if (!Date.now) { | ||
Date.now = function now() { | ||
return new Date().getTime(); | ||
}; | ||
} | ||
this._close = undefined; | ||
// see #118 (Promise#finally with polyfilled Promise) | ||
// see #123 (data URLs crash Edge) | ||
// see #125 (CSP violations) | ||
// see pull/#138 | ||
// => No way to polyfill Promise#finally | ||
start(this, url, options); | ||
} | ||
if (AbortController == undefined) { | ||
var originalFetch2 = fetch; | ||
fetch = function (url, options) { | ||
var signal = options.signal; | ||
return originalFetch2(url, {headers: options.headers, credentials: options.credentials, cache: options.cache}).then(function (response) { | ||
var reader = response.body.getReader(); | ||
signal._reader = reader; | ||
if (signal._aborted) { | ||
signal._reader.cancel(); | ||
} | ||
return { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: response.headers, | ||
body: { | ||
getReader: function () { | ||
return reader; | ||
} | ||
} | ||
}; | ||
}); | ||
}; | ||
AbortController = function () { | ||
this.signal = { | ||
_reader: null, | ||
_aborted: false | ||
}; | ||
this.abort = function () { | ||
if (this.signal._reader != null) { | ||
this.signal._reader.cancel(); | ||
} | ||
this.signal._aborted = true; | ||
}; | ||
}; | ||
} | ||
function getBestXHRTransport() { | ||
return (XMLHttpRequest != undefined && ("withCredentials" in XMLHttpRequest.prototype)) || XDomainRequest == undefined | ||
? new XMLHttpRequest() | ||
: new XDomainRequest(); | ||
} | ||
function TextDecoderPolyfill() { | ||
this.bitsNeeded = 0; | ||
this.codePoint = 0; | ||
} | ||
var isFetchSupported = fetch != undefined && Response != undefined && "body" in Response.prototype; | ||
TextDecoderPolyfill.prototype.decode = function (octets) { | ||
function valid(codePoint, shift, octetsCount) { | ||
if (octetsCount === 1) { | ||
return codePoint >= 0x0080 >> shift && codePoint << shift <= 0x07FF; | ||
} | ||
if (octetsCount === 2) { | ||
return codePoint >= 0x0800 >> shift && codePoint << shift <= 0xD7FF || codePoint >= 0xE000 >> shift && codePoint << shift <= 0xFFFF; | ||
} | ||
if (octetsCount === 3) { | ||
return codePoint >= 0x010000 >> shift && codePoint << shift <= 0x10FFFF; | ||
} | ||
throw new Error(); | ||
} | ||
function octetsCount(bitsNeeded, codePoint) { | ||
if (bitsNeeded === 6 * 1) { | ||
return codePoint >> 6 > 15 ? 3 : codePoint > 31 ? 2 : 1; | ||
} | ||
if (bitsNeeded === 6 * 2) { | ||
return codePoint > 15 ? 3 : 2; | ||
} | ||
if (bitsNeeded === 6 * 3) { | ||
return 3; | ||
} | ||
throw new Error(); | ||
} | ||
var REPLACER = 0xFFFD; | ||
var string = ""; | ||
var bitsNeeded = this.bitsNeeded; | ||
var codePoint = this.codePoint; | ||
for (var i = 0; i < octets.length; i += 1) { | ||
var octet = octets[i]; | ||
if (bitsNeeded !== 0) { | ||
if (octet < 128 || octet > 191 || !valid(codePoint << 6 | octet & 63, bitsNeeded - 6, octetsCount(bitsNeeded, codePoint))) { | ||
bitsNeeded = 0; | ||
codePoint = REPLACER; | ||
string += String.fromCharCode(codePoint); | ||
} | ||
} | ||
if (bitsNeeded === 0) { | ||
if (octet >= 0 && octet <= 127) { | ||
bitsNeeded = 0; | ||
codePoint = octet; | ||
} else if (octet >= 192 && octet <= 223) { | ||
bitsNeeded = 6 * 1; | ||
codePoint = octet & 31; | ||
} else if (octet >= 224 && octet <= 239) { | ||
bitsNeeded = 6 * 2; | ||
codePoint = octet & 15; | ||
} else if (octet >= 240 && octet <= 247) { | ||
bitsNeeded = 6 * 3; | ||
codePoint = octet & 7; | ||
} else { | ||
bitsNeeded = 0; | ||
codePoint = REPLACER; | ||
} | ||
if (bitsNeeded !== 0 && !valid(codePoint, bitsNeeded, octetsCount(bitsNeeded, codePoint))) { | ||
bitsNeeded = 0; | ||
codePoint = REPLACER; | ||
} | ||
} else { | ||
bitsNeeded -= 6; | ||
codePoint = codePoint << 6 | octet & 63; | ||
} | ||
if (bitsNeeded === 0) { | ||
if (codePoint <= 0xFFFF) { | ||
string += String.fromCharCode(codePoint); | ||
} else { | ||
string += String.fromCharCode(0xD800 + (codePoint - 0xFFFF - 1 >> 10)); | ||
string += String.fromCharCode(0xDC00 + (codePoint - 0xFFFF - 1 & 0x3FF)); | ||
} | ||
} | ||
} | ||
this.bitsNeeded = bitsNeeded; | ||
this.codePoint = codePoint; | ||
return string; | ||
}; | ||
function start(es, url, options) { | ||
url = String(url); | ||
var withCredentials = Boolean(options.withCredentials); | ||
var lastEventIdQueryParameterName = options.lastEventIdQueryParameterName || "lastEventId"; | ||
// Firefox < 38 throws an error with stream option | ||
var supportsStreamOption = function () { | ||
try { | ||
return new TextDecoder().decode(new TextEncoder().encode("test"), {stream: true}) === "test"; | ||
} catch (error) { | ||
console.debug("TextDecoder does not support streaming option. Using polyfill instead: " + error); | ||
} | ||
return false; | ||
}; | ||
var initialRetry = clampDuration(1000); | ||
var heartbeatTimeout = parseDuration(options.heartbeatTimeout, 45000); | ||
// IE, Edge | ||
if (TextDecoder == undefined || TextEncoder == undefined || !supportsStreamOption()) { | ||
TextDecoder = TextDecoderPolyfill; | ||
} | ||
var lastEventId = ""; | ||
var retry = initialRetry; | ||
var wasActivity = false; | ||
var textLength = 0; | ||
var headers = options.headers || {}; | ||
var TransportOption = options.Transport; | ||
var xhr = isFetchSupported && TransportOption == undefined ? undefined : new XHRWrapper(TransportOption != undefined ? new TransportOption() : getBestXHRTransport()); | ||
var transport = TransportOption != null && typeof TransportOption !== "string" ? new TransportOption() : (xhr == undefined ? new FetchTransport() : new XHRTransport()); | ||
var abortController = undefined; | ||
var timeout = 0; | ||
var currentState = WAITING; | ||
var dataBuffer = ""; | ||
var lastEventIdBuffer = ""; | ||
var eventTypeBuffer = ""; | ||
var k = function () { | ||
}; | ||
var textBuffer = ""; | ||
var state = FIELD_START; | ||
var fieldStart = 0; | ||
var valueStart = 0; | ||
function XHRWrapper(xhr) { | ||
this.withCredentials = false; | ||
this.readyState = 0; | ||
this.status = 0; | ||
this.statusText = ""; | ||
this.responseText = ""; | ||
this.onprogress = k; | ||
this.onload = k; | ||
this.onerror = k; | ||
this.onreadystatechange = k; | ||
this._contentType = ""; | ||
this._xhr = xhr; | ||
this._sendTimeout = 0; | ||
this._abort = k; | ||
} | ||
var onStart = function (status, statusText, contentType, headers) { | ||
if (currentState === CONNECTING) { | ||
if (status === 200 && contentType != undefined && contentTypeRegExp.test(contentType)) { | ||
currentState = OPEN; | ||
wasActivity = Date.now(); | ||
retry = initialRetry; | ||
es.readyState = OPEN; | ||
var event = new ConnectionEvent("open", { | ||
status: status, | ||
statusText: statusText, | ||
headers: headers | ||
}); | ||
es.dispatchEvent(event); | ||
fire(es, es.onopen, event); | ||
} else { | ||
var message = ""; | ||
if (status !== 200) { | ||
if (statusText) { | ||
statusText = statusText.replace(/\s+/g, " "); | ||
} | ||
message = "EventSource's response has a status " + status + " " + statusText + " that is not 200. Aborting the connection."; | ||
} else { | ||
message = "EventSource's response has a Content-Type specifying an unsupported type: " + (contentType == undefined ? "-" : contentType.replace(/\s+/g, " ")) + ". Aborting the connection."; | ||
} | ||
close(); | ||
var event = new ConnectionEvent("error", { | ||
status: status, | ||
statusText: statusText, | ||
headers: headers | ||
}); | ||
es.dispatchEvent(event); | ||
fire(es, es.onerror, event); | ||
console.error(message); | ||
} | ||
} | ||
}; | ||
XHRWrapper.prototype.open = function (method, url) { | ||
this._abort(true); | ||
var onProgress = function (textChunk) { | ||
if (currentState === OPEN) { | ||
var n = -1; | ||
for (var i = 0; i < textChunk.length; i += 1) { | ||
var c = textChunk.charCodeAt(i); | ||
if (c === "\n".charCodeAt(0) || c === "\r".charCodeAt(0)) { | ||
n = i; | ||
} | ||
} | ||
var chunk = (n !== -1 ? textBuffer : "") + textChunk.slice(0, n + 1); | ||
textBuffer = (n === -1 ? textBuffer : "") + textChunk.slice(n + 1); | ||
if (textChunk !== "") { | ||
wasActivity = Date.now(); | ||
textLength += textChunk.length; | ||
} | ||
for (var position = 0; position < chunk.length; position += 1) { | ||
var c = chunk.charCodeAt(position); | ||
if (state === AFTER_CR && c === "\n".charCodeAt(0)) { | ||
state = FIELD_START; | ||
} else { | ||
if (state === AFTER_CR) { | ||
state = FIELD_START; | ||
} | ||
if (c === "\r".charCodeAt(0) || c === "\n".charCodeAt(0)) { | ||
if (state !== FIELD_START) { | ||
if (state === FIELD) { | ||
valueStart = position + 1; | ||
} | ||
var field = chunk.slice(fieldStart, valueStart - 1); | ||
var value = chunk.slice(valueStart + (valueStart < position && chunk.charCodeAt(valueStart) === " ".charCodeAt(0) ? 1 : 0), position); | ||
if (field === "data") { | ||
dataBuffer += "\n"; | ||
dataBuffer += value; | ||
} else if (field === "id") { | ||
lastEventIdBuffer = value; | ||
} else if (field === "event") { | ||
eventTypeBuffer = value; | ||
} else if (field === "retry") { | ||
initialRetry = parseDuration(value, initialRetry); | ||
retry = initialRetry; | ||
} else if (field === "heartbeatTimeout") { | ||
heartbeatTimeout = parseDuration(value, heartbeatTimeout); | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, heartbeatTimeout); | ||
} | ||
} | ||
} | ||
if (state === FIELD_START) { | ||
if (dataBuffer !== "") { | ||
lastEventId = lastEventIdBuffer; | ||
if (eventTypeBuffer === "") { | ||
eventTypeBuffer = "message"; | ||
} | ||
var event = new MessageEvent(eventTypeBuffer, { | ||
data: dataBuffer.slice(1), | ||
lastEventId: lastEventIdBuffer | ||
}); | ||
es.dispatchEvent(event); | ||
if (eventTypeBuffer === "open") { | ||
fire(es, es.onopen, event); | ||
} else if (eventTypeBuffer === "message") { | ||
fire(es, es.onmessage, event); | ||
} else if (eventTypeBuffer === "error") { | ||
fire(es, es.onerror, event); | ||
} | ||
if (currentState === CLOSED) { | ||
return; | ||
} | ||
} | ||
dataBuffer = ""; | ||
eventTypeBuffer = ""; | ||
} | ||
state = c === "\r".charCodeAt(0) ? AFTER_CR : FIELD_START; | ||
} else { | ||
if (state === FIELD_START) { | ||
fieldStart = position; | ||
state = FIELD; | ||
} | ||
if (state === FIELD) { | ||
if (c === ":".charCodeAt(0)) { | ||
valueStart = position + 1; | ||
state = VALUE_START; | ||
} | ||
} else if (state === VALUE_START) { | ||
state = VALUE; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var that = this; | ||
var xhr = this._xhr; | ||
var state = 1; | ||
var timeout = 0; | ||
var onFinish = function (error) { | ||
if (currentState === OPEN || currentState === CONNECTING) { | ||
currentState = WAITING; | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, retry); | ||
retry = clampDuration(Math.min(initialRetry * 16, retry * 2)); | ||
this._abort = function (silent) { | ||
if (that._sendTimeout !== 0) { | ||
clearTimeout(that._sendTimeout); | ||
that._sendTimeout = 0; | ||
} | ||
if (state === 1 || state === 2 || state === 3) { | ||
state = 4; | ||
xhr.onload = k; | ||
xhr.onerror = k; | ||
xhr.onabort = k; | ||
xhr.onprogress = k; | ||
xhr.onreadystatechange = k; | ||
// IE 8 - 9: XDomainRequest#abort() does not fire any event | ||
// Opera < 10: XMLHttpRequest#abort() does not fire any event | ||
xhr.abort(); | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
if (!silent) { | ||
that.readyState = 4; | ||
that.onabort(null); | ||
that.onreadystatechange(); | ||
} | ||
} | ||
state = 0; | ||
}; | ||
es.readyState = CONNECTING; | ||
var event = new ErrorEvent("error", {error: error}); | ||
es.dispatchEvent(event); | ||
fire(es, es.onerror, event); | ||
if (error != undefined) { | ||
console.error(error); | ||
} | ||
} | ||
}; | ||
var onStart = function () { | ||
if (state === 1) { | ||
//state = 2; | ||
var status = 0; | ||
var statusText = ""; | ||
var contentType = undefined; | ||
if (!("contentType" in xhr)) { | ||
try { | ||
status = xhr.status; | ||
statusText = xhr.statusText; | ||
contentType = xhr.getResponseHeader("Content-Type"); | ||
} catch (error) { | ||
// IE < 10 throws exception for `xhr.status` when xhr.readyState === 2 || xhr.readyState === 3 | ||
// Opera < 11 throws exception for `xhr.status` when xhr.readyState === 2 | ||
// https://bugs.webkit.org/show_bug.cgi?id=29121 | ||
status = 0; | ||
statusText = ""; | ||
contentType = undefined; | ||
// Firefox < 14, Chrome ?, Safari ? | ||
// https://bugs.webkit.org/show_bug.cgi?id=29658 | ||
// https://bugs.webkit.org/show_bug.cgi?id=77854 | ||
} | ||
} else { | ||
status = 200; | ||
statusText = "OK"; | ||
contentType = xhr.contentType; | ||
} | ||
if (status !== 0) { | ||
state = 2; | ||
that.readyState = 2; | ||
that.status = status; | ||
that.statusText = statusText; | ||
that._contentType = contentType; | ||
that.onreadystatechange(); | ||
} | ||
} | ||
}; | ||
var onProgress = function () { | ||
onStart(); | ||
if (state === 2 || state === 3) { | ||
state = 3; | ||
var responseText = ""; | ||
try { | ||
responseText = xhr.responseText; | ||
} catch (error) { | ||
// IE 8 - 9 with XMLHttpRequest | ||
} | ||
that.readyState = 3; | ||
that.responseText = responseText; | ||
that.onprogress(); | ||
} | ||
}; | ||
var onFinish = function (type, event) { | ||
if (event == null || event.preventDefault == null) { | ||
event = { | ||
preventDefault: k | ||
}; | ||
} | ||
// Firefox 52 fires "readystatechange" (xhr.readyState === 4) without final "readystatechange" (xhr.readyState === 3) | ||
// IE 8 fires "onload" without "onprogress" | ||
onProgress(); | ||
if (state === 1 || state === 2 || state === 3) { | ||
state = 4; | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
that.readyState = 4; | ||
if (type === "load") { | ||
that.onload(event); | ||
} else if (type === "error") { | ||
that.onerror(event); | ||
} else if (type === "abort") { | ||
that.onabort(event); | ||
} else { | ||
throw new TypeError(); | ||
} | ||
that.onreadystatechange(); | ||
} | ||
}; | ||
var onReadyStateChange = function (event) { | ||
if (xhr != undefined) { // Opera 12 | ||
if (xhr.readyState === 4) { | ||
if (!("onload" in xhr) || !("onerror" in xhr) || !("onabort" in xhr)) { | ||
onFinish(xhr.responseText === "" ? "error" : "load", event); | ||
} | ||
} else if (xhr.readyState === 3) { | ||
if (!("onprogress" in xhr)) { // testing XMLHttpRequest#responseText too many times is too slow in IE 11 | ||
// and in Firefox 3.6 | ||
onProgress(); | ||
} | ||
} else if (xhr.readyState === 2) { | ||
onStart(); | ||
} | ||
} | ||
}; | ||
var onTimeout = function () { | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, 500); | ||
if (xhr.readyState === 3) { | ||
onProgress(); | ||
} | ||
}; | ||
var close = function () { | ||
currentState = CLOSED; | ||
if (abortController != undefined) { | ||
abortController.abort(); | ||
abortController = undefined; | ||
} | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
es.readyState = CLOSED; | ||
}; | ||
// XDomainRequest#abort removes onprogress, onerror, onload | ||
if ("onload" in xhr) { | ||
xhr.onload = function (event) { | ||
onFinish("load", event); | ||
}; | ||
} | ||
if ("onerror" in xhr) { | ||
xhr.onerror = function (event) { | ||
onFinish("error", event); | ||
}; | ||
} | ||
// improper fix to match Firefox behaviour, but it is better than just ignore abort | ||
// see https://bugzilla.mozilla.org/show_bug.cgi?id=768596 | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=880200 | ||
// https://code.google.com/p/chromium/issues/detail?id=153570 | ||
// IE 8 fires "onload" without "onprogress | ||
if ("onabort" in xhr) { | ||
xhr.onabort = function (event) { | ||
onFinish("abort", event); | ||
}; | ||
} | ||
var onTimeout = function () { | ||
timeout = 0; | ||
if ("onprogress" in xhr) { | ||
xhr.onprogress = onProgress; | ||
} | ||
if (currentState !== WAITING) { | ||
if (!wasActivity && abortController != undefined) { | ||
onFinish(new Error("No activity within " + heartbeatTimeout + " milliseconds." + " " + (currentState === CONNECTING ? "No response received." : textLength + " chars received.") + " " + "Reconnecting.")); | ||
if (abortController != undefined) { | ||
abortController.abort(); | ||
abortController = undefined; | ||
} | ||
} else { | ||
var nextHeartbeat = Math.max((wasActivity || Date.now()) + heartbeatTimeout - Date.now(), 1); | ||
wasActivity = false; | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, nextHeartbeat); | ||
} | ||
return; | ||
} | ||
// IE 8 - 9 (XMLHTTPRequest) | ||
// Opera < 12 | ||
// Firefox < 3.5 | ||
// Firefox 3.5 - 3.6 - ? < 9.0 | ||
// onprogress is not fired sometimes or delayed | ||
// see also #64 (significant lag in IE 11) | ||
if ("onreadystatechange" in xhr) { | ||
xhr.onreadystatechange = function (event) { | ||
onReadyStateChange(event); | ||
}; | ||
} | ||
wasActivity = false; | ||
textLength = 0; | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, heartbeatTimeout); | ||
if ("contentType" in xhr || !("ontimeout" in XMLHttpRequest.prototype)) { | ||
url += (url.indexOf("?") === -1 ? "?" : "&") + "padding=true"; | ||
} | ||
xhr.open(method, url, true); | ||
currentState = CONNECTING; | ||
dataBuffer = ""; | ||
eventTypeBuffer = ""; | ||
lastEventIdBuffer = lastEventId; | ||
textBuffer = ""; | ||
fieldStart = 0; | ||
valueStart = 0; | ||
state = FIELD_START; | ||
if ("readyState" in xhr) { | ||
// workaround for Opera 12 issue with "progress" events | ||
// #91 (XMLHttpRequest onprogress not fired for streaming response in Edge 14-15-?) | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, 0); | ||
} | ||
}; | ||
XHRWrapper.prototype.abort = function () { | ||
this._abort(false); | ||
}; | ||
XHRWrapper.prototype.getResponseHeader = function (name) { | ||
return this._contentType; | ||
}; | ||
XHRWrapper.prototype.setRequestHeader = function (name, value) { | ||
var xhr = this._xhr; | ||
if ("setRequestHeader" in xhr) { | ||
xhr.setRequestHeader(name, value); | ||
} | ||
}; | ||
XHRWrapper.prototype.getAllResponseHeaders = function () { | ||
// XMLHttpRequest#getAllResponseHeaders returns null for CORS requests in Firefox 3.6.28 | ||
return this._xhr.getAllResponseHeaders != undefined ? this._xhr.getAllResponseHeaders() || "" : ""; | ||
}; | ||
XHRWrapper.prototype.send = function () { | ||
// loading indicator in Safari < ? (6), Chrome < 14, Firefox | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=736723 | ||
if ((!("ontimeout" in XMLHttpRequest.prototype) || (!("sendAsBinary" in XMLHttpRequest.prototype) && !("mozAnon" in XMLHttpRequest.prototype))) && | ||
document != undefined && | ||
document.readyState != undefined && | ||
document.readyState !== "complete") { | ||
var that = this; | ||
that._sendTimeout = setTimeout(function () { | ||
that._sendTimeout = 0; | ||
that.send(); | ||
}, 4); | ||
return; | ||
} | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=428916 | ||
// Request header field Last-Event-ID is not allowed by Access-Control-Allow-Headers. | ||
var requestURL = url; | ||
if (url.slice(0, 5) !== "data:" && url.slice(0, 5) !== "blob:") { | ||
if (lastEventId !== "") { | ||
requestURL += (url.indexOf("?") === -1 ? "?" : "&") + lastEventIdQueryParameterName +"=" + encodeURIComponent(lastEventId); | ||
} | ||
} | ||
var withCredentials = es.withCredentials; | ||
var requestHeaders = {}; | ||
requestHeaders["Accept"] = "text/event-stream"; | ||
var headers = es.headers; | ||
if (headers != undefined) { | ||
for (var name in headers) { | ||
if (Object.prototype.hasOwnProperty.call(headers, name)) { | ||
requestHeaders[name] = headers[name]; | ||
} | ||
} | ||
} | ||
try { | ||
abortController = transport.open(xhr, onStart, onProgress, onFinish, requestURL, withCredentials, requestHeaders); | ||
} catch (error) { | ||
close(); | ||
throw error; | ||
} | ||
}; | ||
var xhr = this._xhr; | ||
// withCredentials should be set after "open" for Safari and Chrome (< 19 ?) | ||
if ("withCredentials" in xhr) { | ||
xhr.withCredentials = this.withCredentials; | ||
} | ||
try { | ||
// xhr.send(); throws "Not enough arguments" in Firefox 3.0 | ||
xhr.send(undefined); | ||
} catch (error1) { | ||
// Safari 5.1.7, Opera 12 | ||
throw error1; | ||
} | ||
}; | ||
es.url = url; | ||
es.readyState = CONNECTING; | ||
es.withCredentials = withCredentials; | ||
es.headers = headers; | ||
es._close = close; | ||
function toLowerCase(name) { | ||
return name.replace(/[A-Z]/g, function (c) { | ||
return String.fromCharCode(c.charCodeAt(0) + 0x20); | ||
}); | ||
} | ||
onTimeout(); | ||
} | ||
function HeadersPolyfill(all) { | ||
// Get headers: implemented according to mozilla's example code: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#Example | ||
var map = Object.create(null); | ||
var array = all.split("\r\n"); | ||
for (var i = 0; i < array.length; i += 1) { | ||
var line = array[i]; | ||
var parts = line.split(": "); | ||
var name = parts.shift(); | ||
var value = parts.join(": "); | ||
map[toLowerCase(name)] = value; | ||
} | ||
this._map = map; | ||
} | ||
HeadersPolyfill.prototype.get = function (name) { | ||
return this._map[toLowerCase(name)]; | ||
}; | ||
EventSourcePolyfill.prototype = Object.create(EventTarget.prototype); | ||
EventSourcePolyfill.prototype.CONNECTING = CONNECTING; | ||
EventSourcePolyfill.prototype.OPEN = OPEN; | ||
EventSourcePolyfill.prototype.CLOSED = CLOSED; | ||
EventSourcePolyfill.prototype.close = function () { | ||
this._close(); | ||
}; | ||
if (XMLHttpRequest != null && XMLHttpRequest.HEADERS_RECEIVED == null) { // IE < 9, Firefox 3.6 | ||
XMLHttpRequest.HEADERS_RECEIVED = 2; | ||
} | ||
EventSourcePolyfill.CONNECTING = CONNECTING; | ||
EventSourcePolyfill.OPEN = OPEN; | ||
EventSourcePolyfill.CLOSED = CLOSED; | ||
EventSourcePolyfill.prototype.withCredentials = undefined; | ||
function XHRTransport() { | ||
} | ||
var R = NativeEventSource; | ||
if (XMLHttpRequest != undefined && (NativeEventSource == undefined || !("withCredentials" in NativeEventSource.prototype))) { | ||
// Why replace a native EventSource ? | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=444328 | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=831392 | ||
// https://code.google.com/p/chromium/issues/detail?id=260144 | ||
// https://code.google.com/p/chromium/issues/detail?id=225654 | ||
// ... | ||
R = EventSourcePolyfill; | ||
} | ||
XHRTransport.prototype.open = function (xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers) { | ||
xhr.open("GET", url); | ||
var offset = 0; | ||
xhr.onprogress = function () { | ||
var responseText = xhr.responseText; | ||
var chunk = responseText.slice(offset); | ||
offset += chunk.length; | ||
onProgressCallback(chunk); | ||
}; | ||
xhr.onerror = function (event) { | ||
event.preventDefault(); | ||
onFinishCallback(new Error("NetworkError")); | ||
}; | ||
xhr.onload = function () { | ||
onFinishCallback(null); | ||
}; | ||
xhr.onabort = function () { | ||
onFinishCallback(null); | ||
}; | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) { | ||
var status = xhr.status; | ||
var statusText = xhr.statusText; | ||
var contentType = xhr.getResponseHeader("Content-Type"); | ||
var headers = xhr.getAllResponseHeaders(); | ||
onStartCallback(status, statusText, contentType, new HeadersPolyfill(headers)); | ||
} | ||
}; | ||
xhr.withCredentials = withCredentials; | ||
for (var name in headers) { | ||
if (Object.prototype.hasOwnProperty.call(headers, name)) { | ||
xhr.setRequestHeader(name, headers[name]); | ||
} | ||
} | ||
xhr.send(); | ||
return xhr; | ||
}; | ||
(function (factory) { | ||
{ | ||
var v = factory(exports); | ||
if (v !== undefined) { module.exports = v; } | ||
} | ||
})(function (exports) { | ||
exports.EventSourcePolyfill = EventSourcePolyfill; | ||
exports.NativeEventSource = NativeEventSource; | ||
exports.EventSource = R; | ||
}); | ||
}(typeof globalThis === 'undefined' ? (typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : commonjsGlobal) : globalThis)); | ||
}); | ||
function HeadersWrapper(headers) { | ||
this._headers = headers; | ||
} | ||
HeadersWrapper.prototype.get = function (name) { | ||
return this._headers.get(name); | ||
}; | ||
var eventsource$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), eventsource, { | ||
'default': eventsource | ||
})); | ||
function FetchTransport() { | ||
} | ||
var formatText = function (e) { return e.data; }; | ||
FetchTransport.prototype.open = function (xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers) { | ||
var reader = null; | ||
var controller = new AbortController(); | ||
var signal = controller.signal; | ||
var textDecoder = new TextDecoder(); | ||
fetch(url, { | ||
headers: headers, | ||
credentials: withCredentials ? "include" : "same-origin", | ||
signal: signal, | ||
cache: "no-store" | ||
}).then(function (response) { | ||
reader = response.body.getReader(); | ||
onStartCallback(response.status, response.statusText, response.headers.get("Content-Type"), new HeadersWrapper(response.headers)); | ||
// see https://github.com/promises-aplus/promises-spec/issues/179 | ||
return new Promise(function (resolve, reject) { | ||
var readNextChunk = function () { | ||
reader.read().then(function (result) { | ||
if (result.done) { | ||
//Note: bytes in textDecoder are ignored | ||
resolve(undefined); | ||
} else { | ||
var chunk = textDecoder.decode(result.value, {stream: true}); | ||
onProgressCallback(chunk); | ||
readNextChunk(); | ||
} | ||
})["catch"](function (error) { | ||
reject(error); | ||
}); | ||
}; | ||
readNextChunk(); | ||
}); | ||
})["catch"](function (error) { | ||
if (error.name === "AbortError") { | ||
return undefined; | ||
} else { | ||
return error; | ||
} | ||
}).then(function (error) { | ||
onFinishCallback(error); | ||
}); | ||
return { | ||
abort: function () { | ||
if (reader != null) { | ||
reader.cancel(); // https://bugzilla.mozilla.org/show_bug.cgi?id=1583815 | ||
} | ||
controller.abort(); | ||
} | ||
}; | ||
}; | ||
var formatJSON = function (e) { return JSON.parse(e.data); }; | ||
function EventTarget() { | ||
this._listeners = Object.create(null); | ||
} | ||
var SSEClient = function SSEClient(config) { | ||
this._handlers = {}; | ||
this._listeners = {}; | ||
this._source = null; | ||
function throwError(e) { | ||
setTimeout(function () { | ||
throw e; | ||
}, 0); | ||
} | ||
if (config.format) { | ||
if (typeof config.format === 'string') { | ||
if (config.format === 'plain') { | ||
this._format = formatText; | ||
} else if (config.format === 'json') { | ||
this._format = formatJSON; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else if (typeof config.format === 'function') { | ||
this._format = config.format; | ||
} else { | ||
this._format = formatText; | ||
} | ||
} else { | ||
this._format = formatText; | ||
} | ||
EventTarget.prototype.dispatchEvent = function (event) { | ||
event.target = this; | ||
var typeListeners = this._listeners[event.type]; | ||
if (typeListeners != undefined) { | ||
var length = typeListeners.length; | ||
for (var i = 0; i < length; i += 1) { | ||
var listener = typeListeners[i]; | ||
try { | ||
if (typeof listener.handleEvent === "function") { | ||
listener.handleEvent(event); | ||
} else { | ||
listener.call(this, event); | ||
} | ||
} catch (e) { | ||
throwError(e); | ||
} | ||
} | ||
} | ||
}; | ||
EventTarget.prototype.addEventListener = function (type, listener) { | ||
type = String(type); | ||
var listeners = this._listeners; | ||
var typeListeners = listeners[type]; | ||
if (typeListeners == undefined) { | ||
typeListeners = []; | ||
listeners[type] = typeListeners; | ||
} | ||
var found = false; | ||
for (var i = 0; i < typeListeners.length; i += 1) { | ||
if (typeListeners[i] === listener) { | ||
found = true; | ||
} | ||
} | ||
if (!found) { | ||
typeListeners.push(listener); | ||
} | ||
}; | ||
EventTarget.prototype.removeEventListener = function (type, listener) { | ||
type = String(type); | ||
var listeners = this._listeners; | ||
var typeListeners = listeners[type]; | ||
if (typeListeners != undefined) { | ||
var filtered = []; | ||
for (var i = 0; i < typeListeners.length; i += 1) { | ||
if (typeListeners[i] !== listener) { | ||
filtered.push(typeListeners[i]); | ||
} | ||
} | ||
if (filtered.length === 0) { | ||
delete listeners[type]; | ||
} else { | ||
listeners[type] = filtered; | ||
} | ||
} | ||
}; | ||
if (config.handlers) { | ||
for (var event in config.handlers) { | ||
this.on(event, config.handlers[event]); | ||
} | ||
} | ||
function Event(type) { | ||
this.type = type; | ||
this.target = undefined; | ||
} | ||
this.url = config.url; | ||
this.withCredentials = !!config.withCredentials; | ||
this.polyfillOptions = config.polyfillOptions || {}; | ||
this.forcePolyfill = !!config.forcePolyfill; | ||
}; | ||
function MessageEvent(type, options) { | ||
Event.call(this, type); | ||
this.data = options.data; | ||
this.lastEventId = options.lastEventId; | ||
} | ||
var prototypeAccessors = { source: { configurable: true } }; | ||
MessageEvent.prototype = Object.create(Event.prototype); | ||
prototypeAccessors.source.get = function () { | ||
return this._source; | ||
}; | ||
function ConnectionEvent(type, options) { | ||
Event.call(this, type); | ||
this.status = options.status; | ||
this.statusText = options.statusText; | ||
this.headers = options.headers; | ||
} | ||
SSEClient.prototype.connect = function connect () { | ||
var this$1 = this; | ||
ConnectionEvent.prototype = Object.create(Event.prototype); | ||
if (this.forcePolyfill) { | ||
this._source = eventsource.EventSourcePolyfill( | ||
this.url, | ||
Object.assign({}, this.config.polyfillOptions, { | ||
withCredentials: this.withCredentials, | ||
}) | ||
); | ||
} else { | ||
this._source = new window.EventSource(this.url, { | ||
withCredentials: this.withCredentials, | ||
}); | ||
} | ||
function ErrorEvent(type, options) { | ||
Event.call(this, type); | ||
this.error = options.error; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
this$1._source.onopen = function () { | ||
// Add event listeners that were added before we connected | ||
for (var event in this$1._listeners) { | ||
this$1._source.addEventListener(event, this$1._listeners[event]); | ||
} | ||
ErrorEvent.prototype = Object.create(Event.prototype); | ||
this$1._source.onerror = null; | ||
var WAITING = -1; | ||
var CONNECTING = 0; | ||
var OPEN = 1; | ||
var CLOSED = 2; | ||
resolve(this$1); | ||
}; | ||
var AFTER_CR = -1; | ||
var FIELD_START = 0; | ||
var FIELD = 1; | ||
var VALUE_START = 2; | ||
var VALUE = 3; | ||
this$1._source.onerror = reject; | ||
}); | ||
}; | ||
var contentTypeRegExp = /^text\/event\-stream(;.*)?$/i; | ||
SSEClient.prototype.disconnect = function disconnect () { | ||
if (this._source !== null) { | ||
this._source.close(); | ||
this._source = null; | ||
} | ||
}; | ||
var MINIMUM_DURATION = 1000; | ||
var MAXIMUM_DURATION = 18000000; | ||
SSEClient.prototype.on = function on (event, handler) { | ||
if (!event) { | ||
// Default "event-less" event | ||
event = 'message'; | ||
} | ||
var parseDuration = function (value, def) { | ||
var n = value == null ? def : parseInt(value, 10); | ||
if (n !== n) { | ||
n = def; | ||
} | ||
return clampDuration(n); | ||
}; | ||
var clampDuration = function (n) { | ||
return Math.min(Math.max(n, MINIMUM_DURATION), MAXIMUM_DURATION); | ||
}; | ||
if (!this._listeners[event]) { | ||
this._create(event); | ||
} | ||
var fire = function (that, f, event) { | ||
try { | ||
if (typeof f === "function") { | ||
f.call(that, event); | ||
} | ||
} catch (e) { | ||
throwError(e); | ||
} | ||
}; | ||
this._handlers[event].push(handler); | ||
function EventSourcePolyfill(url, options) { | ||
EventTarget.call(this); | ||
options = options || {}; | ||
return this; | ||
}; | ||
this.onopen = undefined; | ||
this.onmessage = undefined; | ||
this.onerror = undefined; | ||
SSEClient.prototype.once = function once (event, handler) { | ||
var this$1 = this; | ||
this.url = undefined; | ||
this.readyState = undefined; | ||
this.withCredentials = undefined; | ||
this.headers = undefined; | ||
this.on(event, function (e) { | ||
this$1.off(event, handler); | ||
this._close = undefined; | ||
handler(e); | ||
}); | ||
start(this, url, options); | ||
} | ||
return this; | ||
}; | ||
function getBestXHRTransport() { | ||
return (XMLHttpRequest != undefined && ("withCredentials" in XMLHttpRequest.prototype)) || XDomainRequest == undefined | ||
? new XMLHttpRequest() | ||
: new XDomainRequest(); | ||
} | ||
SSEClient.prototype.off = function off (event, handler) { | ||
if (!this._handlers[event]) { | ||
// no handlers registered for event | ||
return this; | ||
} | ||
var isFetchSupported = fetch != undefined && Response != undefined && "body" in Response.prototype; | ||
var idx = this._handlers[event].indexOf(handler); | ||
if (idx === -1) { | ||
// handler not registered for event | ||
return this; | ||
} | ||
function start(es, url, options) { | ||
url = String(url); | ||
var withCredentials = Boolean(options.withCredentials); | ||
var lastEventIdQueryParameterName = options.lastEventIdQueryParameterName || "lastEventId"; | ||
// remove handler from event | ||
this._handlers[event].splice(idx, 1); | ||
var initialRetry = clampDuration(1000); | ||
var heartbeatTimeout = parseDuration(options.heartbeatTimeout, 45000); | ||
if (this._handlers[event].length === 0) { | ||
// remove listener since no handlers exist | ||
this._source.removeEventListener(event, this._listeners[event]); | ||
delete this._handlers[event]; | ||
delete this._listeners[event]; | ||
} | ||
var lastEventId = ""; | ||
var retry = initialRetry; | ||
var wasActivity = false; | ||
var textLength = 0; | ||
var headers = options.headers || {}; | ||
var TransportOption = options.Transport; | ||
var xhr = isFetchSupported && TransportOption == undefined ? undefined : new XHRWrapper(TransportOption != undefined ? new TransportOption() : getBestXHRTransport()); | ||
var transport = TransportOption != null && typeof TransportOption !== "string" ? new TransportOption() : (xhr == undefined ? new FetchTransport() : new XHRTransport()); | ||
var abortController = undefined; | ||
var timeout = 0; | ||
var currentState = WAITING; | ||
var dataBuffer = ""; | ||
var lastEventIdBuffer = ""; | ||
var eventTypeBuffer = ""; | ||
return this; | ||
}; | ||
var textBuffer = ""; | ||
var state = FIELD_START; | ||
var fieldStart = 0; | ||
var valueStart = 0; | ||
SSEClient.prototype._create = function _create (event) { | ||
var this$1 = this; | ||
var onStart = function (status, statusText, contentType, headers) { | ||
if (currentState === CONNECTING) { | ||
if (status === 200 && contentType != undefined && contentTypeRegExp.test(contentType)) { | ||
currentState = OPEN; | ||
wasActivity = Date.now(); | ||
retry = initialRetry; | ||
es.readyState = OPEN; | ||
var event = new ConnectionEvent("open", { | ||
status: status, | ||
statusText: statusText, | ||
headers: headers | ||
}); | ||
es.dispatchEvent(event); | ||
fire(es, es.onopen, event); | ||
} else { | ||
var message = ""; | ||
if (status !== 200) { | ||
if (statusText) { | ||
statusText = statusText.replace(/\s+/g, " "); | ||
} | ||
message = "EventSource's response has a status " + status + " " + statusText + " that is not 200. Aborting the connection."; | ||
} else { | ||
message = "EventSource's response has a Content-Type specifying an unsupported type: " + (contentType == undefined ? "-" : contentType.replace(/\s+/g, " ")) + ". Aborting the connection."; | ||
} | ||
close(); | ||
var event = new ConnectionEvent("error", { | ||
status: status, | ||
statusText: statusText, | ||
headers: headers | ||
}); | ||
es.dispatchEvent(event); | ||
fire(es, es.onerror, event); | ||
console.error(message); | ||
} | ||
} | ||
}; | ||
this._handlers[event] = []; | ||
var onProgress = function (textChunk) { | ||
if (currentState === OPEN) { | ||
var n = -1; | ||
for (var i = 0; i < textChunk.length; i += 1) { | ||
var c = textChunk.charCodeAt(i); | ||
if (c === "\n".charCodeAt(0) || c === "\r".charCodeAt(0)) { | ||
n = i; | ||
} | ||
} | ||
var chunk = (n !== -1 ? textBuffer : "") + textChunk.slice(0, n + 1); | ||
textBuffer = (n === -1 ? textBuffer : "") + textChunk.slice(n + 1); | ||
if (textChunk !== "") { | ||
wasActivity = Date.now(); | ||
textLength += textChunk.length; | ||
} | ||
for (var position = 0; position < chunk.length; position += 1) { | ||
var c = chunk.charCodeAt(position); | ||
if (state === AFTER_CR && c === "\n".charCodeAt(0)) { | ||
state = FIELD_START; | ||
} else { | ||
if (state === AFTER_CR) { | ||
state = FIELD_START; | ||
} | ||
if (c === "\r".charCodeAt(0) || c === "\n".charCodeAt(0)) { | ||
if (state !== FIELD_START) { | ||
if (state === FIELD) { | ||
valueStart = position + 1; | ||
} | ||
var field = chunk.slice(fieldStart, valueStart - 1); | ||
var value = chunk.slice(valueStart + (valueStart < position && chunk.charCodeAt(valueStart) === " ".charCodeAt(0) ? 1 : 0), position); | ||
if (field === "data") { | ||
dataBuffer += "\n"; | ||
dataBuffer += value; | ||
} else if (field === "id") { | ||
lastEventIdBuffer = value; | ||
} else if (field === "event") { | ||
eventTypeBuffer = value; | ||
} else if (field === "retry") { | ||
initialRetry = parseDuration(value, initialRetry); | ||
retry = initialRetry; | ||
} else if (field === "heartbeatTimeout") { | ||
heartbeatTimeout = parseDuration(value, heartbeatTimeout); | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, heartbeatTimeout); | ||
} | ||
} | ||
} | ||
if (state === FIELD_START) { | ||
if (dataBuffer !== "") { | ||
lastEventId = lastEventIdBuffer; | ||
if (eventTypeBuffer === "") { | ||
eventTypeBuffer = "message"; | ||
} | ||
var event = new MessageEvent(eventTypeBuffer, { | ||
data: dataBuffer.slice(1), | ||
lastEventId: lastEventIdBuffer | ||
}); | ||
es.dispatchEvent(event); | ||
if (eventTypeBuffer === "open") { | ||
fire(es, es.onopen, event); | ||
} else if (eventTypeBuffer === "message") { | ||
fire(es, es.onmessage, event); | ||
} else if (eventTypeBuffer === "error") { | ||
fire(es, es.onerror, event); | ||
} | ||
if (currentState === CLOSED) { | ||
return; | ||
} | ||
} | ||
dataBuffer = ""; | ||
eventTypeBuffer = ""; | ||
} | ||
state = c === "\r".charCodeAt(0) ? AFTER_CR : FIELD_START; | ||
} else { | ||
if (state === FIELD_START) { | ||
fieldStart = position; | ||
state = FIELD; | ||
} | ||
if (state === FIELD) { | ||
if (c === ":".charCodeAt(0)) { | ||
valueStart = position + 1; | ||
state = VALUE_START; | ||
} | ||
} else if (state === VALUE_START) { | ||
state = VALUE; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
this._listeners[event] = function (message) { | ||
var data; | ||
var onFinish = function (error) { | ||
if (currentState === OPEN || currentState === CONNECTING) { | ||
currentState = WAITING; | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, retry); | ||
retry = clampDuration(Math.min(initialRetry * 16, retry * 2)); | ||
try { | ||
data = this$1._format(message); | ||
} catch (err) { | ||
if (typeof this$1._source.onerror === 'function') { | ||
this$1._source.onerror(err); | ||
} | ||
return; | ||
} | ||
es.readyState = CONNECTING; | ||
var event = new ErrorEvent("error", {error: error}); | ||
es.dispatchEvent(event); | ||
fire(es, es.onerror, event); | ||
if (error != undefined) { | ||
console.error(error); | ||
} | ||
} | ||
}; | ||
this$1._handlers[event].forEach(function (handler) { return handler(data); }); | ||
}; | ||
var close = function () { | ||
currentState = CLOSED; | ||
if (abortController != undefined) { | ||
abortController.abort(); | ||
abortController = undefined; | ||
} | ||
if (timeout !== 0) { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
} | ||
es.readyState = CLOSED; | ||
}; | ||
if (this._source) { | ||
this._source.addEventListener(event, this._listeners[event]); | ||
} | ||
}; | ||
var onTimeout = function () { | ||
timeout = 0; | ||
Object.defineProperties( SSEClient.prototype, prototypeAccessors ); | ||
if (currentState !== WAITING) { | ||
if (!wasActivity && abortController != undefined) { | ||
onFinish(new Error("No activity within " + heartbeatTimeout + " milliseconds." + " " + (currentState === CONNECTING ? "No response received." : textLength + " chars received.") + " " + "Reconnecting.")); | ||
if (abortController != undefined) { | ||
abortController.abort(); | ||
abortController = undefined; | ||
} | ||
} else { | ||
var nextHeartbeat = Math.max((wasActivity || Date.now()) + heartbeatTimeout - Date.now(), 1); | ||
wasActivity = false; | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, nextHeartbeat); | ||
} | ||
return; | ||
} | ||
function install(Vue, config) { | ||
// eslint-disable-next-line no-param-reassign, no-multi-assign | ||
Vue.$sse = Vue.prototype.$sse = new SSEManager(config); | ||
wasActivity = false; | ||
textLength = 0; | ||
timeout = setTimeout(function () { | ||
onTimeout(); | ||
}, heartbeatTimeout); | ||
if (config && config.polyfill) { | ||
Promise.resolve().then(function () { return eventsource$1; }); | ||
} | ||
currentState = CONNECTING; | ||
dataBuffer = ""; | ||
eventTypeBuffer = ""; | ||
lastEventIdBuffer = lastEventId; | ||
textBuffer = ""; | ||
fieldStart = 0; | ||
valueStart = 0; | ||
state = FIELD_START; | ||
// This mixin allows components to specify that all clients that were | ||
// created within it should be automatically disconnected (cleanup) | ||
// when the component is destroyed. | ||
Vue.mixin({ | ||
beforeCreate: function beforeCreate() { | ||
if (this.$options.sse && this.$options.sse.cleanup) { | ||
// We instantiate an SSEManager for this specific instance | ||
// in order to track it (see discussions in #13 for rationale). | ||
this.$sse = new SSEManager(); | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=428916 | ||
// Request header field Last-Event-ID is not allowed by Access-Control-Allow-Headers. | ||
var requestURL = url; | ||
if (url.slice(0, 5) !== "data:" && url.slice(0, 5) !== "blob:") { | ||
if (lastEventId !== "") { | ||
requestURL += (url.indexOf("?") === -1 ? "?" : "&") + lastEventIdQueryParameterName +"=" + encodeURIComponent(lastEventId); | ||
} | ||
} | ||
var withCredentials = es.withCredentials; | ||
var requestHeaders = {}; | ||
requestHeaders["Accept"] = "text/event-stream"; | ||
var headers = es.headers; | ||
if (headers != undefined) { | ||
for (var name in headers) { | ||
if (Object.prototype.hasOwnProperty.call(headers, name)) { | ||
requestHeaders[name] = headers[name]; | ||
} | ||
} | ||
} | ||
try { | ||
abortController = transport.open(xhr, onStart, onProgress, onFinish, requestURL, withCredentials, requestHeaders); | ||
} catch (error) { | ||
close(); | ||
throw error; | ||
} | ||
}; | ||
// We also set $clients to an empty array, as opposed to null, | ||
// so that beforeDestroy and create know to use it. | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
beforeDestroy: function beforeDestroy() { | ||
if (this.$sse.$clients !== null) { | ||
this.$sse.$clients.forEach(function (c) { return c.disconnect(); }); | ||
this.$sse.$clients = []; | ||
} | ||
}, | ||
}); | ||
} | ||
es.url = url; | ||
es.readyState = CONNECTING; | ||
es.withCredentials = withCredentials; | ||
es.headers = headers; | ||
es._close = close; | ||
var SSEManager = function SSEManager(config) { | ||
this.$defaultConfig = Object.assign( | ||
{ | ||
format: formatText, | ||
sendCredentials: false, | ||
}, | ||
config | ||
); | ||
onTimeout(); | ||
} | ||
this.$clients = null; | ||
}; | ||
EventSourcePolyfill.prototype = Object.create(EventTarget.prototype); | ||
EventSourcePolyfill.prototype.CONNECTING = CONNECTING; | ||
EventSourcePolyfill.prototype.OPEN = OPEN; | ||
EventSourcePolyfill.prototype.CLOSED = CLOSED; | ||
EventSourcePolyfill.prototype.close = function () { | ||
this._close(); | ||
}; | ||
SSEManager.prototype.create = function create (configOrURL) { | ||
var config; | ||
if (typeof configOrURL === 'object') { | ||
config = configOrURL; | ||
} else if (typeof configOrURL === 'string') { | ||
config = { | ||
url: configOrURL, | ||
}; | ||
} else { | ||
config = {}; | ||
} | ||
EventSourcePolyfill.CONNECTING = CONNECTING; | ||
EventSourcePolyfill.OPEN = OPEN; | ||
EventSourcePolyfill.CLOSED = CLOSED; | ||
EventSourcePolyfill.prototype.withCredentials = undefined; | ||
var client = new SSEClient(Object.assign({}, this.$defaultConfig, config)); | ||
var R = NativeEventSource; | ||
if (XMLHttpRequest != undefined && (NativeEventSource == undefined || !("withCredentials" in NativeEventSource.prototype))) { | ||
// Why replace a native EventSource ? | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=444328 | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=831392 | ||
// https://code.google.com/p/chromium/issues/detail?id=260144 | ||
// https://code.google.com/p/chromium/issues/detail?id=225654 | ||
// ... | ||
R = EventSourcePolyfill; | ||
} | ||
// If $clients is not null, then it's array that we should push this | ||
// client into for later cleanup in our mixin's beforeDestroy. | ||
if (this.$clients !== null) { | ||
this.$clients.push(client); | ||
} | ||
(function (factory) { | ||
{ | ||
var v = factory(exports); | ||
if (v !== undefined) { module.exports = v; } | ||
} | ||
})(function (exports) { | ||
exports.EventSourcePolyfill = EventSourcePolyfill; | ||
exports.NativeEventSource = NativeEventSource; | ||
exports.EventSource = R; | ||
}); | ||
}(typeof globalThis === 'undefined' ? (typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : commonjsGlobal) : globalThis)); | ||
}); | ||
return client; | ||
}; | ||
var eventsource$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), eventsource, { | ||
'default': eventsource | ||
})); | ||
var index_cjs = { | ||
SSEManager: SSEManager, | ||
install: install, | ||
}; | ||
return index_cjs; | ||
return index_cjs; | ||
}))); |
/*! | ||
* vue-sse v2.2.0 | ||
* vue-sse v2.3.0 | ||
* (c) 2021 James Churchard | ||
* @license MIT | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).VueSSE=t()}(this,(function(){"use strict";var e=function(e){return e.data},t=function(e){return JSON.parse(e.data)},n=function(n){if(this._handlers={},this._listeners={},this._source=null,n.format?"string"==typeof n.format?"plain"===n.format?this._format=e:"json"===n.format?this._format=t:this._format=e:"function"==typeof n.format?this._format=n.format:this._format=e:this._format=e,n.handlers)for(var r in n.handlers)this.on(r,n.handlers[r]);this.url=n.url,this.withCredentials=!!n.withCredentials},r={source:{configurable:!0}};r.source.get=function(){return this._source},n.prototype.connect=function(){var e=this;return this._source=new window.EventSource(this.url,{withCredentials:this.withCredentials}),new Promise((function(t,n){e._source.onopen=function(){for(var n in e._listeners)e._source.addEventListener(n,e._listeners[n]);e._source.onerror=null,t(e)},e._source.onerror=n}))},n.prototype.disconnect=function(){null!==this._source&&(this._source.close(),this._source=null)},n.prototype.on=function(e,t){return e||(e="message"),this._listeners[e]||this._create(e),this._handlers[e].push(t),this},n.prototype.once=function(e,t){var n=this;return this.on(e,(function(r){n.off(e,t),t(r)})),this},n.prototype.off=function(e,t){if(!this._handlers[e])return this;var n=this._handlers[e].indexOf(t);return-1===n||(this._handlers[e].splice(n,1),0===this._handlers[e].length&&(this._source.removeEventListener(e,this._listeners[e]),delete this._handlers[e],delete this._listeners[e])),this},n.prototype._create=function(e){var t=this;this._handlers[e]=[],this._listeners[e]=function(n){var r;try{r=t._format(n)}catch(e){return void("function"==typeof t._source.onerror&&t._source.onerror(e))}t._handlers[e].forEach((function(e){return e(r)}))},this._source&&this._source.addEventListener(e,this._listeners[e])},Object.defineProperties(n.prototype,r);var o=function(t){this.$defaultConfig=Object.assign({format:e,sendCredentials:!1},t),this.$clients=null};o.prototype.create=function(e){var t;t="object"==typeof e?e:"string"==typeof e?{url:e}:{};var r=new n(Object.assign({},this.$defaultConfig,t));return null!==this.$clients&&this.$clients.push(r),r};var i={SSEManager:o,install:function(e,t){e.$sse=e.prototype.$sse=new o(t),t&&t.polyfill&&Promise.resolve().then((function(){return u})),e.mixin({beforeCreate:function(){this.$options.sse&&this.$options.sse.cleanup&&(this.$sse=new o,this.$sse.$clients=[])},beforeDestroy:function(){null!==this.$sse.$clients&&(this.$sse.$clients.forEach((function(e){return e.disconnect()})),this.$sse.$clients=[])}})}},s="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{}; | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).VueSSE=t()}(this,(function(){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{}; | ||
/** @license | ||
* eventsource.js | ||
* Available under MIT License (MIT) | ||
* https://github.com/Yaffle/EventSource/ | ||
*/ | ||
var a=function(e){var t={exports:{}};return e(t,t.exports),t.exports}((function(e,t){!function(n){var r=n.setTimeout,o=n.clearTimeout,i=n.XMLHttpRequest,s=n.XDomainRequest,a=n.ActiveXObject,u=n.EventSource,c=n.document,l=n.Promise,d=n.fetch,h=n.Response,f=n.TextDecoder,p=n.TextEncoder,y=n.AbortController;if("undefined"==typeof window||"readyState"in c||null!=c.body||(c.readyState="loading",window.addEventListener("load",(function(e){c.readyState="complete"}),!1)),null==i&&null!=a&&(i=function(){return new a("Microsoft.XMLHTTP")}),null==Object.create&&(Object.create=function(e){function t(){}return t.prototype=e,new t}),Date.now||(Date.now=function(){return(new Date).getTime()}),null==y){var v=d;d=function(e,t){var n=t.signal;return v(e,{headers:t.headers,credentials:t.credentials,cache:t.cache}).then((function(e){var t=e.body.getReader();return n._reader=t,n._aborted&&n._reader.cancel(),{status:e.status,statusText:e.statusText,headers:e.headers,body:{getReader:function(){return t}}}}))},y=function(){this.signal={_reader:null,_aborted:!1},this.abort=function(){null!=this.signal._reader&&this.signal._reader.cancel(),this.signal._aborted=!0}}}function g(){this.bitsNeeded=0,this.codePoint=0}g.prototype.decode=function(e){function t(e,t,n){if(1===n)return e>=128>>t&&e<<t<=2047;if(2===n)return e>=2048>>t&&e<<t<=55295||e>=57344>>t&&e<<t<=65535;if(3===n)return e>=65536>>t&&e<<t<=1114111;throw new Error}function n(e,t){if(6===e)return t>>6>15?3:t>31?2:1;if(12===e)return t>15?3:2;if(18===e)return 3;throw new Error}for(var r=65533,o="",i=this.bitsNeeded,s=this.codePoint,a=0;a<e.length;a+=1){var u=e[a];0!==i&&(u<128||u>191||!t(s<<6|63&u,i-6,n(i,s)))&&(i=0,s=r,o+=String.fromCharCode(s)),0===i?(u>=0&&u<=127?(i=0,s=u):u>=192&&u<=223?(i=6,s=31&u):u>=224&&u<=239?(i=12,s=15&u):u>=240&&u<=247?(i=18,s=7&u):(i=0,s=r),0===i||t(s,i,n(i,s))||(i=0,s=r)):(i-=6,s=s<<6|63&u),0===i&&(s<=65535?o+=String.fromCharCode(s):(o+=String.fromCharCode(55296+(s-65535-1>>10)),o+=String.fromCharCode(56320+(s-65535-1&1023))))}return this.bitsNeeded=i,this.codePoint=s,o};null!=f&&null!=p&&function(){try{return"test"===(new f).decode((new p).encode("test"),{stream:!0})}catch(e){console.debug("TextDecoder does not support streaming option. Using polyfill instead: "+e)}return!1}()||(f=g);var _=function(){};function w(e){this.withCredentials=!1,this.readyState=0,this.status=0,this.statusText="",this.responseText="",this.onprogress=_,this.onload=_,this.onerror=_,this.onreadystatechange=_,this._contentType="",this._xhr=e,this._sendTimeout=0,this._abort=_}function b(e){return e.replace(/[A-Z]/g,(function(e){return String.fromCharCode(e.charCodeAt(0)+32)}))}function m(e){for(var t=Object.create(null),n=e.split("\r\n"),r=0;r<n.length;r+=1){var o=n[r].split(": "),i=o.shift(),s=o.join(": ");t[b(i)]=s}this._map=t}function E(){}function C(e){this._headers=e}function T(){}function S(){this._listeners=Object.create(null)}function x(e){r((function(){throw e}),0)}function O(e){this.type=e,this.target=void 0}function A(e,t){O.call(this,e),this.data=t.data,this.lastEventId=t.lastEventId}function R(e,t){O.call(this,e),this.status=t.status,this.statusText=t.statusText,this.headers=t.headers}function D(e,t){O.call(this,e),this.error=t.error}w.prototype.open=function(e,t){this._abort(!0);var n=this,s=this._xhr,a=1,u=0;this._abort=function(e){0!==n._sendTimeout&&(o(n._sendTimeout),n._sendTimeout=0),1!==a&&2!==a&&3!==a||(a=4,s.onload=_,s.onerror=_,s.onabort=_,s.onprogress=_,s.onreadystatechange=_,s.abort(),0!==u&&(o(u),u=0),e||(n.readyState=4,n.onabort(null),n.onreadystatechange())),a=0};var c=function(){if(1===a){var e=0,t="",r=void 0;if("contentType"in s)e=200,t="OK",r=s.contentType;else try{e=s.status,t=s.statusText,r=s.getResponseHeader("Content-Type")}catch(n){e=0,t="",r=void 0}0!==e&&(a=2,n.readyState=2,n.status=e,n.statusText=t,n._contentType=r,n.onreadystatechange())}},l=function(){if(c(),2===a||3===a){a=3;var e="";try{e=s.responseText}catch(e){}n.readyState=3,n.responseText=e,n.onprogress()}},d=function(e,t){if(null!=t&&null!=t.preventDefault||(t={preventDefault:_}),l(),1===a||2===a||3===a){if(a=4,0!==u&&(o(u),u=0),n.readyState=4,"load"===e)n.onload(t);else if("error"===e)n.onerror(t);else{if("abort"!==e)throw new TypeError;n.onabort(t)}n.onreadystatechange()}},h=function(){u=r((function(){h()}),500),3===s.readyState&&l()};"onload"in s&&(s.onload=function(e){d("load",e)}),"onerror"in s&&(s.onerror=function(e){d("error",e)}),"onabort"in s&&(s.onabort=function(e){d("abort",e)}),"onprogress"in s&&(s.onprogress=l),"onreadystatechange"in s&&(s.onreadystatechange=function(e){!function(e){null!=s&&(4===s.readyState?"onload"in s&&"onerror"in s&&"onabort"in s||d(""===s.responseText?"error":"load",e):3===s.readyState?"onprogress"in s||l():2===s.readyState&&c())}(e)}),!("contentType"in s)&&"ontimeout"in i.prototype||(t+=(-1===t.indexOf("?")?"?":"&")+"padding=true"),s.open(e,t,!0),"readyState"in s&&(u=r((function(){h()}),0))},w.prototype.abort=function(){this._abort(!1)},w.prototype.getResponseHeader=function(e){return this._contentType},w.prototype.setRequestHeader=function(e,t){var n=this._xhr;"setRequestHeader"in n&&n.setRequestHeader(e,t)},w.prototype.getAllResponseHeaders=function(){return null!=this._xhr.getAllResponseHeaders&&this._xhr.getAllResponseHeaders()||""},w.prototype.send=function(){if("ontimeout"in i.prototype&&("sendAsBinary"in i.prototype||"mozAnon"in i.prototype)||null==c||null==c.readyState||"complete"===c.readyState){var e=this._xhr;"withCredentials"in e&&(e.withCredentials=this.withCredentials);try{e.send(void 0)}catch(e){throw e}}else{var t=this;t._sendTimeout=r((function(){t._sendTimeout=0,t.send()}),4)}},m.prototype.get=function(e){return this._map[b(e)]},null!=i&&null==i.HEADERS_RECEIVED&&(i.HEADERS_RECEIVED=2),E.prototype.open=function(e,t,n,r,o,s,a){e.open("GET",o);var u=0;for(var c in e.onprogress=function(){var t=e.responseText.slice(u);u+=t.length,n(t)},e.onerror=function(e){e.preventDefault(),r(new Error("NetworkError"))},e.onload=function(){r(null)},e.onabort=function(){r(null)},e.onreadystatechange=function(){if(e.readyState===i.HEADERS_RECEIVED){var n=e.status,r=e.statusText,o=e.getResponseHeader("Content-Type"),s=e.getAllResponseHeaders();t(n,r,o,new m(s))}},e.withCredentials=s,a)Object.prototype.hasOwnProperty.call(a,c)&&e.setRequestHeader(c,a[c]);return e.send(),e},C.prototype.get=function(e){return this._headers.get(e)},T.prototype.open=function(e,t,n,r,o,i,s){var a=null,u=new y,c=u.signal,h=new f;return d(o,{headers:s,credentials:i?"include":"same-origin",signal:c,cache:"no-store"}).then((function(e){return a=e.body.getReader(),t(e.status,e.statusText,e.headers.get("Content-Type"),new C(e.headers)),new l((function(e,t){var r=function(){a.read().then((function(t){if(t.done)e(void 0);else{var o=h.decode(t.value,{stream:!0});n(o),r()}})).catch((function(e){t(e)}))};r()}))})).catch((function(e){return"AbortError"===e.name?void 0:e})).then((function(e){r(e)})),{abort:function(){null!=a&&a.cancel(),u.abort()}}},S.prototype.dispatchEvent=function(e){e.target=this;var t=this._listeners[e.type];if(null!=t)for(var n=t.length,r=0;r<n;r+=1){var o=t[r];try{"function"==typeof o.handleEvent?o.handleEvent(e):o.call(this,e)}catch(e){x(e)}}},S.prototype.addEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];null==r&&(r=[],n[e]=r);for(var o=!1,i=0;i<r.length;i+=1)r[i]===t&&(o=!0);o||r.push(t)},S.prototype.removeEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];if(null!=r){for(var o=[],i=0;i<r.length;i+=1)r[i]!==t&&o.push(r[i]);0===o.length?delete n[e]:n[e]=o}},A.prototype=Object.create(O.prototype),R.prototype=Object.create(O.prototype),D.prototype=Object.create(O.prototype);var j=-1,$=-1,N=/^text\/event\-stream(;.*)?$/i,H=function(e,t){var n=null==e?t:parseInt(e,10);return n!=n&&(n=t),P(n)},P=function(e){return Math.min(Math.max(e,1e3),18e6)},I=function(e,t,n){try{"function"==typeof t&&t.call(e,n)}catch(e){x(e)}};function L(e,t){S.call(this),t=t||{},this.onopen=void 0,this.onmessage=void 0,this.onerror=void 0,this.url=void 0,this.readyState=void 0,this.withCredentials=void 0,this.headers=void 0,this._close=void 0,function(e,t,n){t=String(t);var a=Boolean(n.withCredentials),u=n.lastEventIdQueryParameterName||"lastEventId",c=P(1e3),l=H(n.heartbeatTimeout,45e3),d="",h=c,f=!1,p=0,y=n.headers||{},v=n.Transport,g=M&&null==v?void 0:new w(null!=v?new v:null!=i&&"withCredentials"in i.prototype||null==s?new i:new s),_=null!=v&&"string"!=typeof v?new v:null==g?new T:new E,b=void 0,m=0,C=j,S="",x="",O="",L="",q=0,V=0,X=0,G=function(t,n,r,o){if(0===C)if(200===t&&null!=r&&N.test(r)){C=1,f=Date.now(),h=c,e.readyState=1;var i=new R("open",{status:t,statusText:n,headers:o});e.dispatchEvent(i),I(e,e.onopen,i)}else{var s="";200!==t?(n&&(n=n.replace(/\s+/g," ")),s="EventSource's response has a status "+t+" "+n+" that is not 200. Aborting the connection."):s="EventSource's response has a Content-Type specifying an unsupported type: "+(null==r?"-":r.replace(/\s+/g," "))+". Aborting the connection.",U();i=new R("error",{status:t,statusText:n,headers:o});e.dispatchEvent(i),I(e,e.onerror,i),console.error(s)}},z=function(t){if(1===C){for(var n=-1,i=0;i<t.length;i+=1){(u=t.charCodeAt(i))!=="\n".charCodeAt(0)&&u!=="\r".charCodeAt(0)||(n=i)}var s=(-1!==n?L:"")+t.slice(0,n+1);L=(-1===n?L:"")+t.slice(n+1),""!==t&&(f=Date.now(),p+=t.length);for(var a=0;a<s.length;a+=1){var u=s.charCodeAt(a);if(q===$&&u==="\n".charCodeAt(0))q=0;else if(q===$&&(q=0),u==="\r".charCodeAt(0)||u==="\n".charCodeAt(0)){if(0!==q){1===q&&(X=a+1);var y=s.slice(V,X-1),v=s.slice(X+(X<a&&s.charCodeAt(X)===" ".charCodeAt(0)?1:0),a);"data"===y?(S+="\n",S+=v):"id"===y?x=v:"event"===y?O=v:"retry"===y?(c=H(v,c),h=c):"heartbeatTimeout"===y&&(l=H(v,l),0!==m&&(o(m),m=r((function(){k()}),l)))}if(0===q){if(""!==S){d=x,""===O&&(O="message");var g=new A(O,{data:S.slice(1),lastEventId:x});if(e.dispatchEvent(g),"open"===O?I(e,e.onopen,g):"message"===O?I(e,e.onmessage,g):"error"===O&&I(e,e.onerror,g),2===C)return}S="",O=""}q=u==="\r".charCodeAt(0)?$:0}else 0===q&&(V=a,q=1),1===q?u===":".charCodeAt(0)&&(X=a+1,q=2):2===q&&(q=3)}}},B=function(t){if(1===C||0===C){C=j,0!==m&&(o(m),m=0),m=r((function(){k()}),h),h=P(Math.min(16*c,2*h)),e.readyState=0;var n=new D("error",{error:t});e.dispatchEvent(n),I(e,e.onerror,n),null!=t&&console.error(t)}},U=function(){C=2,null!=b&&(b.abort(),b=void 0),0!==m&&(o(m),m=0),e.readyState=2},k=function(){if(m=0,C===j){f=!1,p=0,m=r((function(){k()}),l),C=0,S="",O="",x=d,L="",V=0,X=0,q=0;var n=t;"data:"!==t.slice(0,5)&&"blob:"!==t.slice(0,5)&&""!==d&&(n+=(-1===t.indexOf("?")?"?":"&")+u+"="+encodeURIComponent(d));var o=e.withCredentials,i={Accept:"text/event-stream"},s=e.headers;if(null!=s)for(var a in s)Object.prototype.hasOwnProperty.call(s,a)&&(i[a]=s[a]);try{b=_.open(g,G,z,B,n,o,i)}catch(e){throw U(),e}}else if(f||null==b){var c=Math.max((f||Date.now())+l-Date.now(),1);f=!1,m=r((function(){k()}),c)}else B(new Error("No activity within "+l+" milliseconds. "+(0===C?"No response received.":p+" chars received.")+" Reconnecting.")),null!=b&&(b.abort(),b=void 0)};e.url=t,e.readyState=0,e.withCredentials=a,e.headers=y,e._close=U,k()}(this,e,t)}var M=null!=d&&null!=h&&"body"in h.prototype;L.prototype=Object.create(S.prototype),L.prototype.CONNECTING=0,L.prototype.OPEN=1,L.prototype.CLOSED=2,L.prototype.close=function(){this._close()},L.CONNECTING=0,L.OPEN=1,L.CLOSED=2,L.prototype.withCredentials=void 0;var q,V=u;null==i||null!=u&&"withCredentials"in u.prototype||(V=L),void 0!==(q=function(e){e.EventSourcePolyfill=L,e.NativeEventSource=u,e.EventSource=V}(t))&&(e.exports=q)}("undefined"==typeof globalThis?"undefined"!=typeof window?window:"undefined"!=typeof self?self:s:globalThis)})),u=Object.freeze(Object.assign(Object.create(null),a,{default:a}));return i})); | ||
* eventsource.js | ||
* Available under MIT License (MIT) | ||
* https://github.com/Yaffle/EventSource/ | ||
*/ | ||
var t=function(e){var t={exports:{}};return e(t,t.exports),t.exports}((function(t,n){!function(e){var r=e.setTimeout,o=e.clearTimeout,i=e.XMLHttpRequest,s=e.XDomainRequest,a=e.ActiveXObject,l=e.EventSource,c=e.document,u=e.Promise,h=e.fetch,d=e.Response,f=e.TextDecoder,p=e.TextEncoder,y=e.AbortController;if("undefined"==typeof window||"readyState"in c||null!=c.body||(c.readyState="loading",window.addEventListener("load",(function(e){c.readyState="complete"}),!1)),null==i&&null!=a&&(i=function(){return new a("Microsoft.XMLHTTP")}),null==Object.create&&(Object.create=function(e){function t(){}return t.prototype=e,new t}),Date.now||(Date.now=function(){return(new Date).getTime()}),null==y){var v=h;h=function(e,t){var n=t.signal;return v(e,{headers:t.headers,credentials:t.credentials,cache:t.cache}).then((function(e){var t=e.body.getReader();return n._reader=t,n._aborted&&n._reader.cancel(),{status:e.status,statusText:e.statusText,headers:e.headers,body:{getReader:function(){return t}}}}))},y=function(){this.signal={_reader:null,_aborted:!1},this.abort=function(){null!=this.signal._reader&&this.signal._reader.cancel(),this.signal._aborted=!0}}}function g(){this.bitsNeeded=0,this.codePoint=0}g.prototype.decode=function(e){function t(e,t,n){if(1===n)return e>=128>>t&&e<<t<=2047;if(2===n)return e>=2048>>t&&e<<t<=55295||e>=57344>>t&&e<<t<=65535;if(3===n)return e>=65536>>t&&e<<t<=1114111;throw new Error}function n(e,t){if(6===e)return t>>6>15?3:t>31?2:1;if(12===e)return t>15?3:2;if(18===e)return 3;throw new Error}for(var r=65533,o="",i=this.bitsNeeded,s=this.codePoint,a=0;a<e.length;a+=1){var l=e[a];0!==i&&(l<128||l>191||!t(s<<6|63&l,i-6,n(i,s)))&&(i=0,s=r,o+=String.fromCharCode(s)),0===i?(l>=0&&l<=127?(i=0,s=l):l>=192&&l<=223?(i=6,s=31&l):l>=224&&l<=239?(i=12,s=15&l):l>=240&&l<=247?(i=18,s=7&l):(i=0,s=r),0===i||t(s,i,n(i,s))||(i=0,s=r)):(i-=6,s=s<<6|63&l),0===i&&(s<=65535?o+=String.fromCharCode(s):(o+=String.fromCharCode(55296+(s-65535-1>>10)),o+=String.fromCharCode(56320+(s-65535-1&1023))))}return this.bitsNeeded=i,this.codePoint=s,o};null!=f&&null!=p&&function(){try{return"test"===(new f).decode((new p).encode("test"),{stream:!0})}catch(e){console.debug("TextDecoder does not support streaming option. Using polyfill instead: "+e)}return!1}()||(f=g);var _=function(){};function w(e){this.withCredentials=!1,this.readyState=0,this.status=0,this.statusText="",this.responseText="",this.onprogress=_,this.onload=_,this.onerror=_,this.onreadystatechange=_,this._contentType="",this._xhr=e,this._sendTimeout=0,this._abort=_}function b(e){return e.replace(/[A-Z]/g,(function(e){return String.fromCharCode(e.charCodeAt(0)+32)}))}function m(e){for(var t=Object.create(null),n=e.split("\r\n"),r=0;r<n.length;r+=1){var o=n[r].split(": "),i=o.shift(),s=o.join(": ");t[b(i)]=s}this._map=t}function E(){}function C(e){this._headers=e}function T(){}function S(){this._listeners=Object.create(null)}function x(e){r((function(){throw e}),0)}function O(e){this.type=e,this.target=void 0}function A(e,t){O.call(this,e),this.data=t.data,this.lastEventId=t.lastEventId}function R(e,t){O.call(this,e),this.status=t.status,this.statusText=t.statusText,this.headers=t.headers}function j(e,t){O.call(this,e),this.error=t.error}w.prototype.open=function(e,t){this._abort(!0);var n=this,s=this._xhr,a=1,l=0;this._abort=function(e){0!==n._sendTimeout&&(o(n._sendTimeout),n._sendTimeout=0),1!==a&&2!==a&&3!==a||(a=4,s.onload=_,s.onerror=_,s.onabort=_,s.onprogress=_,s.onreadystatechange=_,s.abort(),0!==l&&(o(l),l=0),e||(n.readyState=4,n.onabort(null),n.onreadystatechange())),a=0};var c=function(){if(1===a){var e=0,t="",r=void 0;if("contentType"in s)e=200,t="OK",r=s.contentType;else try{e=s.status,t=s.statusText,r=s.getResponseHeader("Content-Type")}catch(n){e=0,t="",r=void 0}0!==e&&(a=2,n.readyState=2,n.status=e,n.statusText=t,n._contentType=r,n.onreadystatechange())}},u=function(){if(c(),2===a||3===a){a=3;var e="";try{e=s.responseText}catch(e){}n.readyState=3,n.responseText=e,n.onprogress()}},h=function(e,t){if(null!=t&&null!=t.preventDefault||(t={preventDefault:_}),u(),1===a||2===a||3===a){if(a=4,0!==l&&(o(l),l=0),n.readyState=4,"load"===e)n.onload(t);else if("error"===e)n.onerror(t);else{if("abort"!==e)throw new TypeError;n.onabort(t)}n.onreadystatechange()}},d=function(){l=r((function(){d()}),500),3===s.readyState&&u()};"onload"in s&&(s.onload=function(e){h("load",e)}),"onerror"in s&&(s.onerror=function(e){h("error",e)}),"onabort"in s&&(s.onabort=function(e){h("abort",e)}),"onprogress"in s&&(s.onprogress=u),"onreadystatechange"in s&&(s.onreadystatechange=function(e){!function(e){null!=s&&(4===s.readyState?"onload"in s&&"onerror"in s&&"onabort"in s||h(""===s.responseText?"error":"load",e):3===s.readyState?"onprogress"in s||u():2===s.readyState&&c())}(e)}),!("contentType"in s)&&"ontimeout"in i.prototype||(t+=(-1===t.indexOf("?")?"?":"&")+"padding=true"),s.open(e,t,!0),"readyState"in s&&(l=r((function(){d()}),0))},w.prototype.abort=function(){this._abort(!1)},w.prototype.getResponseHeader=function(e){return this._contentType},w.prototype.setRequestHeader=function(e,t){var n=this._xhr;"setRequestHeader"in n&&n.setRequestHeader(e,t)},w.prototype.getAllResponseHeaders=function(){return null!=this._xhr.getAllResponseHeaders&&this._xhr.getAllResponseHeaders()||""},w.prototype.send=function(){if("ontimeout"in i.prototype&&("sendAsBinary"in i.prototype||"mozAnon"in i.prototype)||null==c||null==c.readyState||"complete"===c.readyState){var e=this._xhr;"withCredentials"in e&&(e.withCredentials=this.withCredentials);try{e.send(void 0)}catch(e){throw e}}else{var t=this;t._sendTimeout=r((function(){t._sendTimeout=0,t.send()}),4)}},m.prototype.get=function(e){return this._map[b(e)]},null!=i&&null==i.HEADERS_RECEIVED&&(i.HEADERS_RECEIVED=2),E.prototype.open=function(e,t,n,r,o,s,a){e.open("GET",o);var l=0;for(var c in e.onprogress=function(){var t=e.responseText.slice(l);l+=t.length,n(t)},e.onerror=function(e){e.preventDefault(),r(new Error("NetworkError"))},e.onload=function(){r(null)},e.onabort=function(){r(null)},e.onreadystatechange=function(){if(e.readyState===i.HEADERS_RECEIVED){var n=e.status,r=e.statusText,o=e.getResponseHeader("Content-Type"),s=e.getAllResponseHeaders();t(n,r,o,new m(s))}},e.withCredentials=s,a)Object.prototype.hasOwnProperty.call(a,c)&&e.setRequestHeader(c,a[c]);return e.send(),e},C.prototype.get=function(e){return this._headers.get(e)},T.prototype.open=function(e,t,n,r,o,i,s){var a=null,l=new y,c=l.signal,d=new f;return h(o,{headers:s,credentials:i?"include":"same-origin",signal:c,cache:"no-store"}).then((function(e){return a=e.body.getReader(),t(e.status,e.statusText,e.headers.get("Content-Type"),new C(e.headers)),new u((function(e,t){var r=function(){a.read().then((function(t){if(t.done)e(void 0);else{var o=d.decode(t.value,{stream:!0});n(o),r()}})).catch((function(e){t(e)}))};r()}))})).catch((function(e){return"AbortError"===e.name?void 0:e})).then((function(e){r(e)})),{abort:function(){null!=a&&a.cancel(),l.abort()}}},S.prototype.dispatchEvent=function(e){e.target=this;var t=this._listeners[e.type];if(null!=t)for(var n=t.length,r=0;r<n;r+=1){var o=t[r];try{"function"==typeof o.handleEvent?o.handleEvent(e):o.call(this,e)}catch(e){x(e)}}},S.prototype.addEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];null==r&&(r=[],n[e]=r);for(var o=!1,i=0;i<r.length;i+=1)r[i]===t&&(o=!0);o||r.push(t)},S.prototype.removeEventListener=function(e,t){e=String(e);var n=this._listeners,r=n[e];if(null!=r){for(var o=[],i=0;i<r.length;i+=1)r[i]!==t&&o.push(r[i]);0===o.length?delete n[e]:n[e]=o}},A.prototype=Object.create(O.prototype),R.prototype=Object.create(O.prototype),j.prototype=Object.create(O.prototype);var D=-1,$=-1,P=/^text\/event\-stream(;.*)?$/i,N=function(e,t){var n=null==e?t:parseInt(e,10);return n!=n&&(n=t),H(n)},H=function(e){return Math.min(Math.max(e,1e3),18e6)},I=function(e,t,n){try{"function"==typeof t&&t.call(e,n)}catch(e){x(e)}};function L(e,t){S.call(this),t=t||{},this.onopen=void 0,this.onmessage=void 0,this.onerror=void 0,this.url=void 0,this.readyState=void 0,this.withCredentials=void 0,this.headers=void 0,this._close=void 0,function(e,t,n){t=String(t);var a=Boolean(n.withCredentials),l=n.lastEventIdQueryParameterName||"lastEventId",c=H(1e3),u=N(n.heartbeatTimeout,45e3),h="",d=c,f=!1,p=0,y=n.headers||{},v=n.Transport,g=M&&null==v?void 0:new w(null!=v?new v:null!=i&&"withCredentials"in i.prototype||null==s?new i:new s),_=null!=v&&"string"!=typeof v?new v:null==g?new T:new E,b=void 0,m=0,C=D,S="",x="",O="",L="",q=0,V=0,X=0,G=function(t,n,r,o){if(0===C)if(200===t&&null!=r&&P.test(r)){C=1,f=Date.now(),d=c,e.readyState=1;var i=new R("open",{status:t,statusText:n,headers:o});e.dispatchEvent(i),I(e,e.onopen,i)}else{var s="";200!==t?(n&&(n=n.replace(/\s+/g," ")),s="EventSource's response has a status "+t+" "+n+" that is not 200. Aborting the connection."):s="EventSource's response has a Content-Type specifying an unsupported type: "+(null==r?"-":r.replace(/\s+/g," "))+". Aborting the connection.",U();i=new R("error",{status:t,statusText:n,headers:o});e.dispatchEvent(i),I(e,e.onerror,i),console.error(s)}},z=function(t){if(1===C){for(var n=-1,i=0;i<t.length;i+=1){(l=t.charCodeAt(i))!=="\n".charCodeAt(0)&&l!=="\r".charCodeAt(0)||(n=i)}var s=(-1!==n?L:"")+t.slice(0,n+1);L=(-1===n?L:"")+t.slice(n+1),""!==t&&(f=Date.now(),p+=t.length);for(var a=0;a<s.length;a+=1){var l=s.charCodeAt(a);if(q===$&&l==="\n".charCodeAt(0))q=0;else if(q===$&&(q=0),l==="\r".charCodeAt(0)||l==="\n".charCodeAt(0)){if(0!==q){1===q&&(X=a+1);var y=s.slice(V,X-1),v=s.slice(X+(X<a&&s.charCodeAt(X)===" ".charCodeAt(0)?1:0),a);"data"===y?(S+="\n",S+=v):"id"===y?x=v:"event"===y?O=v:"retry"===y?(c=N(v,c),d=c):"heartbeatTimeout"===y&&(u=N(v,u),0!==m&&(o(m),m=r((function(){k()}),u)))}if(0===q){if(""!==S){h=x,""===O&&(O="message");var g=new A(O,{data:S.slice(1),lastEventId:x});if(e.dispatchEvent(g),"open"===O?I(e,e.onopen,g):"message"===O?I(e,e.onmessage,g):"error"===O&&I(e,e.onerror,g),2===C)return}S="",O=""}q=l==="\r".charCodeAt(0)?$:0}else 0===q&&(V=a,q=1),1===q?l===":".charCodeAt(0)&&(X=a+1,q=2):2===q&&(q=3)}}},B=function(t){if(1===C||0===C){C=D,0!==m&&(o(m),m=0),m=r((function(){k()}),d),d=H(Math.min(16*c,2*d)),e.readyState=0;var n=new j("error",{error:t});e.dispatchEvent(n),I(e,e.onerror,n),null!=t&&console.error(t)}},U=function(){C=2,null!=b&&(b.abort(),b=void 0),0!==m&&(o(m),m=0),e.readyState=2},k=function(){if(m=0,C===D){f=!1,p=0,m=r((function(){k()}),u),C=0,S="",O="",x=h,L="",V=0,X=0,q=0;var n=t;"data:"!==t.slice(0,5)&&"blob:"!==t.slice(0,5)&&""!==h&&(n+=(-1===t.indexOf("?")?"?":"&")+l+"="+encodeURIComponent(h));var o=e.withCredentials,i={Accept:"text/event-stream"},s=e.headers;if(null!=s)for(var a in s)Object.prototype.hasOwnProperty.call(s,a)&&(i[a]=s[a]);try{b=_.open(g,G,z,B,n,o,i)}catch(e){throw U(),e}}else if(f||null==b){var c=Math.max((f||Date.now())+u-Date.now(),1);f=!1,m=r((function(){k()}),c)}else B(new Error("No activity within "+u+" milliseconds. "+(0===C?"No response received.":p+" chars received.")+" Reconnecting.")),null!=b&&(b.abort(),b=void 0)};e.url=t,e.readyState=0,e.withCredentials=a,e.headers=y,e._close=U,k()}(this,e,t)}var M=null!=h&&null!=d&&"body"in d.prototype;L.prototype=Object.create(S.prototype),L.prototype.CONNECTING=0,L.prototype.OPEN=1,L.prototype.CLOSED=2,L.prototype.close=function(){this._close()},L.CONNECTING=0,L.OPEN=1,L.CLOSED=2,L.prototype.withCredentials=void 0;var q,V=l;null==i||null!=l&&"withCredentials"in l.prototype||(V=L),void 0!==(q=function(e){e.EventSourcePolyfill=L,e.NativeEventSource=l,e.EventSource=V}(n))&&(t.exports=q)}("undefined"==typeof globalThis?"undefined"!=typeof window?window:"undefined"!=typeof self?self:e:globalThis)})),n=Object.freeze(Object.assign(Object.create(null),t,{default:t})),r=function(e){return e.data},o=function(e){return JSON.parse(e.data)},i=function(e){if(this._handlers={},this._listeners={},this._source=null,e.format?"string"==typeof e.format?"plain"===e.format?this._format=r:"json"===e.format?this._format=o:this._format=r:"function"==typeof e.format?this._format=e.format:this._format=r:this._format=r,e.handlers)for(var t in e.handlers)this.on(t,e.handlers[t]);this.url=e.url,this.withCredentials=!!e.withCredentials,this.polyfillOptions=e.polyfillOptions||{},this.forcePolyfill=!!e.forcePolyfill},s={source:{configurable:!0}};s.source.get=function(){return this._source},i.prototype.connect=function(){var e=this;return this.forcePolyfill?this._source=t.EventSourcePolyfill(this.url,Object.assign({},this.config.polyfillOptions,{withCredentials:this.withCredentials})):this._source=new window.EventSource(this.url,{withCredentials:this.withCredentials}),new Promise((function(t,n){e._source.onopen=function(){for(var n in e._listeners)e._source.addEventListener(n,e._listeners[n]);e._source.onerror=null,t(e)},e._source.onerror=n}))},i.prototype.disconnect=function(){null!==this._source&&(this._source.close(),this._source=null)},i.prototype.on=function(e,t){return e||(e="message"),this._listeners[e]||this._create(e),this._handlers[e].push(t),this},i.prototype.once=function(e,t){var n=this;return this.on(e,(function(r){n.off(e,t),t(r)})),this},i.prototype.off=function(e,t){if(!this._handlers[e])return this;var n=this._handlers[e].indexOf(t);return-1===n||(this._handlers[e].splice(n,1),0===this._handlers[e].length&&(this._source.removeEventListener(e,this._listeners[e]),delete this._handlers[e],delete this._listeners[e])),this},i.prototype._create=function(e){var t=this;this._handlers[e]=[],this._listeners[e]=function(n){var r;try{r=t._format(n)}catch(e){return void("function"==typeof t._source.onerror&&t._source.onerror(e))}t._handlers[e].forEach((function(e){return e(r)}))},this._source&&this._source.addEventListener(e,this._listeners[e])},Object.defineProperties(i.prototype,s);var a=function(e){this.$defaultConfig=Object.assign({format:r,sendCredentials:!1},e),this.$clients=null};return a.prototype.create=function(e){var t;t="object"==typeof e?e:"string"==typeof e?{url:e}:{};var n=new i(Object.assign({},this.$defaultConfig,t));return null!==this.$clients&&this.$clients.push(n),n},{SSEManager:a,install:function(e,t){e.$sse=e.prototype.$sse=new a(t),t&&t.polyfill&&Promise.resolve().then((function(){return n})),e.mixin({beforeCreate:function(){this.$options.sse&&this.$options.sse.cleanup&&(this.$sse=new a,this.$sse.$clients=[])},beforeDestroy:function(){null!==this.$sse.$clients&&(this.$sse.$clients.forEach((function(e){return e.disconnect()})),this.$sse.$clients=[])}})}}})); |
{ | ||
"name": "vue-sse", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "A Vue plugin for using Server-Sent Events (EventSource)", | ||
@@ -5,0 +5,0 @@ "main": "dist/vue-sse.common.js", |
@@ -27,3 +27,3 @@ # VueSSE | ||
polyfill: true, | ||
url: '/my-events-server' | ||
url: '/my-events-server', | ||
withCredentials: true, | ||
@@ -30,0 +30,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
204898
4859