@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/src/key.ts CHANGED
@@ -22,6 +22,39 @@ const algorithms: Record<
22
22
  "1.3.101.112": "Ed25519",
23
23
  };
24
24
 
25
+ const DID_KEY_PREFIX = "did:key:";
26
+ const ED25519_PUBLIC_KEY_MULTICODEC = 0xed;
27
+ const ED25519_PUBLIC_KEY_LENGTH = 32;
28
+ const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
29
+ const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
30
+
31
+ /**
32
+ * Parsed `did:key` verification method.
33
+ *
34
+ * @since 2.4.0
35
+ */
36
+ export interface DidKeyVerificationMethod {
37
+ /**
38
+ * The DID URL identifying the verification method.
39
+ */
40
+ readonly id: URL;
41
+
42
+ /**
43
+ * The controller DID.
44
+ */
45
+ readonly controller: URL;
46
+
47
+ /**
48
+ * The Ed25519 public key encoded as a Multibase Multikey value.
49
+ */
50
+ readonly publicKeyMultibase: string;
51
+
52
+ /**
53
+ * The Ed25519 public key.
54
+ */
55
+ readonly publicKey: CryptoKey;
56
+ }
57
+
25
58
  /**
26
59
  * Imports a PEM-SPKI formatted public key.
27
60
  * @param pem The PEM-SPKI formatted public key.
@@ -93,6 +126,98 @@ export function importPem(pem: string): Promise<CryptoKey> {
93
126
  return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
94
127
  }
95
128
 
129
+ function decodeEd25519DidKeyMultibase(multibaseKey: string): Uint8Array {
130
+ if (!multibaseKey.startsWith("z")) {
131
+ throw new TypeError("did:key must use base58-btc Multibase encoding.");
132
+ }
133
+ let decoded: Uint8Array;
134
+ try {
135
+ decoded = decodeMultibase(multibaseKey);
136
+ } catch (error) {
137
+ throw new TypeError("Invalid did:key Multibase encoding.", {
138
+ cause: error,
139
+ });
140
+ }
141
+ const { code } = getMulticodecPrefix(decoded);
142
+ if (code !== ED25519_PUBLIC_KEY_MULTICODEC) {
143
+ throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
144
+ }
145
+ const content = removeMulticodecPrefix(decoded);
146
+ if (content.length !== ED25519_PUBLIC_KEY_LENGTH) {
147
+ throw new TypeError("Invalid Ed25519 did:key length.");
148
+ }
149
+ return content;
150
+ }
151
+
152
+ /**
153
+ * Imports an Ed25519 `did:key` DID.
154
+ *
155
+ * @param did The `did:key` DID.
156
+ * @returns The imported Ed25519 public key.
157
+ * @throws {TypeError} If the DID is malformed or uses an unsupported key type.
158
+ * @since 2.4.0
159
+ */
160
+ export async function importDidKey(did: string | URL): Promise<CryptoKey> {
161
+ const didString = did instanceof URL ? did.href : did;
162
+ const match = didString.match(DID_KEY_PATTERN);
163
+ if (match == null) throw new TypeError("Invalid did:key DID.");
164
+ const content = decodeEd25519DidKeyMultibase(match[1]);
165
+ return await crypto.subtle.importKey(
166
+ "raw",
167
+ content.slice(),
168
+ "Ed25519",
169
+ true,
170
+ ["verify"],
171
+ );
172
+ }
173
+
174
+ /**
175
+ * Exports an Ed25519 public key as a `did:key` DID.
176
+ *
177
+ * @param key The Ed25519 public key.
178
+ * @returns The `did:key` DID.
179
+ * @throws {TypeError} If the key is invalid or unsupported.
180
+ * @since 2.4.0
181
+ */
182
+ export async function exportDidKey(key: CryptoKey): Promise<string> {
183
+ if (key.algorithm.name !== "Ed25519") {
184
+ throw new TypeError(
185
+ "Unsupported key type: " + JSON.stringify(key.algorithm),
186
+ );
187
+ }
188
+ return DID_KEY_PREFIX + await exportMultibaseKey(key);
189
+ }
190
+
191
+ /**
192
+ * Parses an Ed25519 `did:key` verification method DID URL.
193
+ *
194
+ * @param didUrl The `did:key` DID URL.
195
+ * @returns The parsed verification method.
196
+ * @throws {TypeError} If the DID URL is malformed, unsupported, or its
197
+ * fragment does not identify the same key as the DID.
198
+ * @since 2.4.0
199
+ */
200
+ export async function parseDidKeyVerificationMethod(
201
+ didUrl: string | URL,
202
+ ): Promise<DidKeyVerificationMethod> {
203
+ const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
204
+ const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
205
+ if (match == null) {
206
+ throw new TypeError("Invalid did:key verification method.");
207
+ }
208
+ const [, publicKeyMultibase, fragment] = match;
209
+ if (publicKeyMultibase !== fragment) {
210
+ throw new TypeError("Invalid did:key verification method fragment.");
211
+ }
212
+ const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
213
+ return {
214
+ id: new URL(didUrlString),
215
+ controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
216
+ publicKeyMultibase,
217
+ publicKey,
218
+ };
219
+ }
220
+
96
221
  /**
97
222
  * Imports a [Multibase]-encoded public key.
98
223
  *
package/src/mod.ts CHANGED
@@ -17,12 +17,16 @@ export {
17
17
  type RemoteDocument,
18
18
  } from "./docloader.ts";
19
19
  export {
20
+ type DidKeyVerificationMethod,
21
+ exportDidKey,
20
22
  exportMultibaseKey,
21
23
  exportSpki,
24
+ importDidKey,
22
25
  importMultibaseKey,
23
26
  importPem,
24
27
  importPkcs1,
25
28
  importSpki,
29
+ parseDidKeyVerificationMethod,
26
30
  } from "./key.ts";
27
31
  export {
28
32
  canParseDecimal,
@@ -51,8 +55,12 @@ export {
51
55
  } from "./preprocessor.ts";
52
56
  export {
53
57
  expandIPv6Address,
58
+ formatIri,
59
+ haveSameIriOrigin,
54
60
  isValidPublicIPv4Address,
55
61
  isValidPublicIPv6Address,
62
+ parseIri,
63
+ parseJsonLdId,
56
64
  UrlError,
57
65
  validatePublicUrl,
58
66
  } from "./url.ts";
package/src/url.test.ts CHANGED
@@ -1,13 +1,264 @@
1
- import { deepStrictEqual, ok, rejects } from "node:assert";
1
+ import { deepStrictEqual, ok, rejects, throws } from "node:assert";
2
2
  import { test } from "node:test";
3
3
  import {
4
4
  expandIPv6Address,
5
+ formatIri,
6
+ haveSameIriOrigin,
5
7
  isValidPublicIPv4Address,
6
8
  isValidPublicIPv6Address,
9
+ parseIri,
10
+ parseJsonLdId,
7
11
  UrlError,
8
12
  validatePublicUrl,
9
13
  } from "./url.ts";
10
14
 
15
+ test("parseIri() accepts portable ActivityPub URI schemes", () => {
16
+ const cases = [
17
+ "ap://did:key:z6Mkabc/actor",
18
+ "ap://did%3Akey%3Az6Mkabc/actor",
19
+ "ap+ef61://did:key:z6Mkabc/actor",
20
+ "ap+ef61://did%3Akey%3Az6Mkabc/actor",
21
+ "AP+EF61://did:key:z6Mkabc/actor",
22
+ ];
23
+ for (const iri of cases) {
24
+ deepStrictEqual(
25
+ parseIri(iri),
26
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor"),
27
+ );
28
+ }
29
+ });
30
+
31
+ test("parseIri() accepts DID schemes case-insensitively", () => {
32
+ const cases = [
33
+ "ap://DID:key:z6Mkabc/actor",
34
+ "ap://DID%3Akey%3Az6Mkabc/actor",
35
+ ];
36
+ for (const iri of cases) {
37
+ deepStrictEqual(
38
+ parseIri(iri),
39
+ new URL("ap+ef61://DID%3Akey%3Az6Mkabc/actor"),
40
+ );
41
+ }
42
+ });
43
+
44
+ test("parseIri() accepts DID method names that start with digits", () => {
45
+ deepStrictEqual(
46
+ parseIri("ap://did:3:abc/actor"),
47
+ new URL("ap+ef61://did%3A3%3Aabc/actor"),
48
+ );
49
+ });
50
+
51
+ test("parseIri() accepts hyphens in DID method-specific IDs", () => {
52
+ deepStrictEqual(
53
+ parseIri("ap+ef61://did:web:foo-bar.example/actor"),
54
+ new URL("ap+ef61://did%3Aweb%3Afoo-bar.example/actor"),
55
+ );
56
+ });
57
+
58
+ test("parseIri() preserves existing URL parsing behavior", () => {
59
+ deepStrictEqual(
60
+ parseIri("/actor", new URL("https://example.com/users/alice")),
61
+ new URL("https://example.com/actor"),
62
+ );
63
+ deepStrictEqual(
64
+ parseIri("at://did:plc:example/record"),
65
+ new URL("at://did%3Aplc%3Aexample/record"),
66
+ );
67
+ throws(() => parseIri("ap://not-a-did/actor"), TypeError);
68
+ });
69
+
70
+ test("parseJsonLdId() parses JSON-LD ids", () => {
71
+ deepStrictEqual(parseJsonLdId(undefined), undefined);
72
+ deepStrictEqual(parseJsonLdId("_:blank"), undefined);
73
+ deepStrictEqual(
74
+ parseJsonLdId("/actor", new URL("https://example.com/users/alice")),
75
+ new URL("https://example.com/actor"),
76
+ );
77
+ deepStrictEqual(
78
+ parseJsonLdId("ap://did:key:z6Mkabc/actor"),
79
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor"),
80
+ );
81
+ throws(() => parseJsonLdId("/actor"), {
82
+ name: "TypeError",
83
+ message: "Invalid @id: /actor",
84
+ });
85
+ });
86
+
87
+ test("parseIri() resolves relative IRIs against portable string bases", () => {
88
+ deepStrictEqual(
89
+ parseIri("/actor", "ap://did:key:z6Mkabc/objects/1"),
90
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor"),
91
+ );
92
+ deepStrictEqual(
93
+ parseIri("attachments/1", "ap://did:key:z6Mkabc/objects/1"),
94
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/objects/attachments/1"),
95
+ );
96
+ throws(
97
+ () => parseIri("//example.com/outbox", "ap://did:key:z6Mkabc/objects/1"),
98
+ TypeError,
99
+ );
100
+ });
101
+
102
+ test("parseIri() resolves relative IRIs against at:// string bases", () => {
103
+ deepStrictEqual(
104
+ parseIri("/record", "at://did:plc:example/collection/item"),
105
+ new URL("at://did%3Aplc%3Aexample/record"),
106
+ );
107
+ deepStrictEqual(
108
+ parseIri("reply", "at://did:plc:example/collection/item"),
109
+ new URL("at://did%3Aplc%3Aexample/collection/reply"),
110
+ );
111
+ deepStrictEqual(
112
+ parseIri("reply", "at://did%3Aplc%3Aexample/collection/item"),
113
+ new URL("at://did%3Aplc%3Aexample/collection/reply"),
114
+ );
115
+ });
116
+
117
+ test("parseIri() rejects portable IRIs without paths", () => {
118
+ throws(() => parseIri("ap://did:key:z6Mkabc"), TypeError);
119
+ throws(
120
+ () =>
121
+ parseIri("ap://did:key:z6Mkabc?gateways=https%3A%2F%2Fserver.example"),
122
+ TypeError,
123
+ );
124
+ throws(() => parseIri("ap://did:key:z6Mkabc#actor"), TypeError);
125
+ });
126
+
127
+ test("parseIri() rejects malformed portable DID authorities", () => {
128
+ const cases = [
129
+ "ap://did:/actor",
130
+ "ap://did:key/actor",
131
+ "ap://did%3Akey%3Aabc%25zz/actor",
132
+ "ap://did:key:abc%25zz/actor",
133
+ ];
134
+ for (const iri of cases) {
135
+ throws(() => parseIri(iri), TypeError);
136
+ }
137
+ });
138
+
139
+ test("haveSameIriOrigin() compares portable IRI authorities", () => {
140
+ ok(haveSameIriOrigin(
141
+ parseIri("ap://did:key:z6Mkabc/actor"),
142
+ parseIri("ap://did:key:z6Mkabc/outbox"),
143
+ ));
144
+ ok(haveSameIriOrigin(
145
+ new URL("ap://did%3Akey%3Az6Mkabc/actor"),
146
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/outbox"),
147
+ ));
148
+ ok(haveSameIriOrigin(
149
+ parseIri("ap://DID:key:z6Mkabc/actor"),
150
+ parseIri("ap://did:key:z6Mkabc/outbox"),
151
+ ));
152
+ ok(haveSameIriOrigin(
153
+ new URL("ap+ef61://DID%3Akey%3Az6Mkabc/actor"),
154
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/outbox"),
155
+ ));
156
+ ok(
157
+ !haveSameIriOrigin(
158
+ parseIri("ap://did:key:z6Mkabc/actor"),
159
+ parseIri("ap://did:key:z6Mkdef/actor"),
160
+ ),
161
+ );
162
+ });
163
+
164
+ test("parseIri() normalizes portable URL instances", () => {
165
+ deepStrictEqual(
166
+ parseIri(new URL("ap+ef61://did%3Aexample%3Aabc%2Fdef/actor")),
167
+ new URL("ap+ef61://did%3Aexample%3Aabc%252Fdef/actor"),
168
+ );
169
+ throws(() => parseIri("ap+ef61://not-a-did/actor"), TypeError);
170
+ });
171
+
172
+ test("formatIri() emits canonical portable ActivityPub URI syntax", () => {
173
+ const cases = [
174
+ new URL("ap://did%3Akey%3Az6Mkabc/actor"),
175
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor"),
176
+ ];
177
+ for (const iri of cases) {
178
+ deepStrictEqual(formatIri(iri), "ap+ef61://did:key:z6Mkabc/actor");
179
+ }
180
+ deepStrictEqual(
181
+ formatIri(new URL("https://example.com/actor")),
182
+ "https://example.com/actor",
183
+ );
184
+ deepStrictEqual(formatIri("/actor"), "/actor");
185
+ });
186
+
187
+ test("formatIri() preserves DID authority pct-encoded delimiters", () => {
188
+ const parsed = parseIri("ap://did:example:abc%2Fdef/actor");
189
+ deepStrictEqual(
190
+ parsed,
191
+ new URL("ap+ef61://did%3Aexample%3Aabc%252Fdef/actor"),
192
+ );
193
+ deepStrictEqual(
194
+ formatIri(parsed),
195
+ "ap+ef61://did:example:abc%2Fdef/actor",
196
+ );
197
+ deepStrictEqual(
198
+ parseIri(formatIri(parsed)),
199
+ parsed,
200
+ );
201
+ deepStrictEqual(
202
+ formatIri(new URL("ap+ef61://did%3Aexample%3Aabc%2Fdef/actor")),
203
+ "ap+ef61://did:example:abc%2Fdef/actor",
204
+ );
205
+ });
206
+
207
+ test("parseIri() normalizes equivalent encoded DID authorities", () => {
208
+ deepStrictEqual(
209
+ parseIri("ap://did:example:abc%252Fdef/actor"),
210
+ parseIri("ap://did%3Aexample%3Aabc%252Fdef/actor"),
211
+ );
212
+ });
213
+
214
+ test("formatIri() preserves DID-internal pct-encoded authority characters", () => {
215
+ const parsed = parseIri("ap://did:web:example.com%3A3000/actor");
216
+ deepStrictEqual(
217
+ parsed,
218
+ new URL("ap+ef61://did%3Aweb%3Aexample.com%253A3000/actor"),
219
+ );
220
+ deepStrictEqual(
221
+ formatIri(parsed),
222
+ "ap+ef61://did:web:example.com%3A3000/actor",
223
+ );
224
+ });
225
+
226
+ test("parseIri() accepts portable DID URLs with encoded DID delimiters", () => {
227
+ const parsed = parseIri("ap://did:web:example.com%3A3000/u/1");
228
+ deepStrictEqual(
229
+ parsed,
230
+ new URL("ap+ef61://did%3Aweb%3Aexample.com%253A3000/u/1"),
231
+ );
232
+ deepStrictEqual(
233
+ formatIri(parsed),
234
+ "ap+ef61://did:web:example.com%3A3000/u/1",
235
+ );
236
+ });
237
+
238
+ test("parseIri() decodes pct-encoded DID delimiters in order", () => {
239
+ const parsed = parseIri("ap://did%3Aweb%3Aexample.com%253A3000/u/1");
240
+ deepStrictEqual(
241
+ parsed,
242
+ new URL("ap+ef61://did%3Aweb%3Aexample.com%253A3000/u/1"),
243
+ );
244
+ deepStrictEqual(
245
+ formatIri(parsed),
246
+ "ap+ef61://did:web:example.com%3A3000/u/1",
247
+ );
248
+ });
249
+
250
+ test("parseIri() preserves encoded percent signs while decoding delimiters", () => {
251
+ const parsed = parseIri("ap://did%3Aweb%3Aexample.com%2500/u/1");
252
+ deepStrictEqual(
253
+ parsed,
254
+ new URL("ap+ef61://did%3Aweb%3Aexample.com%2500/u/1"),
255
+ );
256
+ deepStrictEqual(
257
+ formatIri(parsed),
258
+ "ap+ef61://did:web:example.com%00/u/1",
259
+ );
260
+ });
261
+
11
262
  test("validatePublicUrl()", async () => {
12
263
  await rejects(() => validatePublicUrl("ftp://localhost"), UrlError);
13
264
  await rejects(
package/src/url.ts CHANGED
@@ -9,6 +9,147 @@ export class UrlError extends Error {
9
9
  }
10
10
  }
11
11
 
12
+ const PORTABLE_IRI_PATTERN =
13
+ /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
14
+ const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
15
+ const DID_SCHEME_PATTERN = /^did:/i;
16
+ const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
17
+
18
+ /**
19
+ * Parses a JSON-LD `@id` value as an IRI.
20
+ */
21
+ export function parseJsonLdId(
22
+ id: string | undefined,
23
+ base?: string | URL,
24
+ ): URL | undefined {
25
+ if (id == null || id.startsWith("_:")) return undefined;
26
+ try {
27
+ return parseIri(id, base);
28
+ } catch {
29
+ throw new TypeError("Invalid @id: " + id);
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Parses an IRI as a URL, including FEP-ef61 portable ActivityPub IRIs.
35
+ */
36
+ export function parseIri(iri: string | URL, base?: string | URL): URL {
37
+ if (iri instanceof URL) {
38
+ return normalizePortableUrl(iri) ?? new URL(iri.href);
39
+ }
40
+ const portable = parsePortableIri(iri);
41
+ if (portable != null) return portable;
42
+ base = normalizeBaseIri(base);
43
+ if (!URL.canParse(iri, base) && iri.startsWith("at://")) {
44
+ return parseAtUri(iri);
45
+ }
46
+ const parsed = new URL(iri, base);
47
+ return normalizePortableUrl(parsed) ?? parsed;
48
+ }
49
+
50
+ /**
51
+ * Formats a URL as an IRI, including FEP-ef61 portable ActivityPub IRIs.
52
+ */
53
+ export function formatIri(iri: string | URL): string {
54
+ const parsed = parsePortableIri(iri instanceof URL ? iri.href : iri);
55
+ if (parsed == null) {
56
+ return iri instanceof URL
57
+ ? iri.href
58
+ : URL.canParse(iri)
59
+ ? new URL(iri).href
60
+ : iri;
61
+ }
62
+ const authority = decodePortableAuthority(parsed.host);
63
+ return `ap+ef61://${authority}${parsed.pathname}${parsed.search}${parsed.hash}`;
64
+ }
65
+
66
+ /**
67
+ * Checks whether two IRIs have the same origin.
68
+ */
69
+ export function haveSameIriOrigin(left: URL, right: URL): boolean {
70
+ return getComparableIriOrigin(left) === getComparableIriOrigin(right);
71
+ }
72
+
73
+ function getComparableIriOrigin(iri: URL): string {
74
+ iri = normalizePortableUrl(iri) ?? iri;
75
+ if (iri.origin !== "null") return iri.origin;
76
+ if (iri.host !== "") {
77
+ const host = iri.protocol === "ap+ef61:"
78
+ ? encodeURIComponent(
79
+ decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:"),
80
+ )
81
+ : iri.host;
82
+ return `${iri.protocol}//${host}`;
83
+ }
84
+ return iri.href;
85
+ }
86
+
87
+ function parsePortableIri(iri: string): URL | null {
88
+ const match = iri.match(PORTABLE_IRI_PATTERN);
89
+ if (match == null) return null;
90
+ // The readable ap://did:... authority form is not RFC 3986 compliant:
91
+ // colons are not valid in a URI reg-name authority. Keep accepting it for
92
+ // current FEP-ef61 interoperability, but normalize it to a percent-encoded
93
+ // URL authority internally. The ap: URI syntax may change later; see:
94
+ // https://bnewbold.leaflet.pub/3mph4hzvbdc2v
95
+ const authority = decodePortableAuthority(match[2]);
96
+ if (!DID_PATTERN.test(authority)) {
97
+ throw new TypeError("Invalid portable ActivityPub IRI authority.");
98
+ }
99
+ if (match[3] === "") {
100
+ throw new TypeError("Invalid portable ActivityPub IRI path.");
101
+ }
102
+ return new URL(
103
+ `ap+ef61://${encodeURIComponent(authority)}${match[3]}${match[4] ?? ""}${
104
+ match[5] ?? ""
105
+ }`,
106
+ );
107
+ }
108
+
109
+ function normalizePortableUrl(iri: URL): URL | null {
110
+ if (iri.protocol !== "ap:" && iri.protocol !== "ap+ef61:") return null;
111
+ return parsePortableIri(
112
+ `ap+ef61://${iri.host}${iri.pathname}${iri.search}${iri.hash}`,
113
+ );
114
+ }
115
+
116
+ function normalizeBaseIri(base?: string | URL): string | URL | undefined {
117
+ if (base == null) return undefined;
118
+ if (base instanceof URL) return normalizePortableUrl(base) ?? base;
119
+ return parsePortableIri(base) ??
120
+ (base.startsWith("at://") && !URL.canParse(".", base)
121
+ ? parseAtUri(base)
122
+ : base);
123
+ }
124
+
125
+ function decodePortableAuthority(authority: string): string {
126
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(authority)) {
127
+ throw new TypeError("Invalid portable ActivityPub IRI authority.");
128
+ }
129
+ if (DID_SCHEME_PATTERN.test(authority)) {
130
+ const decoded = authority.replace(/%25/gi, "%");
131
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) {
132
+ throw new TypeError("Invalid portable ActivityPub IRI authority.");
133
+ }
134
+ return decoded;
135
+ }
136
+ const decoded = authority.replace(
137
+ /%(25|3A)/gi,
138
+ (match) => match.toLowerCase() === "%3a" ? ":" : "%",
139
+ );
140
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) {
141
+ throw new TypeError("Invalid portable ActivityPub IRI authority.");
142
+ }
143
+ return decoded;
144
+ }
145
+
146
+ function parseAtUri(uri: string): URL {
147
+ const index = uri.indexOf("/", 5);
148
+ const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
149
+ const path = index >= 0 ? uri.slice(index) : "";
150
+ return new URL("at://" + encodeURIComponent(authority) + path);
151
+ }
152
+
12
153
  /**
13
154
  * Validates a URL to prevent SSRF attacks.
14
155
  */
package/tsdown.config.ts CHANGED
@@ -4,7 +4,12 @@ import { defineConfig } from "tsdown";
4
4
 
5
5
  export default [
6
6
  defineConfig({
7
- entry: ["src/mod.ts", "src/jsonld.ts", "src/temporal.ts"],
7
+ entry: [
8
+ "src/mod.ts",
9
+ "src/internal/jsonld-cache.ts",
10
+ "src/jsonld.ts",
11
+ "src/temporal.ts",
12
+ ],
8
13
  dts: { compilerOptions: { isolatedDeclarations: true, declaration: true } },
9
14
  format: ["esm", "cjs"],
10
15
  platform: "neutral",