corifeus-utils
Advanced tools
Comparing version 1.1.328-18 to 1.1.329-21
{ | ||
"name": "corifeus-utils", | ||
"version": "1.1.328-18", | ||
"version": "1.1.329-21", | ||
"corifeus": { | ||
@@ -35,3 +35,3 @@ "icon": "fa fa-lightbulb-o", | ||
"devDependencies": { | ||
"corifeus-builder": "^1.7.659-16" | ||
"corifeus-builder": "^1.7.660-16" | ||
}, | ||
@@ -38,0 +38,0 @@ "dependencies": { |
@@ -101,3 +101,3 @@ [//]: #@corifeus-header | ||
--- | ||
[**CORIFEUS-UTILS**](https://pages.corifeus.tk/corifeus-utils) Build v1.1.328-18 | ||
[**CORIFEUS-UTILS**](https://pages.corifeus.tk/corifeus-utils) Build v1.1.329-21 | ||
@@ -104,0 +104,0 @@ [Corifeus](http://www.corifeus.tk) by [Patrik Laszlo](http://patrikx3.tk) |
const base62Charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | ||
const base36Charset = 'abcdefghijklmnopqrstuvwxyz0123456789'; | ||
module.exports.base36Charset = base36Charset; | ||
module.exports.base62Charset = base62Charset; | ||
module.exports.charset = (bytes, charset = base62Charset) => { | ||
@@ -4,0 +9,0 @@ let string = '' |
@@ -1,75 +0,2 @@ | ||
const url = require('url'); | ||
module.exports.request = (options) => { | ||
if (typeof(options) === 'string') { | ||
options = { | ||
url: options, | ||
method: 'GET', | ||
} | ||
} | ||
/* | ||
https://nodejs.org/api/http.html#http_http_request_options_callback | ||
*/ | ||
const parsedUrl = url.parse(options.url); | ||
let request; | ||
if (parsedUrl.protocol === 'https:') { | ||
request = require('https').request; | ||
if (parsedUrl.port === null) { | ||
parsedUrl.port = 443; | ||
} | ||
} else if (parsedUrl.protocol === 'http:') { | ||
request = require('http').request; | ||
if (parsedUrl.port === null) { | ||
parsedUrl.port = 80; | ||
} | ||
} else { | ||
throw new Error(`Unknown protocol ${parsedUrl.protocol}`); | ||
} | ||
options.protocol = parsedUrl.protocol; | ||
options.port = parsedUrl.port; | ||
options.host = parsedUrl.host; | ||
options.hostname = parsedUrl.hostname; | ||
options.path = parsedUrl.path; | ||
let body; | ||
if (options.body !== undefined) { | ||
if (typeof(options.body) === 'object') { | ||
body = JSON.stringify(options.body); | ||
options.headers = options.headers || {}; | ||
if (!options.headers.hasOwnProperty('Content-Type')) { | ||
options.headers['Content-Type'] = 'application/json; charset=utf-8' | ||
options.headers['Content-Length'] = Buffer.byteLength(body) | ||
} | ||
} | ||
} | ||
return new Promise((resolve, reject) => { | ||
const req = request(options, (res) => { | ||
res.setEncoding('utf8'); | ||
let rawData = ''; | ||
res.on('data', (chunk) => { | ||
rawData += chunk; | ||
}); | ||
res.on('end', () => { | ||
try { | ||
if (res.headers.hasOwnProperty('content-type') && res.headers['content-type'].startsWith('application/json')) { | ||
const parsedData = JSON.parse(rawData); | ||
res.body = parsedData; | ||
} else { | ||
res.body = rawData; | ||
} | ||
resolve(res); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}) | ||
req.on('error', reject); | ||
if (body !== undefined) { | ||
req.write(body); | ||
} | ||
req.end(); | ||
}) | ||
} | ||
module.exports.request = require('./request'); |
@@ -11,2 +11,9 @@ const crypto = require('mz/crypto'); | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
module.exports.lower = async(length = 16) => { | ||
const random = await crypto.randomBytes(length); | ||
const base = require('./base'); | ||
const string = base.charset(random, base.base36Charset) | ||
return string; | ||
} |
@@ -12,2 +12,18 @@ const ms = require('ms'); | ||
module.exports.msParse = (ms) => { | ||
if (typeof ms !== 'number') { | ||
throw new TypeError('Expected a number'); | ||
} | ||
var roundTowardZero = ms > 0 ? Math.floor : Math.ceil; | ||
return { | ||
days: roundTowardZero(ms / 86400000), | ||
hours: roundTowardZero(ms / 3600000) % 24, | ||
minutes: roundTowardZero(ms / 60000) % 60, | ||
seconds: roundTowardZero(ms / 1000) % 60, | ||
milliseconds: roundTowardZero(ms) % 1000 | ||
}; | ||
}; | ||
module.exports.verbose = (timestamp, started) => { | ||
@@ -17,4 +33,6 @@ if (timestamp === undefined) { | ||
} | ||
const leftMs = timestamp - Date.now(); | ||
return { | ||
left: ms(timestamp - Date.now()), | ||
left: ms(leftMs), | ||
leftMs: leftMs, | ||
end: new Date(timestamp).toLocaleString(), | ||
@@ -21,0 +39,0 @@ start: new Date(started).toLocaleString(), |
27244
28
656