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 / 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' } ]
])
@Ethan-Arrowood
Ethan-Arrowood / output
Created April 25, 2021 14:03
Worker based unref test (currently failing). Place both files in a directory and run `node unref-when-idle.js`
node test/unref-when-idle/unref-when-idle.js
TAP version 13
# Subtest: test unref
1..0
ok 1 - test unref # time=6.285ms
TAP version 13
# Subtest: test unref worker
ok 1 - should be equal
1..1
@Ethan-Arrowood
Ethan-Arrowood / array.js
Created April 5, 2021 22:37
Rough perf benchmark for map vs array based header class
'use strict'
const { types } = require('util')
const { validateHeaderName, validateHeaderValue } = require('http')
const kHeaders = Symbol('headers')
function normalizeAndValidateHeaderName (name) {
const normalizedHeaderName = name.toLowerCase()
validateHeaderName(normalizedHeaderName)