@atproto/lex-cbor 0.0.2 → 0.0.4

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.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4457](https://github.com/bluesky-social/atproto/pull/4457) [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Throw an error when attempting to encode a float
8
+
9
+ - Updated dependencies [[`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece), [`e6b6107`](https://github.com/bluesky-social/atproto/commit/e6b6107e028fee964972274b71f5da1329a7bece)]:
10
+ - @atproto/lex-data@0.0.4
11
+
12
+ ## 0.0.3
13
+
14
+ ### Patch Changes
15
+
16
+ - [#4411](https://github.com/bluesky-social/atproto/pull/4411) [`7e1d458`](https://github.com/bluesky-social/atproto/commit/7e1d45877bca0f615e7b1313cfcc66823b3de758) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update name of dist files
17
+
18
+ - Updated dependencies [[`693784c`](https://github.com/bluesky-social/atproto/commit/693784c3a0dee4b6a29aa1e018fce682dcae148f)]:
19
+ - @atproto/lex-data@0.0.3
20
+
3
21
  ## 0.0.2
4
22
 
5
23
  ### Patch Changes
@@ -1,6 +1,5 @@
1
- import type { ByteView } from 'multiformats/block';
2
1
  import { LexValue } from '@atproto/lex-data';
3
- export declare function encode<T extends LexValue>(data: T): ByteView<T>;
4
- export declare function decode<T extends LexValue>(bytes: ByteView<T>): T;
5
- export declare function decodeAll<T = LexValue>(data: ByteView<T>): Generator<T, void, unknown>;
2
+ export declare function encode<T extends LexValue = LexValue>(data: T): Uint8Array;
3
+ export declare function decode<T extends LexValue = LexValue>(bytes: Uint8Array): T;
4
+ export declare function decodeAll<T extends LexValue = LexValue>(data: Uint8Array): Generator<T, void, unknown>;
6
5
  //# sourceMappingURL=encoding.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAO,QAAQ,EAAoB,MAAM,mBAAmB,CAAA;AAyEnE,wBAAgB,MAAM,CAAC,CAAC,SAAS,QAAQ,EAAE,IAAI,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAE/D;AAED,wBAAgB,MAAM,CAAC,CAAC,SAAS,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAEhE;AAED,wBAAiB,SAAS,CAAC,CAAC,GAAG,QAAQ,EACrC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAChB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAM7B"}
1
+ {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAUA,OAAO,EAAO,QAAQ,EAAoB,MAAM,mBAAmB,CAAA;AAsFnE,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"}
@@ -1576,18 +1576,21 @@ function decode$1(data, options) {
1576
1576
  return decoded;
1577
1577
  }
1578
1578
  const CID_CBOR_TAG = 42;
1579
- function cidEncoder(obj) {
1580
- const cid = lexData.asCid(obj);
1581
- if (!cid) return null;
1579
+ function cidEncoder(cid) {
1582
1580
  const bytes = new Uint8Array(cid.bytes.byteLength + 1);
1583
1581
  bytes.set(cid.bytes, 1);
1584
1582
  return [new Token(Type.tag, CID_CBOR_TAG), new Token(Type.bytes, bytes)];
1585
1583
  }
1584
+ function objectEncoder(obj, _typ, _options) {
1585
+ const cid = lexData.asCid(obj);
1586
+ if (cid) return cidEncoder(cid);
1587
+ return null;
1588
+ }
1586
1589
  function undefinedEncoder() {
1587
1590
  throw new Error("`undefined` is not allowed by the AT Data Model");
1588
1591
  }
1589
1592
  function numberEncoder(num) {
1590
- if (Number.isInteger(num)) return null;
1593
+ if (Number.isInteger(num) && Number.isSafeInteger(num)) return null;
1591
1594
  throw new Error("Non-integer numbers are not allowed by the AT Data Model");
1592
1595
  }
1593
1596
  function mapEncoder(map) {
@@ -1601,9 +1604,10 @@ function mapEncoder(map) {
1601
1604
  return null;
1602
1605
  }
1603
1606
  const encodeOptions = {
1607
+ float64: true,
1604
1608
  typeEncoders: {
1605
1609
  Map: mapEncoder,
1606
- Object: cidEncoder,
1610
+ Object: objectEncoder,
1607
1611
  undefined: undefinedEncoder,
1608
1612
  number: numberEncoder
1609
1613
  }
@@ -1575,18 +1575,21 @@ function decode$1(data, options) {
1575
1575
  return decoded;
1576
1576
  }
1577
1577
  const CID_CBOR_TAG = 42;
1578
- function cidEncoder(obj) {
1579
- const cid = asCid(obj);
1580
- if (!cid) return null;
1578
+ function cidEncoder(cid) {
1581
1579
  const bytes = new Uint8Array(cid.bytes.byteLength + 1);
1582
1580
  bytes.set(cid.bytes, 1);
1583
1581
  return [new Token(Type.tag, CID_CBOR_TAG), new Token(Type.bytes, bytes)];
1584
1582
  }
1583
+ function objectEncoder(obj, _typ, _options) {
1584
+ const cid = asCid(obj);
1585
+ if (cid) return cidEncoder(cid);
1586
+ return null;
1587
+ }
1585
1588
  function undefinedEncoder() {
1586
1589
  throw new Error("`undefined` is not allowed by the AT Data Model");
1587
1590
  }
1588
1591
  function numberEncoder(num) {
1589
- if (Number.isInteger(num)) return null;
1592
+ if (Number.isInteger(num) && Number.isSafeInteger(num)) return null;
1590
1593
  throw new Error("Non-integer numbers are not allowed by the AT Data Model");
1591
1594
  }
1592
1595
  function mapEncoder(map) {
@@ -1600,9 +1603,10 @@ function mapEncoder(map) {
1600
1603
  return null;
1601
1604
  }
1602
1605
  const encodeOptions = {
1606
+ float64: true,
1603
1607
  typeEncoders: {
1604
1608
  Map: mapEncoder,
1605
- Object: cidEncoder,
1609
+ Object: objectEncoder,
1606
1610
  undefined: undefinedEncoder,
1607
1611
  number: numberEncoder
1608
1612
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lex-cbor",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "license": "MIT",
5
5
  "description": "Lexicon encoding utilities for AT Lexicon data in CBOR format",
6
6
  "keywords": [
@@ -26,26 +26,26 @@
26
26
  "./CHANGELOG.md"
27
27
  ],
28
28
  "sideEffects": false,
29
- "main": "./dist/index.cjs.js",
30
- "module": "./dist/index.es.js",
29
+ "main": "./dist/index.cjs",
30
+ "module": "./dist/index.mjs",
31
31
  "types": "./dist/index.d.ts",
32
32
  "exports": {
33
33
  ".": {
34
34
  "types": "./dist/index.d.ts",
35
- "import": "./dist/index.es.js",
36
- "require": "./dist/index.cjs.js"
35
+ "import": "./dist/index.mjs",
36
+ "require": "./dist/index.cjs"
37
37
  }
38
38
  },
39
39
  "dependencies": {
40
40
  "multiformats": "^9.9.0",
41
41
  "tslib": "^2.8.1",
42
- "@atproto/lex-data": "0.0.2"
42
+ "@atproto/lex-data": "0.0.4"
43
43
  },
44
44
  "devDependencies": {
45
45
  "cborg": "^4.3.0",
46
46
  "jest": "^28.1.2",
47
47
  "vite": "^6.2.0",
48
- "@atproto/lex-json": "0.0.2"
48
+ "@atproto/lex-json": "0.0.4"
49
49
  },
50
50
  "scripts": {
51
51
  "dev": "vite build --watch",
package/src/encoding.ts CHANGED
@@ -8,7 +8,6 @@ import {
8
8
  decodeFirst as cborgDecodeFirst,
9
9
  encode as cborgEncode,
10
10
  } from 'cborg'
11
- import type { ByteView } from 'multiformats/block'
12
11
  import { Cid, LexValue, asCid, decodeCid } from '@atproto/lex-data'
13
12
 
14
13
  // @NOTE This was inspired by @ipld/dag-cbor implementation, but adapted to
@@ -21,21 +20,33 @@ import { Cid, LexValue, asCid, decodeCid } from '@atproto/lex-data'
21
20
 
22
21
  const CID_CBOR_TAG = 42
23
22
 
24
- function cidEncoder(obj: object): Token[] | null {
25
- const cid = asCid(obj)
26
- if (!cid) return null
27
-
23
+ function cidEncoder(cid: Cid): Token[] {
28
24
  const bytes = new Uint8Array(cid.bytes.byteLength + 1)
29
25
  bytes.set(cid.bytes, 1) // prefix is 0x00, for historical reasons
30
26
  return [new Token(Type.tag, CID_CBOR_TAG), new Token(Type.bytes, bytes)]
31
27
  }
32
28
 
29
+ function objectEncoder(
30
+ obj: object,
31
+ _typ: string,
32
+ _options: EncodeOptions,
33
+ ): Token[] | null {
34
+ const cid = asCid(obj)
35
+ if (cid) return cidEncoder(cid)
36
+
37
+ // @TODO strip undefined values somehow
38
+ // https://github.com/rvagg/cborg/issues/154
39
+
40
+ // Fallback to default object encoder
41
+ return null
42
+ }
43
+
33
44
  function undefinedEncoder(): null {
34
45
  throw new Error('`undefined` is not allowed by the AT Data Model')
35
46
  }
36
47
 
37
48
  function numberEncoder(num: number): null {
38
- if (Number.isInteger(num)) return null
49
+ if (Number.isInteger(num) && Number.isSafeInteger(num)) return null
39
50
 
40
51
  throw new Error('Non-integer numbers are not allowed by the AT Data Model')
41
52
  }
@@ -53,9 +64,10 @@ function mapEncoder(map: Map<unknown, unknown>): null {
53
64
  }
54
65
 
55
66
  const encodeOptions: EncodeOptions = {
67
+ float64: true,
56
68
  typeEncoders: {
57
69
  Map: mapEncoder,
58
- Object: cidEncoder,
70
+ Object: objectEncoder,
59
71
  undefined: undefinedEncoder,
60
72
  number: numberEncoder,
61
73
  },
@@ -82,16 +94,16 @@ const decodeOptions: DecodeOptions = {
82
94
  tags: tagDecoders,
83
95
  }
84
96
 
85
- export function encode<T extends LexValue>(data: T): ByteView<T> {
97
+ export function encode<T extends LexValue = LexValue>(data: T): Uint8Array {
86
98
  return cborgEncode(data, encodeOptions)
87
99
  }
88
100
 
89
- export function decode<T extends LexValue>(bytes: ByteView<T>): T {
101
+ export function decode<T extends LexValue = LexValue>(bytes: Uint8Array): T {
90
102
  return cborgDecode(bytes, decodeOptions)
91
103
  }
92
104
 
93
- export function* decodeAll<T = LexValue>(
94
- data: ByteView<T>,
105
+ export function* decodeAll<T extends LexValue = LexValue>(
106
+ data: Uint8Array,
95
107
  ): Generator<T, void, unknown> {
96
108
  do {
97
109
  const [result, remainingBytes] = cborgDecodeFirst(data, decodeOptions)
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "extends": "../../../tsconfig/tests.json",
3
- "include": ["./tests", "./src/**.test.ts"],
3
+ "include": ["./tests", "./src/**/*.test.ts"],
4
4
  "compilerOptions": {
5
5
  "noImplicitAny": true,
6
6
  "rootDir": "./",