Skip to content

Instantly share code, notes, and snippets.

View Ethan-Arrowood's full-sized avatar
🐢

Ethan Arrowood Ethan-Arrowood

🐢
View GitHub Profile
@Ethan-Arrowood
Ethan-Arrowood / undici-fin.js
Last active July 15, 2025 21:04
A simplified example of Undici properly terminating connection when a FIN packet is forcibly sent
import { createServer } from 'node:http';
import { Client } from 'undici';
const server = createServer(
{ keepAlive: true, keepAliveTimeout: 20 * 1000 },
(req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain',
connection: 'keep-alive',
'keep-alive': 'timeout=20',
@Ethan-Arrowood
Ethan-Arrowood / undici-pool-test.js
Last active July 15, 2025 15:11
A short demonstration of how Undici's `Pool` manages connections and keep-alive directives.
import { createServer } from 'node:http';
import { Pool } from 'undici';
// Create a simple HTTP server that is configured with a 15s keep-alive timeout
// And includes the 'Keep-Alive' header in responses
const server = createServer(
{ joinDuplicateHeaders: true, keepAlive: true, keepAliveTimeout: 15 * 1000 },
(req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain',
@Ethan-Arrowood
Ethan-Arrowood / poll.js
Created March 25, 2025 20:30
A polling function for use in Node.js unit tests.
import { setInterval } from 'node:timers/promises';
import assert from 'node:assert/strict';
/**
* A simple time-based polling function with a default timeout of 100ms.
*
* Some of the tests in this file require waiting for the file watcher to detect changes.
* Files systems are not precise, and this is the best way to test file watcher behavior.
*
* If a test is flaky because of this mechanism, first consider what might be causing the file system to be slow (i.e. blocking I/O).
* 100ms is quite short so for large operations (such as a large file write), it may be necessary to provide a longer timeout.
@Ethan-Arrowood
Ethan-Arrowood / syllabus.md
Created July 21, 2021 16:00
Learn WebSockets

Module 1 - Introduction to WebSockets

Resources:

Lesson 1 - Upgrading HTTP

This lesson will discuss the natural progression from HTTP to WebSockets. It should cover topics such as the Upgrade header, the client/server handshake, transfer of data, and connection persistance. All of these concepts and more are covered in the MDN documentation. Security should also be covered in this lesson

@Ethan-Arrowood
Ethan-Arrowood / connectionMonitor.js
Created July 2, 2021 22:09
Connection Monitor WIP
const { Machine, interpret, send, sendParent } = require("xstate");
const https = require('https');
const CONNECTION_MONITOR_IPC_CHANNEL = 'connection-monitor'
const PING_IPC_CHANNEL = 'ping'
const CONNECTION_MONITOR_EVENTS = {
TOGGLE_PING: 'toggle ping',
CONNECT: 'connect',
DISCONNECT: 'disconnect'
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@Ethan-Arrowood
Ethan-Arrowood / machine.js
Last active July 2, 2021 17:41
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
Benchmark results for Headers class
Tested modules: undici-fetch, node-fetch, ./implementations/mapHeaders.js
Benchmark Suite results for operation: append
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ Module β”‚ Total Time β”‚ Result β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 0 β”‚ 'undici-fetch' β”‚ '552168ns (0.552ms)' β”‚ null β”‚
β”‚ 1 β”‚ 'node-fetch' β”‚ '1095718ns (1.096ms)' β”‚ '98.439% slower than undici-fetch' β”‚
β”‚ 2 β”‚ './implementations/mapHeaders.js' β”‚ '624987ns (0.625ms)' β”‚ '13.188% slower than undici-fetch' β”‚
@Ethan-Arrowood
Ethan-Arrowood / connection.ts
Created May 29, 2021 03:27
A simplified state-machine like connection class
type ConnectionContext = {
state: 'disconnected'
methods: {
connect: () => Promise<void>
}
} | {
state: 'connected',
data: {
endpoint: string
},
@Ethan-Arrowood
Ethan-Arrowood / stoplight.js
Created May 22, 2021 19:13
A Stop Light state machine class built on Node.js EventEmitter
import { deepStrictEqual } from 'assert/strict';
import { EventEmitter } from 'events';
class StopLight extends EventEmitter {
static _data = new Map([
[ 'GO', { color: 'green' } ],
[ 'STOP', { color: 'red' } ],
[ 'SLOW', { color: 'yellow' } ]
])