|
| 1 | +import type { QueueRedisClient } from '@sheetforge/queue'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Upstash REST adapter — implements the QueueRedisClient contract via raw |
| 5 | + * fetch calls to the Upstash REST endpoint. No @upstash/redis dep so we can |
| 6 | + * stay version-proof and keep the shared/redis package light. |
| 7 | + * |
| 8 | + * Upstash REST does not support XREADGROUP BLOCK (HTTP doesn't long-poll |
| 9 | + * here), so the adapter simulates blocking semantics by sleeping for blockMs |
| 10 | + * when the stream is empty. Fine for the V0 inline processor; the long-term |
| 11 | + * story is Cloudflare Workers where short polls are idiomatic anyway. |
| 12 | + */ |
| 13 | +export function createUpstashQueueClient({ |
| 14 | + url, |
| 15 | + token, |
| 16 | +}: { |
| 17 | + url: string; |
| 18 | + token: string; |
| 19 | +}): QueueRedisClient & { disconnect: () => Promise<void> } { |
| 20 | + const endpoint = url.replace(/\/+$/, ''); |
| 21 | + |
| 22 | + async function exec<T = unknown>(...args: (string | number)[]): Promise<T> { |
| 23 | + const res = await fetch(endpoint, { |
| 24 | + method: 'POST', |
| 25 | + headers: { |
| 26 | + Authorization: `Bearer ${token}`, |
| 27 | + 'Content-Type': 'application/json', |
| 28 | + }, |
| 29 | + body: JSON.stringify(args.map(String)), |
| 30 | + }); |
| 31 | + if (!res.ok) { |
| 32 | + throw new Error(`Upstash HTTP ${res.status}: ${await res.text()}`); |
| 33 | + } |
| 34 | + const body = (await res.json()) as { result?: T; error?: string }; |
| 35 | + if (body.error) throw new Error(`Upstash: ${body.error}`); |
| 36 | + return body.result as T; |
| 37 | + } |
| 38 | + |
| 39 | + return { |
| 40 | + async xadd(key, fields) { |
| 41 | + const flat: string[] = []; |
| 42 | + for (const [k, v] of Object.entries(fields)) flat.push(k, v); |
| 43 | + const id = await exec<string>('XADD', key, '*', ...flat); |
| 44 | + return id; |
| 45 | + }, |
| 46 | + |
| 47 | + async xreadgroupSingle({ group, consumer, key, blockMs }) { |
| 48 | + // Upstash REST doesn't long-poll. Call once, sleep blockMs if empty. |
| 49 | + const result = (await exec( |
| 50 | + 'XREADGROUP', |
| 51 | + 'GROUP', |
| 52 | + group, |
| 53 | + consumer, |
| 54 | + 'COUNT', |
| 55 | + '1', |
| 56 | + 'STREAMS', |
| 57 | + key, |
| 58 | + '>', |
| 59 | + )) as Array<[string, Array<[string, string[]]>]> | null; |
| 60 | + |
| 61 | + if (!result || result.length === 0) { |
| 62 | + if (blockMs && blockMs > 0) { |
| 63 | + await new Promise((res) => setTimeout(res, blockMs)); |
| 64 | + } |
| 65 | + return null; |
| 66 | + } |
| 67 | + const stream = result[0]; |
| 68 | + if (!stream) return null; |
| 69 | + const entries = stream[1]; |
| 70 | + if (!entries || entries.length === 0) return null; |
| 71 | + const entry = entries[0]; |
| 72 | + if (!entry) return null; |
| 73 | + const [id, flat] = entry; |
| 74 | + const out: Record<string, string> = {}; |
| 75 | + for (let i = 0; i < flat.length; i += 2) { |
| 76 | + const k = flat[i]; |
| 77 | + const v = flat[i + 1]; |
| 78 | + if (k !== undefined && v !== undefined) out[k] = v; |
| 79 | + } |
| 80 | + return { id, fields: out }; |
| 81 | + }, |
| 82 | + |
| 83 | + async xack(key, group, id) { |
| 84 | + await exec('XACK', key, group, id); |
| 85 | + }, |
| 86 | + |
| 87 | + async xgroupCreateMkstream(key, group) { |
| 88 | + try { |
| 89 | + await exec('XGROUP', 'CREATE', key, group, '$', 'MKSTREAM'); |
| 90 | + } catch (err) { |
| 91 | + const msg = err instanceof Error ? err.message : String(err); |
| 92 | + if (!/BUSYGROUP/.test(msg)) throw err; |
| 93 | + } |
| 94 | + }, |
| 95 | + |
| 96 | + async xtrimMaxlenApprox(key, maxLen) { |
| 97 | + await exec('XTRIM', key, 'MAXLEN', '~', maxLen); |
| 98 | + }, |
| 99 | + |
| 100 | + async setNxPx(key, value, ttlMs) { |
| 101 | + const result = await exec<'OK' | null>( |
| 102 | + 'SET', |
| 103 | + key, |
| 104 | + value, |
| 105 | + 'NX', |
| 106 | + 'PX', |
| 107 | + ttlMs, |
| 108 | + ); |
| 109 | + return result === 'OK'; |
| 110 | + }, |
| 111 | + |
| 112 | + async get(key) { |
| 113 | + return (await exec<string | null>('GET', key)) ?? null; |
| 114 | + }, |
| 115 | + |
| 116 | + async disconnect() { |
| 117 | + // REST adapter holds no long-lived connections. |
| 118 | + }, |
| 119 | + }; |
| 120 | +} |
0 commit comments