Skip to content

Instantly share code, notes, and snippets.

View lithdew's full-sized avatar

Kenta Iwasaki lithdew

View GitHub Profile
@lithdew
lithdew / Dockerfile
Created April 4, 2024 20:31
Dockerfile for deploying a Next.js standalone bundle on Fly.io with Bun.
# syntax = docker/dockerfile:1
# Adjust BUN_VERSION as desired
ARG BUN_VERSION=1.1.1
FROM oven/bun:${BUN_VERSION}-slim as base
LABEL fly_launch_runtime="Next.js"
# Next.js app lives here
WORKDIR /app
@lithdew
lithdew / README.md
Last active March 15, 2024 10:04
typescript(bun): robin hood hash map
% bun run index.ts
Inserting items: 6e340b9cffb37a98, 4bf5122f344554c5, dbc1b4c900ffe48d, 084fed08b978af4d, e52d9c508c502347, e77b9a9ae9e30b0d, 67586e98fad27da0, ca358758f6d27e6c
Are all items in the map? true
Ascending order: 084fed08b978af4d, 4bf5122f344554c5, 67586e98fad27da0, 6e340b9cffb37a98, ca358758f6d27e6c, dbc1b4c900ffe48d, e52d9c508c502347, e77b9a9ae9e30b0d
Descending order: e77b9a9ae9e30b0d, e52d9c508c502347, dbc1b4c900ffe48d, ca358758f6d27e6c, 6e340b9cffb37a98, 67586e98fad27da0, 4bf5122f344554c5, 084fed08b978af4d

Deleting item: ca358758f6d27e6c
Was the item found and deleted? true
Ascending order: 084fed08b978af4d, 4bf5122f344554c5, 67586e98fad27da0, 6e340b9cffb37a98, dbc1b4c900ffe48d, e52d9c508c502347, e77b9a9ae9e30b0d
@lithdew
lithdew / index.ts
Created July 12, 2023 20:25
react server action zod validation
import { z } from "zod";
import { fromZodError } from "zod-validation-error";
export function procedure<TSchema extends z.ZodTypeAny = z.ZodNever>(inputSchema?: TSchema) {
type InputArgs = TSchema extends z.ZodNever ? [] : [z.output<TSchema>]
type TransformedArgs = TSchema extends z.ZodNever ? [] : [z.input<TSchema>]
return {
// eslint-disable-next-line no-unused-vars
do<TOutput>(fn: (...input: InputArgs) => TOutput) {
@lithdew
lithdew / oneshot_broadcast.rs
Created May 13, 2023 10:26
rust: single-threaded futures-aware one-shot broadcast channel
pub mod oneshot_broadcast {
use std::{
cell::UnsafeCell,
pin::Pin,
task::{Context, Poll, Waker},
};
use futures_lite::Future;
use pin_list::PinList;
use pin_project_lite::pin_project;
@lithdew
lithdew / Cargo.toml
Created May 4, 2023 10:42
rust (quinn, rustls): quic holepunching w/ basic stun client
[package]
name = "quic-holepunching"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.71"
bitflags = "2.2.1"
@lithdew
lithdew / main.ts
Last active May 11, 2023 06:31
deno (udp holepunching): basic stun client for fetching public-facing ip:port
// deno run -A --unstable main.ts
export const classToValue = {
request: 0x0000,
indication: 0x0010,
success: 0x0100,
failure: 0x0110,
};
export const methodToValue = {
@lithdew
lithdew / lexicographic_hash_map.zig
Created August 31, 2022 11:06
zig: lexicographic hash map
const std = @import("std");
pub fn LexicographicHashMap(comptime V: type, comptime capacity: u32) type {
const shift = @bitSizeOf(u64) - 1 - std.math.log2_int(u64, capacity) + 1;
const overflow = capacity / 10 + (@bitSizeOf(u64) - 1 - shift + 1) << 1;
const num_entries = capacity + overflow;
return struct {
pub const Entry = packed struct {
key: u32 = 0,
@lithdew
lithdew / sparse.zig
Created June 26, 2022 09:26
zig: generational paged sparse set
const std = @import("std");
const sparse = @This();
/// This is an implementation of a paged sparse set that stores the payload in
/// the sparse array.
//
/// A sparse set has a dense and a sparse array. The sparse array is directly
/// indexed by a 64 bit identifier. The sparse element is linked with a dense
/// element, which allows for liveliness checking. The liveliness check itself
@lithdew
lithdew / main.zig
Created November 1, 2021 14:21
zig: x25519 handshake
const std = @import("std");
pub fn main() !void {
const server_keys = try std.crypto.sign.Ed25519.KeyPair.create(null);
const client_keys = try std.crypto.sign.Ed25519.KeyPair.create(null);
std.log.info("server secret key: {s}", .{std.fmt.fmtSliceHexLower(&server_keys.secret_key)});
std.log.info("server public key: {s}", .{std.fmt.fmtSliceHexLower(&server_keys.public_key)});
std.log.info("client secret key: {s}", .{std.fmt.fmtSliceHexLower(&client_keys.secret_key)});
@lithdew
lithdew / io_uring.go
Last active July 24, 2021 18:31
epoll (go) vs. io_uring (zig)
// GOMAXPROCS=8 go run io_uring.go
package main
import (
"bufio"
"fmt"
"net"
"runtime"
"sync/atomic"