@atproto/lex-cbor 0.0.6 → 0.0.8
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 +18 -0
- package/dist/index.cjs +5 -58
- package/dist/index.d.ts +5 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +6 -57
- package/package.json +3 -4
- package/src/encoding.ts +2 -2
- package/src/index.ts +11 -68
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @atproto/lex-cbor
|
|
2
2
|
|
|
3
|
+
## 0.0.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e), [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e), [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e), [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e)]:
|
|
8
|
+
- @atproto/lex-data@0.0.8
|
|
9
|
+
|
|
10
|
+
## 0.0.7
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- [#4512](https://github.com/bluesky-social/atproto/pull/4512) [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Remove direct depdency on `multiformats`
|
|
15
|
+
|
|
16
|
+
- [#4512](https://github.com/bluesky-social/atproto/pull/4512) [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - **breaking**: Removed CID utilities (they can be imported from `@atproto/lex-data` instead)
|
|
17
|
+
|
|
18
|
+
- Updated dependencies [[`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe), [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe), [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe)]:
|
|
19
|
+
- @atproto/lex-data@0.0.7
|
|
20
|
+
|
|
3
21
|
## 0.0.6
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const digest = require("multiformats/hashes/digest");
|
|
4
|
-
const sha2 = require("multiformats/hashes/sha2");
|
|
5
3
|
const lexData = require("@atproto/lex-data");
|
|
6
4
|
const typeofs = [
|
|
7
5
|
"string",
|
|
@@ -1582,7 +1580,7 @@ function cidEncoder(cid) {
|
|
|
1582
1580
|
return [new Token(Type.tag, CID_CBOR_TAG), new Token(Type.bytes, bytes)];
|
|
1583
1581
|
}
|
|
1584
1582
|
function objectEncoder(obj, _typ, _options) {
|
|
1585
|
-
const cid = lexData.
|
|
1583
|
+
const cid = lexData.ifCid(obj);
|
|
1586
1584
|
if (cid) return cidEncoder(cid);
|
|
1587
1585
|
return null;
|
|
1588
1586
|
}
|
|
@@ -1645,63 +1643,12 @@ function* decodeAll(data) {
|
|
|
1645
1643
|
} while (data.byteLength > 0);
|
|
1646
1644
|
}
|
|
1647
1645
|
async function cidForLex(value) {
|
|
1648
|
-
return cidForCbor(encode(value));
|
|
1649
|
-
}
|
|
1650
|
-
async function cidForCbor(bytes) {
|
|
1651
|
-
const digest2 = await sha2.sha256.digest(bytes);
|
|
1652
|
-
return lexData.createCid(lexData.DAG_CBOR_MULTICODEC, digest2);
|
|
1653
|
-
}
|
|
1654
|
-
async function verifyCidForBytes(cid, bytes) {
|
|
1655
|
-
const digest2 = await sha2.sha256.digest(bytes);
|
|
1656
|
-
const expected = lexData.createCid(cid.code, digest2);
|
|
1657
|
-
if (!cid.equals(expected)) {
|
|
1658
|
-
throw new Error(
|
|
1659
|
-
`Not a valid CID for bytes. Expected: ${expected.toString()} Got: ${cid.toString()}`
|
|
1660
|
-
);
|
|
1661
|
-
}
|
|
1662
|
-
}
|
|
1663
|
-
async function cidForRawBytes(bytes) {
|
|
1664
|
-
const digest2 = await sha2.sha256.digest(bytes);
|
|
1665
|
-
return lexData.createCid(lexData.RAW_BIN_MULTICODEC, digest2);
|
|
1666
|
-
}
|
|
1667
|
-
function cidForRawHash(hash) {
|
|
1668
|
-
const digest$1 = digest.create(sha2.sha256.code, hash);
|
|
1669
|
-
return lexData.createCid(lexData.RAW_BIN_MULTICODEC, digest$1);
|
|
1670
|
-
}
|
|
1671
|
-
function parseCidFromBytes(cidBytes) {
|
|
1672
|
-
const version = cidBytes[0];
|
|
1673
|
-
if (version !== 1) {
|
|
1674
|
-
throw new Error(`Unsupported CID version: ${version}`);
|
|
1675
|
-
}
|
|
1676
|
-
const code = cidBytes[1];
|
|
1677
|
-
if (code !== lexData.RAW_BIN_MULTICODEC && code !== lexData.DAG_CBOR_MULTICODEC) {
|
|
1678
|
-
throw new Error(`Unsupported CID codec: ${code}`);
|
|
1679
|
-
}
|
|
1680
|
-
const hashType = cidBytes[2];
|
|
1681
|
-
if (hashType !== lexData.SHA2_256_MULTIHASH_CODE) {
|
|
1682
|
-
throw new Error(`Unsupported CID hash function: ${hashType}`);
|
|
1683
|
-
}
|
|
1684
|
-
const hashLength = cidBytes[3];
|
|
1685
|
-
if (hashLength !== 32) {
|
|
1686
|
-
throw new Error(`Unexpected CID hash length: ${hashLength}`);
|
|
1687
|
-
}
|
|
1688
|
-
if (hashLength !== cidBytes.length - 4) {
|
|
1689
|
-
throw new Error(`Unexpected CID bytes length: ${hashLength}`);
|
|
1690
|
-
}
|
|
1691
|
-
const hashBytes = cidBytes.slice(4);
|
|
1692
|
-
const digest$1 = digest.create(hashType, hashBytes);
|
|
1693
|
-
return lexData.createCid(code, digest$1);
|
|
1646
|
+
return lexData.cidForCbor(encode(value));
|
|
1694
1647
|
}
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
});
|
|
1699
|
-
exports.cidForCbor = cidForCbor;
|
|
1648
|
+
exports.cborDecode = decode;
|
|
1649
|
+
exports.cborDecodeAll = decodeAll;
|
|
1650
|
+
exports.cborEncode = encode;
|
|
1700
1651
|
exports.cidForLex = cidForLex;
|
|
1701
|
-
exports.cidForRawBytes = cidForRawBytes;
|
|
1702
|
-
exports.cidForRawHash = cidForRawHash;
|
|
1703
1652
|
exports.decode = decode;
|
|
1704
1653
|
exports.decodeAll = decodeAll;
|
|
1705
1654
|
exports.encode = encode;
|
|
1706
|
-
exports.parseCidFromBytes = parseCidFromBytes;
|
|
1707
|
-
exports.verifyCidForBytes = verifyCidForBytes;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export {
|
|
1
|
+
import { CborCid, LexValue } from '@atproto/lex-data';
|
|
2
|
+
export type { Cid } from '@atproto/lex-data';
|
|
3
|
+
export type { CborCid, LexValue };
|
|
4
4
|
export { decode, decodeAll, encode } from './encoding.js';
|
|
5
|
-
export
|
|
6
|
-
export declare function cidForLex(value: LexValue): Promise<
|
|
7
|
-
export declare function cidForCbor(bytes: Uint8Array): Promise<Cid>;
|
|
8
|
-
export declare function verifyCidForBytes(cid: Cid, bytes: Uint8Array): Promise<void>;
|
|
9
|
-
export declare function cidForRawBytes(bytes: Uint8Array): Promise<Cid>;
|
|
10
|
-
export declare function cidForRawHash(hash: Uint8Array): Cid;
|
|
11
|
-
/**
|
|
12
|
-
* @note Only supports DASL CIDs
|
|
13
|
-
* @see {@link https://dasl.ing/cid.html}
|
|
14
|
-
* @throws if the input do not represent a valid DASL {@link Cid}
|
|
15
|
-
*/
|
|
16
|
-
export declare function parseCidFromBytes(cidBytes: Uint8Array): Cid;
|
|
5
|
+
export { decode as cborDecode, decodeAll as cborDecodeAll, encode as cborEncode, } from './encoding.js';
|
|
6
|
+
export declare function cidForLex(value: LexValue): Promise<CborCid>;
|
|
17
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
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,MAAM,IAAI,UAAU,GACrB,MAAM,eAAe,CAAA;AAEtB,wBAAsB,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAEjE"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { sha256 } from "multiformats/hashes/sha2";
|
|
3
|
-
import { sha256 as sha2562 } from "multiformats/hashes/sha2";
|
|
4
|
-
import { decodeCid, asCid, createCid, DAG_CBOR_MULTICODEC, RAW_BIN_MULTICODEC, SHA2_256_MULTIHASH_CODE } from "@atproto/lex-data";
|
|
1
|
+
import { decodeCid, ifCid, cidForCbor } from "@atproto/lex-data";
|
|
5
2
|
const typeofs = [
|
|
6
3
|
"string",
|
|
7
4
|
"number",
|
|
@@ -1581,7 +1578,7 @@ function cidEncoder(cid) {
|
|
|
1581
1578
|
return [new Token(Type.tag, CID_CBOR_TAG), new Token(Type.bytes, bytes)];
|
|
1582
1579
|
}
|
|
1583
1580
|
function objectEncoder(obj, _typ, _options) {
|
|
1584
|
-
const cid =
|
|
1581
|
+
const cid = ifCid(obj);
|
|
1585
1582
|
if (cid) return cidEncoder(cid);
|
|
1586
1583
|
return null;
|
|
1587
1584
|
}
|
|
@@ -1646,60 +1643,12 @@ function* decodeAll(data) {
|
|
|
1646
1643
|
async function cidForLex(value) {
|
|
1647
1644
|
return cidForCbor(encode(value));
|
|
1648
1645
|
}
|
|
1649
|
-
async function cidForCbor(bytes) {
|
|
1650
|
-
const digest = await sha256.digest(bytes);
|
|
1651
|
-
return createCid(DAG_CBOR_MULTICODEC, digest);
|
|
1652
|
-
}
|
|
1653
|
-
async function verifyCidForBytes(cid, bytes) {
|
|
1654
|
-
const digest = await sha256.digest(bytes);
|
|
1655
|
-
const expected = createCid(cid.code, digest);
|
|
1656
|
-
if (!cid.equals(expected)) {
|
|
1657
|
-
throw new Error(
|
|
1658
|
-
`Not a valid CID for bytes. Expected: ${expected.toString()} Got: ${cid.toString()}`
|
|
1659
|
-
);
|
|
1660
|
-
}
|
|
1661
|
-
}
|
|
1662
|
-
async function cidForRawBytes(bytes) {
|
|
1663
|
-
const digest = await sha256.digest(bytes);
|
|
1664
|
-
return createCid(RAW_BIN_MULTICODEC, digest);
|
|
1665
|
-
}
|
|
1666
|
-
function cidForRawHash(hash) {
|
|
1667
|
-
const digest = create(sha256.code, hash);
|
|
1668
|
-
return createCid(RAW_BIN_MULTICODEC, digest);
|
|
1669
|
-
}
|
|
1670
|
-
function parseCidFromBytes(cidBytes) {
|
|
1671
|
-
const version = cidBytes[0];
|
|
1672
|
-
if (version !== 1) {
|
|
1673
|
-
throw new Error(`Unsupported CID version: ${version}`);
|
|
1674
|
-
}
|
|
1675
|
-
const code = cidBytes[1];
|
|
1676
|
-
if (code !== RAW_BIN_MULTICODEC && code !== DAG_CBOR_MULTICODEC) {
|
|
1677
|
-
throw new Error(`Unsupported CID codec: ${code}`);
|
|
1678
|
-
}
|
|
1679
|
-
const hashType = cidBytes[2];
|
|
1680
|
-
if (hashType !== SHA2_256_MULTIHASH_CODE) {
|
|
1681
|
-
throw new Error(`Unsupported CID hash function: ${hashType}`);
|
|
1682
|
-
}
|
|
1683
|
-
const hashLength = cidBytes[3];
|
|
1684
|
-
if (hashLength !== 32) {
|
|
1685
|
-
throw new Error(`Unexpected CID hash length: ${hashLength}`);
|
|
1686
|
-
}
|
|
1687
|
-
if (hashLength !== cidBytes.length - 4) {
|
|
1688
|
-
throw new Error(`Unexpected CID bytes length: ${hashLength}`);
|
|
1689
|
-
}
|
|
1690
|
-
const hashBytes = cidBytes.slice(4);
|
|
1691
|
-
const digest = create(hashType, hashBytes);
|
|
1692
|
-
return createCid(code, digest);
|
|
1693
|
-
}
|
|
1694
1646
|
export {
|
|
1695
|
-
|
|
1647
|
+
decode as cborDecode,
|
|
1648
|
+
decodeAll as cborDecodeAll,
|
|
1649
|
+
encode as cborEncode,
|
|
1696
1650
|
cidForLex,
|
|
1697
|
-
cidForRawBytes,
|
|
1698
|
-
cidForRawHash,
|
|
1699
1651
|
decode,
|
|
1700
1652
|
decodeAll,
|
|
1701
|
-
encode
|
|
1702
|
-
sha2562 as hasher,
|
|
1703
|
-
parseCidFromBytes,
|
|
1704
|
-
verifyCidForBytes
|
|
1653
|
+
encode
|
|
1705
1654
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex-cbor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Lexicon encoding utilities for AT Lexicon data in CBOR format",
|
|
6
6
|
"keywords": [
|
|
@@ -37,15 +37,14 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"multiformats": "^9.9.0",
|
|
41
40
|
"tslib": "^2.8.1",
|
|
42
|
-
"@atproto/lex-data": "0.0.
|
|
41
|
+
"@atproto/lex-data": "0.0.8"
|
|
43
42
|
},
|
|
44
43
|
"devDependencies": {
|
|
45
44
|
"cborg": "^4.3.0",
|
|
46
45
|
"vite": "^6.2.0",
|
|
47
46
|
"vitest": "^4.0.16",
|
|
48
|
-
"@atproto/lex-json": "0.0.
|
|
47
|
+
"@atproto/lex-json": "0.0.8"
|
|
49
48
|
},
|
|
50
49
|
"scripts": {
|
|
51
50
|
"dev": "vite build --watch",
|
package/src/encoding.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
decodeFirst as cborgDecodeFirst,
|
|
9
9
|
encode as cborgEncode,
|
|
10
10
|
} from 'cborg'
|
|
11
|
-
import { Cid, LexValue,
|
|
11
|
+
import { Cid, LexValue, decodeCid, ifCid } from '@atproto/lex-data'
|
|
12
12
|
|
|
13
13
|
// @NOTE This was inspired by @ipld/dag-cbor implementation, but adapted to
|
|
14
14
|
// match the AT Data Model constraints. Floats, in particular, are not allowed.
|
|
@@ -31,7 +31,7 @@ function objectEncoder(
|
|
|
31
31
|
_typ: string,
|
|
32
32
|
_options: EncodeOptions,
|
|
33
33
|
): Token[] | null {
|
|
34
|
-
const cid =
|
|
34
|
+
const cid = ifCid(obj)
|
|
35
35
|
if (cid) return cidEncoder(cid)
|
|
36
36
|
|
|
37
37
|
// @TODO strip undefined values somehow
|
package/src/index.ts
CHANGED
|
@@ -1,74 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { sha256 as hasher } from 'multiformats/hashes/sha2'
|
|
3
|
-
import {
|
|
4
|
-
Cid,
|
|
5
|
-
DAG_CBOR_MULTICODEC,
|
|
6
|
-
LexValue,
|
|
7
|
-
RAW_BIN_MULTICODEC,
|
|
8
|
-
SHA2_256_MULTIHASH_CODE,
|
|
9
|
-
createCid,
|
|
10
|
-
} from '@atproto/lex-data'
|
|
1
|
+
import { CborCid, LexValue, cidForCbor } from '@atproto/lex-data'
|
|
11
2
|
import { encode } from './encoding.js'
|
|
12
3
|
|
|
13
|
-
export {
|
|
14
|
-
export {
|
|
15
|
-
export type { Cid, LexValue }
|
|
16
|
-
|
|
17
|
-
export async function cidForLex(value: LexValue): Promise<Cid> {
|
|
18
|
-
return cidForCbor(encode(value))
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export async function cidForCbor(bytes: Uint8Array): Promise<Cid> {
|
|
22
|
-
const digest = await hasher.digest(bytes)
|
|
23
|
-
return createCid(DAG_CBOR_MULTICODEC, digest)
|
|
24
|
-
}
|
|
4
|
+
export type { Cid } from '@atproto/lex-data'
|
|
5
|
+
export type { CborCid, LexValue }
|
|
25
6
|
|
|
26
|
-
export
|
|
27
|
-
const digest = await hasher.digest(bytes)
|
|
28
|
-
const expected = createCid(cid.code, digest)
|
|
29
|
-
if (!cid.equals(expected)) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
`Not a valid CID for bytes. Expected: ${expected.toString()} Got: ${cid.toString()}`,
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function cidForRawBytes(bytes: Uint8Array): Promise<Cid> {
|
|
37
|
-
const digest = await hasher.digest(bytes)
|
|
38
|
-
return createCid(RAW_BIN_MULTICODEC, digest)
|
|
39
|
-
}
|
|
7
|
+
export { decode, decodeAll, encode } from './encoding.js'
|
|
40
8
|
|
|
41
|
-
export
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
9
|
+
export {
|
|
10
|
+
decode as cborDecode,
|
|
11
|
+
decodeAll as cborDecodeAll,
|
|
12
|
+
encode as cborEncode,
|
|
13
|
+
} from './encoding.js'
|
|
45
14
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* @see {@link https://dasl.ing/cid.html}
|
|
49
|
-
* @throws if the input do not represent a valid DASL {@link Cid}
|
|
50
|
-
*/
|
|
51
|
-
export function parseCidFromBytes(cidBytes: Uint8Array): Cid {
|
|
52
|
-
const version = cidBytes[0]
|
|
53
|
-
if (version !== 0x01) {
|
|
54
|
-
throw new Error(`Unsupported CID version: ${version}`)
|
|
55
|
-
}
|
|
56
|
-
const code = cidBytes[1]
|
|
57
|
-
if (code !== RAW_BIN_MULTICODEC && code !== DAG_CBOR_MULTICODEC) {
|
|
58
|
-
throw new Error(`Unsupported CID codec: ${code}`)
|
|
59
|
-
}
|
|
60
|
-
const hashType = cidBytes[2]
|
|
61
|
-
if (hashType !== SHA2_256_MULTIHASH_CODE) {
|
|
62
|
-
throw new Error(`Unsupported CID hash function: ${hashType}`)
|
|
63
|
-
}
|
|
64
|
-
const hashLength = cidBytes[3]
|
|
65
|
-
if (hashLength !== 32) {
|
|
66
|
-
throw new Error(`Unexpected CID hash length: ${hashLength}`)
|
|
67
|
-
}
|
|
68
|
-
if (hashLength !== cidBytes.length - 4) {
|
|
69
|
-
throw new Error(`Unexpected CID bytes length: ${hashLength}`)
|
|
70
|
-
}
|
|
71
|
-
const hashBytes = cidBytes.slice(4)
|
|
72
|
-
const digest = createDigest(hashType, hashBytes)
|
|
73
|
-
return createCid(code, digest)
|
|
15
|
+
export async function cidForLex(value: LexValue): Promise<CborCid> {
|
|
16
|
+
return cidForCbor(encode(value))
|
|
74
17
|
}
|