Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ucanto-server): proxy post requests #65

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Loading