@fedify/vocab-runtime 2.1.0-dev.565 → 2.1.0-dev.599

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 (71) hide show
  1. package/deno.json +4 -3
  2. package/dist/chunk-CUT6urMc.cjs +30 -0
  3. package/dist/jsonld.cjs +8 -0
  4. package/dist/jsonld.d.cts +6 -0
  5. package/dist/jsonld.d.ts +6 -0
  6. package/dist/jsonld.js +7 -0
  7. package/dist/mod.cjs +4485 -4329
  8. package/dist/mod.d.cts +95 -4
  9. package/dist/mod.d.ts +95 -4
  10. package/dist/mod.js +4474 -4299
  11. package/dist/tests/decimal.test.cjs +154 -0
  12. package/dist/tests/decimal.test.d.cts +1 -0
  13. package/dist/tests/decimal.test.d.ts +1 -0
  14. package/dist/tests/decimal.test.js +153 -0
  15. package/dist/tests/docloader-B7jXFZpf.cjs +4562 -0
  16. package/dist/tests/docloader-DMCOWvVB.js +4550 -0
  17. package/dist/tests/docloader.test.cjs +7 -4491
  18. package/dist/tests/docloader.test.js +4 -4488
  19. package/dist/tests/internal/multicodec.test.cjs +77 -0
  20. package/dist/tests/internal/multicodec.test.d.cts +1 -0
  21. package/dist/tests/internal/multicodec.test.d.ts +1 -0
  22. package/dist/tests/internal/multicodec.test.js +76 -0
  23. package/dist/tests/key-ByCmSI2y.js +183 -0
  24. package/dist/tests/key-CCPn6TEY.cjs +231 -0
  25. package/dist/tests/key.test.cjs +36 -212
  26. package/dist/tests/key.test.js +9 -185
  27. package/dist/tests/langstr-BsVE3s9u.js +30 -0
  28. package/dist/tests/langstr-EPh86hXK.cjs +36 -0
  29. package/dist/tests/langstr.test.cjs +5 -33
  30. package/dist/tests/langstr.test.js +1 -29
  31. package/dist/tests/link.test.cjs +1 -1
  32. package/dist/tests/link.test.js +1 -1
  33. package/dist/tests/multibase/multibase.test.cjs +1 -1
  34. package/dist/tests/multibase/multibase.test.js +1 -1
  35. package/dist/tests/multicodec--6hQ74zI.cjs +59 -0
  36. package/dist/tests/multicodec-Dq3IiOV4.js +41 -0
  37. package/dist/tests/{request-3ywvRFy1.js → request-BQtyeAfw.js} +6 -3
  38. package/dist/tests/{request-Dhnqve0g.cjs → request-CW9KOmQu.cjs} +6 -3
  39. package/dist/tests/request.test.cjs +1 -1
  40. package/dist/tests/request.test.js +1 -1
  41. package/dist/tests/url.test.cjs +1 -1
  42. package/dist/tests/url.test.js +1 -1
  43. package/package.json +12 -2
  44. package/src/contexts/activitystreams.json +379 -0
  45. package/src/contexts/did-v1.json +57 -0
  46. package/src/contexts/fep-5711.json +36 -0
  47. package/src/contexts/gotosocial.json +86 -0
  48. package/src/contexts/identity-v1.json +152 -0
  49. package/src/contexts/joinmastodon.json +28 -0
  50. package/src/contexts/schemaorg.json +8845 -0
  51. package/src/contexts/security-data-integrity-v1.json +78 -0
  52. package/src/contexts/security-data-integrity-v2.json +81 -0
  53. package/src/contexts/security-multikey-v1.json +35 -0
  54. package/src/contexts/security-v1.json +74 -0
  55. package/src/contexts/webfinger.json +10 -0
  56. package/src/contexts.ts +33 -4392
  57. package/src/decimal.test.ts +90 -0
  58. package/src/decimal.ts +112 -0
  59. package/src/internal/multicodec.test.ts +68 -0
  60. package/src/internal/multicodec.ts +53 -0
  61. package/src/jsonld.ts +4 -0
  62. package/src/key.test.ts +22 -1
  63. package/src/key.ts +9 -8
  64. package/src/mod.ts +6 -0
  65. package/tsdown.config.ts +1 -1
  66. /package/dist/tests/{link-Ck2yj4dH.js → link-C3q2TC2G.js} +0 -0
  67. /package/dist/tests/{link-CdFPEo9O.cjs → link-DYNFAdNu.cjs} +0 -0
  68. /package/dist/tests/{multibase-DStmqni9.js → multibase-B4g8pz6F.js} +0 -0
  69. /package/dist/tests/{multibase-BFbBiaPE.cjs → multibase-o_ovPHYJ.cjs} +0 -0
  70. /package/dist/tests/{url-fW_DHbih.js → url-CWEP9Zs9.js} +0 -0
  71. /package/dist/tests/{url-C5Vs9nYh.cjs → url-DIjOdK8Q.cjs} +0 -0
@@ -0,0 +1,90 @@
1
+ import { deepStrictEqual, throws } from "node:assert";
2
+ import { test } from "node:test";
3
+ import { canParseDecimal, isDecimal, parseDecimal } from "./decimal.ts";
4
+ import {
5
+ canParseDecimal as canParseDecimalFromModule,
6
+ isDecimal as isDecimalFromModule,
7
+ parseDecimal as parseDecimalFromModule,
8
+ } from "./mod.ts";
9
+
10
+ test("parseDecimal() accepts valid xsd:decimal lexical forms", () => {
11
+ const values = [
12
+ "-1.23",
13
+ "12678967.543233",
14
+ "+100000.00",
15
+ "210",
16
+ ".5",
17
+ "5.",
18
+ "0",
19
+ "-0.0",
20
+ ];
21
+
22
+ for (const value of values) {
23
+ deepStrictEqual(parseDecimal(value), value);
24
+ }
25
+ });
26
+
27
+ test("isDecimal() reports valid xsd:decimal lexical forms", () => {
28
+ deepStrictEqual(isDecimal("12.50"), true);
29
+ deepStrictEqual(isDecimal(".5"), true);
30
+ deepStrictEqual(isDecimal("1e3"), false);
31
+ deepStrictEqual(isDecimal(" 12.50 "), false);
32
+ deepStrictEqual(isDecimal("\t12.50\n"), false);
33
+ });
34
+
35
+ test("canParseDecimal() accepts whitespace-normalized xsd:decimal strings", () => {
36
+ deepStrictEqual(canParseDecimal("12.50"), true);
37
+ deepStrictEqual(canParseDecimal(" 12.50 "), true);
38
+ deepStrictEqual(canParseDecimal("\t+100000.00\r\n"), true);
39
+ deepStrictEqual(canParseDecimal(" .5 "), true);
40
+ deepStrictEqual(canParseDecimal("1e3"), false);
41
+ deepStrictEqual(canParseDecimal("1 2.50"), false);
42
+ deepStrictEqual(canParseDecimal("1\t2.50"), false);
43
+ });
44
+
45
+ test("parseDecimal() normalizes XML Schema whitespace", () => {
46
+ deepStrictEqual(parseDecimal("12.50"), "12.50");
47
+ deepStrictEqual(parseDecimal(" 12.50 "), "12.50");
48
+ deepStrictEqual(parseDecimal("\t+100000.00\r\n"), "+100000.00");
49
+ deepStrictEqual(parseDecimal(" .5 "), ".5");
50
+ });
51
+
52
+ test("parseDecimal() rejects invalid xsd:decimal lexical forms", () => {
53
+ const values = [
54
+ "",
55
+ ".",
56
+ "+",
57
+ "-",
58
+ "1e3",
59
+ "NaN",
60
+ "INF",
61
+ "1,2",
62
+ "1..2",
63
+ "1 2.3",
64
+ "1\t2.3",
65
+ ];
66
+
67
+ for (const value of values) {
68
+ throws(
69
+ () => parseDecimal(value),
70
+ {
71
+ name: "TypeError",
72
+ message: `${
73
+ JSON.stringify(value)
74
+ } is not a valid xsd:decimal lexical form.`,
75
+ },
76
+ );
77
+ }
78
+ });
79
+
80
+ test("parseDecimal() is exported from the package root", () => {
81
+ deepStrictEqual(parseDecimalFromModule("12.50"), "12.50");
82
+ });
83
+
84
+ test("canParseDecimal() is exported from the package root", () => {
85
+ deepStrictEqual(canParseDecimalFromModule(" 12.50 "), true);
86
+ });
87
+
88
+ test("isDecimal() is exported from the package root", () => {
89
+ deepStrictEqual(isDecimalFromModule("12.50"), true);
90
+ });
package/src/decimal.ts ADDED
@@ -0,0 +1,112 @@
1
+ const DECIMAL_PATTERN = /^(\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)$/;
2
+ const XML_SCHEMA_WHITESPACE_PATTERN = /[\t\n\r ]+/g;
3
+
4
+ function collapseXmlSchemaWhitespace(value: string): string {
5
+ return value.replace(XML_SCHEMA_WHITESPACE_PATTERN, " ").trim();
6
+ }
7
+
8
+ /**
9
+ * A branded string representing an `xsd:decimal` value.
10
+ *
11
+ * Unlike JavaScript's `number`, `xsd:decimal` is intended for exact decimal
12
+ * values such as prices, quantities, and measurements where binary
13
+ * floating-point rounding would be inappropriate. Fedify therefore represents
14
+ * these values as validated strings at runtime while preserving a distinct
15
+ * TypeScript type.
16
+ *
17
+ * Values of this type must be created through {@link parseDecimal}, which
18
+ * validates that the string matches the XML Schema `xsd:decimal` lexical form.
19
+ *
20
+ * The runtime representation is still a plain string. The brand exists only
21
+ * at the type level so APIs can distinguish arbitrary strings from validated
22
+ * decimal literals without introducing a decimal arithmetic dependency.
23
+ *
24
+ * Supported lexical forms include signed and unsigned integers and decimal
25
+ * fractions such as `"-1.23"`, `"+100000.00"`, `"210"`, `".5"`, and `"5."`.
26
+ * Scientific notation such as `"1e3"` and special values like `"NaN"` are
27
+ * rejected. Strings with surrounding XML Schema whitespace can be normalized
28
+ * by {@link parseDecimal}, but values of this type are always stored in their
29
+ * normalized lexical form.
30
+ *
31
+ * This representation is designed to be forward-compatible with a future
32
+ * native decimal type if JavaScript eventually gains one, while keeping the
33
+ * public API semantically precise today.
34
+ *
35
+ * @since 2.1.0
36
+ */
37
+ export type Decimal = string & { readonly __brand: "Decimal" };
38
+
39
+ /**
40
+ * Checks whether a string is a valid `xsd:decimal` lexical form.
41
+ *
42
+ * This predicate checks the lexical form strictly, without applying XML Schema
43
+ * whitespace normalization first. It is useful as a type guard for values
44
+ * that are already expected to be normalized decimal strings.
45
+ *
46
+ * @param value A candidate `xsd:decimal` lexical form.
47
+ * @returns `true` if the string matches the XML Schema `xsd:decimal` lexical
48
+ * form, or `false` otherwise.
49
+ * @since 2.1.0
50
+ */
51
+ export function isDecimal(value: string): value is Decimal {
52
+ return DECIMAL_PATTERN.test(value);
53
+ }
54
+
55
+ /**
56
+ * Checks whether a string can be parsed as an `xsd:decimal` lexical form.
57
+ *
58
+ * Unlike {@link isDecimal}, this predicate first applies the XML Schema
59
+ * `whiteSpace="collapse"` normalization step and then validates the
60
+ * normalized string. This means values like `" 12.50 "` are parseable even
61
+ * though they are not already normalized decimal literals.
62
+ *
63
+ * @param value A candidate `xsd:decimal` lexical form.
64
+ * @returns `true` if the normalized string matches the XML Schema
65
+ * `xsd:decimal` lexical form, or `false` otherwise.
66
+ * @since 2.1.0
67
+ */
68
+ export function canParseDecimal(value: string): boolean {
69
+ return isDecimal(collapseXmlSchemaWhitespace(value));
70
+ }
71
+
72
+ /**
73
+ * Parses a string as an `xsd:decimal` lexical form and returns it as a
74
+ * branded {@link Decimal}.
75
+ *
76
+ * This function validates the input against the XML Schema `xsd:decimal`
77
+ * lexical space after applying the XML Schema `whiteSpace="collapse"`
78
+ * normalization step. It returns the normalized string without any further
79
+ * canonicalization.
80
+ *
81
+ * @param value A candidate `xsd:decimal` lexical form.
82
+ * @returns The normalized string branded as {@link Decimal}.
83
+ * @throws {TypeError} Thrown when the value is not a valid `xsd:decimal`
84
+ * lexical form.
85
+ * @example
86
+ * ```typescript
87
+ * const price = parseDecimal("12.50");
88
+ * ```
89
+ * @example
90
+ * ```typescript
91
+ * const price = parseDecimal(" 12.50 ");
92
+ * console.assert(price === "12.50");
93
+ * ```
94
+ * @example
95
+ * ```typescript
96
+ * try {
97
+ * parseDecimal("1e3");
98
+ * } catch (error) {
99
+ * console.assert(error instanceof TypeError);
100
+ * }
101
+ * ```
102
+ * @since 2.1.0
103
+ */
104
+ export function parseDecimal(value: string): Decimal {
105
+ const normalized = collapseXmlSchemaWhitespace(value);
106
+ if (!isDecimal(normalized)) {
107
+ throw new TypeError(
108
+ `${JSON.stringify(value)} is not a valid xsd:decimal lexical form.`,
109
+ );
110
+ }
111
+ return normalized as Decimal;
112
+ }
@@ -0,0 +1,68 @@
1
+ import { deepStrictEqual, throws } from "node:assert";
2
+ import { test } from "node:test";
3
+ import {
4
+ addMulticodecPrefix,
5
+ getMulticodecPrefix,
6
+ removeMulticodecPrefix,
7
+ } from "./multicodec.ts";
8
+
9
+ test("getMulticodecPrefix() decodes supported multicodec prefixes", () => {
10
+ deepStrictEqual(
11
+ getMulticodecPrefix(Uint8Array.from([0xed, 0x01, 0xaa])),
12
+ { code: 0xed, prefixLength: 2 },
13
+ );
14
+ deepStrictEqual(
15
+ getMulticodecPrefix(Uint8Array.from([0x85, 0x24, 0xaa])),
16
+ { code: 0x1205, prefixLength: 2 },
17
+ );
18
+ });
19
+
20
+ test("removeMulticodecPrefix() strips the varint prefix", () => {
21
+ deepStrictEqual(
22
+ removeMulticodecPrefix(Uint8Array.from([0xed, 0x01, 0x11, 0x22])),
23
+ Uint8Array.from([0x11, 0x22]),
24
+ );
25
+ });
26
+
27
+ test("addMulticodecPrefix() prepends the varint-encoded code", () => {
28
+ deepStrictEqual(
29
+ addMulticodecPrefix(0xed, Uint8Array.from([0x11, 0x22])),
30
+ Uint8Array.from([0xed, 0x01, 0x11, 0x22]),
31
+ );
32
+ deepStrictEqual(
33
+ addMulticodecPrefix(0x1205, Uint8Array.from([0x11, 0x22])),
34
+ Uint8Array.from([0x85, 0x24, 0x11, 0x22]),
35
+ );
36
+ });
37
+
38
+ test("multicodec helpers round-trip prefixed payloads", () => {
39
+ const payload = Uint8Array.from([0xde, 0xad, 0xbe, 0xef]);
40
+ const prefixed = addMulticodecPrefix(0x1205, payload);
41
+ deepStrictEqual(getMulticodecPrefix(prefixed), {
42
+ code: 0x1205,
43
+ prefixLength: 2,
44
+ });
45
+ deepStrictEqual(removeMulticodecPrefix(prefixed), payload);
46
+ });
47
+
48
+ test("multicodec helpers reject malformed prefixes", () => {
49
+ throws(
50
+ () => getMulticodecPrefix(new Uint8Array([])),
51
+ new TypeError("Invalid multicodec prefix."),
52
+ );
53
+ throws(
54
+ () => getMulticodecPrefix(Uint8Array.from([0x80])),
55
+ new TypeError("Invalid multicodec prefix."),
56
+ );
57
+ throws(
58
+ () =>
59
+ getMulticodecPrefix(
60
+ Uint8Array.from([0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80]),
61
+ ),
62
+ new TypeError("Invalid multicodec prefix."),
63
+ );
64
+ throws(
65
+ () => addMulticodecPrefix(-1, Uint8Array.from([0x00])),
66
+ new TypeError("Invalid multicodec code."),
67
+ );
68
+ });
@@ -0,0 +1,53 @@
1
+ const INVALID_MULTICODEC_PREFIX = "Invalid multicodec prefix.";
2
+
3
+ export interface MulticodecPrefix {
4
+ code: number;
5
+ prefixLength: number;
6
+ }
7
+
8
+ export function getMulticodecPrefix(
9
+ data: Uint8Array,
10
+ ): MulticodecPrefix {
11
+ if (data.length < 1) throw new TypeError(INVALID_MULTICODEC_PREFIX);
12
+ let code = 0;
13
+ let shift = 0;
14
+ for (let i = 0; i < data.length; i++) {
15
+ const byte = data[i];
16
+ code += (byte & 0x7f) * 2 ** shift;
17
+ if (code > Number.MAX_SAFE_INTEGER) {
18
+ throw new TypeError(INVALID_MULTICODEC_PREFIX);
19
+ }
20
+ if ((byte & 0x80) === 0) {
21
+ return { code, prefixLength: i + 1 };
22
+ }
23
+ shift += 7;
24
+ if (shift >= 53) throw new TypeError(INVALID_MULTICODEC_PREFIX);
25
+ }
26
+ throw new TypeError(INVALID_MULTICODEC_PREFIX);
27
+ }
28
+
29
+ export function removeMulticodecPrefix(data: Uint8Array): Uint8Array {
30
+ const { prefixLength } = getMulticodecPrefix(data);
31
+ return data.slice(prefixLength);
32
+ }
33
+
34
+ export function addMulticodecPrefix(
35
+ code: number,
36
+ payload: Uint8Array,
37
+ ): Uint8Array {
38
+ if (!Number.isSafeInteger(code) || code < 0) {
39
+ throw new TypeError("Invalid multicodec code.");
40
+ }
41
+ const prefix: number[] = [];
42
+ let value = code;
43
+ do {
44
+ let byte = value & 0x7f;
45
+ value = Math.floor(value / 0x80);
46
+ if (value > 0) byte |= 0x80;
47
+ prefix.push(byte);
48
+ } while (value > 0);
49
+ const prefixed = new Uint8Array(prefix.length + payload.length);
50
+ prefixed.set(prefix);
51
+ prefixed.set(payload, prefix.length);
52
+ return prefixed;
53
+ }
package/src/jsonld.ts ADDED
@@ -0,0 +1,4 @@
1
+ // @ts-ignore TS7016
2
+ import jsonld from "jsonld/dist/jsonld.esm.js";
3
+
4
+ export default jsonld as typeof import("jsonld");
package/src/key.test.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { deepStrictEqual } from "node:assert";
1
+ import { deepStrictEqual, rejects } from "node:assert";
2
2
  import { test } from "node:test";
3
3
  import { exportJwk, importJwk } from "./jwk.ts";
4
4
  import {
@@ -9,6 +9,7 @@ import {
9
9
  importPkcs1,
10
10
  importSpki,
11
11
  } from "./key.ts";
12
+ import { encodeMultibase } from "./multibase/mod.ts";
12
13
 
13
14
  // cSpell: disable
14
15
  const rsaSpki = "-----BEGIN PUBLIC KEY-----\n" +
@@ -122,6 +123,26 @@ test("importMultibase()", async () => {
122
123
  deepStrictEqual(await exportJwk(ed25519Key), ed25519Jwk);
123
124
  });
124
125
 
126
+ test("importMultibase() rejects malformed multicodec prefixes", async () => {
127
+ const decoder = new TextDecoder();
128
+ await rejects(
129
+ () =>
130
+ importMultibaseKey(
131
+ decoder.decode(encodeMultibase("base58btc", new Uint8Array([]))),
132
+ ),
133
+ new TypeError("Invalid multicodec prefix."),
134
+ );
135
+ await rejects(
136
+ () =>
137
+ importMultibaseKey(
138
+ decoder.decode(
139
+ encodeMultibase("base58btc", Uint8Array.from([0x80])),
140
+ ),
141
+ ),
142
+ new TypeError("Invalid multicodec prefix."),
143
+ );
144
+ });
145
+
125
146
  test("exportMultibaseKey()", async () => {
126
147
  const rsaKey = await importJwk(rsaJwk, "public");
127
148
  const rsaMb = await exportMultibaseKey(rsaKey);
package/src/key.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import { Integer, Sequence } from "asn1js";
2
2
  import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
3
3
  import { decodeBase64Url } from "byte-encodings/base64url";
4
- import { decodeHex } from "byte-encodings/hex";
5
- import { addPrefix, getCodeFromData, rmPrefix } from "multicodec";
6
4
  import { createPublicKey } from "node:crypto";
7
5
  import { PublicKeyInfo } from "pkijs";
6
+ import {
7
+ addMulticodecPrefix,
8
+ getMulticodecPrefix,
9
+ removeMulticodecPrefix,
10
+ } from "./internal/multicodec.ts";
8
11
  import { validateCryptoKey } from "./jwk.ts";
9
12
  import { decodeMultibase, encodeMultibase } from "./multibase/mod.ts";
10
13
 
@@ -101,8 +104,8 @@ export function importPem(pem: string): Promise<CryptoKey> {
101
104
  */
102
105
  export async function importMultibaseKey(key: string): Promise<CryptoKey> {
103
106
  const decoded = decodeMultibase(key);
104
- const code: number = getCodeFromData(decoded);
105
- const content = rmPrefix(decoded);
107
+ const { code } = getMulticodecPrefix(decoded);
108
+ const content = removeMulticodecPrefix(decoded);
106
109
  if (code === 0x1205) { // rsa-pub
107
110
  const keyObject = createPublicKey({
108
111
  // deno-lint-ignore no-explicit-any
@@ -177,11 +180,9 @@ export async function exportMultibaseKey(key: CryptoKey): Promise<string> {
177
180
  "Unsupported key type: " + JSON.stringify(key.algorithm),
178
181
  );
179
182
  }
180
- const codeHex = code.toString(16);
181
- const codeBytes = decodeHex(codeHex.length % 2 < 1 ? codeHex : "0" + codeHex);
182
- const prefixed = addPrefix(codeBytes, new Uint8Array(content));
183
+ const prefixed = addMulticodecPrefix(code, new Uint8Array(content));
183
184
  const encoded = encodeMultibase("base58btc", prefixed);
184
185
  return new TextDecoder().decode(encoded);
185
186
  }
186
187
 
187
- // cSpell: ignore multicodec pkijs
188
+ // cSpell: ignore pkijs
package/src/mod.ts CHANGED
@@ -24,6 +24,12 @@ export {
24
24
  importPkcs1,
25
25
  importSpki,
26
26
  } from "./key.ts";
27
+ export {
28
+ canParseDecimal,
29
+ type Decimal,
30
+ isDecimal,
31
+ parseDecimal,
32
+ } from "./decimal.ts";
27
33
  export { LanguageString } from "./langstr.ts";
28
34
  export {
29
35
  decodeMultibase,
package/tsdown.config.ts CHANGED
@@ -4,7 +4,7 @@ import { defineConfig } from "tsdown";
4
4
 
5
5
  export default [
6
6
  defineConfig({
7
- entry: ["src/mod.ts"],
7
+ entry: ["src/mod.ts", "src/jsonld.ts"],
8
8
  dts: { compilerOptions: { isolatedDeclarations: true, declaration: true } },
9
9
  format: ["esm", "cjs"],
10
10
  platform: "neutral",
File without changes