server-reach-by-image
Advanced tools
Sorry, the diff of this file is not supported yet
@@ -27,6 +27,4 @@ function _classCallCheck(instance, Constructor) { | ||
| var errors = { | ||
| load_timeout: 'Loading image timed out', | ||
| load_fail: 'Loading image failed', | ||
| param_missing_urls: 'Url param(s) not specified', | ||
| param_timeout_integer: 'Timeout param should be an integer' | ||
| E_LOAD_TIMEOUT: 'Loading image timed out', | ||
| E_LOAD_FAIL: 'Loading image failed' | ||
| }; | ||
@@ -52,14 +50,11 @@ | ||
| return new Promise(function (resolve, reject) { | ||
| /* eslint-disable prefer-promise-reject-errors */ | ||
| var fail = function fail(msg) { | ||
| return reject({ | ||
| msg: new Error(msg), | ||
| status: false | ||
| }); | ||
| var fail = function fail(code) { | ||
| return reject(Object.assign(new Error(errors[code]), { | ||
| status: false, | ||
| code: code | ||
| })); | ||
| }; | ||
| /* eslint-disable no-undef */ | ||
| var img = new window.Image(); | ||
| var img = new Image(); | ||
| img.onload = function () { | ||
@@ -73,3 +68,3 @@ return resolve({ | ||
| img.onerror = function (e) { | ||
| return fail(errors.load_fail); | ||
| return fail('E_LOAD_FAIL'); | ||
| }; | ||
@@ -79,3 +74,3 @@ | ||
| setTimeout(function () { | ||
| return fail(errors.load_timeout); | ||
| return fail('E_LOAD_TIMEOUT'); | ||
| }, _this.options.timeout); | ||
@@ -88,3 +83,3 @@ }); | ||
| if (!this.options.url || !this.options.imgUrl) { | ||
| throw new Error(errors.param_missing_urls); | ||
| throw new Error('Url param(s) not specified'); | ||
| } | ||
@@ -99,3 +94,3 @@ | ||
| if (!Number.isInteger(this.options.timeout)) { | ||
| throw new Error(errors.param_timeout_integer); | ||
| throw new TypeError('Timeout param should be an integer'); | ||
| } | ||
@@ -108,2 +103,3 @@ } | ||
| ServerReachByImage.errors = Object.keys(errors); | ||
| module.exports = ServerReachByImage; |
@@ -33,6 +33,4 @@ (function (global, factory) { | ||
| var errors = { | ||
| load_timeout: 'Loading image timed out', | ||
| load_fail: 'Loading image failed', | ||
| param_missing_urls: 'Url param(s) not specified', | ||
| param_timeout_integer: 'Timeout param should be an integer' | ||
| E_LOAD_TIMEOUT: 'Loading image timed out', | ||
| E_LOAD_FAIL: 'Loading image failed' | ||
| }; | ||
@@ -58,14 +56,11 @@ | ||
| return new Promise(function (resolve, reject) { | ||
| /* eslint-disable prefer-promise-reject-errors */ | ||
| var fail = function fail(msg) { | ||
| return reject({ | ||
| msg: new Error(msg), | ||
| status: false | ||
| }); | ||
| var fail = function fail(code) { | ||
| return reject(Object.assign(new Error(errors[code]), { | ||
| status: false, | ||
| code: code | ||
| })); | ||
| }; | ||
| /* eslint-disable no-undef */ | ||
| var img = new window.Image(); | ||
| var img = new Image(); | ||
| img.onload = function () { | ||
@@ -79,3 +74,3 @@ return resolve({ | ||
| img.onerror = function (e) { | ||
| return fail(errors.load_fail); | ||
| return fail('E_LOAD_FAIL'); | ||
| }; | ||
@@ -85,3 +80,3 @@ | ||
| setTimeout(function () { | ||
| return fail(errors.load_timeout); | ||
| return fail('E_LOAD_TIMEOUT'); | ||
| }, _this.options.timeout); | ||
@@ -94,3 +89,3 @@ }); | ||
| if (!this.options.url || !this.options.imgUrl) { | ||
| throw new Error(errors.param_missing_urls); | ||
| throw new Error('Url param(s) not specified'); | ||
| } | ||
@@ -105,3 +100,3 @@ | ||
| if (!Number.isInteger(this.options.timeout)) { | ||
| throw new Error(errors.param_timeout_integer); | ||
| throw new TypeError('Timeout param should be an integer'); | ||
| } | ||
@@ -114,4 +109,5 @@ } | ||
| ServerReachByImage.errors = Object.keys(errors); | ||
| module.exports = ServerReachByImage; | ||
| }))); |
@@ -1,2 +0,2 @@ | ||
| !function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n():"function"==typeof define&&define.amd?define(n):n()}(0,function(){"use strict";function t(t,n){for(var e=0;e<n.length;e++){var o=n[e];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}var n={timeout:2500},e="Loading image timed out",o="Loading image failed",i="Url param(s) not specified",r="Timeout param should be an integer",a=function(){function a(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}(this,a),this.options=Object.assign({},n,t),this.validateParams()}var s,u,c;return s=a,(u=[{key:"load",value:function(){var t=this;return new Promise(function(n,i){var r=function(t){return i({msg:new Error(t),status:!1})},a=new Image;a.onload=function(){return n({msg:null,status:!0})},a.onerror=function(t){return r(o)},a.src="".concat(t.options.url).concat(t.options.imgUrl,"?").concat((new Date).getTime()),setTimeout(function(){return r(e)},t.options.timeout)})}},{key:"validateParams",value:function(){if(!this.options.url||!this.options.imgUrl)throw new Error(i);if(this.options.url=this.options.url.replace(/\/+$/,""),this.options.imgUrl.startsWith("/")||(this.options.imgUrl="/".concat(this.options.imgUrl)),!Number.isInteger(this.options.timeout))throw new Error(r)}}])&&t(s.prototype,u),c&&t(s,c),a}();module.exports=a}); | ||
| !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){"use strict";function t(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}var e={timeout:2500},n={E_LOAD_TIMEOUT:"Loading image timed out",E_LOAD_FAIL:"Loading image failed"},o=function(){function o(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,o),this.options=Object.assign({},e,t),this.validateParams()}var i,r,a;return i=o,(r=[{key:"load",value:function(){var t=this;return new Promise(function(e,o){var i=function(t){return o(Object.assign(new Error(n[t]),{status:!1,code:t}))},r=new window.Image;r.onload=function(){return e({msg:null,status:!0})},r.onerror=function(t){return i("E_LOAD_FAIL")},r.src="".concat(t.options.url).concat(t.options.imgUrl,"?").concat((new Date).getTime()),setTimeout(function(){return i("E_LOAD_TIMEOUT")},t.options.timeout)})}},{key:"validateParams",value:function(){if(!this.options.url||!this.options.imgUrl)throw new Error("Url param(s) not specified");if(this.options.url=this.options.url.replace(/\/+$/,""),this.options.imgUrl.startsWith("/")||(this.options.imgUrl="/".concat(this.options.imgUrl)),!Number.isInteger(this.options.timeout))throw new TypeError("Timeout param should be an integer")}}])&&t(i.prototype,r),a&&t(i,a),o}();o.errors=Object.keys(n),module.exports=o}); | ||
| //# sourceMappingURL=server-reach-by-image.min.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"server-reach-by-image.min.js","sources":["../src/index.js"],"sourcesContent":["const defaults = {\n timeout: 2500\n};\nconst errors = {\n load_timeout: 'Loading image timed out',\n load_fail: 'Loading image failed',\n param_missing_urls: 'Url param(s) not specified',\n param_timeout_integer: 'Timeout param should be an integer'\n};\n\nclass ServerReachByImage {\n constructor(options = {}) {\n this.options = Object.assign({}, defaults, options);\n this.validateParams();\n }\n\n load() {\n return new Promise((resolve, reject) => {\n /* eslint-disable prefer-promise-reject-errors */\n const fail = msg => reject({ msg: new Error(msg), status: false });\n\n /* eslint-disable no-undef */\n const img = new Image();\n img.onload = () => resolve({ msg: null, status: true });\n img.onerror = e => fail(errors.load_fail);\n img.src = `${this.options.url}${this.options.imgUrl}?${new Date().getTime()}`;\n\n setTimeout(() => fail(errors.load_timeout), this.options.timeout);\n });\n }\n\n validateParams() {\n if (!this.options.url || !this.options.imgUrl) {\n throw new Error(errors.param_missing_urls);\n }\n\n this.options.url = this.options.url.replace(/\\/+$/, '');\n if (!this.options.imgUrl.startsWith('/')) {\n this.options.imgUrl = `/${this.options.imgUrl}`;\n }\n\n if (!Number.isInteger(this.options.timeout)) {\n throw new Error(errors.param_timeout_integer);\n }\n }\n}\n\nmodule.exports = ServerReachByImage;\n"],"names":["defaults","errors","ServerReachByImage","options","Object","assign","validateParams","Promise","resolve","reject","fail","Error","msg","img","Image","onload","onerror","src","_this","url","imgUrl","Date","getTime","timeout","this","replace","startsWith","Number","isInteger","module","exports"],"mappings":"6TAAA,IAAMA,WACK,MAELC,EACU,0BADVA,EAEO,uBAFPA,EAGgB,6BAHhBA,EAImB,qCAGnBC,8BACQC,uKACLA,QAAUC,OAAOC,UAAWL,EAAUG,QACtCG,yFAIE,IAAIC,QAAQ,SAACC,EAASC,OAErBC,EAAO,mBAAOD,OAAc,IAAIE,MAAMC,WAAc,KAGpDC,EAAM,IAAIC,QACZC,OAAS,kBAAMP,OAAe,aAAc,OAC5CQ,QAAU,mBAAKN,EAAKT,MACpBgB,cAASC,EAAKf,QAAQgB,YAAMD,EAAKf,QAAQiB,oBAAU,IAAIC,MAAOC,sBAEvD,kBAAMZ,EAAKT,IAAsBiB,EAAKf,QAAQoB,wDAKtDC,KAAKrB,QAAQgB,MAAQK,KAAKrB,QAAQiB,aAC/B,IAAIT,MAAMV,WAGbE,QAAQgB,IAAMK,KAAKrB,QAAQgB,IAAIM,QAAQ,OAAQ,IAC/CD,KAAKrB,QAAQiB,OAAOM,WAAW,YAC7BvB,QAAQiB,kBAAaI,KAAKrB,QAAQiB,UAGpCO,OAAOC,UAAUJ,KAAKrB,QAAQoB,eAC3B,IAAIZ,MAAMV,wCAKtB4B,OAAOC,QAAU5B"} | ||
| {"version":3,"file":"server-reach-by-image.min.js","sources":["../src/index.js"],"sourcesContent":["const defaults = {\n timeout: 2500\n};\nconst errors = {\n E_LOAD_TIMEOUT: 'Loading image timed out',\n E_LOAD_FAIL: 'Loading image failed'\n};\n\nclass ServerReachByImage {\n constructor(options = {}) {\n this.options = Object.assign({}, defaults, options);\n this.validateParams();\n }\n\n load() {\n return new Promise((resolve, reject) => {\n const fail = code => reject(Object.assign(new Error(errors[code]), { status: false, code }));\n\n const img = new window.Image();\n img.onload = () => resolve({ msg: null, status: true });\n img.onerror = e => fail('E_LOAD_FAIL');\n img.src = `${this.options.url}${this.options.imgUrl}?${new Date().getTime()}`;\n\n setTimeout(() => fail('E_LOAD_TIMEOUT'), this.options.timeout);\n });\n }\n\n validateParams() {\n if (!this.options.url || !this.options.imgUrl) {\n throw new Error('Url param(s) not specified');\n }\n\n this.options.url = this.options.url.replace(/\\/+$/, '');\n if (!this.options.imgUrl.startsWith('/')) {\n this.options.imgUrl = `/${this.options.imgUrl}`;\n }\n\n if (!Number.isInteger(this.options.timeout)) {\n throw new TypeError('Timeout param should be an integer');\n }\n }\n}\n\nServerReachByImage.errors = Object.keys(errors);\nmodule.exports = ServerReachByImage;\n"],"names":["defaults","errors","ServerReachByImage","options","Object","assign","validateParams","Promise","resolve","reject","fail","Error","code","img","window","Image","onload","onerror","src","_this","url","imgUrl","Date","getTime","timeout","this","replace","startsWith","Number","isInteger","TypeError","keys","module","exports"],"mappings":"6TAAA,IAAMA,WACK,MAELC,kBACY,sCACH,wBAGTC,8BACQC,uKACLA,QAAUC,OAAOC,UAAWL,EAAUG,QACtCG,yFAIE,IAAIC,QAAQ,SAACC,EAASC,OACrBC,EAAO,mBAAQD,EAAOL,OAAOC,OAAO,IAAIM,MAAMV,EAAOW,aAAkB,aAEvEC,EAAM,IAAIC,OAAOC,QACnBC,OAAS,kBAAMR,OAAe,aAAc,OAC5CS,QAAU,mBAAKP,EAAK,kBACpBQ,cAASC,EAAKhB,QAAQiB,YAAMD,EAAKhB,QAAQkB,oBAAU,IAAIC,MAAOC,sBAEvD,kBAAMb,EAAK,mBAAmBS,EAAKhB,QAAQqB,wDAKnDC,KAAKtB,QAAQiB,MAAQK,KAAKtB,QAAQkB,aAC/B,IAAIV,MAAM,sCAGbR,QAAQiB,IAAMK,KAAKtB,QAAQiB,IAAIM,QAAQ,OAAQ,IAC/CD,KAAKtB,QAAQkB,OAAOM,WAAW,YAC7BxB,QAAQkB,kBAAaI,KAAKtB,QAAQkB,UAGpCO,OAAOC,UAAUJ,KAAKtB,QAAQqB,eAC3B,IAAIM,UAAU,2EAK1B5B,EAAmBD,OAASG,OAAO2B,KAAK9B,GACxC+B,OAAOC,QAAU/B"} |
+1
-1
| { | ||
| "name": "server-reach-by-image", | ||
| "version": "1.0.7", | ||
| "version": "1.0.8", | ||
| "description": "Check can you reach a specific server (address or ip) by using image hosted on that server.", | ||
@@ -5,0 +5,0 @@ "main": "dist/server-reach-by-image.min.js", |
+6
-2
@@ -23,7 +23,11 @@ # Server Reach By Image | ||
| ```js | ||
| import ServerReachByImage from 'server-reach-by-image'; | ||
| import ServerReachByImage, { errors } from 'server-reach-by-image'; | ||
| const options = {}; | ||
| const serverByImage = new ServerReachByImage(options); | ||
| serverByImage.load().then(success).catch(fail); | ||
| serverByImage.load() | ||
| .then(data => {}) | ||
| .catch({ code: errors.E_LOAD_FAIL }, e => {}) | ||
| .catch({ code: errors.E_LOAD_TIMEOUT }, e => {}) | ||
| .catch(e => {}); | ||
| ``` | ||
@@ -30,0 +34,0 @@ Fail method will receive error message as first parameter. |
+9
-12
@@ -5,6 +5,4 @@ const defaults = { | ||
| const errors = { | ||
| load_timeout: 'Loading image timed out', | ||
| load_fail: 'Loading image failed', | ||
| param_missing_urls: 'Url param(s) not specified', | ||
| param_timeout_integer: 'Timeout param should be an integer' | ||
| E_LOAD_TIMEOUT: 'Loading image timed out', | ||
| E_LOAD_FAIL: 'Loading image failed' | ||
| }; | ||
@@ -20,12 +18,10 @@ | ||
| return new Promise((resolve, reject) => { | ||
| /* eslint-disable prefer-promise-reject-errors */ | ||
| const fail = msg => reject({ msg: new Error(msg), status: false }); | ||
| const fail = code => reject(Object.assign(new Error(errors[code]), { status: false, code })); | ||
| /* eslint-disable no-undef */ | ||
| const img = new Image(); | ||
| const img = new window.Image(); | ||
| img.onload = () => resolve({ msg: null, status: true }); | ||
| img.onerror = e => fail(errors.load_fail); | ||
| img.onerror = e => fail('E_LOAD_FAIL'); | ||
| img.src = `${this.options.url}${this.options.imgUrl}?${new Date().getTime()}`; | ||
| setTimeout(() => fail(errors.load_timeout), this.options.timeout); | ||
| setTimeout(() => fail('E_LOAD_TIMEOUT'), this.options.timeout); | ||
| }); | ||
@@ -36,3 +32,3 @@ } | ||
| if (!this.options.url || !this.options.imgUrl) { | ||
| throw new Error(errors.param_missing_urls); | ||
| throw new Error('Url param(s) not specified'); | ||
| } | ||
@@ -46,3 +42,3 @@ | ||
| if (!Number.isInteger(this.options.timeout)) { | ||
| throw new Error(errors.param_timeout_integer); | ||
| throw new TypeError('Timeout param should be an integer'); | ||
| } | ||
@@ -52,2 +48,3 @@ } | ||
| ServerReachByImage.errors = Object.keys(errors); | ||
| module.exports = ServerReachByImage; |
12
9.09%43
10.26%15576
-1.02%230
-3.77%