Skip to content

Instantly share code, notes, and snippets.

View danew's full-sized avatar

Dane Wilson danew

View GitHub Profile

Keybase proof

I hereby claim:

  • I am danew on github.
  • I am daneo (https://keybase.io/daneo) on keybase.
  • I have a public key ASCKVoT9q0qq3_xB5kbMietw0twxS6yt0dVDPFFkAk7aDwo

To claim this, I am signing this object:

@danew
danew / ses-smtp-password.py
Created November 13, 2017 01:06
Ansible filter to create AWS SES SMTP password from aws access secret
#!/usr/bin/env python
import base64
from Crypto.Hash import HMAC, SHA256
class PasswordEncoder:
def encode(self, secret):
signature = self._hmac_sha256(b'SendRawEmail', secret)
byte_array = self._version_signature(signature)
return self._base64_encode(byte_array)
@danew
danew / cssMock.js
Created January 9, 2018 20:08
Jest mock for CSS that supports multiple CSSModules
module.exports = new Proxy({}, { get: () => 'cssMock' });
@danew
danew / Mastermind.js
Created October 8, 2018 00:33
Small sample for the Mastermind problem solving game
class Mastermind {
constructor() {
this.answer = [];
this.blocks = [
'green',
'red',
'blue',
'yellow',
];
}
@danew
danew / verifierAndChallenge.js
Created March 21, 2019 16:03
PKCE in the browser
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_3_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
const base64 = require('base64-js');
const urlSafe = (buffer) => {
const bytes = new Uint8Array(buffer);
return base64.fromByteArray(bytes)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
};
@danew
danew / Logger.js
Created October 24, 2019 20:36
Simple logger for browser
import axios from 'axios';
let active = true;
const save = (message, level = 'info') => {
if (process.env.NODE_ENV !== 'production') {
if (active) {
axios.post('http://localhost:9999/log', { level, message }).catch(() => {
active = false;
});
@danew
danew / logg.js
Last active February 13, 2020 01:29
Console log with colours and can filter by /\*/
window.log = function(category, ...attr) {
const str = `%c* %c${category} %c[%c ${attr.join(', ')} %c]`;
console.log(
str,
'color:white',
'color:red; font-weight:bold',
'font-weight:bold',
'color:blue',
'font-weight:bold'
);
@danew
danew / intro.md
Last active July 21, 2020 11:53
Headless Chrome lighthouse with a subset of audits

This is a simple script to run a few performance audits with Chrome's Lighthouse audit tool to help verify if you are making any performance improvements while coding.

It will print out an audit if it is outside of the given threshold; in green if it's improved or red if it hasn't.

Getting started

Run npm i -D lighthouse pretty-ms chalk
Run node test

@danew
danew / lintr
Last active October 6, 2020 19:25
ESlint pre-commit hook
#!/bin/bash
FILE_LIST=$(git diff --staged --diff-filter=ACMTUXB --name-only | grep -E \\.[tj]sx?$)
if [ -z "$FILE_LIST" ]; then
echo "Nothing to lint"
exit 0
fi
if ! npx eslint --fix --max-warnings 0 $FILE_LIST; then
@danew
danew / broadcast-channel.ts
Created December 6, 2022 17:56
BroadcastChannel polyfill for Jest
import { EventEmitter } from 'node:events';
const channels: Map<string, EventEmitter> = new Map();
class MockBroadcastChannel implements BroadcastChannel {
private emitter: EventEmitter;
constructor(public name: string) {
if (channels.has(name)) {
this.emitter = channels.get(name);