function EventSource(url, eventSourceInitDict) { var readyState = EventSource.CONNECTING Object.defineProperty(this, 'readyState', { get: function () { return readyState } }) Object.defineProperty(this, 'url', { get: function () { return url } }) var self = this self.reconnectInterval = 1000 function onConnectionClosed () { if (readyState === EventSource.CLOSED) return readyState = EventSource.CONNECTING _emit('error', new Event('error')) // The url may have been changed by a temporary // redirect. If that's the case, revert it now. if (reconnectUrl) { url = reconnectUrl reconnectUrl = null } setTimeout(function () { if (readyState !== EventSource.CONNECTING) { return } connect() }, self.reconnectInterval) } var req var lastEventId = '' if (eventSourceInitDict && eventSourceInitDict.headers && eventSourceInitDict.headers['Last-Event-ID']) { lastEventId = eventSourceInitDict.headers['Last-Event-ID'] delete eventSourceInitDict.headers['Last-Event-ID'] } var discardTrailingNewline = false var data = '' var eventName = '' var reconnectUrl = null function connect () { var options = parse(url) var isSecure = options.protocol === 'https:' options.headers = { 'Cache-Control': 'no-cache', 'Accept': 'text/event-stream' } if (lastEventId) options.headers['Last-Event-ID'] = lastEventId if (eventSourceInitDict && eventSourceInitDict.headers) { for (var i in eventSourceInitDict.headers) { var header = eventSourceInitDict.headers[i] if (header) { options.headers[i] = header } } } // Legacy: this should be specified as `eventSourceInitDict.https.rejectUnauthorized`, // but for now exists as a backwards-compatibility layer options.rejectUnauthorized = !(eventSourceInitDict && !eventSourceInitDict.rejectUnauthorized) // If specify http proxy, make the request to sent to the proxy server, // and include the original url in path and Host headers if (eventSourceInitDict && eventSourceInitDict.proxy) { var proxy = parse(eventSourceInitDict.proxy) options.path = url options.headers.Host = options.host options.hostname = proxy.hostname options.host = proxy.host options.port = proxy.port } // If https options are specified, merge them into the request options if (eventSourceInitDict && eventSourceInitDict.https) { for (var optName in eventSourceInitDict.https) { if (httpsOptions.indexOf(optName) === -1) { continue } var option = eventSourceInitDict.https[optName] if (option !== undefined) { options[optName] = option } } } req = (isSecure ? https : http).request(options, function (res) { // Handle HTTP redirects if (res.statusCode === 301 || res.statusCode === 307) { if (!res.headers.location) { // Server sent redirect response without Location header. _emit('error', new Event('error', {status: res.statusCode})) return } if (res.statusCode === 307) reconnectUrl = url url = res.headers.location process.nextTick(connect) return } if (res.statusCode !== 200) { _emit('error', new Event('error', {status: res.statusCode})) return self.close() } readyState = EventSource.OPEN res.on('close', function () { res.removeAllListeners('close') res.removeAllListeners('end') onConnectionClosed() }) res.on('end', function () { res.removeAllListeners('close') res.removeAllListeners('end') onConnectionClosed() }) _emit('open', new Event('open')) // text/event-stream parser adapted from webkit's // Source/WebCore/page/EventSource.cpp var buf = '' res.on('data', function (chunk) { buf += chunk var pos = 0 var length = buf.length ...
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
function EventSource(url, eventSourceInitDict) { var readyState = EventSource.CONNECTING Object.defineProperty(this, 'readyState', { get: function () { return readyState } }) Object.defineProperty(this, 'url', { get: function () { return url } }) var self = this self.reconnectInterval = 1000 function onConnectionClosed () { if (readyState === EventSource.CLOSED) return readyState = EventSource.CONNECTING _emit('error', new Event('error')) // The url may have been changed by a temporary // redirect. If that's the case, revert it now. if (reconnectUrl) { url = reconnectUrl reconnectUrl = null } setTimeout(function () { if (readyState !== EventSource.CONNECTING) { return } connect() }, self.reconnectInterval) } var req var lastEventId = '' if (eventSourceInitDict && eventSourceInitDict.headers && eventSourceInitDict.headers['Last-Event-ID']) { lastEventId = eventSourceInitDict.headers['Last-Event-ID'] delete eventSourceInitDict.headers['Last-Event-ID'] } var discardTrailingNewline = false var data = '' var eventName = '' var reconnectUrl = null function connect () { var options = parse(url) var isSecure = options.protocol === 'https:' options.headers = { 'Cache-Control': 'no-cache', 'Accept': 'text/event-stream' } if (lastEventId) options.headers['Last-Event-ID'] = lastEventId if (eventSourceInitDict && eventSourceInitDict.headers) { for (var i in eventSourceInitDict.headers) { var header = eventSourceInitDict.headers[i] if (header) { options.headers[i] = header } } } // Legacy: this should be specified as `eventSourceInitDict.https.rejectUnauthorized`, // but for now exists as a backwards-compatibility layer options.rejectUnauthorized = !(eventSourceInitDict && !eventSourceInitDict.rejectUnauthorized) // If specify http proxy, make the request to sent to the proxy server, // and include the original url in path and Host headers if (eventSourceInitDict && eventSourceInitDict.proxy) { var proxy = parse(eventSourceInitDict.proxy) options.path = url options.headers.Host = options.host options.hostname = proxy.hostname options.host = proxy.host options.port = proxy.port } // If https options are specified, merge them into the request options if (eventSourceInitDict && eventSourceInitDict.https) { for (var optName in eventSourceInitDict.https) { if (httpsOptions.indexOf(optName) === -1) { continue } var option = eventSourceInitDict.https[optName] if (option !== undefined) { options[optName] = option } } } req = (isSecure ? https : http).request(options, function (res) { // Handle HTTP redirects if (res.statusCode === 301 || res.statusCode === 307) { if (!res.headers.location) { // Server sent redirect response without Location header. _emit('error', new Event('error', {status: res.statusCode})) return } if (res.statusCode === 307) reconnectUrl = url url = res.headers.location process.nextTick(connect) return } if (res.statusCode !== 200) { _emit('error', new Event('error', {status: res.statusCode})) return self.close() } readyState = EventSource.OPEN res.on('close', function () { res.removeAllListeners('close') res.removeAllListeners('end') onConnectionClosed() }) res.on('end', function () { res.removeAllListeners('close') res.removeAllListeners('end') onConnectionClosed() }) _emit('open', new Event('open')) // text/event-stream parser adapted from webkit's // Source/WebCore/page/EventSource.cpp var buf = '' res.on('data', function (chunk) { buf += chunk var pos = 0 var length = buf.length ...
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
function addEventListener(type, listener) { if (typeof listener === 'function') { // store a reference so we can return the original function again listener._listener = listener this.on(type, listener) } }
n/a
function removeEventListener(type, listener) { if (typeof listener === 'function') { listener._listener = undefined this.removeListener(type, listener) } }
n/a