Skip to content

Commit

Permalink
feat(ucanto-server): proxy post requests (#65)
Browse files Browse the repository at this point in the history
* feat(ucanto-server): proxy post requests

* minor fix
  • Loading branch information
fforbeck authored Dec 10, 2024
1 parent 9fb065f commit d3a5673
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/edge-gateway-link/src/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface EnvInput {
GATEWAY_HOSTNAME: string
CSP_REPORT_URI: string
GOODBITSLIST: KVNamespace
UCANTO_SERVER_URL: string
}

export interface EnvTransformed {
Expand Down
16 changes: 16 additions & 0 deletions packages/edge-gateway-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const router = Router()

router
.all('*', envAll)
.post('*', withCorsHeaders(proxyPostRequest))
.get('/version', withCorsHeaders(versionGet))
.get('/ipfs/:cid', withCorsHeaders(ipfsGet))
.get('/ipfs/:cid/*', withCorsHeaders(ipfsGet))
Expand All @@ -37,6 +38,21 @@ function serverError (error, request, env) {
return addCorsHeaders(request, errorHandler(error, env))
}

/**
* Proxy POST requests to the UCANTO Server defined in the environment.
*
* @param {Request} request
* @param {import('./env').Env} env
* @returns {Promise<Response>}
*/
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)
const response = await fetch(targetUrl.origin, originRequest)
return response
}

export default {
/**
*
Expand Down
24 changes: 24 additions & 0 deletions packages/edge-gateway-link/test/gateway.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { test, getMiniflare } from './utils/setup.js'
import http from 'node:http'

test.before(async () => {
const ucantoServer = http.createServer((req, res) => {
if (req.method === 'POST') {
res.setHeader('X-Proxied-By', 'TestUcantoServer')
res.end()
} else {
res.statusCode = 405
res.end('Method Not Allowed')
}
})
await new Promise(resolve => ucantoServer.listen(8000, () => resolve(undefined)))
})

test.beforeEach((t) => {
// Create a new Miniflare environment for each test
Expand Down Expand Up @@ -75,3 +89,13 @@ test('Gets content with csp header when goodbits csp bypass tag does not exist',
const csp = response.headers.get('content-security-policy')
t.truthy(csp)
})

test('Proxies POST requests to the UCANTO Server', async t => {
const res = await t.context.mf.dispatchFetch('http://localhost:8787', {
method: 'POST',
body: JSON.stringify({ key: 'value' })
})

t.is(res.headers.get('X-Proxied-By'), 'TestUcantoServer')
t.true(res.ok)
})
3 changes: 3 additions & 0 deletions packages/edge-gateway-link/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ GATEWAY_HOSTNAME = 'ipfs.w3s.link'
CSP_REPORT_URI = 'https://csp-report-to.web3.storage'
DEBUG = "false"
ENV = "production"
UCANTO_SERVER_URL = 'https://freeway.dag.haus'

[[env.production.services]]
binding = "EDGE_GATEWAY"
Expand Down Expand Up @@ -64,6 +65,7 @@ GATEWAY_HOSTNAME = 'ipfs-staging.w3s.link'
CSP_REPORT_URI = 'https://staging.csp-report-to.web3.storage'
DEBUG = "true"
ENV = "staging"
UCANTO_SERVER_URL = 'https://freeway-staging.dag.haus'

[[env.staging.services]]
binding = "EDGE_GATEWAY"
Expand All @@ -82,3 +84,4 @@ kv_namespaces = [
GATEWAY_HOSTNAME = 'ipfs.localhost:8787'
DEBUG = "true"
ENV = "test"
UCANTO_SERVER_URL = 'http://localhost:8000'

0 comments on commit d3a5673

Please sign in to comment.