server-reach-by-image
Advanced tools
Comparing version 1.0.1 to 1.0.2
37
index.js
const defaults = { | ||
timeout: 3000 | ||
timeout: 2500 | ||
}; | ||
const errors = { | ||
timeout: 'timeout', | ||
missing_urls: 'urls not specified', | ||
timeout_integer: 'timeout should be an integer' | ||
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' | ||
}; | ||
class ServerReachByImage { | ||
@@ -17,27 +17,18 @@ constructor(options = {}) { | ||
reach() { | ||
load() { | ||
return new Promise((resolve, reject) => { | ||
Promise.race([this.getImage(), this.timeout(this.options.timeout)]) | ||
.then(() => resolve(null, true)) | ||
.catch(e => reject(e, false)) | ||
}); | ||
} | ||
const fail = msg => reject(new Error(msg), false); | ||
getImage() { | ||
return new Promise((resolve, reject) => { | ||
const img = document.createElement('img'); | ||
img.onload = () => resolve(); | ||
img.onerror = e => reject(e); | ||
const img = new Image(); | ||
img.onload = () => resolve(null, true); | ||
img.onerror = e => fail(errors.load_fail); | ||
img.src = `${this.options.url}${this.options.imgUrl}?${new Date().getTime()}`; | ||
setTimeout(() => fail(errors.load_timeout), this.options.timeout); | ||
}); | ||
} | ||
timeout(ms) { | ||
const err = new Error(errors.timeout); | ||
return new Promise((resolve, reject) => setTimeout(() => reject(err), ms)); | ||
} | ||
validateParams() { | ||
if (!this.options.url || !this.options.imgUrl) | ||
throw new Error(errors.missing_urls); | ||
throw new Error(errors.param_missing_urls); | ||
@@ -49,3 +40,3 @@ this.options.url = this.options.url.replace(/\/+$/, ''); | ||
if (!Number.isInteger(this.options.timeout)) | ||
throw new Error(errors.timeout_integer); | ||
throw new Error(errors.param_timeout_integer); | ||
} | ||
@@ -52,0 +43,0 @@ } |
{ | ||
"name": "server-reach-by-image", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Check can you reach a specific server (address or ip) by using image hosted on that server.", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"release": "np --yolo", | ||
"test": "node_modules/.bin/jest" | ||
"test": "node_modules/.bin/jest", | ||
"lint": "eslint" | ||
}, | ||
@@ -27,4 +28,12 @@ "repository": { | ||
"devDependencies": { | ||
"eslint": "^4.19.1", | ||
"eslint-config-semistandard": "^12.0.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.8.0", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"jest": "^23.0.0" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
@@ -26,3 +26,3 @@ # Server Reach By Image | ||
const serverByImage = new ServerReachByImage(options); | ||
serverByImage.reach().then(success).catch(fail); | ||
serverByImage.load().then(success).catch(fail); | ||
``` | ||
@@ -29,0 +29,0 @@ Fail method will receive error message as first parameter. |
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
19463
10
46
8