Skip to content

Instantly share code, notes, and snippets.

View bmeck's full-sized avatar

Bradley Farias bmeck

View GitHub Profile
//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();
// 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