|
| 1 | +import ogs from "open-graph-scraper" |
| 2 | + |
| 3 | +export const runtime = "nodejs" |
| 4 | + |
| 5 | +interface OGResponse { |
| 6 | + title: string |
| 7 | + description: string |
| 8 | + image?: string |
| 9 | +} |
| 10 | + |
| 11 | +function isValidUrl(urlString: string): boolean { |
| 12 | + try { |
| 13 | + const url = new URL(urlString) |
| 14 | + return url.protocol === "http:" || url.protocol === "https:" |
| 15 | + } catch { |
| 16 | + return false |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function isPrivateHost(hostname: string): boolean { |
| 21 | + const lowerHost = hostname.toLowerCase() |
| 22 | + |
| 23 | + // Block localhost variants |
| 24 | + if ( |
| 25 | + lowerHost === "localhost" || |
| 26 | + lowerHost === "127.0.0.1" || |
| 27 | + lowerHost === "::1" || |
| 28 | + lowerHost.startsWith("127.") || |
| 29 | + lowerHost.startsWith("0.0.0.0") |
| 30 | + ) { |
| 31 | + return true |
| 32 | + } |
| 33 | + |
| 34 | + // Block RFC 1918 private IP ranges |
| 35 | + const privateIpPatterns = [ |
| 36 | + /^10\./, |
| 37 | + /^172\.(1[6-9]|2[0-9]|3[01])\./, |
| 38 | + /^192\.168\./, |
| 39 | + ] |
| 40 | + |
| 41 | + return privateIpPatterns.some((pattern) => pattern.test(hostname)) |
| 42 | +} |
| 43 | + |
| 44 | +function extractImageUrl(image: unknown): string | undefined { |
| 45 | + if (!image) return undefined |
| 46 | + |
| 47 | + if (typeof image === "string") { |
| 48 | + return image |
| 49 | + } |
| 50 | + |
| 51 | + if (Array.isArray(image) && image.length > 0) { |
| 52 | + const first = image[0] |
| 53 | + if (first && typeof first === "object" && "url" in first) { |
| 54 | + return String(first.url) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (typeof image === "object" && image !== null && "url" in image) { |
| 59 | + return String(image.url) |
| 60 | + } |
| 61 | + |
| 62 | + return undefined |
| 63 | +} |
| 64 | + |
| 65 | +function resolveImageUrl( |
| 66 | + imageUrl: string | undefined, |
| 67 | + baseUrl: string, |
| 68 | +): string | undefined { |
| 69 | + if (!imageUrl) return undefined |
| 70 | + |
| 71 | + try { |
| 72 | + const url = new URL(imageUrl) |
| 73 | + return url.href |
| 74 | + } catch { |
| 75 | + try { |
| 76 | + const base = new URL(baseUrl) |
| 77 | + return new URL(imageUrl, base.href).href |
| 78 | + } catch { |
| 79 | + return undefined |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export async function GET(request: Request) { |
| 85 | + try { |
| 86 | + const { searchParams } = new URL(request.url) |
| 87 | + const url = searchParams.get("url") |
| 88 | + |
| 89 | + if (!url || !url.trim()) { |
| 90 | + return Response.json( |
| 91 | + { error: "Missing or invalid url parameter" }, |
| 92 | + { status: 400 }, |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + const trimmedUrl = url.trim() |
| 97 | + |
| 98 | + if (!isValidUrl(trimmedUrl)) { |
| 99 | + return Response.json( |
| 100 | + { error: "Invalid URL. Must be http:// or https://" }, |
| 101 | + { status: 400 }, |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + const urlObj = new URL(trimmedUrl) |
| 106 | + if (isPrivateHost(urlObj.hostname)) { |
| 107 | + return Response.json( |
| 108 | + { error: "Private/localhost URLs are not allowed" }, |
| 109 | + { status: 400 }, |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + const { result, error } = await ogs({ |
| 114 | + url: trimmedUrl, |
| 115 | + timeout: 8000, |
| 116 | + fetchOptions: { |
| 117 | + headers: { |
| 118 | + "User-Agent": |
| 119 | + "Mozilla/5.0 (compatible; SuperMemory/1.0; +https://supermemory.ai)", |
| 120 | + }, |
| 121 | + }, |
| 122 | + }) |
| 123 | + |
| 124 | + if (error || !result) { |
| 125 | + console.error("OG scraping error:", error) |
| 126 | + return Response.json( |
| 127 | + { error: "Failed to fetch Open Graph data" }, |
| 128 | + { status: 500 }, |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + const ogTitle = result.ogTitle || result.twitterTitle || "" |
| 133 | + const ogDescription = |
| 134 | + result.ogDescription || result.twitterDescription || "" |
| 135 | + |
| 136 | + const ogImageUrl = |
| 137 | + extractImageUrl(result.ogImage) || extractImageUrl(result.twitterImage) |
| 138 | + |
| 139 | + const resolvedImageUrl = resolveImageUrl(ogImageUrl, trimmedUrl) |
| 140 | + |
| 141 | + const response: OGResponse = { |
| 142 | + title: ogTitle, |
| 143 | + description: ogDescription, |
| 144 | + ...(resolvedImageUrl && { image: resolvedImageUrl }), |
| 145 | + } |
| 146 | + |
| 147 | + return Response.json(response, { |
| 148 | + headers: { |
| 149 | + "Cache-Control": "public, s-maxage=3600, stale-while-revalidate=86400", |
| 150 | + }, |
| 151 | + }) |
| 152 | + } catch (error) { |
| 153 | + console.error("OG route error:", error) |
| 154 | + return Response.json({ error: "Internal server error" }, { status: 500 }) |
| 155 | + } |
| 156 | +} |
0 commit comments