@fedify/vocab-runtime 2.4.0-dev.1570 → 2.4.0-dev.1571

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 (48) hide show
  1. package/deno.json +2 -1
  2. package/dist/docloader-DnUMWHaJ.d.cts +202 -0
  3. package/dist/docloader-xRGn1azD.d.ts +202 -0
  4. package/dist/internal/jsonld-cache.cjs +279 -0
  5. package/dist/internal/jsonld-cache.d.cts +48 -0
  6. package/dist/internal/jsonld-cache.d.ts +48 -0
  7. package/dist/internal/jsonld-cache.js +275 -0
  8. package/dist/mod.cjs +178 -190
  9. package/dist/mod.d.cts +59 -190
  10. package/dist/mod.d.ts +59 -190
  11. package/dist/mod.js +165 -184
  12. package/dist/tests/decimal.test.cjs +3 -3
  13. package/dist/tests/decimal.test.mjs +3 -3
  14. package/dist/tests/{docloader-C76ldE5C.mjs → docloader-DHSAmRW2.mjs} +97 -10
  15. package/dist/tests/{docloader-mvgIWKI7.cjs → docloader-DkiPIIAk.cjs} +102 -9
  16. package/dist/tests/docloader.test.cjs +58 -6
  17. package/dist/tests/docloader.test.mjs +58 -6
  18. package/dist/tests/jsonld-cache.test.cjs +652 -0
  19. package/dist/tests/jsonld-cache.test.d.cts +1 -0
  20. package/dist/tests/jsonld-cache.test.d.mts +1 -0
  21. package/dist/tests/jsonld-cache.test.mjs +651 -0
  22. package/dist/tests/{key-CrrK9mYh.mjs → key-CDGDH_vC.mjs} +69 -1
  23. package/dist/tests/{key-pMmqUKuo.cjs → key-_wXwomh_.cjs} +86 -0
  24. package/dist/tests/key.test.cjs +48 -1
  25. package/dist/tests/key.test.mjs +48 -1
  26. package/dist/tests/{request-BEXkv1ul.mjs → request-CYFeP37O.mjs} +1 -1
  27. package/dist/tests/{request-SworbvLc.cjs → request-D3H8nWtX.cjs} +1 -1
  28. package/dist/tests/request.test.cjs +1 -1
  29. package/dist/tests/request.test.mjs +1 -1
  30. package/dist/tests/{url-C20FhC7p.cjs → url-2XwVbUS_.cjs} +108 -0
  31. package/dist/tests/{url-m9Qzxy-Y.mjs → url-YWJbnRlf.mjs} +85 -1
  32. package/dist/tests/url.test.cjs +104 -1
  33. package/dist/tests/url.test.mjs +105 -2
  34. package/dist/url-BAdyyqAa.cjs +315 -0
  35. package/dist/url-BuxPHxK2.js +261 -0
  36. package/package.json +11 -1
  37. package/src/contexts/fep-7aa9.json +24 -0
  38. package/src/contexts.ts +2 -0
  39. package/src/docloader.test.ts +102 -6
  40. package/src/docloader.ts +76 -8
  41. package/src/internal/jsonld-cache.ts +538 -0
  42. package/src/jsonld-cache.test.ts +554 -0
  43. package/src/key.test.ts +86 -0
  44. package/src/key.ts +125 -0
  45. package/src/mod.ts +8 -0
  46. package/src/url.test.ts +252 -1
  47. package/src/url.ts +141 -0
  48. package/tsdown.config.ts +6 -1
package/deno.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@fedify/vocab-runtime",
3
- "version": "2.4.0-dev.1570+f1b6aaa3",
3
+ "version": "2.4.0-dev.1571+31e2786b",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./src/mod.ts",
7
+ "./internal/jsonld-cache": "./src/internal/jsonld-cache.ts",
7
8
  "./jsonld": "./src/jsonld.ts",
8
9
  "./temporal": "./src/temporal.ts"
9
10
  },
@@ -0,0 +1,202 @@
1
+ /// <reference lib="esnext.temporal" />
2
+ import { Logger } from "@logtape/logtape";
3
+
4
+ //#region src/request.d.ts
5
+ /**
6
+ * Error thrown when fetching a JSON-LD document failed.
7
+ */
8
+ declare class FetchError extends Error {
9
+ /**
10
+ * The URL that failed to fetch.
11
+ */
12
+ url: URL;
13
+ /**
14
+ * The HTTP response that failed, if available.
15
+ */
16
+ response?: Response;
17
+ /**
18
+ * Constructs a new `FetchError`.
19
+ *
20
+ * @param url The URL that failed to fetch.
21
+ * @param message Error message.
22
+ * @param response The failed HTTP response, if available.
23
+ */
24
+ constructor(url: URL | string, message?: string, response?: Response);
25
+ }
26
+ /**
27
+ * Options for creating a request.
28
+ * @internal
29
+ */
30
+ interface CreateRequestOptions {
31
+ userAgent?: GetUserAgentOptions | string;
32
+ }
33
+ /**
34
+ * Creates a request for the given URL.
35
+ * @param url The URL to create the request for.
36
+ * @param options The options for the request.
37
+ * @returns The created request.
38
+ * @internal
39
+ */
40
+ declare function createActivityPubRequest(url: string, options?: CreateRequestOptions): Request;
41
+ /**
42
+ * Options for making `User-Agent` string.
43
+ * @see {@link getUserAgent}
44
+ * @since 1.3.0
45
+ */
46
+ interface GetUserAgentOptions {
47
+ /**
48
+ * An optional software name and version, e.g., `"Hollo/1.0.0"`.
49
+ */
50
+ software?: string | null;
51
+ /**
52
+ * An optional URL to append to the user agent string.
53
+ * Usually the URL of the ActivityPub instance.
54
+ */
55
+ url?: string | URL | null;
56
+ }
57
+ /**
58
+ * Gets the user agent string for the given application and URL.
59
+ * @param options The options for making the user agent string.
60
+ * @returns The user agent string.
61
+ * @since 1.3.0
62
+ */
63
+ declare function getUserAgent({
64
+ software,
65
+ url
66
+ }?: GetUserAgentOptions): string;
67
+ /**
68
+ * Logs the request.
69
+ * @param request The request to log.
70
+ * @internal
71
+ */
72
+ declare function logRequest(logger: Logger, request: Request): void;
73
+ //#endregion
74
+ //#region src/docloader.d.ts
75
+ /**
76
+ * A remote JSON-LD document and its context fetched by
77
+ * a {@link DocumentLoader}.
78
+ */
79
+ interface RemoteDocument {
80
+ /**
81
+ * The URL of the context document.
82
+ */
83
+ contextUrl: string | null;
84
+ /**
85
+ * The fetched JSON-LD document.
86
+ */
87
+ document: unknown;
88
+ /**
89
+ * The URL of the fetched document.
90
+ */
91
+ documentUrl: string;
92
+ }
93
+ /**
94
+ * Options for {@link DocumentLoader}.
95
+ * @since 1.8.0
96
+ */
97
+ interface DocumentLoaderOptions {
98
+ /**
99
+ * An `AbortSignal` for cancellation.
100
+ * @since 1.8.0
101
+ */
102
+ signal?: AbortSignal;
103
+ }
104
+ /**
105
+ * A JSON-LD document loader that fetches documents from the Web.
106
+ * @param url The URL of the document to load.
107
+ * @param options The options for the document loader.
108
+ * @returns The loaded remote document.
109
+ */
110
+ type DocumentLoader = (url: string, options?: DocumentLoaderOptions) => Promise<RemoteDocument>;
111
+ /**
112
+ * A factory function that creates a {@link DocumentLoader} with options.
113
+ * @param options The options for the document loader.
114
+ * @returns The document loader.
115
+ * @since 1.4.0
116
+ */
117
+ type DocumentLoaderFactory = (options?: DocumentLoaderFactoryOptions) => DocumentLoader;
118
+ /**
119
+ * Options for {@link DocumentLoaderFactory}.
120
+ * @see {@link DocumentLoaderFactory}
121
+ * @see {@link AuthenticatedDocumentLoaderFactory}
122
+ * @since 1.4.0
123
+ */
124
+ interface DocumentLoaderFactoryOptions {
125
+ /**
126
+ * Whether to allow fetching private network addresses.
127
+ * Turned off by default.
128
+ * @default `false``
129
+ */
130
+ allowPrivateAddress?: boolean;
131
+ /**
132
+ * Options for making `User-Agent` string.
133
+ * If a string is given, it is used as the `User-Agent` header value.
134
+ * If an object is given, it is passed to {@link getUserAgent} function.
135
+ */
136
+ userAgent?: GetUserAgentOptions | string;
137
+ /**
138
+ * The maximum number of redirections to follow.
139
+ * @default `20`
140
+ * @since 2.2.0
141
+ */
142
+ maxRedirection?: number;
143
+ }
144
+ /**
145
+ * A factory function that creates an authenticated {@link DocumentLoader} for
146
+ * a given identity. This is used for fetching documents that require
147
+ * authentication.
148
+ * @param identity The identity to create the document loader for.
149
+ * The actor's key pair.
150
+ * @param options The options for the document loader.
151
+ * @returns The authenticated document loader.
152
+ * @since 0.4.0
153
+ */
154
+ type AuthenticatedDocumentLoaderFactory = (identity: {
155
+ keyId: URL;
156
+ privateKey: CryptoKey;
157
+ }, options?: DocumentLoaderFactoryOptions) => DocumentLoader;
158
+ /**
159
+ * Gets a {@link RemoteDocument} from the given response.
160
+ * @param url The URL of the document to load.
161
+ * @param response The response to get the document from.
162
+ * @param fetch The function to fetch the document.
163
+ * @returns The loaded remote document.
164
+ * @throws {FetchError} If the response is not OK.
165
+ * @internal
166
+ */
167
+ declare function getRemoteDocument(url: string, response: Response, fetch: (url: string, options?: DocumentLoaderOptions) => Promise<RemoteDocument>): Promise<RemoteDocument>;
168
+ /**
169
+ * Options for {@link getDocumentLoader}.
170
+ * @since 1.3.0
171
+ */
172
+ interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
173
+ /**
174
+ * Whether to preload the frequently used contexts.
175
+ */
176
+ skipPreloadedContexts?: boolean;
177
+ }
178
+ /**
179
+ * Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
180
+ *
181
+ * The created loader preloads the below frequently used contexts by default
182
+ * (unless `options.skipPreloadedContexts` is set to `true`):
183
+ *
184
+ * - <https://www.w3.org/ns/activitystreams>
185
+ * - <https://w3id.org/security/v1>
186
+ * - <https://w3id.org/security/data-integrity/v1>
187
+ * - <https://www.w3.org/ns/did/v1>
188
+ * - <https://w3id.org/security/multikey/v1>
189
+ * - <https://purl.archive.org/socialweb/webfinger>
190
+ * - <http://schema.org/>
191
+ * @param options Options for the document loader.
192
+ * @returns The document loader.
193
+ * @since 1.3.0
194
+ */
195
+ declare function getDocumentLoader({
196
+ allowPrivateAddress,
197
+ maxRedirection,
198
+ skipPreloadedContexts,
199
+ userAgent
200
+ }?: GetDocumentLoaderOptions): DocumentLoader;
201
+ //#endregion
202
+ export { DocumentLoaderOptions as a, getDocumentLoader as c, FetchError as d, GetUserAgentOptions as f, logRequest as h, DocumentLoaderFactoryOptions as i, getRemoteDocument as l, getUserAgent as m, DocumentLoader as n, GetDocumentLoaderOptions as o, createActivityPubRequest as p, DocumentLoaderFactory as r, RemoteDocument as s, AuthenticatedDocumentLoaderFactory as t, CreateRequestOptions as u };
@@ -0,0 +1,202 @@
1
+ /// <reference lib="esnext.temporal" />
2
+ import { Logger } from "@logtape/logtape";
3
+
4
+ //#region src/request.d.ts
5
+ /**
6
+ * Error thrown when fetching a JSON-LD document failed.
7
+ */
8
+ declare class FetchError extends Error {
9
+ /**
10
+ * The URL that failed to fetch.
11
+ */
12
+ url: URL;
13
+ /**
14
+ * The HTTP response that failed, if available.
15
+ */
16
+ response?: Response;
17
+ /**
18
+ * Constructs a new `FetchError`.
19
+ *
20
+ * @param url The URL that failed to fetch.
21
+ * @param message Error message.
22
+ * @param response The failed HTTP response, if available.
23
+ */
24
+ constructor(url: URL | string, message?: string, response?: Response);
25
+ }
26
+ /**
27
+ * Options for creating a request.
28
+ * @internal
29
+ */
30
+ interface CreateRequestOptions {
31
+ userAgent?: GetUserAgentOptions | string;
32
+ }
33
+ /**
34
+ * Creates a request for the given URL.
35
+ * @param url The URL to create the request for.
36
+ * @param options The options for the request.
37
+ * @returns The created request.
38
+ * @internal
39
+ */
40
+ declare function createActivityPubRequest(url: string, options?: CreateRequestOptions): Request;
41
+ /**
42
+ * Options for making `User-Agent` string.
43
+ * @see {@link getUserAgent}
44
+ * @since 1.3.0
45
+ */
46
+ interface GetUserAgentOptions {
47
+ /**
48
+ * An optional software name and version, e.g., `"Hollo/1.0.0"`.
49
+ */
50
+ software?: string | null;
51
+ /**
52
+ * An optional URL to append to the user agent string.
53
+ * Usually the URL of the ActivityPub instance.
54
+ */
55
+ url?: string | URL | null;
56
+ }
57
+ /**
58
+ * Gets the user agent string for the given application and URL.
59
+ * @param options The options for making the user agent string.
60
+ * @returns The user agent string.
61
+ * @since 1.3.0
62
+ */
63
+ declare function getUserAgent({
64
+ software,
65
+ url
66
+ }?: GetUserAgentOptions): string;
67
+ /**
68
+ * Logs the request.
69
+ * @param request The request to log.
70
+ * @internal
71
+ */
72
+ declare function logRequest(logger: Logger, request: Request): void;
73
+ //#endregion
74
+ //#region src/docloader.d.ts
75
+ /**
76
+ * A remote JSON-LD document and its context fetched by
77
+ * a {@link DocumentLoader}.
78
+ */
79
+ interface RemoteDocument {
80
+ /**
81
+ * The URL of the context document.
82
+ */
83
+ contextUrl: string | null;
84
+ /**
85
+ * The fetched JSON-LD document.
86
+ */
87
+ document: unknown;
88
+ /**
89
+ * The URL of the fetched document.
90
+ */
91
+ documentUrl: string;
92
+ }
93
+ /**
94
+ * Options for {@link DocumentLoader}.
95
+ * @since 1.8.0
96
+ */
97
+ interface DocumentLoaderOptions {
98
+ /**
99
+ * An `AbortSignal` for cancellation.
100
+ * @since 1.8.0
101
+ */
102
+ signal?: AbortSignal;
103
+ }
104
+ /**
105
+ * A JSON-LD document loader that fetches documents from the Web.
106
+ * @param url The URL of the document to load.
107
+ * @param options The options for the document loader.
108
+ * @returns The loaded remote document.
109
+ */
110
+ type DocumentLoader = (url: string, options?: DocumentLoaderOptions) => Promise<RemoteDocument>;
111
+ /**
112
+ * A factory function that creates a {@link DocumentLoader} with options.
113
+ * @param options The options for the document loader.
114
+ * @returns The document loader.
115
+ * @since 1.4.0
116
+ */
117
+ type DocumentLoaderFactory = (options?: DocumentLoaderFactoryOptions) => DocumentLoader;
118
+ /**
119
+ * Options for {@link DocumentLoaderFactory}.
120
+ * @see {@link DocumentLoaderFactory}
121
+ * @see {@link AuthenticatedDocumentLoaderFactory}
122
+ * @since 1.4.0
123
+ */
124
+ interface DocumentLoaderFactoryOptions {
125
+ /**
126
+ * Whether to allow fetching private network addresses.
127
+ * Turned off by default.
128
+ * @default `false``
129
+ */
130
+ allowPrivateAddress?: boolean;
131
+ /**
132
+ * Options for making `User-Agent` string.
133
+ * If a string is given, it is used as the `User-Agent` header value.
134
+ * If an object is given, it is passed to {@link getUserAgent} function.
135
+ */
136
+ userAgent?: GetUserAgentOptions | string;
137
+ /**
138
+ * The maximum number of redirections to follow.
139
+ * @default `20`
140
+ * @since 2.2.0
141
+ */
142
+ maxRedirection?: number;
143
+ }
144
+ /**
145
+ * A factory function that creates an authenticated {@link DocumentLoader} for
146
+ * a given identity. This is used for fetching documents that require
147
+ * authentication.
148
+ * @param identity The identity to create the document loader for.
149
+ * The actor's key pair.
150
+ * @param options The options for the document loader.
151
+ * @returns The authenticated document loader.
152
+ * @since 0.4.0
153
+ */
154
+ type AuthenticatedDocumentLoaderFactory = (identity: {
155
+ keyId: URL;
156
+ privateKey: CryptoKey;
157
+ }, options?: DocumentLoaderFactoryOptions) => DocumentLoader;
158
+ /**
159
+ * Gets a {@link RemoteDocument} from the given response.
160
+ * @param url The URL of the document to load.
161
+ * @param response The response to get the document from.
162
+ * @param fetch The function to fetch the document.
163
+ * @returns The loaded remote document.
164
+ * @throws {FetchError} If the response is not OK.
165
+ * @internal
166
+ */
167
+ declare function getRemoteDocument(url: string, response: Response, fetch: (url: string, options?: DocumentLoaderOptions) => Promise<RemoteDocument>): Promise<RemoteDocument>;
168
+ /**
169
+ * Options for {@link getDocumentLoader}.
170
+ * @since 1.3.0
171
+ */
172
+ interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
173
+ /**
174
+ * Whether to preload the frequently used contexts.
175
+ */
176
+ skipPreloadedContexts?: boolean;
177
+ }
178
+ /**
179
+ * Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
180
+ *
181
+ * The created loader preloads the below frequently used contexts by default
182
+ * (unless `options.skipPreloadedContexts` is set to `true`):
183
+ *
184
+ * - <https://www.w3.org/ns/activitystreams>
185
+ * - <https://w3id.org/security/v1>
186
+ * - <https://w3id.org/security/data-integrity/v1>
187
+ * - <https://www.w3.org/ns/did/v1>
188
+ * - <https://w3id.org/security/multikey/v1>
189
+ * - <https://purl.archive.org/socialweb/webfinger>
190
+ * - <http://schema.org/>
191
+ * @param options Options for the document loader.
192
+ * @returns The document loader.
193
+ * @since 1.3.0
194
+ */
195
+ declare function getDocumentLoader({
196
+ allowPrivateAddress,
197
+ maxRedirection,
198
+ skipPreloadedContexts,
199
+ userAgent
200
+ }?: GetDocumentLoaderOptions): DocumentLoader;
201
+ //#endregion
202
+ export { DocumentLoaderOptions as a, getDocumentLoader as c, FetchError as d, GetUserAgentOptions as f, logRequest as h, DocumentLoaderFactoryOptions as i, getRemoteDocument as l, getUserAgent as m, DocumentLoader as n, GetDocumentLoaderOptions as o, createActivityPubRequest as p, DocumentLoaderFactory as r, RemoteDocument as s, AuthenticatedDocumentLoaderFactory as t, CreateRequestOptions as u };
@@ -0,0 +1,279 @@
1
+
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const require_url = require("../url-BAdyyqAa.cjs");
4
+ const require_jsonld = require("../jsonld.cjs");
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 && require_url.haveSameIriOrigin(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 require_url.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 require_jsonld.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 require_jsonld.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 require_jsonld.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 require_jsonld.expand(compactedWithContext, { documentLoader });
266
+ return await require_jsonld.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
+ exports.compactJsonLdCache = compactJsonLdCache;
277
+ exports.getJsonLdContext = getJsonLdContext;
278
+ exports.isTrustedIriOrigin = isTrustedIriOrigin;
279
+ exports.normalizeJsonLdIris = normalizeJsonLdIris;