Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View rjz's full-sized avatar
👋
Hey there!

RJ Zaworski rjz

👋
Hey there!
View GitHub Profile
@rjz
rjz / handler.go
Last active March 26, 2024 23:40
Handle Github webhooks with golang
// Now available in package form at https://github.com/rjz/githubhook
package handler
// https://developer.github.com/webhooks/
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"errors"
@rjz
rjz / crypto-aes-256-gcm-demo.js
Last active March 11, 2024 20:11
example using node.js crypto API with aes-256-gcm
const buffer = require('buffer');
const crypto = require('crypto');
// Demo implementation of using `aes-256-gcm` with node.js's `crypto` lib.
const aes256gcm = (key) => {
const ALGO = 'aes-256-gcm';
// encrypt returns base64-encoded ciphertext
const encrypt = (str) => {
// The `iv` for a given key must be globally unique to prevent
@rjz
rjz / ngrok_hostname.sh
Created August 9, 2016 16:20
Get ngrok hostname from command line
#!/bin/sh
# ngrok's web interface is HTML, but configuration is bootstrapped as a JSON
# string. We can hack out the forwarded hostname by extracting the next
# `*.ngrok.io` string from the JSON
#
# Brittle as all get out--YMMV. If you're still reading, usage is:
#
# $ ./ngrok_hostname.sh <proto> <addr>
#
@rjz
rjz / has_content_type.go
Last active October 31, 2023 06:55
Validate golang http.Request content-type
import (
"mime"
"net/http"
"strings"
)
// Determine whether the request `content-type` includes a
// server-acceptable mime-type
//
// Failure should yield an HTTP 415 (`http.StatusUnsupportedMediaType`)
@rjz
rjz / codec.ts
Created August 8, 2023 22:41
TypeScript wrapper for adding HMAC signatures to an existing codec / SerDe implementation
/** `x/crypto` if running in `deno` */
import { createHmac } from 'node:crypto'
/** A signing secret */
const HMAC_SECRET = '<YOUR SIGNING SECRET HERE>'
/** Length of a hexidecimal HMAC digest */
const HMAC_LENGTH = 64
function hmac(message: string): string {
/**
* Represents an error when an unreachable variant is encountered at runtime
*/
export class ExhaustiveCheckError<T> extends TypeError {
public instance: T;
public readonly isUnexpected = true;
constructor(msg: string, instance: T) {
super(msg);
@rjz
rjz / stdin-and-fs-stream.js
Created March 12, 2014 05:18
Streaming from stdin or a file
// Depends on `through`
//
// $ npm install through
//
// Usage:
//
// $ echo 'hello' | node stdin-and-fs-stream.js
// $ echo 'hello' > tmp && node stdin-and-fs-stream.js tmp
//
var fs = require('fs'),
@rjz
rjz / projects_spec.ts
Created September 30, 2016 19:22
Testing with fetch, fetchmock, and TypeScript
// Example of mocking out global `fetch`
//
// Spec works using Jasmine (via Karma) and _either_ tsc or babel.
//
// $ npm install --save-dev fetch-mock whatwg-fetch
//
// If using `tsc`, grab the type definitions as well:
//
// $ typings --save --global dt~fetch-mock dt~whatwg-fetch
@rjz
rjz / Vagrantfile
Created November 13, 2014 18:10
Vagrant with MariaDB provisioned (ansible)
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
@rjz
rjz / pdf_to_booklet.sh
Created January 5, 2020 01:13
Convert PDF pages into a printable booklet on Linux using psutils
#! /bin/bash
#
# Use `psutils` to convert a PDF into a printable booklet.
#
# Usage:
#
# $ ./pdf_to_booklet.sh my_file.pdf
#
# Distributed under terms of the MIT license.