From e4f9f639680fd6517bf18aae2fffc9042dd79117 Mon Sep 17 00:00:00 2001 From: Felipe Forbeck Date: Tue, 10 Dec 2024 10:40:31 -0300 Subject: [PATCH 1/2] feat(ucanto-server): proxy post requests --- packages/edge-gateway-link/src/bindings.d.ts | 1 + packages/edge-gateway-link/src/index.js | 16 +++++++++++++ .../edge-gateway-link/test/gateway.spec.js | 24 +++++++++++++++++++ packages/edge-gateway-link/wrangler.toml | 3 +++ 4 files changed, 44 insertions(+) diff --git a/packages/edge-gateway-link/src/bindings.d.ts b/packages/edge-gateway-link/src/bindings.d.ts index 2024e7b..c145041 100644 --- a/packages/edge-gateway-link/src/bindings.d.ts +++ b/packages/edge-gateway-link/src/bindings.d.ts @@ -13,6 +13,7 @@ export interface EnvInput { GATEWAY_HOSTNAME: string CSP_REPORT_URI: string GOODBITSLIST: KVNamespace + UCANTO_SERVER_URL: string } export interface EnvTransformed { diff --git a/packages/edge-gateway-link/src/index.js b/packages/edge-gateway-link/src/index.js index 7a645ae..afe013e 100644 --- a/packages/edge-gateway-link/src/index.js +++ b/packages/edge-gateway-link/src/index.js @@ -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)) @@ -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} + */ +async function proxyPostRequest(request, env) { + const originRequest = new Request(request) + const url = new URL(request.url) + const targetUrl = `${env.UCANTO_SERVER_URL}${url.pathname}` + const response = await fetch(targetUrl, originRequest) + return response +} + export default { /** * diff --git a/packages/edge-gateway-link/test/gateway.spec.js b/packages/edge-gateway-link/test/gateway.spec.js index 9b29530..2dc99d9 100644 --- a/packages/edge-gateway-link/test/gateway.spec.js +++ b/packages/edge-gateway-link/test/gateway.spec.js @@ -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 @@ -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) +}) diff --git a/packages/edge-gateway-link/wrangler.toml b/packages/edge-gateway-link/wrangler.toml index 0f1d991..45aefa9 100644 --- a/packages/edge-gateway-link/wrangler.toml +++ b/packages/edge-gateway-link/wrangler.toml @@ -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" @@ -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" @@ -82,3 +84,4 @@ kv_namespaces = [ GATEWAY_HOSTNAME = 'ipfs.localhost:8787' DEBUG = "true" ENV = "test" +UCANTO_SERVER_URL = 'http://localhost:8000' From 497126dd59d1dc4f3b2ed4e688935121f6e8b6de Mon Sep 17 00:00:00 2001 From: Felipe Forbeck Date: Tue, 10 Dec 2024 10:58:27 -0300 Subject: [PATCH 2/2] minor fix --- packages/edge-gateway-link/src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/edge-gateway-link/src/index.js b/packages/edge-gateway-link/src/index.js index afe013e..9019426 100644 --- a/packages/edge-gateway-link/src/index.js +++ b/packages/edge-gateway-link/src/index.js @@ -48,8 +48,8 @@ function serverError (error, request, env) { async function proxyPostRequest(request, env) { const originRequest = new Request(request) const url = new URL(request.url) - const targetUrl = `${env.UCANTO_SERVER_URL}${url.pathname}` - const response = await fetch(targetUrl, originRequest) + const targetUrl = new URL(url.pathname, env.UCANTO_SERVER_URL) + const response = await fetch(targetUrl.origin, originRequest) return response }