@fedify/vocab-runtime 2.4.0-dev.2010 → 2.4.0-dev.2027
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/deno.json +2 -1
- package/dist/contexts-2hEbs3Qk.cjs +4513 -0
- package/dist/contexts-DoZUIW7y.js +4508 -0
- package/dist/internal/signed-representation.cjs +378 -0
- package/dist/internal/signed-representation.d.cts +144 -0
- package/dist/internal/signed-representation.d.ts +144 -0
- package/dist/internal/signed-representation.js +371 -0
- package/dist/mod.cjs +5 -4510
- package/dist/mod.js +2 -4507
- package/dist/tests/{body-CI3XB0P0.mjs → body-C8W6PG-f.mjs} +1 -1
- package/dist/tests/{body-CfLJw2_P.cjs → body-CjqMU6be.cjs} +1 -1
- package/dist/tests/body.test.cjs +1 -1
- package/dist/tests/body.test.mjs +1 -1
- package/dist/tests/{docloader-D9VClviZ.cjs → contexts-BBX_qhBL.mjs} +1 -243
- package/dist/tests/{docloader-BhIci12h.mjs → contexts-Ch3v2Cgu.cjs} +10 -218
- package/dist/tests/decimal.test.cjs +1 -1
- package/dist/tests/decimal.test.mjs +1 -1
- package/dist/tests/docloader-BcthkJvx.mjs +221 -0
- package/dist/tests/docloader-DDwe-rFE.cjs +232 -0
- package/dist/tests/docloader.test.cjs +6 -5
- package/dist/tests/docloader.test.mjs +3 -2
- package/dist/tests/{request-Dq_yJVr_.mjs → request-CeEwiwiU.mjs} +1 -1
- package/dist/tests/{request-zC7c6dLm.cjs → request-DV2_yz3V.cjs} +1 -1
- package/dist/tests/request.test.cjs +1 -1
- package/dist/tests/request.test.mjs +1 -1
- package/dist/tests/signed-representation.test.cjs +588 -0
- package/dist/tests/signed-representation.test.d.cts +1 -0
- package/dist/tests/signed-representation.test.d.mts +1 -0
- package/dist/tests/signed-representation.test.mjs +589 -0
- package/package.json +11 -1
- package/src/internal/signed-representation.ts +565 -0
- package/src/signed-representation.test.ts +338 -0
- package/tsdown.config.ts +1 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { a as name, i as logRequest, n as createActivityPubRequest, o as version, t as FetchError } from "./request-CeEwiwiU.mjs";
|
|
2
|
+
import { n as MAX_BODY_SIZE, r as readBoundedText, t as BodyTooLargeError } from "./body-C8W6PG-f.mjs";
|
|
3
|
+
import { t as preloadedContexts } from "./contexts-BBX_qhBL.mjs";
|
|
4
|
+
import { t as HttpHeaderLink } from "./link-Cevmc87v.mjs";
|
|
5
|
+
import { g as validatePublicUrl, t as UrlError } from "./url-Basm1L_c.mjs";
|
|
6
|
+
import { getLogger } from "@logtape/logtape";
|
|
7
|
+
import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
|
|
8
|
+
//#region src/docloader.ts
|
|
9
|
+
const logger = getLogger([
|
|
10
|
+
"fedify",
|
|
11
|
+
"runtime",
|
|
12
|
+
"docloader"
|
|
13
|
+
]);
|
|
14
|
+
const DEFAULT_MAX_REDIRECTION = 20;
|
|
15
|
+
const MAX_HTML_SIZE = 1024 * 1024;
|
|
16
|
+
function createResponseMetadata(response) {
|
|
17
|
+
return new Response(null, {
|
|
18
|
+
headers: response.headers,
|
|
19
|
+
status: response.status,
|
|
20
|
+
statusText: response.statusText
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets a {@link RemoteDocument} from the given response.
|
|
25
|
+
* @param url The URL of the document to load.
|
|
26
|
+
* @param response The response to get the document from.
|
|
27
|
+
* @param fetch The function to fetch the document.
|
|
28
|
+
* @returns The loaded remote document.
|
|
29
|
+
* @throws {FetchError} If the response is not OK.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
async function getRemoteDocument(url, response, fetch) {
|
|
33
|
+
const documentUrl = response.url === "" ? url : response.url;
|
|
34
|
+
const docUrl = new URL(documentUrl);
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
logger.error("Failed to fetch document: {status} {url} {headers}", {
|
|
37
|
+
status: response.status,
|
|
38
|
+
url: documentUrl,
|
|
39
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
40
|
+
});
|
|
41
|
+
throw new FetchError(documentUrl, `HTTP ${response.status}: ${documentUrl}`, response.clone());
|
|
42
|
+
}
|
|
43
|
+
const contentType = response.headers.get("Content-Type");
|
|
44
|
+
const jsonLd = contentType == null || contentType === "application/activity+json" || contentType.startsWith("application/activity+json;") || contentType === "application/ld+json" || contentType.startsWith("application/ld+json;");
|
|
45
|
+
const linkHeader = response.headers.get("Link");
|
|
46
|
+
let contextUrl = null;
|
|
47
|
+
if (linkHeader != null) {
|
|
48
|
+
let link;
|
|
49
|
+
try {
|
|
50
|
+
link = new HttpHeaderLink(linkHeader);
|
|
51
|
+
} catch (e) {
|
|
52
|
+
if (e instanceof SyntaxError) link = new HttpHeaderLink();
|
|
53
|
+
else throw e;
|
|
54
|
+
}
|
|
55
|
+
if (jsonLd) {
|
|
56
|
+
const entries = link.getByRel("http://www.w3.org/ns/json-ld#context");
|
|
57
|
+
for (const [uri, params] of entries) if ("type" in params && params.type === "application/ld+json") {
|
|
58
|
+
contextUrl = uri;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
const entries = link.getByRel("alternate");
|
|
63
|
+
for (const [uri, params] of entries) {
|
|
64
|
+
const altUri = new URL(uri, docUrl);
|
|
65
|
+
if ("type" in params && (params.type === "application/activity+json" || params.type === "application/ld+json" || params.type.startsWith("application/ld+json;")) && altUri.href !== docUrl.href) {
|
|
66
|
+
logger.debug("Found alternate document: {alternateUrl} from {url}", {
|
|
67
|
+
alternateUrl: altUri.href,
|
|
68
|
+
url: documentUrl
|
|
69
|
+
});
|
|
70
|
+
return await fetch(altUri.href);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
let document;
|
|
76
|
+
if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
|
|
77
|
+
const errorResponse = createResponseMetadata(response);
|
|
78
|
+
let html;
|
|
79
|
+
try {
|
|
80
|
+
html = await readBoundedText(response, MAX_HTML_SIZE, documentUrl);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (!(error instanceof BodyTooLargeError)) throw error;
|
|
83
|
+
logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
|
|
84
|
+
url: documentUrl,
|
|
85
|
+
maxBytes: MAX_HTML_SIZE
|
|
86
|
+
});
|
|
87
|
+
throw new FetchError(documentUrl, `HTML document is too large to scan for an ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
|
|
88
|
+
}
|
|
89
|
+
const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
|
|
90
|
+
const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
|
|
91
|
+
let tagMatch;
|
|
92
|
+
while ((tagMatch = tagPattern.exec(html)) !== null) {
|
|
93
|
+
const tagContent = tagMatch[2];
|
|
94
|
+
let attrMatch;
|
|
95
|
+
const attribs = {};
|
|
96
|
+
attrPattern.lastIndex = 0;
|
|
97
|
+
while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
|
|
98
|
+
const key = attrMatch[1].toLowerCase();
|
|
99
|
+
attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
|
|
100
|
+
}
|
|
101
|
+
if (attribs.rel === "alternate" && "type" in attribs && (attribs.type === "application/activity+json" || attribs.type === "application/ld+json" || attribs.type.startsWith("application/ld+json;")) && "href" in attribs && new URL(attribs.href, docUrl).href !== docUrl.href) {
|
|
102
|
+
logger.debug("Found alternate document: {alternateUrl} from {url}", {
|
|
103
|
+
alternateUrl: attribs.href,
|
|
104
|
+
url: documentUrl
|
|
105
|
+
});
|
|
106
|
+
return await fetch(new URL(attribs.href, docUrl).href);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
document = JSON.parse(html);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
113
|
+
throw new FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
|
|
114
|
+
}
|
|
115
|
+
} else document = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, documentUrl));
|
|
116
|
+
logger.debug("Fetched document: {status} {url} {headers}", {
|
|
117
|
+
status: response.status,
|
|
118
|
+
url: documentUrl,
|
|
119
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
contextUrl,
|
|
123
|
+
document,
|
|
124
|
+
documentUrl
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
|
|
129
|
+
*
|
|
130
|
+
* The created loader preloads the below frequently used contexts by default
|
|
131
|
+
* (unless `options.skipPreloadedContexts` is set to `true`):
|
|
132
|
+
*
|
|
133
|
+
* - <https://www.w3.org/ns/activitystreams>
|
|
134
|
+
* - <https://w3id.org/security/v1>
|
|
135
|
+
* - <https://w3id.org/security/data-integrity/v1>
|
|
136
|
+
* - <https://w3id.org/security/data-integrity/v2>
|
|
137
|
+
* - <https://www.w3.org/ns/did/v1>
|
|
138
|
+
* - <https://www.w3.org/ns/cid/v1>
|
|
139
|
+
* - <https://w3id.org/security/multikey/v1>
|
|
140
|
+
* - <https://w3id.org/fep/ef61>
|
|
141
|
+
* - <https://purl.archive.org/socialweb/webfinger>
|
|
142
|
+
* - <http://schema.org/>
|
|
143
|
+
* @param options Options for the document loader.
|
|
144
|
+
* @returns The document loader.
|
|
145
|
+
* @since 1.3.0
|
|
146
|
+
*/
|
|
147
|
+
function getDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedContexts, userAgent } = {}) {
|
|
148
|
+
const tracer = trace.getTracerProvider().getTracer(name, version);
|
|
149
|
+
const maximumRedirection = maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
150
|
+
async function load(url, options, redirected = 0, visited = /* @__PURE__ */ new Set()) {
|
|
151
|
+
options?.signal?.throwIfAborted();
|
|
152
|
+
const currentUrl = new URL(url).href;
|
|
153
|
+
if (!skipPreloadedContexts && currentUrl in preloadedContexts) {
|
|
154
|
+
logger.debug("Using preloaded context: {url}.", { url: currentUrl });
|
|
155
|
+
return {
|
|
156
|
+
contextUrl: null,
|
|
157
|
+
document: preloadedContexts[currentUrl],
|
|
158
|
+
documentUrl: currentUrl
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (!allowPrivateAddress) try {
|
|
162
|
+
await validatePublicUrl(currentUrl);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (error instanceof UrlError) logger.error("Disallowed private URL: {url}", {
|
|
165
|
+
url: currentUrl,
|
|
166
|
+
error
|
|
167
|
+
});
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
visited.add(currentUrl);
|
|
171
|
+
return await tracer.startActiveSpan("activitypub.fetch_document", {
|
|
172
|
+
kind: SpanKind.CLIENT,
|
|
173
|
+
attributes: { "url.full": currentUrl }
|
|
174
|
+
}, async (span) => {
|
|
175
|
+
try {
|
|
176
|
+
const request = createActivityPubRequest(currentUrl, { userAgent });
|
|
177
|
+
logRequest(logger, request);
|
|
178
|
+
const response = await fetch(request, {
|
|
179
|
+
redirect: "manual",
|
|
180
|
+
signal: options?.signal
|
|
181
|
+
});
|
|
182
|
+
span.setAttribute("http.response.status_code", response.status);
|
|
183
|
+
if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
|
|
184
|
+
if (redirected >= maximumRedirection) {
|
|
185
|
+
logger.error("Too many redirections ({redirections}) while fetching document.", {
|
|
186
|
+
redirections: redirected + 1,
|
|
187
|
+
url: currentUrl
|
|
188
|
+
});
|
|
189
|
+
throw new FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
|
|
190
|
+
}
|
|
191
|
+
const redirectUrl = new URL(response.headers.get("Location"), response.url === "" ? currentUrl : response.url).href;
|
|
192
|
+
span.setAttribute("http.redirect.url", redirectUrl);
|
|
193
|
+
if (visited.has(redirectUrl)) {
|
|
194
|
+
logger.error("Detected a redirect loop while fetching document: {url} -> {redirectUrl}", {
|
|
195
|
+
url: currentUrl,
|
|
196
|
+
redirectUrl
|
|
197
|
+
});
|
|
198
|
+
throw new FetchError(currentUrl, `Redirect loop detected: ${redirectUrl}`);
|
|
199
|
+
}
|
|
200
|
+
return await load(redirectUrl, options, redirected + 1, visited);
|
|
201
|
+
}
|
|
202
|
+
const result = await getRemoteDocument(currentUrl, response, load);
|
|
203
|
+
span.setAttribute("docloader.document_url", result.documentUrl);
|
|
204
|
+
if (result.contextUrl != null) span.setAttribute("docloader.context_url", result.contextUrl);
|
|
205
|
+
return result;
|
|
206
|
+
} catch (error) {
|
|
207
|
+
span.recordException(error);
|
|
208
|
+
span.setStatus({
|
|
209
|
+
code: SpanStatusCode.ERROR,
|
|
210
|
+
message: String(error)
|
|
211
|
+
});
|
|
212
|
+
throw error;
|
|
213
|
+
} finally {
|
|
214
|
+
span.end();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return load;
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
export { getRemoteDocument as n, getDocumentLoader as t };
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
const require_request = require("./request-DV2_yz3V.cjs");
|
|
2
|
+
const require_body = require("./body-CjqMU6be.cjs");
|
|
3
|
+
const require_contexts = require("./contexts-Ch3v2Cgu.cjs");
|
|
4
|
+
const require_link = require("./link-DlKm8bEr.cjs");
|
|
5
|
+
const require_url = require("./url-CPfMatUL.cjs");
|
|
6
|
+
let _logtape_logtape = require("@logtape/logtape");
|
|
7
|
+
let _opentelemetry_api = require("@opentelemetry/api");
|
|
8
|
+
//#region src/docloader.ts
|
|
9
|
+
const logger = (0, _logtape_logtape.getLogger)([
|
|
10
|
+
"fedify",
|
|
11
|
+
"runtime",
|
|
12
|
+
"docloader"
|
|
13
|
+
]);
|
|
14
|
+
const DEFAULT_MAX_REDIRECTION = 20;
|
|
15
|
+
const MAX_HTML_SIZE = 1024 * 1024;
|
|
16
|
+
function createResponseMetadata(response) {
|
|
17
|
+
return new Response(null, {
|
|
18
|
+
headers: response.headers,
|
|
19
|
+
status: response.status,
|
|
20
|
+
statusText: response.statusText
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets a {@link RemoteDocument} from the given response.
|
|
25
|
+
* @param url The URL of the document to load.
|
|
26
|
+
* @param response The response to get the document from.
|
|
27
|
+
* @param fetch The function to fetch the document.
|
|
28
|
+
* @returns The loaded remote document.
|
|
29
|
+
* @throws {FetchError} If the response is not OK.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
async function getRemoteDocument(url, response, fetch) {
|
|
33
|
+
const documentUrl = response.url === "" ? url : response.url;
|
|
34
|
+
const docUrl = new URL(documentUrl);
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
logger.error("Failed to fetch document: {status} {url} {headers}", {
|
|
37
|
+
status: response.status,
|
|
38
|
+
url: documentUrl,
|
|
39
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
40
|
+
});
|
|
41
|
+
throw new require_request.FetchError(documentUrl, `HTTP ${response.status}: ${documentUrl}`, response.clone());
|
|
42
|
+
}
|
|
43
|
+
const contentType = response.headers.get("Content-Type");
|
|
44
|
+
const jsonLd = contentType == null || contentType === "application/activity+json" || contentType.startsWith("application/activity+json;") || contentType === "application/ld+json" || contentType.startsWith("application/ld+json;");
|
|
45
|
+
const linkHeader = response.headers.get("Link");
|
|
46
|
+
let contextUrl = null;
|
|
47
|
+
if (linkHeader != null) {
|
|
48
|
+
let link;
|
|
49
|
+
try {
|
|
50
|
+
link = new require_link.HttpHeaderLink(linkHeader);
|
|
51
|
+
} catch (e) {
|
|
52
|
+
if (e instanceof SyntaxError) link = new require_link.HttpHeaderLink();
|
|
53
|
+
else throw e;
|
|
54
|
+
}
|
|
55
|
+
if (jsonLd) {
|
|
56
|
+
const entries = link.getByRel("http://www.w3.org/ns/json-ld#context");
|
|
57
|
+
for (const [uri, params] of entries) if ("type" in params && params.type === "application/ld+json") {
|
|
58
|
+
contextUrl = uri;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
const entries = link.getByRel("alternate");
|
|
63
|
+
for (const [uri, params] of entries) {
|
|
64
|
+
const altUri = new URL(uri, docUrl);
|
|
65
|
+
if ("type" in params && (params.type === "application/activity+json" || params.type === "application/ld+json" || params.type.startsWith("application/ld+json;")) && altUri.href !== docUrl.href) {
|
|
66
|
+
logger.debug("Found alternate document: {alternateUrl} from {url}", {
|
|
67
|
+
alternateUrl: altUri.href,
|
|
68
|
+
url: documentUrl
|
|
69
|
+
});
|
|
70
|
+
return await fetch(altUri.href);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
let document;
|
|
76
|
+
if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
|
|
77
|
+
const errorResponse = createResponseMetadata(response);
|
|
78
|
+
let html;
|
|
79
|
+
try {
|
|
80
|
+
html = await require_body.readBoundedText(response, MAX_HTML_SIZE, documentUrl);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (!(error instanceof require_body.BodyTooLargeError)) throw error;
|
|
83
|
+
logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
|
|
84
|
+
url: documentUrl,
|
|
85
|
+
maxBytes: MAX_HTML_SIZE
|
|
86
|
+
});
|
|
87
|
+
throw new require_request.FetchError(documentUrl, `HTML document is too large to scan for an ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
|
|
88
|
+
}
|
|
89
|
+
const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
|
|
90
|
+
const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
|
|
91
|
+
let tagMatch;
|
|
92
|
+
while ((tagMatch = tagPattern.exec(html)) !== null) {
|
|
93
|
+
const tagContent = tagMatch[2];
|
|
94
|
+
let attrMatch;
|
|
95
|
+
const attribs = {};
|
|
96
|
+
attrPattern.lastIndex = 0;
|
|
97
|
+
while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
|
|
98
|
+
const key = attrMatch[1].toLowerCase();
|
|
99
|
+
attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
|
|
100
|
+
}
|
|
101
|
+
if (attribs.rel === "alternate" && "type" in attribs && (attribs.type === "application/activity+json" || attribs.type === "application/ld+json" || attribs.type.startsWith("application/ld+json;")) && "href" in attribs && new URL(attribs.href, docUrl).href !== docUrl.href) {
|
|
102
|
+
logger.debug("Found alternate document: {alternateUrl} from {url}", {
|
|
103
|
+
alternateUrl: attribs.href,
|
|
104
|
+
url: documentUrl
|
|
105
|
+
});
|
|
106
|
+
return await fetch(new URL(attribs.href, docUrl).href);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
document = JSON.parse(html);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
113
|
+
throw new require_request.FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
|
|
114
|
+
}
|
|
115
|
+
} else document = JSON.parse(await require_body.readBoundedText(response, require_body.MAX_BODY_SIZE, documentUrl));
|
|
116
|
+
logger.debug("Fetched document: {status} {url} {headers}", {
|
|
117
|
+
status: response.status,
|
|
118
|
+
url: documentUrl,
|
|
119
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
contextUrl,
|
|
123
|
+
document,
|
|
124
|
+
documentUrl
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
|
|
129
|
+
*
|
|
130
|
+
* The created loader preloads the below frequently used contexts by default
|
|
131
|
+
* (unless `options.skipPreloadedContexts` is set to `true`):
|
|
132
|
+
*
|
|
133
|
+
* - <https://www.w3.org/ns/activitystreams>
|
|
134
|
+
* - <https://w3id.org/security/v1>
|
|
135
|
+
* - <https://w3id.org/security/data-integrity/v1>
|
|
136
|
+
* - <https://w3id.org/security/data-integrity/v2>
|
|
137
|
+
* - <https://www.w3.org/ns/did/v1>
|
|
138
|
+
* - <https://www.w3.org/ns/cid/v1>
|
|
139
|
+
* - <https://w3id.org/security/multikey/v1>
|
|
140
|
+
* - <https://w3id.org/fep/ef61>
|
|
141
|
+
* - <https://purl.archive.org/socialweb/webfinger>
|
|
142
|
+
* - <http://schema.org/>
|
|
143
|
+
* @param options Options for the document loader.
|
|
144
|
+
* @returns The document loader.
|
|
145
|
+
* @since 1.3.0
|
|
146
|
+
*/
|
|
147
|
+
function getDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedContexts, userAgent } = {}) {
|
|
148
|
+
const tracer = _opentelemetry_api.trace.getTracerProvider().getTracer(require_request.name, require_request.version);
|
|
149
|
+
const maximumRedirection = maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
150
|
+
async function load(url, options, redirected = 0, visited = /* @__PURE__ */ new Set()) {
|
|
151
|
+
options?.signal?.throwIfAborted();
|
|
152
|
+
const currentUrl = new URL(url).href;
|
|
153
|
+
if (!skipPreloadedContexts && currentUrl in require_contexts.preloadedContexts) {
|
|
154
|
+
logger.debug("Using preloaded context: {url}.", { url: currentUrl });
|
|
155
|
+
return {
|
|
156
|
+
contextUrl: null,
|
|
157
|
+
document: require_contexts.preloadedContexts[currentUrl],
|
|
158
|
+
documentUrl: currentUrl
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (!allowPrivateAddress) try {
|
|
162
|
+
await require_url.validatePublicUrl(currentUrl);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (error instanceof require_url.UrlError) logger.error("Disallowed private URL: {url}", {
|
|
165
|
+
url: currentUrl,
|
|
166
|
+
error
|
|
167
|
+
});
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
visited.add(currentUrl);
|
|
171
|
+
return await tracer.startActiveSpan("activitypub.fetch_document", {
|
|
172
|
+
kind: _opentelemetry_api.SpanKind.CLIENT,
|
|
173
|
+
attributes: { "url.full": currentUrl }
|
|
174
|
+
}, async (span) => {
|
|
175
|
+
try {
|
|
176
|
+
const request = require_request.createActivityPubRequest(currentUrl, { userAgent });
|
|
177
|
+
require_request.logRequest(logger, request);
|
|
178
|
+
const response = await fetch(request, {
|
|
179
|
+
redirect: "manual",
|
|
180
|
+
signal: options?.signal
|
|
181
|
+
});
|
|
182
|
+
span.setAttribute("http.response.status_code", response.status);
|
|
183
|
+
if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
|
|
184
|
+
if (redirected >= maximumRedirection) {
|
|
185
|
+
logger.error("Too many redirections ({redirections}) while fetching document.", {
|
|
186
|
+
redirections: redirected + 1,
|
|
187
|
+
url: currentUrl
|
|
188
|
+
});
|
|
189
|
+
throw new require_request.FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
|
|
190
|
+
}
|
|
191
|
+
const redirectUrl = new URL(response.headers.get("Location"), response.url === "" ? currentUrl : response.url).href;
|
|
192
|
+
span.setAttribute("http.redirect.url", redirectUrl);
|
|
193
|
+
if (visited.has(redirectUrl)) {
|
|
194
|
+
logger.error("Detected a redirect loop while fetching document: {url} -> {redirectUrl}", {
|
|
195
|
+
url: currentUrl,
|
|
196
|
+
redirectUrl
|
|
197
|
+
});
|
|
198
|
+
throw new require_request.FetchError(currentUrl, `Redirect loop detected: ${redirectUrl}`);
|
|
199
|
+
}
|
|
200
|
+
return await load(redirectUrl, options, redirected + 1, visited);
|
|
201
|
+
}
|
|
202
|
+
const result = await getRemoteDocument(currentUrl, response, load);
|
|
203
|
+
span.setAttribute("docloader.document_url", result.documentUrl);
|
|
204
|
+
if (result.contextUrl != null) span.setAttribute("docloader.context_url", result.contextUrl);
|
|
205
|
+
return result;
|
|
206
|
+
} catch (error) {
|
|
207
|
+
span.recordException(error);
|
|
208
|
+
span.setStatus({
|
|
209
|
+
code: _opentelemetry_api.SpanStatusCode.ERROR,
|
|
210
|
+
message: String(error)
|
|
211
|
+
});
|
|
212
|
+
throw error;
|
|
213
|
+
} finally {
|
|
214
|
+
span.end();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return load;
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
Object.defineProperty(exports, "getDocumentLoader", {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
get: function() {
|
|
224
|
+
return getDocumentLoader;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(exports, "getRemoteDocument", {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get: function() {
|
|
230
|
+
return getRemoteDocument;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const require_rolldown_runtime = require("./rolldown-runtime-emK7D4bc.cjs");
|
|
2
|
-
const require_request = require("./request-
|
|
3
|
-
const
|
|
2
|
+
const require_request = require("./request-DV2_yz3V.cjs");
|
|
3
|
+
const require_contexts = require("./contexts-Ch3v2Cgu.cjs");
|
|
4
4
|
const require_url = require("./url-CPfMatUL.cjs");
|
|
5
|
+
const require_docloader = require("./docloader-DDwe-rFE.cjs");
|
|
5
6
|
let node_test = require("node:test");
|
|
6
7
|
let node_zlib = require("node:zlib");
|
|
7
8
|
let node_assert = require("node:assert");
|
|
@@ -1486,7 +1487,7 @@ var esm_default = new class FetchMock {
|
|
|
1486
1487
|
await (0, node_assert.rejects)(() => fetchDocumentLoader("https://example.com/404"), require_request.FetchError, "HTTP 404: https://example.com/404");
|
|
1487
1488
|
});
|
|
1488
1489
|
await t.test("preloaded contexts", async () => {
|
|
1489
|
-
for (const [url, document] of Object.entries(
|
|
1490
|
+
for (const [url, document] of Object.entries(require_contexts.preloadedContexts)) (0, node_assert.deepStrictEqual)(await fetchDocumentLoader(url), {
|
|
1490
1491
|
contextUrl: null,
|
|
1491
1492
|
documentUrl: url,
|
|
1492
1493
|
document
|
|
@@ -1498,12 +1499,12 @@ var esm_default = new class FetchMock {
|
|
|
1498
1499
|
(0, node_assert.deepStrictEqual)(await fetchDocumentLoader(url), {
|
|
1499
1500
|
contextUrl: null,
|
|
1500
1501
|
documentUrl: url,
|
|
1501
|
-
document:
|
|
1502
|
+
document: require_contexts.cid_v1_default
|
|
1502
1503
|
});
|
|
1503
1504
|
});
|
|
1504
1505
|
await t.test("preloaded FEP-ef61 context", async () => {
|
|
1505
1506
|
const url = "https://w3id.org/fep/ef61";
|
|
1506
|
-
(0, node_assert.ok)(url in
|
|
1507
|
+
(0, node_assert.ok)(url in require_contexts.preloadedContexts);
|
|
1507
1508
|
(0, node_assert.deepStrictEqual)(await fetchDocumentLoader(url), {
|
|
1508
1509
|
contextUrl: null,
|
|
1509
1510
|
documentUrl: url,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { t as FetchError } from "./request-
|
|
2
|
-
import {
|
|
1
|
+
import { t as FetchError } from "./request-CeEwiwiU.mjs";
|
|
2
|
+
import { n as cid_v1_default, t as preloadedContexts } from "./contexts-BBX_qhBL.mjs";
|
|
3
3
|
import { t as UrlError } from "./url-Basm1L_c.mjs";
|
|
4
|
+
import { n as getRemoteDocument, t as getDocumentLoader } from "./docloader-BcthkJvx.mjs";
|
|
4
5
|
import { test } from "node:test";
|
|
5
6
|
import { gzipSync } from "node:zlib";
|
|
6
7
|
import { deepStrictEqual, ok, rejects } from "node:assert";
|
|
@@ -3,7 +3,7 @@ let node_process = require("node:process");
|
|
|
3
3
|
node_process = require_rolldown_runtime.__toESM(node_process, 1);
|
|
4
4
|
//#region deno.json
|
|
5
5
|
var name = "@fedify/vocab-runtime";
|
|
6
|
-
var version = "2.4.0-dev.
|
|
6
|
+
var version = "2.4.0-dev.2027+fcf88bfc";
|
|
7
7
|
//#endregion
|
|
8
8
|
//#region src/request.ts
|
|
9
9
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_rolldown_runtime = require("./rolldown-runtime-emK7D4bc.cjs");
|
|
2
|
-
const require_request = require("./request-
|
|
2
|
+
const require_request = require("./request-DV2_yz3V.cjs");
|
|
3
3
|
let node_test = require("node:test");
|
|
4
4
|
let node_process = require("node:process");
|
|
5
5
|
node_process = require_rolldown_runtime.__toESM(node_process, 1);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { o as version, r as getUserAgent } from "./request-
|
|
1
|
+
import { o as version, r as getUserAgent } from "./request-CeEwiwiU.mjs";
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import process from "node:process";
|
|
4
4
|
import { deepStrictEqual } from "node:assert";
|