Skip to content

Commit

Permalink
include xhrpromise dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
beradrian committed Mar 27, 2016
1 parent aa96679 commit 81fc5a6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 60 deletions.
61 changes: 7 additions & 54 deletions jsbandwidth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ var _extend = require("extend");

var _extend2 = _interopRequireDefault(_extend);

var _xhrpromise = require("xhrpromise");

var _xhrpromise2 = _interopRequireDefault(_xhrpromise);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
Expand Down Expand Up @@ -44,57 +48,6 @@ if (typeof angular != "undefined") {
}]);
}

var XHRPromise = { get: function get(options) {
var xhr = new XMLHttpRequest();
var p = new Promise(function (resolve, reject) {
xhr.open(options.method, options.url);
for (var h in options.headers) {
if (options.headers.hasOwnProperty(h)) {
xhr.setRequestHeader(h, options.headers[h]);
}
}
xhr.onload = function () {
if (this.readyState != 4) {
return;
}
if (this.status >= 200 && this.status < 300) {
resolve(this.responseText);
} else {
reject({
status: this.status,
statusText: this.statusText
});
}
};
xhr.onreadystatechange = function () {
if (this.status == 0) {
reject(REJECT_RESPONSE);
};
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: this.statusText
});
};
xhr.onabort = function () {
reject(REJECT_RESPONSE);
};
xhr.send();
});
p.xhr = xhr;
p.abort = p.cancel = function () {
p.xhr.abort();
};
p.xhr.send();
return p;
} };

var REJECT_RESPONSE = {
status: -1,
statusText: "Canceled"
};

var JsBandwidth = function () {
_createClass(JsBandwidth, null, [{
key: "calculateBandwidth",
Expand Down Expand Up @@ -161,7 +114,7 @@ var JsBandwidth = function () {
var self = this;
options = (0, _extend2.default)({}, this.options, options);
var start = new Date().getTime();
var r = XHRPromise.get({
var r = _xhrpromise2.default.create({
method: "GET",
url: options.downloadUrl + "?id=" + start,
dataType: 'application/octet-stream',
Expand All @@ -187,7 +140,7 @@ var JsBandwidth = function () {
options.uploadData = JsBandwidth.truncate(options.uploadData, options.uploadDataMaxSize);
}
var start = new Date().getTime();
var r = XHRPromise.get({
var r = _xhrpromise2.default.create({
method: "POST",
url: options.uploadUrl + "?id=" + start,
data: options.uploadData,
Expand All @@ -206,7 +159,7 @@ var JsBandwidth = function () {
options = (0, _extend2.default)({}, this.options, options);
options.latencyTestUrl = options.latencyTestUrl || options.downloadUrl;
var start = new Date().getTime();
var r = XHRPromise.get({
var r = _xhrpromise2.default.create({
method: "HEAD",
url: options.latencyTestUrl + "?id=" + start,
dataType: 'application/octet-stream',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dependencies":
{
"extend": ">=3.0.0"
, "xhrpromise": "beradrian/xhrpromise"
, "xhrpromise": ">=1.0.0"
},

"devDependencies":
Expand Down
6 changes: 1 addition & 5 deletions src/main/js/jsbandwidth.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ if (typeof angular != "undefined") {
}]);
}

var REJECT_RESPONSE = {
status: -1,
statusText: "Canceled"
};

import extend from "extend";
import XhrPromise from "xhrpromise";

export default class JsBandwidth {

Expand Down

0 comments on commit 81fc5a6

Please sign in to comment.