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

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.
@@ -1,4 +1,4 @@
1
- import { a as isValidPublicIPv4Address, c as parseJsonLdId, i as haveSameIriOrigin, l as validatePublicUrl, n as expandIPv6Address, o as isValidPublicIPv6Address, r as formatIri, s as parseIri, t as UrlError } from "./url-YWJbnRlf.mjs";
1
+ import { a as formatIri, c as isValidPublicIPv6Address, d as validatePublicUrl, i as expandIPv6Address, l as parseIri, n as arePortableUrisEqual, o as haveSameIriOrigin, r as canonicalizePortableUri, s as isValidPublicIPv4Address, t as UrlError, u as parseJsonLdId } from "./url-D7ay_5pk.mjs";
2
2
  import { deepStrictEqual, ok, rejects, throws } from "node:assert";
3
3
  import { test } from "node:test";
4
4
  //#region src/url.test.ts
@@ -75,6 +75,86 @@ test("formatIri() emits canonical portable ActivityPub URI syntax", () => {
75
75
  deepStrictEqual(formatIri(new URL("https://example.com/actor")), "https://example.com/actor");
76
76
  deepStrictEqual(formatIri("/actor"), "/actor");
77
77
  });
78
+ test("canonicalizePortableUri() emits comparison forms", () => {
79
+ for (const iri of [
80
+ "ap://did:key:z6Mkabc/actor",
81
+ "ap://did%3Akey%3Az6Mkabc/actor",
82
+ "ap://did:key:z6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
83
+ "ap+ef61://did:key:z6Mkabc/actor",
84
+ "ap+ef61://did%3Akey%3Az6Mkabc/actor?gateways=https%3A%2F%2Fa.example"
85
+ ]) deepStrictEqual(canonicalizePortableUri(iri), "ap+ef61://did:key:z6Mkabc/actor");
86
+ });
87
+ test("canonicalizePortableUri() preserves paths and fragments", () => {
88
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/objects/1/attachments/2?gateways=https%3A%2F%2Fa.example#image"), "ap+ef61://did:key:z6Mkabc/objects/1/attachments/2#image");
89
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/objects/1#reply"), "ap+ef61://did:key:z6Mkabc/objects/1#reply");
90
+ });
91
+ test("canonicalizePortableUri() preserves DID-internal pct-encoded characters", () => {
92
+ deepStrictEqual(canonicalizePortableUri("ap://did:example:abc%2Fdef/actor?x=1"), "ap+ef61://did:example:abc%2Fdef/actor");
93
+ deepStrictEqual(canonicalizePortableUri("ap://did%3Aweb%3Aexample.com%253A3000/u/1"), "ap+ef61://did:web:example.com%3A3000/u/1");
94
+ });
95
+ test("canonicalizePortableUri() normalizes authority pct-encoding casing", () => {
96
+ deepStrictEqual(canonicalizePortableUri("ap://did:example:abc%2fdef/actor"), "ap+ef61://did:example:abc%2Fdef/actor");
97
+ deepStrictEqual(canonicalizePortableUri("ap://did:example:abc%2fdef%3abar/actor"), "ap+ef61://did:example:abc%2Fdef%3Abar/actor");
98
+ deepStrictEqual(canonicalizePortableUri("ap://did%3Aexample%3Aabc%252fdef/actor"), "ap+ef61://did:example:abc%2Fdef/actor");
99
+ deepStrictEqual(canonicalizePortableUri("ap://did%3Akey%3Az6Mk%2Dabc/actor"), "ap+ef61://did:key:z6Mk-abc/actor");
100
+ ok(arePortableUrisEqual("ap://did:example:abc%2fdef/actor", "ap://did:example:abc%2Fdef/actor"));
101
+ ok(arePortableUrisEqual("ap://did%3Akey%3Az6Mk%2Dabc/actor", "ap://did:key:z6Mk-abc/actor"));
102
+ });
103
+ test("canonicalizePortableUri() normalizes path and fragment pct-encoding casing", () => {
104
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2fprofile#part%2ftwo"), "ap+ef61://did:key:z6Mkabc/actor%2Fprofile#part%2Ftwo");
105
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2fprofile%3aimage"), "ap+ef61://did:key:z6Mkabc/actor%2Fprofile%3Aimage");
106
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2Dprofile#part%7Etwo"), "ap+ef61://did:key:z6Mkabc/actor-profile#part~two");
107
+ ok(arePortableUrisEqual("ap://did:key:z6Mkabc/actor%2fprofile#part%2ftwo", "ap://did:key:z6Mkabc/actor%2Fprofile#part%2Ftwo"));
108
+ ok(arePortableUrisEqual("ap://did:key:z6Mkabc/actor%2Dprofile#part%7Etwo", "ap://did:key:z6Mkabc/actor-profile#part~two"));
109
+ });
110
+ test("canonicalizePortableUri() encodes raw path and fragment characters", () => {
111
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/é#é"), "ap+ef61://did:key:z6Mkabc/%C3%A9#%C3%A9");
112
+ ok(arePortableUrisEqual("ap://did:key:z6Mkabc/é#é", "ap://did:key:z6Mkabc/%C3%A9#%C3%A9"));
113
+ });
114
+ test("canonicalizePortableUri() normalizes DID scheme casing", () => {
115
+ deepStrictEqual(canonicalizePortableUri("ap://DID:key:z6Mkabc/actor"), "ap+ef61://did:key:z6Mkabc/actor");
116
+ deepStrictEqual(canonicalizePortableUri("ap://DID%3Akey%3Az6Mkabc/actor"), "ap+ef61://did:key:z6Mkabc/actor");
117
+ });
118
+ test("canonicalizePortableUri() preserves opaque path segments", () => {
119
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/a/../b?gateways=x"), "ap+ef61://did:key:z6Mkabc/a/../b");
120
+ deepStrictEqual(canonicalizePortableUri("ap://did:key:z6Mkabc/a/%2e%2e/b"), "ap+ef61://did:key:z6Mkabc/a/../b");
121
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/a/../b", "ap://did:key:z6Mkabc/b"));
122
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/a/%2e%2e/b", "ap://did:key:z6Mkabc/b"));
123
+ });
124
+ test("canonicalizePortableUri() rejects non-portable URIs", () => {
125
+ for (const iri of [
126
+ "https://example.com/actor",
127
+ "at://did:plc:example/record",
128
+ "/actor",
129
+ "ap://not-a-did/actor",
130
+ "ap://did:key:z6Mkabc"
131
+ ]) throws(() => canonicalizePortableUri(iri), TypeError);
132
+ throws(() => canonicalizePortableUri(new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor")), TypeError);
133
+ });
134
+ test("canonicalizePortableUri() rejects invalid path and fragment pct-encoding", () => {
135
+ throws(() => canonicalizePortableUri("ap://did:key:z6Mkabc/a%zz"), TypeError);
136
+ throws(() => canonicalizePortableUri("ap://did:key:z6Mkabc/actor#part%zz"), TypeError);
137
+ throws(() => canonicalizePortableUri("ap://did:key:z6Mkabc/\ud800"), TypeError);
138
+ throws(() => canonicalizePortableUri("ap://did:key:z6Mkabc/actor#\ud800"), TypeError);
139
+ });
140
+ test("arePortableUrisEqual() compares canonical portable URI forms", () => {
141
+ ok(arePortableUrisEqual("ap://did:key:z6Mkabc/actor", "ap+ef61://did%3Akey%3Az6Mkabc/actor?gateways=https%3A%2F%2Fa.example"));
142
+ ok(arePortableUrisEqual("ap://DID:key:z6Mkabc/actor", "ap://did:key:z6Mkabc/actor"));
143
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/actor", "ap://did:key:z6Mkdef/actor"));
144
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/actor", "ap://did:key:z6Mkabc/outbox"));
145
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/actor#one", "ap://did:key:z6Mkabc/actor#two"));
146
+ });
147
+ test("arePortableUrisEqual() handles non-portable URI strings", () => {
148
+ ok(arePortableUrisEqual("https://example.com/actor", "https://example.com/actor"));
149
+ ok(!arePortableUrisEqual("https://example.com/actor", "https://example.com/outbox"));
150
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/actor", "https://example.com/actor"));
151
+ ok(arePortableUrisEqual("ap://not-a-did/actor", "ap://not-a-did/actor"));
152
+ ok(!arePortableUrisEqual("ap://not-a-did/actor", "ap://not-a-did/outbox"));
153
+ ok(!arePortableUrisEqual("ap://not-a-did/actor", "ap://did:key:z6Mkabc/actor"));
154
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/a%zz", "ap://did:key:z6Mkabc/a%25zz"));
155
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/\ud800", "ap://did:key:z6Mkabc/%EF%BF%BD"));
156
+ ok(!arePortableUrisEqual("ap://did:key:z6Mkabc/actor#\ud800", "ap://did:key:z6Mkabc/actor#%EF%BF%BD"));
157
+ });
78
158
  test("formatIri() preserves DID authority pct-encoded delimiters", () => {
79
159
  const parsed = parseIri("ap://did:example:abc%2Fdef/actor");
80
160
  deepStrictEqual(parsed, new URL("ap+ef61://did%3Aexample%3Aabc%252Fdef/actor"));
@@ -11,6 +11,7 @@ var UrlError = class extends Error {
11
11
  };
12
12
  const PORTABLE_IRI_PATTERN = /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
13
13
  const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
14
+ const PERCENT_ENCODING_PATTERN = /%[0-9A-Fa-f]{2}/g;
14
15
  const DID_SCHEME_PATTERN = /^did:/i;
15
16
  const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
16
17
  /**
@@ -45,6 +46,49 @@ function formatIri(iri) {
45
46
  return `ap+ef61://${decodePortableAuthority(parsed.host)}${parsed.pathname}${parsed.search}${parsed.hash}`;
46
47
  }
47
48
  /**
49
+ * Canonicalizes a FEP-ef61 portable ActivityPub URI for comparison.
50
+ *
51
+ * This accepts both `ap:` and `ap+ef61:` URI strings with decoded or
52
+ * percent-encoded DID authorities. The returned value uses the `ap+ef61:`
53
+ * scheme, a decoded DID authority, and no query component. Pass the raw URI
54
+ * string, not a `URL` object, because JavaScript `URL` normalizes opaque path
55
+ * segments before Fedify can compare them.
56
+ *
57
+ * @param input The raw portable ActivityPub URI string to canonicalize.
58
+ * @returns The canonical portable ActivityPub URI string.
59
+ * @throws {TypeError} If the input is not a valid portable ActivityPub IRI.
60
+ * @since 2.4.0
61
+ */
62
+ function canonicalizePortableUri(input) {
63
+ if (typeof input !== "string") throw new TypeError("Invalid portable ActivityPub IRI.");
64
+ const parsed = parsePortableIri(input);
65
+ if (parsed == null) throw new TypeError("Invalid portable ActivityPub IRI.");
66
+ const match = input.match(PORTABLE_IRI_PATTERN);
67
+ return `ap+ef61://${normalizePortableAuthority(decodePortableAuthority(parsed.host).replace(DID_SCHEME_PATTERN, "did:"))}${normalizePortableComponent(match[3])}${match[5] == null ? "" : normalizePortableComponent(match[5])}`;
68
+ }
69
+ /**
70
+ * Checks whether two FEP-ef61 portable ActivityPub URIs identify the same
71
+ * portable object.
72
+ *
73
+ * Non-string inputs return `false`. Non-portable URI strings use strict string
74
+ * equality. Portable URI strings are compared through
75
+ * {@link canonicalizePortableUri}; malformed portable URI strings return
76
+ * `false` unless they are exactly equal.
77
+ *
78
+ * @since 2.4.0
79
+ */
80
+ function arePortableUrisEqual(left, right) {
81
+ if (typeof left !== "string" || typeof right !== "string") return false;
82
+ if (left === right) return true;
83
+ if (!PORTABLE_IRI_PATTERN.test(left) || !PORTABLE_IRI_PATTERN.test(right)) return false;
84
+ try {
85
+ return canonicalizePortableUri(left) === canonicalizePortableUri(right);
86
+ } catch (error) {
87
+ if (error instanceof TypeError) return false;
88
+ throw error;
89
+ }
90
+ }
91
+ /**
48
92
  * Checks whether two IRIs have the same origin.
49
93
  */
50
94
  function haveSameIriOrigin(left, right) {
@@ -87,6 +131,31 @@ function decodePortableAuthority(authority) {
87
131
  if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
88
132
  return decoded;
89
133
  }
134
+ function normalizePercentEncoding(value) {
135
+ return value.replace(PERCENT_ENCODING_PATTERN, (match) => match.toUpperCase());
136
+ }
137
+ function normalizePortableAuthority(authority) {
138
+ return normalizePercentEncoding(authority).replace(PERCENT_ENCODING_PATTERN, (match) => {
139
+ const decoded = String.fromCharCode(Number.parseInt(match.slice(1), 16));
140
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : match;
141
+ });
142
+ }
143
+ function normalizePortableComponent(value) {
144
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(value)) throw new TypeError("Invalid portable ActivityPub IRI component.");
145
+ return value.replace(/%[0-9A-Fa-f]{2}|[^%]+/g, (match) => {
146
+ if (match.startsWith("%")) {
147
+ const upper = match.toUpperCase();
148
+ const decoded = String.fromCharCode(Number.parseInt(upper.slice(1), 16));
149
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : upper;
150
+ }
151
+ try {
152
+ return encodeURI(match);
153
+ } catch (error) {
154
+ if (error instanceof URIError) throw new TypeError("Invalid portable ActivityPub IRI component.");
155
+ throw error;
156
+ }
157
+ });
158
+ }
90
159
  function parseAtUri(uri) {
91
160
  const index = uri.indexOf("/", 5);
92
161
  const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
@@ -265,6 +334,18 @@ Object.defineProperty(exports, "UrlError", {
265
334
  return UrlError;
266
335
  }
267
336
  });
337
+ Object.defineProperty(exports, "arePortableUrisEqual", {
338
+ enumerable: true,
339
+ get: function() {
340
+ return arePortableUrisEqual;
341
+ }
342
+ });
343
+ Object.defineProperty(exports, "canonicalizePortableUri", {
344
+ enumerable: true,
345
+ get: function() {
346
+ return canonicalizePortableUri;
347
+ }
348
+ });
268
349
  Object.defineProperty(exports, "expandIPv6Address", {
269
350
  enumerable: true,
270
351
  get: function() {
@@ -10,6 +10,7 @@ var UrlError = class extends Error {
10
10
  };
11
11
  const PORTABLE_IRI_PATTERN = /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
12
12
  const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
13
+ const PERCENT_ENCODING_PATTERN = /%[0-9A-Fa-f]{2}/g;
13
14
  const DID_SCHEME_PATTERN = /^did:/i;
14
15
  const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
15
16
  /**
@@ -44,6 +45,49 @@ function formatIri(iri) {
44
45
  return `ap+ef61://${decodePortableAuthority(parsed.host)}${parsed.pathname}${parsed.search}${parsed.hash}`;
45
46
  }
46
47
  /**
48
+ * Canonicalizes a FEP-ef61 portable ActivityPub URI for comparison.
49
+ *
50
+ * This accepts both `ap:` and `ap+ef61:` URI strings with decoded or
51
+ * percent-encoded DID authorities. The returned value uses the `ap+ef61:`
52
+ * scheme, a decoded DID authority, and no query component. Pass the raw URI
53
+ * string, not a `URL` object, because JavaScript `URL` normalizes opaque path
54
+ * segments before Fedify can compare them.
55
+ *
56
+ * @param input The raw portable ActivityPub URI string to canonicalize.
57
+ * @returns The canonical portable ActivityPub URI string.
58
+ * @throws {TypeError} If the input is not a valid portable ActivityPub IRI.
59
+ * @since 2.4.0
60
+ */
61
+ function canonicalizePortableUri(input) {
62
+ if (typeof input !== "string") throw new TypeError("Invalid portable ActivityPub IRI.");
63
+ const parsed = parsePortableIri(input);
64
+ if (parsed == null) throw new TypeError("Invalid portable ActivityPub IRI.");
65
+ const match = input.match(PORTABLE_IRI_PATTERN);
66
+ return `ap+ef61://${normalizePortableAuthority(decodePortableAuthority(parsed.host).replace(DID_SCHEME_PATTERN, "did:"))}${normalizePortableComponent(match[3])}${match[5] == null ? "" : normalizePortableComponent(match[5])}`;
67
+ }
68
+ /**
69
+ * Checks whether two FEP-ef61 portable ActivityPub URIs identify the same
70
+ * portable object.
71
+ *
72
+ * Non-string inputs return `false`. Non-portable URI strings use strict string
73
+ * equality. Portable URI strings are compared through
74
+ * {@link canonicalizePortableUri}; malformed portable URI strings return
75
+ * `false` unless they are exactly equal.
76
+ *
77
+ * @since 2.4.0
78
+ */
79
+ function arePortableUrisEqual(left, right) {
80
+ if (typeof left !== "string" || typeof right !== "string") return false;
81
+ if (left === right) return true;
82
+ if (!PORTABLE_IRI_PATTERN.test(left) || !PORTABLE_IRI_PATTERN.test(right)) return false;
83
+ try {
84
+ return canonicalizePortableUri(left) === canonicalizePortableUri(right);
85
+ } catch (error) {
86
+ if (error instanceof TypeError) return false;
87
+ throw error;
88
+ }
89
+ }
90
+ /**
47
91
  * Checks whether two IRIs have the same origin.
48
92
  */
49
93
  function haveSameIriOrigin(left, right) {
@@ -86,6 +130,31 @@ function decodePortableAuthority(authority) {
86
130
  if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
87
131
  return decoded;
88
132
  }
133
+ function normalizePercentEncoding(value) {
134
+ return value.replace(PERCENT_ENCODING_PATTERN, (match) => match.toUpperCase());
135
+ }
136
+ function normalizePortableAuthority(authority) {
137
+ return normalizePercentEncoding(authority).replace(PERCENT_ENCODING_PATTERN, (match) => {
138
+ const decoded = String.fromCharCode(Number.parseInt(match.slice(1), 16));
139
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : match;
140
+ });
141
+ }
142
+ function normalizePortableComponent(value) {
143
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(value)) throw new TypeError("Invalid portable ActivityPub IRI component.");
144
+ return value.replace(/%[0-9A-Fa-f]{2}|[^%]+/g, (match) => {
145
+ if (match.startsWith("%")) {
146
+ const upper = match.toUpperCase();
147
+ const decoded = String.fromCharCode(Number.parseInt(upper.slice(1), 16));
148
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : upper;
149
+ }
150
+ try {
151
+ return encodeURI(match);
152
+ } catch (error) {
153
+ if (error instanceof URIError) throw new TypeError("Invalid portable ActivityPub IRI component.");
154
+ throw error;
155
+ }
156
+ });
157
+ }
89
158
  function parseAtUri(uri) {
90
159
  const index = uri.indexOf("/", 5);
91
160
  const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
@@ -258,4 +327,4 @@ function matchesIPv6Prefix(address, prefixWords, prefixLength) {
258
327
  return true;
259
328
  }
260
329
  //#endregion
261
- export { isValidPublicIPv4Address as a, parseJsonLdId as c, haveSameIriOrigin as i, validatePublicUrl as l, expandIPv6Address as n, isValidPublicIPv6Address as o, formatIri as r, parseIri as s, UrlError as t };
330
+ export { formatIri as a, isValidPublicIPv6Address as c, validatePublicUrl as d, expandIPv6Address as i, parseIri as l, arePortableUrisEqual as n, haveSameIriOrigin as o, canonicalizePortableUri as r, isValidPublicIPv4Address as s, UrlError as t, parseJsonLdId as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-runtime",
3
- "version": "2.4.0-dev.1583+1a6f31cb",
3
+ "version": "2.4.0-dev.1599+7b19967f",
4
4
  "homepage": "https://fedify.dev/",
5
5
  "repository": {
6
6
  "type": "git",
package/src/mod.ts CHANGED
@@ -54,6 +54,8 @@ export {
54
54
  type PropertyPreprocessorContext,
55
55
  } from "./preprocessor.ts";
56
56
  export {
57
+ arePortableUrisEqual,
58
+ canonicalizePortableUri,
57
59
  expandIPv6Address,
58
60
  formatIri,
59
61
  haveSameIriOrigin,
package/src/url.test.ts CHANGED
@@ -1,6 +1,8 @@
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,
6
8
  haveSameIriOrigin,
@@ -184,6 +186,256 @@ test("formatIri() emits canonical portable ActivityPub URI syntax", () => {
184
186
  deepStrictEqual(formatIri("/actor"), "/actor");
185
187
  });
186
188
 
189
+ test("canonicalizePortableUri() emits comparison forms", () => {
190
+ const cases = [
191
+ "ap://did:key:z6Mkabc/actor",
192
+ "ap://did%3Akey%3Az6Mkabc/actor",
193
+ "ap://did:key:z6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
194
+ "ap+ef61://did:key:z6Mkabc/actor",
195
+ "ap+ef61://did%3Akey%3Az6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
196
+ ];
197
+ for (const iri of cases) {
198
+ deepStrictEqual(
199
+ canonicalizePortableUri(iri),
200
+ "ap+ef61://did:key:z6Mkabc/actor",
201
+ );
202
+ }
203
+ });
204
+
205
+ test("canonicalizePortableUri() preserves paths and fragments", () => {
206
+ deepStrictEqual(
207
+ canonicalizePortableUri(
208
+ "ap://did:key:z6Mkabc/objects/1/attachments/2?gateways=https%3A%2F%2Fa.example#image",
209
+ ),
210
+ "ap+ef61://did:key:z6Mkabc/objects/1/attachments/2#image",
211
+ );
212
+ deepStrictEqual(
213
+ canonicalizePortableUri("ap://did:key:z6Mkabc/objects/1#reply"),
214
+ "ap+ef61://did:key:z6Mkabc/objects/1#reply",
215
+ );
216
+ });
217
+
218
+ test("canonicalizePortableUri() preserves DID-internal pct-encoded characters", () => {
219
+ deepStrictEqual(
220
+ canonicalizePortableUri("ap://did:example:abc%2Fdef/actor?x=1"),
221
+ "ap+ef61://did:example:abc%2Fdef/actor",
222
+ );
223
+ deepStrictEqual(
224
+ canonicalizePortableUri("ap://did%3Aweb%3Aexample.com%253A3000/u/1"),
225
+ "ap+ef61://did:web:example.com%3A3000/u/1",
226
+ );
227
+ });
228
+
229
+ test("canonicalizePortableUri() normalizes authority pct-encoding casing", () => {
230
+ deepStrictEqual(
231
+ canonicalizePortableUri("ap://did:example:abc%2fdef/actor"),
232
+ "ap+ef61://did:example:abc%2Fdef/actor",
233
+ );
234
+ deepStrictEqual(
235
+ canonicalizePortableUri("ap://did:example:abc%2fdef%3abar/actor"),
236
+ "ap+ef61://did:example:abc%2Fdef%3Abar/actor",
237
+ );
238
+ deepStrictEqual(
239
+ canonicalizePortableUri("ap://did%3Aexample%3Aabc%252fdef/actor"),
240
+ "ap+ef61://did:example:abc%2Fdef/actor",
241
+ );
242
+ deepStrictEqual(
243
+ canonicalizePortableUri("ap://did%3Akey%3Az6Mk%2Dabc/actor"),
244
+ "ap+ef61://did:key:z6Mk-abc/actor",
245
+ );
246
+ ok(arePortableUrisEqual(
247
+ "ap://did:example:abc%2fdef/actor",
248
+ "ap://did:example:abc%2Fdef/actor",
249
+ ));
250
+ ok(arePortableUrisEqual(
251
+ "ap://did%3Akey%3Az6Mk%2Dabc/actor",
252
+ "ap://did:key:z6Mk-abc/actor",
253
+ ));
254
+ });
255
+
256
+ test("canonicalizePortableUri() normalizes path and fragment pct-encoding casing", () => {
257
+ deepStrictEqual(
258
+ canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2fprofile#part%2ftwo"),
259
+ "ap+ef61://did:key:z6Mkabc/actor%2Fprofile#part%2Ftwo",
260
+ );
261
+ deepStrictEqual(
262
+ canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2fprofile%3aimage"),
263
+ "ap+ef61://did:key:z6Mkabc/actor%2Fprofile%3Aimage",
264
+ );
265
+ deepStrictEqual(
266
+ canonicalizePortableUri("ap://did:key:z6Mkabc/actor%2Dprofile#part%7Etwo"),
267
+ "ap+ef61://did:key:z6Mkabc/actor-profile#part~two",
268
+ );
269
+ ok(arePortableUrisEqual(
270
+ "ap://did:key:z6Mkabc/actor%2fprofile#part%2ftwo",
271
+ "ap://did:key:z6Mkabc/actor%2Fprofile#part%2Ftwo",
272
+ ));
273
+ ok(arePortableUrisEqual(
274
+ "ap://did:key:z6Mkabc/actor%2Dprofile#part%7Etwo",
275
+ "ap://did:key:z6Mkabc/actor-profile#part~two",
276
+ ));
277
+ });
278
+
279
+ test("canonicalizePortableUri() encodes raw path and fragment characters", () => {
280
+ deepStrictEqual(
281
+ canonicalizePortableUri("ap://did:key:z6Mkabc/\u00e9#\u00e9"),
282
+ "ap+ef61://did:key:z6Mkabc/%C3%A9#%C3%A9",
283
+ );
284
+ ok(arePortableUrisEqual(
285
+ "ap://did:key:z6Mkabc/\u00e9#\u00e9",
286
+ "ap://did:key:z6Mkabc/%C3%A9#%C3%A9",
287
+ ));
288
+ });
289
+
290
+ test("canonicalizePortableUri() normalizes DID scheme casing", () => {
291
+ deepStrictEqual(
292
+ canonicalizePortableUri("ap://DID:key:z6Mkabc/actor"),
293
+ "ap+ef61://did:key:z6Mkabc/actor",
294
+ );
295
+ deepStrictEqual(
296
+ canonicalizePortableUri("ap://DID%3Akey%3Az6Mkabc/actor"),
297
+ "ap+ef61://did:key:z6Mkabc/actor",
298
+ );
299
+ });
300
+
301
+ test("canonicalizePortableUri() preserves opaque path segments", () => {
302
+ deepStrictEqual(
303
+ canonicalizePortableUri("ap://did:key:z6Mkabc/a/../b?gateways=x"),
304
+ "ap+ef61://did:key:z6Mkabc/a/../b",
305
+ );
306
+ deepStrictEqual(
307
+ canonicalizePortableUri("ap://did:key:z6Mkabc/a/%2e%2e/b"),
308
+ "ap+ef61://did:key:z6Mkabc/a/../b",
309
+ );
310
+ ok(
311
+ !arePortableUrisEqual(
312
+ "ap://did:key:z6Mkabc/a/../b",
313
+ "ap://did:key:z6Mkabc/b",
314
+ ),
315
+ );
316
+ ok(
317
+ !arePortableUrisEqual(
318
+ "ap://did:key:z6Mkabc/a/%2e%2e/b",
319
+ "ap://did:key:z6Mkabc/b",
320
+ ),
321
+ );
322
+ });
323
+
324
+ test("canonicalizePortableUri() rejects non-portable URIs", () => {
325
+ const cases = [
326
+ "https://example.com/actor",
327
+ "at://did:plc:example/record",
328
+ "/actor",
329
+ "ap://not-a-did/actor",
330
+ "ap://did:key:z6Mkabc",
331
+ ];
332
+ for (const iri of cases) {
333
+ throws(() => canonicalizePortableUri(iri), TypeError);
334
+ }
335
+ throws(
336
+ () =>
337
+ canonicalizePortableUri(
338
+ new URL("ap+ef61://did%3Akey%3Az6Mkabc/actor") as unknown as string,
339
+ ),
340
+ TypeError,
341
+ );
342
+ });
343
+
344
+ test("canonicalizePortableUri() rejects invalid path and fragment pct-encoding", () => {
345
+ throws(() => canonicalizePortableUri("ap://did:key:z6Mkabc/a%zz"), TypeError);
346
+ throws(
347
+ () => canonicalizePortableUri("ap://did:key:z6Mkabc/actor#part%zz"),
348
+ TypeError,
349
+ );
350
+ throws(
351
+ () => canonicalizePortableUri("ap://did:key:z6Mkabc/\ud800"),
352
+ TypeError,
353
+ );
354
+ throws(
355
+ () => canonicalizePortableUri("ap://did:key:z6Mkabc/actor#\ud800"),
356
+ TypeError,
357
+ );
358
+ });
359
+
360
+ test("arePortableUrisEqual() compares canonical portable URI forms", () => {
361
+ ok(arePortableUrisEqual(
362
+ "ap://did:key:z6Mkabc/actor",
363
+ "ap+ef61://did%3Akey%3Az6Mkabc/actor?gateways=https%3A%2F%2Fa.example",
364
+ ));
365
+ ok(arePortableUrisEqual(
366
+ "ap://DID:key:z6Mkabc/actor",
367
+ "ap://did:key:z6Mkabc/actor",
368
+ ));
369
+ ok(
370
+ !arePortableUrisEqual(
371
+ "ap://did:key:z6Mkabc/actor",
372
+ "ap://did:key:z6Mkdef/actor",
373
+ ),
374
+ );
375
+ ok(
376
+ !arePortableUrisEqual(
377
+ "ap://did:key:z6Mkabc/actor",
378
+ "ap://did:key:z6Mkabc/outbox",
379
+ ),
380
+ );
381
+ ok(
382
+ !arePortableUrisEqual(
383
+ "ap://did:key:z6Mkabc/actor#one",
384
+ "ap://did:key:z6Mkabc/actor#two",
385
+ ),
386
+ );
387
+ });
388
+
389
+ test("arePortableUrisEqual() handles non-portable URI strings", () => {
390
+ ok(arePortableUrisEqual(
391
+ "https://example.com/actor",
392
+ "https://example.com/actor",
393
+ ));
394
+ ok(
395
+ !arePortableUrisEqual(
396
+ "https://example.com/actor",
397
+ "https://example.com/outbox",
398
+ ),
399
+ );
400
+ ok(
401
+ !arePortableUrisEqual(
402
+ "ap://did:key:z6Mkabc/actor",
403
+ "https://example.com/actor",
404
+ ),
405
+ );
406
+ ok(arePortableUrisEqual("ap://not-a-did/actor", "ap://not-a-did/actor"));
407
+ ok(
408
+ !arePortableUrisEqual(
409
+ "ap://not-a-did/actor",
410
+ "ap://not-a-did/outbox",
411
+ ),
412
+ );
413
+ ok(
414
+ !arePortableUrisEqual(
415
+ "ap://not-a-did/actor",
416
+ "ap://did:key:z6Mkabc/actor",
417
+ ),
418
+ );
419
+ ok(
420
+ !arePortableUrisEqual(
421
+ "ap://did:key:z6Mkabc/a%zz",
422
+ "ap://did:key:z6Mkabc/a%25zz",
423
+ ),
424
+ );
425
+ ok(
426
+ !arePortableUrisEqual(
427
+ "ap://did:key:z6Mkabc/\ud800",
428
+ "ap://did:key:z6Mkabc/%EF%BF%BD",
429
+ ),
430
+ );
431
+ ok(
432
+ !arePortableUrisEqual(
433
+ "ap://did:key:z6Mkabc/actor#\ud800",
434
+ "ap://did:key:z6Mkabc/actor#%EF%BF%BD",
435
+ ),
436
+ );
437
+ });
438
+
187
439
  test("formatIri() preserves DID authority pct-encoded delimiters", () => {
188
440
  const parsed = parseIri("ap://did:example:abc%2Fdef/actor");
189
441
  deepStrictEqual(