.gitignore000066400000000000000000000000551517007421500130500ustar00rootroot00000000000000/build /node_modules /*.log /yarn.lock /.idea.npmignore000066400000000000000000000002501517007421500130540ustar00rootroot00000000000000/.idea /artifacts /build /Gemfile /_layouts /_site /_includes /test /node_modules /*.iml /*.ipr /*.iws /.travis.yml /.scrutinizer.yml /Gruntfile.js /corifeus-boot.json .scrutinizer.yml000066400000000000000000000005471517007421500142500ustar00rootroot00000000000000checks: javascript: true filter: excluded_paths: - test/* - node_modules/* - build/* - docs/* build: environment: node: 7.8 dependencies: before: - npm install -g grunt-cli tests: override: - command: 'grunt' coverage: file: 'build/coverage/clover.xml' format: 'clover' .travis.yml000066400000000000000000000001331517007421500131660ustar00rootroot00000000000000language: node_js node_js: - "7" - "node" before_script: - npm install grunt-cli -g Gruntfile.js000066400000000000000000000004201517007421500133510ustar00rootroot00000000000000module.exports = (grunt) => { const builder = require(`corifeus-builder`); const loader = new builder.loader(grunt); loader.js({ replacer: { npmio: true } }); grunt.registerTask('default', builder.config.task.build.js); } LICENSE000066400000000000000000000021351517007421500120660ustar00rootroot00000000000000MIT License Copyright (c) 2017 Patrik Laszlo Corifeus http://patrikx3.tk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.README.md000066400000000000000000000025231517007421500123410ustar00rootroot00000000000000[//]: #@corifeus-header # Corifeus Utils - Memory ### Node Version Requirement ``` >=7.8.0 ``` The ```async``` and ```await``` keywords are required. Install NodeJs: https://nodejs.org/en/download/package-manager/ # Description --- [//]: #@corifeus-header:end Misc utils (async array iterator, random characters, exit on silent ```unhandledRejection``` errors, etc...) ```javascript const utils = require('corifeus-utils'); ``` * Modules * Async Array Iterator ```forEachAsync``` * ```JSON.strintify``` for ```Error``` * Lodash Pascal * Remove silent process ```unhandledRejection``` end ```process.exit()```, adds timestamp * Process ```uncaughtException``` that shows timestamp. * Random async Base62 string * Replace inject - finds a prefix and postfix in a string and replace the content, ```strings.inject``` * Convert a byte array or string to base62, ```utils.base.charset(string)``` * Async Hash (SHA-512, SHA-256) file using Base62, * Promise based HTTP Request * Time utilies * Additional file system utils * JSON based file database * HTTP/HTTPS based async/Promise request [//]: #@corifeus-footer --- [**CORIFEUS-UTILS**](https://pages.corifeus.tk/corifeus-utils) Build v1.1.127-21 [Corifeus](http://www.corifeus.tk)) by [Patrik Laszlo](http://patrikx3.tk) [//]: #@corifeus-footer:end corifeus-utils.iml000066400000000000000000000007601517007421500145430ustar00rootroot00000000000000 package.json000066400000000000000000000022411517007421500133450ustar00rootroot00000000000000{ "name": "corifeus-utils", "version": "1.1.134-24", "corifeus": { "icon": "fa fa-lightbulb-o", "code": "Memory", "publish": true }, "description": "Corifeus Utils - Memory", "main": "src/index.js", "directories": { "test": "test" }, "scripts": { "test": "grunt", "coverage": "istanbul cover node_modules/mocha/bin/_mocha --report clover -- -R spec test/mocha/**/*.js" }, "repository": { "type": "git", "url": "git+https://github.com/patrikx3/corifeus-utils.git" }, "keywords": [ "corifeus", "nodejs", "node", "utils" ], "author": "Patrik Laszlo ", "license": "MIT", "bugs": { "url": "https://github.com/patrikx3/corifeus-utils/issues" }, "homepage": "https://pages.corifeus.tk/corifeus-utils", "devDependencies": { "corifeus-builder": "^1.7.552-27" }, "dependencies": { "fs-extra": "^3.0.1", "lodash": "^4.17.4", "ms": "^1.0.0", "mz": "^2.6.0", "timestring": "^4.0.0" }, "engines": { "node": ">=7.8.0" } }src/000077500000000000000000000000001517007421500116475ustar00rootroot00000000000000src/array/000077500000000000000000000000001517007421500127655ustar00rootroot00000000000000src/array/for-each-async.js000066400000000000000000000004761517007421500161310ustar00rootroot00000000000000const forEachAsync = (array, cb) => { const promises = []; for(let index = 0; index < array.length; index++) { const item = array[index]; promises.push(cb(item)) } const result = Promise.all(promises); result.promises = promises; return result; } module.exports = forEachAsync;src/array/index.js000066400000000000000000000005631517007421500144360ustar00rootroot00000000000000module.exports.forEachAsync = require('./for-each-async'); if (!Array.prototype.forEachAsync) { const forEachAsync = module.exports.forEachAsync; Object.defineProperty(Array.prototype, 'forEachAsync', { enumerable: false, writable: true, value: function(callback) { return forEachAsync(this, callback); } }); } src/base.js000066400000000000000000000007361517007421500131250ustar00rootroot00000000000000const base62Charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; module.exports.charset = (bytes, charset = base62Charset) => { let string = '' for(let index = 0; index < bytes.length; index++) { const byte = bytes[index]; const percent = (100 / 256 * byte) / 100; const charIndex = Math.floor(charset.length * percent) ; const char = charset.charAt(charIndex) string += char; } return string; } src/child-process/000077500000000000000000000000001517007421500144065ustar00rootroot00000000000000src/child-process/index.js000066400000000000000000000016301517007421500160530ustar00rootroot00000000000000const childProcess = require('child_process'); const promisable = require('../promise'); module.exports.exec = (command, options) => { if (options === true) { options = { display: true } } options = options || {}; options.stdio = options.stdio || 'inherit'; options.display = options.display || false; const { resolve, reject, promise} = promisable.extract(); const run = childProcess.exec(command, options, (err, stdout, stderr) => { if (err) { reject(err); return; } resolve({ stdout: stdout, stderr: stderr, }) }) if (options.display === true) { run.stdout.on('data', (data) => { console.log(data); }); run.stderr.on('data', (data) => { console.error(data); }); } promise.exec = run; return promise }src/db/000077500000000000000000000000001517007421500122345ustar00rootroot00000000000000src/db/index.js000066400000000000000000000021201517007421500136740ustar00rootroot00000000000000const coryFs = require('../fs'); const mz = require('mz'); const load = async (dbFile, data = {}) => { data = await coryFs.ensureFile(dbFile, data) data = JSON.parse(data.toString()); return new db(dbFile, data); } const db = function(dbFile, data) { const getTime = () => { return Date.now(); } if (!data.hasOwnProperty('_createdAt')) { data._createdAt = getTime(); data._updatedAt = getTime(); } this.save = function(entity, key, saveData) { if (!data.hasOwnProperty(entity)) { data[entity] = {}; } if (data[entity].hasOwnProperty(key)) { saveData._updatedAt = getTime(); data[entity][key] = Object.assign(data[entity][key], saveData); } else { saveData._createdAt = getTime(); saveData._updatedAt = getTime(); data[entity][key] = saveData; } } this.write = async function() { data._updatedAt = getTime(); await mz.fs.writeFile(dbFile, JSON.stringify(data, null, 4)); } } module.exports.load = load;src/error.js000066400000000000000000000005711517007421500133410ustar00rootroot00000000000000if (!('toJSON' in Error.prototype)) { Object.defineProperty(Error.prototype, 'toJSON', { value: function () { const alt = {}; Object.getOwnPropertyNames(this).forEach(function (key) { alt[key] = this[key]; }, this); return alt; }, configurable: true, writable: true }); } src/fs.js000066400000000000000000000013401517007421500126130ustar00rootroot00000000000000const fs = require('fs'); const mz = require('mz'); const path = require('path'); const fsExtra = require('fs-extra'); const ensureDir = async (dir) => { const exists = await mz.fs.exists(dir); if (exists) { return; } await mz.fs.mkdir(dir); } const ensureFile = async(file, defaultData = '') => { const exists = await mz.fs.exists(file); if (exists) { return await mz.fs.readFile(file); } if (typeof(defaultData) === 'object') { defaultData = JSON.stringify(defaultData); } await fsExtra.ensureDir(path.dirname(file)); await mz.fs.writeFile(file, defaultData) return defaultData; } module.exports.ensureFile = ensureFile; module.exports.ensureDir = ensureDir;src/hash.js000066400000000000000000000011411517007421500131250ustar00rootroot00000000000000const fs = require('fs'); const crypto = require('crypto'); const fileHash = (file, cryptoName = 'sha256', key = 'corifeus-hash-unsecure') => { return new Promise((resolve, reject) => { const fstream = fs.createReadStream(file); const hash = crypto.createHash(cryptoName, key); fstream.on('error', reject); fstream.on('end', function() { hash.end(); const bytes = hash.read(); const string = require('./base').charset(bytes) resolve(string); }); fstream.pipe(hash); }) } module.exports.file = fileHash; src/http/000077500000000000000000000000001517007421500126265ustar00rootroot00000000000000src/http/index.js000066400000000000000000000043501517007421500142750ustar00rootroot00000000000000const 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; } resolve(res); } catch (e) { reject(e); } }); }) req.on('error', reject); if (body !== undefined) { req.write(body); } req.end(); }) }src/index.js000066400000000000000000000007461517007421500133230ustar00rootroot00000000000000require('./lodash') module.exports = { error: require('./error'), process: require('./process'), base: require('./base'), random: require('./random'), array: require('./array'), string: require('./string'), time: require('./time'), object: require('./object'), hash: require('./hash'), http: require('./http'), childProcess: require('./child-process'), promise: require('./promise'), fs: require('./fs'), db: require('./db'), } src/lodash/000077500000000000000000000000001517007421500131215ustar00rootroot00000000000000src/lodash/index.js000066400000000000000000000001331517007421500145630ustar00rootroot00000000000000const _ = require('lodash'); _.mixin({ 'pascalCase': _.flow(_.camelCase, _.upperFirst) });src/object.js000066400000000000000000000004511517007421500134530ustar00rootroot00000000000000module.exports.reduce = (obj, props = ['content'], length = 100) => { const reduced = Object.assign({}, obj); if (Array.isArray(props)) { props = [props]; } props.forEach((prop) => { reduced[prop] = reduced[prop].substring(0, length); }) return reduced; } src/process.js000066400000000000000000000012361517007421500136650ustar00rootroot00000000000000process.on("unhandledRejection", (err, promise) => { if (process.listeners('unhandledRejection').length > 1) { return; } console.error(new Date().toLocaleString(), 'unhandledRejection', err, promise); process.exit(1); }); process.on('uncaughtException', function (err) { if (process.listeners('uncaughtException').length > 1) { return; } console.error(new Date().toLocaleString(), 'uncaughtException', err); process.exit(1); }); module.exports.writableCallbackExit = (callback) => { process.on('exit', function () { if (!this.stdin._writableState.ended) { callback(this.stdin) } }); }src/promise.js000066400000000000000000000004001517007421500136550ustar00rootroot00000000000000module.exports.extract = () => { let resolver, rejecter; const promise = new Promise((resolve, reject) => { resolver = resolve, rejecter = reject }) return { resolve: resolver, reject: rejecter, promise: promise, } }src/random.js000066400000000000000000000003211517007421500134610ustar00rootroot00000000000000const crypto = require('mz/crypto'); module.exports = async(length = 128) => { const random = await crypto.randomBytes(length); const string = require('./base').charset(random) return string; } src/string/000077500000000000000000000000001517007421500131555ustar00rootroot00000000000000src/string/index.js000066400000000000000000000030441517007421500146230ustar00rootroot00000000000000if (!String.prototype.padStart) { String.prototype.padStart = function padStart(targetLength,padString) { targetLength = targetLength>>0; //floor if number or convert non-number to 0; padString = String(padString || ' '); if (this.length > targetLength) { return String(this); } else { targetLength = targetLength-this.length; if (targetLength > padString.length) { padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed } return padString.slice(0,targetLength) + String(this); } }; } module.exports.empty = (spec) => { spec = String(spec).trim(); return spec === undefined || spec === '' || spec === 'undefined' } module.exports.inject = (str, options) => { const findPrefix = str.indexOf(options.prefix); const postixIndex = str.indexOf(options.postfix); if (findPrefix == -1 && postixIndex == -1 && !options.header && !options.footer) { return; } if (findPrefix == -1 && postixIndex == -1 ) { let replaceText = options.prefix + '\n' + options.replace + '\n' + options.postfix; if (options.header) { str = replaceText + '\n' +str; } else { str += '\n' + replaceText; } } else { const prefixIndex = findPrefix + options.prefix.length; str = str.substring(0, prefixIndex) + '\n' + options.replace + '\n' + str.substring(postixIndex); } return str; } src/time.js000066400000000000000000000011141517007421500131400ustar00rootroot00000000000000const ms = require('ms'); const timestring = require('timestring'); module.exports.span = (spec) => { let result = timestring(spec); if (typeof(result) === 'number') { result = result * 1000; } return result; } module.exports.verbose = (timestamp, started = Date.now()) => { if (timestamp === undefined) { return undefined; } return { left: ms(timestamp - started), end: new Date(timestamp).toLocaleString(), start: new Date(started).toLocaleString(), startstamp: started, timestamp: timestamp, } }test/000077500000000000000000000000001517007421500120375ustar00rootroot00000000000000test/script/000077500000000000000000000000001517007421500133435ustar00rootroot00000000000000test/script/error.js000077500000000000000000000001041517007421500150300ustar00rootroot00000000000000#!/usr/bin/env node require('../../src'); throw new Error('hiba');test/script/hash.js000077500000000000000000000002621517007421500146270ustar00rootroot00000000000000#!/usr/bin/env node const utils = require('../../src'); const start = async() => { const hash = await utils.hash.file('./package.json'); console.log(hash); } start();test/script/last-day.js000077500000000000000000000007611517007421500154260ustar00rootroot00000000000000#!/usr/bin/env node const utils = require('../../src'); console.log(new Date(utils.time.lastDay(0)).toLocaleString()); console.log(new Date(utils.time.lastDay(1)).toLocaleString()); console.log(new Date(utils.time.lastDay(2)).toLocaleString()); console.log(new Date(utils.time.lastDay(3)).toLocaleString()); console.log(new Date(utils.time.lastDay(4)).toLocaleString()); console.log(new Date(utils.time.lastDay(5)).toLocaleString()); console.log(new Date(utils.time.lastDay(6)).toLocaleString()); test/script/random.js000077500000000000000000000002461517007421500151660ustar00rootroot00000000000000#!/usr/bin/env node const utils = require('../../src'); const start = async() => { const random = await utils.random(128); console.log(random); } start();