@fedify/vocab-runtime 2.4.0-dev.1581 → 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.
- package/deno.json +1 -1
- package/dist/internal/jsonld-cache.cjs +1 -1
- package/dist/internal/jsonld-cache.js +1 -1
- package/dist/mod.cjs +4 -2
- package/dist/mod.d.cts +28 -1
- package/dist/mod.d.ts +28 -1
- package/dist/mod.js +3 -3
- package/dist/tests/decimal.test.cjs +2 -2
- package/dist/tests/decimal.test.mjs +2 -2
- package/dist/tests/{docloader-CI6F0enJ.cjs → docloader-BiSxywul.cjs} +2 -2
- package/dist/tests/{docloader-C66V4jZk.mjs → docloader-CDde_fhH.mjs} +2 -2
- package/dist/tests/docloader.test.cjs +3 -3
- package/dist/tests/docloader.test.mjs +3 -3
- package/dist/tests/jsonld-cache.test.cjs +1 -1
- package/dist/tests/jsonld-cache.test.mjs +1 -1
- package/dist/tests/{request-BAflhLW8.mjs → request-BBw3lNNz.mjs} +1 -1
- package/dist/tests/{request-qjVw42gq.cjs → request-C9VKqYdG.cjs} +1 -1
- package/dist/tests/request.test.cjs +1 -1
- package/dist/tests/request.test.mjs +1 -1
- package/dist/tests/{url-2XwVbUS_.cjs → url-CtKcX6ma.cjs} +81 -0
- package/dist/tests/{url-YWJbnRlf.mjs → url-D7ay_5pk.mjs} +70 -1
- package/dist/tests/url.test.cjs +81 -1
- package/dist/tests/url.test.mjs +81 -1
- package/dist/{url-BAdyyqAa.cjs → url-BMDN9GnE.cjs} +81 -0
- package/dist/{url-BuxPHxK2.js → url-BlFta-Eh.js} +70 -1
- package/package.json +1 -1
- package/src/mod.ts +2 -0
- package/src/url.test.ts +252 -0
- package/src/url.ts +111 -0
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,73 @@ 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
|
+
decodePortableAuthority(parsed.host).replace(DID_SCHEME_PATTERN, "did:"),
|
|
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
|
+
|
|
66
134
|
/**
|
|
67
135
|
* Checks whether two IRIs have the same origin.
|
|
68
136
|
*/
|
|
@@ -143,6 +211,49 @@ function decodePortableAuthority(authority: string): string {
|
|
|
143
211
|
return decoded;
|
|
144
212
|
}
|
|
145
213
|
|
|
214
|
+
function normalizePercentEncoding(value: string): string {
|
|
215
|
+
return value.replace(
|
|
216
|
+
PERCENT_ENCODING_PATTERN,
|
|
217
|
+
(match) => match.toUpperCase(),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function normalizePortableAuthority(authority: string): string {
|
|
222
|
+
return normalizePercentEncoding(authority).replace(
|
|
223
|
+
PERCENT_ENCODING_PATTERN,
|
|
224
|
+
(match) => {
|
|
225
|
+
const decoded = String.fromCharCode(Number.parseInt(match.slice(1), 16));
|
|
226
|
+
return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : match;
|
|
227
|
+
},
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function normalizePortableComponent(value: string): string {
|
|
232
|
+
if (INVALID_PERCENT_ENCODING_PATTERN.test(value)) {
|
|
233
|
+
throw new TypeError("Invalid portable ActivityPub IRI component.");
|
|
234
|
+
}
|
|
235
|
+
return value.replace(
|
|
236
|
+
/%[0-9A-Fa-f]{2}|[^%]+/g,
|
|
237
|
+
(match) => {
|
|
238
|
+
if (match.startsWith("%")) {
|
|
239
|
+
const upper = match.toUpperCase();
|
|
240
|
+
const decoded = String.fromCharCode(
|
|
241
|
+
Number.parseInt(upper.slice(1), 16),
|
|
242
|
+
);
|
|
243
|
+
return /[A-Za-z0-9._~-]/.test(decoded) ? decoded : upper;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
return encodeURI(match);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (error instanceof URIError) {
|
|
249
|
+
throw new TypeError("Invalid portable ActivityPub IRI component.");
|
|
250
|
+
}
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
146
257
|
function parseAtUri(uri: string): URL {
|
|
147
258
|
const index = uri.indexOf("/", 5);
|
|
148
259
|
const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
|