Skip to content

Instantly share code, notes, and snippets.


Peter Marklund peter

View GitHub Profile
View node-hash-and-partition.js
// See:
function hash(data, digest = 'hex') {
return require('crypto').createHash('sha256').update(data).digest(digest)
// ('foo', 10) => 6
// ('bar', 10) => 6
// ('zz', 10) => 2
// ('foo', 6) => 2
peter / terraform-util.js
Created Dec 17, 2020
De-duplicating Terraform code by generating it from JavaScript
View terraform-util.js
const fs = require('fs')
module.exports = {
// Templates are just terraform (.tf files) with the following kinds of interpolations:
// 1. Variables embedded in a string: "terraform-states-<id>"
// 2. Variables occupying an entire string: "<apiId>".
peter / traverse-object-generator.js
Last active Dec 11, 2020
Traverse an object in Node.js/Javascript with a generator function
View traverse-object-generator.js
function isObject (value) {
return value != null && typeof value === 'object' && value.constructor === Object
function * traverseObj (value, path = []) {
uname -a
if test -f "/etc/os-release"; then
cat /etc/os-release
if command -v lsb_release; then
lsb_release -a
cat /proc/version
peter / cors-proxy.js
Last active Aug 24, 2020
Local Node http proxy with CORS headers
View cors-proxy.js
// The use case is that you are developing a web app locally that makes Ajax requests to some external
// API that doesn't have sufficiently permissive CORS headers so your requests fail. The solution is to point the app
// to this local proxy which will allow CORS.
const http = require('http')
const axios = require('axios')
const HOSTNAME = ''
const PROXY_BASE_URL = process.env.PROXY_BASE_URL;
const PORT = process.env.PORT || 9999
peter / koa-naive-basic-auth-first-draft.ts
Created Jan 29, 2020
Naive Basic Auth Middleware for Koa - First Draft
View koa-naive-basic-auth-first-draft.ts
/* eslint-disable no-plusplus */
import crypto from 'crypto';
function parse(header: string | null): string | null {
const match = (header || '').match(/^Basic (\S{3,300})$/);
return match && Buffer.from(match[1], 'base64').toString();
function isEqual(s1: string | null, s2: string | null): boolean {
return (
peter / koa-naive-basic-auth.ts
Last active Jan 29, 2020
Naive Basic Auth Middleware for Koa
View koa-naive-basic-auth.ts
import crypto from 'crypto';
export function basicAuthMiddleware({ name, pass }: any): Function {
const auth = `Basic ${Buffer.from([name, pass].join(':')).toString(
return async (ctx: any, next: any): Promise<any> => {
const header = ctx.request.get('Authorization');
const headerMatches =
header &&
peter /
Last active Jan 26, 2020
How to Convert a Node.js App to TypeScript

How to Convert a Node.js App to TypeScript

Here are examples of steps taken to convert a server rendered web app to TypeScript. In my experience you can expect a conversion like this to take a day or a couple of days for a small project (a couple of thousand lines of code).

Install packages

Install Typescript and types for node and dependencies:

npm install typescript --save-dev
peter /
Created Apr 11, 2019
Script to open terminal tabs and run commands on Mac

Open Terminal Tabs on Mac and Run Commands


Requires Node.js and ttab

npm install ttab -g
View vs-code-keybindings.json
"key": "cmd+t",
"command": "workbench.action.quickOpen"
"key": "cmd+p",
"command": "-workbench.action.quickOpen"