Skip to content

Commit

Permalink
feat(dag-w3s-link): proxy post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Dec 9, 2024
1 parent 10dc116 commit a3e769c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
composeMiddleware
} from '@web3-storage/gateway-lib/middleware'
import { HttpError } from '@web3-storage/gateway-lib/util'
import { withDenylist, withCdnCache } from './middleware.js'
import { withDenylist, withCdnCache, withPostProxy } from './middleware.js'

/**
* @typedef {import('./bindings').Environment} Environment
Expand All @@ -29,7 +29,8 @@ export default {
withContext,
withParsedIpfsUrl,
withDenylist,
withCdnCache
withCdnCache,
withPostProxy
)
return middleware(handler)(request, env, ctx)
}
Expand Down
19 changes: 19 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,22 @@ export function withCdnCache (handler) {
return response
}
}

/**
* Middleware to proxy all POST requests to the UCANTO Server defined in the environment.
*
* @type {import('@web3-storage/gateway-lib').Middleware<IpfsUrlContext, IpfsUrlContext, Environment>}
*/
export function withPostProxy (handler) {
return async (request, env, ctx) => {
if (request.method === 'POST') {
const originRequest = new Request(request)
const url = new URL(request.url)
const targetUrl = `https://${env.GATEWAY_URL}/${url.pathname}`
const response = await fetch(targetUrl, originRequest)
return response
}

return handler(request, env, ctx)
}
}

0 comments on commit a3e769c

Please sign in to comment.