Skip to content

Commit 84782e2

Browse files
committed
feat(ipfs.ts): add support for HEAD request method
fix(ipfs.ts): adjust image check conditions to include isHead test(ipfs.test.ts): add test case for HEAD request with image content type
1 parent f570bc6 commit 84782e2

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

image/src/routes/ipfs.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ app.use('/*', cors({ origin: allowedOrigin }))
1212
app.get('/*', async (c) => {
1313
const { original } = c.req.query()
1414
const isOriginal = original === 'true'
15+
const isHead = c.req.method === 'HEAD'
1516

1617
const url = new URL(c.req.url)
1718
const path = url.pathname.replace('/ipfs/', '')
@@ -34,7 +35,7 @@ app.get('/*', async (c) => {
3435
// 1. check existing image on cf-images && !isOriginal
3536
// ----------------------------------------
3637
console.log('step 1')
37-
if (mimeType?.includes('image') && !isOriginal) {
38+
if (mimeType?.includes('image') && !isOriginal && !isHead) {
3839
const publicUrl = await getImageByPath({
3940
token: c.env.IMAGE_API_TOKEN,
4041
imageAccount: c.env.CF_IMAGE_ACCOUNT,
@@ -119,7 +120,7 @@ app.get('/*', async (c) => {
119120
imageAccount: c.env.CF_IMAGE_ACCOUNT,
120121
})
121122

122-
if (imageUrl && !isOriginal) {
123+
if (imageUrl && !isOriginal && !isHead) {
123124
return c.redirect(imageUrl)
124125
}
125126

image/src/tests/ipfs.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ test('[head] ipfs - 200 - json', async () => {
1111
expect(res.headers.get('content-type')).toBe('application/json')
1212
})
1313

14+
test('[head] ipfs - 200 - image', async () => {
15+
const res = await fetch(
16+
'https://image-beta.w.kodadot.xyz/ipfs/bafybeidv3wgydacgpre67lkciihrttvwl5nibzftxfppy6lfanjja4v7zm',
17+
{ method: 'HEAD', redirect: 'manual' }
18+
)
19+
20+
expect(res.ok).toBe(true)
21+
expect(res.status).toBe(200)
22+
expect(res.headers.get('content-type')).toBe('image/jpeg')
23+
})
24+
1425
test('ipfs - 200 - json', async () => {
1526
const res = await fetch(
1627
'https://image-beta.w.kodadot.xyz/ipfs/bafkreihy6xwb35imb5hfwxzgmw2p64yoefuxysh6bkghyjwaj7tz5sfnuq'

0 commit comments

Comments
 (0)