Skip to content

Instantly share code, notes, and snippets.

interface TwoWayWeakMap<K extends object, V> extends WeakMap<K, V> {
keysFor(value: V): K[];
}
interface TwoWayWeakMapConstructor {
new <K extends object = object, V = any>(
entries?: readonly [K, V][] | null
): TwoWayWeakMap<K, V>;
readonly prototype: TwoWayWeakMap<object, any>;
}
@mhofman
mhofman / wrapper-registry.d.ts
Last active December 8, 2021 18:42
Wrap any value into a registered object
export declare class Wrapper<Kind, Value = any> {
private kind: Kind;
private value: Value;
}
export interface WrapperRegistry<Kind> extends Function {
constructor: WrapperRegistryConstructor;
wrap<T>(value: T): Wrapper<Kind, T>;
unwrap<T>(wrapped: Wrapper<Kind, T>): T;
}
@mhofman
mhofman / mathieuagoric-keybase.md
Last active July 14, 2021 05:09
mathieuagoric keybase verification

Keybase proof

I hereby claim:

  • I am mhofman on github.
  • I am mathieuagoric (https://keybase.io/mathieuagoric) on keybase.
  • I have a public key whose fingerprint is 0EFB 233B 5A6C AA0E DE96 972C E07D D96A C72D 83D8

To claim this, I am signing this object:

@mhofman
mhofman / promise-all-entries.js
Created June 9, 2021 18:34
Promise.allEntries
Promise.allEntries = (entries) =>
Promise.all(Array.from(entries).map((entry) => Promise.all(entry)));
const obj = {
foo: Promise.resolve("bar"),
};
const resolvedObject = Object.fromEntries(
await Promise.allEntries(Object.entries(obj))
);
@mhofman
mhofman / MacOS Screen sharing on localhost only.md
Last active August 25, 2023 08:16
MacOS ScreenSharing on localhost only

Force recent MacOS to listen for screen sharing on localhost only, keeping SIP on

All the following has been validated on MacOS Mojave 10.14.6

Problems

While there is a command line preference to accept only local VNC connections, that setting still doesn't prevent the daemon from listening to the wildcard address, and advertise the service on Bonjour. I haven't actually tried to see if it restricted anything in modern versions of the operating system, but here it is for reference:

sudo defaults write /Library/Preferences/com.apple.RemoteManagement.plist VNCOnlyLocalConnections -bool yes
const KEYS = 1;
const VALUES = 2;
const KEYS_VALUES = 3;
export default class WeakValueMap {
#map = new Map();
#group = new FinalizationGroup((iterator) => {
for (const key of iterator) {
this.#map.delete(key);
}
function makeObserver(): [Promise<boolean>, FinalizationGroup<any>] {
let resolve: (collected: boolean) => void;
const collected = new Promise<boolean>(r => (resolve = r));
const finalizationGroup = new FinalizationGroup<any>(items => {
// Let's be nice and cleanup
[...items];
resolve(true);
});
return [collected, finalizationGroup];
}
@mhofman
mhofman / spidermonkey-async-function-leak.js
Last active April 8, 2019 20:00
Spidermonkey memory leak in async functions. Run in JS Shell.
let nextId = 0;
let weakMap;
let savedCallback;
function createWeakMap(callback) {
weakMap = new WeakMap();
savedCallback = callback;
}
@mhofman
mhofman / windowTimers.js
Last active April 7, 2019 16:13 — forked from kevinoid/windowTimers.js
An implementation of setInterval/setTimeout/setImmediate with Microtask checkpoint for Rhino and SpiderMonkey
/* Implementation of HTML Timers (setInterval/setTimeout) based on sleep.
*
* This file is provided under the following terms (MIT License):
* 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 of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@mhofman
mhofman / _weakrefs_tests.js
Last active April 3, 2019 01:55
WeakRef and FinalizationGroup tests
function taskTurn() {
return new Promise(resolve => setTimeout(resolve, 0));
}
function makeGcOf(gc, FinalizationGroup) {
return async function gcOf(target) {
// Avoid creating a closure which may capture target
let resolve;
const collected = new Promise(r => (resolve = r));
const finalizationGroup = new FinalizationGroup(resolve);
finalizationGroup.register(target || {}, 0);