@fedify/vocab-runtime 2.4.0-dev.1583 → 2.4.0-dev.1618

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/src/url.test.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import { deepStrictEqual, ok, rejects, throws } from "node:assert";
2
2
  import { test } from "node:test";
3
3
  import {
4
+ arePortableUrisEqual,
5
+ canonicalizePortableUri,
4
6
  expandIPv6Address,
5
7
  formatIri,
8
+ getFe34Origin,
9
+ haveSameFe34Origin,
6
10
  haveSameIriOrigin,
7
11
  isValidPublicIPv4Address,
8
12
  isValidPublicIPv6Address,
@@ -28,15 +32,16 @@ test("parseIri() accepts portable ActivityPub URI schemes", () => {
28
32
  }
29
33
  });
30
34
 
31
- test("parseIri() accepts DID schemes case-insensitively", () => {
35
+ test("parseIri() normalizes DID scheme and method casing", () => {
32
36
  const cases = [
33
37
  "ap://DID:key:z6Mkabc/actor",
34
38
  "ap://DID%3Akey%3Az6Mkabc/actor",
39
+ "ap://did:KEY:z6Mkabc/actor",
35
40
  ];
36
41
  for (const iri of cases) {
37
42
  deepStrictEqual(
38
43
  parseIri(iri),
39
- new URL("ap+ef61://DID%3Akey%3Az6Mkabc/actor"),
44
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor"),
40
45
  );
41
46
  }
42
47
  });
@@ -137,6 +142,14 @@ test("parseIri() rejects malformed portable DID authorities", () => {
137
142
  });
138
143
 
139
144
  test("haveSameIriOrigin() compares portable IRI authorities", () => {
145
+ ok(haveSameIriOrigin(
146
+ new URL("ftp://example.com/pub/1"),
147
+ new URL("ftp://example.com/pub/2"),
148
+ ));
149
+ ok(haveSameIriOrigin(
150
+ new URL("mailto:alice@example.com"),
151
+ new URL("mailto:alice@example.com"),
152
+ ));
140
153
  ok(haveSameIriOrigin(
141
154
  parseIri("ap://did:key:z6Mkabc/actor"),
142
155
  parseIri("ap://did:key:z6Mkabc/outbox"),
@@ -161,6 +174,122 @@ test("haveSameIriOrigin() compares portable IRI authorities", () => {
161
174
  );
162
175
  });
163
176
 
177
+ test("getFe34Origin() computes web and cryptographic origins", () => {
178
+ deepStrictEqual(
179
+ getFe34Origin("https://Example.COM:443/users/alice"),
180
+ "https://example.com",
181
+ );
182
+ deepStrictEqual(
183
+ getFe34Origin(new URL("http://example.com:8080/notes/1")),
184
+ "http://example.com:8080",
185
+ );
186
+ deepStrictEqual(
187
+ getFe34Origin(
188
+ "ap://did:key:z6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
189
+ ),
190
+ "did:key:z6Mkabc",
191
+ );
192
+ deepStrictEqual(
193
+ getFe34Origin("ap+ef61://did%3Akey%3Az6Mkabc/objects/1#fragment"),
194
+ "did:key:z6Mkabc",
195
+ );
196
+ deepStrictEqual(
197
+ getFe34Origin(new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor")),
198
+ "did:key:z6Mkabc",
199
+ );
200
+ deepStrictEqual(
201
+ getFe34Origin("did:key:z6Mkabc#z6Mkabc"),
202
+ "did:key:z6Mkabc",
203
+ );
204
+ deepStrictEqual(
205
+ getFe34Origin(new URL("did:key:z6Mkabc?service=activitypub#key")),
206
+ "did:key:z6Mkabc",
207
+ );
208
+ deepStrictEqual(
209
+ getFe34Origin("did:key:z6Mkabc/path/to/resource?service=activitypub#key"),
210
+ "did:key:z6Mkabc",
211
+ );
212
+ deepStrictEqual(
213
+ getFe34Origin("did:KEY:z6Mkabc#z6Mkabc"),
214
+ "did:key:z6Mkabc",
215
+ );
216
+ deepStrictEqual(
217
+ getFe34Origin("ap://did%3Aweb%3Afoo%2Dbar.example/actor"),
218
+ "did:web:foo-bar.example",
219
+ );
220
+ deepStrictEqual(
221
+ getFe34Origin("did:web:foo%2dbar.example#key"),
222
+ "did:web:foo-bar.example",
223
+ );
224
+ });
225
+
226
+ test("getFe34Origin() rejects unsupported or malformed identifiers", () => {
227
+ for (
228
+ const iri of [
229
+ "mailto:alice@example.com",
230
+ "ap://not-a-did/actor",
231
+ "ap://did:key/actor",
232
+ "did:key",
233
+ "did:key:",
234
+ "did:",
235
+ ]
236
+ ) {
237
+ throws(() => getFe34Origin(iri), TypeError);
238
+ }
239
+ });
240
+
241
+ test("haveSameFe34Origin() compares web and cryptographic origins", () => {
242
+ ok(haveSameFe34Origin(
243
+ "https://example.com/users/alice",
244
+ "https://example.com/notes/1",
245
+ ));
246
+ ok(
247
+ !haveSameFe34Origin(
248
+ "https://example.com/users/alice",
249
+ "http://example.com/users/alice",
250
+ ),
251
+ );
252
+ ok(
253
+ !haveSameFe34Origin(
254
+ "https://example.com/users/alice",
255
+ "https://example.com:8443/users/alice",
256
+ ),
257
+ );
258
+ ok(haveSameFe34Origin(
259
+ "ap://did:key:z6Mkabc/actor",
260
+ "ap+ef61://did%3Akey%3Az6Mkabc/objects/1",
261
+ ));
262
+ ok(haveSameFe34Origin(
263
+ "ap+ef61://did:key:z6Mkabc/actor",
264
+ "did:key:z6Mkabc#z6Mkabc",
265
+ ));
266
+ ok(haveSameFe34Origin(
267
+ "ap://did:KEY:z6Mkabc/actor",
268
+ "did:key:z6Mkabc#z6Mkabc",
269
+ ));
270
+ ok(haveSameFe34Origin(
271
+ "ap://did%3Aweb%3Afoo%2Dbar.example/actor",
272
+ "ap://did:web:foo-bar.example/note",
273
+ ));
274
+ ok(haveSameFe34Origin(
275
+ "ap://did%3Aexample%3Aabc%252fdef/actor",
276
+ "did:example:abc%2Fdef#key",
277
+ ));
278
+ ok(
279
+ !haveSameFe34Origin(
280
+ "ap+ef61://did:key:z6Mkabc/actor",
281
+ "did:key:z6Mkdef#z6Mkdef",
282
+ ),
283
+ );
284
+ ok(
285
+ !haveSameFe34Origin(
286
+ "ap+ef61://did:key:z6Mkabc/actor",
287
+ "https://example.com/actor",
288
+ ),
289
+ );
290
+ ok(!haveSameFe34Origin("ap://not-a-did/actor", "ap://not-a-did/actor"));
291
+ });
292
+
164
293
  test("parseIri() normalizes portable URL instances", () => {
165
294
  deepStrictEqual(
166
295
  parseIri(new URL("ap+ef61://did%3Aexample%3Aabc%2Fdef/actor")),
@@ -184,6 +313,256 @@ test("formatIri() emits canonical portable ActivityPub URI syntax", () => {
184
313
  deepStrictEqual(formatIri("/actor"), "/actor");
185
314
  });
186
315
 
316
+ test("canonicalizePortableUri() emits comparison forms", () => {
317
+ const cases = [
318
+ "ap://did:key:z6Mkabc/actor",
319
+ "ap://did%3Akey%3Az6Mkabc/actor",
320
+ "ap://did:key:z6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
321
+ "ap+ef61://did:key:z6Mkabc/actor",
322
+ "ap+ef61://did%3Akey%3Az6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
323
+ ];
324
+ for (const iri of cases) {
325
+ deepStrictEqual(
326
+ canonicalizePortableUri(iri),
327
+ "ap+ef61://did:key:z6Mkabc/actor",
328
+ );
329
+ }
330
+ });
331
+
332
+ test("canonicalizePortableUri() preserves paths and fragments", () => {
333
+ deepStrictEqual(
334
+ canonicalizePortableUri(
335
+ "ap://did:key:z6Mkabc/objects/1/attachments/2?gateways=https%3A%2F%2Fa.example#image",
336
+ ),
337
+ "ap+ef61://did:key:z6Mkabc/objects/1/attachments/2#image",
338
+ );
339
+ deepStrictEqual(
340
+ canonicalizePortableUri("ap://did:key:z6Mkabc/objects/1#reply"),
341
+ "ap+ef61://did:key:z6Mkabc/objects/1#reply",
342
+ );
343
+ });
344
+
345
+ test("canonicalizePortableUri() preserves DID-internal pct-encoded characters", () => {
346
+ deepStrictEqual(
347
+ canonicalizePortableUri("ap://did:example:abc%2Fdef/actor?x=1"),
348
+ "ap+ef61://did:example:abc%2Fdef/actor",
349
+ );
350
+ deepStrictEqual(
351
+ canonicalizePortableUri("ap://did%3Aweb%3Aexample.com%253A3000/u/1"),
352
+ "ap+ef61://did:web:example.com%3A3000/u/1",
353
+ );
354
+ });
355
+
356
+ test("canonicalizePortableUri() normalizes authority pct-encoding casing", () => {
357
+ deepStrictEqual(
358
+ canonicalizePortableUri("ap://did:example:abc%2fdef/actor"),
359
+ "ap+ef61://did:example:abc%2Fdef/actor",
360
+ );
361
+ deepStrictEqual(
362
+ canonicalizePortableUri("ap://did:example:abc%2fdef%3abar/actor"),
363
+ "ap+ef61://did:example:abc%2Fdef%3Abar/actor",
364
+ );
365
+ deepStrictEqual(
366
+ canonicalizePortableUri("ap://did%3Aexample%3Aabc%252fdef/actor"),
367
+ "ap+ef61://did:example:abc%2Fdef/actor",
368
+ );
369
+ deepStrictEqual(
370
+ canonicalizePortableUri("ap://did%3Akey%3Az6Mk%2Dabc/actor"),
371
+ "ap+ef61://did:key:z6Mk-abc/actor",
372
+ );
373
+ ok(arePortableUrisEqual(
374
+ "ap://did:example:abc%2fdef/actor",
375
+ "ap://did:example:abc%2Fdef/actor",
376
+ ));
377
+ ok(arePortableUrisEqual(
378
+ "ap://did%3Akey%3Az6Mk%2Dabc/actor",
379
+ "ap://did:key:z6Mk-abc/actor",
380
+ ));
381
+ });
382
+
383
+ test("canonicalizePortableUri() normalizes path and fragment pct-encoding casing", () => {
384
+ deepStrictEqual(
385
+ canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2fprofile#part%2ftwo"),
386
+ "ap+ef61://did:key:z6Mkabc/actor%2Fprofile#part%2Ftwo",
387
+ );
388
+ deepStrictEqual(
389
+ canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2fprofile%3aimage"),
390
+ "ap+ef61://did:key:z6Mkabc/actor%2Fprofile%3Aimage",
391
+ );
392
+ deepStrictEqual(
393
+ canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2Dprofile#part%7Etwo"),
394
+ "ap+ef61://did:key:z6Mkabc/actor-profile#part~two",
395
+ );
396
+ ok(arePortableUrisEqual(
397
+ "ap://did:key:z6Mkabc/actor%2fprofile#part%2ftwo",
398
+ "ap://did:key:z6Mkabc/actor%2Fprofile#part%2Ftwo",
399
+ ));
400
+ ok(arePortableUrisEqual(
401
+ "ap://did:key:z6Mkabc/actor%2Dprofile#part%7Etwo",
402
+ "ap://did:key:z6Mkabc/actor-profile#part~two",
403
+ ));
404
+ });
405
+
406
+ test("canonicalizePortableUri() encodes raw path and fragment characters", () => {
407
+ deepStrictEqual(
408
+ canonicalizePortableUri("ap://did:key:z6Mkabc/\u00e9#\u00e9"),
409
+ "ap+ef61://did:key:z6Mkabc/%C3%A9#%C3%A9",
410
+ );
411
+ ok(arePortableUrisEqual(
412
+ "ap://did:key:z6Mkabc/\u00e9#\u00e9",
413
+ "ap://did:key:z6Mkabc/%C3%A9#%C3%A9",
414
+ ));
415
+ });
416
+
417
+ test("canonicalizePortableUri() normalizes DID scheme casing", () => {
418
+ deepStrictEqual(
419
+ canonicalizePortableUri("ap://DID:key:z6Mkabc/actor"),
420
+ "ap+ef61://did:key:z6Mkabc/actor",
421
+ );
422
+ deepStrictEqual(
423
+ canonicalizePortableUri("ap://DID%3Akey%3Az6Mkabc/actor"),
424
+ "ap+ef61://did:key:z6Mkabc/actor",
425
+ );
426
+ });
427
+
428
+ test("canonicalizePortableUri() preserves opaque path segments", () => {
429
+ deepStrictEqual(
430
+ canonicalizePortableUri("ap://did:key:z6Mkabc/a/../b?gateways=x"),
431
+ "ap+ef61://did:key:z6Mkabc/a/../b",
432
+ );
433
+ deepStrictEqual(
434
+ canonicalizePortableUri("ap://did:key:z6Mkabc/a/%2e%2e/b"),
435
+ "ap+ef61://did:key:z6Mkabc/a/../b",
436
+ );
437
+ ok(
438
+ !arePortableUrisEqual(
439
+ "ap://did:key:z6Mkabc/a/../b",
440
+ "ap://did:key:z6Mkabc/b",
441
+ ),
442
+ );
443
+ ok(
444
+ !arePortableUrisEqual(
445
+ "ap://did:key:z6Mkabc/a/%2e%2e/b",
446
+ "ap://did:key:z6Mkabc/b",
447
+ ),
448
+ );
449
+ });
450
+
451
+ test("canonicalizePortableUri() rejects non-portable URIs", () => {
452
+ const cases = [
453
+ "https://example.com/actor",
454
+ "at://did:plc:example/record",
455
+ "/actor",
456
+ "ap://not-a-did/actor",
457
+ "ap://did:key:z6Mkabc",
458
+ ];
459
+ for (const iri of cases) {
460
+ throws(() => canonicalizePortableUri(iri), TypeError);
461
+ }
462
+ throws(
463
+ () =>
464
+ canonicalizePortableUri(
465
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor") as unknown as string,
466
+ ),
467
+ TypeError,
468
+ );
469
+ });
470
+
471
+ test("canonicalizePortableUri() rejects invalid path and fragment pct-encoding", () => {
472
+ throws(() => canonicalizePortableUri("ap://did:key:z6Mkabc/a%zz"), TypeError);
473
+ throws(
474
+ () => canonicalizePortableUri("ap://did:key:z6Mkabc/actor#part%zz"),
475
+ TypeError,
476
+ );
477
+ throws(
478
+ () => canonicalizePortableUri("ap://did:key:z6Mkabc/\ud800"),
479
+ TypeError,
480
+ );
481
+ throws(
482
+ () => canonicalizePortableUri("ap://did:key:z6Mkabc/actor#\ud800"),
483
+ TypeError,
484
+ );
485
+ });
486
+
487
+ test("arePortableUrisEqual() compares canonical portable URI forms", () => {
488
+ ok(arePortableUrisEqual(
489
+ "ap://did:key:z6Mkabc/actor",
490
+ "ap+ef61://did%3Akey%3Az6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
491
+ ));
492
+ ok(arePortableUrisEqual(
493
+ "ap://DID:key:z6Mkabc/actor",
494
+ "ap://did:key:z6Mkabc/actor",
495
+ ));
496
+ ok(
497
+ !arePortableUrisEqual(
498
+ "ap://did:key:z6Mkabc/actor",
499
+ "ap://did:key:z6Mkdef/actor",
500
+ ),
501
+ );
502
+ ok(
503
+ !arePortableUrisEqual(
504
+ "ap://did:key:z6Mkabc/actor",
505
+ "ap://did:key:z6Mkabc/outbox",
506
+ ),
507
+ );
508
+ ok(
509
+ !arePortableUrisEqual(
510
+ "ap://did:key:z6Mkabc/actor#one",
511
+ "ap://did:key:z6Mkabc/actor#two",
512
+ ),
513
+ );
514
+ });
515
+
516
+ test("arePortableUrisEqual() handles non-portable URI strings", () => {
517
+ ok(arePortableUrisEqual(
518
+ "https://example.com/actor",
519
+ "https://example.com/actor",
520
+ ));
521
+ ok(
522
+ !arePortableUrisEqual(
523
+ "https://example.com/actor",
524
+ "https://example.com/outbox",
525
+ ),
526
+ );
527
+ ok(
528
+ !arePortableUrisEqual(
529
+ "ap://did:key:z6Mkabc/actor",
530
+ "https://example.com/actor",
531
+ ),
532
+ );
533
+ ok(arePortableUrisEqual("ap://not-a-did/actor", "ap://not-a-did/actor"));
534
+ ok(
535
+ !arePortableUrisEqual(
536
+ "ap://not-a-did/actor",
537
+ "ap://not-a-did/outbox",
538
+ ),
539
+ );
540
+ ok(
541
+ !arePortableUrisEqual(
542
+ "ap://not-a-did/actor",
543
+ "ap://did:key:z6Mkabc/actor",
544
+ ),
545
+ );
546
+ ok(
547
+ !arePortableUrisEqual(
548
+ "ap://did:key:z6Mkabc/a%zz",
549
+ "ap://did:key:z6Mkabc/a%25zz",
550
+ ),
551
+ );
552
+ ok(
553
+ !arePortableUrisEqual(
554
+ "ap://did:key:z6Mkabc/\ud800",
555
+ "ap://did:key:z6Mkabc/%EF%BF%BD",
556
+ ),
557
+ );
558
+ ok(
559
+ !arePortableUrisEqual(
560
+ "ap://did:key:z6Mkabc/actor#\ud800",
561
+ "ap://did:key:z6Mkabc/actor#%EF%BF%BD",
562
+ ),
563
+ );
564
+ });
565
+
187
566
  test("formatIri() preserves DID authority pct-encoded delimiters", () => {
188
567
  const parsed = parseIri("ap://did:example:abc%2Fdef/actor");
189
568
  deepStrictEqual(
package/src/url.ts CHANGED
@@ -12,6 +12,7 @@ export class UrlError extends Error {
12
12
  const PORTABLE_IRI_PATTERN =
13
13
  /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
14
14
  const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
15
+ const PERCENT_ENCODING_PATTERN = /%[0-9A-Fa-f]{2}/g;
15
16
  const DID_SCHEME_PATTERN = /^did:/i;
16
17
  const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
17
18
 
@@ -63,6 +64,123 @@ export function formatIri(iri: string | URL): string {
63
64
  return `ap+ef61://${authority}${parsed.pathname}${parsed.search}${parsed.hash}`;
64
65
  }
65
66
 
67
+ /**
68
+ * Canonicalizes a FEP-ef61 portable ActivityPub URI for comparison.
69
+ *
70
+ * This accepts both `ap:` and `ap+ef61:` URI strings with decoded or
71
+ * percent-encoded DID authorities. The returned value uses the `ap+ef61:`
72
+ * scheme, a decoded DID authority, and no query component. Pass the raw URI
73
+ * string, not a `URL` object, because JavaScript `URL` normalizes opaque path
74
+ * segments before Fedify can compare them.
75
+ *
76
+ * @param input The raw portable ActivityPub URI string to canonicalize.
77
+ * @returns The canonical portable ActivityPub URI string.
78
+ * @throws {TypeError} If the input is not a valid portable ActivityPub IRI.
79
+ * @since 2.4.0
80
+ */
81
+ export function canonicalizePortableUri(input: string): string {
82
+ if (typeof input !== "string") {
83
+ throw new TypeError("Invalid portable ActivityPub IRI.");
84
+ }
85
+ const parsed = parsePortableIri(input);
86
+ if (parsed == null) {
87
+ throw new TypeError("Invalid portable ActivityPub IRI.");
88
+ }
89
+ const match = input.match(PORTABLE_IRI_PATTERN)!;
90
+ // parsePortableIri() validates the value but returns a URL, which normalizes
91
+ // opaque path segments. Use the raw match for path and fragment comparison.
92
+ // parsed.host is the encodeURIComponent() output from parsePortableIri(), so
93
+ // decodePortableAuthority() reverses the shared percent-encoded authority
94
+ // path here rather than the raw did:-prefixed branch.
95
+ const authority = normalizePortableAuthority(
96
+ getDidUrlOrigin(decodePortableAuthority(parsed.host)),
97
+ );
98
+ // Keep path and fragment text from the raw match to avoid URL dot-segment
99
+ // normalization, but still encode raw characters and normalize
100
+ // percent-escape hex casing per URI comparison rules.
101
+ const path = normalizePortableComponent(match[3]);
102
+ const fragment = match[5] == null ? "" : normalizePortableComponent(match[5]);
103
+ return `ap+ef61://${authority}${path}${fragment}`;
104
+ }
105
+
106
+ /**
107
+ * Checks whether two FEP-ef61 portable ActivityPub URIs identify the same
108
+ * portable object.
109
+ *
110
+ * Non-string inputs return `false`. Non-portable URI strings use strict string
111
+ * equality. Portable URI strings are compared through
112
+ * {@link canonicalizePortableUri}; malformed portable URI strings return
113
+ * `false` unless they are exactly equal.
114
+ *
115
+ * @since 2.4.0
116
+ */
117
+ export function arePortableUrisEqual(
118
+ left: string,
119
+ right: string,
120
+ ): boolean {
121
+ if (typeof left !== "string" || typeof right !== "string") return false;
122
+ if (left === right) return true;
123
+ if (!PORTABLE_IRI_PATTERN.test(left) || !PORTABLE_IRI_PATTERN.test(right)) {
124
+ return false;
125
+ }
126
+ try {
127
+ return canonicalizePortableUri(left) === canonicalizePortableUri(right);
128
+ } catch (error) {
129
+ if (error instanceof TypeError) return false;
130
+ throw error;
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Computes an IRI's FEP-fe34 origin.
136
+ *
137
+ * HTTP(S) IRIs use their web origin. FEP-ef61 portable ActivityPub IRIs and
138
+ * DID URLs use their DID as a cryptographic origin.
139
+ *
140
+ * @throws {TypeError} If the IRI does not have a supported FEP-fe34 origin.
141
+ * @since 2.4.0
142
+ */
143
+ export function getFe34Origin(input: string | URL): string {
144
+ if (input instanceof URL) {
145
+ const portable = normalizePortableUrl(input);
146
+ if (portable != null) return getPortableCryptographicOrigin(portable);
147
+ if (input.protocol === "did:") return getDidUrlOrigin(input.href);
148
+ if (input.protocol === "http:" || input.protocol === "https:") {
149
+ return input.origin;
150
+ }
151
+ throw new TypeError("Unsupported FEP-fe34 origin IRI.");
152
+ }
153
+
154
+ const portable = parsePortableIri(input);
155
+ if (portable != null) return getPortableCryptographicOrigin(portable);
156
+ if (DID_SCHEME_PATTERN.test(input)) return getDidUrlOrigin(input);
157
+
158
+ const parsed = new URL(input);
159
+ if (parsed.protocol === "http:" || parsed.protocol === "https:") {
160
+ return parsed.origin;
161
+ }
162
+ throw new TypeError("Unsupported FEP-fe34 origin IRI.");
163
+ }
164
+
165
+ /**
166
+ * Checks whether two IRIs have the same FEP-fe34 origin.
167
+ *
168
+ * Malformed or unsupported IRIs are treated as non-matching.
169
+ *
170
+ * @since 2.4.0
171
+ */
172
+ export function haveSameFe34Origin(
173
+ left: string | URL,
174
+ right: string | URL,
175
+ ): boolean {
176
+ try {
177
+ return getFe34Origin(left) === getFe34Origin(right);
178
+ } catch (error) {
179
+ if (error instanceof TypeError) return false;
180
+ throw error;
181
+ }
182
+ }
183
+
66
184
  /**
67
185
  * Checks whether two IRIs have the same origin.
68
186
  */
@@ -76,7 +194,7 @@ function getComparableIriOrigin(iri: URL): string {
76
194
  if (iri.host !== "") {
77
195
  const host = iri.protocol === "ap+ef61:"
78
196
  ? encodeURIComponent(
79
- decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:"),
197
+ getDidUrlOrigin(decodePortableAuthority(iri.host)),
80
198
  )
81
199
  : iri.host;
82
200
  return `${iri.protocol}//${host}`;
@@ -84,6 +202,18 @@ function getComparableIriOrigin(iri: URL): string {
84
202
  return iri.href;
85
203
  }
86
204
 
205
+ function getPortableCryptographicOrigin(iri: URL): string {
206
+ return getDidUrlOrigin(decodePortableAuthority(iri.host));
207
+ }
208
+
209
+ function getDidUrlOrigin(iri: string): string {
210
+ const did = iri.split(/[/?#]/, 1)[0].replace(DID_SCHEME_PATTERN, "did:");
211
+ if (!DID_PATTERN.test(did)) throw new TypeError("Invalid DID URL.");
212
+ const parts = did.split(":");
213
+ parts[1] = parts[1].toLowerCase();
214
+ return normalizePortableAuthority(parts.join(":"));
215
+ }
216
+
87
217
  function parsePortableIri(iri: string): URL | null {
88
218
  const match = iri.match(PORTABLE_IRI_PATTERN);
89
219
  if (match == null) return null;
@@ -92,7 +222,7 @@ function parsePortableIri(iri: string): URL | null {
92
222
  // current FEP-ef61 interoperability, but normalize it to a percent-encoded
93
223
  // URL authority internally. The ap: URI syntax may change later; see:
94
224
  // https://bnewbold.leaflet.pub/3mph4hzvbdc2v
95
- const authority = decodePortableAuthority(match[2]);
225
+ const authority = getDidUrlOrigin(decodePortableAuthority(match[2]));
96
226
  if (!DID_PATTERN.test(authority)) {
97
227
  throw new TypeError("Invalid portable ActivityPub IRI authority.");
98
228
  }
@@ -143,6 +273,49 @@ function decodePortableAuthority(authority: string): string {
143
273
  return decoded;
144
274
  }
145
275
 
276
+ function normalizePercentEncoding(value: string): string {
277
+ return value.replace(
278
+ PERCENT_ENCODING_PATTERN,
279
+ (match) => match.toUpperCase(),
280
+ );
281
+ }
282
+
283
+ function normalizePortableAuthority(authority: string): string {
284
+ return normalizePercentEncoding(authority).replace(
285
+ PERCENT_ENCODING_PATTERN,
286
+ (match) => {
287
+ const decoded = String.fromCharCode(Number.parseInt(match.slice(1), 16));
288
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : match;
289
+ },
290
+ );
291
+ }
292
+
293
+ function normalizePortableComponent(value: string): string {
294
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(value)) {
295
+ throw new TypeError("Invalid portable ActivityPub IRI component.");
296
+ }
297
+ return value.replace(
298
+ /%[0-9A-Fa-f]{2}|[^%]+/g,
299
+ (match) => {
300
+ if (match.startsWith("%")) {
301
+ const upper = match.toUpperCase();
302
+ const decoded = String.fromCharCode(
303
+ Number.parseInt(upper.slice(1), 16),
304
+ );
305
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : upper;
306
+ }
307
+ try {
308
+ return encodeURI(match);
309
+ } catch (error) {
310
+ if (error instanceof URIError) {
311
+ throw new TypeError("Invalid portable ActivityPub IRI component.");
312
+ }
313
+ throw error;
314
+ }
315
+ },
316
+ );
317
+ }
318
+
146
319
  function parseAtUri(uri: string): URL {
147
320
  const index = uri.indexOf("/", 5);
148
321
  const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);