Skip to content

Commit

Permalink
Merge pull request #632 from lumeland/middleware/hatsu
Browse files Browse the repository at this point in the history
feat: new middleware `redirect_as2`
  • Loading branch information
oscarotero committed Jul 15, 2024
2 parents 15727dc + ee55cd8 commit 20eb2ad
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions middlewares/redirect_as2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { Middleware } from "../core/server.ts";

export interface Data {
/**
* User domain.
* @default
* ```ts
* new URL(req.url).host
* ```
* @example
* ```ts
* site.options.location.host
* ```
*/
host?: URL["host"];
}
export interface Options extends Data {
/**
* Rewrite URL function.
* @param url req.url
* @returns Rewritten URLs or empty
*/
rewriteUrl: (url: URL, data: Data) => Promise<URL> | URL | undefined | void;
/**
* User domain.
* @default
* ```ts
* new URL(req.url).host
* ```
* @example
* ```ts
* site.options.location.host
* ```
*/
host?: URL["host"];
}

export const hatsu =
(instance: URL["host"]): Options["rewriteUrl"] => (url, data) => {
const { pathname } = url;
const host = data.host ?? url.host;
if (url.pathname === "/") {
return new URL(`https://${instance}/users/${host}`);
} else {
return new URL(
`https://${host}${pathname}`,
`https://${instance}/posts/`,
);
}
};

export const bridgyFed =
(instance: URL["host"] = "fed.brid.gy"): Options["rewriteUrl"] =>
(url, data) => {
const { pathname } = url;
const host = data.host ?? url.host;
if (pathname === "/") return new URL(`https://${instance}/${host}`);
else return new URL(`https://${host}${pathname}`, `https://${instance}/r/`);
};

export default (options: Options): Middleware => async (req, next) => {
const accept = req.headers.get("accept");
if (
accept && [
"application/activity+json",
'application/ld+json;profile="http://www.w3.org/ns/activitystreams"',
'application/ld+json; profile="http://www.w3.org/ns/activitystreams"',
].some((type) => (accept.includes(type)))
) {
const dest = await options.rewriteUrl(new URL(req.url), {
host: options.host,
});
if (dest) return Response.redirect(dest);
}

return await next(req);
};

0 comments on commit 20eb2ad

Please sign in to comment.