1
0

Federation URL handling

This commit is contained in:
Zef Hemel 2023-11-16 13:55:02 +01:00
parent 1af5e022a0
commit 6336190c20
3 changed files with 54 additions and 40 deletions

View File

@ -4,7 +4,7 @@ import { loadPageObject, replaceTemplateVars } from "../template/template.ts";
import { PageMeta } from "$sb/types.ts";
import { renderTemplate } from "../template/plug_api.ts";
import { renderToText } from "$sb/lib/tree.ts";
import { rewritePageRefs } from "$sb/lib/resolve.ts";
import { rewritePageRefs, rewritePageRefsInString } from "$sb/lib/resolve.ts";
type TemplateConfig = {
// Pull the template from a page
@ -28,6 +28,8 @@ export async function widget(
let templateText = config.template || "";
let templatePage = config.page;
if (templatePage) {
// Rewrite federation page references
templatePage = rewritePageRefsInString(templatePage, pageName);
if (templatePage.startsWith("[[")) {
templatePage = templatePage.slice(2, -2);
}

View File

@ -388,6 +388,7 @@ export class HttpServer {
// Handle federated links through a simple redirect, only used for attachments loads with service workers disabled
if (name.startsWith("!")) {
let url = name.slice(1);
console.log("Handling this as a federated link", url);
if (url.startsWith("localhost")) {
url = `http://${url}`;
} else {
@ -489,9 +490,20 @@ export class HttpServer {
})
.options(filePathRegex, corsMiddleware);
// Federation proxy
const proxyPathRegex = "\/!(.+)";
fsRouter.all(proxyPathRegex, async ({ params, response, request }) => {
fsRouter.all(
proxyPathRegex,
async ({ params, response, request }, next) => {
let url = params[0];
if (!request.headers.has("X-Proxy-Request")) {
// Direct browser request, not explicity fetch proxy request
if (!/\.[a-zA-Z0-9]+$/.test(url)) {
console.log("Directly loading federation page via URL:", url);
// This is not a direct file reference so LIKELY a page request, fall through and load the SB UI
return next();
}
}
console.log("Requested path to proxy", url, request.method);
if (url.startsWith("localhost")) {
url = `http://${url}`;
@ -500,7 +512,9 @@ export class HttpServer {
}
try {
const safeRequestHeaders = new Headers();
for (const headerName of ["Authorization", "Accept", "Content-Type"]) {
for (
const headerName of ["Authorization", "Accept", "Content-Type"]
) {
if (request.headers.has(headerName)) {
safeRequestHeaders.set(
headerName,
@ -516,12 +530,6 @@ export class HttpServer {
: undefined,
});
response.status = req.status;
// // Override X-Permssion header to always be "ro"
// const newHeaders = new Headers();
// for (const [key, value] of req.headers.entries()) {
// newHeaders.set(key, value);
// }
// newHeaders.set("X-Permission", "ro");
response.headers = req.headers;
response.body = req.body;
} catch (e: any) {
@ -530,7 +538,8 @@ export class HttpServer {
response.body = e.message;
}
return;
});
},
);
return fsRouter;
}

View File

@ -32,6 +32,9 @@ export function sandboxFetchSyscalls(
// No SB server to proxy the fetch available so let's execute the request directly
return performLocalFetch(url, fetchOptions);
}
fetchOptions.headers = fetchOptions.headers
? { ...fetchOptions.headers, "X-Proxy-Request": "true" }
: { "X-Proxy-Request": "true" };
const resp = await client.remoteSpacePrimitives.authenticatedFetch(
`${client.remoteSpacePrimitives.url}/!${url}`,
fetchOptions,