Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / koa-naive-basic-auth-first-draft.ts
Created January 29, 2020 09:36
Naive Basic Auth Middleware for Koa - First Draft
/* 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 January 29, 2020 09:36
Naive Basic Auth Middleware for Koa
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 January 26, 2020 19:51
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
peter / script-open-terminal-tabs.md
Created April 11, 2019 09:01
Script to open terminal tabs and run commands on Mac

Open Terminal Tabs on Mac and Run Commands

Dependencies

Requires Node.js and ttab

npm install ttab -g
@peter
peter / vs-code-keybindings.json
Created April 10, 2019 13:32
VS Code Keybindings
[
{
"key": "cmd+t",
"command": "workbench.action.quickOpen"
},
{
"key": "cmd+p",
"command": "-workbench.action.quickOpen"
}
]
@peter
peter / gmail-shortcut-keys.md
Last active April 9, 2019 07:22
Gmail Shortcut Keys

Gmail Shortcut Keys

  • 'o' - open
  • 'u' - return to thread list
  • 'j' - next
  • 'k' - previous
  • '[' - archive and next
  • ']' - archive and previous
  • 'shift ?' - show shortcut keys help
@peter
peter / vs-code-prettier-formatOnSave.json
Created February 28, 2019 14:39
VS Code Prettier Extension Format on Save settings.json
{
"editor.formatOnSave": false,
"[javascript]": {
"editor.formatOnSave": true
},
"[javascriptreact]": {
"editor.formatOnSave": true
}
}
@peter
peter / java-boilerplate-hashcode-equals-tostring.java
Last active January 18, 2019 08:32
Java Boilerplate Nostalgia - DTO classes with hashCode, equals, toString, getter/setter methods
public class SettingDTO {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SettingDTO)) return false;
SettingDTO filterDTO = (SettingDTO) o;
if (key != null ? !key.equals(filterDTO.key) : filterDTO.key != null) return false;
if (value != null ? !value.equals(filterDTO.value) : filterDTO.value != null) return false;
@peter
peter / compose-and-pipe.js
Last active November 29, 2018 09:48
JavaScript Compose and Pipe
//////////////////////////////////////////////
// Compose and Pipe
//////////////////////////////////////////////
const a = v => v + 'a'
const b = v => v + 'b'
const c = v => v + 'c'
function compose (...fns) {
if (fns.length === 0) return arg => arg
function syncBar () {
console.log('in syncBar')
return 'syncBar'
}
function asyncBar () {
console.log('in asyncBar')
return Promise.resolve('asyncBar')
}