Comparing version 0.0.0 to 0.0.1
114
index.js
'use strict'; | ||
var response = require('xhr-response') | ||
, one = require('one-time'); | ||
/** | ||
* Simple nope function that assigned to XHR requests as part of a clean-up | ||
* operation. | ||
* | ||
* @api private | ||
*/ | ||
function nope() {} | ||
/** | ||
* Status codes that might need to be mapped to something more sane. | ||
@@ -32,29 +43,92 @@ * | ||
*/ | ||
module.exports = function loads(xhr, ee, streaming) { | ||
var offset = 0; | ||
function loads(xhr, ee, streaming) { | ||
var onreadystatechange | ||
, onprogress | ||
, ontimeout | ||
, onabort | ||
, onerror | ||
, onload; | ||
function onreadystate(evt) { | ||
var target = evt.target | ||
, body = ''; | ||
/** | ||
* Error listener. | ||
* | ||
* @param {Event} evt Triggered readyState change event. | ||
* @api private | ||
*/ | ||
onerror = xhr.onerror = one(function onerror(evt) { | ||
ee.emit('error', new Error('Network request failed')); | ||
ee.emit('end'); | ||
}); | ||
try { body = target.responseText; } | ||
catch (e) {} | ||
/** | ||
* Fix for FireFox's odd abort handling behaviour. When you press ESC on an | ||
* active request it triggers `error` instead of abort. The same is called | ||
* when an HTTP request is canceled onunload. | ||
* | ||
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=768596 | ||
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=880200 | ||
* @see https://code.google.com/p/chromium/issues/detail?id=153570 | ||
*/ | ||
onabort = xhr.onabort = function onabort(evt) { | ||
onerror(evt); | ||
}; | ||
if (4 === target.readyState) { | ||
if (xhr.multipart) ee.emit('streaming', body); | ||
else if (!streaming) ee.emit('data', body); | ||
} else if (streaming && target.re) | ||
} | ||
/** | ||
* ReadyStateChange listener. | ||
* | ||
* @param {Event} evt Triggered readyState change event. | ||
* @api private | ||
*/ | ||
onreadystatechange = xhr.onreadystatechange = function change(evt) { | ||
var target = evt.target; | ||
function onprogress(evt) { | ||
if (4 === target.readyState) return onload(evt); | ||
}; | ||
} | ||
/** | ||
* The connection has timed out. | ||
* | ||
* @api private | ||
*/ | ||
ontimeout = xhr.ontimeout = function timeout(evt) { | ||
ee.emit('timeout', evt); | ||
}; | ||
function onload(evt) { | ||
/** | ||
* IE needs have it's `onprogress` function assigned to a unique function. So, | ||
* no touchy touchy here! | ||
* | ||
* @param {Event} evt Triggered progress event. | ||
* @api private | ||
*/ | ||
onprogress = xhr.onprogress = function progress(evt) { | ||
var status = codes[xhr.status] || xhr.status | ||
, data; | ||
} | ||
ee.emit('progress', evt, status); | ||
xhr.onreadystatechange = onreadystate; | ||
xhr.onprogress = onprogress; | ||
xhr.onload = onload; | ||
}; | ||
if (xhr.readyState >= 3 && status === 200 && (data = response(xhr))) { | ||
ee.emit('stream', data); | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {Event} evt Triggered progress event. | ||
* @api private | ||
*/ | ||
onload = xhr.onload = one(function load(evt) { | ||
var status = codes[xhr.status] || xhr.status | ||
, data = response(xhr); | ||
if (status < 100 || status > 599) return onerror(evt); | ||
if (data) ee.emit('stream', data); | ||
ee.emit('end'); | ||
}); | ||
} | ||
// | ||
// Expose all the things. | ||
// | ||
module.exports = loads; |
{ | ||
"name": "loads", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Dedicated listening for xhr requests.", | ||
@@ -25,3 +25,7 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/unshiftio/loads" | ||
"homepage": "https://github.com/unshiftio/loads", | ||
"dependencies": { | ||
"one-time": "0.0.x", | ||
"xhr-response": "1.0.x" | ||
} | ||
} |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
9786
5
319
0
19
2
+ Addedone-time@0.0.x
+ Addedxhr-response@1.0.x
+ Addedone-time@0.0.4(transitive)
+ Addedxhr-response@1.0.1(transitive)