@atproto/lex-json 0.0.10 → 0.0.11

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @atproto/lex-json
2
2
 
3
+ ## 0.0.11
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4601](https://github.com/bluesky-social/atproto/pull/4601) [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Prevent `jsonToLex` from throwing in non-strict mode
8
+
9
+ - [#4601](https://github.com/bluesky-social/atproto/pull/4601) [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Fix `exports` field in package.json
10
+
11
+ - [#4601](https://github.com/bluesky-social/atproto/pull/4601) [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add JSDoc
12
+
13
+ - Updated dependencies [[`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015)]:
14
+ - @atproto/lex-data@0.0.11
15
+
3
16
  ## 0.0.10
4
17
 
5
18
  ### Patch Changes
package/dist/blob.d.ts CHANGED
@@ -1,3 +1,37 @@
1
1
  import { BlobRef, BlobRefCheckOptions, LexMap } from '@atproto/lex-data';
2
+ /**
3
+ * Parses a blob reference from a JSON object.
4
+ *
5
+ * In the AT Protocol, blobs are referenced using a specific object structure
6
+ * with `$type: 'blob'`, a `ref` property containing a CID link, and metadata
7
+ * like `mimeType` and `size`. This function validates and parses such objects
8
+ * into `BlobRef` instances.
9
+ *
10
+ * The function handles both cases where the `ref` property is:
11
+ * - A `{$link: string}` object (when parsing from JSON)
12
+ * - Already a `Cid` instance (when the parent object has been partially converted)
13
+ *
14
+ * @param input - A Lex map potentially representing a blob reference
15
+ * @param options - Optional blob reference validation options
16
+ * @returns The parsed `BlobRef` if the input is a valid blob reference,
17
+ * or `undefined` if the input is not a valid blob representation
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * // Parse a blob reference from JSON
22
+ * const blobRef = parseBlobRef({
23
+ * $type: 'blob',
24
+ * ref: { $link: 'bafyreib2rxk3rybloqtqwbo' },
25
+ * mimeType: 'image/png',
26
+ * size: 12345
27
+ * })
28
+ *
29
+ * // blobRef.ref is a Cid instance
30
+ *
31
+ * // Returns undefined for non-blob objects
32
+ * const result = parseBlobRef({ foo: 'bar' })
33
+ * // result is undefined
34
+ * ```
35
+ */
2
36
  export declare function parseBlobRef(input: LexMap, options?: BlobRefCheckOptions): BlobRef | undefined;
3
37
  //# sourceMappingURL=blob.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"blob.d.ts","sourceRoot":"","sources":["../src/blob.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,MAAM,EAEP,MAAM,mBAAmB,CAAA;AAG1B,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,mBAAmB,GAC5B,OAAO,GAAG,SAAS,CAwBrB"}
1
+ {"version":3,"file":"blob.d.ts","sourceRoot":"","sources":["../src/blob.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,MAAM,EAEP,MAAM,mBAAmB,CAAA;AAG1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,mBAAmB,GAC5B,OAAO,GAAG,SAAS,CAwBrB"}
package/dist/blob.js CHANGED
@@ -3,6 +3,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseBlobRef = parseBlobRef;
4
4
  const lex_data_1 = require("@atproto/lex-data");
5
5
  const link_js_1 = require("./link.js");
6
+ /**
7
+ * Parses a blob reference from a JSON object.
8
+ *
9
+ * In the AT Protocol, blobs are referenced using a specific object structure
10
+ * with `$type: 'blob'`, a `ref` property containing a CID link, and metadata
11
+ * like `mimeType` and `size`. This function validates and parses such objects
12
+ * into `BlobRef` instances.
13
+ *
14
+ * The function handles both cases where the `ref` property is:
15
+ * - A `{$link: string}` object (when parsing from JSON)
16
+ * - Already a `Cid` instance (when the parent object has been partially converted)
17
+ *
18
+ * @param input - A Lex map potentially representing a blob reference
19
+ * @param options - Optional blob reference validation options
20
+ * @returns The parsed `BlobRef` if the input is a valid blob reference,
21
+ * or `undefined` if the input is not a valid blob representation
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * // Parse a blob reference from JSON
26
+ * const blobRef = parseBlobRef({
27
+ * $type: 'blob',
28
+ * ref: { $link: 'bafyreib2rxk3rybloqtqwbo' },
29
+ * mimeType: 'image/png',
30
+ * size: 12345
31
+ * })
32
+ *
33
+ * // blobRef.ref is a Cid instance
34
+ *
35
+ * // Returns undefined for non-blob objects
36
+ * const result = parseBlobRef({ foo: 'bar' })
37
+ * // result is undefined
38
+ * ```
39
+ */
6
40
  function parseBlobRef(input, options) {
7
41
  if (input.$type !== 'blob')
8
42
  return undefined;
package/dist/blob.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"blob.js","sourceRoot":"","sources":["../src/blob.ts"],"names":[],"mappings":";;AAQA,oCA2BC;AAnCD,gDAK0B;AAC1B,uCAAwC;AAExC,SAAgB,YAAY,CAC1B,KAAa,EACb,OAA6B;IAE7B,IAAI,KAAK,CAAC,KAAK,KAAK,MAAM;QAAE,OAAO,SAAS,CAAA;IAE5C,MAAM,GAAG,GAAG,KAAK,EAAE,GAAG,CAAA;IACtB,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAA;IAErD,8EAA8E;IAC9E,oEAAoE;IACpE,qEAAqE;IACrE,iDAAiD;IAEjD,IAAI,OAAO,IAAI,GAAG,EAAE,CAAC;QACnB,MAAM,GAAG,GAAG,IAAA,sBAAY,EAAC,GAAG,CAAC,CAAA;QAC7B,IAAI,CAAC,GAAG;YAAE,OAAO,SAAS,CAAA;QAE1B,MAAM,IAAI,GAAG,EAAE,GAAG,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,CAAA;QACnC,IAAI,IAAA,oBAAS,EAAC,IAAI,EAAE,OAAO,CAAC;YAAE,OAAO,IAAI,CAAA;IAC3C,CAAC;IAED,IAAI,IAAA,oBAAS,EAAC,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC","sourcesContent":["import {\n BlobRef,\n BlobRefCheckOptions,\n LexMap,\n isBlobRef,\n} from '@atproto/lex-data'\nimport { parseLexLink } from './link.js'\n\nexport function parseBlobRef(\n input: LexMap,\n options?: BlobRefCheckOptions,\n): BlobRef | undefined {\n if (input.$type !== 'blob') return undefined\n\n const ref = input?.ref\n if (!ref || typeof ref !== 'object') return undefined\n\n // @NOTE Because json to lex conversion can be performed both in a depth-first\n // manner (e.g. via lexParse) or in a breadth-first manner (e.g. via\n // jsonToLex), the `ref` property may either be a LexMap with a $link\n // property, or it may already be a CID instance.\n\n if ('$link' in ref) {\n const cid = parseLexLink(ref)\n if (!cid) return undefined\n\n const blob = { ...input, ref: cid }\n if (isBlobRef(blob, options)) return blob\n }\n\n if (isBlobRef(input)) {\n return input\n }\n\n return undefined\n}\n"]}
1
+ {"version":3,"file":"blob.js","sourceRoot":"","sources":["../src/blob.ts"],"names":[],"mappings":";;AA0CA,oCA2BC;AArED,gDAK0B;AAC1B,uCAAwC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,SAAgB,YAAY,CAC1B,KAAa,EACb,OAA6B;IAE7B,IAAI,KAAK,CAAC,KAAK,KAAK,MAAM;QAAE,OAAO,SAAS,CAAA;IAE5C,MAAM,GAAG,GAAG,KAAK,EAAE,GAAG,CAAA;IACtB,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAA;IAErD,8EAA8E;IAC9E,oEAAoE;IACpE,qEAAqE;IACrE,iDAAiD;IAEjD,IAAI,OAAO,IAAI,GAAG,EAAE,CAAC;QACnB,MAAM,GAAG,GAAG,IAAA,sBAAY,EAAC,GAAG,CAAC,CAAA;QAC7B,IAAI,CAAC,GAAG;YAAE,OAAO,SAAS,CAAA;QAE1B,MAAM,IAAI,GAAG,EAAE,GAAG,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,CAAA;QACnC,IAAI,IAAA,oBAAS,EAAC,IAAI,EAAE,OAAO,CAAC;YAAE,OAAO,IAAI,CAAA;IAC3C,CAAC;IAED,IAAI,IAAA,oBAAS,EAAC,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC","sourcesContent":["import {\n BlobRef,\n BlobRefCheckOptions,\n LexMap,\n isBlobRef,\n} from '@atproto/lex-data'\nimport { parseLexLink } from './link.js'\n\n/**\n * Parses a blob reference from a JSON object.\n *\n * In the AT Protocol, blobs are referenced using a specific object structure\n * with `$type: 'blob'`, a `ref` property containing a CID link, and metadata\n * like `mimeType` and `size`. This function validates and parses such objects\n * into `BlobRef` instances.\n *\n * The function handles both cases where the `ref` property is:\n * - A `{$link: string}` object (when parsing from JSON)\n * - Already a `Cid` instance (when the parent object has been partially converted)\n *\n * @param input - A Lex map potentially representing a blob reference\n * @param options - Optional blob reference validation options\n * @returns The parsed `BlobRef` if the input is a valid blob reference,\n * or `undefined` if the input is not a valid blob representation\n *\n * @example\n * ```typescript\n * // Parse a blob reference from JSON\n * const blobRef = parseBlobRef({\n * $type: 'blob',\n * ref: { $link: 'bafyreib2rxk3rybloqtqwbo' },\n * mimeType: 'image/png',\n * size: 12345\n * })\n *\n * // blobRef.ref is a Cid instance\n *\n * // Returns undefined for non-blob objects\n * const result = parseBlobRef({ foo: 'bar' })\n * // result is undefined\n * ```\n */\nexport function parseBlobRef(\n input: LexMap,\n options?: BlobRefCheckOptions,\n): BlobRef | undefined {\n if (input.$type !== 'blob') return undefined\n\n const ref = input?.ref\n if (!ref || typeof ref !== 'object') return undefined\n\n // @NOTE Because json to lex conversion can be performed both in a depth-first\n // manner (e.g. via lexParse) or in a breadth-first manner (e.g. via\n // jsonToLex), the `ref` property may either be a LexMap with a $link\n // property, or it may already be a CID instance.\n\n if ('$link' in ref) {\n const cid = parseLexLink(ref)\n if (!cid) return undefined\n\n const blob = { ...input, ref: cid }\n if (isBlobRef(blob, options)) return blob\n }\n\n if (isBlobRef(input)) {\n return input\n }\n\n return undefined\n}\n"]}
package/dist/bytes.d.ts CHANGED
@@ -1,6 +1,47 @@
1
1
  import { JsonValue } from './json.js';
2
- export declare function parseLexBytes(input?: {
3
- $bytes?: unknown;
4
- }): Uint8Array | undefined;
2
+ /**
3
+ * Parses a `{$bytes: string}` JSON object into a `Uint8Array`.
4
+ *
5
+ * In the AT Protocol data model, binary data is represented in JSON as an object
6
+ * with a single `$bytes` property containing a base64-encoded string. This function
7
+ * decodes that representation back into raw bytes.
8
+ *
9
+ * @param input - An object potentially containing a `$bytes` property
10
+ * @returns The decoded `Uint8Array` if the input is a valid `$bytes` object,
11
+ * or `undefined` if the input is not a valid `$bytes` representation
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // Parse a $bytes object to Uint8Array
16
+ * const bytes = parseLexBytes({ $bytes: 'SGVsbG8sIHdvcmxkIQ==' })
17
+ * // bytes is Uint8Array containing "Hello, world!"
18
+ *
19
+ * // Returns undefined for non-$bytes objects
20
+ * const result = parseLexBytes({ foo: 'bar' })
21
+ * // result is undefined
22
+ *
23
+ * // Returns undefined for objects with extra properties
24
+ * const invalid = parseLexBytes({ $bytes: 'SGVsbG8=', extra: true })
25
+ * // invalid is undefined
26
+ * ```
27
+ */
28
+ export declare function parseLexBytes(input?: Record<string, unknown>): Uint8Array | undefined;
29
+ /**
30
+ * Encodes a `Uint8Array` into a `{$bytes: string}` JSON representation.
31
+ *
32
+ * In the AT Protocol data model, binary data is represented in JSON as an object
33
+ * with a single `$bytes` property containing a base64-encoded string. This function
34
+ * performs that encoding.
35
+ *
36
+ * @param bytes - The binary data to encode
37
+ * @returns An object with a `$bytes` property containing the base64-encoded data
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const bytes = new TextEncoder().encode('Hello, world!')
42
+ * const encoded = encodeLexBytes(bytes)
43
+ * // encoded is { $bytes: 'SGVsbG8sIHdvcmxkIQ==' }
44
+ * ```
45
+ */
5
46
  export declare function encodeLexBytes(bytes: Uint8Array): JsonValue;
6
47
  //# sourceMappingURL=bytes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,wBAAgB,aAAa,CAAC,KAAK,CAAC,EAAE;IACpC,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,GAAG,UAAU,GAAG,SAAS,CAgBzB;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,CAE3D"}
1
+ {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,aAAa,CAC3B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,UAAU,GAAG,SAAS,CAoBxB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,CAE3D"}
package/dist/bytes.js CHANGED
@@ -3,6 +3,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseLexBytes = parseLexBytes;
4
4
  exports.encodeLexBytes = encodeLexBytes;
5
5
  const lex_data_1 = require("@atproto/lex-data");
6
+ /**
7
+ * Parses a `{$bytes: string}` JSON object into a `Uint8Array`.
8
+ *
9
+ * In the AT Protocol data model, binary data is represented in JSON as an object
10
+ * with a single `$bytes` property containing a base64-encoded string. This function
11
+ * decodes that representation back into raw bytes.
12
+ *
13
+ * @param input - An object potentially containing a `$bytes` property
14
+ * @returns The decoded `Uint8Array` if the input is a valid `$bytes` object,
15
+ * or `undefined` if the input is not a valid `$bytes` representation
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Parse a $bytes object to Uint8Array
20
+ * const bytes = parseLexBytes({ $bytes: 'SGVsbG8sIHdvcmxkIQ==' })
21
+ * // bytes is Uint8Array containing "Hello, world!"
22
+ *
23
+ * // Returns undefined for non-$bytes objects
24
+ * const result = parseLexBytes({ foo: 'bar' })
25
+ * // result is undefined
26
+ *
27
+ * // Returns undefined for objects with extra properties
28
+ * const invalid = parseLexBytes({ $bytes: 'SGVsbG8=', extra: true })
29
+ * // invalid is undefined
30
+ * ```
31
+ */
6
32
  function parseLexBytes(input) {
7
33
  if (!input || !('$bytes' in input)) {
8
34
  return undefined;
@@ -13,10 +39,32 @@ function parseLexBytes(input) {
13
39
  }
14
40
  }
15
41
  if (typeof input.$bytes !== 'string') {
16
- throw new TypeError('$bytes must be a base64-encoded string');
42
+ return undefined;
43
+ }
44
+ try {
45
+ return (0, lex_data_1.fromBase64)(input.$bytes);
46
+ }
47
+ catch {
48
+ return undefined;
17
49
  }
18
- return (0, lex_data_1.fromBase64)(input.$bytes);
19
50
  }
51
+ /**
52
+ * Encodes a `Uint8Array` into a `{$bytes: string}` JSON representation.
53
+ *
54
+ * In the AT Protocol data model, binary data is represented in JSON as an object
55
+ * with a single `$bytes` property containing a base64-encoded string. This function
56
+ * performs that encoding.
57
+ *
58
+ * @param bytes - The binary data to encode
59
+ * @returns An object with a `$bytes` property containing the base64-encoded data
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const bytes = new TextEncoder().encode('Hello, world!')
64
+ * const encoded = encodeLexBytes(bytes)
65
+ * // encoded is { $bytes: 'SGVsbG8sIHdvcmxkIQ==' }
66
+ * ```
67
+ */
20
68
  function encodeLexBytes(bytes) {
21
69
  return { $bytes: (0, lex_data_1.toBase64)(bytes) };
22
70
  }
package/dist/bytes.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":";;AAGA,sCAkBC;AAED,wCAEC;AAzBD,gDAAwD;AAGxD,SAAgB,aAAa,CAAC,KAE7B;IACC,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,QAAQ,IAAI,KAAK,CAAC,EAAE,CAAC;QACnC,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;QACxB,IAAI,GAAG,KAAK,QAAQ,EAAE,CAAC;YACrB,OAAO,SAAS,CAAA;QAClB,CAAC;IACH,CAAC;IAED,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;QACrC,MAAM,IAAI,SAAS,CAAC,wCAAwC,CAAC,CAAA;IAC/D,CAAC;IAED,OAAO,IAAA,qBAAU,EAAC,KAAK,CAAC,MAAM,CAAC,CAAA;AACjC,CAAC;AAED,SAAgB,cAAc,CAAC,KAAiB;IAC9C,OAAO,EAAE,MAAM,EAAE,IAAA,mBAAQ,EAAC,KAAK,CAAC,EAAE,CAAA;AACpC,CAAC","sourcesContent":["import { fromBase64, toBase64 } from '@atproto/lex-data'\nimport { JsonValue } from './json.js'\n\nexport function parseLexBytes(input?: {\n $bytes?: unknown\n}): Uint8Array | undefined {\n if (!input || !('$bytes' in input)) {\n return undefined\n }\n\n for (const key in input) {\n if (key !== '$bytes') {\n return undefined\n }\n }\n\n if (typeof input.$bytes !== 'string') {\n throw new TypeError('$bytes must be a base64-encoded string')\n }\n\n return fromBase64(input.$bytes)\n}\n\nexport function encodeLexBytes(bytes: Uint8Array): JsonValue {\n return { $bytes: toBase64(bytes) }\n}\n"]}
1
+ {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":";;AA6BA,sCAsBC;AAmBD,wCAEC;AAxED,gDAAwD;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,SAAgB,aAAa,CAC3B,KAA+B;IAE/B,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,QAAQ,IAAI,KAAK,CAAC,EAAE,CAAC;QACnC,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;QACxB,IAAI,GAAG,KAAK,QAAQ,EAAE,CAAC;YACrB,OAAO,SAAS,CAAA;QAClB,CAAC;IACH,CAAC;IAED,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;QACrC,OAAO,SAAS,CAAA;IAClB,CAAC;IAED,IAAI,CAAC;QACH,OAAO,IAAA,qBAAU,EAAC,KAAK,CAAC,MAAM,CAAC,CAAA;IACjC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAA;IAClB,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,cAAc,CAAC,KAAiB;IAC9C,OAAO,EAAE,MAAM,EAAE,IAAA,mBAAQ,EAAC,KAAK,CAAC,EAAE,CAAA;AACpC,CAAC","sourcesContent":["import { fromBase64, toBase64 } from '@atproto/lex-data'\nimport { JsonValue } from './json.js'\n\n/**\n * Parses a `{$bytes: string}` JSON object into a `Uint8Array`.\n *\n * In the AT Protocol data model, binary data is represented in JSON as an object\n * with a single `$bytes` property containing a base64-encoded string. This function\n * decodes that representation back into raw bytes.\n *\n * @param input - An object potentially containing a `$bytes` property\n * @returns The decoded `Uint8Array` if the input is a valid `$bytes` object,\n * or `undefined` if the input is not a valid `$bytes` representation\n *\n * @example\n * ```typescript\n * // Parse a $bytes object to Uint8Array\n * const bytes = parseLexBytes({ $bytes: 'SGVsbG8sIHdvcmxkIQ==' })\n * // bytes is Uint8Array containing \"Hello, world!\"\n *\n * // Returns undefined for non-$bytes objects\n * const result = parseLexBytes({ foo: 'bar' })\n * // result is undefined\n *\n * // Returns undefined for objects with extra properties\n * const invalid = parseLexBytes({ $bytes: 'SGVsbG8=', extra: true })\n * // invalid is undefined\n * ```\n */\nexport function parseLexBytes(\n input?: Record<string, unknown>,\n): Uint8Array | undefined {\n if (!input || !('$bytes' in input)) {\n return undefined\n }\n\n for (const key in input) {\n if (key !== '$bytes') {\n return undefined\n }\n }\n\n if (typeof input.$bytes !== 'string') {\n return undefined\n }\n\n try {\n return fromBase64(input.$bytes)\n } catch {\n return undefined\n }\n}\n\n/**\n * Encodes a `Uint8Array` into a `{$bytes: string}` JSON representation.\n *\n * In the AT Protocol data model, binary data is represented in JSON as an object\n * with a single `$bytes` property containing a base64-encoded string. This function\n * performs that encoding.\n *\n * @param bytes - The binary data to encode\n * @returns An object with a `$bytes` property containing the base64-encoded data\n *\n * @example\n * ```typescript\n * const bytes = new TextEncoder().encode('Hello, world!')\n * const encoded = encodeLexBytes(bytes)\n * // encoded is { $bytes: 'SGVsbG8sIHdvcmxkIQ==' }\n * ```\n */\nexport function encodeLexBytes(bytes: Uint8Array): JsonValue {\n return { $bytes: toBase64(bytes) }\n}\n"]}
package/dist/json.d.ts CHANGED
@@ -1,7 +1,50 @@
1
+ /**
2
+ * Primitive JSON values: string, number, boolean, or null.
3
+ *
4
+ * These are the scalar (non-composite) types that can appear in JSON data.
5
+ * In the context of the AT Protocol:
6
+ * - `string` - Text values, including special encoded types like `$link` and `$bytes`
7
+ * - `number` - Numeric values (note: Lex only supports safe integers)
8
+ * - `boolean` - True/false values
9
+ * - `null` - Explicit null values
10
+ *
11
+ * @see {@link JsonValue} for the complete JSON type including arrays and objects
12
+ */
1
13
  export type JsonScalar = number | string | boolean | null;
14
+ /**
15
+ * Any valid JSON value.
16
+ *
17
+ * This is a recursive type that represents the full JSON data model:
18
+ * - Scalars: string, number, boolean, null
19
+ * - Arrays: ordered lists of JSON values
20
+ * - Objects: string-keyed maps of JSON values
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const scalar: JsonValue = "hello"
25
+ * const array: JsonValue = [1, 2, 3]
26
+ * const object: JsonValue = { name: "Alice", age: 30 }
27
+ * const nested: JsonValue = { users: [{ name: "Bob" }] }
28
+ * ```
29
+ */
2
30
  export type JsonValue = JsonScalar | JsonValue[] | {
3
31
  [_ in string]?: JsonValue;
4
32
  };
33
+ /**
34
+ * A JSON object with string keys and JSON values.
35
+ *
36
+ * This type represents a plain JavaScript object that is valid JSON,
37
+ * where all keys are strings and all values are valid JSON values.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const obj: JsonObject = {
42
+ * name: "Alice",
43
+ * tags: ["admin", "user"],
44
+ * metadata: { created: "2024-01-01" }
45
+ * }
46
+ * ```
47
+ */
5
48
  export type JsonObject = {
6
49
  [_ in string]?: JsonValue;
7
50
  };
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;AACzD,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,SAAS,EAAE,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,SAAS;CAAE,CAAA;AAChF,MAAM,MAAM,UAAU,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,SAAS;CAAE,CAAA"}
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,SAAS,EAAE,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,SAAS;CAAE,CAAA;AAEhF;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,UAAU,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,SAAS;CAAE,CAAA"}
package/dist/json.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"","sourcesContent":["export type JsonScalar = number | string | boolean | null\nexport type JsonValue = JsonScalar | JsonValue[] | { [_ in string]?: JsonValue }\nexport type JsonObject = { [_ in string]?: JsonValue }\n"]}
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Primitive JSON values: string, number, boolean, or null.\n *\n * These are the scalar (non-composite) types that can appear in JSON data.\n * In the context of the AT Protocol:\n * - `string` - Text values, including special encoded types like `$link` and `$bytes`\n * - `number` - Numeric values (note: Lex only supports safe integers)\n * - `boolean` - True/false values\n * - `null` - Explicit null values\n *\n * @see {@link JsonValue} for the complete JSON type including arrays and objects\n */\nexport type JsonScalar = number | string | boolean | null\n\n/**\n * Any valid JSON value.\n *\n * This is a recursive type that represents the full JSON data model:\n * - Scalars: string, number, boolean, null\n * - Arrays: ordered lists of JSON values\n * - Objects: string-keyed maps of JSON values\n *\n * @example\n * ```typescript\n * const scalar: JsonValue = \"hello\"\n * const array: JsonValue = [1, 2, 3]\n * const object: JsonValue = { name: \"Alice\", age: 30 }\n * const nested: JsonValue = { users: [{ name: \"Bob\" }] }\n * ```\n */\nexport type JsonValue = JsonScalar | JsonValue[] | { [_ in string]?: JsonValue }\n\n/**\n * A JSON object with string keys and JSON values.\n *\n * This type represents a plain JavaScript object that is valid JSON,\n * where all keys are strings and all values are valid JSON values.\n *\n * @example\n * ```typescript\n * const obj: JsonObject = {\n * name: \"Alice\",\n * tags: [\"admin\", \"user\"],\n * metadata: { created: \"2024-01-01\" }\n * }\n * ```\n */\nexport type JsonObject = { [_ in string]?: JsonValue }\n"]}
@@ -1,14 +1,139 @@
1
1
  import { LexValue } from '@atproto/lex-data';
2
2
  import { JsonValue } from './json.js';
3
+ /**
4
+ * Serialize a Lex value to a JSON string.
5
+ *
6
+ * This function serializes AT Protocol data model values to JSON, automatically
7
+ * encoding special types:
8
+ * - `Cid` instances are encoded as `{$link: string}`
9
+ * - `Uint8Array` instances are encoded as `{$bytes: string}` (base64)
10
+ *
11
+ * @param input - The Lex value to stringify
12
+ * @returns A JSON string representation of the value
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { lexStringify } from '@atproto/lex'
17
+ *
18
+ * // Stringify with CID and bytes encoding
19
+ * const json = lexStringify({
20
+ * ref: someCid,
21
+ * data: new Uint8Array([72, 101, 108, 108, 111])
22
+ * })
23
+ * // json is '{"ref":{"$link":"bafyrei..."},"data":{"$bytes":"SGVsbG8="}}'
24
+ * ```
25
+ */
3
26
  export declare function lexStringify(input: LexValue): string;
27
+ /**
28
+ * Options for parsing JSON to Lex values.
29
+ */
4
30
  export type LexParseOptions = {
5
31
  /**
6
- * Forbids the presence of invalid Lex values (e.g. non-integer numbers,
7
- * malformed $link, $bytes, blob objects, etc.)
32
+ * When enabled, forbids the presence of invalid Lex values such as:
33
+ * - Non-integer numbers (only safe integers are valid in the Lex data model)
34
+ * - Malformed `$link` objects
35
+ * - Malformed `$bytes` objects
36
+ * - Objects with invalid or empty `$type` properties
37
+ * - Invalid {@link BlobRef} (`$type: 'blob'`) objects
38
+ *
39
+ * When disabled (default), invalid special objects are left as plain objects.
40
+ *
41
+ * @default false
8
42
  */
9
43
  strict?: boolean;
10
44
  };
45
+ /**
46
+ * Parses a JSON string into Lex values.
47
+ *
48
+ * This function parses JSON and automatically decodes AT Protocol special types:
49
+ * - `{$link: string}` objects are decoded to `Cid` instances
50
+ * - `{$bytes: string}` objects are decoded to `Uint8Array` instances
51
+ * - `{$type: 'blob'}` objects are validated
52
+ *
53
+ * @typeParam T - Type cast for the resulting Lex value. Use when you want to specify the expected structure of the parsed data.
54
+ * @param input - The JSON string to parse
55
+ * @param options - Parsing options (e.g., strict mode)
56
+ * @returns The parsed Lex value
57
+ * @throws {SyntaxError} If the input is not valid JSON
58
+ * @throws {TypeError} If strict mode is enabled and invalid Lex values are found
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * import { lexParse } from '@atproto/lex'
63
+ *
64
+ * // Parse JSON with $link and $bytes decoding
65
+ * const parsed = lexParse<{
66
+ * ref: Cid
67
+ * data: Uint8Array
68
+ * }>(`{
69
+ * "ref": { "$link": "bafyrei..." },
70
+ * "data": { "$bytes": "SGVsbG8sIHdvcmxkIQ==" }
71
+ * }`)
72
+ *
73
+ * // Parse a single CID
74
+ * const someCid = lexParse<Cid>('{"$link": "bafyrei..."}')
75
+ *
76
+ * // Parse binary data
77
+ * const someBytes = lexParse<Uint8Array>('{"$bytes": "SGVsbG8sIHdvcmxkIQ=="}')
78
+ * ```
79
+ */
11
80
  export declare function lexParse<T extends LexValue = LexValue>(input: string, options?: LexParseOptions): T;
81
+ /**
82
+ * Converts a parsed JSON representation of Lexicon value to a {@link LexValue}.
83
+ *
84
+ * This function transforms already-parsed JSON objects into Lex values by
85
+ * decoding AT Protocol special types:
86
+ * - `{$link: string}` objects are converted to `Cid` instances
87
+ * - `{$bytes: string}` objects are converted to `Uint8Array` instances
88
+ *
89
+ * Use this when you have a JavaScript object (e.g., from `JSON.parse()`) and
90
+ * need to convert it to the Lex data model. For parsing JSON strings directly,
91
+ * use {@link lexParse} instead.
92
+ *
93
+ * @param value - The JSON value to convert
94
+ * @param options - Parsing options (e.g., strict mode)
95
+ * @returns The converted Lex value
96
+ * @throws {TypeError} If strict mode is enabled and invalid Lex values are found
97
+ * @throws {TypeError} If the value contains unsupported types (e.g., undefined at top level)
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * import { jsonToLex } from '@atproto/lex'
102
+ *
103
+ * // Convert parsed JSON to Lex values
104
+ * const lex = jsonToLex({
105
+ * ref: { $link: 'bafyrei...' }, // Converted to Cid
106
+ * data: { $bytes: 'SGVsbG8sIHdvcmxkIQ==' } // Converted to Uint8Array
107
+ * })
108
+ * ```
109
+ */
12
110
  export declare function jsonToLex(value: JsonValue, options?: LexParseOptions): LexValue;
111
+ /**
112
+ * Converts a Lex value to a JSON-compatible value.
113
+ *
114
+ * This function transforms Lex data model values into plain JavaScript objects
115
+ * suitable for JSON serialization:
116
+ * - `Cid` instances are converted to `{$link: string}` objects
117
+ * - `Uint8Array` instances are converted to `{$bytes: string}` objects (base64)
118
+ *
119
+ * Use this when you need to convert Lex values to plain objects (e.g., for
120
+ * custom serialization or inspection). For direct JSON string output, use
121
+ * {@link lexStringify} instead.
122
+ *
123
+ * @param value - The Lex value to convert
124
+ * @returns The JSON-compatible value
125
+ * @throws {TypeError} If the value contains unsupported types
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * import { lexToJson } from '@atproto/lex'
130
+ *
131
+ * // Convert Lex values to JSON-compatible objects
132
+ * const obj = lexToJson({
133
+ * ref: someCid, // Converted to { $link: string }
134
+ * data: someBytes // Converted to { $bytes: string }
135
+ * })
136
+ * ```
137
+ */
13
138
  export declare function lexToJson(value: LexValue): JsonValue;
14
139
  //# sourceMappingURL=lex-json.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"lex-json.d.ts","sourceRoot":"","sources":["../src/lex-json.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,QAAQ,EAET,MAAM,mBAAmB,CAAA;AAG1B,OAAO,EAAc,SAAS,EAAE,MAAM,WAAW,CAAA;AAGjD,wBAAgB,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,CAKpD;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,wBAAgB,QAAQ,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EACpD,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAmC,GAC3C,CAAC,CAiBH;AAED,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,EAChB,OAAO,GAAE,eAAmC,GAC3C,QAAQ,CAsBV;AA+CD,wBAAgB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,CAqBpD"}
1
+ {"version":3,"file":"lex-json.d.ts","sourceRoot":"","sources":["../src/lex-json.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,QAAQ,EAET,MAAM,mBAAmB,CAAA;AAG1B,OAAO,EAAc,SAAS,EAAE,MAAM,WAAW,CAAA;AAGjD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,CAKpD;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EACpD,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAmC,GAC3C,CAAC,CAiBH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,EAChB,OAAO,GAAE,eAAmC,GAC3C,QAAQ,CAsBV;AA+CD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,CAqBpD"}
package/dist/lex-json.js CHANGED
@@ -8,12 +8,70 @@ const lex_data_1 = require("@atproto/lex-data");
8
8
  const blob_js_1 = require("./blob.js");
9
9
  const bytes_js_1 = require("./bytes.js");
10
10
  const link_js_1 = require("./link.js");
11
+ /**
12
+ * Serialize a Lex value to a JSON string.
13
+ *
14
+ * This function serializes AT Protocol data model values to JSON, automatically
15
+ * encoding special types:
16
+ * - `Cid` instances are encoded as `{$link: string}`
17
+ * - `Uint8Array` instances are encoded as `{$bytes: string}` (base64)
18
+ *
19
+ * @param input - The Lex value to stringify
20
+ * @returns A JSON string representation of the value
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * import { lexStringify } from '@atproto/lex'
25
+ *
26
+ * // Stringify with CID and bytes encoding
27
+ * const json = lexStringify({
28
+ * ref: someCid,
29
+ * data: new Uint8Array([72, 101, 108, 108, 111])
30
+ * })
31
+ * // json is '{"ref":{"$link":"bafyrei..."},"data":{"$bytes":"SGVsbG8="}}'
32
+ * ```
33
+ */
11
34
  function lexStringify(input) {
12
35
  // @NOTE Because of the way the "replacer" works in JSON.stringify, it's
13
36
  // simpler to convert Lex to JSON first rather than trying to do it
14
37
  // on-the-fly.
15
38
  return JSON.stringify(lexToJson(input));
16
39
  }
40
+ /**
41
+ * Parses a JSON string into Lex values.
42
+ *
43
+ * This function parses JSON and automatically decodes AT Protocol special types:
44
+ * - `{$link: string}` objects are decoded to `Cid` instances
45
+ * - `{$bytes: string}` objects are decoded to `Uint8Array` instances
46
+ * - `{$type: 'blob'}` objects are validated
47
+ *
48
+ * @typeParam T - Type cast for the resulting Lex value. Use when you want to specify the expected structure of the parsed data.
49
+ * @param input - The JSON string to parse
50
+ * @param options - Parsing options (e.g., strict mode)
51
+ * @returns The parsed Lex value
52
+ * @throws {SyntaxError} If the input is not valid JSON
53
+ * @throws {TypeError} If strict mode is enabled and invalid Lex values are found
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * import { lexParse } from '@atproto/lex'
58
+ *
59
+ * // Parse JSON with $link and $bytes decoding
60
+ * const parsed = lexParse<{
61
+ * ref: Cid
62
+ * data: Uint8Array
63
+ * }>(`{
64
+ * "ref": { "$link": "bafyrei..." },
65
+ * "data": { "$bytes": "SGVsbG8sIHdvcmxkIQ==" }
66
+ * }`)
67
+ *
68
+ * // Parse a single CID
69
+ * const someCid = lexParse<Cid>('{"$link": "bafyrei..."}')
70
+ *
71
+ * // Parse binary data
72
+ * const someBytes = lexParse<Uint8Array>('{"$bytes": "SGVsbG8sIHdvcmxkIQ=="}')
73
+ * ```
74
+ */
17
75
  function lexParse(input, options = { strict: false }) {
18
76
  return JSON.parse(input, function (key, value) {
19
77
  switch (typeof value) {
@@ -35,6 +93,35 @@ function lexParse(input, options = { strict: false }) {
35
93
  }
36
94
  });
37
95
  }
96
+ /**
97
+ * Converts a parsed JSON representation of Lexicon value to a {@link LexValue}.
98
+ *
99
+ * This function transforms already-parsed JSON objects into Lex values by
100
+ * decoding AT Protocol special types:
101
+ * - `{$link: string}` objects are converted to `Cid` instances
102
+ * - `{$bytes: string}` objects are converted to `Uint8Array` instances
103
+ *
104
+ * Use this when you have a JavaScript object (e.g., from `JSON.parse()`) and
105
+ * need to convert it to the Lex data model. For parsing JSON strings directly,
106
+ * use {@link lexParse} instead.
107
+ *
108
+ * @param value - The JSON value to convert
109
+ * @param options - Parsing options (e.g., strict mode)
110
+ * @returns The converted Lex value
111
+ * @throws {TypeError} If strict mode is enabled and invalid Lex values are found
112
+ * @throws {TypeError} If the value contains unsupported types (e.g., undefined at top level)
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * import { jsonToLex } from '@atproto/lex'
117
+ *
118
+ * // Convert parsed JSON to Lex values
119
+ * const lex = jsonToLex({
120
+ * ref: { $link: 'bafyrei...' }, // Converted to Cid
121
+ * data: { $bytes: 'SGVsbG8sIHdvcmxkIQ==' } // Converted to Uint8Array
122
+ * })
123
+ * ```
124
+ */
38
125
  function jsonToLex(value, options = { strict: false }) {
39
126
  switch (typeof value) {
40
127
  case 'object': {
@@ -94,6 +181,33 @@ function jsonObjectToLexMap(input, options) {
94
181
  }
95
182
  return copy ?? input;
96
183
  }
184
+ /**
185
+ * Converts a Lex value to a JSON-compatible value.
186
+ *
187
+ * This function transforms Lex data model values into plain JavaScript objects
188
+ * suitable for JSON serialization:
189
+ * - `Cid` instances are converted to `{$link: string}` objects
190
+ * - `Uint8Array` instances are converted to `{$bytes: string}` objects (base64)
191
+ *
192
+ * Use this when you need to convert Lex values to plain objects (e.g., for
193
+ * custom serialization or inspection). For direct JSON string output, use
194
+ * {@link lexStringify} instead.
195
+ *
196
+ * @param value - The Lex value to convert
197
+ * @returns The JSON-compatible value
198
+ * @throws {TypeError} If the value contains unsupported types
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * import { lexToJson } from '@atproto/lex'
203
+ *
204
+ * // Convert Lex values to JSON-compatible objects
205
+ * const obj = lexToJson({
206
+ * ref: someCid, // Converted to { $link: string }
207
+ * data: someBytes // Converted to { $bytes: string }
208
+ * })
209
+ * ```
210
+ */
97
211
  function lexToJson(value) {
98
212
  switch (typeof value) {
99
213
  case 'object':