-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add fetch timeout and caching to supermemory-docs-mintlify proxy worker #1179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "@repo/docs-proxy", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "wrangler dev", | ||
| "deploy": "wrangler deploy --minify" | ||
| }, | ||
| "devDependencies": { | ||
| "@cloudflare/workers-types": "^4.20250620.0", | ||
| "typescript": "^5.8.3", | ||
| "wrangler": "^4.4.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| const UPSTREAM_BASE = "https://supermemory.mintlify.dev" | ||
| const CACHE_TTL = 300 // 5 minutes | ||
| const FETCH_TIMEOUT_MS = 10000 // 10 seconds | ||
|
|
||
| export default { | ||
| async fetch(request: Request): Promise<Response> { | ||
| const url = new URL(request.url) | ||
| const proxyUrl = UPSTREAM_BASE + url.pathname + url.search | ||
|
|
||
| const cache = caches.default | ||
|
|
||
| if (request.method === "GET") { | ||
| const cached = await cache.match(request) | ||
| if (cached) return cached | ||
| } | ||
|
|
||
| const controller = new AbortController() | ||
| const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS) | ||
|
|
||
| try { | ||
| const proxyRequest = new Request(proxyUrl, request) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const response = await fetch(proxyRequest, { signal: controller.signal }) | ||
|
|
||
| if (request.method === "GET" && response.ok) { | ||
| const cachedResponse = new Response(response.clone().body, response) | ||
| cachedResponse.headers.set( | ||
| "Cache-Control", | ||
| `public, max-age=${CACHE_TTL}`, | ||
| ) | ||
| await cache.put(request, cachedResponse) | ||
| } | ||
|
|
||
| return response | ||
| } catch (e) { | ||
| if (e instanceof Error && e.name === "AbortError") { | ||
| return new Response( | ||
| "Documentation temporarily unavailable. Please try again in a moment.", | ||
| { | ||
| status: 504, | ||
| headers: { "Content-Type": "text/plain; charset=utf-8" }, | ||
| }, | ||
| ) | ||
| } | ||
| throw e | ||
| } finally { | ||
| clearTimeout(timeoutId) | ||
| } | ||
| }, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "module": "ESNext", | ||
| "moduleResolution": "Bundler", | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "lib": ["ESNext"], | ||
| "types": ["@cloudflare/workers-types"], | ||
| "outDir": "dist", | ||
| "rootDir": "src" | ||
| }, | ||
| "include": ["src/**/*"], | ||
| "exclude": ["node_modules"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "$schema": "node_modules/wrangler/config-schema.json", | ||
| "name": "supermemory-docs-mintlify", | ||
| "main": "src/index.ts", | ||
| "compatibility_date": "2025-06-20", | ||
| "compatibility_flags": [], | ||
|
|
||
| "observability": { | ||
| "enabled": true | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new file is not Biome-formatted, and repo CI runs Biome on changed files. The delegated check reported
biome cifails withFile content differs from formatting output; please format the file so CI passes.