Skip to content

Commit

Permalink
refactor(image/src): simplify route handling and type usage
Browse files Browse the repository at this point in the history
test(type-url.test.ts): add HEAD method tests for json and image
refactor(utils/types.ts): rename IPFSResponseType to ResponseType
  • Loading branch information
preschian committed Dec 6, 2023
1 parent 43bddd6 commit 72a5e67
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
8 changes: 2 additions & 6 deletions image/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { Hono } from 'hono'
import { cors } from 'hono/cors'

import { Env } from './utils/constants'
import { allowedOrigin } from './utils/cors'
import { getTypeUrl } from './routes/type-url'
import typeUrlRoute from './routes/type-url'
import ipfsRoute from './routes/ipfs'

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

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

app.route('/ipfs', ipfsRoute)

app.use('/type/url', cors({ origin: allowedOrigin }))
app.on(['GET', 'HEAD'], '/type/url', getTypeUrl)
app.route('/type/url', typeUrlRoute)

export default app
4 changes: 2 additions & 2 deletions image/src/routes/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CACHE_DAY, Env } from '../utils/constants'
import { fetchIPFS } from '../utils/ipfs'
import { getImageByPath, ipfsToCFI } from '../utils/cloudflare-images'
import { allowedOrigin } from '../utils/cors'
import type { IPFSResponseType } from '../utils/types'
import type { ResponseType } from '../utils/types'

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

Expand Down Expand Up @@ -101,7 +101,7 @@ app.get('/*', async (c) => {
if (contentLength === null) {
body = await status.response?.text()
} else {
body = status.response.body as IPFSResponseType
body = status.response.body as ResponseType
}

await c.env.MY_BUCKET.put(objectName, body, {
Expand Down
29 changes: 15 additions & 14 deletions image/src/routes/type-url.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import type { Context } from 'hono'
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { CACHE_TTL_BY_STATUS, type Env } from '../utils/constants'
import { urlToCFI } from '../utils/cloudflare-images'
import { allowedOrigin } from '../utils/cors'
import { ResponseType } from '../utils/types'

type HonoInterface = Context<
{
Bindings: Env
},
'/type/url',
{}
>
const app = new Hono<{ Bindings: Env }>()

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

export const getTypeUrl = async (c: HonoInterface) => {
app.use('/*', cors({ origin: allowedOrigin }))

app.get('/*', async (c) => {
const { endpoint } = c.req.query()
const path = encodeEndpoint(endpoint)
const isHead = c.req.method === 'HEAD'

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

if (currentImage.ok) {
if (currentImage.ok && !isHead) {
return c.redirect(cfImage, 302)
}

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

if (imageUrl) {
if (imageUrl && !isHead) {
return c.redirect(imageUrl, 302)
}

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

if (statusCode === 200) {
await c.env.MY_BUCKET.put(objectName, fetchObject.body, {
await c.env.MY_BUCKET.put(objectName, fetchObject.body as ResponseType, {
httpMetadata: fetchObject.headers,
})

Expand All @@ -86,4 +85,6 @@ export const getTypeUrl = async (c: HonoInterface) => {
// 5. if all else fails, redirect to original endpoint
// ----------------------------------------
return c.redirect(endpoint, 302)
}
})

export default app
22 changes: 22 additions & 0 deletions image/src/tests/type-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { expect, test } from 'vitest'

test('[head] ipfs - 200 - json', async () => {
const res = await fetch(
'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',
{ method: 'HEAD' }
)

expect(res.ok).toBe(true)
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('application/json')
})

test('[head] ipfs - 200 - image', async () => {
const res = await fetch(
'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',
{ method: 'HEAD', redirect: 'manual' }
)

expect(res.ok).toBe(true)
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('image/png')
})

test('type-url - 200 - json', async () => {
const res = await fetch(
'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'
Expand Down
2 changes: 1 addition & 1 deletion image/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface CFIApiResponse {
success: boolean
}

export type IPFSResponseType =
export type ResponseType =
| string
| ArrayBuffer
| Blob
Expand Down

0 comments on commit 72a5e67

Please sign in to comment.