Skip to content

Instantly share code, notes, and snippets.

View laat's full-sized avatar
💤

Sigurd Fosseng laat

💤
View GitHub Profile
@laat
laat / vite-plugin-selfpack.js
Created August 22, 2026 16:35
Single-file self-extracting HTML packer for Vite
// vite-plugin-selfpack.js
//
// DecompressionStream('brotli') support (current engines):
// Chromium — long-standing · Safari 18.4+ · Firefox 147+
// Fallback for older builds: 'deflate-raw' (universal since 2023).
import zlib from 'node:zlib';
export default function selfpack({
format = 'brotli', // 'brotli' | 'deflate-raw' | 'gzip'
fileName = 'index.html',
const sleep = (s) =>
new Promise((resolve) =>
setTimeout(() => resolve(s), s * 1000)
);
const sleepSort = async (arr) => {
const res = [];
await Promise.all(
arr
.map(sleep)
const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65];
const reduceSum = arr => arr.reduce((acc, val) => acc + val);
const forEachSum = arr => {
let sum = 0;
arr.forEach(el => sum += el);
return sum;
};
const forSum = arr => {
let sum = 0;
for (let i = 0; i < arr.length; i++ ) sum += arr[i];
@laat
laat / sse.http2.ts
Last active February 9, 2022 09:40
Server-Sent Events over HTTP/2 on Node.JS
Relays messages published on a redis channel over Server-Sent Events.
import mime from "mime";
import { stat } from "node:fs/promises";
import http2, { Http2ServerRequest, Http2ServerResponse } from "node:http2";
import { join } from "node:path";
export type Http2Handler = (
next?: (
req: Http2ServerRequest,
res: Http2ServerResponse
) => Promise<void> | void

Invert Binary Tree in F#

graph graph-inverted

An inverted Binary Tree is a Binary Tree with left and right children of all non-leaf nodes interchanged.

type Node =
  { Value: int
@laat
laat / episodes_in_wrong_series.sql
Last active May 14, 2021 19:02
Episodes in wrong TV Series.
WITH
external_data
/*
In an external system, we have some episode to series relations.
An episode can be in multiple series.
The SERIES_ID is monotonically increasing database ID.
*/
AS (SELECT 10 SERIES_ID, 1 EPISODE_ID FROM dual UNION ALL
SELECT 2 SERIES_ID, 1 EPISODE_ID FROM dual),
transformed_data
@laat
laat / 1-json-schema.fsx
Last active October 13, 2022 20:54
JsonSchema.Net validation in F#
#!/usr/bin/env -S dotnet fsi --quiet
#r "nuget: JsonSchema.Net"
//
// This file contains a basic example of json schema validation
//
open Json.Schema
open System.Text.Json
let schema =

The times macro

(defmacro laat/times (n fn arg) 
  (cons 'thread-last (cons arg (cl-loop for i below n collect fn))))
(laat/times 3 sqrt 256)
@laat
laat / aoc.org
Created December 1, 2020 23:03
org-mode and elisp

Advent Of Code 2020

Using org-mode and Emacs Lisp to solve Advent of Code.

(org-babel-lob-ingest "./library-of-babel.org")

Day 1

Part 1