Skip to content

Commit

Permalink
fix: handling preflight requests (#66)
Browse files Browse the repository at this point in the history
* fix: handling preflight requests

* rename handler
  • Loading branch information
fforbeck authored Jan 10, 2025
1 parent f9d7c7b commit 60f8337
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
14 changes: 14 additions & 0 deletions packages/edge-gateway-link/src/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ export function addCorsHeaders (request, response) {
response.headers.set('Access-Control-Expose-Headers', 'Link')
return response
}

/**
* Handle OPTIONS requests in the proxy itself by returning a 204 No Content response with CORS headers.
*
* @param {Request} request
* @returns {Response}
*/
export function corsPreflightRequest (request) {
const headers = new Headers()
headers.set('Access-Control-Allow-Origin', request.headers.get('origin') || '*')
headers.set('Access-Control-Allow-Methods', 'GET, HEAD, POST, OPTIONS')
headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization')
return new Response(null, { headers, status: 204 })
}
5 changes: 3 additions & 2 deletions packages/edge-gateway-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ipnsGet } from './ipns.js'
import { versionGet } from './version.js'
import { gatewayGet } from './gateway.js'

import { addCorsHeaders, withCorsHeaders } from './cors.js'
import { addCorsHeaders, corsPreflightRequest, withCorsHeaders } from './cors.js'
import { errorHandler } from './error-handler.js'
import { envAll } from './env.js'

Expand All @@ -18,6 +18,7 @@ const router = Router()

router
.all('*', envAll)
.options('*', corsPreflightRequest)
.post('*', withCorsHeaders(proxyPostRequest))
.get('/version', withCorsHeaders(versionGet))
.get('/ipfs/:cid', withCorsHeaders(ipfsGet))
Expand Down Expand Up @@ -45,7 +46,7 @@ function serverError (error, request, env) {
* @param {import('./env').Env} env
* @returns {Promise<Response>}
*/
async function proxyPostRequest(request, env) {
async function proxyPostRequest (request, env) {
const originRequest = new Request(request)
const url = new URL(request.url)
const targetUrl = new URL(url.pathname, env.UCANTO_SERVER_URL)
Expand Down
7 changes: 7 additions & 0 deletions packages/edge-gateway-link/test/gateway.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,10 @@ test('Proxies POST requests to the UCANTO Server', async t => {
t.is(res.headers.get('X-Proxied-By'), 'TestUcantoServer')
t.true(res.ok)
})

test('Handles OPTIONS requests', async t => {
const res = await t.context.mf.dispatchFetch('http://localhost:8787', {
method: 'OPTIONS'
})
t.is(res.status, 204)
})
3 changes: 1 addition & 2 deletions packages/edge-gateway-link/test/scripts/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export default {
request.url.replace('localhost:8787', IPFS_GATEWAY_REDIRECT_HOSTNAME),
307
)
}
else if (subdomainCid) {
} else if (subdomainCid) {
const headers = new Headers({
etag: subdomainCid
})
Expand Down

0 comments on commit 60f8337

Please sign in to comment.