Skip to content
Merged
Changes from 5 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
68 changes: 68 additions & 0 deletions middlewares/redirect_as2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { Middleware } from "../core/server.ts";

export type Options = (url: URL) => Promise<URL> | URL | undefined | void;
Comment thread
kwaa marked this conversation as resolved.
Outdated

export interface CommonOptions {
/**
* User domain.
* @default
* ```ts
* new URL(req.url).host
* ```
* @example
* ```ts
* site.options.location.host
* ```
*/
host?: URL["host"];
}

export interface HatsuOptions extends CommonOptions {
/**
* Hatsu instance.
* @default `hatsu.local`
*/
instance: URL["host"];
}

export interface BridgyFedOptions extends CommonOptions {
/**
* BridgyFed instance.
* @default `fed.brid.gy`
*/
instance?: URL["host"];
}

export const hatsu = (options: HatsuOptions): Options => (url: URL) => {
Comment thread
kwaa marked this conversation as resolved.
Outdated
const { host, origin, pathname } = new URL(url);
if (pathname === "/") {
return new URL(`https://${options.instance}/users/${options.host ?? host}`);
} else {return new URL(
`${origin}${pathname}`,
`https://${options.instance}/posts/`,
);}
};

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

export default (rewriteUrl: 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 rewriteUrl(new URL(req.url));
if (dest) return Response.redirect(dest);
}

return await next(req);
};