Skip to content

Instantly share code, notes, and snippets.

const progress = async (flag = '=') => {
const sleep = (timeout) => {
return new Promise((resovle) => { setTimeout(resovle, timeout) });
}
const total = 100;
for (let j = 1; j <= total; j++) {
process.stdout.write(`\u001b[${10000}D`);
@monjer
monjer / fileToBuffer.mjs
Created July 7, 2022 09:25
Convert file to buffer
import fs from 'fs';
const fileToBuffer = async (filePath) => {
return new Promise((resovle, reject) => {
fs.readFile(filePath, (error, data) => {
if (error) {
reject(error);
} else {
resovle(data);
}
@monjer
monjer / streamToBuffer.mjs
Created July 7, 2022 07:38
Convert readable stream to buffer
const streamToBuffer = (readableStream) => {
if (!readableStream) {
throw new Error('Error: readableStream can not be null')
}
return new Promise((resovle, reject) => {
const buffers = [];
readableStream.on('data', (chunks) => {
buffers.push(chunks);
});
@monjer
monjer / download.mjs
Last active September 14, 2023 01:50
Download file
import http from 'http';
import https from 'https';
import fs from 'fs';
export default async (url, dest) => {
const protocol = url.indexOf('https') === 0 ? https : http;
const fileStream = fs.createWriteStream(dest);
return new Promise((resovle, reject) => {
// http.IncomingMessage
const request = protocol.get(url, (response) => {
@monjer
monjer / getLocalIp.mjs
Created July 7, 2022 06:42
Get local ip
var ip = require('ip');
export default () => ip.address()
@monjer
monjer / getFIleMD5.mjs
Created July 7, 2022 06:31
get file or remote file md5
import crypto from 'crypto';
import fs from 'fs';
import http from 'http';
import https from 'https';
export const getFileMD5 = async (filePath, encoding='hex') => {
const hash = crypto.createHash('md5');
hash.setEncoding(encoding);
return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
import http from 'http';
/**
* @see https://stackoverflow.com/questions/38533580/nodejs-how-to-promisify-http-request-reject-got-called-two-times
*/
export default async (options, postData) => {
return new Promise((resolve, reject) => {
let body = [];
const request = http.request(options, (res) => {
@monjer
monjer / .manifest
Created June 24, 2022 10:12 — forked from devjin0617/.manifest
chrome extension using a content script to access the `window` object
{
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["inject.js"],
"all_frames": true
}
],
"web_accessible_resources": [
"content.js"
@monjer
monjer / review-checklist.md
Created June 22, 2022 13:56 — forked from bigsergey/review-checklist.md
Front-end Code Review Checklist

Review checklist

General

  1. Does the code work?
  2. Description of the project status is included.
  3. Code is easily understand.
  4. Code is written following the coding standarts/guidelines (React in our case).
  5. Code is in sync with existing code patterns/technologies.
  6. DRY. Is the same code duplicated more than twice?
@monjer
monjer / commit-message-format.md
Created December 16, 2021 07:29 — forked from develar/commit-message-format.md
Commit Message Format

Commit Message Format

Each commit message consists of a header, a body and a footer. The header has a special format that includes a type, a scope and a subject:

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>