xhr-shaper
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "xhr-shaper", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Shapes your XHR requests to a max emulated bandwidth and latency, randomizes frequency of progress events", | ||
@@ -10,4 +10,5 @@ "main": "index.js", | ||
"build": "./node_modules/.bin/webpack --entry ./index --output-file dist/build.js --output-library XHRShaper", | ||
"dev": "./node_modules/.bin/webpack-dev-server --entry ./index --output-file ./index.js --output-library XHRShaper", | ||
"lint": "./node_modules/.bin/eslint ." | ||
"dev": "./node_modules/.bin/webpack-dev-server --port 8090 --entry ./index --output-file ./index.js --output-library XHRShaper", | ||
"lint": "./node_modules/.bin/eslint .", | ||
"start": "npm run dev" | ||
}, | ||
@@ -14,0 +15,0 @@ "keywords": [ |
@@ -12,6 +12,4 @@ # xhr-shaper | ||
``` | ||
var XHRShaper = require('xhr-shaper'); | ||
require('xhr-shaper').useGlobal(); | ||
XHRShaper.useGlobal(); | ||
// now XMLHttpRequest object is the shim | ||
@@ -26,3 +24,3 @@ ``` | ||
var xhr = XHRShaper.XMLHttpRequest; | ||
var xhr = new XHRShaper.XMLHttpRequest(); | ||
``` | ||
@@ -29,0 +27,0 @@ |
@@ -1,2 +0,2 @@ | ||
var XHRShaper = function() { | ||
var Shaper = function() { | ||
var _maxBandwidth = Infinity; | ||
@@ -9,3 +9,3 @@ var _minLatency = 0; | ||
get: function() { | ||
return Math.min(_maxBandwidth, XHRShaper.maxBandwidth); | ||
return Math.min(_maxBandwidth, Shaper.maxBandwidth); | ||
}, | ||
@@ -16,3 +16,3 @@ set: function(val) { _maxBandwidth = val; } | ||
get: function() { | ||
return Math.max(_minLatency, XHRShaper.minLatency); | ||
return Math.max(_minLatency, Shaper.minLatency); | ||
}, | ||
@@ -23,3 +23,3 @@ set: function(val) { _minLatency = val; } | ||
get: function() { | ||
return Math.max(_minProgressEvents, XHRShaper.minProgressEvents); | ||
return Math.max(_minProgressEvents, Shaper.minProgressEvents); | ||
}, | ||
@@ -30,3 +30,3 @@ set: function(val) { _minProgressEvents = val; } | ||
get: function() { | ||
return Math.max(_randomness, XHRShaper.randomness); | ||
return Math.max(_randomness, Shaper.randomness); | ||
}, | ||
@@ -37,7 +37,7 @@ set: function(val) { _randomness = val; } | ||
XHRShaper.maxBandwidth = Infinity; | ||
XHRShaper.minLatency = 0; | ||
XHRShaper.minProgressEvents = 0; | ||
XHRShaper.randomness = 0; | ||
Shaper.maxBandwidth = Infinity; | ||
Shaper.minLatency = 0; | ||
Shaper.minProgressEvents = 0; | ||
Shaper.randomness = 0; | ||
module.exports = XHRShaper; | ||
module.exports = Shaper; |
// This will shim the XHR object in your window and add some custom functionnality on top in the Shaper object | ||
var BaseXHR = require('./base-xhr'); | ||
var XHRShaper = require('./shaper'); | ||
var Shaper = require('./shaper'); | ||
var objectMirrors = require('./object-mirrors'); | ||
@@ -9,5 +9,5 @@ | ||
var XMLHttpRequest = function() { | ||
var XMLHttpRequestShim = function() { | ||
var xhr = new WindowXHR(); | ||
var shaper = new XHRShaper(); | ||
var shaper = new Shaper(); | ||
var _onreadystatechange, _onprogress, _onloadend; | ||
@@ -22,7 +22,8 @@ | ||
var loadEndEvent; | ||
var loadEvent; | ||
var done = false; | ||
xhr.onloadend = function(event) { | ||
loadEndEvent = event; | ||
if (_onloadend && xhr.readyState === 4) { | ||
if (_onloadend && done) { | ||
_onloadend(event); | ||
@@ -32,2 +33,9 @@ } | ||
xhr.onload = function(event) { | ||
loadEvent = event; | ||
if (_onload && done) { | ||
_onload(event); | ||
} | ||
}; | ||
xhr.onreadystatechange = function(event) { | ||
@@ -77,2 +85,8 @@ | ||
done = true; | ||
if (loadEvent && _onload) { | ||
_onload(loadEvent); | ||
} | ||
if (loadEndEvent && _onloadend) { | ||
@@ -144,2 +158,6 @@ _onloadend(loadEndEvent); | ||
}); | ||
objectMirrors.mirrorRwProp(instance, xhr, "onload", function(val) { | ||
_onload = val; | ||
return {override: true}; | ||
}); | ||
@@ -151,4 +169,4 @@ instance.shaper = shaper; | ||
XMLHttpRequest.Shaper = XHRShaper; | ||
XMLHttpRequestShim.Shaper = XHRShaper; | ||
module.exports = XMLHttpRequest; | ||
module.exports = XMLHttpRequestShim; |
Sorry, the diff of this file is not supported yet
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
16746
275
88