@fedify/fedify 1.3.0-dev.570 → 1.3.0-dev.571

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/esm/deno.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export default {
2
2
  "name": "@fedify/fedify",
3
- "version": "1.3.0-dev.570+61d94afc",
3
+ "version": "1.3.0-dev.571+be4ca8c6",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./mod.ts",
@@ -7,7 +7,6 @@ import { verifyRequest } from "../sig/http.js";
7
7
  import { detachSignature, verifyJsonLd } from "../sig/ld.js";
8
8
  import { doesActorOwnKey } from "../sig/owner.js";
9
9
  import { verifyObject } from "../sig/proof.js";
10
- import { getTypeId } from "../vocab/type.js";
11
10
  import { Activity, CryptographicKey, Link, Multikey, Object, OrderedCollection, OrderedCollectionPage, } from "../vocab/vocab.js";
12
11
  export function acceptsJsonLd(request) {
13
12
  const types = accepts(request);
@@ -71,28 +70,12 @@ export async function handleObject(request, { values, context, objectDispatcher,
71
70
  },
72
71
  });
73
72
  }
74
- export function handleCollection(request, params) {
75
- const name = params.name.trim().replace(/\s+/g, "_");
76
- const tracerProvider = params.tracerProvider ?? trace.getTracerProvider();
73
+ export async function handleCollection(request, { name, identifier, uriGetter, filter, filterPredicate, context, collectionCallbacks, tracerProvider, onUnauthorized, onNotFound, onNotAcceptable, }) {
74
+ const spanName = name.trim().replace(/\s+/g, "_");
75
+ tracerProvider = tracerProvider ?? trace.getTracerProvider();
77
76
  const tracer = tracerProvider.getTracer(metadata.name, metadata.version);
78
77
  const url = new URL(request.url);
79
78
  const cursor = url.searchParams.get("cursor");
80
- return tracer.startActiveSpan(cursor == null
81
- ? `activitypub.dispatch_collection ${name}`
82
- : `activitypub.dispatch_collection_page ${name}`, { kind: SpanKind.SERVER }, async (span) => {
83
- try {
84
- return await handleCollectionInternal(request, cursor, params, span);
85
- }
86
- catch (e) {
87
- span.setStatus({ code: SpanStatusCode.ERROR, message: String(e) });
88
- throw e;
89
- }
90
- finally {
91
- span.end();
92
- }
93
- });
94
- }
95
- async function handleCollectionInternal(request, cursor, { name, identifier, uriGetter, filter, filterPredicate, context, collectionCallbacks, onUnauthorized, onNotFound, onNotAcceptable, }, span) {
96
79
  if (collectionCallbacks == null)
97
80
  return await onNotFound(request);
98
81
  let collection;
@@ -100,21 +83,41 @@ async function handleCollectionInternal(request, cursor, { name, identifier, uri
100
83
  if (cursor == null) {
101
84
  const firstCursor = await collectionCallbacks.firstCursor?.(context, identifier);
102
85
  const totalItems = await collectionCallbacks.counter?.(context, identifier);
103
- if (totalItems != null) {
104
- span.setAttribute("activitypub.collection.total_items", Number(totalItems));
105
- }
106
86
  if (firstCursor == null) {
107
- const page = await collectionCallbacks.dispatcher(context, identifier, null, filter);
108
- if (page == null) {
109
- span.setStatus({ code: SpanStatusCode.ERROR });
110
- return await onNotFound(request);
111
- }
112
- const { items } = page;
113
- span.setAttribute("fedify.collection.items", items.length);
87
+ const itemsOrResponse = await tracer.startActiveSpan(`activitypub.dispatch_collection ${spanName}`, {
88
+ kind: SpanKind.SERVER,
89
+ attributes: {
90
+ "activitypub.collection.id": baseUri.href,
91
+ "activitypub.collection.type": OrderedCollection.typeId.href,
92
+ },
93
+ }, async (span) => {
94
+ if (totalItems != null) {
95
+ span.setAttribute("activitypub.collection.total_items", Number(totalItems));
96
+ }
97
+ try {
98
+ const page = await collectionCallbacks.dispatcher(context, identifier, null, filter);
99
+ if (page == null) {
100
+ span.setStatus({ code: SpanStatusCode.ERROR });
101
+ return await onNotFound(request);
102
+ }
103
+ const { items } = page;
104
+ span.setAttribute("fedify.collection.items", items.length);
105
+ return items;
106
+ }
107
+ catch (e) {
108
+ span.setStatus({ code: SpanStatusCode.ERROR, message: String(e) });
109
+ throw e;
110
+ }
111
+ finally {
112
+ span.end();
113
+ }
114
+ });
115
+ if (itemsOrResponse instanceof Response)
116
+ return itemsOrResponse;
114
117
  collection = new OrderedCollection({
115
118
  id: baseUri,
116
119
  totalItems: totalItems == null ? null : Number(totalItems),
117
- items: filterCollectionItems(items, name, filterPredicate),
120
+ items: filterCollectionItems(itemsOrResponse, name, filterPredicate),
118
121
  });
119
122
  }
120
123
  else {
@@ -135,16 +138,36 @@ async function handleCollectionInternal(request, cursor, { name, identifier, uri
135
138
  }
136
139
  }
137
140
  else {
138
- span.setAttribute("fedify.collection.cursor", cursor);
139
141
  const uri = new URL(baseUri);
140
142
  uri.searchParams.set("cursor", cursor);
141
- const page = await collectionCallbacks.dispatcher(context, identifier, cursor, filter);
142
- if (page == null) {
143
- span.setStatus({ code: SpanStatusCode.ERROR });
144
- return await onNotFound(request);
145
- }
146
- const { items, prevCursor, nextCursor } = page;
147
- span.setAttribute("fedify.collection.items", items.length);
143
+ const pageOrResponse = await tracer.startActiveSpan(`activitypub.dispatch_collection_page ${name}`, {
144
+ kind: SpanKind.SERVER,
145
+ attributes: {
146
+ "activitypub.collection.id": uri.href,
147
+ "activitypub.collection.type": OrderedCollectionPage.typeId.href,
148
+ "fedify.collection.cursor": cursor,
149
+ },
150
+ }, async (span) => {
151
+ try {
152
+ const page = await collectionCallbacks.dispatcher(context, identifier, cursor, filter);
153
+ if (page == null) {
154
+ span.setStatus({ code: SpanStatusCode.ERROR });
155
+ return await onNotFound(request);
156
+ }
157
+ span.setAttribute("fedify.collection.items", page.items.length);
158
+ return page;
159
+ }
160
+ catch (e) {
161
+ span.setStatus({ code: SpanStatusCode.ERROR, message: String(e) });
162
+ throw e;
163
+ }
164
+ finally {
165
+ span.end();
166
+ }
167
+ });
168
+ if (pageOrResponse instanceof Response)
169
+ return pageOrResponse;
170
+ const { items, prevCursor, nextCursor } = pageOrResponse;
148
171
  let prev = null;
149
172
  if (prevCursor != null) {
150
173
  prev = new URL(context.url);
@@ -174,10 +197,6 @@ async function handleCollectionInternal(request, cursor, { name, identifier, uri
174
197
  return await onUnauthorized(request);
175
198
  }
176
199
  }
177
- if (collection.id != null) {
178
- span.setAttribute("activitypub.collection.id", collection.id.href);
179
- }
180
- span.setAttribute("activitypub.collection.type", getTypeId(collection).href);
181
200
  const jsonLd = await collection.toJsonLd(context);
182
201
  return new Response(JSON.stringify(jsonLd), {
183
202
  headers: {