Skip to content

Instantly share code, notes, and snippets.

Avatar

Peter Marklund peter

View GitHub Profile
View node-hash-and-partition.js
// See: https://medium.com/@promentol/cryptography-for-javascript-node-js-developers-part-1-hash-function-86d119c7304
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
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 = {
writeTerraform,
generateMain,
}
// 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
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
}
//////////////////////////////////////////
// GENERATOR
//////////////////////////////////////////
function * traverseObj (value, path = []) {
View check-linux-version.sh
#!/bin/bash
uname -a
if test -f "/etc/os-release"; then
cat /etc/os-release
fi
if command -v lsb_release; then
lsb_release -a
fi
cat /proc/version
@peter
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 = '127.0.0.1'
const PROXY_BASE_URL = process.env.PROXY_BASE_URL;
const PORT = process.env.PORT || 9999
@peter
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
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(
'base64'
)}`;
return async (ctx: any, next: any): Promise<any> => {
const header = ctx.request.get('Authorization');
const headerMatches =
header &&
@peter
peter / convert-node-to-typescript.md
Last active Jan 26, 2020
How to Convert a Node.js App to TypeScript
View convert-node-to-typescript.md

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
peter / script-open-terminal-tabs.md
Created Apr 11, 2019
Script to open terminal tabs and run commands on Mac
View script-open-terminal-tabs.md

Open Terminal Tabs on Mac and Run Commands

Dependencies

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"
}
]