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

Sign up to get free protection for your applications and to get access to all the features.
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/esm/x/astro.js ADDED
@@ -0,0 +1,84 @@
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 { federation } from "./federation"; // Import the `Federation` object
9
+ *
10
+ * export const onRequest = defineMiddleware((context, next) => {
11
+ * return federation.fetch(context.request, {
12
+ * contextData: undefined,
13
+ * ...createFetchOptions(context, next),
14
+ * });
15
+ * });
16
+ * ```
17
+ *
18
+ * @typeParam TAstroContext A type of the Astro context.
19
+ * @param context An Astro context.
20
+ * @param next A function to call the next middleware.
21
+ * @returns Options for the {@link Federation.fetch} method.
22
+ * @since 0.12.0
23
+ */
24
+ export function createFetchOptions(_context, next) {
25
+ return {
26
+ // If the `federation` object finds a request not responsible for it
27
+ // (i.e., not a federation-related request), it will call the `next`
28
+ // provided by the Astro framework to continue the request handling
29
+ // by Astro:
30
+ onNotFound: next,
31
+ // Similar to `onNotFound`, but slightly more tricky one.
32
+ // When the `federation` object finds a request not acceptable type-wise
33
+ // (i.e., a user-agent doesn't want JSON-LD), it will call the `next`
34
+ // provided by the Astro framework so that it renders HTML if there's some
35
+ // page. Otherwise, it will simply return a 406 Not Acceptable response.
36
+ // This kind of trick enables the Fedify and Astro to share the same routes
37
+ // and they do content negotiation depending on `Accept` header:
38
+ async onNotAcceptable(_request) {
39
+ const response = await next();
40
+ if (response.status !== 404)
41
+ return response;
42
+ return new Response("Not acceptable", {
43
+ status: 406,
44
+ headers: {
45
+ "Content-Type": "text/plain",
46
+ Vary: "Accept",
47
+ },
48
+ });
49
+ },
50
+ };
51
+ }
52
+ /**
53
+ * Create an Astro middleware handler to integrate with the {@link Federation}
54
+ * object.
55
+ *
56
+ * @example src/middleware.ts
57
+ * ``` typescript
58
+ * import type { MiddlewareHandler } from "astro";
59
+ * import { federation } from "./federation"; // Import the `Federation` object
60
+ *
61
+ * export const onRequest: MiddlewareHandler = createMiddleware(
62
+ * federation,
63
+ * (astroContext) => "context data",
64
+ * );
65
+ * ```
66
+ *
67
+ * @typeParam TContextData A type of the context data for the {@link Federation}
68
+ * object.
69
+ * @typeParam TAstroContext A type of the Astro context.
70
+ * @param federation A {@link Federation} object to integrate with Astro.
71
+ * @param contextDataFactory A factory function to create a context data for the
72
+ * {@link Federation} object.
73
+ * @returns An Astro middleware handler.
74
+ * @since 0.12.0
75
+ */
76
+ export function createMiddleware(federation, contextDataFactory) {
77
+ return async (context, next) => {
78
+ const contextData = await contextDataFactory(context);
79
+ return await federation.fetch(context.request, {
80
+ contextData,
81
+ ...createFetchOptions(context, next),
82
+ });
83
+ };
84
+ }
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.284+5b347b26",
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",
@@ -0,0 +1,83 @@
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
+ * [Astro]: https://astro.build/
11
+ *
12
+ * @module
13
+ * @since 0.12.0
14
+ */
15
+ import type { Federation, FederationFetchOptions } from "../federation/middleware.js";
16
+ interface AstroContext {
17
+ request: Request;
18
+ }
19
+ type RewritePayload = string | URL | Request;
20
+ type MiddlewareNext = (rewritePayload?: RewritePayload) => Promise<Response>;
21
+ type MiddlewareHandler<TAstroContext extends AstroContext> = (context: TAstroContext, next: MiddlewareNext) => Promise<Response> | Response | Promise<void> | void;
22
+ /**
23
+ * Create options for the {@link Federation.fetch} method to integrate with
24
+ * Astro.
25
+ *
26
+ * @example src/middleware.ts
27
+ * ``` typescript
28
+ * import { defineMiddleware } from "astro:middleware";
29
+ * import { federation } from "./federation"; // Import the `Federation` object
30
+ *
31
+ * export const onRequest = defineMiddleware((context, next) => {
32
+ * return federation.fetch(context.request, {
33
+ * contextData: undefined,
34
+ * ...createFetchOptions(context, next),
35
+ * });
36
+ * });
37
+ * ```
38
+ *
39
+ * @typeParam TAstroContext A type of the Astro context.
40
+ * @param context An Astro context.
41
+ * @param next A function to call the next middleware.
42
+ * @returns Options for the {@link Federation.fetch} method.
43
+ * @since 0.12.0
44
+ */
45
+ export declare function createFetchOptions<TAstroContext extends AstroContext>(_context: TAstroContext, next: MiddlewareNext): Omit<FederationFetchOptions<void>, "contextData">;
46
+ /**
47
+ * The factory function to create a context data for
48
+ * the {@link Federation.fetch}.
49
+ *
50
+ * @typeParam TContextData A type of the context data.
51
+ * @typeParam TAstroContext A type of the Astro context.
52
+ * @param context An Astro context.
53
+ * @returns The context data for the {@link Federation.fetch}.
54
+ * @since 0.12.0
55
+ */
56
+ export type ContextDataFactory<TContextData, TAstroContext extends AstroContext> = (context: TAstroContext) => TContextData | Promise<TContextData>;
57
+ /**
58
+ * Create an Astro middleware handler to integrate with the {@link Federation}
59
+ * object.
60
+ *
61
+ * @example src/middleware.ts
62
+ * ``` typescript
63
+ * import type { MiddlewareHandler } from "astro";
64
+ * import { federation } from "./federation"; // Import the `Federation` object
65
+ *
66
+ * export const onRequest: MiddlewareHandler = createMiddleware(
67
+ * federation,
68
+ * (astroContext) => "context data",
69
+ * );
70
+ * ```
71
+ *
72
+ * @typeParam TContextData A type of the context data for the {@link Federation}
73
+ * object.
74
+ * @typeParam TAstroContext A type of the Astro context.
75
+ * @param federation A {@link Federation} object to integrate with Astro.
76
+ * @param contextDataFactory A factory function to create a context data for the
77
+ * {@link Federation} object.
78
+ * @returns An Astro middleware handler.
79
+ * @since 0.12.0
80
+ */
81
+ export declare function createMiddleware<TContextData, TAstroContext extends AstroContext>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData, TAstroContext>): MiddlewareHandler<TAstroContext>;
82
+ export {};
83
+ //# sourceMappingURL=astro.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"astro.d.ts","sourceRoot":"","sources":["../../src/x/astro.ts"],"names":[],"mappings":";;AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EACV,UAAU,EACV,sBAAsB,EACvB,MAAM,6BAA6B,CAAC;AAErC,UAAU,YAAY;IACpB,OAAO,EAAE,OAAO,CAAC;CAClB;AACD,KAAK,cAAc,GAAG,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC;AAC7C,KAAK,cAAc,GAAG,CACpB,cAAc,CAAC,EAAE,cAAc,KAC5B,OAAO,CAAC,QAAQ,CAAC,CAAC;AACvB,KAAK,iBAAiB,CAAC,aAAa,SAAS,YAAY,IAAI,CAC3D,OAAO,EAAE,aAAa,EACtB,IAAI,EAAE,cAAc,KACjB,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,kBAAkB,CAAC,aAAa,SAAS,YAAY,EACnE,QAAQ,EAAE,aAAa,EACvB,IAAI,EAAE,cAAc,GACnB,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,EAAE,aAAa,CAAC,CA2BnD;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,kBAAkB,CAC5B,YAAY,EACZ,aAAa,SAAS,YAAY,IAChC,CACF,OAAO,EAAE,aAAa,KACnB,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAC9B,YAAY,EACZ,aAAa,SAAS,YAAY,EAElC,UAAU,EAAE,UAAU,CAAC,YAAY,CAAC,EACpC,kBAAkB,EAAE,kBAAkB,CAAC,YAAY,EAAE,aAAa,CAAC,GAClE,iBAAiB,CAAC,aAAa,CAAC,CAQlC"}