Skip to content

Instantly share code, notes, and snippets.

View bmeck's full-sized avatar

Bradley Farias bmeck

View GitHub Profile
@bmeck
bmeck / yield-star.js
Created February 10, 2014 05:29
Example of how to use yield* to combine generators
// Our incremental JSON parser (simplified only to use strings and arrays)
// while iterator result != done keep using .next(moreSrc) or .next(null) if you are done providing data
// while the iterator is not done it will return the state of the iterator
// the iterator is seeking more data to get a valid value
// call .next(str) to provide it more data
var NEED_MORE_DATA = 0;
// the iterator has a valid result and will return a result if you provide .next(null)
// this is not automatic, due to numbers and potentially invalid trailing characters after an expected end
bmeck@Bradleys-MacBook-Pro promise-diagnostics-hook % sh ./cover.sh node ./test.mts
Unused Promise 64 (allocated 1 times):
node:internal/deps/undici/undici:6402:24
node:internal/deps/undici/undici:6:50
node:internal/deps/undici/undici:8095:21
node:internal/deps/undici/undici:6:50
node:internal/deps/undici/undici:8548:18
node:internal/deps/undici/undici:6:50
node:internal/deps/undici/undici:8649:16
node:internal/deps/undici/undici:6:50
@bmeck
bmeck / hooks.mjs
Created July 14, 2025 14:57
A WIP helper that needs some love around detecting wasted Promise usages
// LICENSE: MIT License
import {
triggerAsyncId,
createHook,
executionAsyncId,
} from "node:async_hooks"
import fs from 'node:fs'
import { debug as createDebug } from "node:util"
let log_raw = new Set()
@bmeck
bmeck / bench.mjs
Created July 9, 2025 20:20
Why I want a sane await deferal and cache
import { createHook } from 'node:async_hooks';
let ticks = 0
const asyncHook = createHook({
before() {
ticks++
},
});
asyncHook.enable();
let normal = Promise.resolve('cloud_secret');
//Attach me to Node.prototype as well
var DOCUMENT_POSITION_DISCONNECTED = Node.prototype.DOCUMENT_POSITION_DISCONNECTED = 0x01
var DOCUMENT_POSITION_PRECEDING = Node.prototype.DOCUMENT_POSITION_PRECEDING = 0x02
var DOCUMENT_POSITION_FOLLOWING = Node.prototype.DOCUMENT_POSITION_FOLLOWING = 0x04
var DOCUMENT_POSITION_CONTAINS = Node.prototype.DOCUMENT_POSITION_CONTAINS = 0x08
var DOCUMENT_POSITION_CONTAINED_BY = Node.prototype.DOCUMENT_POSITION_CONTAINED_BY = 0x10
Node.prototype.compareDocumentPosition = function compareDocumentPosition( otherNode ) {
if( !(otherNode instanceof Node) ) {
throw Error("Comparing position against non-Node values is not allowed")
alert('GOOD')

If you have ever had to deal with Promise heavy code, and in particular deep data structures you will likely be familiar with code like the following:

const body = await (await fetch(url)).json()

This has a LHS RHS interchange that causes non-linear edits while writing code (especially with autocomplete):

fetch(url) // LTR
/// <reference types="node" />
// See NOTES below
import flatstr from 'flatstr'
import v8 from 'v8'
// @ts-check
class ArrayOfObjects {
store = []
constructor(count) {

Various command line applications use an Interpreter Directive to define how they should be run.

#! js -m foo
#! node foo
@bmeck
bmeck / wip.mjs
Created June 24, 2021 14:00
in-progress in-thread heapsnapshot reflective API
import * as ns from './index.js';
import {promisify} from 'util';
import {createReadStream, createWriteStream} from 'fs';
import {Session} from 'inspector';
import {PassThrough} from 'stream';
import {createHash} from 'crypto';
const session = new Session();
const stream = new PassThrough();