Skip to content

Instantly share code, notes, and snippets.

View akirattii's full-sized avatar

Akira TANAKA akirattii

View GitHub Profile
@akirattii
akirattii / headlesschrome-puppeteer-example.js
Created May 28, 2018 03:39
HeadlessChrome: puppeteer example
const puppeteer = require('puppeteer');
const evaluateFn = async() => {
// const title = document.title; // by pure js
const title = $("title").text(); // by jquery
return {
title,
// width: document.documentElement.clientWidth,
// height: document.documentElement.clientHeight,
@akirattii
akirattii / read-write-transform-stream-example.js
Created May 24, 2018 07:05
NodeJS: readStream & writeStream & transform example
const fs = require("fs");
const { Transform } = require('stream');
// upperCase transformer:
const upperCaseTr = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});
@akirattii
akirattii / markdown-for-blogger-monokai-like-code-highlight.html
Created May 24, 2018 04:08
markdown for blogger (monokai like code highlight)
<html>
...
<!-- for markdown -->
<script src="//cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<link rel='stylesheet' href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/monokai.min.css" />
<script src='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js'></script>
<script
src="//code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
@akirattii
akirattii / async-await-sleep-loop.js
Last active May 17, 2018 22:22
NodeJS: complex example of async/await + sleep + loop (also with express)
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec));
async function hoge() {
var ret = "";
for (let len = 2, i = 0; i < len; i++) {
console.log("hoge loop ...");
ret += await foo();
}
return "HOGE!" + ret;
}
@akirattii
akirattii / localforage.example.js
Created May 10, 2018 05:30
How to use localforage.js (localstorage, indexeddb, websql)
<script src="node_modules/localforage/dist/localforage.nopromises.min.js">
// $ npm install --save localforage
</script>
<script>
const dbname = "mydb";
const opts = {
driver: localforage.INDEXEDDB, // localforage.WEBSQL, or localforage.LOCALSTORAGE
name: dbname
};
console.log("creating instance with opts:", opts);
@akirattii
akirattii / example-client-session.js
Last active April 25, 2018 07:12
ExpressJS: clientside secure cookie session example using `client-sessions`, which is faster than using any session-store and not required any database.
const express = require('express');
const app = express();
/**
* clientside cookie session.
* it's faster than using any express-session-store, and not required any database server ;)
*/
const sessions = require("client-sessions");
app.use(sessions({
cookieName: 'mySession', // cookie name dictates the key name added to the request object
@akirattii
akirattii / example-create-shorthash.js
Created April 20, 2018 06:44
NodeJS: How to create a shorthash, but NOT smart way!
/**
* Create a short hash to **roughly** specify the accessed user's identity.
* The generated hash could be immutable only when either user's IP address and access date are same.
*
* CAUTION:
* NEVER use it for generate unique ID, it could NOT be unique!
*/
const crypto = require("crypto");
var text = "2018/04/01 xxx.xxx.xxx.xxx"; // "<Date> <IP>"
@akirattii
akirattii / example-read-write-mysql-blob.js
Created April 17, 2018 05:47
node-mysql example: How to write & read BLOB
/*
DDL:
CREATE TABLE `bindata` (
`id` INT NOT NULL AUTO_INCREMENT,
`data` BLOB,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
*/
const fs = require("fs");
@akirattii
akirattii / example-data-uri-image-write-read.js
Created April 16, 2018 06:33
Example: How to write an image from data URI and read it.
const fs = require('fs');
const dataUri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
const outputFilename = "output";
writeImage(dataUri, outputFilename);
readImage(outputFilename + ".png");
function writeImage(dataUri, outputFilename) {
const regex = /^data:.+\/(.+);base64,(.*)$/;
@akirattii
akirattii / generate-keypair-pem-by-openssl.md
Created April 11, 2018 01:48
Generate a private/public key pem file using `openssl`

Generate a private/public key pem file

# First, create a file as random seed, written as you like:
$ echo "<input_many_charactors_for_random_seed>" > random.txt

# Generate a private key:
$ openssl genrsa -rand random.txt 2048 > private.pem

# Generate a public key from above private key: