-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #632 from lumeland/middleware/hatsu
feat: new middleware `redirect_as2`
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |