@fedify/vocab-runtime 2.4.0-dev.1508 → 2.4.0-dev.1528

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 (38) 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 +13 -183
  9. package/dist/mod.d.cts +18 -200
  10. package/dist/mod.d.ts +18 -200
  11. package/dist/mod.js +3 -177
  12. package/dist/tests/decimal.test.cjs +2 -2
  13. package/dist/tests/decimal.test.mjs +2 -2
  14. package/dist/tests/{docloader-0m6FCm4u.mjs → docloader-9JRkIgYh.mjs} +2 -2
  15. package/dist/tests/{docloader-60uMNsd3.cjs → docloader-yxXccHZv.cjs} +2 -2
  16. package/dist/tests/docloader.test.cjs +3 -3
  17. package/dist/tests/docloader.test.mjs +3 -3
  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/{request-CO5esooK.mjs → request-4-bNhrbG.mjs} +1 -1
  23. package/dist/tests/{request-CcBmdgDa.cjs → request-_LWX0dnv.cjs} +1 -1
  24. package/dist/tests/request.test.cjs +1 -1
  25. package/dist/tests/request.test.mjs +1 -1
  26. package/dist/tests/{url-C20FhC7p.cjs → url-2XwVbUS_.cjs} +108 -0
  27. package/dist/tests/{url-m9Qzxy-Y.mjs → url-YWJbnRlf.mjs} +85 -1
  28. package/dist/tests/url.test.cjs +104 -1
  29. package/dist/tests/url.test.mjs +105 -2
  30. package/dist/url-BAdyyqAa.cjs +315 -0
  31. package/dist/url-BuxPHxK2.js +261 -0
  32. package/package.json +11 -1
  33. package/src/internal/jsonld-cache.ts +538 -0
  34. package/src/jsonld-cache.test.ts +554 -0
  35. package/src/mod.ts +4 -0
  36. package/src/url.test.ts +252 -1
  37. package/src/url.ts +141 -0
  38. package/tsdown.config.ts +6 -1
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",