@fedify/vocab-runtime 2.3.0-dev.994 → 2.3.0-pr.809.36

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 (56) hide show
  1. package/deno.json +4 -3
  2. package/dist/{chunk-CKQMccvm.cjs → chunk-M78iaK0I.cjs} +1 -0
  3. package/dist/jsonld.cjs +3 -2
  4. package/dist/jsonld.d.cts +1 -0
  5. package/dist/jsonld.d.ts +1 -0
  6. package/dist/jsonld.js +1 -0
  7. package/dist/mod.cjs +4456 -4373
  8. package/dist/mod.d.cts +36 -1
  9. package/dist/mod.d.ts +36 -1
  10. package/dist/mod.js +4453 -4370
  11. package/dist/temporal.cjs +60 -0
  12. package/dist/temporal.d.cts +54 -0
  13. package/dist/temporal.d.ts +54 -0
  14. package/dist/temporal.js +58 -0
  15. package/dist/tests/{chunk-Do9eywBl.cjs → chunk-C2EiDwsr.cjs} +1 -1
  16. package/dist/tests/decimal.test.cjs +5 -4
  17. package/dist/tests/decimal.test.mjs +4 -3
  18. package/dist/tests/docloader-5FLgqUWX.cjs +4543 -0
  19. package/dist/tests/docloader-sUZ8fBQ-.mjs +4531 -0
  20. package/dist/tests/docloader.test.cjs +4 -4
  21. package/dist/tests/docloader.test.mjs +4 -4
  22. package/dist/tests/internal/multicodec.test.cjs +1 -1
  23. package/dist/tests/{key-BeTHFQJK.mjs → key-CrrK9mYh.mjs} +1 -1
  24. package/dist/tests/{key-DTTIntwb.cjs → key-pMmqUKuo.cjs} +2 -2
  25. package/dist/tests/key.test.cjs +3 -3
  26. package/dist/tests/key.test.mjs +2 -2
  27. package/dist/tests/langstr.test.cjs +2 -2
  28. package/dist/tests/link.test.cjs +1 -1
  29. package/dist/tests/multibase/multibase.test.cjs +2 -2
  30. package/dist/tests/multibase/multibase.test.mjs +1 -1
  31. package/dist/tests/{multibase-BgU9XRf7.mjs → multibase-B4bvakyA.mjs} +3 -0
  32. package/dist/tests/{multibase-F7LtMMsK.cjs → multibase-Bz_UUDtL.cjs} +5 -2
  33. package/dist/tests/{request-JMzY0HsO.mjs → request-DLMAbwhV.mjs} +1 -1
  34. package/dist/tests/{request-OgmNfphs.cjs → request-KpJUwFOb.cjs} +3 -3
  35. package/dist/tests/request.test.cjs +3 -3
  36. package/dist/tests/request.test.mjs +1 -1
  37. package/dist/tests/temporal.test.cjs +134 -0
  38. package/dist/tests/temporal.test.d.cts +1 -0
  39. package/dist/tests/temporal.test.d.mts +1 -0
  40. package/dist/tests/temporal.test.mjs +134 -0
  41. package/dist/tests/url-C20FhC7p.cjs +206 -0
  42. package/dist/tests/url-m9Qzxy-Y.mjs +176 -0
  43. package/dist/tests/url.test.cjs +56 -2
  44. package/dist/tests/url.test.mjs +55 -1
  45. package/package.json +17 -6
  46. package/src/mod.ts +5 -0
  47. package/src/preprocessor.ts +43 -0
  48. package/src/temporal.test.ts +121 -0
  49. package/src/temporal.ts +74 -0
  50. package/src/url.test.ts +82 -0
  51. package/src/url.ts +199 -24
  52. package/tsdown.config.ts +6 -3
  53. package/dist/tests/docloader-CQYpRois.cjs +0 -4581
  54. package/dist/tests/docloader-xNl5RAkG.mjs +0 -4569
  55. package/dist/tests/url-BQ_kgmCk.mjs +0 -59
  56. package/dist/tests/url-pFuSds44.cjs +0 -89
@@ -0,0 +1,121 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { strictEqual } from "node:assert";
3
+ import { test } from "node:test";
4
+ import { isTemporalDuration, isTemporalInstant } from "./temporal.ts";
5
+
6
+ test("isTemporalInstant() accepts polyfill instances", () => {
7
+ strictEqual(
8
+ isTemporalInstant(Temporal.Instant.from("2026-05-14T00:00:00Z")),
9
+ true,
10
+ );
11
+ });
12
+
13
+ test("isTemporalInstant() accepts spec-compliant non-polyfill objects", () => {
14
+ // Mimics the shape of a native `Temporal.Instant` from a host that does
15
+ // not share class identity with the bundled polyfill.
16
+ const nativeLike = Object.create(null, {
17
+ [Symbol.toStringTag]: { value: "Temporal.Instant" },
18
+ epochNanoseconds: { value: 0n },
19
+ toString: { value: () => "1970-01-01T00:00:00Z" },
20
+ });
21
+ strictEqual(isTemporalInstant(nativeLike), true);
22
+ });
23
+
24
+ test("isTemporalInstant() rejects unrelated values", () => {
25
+ strictEqual(isTemporalInstant(null), false);
26
+ strictEqual(isTemporalInstant(undefined), false);
27
+ strictEqual(isTemporalInstant("2026-05-14T00:00:00Z"), false);
28
+ strictEqual(isTemporalInstant(new Date()), false);
29
+ strictEqual(
30
+ isTemporalInstant(Temporal.Duration.from({ seconds: 1 })),
31
+ false,
32
+ );
33
+ });
34
+
35
+ test("isTemporalInstant() rejects bare objects tagged but missing shape", () => {
36
+ const decoy = Object.create(null, {
37
+ [Symbol.toStringTag]: { value: "Temporal.Instant" },
38
+ });
39
+ strictEqual(isTemporalInstant(decoy), false);
40
+ });
41
+
42
+ test("isTemporalInstant() rejects non-bigint epochNanoseconds", () => {
43
+ const decoy = Object.create(null, {
44
+ [Symbol.toStringTag]: { value: "Temporal.Instant" },
45
+ epochNanoseconds: { value: 0 },
46
+ toString: { value: () => "1970-01-01T00:00:00Z" },
47
+ });
48
+ strictEqual(isTemporalInstant(decoy), false);
49
+ });
50
+
51
+ test("isTemporalInstant() rejects default Object.prototype.toString", () => {
52
+ // A plain object inherits `toString` from `Object.prototype`, so calling
53
+ // it would produce `"[object Temporal.Instant]"` instead of an RFC 3339
54
+ // timestamp. The guard must reject these to keep the serializer honest.
55
+ const decoy = {
56
+ [Symbol.toStringTag]: "Temporal.Instant",
57
+ epochNanoseconds: 0n,
58
+ };
59
+ strictEqual(isTemporalInstant(decoy), false);
60
+ });
61
+
62
+ test("isTemporalDuration() accepts polyfill instances", () => {
63
+ strictEqual(
64
+ isTemporalDuration(Temporal.Duration.from({ hours: 1 })),
65
+ true,
66
+ );
67
+ });
68
+
69
+ test("isTemporalDuration() accepts spec-compliant non-polyfill objects", () => {
70
+ const nativeLike = Object.create(null, {
71
+ [Symbol.toStringTag]: { value: "Temporal.Duration" },
72
+ sign: { value: 0 },
73
+ toString: { value: () => "PT0S" },
74
+ });
75
+ strictEqual(isTemporalDuration(nativeLike), true);
76
+ });
77
+
78
+ test("isTemporalDuration() rejects unrelated values", () => {
79
+ strictEqual(isTemporalDuration(null), false);
80
+ strictEqual(isTemporalDuration(undefined), false);
81
+ strictEqual(isTemporalDuration("PT1H"), false);
82
+ strictEqual(
83
+ isTemporalDuration(Temporal.Instant.from("2026-05-14T00:00:00Z")),
84
+ false,
85
+ );
86
+ });
87
+
88
+ test("isTemporalDuration() rejects bare objects tagged but missing shape", () => {
89
+ const decoy = Object.create(null, {
90
+ [Symbol.toStringTag]: { value: "Temporal.Duration" },
91
+ });
92
+ strictEqual(isTemporalDuration(decoy), false);
93
+ });
94
+
95
+ test("isTemporalDuration() rejects non-number sign", () => {
96
+ const decoy = Object.create(null, {
97
+ [Symbol.toStringTag]: { value: "Temporal.Duration" },
98
+ sign: { value: "0" },
99
+ toString: { value: () => "PT0S" },
100
+ });
101
+ strictEqual(isTemporalDuration(decoy), false);
102
+ });
103
+
104
+ test("isTemporalDuration() rejects out-of-range sign values", () => {
105
+ // Real Temporal.Duration#sign is `-1 | 0 | 1` per spec, so anything else
106
+ // (here, 42) must be rejected even though it is a number.
107
+ const decoy = Object.create(null, {
108
+ [Symbol.toStringTag]: { value: "Temporal.Duration" },
109
+ sign: { value: 42 },
110
+ toString: { value: () => "PT42S" },
111
+ });
112
+ strictEqual(isTemporalDuration(decoy), false);
113
+ });
114
+
115
+ test("isTemporalDuration() rejects default Object.prototype.toString", () => {
116
+ const decoy = {
117
+ [Symbol.toStringTag]: "Temporal.Duration",
118
+ sign: 0,
119
+ };
120
+ strictEqual(isTemporalDuration(decoy), false);
121
+ });
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Type guards for `Temporal` namespace objects.
3
+ *
4
+ * Fedify accepts both runtime polyfills (e.g. `@js-temporal/polyfill`,
5
+ * `temporal-polyfill`) and the host's native `Temporal` implementation
6
+ * (Node.js 26+, Bun, Deno). The guards below rely on `Symbol.toStringTag`,
7
+ * which is mandated by the Temporal specification, so they accept any
8
+ * spec-conformant implementation regardless of which class produced the
9
+ * value.
10
+ *
11
+ * @module
12
+ */
13
+
14
+ /**
15
+ * Checks whether the given value is a `Temporal.Instant` object, regardless
16
+ * of whether it came from a polyfill or the host's native implementation.
17
+ *
18
+ * The guard verifies the spec-mandated `Symbol.toStringTag`, that the
19
+ * `epochNanoseconds` accessor exposes a `bigint`, and that `toString` is
20
+ * not the default inherited from `Object.prototype`. Together they reject
21
+ * bare objects whose tag was set to `"Temporal.Instant"` without exposing
22
+ * the rest of the shape; the `toString` check in particular prevents a
23
+ * spoof from reaching the JSON-LD serializer (which calls `toString()`)
24
+ * and emitting `"[object Temporal.Instant]"` instead of an RFC 3339
25
+ * timestamp.
26
+ *
27
+ * @param value The value to test.
28
+ * @returns `true` if the value reports `Temporal.Instant` via
29
+ * `Symbol.toStringTag`, exposes a `bigint`-valued
30
+ * `epochNanoseconds`, and overrides `toString`; `false` otherwise.
31
+ */
32
+ export function isTemporalInstant(value: unknown): value is Temporal.Instant {
33
+ return (
34
+ typeof value === "object" &&
35
+ value !== null &&
36
+ Object.prototype.toString.call(value) === "[object Temporal.Instant]" &&
37
+ "epochNanoseconds" in value &&
38
+ typeof value.epochNanoseconds === "bigint" &&
39
+ "toString" in value &&
40
+ typeof value.toString === "function" &&
41
+ value.toString !== Object.prototype.toString
42
+ );
43
+ }
44
+
45
+ /**
46
+ * Checks whether the given value is a `Temporal.Duration` object, regardless
47
+ * of whether it came from a polyfill or the host's native implementation.
48
+ *
49
+ * The guard verifies the spec-mandated `Symbol.toStringTag`, that the
50
+ * `sign` accessor returns one of the three spec-valid values (`-1`, `0`,
51
+ * or `1`), and that `toString` is not the default inherited from
52
+ * `Object.prototype`. Together they reject bare objects whose tag was set
53
+ * to `"Temporal.Duration"` without exposing the rest of the shape; the
54
+ * `toString` check in particular prevents a spoof from reaching the
55
+ * JSON-LD serializer (which calls `toString()`) and emitting
56
+ * `"[object Temporal.Duration]"` instead of an ISO 8601 duration.
57
+ *
58
+ * @param value The value to test.
59
+ * @returns `true` if the value reports `Temporal.Duration` via
60
+ * `Symbol.toStringTag`, exposes a `sign` of `-1`, `0`, or `1`,
61
+ * and overrides `toString`; `false` otherwise.
62
+ */
63
+ export function isTemporalDuration(value: unknown): value is Temporal.Duration {
64
+ return (
65
+ typeof value === "object" &&
66
+ value !== null &&
67
+ Object.prototype.toString.call(value) === "[object Temporal.Duration]" &&
68
+ "sign" in value &&
69
+ (value.sign === -1 || value.sign === 0 || value.sign === 1) &&
70
+ "toString" in value &&
71
+ typeof value.toString === "function" &&
72
+ value.toString !== Object.prototype.toString
73
+ );
74
+ }
package/src/url.test.ts CHANGED
@@ -19,6 +19,50 @@ test("validatePublicUrl()", async () => {
19
19
  await rejects(() => validatePublicUrl("https://localhost"), UrlError);
20
20
  await rejects(() => validatePublicUrl("https://127.0.0.1"), UrlError);
21
21
  await rejects(() => validatePublicUrl("https://[::1]"), UrlError);
22
+ await rejects(
23
+ () => validatePublicUrl("http://[::ffff:7f00:1]/"),
24
+ UrlError,
25
+ );
26
+ await rejects(
27
+ () => validatePublicUrl("https://[64:ff9b::7f00:1]/"),
28
+ UrlError,
29
+ );
30
+ await rejects(
31
+ () => validatePublicUrl("https://[64:ff9b::a00:1]/"),
32
+ UrlError,
33
+ );
34
+ await rejects(
35
+ () => validatePublicUrl("https://[64:ff9b:1::a00:1]/"),
36
+ UrlError,
37
+ );
38
+ await rejects(
39
+ () => validatePublicUrl("https://[64:ff9b:1::808:808]/"),
40
+ UrlError,
41
+ );
42
+ await rejects(
43
+ () => validatePublicUrl("https://[2001::]/"),
44
+ UrlError,
45
+ );
46
+ await rejects(
47
+ () => validatePublicUrl("https://[2002:a00:1::]/"),
48
+ UrlError,
49
+ );
50
+ for (
51
+ const url of [
52
+ "https://100.64.0.1",
53
+ "https://198.18.0.1",
54
+ "https://224.0.0.1",
55
+ "https://240.0.0.1",
56
+ "https://192.0.2.1",
57
+ "https://192.88.99.1",
58
+ "https://198.51.100.1",
59
+ "https://203.0.113.1",
60
+ ]
61
+ ) {
62
+ await rejects(() => validatePublicUrl(url), UrlError);
63
+ }
64
+ await validatePublicUrl("https://[2001:db8::1]");
65
+ await validatePublicUrl("https://[64:ff9b::8.8.8.8]");
22
66
  });
23
67
 
24
68
  test("isValidPublicIPv4Address()", () => {
@@ -28,6 +72,24 @@ test("isValidPublicIPv4Address()", () => {
28
72
  ok(!isValidPublicIPv4Address("10.0.0.1")); // private
29
73
  ok(!isValidPublicIPv4Address("127.16.0.1")); // private
30
74
  ok(!isValidPublicIPv4Address("169.254.0.1")); // link-local
75
+ ok(!isValidPublicIPv4Address("100.64.0.1")); // shared address space
76
+ ok(!isValidPublicIPv4Address("100.127.255.255"));
77
+ ok(!isValidPublicIPv4Address("192.0.0.1")); // IETF protocol
78
+ ok(!isValidPublicIPv4Address("192.0.2.1")); // documentation
79
+ ok(!isValidPublicIPv4Address("192.88.99.0")); // 6to4 relay anycast
80
+ ok(!isValidPublicIPv4Address("192.88.99.1"));
81
+ ok(!isValidPublicIPv4Address("192.88.99.2")); // 6a44 relay anycast
82
+ ok(!isValidPublicIPv4Address("192.88.99.255"));
83
+ ok(!isValidPublicIPv4Address("198.18.0.1")); // benchmarking
84
+ ok(!isValidPublicIPv4Address("198.19.255.255"));
85
+ ok(!isValidPublicIPv4Address("198.51.100.1")); // documentation
86
+ ok(!isValidPublicIPv4Address("203.0.113.1")); // documentation
87
+ ok(!isValidPublicIPv4Address("224.0.0.1")); // multicast
88
+ ok(!isValidPublicIPv4Address("239.255.255.255"));
89
+ ok(!isValidPublicIPv4Address("240.0.0.1")); // reserved
90
+ ok(!isValidPublicIPv4Address("255.255.255.255")); // broadcast
91
+ ok(!isValidPublicIPv4Address("1.2.3"));
92
+ ok(!isValidPublicIPv4Address("999.1.1.1"));
31
93
  });
32
94
 
33
95
  test("isValidPublicIPv6Address()", () => {
@@ -37,6 +99,22 @@ test("isValidPublicIPv6Address()", () => {
37
99
  ok(!isValidPublicIPv6Address("fe80::1")); // link-local
38
100
  ok(!isValidPublicIPv6Address("ff00::1")); // multicast
39
101
  ok(!isValidPublicIPv6Address("::")); // unspecified
102
+ ok(!isValidPublicIPv6Address("::ffff:7f00:1")); // IPv4-mapped
103
+ ok(!isValidPublicIPv6Address("64:ff9b::7f00:1")); // NAT64 localhost
104
+ ok(!isValidPublicIPv6Address("64:ff9b::127.0.0.1"));
105
+ ok(!isValidPublicIPv6Address("64:ff9b::a00:1")); // NAT64 private
106
+ ok(!isValidPublicIPv6Address("64:ff9b::10.0.0.1"));
107
+ ok(!isValidPublicIPv6Address("64:ff9b:1::")); // local-use NAT64
108
+ ok(!isValidPublicIPv6Address("64:ff9b:1::a00:1"));
109
+ ok(!isValidPublicIPv6Address("64:ff9b:1::10.0.0.1"));
110
+ ok(!isValidPublicIPv6Address("2001::")); // Teredo
111
+ ok(!isValidPublicIPv6Address("2001:0:4136:e378:8000:63bf:3fff:fdd2"));
112
+ ok(!isValidPublicIPv6Address("2002:a00:1::")); // 6to4
113
+ ok(!isValidPublicIPv6Address("2002:7f00:1::"));
114
+ ok(!isValidPublicIPv6Address("2002:c0a8:1::"));
115
+ ok(!isValidPublicIPv6Address("2002:a9fe:1::"));
116
+ ok(isValidPublicIPv6Address("64:ff9b::808:808")); // NAT64 public
117
+ ok(isValidPublicIPv6Address("64:ff9b::8.8.8.8"));
40
118
  });
41
119
 
42
120
  test("expandIPv6Address()", () => {
@@ -56,4 +134,8 @@ test("expandIPv6Address()", () => {
56
134
  expandIPv6Address("2001:db8::1"),
57
135
  "2001:0db8:0000:0000:0000:0000:0000:0001",
58
136
  );
137
+ deepStrictEqual(
138
+ expandIPv6Address("64:ff9b::8.8.8.8"),
139
+ "0064:ff9b:0000:0000:0000:0000:0808:0808",
140
+ );
59
141
  });
package/src/url.ts CHANGED
@@ -19,11 +19,16 @@ export async function validatePublicUrl(url: string): Promise<void> {
19
19
  }
20
20
  let hostname = parsed.hostname;
21
21
  if (hostname.startsWith("[") && hostname.endsWith("]")) {
22
- hostname = hostname.substring(1, hostname.length - 2);
22
+ hostname = hostname.slice(1, -1);
23
23
  }
24
24
  if (hostname === "localhost") {
25
25
  throw new UrlError("Localhost is not allowed");
26
26
  }
27
+ const hostnameFamily = isIP(hostname);
28
+ if (hostnameFamily !== 0) {
29
+ validatePublicIpAddress(hostname, hostnameFamily);
30
+ return;
31
+ }
27
32
  if ("Deno" in globalThis && !isIP(hostname)) {
28
33
  // If the `net` permission is not granted, we can't resolve the hostname.
29
34
  // However, we can safely assume that it cannot gain access to private
@@ -50,40 +55,60 @@ export async function validatePublicUrl(url: string): Promise<void> {
50
55
  addresses = [];
51
56
  }
52
57
  for (const { address, family } of addresses) {
53
- if (
54
- family === 4 && !isValidPublicIPv4Address(address) ||
55
- family === 6 && !isValidPublicIPv6Address(address) ||
56
- family < 4 || family === 5 || family > 6
57
- ) {
58
- throw new UrlError(`Invalid or private address: ${address}`);
59
- }
58
+ validatePublicIpAddress(address, family);
59
+ }
60
+ }
61
+
62
+ function validatePublicIpAddress(address: string, family: number): void {
63
+ if (
64
+ family === 4 && isValidPublicIPv4Address(address) ||
65
+ family === 6 && isValidPublicIPv6Address(address)
66
+ ) {
67
+ return;
60
68
  }
69
+ throw new UrlError(`Invalid or private address: ${address}`);
61
70
  }
62
71
 
63
72
  export function isValidPublicIPv4Address(address: string): boolean {
64
- const parts = address.split(".");
65
- const first = parseInt(parts[0]);
66
- if (first === 0 || first === 10 || first === 127) return false;
67
- const second = parseInt(parts[1]);
68
- if (first === 169 && second === 254) return false;
69
- if (first === 172 && second >= 16 && second <= 31) return false;
70
- if (first === 192 && second === 168) return false;
71
- return true;
73
+ const parts = parseIPv4Address(address);
74
+ if (parts == null) return false;
75
+ const value = ipv4PartsToNumber(parts);
76
+ return !nonPublicIPv4Prefixes.some(({ base, prefix }) =>
77
+ matchesIPv4Prefix(value, base, prefix)
78
+ );
72
79
  }
73
80
 
74
81
  export function isValidPublicIPv6Address(address: string): boolean {
75
- address = expandIPv6Address(address);
76
- if (address.at(4) !== ":") return false;
77
- const firstWord = parseInt(address.substring(0, 4), 16);
78
- return !(
79
- (firstWord >= 0xfc00 && firstWord <= 0xfdff) || // ULA
80
- (firstWord >= 0xfe80 && firstWord <= 0xfebf) || // Link-local
81
- firstWord === 0 || firstWord >= 0xff00 // Multicast
82
- );
82
+ const words = parseIPv6Address(address);
83
+ if (words == null) return false;
84
+ if (
85
+ nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) =>
86
+ matchesIPv6Prefix(words, prefixWords, prefix)
87
+ )
88
+ ) return false;
89
+ for (
90
+ const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes
91
+ ) {
92
+ if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
93
+ const ipv4Address = extractIPv4(words);
94
+ if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) {
95
+ return false;
96
+ }
97
+ }
98
+ return true;
83
99
  }
84
100
 
85
101
  export function expandIPv6Address(address: string): string {
86
102
  address = address.toLowerCase();
103
+ const ipv4Delimiter = address.lastIndexOf(":");
104
+ if (address.includes(".") && ipv4Delimiter >= 0) {
105
+ const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
106
+ if (ipv4Parts == null) return address;
107
+ const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
108
+ const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
109
+ address = address.substring(0, ipv4Delimiter + 1) +
110
+ high.toString(16) + ":" + low.toString(16);
111
+ }
87
112
  if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
88
113
  if (address.startsWith("::")) address = "0000" + address;
89
114
  if (address.endsWith("::")) address = address + "0000";
@@ -94,3 +119,153 @@ export function expandIPv6Address(address: string): string {
94
119
  const parts = address.split(":");
95
120
  return parts.map((part) => part.padStart(4, "0")).join(":");
96
121
  }
122
+
123
+ type IPv4Prefix = {
124
+ cidr: string;
125
+ base: number;
126
+ prefix: number;
127
+ rfc: string;
128
+ };
129
+
130
+ // Keep CIDR and RFC metadata in the table instead of row comments so security
131
+ // reviewers can audit each blocked range without duplicating source text.
132
+ const nonPublicIPv4Prefixes = [
133
+ ipv4Prefix("0.0.0.0/8", "RFC 6890"),
134
+ ipv4Prefix("10.0.0.0/8", "RFC 1918"),
135
+ ipv4Prefix("100.64.0.0/10", "RFC 6598"),
136
+ ipv4Prefix("127.0.0.0/8", "RFC 1122"),
137
+ ipv4Prefix("169.254.0.0/16", "RFC 3927"),
138
+ ipv4Prefix("172.16.0.0/12", "RFC 1918"),
139
+ ipv4Prefix("192.0.0.0/24", "RFC 6890"),
140
+ ipv4Prefix("192.0.2.0/24", "RFC 5737"),
141
+ ipv4Prefix("192.88.99.0/24", "RFC 7526"),
142
+ ipv4Prefix("192.168.0.0/16", "RFC 1918"),
143
+ ipv4Prefix("198.18.0.0/15", "RFC 2544"),
144
+ ipv4Prefix("198.51.100.0/24", "RFC 5737"),
145
+ ipv4Prefix("203.0.113.0/24", "RFC 5737"),
146
+ ipv4Prefix("224.0.0.0/4", "RFC 5771"),
147
+ ipv4Prefix("240.0.0.0/4", "RFC 1112"),
148
+ ];
149
+
150
+ type IPv6Prefix = {
151
+ cidr: string;
152
+ words: number[];
153
+ prefix: number;
154
+ rfc: string;
155
+ };
156
+
157
+ const nonPublicIPv6Prefixes = [
158
+ ipv6Prefix("::/16", "RFC 4291"),
159
+ ipv6Prefix("2001::/32", "RFC 4380"),
160
+ ipv6Prefix("2002::/16", "RFC 3056"),
161
+ ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
162
+ ipv6Prefix("fc00::/7", "RFC 4193"),
163
+ ipv6Prefix("fe80::/10", "RFC 4291"),
164
+ ipv6Prefix("ff00::/8", "RFC 4291"),
165
+ ];
166
+
167
+ type IPv6WithIPv4Prefix = IPv6Prefix & {
168
+ extractIPv4: (words: number[]) => string | null;
169
+ };
170
+
171
+ // This table has one entry for now, but keeps embedded IPv4 extraction aligned
172
+ // with the CIDR metadata above if another translation prefix needs it later.
173
+ const ipv6WithIPv4Prefixes: IPv6WithIPv4Prefix[] = [
174
+ {
175
+ ...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
176
+ extractIPv4: (words) => ipv4FromWords(words[6], words[7]),
177
+ },
178
+ ];
179
+
180
+ function ipv4Prefix(cidr: string, rfc: string): IPv4Prefix {
181
+ const [address, prefixText] = cidr.split("/");
182
+ const prefix = parseInt(prefixText, 10);
183
+ const parts = parseIPv4Address(address);
184
+ if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) {
185
+ throw new Error(`Invalid IPv4 prefix: ${cidr}`);
186
+ }
187
+ return { cidr, base: ipv4PartsToNumber(parts), prefix, rfc };
188
+ }
189
+
190
+ function ipv6Prefix(cidr: string, rfc: string): IPv6Prefix {
191
+ const [address, prefixText] = cidr.split("/");
192
+ const prefix = parseInt(prefixText, 10);
193
+ const words = parseIPv6Address(address);
194
+ if (
195
+ words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128
196
+ ) {
197
+ throw new Error(`Invalid IPv6 prefix: ${cidr}`);
198
+ }
199
+ return { cidr, words, prefix, rfc };
200
+ }
201
+
202
+ function parseIPv4Address(address: string): number[] | null {
203
+ const parts = address.split(".").map((part) => {
204
+ if (!/^\d+$/.test(part)) return NaN;
205
+ return parseInt(part, 10);
206
+ });
207
+ // Keep explicit bounds checks even though the regex narrows today's parser;
208
+ // they make future parser changes fail closed.
209
+ if (
210
+ parts.length !== 4 ||
211
+ parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)
212
+ ) return null;
213
+ return parts;
214
+ }
215
+
216
+ function parseIPv6Address(address: string): number[] | null {
217
+ const parts = expandIPv6Address(address).split(":");
218
+ if (parts.length !== 8) return null;
219
+ const words = parts.map((part) => {
220
+ if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
221
+ return parseInt(part, 16);
222
+ });
223
+ // Keep explicit bounds checks even though the regex narrows today's parser;
224
+ // they make future parser changes fail closed.
225
+ if (
226
+ words.some((word) => !Number.isInteger(word) || word < 0 || word > 0xffff)
227
+ ) return null;
228
+ return words;
229
+ }
230
+
231
+ function ipv4PartsToNumber(parts: number[]): number {
232
+ return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 +
233
+ parts[3];
234
+ }
235
+
236
+ function ipv4FromWords(highWord: number, lowWord: number): string {
237
+ return [
238
+ highWord >> 8,
239
+ highWord & 0xff,
240
+ lowWord >> 8,
241
+ lowWord & 0xff,
242
+ ].join(".");
243
+ }
244
+
245
+ function matchesIPv4Prefix(
246
+ address: number,
247
+ prefixBase: number,
248
+ prefixLength: number,
249
+ ): boolean {
250
+ const blockSize = 2 ** (32 - prefixLength);
251
+ return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
252
+ }
253
+
254
+ function matchesIPv6Prefix(
255
+ address: number[],
256
+ prefixWords: number[],
257
+ prefixLength: number,
258
+ ): boolean {
259
+ let remaining = prefixLength;
260
+ for (let i = 0; i < 8 && remaining > 0; i++) {
261
+ if (remaining >= 16) {
262
+ if (address[i] !== prefixWords[i]) return false;
263
+ remaining -= 16;
264
+ } else {
265
+ const mask = (0xffff << (16 - remaining)) & 0xffff;
266
+ if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
267
+ remaining = 0;
268
+ }
269
+ }
270
+ return true;
271
+ }
package/tsdown.config.ts CHANGED
@@ -4,11 +4,14 @@ import { defineConfig } from "tsdown";
4
4
 
5
5
  export default [
6
6
  defineConfig({
7
- entry: ["src/mod.ts", "src/jsonld.ts"],
7
+ entry: ["src/mod.ts", "src/jsonld.ts", "src/temporal.ts"],
8
8
  dts: { compilerOptions: { isolatedDeclarations: true, declaration: true } },
9
9
  format: ["esm", "cjs"],
10
10
  platform: "neutral",
11
- external: [/^node:/],
11
+ deps: { neverBundle: [/^node:/] },
12
+ banner: {
13
+ dts: `/// <reference lib="esnext.temporal" />`,
14
+ },
12
15
  }),
13
16
  defineConfig({
14
17
  outDir: "dist/tests",
@@ -16,6 +19,6 @@ export default [
16
19
  .map((f) => f.replace(sep, "/")),
17
20
  format: ["esm", "cjs"],
18
21
  platform: "node",
19
- external: [/^node:/, "@fedify/fixture"],
22
+ deps: { neverBundle: [/^node:/, "@fedify/fixture"] },
20
23
  }),
21
24
  ];