@fedify/next 1.9.0-dev.1404 → 1.9.0-dev.1408

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/README.md CHANGED
@@ -37,16 +37,30 @@ export default fedifyWith(federation)();
37
37
  // This config must be defined on `middleware.ts`.
38
38
  export const config = {
39
39
  runtime: "nodejs",
40
- matcher: [{
41
- source: "/:path*",
42
- has: [
43
- {
44
- type: "header",
45
- key: "Accept",
46
- value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
47
- },
48
- ],
49
- }],
40
+ matcher: [
41
+ {
42
+ source: "/:path*",
43
+ has: [
44
+ {
45
+ type: "header",
46
+ key: "Accept",
47
+ value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
48
+ },
49
+ ],
50
+ },
51
+ {
52
+ source: "/:path*",
53
+ has: [
54
+ {
55
+ type: "header",
56
+ key: "content-type",
57
+ value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
58
+ },
59
+ ],
60
+ },
61
+ { source: "/.well-known/nodeinfo" },
62
+ { source: "/.well-known/x-nodeinfo2" },
63
+ ],
50
64
  };
51
65
  ~~~~
52
66
 
@@ -108,16 +122,30 @@ type ErrorHandlers = Omit<FederationFetchOptions<unknown>, "contextData">;
108
122
  * // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
109
123
  * export const config = {
110
124
  * runtime: "nodejs",
111
- * matcher: [{
112
- * source: "/:path*",
113
- * has: [
114
- * {
115
- * type: "header",
116
- * key: "Accept",
117
- * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
118
- * },
119
- * ],
120
- * }],
125
+ * matcher: [
126
+ * {
127
+ * source: "/:path*",
128
+ * has: [
129
+ * {
130
+ * type: "header",
131
+ * key: "Accept",
132
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
133
+ * },
134
+ * ],
135
+ * },
136
+ * {
137
+ * source: "/:path*",
138
+ * has: [
139
+ * {
140
+ * type: "header",
141
+ * key: "content-type",
142
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
143
+ * },
144
+ * ],
145
+ * },
146
+ * { source: "/.well-known/nodeinfo" },
147
+ * { source: "/.well-known/x-nodeinfo2" },
148
+ * ],
121
149
  * };
122
150
  * ```
123
151
  */
package/dist/index.d.ts CHANGED
@@ -38,29 +38,45 @@ type ErrorHandlers = Omit<FederationFetchOptions<unknown>, "contextData">;
38
38
  * // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
39
39
  * export const config = {
40
40
  * runtime: "nodejs",
41
- * matcher: [{
42
- * source: "/:path*",
43
- * has: [
44
- * {
45
- * type: "header",
46
- * key: "Accept",
47
- * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
48
- * },
49
- * ],
50
- * }],
41
+ * matcher: [
42
+ * {
43
+ * source: "/:path*",
44
+ * has: [
45
+ * {
46
+ * type: "header",
47
+ * key: "Accept",
48
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
49
+ * },
50
+ * ],
51
+ * },
52
+ * {
53
+ * source: "/:path*",
54
+ * has: [
55
+ * {
56
+ * type: "header",
57
+ * key: "content-type",
58
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
59
+ * },
60
+ * ],
61
+ * },
62
+ * { source: "/.well-known/nodeinfo" },
63
+ * { source: "/.well-known/x-nodeinfo2" },
64
+ * ],
51
65
  * };
52
66
  * ```
53
67
  */
54
68
  declare const fedifyWith: <TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>) => (middleware?: (request: Request) => unknown) => (request: Request) => unknown;
69
+ declare const isFederationRequest: (request: Request) => boolean;
55
70
  /**
56
- * Check if the request has the "Accept" header matching the federation
71
+ * Check if the request has the header matching the federation
57
72
  * accept regex.
58
- *
73
+ * @param key The header key to check.
59
74
  * @param request The request to check.
60
- * @returns `true` if the request has the "Accept" header matching
75
+ * @returns `true` if the request has the header matching
61
76
  * the federation accept regex, `false` otherwise.
62
77
  */
63
- declare const hasFederationAcceptHeader: (request: Request) => boolean;
78
+ declare const hasFederationHeader: (key: string) => (request: Request) => boolean;
79
+ declare const isNodeInfoRequest: (request: Request) => boolean;
64
80
  /**
65
81
  * Create a Next.js handler to integrate with the {@link Federation} object.
66
82
  *
@@ -75,4 +91,4 @@ declare const hasFederationAcceptHeader: (request: Request) => boolean;
75
91
  */
76
92
  declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>): (request: Request) => Promise<Response>;
77
93
  //#endregion
78
- export { fedifyWith, hasFederationAcceptHeader, integrateFederation };
94
+ export { fedifyWith, hasFederationHeader, integrateFederation, isFederationRequest, isNodeInfoRequest };
package/dist/index.js CHANGED
@@ -33,35 +33,59 @@ import { NextResponse } from "next/server";
33
33
  * // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
34
34
  * export const config = {
35
35
  * runtime: "nodejs",
36
- * matcher: [{
37
- * source: "/:path*",
38
- * has: [
39
- * {
40
- * type: "header",
41
- * key: "Accept",
42
- * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
43
- * },
44
- * ],
45
- * }],
36
+ * matcher: [
37
+ * {
38
+ * source: "/:path*",
39
+ * has: [
40
+ * {
41
+ * type: "header",
42
+ * key: "Accept",
43
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
44
+ * },
45
+ * ],
46
+ * },
47
+ * {
48
+ * source: "/:path*",
49
+ * has: [
50
+ * {
51
+ * type: "header",
52
+ * key: "content-type",
53
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
54
+ * },
55
+ * ],
56
+ * },
57
+ * { source: "/.well-known/nodeinfo" },
58
+ * { source: "/.well-known/x-nodeinfo2" },
59
+ * ],
46
60
  * };
47
61
  * ```
48
62
  */
49
63
  const fedifyWith = (federation, contextDataFactory, errorHandlers) => (middleware = (_) => NextResponse.next()) => async (request) => {
50
- if (hasFederationAcceptHeader(request)) return await integrateFederation(federation, contextDataFactory, errorHandlers)(request);
64
+ if (isFederationRequest(request)) return await integrateFederation(federation, contextDataFactory, errorHandlers)(request);
51
65
  return await middleware(request);
52
66
  };
67
+ const isFederationRequest = (request) => [
68
+ hasFederationHeader("accept"),
69
+ hasFederationHeader("content-type"),
70
+ isNodeInfoRequest
71
+ ].some((f) => f(request));
53
72
  /**
54
- * Check if the request has the "Accept" header matching the federation
73
+ * Check if the request has the header matching the federation
55
74
  * accept regex.
56
- *
75
+ * @param key The header key to check.
57
76
  * @param request The request to check.
58
- * @returns `true` if the request has the "Accept" header matching
77
+ * @returns `true` if the request has the header matching
59
78
  * the federation accept regex, `false` otherwise.
60
79
  */
61
- const hasFederationAcceptHeader = (request) => {
62
- const acceptHeader = request.headers.get("Accept");
63
- return acceptHeader ? FEDERATION_ACCEPT_REGEX.test(acceptHeader) : false;
80
+ const hasFederationHeader = (key) => (request) => {
81
+ const value = request.headers.get(key);
82
+ return value ? FEDERATION_ACCEPT_REGEX.test(value) : false;
83
+ };
84
+ const isNodeInfoRequest = (request) => {
85
+ const url = new URL(request.url);
86
+ return NODEINFO_PATHS.some((path) => url.pathname.startsWith(path));
64
87
  };
88
+ const NODEINFO_PATHS = ["/.well-known/nodeinfo", "/.well-known/x-nodeinfo2"];
65
89
  const FEDERATION_ACCEPT_REGEX = /.*application\/((jrd|activity|ld)\+json|xrd\+xml).*/;
66
90
  /**
67
91
  * Create a Next.js handler to integrate with the {@link Federation} object.
@@ -93,4 +117,4 @@ const onNotAcceptable = () => new Response("Not acceptable", {
93
117
  });
94
118
 
95
119
  //#endregion
96
- export { fedifyWith, hasFederationAcceptHeader, integrateFederation };
120
+ export { fedifyWith, hasFederationHeader, integrateFederation, isFederationRequest, isNodeInfoRequest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/next",
3
- "version": "1.9.0-dev.1404+88792aac",
3
+ "version": "1.9.0-dev.1408+7c78ce1a",
4
4
  "description": "Integrate Fedify with Next.js",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -53,7 +53,7 @@
53
53
  ],
54
54
  "peerDependencies": {
55
55
  "next": "^15.4.6",
56
- "@fedify/fedify": "1.9.0-dev.1404+88792aac"
56
+ "@fedify/fedify": "1.9.0-dev.1408+7c78ce1a"
57
57
  },
58
58
  "devDependencies": {
59
59
  "tsdown": "^0.12.9",