forked from markogresak/canihazip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (47 loc) · 1.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
/**
* Import HTTP interface module.
*
* TODO: Implement HTTPS. - has to be implemented on canihazip.com first.
*/
var protocol = require('https');
/**
* Save reference to native Promise implementation or import promise module.
*
* @type {Promise}
*/
var Promise = Promise || require('promise');
/**
* CanIHazIp constructor function.
*
* @param {Function=} callback (optional) Function to be called with IP data after server responds.
*
* @returns {Promise} Promise which is resolved with IP data after server responds.
*/
function CanIHazIp(callback) {
return new Promise(function (resolve) {
var httpCallback = function (response) {
var responseData = '';
// When data is received, append it to `responseData`.
response.on('data', function (chunk) {
responseData += chunk;
});
// When response ends, resolve returned Promise and call `callback` if it's a function.
response.on('end', function () {
resolve(responseData);
if (typeof callback === 'function') {
callback(responseData);
}
});
};
// Connect options for canihazip API, GET method is implicit.
var canihazipOptions = {
host: 'canihazip.com',
path: '/s'
};
// Make a request to canihazip and end it immediately.
protocol.request(canihazipOptions, httpCallback)
.end();
});
}
module.exports = CanIHazIp;