@fedify/vocab-runtime 2.4.0-dev.1570 → 2.4.0-dev.1573

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 (48) 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 +178 -190
  9. package/dist/mod.d.cts +59 -190
  10. package/dist/mod.d.ts +59 -190
  11. package/dist/mod.js +165 -184
  12. package/dist/tests/decimal.test.cjs +3 -3
  13. package/dist/tests/decimal.test.mjs +3 -3
  14. package/dist/tests/{docloader-C76ldE5C.mjs → docloader-B5Upa3Ox.mjs} +97 -10
  15. package/dist/tests/{docloader-mvgIWKI7.cjs → docloader-C69O-D5T.cjs} +102 -9
  16. package/dist/tests/docloader.test.cjs +58 -6
  17. package/dist/tests/docloader.test.mjs +58 -6
  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/{key-CrrK9mYh.mjs → key-CDGDH_vC.mjs} +69 -1
  23. package/dist/tests/{key-pMmqUKuo.cjs → key-_wXwomh_.cjs} +86 -0
  24. package/dist/tests/key.test.cjs +48 -1
  25. package/dist/tests/key.test.mjs +48 -1
  26. package/dist/tests/{request-BEXkv1ul.mjs → request-BGoZTy5L.mjs} +1 -1
  27. package/dist/tests/{request-SworbvLc.cjs → request-DX4gki5x.cjs} +1 -1
  28. package/dist/tests/request.test.cjs +1 -1
  29. package/dist/tests/request.test.mjs +1 -1
  30. package/dist/tests/{url-C20FhC7p.cjs → url-2XwVbUS_.cjs} +108 -0
  31. package/dist/tests/{url-m9Qzxy-Y.mjs → url-YWJbnRlf.mjs} +85 -1
  32. package/dist/tests/url.test.cjs +104 -1
  33. package/dist/tests/url.test.mjs +105 -2
  34. package/dist/url-BAdyyqAa.cjs +315 -0
  35. package/dist/url-BuxPHxK2.js +261 -0
  36. package/package.json +11 -1
  37. package/src/contexts/fep-7aa9.json +24 -0
  38. package/src/contexts.ts +2 -0
  39. package/src/docloader.test.ts +102 -6
  40. package/src/docloader.ts +76 -8
  41. package/src/internal/jsonld-cache.ts +538 -0
  42. package/src/jsonld-cache.test.ts +554 -0
  43. package/src/key.test.ts +86 -0
  44. package/src/key.ts +125 -0
  45. package/src/mod.ts +8 -0
  46. package/src/url.test.ts +252 -1
  47. package/src/url.ts +141 -0
  48. package/tsdown.config.ts +6 -1
@@ -45,6 +45,11 @@ const algorithms = {
45
45
  },
46
46
  "1.3.101.112": "Ed25519"
47
47
  };
48
+ const DID_KEY_PREFIX = "did:key:";
49
+ const ED25519_PUBLIC_KEY_MULTICODEC = 237;
50
+ const ED25519_PUBLIC_KEY_LENGTH = 32;
51
+ const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
52
+ const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
48
53
  /**
49
54
  * Imports a PEM-SPKI formatted public key.
50
55
  * @param pem The PEM-SPKI formatted public key.
@@ -106,6 +111,69 @@ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
106
111
  function importPem(pem) {
107
112
  return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
108
113
  }
114
+ function decodeEd25519DidKeyMultibase(multibaseKey) {
115
+ if (!multibaseKey.startsWith("z")) throw new TypeError("did:key must use base58-btc Multibase encoding.");
116
+ let decoded;
117
+ try {
118
+ decoded = decodeMultibase(multibaseKey);
119
+ } catch (error) {
120
+ throw new TypeError("Invalid did:key Multibase encoding.", { cause: error });
121
+ }
122
+ const { code } = getMulticodecPrefix(decoded);
123
+ if (code !== ED25519_PUBLIC_KEY_MULTICODEC) throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
124
+ const content = removeMulticodecPrefix(decoded);
125
+ if (content.length !== ED25519_PUBLIC_KEY_LENGTH) throw new TypeError("Invalid Ed25519 did:key length.");
126
+ return content;
127
+ }
128
+ /**
129
+ * Imports an Ed25519 `did:key` DID.
130
+ *
131
+ * @param did The `did:key` DID.
132
+ * @returns The imported Ed25519 public key.
133
+ * @throws {TypeError} If the DID is malformed or uses an unsupported key type.
134
+ * @since 2.4.0
135
+ */
136
+ async function importDidKey(did) {
137
+ const match = (did instanceof URL ? did.href : did).match(DID_KEY_PATTERN);
138
+ if (match == null) throw new TypeError("Invalid did:key DID.");
139
+ const content = decodeEd25519DidKeyMultibase(match[1]);
140
+ return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
141
+ }
142
+ /**
143
+ * Exports an Ed25519 public key as a `did:key` DID.
144
+ *
145
+ * @param key The Ed25519 public key.
146
+ * @returns The `did:key` DID.
147
+ * @throws {TypeError} If the key is invalid or unsupported.
148
+ * @since 2.4.0
149
+ */
150
+ async function exportDidKey(key) {
151
+ if (key.algorithm.name !== "Ed25519") throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
152
+ return DID_KEY_PREFIX + await exportMultibaseKey(key);
153
+ }
154
+ /**
155
+ * Parses an Ed25519 `did:key` verification method DID URL.
156
+ *
157
+ * @param didUrl The `did:key` DID URL.
158
+ * @returns The parsed verification method.
159
+ * @throws {TypeError} If the DID URL is malformed, unsupported, or its
160
+ * fragment does not identify the same key as the DID.
161
+ * @since 2.4.0
162
+ */
163
+ async function parseDidKeyVerificationMethod(didUrl) {
164
+ const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
165
+ const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
166
+ if (match == null) throw new TypeError("Invalid did:key verification method.");
167
+ const [, publicKeyMultibase, fragment] = match;
168
+ if (publicKeyMultibase !== fragment) throw new TypeError("Invalid did:key verification method fragment.");
169
+ const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
170
+ return {
171
+ id: new URL(didUrlString),
172
+ controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
173
+ publicKeyMultibase,
174
+ publicKey
175
+ };
176
+ }
109
177
  /**
110
178
  * Imports a [Multibase]-encoded public key.
111
179
  *
@@ -169,4 +237,4 @@ async function exportMultibaseKey(key) {
169
237
  return new TextDecoder().decode(encoded);
170
238
  }
171
239
  //#endregion
172
- export { importPkcs1 as a, importJwk as c, importPem as i, exportSpki as n, importSpki as o, importMultibaseKey as r, exportJwk as s, exportMultibaseKey as t };
240
+ export { importMultibaseKey as a, importSpki as c, importJwk as d, importDidKey as i, parseDidKeyVerificationMethod as l, exportMultibaseKey as n, importPem as o, exportSpki as r, importPkcs1 as s, exportDidKey as t, exportJwk as u };
@@ -46,6 +46,11 @@ const algorithms = {
46
46
  },
47
47
  "1.3.101.112": "Ed25519"
48
48
  };
49
+ const DID_KEY_PREFIX = "did:key:";
50
+ const ED25519_PUBLIC_KEY_MULTICODEC = 237;
51
+ const ED25519_PUBLIC_KEY_LENGTH = 32;
52
+ const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
53
+ const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
49
54
  /**
50
55
  * Imports a PEM-SPKI formatted public key.
51
56
  * @param pem The PEM-SPKI formatted public key.
@@ -107,6 +112,69 @@ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
107
112
  function importPem(pem) {
108
113
  return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
109
114
  }
115
+ function decodeEd25519DidKeyMultibase(multibaseKey) {
116
+ if (!multibaseKey.startsWith("z")) throw new TypeError("did:key must use base58-btc Multibase encoding.");
117
+ let decoded;
118
+ try {
119
+ decoded = require_multibase.decodeMultibase(multibaseKey);
120
+ } catch (error) {
121
+ throw new TypeError("Invalid did:key Multibase encoding.", { cause: error });
122
+ }
123
+ const { code } = require_multicodec.getMulticodecPrefix(decoded);
124
+ if (code !== ED25519_PUBLIC_KEY_MULTICODEC) throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
125
+ const content = require_multicodec.removeMulticodecPrefix(decoded);
126
+ if (content.length !== ED25519_PUBLIC_KEY_LENGTH) throw new TypeError("Invalid Ed25519 did:key length.");
127
+ return content;
128
+ }
129
+ /**
130
+ * Imports an Ed25519 `did:key` DID.
131
+ *
132
+ * @param did The `did:key` DID.
133
+ * @returns The imported Ed25519 public key.
134
+ * @throws {TypeError} If the DID is malformed or uses an unsupported key type.
135
+ * @since 2.4.0
136
+ */
137
+ async function importDidKey(did) {
138
+ const match = (did instanceof URL ? did.href : did).match(DID_KEY_PATTERN);
139
+ if (match == null) throw new TypeError("Invalid did:key DID.");
140
+ const content = decodeEd25519DidKeyMultibase(match[1]);
141
+ return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
142
+ }
143
+ /**
144
+ * Exports an Ed25519 public key as a `did:key` DID.
145
+ *
146
+ * @param key The Ed25519 public key.
147
+ * @returns The `did:key` DID.
148
+ * @throws {TypeError} If the key is invalid or unsupported.
149
+ * @since 2.4.0
150
+ */
151
+ async function exportDidKey(key) {
152
+ if (key.algorithm.name !== "Ed25519") throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
153
+ return DID_KEY_PREFIX + await exportMultibaseKey(key);
154
+ }
155
+ /**
156
+ * Parses an Ed25519 `did:key` verification method DID URL.
157
+ *
158
+ * @param didUrl The `did:key` DID URL.
159
+ * @returns The parsed verification method.
160
+ * @throws {TypeError} If the DID URL is malformed, unsupported, or its
161
+ * fragment does not identify the same key as the DID.
162
+ * @since 2.4.0
163
+ */
164
+ async function parseDidKeyVerificationMethod(didUrl) {
165
+ const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
166
+ const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
167
+ if (match == null) throw new TypeError("Invalid did:key verification method.");
168
+ const [, publicKeyMultibase, fragment] = match;
169
+ if (publicKeyMultibase !== fragment) throw new TypeError("Invalid did:key verification method fragment.");
170
+ const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
171
+ return {
172
+ id: new URL(didUrlString),
173
+ controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
174
+ publicKeyMultibase,
175
+ publicKey
176
+ };
177
+ }
110
178
  /**
111
179
  * Imports a [Multibase]-encoded public key.
112
180
  *
@@ -170,6 +238,12 @@ async function exportMultibaseKey(key) {
170
238
  return new TextDecoder().decode(encoded);
171
239
  }
172
240
  //#endregion
241
+ Object.defineProperty(exports, "exportDidKey", {
242
+ enumerable: true,
243
+ get: function() {
244
+ return exportDidKey;
245
+ }
246
+ });
173
247
  Object.defineProperty(exports, "exportJwk", {
174
248
  enumerable: true,
175
249
  get: function() {
@@ -188,6 +262,12 @@ Object.defineProperty(exports, "exportSpki", {
188
262
  return exportSpki;
189
263
  }
190
264
  });
265
+ Object.defineProperty(exports, "importDidKey", {
266
+ enumerable: true,
267
+ get: function() {
268
+ return importDidKey;
269
+ }
270
+ });
191
271
  Object.defineProperty(exports, "importJwk", {
192
272
  enumerable: true,
193
273
  get: function() {
@@ -218,3 +298,9 @@ Object.defineProperty(exports, "importSpki", {
218
298
  return importSpki;
219
299
  }
220
300
  });
301
+ Object.defineProperty(exports, "parseDidKeyVerificationMethod", {
302
+ enumerable: true,
303
+ get: function() {
304
+ return parseDidKeyVerificationMethod;
305
+ }
306
+ });
@@ -1,5 +1,6 @@
1
1
  require("./chunk-C2EiDwsr.cjs");
2
- const require_key = require("./key-pMmqUKuo.cjs");
2
+ const require_multicodec = require("./multicodec-CxGVGa91.cjs");
3
+ const require_key = require("./key-_wXwomh_.cjs");
3
4
  const require_multibase = require("./multibase-Bz_UUDtL.cjs");
4
5
  let node_assert = require("node:assert");
5
6
  let node_test = require("node:test");
@@ -71,4 +72,50 @@ const ed25519Multibase = "z6MksHj1MJnidCtDiyYW9ugNFftoX9fLK4bornTxmMZ6X7vq";
71
72
  x: "Lm_M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY"
72
73
  }, "public")), "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
73
74
  });
75
+ (0, node_test.test)("exportDidKey() and importDidKey()", async () => {
76
+ const did = await require_key.exportDidKey(await require_key.importJwk(ed25519Jwk, "public"));
77
+ (0, node_assert.deepStrictEqual)(did, `did:key:${ed25519Multibase}`);
78
+ (0, node_assert.deepStrictEqual)(await require_key.exportJwk(await require_key.importDidKey(did)), ed25519Jwk);
79
+ (0, node_assert.deepStrictEqual)(await require_key.exportJwk(await require_key.importDidKey(new URL(did))), ed25519Jwk);
80
+ const rsaKey = await require_key.importJwk(rsaJwk, "public");
81
+ await (0, node_assert.rejects)(() => require_key.exportDidKey(rsaKey), TypeError);
82
+ await (0, node_assert.rejects)(() => require_key.importDidKey(`did:key:${rsaMultibase}`), /* @__PURE__ */ new TypeError("Unsupported did:key type: 0x1205"));
83
+ });
84
+ (0, node_test.test)("parseDidKeyVerificationMethod()", async () => {
85
+ const verificationMethod = `did:key:${ed25519Multibase}#${ed25519Multibase}`;
86
+ const parsed = await require_key.parseDidKeyVerificationMethod(verificationMethod);
87
+ (0, node_assert.deepStrictEqual)(parsed.id, new URL(verificationMethod));
88
+ (0, node_assert.deepStrictEqual)(parsed.controller, new URL(`did:key:${ed25519Multibase}`));
89
+ (0, node_assert.deepStrictEqual)(parsed.publicKeyMultibase, ed25519Multibase);
90
+ (0, node_assert.deepStrictEqual)(await require_key.exportJwk(parsed.publicKey), ed25519Jwk);
91
+ });
92
+ (0, node_test.test)("did:key helpers reject malformed values", async () => {
93
+ const ed25519Key = await require_key.importJwk(ed25519Jwk, "public");
94
+ const raw = new Uint8Array(await crypto.subtle.exportKey("raw", ed25519Key));
95
+ const shortEd25519 = new TextDecoder().decode(require_multibase.encodeMultibase("base58btc", require_multicodec.addMulticodecPrefix(237, raw.slice(0, 31))));
96
+ const base64UrlEd25519 = new TextDecoder().decode(require_multibase.encodeMultibase("base64url", require_multicodec.addMulticodecPrefix(237, raw)));
97
+ const malformedMulticodec = new TextDecoder().decode(require_multibase.encodeMultibase("base58btc", Uint8Array.from([128])));
98
+ for (const value of [
99
+ "did:web:example.com",
100
+ "did:key:",
101
+ `did:key:${ed25519Multibase}/path`,
102
+ `did:key:${ed25519Multibase}?query`,
103
+ `did:key:${ed25519Multibase}#${ed25519Multibase}`,
104
+ "did:key:z0",
105
+ `did:key:${base64UrlEd25519}`,
106
+ `did:key:${shortEd25519}`,
107
+ `did:key:${malformedMulticodec}`
108
+ ]) await (0, node_assert.rejects)(() => require_key.importDidKey(value), TypeError);
109
+ for (const value of [
110
+ `did:key:${ed25519Multibase}`,
111
+ `did:key:${ed25519Multibase}#`,
112
+ `did:key:${ed25519Multibase}#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK`,
113
+ `did:key:${ed25519Multibase}#${base64UrlEd25519}`,
114
+ "did:key:z0#z0",
115
+ `did:key:${rsaMultibase}#${rsaMultibase}`,
116
+ `did:key:${shortEd25519}#${shortEd25519}`,
117
+ `did:key:${malformedMulticodec}#${malformedMulticodec}`,
118
+ "https://example.com/key"
119
+ ]) await (0, node_assert.rejects)(() => require_key.parseDidKeyVerificationMethod(value), TypeError);
120
+ });
74
121
  //#endregion
@@ -1,4 +1,5 @@
1
- import { a as importPkcs1, c as importJwk, i as importPem, n as exportSpki, o as importSpki, r as importMultibaseKey, s as exportJwk, t as exportMultibaseKey } from "./key-CrrK9mYh.mjs";
1
+ import { t as addMulticodecPrefix } from "./multicodec-CyFp54fI.mjs";
2
+ import { a as importMultibaseKey, c as importSpki, d as importJwk, i as importDidKey, l as parseDidKeyVerificationMethod, n as exportMultibaseKey, o as importPem, r as exportSpki, s as importPkcs1, t as exportDidKey, u as exportJwk } from "./key-CDGDH_vC.mjs";
2
3
  import { n as encodeMultibase } from "./multibase-B4bvakyA.mjs";
3
4
  import { deepStrictEqual, rejects } from "node:assert";
4
5
  import { test } from "node:test";
@@ -70,5 +71,51 @@ test("exportMultibaseKey()", async () => {
70
71
  x: "Lm_M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY"
71
72
  }, "public")), "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
72
73
  });
74
+ test("exportDidKey() and importDidKey()", async () => {
75
+ const did = await exportDidKey(await importJwk(ed25519Jwk, "public"));
76
+ deepStrictEqual(did, `did:key:${ed25519Multibase}`);
77
+ deepStrictEqual(await exportJwk(await importDidKey(did)), ed25519Jwk);
78
+ deepStrictEqual(await exportJwk(await importDidKey(new URL(did))), ed25519Jwk);
79
+ const rsaKey = await importJwk(rsaJwk, "public");
80
+ await rejects(() => exportDidKey(rsaKey), TypeError);
81
+ await rejects(() => importDidKey(`did:key:${rsaMultibase}`), /* @__PURE__ */ new TypeError("Unsupported did:key type: 0x1205"));
82
+ });
83
+ test("parseDidKeyVerificationMethod()", async () => {
84
+ const verificationMethod = `did:key:${ed25519Multibase}#${ed25519Multibase}`;
85
+ const parsed = await parseDidKeyVerificationMethod(verificationMethod);
86
+ deepStrictEqual(parsed.id, new URL(verificationMethod));
87
+ deepStrictEqual(parsed.controller, new URL(`did:key:${ed25519Multibase}`));
88
+ deepStrictEqual(parsed.publicKeyMultibase, ed25519Multibase);
89
+ deepStrictEqual(await exportJwk(parsed.publicKey), ed25519Jwk);
90
+ });
91
+ test("did:key helpers reject malformed values", async () => {
92
+ const ed25519Key = await importJwk(ed25519Jwk, "public");
93
+ const raw = new Uint8Array(await crypto.subtle.exportKey("raw", ed25519Key));
94
+ const shortEd25519 = new TextDecoder().decode(encodeMultibase("base58btc", addMulticodecPrefix(237, raw.slice(0, 31))));
95
+ const base64UrlEd25519 = new TextDecoder().decode(encodeMultibase("base64url", addMulticodecPrefix(237, raw)));
96
+ const malformedMulticodec = new TextDecoder().decode(encodeMultibase("base58btc", Uint8Array.from([128])));
97
+ for (const value of [
98
+ "did:web:example.com",
99
+ "did:key:",
100
+ `did:key:${ed25519Multibase}/path`,
101
+ `did:key:${ed25519Multibase}?query`,
102
+ `did:key:${ed25519Multibase}#${ed25519Multibase}`,
103
+ "did:key:z0",
104
+ `did:key:${base64UrlEd25519}`,
105
+ `did:key:${shortEd25519}`,
106
+ `did:key:${malformedMulticodec}`
107
+ ]) await rejects(() => importDidKey(value), TypeError);
108
+ for (const value of [
109
+ `did:key:${ed25519Multibase}`,
110
+ `did:key:${ed25519Multibase}#`,
111
+ `did:key:${ed25519Multibase}#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK`,
112
+ `did:key:${ed25519Multibase}#${base64UrlEd25519}`,
113
+ "did:key:z0#z0",
114
+ `did:key:${rsaMultibase}#${rsaMultibase}`,
115
+ `did:key:${shortEd25519}#${shortEd25519}`,
116
+ `did:key:${malformedMulticodec}#${malformedMulticodec}`,
117
+ "https://example.com/key"
118
+ ]) await rejects(() => parseDidKeyVerificationMethod(value), TypeError);
119
+ });
73
120
  //#endregion
74
121
  export {};
@@ -1,7 +1,7 @@
1
1
  import process from "node:process";
2
2
  //#region deno.json
3
3
  var name = "@fedify/vocab-runtime";
4
- var version = "2.4.0-dev.1570+f1b6aaa3";
4
+ var version = "2.4.0-dev.1573+246bd0b2";
5
5
  //#endregion
6
6
  //#region src/request.ts
7
7
  /**
@@ -3,7 +3,7 @@ let node_process = require("node:process");
3
3
  node_process = require_chunk.__toESM(node_process, 1);
4
4
  //#region deno.json
5
5
  var name = "@fedify/vocab-runtime";
6
- var version = "2.4.0-dev.1570+f1b6aaa3";
6
+ var version = "2.4.0-dev.1573+246bd0b2";
7
7
  //#endregion
8
8
  //#region src/request.ts
9
9
  /**
@@ -1,5 +1,5 @@
1
1
  const require_chunk = require("./chunk-C2EiDwsr.cjs");
2
- const require_request = require("./request-SworbvLc.cjs");
2
+ const require_request = require("./request-DX4gki5x.cjs");
3
3
  let node_assert = require("node:assert");
4
4
  let node_test = require("node:test");
5
5
  let node_process = require("node:process");
@@ -1,4 +1,4 @@
1
- import { o as version, r as getUserAgent } from "./request-BEXkv1ul.mjs";
1
+ import { o as version, r as getUserAgent } from "./request-BGoZTy5L.mjs";
2
2
  import { deepStrictEqual } from "node:assert";
3
3
  import { test } from "node:test";
4
4
  import process from "node:process";
@@ -8,6 +8,90 @@ var UrlError = class extends Error {
8
8
  this.name = "UrlError";
9
9
  }
10
10
  };
11
+ const PORTABLE_IRI_PATTERN = /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
12
+ const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
13
+ const DID_SCHEME_PATTERN = /^did:/i;
14
+ const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
15
+ /**
16
+ * Parses a JSON-LD `@id` value as an IRI.
17
+ */
18
+ function parseJsonLdId(id, base) {
19
+ if (id == null || id.startsWith("_:")) return void 0;
20
+ try {
21
+ return parseIri(id, base);
22
+ } catch {
23
+ throw new TypeError("Invalid @id: " + id);
24
+ }
25
+ }
26
+ /**
27
+ * Parses an IRI as a URL, including FEP-ef61 portable ActivityPub IRIs.
28
+ */
29
+ function parseIri(iri, base) {
30
+ if (iri instanceof URL) return normalizePortableUrl(iri) ?? new URL(iri.href);
31
+ const portable = parsePortableIri(iri);
32
+ if (portable != null) return portable;
33
+ base = normalizeBaseIri(base);
34
+ if (!URL.canParse(iri, base) && iri.startsWith("at://")) return parseAtUri(iri);
35
+ const parsed = new URL(iri, base);
36
+ return normalizePortableUrl(parsed) ?? parsed;
37
+ }
38
+ /**
39
+ * Formats a URL as an IRI, including FEP-ef61 portable ActivityPub IRIs.
40
+ */
41
+ function formatIri(iri) {
42
+ const parsed = parsePortableIri(iri instanceof URL ? iri.href : iri);
43
+ if (parsed == null) return iri instanceof URL ? iri.href : URL.canParse(iri) ? new URL(iri).href : iri;
44
+ return `ap+ef61://${decodePortableAuthority(parsed.host)}${parsed.pathname}${parsed.search}${parsed.hash}`;
45
+ }
46
+ /**
47
+ * Checks whether two IRIs have the same origin.
48
+ */
49
+ function haveSameIriOrigin(left, right) {
50
+ return getComparableIriOrigin(left) === getComparableIriOrigin(right);
51
+ }
52
+ function getComparableIriOrigin(iri) {
53
+ iri = normalizePortableUrl(iri) ?? iri;
54
+ if (iri.origin !== "null") return iri.origin;
55
+ if (iri.host !== "") {
56
+ const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:")) : iri.host;
57
+ return `${iri.protocol}//${host}`;
58
+ }
59
+ return iri.href;
60
+ }
61
+ function parsePortableIri(iri) {
62
+ const match = iri.match(PORTABLE_IRI_PATTERN);
63
+ if (match == null) return null;
64
+ const authority = decodePortableAuthority(match[2]);
65
+ if (!DID_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
66
+ if (match[3] === "") throw new TypeError("Invalid portable ActivityPub IRI path.");
67
+ return new URL(`ap+ef61://${encodeURIComponent(authority)}${match[3]}${match[4] ?? ""}${match[5] ?? ""}`);
68
+ }
69
+ function normalizePortableUrl(iri) {
70
+ if (iri.protocol !== "ap:" && iri.protocol !== "ap+ef61:") return null;
71
+ return parsePortableIri(`ap+ef61://${iri.host}${iri.pathname}${iri.search}${iri.hash}`);
72
+ }
73
+ function normalizeBaseIri(base) {
74
+ if (base == null) return void 0;
75
+ if (base instanceof URL) return normalizePortableUrl(base) ?? base;
76
+ return parsePortableIri(base) ?? (base.startsWith("at://") && !URL.canParse(".", base) ? parseAtUri(base) : base);
77
+ }
78
+ function decodePortableAuthority(authority) {
79
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
80
+ if (DID_SCHEME_PATTERN.test(authority)) {
81
+ const decoded = authority.replace(/%25/gi, "%");
82
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
83
+ return decoded;
84
+ }
85
+ const decoded = authority.replace(/%(25|3A)/gi, (match) => match.toLowerCase() === "%3a" ? ":" : "%");
86
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
87
+ return decoded;
88
+ }
89
+ function parseAtUri(uri) {
90
+ const index = uri.indexOf("/", 5);
91
+ const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
92
+ const path = index >= 0 ? uri.slice(index) : "";
93
+ return new URL("at://" + encodeURIComponent(authority) + path);
94
+ }
11
95
  /**
12
96
  * Validates a URL to prevent SSRF attacks.
13
97
  */
@@ -186,6 +270,18 @@ Object.defineProperty(exports, "expandIPv6Address", {
186
270
  return expandIPv6Address;
187
271
  }
188
272
  });
273
+ Object.defineProperty(exports, "formatIri", {
274
+ enumerable: true,
275
+ get: function() {
276
+ return formatIri;
277
+ }
278
+ });
279
+ Object.defineProperty(exports, "haveSameIriOrigin", {
280
+ enumerable: true,
281
+ get: function() {
282
+ return haveSameIriOrigin;
283
+ }
284
+ });
189
285
  Object.defineProperty(exports, "isValidPublicIPv4Address", {
190
286
  enumerable: true,
191
287
  get: function() {
@@ -198,6 +294,18 @@ Object.defineProperty(exports, "isValidPublicIPv6Address", {
198
294
  return isValidPublicIPv6Address;
199
295
  }
200
296
  });
297
+ Object.defineProperty(exports, "parseIri", {
298
+ enumerable: true,
299
+ get: function() {
300
+ return parseIri;
301
+ }
302
+ });
303
+ Object.defineProperty(exports, "parseJsonLdId", {
304
+ enumerable: true,
305
+ get: function() {
306
+ return parseJsonLdId;
307
+ }
308
+ });
201
309
  Object.defineProperty(exports, "validatePublicUrl", {
202
310
  enumerable: true,
203
311
  get: function() {
@@ -7,6 +7,90 @@ var UrlError = class extends Error {
7
7
  this.name = "UrlError";
8
8
  }
9
9
  };
10
+ const PORTABLE_IRI_PATTERN = /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
11
+ const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
12
+ const DID_SCHEME_PATTERN = /^did:/i;
13
+ const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
14
+ /**
15
+ * Parses a JSON-LD `@id` value as an IRI.
16
+ */
17
+ function parseJsonLdId(id, base) {
18
+ if (id == null || id.startsWith("_:")) return void 0;
19
+ try {
20
+ return parseIri(id, base);
21
+ } catch {
22
+ throw new TypeError("Invalid @id: " + id);
23
+ }
24
+ }
25
+ /**
26
+ * Parses an IRI as a URL, including FEP-ef61 portable ActivityPub IRIs.
27
+ */
28
+ function parseIri(iri, base) {
29
+ if (iri instanceof URL) return normalizePortableUrl(iri) ?? new URL(iri.href);
30
+ const portable = parsePortableIri(iri);
31
+ if (portable != null) return portable;
32
+ base = normalizeBaseIri(base);
33
+ if (!URL.canParse(iri, base) && iri.startsWith("at://")) return parseAtUri(iri);
34
+ const parsed = new URL(iri, base);
35
+ return normalizePortableUrl(parsed) ?? parsed;
36
+ }
37
+ /**
38
+ * Formats a URL as an IRI, including FEP-ef61 portable ActivityPub IRIs.
39
+ */
40
+ function formatIri(iri) {
41
+ const parsed = parsePortableIri(iri instanceof URL ? iri.href : iri);
42
+ if (parsed == null) return iri instanceof URL ? iri.href : URL.canParse(iri) ? new URL(iri).href : iri;
43
+ return `ap+ef61://${decodePortableAuthority(parsed.host)}${parsed.pathname}${parsed.search}${parsed.hash}`;
44
+ }
45
+ /**
46
+ * Checks whether two IRIs have the same origin.
47
+ */
48
+ function haveSameIriOrigin(left, right) {
49
+ return getComparableIriOrigin(left) === getComparableIriOrigin(right);
50
+ }
51
+ function getComparableIriOrigin(iri) {
52
+ iri = normalizePortableUrl(iri) ?? iri;
53
+ if (iri.origin !== "null") return iri.origin;
54
+ if (iri.host !== "") {
55
+ const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:")) : iri.host;
56
+ return `${iri.protocol}//${host}`;
57
+ }
58
+ return iri.href;
59
+ }
60
+ function parsePortableIri(iri) {
61
+ const match = iri.match(PORTABLE_IRI_PATTERN);
62
+ if (match == null) return null;
63
+ const authority = decodePortableAuthority(match[2]);
64
+ if (!DID_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
65
+ if (match[3] === "") throw new TypeError("Invalid portable ActivityPub IRI path.");
66
+ return new URL(`ap+ef61://${encodeURIComponent(authority)}${match[3]}${match[4] ?? ""}${match[5] ?? ""}`);
67
+ }
68
+ function normalizePortableUrl(iri) {
69
+ if (iri.protocol !== "ap:" && iri.protocol !== "ap+ef61:") return null;
70
+ return parsePortableIri(`ap+ef61://${iri.host}${iri.pathname}${iri.search}${iri.hash}`);
71
+ }
72
+ function normalizeBaseIri(base) {
73
+ if (base == null) return void 0;
74
+ if (base instanceof URL) return normalizePortableUrl(base) ?? base;
75
+ return parsePortableIri(base) ?? (base.startsWith("at://") && !URL.canParse(".", base) ? parseAtUri(base) : base);
76
+ }
77
+ function decodePortableAuthority(authority) {
78
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
79
+ if (DID_SCHEME_PATTERN.test(authority)) {
80
+ const decoded = authority.replace(/%25/gi, "%");
81
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
82
+ return decoded;
83
+ }
84
+ const decoded = authority.replace(/%(25|3A)/gi, (match) => match.toLowerCase() === "%3a" ? ":" : "%");
85
+ if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
86
+ return decoded;
87
+ }
88
+ function parseAtUri(uri) {
89
+ const index = uri.indexOf("/", 5);
90
+ const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
91
+ const path = index >= 0 ? uri.slice(index) : "";
92
+ return new URL("at://" + encodeURIComponent(authority) + path);
93
+ }
10
94
  /**
11
95
  * Validates a URL to prevent SSRF attacks.
12
96
  */
@@ -173,4 +257,4 @@ function matchesIPv6Prefix(address, prefixWords, prefixLength) {
173
257
  return true;
174
258
  }
175
259
  //#endregion
176
- export { validatePublicUrl as a, isValidPublicIPv6Address as i, expandIPv6Address as n, isValidPublicIPv4Address as r, UrlError as t };
260
+ 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 };