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 / 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)
@Ethan-Arrowood
Ethan-Arrowood / mergesort.js
Created April 5, 2021 21:29
An in-place merge sort algorithm for a flattened array of pairs (i.e. Header entries)
function merge(arr, start, mid, end) {
// console.log(`m(${start}, ${mid}, ${end}) -> ${arr.slice(start, end)}`)
for (let i = mid; i < end; i+=2) {
let p = i
for (let j = i - 2; j >= start; j-=2) {
if (arr[p] < arr[j]) {
[
arr[p], arr[p+1],
arr[j], arr[j+1]