Skip to content

Instantly share code, notes, and snippets.

View henbr's full-sized avatar

Henrik Brandt henbr

View GitHub Profile
import Fastify, { FastifyInstance, FastifyPluginAsync } from 'fastify'
import { Pool } from 'pg'
interface User {
id: string
email: string
}
interface UserStore {
findBySession(sessionId: string): Promise<User | null>
@henbr
henbr / create-getter-setter.ts
Created March 15, 2025 12:05
Getter / setter creator function experiment
type Thing = {
id: string;
category: string;
name: string;
};
const cache: Record<string, any> = {};
function createGetSet<
F extends (...args: any[]) => Promise<any>,
@henbr
henbr / build-deps.sh
Created March 8, 2025 11:47
Script to that uses NX to build or watch all deps for a project in a monorepo
#! /bin/bash
# This script builds or watches all dependencies of a project.
#
# Example: build all depdencies
# ./build-dependencies.sh my-app build
show_help_and_exit() {
echo "Incorrect or missing arguments"
echo "Usage: $0 <project> <build|watch>"
@henbr
henbr / hello.ts
Created January 29, 2025 06:17
Good enough string encryption
import crypto from "crypto";
const SALT_LENGTH = 8;
function encrypt(password: string, text: string) {
const iv = crypto.randomBytes(16);
const key = Buffer.concat([Buffer.from(password)], 16);
const cipher = crypto.createCipheriv("aes128", key, iv);
const salt = crypto.randomBytes(SALT_LENGTH);
const enc = Buffer.concat([cipher.update(salt), cipher.update(text), cipher.final()]);
@henbr
henbr / zsh_history_to_fish_history.ts
Created December 30, 2024 09:50
Converts zsh_history to fish_history
// Run:
// bun run zsh_history_to_fish_history.ts > ~/.local/share/fish/fish_history
const zshData = await Bun.file(".zsh_history").text();
const lines = zshData.split("\n");
let cmdBuffer = "";
let when = 0;
function flush() {
if (cmdBuffer.trim()) {