@atproto/lex-cbor 0.0.10 → 0.0.12

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,23 @@
1
1
  # @atproto/lex-cbor
2
2
 
3
+ ## 0.0.12
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df)]:
8
+ - @atproto/lex-data@0.0.12
9
+
10
+ ## 0.0.11
11
+
12
+ ### Patch Changes
13
+
14
+ - [#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
15
+
16
+ - [#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
17
+
18
+ - Updated dependencies [[`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015), [`ed61c62`](https://github.com/bluesky-social/atproto/commit/ed61c62f3161fcde85ee9a93f8ed339c7e06c015)]:
19
+ - @atproto/lex-data@0.0.11
20
+
3
21
  ## 0.0.10
4
22
 
5
23
  ### Patch Changes
@@ -1,7 +1,100 @@
1
1
  import { LexValue } from '@atproto/lex-data';
2
+ /**
3
+ * Configuration options for CBOR encoding that enforces AT Protocol data model
4
+ * constraints.
5
+ *
6
+ * This configuration ensures:
7
+ * - CIDs are encoded using CBOR tag 42 with a leading 0x00 byte prefix
8
+ * - Map keys must be strings (no numeric or other key types allowed)
9
+ * - `undefined` values are not permitted (undefined object properties will be stripped)
10
+ * - Only safe integer numbers are allowed (no floats or non-integer values)
11
+ */
2
12
  export declare const encodeOptions: Readonly<import("cborg/interface").EncodeOptions>;
13
+ /**
14
+ * Configuration options for CBOR decoding that enforces AT Protocol data model
15
+ * constraints.
16
+ */
3
17
  export declare const decodeOptions: Readonly<import("cborg/interface").DecodeOptions>;
18
+ /**
19
+ * Encodes a LexValue to CBOR bytes using the AT Protocol data model (DRISL format).
20
+ *
21
+ * @param data - The LexValue to encode
22
+ * @returns The CBOR-encoded bytes
23
+ * @throws {Error} If the data contains non-string map keys, undefined values, or non-integer numbers
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * import { encode } from '@atproto/lex-cbor'
28
+ *
29
+ * // Encode a simple object
30
+ * const bytes = encode({ text: 'Hello', count: 42 })
31
+ *
32
+ * // Encode an AT Protocol record
33
+ * const recordBytes = encode({
34
+ * $type: 'app.bsky.feed.post',
35
+ * text: 'Hello from AT Protocol!',
36
+ * createdAt: new Date().toISOString(),
37
+ * })
38
+ * ```
39
+ */
4
40
  export declare function encode<T extends LexValue = LexValue>(data: T): Uint8Array;
41
+ /**
42
+ * Decodes CBOR bytes to a LexValue using the AT Protocol data model (DRISL format).
43
+ *
44
+ * @typeParam T - Allows casting the decoded values to a specific LexValue subtype
45
+ * @param bytes - The CBOR bytes to decode
46
+ * @returns The decoded LexValue
47
+ * @throws {Error} If the bytes are not valid CBOR or violate AT Protocol constraints
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * import { encode, decode } from '@atproto/lex-cbor'
52
+ * import type { LexValue } from '@atproto/lex'
53
+ *
54
+ * // Round-trip encoding and decoding
55
+ * const original = { text: 'Hello', count: 42 }
56
+ * const bytes = encode(original)
57
+ * const decoded: LexValue = decode(bytes)
58
+ *
59
+ * // Decode with a specific type
60
+ * interface Post {
61
+ * $type: string
62
+ * text: string
63
+ * createdAt: string
64
+ * }
65
+ * const post = decode<Post>(recordBytes)
66
+ * ```
67
+ */
5
68
  export declare function decode<T extends LexValue = LexValue>(bytes: Uint8Array): T;
69
+ /**
70
+ * Generator that yields multiple decoded LexValues from a buffer containing
71
+ * concatenated CBOR-encoded values.
72
+ *
73
+ * This is useful for processing streams or files containing multiple
74
+ * CBOR-encoded records back-to-back (e.g., CAR file blocks or event streams).
75
+ *
76
+ * @typeParam T - Allows casting the decoded values to a specific LexValue subtype
77
+ * @param data - The buffer containing one or more CBOR-encoded values
78
+ * @yields Decoded LexValues one at a time
79
+ * @throws {Error} If any value in the buffer is not valid CBOR or violates AT Protocol constraints
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * import { encode, decodeAll } from '@atproto/lex-cbor'
84
+ *
85
+ * // Concatenate multiple encoded values
86
+ * const bytes1 = encode({ id: 1, text: 'First' })
87
+ * const bytes2 = encode({ id: 2, text: 'Second' })
88
+ * const combined = new Uint8Array([...bytes1, ...bytes2])
89
+ *
90
+ * // Decode all values from the combined buffer
91
+ * for (const value of decodeAll(combined)) {
92
+ * console.log(value)
93
+ * }
94
+ * // Output:
95
+ * // { id: 1, text: 'First' }
96
+ * // { id: 2, text: 'Second' }
97
+ * ```
98
+ */
6
99
  export declare function decodeAll<T extends LexValue = LexValue>(data: Uint8Array): Generator<T, void, unknown>;
7
100
  //# sourceMappingURL=encoding.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAWA,OAAO,EAAO,QAAQ,EAAoB,MAAM,mBAAmB,CAAA;AASnE,eAAO,MAAM,aAAa,mDAsCxB,CAAA;AAEF,eAAO,MAAM,aAAa,mDAoBxB,CAAA;AAEF,wBAAgB,MAAM,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAAE,IAAI,EAAE,CAAC,GAAG,UAAU,CAEzE;AAED,wBAAgB,MAAM,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAAE,KAAK,EAAE,UAAU,GAAG,CAAC,CAE1E;AAED,wBAAiB,SAAS,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EACtD,IAAI,EAAE,UAAU,GACf,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAM7B"}
1
+ {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAWA,OAAO,EAAO,QAAQ,EAAoB,MAAM,mBAAmB,CAAA;AASnE;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,mDAsCxB,CAAA;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,mDAoBxB,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAAE,IAAI,EAAE,CAAC,GAAG,UAAU,CAEzE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAAE,KAAK,EAAE,UAAU,GAAG,CAAC,CAE1E;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAiB,SAAS,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,EACtD,IAAI,EAAE,UAAU,GACf,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAM7B"}
package/dist/index.d.ts CHANGED
@@ -3,5 +3,29 @@ export type { Cid } from '@atproto/lex-data';
3
3
  export type { CborCid, LexValue };
4
4
  export { decode, decodeAll, encode } from './encoding.js';
5
5
  export { decode as cborDecode, decodeAll as cborDecodeAll, decodeOptions, encode as cborEncode, encodeOptions, } from './encoding.js';
6
+ /**
7
+ * Computes a CID (Content Identifier) for a given LexValue.
8
+ *
9
+ * This function first encodes the value to CBOR bytes using the AT Protocol
10
+ * data model constraints, then computes the CID hash of those bytes. The
11
+ * resulting CID can be used to uniquely identify and reference the content.
12
+ *
13
+ * @param value - The LexValue to compute a CID for
14
+ * @returns A promise that resolves to the CID for the CBOR-encoded value
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { cidForLex } from '@atproto/lex-cbor'
19
+ *
20
+ * const record = {
21
+ * $type: 'app.bsky.feed.post',
22
+ * text: 'Hello, AT Protocol!',
23
+ * createdAt: new Date().toISOString(),
24
+ * }
25
+ *
26
+ * const cid = await cidForLex(record)
27
+ * console.log(cid.toString()) // e.g., 'bafyreih...'
28
+ * ```
29
+ */
6
30
  export declare function cidForLex(value: LexValue): Promise<CborCid>;
7
31
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAc,MAAM,mBAAmB,CAAA;AAGjE,YAAY,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AAC5C,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAA;AAEjC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAEzD,OAAO,EACL,MAAM,IAAI,UAAU,EACpB,SAAS,IAAI,aAAa,EAC1B,aAAa,EACb,MAAM,IAAI,UAAU,EACpB,aAAa,GACd,MAAM,eAAe,CAAA;AAEtB,wBAAsB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAEjE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAc,MAAM,mBAAmB,CAAA;AAGjE,YAAY,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AAC5C,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAA;AAEjC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAEzD,OAAO,EACL,MAAM,IAAI,UAAU,EACpB,SAAS,IAAI,aAAa,EAC1B,aAAa,EACb,MAAM,IAAI,UAAU,EACpB,aAAa,GACd,MAAM,eAAe,CAAA;AAEtB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAEjE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lex-cbor",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "license": "MIT",
5
5
  "description": "Lexicon encoding utilities for AT Lexicon data in CBOR format",
6
6
  "keywords": [
@@ -26,25 +26,27 @@
26
26
  "./CHANGELOG.md"
27
27
  ],
28
28
  "sideEffects": false,
29
+ "type": "commonjs",
29
30
  "main": "./dist/index.cjs",
30
31
  "module": "./dist/index.mjs",
31
32
  "types": "./dist/index.d.ts",
32
33
  "exports": {
33
34
  ".": {
34
35
  "types": "./dist/index.d.ts",
36
+ "browser": "./dist/index.mjs",
35
37
  "import": "./dist/index.mjs",
36
- "require": "./dist/index.cjs"
38
+ "default": "./dist/index.cjs"
37
39
  }
38
40
  },
39
41
  "dependencies": {
40
42
  "tslib": "^2.8.1",
41
- "@atproto/lex-data": "^0.0.10"
43
+ "@atproto/lex-data": "^0.0.12"
42
44
  },
43
45
  "devDependencies": {
44
46
  "cborg": "^4.5.8",
45
47
  "vite": "^6.2.0",
46
48
  "vitest": "^4.0.16",
47
- "@atproto/lex-json": "^0.0.10"
49
+ "@atproto/lex-json": "^0.0.12"
48
50
  },
49
51
  "scripts": {
50
52
  "dev": "vite build --watch",
package/src/encoding.ts CHANGED
@@ -18,6 +18,16 @@ import { Cid, LexValue, decodeCid, ifCid } from '@atproto/lex-data'
18
18
 
19
19
  const CID_CBOR_TAG = 42
20
20
 
21
+ /**
22
+ * Configuration options for CBOR encoding that enforces AT Protocol data model
23
+ * constraints.
24
+ *
25
+ * This configuration ensures:
26
+ * - CIDs are encoded using CBOR tag 42 with a leading 0x00 byte prefix
27
+ * - Map keys must be strings (no numeric or other key types allowed)
28
+ * - `undefined` values are not permitted (undefined object properties will be stripped)
29
+ * - Only safe integer numbers are allowed (no floats or non-integer values)
30
+ */
21
31
  export const encodeOptions = Object.freeze<EncodeOptions>({
22
32
  float64: true,
23
33
  ignoreUndefinedProperties: true,
@@ -58,6 +68,10 @@ export const encodeOptions = Object.freeze<EncodeOptions>({
58
68
  }),
59
69
  })
60
70
 
71
+ /**
72
+ * Configuration options for CBOR decoding that enforces AT Protocol data model
73
+ * constraints.
74
+ */
61
75
  export const decodeOptions = /*#__PURE__*/ Object.freeze<DecodeOptions>({
62
76
  allowIndefinite: false,
63
77
  coerceUndefinedToNull: true,
@@ -80,14 +94,93 @@ export const decodeOptions = /*#__PURE__*/ Object.freeze<DecodeOptions>({
80
94
  ) as TagDecoder[],
81
95
  })
82
96
 
97
+ /**
98
+ * Encodes a LexValue to CBOR bytes using the AT Protocol data model (DRISL format).
99
+ *
100
+ * @param data - The LexValue to encode
101
+ * @returns The CBOR-encoded bytes
102
+ * @throws {Error} If the data contains non-string map keys, undefined values, or non-integer numbers
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * import { encode } from '@atproto/lex-cbor'
107
+ *
108
+ * // Encode a simple object
109
+ * const bytes = encode({ text: 'Hello', count: 42 })
110
+ *
111
+ * // Encode an AT Protocol record
112
+ * const recordBytes = encode({
113
+ * $type: 'app.bsky.feed.post',
114
+ * text: 'Hello from AT Protocol!',
115
+ * createdAt: new Date().toISOString(),
116
+ * })
117
+ * ```
118
+ */
83
119
  export function encode<T extends LexValue = LexValue>(data: T): Uint8Array {
84
120
  return cborgEncode(data, encodeOptions)
85
121
  }
86
122
 
123
+ /**
124
+ * Decodes CBOR bytes to a LexValue using the AT Protocol data model (DRISL format).
125
+ *
126
+ * @typeParam T - Allows casting the decoded values to a specific LexValue subtype
127
+ * @param bytes - The CBOR bytes to decode
128
+ * @returns The decoded LexValue
129
+ * @throws {Error} If the bytes are not valid CBOR or violate AT Protocol constraints
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * import { encode, decode } from '@atproto/lex-cbor'
134
+ * import type { LexValue } from '@atproto/lex'
135
+ *
136
+ * // Round-trip encoding and decoding
137
+ * const original = { text: 'Hello', count: 42 }
138
+ * const bytes = encode(original)
139
+ * const decoded: LexValue = decode(bytes)
140
+ *
141
+ * // Decode with a specific type
142
+ * interface Post {
143
+ * $type: string
144
+ * text: string
145
+ * createdAt: string
146
+ * }
147
+ * const post = decode<Post>(recordBytes)
148
+ * ```
149
+ */
87
150
  export function decode<T extends LexValue = LexValue>(bytes: Uint8Array): T {
88
151
  return cborgDecode(bytes, decodeOptions)
89
152
  }
90
153
 
154
+ /**
155
+ * Generator that yields multiple decoded LexValues from a buffer containing
156
+ * concatenated CBOR-encoded values.
157
+ *
158
+ * This is useful for processing streams or files containing multiple
159
+ * CBOR-encoded records back-to-back (e.g., CAR file blocks or event streams).
160
+ *
161
+ * @typeParam T - Allows casting the decoded values to a specific LexValue subtype
162
+ * @param data - The buffer containing one or more CBOR-encoded values
163
+ * @yields Decoded LexValues one at a time
164
+ * @throws {Error} If any value in the buffer is not valid CBOR or violates AT Protocol constraints
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * import { encode, decodeAll } from '@atproto/lex-cbor'
169
+ *
170
+ * // Concatenate multiple encoded values
171
+ * const bytes1 = encode({ id: 1, text: 'First' })
172
+ * const bytes2 = encode({ id: 2, text: 'Second' })
173
+ * const combined = new Uint8Array([...bytes1, ...bytes2])
174
+ *
175
+ * // Decode all values from the combined buffer
176
+ * for (const value of decodeAll(combined)) {
177
+ * console.log(value)
178
+ * }
179
+ * // Output:
180
+ * // { id: 1, text: 'First' }
181
+ * // { id: 2, text: 'Second' }
182
+ * ```
183
+ */
91
184
  export function* decodeAll<T extends LexValue = LexValue>(
92
185
  data: Uint8Array,
93
186
  ): Generator<T, void, unknown> {
package/src/index.ts CHANGED
@@ -14,6 +14,30 @@ export {
14
14
  encodeOptions,
15
15
  } from './encoding.js'
16
16
 
17
+ /**
18
+ * Computes a CID (Content Identifier) for a given LexValue.
19
+ *
20
+ * This function first encodes the value to CBOR bytes using the AT Protocol
21
+ * data model constraints, then computes the CID hash of those bytes. The
22
+ * resulting CID can be used to uniquely identify and reference the content.
23
+ *
24
+ * @param value - The LexValue to compute a CID for
25
+ * @returns A promise that resolves to the CID for the CBOR-encoded value
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * import { cidForLex } from '@atproto/lex-cbor'
30
+ *
31
+ * const record = {
32
+ * $type: 'app.bsky.feed.post',
33
+ * text: 'Hello, AT Protocol!',
34
+ * createdAt: new Date().toISOString(),
35
+ * }
36
+ *
37
+ * const cid = await cidForLex(record)
38
+ * console.log(cid.toString()) // e.g., 'bafyreih...'
39
+ * ```
40
+ */
17
41
  export async function cidForLex(value: LexValue): Promise<CborCid> {
18
42
  return cidForCbor(encode(value))
19
43
  }