@atproto/lex-data 0.0.7 → 0.0.9
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 +22 -0
- package/LICENSE.txt +1 -1
- package/dist/cid.d.ts +73 -48
- package/dist/cid.d.ts.map +1 -1
- package/dist/cid.js +132 -39
- package/dist/cid.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/uint8array.d.ts +1 -0
- package/dist/uint8array.d.ts.map +1 -1
- package/dist/uint8array.js +12 -6
- package/dist/uint8array.js.map +1 -1
- package/dist/utf8.d.ts.map +1 -1
- package/dist/utf8.js +4 -8
- package/dist/utf8.js.map +1 -1
- package/package.json +2 -3
- package/src/cid-implementation.test.ts +137 -0
- package/src/cid.test.ts +232 -23
- package/src/cid.ts +228 -99
- package/src/index.ts +0 -1
- package/src/uint8array.test.ts +24 -5
- package/src/uint8array.ts +13 -6
- package/src/utf8-len.test.ts +19 -28
- package/src/utf8.ts +4 -8
- package/dist/language.d.ts +0 -18
- package/dist/language.d.ts.map +0 -1
- package/dist/language.js +0 -30
- package/dist/language.js.map +0 -1
- package/src/language.test.ts +0 -88
- package/src/language.ts +0 -39
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @atproto/lex-data
|
|
2
2
|
|
|
3
|
+
## 0.0.9
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4562](https://github.com/bluesky-social/atproto/pull/4562) [`7310b97`](https://github.com/bluesky-social/atproto/commit/7310b9704de678a3b389a741784d58bb7f79b10b) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add `ifUint8Array` utility function
|
|
8
|
+
|
|
9
|
+
- [#4571](https://github.com/bluesky-social/atproto/pull/4571) [`99963d0`](https://github.com/bluesky-social/atproto/commit/99963d002a9e030e79aed5fba700e0a68f31e101) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Rename `isLanguageString` to `validateLanguage`
|
|
10
|
+
|
|
11
|
+
- [#4571](https://github.com/bluesky-social/atproto/pull/4571) [`99963d0`](https://github.com/bluesky-social/atproto/commit/99963d002a9e030e79aed5fba700e0a68f31e101) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve code coverage of tests
|
|
12
|
+
|
|
13
|
+
## 0.0.8
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add `asMultiformatsCID` utility for improved compatibility between `Cid` interface and legacy use of multiformat's `CID`
|
|
18
|
+
|
|
19
|
+
- [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Make the `Cid` interface stricter to allow for better futur proofing.
|
|
20
|
+
|
|
21
|
+
- [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - `isCid`, `ifCid` and `asCid` now always return the input value instead a potentially different instance.
|
|
22
|
+
|
|
23
|
+
- [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `createCid` utility
|
|
24
|
+
|
|
3
25
|
## 0.0.7
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Dual MIT/Apache-2.0 License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022-
|
|
3
|
+
Copyright (c) 2022-2026 Bluesky Social PBC, and Contributors
|
|
4
4
|
|
|
5
5
|
Except as otherwise noted in individual files, this software is licensed under the MIT license (<http://opensource.org/licenses/MIT>), or the Apache License, Version 2.0 (<http://www.apache.org/licenses/LICENSE-2.0>).
|
|
6
6
|
|
package/dist/cid.d.ts
CHANGED
|
@@ -5,12 +5,19 @@ export declare const RAW_MULTICODEC = 85;
|
|
|
5
5
|
export type RAW_MULTICODEC = typeof RAW_MULTICODEC;
|
|
6
6
|
export declare const SHA256_MULTIHASH: 18;
|
|
7
7
|
export type SHA256_MULTIHASH = typeof SHA256_MULTIHASH;
|
|
8
|
-
export
|
|
9
|
-
|
|
8
|
+
export declare const SHA512_MULTIHASH: 19;
|
|
9
|
+
export type SHA512_MULTIHASH = typeof SHA512_MULTIHASH;
|
|
10
|
+
export interface Multihash<TCode extends number = number> {
|
|
11
|
+
/**
|
|
12
|
+
* Code of the multihash
|
|
13
|
+
*/
|
|
14
|
+
code: TCode;
|
|
15
|
+
/**
|
|
16
|
+
* Raw digest
|
|
17
|
+
*/
|
|
10
18
|
digest: Uint8Array;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
19
|
+
}
|
|
20
|
+
export declare function multihashEquals(a: Multihash, b: Multihash): boolean;
|
|
14
21
|
declare module 'multiformats/cid' {
|
|
15
22
|
/**
|
|
16
23
|
* @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and
|
|
@@ -28,53 +35,58 @@ declare module 'multiformats/cid' {
|
|
|
28
35
|
*
|
|
29
36
|
* In order to avoid compatibility issues, while preparing for future breaking
|
|
30
37
|
* changes (CID in multiformats v10+ has a slightly different interface), as
|
|
31
|
-
* we update or swap out `multiformats`,
|
|
32
|
-
* interface.
|
|
38
|
+
* we update or swap out `multiformats`, `@atproto/lex-data` provides its own
|
|
39
|
+
* stable {@link Cid} interface.
|
|
33
40
|
*/
|
|
34
41
|
interface CID {
|
|
35
42
|
}
|
|
36
43
|
}
|
|
37
|
-
export { CID };
|
|
44
|
+
export { /** @deprecated */ CID };
|
|
38
45
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
46
|
+
* Converts a {@link Cid} to a multiformats {@link CID} instance.
|
|
47
|
+
*
|
|
48
|
+
* @deprecated Packages depending on `@atproto/lex-data` should use the
|
|
49
|
+
* {@link Cid} interface instead of relying on `multiformats`'s {@link CID}
|
|
50
|
+
* implementation directly. This is to avoid compatibility issues, and in order
|
|
51
|
+
* to allow better portability, compatibility and future updates.
|
|
41
52
|
*/
|
|
42
|
-
export
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
export declare function asMultiformatsCID<TVersion extends 0 | 1 = 0 | 1, TCode extends number = number, TMultihashCode extends number = number>(input: Cid<TVersion, TCode, TMultihashCode>): CID & Cid<TVersion, TCode, TMultihashCode>;
|
|
54
|
+
/**
|
|
55
|
+
* Interface for working with CIDs
|
|
56
|
+
*/
|
|
57
|
+
export interface Cid<TVersion extends 0 | 1 = 0 | 1, TCode extends number = number, TMultihashCode extends number = number> {
|
|
58
|
+
readonly version: TVersion;
|
|
59
|
+
readonly code: TCode;
|
|
60
|
+
readonly multihash: Multihash<TMultihashCode>;
|
|
61
|
+
/**
|
|
62
|
+
* Binary representation of the whole CID.
|
|
63
|
+
*/
|
|
64
|
+
readonly bytes: Uint8Array;
|
|
65
|
+
equals(other: Cid): boolean;
|
|
48
66
|
toString(): string;
|
|
49
67
|
}
|
|
50
68
|
/**
|
|
51
69
|
* Represents the cid of raw binary data (like media blobs).
|
|
70
|
+
*
|
|
71
|
+
* The use of {@link SHA256_MULTIHASH} is recommended but not required for raw CIDs.
|
|
72
|
+
*
|
|
52
73
|
* @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}
|
|
53
74
|
*/
|
|
54
|
-
export
|
|
55
|
-
version: 1;
|
|
56
|
-
code: RAW_MULTICODEC;
|
|
57
|
-
}
|
|
75
|
+
export type RawCid = Cid<1, RAW_MULTICODEC>;
|
|
58
76
|
export declare function isRawCid(cid: Cid): cid is RawCid;
|
|
59
77
|
/**
|
|
60
78
|
* Represents a DASL compliant CID.
|
|
61
79
|
* @see {@link https://dasl.ing/cid.html DASL-CIDs}
|
|
62
80
|
*/
|
|
63
|
-
export
|
|
64
|
-
version: 1;
|
|
65
|
-
code: RAW_MULTICODEC | DAG_CBOR_MULTICODEC;
|
|
66
|
-
multihash: MultihashDigest<SHA256_MULTIHASH>;
|
|
67
|
-
}
|
|
81
|
+
export type DaslCid = Cid<1, RAW_MULTICODEC | DAG_CBOR_MULTICODEC, SHA256_MULTIHASH>;
|
|
68
82
|
export declare function isDaslCid(cid: Cid): cid is DaslCid;
|
|
69
83
|
/**
|
|
70
84
|
* Represents the cid of ATProto DAG-CBOR data (like repository MST nodes).
|
|
71
85
|
* @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}
|
|
72
86
|
*/
|
|
73
|
-
export
|
|
74
|
-
code: DAG_CBOR_MULTICODEC;
|
|
75
|
-
}
|
|
87
|
+
export type CborCid = Cid<1, DAG_CBOR_MULTICODEC, SHA256_MULTIHASH>;
|
|
76
88
|
export declare function isCborCid(cid: Cid): cid is CborCid;
|
|
77
|
-
export type
|
|
89
|
+
export type CheckCidOptions = {
|
|
78
90
|
flavor?: 'raw' | 'cbor' | 'dasl';
|
|
79
91
|
};
|
|
80
92
|
export type InferCheckedCid<TOptions> = TOptions extends {
|
|
@@ -83,36 +95,48 @@ export type InferCheckedCid<TOptions> = TOptions extends {
|
|
|
83
95
|
flavor: 'cbor';
|
|
84
96
|
} ? CborCid : Cid;
|
|
85
97
|
/**
|
|
86
|
-
*
|
|
98
|
+
* Type guard to check whether a {@link Cid} instance meets specific flavor
|
|
99
|
+
* constraints.
|
|
87
100
|
*/
|
|
88
|
-
export declare function
|
|
89
|
-
export declare function
|
|
90
|
-
export declare function isCid<TOptions extends CidCheckOptions>(value: unknown, options: TOptions): value is InferCheckedCid<TOptions>;
|
|
91
|
-
export declare function isCid(value: unknown, options?: CidCheckOptions): value is Cid;
|
|
101
|
+
export declare function checkCid<TOptions extends CheckCidOptions>(cid: Cid, options: TOptions): cid is InferCheckedCid<TOptions>;
|
|
102
|
+
export declare function checkCid(cid: Cid, options?: CheckCidOptions): boolean;
|
|
92
103
|
/**
|
|
93
|
-
*
|
|
104
|
+
* Type guard to check whether a value is a valid {@link Cid} instance,
|
|
105
|
+
* optionally checking for specific flavor constraints.
|
|
94
106
|
*/
|
|
95
|
-
export declare function
|
|
96
|
-
export declare function
|
|
107
|
+
export declare function isCid<TOptions extends CheckCidOptions>(value: unknown, options: TOptions): value is InferCheckedCid<TOptions>;
|
|
108
|
+
export declare function isCid(value: unknown, options?: CheckCidOptions): value is Cid;
|
|
97
109
|
/**
|
|
98
|
-
*
|
|
110
|
+
* Returns the input value as a {@link Cid} if it is valid, or `null` otherwise.
|
|
111
|
+
*/
|
|
112
|
+
export declare function ifCid<TValue, TOptions extends CheckCidOptions>(value: unknown, options: TOptions): (TValue & InferCheckedCid<TOptions>) | null;
|
|
113
|
+
export declare function ifCid<TValue>(value: TValue, options?: CheckCidOptions): (TValue & Cid) | null;
|
|
114
|
+
/**
|
|
115
|
+
* Returns the input value as a {@link Cid} if it is valid.
|
|
99
116
|
*
|
|
100
|
-
* @throws if the input is not a valid
|
|
117
|
+
* @throws if the input is not a valid {@link Cid}.
|
|
101
118
|
*/
|
|
102
|
-
export declare function
|
|
103
|
-
export declare function
|
|
119
|
+
export declare function asCid<TValue, TOptions extends CheckCidOptions>(value: TValue, options: TOptions): TValue & InferCheckedCid<TOptions>;
|
|
120
|
+
export declare function asCid<TValue>(value: TValue, options?: CheckCidOptions): Cid & TValue;
|
|
104
121
|
/**
|
|
105
122
|
* Decodes a CID from its binary representation.
|
|
106
123
|
*
|
|
107
124
|
* @see {@link https://dasl.ing/cid.html DASL-CIDs}
|
|
108
125
|
* @throws if the input do not represent a valid DASL {@link Cid}
|
|
109
126
|
*/
|
|
110
|
-
export declare function decodeCid<TOptions extends
|
|
111
|
-
export declare function decodeCid(cidBytes: Uint8Array, options?:
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
127
|
+
export declare function decodeCid<TOptions extends CheckCidOptions>(cidBytes: Uint8Array, options: TOptions): InferCheckedCid<TOptions>;
|
|
128
|
+
export declare function decodeCid(cidBytes: Uint8Array, options?: CheckCidOptions): Cid;
|
|
129
|
+
/**
|
|
130
|
+
* Parses a CID string into a Cid object.
|
|
131
|
+
*
|
|
132
|
+
* @throws if the input is not a valid CID string.
|
|
133
|
+
*/
|
|
134
|
+
export declare function parseCid<TOptions extends CheckCidOptions>(input: string, options: TOptions): InferCheckedCid<TOptions>;
|
|
135
|
+
export declare function parseCid(input: string, options?: CheckCidOptions): Cid;
|
|
136
|
+
export declare function validateCidString(input: string, options?: CheckCidOptions): boolean;
|
|
137
|
+
export declare function parseCidSafe<TOptions extends CheckCidOptions>(input: string, options: TOptions): InferCheckedCid<TOptions> | null;
|
|
138
|
+
export declare function parseCidSafe(input: string, options?: CheckCidOptions): Cid | null;
|
|
139
|
+
export declare function ensureValidCidString(input: string, options?: CheckCidOptions): void;
|
|
116
140
|
/**
|
|
117
141
|
* Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.
|
|
118
142
|
* @params cid The CID to match against the bytes.
|
|
@@ -120,7 +144,8 @@ export declare function ensureValidCidString(input: string, options?: CidCheckOp
|
|
|
120
144
|
* @returns true if the CID matches the bytes, false otherwise.
|
|
121
145
|
*/
|
|
122
146
|
export declare function isCidForBytes(cid: Cid, bytes: Uint8Array): Promise<boolean>;
|
|
147
|
+
export declare function createCid<TCode extends number, TMultihashCode extends number>(code: TCode, multihashCode: TMultihashCode, digest: Uint8Array): Cid<1, TCode, TMultihashCode>;
|
|
123
148
|
export declare function cidForCbor(bytes: Uint8Array): Promise<CborCid>;
|
|
124
149
|
export declare function cidForRawBytes(bytes: Uint8Array): Promise<RawCid>;
|
|
125
|
-
export declare function cidForRawHash(
|
|
150
|
+
export declare function cidForRawHash(digest: Uint8Array): RawCid;
|
|
126
151
|
//# sourceMappingURL=cid.d.ts.map
|
package/dist/cid.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cid.d.ts","sourceRoot":"","sources":["../src/cid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"cid.d.ts","sourceRoot":"","sources":["../src/cid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAMtC,eAAO,MAAM,mBAAmB,MAAO,CAAA;AACvC,MAAM,MAAM,mBAAmB,GAAG,OAAO,mBAAmB,CAAA;AAE5D,eAAO,MAAM,cAAc,KAAO,CAAA;AAClC,MAAM,MAAM,cAAc,GAAG,OAAO,cAAc,CAAA;AAElD,eAAO,MAAM,gBAAgB,IAAc,CAAA;AAC3C,MAAM,MAAM,gBAAgB,GAAG,OAAO,gBAAgB,CAAA;AAEtD,eAAO,MAAM,gBAAgB,IAAc,CAAA;AAC3C,MAAM,MAAM,gBAAgB,GAAG,OAAO,gBAAgB,CAAA;AAEtD,MAAM,WAAW,SAAS,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM;IACtD;;OAEG;IACH,IAAI,EAAE,KAAK,CAAA;IAEX;;OAEG;IACH,MAAM,EAAE,UAAU,CAAA;CACnB;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAEnE;AAED,OAAO,QAAQ,kBAAkB,CAAC;IAChC;;;;;;;;;;;;;;;;;;OAkBG;IACH,UAAU,GAAG;KAAG;CACjB;AAqBD,OAAO,EAAE,kBAAkB,CAAC,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;;;GAKG;AACH,wBAAsB,aAAa,CACjC,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,UAAU,GAChB,OAAO,CAAC,OAAO,CAAC,CAalB;AAED,wBAAgB,SAAS,CAAC,KAAK,SAAS,MAAM,EAAE,cAAc,SAAS,MAAM,EAC3E,IAAI,EAAE,KAAK,EACX,aAAa,EAAE,cAAc,EAC7B,MAAM,EAAE,UAAU,GAGJ,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,cAAc,CAAC,CAC5C;AAED,wBAAsB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAGpE;AAED,wBAAsB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAGvE;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAMxD"}
|
package/dist/cid.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CID = exports.SHA256_MULTIHASH = exports.RAW_MULTICODEC = exports.DAG_CBOR_MULTICODEC = void 0;
|
|
3
|
+
exports.CID = exports.SHA512_MULTIHASH = exports.SHA256_MULTIHASH = exports.RAW_MULTICODEC = exports.DAG_CBOR_MULTICODEC = void 0;
|
|
4
|
+
exports.multihashEquals = multihashEquals;
|
|
5
|
+
exports.asMultiformatsCID = asMultiformatsCID;
|
|
4
6
|
exports.isRawCid = isRawCid;
|
|
5
7
|
exports.isDaslCid = isDaslCid;
|
|
6
8
|
exports.isCborCid = isCborCid;
|
|
7
|
-
exports.
|
|
9
|
+
exports.checkCid = checkCid;
|
|
8
10
|
exports.isCid = isCid;
|
|
11
|
+
exports.ifCid = ifCid;
|
|
9
12
|
exports.asCid = asCid;
|
|
10
|
-
exports.parseCid = parseCid;
|
|
11
13
|
exports.decodeCid = decodeCid;
|
|
14
|
+
exports.parseCid = parseCid;
|
|
12
15
|
exports.validateCidString = validateCidString;
|
|
13
|
-
exports.
|
|
16
|
+
exports.parseCidSafe = parseCidSafe;
|
|
14
17
|
exports.ensureValidCidString = ensureValidCidString;
|
|
15
18
|
exports.isCidForBytes = isCidForBytes;
|
|
19
|
+
exports.createCid = createCid;
|
|
16
20
|
exports.cidForCbor = cidForCbor;
|
|
17
21
|
exports.cidForRawBytes = cidForRawBytes;
|
|
18
22
|
exports.cidForRawHash = cidForRawHash;
|
|
@@ -20,9 +24,35 @@ const cid_1 = require("multiformats/cid");
|
|
|
20
24
|
Object.defineProperty(exports, "CID", { enumerable: true, get: function () { return cid_1.CID; } });
|
|
21
25
|
const digest_1 = require("multiformats/hashes/digest");
|
|
22
26
|
const sha2_1 = require("multiformats/hashes/sha2");
|
|
27
|
+
const object_js_1 = require("./object.js");
|
|
28
|
+
const uint8array_js_1 = require("./uint8array.js");
|
|
23
29
|
exports.DAG_CBOR_MULTICODEC = 0x71; // DRISL conformant DAG-CBOR
|
|
24
30
|
exports.RAW_MULTICODEC = 0x55; // raw binary codec used in DASL CIDs
|
|
25
31
|
exports.SHA256_MULTIHASH = sha2_1.sha256.code;
|
|
32
|
+
exports.SHA512_MULTIHASH = sha2_1.sha512.code;
|
|
33
|
+
function multihashEquals(a, b) {
|
|
34
|
+
return a.code === b.code && (0, uint8array_js_1.ui8Equals)(a.digest, b.digest);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Converts a {@link Cid} to a multiformats {@link CID} instance.
|
|
38
|
+
*
|
|
39
|
+
* @deprecated Packages depending on `@atproto/lex-data` should use the
|
|
40
|
+
* {@link Cid} interface instead of relying on `multiformats`'s {@link CID}
|
|
41
|
+
* implementation directly. This is to avoid compatibility issues, and in order
|
|
42
|
+
* to allow better portability, compatibility and future updates.
|
|
43
|
+
*/
|
|
44
|
+
function asMultiformatsCID(input) {
|
|
45
|
+
const cid =
|
|
46
|
+
// Already a multiformats CID instance
|
|
47
|
+
cid_1.CID.asCID(input) ??
|
|
48
|
+
// Create a new multiformats CID instance
|
|
49
|
+
cid_1.CID.create(input.version, input.code, (0, digest_1.create)(input.multihash.code, input.multihash.digest));
|
|
50
|
+
// @NOTE: the "satisfies" operator is used here to ensure that the Cid
|
|
51
|
+
// interface is indeed compatible with multiformats' CID implementation, which
|
|
52
|
+
// allows us to safely rely on multiformats' CID implementation where Cid are
|
|
53
|
+
// needed.
|
|
54
|
+
return cid;
|
|
55
|
+
}
|
|
26
56
|
function isRawCid(cid) {
|
|
27
57
|
return cid.version === 1 && cid.code === exports.RAW_MULTICODEC;
|
|
28
58
|
}
|
|
@@ -30,54 +60,56 @@ function isDaslCid(cid) {
|
|
|
30
60
|
return (cid.version === 1 &&
|
|
31
61
|
(cid.code === exports.RAW_MULTICODEC || cid.code === exports.DAG_CBOR_MULTICODEC) &&
|
|
32
62
|
cid.multihash.code === exports.SHA256_MULTIHASH &&
|
|
33
|
-
cid.multihash.
|
|
63
|
+
cid.multihash.digest.byteLength === 32 // Should always be 32 bytes (256 bits) for SHA-256, but double-checking anyways
|
|
34
64
|
);
|
|
35
65
|
}
|
|
36
66
|
function isCborCid(cid) {
|
|
37
67
|
return cid.code === exports.DAG_CBOR_MULTICODEC && isDaslCid(cid);
|
|
38
68
|
}
|
|
39
|
-
function
|
|
40
|
-
const cid = cid_1.CID.asCID(value);
|
|
41
|
-
if (!cid) {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
69
|
+
function checkCid(cid, options) {
|
|
44
70
|
switch (options?.flavor) {
|
|
71
|
+
case undefined:
|
|
72
|
+
return true;
|
|
45
73
|
case 'cbor':
|
|
46
|
-
return isCborCid(cid)
|
|
47
|
-
case 'raw':
|
|
48
|
-
return isRawCid(cid) ? cid : null;
|
|
74
|
+
return isCborCid(cid);
|
|
49
75
|
case 'dasl':
|
|
50
|
-
return isDaslCid(cid)
|
|
76
|
+
return isDaslCid(cid);
|
|
77
|
+
case 'raw':
|
|
78
|
+
return isRawCid(cid);
|
|
51
79
|
default:
|
|
52
|
-
|
|
80
|
+
throw new TypeError(`Unknown CID flavor: ${options?.flavor}`);
|
|
53
81
|
}
|
|
54
82
|
}
|
|
55
83
|
function isCid(value, options) {
|
|
56
|
-
return
|
|
84
|
+
return isCidImplementation(value) && checkCid(value, options);
|
|
85
|
+
}
|
|
86
|
+
function ifCid(value, options) {
|
|
87
|
+
if (isCidImplementation(value) && checkCid(value, options))
|
|
88
|
+
return value;
|
|
89
|
+
return null;
|
|
57
90
|
}
|
|
58
91
|
function asCid(value, options) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return cid;
|
|
92
|
+
if (isCidImplementation(value) && checkCid(value, options))
|
|
93
|
+
return value;
|
|
62
94
|
throw new Error('Not a valid CID');
|
|
63
95
|
}
|
|
64
|
-
function parseCid(input, options) {
|
|
65
|
-
const cid = cid_1.CID.parse(input);
|
|
66
|
-
return asCid(cid, options);
|
|
67
|
-
}
|
|
68
96
|
function decodeCid(cidBytes, options) {
|
|
69
97
|
const cid = cid_1.CID.decode(cidBytes);
|
|
70
98
|
return asCid(cid, options);
|
|
71
99
|
}
|
|
100
|
+
function parseCid(input, options) {
|
|
101
|
+
const cid = cid_1.CID.parse(input);
|
|
102
|
+
return asCid(cid, options);
|
|
103
|
+
}
|
|
72
104
|
function validateCidString(input, options) {
|
|
73
|
-
return
|
|
105
|
+
return parseCidSafe(input, options)?.toString() === input;
|
|
74
106
|
}
|
|
75
|
-
function
|
|
107
|
+
function parseCidSafe(input, options) {
|
|
76
108
|
try {
|
|
77
109
|
return parseCid(input, options);
|
|
78
110
|
}
|
|
79
111
|
catch {
|
|
80
|
-
return
|
|
112
|
+
return null;
|
|
81
113
|
}
|
|
82
114
|
}
|
|
83
115
|
function ensureValidCidString(input, options) {
|
|
@@ -93,30 +125,91 @@ function ensureValidCidString(input, options) {
|
|
|
93
125
|
*/
|
|
94
126
|
async function isCidForBytes(cid, bytes) {
|
|
95
127
|
if (cid.multihash.code === sha2_1.sha256.code) {
|
|
96
|
-
const
|
|
97
|
-
return (
|
|
128
|
+
const multihash = await sha2_1.sha256.digest(bytes);
|
|
129
|
+
return multihashEquals(multihash, cid.multihash);
|
|
98
130
|
}
|
|
99
131
|
if (cid.multihash.code === sha2_1.sha512.code) {
|
|
100
|
-
const
|
|
101
|
-
return (
|
|
132
|
+
const multihash = await sha2_1.sha512.digest(bytes);
|
|
133
|
+
return multihashEquals(multihash, cid.multihash);
|
|
102
134
|
}
|
|
103
135
|
// Don't know how to verify other multihash codes
|
|
104
136
|
throw new Error('Unsupported CID multihash');
|
|
105
137
|
}
|
|
138
|
+
function createCid(code, multihashCode, digest) {
|
|
139
|
+
const cid = cid_1.CID.createV1(code, (0, digest_1.create)(multihashCode, digest));
|
|
140
|
+
return cid;
|
|
141
|
+
}
|
|
106
142
|
async function cidForCbor(bytes) {
|
|
107
|
-
const
|
|
108
|
-
return cid_1.CID.createV1(exports.DAG_CBOR_MULTICODEC,
|
|
143
|
+
const multihash = await sha2_1.sha256.digest(bytes);
|
|
144
|
+
return cid_1.CID.createV1(exports.DAG_CBOR_MULTICODEC, multihash);
|
|
109
145
|
}
|
|
110
146
|
async function cidForRawBytes(bytes) {
|
|
111
|
-
const
|
|
112
|
-
return cid_1.CID.createV1(exports.RAW_MULTICODEC,
|
|
147
|
+
const multihash = await sha2_1.sha256.digest(bytes);
|
|
148
|
+
return cid_1.CID.createV1(exports.RAW_MULTICODEC, multihash);
|
|
113
149
|
}
|
|
114
|
-
function cidForRawHash(
|
|
150
|
+
function cidForRawHash(digest) {
|
|
115
151
|
// Fool-proofing
|
|
116
|
-
if (
|
|
117
|
-
throw new Error(`Invalid SHA-256 hash length: ${
|
|
152
|
+
if (digest.length !== 32) {
|
|
153
|
+
throw new Error(`Invalid SHA-256 hash length: ${digest.length}`);
|
|
118
154
|
}
|
|
119
|
-
|
|
120
|
-
|
|
155
|
+
return createCid(exports.RAW_MULTICODEC, sha2_1.sha256.code, digest);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* @internal
|
|
159
|
+
*/
|
|
160
|
+
function isCidImplementation(value) {
|
|
161
|
+
if (cid_1.CID.asCID(value)) {
|
|
162
|
+
// CIDs created using older multiformats versions did not have a "bytes"
|
|
163
|
+
// property.
|
|
164
|
+
return value.bytes != null;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
// Unknown implementation, do a structural check
|
|
168
|
+
try {
|
|
169
|
+
if (!(0, object_js_1.isObject)(value))
|
|
170
|
+
return false;
|
|
171
|
+
const val = value;
|
|
172
|
+
if (val.version !== 0 && val.version !== 1)
|
|
173
|
+
return false;
|
|
174
|
+
if (!isUint8(val.code))
|
|
175
|
+
return false;
|
|
176
|
+
if (!(0, object_js_1.isObject)(val.multihash))
|
|
177
|
+
return false;
|
|
178
|
+
const mh = val.multihash;
|
|
179
|
+
if (!isUint8(mh.code))
|
|
180
|
+
return false;
|
|
181
|
+
if (!(mh.digest instanceof Uint8Array))
|
|
182
|
+
return false;
|
|
183
|
+
// Ensure that the bytes array is consistent with other properties
|
|
184
|
+
if (!(val.bytes instanceof Uint8Array))
|
|
185
|
+
return false;
|
|
186
|
+
if (val.bytes[0] !== val.version)
|
|
187
|
+
return false;
|
|
188
|
+
if (val.bytes[1] !== val.code)
|
|
189
|
+
return false;
|
|
190
|
+
if (val.bytes[2] !== mh.code)
|
|
191
|
+
return false;
|
|
192
|
+
if (val.bytes[3] !== mh.digest.length)
|
|
193
|
+
return false;
|
|
194
|
+
if (val.bytes.length !== 4 + mh.digest.length)
|
|
195
|
+
return false;
|
|
196
|
+
if (!(0, uint8array_js_1.ui8Equals)(val.bytes.subarray(4), mh.digest))
|
|
197
|
+
return false;
|
|
198
|
+
if (typeof val.equals !== 'function')
|
|
199
|
+
return false;
|
|
200
|
+
if (val.equals(val) !== true)
|
|
201
|
+
return false;
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* @internal
|
|
211
|
+
*/
|
|
212
|
+
function isUint8(val) {
|
|
213
|
+
return Number.isInteger(val) && val >= 0 && val < 256;
|
|
121
214
|
}
|
|
122
215
|
//# sourceMappingURL=cid.js.map
|
package/dist/cid.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cid.js","sourceRoot":"","sources":["../src/cid.ts"],"names":[],"mappings":";;;AAyFA,4BAEC;AAYD,8BAOC;AAUD,8BAEC;AAmBD,sBAgBC;AAOD,sBAEC;AAUD,sBAIC;AAYD,4BAGC;AAaD,8BAMC;AAED,8CAKC;AAUD,wCASC;AAED,oDAOC;AAQD,sCAgBC;AAED,gCAGC;AAED,wCAGC;AAED,sCAOC;AApSD,0CAAsC;AAiE7B,oFAjEA,SAAG,OAiEA;AAhEZ,uDAGmC;AACnC,mDAAyD;AAE5C,QAAA,mBAAmB,GAAG,IAAI,CAAA,CAAC,4BAA4B;AAGvD,QAAA,cAAc,GAAG,IAAI,CAAA,CAAC,qCAAqC;AAG3D,QAAA,gBAAgB,GAAG,aAAM,CAAC,IAAI,CAAA;AA4E3C,SAAgB,QAAQ,CAAC,GAAQ;IAC/B,OAAO,GAAG,CAAC,OAAO,KAAK,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,sBAAc,CAAA;AACzD,CAAC;AAYD,SAAgB,SAAS,CAAC,GAAQ;IAChC,OAAO,CACL,GAAG,CAAC,OAAO,KAAK,CAAC;QACjB,CAAC,GAAG,CAAC,IAAI,KAAK,sBAAc,IAAI,GAAG,CAAC,IAAI,KAAK,2BAAmB,CAAC;QACjE,GAAG,CAAC,SAAS,CAAC,IAAI,KAAK,wBAAgB;QACvC,GAAG,CAAC,SAAS,CAAC,IAAI,KAAK,EAAE,CAAC,mDAAmD;KAC9E,CAAA;AACH,CAAC;AAUD,SAAgB,SAAS,CAAC,GAAQ;IAChC,OAAO,GAAG,CAAC,IAAI,KAAK,2BAAmB,IAAI,SAAS,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A;AAC9C,CAAC;AAEM,KAAK,UAAU,UAAU,CAAC,KAAiB;IAChD,MAAM,MAAM,GAAG,MAAM,aAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACzC,OAAO,SAAG,CAAC,QAAQ,CAAC,2BAAmB,EAAE,MAAM,CAAY,CAAA;AAC7D,CAAC;AAEM,KAAK,UAAU,cAAc,CAAC,KAAiB;IACpD,MAAM,MAAM,GAAG,MAAM,aAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACzC,OAAO,SAAG,CAAC,QAAQ,CAAC,sBAAc,EAAE,MAAM,CAAW,CAAA;AACvD,CAAC;AAED,SAAgB,aAAa,CAAC,IAAgB;IAC5C,gBAAgB;IAChB,IAAI,IAAI,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,gCAAgC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAA;IAChE,CAAC;IACD,MAAM,MAAM,GAAG,IAAA,eAAY,EAAC,aAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;IAC9C,OAAO,SAAG,CAAC,QAAQ,CAAC,sBAAc,EAAE,MAAM,CAAW,CAAA;AACvD,CAAC","sourcesContent":["import { CID } from 'multiformats/cid'\nimport {\n create as createDigest,\n equals as digestEquals,\n} from 'multiformats/hashes/digest'\nimport { sha256, sha512 } from 'multiformats/hashes/sha2'\n\nexport const DAG_CBOR_MULTICODEC = 0x71 // DRISL conformant DAG-CBOR\nexport type DAG_CBOR_MULTICODEC = typeof DAG_CBOR_MULTICODEC\n\nexport const RAW_MULTICODEC = 0x55 // raw binary codec used in DASL CIDs\nexport type RAW_MULTICODEC = typeof RAW_MULTICODEC\n\nexport const SHA256_MULTIHASH = sha256.code\nexport type SHA256_MULTIHASH = typeof SHA256_MULTIHASH\n\nexport type MultihashDigest<Code extends number = number> = {\n code: Code\n digest: Uint8Array\n size: number\n bytes: Uint8Array\n}\n\ndeclare module 'multiformats/cid' {\n /**\n * @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and\n * related helpers ({@link isCid}, {@link ifCid}, {@link asCid},\n * {@link parseCid}, {@link decodeCid}), instead.\n *\n * This is marked as deprecated because we want to discourage direct usage of\n * `multiformats/cid` in dependent packages, and instead have them rely on the\n * {@link Cid} interface from `@atproto/lex-data`. The {@link CID} class from\n * `multiformats` version <10 has compatibility issues with certain TypeScript\n * configuration, which can lead to type errors in dependent packages.\n *\n * We are stuck with version 9 because `@atproto` packages did not drop\n * CommonJS support yet, and multiformats version 10 only supports ES modules.\n *\n * In order to avoid compatibility issues, while preparing for future breaking\n * changes (CID in multiformats v10+ has a slightly different interface), as\n * we update or swap out `multiformats`, we provide our own stable {@link Cid}\n * interface.\n */\n interface CID {}\n}\n\n// multiformats' CID class is not very portable because:\n//\n// - In dependent packages that use \"moduleResolution\" set to \"node16\",\n// \"nodenext\" or \"bundler\", TypeScript fails to properly resolve the\n// multiformats package when importing CID from @atproto/lex-data. This causes\n// type errors in those packages. This is caused by the fact that the\n// multiformats version <10 (which is the last version that supports CommonJS)\n// uses \"exports\" field in package.json, which do not contain \"types\"\n// entrypoints.\n// https://www.npmjs.com/package/multiformats/v/9.9.0?activeTab=code\n// - By defining our own interface and helper functions, we can have more\n// control over the public API exposed by this package.\n// - It allow us to have a stable interface in case we need to swap out, or\n// eventually update multiformats (should we choose to drop CommonJS support)\n// in the future.\n\n// @NOTE Even though it is not portable, we still re-export CID here so that\n// dependent packages where it can be used, have access to it (instead of\n// importing directly from \"multiformats\" or\"multiformats/cid\").\nexport { CID }\n\n/**\n * Interface for working with decoded CID string, compatible with\n * {@link CID} implementation.\n */\nexport interface Cid {\n version: 0 | 1\n code: number\n multihash: MultihashDigest\n bytes: Uint8Array\n equals(other: unknown): boolean\n toString(): string\n}\n\n/**\n * Represents the cid of raw binary data (like media blobs).\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}\n */\nexport interface RawCid extends Cid {\n version: 1\n code: RAW_MULTICODEC\n}\n\nexport function isRawCid(cid: Cid): cid is RawCid {\n return cid.version === 1 && cid.code === RAW_MULTICODEC\n}\n\n/**\n * Represents a DASL compliant CID.\n * @see {@link https://dasl.ing/cid.html DASL-CIDs}\n */\nexport interface DaslCid extends Cid {\n version: 1\n code: RAW_MULTICODEC | DAG_CBOR_MULTICODEC\n multihash: MultihashDigest<SHA256_MULTIHASH>\n}\n\nexport function isDaslCid(cid: Cid): cid is DaslCid {\n return (\n cid.version === 1 &&\n (cid.code === RAW_MULTICODEC || cid.code === DAG_CBOR_MULTICODEC) &&\n cid.multihash.code === SHA256_MULTIHASH &&\n cid.multihash.size === 32 // Should always be 32 bytes (256 bits) for SHA-256\n )\n}\n\n/**\n * Represents the cid of ATProto DAG-CBOR data (like repository MST nodes).\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}\n */\nexport interface CborCid extends DaslCid {\n code: DAG_CBOR_MULTICODEC\n}\n\nexport function isCborCid(cid: Cid): cid is CborCid {\n return cid.code === DAG_CBOR_MULTICODEC && isDaslCid(cid)\n}\n\nexport type CidCheckOptions = {\n flavor?: 'raw' | 'cbor' | 'dasl'\n}\nexport type InferCheckedCid<TOptions> = TOptions extends { flavor: 'raw' }\n ? RawCid\n : TOptions extends { flavor: 'cbor' }\n ? CborCid\n : Cid\n\n/**\n * Coerces the input value to a Cid, or returns null if not possible.\n */\nexport function ifCid<TOptions extends CidCheckOptions>(\n value: unknown,\n options: TOptions,\n): InferCheckedCid<TOptions> | null\nexport function ifCid(value: unknown, options?: CidCheckOptions): Cid | null\nexport function ifCid(value: unknown, options?: CidCheckOptions): Cid | null {\n const cid = CID.asCID(value)\n if (!cid) {\n return null\n }\n\n switch (options?.flavor) {\n case 'cbor':\n return isCborCid(cid) ? cid : null\n case 'raw':\n return isRawCid(cid) ? cid : null\n case 'dasl':\n return isDaslCid(cid) ? cid : null\n default:\n return cid\n }\n}\n\nexport function isCid<TOptions extends CidCheckOptions>(\n value: unknown,\n options: TOptions,\n): value is InferCheckedCid<TOptions>\nexport function isCid(value: unknown, options?: CidCheckOptions): value is Cid\nexport function isCid(value: unknown, options?: CidCheckOptions): value is Cid {\n return ifCid(value, options) !== null\n}\n\n/**\n * Coerces the input value to a Cid, or throws if not possible.\n */\nexport function asCid<TOptions extends CidCheckOptions>(\n value: unknown,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function asCid(value: unknown, options?: CidCheckOptions): Cid\nexport function asCid(value: unknown, options?: CidCheckOptions): Cid {\n const cid = ifCid(value, options)\n if (cid) return cid\n throw new Error('Not a valid CID')\n}\n\n/**\n * Parses a CID string into a Cid object.\n *\n * @throws if the input is not a valid CID string.\n */\nexport function parseCid<TOptions extends CidCheckOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function parseCid(input: string, options?: CidCheckOptions): Cid\nexport function parseCid(input: string, options?: CidCheckOptions): Cid {\n const cid = CID.parse(input)\n return asCid(cid, options)\n}\n\n/**\n * Decodes a CID from its binary representation.\n *\n * @see {@link https://dasl.ing/cid.html DASL-CIDs}\n * @throws if the input do not represent a valid DASL {@link Cid}\n */\nexport function decodeCid<TOptions extends CidCheckOptions>(\n cidBytes: Uint8Array,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function decodeCid(cidBytes: Uint8Array, options?: CidCheckOptions): Cid\nexport function decodeCid(\n cidBytes: Uint8Array,\n options?: CidCheckOptions,\n): Cid {\n const cid = CID.decode(cidBytes)\n return asCid(cid, options)\n}\n\nexport function validateCidString(\n input: string,\n options?: CidCheckOptions,\n): boolean {\n return parseCidString(input, options)?.toString() === input\n}\n\nexport function parseCidString<TOptions extends CidCheckOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions> | undefined\nexport function parseCidString(\n input: string,\n options?: CidCheckOptions,\n): Cid | undefined\nexport function parseCidString(\n input: string,\n options?: CidCheckOptions,\n): Cid | undefined {\n try {\n return parseCid(input, options)\n } catch {\n return undefined\n }\n}\n\nexport function ensureValidCidString(\n input: string,\n options?: CidCheckOptions,\n): void {\n if (!validateCidString(input, options)) {\n throw new Error(`Invalid CID string`)\n }\n}\n\n/**\n * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.\n * @params cid The CID to match against the bytes.\n * @params bytes The bytes to verify.\n * @returns true if the CID matches the bytes, false otherwise.\n */\nexport async function isCidForBytes(\n cid: Cid,\n bytes: Uint8Array,\n): Promise<boolean> {\n if (cid.multihash.code === sha256.code) {\n const digest = await sha256.digest(bytes)\n return digestEquals(cid.multihash, digest)\n }\n\n if (cid.multihash.code === sha512.code) {\n const digest = await sha512.digest(bytes)\n return digestEquals(cid.multihash, digest)\n }\n\n // Don't know how to verify other multihash codes\n throw new Error('Unsupported CID multihash')\n}\n\nexport async function cidForCbor(bytes: Uint8Array): Promise<CborCid> {\n const digest = await sha256.digest(bytes)\n return CID.createV1(DAG_CBOR_MULTICODEC, digest) as CborCid\n}\n\nexport async function cidForRawBytes(bytes: Uint8Array): Promise<RawCid> {\n const digest = await sha256.digest(bytes)\n return CID.createV1(RAW_MULTICODEC, digest) as RawCid\n}\n\nexport function cidForRawHash(hash: Uint8Array): RawCid {\n // Fool-proofing\n if (hash.length !== 32) {\n throw new Error(`Invalid SHA-256 hash length: ${hash.length}`)\n }\n const digest = createDigest(sha256.code, hash)\n return CID.createV1(RAW_MULTICODEC, digest) as RawCid\n}\n"]}
|
|
1
|
+
{"version":3,"file":"cid.js","sourceRoot":"","sources":["../src/cid.ts"],"names":[],"mappings":";;;AA8BA,0CAEC;AAsDD,8CAoBC;AAmCD,4BAEC;AAYD,8BAOC;AAQD,8BAEC;AAqBD,4BAaC;AAWD,sBAEC;AAaD,sBAGC;AAeD,sBAGC;AAaD,8BAMC;AAYD,4BAGC;AAED,8CAKC;AAUD,oCASC;AAED,oDAOC;AAQD,sCAgBC;AAED,8BAOC;AAED,gCAGC;AAED,wCAGC;AAED,sCAMC;AArXD,0CAAsC;AA4EV,oF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CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM;gBAAE,OAAO,KAAK,CAAA;YAC3D,IAAI,CAAC,IAAA,yBAAS,EAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC;gBAAE,OAAO,KAAK,CAAA;YAE9D,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,UAAU;gBAAE,OAAO,KAAK,CAAA;YAClD,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI;gBAAE,OAAO,KAAK,CAAA;YAE1C,OAAO,IAAI,CAAA;QACb,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;AACH,CAAC;AAED;;GAEG;AACH,SAAS,OAAO,CAAC,GAAY;IAC3B,OAAO,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,IAAK,GAAc,IAAI,CAAC,IAAK,GAAc,GAAG,GAAG,CAAA;AAC/E,CAAC","sourcesContent":["import { CID } from 'multiformats/cid'\nimport { create as createDigest } from 'multiformats/hashes/digest'\nimport { sha256, sha512 } from 'multiformats/hashes/sha2'\nimport { isObject } from './object.js'\nimport { ui8Equals } from './uint8array.js'\n\nexport const DAG_CBOR_MULTICODEC = 0x71 // DRISL conformant DAG-CBOR\nexport type DAG_CBOR_MULTICODEC = typeof DAG_CBOR_MULTICODEC\n\nexport const RAW_MULTICODEC = 0x55 // raw binary codec used in DASL CIDs\nexport type RAW_MULTICODEC = typeof RAW_MULTICODEC\n\nexport const SHA256_MULTIHASH = sha256.code\nexport type SHA256_MULTIHASH = typeof SHA256_MULTIHASH\n\nexport const SHA512_MULTIHASH = sha512.code\nexport type SHA512_MULTIHASH = typeof SHA512_MULTIHASH\n\nexport interface Multihash<TCode extends number = number> {\n /**\n * Code of the multihash\n */\n code: TCode\n\n /**\n * Raw digest\n */\n digest: Uint8Array\n}\n\nexport function multihashEquals(a: Multihash, b: Multihash): boolean {\n return a.code === b.code && ui8Equals(a.digest, b.digest)\n}\n\ndeclare module 'multiformats/cid' {\n /**\n * @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and\n * related helpers ({@link isCid}, {@link ifCid}, {@link asCid},\n * {@link parseCid}, {@link decodeCid}), instead.\n *\n * This is marked as deprecated because we want to discourage direct usage of\n * `multiformats/cid` in dependent packages, and instead have them rely on the\n * {@link Cid} interface from `@atproto/lex-data`. The {@link CID} class from\n * `multiformats` version <10 has compatibility issues with certain TypeScript\n * configuration, which can lead to type errors in dependent packages.\n *\n * We are stuck with version 9 because `@atproto` packages did not drop\n * CommonJS support yet, and multiformats version 10 only supports ES modules.\n *\n * In order to avoid compatibility issues, while preparing for future breaking\n * changes (CID in multiformats v10+ has a slightly different interface), as\n * we update or swap out `multiformats`, `@atproto/lex-data` provides its own\n * stable {@link Cid} interface.\n */\n interface CID {}\n}\n\n// multiformats' CID class is not very portable because:\n//\n// - In dependent packages that use \"moduleResolution\" set to \"node16\",\n// \"nodenext\" or \"bundler\", TypeScript fails to properly resolve the\n// multiformats package when importing CID from @atproto/lex-data. This causes\n// type errors in those packages. This is caused by the fact that the\n// multiformats version <10 (which is the last version that supports CommonJS)\n// uses \"exports\" field in package.json, which do not contain \"types\"\n// entrypoints.\n// https://www.npmjs.com/package/multiformats/v/9.9.0?activeTab=code\n// - By defining our own interface and helper functions, we can have more\n// control over the public API exposed by this package.\n// - It allow us to have a stable interface in case we need to swap out, or\n// eventually update multiformats (should we choose to drop CommonJS support)\n// in the future.\n\n// @NOTE Even though it is not portable, we still re-export CID here so that\n// dependent packages where it can be used, have access to it (instead of\n// importing directly from \"multiformats\" or \"multiformats/cid\").\nexport { /** @deprecated */ CID }\n\n/**\n * Converts a {@link Cid} to a multiformats {@link CID} instance.\n *\n * @deprecated Packages depending on `@atproto/lex-data` should use the\n * {@link Cid} interface instead of relying on `multiformats`'s {@link CID}\n * implementation directly. This is to avoid compatibility issues, and in order\n * to allow better portability, compatibility and future updates.\n */\nexport function asMultiformatsCID<\n TVersion extends 0 | 1 = 0 | 1,\n TCode extends number = number,\n TMultihashCode extends number = number,\n>(input: Cid<TVersion, TCode, TMultihashCode>) {\n const cid =\n // Already a multiformats CID instance\n CID.asCID(input) ??\n // Create a new multiformats CID instance\n CID.create(\n input.version,\n input.code,\n createDigest(input.multihash.code, input.multihash.digest),\n )\n\n // @NOTE: the \"satisfies\" operator is used here to ensure that the Cid\n // interface is indeed compatible with multiformats' CID implementation, which\n // allows us to safely rely on multiformats' CID implementation where Cid are\n // needed.\n return cid satisfies Cid as CID & Cid<TVersion, TCode, TMultihashCode>\n}\n\n/**\n * Interface for working with CIDs\n */\nexport interface Cid<\n TVersion extends 0 | 1 = 0 | 1,\n TCode extends number = number,\n TMultihashCode extends number = number,\n> {\n // @NOTE This interface is compatible with multiformats' CID implementation\n // which we are using under the hood.\n\n readonly version: TVersion\n readonly code: TCode\n readonly multihash: Multihash<TMultihashCode>\n\n /**\n * Binary representation of the whole CID.\n */\n readonly bytes: Uint8Array\n\n equals(other: Cid): boolean\n toString(): string\n}\n\n/**\n * Represents the cid of raw binary data (like media blobs).\n *\n * The use of {@link SHA256_MULTIHASH} is recommended but not required for raw CIDs.\n *\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}\n */\nexport type RawCid = Cid<1, RAW_MULTICODEC>\n\nexport function isRawCid(cid: Cid): cid is RawCid {\n return cid.version === 1 && cid.code === RAW_MULTICODEC\n}\n\n/**\n * Represents a DASL compliant CID.\n * @see {@link https://dasl.ing/cid.html DASL-CIDs}\n */\nexport type DaslCid = Cid<\n 1,\n RAW_MULTICODEC | DAG_CBOR_MULTICODEC,\n SHA256_MULTIHASH\n>\n\nexport function isDaslCid(cid: Cid): cid is DaslCid {\n return (\n cid.version === 1 &&\n (cid.code === RAW_MULTICODEC || cid.code === DAG_CBOR_MULTICODEC) &&\n cid.multihash.code === SHA256_MULTIHASH &&\n cid.multihash.digest.byteLength === 32 // Should always be 32 bytes (256 bits) for SHA-256, but double-checking anyways\n )\n}\n\n/**\n * Represents the cid of ATProto DAG-CBOR data (like repository MST nodes).\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}\n */\nexport type CborCid = Cid<1, DAG_CBOR_MULTICODEC, SHA256_MULTIHASH>\n\nexport function isCborCid(cid: Cid): cid is CborCid {\n return cid.code === DAG_CBOR_MULTICODEC && isDaslCid(cid)\n}\n\nexport type CheckCidOptions = {\n flavor?: 'raw' | 'cbor' | 'dasl'\n}\n\nexport type InferCheckedCid<TOptions> = TOptions extends { flavor: 'raw' }\n ? RawCid\n : TOptions extends { flavor: 'cbor' }\n ? CborCid\n : Cid\n\n/**\n * Type guard to check whether a {@link Cid} instance meets specific flavor\n * constraints.\n */\nexport function checkCid<TOptions extends CheckCidOptions>(\n cid: Cid,\n options: TOptions,\n): cid is InferCheckedCid<TOptions>\nexport function checkCid(cid: Cid, options?: CheckCidOptions): boolean\nexport function checkCid(cid: Cid, options?: CheckCidOptions): boolean {\n switch (options?.flavor) {\n case undefined:\n return true\n case 'cbor':\n return isCborCid(cid)\n case 'dasl':\n return isDaslCid(cid)\n case 'raw':\n return isRawCid(cid)\n default:\n throw new TypeError(`Unknown CID flavor: ${options?.flavor}`)\n }\n}\n\n/**\n * Type guard to check whether a value is a valid {@link Cid} instance,\n * optionally checking for specific flavor constraints.\n */\nexport function isCid<TOptions extends CheckCidOptions>(\n value: unknown,\n options: TOptions,\n): value is InferCheckedCid<TOptions>\nexport function isCid(value: unknown, options?: CheckCidOptions): value is Cid\nexport function isCid(value: unknown, options?: CheckCidOptions): value is Cid {\n return isCidImplementation(value) && checkCid(value, options)\n}\n\n/**\n * Returns the input value as a {@link Cid} if it is valid, or `null` otherwise.\n */\nexport function ifCid<TValue, TOptions extends CheckCidOptions>(\n value: unknown,\n options: TOptions,\n): (TValue & InferCheckedCid<TOptions>) | null\nexport function ifCid<TValue>(\n value: TValue,\n options?: CheckCidOptions,\n): (TValue & Cid) | null\nexport function ifCid(value: unknown, options?: CheckCidOptions): Cid | null {\n if (isCidImplementation(value) && checkCid(value, options)) return value\n return null\n}\n\n/**\n * Returns the input value as a {@link Cid} if it is valid.\n *\n * @throws if the input is not a valid {@link Cid}.\n */\nexport function asCid<TValue, TOptions extends CheckCidOptions>(\n value: TValue,\n options: TOptions,\n): TValue & InferCheckedCid<TOptions>\nexport function asCid<TValue>(\n value: TValue,\n options?: CheckCidOptions,\n): Cid & TValue\nexport function asCid(value: unknown, options?: CheckCidOptions): Cid {\n if (isCidImplementation(value) && checkCid(value, options)) return value\n throw new Error('Not a valid CID')\n}\n\n/**\n * Decodes a CID from its binary representation.\n *\n * @see {@link https://dasl.ing/cid.html DASL-CIDs}\n * @throws if the input do not represent a valid DASL {@link Cid}\n */\nexport function decodeCid<TOptions extends CheckCidOptions>(\n cidBytes: Uint8Array,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function decodeCid(cidBytes: Uint8Array, options?: CheckCidOptions): Cid\nexport function decodeCid(\n cidBytes: Uint8Array,\n options?: CheckCidOptions,\n): Cid {\n const cid = CID.decode(cidBytes)\n return asCid(cid, options)\n}\n\n/**\n * Parses a CID string into a Cid object.\n *\n * @throws if the input is not a valid CID string.\n */\nexport function parseCid<TOptions extends CheckCidOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function parseCid(input: string, options?: CheckCidOptions): Cid\nexport function parseCid(input: string, options?: CheckCidOptions): Cid {\n const cid = CID.parse(input)\n return asCid(cid, options)\n}\n\nexport function validateCidString(\n input: string,\n options?: CheckCidOptions,\n): boolean {\n return parseCidSafe(input, options)?.toString() === input\n}\n\nexport function parseCidSafe<TOptions extends CheckCidOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions> | null\nexport function parseCidSafe(\n input: string,\n options?: CheckCidOptions,\n): Cid | null\nexport function parseCidSafe(\n input: string,\n options?: CheckCidOptions,\n): Cid | null {\n try {\n return parseCid(input, options)\n } catch {\n return null\n }\n}\n\nexport function ensureValidCidString(\n input: string,\n options?: CheckCidOptions,\n): void {\n if (!validateCidString(input, options)) {\n throw new Error(`Invalid CID string`)\n }\n}\n\n/**\n * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.\n * @params cid The CID to match against the bytes.\n * @params bytes The bytes to verify.\n * @returns true if the CID matches the bytes, false otherwise.\n */\nexport async function isCidForBytes(\n cid: Cid,\n bytes: Uint8Array,\n): Promise<boolean> {\n if (cid.multihash.code === sha256.code) {\n const multihash = await sha256.digest(bytes)\n return multihashEquals(multihash, cid.multihash)\n }\n\n if (cid.multihash.code === sha512.code) {\n const multihash = await sha512.digest(bytes)\n return multihashEquals(multihash, cid.multihash)\n }\n\n // Don't know how to verify other multihash codes\n throw new Error('Unsupported CID multihash')\n}\n\nexport function createCid<TCode extends number, TMultihashCode extends number>(\n code: TCode,\n multihashCode: TMultihashCode,\n digest: Uint8Array,\n) {\n const cid: Cid = CID.createV1(code, createDigest(multihashCode, digest))\n return cid as Cid<1, TCode, TMultihashCode>\n}\n\nexport async function cidForCbor(bytes: Uint8Array): Promise<CborCid> {\n const multihash = await sha256.digest(bytes)\n return CID.createV1(DAG_CBOR_MULTICODEC, multihash) as CborCid\n}\n\nexport async function cidForRawBytes(bytes: Uint8Array): Promise<RawCid> {\n const multihash = await sha256.digest(bytes)\n return CID.createV1(RAW_MULTICODEC, multihash) as RawCid\n}\n\nexport function cidForRawHash(digest: Uint8Array): RawCid {\n // Fool-proofing\n if (digest.length !== 32) {\n throw new Error(`Invalid SHA-256 hash length: ${digest.length}`)\n }\n return createCid(RAW_MULTICODEC, sha256.code, digest)\n}\n\n/**\n * @internal\n */\nfunction isCidImplementation(value: unknown): value is Cid {\n if (CID.asCID(value)) {\n // CIDs created using older multiformats versions did not have a \"bytes\"\n // property.\n return (value as { bytes?: Uint8Array }).bytes != null\n } else {\n // Unknown implementation, do a structural check\n try {\n if (!isObject(value)) return false\n\n const val = value as Record<string, unknown>\n if (val.version !== 0 && val.version !== 1) return false\n if (!isUint8(val.code)) return false\n\n if (!isObject(val.multihash)) return false\n const mh = val.multihash as Record<string, unknown>\n if (!isUint8(mh.code)) return false\n if (!(mh.digest instanceof Uint8Array)) return false\n\n // Ensure that the bytes array is consistent with other properties\n if (!(val.bytes instanceof Uint8Array)) return false\n if (val.bytes[0] !== val.version) return false\n if (val.bytes[1] !== val.code) return false\n if (val.bytes[2] !== mh.code) return false\n if (val.bytes[3] !== mh.digest.length) return false\n if (val.bytes.length !== 4 + mh.digest.length) return false\n if (!ui8Equals(val.bytes.subarray(4), mh.digest)) return false\n\n if (typeof val.equals !== 'function') return false\n if (val.equals(val) !== true) return false\n\n return true\n } catch {\n return false\n }\n }\n}\n\n/**\n * @internal\n */\nfunction isUint8(val: unknown): val is number {\n return Number.isInteger(val) && (val as number) >= 0 && (val as number) < 256\n}\n"]}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./blob.js"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./cid.js"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./language.js"), exports);
|
|
7
6
|
tslib_1.__exportStar(require("./lex-equals.js"), exports);
|
|
8
7
|
tslib_1.__exportStar(require("./lex-error.js"), exports);
|
|
9
8
|
tslib_1.__exportStar(require("./lex.js"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,oDAAyB;AACzB,mDAAwB;AACxB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,oDAAyB;AACzB,mDAAwB;AACxB,0DAA+B;AAC/B,yDAA8B;AAC9B,mDAAwB;AACxB,sDAA2B;AAC3B,0DAA+B;AAC/B,oDAAyB","sourcesContent":["export * from './blob.js'\nexport * from './cid.js'\nexport * from './lex-equals.js'\nexport * from './lex-error.js'\nexport * from './lex.js'\nexport * from './object.js'\nexport * from './uint8array.js'\nexport * from './utf8.js'\n"]}
|
package/dist/uint8array.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export declare const toBase64: (bytes: Uint8Array, alphabet?: Base64Alphabet) =>
|
|
|
14
14
|
* @throws If the input is not a valid base64 string
|
|
15
15
|
*/
|
|
16
16
|
export declare const fromBase64: (b64: string, alphabet?: Base64Alphabet) => Uint8Array;
|
|
17
|
+
export declare function ifUint8Array(input: unknown): Uint8Array | undefined;
|
|
17
18
|
/**
|
|
18
19
|
* Coerces various binary data representations into a Uint8Array.
|
|
19
20
|
*
|
package/dist/uint8array.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uint8array.d.ts","sourceRoot":"","sources":["../src/uint8array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAavD,YAAY,EAAE,cAAc,EAAE,CAAA;AAO9B;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,CACrB,KAAK,EAAE,UAAU,EACjB,QAAQ,CAAC,EAAE,cAAc,KACtB,
|
|
1
|
+
{"version":3,"file":"uint8array.d.ts","sourceRoot":"","sources":["../src/uint8array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAavD,YAAY,EAAE,cAAc,EAAE,CAAA;AAO9B;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,CACrB,KAAK,EAAE,UAAU,EACjB,QAAQ,CAAC,EAAE,cAAc,KACtB,MAGa,CAAA;AAElB;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,EAAE,CACvB,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,cAAc,KACtB,UAGe,CAAA;AAUpB,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CAMnE;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CAkBnE;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAY/D;AAED,eAAO,MAAM,SAAS,8CACgD,CAAA"}
|