Skip to content

Instantly share code, notes, and snippets.

View bmeck's full-sized avatar

Bradley Farias bmeck

View GitHub Profile

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();
// jslint.js
// 2010-08-08
/*
Copyright (c) 2002 Douglas Crockford (www.JSLint.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// node [--trace-gc] local-storage-exmaple.cjs
// creates a server
// will drop /favicon.ico connections poorly without properly closing them
// `cleanupContext` AsyncLocalStorage + `cleanup` FinalizationRegistry will clean up
// will track the http request URL for errors in httpContext
// will show up in error generated for bad permissions
// will grant permissions in permissionsContext based upon query params / search params
// need ?fs=true to make / respond with a 200
//
// goto /?fs=true in browser to see it work
// node [--trace-gc] local-storage-exmaple.cjs
// creates a server
// will drop /favicon.ico connections poorly without properly closing them
// `cleanupContext` AsyncLocalStorage + `cleanup` FinalizationRegistry will clean up
// will track the http request URL for errors in httpContext
// will show up in error generated for bad permissions
// will grant permissions in permissionsContext based upon query params / search params
// need ?fs=true to make / respond with a 200
//
// goto /?fs=true in browser to see it work
import util from 'util';
const promiseDebugging = util.debuglog('promises').enabled;
const danglingPromise = new FinalizationRegistry((stack) => {
danglingStacks.delete(stack);
console.log(stack);
});
const danglingStacks = new Set();
const stackCache = new WeakMap();
/**
* @template T
@bmeck
bmeck / Readme.md
Created April 3, 2012 15:02
Simple Vertical Load Balancing for TCP in Node.js

About

This is the most basic example of vertical scaling by transferring connections to workers, rather than sharing server sockets. It was presented at JSConf US 2012. With this you can create some interesting balancing algorithms that cluster would defer to internal/OS logic.

Exercises

A good first project when working with this is to implement sticky sessions based upon connection.remoteAddress and/or connection.remotePort.

A good advanced project is to change from a TCP based balancer to a HTTP(S) one and balance based upon not just connection.remoteAddress etc. but also on the protocol and/or Host header.

@bmeck
bmeck / gc.js
Last active May 10, 2021 20:19
#!/usr/bin/env node
// call using --inspect brk, GC using memory tab of devtools
// call globalThis.tick() to move the code along and see when things reap from
// manually calling GC
{
const registry = new FinalizationRegistry((_) => console.log(_, 'reaped'));
let EASY_TO_FIND = class EASY_TO_FIND2 {
big = new Uint8Array(1024 * 1024 * 10);
constructor(_) {
registry.register(this, _);