Ping utility for testing the reachability of a host.
npm install imgpingPing utility for testing the reachability of a host by image link. It is not reliable to test a host by domain because:
* net::ERR_NAME_NOT_RESOLVED is uncatchable
* domain fail does not mean assets under that domain are fail
* cross origin issue limits the ping methods
^6.2.0^0.21.0``bash`
yarn install
Maximum time (ms) to wait for a image to response
`js
ping.timeout = 1000
// es6
import ping from 'imgping'
ping.timeout = 1000
`
Please choose image by the following condition:
* as small as possible
* valid image source
* same image to race
Get response time (ms) for a single image
`js
ping.img('http://my.image/link.jpg')
.then(function(result) {
// success
console.log(result);
/**
* {
* hash: ''
* host: 'my.image'
* hostname: 'my.image'
* imageSrc: 'http://my.image/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 100
* }
*/
})
.catch(function(result) {
// failure
console.log(result);
/**
* {
* hash: ''
* host: 'my.image'
* hostname: 'my.image'
* imageSrc: 'http://my.image/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 3000
* }
*/
});
// es6
import ping from 'imgping'
ping.img('http://my.image/link.jpg')
.then((result) => {
// success
console.log(result)
/**
* {
* hash: ''
* host: 'my.image'
* hostname: 'my.image'
* imageSrc: 'http://my.image/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 100
* }
*/
})
.catch((result) => {
// failure
console.log(result)
/**
* {
* hash: ''
* host: 'my.image'
* hostname: 'my.image'
* imageSrc: 'http://my.image/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 3000
* }
*/
})
`
Get response time (ms) for a batch of images
`js
ping.imgs(['http://my.image.a/link.jpg', 'http://my.image.b/link.jpg'])
.then(function(result) {
// list of response time
console.log(result);
/**
* [
* {
* hash: ''
* host: 'my.image.a'
* hostname: 'my.image.a'
* imageSrc: 'http://my.image.a/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 100
* success: true
* },
* ...
* ]
*/
});
// es6
import ping from 'imgping'
ping.imgs(['http://my.image.a/link.jpg', 'http://my.image.b/link.jpg'])
.then((result) => {
// list of response time
console.log(result)
/**
* [
* {
* hash: ''
* host: 'my.image.a'
* hostname: 'my.image.a'
* imageSrc: 'http://my.image.a/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 100
* success: true
* },
* ...
* ]
*/
})
`
Resolve as soon as one of the images is loaded
`js
ping.race(['http://my.image.a/link.jpg', 'http://my.image.b/link.jpg'])
.then(function(result) {
// fastest image result
console.log(result);
/**
* {
* hash: ''
* host: 'my.image.a'
* hostname: 'my.image.a'
* imageSrc: 'http://my.image.a/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 100
* success: true
* }
*/
})
.catch(function(result) {
// list of response time if no image is loaded
console.log(result);
/**
* [
* {
* hash: ''
* host: 'my.image.a'
* hostname: 'my.image.a'
* imageSrc: 'http://my.image.a/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 3000
* success: false
* },
* ...
* ]
*/
});
// es6
import ping from 'imgping'
ping.race(['http://my.image.a/link.jpg', 'http://my.image.b/link.jpg'])
.then((result) => {
// response time
console.log(result)
/**
* {
* hash: ''
* host: 'my.image.a'
* hostname: 'my.image.a'
* imageSrc: 'http://my.image.a/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 100
* success: true
* }
*/
})
.catch((result) => {
// list of response time if no image is loaded
console.log(result)
/**
* [
* {
* hash: ''
* host: 'my.image.a'
* hostname: 'my.image.a'
* imageSrc: 'http://my.image.a/link.jpg'
* pathname: '/link.jpg'
* port: ''
* protocol: 'http:'
* responseTime: 3000
* success: false
* },
* ...
* ]
*/
})
`
`bash`
yarn start
`bash``
npm run build
1.1.0
-----
1.0.1
-----
1.0.0
-----