@@ -13,7 +13,11 @@ let loadGoogleFont = async (family: string) => {
1313 await response -> Response .arrayBuffer
1414}
1515
16- type context = {request : FetchAPI .request , params : {path : array <string >}}
16+ type assets
17+ type env = {@as ("ASSETS" ) assets : assets }
18+ type context = {request : FetchAPI .request , params : {path : array <string >}, env : env }
19+
20+ @send external fetchAsset : (assets , string ) => promise <FetchAPI .response > = "fetch"
1721
1822let textResponse = (~status , message ) => Response .fromString (message , ~init = {status : status })
1923
@@ -124,14 +128,14 @@ let isHtmlResponse = (response: FetchAPI.response) =>
124128 contentType -> String .toLowerCase -> String .includes ("text/html" )
125129 )
126130
127- let renderImage = async (~requestUrl : URLAPI .url , ~targetUrl : URLAPI .url ) => {
131+ let renderImage = async (~assets , ~ requestUrl : URLAPI .url , ~targetUrl : URLAPI .url ) => {
128132 if targetUrl .origin != requestUrl .origin {
129133 textResponse (~status = 400 , "Open Graph image URL must be same-origin" )
130134 } else if targetUrl .pathname -> String .startsWith ("/ogimage/" ) {
131135 textResponse (~status = 400 , "Open Graph image URL cannot point at the image endpoint" )
132136 } else {
133137 let pageResponse = try {
134- Some (await fetch (targetUrl .href , ~ init = { redirect : FetchAPI . Error } ))
138+ Some (await assets -> fetchAsset (targetUrl .href ))
135139 } catch {
136140 | _ => None
137141 }
@@ -250,11 +254,11 @@ let renderImage = async (~requestUrl: URLAPI.url, ~targetUrl: URLAPI.url) => {
250254 }
251255}
252256
253- let onRequest = async ({request , params }: context ) => {
257+ let onRequest = async ({request , params , env }: context ) => {
254258 let requestUrl = URL .make (~url = request .url )
255259
256260 switch requestedUrl (~requestUrl , ~params )-> Option .flatMap (parseUrl ) {
257261 | None => textResponse (~status = 400 , "Missing or invalid url" )
258- | Some (targetUrl ) => await renderImage (~requestUrl , ~targetUrl )
262+ | Some (targetUrl ) => await renderImage (~assets = env . assets , ~ requestUrl , ~targetUrl )
259263 }
260264}
0 commit comments