Skip to content

Instantly share code, notes, and snippets.

View mxdvl's full-sized avatar
🧭
I love me an SVG

Max Duval mxdvl

🧭
I love me an SVG
View GitHub Profile
@qntm
qntm / verify.js
Last active February 9, 2026 08:48
Effective user age verification without handling photographs, credit card numbers, government ID etc.
import timersPromises from 'node:timers/promises'
const FEB = 1
const MAR = 2
// `setTimeout` disallows numbers which don't fit into a signed 32-bit integer.
// In other words
const MAX_DELAY = 2 ** 31 - 1
const addYears = (date, numYears) => {
/**
* Requires a stream marked as `type "bytes"`, this can support zero-copy mode,
* which might make it slightly more efficient, but it's a little more complex
* than the code for not using `{ mode: "byob" }`
*/
async function peekByob(input: ReadableStream<Uint8Array>, length: number) {
const [peekable, output] = input.tee()
const reader = peekable.getReader({ mode: "byob" })
const peeked = await readByobBytes(reader, length)
reader.cancel()
@Daanieeel
Daanieeel / next-auth-race-condition-fix.md
Last active January 21, 2026 00:55
Defeat race condition with next-auth jwt token refresh

Defeat race condition with next-auth jwt token refresh

Problem

When using Next.js with next-auth (auth.js) and a backend API (e.g., via tRPC), you may encounter a race condition during JWT token refresh. This typically happens when:

  • Middleware checks the user's session and refreshes the access token if expired.
  • Immediately after, the client (e.g., tRPC) makes a request using the (now stale) access token, triggering another refresh attempt.
  • Both refresh attempts use the same old refresh token, but only the first one succeeds. The second fails with a 401 error because the refresh token has already been invalidated.
@acutmore
acutmore / ts-blank-space-doc.md
Last active December 8, 2025 18:14
Learnings from 'ts-blank-space`

Learnings from ts-blank-space

tags: TypeScript, type erasure, type stripping

ts-blank-space

As part of my work on the JavaScript Tooling team at Bloomberg I have implemented an experimental (not yet used in production) package to transform TypeScript into JavaScript using a somewhat novel approach.

This is a description of what I learned from implementing the idea. The source code will be open sourced soon - it just needs some regular IP approval.

@jonathonherbert
jonathonherbert / script.sh
Last active April 22, 2024 15:26
Select git branch by date (requires fzf)
fbr() {
local branches branch
branches=$(git reflog show --pretty=format:'%gs ~ %gd' --date=relative \
| grep 'checkout:' \
| grep -oE '[^ ]+ ~ .*' \
| awk -F~ '!seen[$1]++' \
| head -n 20 \
| awk -F' ~ HEAD@{' '{printf(" \033[33m%s: \033[37m %s\033[0m\n", substr($2, 1, length($2)-1), $1)}' \
| nl \
| sort -nr \
@jakelazaroff
jakelazaroff / i-frame.js
Last active December 4, 2023 10:16
simple web component that sandboxes its slotted elements inside an iframe
customElements.define(
"i-frame",
class extends HTMLElement {
#shadow = this.attachShadow({ mode: "closed" });
constructor() {
super();
this.#shadow.innerHTML = `
<slot></slot>
<iframe part="frame" srcdoc=""></iframe>
function check_package_manager() {
current_dir=$(pwd)
while [ "$(pwd)" != "/" ]; do
if [ -f "pnpm-lock.yaml" ]; then
if [ "${1}" != "pnpm" ]; then
echo "Looks like you're using the wrong package manager, partner!"
echo "It's pnpm time, cowboy."
cd "${current_dir}"
return 1
@feoktant
feoktant / Day4.scala
Created December 4, 2022 22:13
Day4
val InputRegex = "(\\d+)-(\\d+),(\\d+)-(\\d+)".r
def matchFullContain(str: String): Boolean = str match {
case InputRegex(t1, t2, e1, e2) =>
(t1.toInt <= e1.toInt && e2.toInt <= t2.toInt) ||
(e1.toInt <= t1.toInt && t2.toInt <= e2.toInt)
case _ => false
}
def matchOverlap(str: String): Boolean = str match {
@bryophyta
bryophyta / stack-traces.ts
Created August 19, 2022 16:03
Experiment: Stack traces with TypeScript
// `CallSite` is the class representing a stack frame in the V8 spec
// (see https://v8.dev/docs/stack-trace-api#customizing-stack-traces)
// hat tip to the callsites package, where I'm importing the `CallSite` type from: https://github.com/sindresorhus/callsites
import { CallSite } from "https://raw.githubusercontent.com/sindresorhus/callsites/7fb22a67645ea741cc016937e8f3662eb533eb6e/index.d.ts";
// there is a default implementation for preparing stack traces on the Error object, but TypeScript doesn't recognise
// this. (maybe because it's not present in all runtimes? or maybe the default is initialised in some other way?)
type ErrorWithPrepFn = ErrorConstructor & { prepareStackTrace?: (error: Error, stack: CallSite[]) => unknown}
@mxdvl
mxdvl / json.ts
Last active December 8, 2025 15:43
Get a typed JSON from any API
import { go, no, type Result } from "./result.ts";
/**
* Fetch JSON data and parse it safely.
*/
export const fetchJSON = async <T>(
url: Parameters<typeof fetch>[0],
{
headers,
parser,