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 / 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(
@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);
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 / 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;
@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 / signals.ts
Created September 6, 2019 14:57
TypeScript Signals, A simple typed alternative to events.
/**
* Tiny. Compiled code for signals results in 15 lines of JS, < 500 bytes of uncompressed code.
*
* Usage:
*
* const clickedSave = createSignal<Doc>();
*
* clickedSave(doc => {
* // do something to save the doc, or when the doc is saved
* })
@jacwright
jacwright / Route.html
Created February 5, 2019 22:50
How do I update stores from reactive declaration updates
{#if match !== null}
<slot></slot>
{/if}
<script>
import { getHistory, matchPath, ROUTING_CONTEXT } from './index.js';
import { writable } from 'svelte/store.js';
import { setContext, onDestroy } from 'svelte';
const history = getHistory();
const matchStore = writable(null);
@jacwright
jacwright / svelte-observe.js
Last active September 19, 2018 17:08
One `observe` for all svelte needs.
export function observe(key, callback, opts) {
const single = !Array.isArray(key);
const keypaths = single ? [ key ] : key;
const parts = keypaths.map(keypath => keypath.replace(/\[(\d+)\]/g, '.$1').split('.'));
const keys = parts.map(parts => parts[0]);
const fn = callback.bind(this);
let last = parts.map(parts => getNestedValue(this.get(), parts));
if (!opts || opts.init !== false) {
@jacwright
jacwright / userId.js
Created May 8, 2017 15:41
Auth0: Set unique ID for a user
function (user, context, callback) {
if (!user.app_metadata) {
user.app_metadata = {};
}
if (!user.app_metadata.userId || user.app_metadata.userId.length < 24) {
var idLength = 24;
var chars = (
'0123456789abcdefghijklmnopqrstuvwxyz'
).split('');
@jacwright
jacwright / link-accounts.js
Created May 8, 2017 15:40
Auth0: Link Accounts with Same Email Address while Merging Metadata