@atproto/lex-cbor 0.0.8 → 0.0.10
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 +20 -0
- package/LICENSE.txt +1 -1
- package/dist/encoding.d.ts +2 -0
- package/dist/encoding.d.ts.map +1 -1
- package/dist/index.cjs +313 -244
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +314 -245
- package/package.json +4 -4
- package/src/encoding.ts +50 -64
- package/src/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex-cbor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Lexicon encoding utilities for AT Lexicon data in CBOR format",
|
|
6
6
|
"keywords": [
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"tslib": "^2.8.1",
|
|
41
|
-
"@atproto/lex-data": "0.0.
|
|
41
|
+
"@atproto/lex-data": "^0.0.10"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"cborg": "^4.
|
|
44
|
+
"cborg": "^4.5.8",
|
|
45
45
|
"vite": "^6.2.0",
|
|
46
46
|
"vitest": "^4.0.16",
|
|
47
|
-
"@atproto/lex-json": "0.0.
|
|
47
|
+
"@atproto/lex-json": "^0.0.10"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"dev": "vite build --watch",
|
package/src/encoding.ts
CHANGED
|
@@ -8,11 +8,9 @@ import {
|
|
|
8
8
|
decodeFirst as cborgDecodeFirst,
|
|
9
9
|
encode as cborgEncode,
|
|
10
10
|
} from 'cborg'
|
|
11
|
+
import { OptionalTypeEncoder } from 'cborg/lib/encode'
|
|
11
12
|
import { Cid, LexValue, decodeCid, ifCid } from '@atproto/lex-data'
|
|
12
13
|
|
|
13
|
-
// @NOTE This was inspired by @ipld/dag-cbor implementation, but adapted to
|
|
14
|
-
// match the AT Data Model constraints. Floats, in particular, are not allowed.
|
|
15
|
-
|
|
16
14
|
// @NOTE "cborg" version 4 is required to support multi-decoding via the
|
|
17
15
|
// "decodeFirst" function. However, that version only exposes ES modules.
|
|
18
16
|
// Because this package is using "commonjs", "cborg" will be bundled instead of
|
|
@@ -20,69 +18,47 @@ import { Cid, LexValue, decodeCid, ifCid } from '@atproto/lex-data'
|
|
|
20
18
|
|
|
21
19
|
const CID_CBOR_TAG = 42
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
21
|
+
export const encodeOptions = Object.freeze<EncodeOptions>({
|
|
22
|
+
float64: true,
|
|
23
|
+
ignoreUndefinedProperties: true,
|
|
24
|
+
typeEncoders: Object.freeze<{ [typeName: string]: OptionalTypeEncoder }>({
|
|
25
|
+
Map: (map: Map<unknown, unknown>): null => {
|
|
26
|
+
for (const key of map.keys()) {
|
|
27
|
+
if (typeof key !== 'string') {
|
|
28
|
+
throw new Error(
|
|
29
|
+
'Only string keys are allowed in CBOR "map" by the AT Data Model',
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// @NOTE Maps will be encoded as CBOR "map", which will be decoded as object.
|
|
35
|
+
return null
|
|
36
|
+
},
|
|
37
|
+
Object: (obj: object): Token[] | null => {
|
|
38
|
+
const cid = ifCid(obj)
|
|
39
|
+
if (cid) {
|
|
40
|
+
const bytes = new Uint8Array(cid.bytes.byteLength + 1)
|
|
41
|
+
bytes.set(cid.bytes, 1) // prefix is 0x00, for historical reasons
|
|
42
|
+
return [new Token(Type.tag, CID_CBOR_TAG), new Token(Type.bytes, bytes)]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Fallback to default object encoder
|
|
46
|
+
return null
|
|
47
|
+
},
|
|
48
|
+
undefined: (): null => {
|
|
49
|
+
throw new Error('`undefined` is not supported by the AT Data Model')
|
|
50
|
+
},
|
|
51
|
+
number: (num: number): null => {
|
|
52
|
+
if (Number.isSafeInteger(num)) return null
|
|
53
53
|
|
|
54
|
-
function mapEncoder(map: Map<unknown, unknown>): null {
|
|
55
|
-
for (const key of map.keys()) {
|
|
56
|
-
if (typeof key !== 'string') {
|
|
57
54
|
throw new Error(
|
|
58
|
-
|
|
55
|
+
`Non-integer numbers (${num}) are not supported by the AT Data Model`,
|
|
59
56
|
)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return null
|
|
64
|
-
}
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
})
|
|
65
60
|
|
|
66
|
-
const
|
|
67
|
-
float64: true,
|
|
68
|
-
typeEncoders: {
|
|
69
|
-
Map: mapEncoder,
|
|
70
|
-
Object: objectEncoder,
|
|
71
|
-
undefined: undefinedEncoder,
|
|
72
|
-
number: numberEncoder,
|
|
73
|
-
},
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function cidDecoder(bytes: Uint8Array): Cid {
|
|
77
|
-
if (bytes[0] !== 0) {
|
|
78
|
-
throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
|
|
79
|
-
}
|
|
80
|
-
return decodeCid(bytes.subarray(1)) // ignore leading 0x00
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const tagDecoders: TagDecoder[] = []
|
|
84
|
-
tagDecoders[CID_CBOR_TAG] = cidDecoder
|
|
85
|
-
const decodeOptions: DecodeOptions = {
|
|
61
|
+
export const decodeOptions = /*#__PURE__*/ Object.freeze<DecodeOptions>({
|
|
86
62
|
allowIndefinite: false,
|
|
87
63
|
coerceUndefinedToNull: true,
|
|
88
64
|
allowNaN: false,
|
|
@@ -91,8 +67,18 @@ const decodeOptions: DecodeOptions = {
|
|
|
91
67
|
strict: true,
|
|
92
68
|
useMaps: false,
|
|
93
69
|
rejectDuplicateMapKeys: true,
|
|
94
|
-
tags:
|
|
95
|
-
|
|
70
|
+
tags: /*#__PURE__*/ Object.freeze<TagDecoder[]>(
|
|
71
|
+
/*#__PURE__*/ Object.assign([], {
|
|
72
|
+
[CID_CBOR_TAG]: (bytes: Uint8Array): Cid => {
|
|
73
|
+
if (bytes[0] !== 0) {
|
|
74
|
+
throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
|
|
75
|
+
}
|
|
76
|
+
const cibBytes = bytes.subarray(1) // ignore leading 0x00
|
|
77
|
+
return decodeCid(cibBytes)
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
) as TagDecoder[],
|
|
81
|
+
})
|
|
96
82
|
|
|
97
83
|
export function encode<T extends LexValue = LexValue>(data: T): Uint8Array {
|
|
98
84
|
return cborgEncode(data, encodeOptions)
|
package/src/index.ts
CHANGED
|
@@ -9,7 +9,9 @@ export { decode, decodeAll, encode } from './encoding.js'
|
|
|
9
9
|
export {
|
|
10
10
|
decode as cborDecode,
|
|
11
11
|
decodeAll as cborDecodeAll,
|
|
12
|
+
decodeOptions,
|
|
12
13
|
encode as cborEncode,
|
|
14
|
+
encodeOptions,
|
|
13
15
|
} from './encoding.js'
|
|
14
16
|
|
|
15
17
|
export async function cidForLex(value: LexValue): Promise<CborCid> {
|