Skip to content

Instantly share code, notes, and snippets.

"Untrusted code"

Worst possible approach:

  • Run plugins all in the same vm context on server side Bad approach:
  • Run plugins in the same vm context on browser side

Ish:

  • Run plugins in browser side web workers
/**
* Generate a fragile object that will throw error at any operation.
*/
export function createErrorObj<T = any>(error: string): T {
return new Proxy(
{},
{
get(target, prop, receiver: unknown) {
throw new Error(
@colelawrence
colelawrence / windows.ahk
Last active October 14, 2022 16:17
Making crucial keybindings similar on Windows to expected on personal macOS
#SingleInstance Force
; Debugging
; https://www.autohotkey.com/scite4ahk/pages/debugger.htm
; Map Capslock to control (macOS is Capslock as Command, so most keybindings feel similar)
CapsLock::RControl
; Left Ctrl Tab -> Left Alt Tab
<^Tab::>!Tab
@colelawrence
colelawrence / WeakCache.ts
Created September 6, 2022 12:48
When you needed something a little higher dimension than just a WeakMap.
/** Memory-volatile multi-key cache with a root weak key */
export class WeakCache {
private wm = new WeakMap<object, Map<string | number, unknown>>();
/** It's very important to not call this with the same keys and expect a different result. */
getOrPut<R>(weakKey: object, keys: CacheKeys, fn: () => R): R {
let found = this.wm.get(weakKey);
const innerKey = typeof keys === "object" ? JSON.stringify(keys) : keys;
if (!found) {
const computed = fn();
found = new Map([[innerKey, computed]]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type $IntentionalAny = any;
/**
* This special type can help generate pattern matchers for you!
* Just use it as so:
* // enum-ts
* type Result<Ok, Err> = Enum<{
* Ok: Ok,
* Err: Err,
@colelawrence
colelawrence / createTrpcDurableObject.ts
Last active March 27, 2024 22:10
Create strongly typed Durable Objects with TRPC wrappers (requires trpc@10)
import { createTRPCProxyClient, httpLink } from "@trpc/client";
import { AnyRouter, initTRPC, MaybePromise, Router } from "@trpc/server";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { ParserWithInputOutput, ParserWithoutInput } from "@trpc/server/dist/core/parser";
import type { AnyRouterDef } from "@trpc/server/dist/core/router";
import type { ResponseMetaFn } from "@trpc/server/dist/http/internals/types";
import { getParseFn } from "./getParseFn";
export type TrpcDurableObjectRouter<Router extends AnyRouter> = {
router: Router,
@colelawrence
colelawrence / slack-download.py
Last active February 2, 2022 18:31 — forked from marnitto/slack-download.py
Slack file downloader from export archive (2021)
# -*- coding: utf-8 -*-
#
# Slack file downloader from export archive
#
# Requirements:
# Python 2 (or Python3 if you can use six)
#
# How to use:
# 1. Log in as admin, export your chat logs, and download archive.
# 2. Unarchive archive to directory (ex. TeamName export Apr 24 2016)
@colelawrence
colelawrence / pop-os-keyboard.json
Last active September 7, 2021 03:27
VSCode popOS settings
// Place your key bindings in this file to override the defaultsauto[]
[
{
"key": "ctrl+right",
"command": "-cursorWordEndRight",
"when": "textInputFocus"
},
{
"key": "ctrl+shift+right",
"command": "-cursorWordEndRightSelect",
#!/usr/bin/env bash
set -euxo pipefail
database_url="${1}"
dump_path="${2:-schema}"
schemas=("app_public" "app_hidden" "app_private" "public")
find "$dump_path" -name '*.sql' -delete
JSON.stringify(
mappedSchema,
(_key, val) => {
if (val != null && typeof val === "object") {
return Object.fromEntries(
Object.keys(val)
.sort()
.map((key) => [key, val[key]]),
);
} else {