-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement ipx image proxy + lru cache
- Loading branch information
Showing
4 changed files
with
63 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,46 @@ | ||
import { type Context } from "hono" | ||
import { | ||
createIPX, | ||
createIPXWebServer, | ||
ipxFSStorage, | ||
ipxHttpStorage, | ||
} from 'ipx'; | ||
import { LRUCache } from "lru-cache"; | ||
|
||
const ipx = createIPX({ | ||
storage: ipxFSStorage({ dir: './public/' }), | ||
storage: ipxFSStorage(), | ||
httpStorage: ipxHttpStorage({ | ||
allowAllDomains: true, | ||
domains: ['images.unsplash.com'] | ||
}), | ||
}); | ||
|
||
export const ipxHandler = createIPXWebServer(ipx) | ||
const cache = new LRUCache({ | ||
max: 200, | ||
}); | ||
|
||
export const imageProxyHandler = async (c: Context) => { | ||
const targetUrl = c.req.raw.url.replace(/\/img/, "") | ||
|
||
const cachedResponse = cache.get(targetUrl) | ||
if (cachedResponse) { | ||
return new Response(cachedResponse as Blob, { | ||
status: 200, | ||
headers: { | ||
"x-cache": "HIT", | ||
"Content-Type": "image/jpeg", | ||
'cache-control': 'max-age=31536000, public, s-maxage=31536000', | ||
}, | ||
}) | ||
} | ||
|
||
const response = await createIPXWebServer(ipx)(new Request(targetUrl)) | ||
|
||
const clonedResponse = response.clone() | ||
const data = await clonedResponse.blob() | ||
cache.set(targetUrl, data) | ||
|
||
// const app = new Hono().use('/', (c) => createIPXWebServer(ipx)(c.req.raw)); | ||
return new Response(response.body, { | ||
status: response.status, | ||
statusText: response.statusText | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.