Skip to content

Instantly share code, notes, and snippets.

View ggoodman's full-sized avatar

Geoff Goodman ggoodman

View GitHub Profile
@ggoodman
ggoodman / well_known_error.test.js
Last active February 5, 2025 03:14
Reusable TypeScript well-known error class
// @ts-check
///<reference types="mocha" />
const assert = require('node:assert/strict');
// Since the source file is TypeScript, we have to require the module itself and
// rely on `npm run build` having been called before running tests. ☹️
const { WellKnownError } = require('./well_known_error');
@ggoodman
ggoodman / JSONDecoder.js
Created September 4, 2024 14:45
Streaming JSON decoder POC
// @ts-check
const CURLY_OPEN = '{'.charCodeAt(0);
const CURLY_CLOSE = '}'.charCodeAt(0);
const SQUARE_OPEN = '['.charCodeAt(0);
const SQUARE_CLOSE = ']'.charCodeAt(0);
const DOUBLE_QUOTE = '"'.charCodeAt(0);
const CHARACTER_COLON = ':'.charCodeAt(0);
const CHARACTER_COMMA = ','.charCodeAt(0);
const CHARACTER_BACKSLASH = '\\'.charCodeAt(0);
@ggoodman
ggoodman / graphBuilder.ts
Created April 18, 2021 02:25
Exploration of building a module graph 'quickly' using esbuild and enhanced-resolve
import {
CachedInputFileSystem,
FileSystem,
ResolveContext,
ResolverFactory,
} from 'enhanced-resolve';
import { build, ImportKind, Loader } from 'esbuild';
import * as Fs from 'fs';
import Module from 'module';
import * as Path from 'path';
@ggoodman
ggoodman / README.md
Created February 25, 2021 15:14
Using the concept of defer from golang to simplify resource cleanup in Javascript

The withCleanup helper

Example

const result = await withCleanup(async (defer) => {
  const fileHandle = await getFileHandle();
  defer(() => fileHandle.close());
   
 // Carry on
@ggoodman
ggoodman / luggage.ts
Last active February 19, 2021 18:17
Luggage - Because sometimes your Requests don't pack lightly.
export interface AsyncLuggageFactory<TLuggage, TTraveler extends {}> {
(obj: TTraveler): PromiseLike<TLuggage>;
}
export interface AsyncLuggage<TLuggage, TTraveler extends {}> {
get(obj: TTraveler): Promise<TLuggage>;
}
const swallow = () => undefined;
@ggoodman
ggoodman / styled.ts
Created January 29, 2021 03:04
Experiment making styled-components with twind
import * as React from 'react';
import { tw } from 'twind';
import type { Context } from 'twind';
import { css } from 'twind/css';
import { TwindContext } from './internal';
export type StyledIntrinsicFactories = {
[TIntrinsic in keyof React.ReactHTML]: StyledHTML<TIntrinsic>;
};
@ggoodman
ggoodman / develop.js
Created November 22, 2020 02:37
Bare-bones nodemon + esbuild hybrid for hot-reloading a process
//@ts-check
'use strict';
const ChildProcess = require('child_process');
const Events = require('events');
const Path = require('path');
const { watch } = require('chokidar');
const { startService } = require('esbuild');
const Pino = require('pino');
@ggoodman
ggoodman / README.md
Created May 13, 2020 14:12
Velcro trace output

Velcro bundle tracing

I've found it helpful when hacking on Velcro to be able to trace the different operations performed by the sytem. Attached is an example log output when bundling up an index.js file having a dependency on react-ui.

If you eliminate the network (thank you polly.js), the whole thing takes 591ms. 🎉

@ggoodman
ggoodman / promiseMachine.ts
Created March 10, 2020 18:59
Example of generating a typed FSM for a Promise
type PromiseReject = {
eventName: '@@promiseReject';
err: unknown;
};
type PromiseResolve<T> = {
eventName: '@@promiseResolve';
value: T;
};
@ggoodman
ggoodman / definition.json
Last active March 10, 2020 18:15
Definition for a state machine that tracks a node http request through its lifecycle
{
"PendingSocket": {
"kind": "Intermediate",
"onEnter": [
{}
],
"onEvent": {
"Error": [
{
"targetStates": [