@atproto/common 0.5.6 → 0.5.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/ipld.d.ts +12 -15
- package/dist/ipld.d.ts.map +1 -1
- package/dist/ipld.js +25 -12
- package/dist/ipld.js.map +1 -1
- package/package.json +4 -4
- package/src/ipld.ts +33 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @atproto/common
|
|
2
2
|
|
|
3
|
+
## 0.5.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
|
+
- @atproto/common-web@0.4.12
|
|
10
|
+
- @atproto/lex-cbor@0.0.8
|
|
11
|
+
|
|
12
|
+
## 0.5.7
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- 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), [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe), [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe)]:
|
|
17
|
+
- @atproto/lex-data@0.0.7
|
|
18
|
+
- @atproto/lex-cbor@0.0.7
|
|
19
|
+
- @atproto/common-web@0.4.11
|
|
20
|
+
|
|
3
21
|
## 0.5.6
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/ipld.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Transform } from 'node:stream';
|
|
2
2
|
import { Block, ByteView } from 'multiformats/block';
|
|
3
|
-
import {
|
|
4
|
-
import { CID } from '@atproto/lex-data';
|
|
3
|
+
import { type CID, Cid } from '@atproto/lex-data';
|
|
5
4
|
/**
|
|
6
5
|
* @deprecated Use {@link encode} from `@atproto/lex-cbor` instead.
|
|
7
6
|
*/
|
|
@@ -19,7 +18,7 @@ export declare function dataToCborBlock<T>(value: T): Promise<Block<T>>;
|
|
|
19
18
|
/**
|
|
20
19
|
* @deprecated Use {@link cidForLex} from `@atproto/lex-cbor` instead.
|
|
21
20
|
*/
|
|
22
|
-
|
|
21
|
+
declare function cidForCborLegacy(data: unknown): Promise<CID>;
|
|
23
22
|
export { cidForCborLegacy as cidForCbor };
|
|
24
23
|
/**
|
|
25
24
|
* @deprecated Use {@link validateCidString} from '@atproto/lex-data' instead.
|
|
@@ -30,26 +29,24 @@ export declare function isValidCid(cidStr: string): Promise<boolean>;
|
|
|
30
29
|
*/
|
|
31
30
|
export declare function cborBytesToRecord(bytes: Uint8Array): Record<string, unknown>;
|
|
32
31
|
/**
|
|
33
|
-
* @deprecated Use {@link
|
|
32
|
+
* @deprecated Use {@link isCidForBytes} from `@atproto/lex-cbor` instead.
|
|
34
33
|
*/
|
|
35
|
-
declare
|
|
36
|
-
export { verifyCidForBytesLegacy as verifyCidForBytes };
|
|
34
|
+
export declare function verifyCidForBytes(cid: Cid, bytes: Uint8Array): Promise<void>;
|
|
37
35
|
/**
|
|
38
36
|
* @deprecated Use {@link cidForRawHash} from `@atproto/lex-cbor` instead.
|
|
39
37
|
*/
|
|
40
|
-
export declare
|
|
38
|
+
export declare function sha256RawToCid(hash: Uint8Array): CID;
|
|
41
39
|
/**
|
|
42
|
-
* @deprecated Use {@link
|
|
40
|
+
* @deprecated Use {@link decodeCid} from `@atproto/lex-cbor` instead.
|
|
43
41
|
*/
|
|
44
|
-
declare
|
|
45
|
-
export { parseCidFromBytesLegacy as parseCidFromBytes };
|
|
42
|
+
export declare function parseCidFromBytes(bytes: Uint8Array): CID;
|
|
46
43
|
export declare class VerifyCidTransform extends Transform {
|
|
47
|
-
cid:
|
|
48
|
-
constructor(cid:
|
|
44
|
+
cid: Cid;
|
|
45
|
+
constructor(cid: Cid);
|
|
49
46
|
}
|
|
50
47
|
export declare class VerifyCidError extends Error {
|
|
51
|
-
expected:
|
|
52
|
-
actual:
|
|
53
|
-
constructor(expected:
|
|
48
|
+
expected: Cid;
|
|
49
|
+
actual: Cid;
|
|
50
|
+
constructor(expected: Cid, actual: Cid);
|
|
54
51
|
}
|
|
55
52
|
//# sourceMappingURL=ipld.d.ts.map
|
package/dist/ipld.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipld.d.ts","sourceRoot":"","sources":["../src/ipld.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ipld.d.ts","sourceRoot":"","sources":["../src/ipld.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAyB,MAAM,oBAAoB,CAAA;AAG3E,OAAO,EACL,KAAK,GAAG,EACR,GAAG,EAWJ,MAAM,mBAAmB,CAAA;AAE1B;;GAEG;AACH,QAAA,MAAM,gBAAgB,EAAa,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAA;AACxE,OAAO,EAAE,gBAAgB,IAAI,UAAU,EAAE,CAAA;AAEzC;;GAEG;AACH,QAAA,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
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.VerifyCidError = exports.VerifyCidTransform = exports.
|
|
4
|
+
exports.VerifyCidError = exports.VerifyCidTransform = exports.cborDecode = exports.cborEncode = void 0;
|
|
4
5
|
exports.dataToCborBlock = dataToCborBlock;
|
|
6
|
+
exports.cidForCbor = cidForCborLegacy;
|
|
5
7
|
exports.isValidCid = isValidCid;
|
|
6
8
|
exports.cborBytesToRecord = cborBytesToRecord;
|
|
9
|
+
exports.verifyCidForBytes = verifyCidForBytes;
|
|
10
|
+
exports.sha256RawToCid = sha256RawToCid;
|
|
11
|
+
exports.parseCidFromBytes = parseCidFromBytes;
|
|
7
12
|
const node_crypto_1 = require("node:crypto");
|
|
8
13
|
const node_stream_1 = require("node:stream");
|
|
9
14
|
const block_1 = require("multiformats/block");
|
|
15
|
+
const sha2_1 = require("multiformats/hashes/sha2");
|
|
10
16
|
const lex_cbor_1 = require("@atproto/lex-cbor");
|
|
11
17
|
const lex_data_1 = require("@atproto/lex-data");
|
|
12
18
|
/**
|
|
@@ -30,14 +36,15 @@ async function dataToCborBlock(value) {
|
|
|
30
36
|
code: lex_data_1.DAG_CBOR_MULTICODEC,
|
|
31
37
|
encode: lex_cbor_1.encode,
|
|
32
38
|
},
|
|
33
|
-
hasher:
|
|
39
|
+
hasher: sha2_1.sha256,
|
|
34
40
|
});
|
|
35
41
|
}
|
|
36
42
|
/**
|
|
37
43
|
* @deprecated Use {@link cidForLex} from `@atproto/lex-cbor` instead.
|
|
38
44
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
async function cidForCborLegacy(data) {
|
|
46
|
+
return (0, lex_data_1.asMultiformatsCID)(await (0, lex_cbor_1.cidForLex)(data));
|
|
47
|
+
}
|
|
41
48
|
/**
|
|
42
49
|
* @deprecated Use {@link validateCidString} from '@atproto/lex-data' instead.
|
|
43
50
|
*/
|
|
@@ -56,19 +63,25 @@ function cborBytesToRecord(bytes) {
|
|
|
56
63
|
throw new Error(`Expected record with $type property`);
|
|
57
64
|
}
|
|
58
65
|
/**
|
|
59
|
-
* @deprecated Use {@link
|
|
66
|
+
* @deprecated Use {@link isCidForBytes} from `@atproto/lex-cbor` instead.
|
|
60
67
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
68
|
+
async function verifyCidForBytes(cid, bytes) {
|
|
69
|
+
if (!(await (0, lex_data_1.isCidForBytes)(cid, bytes))) {
|
|
70
|
+
throw new Error(`Not a valid CID for bytes (${cid.toString()})`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
63
73
|
/**
|
|
64
74
|
* @deprecated Use {@link cidForRawHash} from `@atproto/lex-cbor` instead.
|
|
65
75
|
*/
|
|
66
|
-
|
|
76
|
+
function sha256RawToCid(hash) {
|
|
77
|
+
return (0, lex_data_1.asMultiformatsCID)((0, lex_data_1.cidForRawHash)(hash));
|
|
78
|
+
}
|
|
67
79
|
/**
|
|
68
|
-
* @deprecated Use {@link
|
|
80
|
+
* @deprecated Use {@link decodeCid} from `@atproto/lex-cbor` instead.
|
|
69
81
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
82
|
+
function parseCidFromBytes(bytes) {
|
|
83
|
+
return (0, lex_data_1.asMultiformatsCID)((0, lex_data_1.decodeCid)(bytes, { flavor: 'dasl' }));
|
|
84
|
+
}
|
|
72
85
|
class VerifyCidTransform extends node_stream_1.Transform {
|
|
73
86
|
constructor(cid) {
|
|
74
87
|
const hasher = (0, node_crypto_1.createHash)('sha256');
|
|
@@ -79,7 +92,7 @@ class VerifyCidTransform extends node_stream_1.Transform {
|
|
|
79
92
|
},
|
|
80
93
|
flush(callback) {
|
|
81
94
|
try {
|
|
82
|
-
const actual =
|
|
95
|
+
const actual = sha256RawToCid(hasher.digest());
|
|
83
96
|
if (actual.equals(cid)) {
|
|
84
97
|
return callback();
|
|
85
98
|
}
|
package/dist/ipld.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipld.js","sourceRoot":"","sources":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CAAC;oBACb,OAAO,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;gBAC/B,CAAC;YACH,CAAC;SACF,CAAC,CAAA;QAnBQ;;;;mBAAO,GAAG;WAAK;IAoB3B,CAAC;CACF;AAtBD,gDAsBC;AAED,MAAM,OAAO,GAAG,CAAC,GAAY,EAAS,EAAE,CACtC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,kBAAkB,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAA;AAE5E,MAAa,cAAe,SAAQ,KAAK;IACvC,YACS,QAAa,EACb,MAAW;QAElB,KAAK,CAAC,eAAe,CAAC,CAAA;QAHtB;;;;mBAAO,QAAQ;WAAK;QACpB;;;;mBAAO,MAAM;WAAK;IAGpB,CAAC;CACF;AAPD,wCAOC","sourcesContent":["/* eslint-disable import/no-deprecated */\n\nimport { createHash } from 'node:crypto'\nimport { Transform } from 'node:stream'\nimport { Block, ByteView, encode as encodeBlock } from 'multiformats/block'\nimport { sha256 as hasher } from 'multiformats/hashes/sha2'\nimport { cidForLex, decode, encode } from '@atproto/lex-cbor'\nimport {\n type CID,\n Cid,\n DAG_CBOR_MULTICODEC,\n LexValue,\n asMultiformatsCID,\n // eslint-disable-next-line\n cidForCbor,\n cidForRawHash,\n decodeCid,\n isCidForBytes,\n isTypedLexMap,\n validateCidString,\n} from '@atproto/lex-data'\n\n/**\n * @deprecated Use {@link encode} from `@atproto/lex-cbor` instead.\n */\nconst cborEncodeLegacy = encode as <T = unknown>(data: T) => ByteView<T>\nexport { cborEncodeLegacy as cborEncode }\n\n/**\n * @deprecated Use {@link decode} from `@atproto/lex-cbor` instead.\n */\nconst cborDecodeLegacy = decode as <T = unknown>(bytes: ByteView<T>) => T\nexport { cborDecodeLegacy as cborDecode }\n\n/**\n * @deprecated Use {@link encode} and {@link cidForCbor} from `@atproto/lex-cbor` instead.\n */\nexport async function dataToCborBlock<T>(value: T): Promise<Block<T>> {\n return encodeBlock<T, 0x71, 0x12>({\n value,\n codec: {\n name: 'at-cbor', // Not actually used\n code: DAG_CBOR_MULTICODEC,\n encode: encode as (data: T) => ByteView<T>,\n },\n hasher,\n })\n}\n\n/**\n * @deprecated Use {@link cidForLex} from `@atproto/lex-cbor` instead.\n */\nasync function cidForCborLegacy(data: unknown): Promise<CID> {\n return asMultiformatsCID(await cidForLex(data as LexValue))\n}\nexport { cidForCborLegacy as cidForCbor }\n\n/**\n * @deprecated Use {@link validateCidString} from '@atproto/lex-data' instead.\n */\nexport async function isValidCid(cidStr: string): Promise<boolean> {\n // @NOTE we keep the wrapper function to return a Promise (for backward\n // compatibility).\n return validateCidString(cidStr)\n}\n\n/**\n * @deprecated Use {@link decode} from `@atproto/lex-cbor`, and {@link isTypedLexMap} from `@atproto/lex-data` instead.\n */\nexport function cborBytesToRecord(bytes: Uint8Array): Record<string, unknown> {\n const data = decode(bytes) as LexValue\n if (isTypedLexMap(data)) return data\n\n throw new Error(`Expected record with $type property`)\n}\n\n/**\n * @deprecated Use {@link isCidForBytes} from `@atproto/lex-cbor` instead.\n */\nexport async function verifyCidForBytes(\n cid: Cid,\n bytes: Uint8Array,\n): Promise<void> {\n if (!(await isCidForBytes(cid, bytes))) {\n throw new Error(`Not a valid CID for bytes (${cid.toString()})`)\n }\n}\n\n/**\n * @deprecated Use {@link cidForRawHash} from `@atproto/lex-cbor` instead.\n */\nexport function sha256RawToCid(hash: Uint8Array): CID {\n return asMultiformatsCID(cidForRawHash(hash))\n}\n\n/**\n * @deprecated Use {@link decodeCid} from `@atproto/lex-cbor` instead.\n */\nexport function parseCidFromBytes(bytes: Uint8Array): CID {\n return asMultiformatsCID(decodeCid(bytes, { flavor: 'dasl' }))\n}\n\nexport class VerifyCidTransform extends Transform {\n constructor(public cid: Cid) {\n const hasher = createHash('sha256')\n super({\n transform(chunk, encoding, callback) {\n hasher.update(chunk)\n callback(null, chunk)\n },\n flush(callback) {\n try {\n const actual = sha256RawToCid(hasher.digest())\n if (actual.equals(cid)) {\n return callback()\n } else {\n return callback(new VerifyCidError(cid, actual))\n }\n } catch (err) {\n return callback(asError(err))\n }\n },\n })\n }\n}\n\nconst asError = (err: unknown): Error =>\n err instanceof Error ? err : new Error('Unexpected error', { cause: err })\n\nexport class VerifyCidError extends Error {\n constructor(\n public expected: Cid,\n public actual: Cid,\n ) {\n super('Bad cid check')\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/common",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.8",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Shared web-platform-friendly code for atproto libraries",
|
|
6
6
|
"keywords": [
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"iso-datestring-validator": "^2.2.2",
|
|
22
22
|
"multiformats": "^9.9.0",
|
|
23
23
|
"pino": "^8.21.0",
|
|
24
|
-
"@atproto/common-web": "^0.4.
|
|
25
|
-
"@atproto/lex-cbor": "0.0.
|
|
26
|
-
"@atproto/lex-data": "0.0.
|
|
24
|
+
"@atproto/common-web": "^0.4.12",
|
|
25
|
+
"@atproto/lex-cbor": "0.0.8",
|
|
26
|
+
"@atproto/lex-data": "0.0.8"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"jest": "^28.1.2",
|
package/src/ipld.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
+
/* eslint-disable import/no-deprecated */
|
|
2
|
+
|
|
1
3
|
import { createHash } from 'node:crypto'
|
|
2
4
|
import { Transform } from 'node:stream'
|
|
3
5
|
import { Block, ByteView, encode as encodeBlock } from 'multiformats/block'
|
|
6
|
+
import { sha256 as hasher } from 'multiformats/hashes/sha2'
|
|
7
|
+
import { cidForLex, decode, encode } from '@atproto/lex-cbor'
|
|
4
8
|
import {
|
|
9
|
+
type CID,
|
|
10
|
+
Cid,
|
|
11
|
+
DAG_CBOR_MULTICODEC,
|
|
12
|
+
LexValue,
|
|
13
|
+
asMultiformatsCID,
|
|
5
14
|
// eslint-disable-next-line
|
|
6
15
|
cidForCbor,
|
|
7
|
-
cidForLex,
|
|
8
16
|
cidForRawHash,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
hasher,
|
|
12
|
-
parseCidFromBytes,
|
|
13
|
-
verifyCidForBytes,
|
|
14
|
-
} from '@atproto/lex-cbor'
|
|
15
|
-
import {
|
|
16
|
-
CID,
|
|
17
|
-
DAG_CBOR_MULTICODEC,
|
|
18
|
-
LexValue,
|
|
17
|
+
decodeCid,
|
|
18
|
+
isCidForBytes,
|
|
19
19
|
isTypedLexMap,
|
|
20
20
|
validateCidString,
|
|
21
21
|
} from '@atproto/lex-data'
|
|
@@ -50,7 +50,9 @@ export async function dataToCborBlock<T>(value: T): Promise<Block<T>> {
|
|
|
50
50
|
/**
|
|
51
51
|
* @deprecated Use {@link cidForLex} from `@atproto/lex-cbor` instead.
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
async function cidForCborLegacy(data: unknown): Promise<CID> {
|
|
54
|
+
return asMultiformatsCID(await cidForLex(data as LexValue))
|
|
55
|
+
}
|
|
54
56
|
export { cidForCborLegacy as cidForCbor }
|
|
55
57
|
|
|
56
58
|
/**
|
|
@@ -73,24 +75,33 @@ export function cborBytesToRecord(bytes: Uint8Array): Record<string, unknown> {
|
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
/**
|
|
76
|
-
* @deprecated Use {@link
|
|
78
|
+
* @deprecated Use {@link isCidForBytes} from `@atproto/lex-cbor` instead.
|
|
77
79
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
export async function verifyCidForBytes(
|
|
81
|
+
cid: Cid,
|
|
82
|
+
bytes: Uint8Array,
|
|
83
|
+
): Promise<void> {
|
|
84
|
+
if (!(await isCidForBytes(cid, bytes))) {
|
|
85
|
+
throw new Error(`Not a valid CID for bytes (${cid.toString()})`)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
80
88
|
|
|
81
89
|
/**
|
|
82
90
|
* @deprecated Use {@link cidForRawHash} from `@atproto/lex-cbor` instead.
|
|
83
91
|
*/
|
|
84
|
-
export
|
|
92
|
+
export function sha256RawToCid(hash: Uint8Array): CID {
|
|
93
|
+
return asMultiformatsCID(cidForRawHash(hash))
|
|
94
|
+
}
|
|
85
95
|
|
|
86
96
|
/**
|
|
87
|
-
* @deprecated Use {@link
|
|
97
|
+
* @deprecated Use {@link decodeCid} from `@atproto/lex-cbor` instead.
|
|
88
98
|
*/
|
|
89
|
-
|
|
90
|
-
|
|
99
|
+
export function parseCidFromBytes(bytes: Uint8Array): CID {
|
|
100
|
+
return asMultiformatsCID(decodeCid(bytes, { flavor: 'dasl' }))
|
|
101
|
+
}
|
|
91
102
|
|
|
92
103
|
export class VerifyCidTransform extends Transform {
|
|
93
|
-
constructor(public cid:
|
|
104
|
+
constructor(public cid: Cid) {
|
|
94
105
|
const hasher = createHash('sha256')
|
|
95
106
|
super({
|
|
96
107
|
transform(chunk, encoding, callback) {
|
|
@@ -118,8 +129,8 @@ const asError = (err: unknown): Error =>
|
|
|
118
129
|
|
|
119
130
|
export class VerifyCidError extends Error {
|
|
120
131
|
constructor(
|
|
121
|
-
public expected:
|
|
122
|
-
public actual:
|
|
132
|
+
public expected: Cid,
|
|
133
|
+
public actual: Cid,
|
|
123
134
|
) {
|
|
124
135
|
super('Bad cid check')
|
|
125
136
|
}
|