Skip to content

Instantly share code, notes, and snippets.

Peter Marklund peter

Block or report user

Report or block peter

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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"
}
]
@peter
peter / gmail-shortcut-keys.md
Last active Apr 9, 2019
Gmail Shortcut Keys
View gmail-shortcut-keys.md

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 Feb 28, 2019
VS Code Prettier Extension Format on Save settings.json
View vs-code-prettier-formatOnSave.json
{
"editor.formatOnSave": false,
"[javascript]": {
"editor.formatOnSave": true
},
"[javascriptreact]": {
"editor.formatOnSave": true
}
}
@peter
peter / java-boilerplate-hashcode-equals-tostring.java
Last active Jan 18, 2019
Java Boilerplate Nostalgia - DTO classes with hashCode, equals, toString, getter/setter methods
View java-boilerplate-hashcode-equals-tostring.java
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 Nov 29, 2018
JavaScript Compose and Pipe
View compose-and-pipe.js
//////////////////////////////////////////////
// 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
View await-with-sync-function.js
function syncBar () {
console.log('in syncBar')
return 'syncBar'
}
function asyncBar () {
console.log('in asyncBar')
return Promise.resolve('asyncBar')
}
You can’t perform that action at this time.