Skip to content

Instantly share code, notes, and snippets.

View Alex1990's full-sized avatar
🐢
Get Things Done!

Alex Chao Alex1990

🐢
Get Things Done!
View GitHub Profile
@Alex1990
Alex1990 / self-signed.sh
Last active February 15, 2020 16:22
Generate a self-signed ssl certificate
#!/usr/bin/env bash
# The "Common Name (CN)" can be "example.com" or "*.example.com www.example.com"
# This command should be executed in non-root mode
domain="$1"
if [ -z "$domain" ]; then
echo "need domain parameter"
exit 1
@Alex1990
Alex1990 / retry.js
Last active December 28, 2019 19:29
Retry a function the specified times
async function retry(fn, times = 0, ...args) {
if (typeof times !== 'number') throw new Error('times must be a number')
if (times < 0) throw new Error('times must not be less than 0')
const totalTimes = times + 1
let result
let firstError
let i = 0
for (; i < totalTimes; i++) {
try {
result = await fn(...args)
@Alex1990
Alex1990 / alexa.js
Created May 17, 2019 07:08 — forked from chilts/alexa.js
Getting the Alexa top 1 million sites directly from the server, unzipping it, parsing the csv and getting each line as an array.
var request = require('request');
var unzip = require('unzip');
var csv2 = require('csv2');
request.get('http://s3.amazonaws.com/alexa-static/top-1m.csv.zip')
.pipe(unzip.Parse())
.on('entry', function (entry) {
entry.pipe(csv2()).on('data', console.log);
})
;
@Alex1990
Alex1990 / Perf.js
Created March 30, 2019 07:53
Perf class
class Perf {
constructor () {
this.init()
this.phases = []
}
init () {
const now = Date.now()
this.start = now
this.lastTime = now
@Alex1990
Alex1990 / promisify.js
Created January 10, 2019 11:35
Like Node.js util.promisify
function promisify(func) {
return (...args) => {
const args1 = args.slice(0, args.length);
return new Promise((resolve, reject) => {
const callback = (err, ...rest) => {
if (err) {
reject(err);
} else {
resolve(...rest);
}
@Alex1990
Alex1990 / getTransferSize.js
Created December 19, 2018 06:17
Get the encoded data size of all requests in a page
const puppeteer = require('puppeteer');
const prettyBytes = require('pretty-bytes');
const chalk = require('chalk');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const devToolsResponses = new Map();
const devTools = await page.target().createCDPSession();
await devTools.send('Network.enable');
@Alex1990
Alex1990 / open-source-search-compare.md
Created August 24, 2017 07:43 — forked from jeremyfelt/open-source-search-compare.md
Comparing open source search solutions
/**
* Some spaces (margin or padding) atomic classes
*/
@spaceProps: margin, padding;
@spacePropShortands: m, p;
@directions: top, right, bottom, left;
@directionShortands: t, r, b, l;
@spaceValues: 0, 4, 8, 16;
/**
* Tests:
* - `isPlainObject(null)`
* - `isPlainObject([1, 2, 3])`
* - `isPlainObject(window)`
* - `isPlainObject(new String('hello'))`
* - `isPlainObject(Object.create(null))`
* - `isPlainObject({ a: 1, b: 2 })`
* - `isPlainObject(new Person('Alex'))`
*
/**
* Tests:
* - `isPlainObject(null)`
* - `isPlainObject([1, 2, 3])`
* - `isPlainObject(window)`
* - `isPlainObject(new String('hello'))`
* - `isPlainObject(Object.create(null))`
* - `isPlainObject({ a: 1, b: 2 })`
* - `isPlainObject(new Person('Alex'))`
*