Skip to content

Commit 72a5e67

Browse files
committed
refactor(image/src): simplify route handling and type usage
test(type-url.test.ts): add HEAD method tests for json and image refactor(utils/types.ts): rename IPFSResponseType to ResponseType
1 parent 43bddd6 commit 72a5e67

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

image/src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { Hono } from 'hono'
2-
import { cors } from 'hono/cors'
32

43
import { Env } from './utils/constants'
5-
import { allowedOrigin } from './utils/cors'
6-
import { getTypeUrl } from './routes/type-url'
4+
import typeUrlRoute from './routes/type-url'
75
import ipfsRoute from './routes/ipfs'
86

97
const app = new Hono<{ Bindings: Env }>()
108

119
app.get('/', (c) => c.text('Hello! cf-workers!'))
1210

1311
app.route('/ipfs', ipfsRoute)
14-
15-
app.use('/type/url', cors({ origin: allowedOrigin }))
16-
app.on(['GET', 'HEAD'], '/type/url', getTypeUrl)
12+
app.route('/type/url', typeUrlRoute)
1713

1814
export default app

image/src/routes/ipfs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CACHE_DAY, Env } from '../utils/constants'
44
import { fetchIPFS } from '../utils/ipfs'
55
import { getImageByPath, ipfsToCFI } from '../utils/cloudflare-images'
66
import { allowedOrigin } from '../utils/cors'
7-
import type { IPFSResponseType } from '../utils/types'
7+
import type { ResponseType } from '../utils/types'
88

99
const app = new Hono<{ Bindings: Env }>()
1010

@@ -101,7 +101,7 @@ app.get('/*', async (c) => {
101101
if (contentLength === null) {
102102
body = await status.response?.text()
103103
} else {
104-
body = status.response.body as IPFSResponseType
104+
body = status.response.body as ResponseType
105105
}
106106

107107
await c.env.MY_BUCKET.put(objectName, body, {

image/src/routes/type-url.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import type { Context } from 'hono'
1+
import { Hono } from 'hono'
2+
import { cors } from 'hono/cors'
23
import { CACHE_TTL_BY_STATUS, type Env } from '../utils/constants'
34
import { urlToCFI } from '../utils/cloudflare-images'
5+
import { allowedOrigin } from '../utils/cors'
6+
import { ResponseType } from '../utils/types'
47

5-
type HonoInterface = Context<
6-
{
7-
Bindings: Env
8-
},
9-
'/type/url',
10-
{}
11-
>
8+
const app = new Hono<{ Bindings: Env }>()
129

1310
export const encodeEndpoint = (endpoint: string) => {
14-
// return encodeURIComponent(endpoint);
1511
return endpoint.replace(/[:,._/]/g, '-')
1612
}
1713

18-
export const getTypeUrl = async (c: HonoInterface) => {
14+
app.use('/*', cors({ origin: allowedOrigin }))
15+
16+
app.get('/*', async (c) => {
1917
const { endpoint } = c.req.query()
2018
const path = encodeEndpoint(endpoint)
19+
const isHead = c.req.method === 'HEAD'
2120

2221
// 1. check existing image on cf-images
2322
// ----------------------------------------
@@ -27,7 +26,7 @@ export const getTypeUrl = async (c: HonoInterface) => {
2726
cf: CACHE_TTL_BY_STATUS,
2827
})
2928

30-
if (currentImage.ok) {
29+
if (currentImage.ok && !isHead) {
3130
return c.redirect(cfImage, 302)
3231
}
3332

@@ -55,7 +54,7 @@ export const getTypeUrl = async (c: HonoInterface) => {
5554
imageAccount: c.env.CF_IMAGE_ACCOUNT,
5655
})
5756

58-
if (imageUrl) {
57+
if (imageUrl && !isHead) {
5958
return c.redirect(imageUrl, 302)
6059
}
6160

@@ -65,7 +64,7 @@ export const getTypeUrl = async (c: HonoInterface) => {
6564
const statusCode = fetchObject.status
6665

6766
if (statusCode === 200) {
68-
await c.env.MY_BUCKET.put(objectName, fetchObject.body, {
67+
await c.env.MY_BUCKET.put(objectName, fetchObject.body as ResponseType, {
6968
httpMetadata: fetchObject.headers,
7069
})
7170

@@ -86,4 +85,6 @@ export const getTypeUrl = async (c: HonoInterface) => {
8685
// 5. if all else fails, redirect to original endpoint
8786
// ----------------------------------------
8887
return c.redirect(endpoint, 302)
89-
}
88+
})
89+
90+
export default app

image/src/tests/type-url.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
import { expect, test } from 'vitest'
22

3+
test('[head] ipfs - 200 - json', async () => {
4+
const res = await fetch(
5+
'https://image-beta.w.kodadot.xyz/type/url?endpoint=https://polkadot-data.s3.us-east-2.amazonaws.com/metadata/nfts-88/nfts-88_collection-meta.json',
6+
{ method: 'HEAD' }
7+
)
8+
9+
expect(res.ok).toBe(true)
10+
expect(res.status).toBe(200)
11+
expect(res.headers.get('content-type')).toBe('application/json')
12+
})
13+
14+
test('[head] ipfs - 200 - image', async () => {
15+
const res = await fetch(
16+
'https://image-beta.w.kodadot.xyz/type/url?endpoint=https://polkadot-data.s3.us-east-2.amazonaws.com/metadata/nfts-88/nfts-88_collection-img.png',
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/png')
23+
})
24+
325
test('type-url - 200 - json', async () => {
426
const res = await fetch(
527
'https://image-beta.w.kodadot.xyz/type/url?endpoint=https://polkadot-data.s3.us-east-2.amazonaws.com/metadata/nfts-88/nfts-88_collection-meta.json'

image/src/utils/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface CFIApiResponse {
1515
success: boolean
1616
}
1717

18-
export type IPFSResponseType =
18+
export type ResponseType =
1919
| string
2020
| ArrayBuffer
2121
| Blob

0 commit comments

Comments
 (0)