@oceanum/datamesh / Proxy Guide
A reverse proxy lets you call Datamesh from a public web app without exposing your Datamesh token and helps you avoid CORS issues.
This repo includes example proxies you can deploy quickly, and shows how to point the Connector to them.
packages/datamesh/proxy/cloudflare/index.jspackages/datamesh/proxy/express/index.js@oceanum/datameshThe example Cloudflare Worker:
x-DATAMESH-TOKEN header using a Worker Secret.// Example Cloudflare Worker reverse proxy
// Add your datamesh token as a secret in the Cloudflare worker environment
const DATAMESH = "https://datamesh.oceanum.io";
export default {
async fetch(request, env) {
const url = new URL(request.url);
const DATAMESH_TOKEN = env.DATAMESH_TOKEN;
// Build the upstream request URL
const datameshUrl = new URL(url.pathname + url.search, DATAMESH);
// Clone request and forward body/headers/method
const modifiedRequest = new Request(datameshUrl.toString(), {
method: request.method,
headers: request.headers,
body: request.body,
});
// Inject/overwrite the token
modifiedRequest.headers.set("x-DATAMESH-TOKEN", `${DATAMESH_TOKEN}`);
// Forward
const response = await fetch(modifiedRequest);
// Add CORS headers for the browser
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: {
...response.headers,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers":
"Content-Type, Authorization, x-requested-with, x-datamesh-token",
},
});
},
};
packages/datamesh/proxy/cloudflare/index.js.DATAMESH_TOKEN and set it to your Datamesh token.DATAMESH to point at a different upstream if needed.https://your-proxy.workers.dev.wrangler dev.You can also run a simple Node/Express reverse proxy locally or deploy it to your own infrastructure.
packages/datamesh/proxy/express/index.jsInstall dependencies in the example folder:
cd packages/datamesh/proxy/express
npm init -y
npm install express
Start the proxy:
DATAMESH_TOKEN=your_token_here node index.js
# Optional:
# DATAMESH_URL=https://datamesh.oceanum.io PORT=8787 DATAMESH_TOKEN=your_token_here node index.js
The proxy will listen on http://localhost:8787 by default and forward all requests to DATAMESH_URL, injecting x-DATAMESH-TOKEN and adding permissive CORS headers.
Point both service and gateway to your proxy origin. The proxy injects the token, so you can pass any non-empty string for the required token parameter.
import { Connector } from "@oceanum/datamesh";
const PROXY_URL = "https://your-proxy.workers.dev"; // or your custom domain
const connector = new Connector("proxy", {
service: PROXY_URL,
gateway: PROXY_URL,
// Optional: tweak caching and session duration as needed
// nocache: true,
// sessionDuration: 1,
});
Notes:
token. When using the proxy, the token you pass here is ignored by the upstream because the proxy overwrites the x-DATAMESH-TOKEN header with the secret.GET /session on the gateway to detect the API version. Ensure your proxy forwards that path.x-DATAMESH-TOKEN to prevent client-supplied values from leaking upstream.