@fedify/astro 2.1.0-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright 2024–2026 Hong Minhee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ <!-- deno-fmt-ignore-file -->
2
+
3
+ @fedify/astro: Integrate Fedify with `Astro`
4
+ ============================================
5
+
6
+ [![JSR][JSR badge]][JSR]
7
+ [![npm][npm badge]][npm]
8
+ [![Follow @fedify@hollo.social][@fedify@hollo.social badge]][@fedify@hollo.social]
9
+
10
+ *This package is available since Fedify 2.1.0.*
11
+
12
+ This package provides a simple way to integrate [Fedify] with [Astro].
13
+
14
+ First, add the integration to your *astro.config.mjs*:
15
+
16
+ ~~~~ typescript
17
+ import { defineConfig } from "astro/config";
18
+ import { fedifyIntegration } from "@fedify/astro";
19
+
20
+ export default defineConfig({
21
+ integrations: [fedifyIntegration()],
22
+ output: "server",
23
+ });
24
+ ~~~~
25
+
26
+ Then, create your middleware in *src/middleware.ts*:
27
+
28
+ ~~~~ typescript
29
+ import { createFederation } from "@fedify/fedify";
30
+ import { fedifyMiddleware } from "@fedify/astro";
31
+
32
+ const federation = createFederation<void>({
33
+ // Omitted for brevity; see the related section for details.
34
+ });
35
+
36
+ export const onRequest = fedifyMiddleware(
37
+ federation,
38
+ (context) => void 0,
39
+ );
40
+ ~~~~
41
+
42
+ [JSR badge]: https://jsr.io/badges/@fedify/astro
43
+ [JSR]: https://jsr.io/@fedify/astro
44
+ [npm badge]: https://img.shields.io/npm/v/@fedify/astro?logo=npm
45
+ [npm]: https://www.npmjs.com/package/@fedify/astro
46
+ [@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
47
+ [@fedify@hollo.social]: https://hollo.social/@fedify
48
+ [Fedify]: https://fedify.dev/
49
+ [Astro]: https://astro.build/
50
+
51
+
52
+ For Deno users
53
+ --------------
54
+
55
+ If you are using Deno, you should import `@deno/vite-adapter` in
56
+ *astro.config.mjs* and use it as the adapter:
57
+
58
+ ~~~~ typescript
59
+ import { defineConfig } from "astro/config";
60
+ import { fedifyIntegration } from "@fedify/astro";
61
+ import deno from "@deno/vite-adapter";
62
+ import deno from "@deno/astro-adapter";
63
+
64
+ export default defineConfig({
65
+ integrations: [fedifyIntegration()],
66
+ output: "server",
67
+ adapter: deno(),
68
+ });
69
+ ~~~~
70
+
71
+ And the tasks in *deno.json* should be updated to use `deno run npm:astro`
72
+ instead of `astro`:
73
+
74
+ ~~~~ json
75
+ {
76
+ "tasks": {
77
+ "dev": "deno run -A npm:astro dev",
78
+ "build": "deno run -A npm:astro build",
79
+ "preview": "deno run -A npm:astro preview"
80
+ }
81
+ }
82
+ ~~~~
83
+
84
+
85
+ How it works
86
+ ------------
87
+
88
+ Fedify behaves as a middleware that wraps around the Astro request handler.
89
+ The middleware intercepts the incoming HTTP requests and dispatches them to
90
+ the appropriate handler based on the request path and the `Accept` header
91
+ (i.e., content negotiation). This architecture allows Fedify and your Astro
92
+ application to coexist in the same domain and port.
93
+
94
+ The `fedifyIntegration()` function configures Vite's SSR settings to ensure
95
+ that `@fedify/fedify` and `@fedify/vocab` are properly bundled during SSR.
96
+
97
+ For example, if you make a request to */.well-known/webfinger* Fedify will
98
+ handle the request by itself, but if you make a request to */users/alice*
99
+ (assuming your Astro app has a page for that path) with
100
+ `Accept: text/html` header, Fedify will dispatch the request to Astro's
101
+ page handler. Or if you define an actor dispatcher
102
+ for `/users/{identifier}` in Fedify, and the request is made with
103
+ `Accept: application/activity+json` header, Fedify will dispatch the request
104
+ to the appropriate actor dispatcher.
105
+
106
+
107
+ Installation
108
+ ------------
109
+
110
+ ~~~~ sh
111
+ deno add jsr:@fedify/astro # Deno
112
+ npm add @fedify/astro # npm
113
+ pnpm add @fedify/astro # pnpm
114
+ yarn add @fedify/astro # Yarn
115
+ bun add @fedify/astro # Bun
116
+ ~~~~
package/dist/mod.cjs ADDED
@@ -0,0 +1,89 @@
1
+
2
+ //#region src/mod.ts
3
+ /**
4
+ * Create an Astro integration that configures Vite SSR settings for
5
+ * Fedify compatibility.
6
+ *
7
+ * This integration adds `@fedify/fedify` and `@fedify/vocab` to Vite's
8
+ * `ssr.noExternal` list, which is necessary because these packages contain
9
+ * dependencies that Vite must bundle for SSR.
10
+ *
11
+ * @example astro.config.mjs
12
+ * ``` typescript
13
+ * import { defineConfig } from "astro/config";
14
+ * import { fedifyIntegration } from "@fedify/astro";
15
+ *
16
+ * export default defineConfig({
17
+ * integrations: [fedifyIntegration()],
18
+ * output: "server",
19
+ * });
20
+ * ```
21
+ *
22
+ * @returns An Astro integration object.
23
+ * @since 2.1.0
24
+ */
25
+ function fedifyIntegration() {
26
+ return {
27
+ name: "@fedify/astro",
28
+ hooks: { "astro:config:setup": ({ updateConfig }) => {
29
+ updateConfig({ vite: { ssr: { noExternal: ["@fedify/fedify", "@fedify/vocab"] } } });
30
+ } }
31
+ };
32
+ }
33
+ /**
34
+ * Create an Astro middleware to integrate with the {@link Federation} object.
35
+ *
36
+ * @example src/middleware.ts
37
+ * ``` typescript
38
+ * import { createFederation } from "@fedify/fedify";
39
+ * import { fedifyMiddleware } from "@fedify/astro";
40
+ *
41
+ * const federation = createFederation<void>({
42
+ * // Omitted for brevity; see the related section for details.
43
+ * });
44
+ *
45
+ * export const onRequest = fedifyMiddleware(
46
+ * federation,
47
+ * (context) => undefined,
48
+ * );
49
+ * ```
50
+ *
51
+ * @template TContextData A type of the context data for the
52
+ * {@link Federation} object.
53
+ * @param federation A {@link Federation} object to integrate with Astro.
54
+ * @param contextDataFactory A function to create a context data for the
55
+ * {@link Federation} object.
56
+ * @returns An Astro middleware function.
57
+ * @since 2.1.0
58
+ */
59
+ function fedifyMiddleware(federation, contextDataFactory) {
60
+ return async (context, next) => {
61
+ const contextData = await contextDataFactory(context);
62
+ return await federation.fetch(context.request, {
63
+ contextData,
64
+ ...integrateFetchOptions(next)
65
+ });
66
+ };
67
+ }
68
+ function integrateFetchOptions(next) {
69
+ return {
70
+ async onNotFound(_request) {
71
+ return await next();
72
+ },
73
+ async onNotAcceptable(_request) {
74
+ const response = await next();
75
+ if (response.status !== 404) return response;
76
+ return new Response("Not acceptable", {
77
+ status: 406,
78
+ headers: {
79
+ "Content-Type": "text/plain",
80
+ Vary: "Accept"
81
+ }
82
+ });
83
+ }
84
+ };
85
+ }
86
+
87
+ //#endregion
88
+ exports.fedifyIntegration = fedifyIntegration;
89
+ exports.fedifyMiddleware = fedifyMiddleware;
package/dist/mod.d.cts ADDED
@@ -0,0 +1,68 @@
1
+ import { Federation } from "@fedify/fedify/federation";
2
+ import { APIContext, AstroIntegration, MiddlewareHandler } from "astro";
3
+
4
+ //#region src/mod.d.ts
5
+
6
+ /**
7
+ * A factory function to create a context data for the {@link Federation}
8
+ * object.
9
+ *
10
+ * @template TContextData A type of the context data for the
11
+ * {@link Federation} object.
12
+ * @param context An Astro context object.
13
+ * @returns A context data for the {@link Federation} object.
14
+ * @since 2.1.0
15
+ */
16
+ type ContextDataFactory<TContextData> = (context: APIContext) => TContextData | Promise<TContextData>;
17
+ /**
18
+ * Create an Astro integration that configures Vite SSR settings for
19
+ * Fedify compatibility.
20
+ *
21
+ * This integration adds `@fedify/fedify` and `@fedify/vocab` to Vite's
22
+ * `ssr.noExternal` list, which is necessary because these packages contain
23
+ * dependencies that Vite must bundle for SSR.
24
+ *
25
+ * @example astro.config.mjs
26
+ * ``` typescript
27
+ * import { defineConfig } from "astro/config";
28
+ * import { fedifyIntegration } from "@fedify/astro";
29
+ *
30
+ * export default defineConfig({
31
+ * integrations: [fedifyIntegration()],
32
+ * output: "server",
33
+ * });
34
+ * ```
35
+ *
36
+ * @returns An Astro integration object.
37
+ * @since 2.1.0
38
+ */
39
+ declare function fedifyIntegration(): AstroIntegration;
40
+ /**
41
+ * Create an Astro middleware to integrate with the {@link Federation} object.
42
+ *
43
+ * @example src/middleware.ts
44
+ * ``` typescript
45
+ * import { createFederation } from "@fedify/fedify";
46
+ * import { fedifyMiddleware } from "@fedify/astro";
47
+ *
48
+ * const federation = createFederation<void>({
49
+ * // Omitted for brevity; see the related section for details.
50
+ * });
51
+ *
52
+ * export const onRequest = fedifyMiddleware(
53
+ * federation,
54
+ * (context) => undefined,
55
+ * );
56
+ * ```
57
+ *
58
+ * @template TContextData A type of the context data for the
59
+ * {@link Federation} object.
60
+ * @param federation A {@link Federation} object to integrate with Astro.
61
+ * @param contextDataFactory A function to create a context data for the
62
+ * {@link Federation} object.
63
+ * @returns An Astro middleware function.
64
+ * @since 2.1.0
65
+ */
66
+ declare function fedifyMiddleware<TContextData>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData>): MiddlewareHandler;
67
+ //#endregion
68
+ export { ContextDataFactory, fedifyIntegration, fedifyMiddleware };
package/dist/mod.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ import { Federation } from "@fedify/fedify/federation";
2
+ import { APIContext, AstroIntegration, MiddlewareHandler } from "astro";
3
+
4
+ //#region src/mod.d.ts
5
+
6
+ /**
7
+ * A factory function to create a context data for the {@link Federation}
8
+ * object.
9
+ *
10
+ * @template TContextData A type of the context data for the
11
+ * {@link Federation} object.
12
+ * @param context An Astro context object.
13
+ * @returns A context data for the {@link Federation} object.
14
+ * @since 2.1.0
15
+ */
16
+ type ContextDataFactory<TContextData> = (context: APIContext) => TContextData | Promise<TContextData>;
17
+ /**
18
+ * Create an Astro integration that configures Vite SSR settings for
19
+ * Fedify compatibility.
20
+ *
21
+ * This integration adds `@fedify/fedify` and `@fedify/vocab` to Vite's
22
+ * `ssr.noExternal` list, which is necessary because these packages contain
23
+ * dependencies that Vite must bundle for SSR.
24
+ *
25
+ * @example astro.config.mjs
26
+ * ``` typescript
27
+ * import { defineConfig } from "astro/config";
28
+ * import { fedifyIntegration } from "@fedify/astro";
29
+ *
30
+ * export default defineConfig({
31
+ * integrations: [fedifyIntegration()],
32
+ * output: "server",
33
+ * });
34
+ * ```
35
+ *
36
+ * @returns An Astro integration object.
37
+ * @since 2.1.0
38
+ */
39
+ declare function fedifyIntegration(): AstroIntegration;
40
+ /**
41
+ * Create an Astro middleware to integrate with the {@link Federation} object.
42
+ *
43
+ * @example src/middleware.ts
44
+ * ``` typescript
45
+ * import { createFederation } from "@fedify/fedify";
46
+ * import { fedifyMiddleware } from "@fedify/astro";
47
+ *
48
+ * const federation = createFederation<void>({
49
+ * // Omitted for brevity; see the related section for details.
50
+ * });
51
+ *
52
+ * export const onRequest = fedifyMiddleware(
53
+ * federation,
54
+ * (context) => undefined,
55
+ * );
56
+ * ```
57
+ *
58
+ * @template TContextData A type of the context data for the
59
+ * {@link Federation} object.
60
+ * @param federation A {@link Federation} object to integrate with Astro.
61
+ * @param contextDataFactory A function to create a context data for the
62
+ * {@link Federation} object.
63
+ * @returns An Astro middleware function.
64
+ * @since 2.1.0
65
+ */
66
+ declare function fedifyMiddleware<TContextData>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData>): MiddlewareHandler;
67
+ //#endregion
68
+ export { ContextDataFactory, fedifyIntegration, fedifyMiddleware };
package/dist/mod.js ADDED
@@ -0,0 +1,87 @@
1
+ //#region src/mod.ts
2
+ /**
3
+ * Create an Astro integration that configures Vite SSR settings for
4
+ * Fedify compatibility.
5
+ *
6
+ * This integration adds `@fedify/fedify` and `@fedify/vocab` to Vite's
7
+ * `ssr.noExternal` list, which is necessary because these packages contain
8
+ * dependencies that Vite must bundle for SSR.
9
+ *
10
+ * @example astro.config.mjs
11
+ * ``` typescript
12
+ * import { defineConfig } from "astro/config";
13
+ * import { fedifyIntegration } from "@fedify/astro";
14
+ *
15
+ * export default defineConfig({
16
+ * integrations: [fedifyIntegration()],
17
+ * output: "server",
18
+ * });
19
+ * ```
20
+ *
21
+ * @returns An Astro integration object.
22
+ * @since 2.1.0
23
+ */
24
+ function fedifyIntegration() {
25
+ return {
26
+ name: "@fedify/astro",
27
+ hooks: { "astro:config:setup": ({ updateConfig }) => {
28
+ updateConfig({ vite: { ssr: { noExternal: ["@fedify/fedify", "@fedify/vocab"] } } });
29
+ } }
30
+ };
31
+ }
32
+ /**
33
+ * Create an Astro middleware to integrate with the {@link Federation} object.
34
+ *
35
+ * @example src/middleware.ts
36
+ * ``` typescript
37
+ * import { createFederation } from "@fedify/fedify";
38
+ * import { fedifyMiddleware } from "@fedify/astro";
39
+ *
40
+ * const federation = createFederation<void>({
41
+ * // Omitted for brevity; see the related section for details.
42
+ * });
43
+ *
44
+ * export const onRequest = fedifyMiddleware(
45
+ * federation,
46
+ * (context) => undefined,
47
+ * );
48
+ * ```
49
+ *
50
+ * @template TContextData A type of the context data for the
51
+ * {@link Federation} object.
52
+ * @param federation A {@link Federation} object to integrate with Astro.
53
+ * @param contextDataFactory A function to create a context data for the
54
+ * {@link Federation} object.
55
+ * @returns An Astro middleware function.
56
+ * @since 2.1.0
57
+ */
58
+ function fedifyMiddleware(federation, contextDataFactory) {
59
+ return async (context, next) => {
60
+ const contextData = await contextDataFactory(context);
61
+ return await federation.fetch(context.request, {
62
+ contextData,
63
+ ...integrateFetchOptions(next)
64
+ });
65
+ };
66
+ }
67
+ function integrateFetchOptions(next) {
68
+ return {
69
+ async onNotFound(_request) {
70
+ return await next();
71
+ },
72
+ async onNotAcceptable(_request) {
73
+ const response = await next();
74
+ if (response.status !== 404) return response;
75
+ return new Response("Not acceptable", {
76
+ status: 406,
77
+ headers: {
78
+ "Content-Type": "text/plain",
79
+ Vary: "Accept"
80
+ }
81
+ });
82
+ }
83
+ };
84
+ }
85
+
86
+ //#endregion
87
+ export { fedifyIntegration, fedifyMiddleware };
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@fedify/astro",
3
+ "version": "2.1.0-dev.0",
4
+ "description": "Integrate Fedify with Astro",
5
+ "keywords": [
6
+ "Fedify",
7
+ "ActivityPub",
8
+ "Fediverse",
9
+ "Astro"
10
+ ],
11
+ "author": {
12
+ "name": "ChanHaeng Lee",
13
+ "email": "2chanhaeng@gmail.com",
14
+ "url": "https://chomu.dev/"
15
+ },
16
+ "homepage": "https://fedify.dev/",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/fedify-dev/fedify.git",
20
+ "directory": "packages/astro"
21
+ },
22
+ "license": "MIT",
23
+ "bugs": {
24
+ "url": "https://github.com/fedify-dev/fedify/issues"
25
+ },
26
+ "funding": [
27
+ "https://opencollective.com/fedify",
28
+ "https://github.com/sponsors/dahlia"
29
+ ],
30
+ "type": "module",
31
+ "main": "./dist/mod.cjs",
32
+ "module": "./dist/mod.js",
33
+ "types": "./dist/mod.d.ts",
34
+ "exports": {
35
+ ".": {
36
+ "require": {
37
+ "types": "./dist/mod.d.cts",
38
+ "default": "./dist/mod.cjs"
39
+ },
40
+ "import": {
41
+ "types": "./dist/mod.d.ts",
42
+ "default": "./dist/mod.js"
43
+ }
44
+ },
45
+ "./package.json": "./package.json"
46
+ },
47
+ "files": [
48
+ "dist/",
49
+ "package.json"
50
+ ],
51
+ "peerDependencies": {
52
+ "astro": "^5.0.0",
53
+ "@fedify/fedify": "^2.1.0"
54
+ },
55
+ "devDependencies": {
56
+ "tsdown": "^0.12.9",
57
+ "typescript": "^5.9.3"
58
+ },
59
+ "scripts": {
60
+ "build:self": "tsdown",
61
+ "build": "pnpm --filter @fedify/astro... run build:self",
62
+ "prepublish": "pnpm build",
63
+ "test": "node --experimental-transform-types --test"
64
+ }
65
+ }