@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.
@@ -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,88 @@ 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(getDidUrlOrigin(decodePortableAuthority(parsed.host)))}${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
+ /**
92
+ * Computes an IRI's FEP-fe34 origin.
93
+ *
94
+ * HTTP(S) IRIs use their web origin. FEP-ef61 portable ActivityPub IRIs and
95
+ * DID URLs use their DID as a cryptographic origin.
96
+ *
97
+ * @throws {TypeError} If the IRI does not have a supported FEP-fe34 origin.
98
+ * @since 2.4.0
99
+ */
100
+ function getFe34Origin(input) {
101
+ if (input instanceof URL) {
102
+ const portable = normalizePortableUrl(input);
103
+ if (portable != null) return getPortableCryptographicOrigin(portable);
104
+ if (input.protocol === "did:") return getDidUrlOrigin(input.href);
105
+ if (input.protocol === "http:" || input.protocol === "https:") return input.origin;
106
+ throw new TypeError("Unsupported FEP-fe34 origin IRI.");
107
+ }
108
+ const portable = parsePortableIri(input);
109
+ if (portable != null) return getPortableCryptographicOrigin(portable);
110
+ if (DID_SCHEME_PATTERN.test(input)) return getDidUrlOrigin(input);
111
+ const parsed = new URL(input);
112
+ if (parsed.protocol === "http:" || parsed.protocol === "https:") return parsed.origin;
113
+ throw new TypeError("Unsupported FEP-fe34 origin IRI.");
114
+ }
115
+ /**
116
+ * Checks whether two IRIs have the same FEP-fe34 origin.
117
+ *
118
+ * Malformed or unsupported IRIs are treated as non-matching.
119
+ *
120
+ * @since 2.4.0
121
+ */
122
+ function haveSameFe34Origin(left, right) {
123
+ try {
124
+ return getFe34Origin(left) === getFe34Origin(right);
125
+ } catch (error) {
126
+ if (error instanceof TypeError) return false;
127
+ throw error;
128
+ }
129
+ }
130
+ /**
48
131
  * Checks whether two IRIs have the same origin.
49
132
  */
50
133
  function haveSameIriOrigin(left, right) {
@@ -54,15 +137,25 @@ function getComparableIriOrigin(iri) {
54
137
  iri = normalizePortableUrl(iri) ?? iri;
55
138
  if (iri.origin !== "null") return iri.origin;
56
139
  if (iri.host !== "") {
57
- const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:")) : iri.host;
140
+ const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(getDidUrlOrigin(decodePortableAuthority(iri.host))) : iri.host;
58
141
  return `${iri.protocol}//${host}`;
59
142
  }
60
143
  return iri.href;
61
144
  }
145
+ function getPortableCryptographicOrigin(iri) {
146
+ return getDidUrlOrigin(decodePortableAuthority(iri.host));
147
+ }
148
+ function getDidUrlOrigin(iri) {
149
+ const did = iri.split(/[/?#]/, 1)[0].replace(DID_SCHEME_PATTERN, "did:");
150
+ if (!DID_PATTERN.test(did)) throw new TypeError("Invalid DID URL.");
151
+ const parts = did.split(":");
152
+ parts[1] = parts[1].toLowerCase();
153
+ return normalizePortableAuthority(parts.join(":"));
154
+ }
62
155
  function parsePortableIri(iri) {
63
156
  const match = iri.match(PORTABLE_IRI_PATTERN);
64
157
  if (match == null) return null;
65
- const authority = decodePortableAuthority(match[2]);
158
+ const authority = getDidUrlOrigin(decodePortableAuthority(match[2]));
66
159
  if (!DID_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
67
160
  if (match[3] === "") throw new TypeError("Invalid portable ActivityPub IRI path.");
68
161
  return new URL(`ap+ef61://${encodeURIComponent(authority)}${match[3]}${match[4] ?? ""}${match[5] ?? ""}`);
@@ -87,6 +180,31 @@ function decodePortableAuthority(authority) {
87
180
  if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
88
181
  return decoded;
89
182
  }
183
+ function normalizePercentEncoding(value) {
184
+ return value.replace(PERCENT_ENCODING_PATTERN, (match) => match.toUpperCase());
185
+ }
186
+ function normalizePortableAuthority(authority) {
187
+ return normalizePercentEncoding(authority).replace(PERCENT_ENCODING_PATTERN, (match) => {
188
+ const decoded = String.fromCharCode(Number.parseInt(match.slice(1), 16));
189
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : match;
190
+ });
191
+ }
192
+ function normalizePortableComponent(value) {
193
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(value)) throw new TypeError("Invalid portable ActivityPub IRI component.");
194
+ return value.replace(/%[0-9A-Fa-f]{2}|[^%]+/g, (match) => {
195
+ if (match.startsWith("%")) {
196
+ const upper = match.toUpperCase();
197
+ const decoded = String.fromCharCode(Number.parseInt(upper.slice(1), 16));
198
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : upper;
199
+ }
200
+ try {
201
+ return encodeURI(match);
202
+ } catch (error) {
203
+ if (error instanceof URIError) throw new TypeError("Invalid portable ActivityPub IRI component.");
204
+ throw error;
205
+ }
206
+ });
207
+ }
90
208
  function parseAtUri(uri) {
91
209
  const index = uri.indexOf("/", 5);
92
210
  const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
@@ -265,6 +383,18 @@ Object.defineProperty(exports, "UrlError", {
265
383
  return UrlError;
266
384
  }
267
385
  });
386
+ Object.defineProperty(exports, "arePortableUrisEqual", {
387
+ enumerable: true,
388
+ get: function() {
389
+ return arePortableUrisEqual;
390
+ }
391
+ });
392
+ Object.defineProperty(exports, "canonicalizePortableUri", {
393
+ enumerable: true,
394
+ get: function() {
395
+ return canonicalizePortableUri;
396
+ }
397
+ });
268
398
  Object.defineProperty(exports, "expandIPv6Address", {
269
399
  enumerable: true,
270
400
  get: function() {
@@ -277,6 +407,18 @@ Object.defineProperty(exports, "formatIri", {
277
407
  return formatIri;
278
408
  }
279
409
  });
410
+ Object.defineProperty(exports, "getFe34Origin", {
411
+ enumerable: true,
412
+ get: function() {
413
+ return getFe34Origin;
414
+ }
415
+ });
416
+ Object.defineProperty(exports, "haveSameFe34Origin", {
417
+ enumerable: true,
418
+ get: function() {
419
+ return haveSameFe34Origin;
420
+ }
421
+ });
280
422
  Object.defineProperty(exports, "haveSameIriOrigin", {
281
423
  enumerable: true,
282
424
  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,88 @@ 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(getDidUrlOrigin(decodePortableAuthority(parsed.host)))}${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
+ /**
91
+ * Computes an IRI's FEP-fe34 origin.
92
+ *
93
+ * HTTP(S) IRIs use their web origin. FEP-ef61 portable ActivityPub IRIs and
94
+ * DID URLs use their DID as a cryptographic origin.
95
+ *
96
+ * @throws {TypeError} If the IRI does not have a supported FEP-fe34 origin.
97
+ * @since 2.4.0
98
+ */
99
+ function getFe34Origin(input) {
100
+ if (input instanceof URL) {
101
+ const portable = normalizePortableUrl(input);
102
+ if (portable != null) return getPortableCryptographicOrigin(portable);
103
+ if (input.protocol === "did:") return getDidUrlOrigin(input.href);
104
+ if (input.protocol === "http:" || input.protocol === "https:") return input.origin;
105
+ throw new TypeError("Unsupported FEP-fe34 origin IRI.");
106
+ }
107
+ const portable = parsePortableIri(input);
108
+ if (portable != null) return getPortableCryptographicOrigin(portable);
109
+ if (DID_SCHEME_PATTERN.test(input)) return getDidUrlOrigin(input);
110
+ const parsed = new URL(input);
111
+ if (parsed.protocol === "http:" || parsed.protocol === "https:") return parsed.origin;
112
+ throw new TypeError("Unsupported FEP-fe34 origin IRI.");
113
+ }
114
+ /**
115
+ * Checks whether two IRIs have the same FEP-fe34 origin.
116
+ *
117
+ * Malformed or unsupported IRIs are treated as non-matching.
118
+ *
119
+ * @since 2.4.0
120
+ */
121
+ function haveSameFe34Origin(left, right) {
122
+ try {
123
+ return getFe34Origin(left) === getFe34Origin(right);
124
+ } catch (error) {
125
+ if (error instanceof TypeError) return false;
126
+ throw error;
127
+ }
128
+ }
129
+ /**
47
130
  * Checks whether two IRIs have the same origin.
48
131
  */
49
132
  function haveSameIriOrigin(left, right) {
@@ -53,15 +136,25 @@ function getComparableIriOrigin(iri) {
53
136
  iri = normalizePortableUrl(iri) ?? iri;
54
137
  if (iri.origin !== "null") return iri.origin;
55
138
  if (iri.host !== "") {
56
- const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:")) : iri.host;
139
+ const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(getDidUrlOrigin(decodePortableAuthority(iri.host))) : iri.host;
57
140
  return `${iri.protocol}//${host}`;
58
141
  }
59
142
  return iri.href;
60
143
  }
144
+ function getPortableCryptographicOrigin(iri) {
145
+ return getDidUrlOrigin(decodePortableAuthority(iri.host));
146
+ }
147
+ function getDidUrlOrigin(iri) {
148
+ const did = iri.split(/[/?#]/, 1)[0].replace(DID_SCHEME_PATTERN, "did:");
149
+ if (!DID_PATTERN.test(did)) throw new TypeError("Invalid DID URL.");
150
+ const parts = did.split(":");
151
+ parts[1] = parts[1].toLowerCase();
152
+ return normalizePortableAuthority(parts.join(":"));
153
+ }
61
154
  function parsePortableIri(iri) {
62
155
  const match = iri.match(PORTABLE_IRI_PATTERN);
63
156
  if (match == null) return null;
64
- const authority = decodePortableAuthority(match[2]);
157
+ const authority = getDidUrlOrigin(decodePortableAuthority(match[2]));
65
158
  if (!DID_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
66
159
  if (match[3] === "") throw new TypeError("Invalid portable ActivityPub IRI path.");
67
160
  return new URL(`ap+ef61://${encodeURIComponent(authority)}${match[3]}${match[4] ?? ""}${match[5] ?? ""}`);
@@ -86,6 +179,31 @@ function decodePortableAuthority(authority) {
86
179
  if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
87
180
  return decoded;
88
181
  }
182
+ function normalizePercentEncoding(value) {
183
+ return value.replace(PERCENT_ENCODING_PATTERN, (match) => match.toUpperCase());
184
+ }
185
+ function normalizePortableAuthority(authority) {
186
+ return normalizePercentEncoding(authority).replace(PERCENT_ENCODING_PATTERN, (match) => {
187
+ const decoded = String.fromCharCode(Number.parseInt(match.slice(1), 16));
188
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : match;
189
+ });
190
+ }
191
+ function normalizePortableComponent(value) {
192
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(value)) throw new TypeError("Invalid portable ActivityPub IRI component.");
193
+ return value.replace(/%[0-9A-Fa-f]{2}|[^%]+/g, (match) => {
194
+ if (match.startsWith("%")) {
195
+ const upper = match.toUpperCase();
196
+ const decoded = String.fromCharCode(Number.parseInt(upper.slice(1), 16));
197
+ return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : upper;
198
+ }
199
+ try {
200
+ return encodeURI(match);
201
+ } catch (error) {
202
+ if (error instanceof URIError) throw new TypeError("Invalid portable ActivityPub IRI component.");
203
+ throw error;
204
+ }
205
+ });
206
+ }
89
207
  function parseAtUri(uri) {
90
208
  const index = uri.indexOf("/", 5);
91
209
  const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
@@ -258,4 +376,4 @@ function matchesIPv6Prefix(address, prefixWords, prefixLength) {
258
376
  return true;
259
377
  }
260
378
  //#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 };
379
+ export { formatIri as a, haveSameIriOrigin as c, parseIri as d, parseJsonLdId as f, expandIPv6Address as i, isValidPublicIPv4Address as l, arePortableUrisEqual as n, getFe34Origin as o, validatePublicUrl as p, canonicalizePortableUri as r, haveSameFe34Origin as s, UrlError as t, isValidPublicIPv6Address 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.1618+58858243",
4
4
  "homepage": "https://fedify.dev/",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,6 +1,6 @@
1
1
  import type { DocumentLoader } from "../docloader.ts";
2
2
  import jsonld from "../jsonld.ts";
3
- import { formatIri, haveSameIriOrigin } from "../url.ts";
3
+ import { formatIri, haveSameFe34Origin, haveSameIriOrigin } from "../url.ts";
4
4
 
5
5
  const noJsonLdContext = Symbol("noJsonLdContext");
6
6
 
@@ -28,7 +28,8 @@ export function isTrustedIriOrigin(
28
28
  right: URL | null | undefined,
29
29
  ): boolean {
30
30
  return options.crossOrigin === "trust" || left == null ||
31
- (right != null && haveSameIriOrigin(left, right));
31
+ (right != null &&
32
+ (haveSameIriOrigin(left, right) || haveSameFe34Origin(left, right)));
32
33
  }
33
34
 
34
35
  /**
package/src/mod.ts CHANGED
@@ -54,8 +54,12 @@ export {
54
54
  type PropertyPreprocessorContext,
55
55
  } from "./preprocessor.ts";
56
56
  export {
57
+ arePortableUrisEqual,
58
+ canonicalizePortableUri,
57
59
  expandIPv6Address,
58
60
  formatIri,
61
+ getFe34Origin,
62
+ haveSameFe34Origin,
59
63
  haveSameIriOrigin,
60
64
  isValidPublicIPv4Address,
61
65
  isValidPublicIPv6Address,