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) {
// 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 / 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();
@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, _);
@bmeck
bmeck / harness.js
Created March 17, 2021 02:27
bradley needed to dump this devtools type tracer PoC somewhere
function foo(x, y) {
return Object;
}
const { stop } = trace();
new foo([1], new Date());
stop();
// boilerplate after this
# see the replacement character ( Buffer.from(new TextEncoder().encode('\u{fffd}')) ) instead
# https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
node -e 'process.stdout.write("👌"[0]);setTimeout(() => {process.stdout.end("👌"[1]); process.exit()},3e3)' |
node -e '(async (s = "") => {for await (const a of process.stdin) {console.log(a); s+= a}; console.log(s)})()'
{
"type": "object",
"title": "Node.js Policy Manifest",
"description": "https://nodejs.org/api/policy.html",
"definitions": {
"cascade": {
"defaultSnippets": [
{
"label": "Allow scope fall through.",
"body": true