Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
pesterhazy / javascript-day-of-week.js
Created January 9, 2025 12:40
Sad Sunday: The great JavaScript day-of-week confusion
/**
* Returns a normalized weekday int from 0..6
*
* Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6
*
* This has caused a number of bugs so an explanation is in
* order. Javascripts's Date#getDay() methods returns a value from
* Sun=0...Sat=6. In other words, the week starts on Sunday. However,
* the ISO 8601 week starts on Monday, so the int values range from
* Mon=1...Sun=7. The numeric values for Mon...Sat are the same, but
@pesterhazy
pesterhazy / enemy-of-the-state.md
Last active December 7, 2024 22:13
Websocket Deployments — Enemy of the State

Websocket Deployments — Enemy of the State

How do you deploy new code continuously into a system that needs to support stateful, uninterruptible computations?

Some workloads are inherently stateful

As web developers, we're used to valuing stateless computation, for good reason – reducing state makes everything easier. Servers (or cloud instances) hold no essential state, delegating any important data to databases like Postgres and Redis. This is a great architecture. Among other benefits, it makes zero-downtime deployments a breeze: to deploy a new code version, you simply spin up a set of new instances with the new git sha, add them to the load balancer, and kill the old instances after draining them of traffic (perhaps after 60 s).

However, some workloads don't fit this "short-duration request & response" mold. As an example, consider connecting OpenAI Realtime to Twilio's API, which requires you to:

@pesterhazy
pesterhazy / test.js
Created November 28, 2024 08:11
Does return await create better stack traces?
async function bar() {
throw new Error("boom");
}
async function foo() {
return await bar();
// same stacktrace printed with return bar();
}
function test() {
@pesterhazy
pesterhazy / why-classes-in-typescript.md
Last active November 28, 2024 12:40
Why Use Classes in TypeScript?

Why use classes in TypeScript?

There are many reasons why people like Object-Oriented Programming, but personally, my main reason for the use of classes in TypeScript is making dependencies explicit and replaceable. For example, suppose you have a module, Calendar, which depends on another module, GCalClient - i.e. Calendar uses the functionality from GCalClient but not vice versa. The most straightforward way to write that code is by simply using the functionality from GCalClient when needed:

// calendar.ts

import {bookAppointment} from "google-calendar-client"

function createMeeting() {
@pesterhazy
pesterhazy / tech-interviews.md
Last active June 1, 2024 19:43
Interviewing for Jobs in Tech: a Reading List

Behavioral questions (aka "Tell me about a time when...")

@pesterhazy
pesterhazy / macos-setup.md
Last active January 25, 2024 15:39
Setting up macOS

This guide helps me set up my macOS laptop in a fairly simple manner. I'll need to go back to this guide roughly every 2–3 years. Maybe it'll help you, too!

Smart Caps Lock

Use Karabiner Elements

Map Caps Lock to Ctrl (when long-pressed) or Escape (when tapped). See this guide

Put backtick/tilde character to the left of 1 key

@pesterhazy
pesterhazy / .zshrc
Created November 13, 2023 15:26
ZSH: show current git branch on enter
# This prints the current git branch when you submit an empty
# line (i.e. press enter) in zsh
#
# Less distracting than adding it to your prompt, but still
# very easy to run.
git-status-on-enter () {
if [ ${#${(z)BUFFER}} -eq 0 ]; then
if git rev-parse --git-dir > /dev/null 2>&1 ; then
echo
@pesterhazy
pesterhazy / third-party-failures.md
Created November 2, 2023 12:58
HTTP endpoints and handling third party service failures

Let's say I'm building a backend endpoint /find-icon?q=apple as part of a large monolithic app. What it does is simply forward the request to a 3rd party service, FindIcon.com. Essentially it's a proxy except that it adds FindIcon.com API credentials.

What should this endpoint do in the case of an 400–599 status response from the 3rd party service? Specifically:

First, what status code should /find-icon return in case of a third-party error? Each class of codes seems to have downsides:

  • 20x hides the error from the client. For example, it doesn't show up in red in the browser tab, making debugging harder.
  • 40x seems semantically wrong, given that, according to the HTTP spec, "the 4xx (Client Error) class of status code indicates that the client seems to have erred".
  • 50x on the backend or Load Balancer typically causes backend alerts, which are set to trigger when X amount of 50x errors occur in a given time frame. This will cause an on-call incident, waking up people, which may or may not be what
@pesterhazy
pesterhazy / README.md
Last active October 10, 2023 16:25
Clojure: timing for namepsace loading

This is intended to help with profiling slow Clojure startup times:

  • Create a csv with start/end times for loading namespaces
  • Analyzer script that measures how long each namespace take (substracting time that's needed for loading its dependencies)

Requires building your own patched version of Clojure

Sample output (time in milliseconds):