diff --git a/jsbandwidth.js b/jsbandwidth.js index 43f3be8..531fd27 100644 --- a/jsbandwidth.js +++ b/jsbandwidth.js @@ -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"); } } @@ -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", @@ -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', @@ -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, @@ -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', diff --git a/package.json b/package.json index f3742d6..b701aa0 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "dependencies": { "extend": ">=3.0.0" - , "xhrpromise": "beradrian/xhrpromise" + , "xhrpromise": ">=1.0.0" }, "devDependencies": diff --git a/src/main/js/jsbandwidth.js b/src/main/js/jsbandwidth.js index 76b8edb..097ab92 100644 --- a/src/main/js/jsbandwidth.js +++ b/src/main/js/jsbandwidth.js @@ -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 {