This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bun --hot sse.ts | |
import { randomUUID } from "node:crypto"; | |
import { EventEmitter } from "node:events"; | |
const sseEvents = new EventEmitter(); | |
export const sse = (data) => { | |
sseEvents.emit( | |
"sse", | |
`id: ${randomUUID()}\ndata: ${JSON.stringify(data)}\n\n` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function wait(ms: number): Promise<void> { | |
// Wait for the specified amount of time | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Redis from 'redis'; | |
import * as superjson from 'superjson'; | |
export class Cache { | |
private static client: Redis.RedisClient; | |
private static readonly prefix = process.env.CACHE_PREFIX || ''; | |
private static readonly bypassCache = process.env.CACHE_BYPASS === 'true'; | |
static async get<T>(key: (string | number | Date)[], getValue: () => Promise<T>): Promise<T> { | |
if (this.bypassCache) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Concatenate the specified classNames automatically with spaces. | |
* - If the className is undefined, it is ignored. | |
* - If the className is passed as a record of {class: condition }, it returns the class only if the condition is true. | |
* | |
* @example | |
* clsx('class1', 'class2', { class: true }) // 'class1 class2 class' | |
* clsx('class1', 'class2', { class: false }) // 'class1 class2' | |
* clsx('class1', undefined) // 'class1' | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const corsHeaders = { | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', | |
'Access-Control-Allow-Credentials': true, | |
}; | |
const ALLOWED_HOSTS = [ | |
// Put your allowed cors hosts here | |
// By default localhost is allowed | |
// Your fetch should have a `mode: cors` enabled for it to work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyRange implements Iterable<number> { | |
public readonly [Symbol.iterator]: () => Iterator<number> = function* () { } | |
constructor(private readonly start: number,private readonly end: number,private readonly step: number = 1) { | |
if (step === 0) return | |
if (step < 0) step = -step | |
if (start > end) this[Symbol.iterator] = function* () { | |
for (let i = start; i >= end; i -= step) yield i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function jsonFetch<T>( | |
url: string, | |
options: RequestInit = {} | |
): Promise<T> { | |
// Set the default headers correctly | |
const headers: HeadersInit = new Headers(options.headers); | |
headers.set('Accept', 'application/json'); | |
headers.set('Content-Type', 'application/json'); | |
if (process.env.NODE_ENV === 'development') { |