Skip to content

Instantly share code, notes, and snippets.

View brandonhimpfen's full-sized avatar
:octocat:

Brandon Himpfen brandonhimpfen

:octocat:
View GitHub Profile
@brandonhimpfen
brandonhimpfen / node-split-jsonl-file.js
Created March 6, 2026 02:12
Split a large JSONL/NDJSON file in Node.js into smaller chunks by line count or approximate size, using streams and backpressure (no deps).
#!/usr/bin/env node
/**
* Split a large JSONL (NDJSON) file into chunks by line count or size.
*
* Features:
* - Streams input line-by-line
* - Writes chunk files incrementally
* - Supports splitting by:
* --max-lines=N
* --max-bytes=N
@brandonhimpfen
brandonhimpfen / node-transform-jsonl-stream.js
Created March 6, 2026 02:10
Stream-transform JSONL/NDJSON in Node.js with filter/map style processing, stdin to stdout, and proper backpressure handling (no deps).
#!/usr/bin/env node
/**
* Stream-transform JSONL (NDJSON) from stdin to stdout with backpressure.
*
* Features:
* - Reads JSONL from stdin
* - Parses one JSON object per line
* - Applies transform/filter logic
* - Writes JSONL to stdout
* - Respects stdout backpressure
@brandonhimpfen
brandonhimpfen / node-write-jsonl-atomic.js
Last active March 1, 2026 19:51
Stream-write JSON Lines (JSONL/NDJSON) to a file in Node.js safely using a temp file + atomic rename (no deps).
#!/usr/bin/env node
/**
* Stream-write JSONL (NDJSON) safely with atomic write.
*
* Pattern:
* 1) Write to a temp file in the same directory
* 2) fsync (best-effort) to reduce risk of partial writes
* 3) Rename temp -> final (atomic on the same filesystem)
*
* Usage:
@brandonhimpfen
brandonhimpfen / node-parse-delimited-streaming.js
Created March 1, 2026 19:48
Parse a delimited (CSV-like) file line-by-line in Node.js with streaming output (no deps, supports quotes, header mapping, and large files).
#!/usr/bin/env node
/**
* Parse a delimited file (CSV-like) line-by-line with streaming output (no deps).
*
* Features:
* - Streams the input file (memory efficient)
* - Supports a delimiter (default: comma)
* - Handles quoted fields (basic CSV rules: "escaped "" quotes")
* - Optional header row -> outputs objects
* - Outputs JSON Lines (NDJSON) to stdout (great for pipelines)
@brandonhimpfen
brandonhimpfen / node-read-large-file-line-by-line.js
Created February 25, 2026 18:08
Read a large file line-by-line in Node.js using streams + readline (memory efficient, backpressure-friendly).
#!/usr/bin/env node
/**
* Read a large file line-by-line (streams) in Node.js.
*
* Usage:
* node node-read-large-file-line-by-line.js ./bigfile.txt
*
* Notes:
* - Uses streams to avoid loading the entire file into memory.
* - readline provides a clean async iterator interface.
@brandonhimpfen
brandonhimpfen / python-sqlite-migrations-no-deps.py
Created February 22, 2026 20:07
Dependency-free SQLite migrations for Python: migrations table + apply migrations in order with transaction safety.
#!/usr/bin/env python3
"""
Basic SQLite migrations runner (no dependencies).
Pattern:
- Keep a list of migrations (id, name, sql)
- Store applied migrations in a table: schema_migrations
- Apply missing migrations in order inside a transaction
Good for:
@brandonhimpfen
brandonhimpfen / python-sqlite-helper.py
Created February 22, 2026 20:05
Basic SQLite helper for Python: connect with sensible defaults, execute/query helpers, and dict-like rows (no deps).
#!/usr/bin/env python3
"""
Basic SQLite helper (no dependencies).
Features:
- Context-managed connection
- Row results as dict-like objects (sqlite3.Row)
- Helpers for execute / executemany / fetchone / fetchall
- Optional pragmas for better defaults
@brandonhimpfen
brandonhimpfen / bash-retry-with-jitter.sh
Created February 15, 2026 21:05
Bash retry helper with exponential backoff + random jitter (useful for distributed systems and flaky network calls).
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Bash retry helper with exponential backoff + jitter
# -----------------------------------------------------------------------------
# Why jitter?
# - If many clients retry at the same fixed interval, they can synchronize and
# cause thundering herds. Jitter spreads retries out randomly.
#
# Usage:
# retry_jitter <attempts> <base_delay_seconds> <max_delay_seconds> <command...>
@brandonhimpfen
brandonhimpfen / bash-retry.sh
Created February 15, 2026 21:03
Bash retry helper to retry a command N times with delay and optional exponential backoff (strict-mode friendly).
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Bash retry helper
# -----------------------------------------------------------------------------
# Usage:
# retry <attempts> <delay_seconds> <command...>
#
# Example:
# retry 5 2 curl -f https://example.com
#
@brandonhimpfen
brandonhimpfen / html-responsive-images-srcset-sizes.html
Created February 8, 2026 23:00
Responsive image patterns using srcset + sizes (with WebP/AVIF via <picture>), plus practical examples for width- and density-based images.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Responsive Images: srcset + sizes</title>
<style>
/* Demo-only styling */
.container { max-width: 900px; margin: 2rem auto; padding: 0 1rem; }
img { max-width: 100%; height: auto; display: block; border-radius: 12px; }