Last active
December 28, 2017 07:37
-
-
Save velizarn/bc9a5c17ce0d3fb2a488720adf532f95 to your computer and use it in GitHub Desktop.
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
const crypto = require('crypto'), https = require('https') | |
// -------------------------------------------------------------------- | |
let setString = (str = '') => { | |
return new Promise((resolve, reject) => { | |
let _name = str || 'John Doe'; | |
try { | |
resolve( _name ) | |
} catch (e) { | |
return reject(e + '') | |
} | |
}) | |
} | |
let getStringLength = (str) => { | |
return new Promise((resolve, reject) => { | |
try { | |
resolve( {str: str, result: str.length} ) | |
} catch (e) { | |
return reject(e + '') | |
} | |
}) | |
} | |
let getStringHash = (str) => { | |
return new Promise((resolve, reject) => { | |
try { | |
resolve({ | |
str: str, | |
md5: crypto.createHash('md5').update(str).digest('hex'), | |
sha256: crypto.createHash('sha256').update(str).digest('hex') | |
}) | |
} catch (e) { | |
return reject(e + '') | |
} | |
}) | |
} | |
let stringBase64Encode = (str) => { | |
return new Promise((resolve, reject) => { | |
try { | |
resolve( {str: str, base64: new Buffer(str).toString('base64')} ) | |
} catch (e) { | |
return reject(e + '') | |
} | |
}) | |
} | |
let stringBase64Decode = (str) => { | |
return new Promise((resolve, reject) => { | |
try { | |
resolve( new Buffer(str, 'base64').toString('ascii') ) | |
} catch (e) { | |
return reject(e + '') | |
} | |
}) | |
} | |
let getTime = (timestamp) => { | |
return new Promise((resolve, reject) => { | |
try { | |
if (timestamp === undefined || timestamp === '' || Number(timestamp) === NaN) { | |
reject(new Error('Empty timestamp')) | |
} | |
if (Number.isNaN(Number(timestamp))) { | |
reject(new Error('Invalid timestamp')) | |
} | |
let url = 'https://now.httpbin.org/when/' + timestamp | |
https.get(url, (res) => { | |
let body = ''; | |
res.on('data', (chunk) => { body += chunk }) | |
res.on('end', () => { | |
let response = JSON.parse(body) | |
resolve( response.timestamp ) | |
}) | |
}).on('error', (e) => { | |
return reject(e + '') | |
}) | |
} catch (e) { | |
return reject(e + '') | |
} | |
}) | |
} | |
setString('username:password') | |
.then((result) => { | |
console.log('String: "' + result + '"') | |
return result | |
}) | |
.then(getStringLength) | |
.then((result) => { | |
console.log('String length: ' + result.result) | |
return result.str | |
}) | |
.then(getStringHash) | |
.then((result) => { | |
console.log('String MD5 hash: ' + result.md5) | |
console.log('String SHA256 hash: ' + result.sha256) | |
return result.str | |
}) | |
.then(stringBase64Encode) | |
.then((result) => { | |
console.log('String base64 encode: ' + result.base64) | |
return result.base64 | |
}) | |
.then(stringBase64Decode) | |
.then((result) => { | |
console.log('String base64 decode: ' + result) | |
}) | |
.then(() => { | |
let ts = (new Date()).getTime() | |
return ts | |
}) | |
.then(getTime) | |
.then((result) => { | |
console.log('RFC2822: ' + result.rfc2822) | |
console.log('RFC3339: ' + result.rfc3339) | |
}) | |
.catch((e) => { | |
console.error(e + '') | |
}) | |
/** | |
* https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/ | |
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error | |
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw | |
*/ |
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
String: "username:password" | |
String length: 17 | |
String MD5 hash: 133e1b8eda335c4c7f7a508620ca7f10 | |
String SHA256 hash: bc842c31a9e54efe320d30d948be61291f3ceee4766e36ab25fa65243cd76e0e | |
String base64 encode: dXNlcm5hbWU6cGFzc3dvcmQ= | |
String base64 decode: username:password | |
RFC2822: Thu, 28 Dec 2017 07:36:28 GMT | |
RFC3339: 2017-12-28T07:36:28.00Z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment