-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from johncrisostomo/add-timeout
Implements add timeout
- Loading branch information
Showing
5 changed files
with
410 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,71 @@ | ||
var https = require('https') | ||
var https = require('https'); | ||
|
||
function isEmpty(object) { | ||
for (var prop in object) { | ||
if (object.hasOwnProperty(prop)) return false | ||
if (object.hasOwnProperty(prop)) return false; | ||
} | ||
|
||
return true | ||
return true; | ||
} | ||
|
||
function pemEncode(str, n) { | ||
var ret = [] | ||
var ret = []; | ||
|
||
for (var i = 1; i <= str.length; i++) { | ||
ret.push(str[i - 1]) | ||
var mod = i % n | ||
ret.push(str[i - 1]); | ||
var mod = i % n; | ||
|
||
if (mod === 0) { | ||
ret.push('\n') | ||
ret.push('\n'); | ||
} | ||
} | ||
|
||
var returnString = `-----BEGIN CERTIFICATE-----\n${ret.join('')}\n-----END CERTIFICATE-----` | ||
var returnString = `-----BEGIN CERTIFICATE-----\n${ret.join('')}\n-----END CERTIFICATE-----`; | ||
|
||
return returnString | ||
return returnString; | ||
} | ||
|
||
function get(url) { | ||
function get(url, timeout) { | ||
if (url.length <= 0 || typeof url !== 'string') { | ||
throw Error('A valid URL is required') | ||
throw Error('A valid URL is required'); | ||
} | ||
|
||
var options = { | ||
hostname: url, | ||
agent: false, | ||
rejectUnauthorized: false, | ||
ciphers: 'ALL' | ||
} | ||
}; | ||
|
||
return new Promise(function(resolve, reject) { | ||
var req = https.get(options, function(res) { | ||
var certificate = res.socket.getPeerCertificate() | ||
var certificate = res.socket.getPeerCertificate(); | ||
|
||
if (isEmpty(certificate) || certificate === null) { | ||
reject({ message: 'The website did not provide a certificate' }) | ||
reject({ message: 'The website did not provide a certificate' }); | ||
} else { | ||
if (certificate.raw) { | ||
certificate.pemEncoded = pemEncode(certificate.raw.toString('base64'), 64) | ||
certificate.pemEncoded = pemEncode(certificate.raw.toString('base64'), 64); | ||
} | ||
resolve(certificate) | ||
resolve(certificate); | ||
} | ||
}) | ||
}); | ||
|
||
if (timeout) { | ||
req.setTimeout(timeout, function() { | ||
reject({ message: 'Request timed out.' }); | ||
req.abort(); | ||
}); | ||
} | ||
|
||
req.on('error', function(e) { | ||
reject(e) | ||
}) | ||
reject(e); | ||
}); | ||
|
||
req.end() | ||
}) | ||
req.end(); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
get: get | ||
} | ||
}; |
Oops, something went wrong.