@fedify/vocab-runtime 2.4.0-dev.2030 → 2.4.0-dev.2047

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 (51) hide show
  1. package/deno.json +2 -1
  2. package/dist/internal/jsonld-cache.cjs +1 -1
  3. package/dist/internal/jsonld-cache.js +1 -1
  4. package/dist/internal/portable-dereference.cjs +315 -0
  5. package/dist/internal/portable-dereference.d.cts +87 -0
  6. package/dist/internal/portable-dereference.d.ts +87 -0
  7. package/dist/internal/portable-dereference.js +311 -0
  8. package/dist/mod.cjs +11 -3
  9. package/dist/mod.d.cts +75 -1
  10. package/dist/mod.d.ts +75 -1
  11. package/dist/mod.js +10 -4
  12. package/dist/portable-CduElToy.d.ts +52 -0
  13. package/dist/portable-DxF2Ut-Z.d.cts +52 -0
  14. package/dist/tests/{body-B9qsaRLZ.cjs → body-BgDeabVX.cjs} +1 -1
  15. package/dist/tests/{body-BhoBj6zg.mjs → body-D7iDYZxi.mjs} +1 -1
  16. package/dist/tests/body.test.cjs +1 -1
  17. package/dist/tests/body.test.mjs +1 -1
  18. package/dist/tests/decimal.test.cjs +2 -2
  19. package/dist/tests/decimal.test.mjs +2 -2
  20. package/dist/tests/{docloader--w8_lTnA.cjs → docloader-0r0eohuK.cjs} +3 -3
  21. package/dist/tests/{docloader-DKk8OANv.mjs → docloader-DzfRRzKc.mjs} +3 -3
  22. package/dist/tests/docloader.test.cjs +3 -3
  23. package/dist/tests/docloader.test.mjs +3 -3
  24. package/dist/tests/internal/portable-dereference.test.cjs +123 -0
  25. package/dist/tests/internal/portable-dereference.test.d.cts +1 -0
  26. package/dist/tests/internal/portable-dereference.test.d.mts +1 -0
  27. package/dist/tests/internal/portable-dereference.test.mjs +124 -0
  28. package/dist/tests/jsonld-CmDhHNTx.cjs +12 -0
  29. package/dist/tests/jsonld-DweIJ07b.mjs +5 -0
  30. package/dist/tests/jsonld-cache.test.cjs +18 -23
  31. package/dist/tests/jsonld-cache.test.mjs +2 -5
  32. package/dist/tests/{request-Dm_aAoKt.mjs → request-DOyNI0N1.mjs} +8 -2
  33. package/dist/tests/{request-D3Z--Onp.cjs → request-DdT5TslC.cjs} +8 -2
  34. package/dist/tests/request.test.cjs +4 -1
  35. package/dist/tests/request.test.mjs +4 -1
  36. package/dist/{url-i33c0US6.cjs → tests/url-BqlldzT8.cjs} +164 -1
  37. package/dist/tests/{url-Basm1L_c.mjs → url-DgJM9WJn.mjs} +147 -1
  38. package/dist/tests/url.test.cjs +220 -1
  39. package/dist/tests/url.test.mjs +220 -1
  40. package/dist/{url-BHAShw9J.js → url-CEhi4hb4.js} +147 -1
  41. package/dist/{tests/url-CPfMatUL.cjs → url-Cke4irYD.cjs} +155 -2
  42. package/package.json +12 -2
  43. package/src/internal/portable-dereference.test.ts +74 -0
  44. package/src/internal/portable-dereference.ts +426 -0
  45. package/src/mod.ts +6 -0
  46. package/src/portable.ts +52 -0
  47. package/src/request.test.ts +10 -1
  48. package/src/request.ts +9 -1
  49. package/src/url.test.ts +361 -0
  50. package/src/url.ts +235 -0
  51. package/tsdown.config.ts +1 -0
@@ -1,4 +1,4 @@
1
- import { a as formatIri, c as haveSameIriOrigin, d as isValidPublicIPv6Address, f as parseGatewayUrl, g as validatePublicUrl, h as validateLookupAddresses, i as expandIPv6Address, l as isGatewayUrl, m as parseJsonLdId, n as arePortableUrisEqual, o as getFe34Origin, p as parseIri, r as canonicalizePortableUri, s as haveSameFe34Origin, t as UrlError, u as isValidPublicIPv4Address } from "./url-Basm1L_c.mjs";
1
+ import { _ as validateLookupAddresses, a as formatIri, c as haveSameFe34Origin, d as isValidPublicIPv4Address, f as isValidPublicIPv6Address, g as toCompatibleEf61Id, h as parseJsonLdId, i as expandIPv6Address, l as haveSameIriOrigin, m as parseIri, n as arePortableUrisEqual, o as fromCompatibleEf61Id, p as parseGatewayUrl, r as canonicalizePortableUri, s as getFe34Origin, t as UrlError, u as isGatewayUrl, v as validatePublicUrl } from "./url-DgJM9WJn.mjs";
2
2
  import { test } from "node:test";
3
3
  import { deepStrictEqual, ok, rejects, throws } from "node:assert";
4
4
  //#region src/url.test.ts
@@ -244,6 +244,225 @@ test("parseGatewayUrl() accepts only HTTP(S) base URIs", () => {
244
244
  ok(!isGatewayUrl(new URL(url)));
245
245
  }
246
246
  });
247
+ test("fromCompatibleEf61Id() converts compatible identifiers", () => {
248
+ for (const [input, expected] of [
249
+ ["https://server.example/.well-known/apgateway/did:key:z6MkAlice/objects/1", "ap+ef61://did%3Akey%3Az6MkAlice/objects/1"],
250
+ ["http://server.example:8080/.well-known/apgateway/did:key:z6MkAlice/actor", "ap+ef61://did%3Akey%3Az6MkAlice/actor"],
251
+ ["https://server.example/.well-known/apgateway/did%3Akey%3Az6MkAlice/actor", "ap+ef61://did%3Akey%3Az6MkAlice/actor"],
252
+ ["https://server.example/.well-known/apgateway/DID:key:z6MkAlice/actor", "ap+ef61://did%3Akey%3Az6MkAlice/actor"],
253
+ ["https://server.example/.well-known/apgateway/did:key:z6MkAlice/a/b/c", "ap+ef61://did%3Akey%3Az6MkAlice/a/b/c"],
254
+ ["https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor#main-key", "ap+ef61://did%3Akey%3Az6MkAlice/actor#main-key"],
255
+ ["https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor#", "ap+ef61://did%3Akey%3Az6MkAlice/actor#"],
256
+ ["https://server.example/.well-known/apgateway/did:key:z6MkAlice/outbox?page=2", "ap+ef61://did%3Akey%3Az6MkAlice/outbox?page=2"],
257
+ ["https://server.example/.well-known/apgateway/did:key:z6MkAlice/a%2Fb", "ap+ef61://did%3Akey%3Az6MkAlice/a%2Fb"],
258
+ ["https://server.example/.well-known/apgateway/did:web:example.com%3A8080/actor", "ap+ef61://did%3Aweb%3Aexample.com%253A8080/actor"],
259
+ ["https://server.example/.well-known/apgateway/did:example:a%2525/x", "ap+ef61://did%3Aexample%3Aa%2525/x"]
260
+ ]) {
261
+ deepStrictEqual(fromCompatibleEf61Id(input)?.href, expected, input);
262
+ deepStrictEqual(fromCompatibleEf61Id(new URL(input))?.href, expected, input);
263
+ }
264
+ const converted = fromCompatibleEf61Id("https://server.example/.well-known/apgateway/did:key:z6MkAlice/objects/1");
265
+ deepStrictEqual(formatIri(converted), "ap+ef61://did:key:z6MkAlice/objects/1");
266
+ deepStrictEqual(canonicalizePortableUri(converted.href), "ap+ef61://did:key:z6MkAlice/objects/1");
267
+ ok(arePortableUrisEqual(fromCompatibleEf61Id("https://a.example/.well-known/apgateway/did:key:z6MkAlice/actor").href, fromCompatibleEf61Id("https://b.example/.well-known/apgateway/did:key:z6MkAlice/actor").href));
268
+ });
269
+ test("fromCompatibleEf61Id() returns null for other URLs", () => {
270
+ for (const input of [
271
+ "https://server.example/users/alice",
272
+ "https://server.example/.well-known/webfinger?resource=acct:a@b",
273
+ "https://server.example/.well-known/apgateway",
274
+ "https://server.example/.well-known/apgateway/",
275
+ "https://server.example/.well-known/apgateway/hl:zQmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n",
276
+ "https://server.example/.well-known/apgateway/objects/did:key:z6MkAlice/x",
277
+ "https://server.example/.well-known/apgatewayx/did:key:z6MkAlice/x",
278
+ "https://server.example/ap/did:key:z6MkAlice/actor",
279
+ "https://server.example/?id=/.well-known/apgateway/did:key:z6MkAlice/x",
280
+ "ap://did:key:z6MkAlice/actor",
281
+ "ap+ef61://did:key:z6MkAlice/actor",
282
+ "ftp://server.example/.well-known/apgateway/did:key:z6MkAlice/actor",
283
+ "not a URL",
284
+ "/.well-known/apgateway/did:key:z6MkAlice/actor"
285
+ ]) deepStrictEqual(fromCompatibleEf61Id(input), null, input);
286
+ deepStrictEqual(fromCompatibleEf61Id(new URL("ap+ef61://did%3Akey%3Az6MkAlice/actor")), null);
287
+ deepStrictEqual(fromCompatibleEf61Id(123), null);
288
+ });
289
+ test("fromCompatibleEf61Id() rejects malformed compatible identifiers", () => {
290
+ for (const input of [
291
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice",
292
+ "https://server.example/.well-known/apgateway/did:",
293
+ "https://server.example/.well-known/apgateway/did:key/actor",
294
+ "https://server.example/.well-known/apgateway/did:ke%y:z6MkAlice/actor",
295
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a%zz",
296
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a#%zz",
297
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a?x=%zz",
298
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a?@gateway=https%3A%2F%2Fevil.example",
299
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a?page=2&%40gateway=x",
300
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a?@%67ateway=x",
301
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a?gateways=x",
302
+ "https://server.example/.well-known/apgateway/did:example:a%25/x",
303
+ "https://user:pass@server.example/.well-known/apgateway/did:key:z6MkAlice/a",
304
+ "https://user@server.example/.well-known/apgateway/did:key:z6MkAlice/a"
305
+ ]) throws(() => fromCompatibleEf61Id(input), TypeError, input);
306
+ });
307
+ test("toCompatibleEf61Id() converts portable URIs", () => {
308
+ const cases = [
309
+ [
310
+ "ap+ef61://did:key:z6MkAlice/objects/1",
311
+ "https://server.example",
312
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/objects/1"
313
+ ],
314
+ [
315
+ "ap://did:key:z6MkAlice/objects/1",
316
+ "https://server.example/",
317
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/objects/1"
318
+ ],
319
+ [
320
+ "ap://did%3Akey%3Az6MkAlice/actor",
321
+ new URL("http://server.example:8080"),
322
+ "http://server.example:8080/.well-known/apgateway/did:key:z6MkAlice/actor"
323
+ ],
324
+ [
325
+ new URL("ap+ef61://did%3Akey%3Az6MkAlice/actor"),
326
+ "https://server.example",
327
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor"
328
+ ],
329
+ [
330
+ new URL("ap://did%3Akey%3Az6MkAlice/actor"),
331
+ "https://SERVER.example",
332
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor"
333
+ ],
334
+ [
335
+ "ap://did:key:z6MkAlice/a/b/c",
336
+ "https://server.example",
337
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a/b/c"
338
+ ],
339
+ [
340
+ "ap://did:key:z6MkAlice/actor#main-key",
341
+ "https://server.example",
342
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor#main-key"
343
+ ],
344
+ [
345
+ "ap://did:key:z6MkAlice/actor#",
346
+ "https://server.example",
347
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor#"
348
+ ],
349
+ [
350
+ "ap://DID:key:z6MkAlice/actor",
351
+ "https://server.example",
352
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor"
353
+ ],
354
+ [
355
+ "ap://did:example:a%2525/x",
356
+ "https://server.example",
357
+ "https://server.example/.well-known/apgateway/did:example:a%2525/x"
358
+ ],
359
+ [
360
+ "ap://did:web:example.com%3A8080/actor",
361
+ "https://server.example",
362
+ "https://server.example/.well-known/apgateway/did:web:example.com%3A8080/actor"
363
+ ],
364
+ [
365
+ "ap://did:key:z6MkAlice/a%2Fb/%252e",
366
+ "https://server.example",
367
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a%2Fb/%252e"
368
+ ],
369
+ [
370
+ "ap://did:key:z6MkAlice/a\\b c é/%7euser",
371
+ "https://server.example",
372
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/a%5Cb%20c%09%C3%A9/~user"
373
+ ]
374
+ ];
375
+ for (const [portableId, gateway, expected] of cases) deepStrictEqual(toCompatibleEf61Id(portableId, gateway).href, expected, String(portableId));
376
+ });
377
+ test("toCompatibleEf61Id() removes location hints from queries", () => {
378
+ const base = "https://server.example/.well-known/apgateway/did:key:z6MkAlice";
379
+ const cases = [
380
+ ["ap://did:key:z6MkAlice/actor?@gateway=https%3A%2F%2Fa.example&@gateway=https%3A%2F%2Fb.example", `${base}/actor`],
381
+ ["ap://did:key:z6MkAlice/actor?gateways=https%3A%2F%2Fa.example", `${base}/actor`],
382
+ ["ap://did:key:z6MkAlice/actor?%40gateway=https%3A%2F%2Fa.example", `${base}/actor`],
383
+ ["ap://did:key:z6MkAlice/actor?@%67ateway=https%3A%2F%2Fa.example", `${base}/actor`],
384
+ ["ap://did:key:z6MkAlice/actor?@gateway", `${base}/actor`],
385
+ ["ap://did:key:z6MkAlice/c?page=3&@gateway=https%3A%2F%2Fa.example&maxItems=20&page=4", `${base}/c?page=3&maxItems=20&page=4`],
386
+ ["ap://did:key:z6MkAlice/c?a=1&&b=2#frag", `${base}/c?a=1&&b=2#frag`],
387
+ ["ap://did:key:z6MkAlice/c?", `${base}/c?`],
388
+ ["ap://did:key:z6MkAlice/c?x='y'", `${base}/c?x=%27y%27`],
389
+ ["ap://did:key:z6MkAlice/c?@gate way=https%3A%2F%2Fa.example", `${base}/c?@gate%09way=https%3A%2F%2Fa.example`],
390
+ ["ap://did:key:z6MkAlice/c?@gate\nway=1&x=\r2", `${base}/c?@gate%0Away=1&x=%0D2`],
391
+ ["ap://did:key:z6MkAlice/c?%ff=1", `${base}/c?%FF=1`]
392
+ ];
393
+ for (const [portableId, expected] of cases) {
394
+ const result = toCompatibleEf61Id(portableId, "https://server.example");
395
+ deepStrictEqual(result.href, expected, portableId);
396
+ ok(!/[?&](%40|@)gateway(=|&|$)/i.test(result.search), portableId);
397
+ }
398
+ });
399
+ test("toCompatibleEf61Id() rejects invalid portable IDs", () => {
400
+ const cases = [
401
+ "https://server.example/actor",
402
+ "https://server.example/.well-known/apgateway/did:key:z6MkAlice/actor",
403
+ "at://did:plc:example/record",
404
+ "ap://not-a-did/actor",
405
+ "ap://did:key:z6MkAlice",
406
+ "ap://did:key:z6MkAlice/a%zz",
407
+ "ap://did:key:z6MkAlice/a#%zz",
408
+ "ap://did:key:z6MkAlice/a?x=%zz",
409
+ "ap://did:key:z6MkAlice/.",
410
+ "ap://did:key:z6MkAlice/..",
411
+ "ap://did:key:z6MkAlice/a/./b",
412
+ "ap://did:key:z6MkAlice/a/../b",
413
+ "ap://did:key:z6MkAlice/a/%2e/b",
414
+ "ap://did:key:z6MkAlice/a/%2E%2e/b",
415
+ "ap://did:key:z6MkAlice/a/.%2e/b",
416
+ "ap://did:key:z6MkAlice/a/%2e./b",
417
+ new URL("https://server.example/actor"),
418
+ new URL("ap+ef61://user:pass@did%3Akey%3Az6MkAlice/actor"),
419
+ new URL("ap+ef61://did%3Akey%3Az6MkAlice:8080/actor"),
420
+ 123
421
+ ];
422
+ for (const portableId of cases) throws(() => toCompatibleEf61Id(portableId, "https://server.example"), TypeError, String(portableId));
423
+ });
424
+ test("toCompatibleEf61Id() rejects gateways that are not HTTP(S) origins", () => {
425
+ const cases = [
426
+ "ftp://server.example",
427
+ "ap+ef61://did:key:z6MkAlice/actor",
428
+ "https://user:pass@server.example",
429
+ "https://user@server.example",
430
+ "https://server.example/ap",
431
+ "https://server.example/ap/",
432
+ "https://server.example/.well-known/apgateway",
433
+ "https://server.example/?x=1",
434
+ "https://server.example/?",
435
+ "https://server.example/#fragment",
436
+ "https://server.example/#",
437
+ "server.example",
438
+ "",
439
+ new URL("https://server.example/?"),
440
+ new URL("https://server.example/#"),
441
+ new URL("https://server.example/ap"),
442
+ new URL("ftp://server.example/"),
443
+ 123
444
+ ];
445
+ for (const gateway of cases) throws(() => toCompatibleEf61Id("ap://did:key:z6MkAlice/actor", gateway), TypeError, String(gateway));
446
+ });
447
+ test("compatible identifier conversion round-trips", () => {
448
+ for (const portableId of [
449
+ "ap+ef61://did:key:z6MkAlice/objects/1",
450
+ "ap://did:key:z6MkAlice/actor",
451
+ "ap://did%3Akey%3Az6MkAlice/actor/inbox",
452
+ "ap://did:key:z6MkAlice/actor#",
453
+ "ap://did:key:z6MkAlice/actor#main-key",
454
+ "ap://did:key:z6MkAlice/a%2Fb/%252e",
455
+ "ap://did:key:z6MkAlice/a\\b c é",
456
+ "ap://did:example:a%2525/x",
457
+ "ap://did:web:example.com%3A8080/actor",
458
+ "ap://did:key:z6MkAlice/c?page=3&@gateway=https%3A%2F%2Fa.example"
459
+ ]) {
460
+ const converted = fromCompatibleEf61Id(toCompatibleEf61Id(portableId, "https://server.example"));
461
+ ok(converted != null, portableId);
462
+ deepStrictEqual(canonicalizePortableUri(converted.href), canonicalizePortableUri(portableId), portableId);
463
+ }
464
+ ok(!arePortableUrisEqual(fromCompatibleEf61Id(toCompatibleEf61Id("ap://did:key:z6MkAlice/actor", "https://server.example")).href, fromCompatibleEf61Id(toCompatibleEf61Id("ap://did:key:z6MkAlice/actor#", "https://server.example")).href));
465
+ });
247
466
  test("validatePublicUrl()", async () => {
248
467
  await rejects(() => validatePublicUrl("ftp://localhost"), UrlError);
249
468
  await rejects(() => validatePublicUrl("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="), UrlError);
@@ -224,6 +224,152 @@ function parseGatewayUrl(url) {
224
224
  if (!isGatewayUrl(parsed)) throw new TypeError("FEP-ef61 gateways must be HTTP(S) base URIs with no credentials, path, query, or fragment.");
225
225
  return parsed;
226
226
  }
227
+ const COMPATIBLE_ID_PATH_PREFIX = "/.well-known/apgateway/";
228
+ const COMPATIBLE_ID_DID_PATTERN = /^did(?::|%3A)/i;
229
+ const LOCATION_HINT_PARAMETERS = /* @__PURE__ */ new Set(["@gateway", "gateways"]);
230
+ /**
231
+ * Converts an [FEP-ef61] compatible identifier into a portable ActivityPub
232
+ * URI.
233
+ *
234
+ * A compatible identifier is an HTTP(S) URL under a gateway's fixed
235
+ * `/.well-known/apgateway/` path, such as
236
+ * `https://server.example/.well-known/apgateway/did:key:z6Mk.../objects/1`.
237
+ * This function removes the gateway part and returns the corresponding
238
+ * portable URI, e.g., `ap+ef61://did:key:z6Mk.../objects/1`, in the same
239
+ * internal `URL` form that {@link parseIri} produces. The path, query, and
240
+ * fragment are preserved, so the result is a portable URI, not a comparison
241
+ * form; pass its `href` to {@link canonicalizePortableUri} or
242
+ * {@link arePortableUrisEqual} to compare it with other portable URIs.
243
+ *
244
+ * The conversion only reveals the *claimed* portable identifier. Anyone can
245
+ * publish a compatible identifier for any DID on their own server, so the
246
+ * gateway that served it is neither the object's origin nor authorized to act
247
+ * for the DID. Callers must still verify the retrieved document's Object
248
+ * Integrity Proof against the DID, as FEP-ef61 requires, and should keep the
249
+ * original URL if they need the gateway as a retrieval hint.
250
+ *
251
+ * Arbitrary gateway paths are not supported.
252
+ *
253
+ * [FEP-ef61]: https://w3id.org/fep/ef61
254
+ *
255
+ * @param input The URL to convert.
256
+ * @returns The portable ActivityPub URI, or `null` if the input is not an
257
+ * HTTP(S) URL whose path starts with `/.well-known/apgateway/did:`.
258
+ * Other gateway routes, such as the gateway discovery endpoint and
259
+ * hashlink media URLs, also yield `null`.
260
+ * @throws {TypeError} If the input looks like a compatible identifier but is
261
+ * malformed, e.g., it has an invalid DID, no object path,
262
+ * invalid percent-encoding, credentials, or location
263
+ * hints (`@gateway` query parameters, or the legacy
264
+ * `gateways` parameter), which FEP-ef61 forbids in
265
+ * compatible identifiers.
266
+ * @since 2.4.0
267
+ */
268
+ function fromCompatibleEf61Id(input) {
269
+ return convertCompatibleEf61Id(input)?.url ?? null;
270
+ }
271
+ function convertCompatibleEf61Id(input) {
272
+ let url;
273
+ if (input instanceof URL) url = input;
274
+ else if (typeof input === "string" && URL.canParse(input)) url = new URL(input);
275
+ else return null;
276
+ if (url.protocol !== "http:" && url.protocol !== "https:") return null;
277
+ if (!url.pathname.startsWith(COMPATIBLE_ID_PATH_PREFIX)) return null;
278
+ const tail = url.pathname.slice(23);
279
+ if (!COMPATIBLE_ID_DID_PATTERN.test(tail)) return null;
280
+ if (url.username !== "" || url.password !== "") throw new TypeError("Invalid FEP-ef61 compatible identifier: credentials are not allowed.");
281
+ const iri = "ap+ef61://" + url.href.slice(url.origin.length + 23);
282
+ try {
283
+ const parsed = parsePortableIri(iri);
284
+ if (parsed == null) throw new TypeError("Not a portable IRI.");
285
+ canonicalizePortableUri(iri);
286
+ if ((url.search === "" ? [] : normalizePortableComponent(url.search.slice(1)).split("&")).some(isLocationHint)) throw new TypeError("Location hints are not allowed.");
287
+ return {
288
+ url: parsed,
289
+ iri
290
+ };
291
+ } catch (error) {
292
+ if (error instanceof TypeError) throw new TypeError("Invalid FEP-ef61 compatible identifier.", { cause: error });
293
+ throw error;
294
+ }
295
+ }
296
+ /**
297
+ * Converts a portable ActivityPub URI into an [FEP-ef61] compatible
298
+ * identifier, which is an HTTP(S) URL under the gateway's fixed
299
+ * `/.well-known/apgateway/` path.
300
+ *
301
+ * For example, `ap+ef61://did:key:z6Mk.../objects/1` and
302
+ * `https://server.example` yield
303
+ * `https://server.example/.well-known/apgateway/did:key:z6Mk.../objects/1`.
304
+ * Both `ap:` and `ap+ef61:` URIs with decoded or percent-encoded DID
305
+ * authorities are accepted. Publishers should use the first gateway in the
306
+ * actor's `gateways` list, as FEP-ef61 requires.
307
+ *
308
+ * The path and fragment are preserved, with characters that are not allowed
309
+ * in HTTP(S) URLs percent-encoded the same way as
310
+ * {@link canonicalizePortableUri} does. FEP-ef61 location hints (`@gateway`
311
+ * query parameters, and the legacy `gateways` parameter) are removed, since
312
+ * compatible identifiers must not have them; other query parameters are kept
313
+ * in order.
314
+ *
315
+ * Arbitrary gateway paths are not supported.
316
+ *
317
+ * [FEP-ef61]: https://w3id.org/fep/ef61
318
+ *
319
+ * @param portableId The `ap:` or `ap+ef61:` URI to convert.
320
+ * @param gateway The gateway's HTTP(S) origin, e.g., `https://server.example`.
321
+ * @returns The compatible identifier.
322
+ * @throws {TypeError} If the portable ID is not a valid `ap:` or `ap+ef61:`
323
+ * URI, if its path has `.` or `..` segments (which
324
+ * HTTP(S) URLs cannot represent), or if the gateway is
325
+ * not an HTTP(S) origin with no credentials, path, query,
326
+ * or fragment.
327
+ * @since 2.4.0
328
+ */
329
+ function toCompatibleEf61Id(portableId, gateway) {
330
+ const gatewayUrl = parseCompatibleEf61Gateway(gateway);
331
+ const raw = getRawPortableIri(portableId);
332
+ const match = raw.match(PORTABLE_IRI_PATTERN);
333
+ const parsed = parsePortableIri(raw);
334
+ if (match == null || parsed == null) throw new TypeError("Invalid portable ActivityPub IRI.");
335
+ let did = decodePortableAuthority(parsed.host);
336
+ if (/%25/i.test(did)) did = did.replace(/%/g, "%25");
337
+ const path = normalizePortableComponent(match[3]);
338
+ if (path.split("/").some((segment) => segment === "." || segment === "..")) throw new TypeError("FEP-ef61 compatible identifiers cannot represent portable IRI paths with dot segments.");
339
+ const query = match[4] == null ? "" : stripLocationHints(normalizePortableComponent(match[4].slice(1)));
340
+ const fragment = match[5] == null ? "" : normalizePortableComponent(match[5]);
341
+ const result = new URL(gatewayUrl.origin + COMPATIBLE_ID_PATH_PREFIX + did + path + query + fragment);
342
+ const converted = convertCompatibleEf61Id(result);
343
+ if (converted == null || canonicalizePortableUri(converted.iri) !== canonicalizePortableUri(raw)) throw new TypeError("The portable ActivityPub IRI cannot be represented as an FEP-ef61 compatible identifier.");
344
+ return result;
345
+ }
346
+ function parseCompatibleEf61Gateway(gateway) {
347
+ const url = gateway instanceof URL ? gateway : typeof gateway === "string" && URL.canParse(gateway) ? new URL(gateway) : null;
348
+ if (url == null || url.protocol !== "http:" && url.protocol !== "https:" || url.href !== `${url.origin}/`) throw new TypeError("FEP-ef61 gateways for compatible identifiers must be HTTP(S) origins with no credentials, path, query, or fragment.");
349
+ return url;
350
+ }
351
+ function getRawPortableIri(portableId) {
352
+ if (portableId instanceof URL) {
353
+ if (portableId.protocol !== "ap:" && portableId.protocol !== "ap+ef61:") throw new TypeError("Invalid portable ActivityPub IRI.");
354
+ if (portableId.username !== "" || portableId.password !== "" || portableId.port !== "") throw new TypeError("Invalid portable ActivityPub IRI authority.");
355
+ return portableId.href;
356
+ }
357
+ if (typeof portableId !== "string") throw new TypeError("Invalid portable ActivityPub IRI.");
358
+ return portableId;
359
+ }
360
+ function stripLocationHints(query) {
361
+ const pairs = query.split("&").filter((pair) => !isLocationHint(pair));
362
+ return pairs.length < 1 ? "" : `?${pairs.join("&")}`;
363
+ }
364
+ function isLocationHint(pair) {
365
+ const name = pair.split("=", 1)[0].replace(/\+/g, " ");
366
+ try {
367
+ return LOCATION_HINT_PARAMETERS.has(decodeURIComponent(name));
368
+ } catch (error) {
369
+ if (error instanceof URIError) return false;
370
+ throw error;
371
+ }
372
+ }
227
373
  /**
228
374
  * Validates a URL to prevent SSRF attacks.
229
375
  */
@@ -417,4 +563,4 @@ function matchesIPv6Prefix(address, prefixWords, prefixLength) {
417
563
  return true;
418
564
  }
419
565
  //#endregion
420
- export { formatIri as a, haveSameIriOrigin as c, isValidPublicIPv6Address as d, parseGatewayUrl as f, validatePublicUrl as h, expandIPv6Address as i, isGatewayUrl as l, parseJsonLdId as m, arePortableUrisEqual as n, getFe34Origin as o, parseIri as p, canonicalizePortableUri as r, haveSameFe34Origin as s, UrlError as t, isValidPublicIPv4Address as u };
566
+ export { validatePublicUrl as _, formatIri as a, haveSameFe34Origin as c, isValidPublicIPv4Address as d, isValidPublicIPv6Address as f, toCompatibleEf61Id as g, parseJsonLdId as h, expandIPv6Address as i, haveSameIriOrigin as l, parseIri as m, arePortableUrisEqual as n, fromCompatibleEf61Id as o, parseGatewayUrl as p, canonicalizePortableUri as r, getFe34Origin as s, UrlError as t, isGatewayUrl as u };
@@ -1,3 +1,4 @@
1
+
1
2
  let node_dns_promises = require("node:dns/promises");
2
3
  let node_net = require("node:net");
3
4
  //#region src/url.ts
@@ -223,6 +224,152 @@ function parseGatewayUrl(url) {
223
224
  if (!isGatewayUrl(parsed)) throw new TypeError("FEP-ef61 gateways must be HTTP(S) base URIs with no credentials, path, query, or fragment.");
224
225
  return parsed;
225
226
  }
227
+ const COMPATIBLE_ID_PATH_PREFIX = "/.well-known/apgateway/";
228
+ const COMPATIBLE_ID_DID_PATTERN = /^did(?::|%3A)/i;
229
+ const LOCATION_HINT_PARAMETERS = /* @__PURE__ */ new Set(["@gateway", "gateways"]);
230
+ /**
231
+ * Converts an [FEP-ef61] compatible identifier into a portable ActivityPub
232
+ * URI.
233
+ *
234
+ * A compatible identifier is an HTTP(S) URL under a gateway's fixed
235
+ * `/.well-known/apgateway/` path, such as
236
+ * `https://server.example/.well-known/apgateway/did:key:z6Mk.../objects/1`.
237
+ * This function removes the gateway part and returns the corresponding
238
+ * portable URI, e.g., `ap+ef61://did:key:z6Mk.../objects/1`, in the same
239
+ * internal `URL` form that {@link parseIri} produces. The path, query, and
240
+ * fragment are preserved, so the result is a portable URI, not a comparison
241
+ * form; pass its `href` to {@link canonicalizePortableUri} or
242
+ * {@link arePortableUrisEqual} to compare it with other portable URIs.
243
+ *
244
+ * The conversion only reveals the *claimed* portable identifier. Anyone can
245
+ * publish a compatible identifier for any DID on their own server, so the
246
+ * gateway that served it is neither the object's origin nor authorized to act
247
+ * for the DID. Callers must still verify the retrieved document's Object
248
+ * Integrity Proof against the DID, as FEP-ef61 requires, and should keep the
249
+ * original URL if they need the gateway as a retrieval hint.
250
+ *
251
+ * Arbitrary gateway paths are not supported.
252
+ *
253
+ * [FEP-ef61]: https://w3id.org/fep/ef61
254
+ *
255
+ * @param input The URL to convert.
256
+ * @returns The portable ActivityPub URI, or `null` if the input is not an
257
+ * HTTP(S) URL whose path starts with `/.well-known/apgateway/did:`.
258
+ * Other gateway routes, such as the gateway discovery endpoint and
259
+ * hashlink media URLs, also yield `null`.
260
+ * @throws {TypeError} If the input looks like a compatible identifier but is
261
+ * malformed, e.g., it has an invalid DID, no object path,
262
+ * invalid percent-encoding, credentials, or location
263
+ * hints (`@gateway` query parameters, or the legacy
264
+ * `gateways` parameter), which FEP-ef61 forbids in
265
+ * compatible identifiers.
266
+ * @since 2.4.0
267
+ */
268
+ function fromCompatibleEf61Id(input) {
269
+ return convertCompatibleEf61Id(input)?.url ?? null;
270
+ }
271
+ function convertCompatibleEf61Id(input) {
272
+ let url;
273
+ if (input instanceof URL) url = input;
274
+ else if (typeof input === "string" && URL.canParse(input)) url = new URL(input);
275
+ else return null;
276
+ if (url.protocol !== "http:" && url.protocol !== "https:") return null;
277
+ if (!url.pathname.startsWith(COMPATIBLE_ID_PATH_PREFIX)) return null;
278
+ const tail = url.pathname.slice(23);
279
+ if (!COMPATIBLE_ID_DID_PATTERN.test(tail)) return null;
280
+ if (url.username !== "" || url.password !== "") throw new TypeError("Invalid FEP-ef61 compatible identifier: credentials are not allowed.");
281
+ const iri = "ap+ef61://" + url.href.slice(url.origin.length + 23);
282
+ try {
283
+ const parsed = parsePortableIri(iri);
284
+ if (parsed == null) throw new TypeError("Not a portable IRI.");
285
+ canonicalizePortableUri(iri);
286
+ if ((url.search === "" ? [] : normalizePortableComponent(url.search.slice(1)).split("&")).some(isLocationHint)) throw new TypeError("Location hints are not allowed.");
287
+ return {
288
+ url: parsed,
289
+ iri
290
+ };
291
+ } catch (error) {
292
+ if (error instanceof TypeError) throw new TypeError("Invalid FEP-ef61 compatible identifier.", { cause: error });
293
+ throw error;
294
+ }
295
+ }
296
+ /**
297
+ * Converts a portable ActivityPub URI into an [FEP-ef61] compatible
298
+ * identifier, which is an HTTP(S) URL under the gateway's fixed
299
+ * `/.well-known/apgateway/` path.
300
+ *
301
+ * For example, `ap+ef61://did:key:z6Mk.../objects/1` and
302
+ * `https://server.example` yield
303
+ * `https://server.example/.well-known/apgateway/did:key:z6Mk.../objects/1`.
304
+ * Both `ap:` and `ap+ef61:` URIs with decoded or percent-encoded DID
305
+ * authorities are accepted. Publishers should use the first gateway in the
306
+ * actor's `gateways` list, as FEP-ef61 requires.
307
+ *
308
+ * The path and fragment are preserved, with characters that are not allowed
309
+ * in HTTP(S) URLs percent-encoded the same way as
310
+ * {@link canonicalizePortableUri} does. FEP-ef61 location hints (`@gateway`
311
+ * query parameters, and the legacy `gateways` parameter) are removed, since
312
+ * compatible identifiers must not have them; other query parameters are kept
313
+ * in order.
314
+ *
315
+ * Arbitrary gateway paths are not supported.
316
+ *
317
+ * [FEP-ef61]: https://w3id.org/fep/ef61
318
+ *
319
+ * @param portableId The `ap:` or `ap+ef61:` URI to convert.
320
+ * @param gateway The gateway's HTTP(S) origin, e.g., `https://server.example`.
321
+ * @returns The compatible identifier.
322
+ * @throws {TypeError} If the portable ID is not a valid `ap:` or `ap+ef61:`
323
+ * URI, if its path has `.` or `..` segments (which
324
+ * HTTP(S) URLs cannot represent), or if the gateway is
325
+ * not an HTTP(S) origin with no credentials, path, query,
326
+ * or fragment.
327
+ * @since 2.4.0
328
+ */
329
+ function toCompatibleEf61Id(portableId, gateway) {
330
+ const gatewayUrl = parseCompatibleEf61Gateway(gateway);
331
+ const raw = getRawPortableIri(portableId);
332
+ const match = raw.match(PORTABLE_IRI_PATTERN);
333
+ const parsed = parsePortableIri(raw);
334
+ if (match == null || parsed == null) throw new TypeError("Invalid portable ActivityPub IRI.");
335
+ let did = decodePortableAuthority(parsed.host);
336
+ if (/%25/i.test(did)) did = did.replace(/%/g, "%25");
337
+ const path = normalizePortableComponent(match[3]);
338
+ if (path.split("/").some((segment) => segment === "." || segment === "..")) throw new TypeError("FEP-ef61 compatible identifiers cannot represent portable IRI paths with dot segments.");
339
+ const query = match[4] == null ? "" : stripLocationHints(normalizePortableComponent(match[4].slice(1)));
340
+ const fragment = match[5] == null ? "" : normalizePortableComponent(match[5]);
341
+ const result = new URL(gatewayUrl.origin + COMPATIBLE_ID_PATH_PREFIX + did + path + query + fragment);
342
+ const converted = convertCompatibleEf61Id(result);
343
+ if (converted == null || canonicalizePortableUri(converted.iri) !== canonicalizePortableUri(raw)) throw new TypeError("The portable ActivityPub IRI cannot be represented as an FEP-ef61 compatible identifier.");
344
+ return result;
345
+ }
346
+ function parseCompatibleEf61Gateway(gateway) {
347
+ const url = gateway instanceof URL ? gateway : typeof gateway === "string" && URL.canParse(gateway) ? new URL(gateway) : null;
348
+ if (url == null || url.protocol !== "http:" && url.protocol !== "https:" || url.href !== `${url.origin}/`) throw new TypeError("FEP-ef61 gateways for compatible identifiers must be HTTP(S) origins with no credentials, path, query, or fragment.");
349
+ return url;
350
+ }
351
+ function getRawPortableIri(portableId) {
352
+ if (portableId instanceof URL) {
353
+ if (portableId.protocol !== "ap:" && portableId.protocol !== "ap+ef61:") throw new TypeError("Invalid portable ActivityPub IRI.");
354
+ if (portableId.username !== "" || portableId.password !== "" || portableId.port !== "") throw new TypeError("Invalid portable ActivityPub IRI authority.");
355
+ return portableId.href;
356
+ }
357
+ if (typeof portableId !== "string") throw new TypeError("Invalid portable ActivityPub IRI.");
358
+ return portableId;
359
+ }
360
+ function stripLocationHints(query) {
361
+ const pairs = query.split("&").filter((pair) => !isLocationHint(pair));
362
+ return pairs.length < 1 ? "" : `?${pairs.join("&")}`;
363
+ }
364
+ function isLocationHint(pair) {
365
+ const name = pair.split("=", 1)[0].replace(/\+/g, " ");
366
+ try {
367
+ return LOCATION_HINT_PARAMETERS.has(decodeURIComponent(name));
368
+ } catch (error) {
369
+ if (error instanceof URIError) return false;
370
+ throw error;
371
+ }
372
+ }
226
373
  /**
227
374
  * Validates a URL to prevent SSRF attacks.
228
375
  */
@@ -446,6 +593,12 @@ Object.defineProperty(exports, "formatIri", {
446
593
  return formatIri;
447
594
  }
448
595
  });
596
+ Object.defineProperty(exports, "fromCompatibleEf61Id", {
597
+ enumerable: true,
598
+ get: function() {
599
+ return fromCompatibleEf61Id;
600
+ }
601
+ });
449
602
  Object.defineProperty(exports, "getFe34Origin", {
450
603
  enumerable: true,
451
604
  get: function() {
@@ -500,10 +653,10 @@ Object.defineProperty(exports, "parseJsonLdId", {
500
653
  return parseJsonLdId;
501
654
  }
502
655
  });
503
- Object.defineProperty(exports, "validateLookupAddresses", {
656
+ Object.defineProperty(exports, "toCompatibleEf61Id", {
504
657
  enumerable: true,
505
658
  get: function() {
506
- return validateLookupAddresses;
659
+ return toCompatibleEf61Id;
507
660
  }
508
661
  });
509
662
  Object.defineProperty(exports, "validatePublicUrl", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-runtime",
3
- "version": "2.4.0-dev.2030+e6b3de3d",
3
+ "version": "2.4.0-dev.2047+140b94db",
4
4
  "homepage": "https://fedify.dev/",
5
5
  "repository": {
6
6
  "type": "git",
@@ -52,6 +52,16 @@
52
52
  "require": "./dist/internal/jsonld-cache.cjs",
53
53
  "default": "./dist/internal/jsonld-cache.js"
54
54
  },
55
+ "./internal/portable-dereference": {
56
+ "types": {
57
+ "import": "./dist/internal/portable-dereference.d.ts",
58
+ "require": "./dist/internal/portable-dereference.d.cts",
59
+ "default": "./dist/internal/portable-dereference.d.ts"
60
+ },
61
+ "import": "./dist/internal/portable-dereference.js",
62
+ "require": "./dist/internal/portable-dereference.cjs",
63
+ "default": "./dist/internal/portable-dereference.js"
64
+ },
55
65
  "./internal/signed-representation": {
56
66
  "types": {
57
67
  "import": "./dist/internal/signed-representation.d.ts",
@@ -86,7 +96,7 @@
86
96
  },
87
97
  "license": "MIT",
88
98
  "devDependencies": {
89
- "@types/node": "^24.2.1",
99
+ "@types/node": "^24.12.2",
90
100
  "fetch-mock": "^12.5.4",
91
101
  "tsdown": "^0.22.0",
92
102
  "typescript": "^6.0.0"