Skip to content

Instantly share code, notes, and snippets.

View jacwright's full-sized avatar
👨‍💻
Working on Dabble

Jacob Wright jacwright

👨‍💻
Working on Dabble
View GitHub Profile
@jacwright
jacwright / concurrency.ts
Last active December 13, 2021 03:28
Concurrency: Easy, Fast, Correct — Choose three
/**
* Simplify concurrency using the strategies described in
* https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/ using input gates and output gates by
* allowing certain actions to block other actions or to block the responses of other actions.
* To "block" an action or response means to defer it until after the blocking actions have been processed. This
* allows the state of an object to be consistent and avoid race conditions which may be hard to discover.
* It is a good idea to use this in conjunction with a cache for blockable actions (e.g. a storage mechanism) to ensure
* the blocking doesn't slow down the system.
*
* Definitions:
@jacwright
jacwright / location.ts
Created May 15, 2023 21:12
Location Cloudflare Durable Objects close to users based on continent and latitude/longitude.
export function getLocationHint(location: Location = {}) {
if (!location.continentCode && !location.point) return 'enam';
const areas = continentCodes[location.continentCode || 'AN'];
let closest = areas[0];
let shortestDistance = Infinity;
if (areas.length === 1 || !location.point) return closest;
const { lat, lon } = location.point;
const purpose = "Purpose: obfuscate ttf file (convert to Microsoft's ODTTF format)"
const usage = "Usage: node obfuscate-ttf-to-odttf.js <guid-font-file.ttf> [<output-file.odttf>]"
const obfuscatedStartOffset = 0
const obfuscatedEndOffset = 32
const guidSize = 32
const fs = require('fs')
const path = require('path')
@jacwright
jacwright / convext.ts
Last active September 22, 2023 19:28
Svelte Convex query store
import type { ConvexClient } from 'convex/browser';
import type { FunctionReference } from 'convex/server';
import { derived, writable } from 'svelte/store';
export function queryResults<Query extends FunctionReference<'query'>>(
client: ConvexClient,
query: Query,
initialArgs: Query['_args']
): Query['_returnType'] {
const args = writable(initialArgs);
@jacwright
jacwright / auth-firebase.ts
Created September 29, 2023 00:20
Basic idea of how you would verify a Firebase auth JWT on the server in Cloudflare.
import jwt from '@tsndr/cloudflare-worker-jwt';
import { StatusError } from '../lib/errors';
import { Config, RouterRequest, SocketAuth } from '../types';
export function uses(request: RouterRequest) {
return (request.headers.get('authorization') || '').match(/^Bearer /i);
}
export async function auth(request: RouterRequest, noError?: boolean) {
return authSocket(