@fedify/vocab-runtime 2.4.0-dev.2115 → 2.4.0-dev.2127

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 (46) hide show
  1. package/deno.json +1 -1
  2. package/dist/{docloader-Dr8wEkif.d.cts → docloader-oRpq1nQ8.d.cts} +8 -1
  3. package/dist/{docloader-Dr8wEkif.d.ts → docloader-oRpq1nQ8.d.ts} +8 -1
  4. package/dist/internal/jsonld-cache.cjs +50 -0
  5. package/dist/internal/jsonld-cache.d.cts +28 -2
  6. package/dist/internal/jsonld-cache.d.ts +28 -2
  7. package/dist/internal/jsonld-cache.js +48 -1
  8. package/dist/internal/portable-dereference.cjs +28 -12
  9. package/dist/internal/portable-dereference.d.cts +12 -4
  10. package/dist/internal/portable-dereference.d.ts +12 -4
  11. package/dist/internal/portable-dereference.js +28 -12
  12. package/dist/mod.cjs +15 -9
  13. package/dist/mod.d.cts +8 -2
  14. package/dist/mod.d.ts +8 -2
  15. package/dist/mod.js +15 -9
  16. package/dist/{portable-comBI05Y.d.cts → portable-Drfl02vZ.d.cts} +1 -1
  17. package/dist/{portable-DH3947GK.d.ts → portable-rwhiyMvY.d.ts} +1 -1
  18. package/dist/tests/{body-Cr12B-9D.cjs → body-BCEAkwBy.cjs} +1 -1
  19. package/dist/tests/{body-CVwHI1SD.mjs → body-BUO7KBud.mjs} +1 -1
  20. package/dist/tests/body.test.cjs +1 -1
  21. package/dist/tests/body.test.mjs +1 -1
  22. package/dist/tests/decimal.test.cjs +1 -1
  23. package/dist/tests/decimal.test.mjs +1 -1
  24. package/dist/tests/{docloader-DWKS3B09.mjs → docloader-BBjIii4r.mjs} +16 -10
  25. package/dist/tests/{docloader-Bknd2EAC.cjs → docloader-DOtl16c7.cjs} +16 -10
  26. package/dist/tests/docloader.test.cjs +49 -2
  27. package/dist/tests/docloader.test.mjs +49 -2
  28. package/dist/tests/internal/portable-dereference.test.cjs +67 -6
  29. package/dist/tests/internal/portable-dereference.test.mjs +67 -6
  30. package/dist/tests/jsonld-cache-9jap4yM0.cjs +373 -0
  31. package/dist/tests/jsonld-cache-DnZS0z1G.mjs +324 -0
  32. package/dist/tests/jsonld-cache.test.cjs +20 -291
  33. package/dist/tests/jsonld-cache.test.mjs +2 -273
  34. package/dist/tests/{request-f7iixEM_.mjs → request-C6bviL-E.mjs} +1 -1
  35. package/dist/tests/{request-BdOpOc_q.cjs → request-DzzSwifZ.cjs} +1 -1
  36. package/dist/tests/request.test.cjs +1 -1
  37. package/dist/tests/request.test.mjs +1 -1
  38. package/package.json +1 -1
  39. package/src/docloader.test.ts +73 -0
  40. package/src/docloader.ts +46 -17
  41. package/src/internal/jsonld-cache.ts +60 -0
  42. package/src/internal/portable-dereference.test.ts +71 -0
  43. package/src/internal/portable-dereference.ts +37 -24
  44. package/src/preprocessor.ts +7 -0
  45. package/dist/tests/jsonld-CmDhHNTx.cjs +0 -12
  46. package/dist/tests/jsonld-DweIJ07b.mjs +0 -5
@@ -1,278 +1,7 @@
1
- import { a as formatIri, c as haveSameFe34Origin, l as haveSameIriOrigin, m as parseIri } from "./url-DgJM9WJn.mjs";
2
- import { t as jsonld_default } from "./jsonld-DweIJ07b.mjs";
1
+ import { m as parseIri } from "./url-DgJM9WJn.mjs";
2
+ import { a as normalizeJsonLdIris, c as jsonld_default, i as isTrustedIriOrigin, r as getJsonLdContext, t as compactJsonLdCache } from "./jsonld-cache-DnZS0z1G.mjs";
3
3
  import { test } from "node:test";
4
4
  import { deepStrictEqual, ok, strictEqual } from "node:assert";
5
- //#region src/internal/jsonld-cache.ts
6
- const noJsonLdContext = Symbol("noJsonLdContext");
7
- /**
8
- * Checks whether an IRI is trusted relative to another IRI.
9
- *
10
- * @internal Technically exported for generated vocabulary classes, but not
11
- * part of the public API contract. This is not considered public API for
12
- * Semantic Versioning decisions.
13
- */
14
- function isTrustedIriOrigin(options, left, right) {
15
- return options.crossOrigin === "trust" || left == null || right != null && (haveSameIriOrigin(left, right) || haveSameFe34Origin(left, right));
16
- }
17
- /**
18
- * Normalizes portable IRIs in JSON-LD cache data.
19
- *
20
- * @internal Technically exported for generated vocabulary classes, but not
21
- * part of the public API contract. This is not considered public API for
22
- * Semantic Versioning decisions.
23
- */
24
- function normalizeJsonLdIris(value, iriKeys, iriPattern = /^ap(?:\+ef61)?:\/\//i) {
25
- return normalize(value, void 0, 0, void 0);
26
- function isIriPosition(key, parentKey) {
27
- return iriKeys.has(key) || (key === "@value" || key === "@list" || key === "@set") && parentKey != null && iriKeys.has(parentKey);
28
- }
29
- function arrayItemParentKey(key, parentKey) {
30
- return key === "@list" || key === "@set" ? parentKey : key;
31
- }
32
- function entryParentKey(entryKey, key, parentKey) {
33
- if (entryKey === "@list" || entryKey === "@set") return parentKey ?? key;
34
- return key === "@list" || key === "@set" ? parentKey : key;
35
- }
36
- function normalize(value, key, depth = 0, parentKey) {
37
- if (depth > 32 || key === "@context") return value;
38
- if (typeof value === "string") {
39
- if (key != null && isIriPosition(key, parentKey) && iriPattern.test(value)) try {
40
- return formatIri(value);
41
- } catch {
42
- return value;
43
- }
44
- return value;
45
- }
46
- if (Array.isArray(value)) {
47
- let clone;
48
- const itemParentKey = arrayItemParentKey(key, parentKey);
49
- for (let i = 0; i < value.length; i++) {
50
- const result = normalize(value[i], key, depth + 1, itemParentKey);
51
- if (result !== value[i]) {
52
- clone ??= value.slice(0, i);
53
- clone.push(result);
54
- } else if (clone != null) clone.push(value[i]);
55
- }
56
- return clone ?? value;
57
- }
58
- if (value == null || typeof value !== "object") return value;
59
- const object = value;
60
- let clone;
61
- for (const entryKey of globalThis.Object.keys(object)) {
62
- const result = normalize(object[entryKey], entryKey, depth + 1, entryParentKey(entryKey, key, parentKey));
63
- if (result !== object[entryKey]) {
64
- clone ??= { ...object };
65
- defineJsonLdProperty(clone, entryKey, result);
66
- }
67
- }
68
- return clone ?? object;
69
- }
70
- }
71
- /**
72
- * Finds the first JSON-LD context in a value.
73
- *
74
- * @internal Technically exported for generated vocabulary classes, but not
75
- * part of the public API contract. This is not considered public API for
76
- * Semantic Versioning decisions.
77
- */
78
- function getJsonLdContext(value, depth = 0) {
79
- if (depth > 32) return void 0;
80
- if (Array.isArray(value)) {
81
- for (const item of value) {
82
- const context = getJsonLdContext(item, depth + 1);
83
- if (context !== void 0) return context;
84
- }
85
- return;
86
- }
87
- if (value == null || typeof value !== "object" || !("@context" in value)) return;
88
- return value["@context"];
89
- }
90
- function getOwnJsonLdContext(value) {
91
- if (value == null || typeof value !== "object" || Array.isArray(value) || !("@context" in value)) return noJsonLdContext;
92
- return value["@context"];
93
- }
94
- /**
95
- * Recompacts normalized JSON-LD cache data against the original context.
96
- *
97
- * @internal Technically exported for generated vocabulary classes, but not
98
- * part of the public API contract. This is not considered public API for
99
- * Semantic Versioning decisions.
100
- */
101
- async function compactJsonLdCache(normalized, original, documentLoader, depth = 0, inheritedContext) {
102
- if (depth > 32) return normalized;
103
- if (Array.isArray(original)) {
104
- const normalizedArray = Array.isArray(normalized) ? normalized : normalized != null && typeof normalized === "object" && "@graph" in normalized && Array.isArray(normalized["@graph"]) ? normalized["@graph"] : void 0;
105
- if (normalizedArray == null) return normalized;
106
- let clone;
107
- for (let i = 0; i < normalizedArray.length; i++) {
108
- const item = await compactJsonLdCache(normalizedArray[i], original[i], documentLoader, depth + 1, inheritedContext);
109
- if (item !== normalizedArray[i]) {
110
- clone ??= normalizedArray.slice(0, i);
111
- clone.push(item);
112
- } else if (clone != null) clone.push(normalizedArray[i]);
113
- }
114
- return clone ?? (Array.isArray(normalized) ? normalized : normalizedArray);
115
- }
116
- const ownContext = getOwnJsonLdContext(original);
117
- const context = ownContext === noJsonLdContext ? inheritedContext : ownContext;
118
- if (context == null) return preserveNoContextJsonLdShape(normalized, original, depth);
119
- return await preserveJsonLdShape(await mergeUnmappedTerms(await jsonld_default.compact(Array.isArray(normalized) && normalized.length === 1 ? normalized[0] : normalized, context, { documentLoader }), original, context, documentLoader), original, context, documentLoader, depth);
120
- }
121
- function preserveNoContextJsonLdShape(normalized, original, depth = 0) {
122
- if (depth > 32) return normalized;
123
- if (original == null || typeof original !== "object" || Array.isArray(original)) return normalized;
124
- const normalizedObject = Array.isArray(normalized) && normalized.length === 1 ? normalized[0] : normalized;
125
- if (normalizedObject == null || typeof normalizedObject !== "object" || Array.isArray(normalizedObject)) return normalized;
126
- const source = normalizedObject;
127
- const originalObject = original;
128
- let clone;
129
- for (const key of globalThis.Object.keys(originalObject)) {
130
- if (!globalThis.Object.prototype.hasOwnProperty.call(source, key)) continue;
131
- const value = preserveNoContextValue(source[key], originalObject[key], depth + 1);
132
- if (value !== originalObject[key]) {
133
- clone ??= { ...originalObject };
134
- defineJsonLdProperty(clone, key, value);
135
- }
136
- }
137
- return clone ?? original;
138
- }
139
- function preserveNoContextValue(normalized, original, depth) {
140
- if (depth > 32) return normalized;
141
- if (Array.isArray(original)) {
142
- const normalizedArray = Array.isArray(normalized) ? normalized : [normalized];
143
- let clone;
144
- for (let i = 0; i < original.length; i++) {
145
- const value = preserveNoContextValue(normalizedArray[i], original[i], depth + 1);
146
- if (value !== original[i]) {
147
- clone ??= original.slice(0, i);
148
- clone.push(value);
149
- } else if (clone != null) clone.push(original[i]);
150
- }
151
- return clone ?? original;
152
- }
153
- const normalizedValue = Array.isArray(normalized) ? normalized[0] : normalized;
154
- if (normalizedValue == null || typeof normalizedValue !== "object" || Array.isArray(normalizedValue)) return normalizedValue;
155
- const normalizedObject = normalizedValue;
156
- if (original != null && typeof original === "object" && !Array.isArray(original)) return preserveNoContextJsonLdShape(normalizedObject, original, depth + 1);
157
- if (typeof normalizedObject["@id"] === "string") return normalizedObject["@id"];
158
- if ("@value" in normalizedObject) return normalizedObject["@value"];
159
- return normalizedValue;
160
- }
161
- function getTopLevelTerms(value) {
162
- const terms = /* @__PURE__ */ new Set();
163
- const nodes = Array.isArray(value) ? value : [value];
164
- for (const node of nodes) {
165
- if (node == null || typeof node !== "object" || Array.isArray(node)) continue;
166
- for (const key of globalThis.Object.keys(node)) {
167
- if (key.startsWith("@")) continue;
168
- terms.add(key);
169
- }
170
- }
171
- return terms;
172
- }
173
- async function mergeUnmappedTerms(compacted, original, context, documentLoader) {
174
- if (original == null || typeof original !== "object" || Array.isArray(original) || compacted == null || typeof compacted !== "object" || Array.isArray(compacted)) return compacted;
175
- const unmappedKeys = globalThis.Object.keys(original).filter((key) => key !== "@context" && !globalThis.Object.prototype.hasOwnProperty.call(compacted, key));
176
- if (unmappedKeys.length < 1) return compacted;
177
- const result = { ...compacted };
178
- const compactedWithContext = compacted != null && typeof compacted === "object" && !Array.isArray(compacted) && !("@context" in compacted) ? {
179
- "@context": context,
180
- ...compacted
181
- } : compacted;
182
- const compactedTerms = getTopLevelTerms(await jsonld_default.expand(compactedWithContext, { documentLoader }));
183
- const dummyPrefix = "urn:fedify:dummy:";
184
- const dummy = { "@context": context };
185
- for (let i = 0; i < unmappedKeys.length; i++) defineJsonLdProperty(dummy, unmappedKeys[i], `${dummyPrefix}${i}`);
186
- const expanded = await jsonld_default.expand(dummy, { documentLoader });
187
- const representedKeys = /* @__PURE__ */ new Set();
188
- const nodes = Array.isArray(expanded) ? expanded : [expanded];
189
- for (const node of nodes) {
190
- if (node == null || typeof node !== "object" || Array.isArray(node)) continue;
191
- for (const [term, termValue] of globalThis.Object.entries(node)) {
192
- if (!compactedTerms.has(term)) continue;
193
- for (let i = 0; i < unmappedKeys.length; i++) if (containsValue(termValue, `${dummyPrefix}${i}`)) representedKeys.add(unmappedKeys[i]);
194
- }
195
- }
196
- for (const key of unmappedKeys) if (!representedKeys.has(key)) defineJsonLdProperty(result, key, original[key]);
197
- return result;
198
- }
199
- function defineJsonLdProperty(object, key, value) {
200
- globalThis.Object.defineProperty(object, key, {
201
- value,
202
- enumerable: true,
203
- configurable: true,
204
- writable: true
205
- });
206
- }
207
- function containsValue(value, expected, depth = 0) {
208
- if (depth > 32) return false;
209
- if (value === expected) return true;
210
- if (Array.isArray(value)) return value.some((item) => containsValue(item, expected, depth + 1));
211
- if (value == null || typeof value !== "object") return false;
212
- return globalThis.Object.entries(value).some(([key, item]) => key !== "@context" && containsValue(item, expected, depth + 1));
213
- }
214
- async function preserveJsonLdShape(compacted, original, context, documentLoader, depth = 0) {
215
- if (depth > 32) return compacted;
216
- if (compacted === original) return compacted;
217
- if (original == null || typeof original !== "object" || compacted == null || typeof compacted !== "object") return compacted;
218
- if (Array.isArray(original)) {
219
- const compactedArray = Array.isArray(compacted) ? compacted : "@graph" in compacted && Array.isArray(compacted["@graph"]) ? compacted["@graph"] : void 0;
220
- if (compactedArray == null) return compacted;
221
- let clone;
222
- for (let i = 0; i < compactedArray.length; i++) {
223
- const value = await preserveJsonLdShape(compactedArray[i], original[i], context, documentLoader, depth + 1);
224
- const originalContext = original[i] != null && typeof original[i] === "object" && !Array.isArray(original[i]) && "@context" in original[i] ? original[i]["@context"] : void 0;
225
- const shaped = originalContext !== void 0 && value != null && typeof value === "object" && !Array.isArray(value) && !("@context" in value) ? {
226
- "@context": originalContext,
227
- ...value
228
- } : value;
229
- if (shaped !== compactedArray[i]) {
230
- clone ??= compactedArray.slice(0, i);
231
- clone.push(shaped);
232
- } else if (clone != null) clone.push(compactedArray[i]);
233
- }
234
- return clone ?? (Array.isArray(compacted) ? compacted : compactedArray);
235
- }
236
- if (Array.isArray(compacted)) return compacted;
237
- let clone;
238
- const ownContext = getOwnJsonLdContext(original);
239
- const objectContext = combineContexts(context, ownContext);
240
- const objectCompacted = depth > 0 && ownContext === null ? await compactWithEmptyContext(compacted, context, documentLoader) : compacted;
241
- const compactedObject = depth > 0 ? await mergeUnmappedTerms(objectCompacted, original, objectContext, documentLoader) : objectCompacted;
242
- const originalObject = original;
243
- for (const key of globalThis.Object.keys(compactedObject)) {
244
- if (key === "@context") continue;
245
- const value = await preserveJsonLdShape(compactedObject[key], originalObject[key], objectContext, documentLoader, depth + 1);
246
- const shaped = Array.isArray(originalObject[key]) && !Array.isArray(value) ? [value] : value;
247
- if (shaped !== compactedObject[key]) {
248
- clone ??= { ...compactedObject };
249
- defineJsonLdProperty(clone, key, shaped);
250
- }
251
- }
252
- if (depth > 0) {
253
- if ("@context" in originalObject && !("@context" in compactedObject)) {
254
- clone ??= { ...compactedObject };
255
- clone["@context"] = originalObject["@context"];
256
- }
257
- }
258
- return clone ?? compactedObject;
259
- }
260
- async function compactWithEmptyContext(compacted, context, documentLoader) {
261
- const compactedWithContext = compacted != null && typeof compacted === "object" && !Array.isArray(compacted) && !("@context" in compacted) ? {
262
- "@context": context,
263
- ...compacted
264
- } : compacted;
265
- const expanded = await jsonld_default.expand(compactedWithContext, { documentLoader });
266
- return await jsonld_default.compact(expanded, {}, { documentLoader });
267
- }
268
- function combineContexts(inheritedContext, ownContext) {
269
- if (ownContext === noJsonLdContext || ownContext === inheritedContext) return inheritedContext;
270
- if (ownContext == null) return ownContext;
271
- const inherited = Array.isArray(inheritedContext) ? inheritedContext : [inheritedContext];
272
- const own = Array.isArray(ownContext) ? ownContext : [ownContext];
273
- return [...inherited, ...own];
274
- }
275
- //#endregion
276
5
  //#region src/jsonld-cache.test.ts
277
6
  test("isTrustedIriOrigin() trusts same portable IRI origins", () => {
278
7
  ok(isTrustedIriOrigin({}, parseIri("ap://did:key:z6Mkabc/actor"), parseIri("ap+ef61://did:key:z6Mkabc/outbox")));
@@ -1,7 +1,7 @@
1
1
  import process from "node:process";
2
2
  //#region deno.json
3
3
  var name = "@fedify/vocab-runtime";
4
- var version = "2.4.0-dev.2115+e2feaf2b";
4
+ var version = "2.4.0-dev.2127+c41ce2eb";
5
5
  //#endregion
6
6
  //#region src/request.ts
7
7
  /**
@@ -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.2115+e2feaf2b";
6
+ var version = "2.4.0-dev.2127+c41ce2eb";
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-BdOpOc_q.cjs");
2
+ const require_request = require("./request-DzzSwifZ.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 { n as createActivityPubRequest, o as version, r as getUserAgent } from "./request-f7iixEM_.mjs";
1
+ import { n as createActivityPubRequest, o as version, r as getUserAgent } from "./request-C6bviL-E.mjs";
2
2
  import { test } from "node:test";
3
3
  import process from "node:process";
4
4
  import { deepStrictEqual } from "node:assert";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-runtime",
3
- "version": "2.4.0-dev.2115+e2feaf2b",
3
+ "version": "2.4.0-dev.2127+c41ce2eb",
4
4
  "homepage": "https://fedify.dev/",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,4 +1,5 @@
1
1
  import fetchMock from "fetch-mock";
2
+ import { configure, type LogRecord, reset } from "@logtape/logtape";
2
3
  import { deepStrictEqual, ok, rejects } from "node:assert";
3
4
  import { test } from "node:test";
4
5
  import { createServer } from "node:http";
@@ -9,6 +10,78 @@ import { getDocumentLoader, getRemoteDocument } from "./docloader.ts";
9
10
  import { FetchError } from "./request.ts";
10
11
  import { UrlError } from "./url.ts";
11
12
 
13
+ test("getRemoteDocument() forwards options to alternate documents", async () => {
14
+ const records: LogRecord[] = [];
15
+ await reset();
16
+ await configure({
17
+ sinks: { capture: (record) => records.push(record) },
18
+ loggers: [{
19
+ category: ["fedify"],
20
+ lowestLevel: "debug",
21
+ sinks: ["capture"],
22
+ }],
23
+ });
24
+ try {
25
+ for (const linkHeader of [true, false]) {
26
+ for (const suppressError of [true, false, undefined]) {
27
+ records.length = 0;
28
+ const url = "https://example.com/alternate-source";
29
+ const alternateUrl = "https://example.com/missing-alternate";
30
+ const options = {
31
+ suppressError,
32
+ signal: new AbortController().signal,
33
+ };
34
+ const response = new Response(
35
+ `<link rel="alternate" type="application/activity+json" href="${alternateUrl}">`,
36
+ {
37
+ headers: {
38
+ "Content-Type": "text/html",
39
+ ...(linkHeader
40
+ ? {
41
+ Link:
42
+ `<${alternateUrl}>; rel="alternate"; type="application/activity+json"`,
43
+ }
44
+ : {}),
45
+ },
46
+ },
47
+ );
48
+ let alternateRequests = 0;
49
+ await rejects(
50
+ () =>
51
+ getRemoteDocument(
52
+ url,
53
+ response,
54
+ async (requestedUrl, forwarded) => {
55
+ alternateRequests++;
56
+ deepStrictEqual(requestedUrl, alternateUrl);
57
+ deepStrictEqual(forwarded, options);
58
+ return await getRemoteDocument(
59
+ requestedUrl,
60
+ new Response(null, { status: 404 }),
61
+ () => {
62
+ throw new Error("Unexpected additional alternate");
63
+ },
64
+ forwarded,
65
+ );
66
+ },
67
+ options,
68
+ ),
69
+ FetchError,
70
+ );
71
+ deepStrictEqual(alternateRequests, 1);
72
+ const failures = records.filter((record) =>
73
+ record.rawMessage ===
74
+ "Failed to fetch document: {status} {url} {headers}"
75
+ );
76
+ deepStrictEqual(failures.length, 1);
77
+ deepStrictEqual(failures[0].level, suppressError ? "warning" : "error");
78
+ }
79
+ }
80
+ } finally {
81
+ await reset();
82
+ }
83
+ });
84
+
12
85
  test("new FetchError()", () => {
13
86
  const e = new FetchError("https://example.com/", "An error message.");
14
87
  deepStrictEqual(e.name, "FetchError");
package/src/docloader.ts CHANGED
@@ -47,6 +47,13 @@ export interface DocumentLoaderOptions {
47
47
  * @since 1.8.0
48
48
  */
49
49
  signal?: AbortSignal;
50
+
51
+ /**
52
+ * Whether to lower error-level logs for recoverable document loading
53
+ * failures to warning-level logs. The loader still throws the error.
54
+ * @default `false`
55
+ */
56
+ suppressError?: boolean;
50
57
  }
51
58
 
52
59
  /**
@@ -127,6 +134,7 @@ function createResponseMetadata(response: Response): Response {
127
134
  * @param url The URL of the document to load.
128
135
  * @param response The response to get the document from.
129
136
  * @param fetch The function to fetch the document.
137
+ * @param options The options for loading the document.
130
138
  * @returns The loaded remote document.
131
139
  * @throws {FetchError} If the response is not OK.
132
140
  * @internal
@@ -138,18 +146,31 @@ export async function getRemoteDocument(
138
146
  url: string,
139
147
  options?: DocumentLoaderOptions,
140
148
  ) => Promise<RemoteDocument>,
149
+ options?: DocumentLoaderOptions,
141
150
  ): Promise<RemoteDocument> {
142
151
  const documentUrl = response.url === "" ? url : response.url;
143
152
  const docUrl = new URL(documentUrl);
144
153
  if (!response.ok) {
145
- logger.error(
146
- "Failed to fetch document: {status} {url} {headers}",
147
- {
148
- status: response.status,
149
- url: documentUrl,
150
- headers: Object.fromEntries(response.headers.entries()),
151
- },
152
- );
154
+ if (options?.suppressError) {
155
+ logger.warn(
156
+ "Failed to fetch document: {status} {url} {headers}",
157
+ {
158
+ status: response.status,
159
+ url: documentUrl,
160
+ headers: Object.fromEntries(response.headers.entries()),
161
+ },
162
+ );
163
+ } else {
164
+ logger.error(
165
+ "Failed to fetch document: {status} {url} {headers}",
166
+ {
167
+ status: response.status,
168
+ url: documentUrl,
169
+ headers: Object.fromEntries(response.headers.entries()),
170
+ },
171
+ );
172
+ }
173
+
153
174
  throw new FetchError(
154
175
  documentUrl,
155
176
  `HTTP ${response.status}: ${documentUrl}`,
@@ -198,7 +219,7 @@ export async function getRemoteDocument(
198
219
  "Found alternate document: {alternateUrl} from {url}",
199
220
  { alternateUrl: altUri.href, url: documentUrl },
200
221
  );
201
- return await fetch(altUri.href);
222
+ return await fetch(altUri.href, options);
202
223
  }
203
224
  }
204
225
  }
@@ -260,7 +281,7 @@ export async function getRemoteDocument(
260
281
  "Found alternate document: {alternateUrl} from {url}",
261
282
  { alternateUrl: attribs.href, url: documentUrl },
262
283
  );
263
- return await fetch(new URL(attribs.href, docUrl).href);
284
+ return await fetch(new URL(attribs.href, docUrl).href, options);
264
285
  }
265
286
  }
266
287
  try {
@@ -350,10 +371,13 @@ export function getDocumentLoader(
350
371
  await validatePublicUrl(currentUrl);
351
372
  } catch (error) {
352
373
  if (error instanceof UrlError) {
353
- logger.error("Disallowed private URL: {url}", {
354
- url: currentUrl,
355
- error,
356
- });
374
+ logger[options?.suppressError ? "warn" : "error"](
375
+ "Disallowed private URL: {url}",
376
+ {
377
+ url: currentUrl,
378
+ error,
379
+ },
380
+ );
357
381
  }
358
382
  throw error;
359
383
  }
@@ -387,7 +411,7 @@ export function getDocumentLoader(
387
411
  response.headers.has("Location")
388
412
  ) {
389
413
  if (redirected >= maximumRedirection) {
390
- logger.error(
414
+ logger[options?.suppressError ? "warn" : "error"](
391
415
  "Too many redirections ({redirections}) while fetching document.",
392
416
  { redirections: redirected + 1, url: currentUrl },
393
417
  );
@@ -402,7 +426,7 @@ export function getDocumentLoader(
402
426
  ).href;
403
427
  span.setAttribute("http.redirect.url", redirectUrl);
404
428
  if (visited.has(redirectUrl)) {
405
- logger.error(
429
+ logger[options?.suppressError ? "warn" : "error"](
406
430
  "Detected a redirect loop while fetching document: {url} -> " +
407
431
  "{redirectUrl}",
408
432
  { url: currentUrl, redirectUrl },
@@ -415,7 +439,12 @@ export function getDocumentLoader(
415
439
  return await load(redirectUrl, options, redirected + 1, visited);
416
440
  }
417
441
 
418
- const result = await getRemoteDocument(currentUrl, response, load);
442
+ const result = await getRemoteDocument(
443
+ currentUrl,
444
+ response,
445
+ load,
446
+ options,
447
+ );
419
448
  span.setAttribute("docloader.document_url", result.documentUrl);
420
449
  if (result.contextUrl != null) {
421
450
  span.setAttribute("docloader.context_url", result.contextUrl);
@@ -4,6 +4,66 @@ import { formatIri, haveSameFe34Origin, haveSameIriOrigin } from "../url.ts";
4
4
 
5
5
  const noJsonLdContext = Symbol("noJsonLdContext");
6
6
 
7
+ const documentLoaderWrappers = new WeakMap<
8
+ DocumentLoader,
9
+ { readonly base: DocumentLoader; released: boolean }
10
+ >();
11
+
12
+ /**
13
+ * Registers a loader wrapper with the shared released-wrapper registry.
14
+ * Both suppression and snapshot wrappers must use this registry so mixed
15
+ * chains can be unwrapped. The caller updates the state on release.
16
+ * @internal Not a public API.
17
+ */
18
+ export function registerDocumentLoaderWrapper(
19
+ loader: DocumentLoader,
20
+ state: { readonly base: DocumentLoader; released: boolean },
21
+ ): void {
22
+ documentLoaderWrappers.set(loader, state);
23
+ }
24
+
25
+ /**
26
+ * Removes released loader wrappers, stopping at the first active wrapper.
27
+ * Active wrappers retain their operation's suppression or snapshot cache.
28
+ * @internal Not a public API.
29
+ */
30
+ export function unwrapReleasedDocumentLoader(
31
+ base: DocumentLoader,
32
+ ): DocumentLoader {
33
+ for (
34
+ let state = documentLoaderWrappers.get(base);
35
+ state?.released;
36
+ state = documentLoaderWrappers.get(base)
37
+ ) {
38
+ base = state.base;
39
+ }
40
+ return base;
41
+ }
42
+
43
+ /**
44
+ * Lowers context loading failure logs for one accessor operation. Parsed
45
+ * objects retain the loader, so release it before returning to the caller.
46
+ * Released wrappers are unwrapped to avoid accumulating loader chains.
47
+ * @internal Used by generated vocabulary accessors; not a public API.
48
+ */
49
+ export function createScopedContextLoader(
50
+ base: DocumentLoader,
51
+ suppressError?: boolean,
52
+ ): { loader: DocumentLoader; release: () => void } {
53
+ base = unwrapReleasedDocumentLoader(base);
54
+ if (!suppressError) return { loader: base, release: () => {} };
55
+ const state = { base, released: false };
56
+ const loader: DocumentLoader = (url, options) =>
57
+ base(url, state.released ? options : { ...options, suppressError: true });
58
+ registerDocumentLoaderWrapper(loader, state);
59
+ return {
60
+ loader,
61
+ release: () => {
62
+ state.released = true;
63
+ },
64
+ };
65
+ }
66
+
7
67
  /**
8
68
  * Options for deciding whether two IRIs should be treated as same-origin.
9
69
  *