@fedify/fedify 0.12.0-dev.283 → 0.12.0-dev.285

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/CHANGES.md CHANGED
@@ -96,17 +96,26 @@ To be released.
96
96
  loader that throws an error when the given URL is not an HTTP or HTTPS
97
97
  URL or refers to a private network address.
98
98
 
99
+ - Added `@fedify/fedify/x/astro` module for integrating with [Astro]
100
+ middleware. [[#50]]
101
+
102
+ - Added `createMiddleware()` function.
103
+ - Added `createFetchOptions()` function.
104
+ - Added `ContextDataFactory` type.
105
+
99
106
  - Added more log messages using the [LogTape] library. Currently the below
100
107
  logger categories are used:
101
108
 
102
109
  - `["fedify", "federation", "queue"]`
103
110
 
111
+ [#50]: https://github.com/dahlia/fedify/issues/50
104
112
  [#53]: https://github.com/dahlia/fedify/issues/53
105
113
  [#66]: https://github.com/dahlia/fedify/issues/66
106
114
  [#70]: https://github.com/dahlia/fedify/issues/70
107
115
  [#81]: https://github.com/dahlia/fedify/issues/81
108
116
  [#85]: https://github.com/dahlia/fedify/issues/85
109
117
  [#92]: https://github.com/dahlia/fedify/pull/92
118
+ [Astro]: https://astro.build/
110
119
 
111
120
 
112
121
  Version 0.11.2
package/README.md CHANGED
@@ -30,6 +30,7 @@ Currently, Fedify provides the following features out of the box:
30
30
  - [NodeInfo] protocol
31
31
  - Special touch for interoperability with Mastodon and few other popular
32
32
  fediverse software
33
+ - Integration with various web frameworks
33
34
  - CLI toolchain for testing and debugging
34
35
 
35
36
  If you want to know more about the project, please take a look at the following
package/esm/mod.js CHANGED
@@ -19,6 +19,7 @@
19
19
  * - [NodeInfo] protocol
20
20
  * - Special touch for interoperability with Mastodon and few other popular
21
21
  * fediverse software
22
+ * - Integration with various web frameworks
22
23
  * - CLI toolchain for testing and debugging
23
24
  *
24
25
  * If you want to know more about the project, please take a look at the
package/esm/x/astro.js ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Create options for the {@link Federation.fetch} method to integrate with
3
+ * Astro.
4
+ *
5
+ * @example src/middleware.ts
6
+ * ``` typescript
7
+ * import { defineMiddleware } from "astro:middleware";
8
+ * import { createFetchOptions } from "@fedify/fedify/x/astro";
9
+ * import { federation } from "./federation"; // Import the `Federation` object
10
+ *
11
+ * export const onRequest = defineMiddleware((context, next) => {
12
+ * return federation.fetch(context.request, {
13
+ * contextData: undefined,
14
+ * ...createFetchOptions(context, next),
15
+ * });
16
+ * });
17
+ * ```
18
+ *
19
+ * @typeParam TAstroContext A type of the Astro context.
20
+ * @param context An Astro context.
21
+ * @param next A function to call the next middleware.
22
+ * @returns Options for the {@link Federation.fetch} method.
23
+ * @since 0.12.0
24
+ */
25
+ export function createFetchOptions(_context, next) {
26
+ return {
27
+ // If the `federation` object finds a request not responsible for it
28
+ // (i.e., not a federation-related request), it will call the `next`
29
+ // provided by the Astro framework to continue the request handling
30
+ // by Astro:
31
+ onNotFound: next,
32
+ // Similar to `onNotFound`, but slightly more tricky one.
33
+ // When the `federation` object finds a request not acceptable type-wise
34
+ // (i.e., a user-agent doesn't want JSON-LD), it will call the `next`
35
+ // provided by the Astro framework so that it renders HTML if there's some
36
+ // page. Otherwise, it will simply return a 406 Not Acceptable response.
37
+ // This kind of trick enables the Fedify and Astro to share the same routes
38
+ // and they do content negotiation depending on `Accept` header:
39
+ async onNotAcceptable(_request) {
40
+ const response = await next();
41
+ if (response.status !== 404)
42
+ return response;
43
+ return new Response("Not acceptable", {
44
+ status: 406,
45
+ headers: {
46
+ "Content-Type": "text/plain",
47
+ Vary: "Accept",
48
+ },
49
+ });
50
+ },
51
+ };
52
+ }
53
+ /**
54
+ * Create an Astro middleware handler to integrate with the {@link Federation}
55
+ * object.
56
+ *
57
+ * @example src/middleware.ts
58
+ * ``` typescript
59
+ * import type { MiddlewareHandler } from "astro";
60
+ * import { createMiddleware } from "@fedify/fedify/x/astro";
61
+ * import { federation } from "./federation"; // Import the `Federation` object
62
+ *
63
+ * export const onRequest: MiddlewareHandler = createMiddleware(
64
+ * federation,
65
+ * (astroContext) => "context data",
66
+ * );
67
+ * ```
68
+ *
69
+ * @typeParam TContextData A type of the context data for the {@link Federation}
70
+ * object.
71
+ * @typeParam TAstroContext A type of the Astro context.
72
+ * @param federation A {@link Federation} object to integrate with Astro.
73
+ * @param contextDataFactory A factory function to create a context data for the
74
+ * {@link Federation} object.
75
+ * @returns An Astro middleware handler.
76
+ * @since 0.12.0
77
+ */
78
+ export function createMiddleware(federation, contextDataFactory) {
79
+ return async (context, next) => {
80
+ const contextData = await contextDataFactory(context);
81
+ return await federation.fetch(context.request, {
82
+ contextData,
83
+ ...createFetchOptions(context, next),
84
+ });
85
+ };
86
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/fedify",
3
- "version": "0.12.0-dev.283+1711224a",
3
+ "version": "0.12.0-dev.285+e564a8c0",
4
4
  "description": "An ActivityPub server framework",
5
5
  "keywords": [
6
6
  "ActivityPub",
@@ -65,6 +65,12 @@
65
65
  "default": "./esm/webfinger/mod.js"
66
66
  }
67
67
  },
68
+ "./x/astro": {
69
+ "import": {
70
+ "types": "./types/x/astro.d.ts",
71
+ "default": "./esm/x/astro.js"
72
+ }
73
+ },
68
74
  "./x/hono": {
69
75
  "import": {
70
76
  "types": "./types/x/hono.d.ts",
package/types/mod.d.ts CHANGED
@@ -19,6 +19,7 @@
19
19
  * - [NodeInfo] protocol
20
20
  * - Special touch for interoperability with Mastodon and few other popular
21
21
  * fediverse software
22
+ * - Integration with various web frameworks
22
23
  * - CLI toolchain for testing and debugging
23
24
  *
24
25
  * If you want to know more about the project, please take a look at the
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,OAAO,EAAE,eAAe,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,OAAO,EAAE,eAAe,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,90 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /**
4
+ * Fedify with Astro
5
+ * =================
6
+ *
7
+ * This module contains some utilities for integrating Fedify with
8
+ * the [Astro] framework.
9
+ *
10
+ * > [!NOTE]
11
+ * >
12
+ * > Astro integration requires [on-demand server rendering][1].
13
+ *
14
+ * [Astro]: https://astro.build/
15
+ * [1]: https://docs.astro.build/en/guides/server-side-rendering/
16
+ *
17
+ * @module
18
+ * @since 0.12.0
19
+ */
20
+ import type { Federation, FederationFetchOptions } from "../federation/middleware.js";
21
+ interface AstroContext {
22
+ request: Request;
23
+ }
24
+ type RewritePayload = string | URL | Request;
25
+ type MiddlewareNext = (rewritePayload?: RewritePayload) => Promise<Response>;
26
+ type MiddlewareHandler<TAstroContext extends AstroContext> = (context: TAstroContext, next: MiddlewareNext) => Promise<Response> | Response | Promise<void> | void;
27
+ /**
28
+ * Create options for the {@link Federation.fetch} method to integrate with
29
+ * Astro.
30
+ *
31
+ * @example src/middleware.ts
32
+ * ``` typescript
33
+ * import { defineMiddleware } from "astro:middleware";
34
+ * import { createFetchOptions } from "@fedify/fedify/x/astro";
35
+ * import { federation } from "./federation"; // Import the `Federation` object
36
+ *
37
+ * export const onRequest = defineMiddleware((context, next) => {
38
+ * return federation.fetch(context.request, {
39
+ * contextData: undefined,
40
+ * ...createFetchOptions(context, next),
41
+ * });
42
+ * });
43
+ * ```
44
+ *
45
+ * @typeParam TAstroContext A type of the Astro context.
46
+ * @param context An Astro context.
47
+ * @param next A function to call the next middleware.
48
+ * @returns Options for the {@link Federation.fetch} method.
49
+ * @since 0.12.0
50
+ */
51
+ export declare function createFetchOptions<TAstroContext extends AstroContext>(_context: TAstroContext, next: MiddlewareNext): Omit<FederationFetchOptions<void>, "contextData">;
52
+ /**
53
+ * The factory function to create a context data for
54
+ * the {@link Federation.fetch}.
55
+ *
56
+ * @typeParam TContextData A type of the context data.
57
+ * @typeParam TAstroContext A type of the Astro context.
58
+ * @param context An Astro context.
59
+ * @returns The context data for the {@link Federation.fetch}.
60
+ * @since 0.12.0
61
+ */
62
+ export type ContextDataFactory<TContextData, TAstroContext extends AstroContext> = (context: TAstroContext) => TContextData | Promise<TContextData>;
63
+ /**
64
+ * Create an Astro middleware handler to integrate with the {@link Federation}
65
+ * object.
66
+ *
67
+ * @example src/middleware.ts
68
+ * ``` typescript
69
+ * import type { MiddlewareHandler } from "astro";
70
+ * import { createMiddleware } from "@fedify/fedify/x/astro";
71
+ * import { federation } from "./federation"; // Import the `Federation` object
72
+ *
73
+ * export const onRequest: MiddlewareHandler = createMiddleware(
74
+ * federation,
75
+ * (astroContext) => "context data",
76
+ * );
77
+ * ```
78
+ *
79
+ * @typeParam TContextData A type of the context data for the {@link Federation}
80
+ * object.
81
+ * @typeParam TAstroContext A type of the Astro context.
82
+ * @param federation A {@link Federation} object to integrate with Astro.
83
+ * @param contextDataFactory A factory function to create a context data for the
84
+ * {@link Federation} object.
85
+ * @returns An Astro middleware handler.
86
+ * @since 0.12.0
87
+ */
88
+ export declare function createMiddleware<TContextData, TAstroContext extends AstroContext>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData, TAstroContext>): MiddlewareHandler<TAstroContext>;
89
+ export {};
90
+ //# sour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