@oxy.so/federation 2.16.0 → 3.0.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.
Files changed (43) hide show
  1. package/dist/cjs/apUri.js +0 -16
  2. package/dist/cjs/apValue.js +164 -0
  3. package/dist/cjs/index.js +17 -2
  4. package/dist/cjs/networkIdentity.js +2 -3
  5. package/dist/cjs/node/actorResolver.js +81 -110
  6. package/dist/cjs/node/actorRouter.js +2 -1
  7. package/dist/cjs/node/boundedBody.js +44 -0
  8. package/dist/cjs/node/inboundDispatch.js +6 -15
  9. package/dist/cjs/node/index.js +11 -1
  10. package/dist/cjs/node/signedFetch.js +15 -5
  11. package/dist/esm/apUri.js +0 -15
  12. package/dist/esm/apValue.js +155 -0
  13. package/dist/esm/index.js +10 -1
  14. package/dist/esm/networkIdentity.js +2 -3
  15. package/dist/esm/node/actorResolver.js +72 -101
  16. package/dist/esm/node/actorRouter.js +2 -1
  17. package/dist/esm/node/boundedBody.js +42 -0
  18. package/dist/esm/node/inboundDispatch.js +6 -15
  19. package/dist/esm/node/index.js +6 -1
  20. package/dist/esm/node/signedFetch.js +14 -5
  21. package/dist/types/apUri.d.ts +0 -7
  22. package/dist/types/apValue.d.ts +75 -0
  23. package/dist/types/index.d.ts +15 -16
  24. package/dist/types/node/actorResolver.d.ts +53 -11
  25. package/dist/types/node/boundedBody.d.ts +19 -0
  26. package/dist/types/node/inboundDispatch.d.ts +1 -0
  27. package/dist/types/node/index.d.ts +7 -2
  28. package/dist/types/node/signedFetch.d.ts +23 -2
  29. package/package.json +1 -1
  30. package/src/__tests__/actorResolver.test.ts +90 -2
  31. package/src/__tests__/apValue.test.ts +93 -0
  32. package/src/__tests__/networkIdentity.test.ts +4 -2
  33. package/src/__tests__/signedFetch.test.ts +26 -3
  34. package/src/apUri.ts +0 -15
  35. package/src/apValue.ts +153 -0
  36. package/src/index.ts +23 -17
  37. package/src/networkIdentity.ts +2 -2
  38. package/src/node/actorResolver.ts +139 -123
  39. package/src/node/actorRouter.ts +2 -2
  40. package/src/node/boundedBody.ts +62 -0
  41. package/src/node/inboundDispatch.ts +6 -14
  42. package/src/node/index.ts +18 -0
  43. package/src/node/signedFetch.ts +44 -6
@@ -44,10 +44,18 @@ export interface SingleHopFetchInit {
44
44
  */
45
45
  export type SingleHopFetch = (url: string, init: SingleHopFetchInit) => Promise<Response>;
46
46
 
47
+ /** What a {@link SignedFetchLogger} line is about, for a sink that formats its own. */
48
+ export interface SignedFetchLogDetail {
49
+ /** The status that prompted the line. */
50
+ status: number;
51
+ /** The URL the answer came from (after redirects). */
52
+ url: string;
53
+ }
54
+
47
55
  /** Non-fatal diagnostics sink for signed fetches. */
48
56
  export interface SignedFetchLogger {
49
- info(message: string): void;
50
- warn(message: string): void;
57
+ info(message: string, detail?: SignedFetchLogDetail): void;
58
+ warn(message: string, detail?: SignedFetchLogDetail): void;
51
59
  }
52
60
 
53
61
  /** Adapters + config a {@link SignedFetch} is built from. */
@@ -85,6 +93,24 @@ export type SignedFetch = (
85
93
  init?: SignedFetchInit,
86
94
  ) => Promise<Response>;
87
95
 
96
+ /** A {@link SignedFetch} answer with the URL it came from, after redirects. */
97
+ export interface SignedFetchResult {
98
+ response: Response;
99
+ /** The URL that answered: the request URL, or the last redirect hop followed. */
100
+ finalUrl: string;
101
+ }
102
+
103
+ /**
104
+ * A signed ActivityPub GET that also reports the URL that answered — for a
105
+ * caller that binds a document to where it was served from (an actor whose `id`
106
+ * may name the redirect target rather than the URL asked for).
107
+ */
108
+ export type SignedFetcher = (
109
+ url: string,
110
+ accept: string,
111
+ init?: SignedFetchInit,
112
+ ) => Promise<SignedFetchResult>;
113
+
88
114
  function requestInitHeaders(init: RequestInit): Record<string, string> {
89
115
  if (!init.headers) return {};
90
116
  if (init.headers instanceof Headers) {
@@ -121,11 +147,17 @@ function requestInitHeaders(init: RequestInit): Record<string, string> {
121
147
  * (see {@link SignedFetchInit.keyId}); such a request is never retried unsigned.
122
148
  */
123
149
  export function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch {
150
+ const fetcher = createSignedFetcher(config);
151
+ return async (url, accept, init) => (await fetcher(url, accept, init)).response;
152
+ }
153
+
154
+ /** {@link createSignedFetch}, answering with the final URL as well ({@link SignedFetcher}). */
155
+ export function createSignedFetcher(config: CreateSignedFetchConfig): SignedFetcher {
124
156
  return async function signedFetch(
125
157
  url: string,
126
158
  accept: string,
127
159
  init: SignedFetchInit = {},
128
- ): Promise<Response> {
160
+ ): Promise<SignedFetchResult> {
129
161
  const acceptHeader = `${accept}, application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
130
162
  const actorSigned = init.keyId !== undefined;
131
163
  const keyId = init.keyId ?? (await config.getInstanceKeyId());
@@ -164,6 +196,7 @@ export function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch
164
196
  if (hop === SIGNED_FETCH_MAX_REDIRECTS || !location) {
165
197
  return { res, finalUrl: currentUrl };
166
198
  }
199
+ await res.body?.cancel().catch(() => {});
167
200
  currentUrl = new URL(location, currentUrl).toString();
168
201
  }
169
202
  throw new Error('redirect loop exhausted');
@@ -173,7 +206,7 @@ export function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch
173
206
 
174
207
  // An actor-signed answer is the remote's verdict for THAT actor (a follower or
175
208
  // recipient): an unsigned retry asks a different question, so it never runs.
176
- if (actorSigned) return res;
209
+ if (actorSigned) return { response: res, finalUrl };
177
210
 
178
211
  // If the remote server returns a 5xx (e.g. it can't resolve our keyId to
179
212
  // verify the signature), retry without the signature as a fallback for public
@@ -182,8 +215,12 @@ export function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch
182
215
  if (res.status >= 500) {
183
216
  config.logger?.info(
184
217
  `[FedSync] signedFetch got ${res.status} for ${finalUrl}, retrying unsigned`,
218
+ { status: res.status, url: finalUrl },
185
219
  );
186
- return fetchFollowingRedirects(finalUrl, false).then(({ res: unsignedRes }) => unsignedRes);
220
+ // The failed signed answer's body is never read; release its stream.
221
+ await res.body?.cancel().catch(() => {});
222
+ const unsigned = await fetchFollowingRedirects(finalUrl, false);
223
+ return { response: unsigned.res, finalUrl: unsigned.finalUrl };
187
224
  }
188
225
 
189
226
  // A 401/403 on a signed request means the remote rejected OUR signature (e.g.
@@ -195,9 +232,10 @@ export function createSignedFetch(config: CreateSignedFetchConfig): SignedFetch
195
232
  if (res.status === 401 || res.status === 403) {
196
233
  config.logger?.warn(
197
234
  `[FedSync] signedFetch got ${res.status} ${res.statusText} for ${url} — remote rejected our HTTP signature (check instance key pair / service token); returning the failed response so no posts are imported from this source`,
235
+ { status: res.status, url: finalUrl },
198
236
  );
199
237
  }
200
238
 
201
- return res;
239
+ return { response: res, finalUrl };
202
240
  };
203
241
  }