|
| 1 | +import { redisPipeline } from './_upstash-json.js'; |
| 2 | + |
| 3 | +export const IDEMPOTENCY_HEADER = 'Idempotency-Key'; |
| 4 | +export const IDEMPOTENT_REPLAYED_HEADER = 'Idempotent-Replayed'; |
| 5 | + |
| 6 | +const KEY_MAX_LENGTH = 255; |
| 7 | +const KEY_PATTERN = /^[\x21-\x7e]{1,255}$/; |
| 8 | +const PROCESSING_TTL_SECONDS = 180; |
| 9 | +const DEFAULT_COMPLETED_TTL_SECONDS = 24 * 60 * 60; |
| 10 | +const MAX_STORED_BODY_BYTES = 256 * 1024; |
| 11 | +const PROCESSING_MARKER = JSON.stringify({ state: 'processing' }); |
| 12 | + |
| 13 | +export function isValidIdempotencyKey(key) { |
| 14 | + return typeof key === 'string' && key.length <= KEY_MAX_LENGTH && KEY_PATTERN.test(key); |
| 15 | +} |
| 16 | + |
| 17 | +async function sha256Hex(input) { |
| 18 | + const data = typeof input === 'string' ? new TextEncoder().encode(input) : input; |
| 19 | + const digest = await crypto.subtle.digest('SHA-256', data); |
| 20 | + return Array.from(new Uint8Array(digest)) |
| 21 | + .map((b) => b.toString(16).padStart(2, '0')) |
| 22 | + .join(''); |
| 23 | +} |
| 24 | + |
| 25 | +function anonScope(request) { |
| 26 | + const ip = |
| 27 | + request.headers.get('cf-connecting-ip') || |
| 28 | + request.headers.get('x-real-ip') || |
| 29 | + (request.headers.get('x-forwarded-for') || '').split(',')[0]?.trim() || |
| 30 | + 'unknown'; |
| 31 | + return `ip:${ip}`; |
| 32 | +} |
| 33 | + |
| 34 | +function jsonResponse(status, body, corsHeaders, extraHeaders = {}) { |
| 35 | + return new Response(JSON.stringify(body), { |
| 36 | + status, |
| 37 | + headers: { |
| 38 | + 'Content-Type': 'application/json', |
| 39 | + 'Cache-Control': 'no-store', |
| 40 | + ...corsHeaders, |
| 41 | + ...extraHeaders, |
| 42 | + }, |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +function isReplayableTextBody(contentType) { |
| 47 | + if (!contentType) return false; |
| 48 | + const ct = contentType.toLowerCase(); |
| 49 | + return ct.includes('json') || ct.startsWith('text/'); |
| 50 | +} |
| 51 | + |
| 52 | +function isRetryableStatus(status) { |
| 53 | + return status === 408 || status === 409 || status === 429 || status >= 500; |
| 54 | +} |
| 55 | + |
| 56 | +async function getRequestHashAndRedisKey(request, pathname, scope, idempotencyKey) { |
| 57 | + try { |
| 58 | + const bodyBuf = await request.clone().arrayBuffer(); |
| 59 | + const reqHash = await sha256Hex(bodyBuf); |
| 60 | + const effectiveScope = scope || anonScope(request); |
| 61 | + const redisKey = `idem:v1:${await sha256Hex(`${effectiveScope}\n${pathname}\n${idempotencyKey}`)}`; |
| 62 | + return { reqHash, redisKey }; |
| 63 | + } catch { |
| 64 | + return null; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function outcomeFromStoredRecord(raw, reqHash, idempotencyKey, corsHeaders) { |
| 69 | + if (raw == null) return { kind: 'miss' }; |
| 70 | + |
| 71 | + let record = null; |
| 72 | + if (typeof raw === 'string') { |
| 73 | + try { |
| 74 | + record = JSON.parse(raw); |
| 75 | + } catch { |
| 76 | + record = null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (!record || typeof record !== 'object') return { kind: 'disabled' }; |
| 81 | + |
| 82 | + if (record.state === 'processing') { |
| 83 | + return { |
| 84 | + kind: 'conflict', |
| 85 | + response: jsonResponse( |
| 86 | + 409, |
| 87 | + { |
| 88 | + error: 'idempotency_conflict', |
| 89 | + message: `A request with this ${IDEMPOTENCY_HEADER} is still being processed. Retry shortly.`, |
| 90 | + }, |
| 91 | + corsHeaders, |
| 92 | + { 'Retry-After': '2', [IDEMPOTENCY_HEADER]: idempotencyKey }, |
| 93 | + ), |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + if (record.state !== 'completed') return { kind: 'disabled' }; |
| 98 | + |
| 99 | + if (record.reqHash !== reqHash) { |
| 100 | + return { |
| 101 | + kind: 'mismatch', |
| 102 | + response: jsonResponse( |
| 103 | + 422, |
| 104 | + { |
| 105 | + error: 'idempotency_key_reused', |
| 106 | + message: `This ${IDEMPOTENCY_HEADER} was already used with a different request body.`, |
| 107 | + }, |
| 108 | + corsHeaders, |
| 109 | + { [IDEMPOTENCY_HEADER]: idempotencyKey }, |
| 110 | + ), |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + return { |
| 115 | + kind: 'replay', |
| 116 | + response: new Response(record.body, { |
| 117 | + status: record.status, |
| 118 | + headers: { |
| 119 | + 'Content-Type': record.contentType ?? 'application/json', |
| 120 | + 'Cache-Control': 'no-store', |
| 121 | + ...corsHeaders, |
| 122 | + [IDEMPOTENCY_HEADER]: idempotencyKey, |
| 123 | + [IDEMPOTENT_REPLAYED_HEADER]: 'true', |
| 124 | + }, |
| 125 | + }), |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +async function releaseProcessingLock(redisKey) { |
| 130 | + await redisPipeline([['DEL', redisKey]]); |
| 131 | +} |
| 132 | + |
| 133 | +export async function peekStandaloneIdempotency({ |
| 134 | + request, |
| 135 | + pathname, |
| 136 | + scope, |
| 137 | + idempotencyKey, |
| 138 | + corsHeaders, |
| 139 | +}) { |
| 140 | + if (!isValidIdempotencyKey(idempotencyKey)) { |
| 141 | + return { |
| 142 | + kind: 'invalid', |
| 143 | + response: jsonResponse( |
| 144 | + 400, |
| 145 | + { |
| 146 | + error: 'invalid_idempotency_key', |
| 147 | + message: `The ${IDEMPOTENCY_HEADER} header must be 1-${KEY_MAX_LENGTH} printable ASCII characters.`, |
| 148 | + }, |
| 149 | + corsHeaders, |
| 150 | + ), |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + const resolved = await getRequestHashAndRedisKey(request, pathname, scope, idempotencyKey); |
| 155 | + if (!resolved) return { kind: 'disabled' }; |
| 156 | + |
| 157 | + const pipeline = await redisPipeline([['GET', resolved.redisKey]]); |
| 158 | + if (!pipeline || pipeline.length < 1) return { kind: 'disabled' }; |
| 159 | + |
| 160 | + const entry = pipeline[0]; |
| 161 | + if (entry?.error) return { kind: 'disabled' }; |
| 162 | + |
| 163 | + return outcomeFromStoredRecord(entry?.result, resolved.reqHash, idempotencyKey, corsHeaders); |
| 164 | +} |
| 165 | + |
| 166 | +export async function beginStandaloneIdempotency({ |
| 167 | + request, |
| 168 | + pathname, |
| 169 | + scope, |
| 170 | + idempotencyKey, |
| 171 | + corsHeaders, |
| 172 | + completedTtlSeconds = DEFAULT_COMPLETED_TTL_SECONDS, |
| 173 | +}) { |
| 174 | + if (!isValidIdempotencyKey(idempotencyKey)) { |
| 175 | + return { |
| 176 | + kind: 'invalid', |
| 177 | + response: jsonResponse( |
| 178 | + 400, |
| 179 | + { |
| 180 | + error: 'invalid_idempotency_key', |
| 181 | + message: `The ${IDEMPOTENCY_HEADER} header must be 1-${KEY_MAX_LENGTH} printable ASCII characters.`, |
| 182 | + }, |
| 183 | + corsHeaders, |
| 184 | + ), |
| 185 | + }; |
| 186 | + } |
| 187 | + |
| 188 | + const resolved = await getRequestHashAndRedisKey(request, pathname, scope, idempotencyKey); |
| 189 | + if (!resolved) return { kind: 'disabled' }; |
| 190 | + |
| 191 | + const pipeline = await redisPipeline([ |
| 192 | + ['SET', resolved.redisKey, PROCESSING_MARKER, 'NX', 'EX', String(PROCESSING_TTL_SECONDS)], |
| 193 | + ['GET', resolved.redisKey], |
| 194 | + ]); |
| 195 | + if (!pipeline || pipeline.length < 2) return { kind: 'disabled' }; |
| 196 | + |
| 197 | + const claim = pipeline[0]; |
| 198 | + if (claim?.error) return { kind: 'disabled' }; |
| 199 | + |
| 200 | + if (claim?.result === 'OK') { |
| 201 | + return { |
| 202 | + kind: 'proceed', |
| 203 | + key: idempotencyKey, |
| 204 | + store: (status, body, contentType) => |
| 205 | + storeStandaloneResult(resolved.redisKey, status, body, contentType, resolved.reqHash, completedTtlSeconds), |
| 206 | + }; |
| 207 | + } |
| 208 | + |
| 209 | + const outcome = outcomeFromStoredRecord(pipeline[1]?.result, resolved.reqHash, idempotencyKey, corsHeaders); |
| 210 | + return outcome.kind === 'miss' ? { kind: 'disabled' } : outcome; |
| 211 | +} |
| 212 | + |
| 213 | +export function getIdempotencyKey(request) { |
| 214 | + return request.headers.get(IDEMPOTENCY_HEADER); |
| 215 | +} |
| 216 | + |
| 217 | +export async function completeStandaloneIdempotency(idempotency, response) { |
| 218 | + if (!idempotency || idempotency.kind !== 'proceed') return response; |
| 219 | + |
| 220 | + const body = await response.arrayBuffer(); |
| 221 | + await idempotency.store(response.status, body, response.headers.get('content-type')); |
| 222 | + |
| 223 | + const headers = new Headers(response.headers); |
| 224 | + headers.set(IDEMPOTENCY_HEADER, idempotency.key); |
| 225 | + headers.set(IDEMPOTENT_REPLAYED_HEADER, 'false'); |
| 226 | + return new Response(body, { |
| 227 | + status: response.status, |
| 228 | + statusText: response.statusText, |
| 229 | + headers, |
| 230 | + }); |
| 231 | +} |
| 232 | + |
| 233 | +async function storeStandaloneResult(redisKey, status, body, contentType, reqHash, completedTtlSeconds) { |
| 234 | + try { |
| 235 | + if ( |
| 236 | + isRetryableStatus(status) || |
| 237 | + body.byteLength > MAX_STORED_BODY_BYTES || |
| 238 | + !isReplayableTextBody(contentType) |
| 239 | + ) { |
| 240 | + await releaseProcessingLock(redisKey); |
| 241 | + return; |
| 242 | + } |
| 243 | + |
| 244 | + const record = { |
| 245 | + state: 'completed', |
| 246 | + status, |
| 247 | + contentType, |
| 248 | + reqHash, |
| 249 | + body: new TextDecoder().decode(body), |
| 250 | + }; |
| 251 | + const pipeline = await redisPipeline([ |
| 252 | + ['SET', redisKey, JSON.stringify(record), 'EX', String(completedTtlSeconds)], |
| 253 | + ]); |
| 254 | + const setResult = pipeline?.[0]; |
| 255 | + if (setResult?.error || setResult?.result !== 'OK') await releaseProcessingLock(redisKey); |
| 256 | + } catch { |
| 257 | + try { |
| 258 | + await releaseProcessingLock(redisKey); |
| 259 | + } catch { |
| 260 | + // Ignore release failures; the processing marker has a short TTL. |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments