@atproto/common 0.5.5 → 0.5.7
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 +10 -13
- package/dist/ipld.d.ts.map +1 -1
- package/dist/ipld.js +19 -9
- package/dist/ipld.js.map +1 -1
- package/package.json +4 -4
- package/src/ipld.ts +26 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @atproto/common
|
|
2
2
|
|
|
3
|
+
## 0.5.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 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)]:
|
|
8
|
+
- @atproto/lex-data@0.0.7
|
|
9
|
+
- @atproto/lex-cbor@0.0.7
|
|
10
|
+
- @atproto/common-web@0.4.11
|
|
11
|
+
|
|
12
|
+
## 0.5.6
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [[`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98), [`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98), [`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98)]:
|
|
17
|
+
- @atproto/lex-data@0.0.6
|
|
18
|
+
- @atproto/common-web@0.4.10
|
|
19
|
+
- @atproto/lex-cbor@0.0.6
|
|
20
|
+
|
|
3
21
|
## 0.5.5
|
|
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 { CID, Cid } from '@atproto/lex-data';
|
|
5
4
|
/**
|
|
6
5
|
* @deprecated Use {@link encode} from `@atproto/lex-cbor` instead.
|
|
7
6
|
*/
|
|
@@ -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
38
|
export declare const 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":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAyB,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"ipld.d.ts","sourceRoot":"","sources":["../src/ipld.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAyB,MAAM,oBAAoB,CAAA;AAG3E,OAAO,EACL,GAAG,EACH,GAAG,EAUJ,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,MAAM,gBAAgB,EAAa,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;AACzE,OAAO,EAAE,gBAAgB,IAAI,UAAU,EAAE,CAAA;AAEzC;;GAEG;AACH,wBAAsB,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAUpE;AAED;;GAEG;AACH,eAAO,MAAM,gBAAgB,EAAgB,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;AAC5E,OAAO,EAAE,gBAAgB,IAAI,UAAU,EAAE,CAAA;AAEzC;;GAEG;AACH,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAIjE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAK5E;AAED;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,UAAU,GAChB,OAAO,CAAC,IAAI,CAAC,CAIf;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,EAAoB,CAAC,IAAI,EAAE,UAAU,KAAK,GAAG,CAAA;AAExE;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,GAAG,CAIxD;AAED,qBAAa,kBAAmB,SAAQ,SAAS;IAC5B,GAAG,EAAE,GAAG;gBAAR,GAAG,EAAE,GAAG;CAqB5B;AAKD,qBAAa,cAAe,SAAQ,KAAK;IAE9B,QAAQ,EAAE,GAAG;IACb,MAAM,EAAE,GAAG;gBADX,QAAQ,EAAE,GAAG,EACb,MAAM,EAAE,GAAG;CAIrB"}
|
package/dist/ipld.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.VerifyCidError = exports.VerifyCidTransform = exports.
|
|
3
|
+
exports.VerifyCidError = exports.VerifyCidTransform = exports.sha256RawToCid = exports.cidForCbor = exports.cidForCborLegacy = exports.cborDecode = exports.cborEncode = void 0;
|
|
4
4
|
exports.dataToCborBlock = dataToCborBlock;
|
|
5
5
|
exports.isValidCid = isValidCid;
|
|
6
6
|
exports.cborBytesToRecord = cborBytesToRecord;
|
|
7
|
+
exports.verifyCidForBytes = verifyCidForBytes;
|
|
8
|
+
exports.parseCidFromBytes = parseCidFromBytes;
|
|
7
9
|
const node_crypto_1 = require("node:crypto");
|
|
8
10
|
const node_stream_1 = require("node:stream");
|
|
9
11
|
const block_1 = require("multiformats/block");
|
|
12
|
+
const sha2_1 = require("multiformats/hashes/sha2");
|
|
10
13
|
const lex_cbor_1 = require("@atproto/lex-cbor");
|
|
11
14
|
const lex_data_1 = require("@atproto/lex-data");
|
|
12
15
|
/**
|
|
@@ -30,7 +33,7 @@ async function dataToCborBlock(value) {
|
|
|
30
33
|
code: lex_data_1.DAG_CBOR_MULTICODEC,
|
|
31
34
|
encode: lex_cbor_1.encode,
|
|
32
35
|
},
|
|
33
|
-
hasher:
|
|
36
|
+
hasher: sha2_1.sha256,
|
|
34
37
|
});
|
|
35
38
|
}
|
|
36
39
|
/**
|
|
@@ -56,19 +59,26 @@ function cborBytesToRecord(bytes) {
|
|
|
56
59
|
throw new Error(`Expected record with $type property`);
|
|
57
60
|
}
|
|
58
61
|
/**
|
|
59
|
-
* @deprecated Use {@link
|
|
62
|
+
* @deprecated Use {@link isCidForBytes} from `@atproto/lex-cbor` instead.
|
|
60
63
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
async function verifyCidForBytes(cid, bytes) {
|
|
65
|
+
if (!(await (0, lex_data_1.isCidForBytes)(cid, bytes))) {
|
|
66
|
+
throw new Error(`Not a valid CID for bytes (${cid.toString()})`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
63
69
|
/**
|
|
64
70
|
* @deprecated Use {@link cidForRawHash} from `@atproto/lex-cbor` instead.
|
|
65
71
|
*/
|
|
66
|
-
exports.sha256RawToCid =
|
|
72
|
+
exports.sha256RawToCid = lex_data_1.cidForRawHash;
|
|
67
73
|
/**
|
|
68
|
-
* @deprecated Use {@link
|
|
74
|
+
* @deprecated Use {@link decodeCid} from `@atproto/lex-cbor` instead.
|
|
69
75
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
function parseCidFromBytes(bytes) {
|
|
77
|
+
const cid = lex_data_1.CID.asCID((0, lex_data_1.decodeCid)(bytes, { flavor: 'dasl' }));
|
|
78
|
+
if (!cid)
|
|
79
|
+
throw new Error('Failed to parse CID from bytes');
|
|
80
|
+
return cid;
|
|
81
|
+
}
|
|
72
82
|
class VerifyCidTransform extends node_stream_1.Transform {
|
|
73
83
|
constructor(cid) {
|
|
74
84
|
const hasher = (0, node_crypto_1.createHash)('sha256');
|
package/dist/ipld.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipld.js","sourceRoot":"","sources":["../src/ipld.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"ipld.js","sourceRoot":"","sources":["../src/ipld.ts"],"names":[],"mappings":"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","sourcesContent":["import { 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 CID,\n Cid,\n DAG_CBOR_MULTICODEC,\n LexValue,\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 */\nexport const cidForCborLegacy = cidForLex as (data: unknown) => Promise<CID>\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 const sha256RawToCid = cidForRawHash as (hash: Uint8Array) => CID\n\n/**\n * @deprecated Use {@link decodeCid} from `@atproto/lex-cbor` instead.\n */\nexport function parseCidFromBytes(bytes: Uint8Array): CID {\n const cid = CID.asCID(decodeCid(bytes, { flavor: 'dasl' }))\n if (!cid) throw new Error('Failed to parse CID from bytes')\n return cid\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.7",
|
|
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.11",
|
|
25
|
+
"@atproto/lex-cbor": "0.0.7",
|
|
26
|
+
"@atproto/lex-data": "0.0.7"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"jest": "^28.1.2",
|
package/src/ipld.ts
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto'
|
|
2
2
|
import { Transform } from 'node:stream'
|
|
3
3
|
import { Block, ByteView, encode as encodeBlock } from 'multiformats/block'
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
cidForCbor,
|
|
7
|
-
cidForLex,
|
|
8
|
-
cidForRawHash,
|
|
9
|
-
decode,
|
|
10
|
-
encode,
|
|
11
|
-
hasher,
|
|
12
|
-
parseCidFromBytes,
|
|
13
|
-
verifyCidForBytes,
|
|
14
|
-
} from '@atproto/lex-cbor'
|
|
4
|
+
import { sha256 as hasher } from 'multiformats/hashes/sha2'
|
|
5
|
+
import { cidForLex, decode, encode } from '@atproto/lex-cbor'
|
|
15
6
|
import {
|
|
16
7
|
CID,
|
|
8
|
+
Cid,
|
|
17
9
|
DAG_CBOR_MULTICODEC,
|
|
18
10
|
LexValue,
|
|
11
|
+
// eslint-disable-next-line
|
|
12
|
+
cidForCbor,
|
|
13
|
+
cidForRawHash,
|
|
14
|
+
decodeCid,
|
|
15
|
+
isCidForBytes,
|
|
19
16
|
isTypedLexMap,
|
|
20
17
|
validateCidString,
|
|
21
18
|
} from '@atproto/lex-data'
|
|
@@ -73,10 +70,16 @@ export function cborBytesToRecord(bytes: Uint8Array): Record<string, unknown> {
|
|
|
73
70
|
}
|
|
74
71
|
|
|
75
72
|
/**
|
|
76
|
-
* @deprecated Use {@link
|
|
73
|
+
* @deprecated Use {@link isCidForBytes} from `@atproto/lex-cbor` instead.
|
|
77
74
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
export async function verifyCidForBytes(
|
|
76
|
+
cid: Cid,
|
|
77
|
+
bytes: Uint8Array,
|
|
78
|
+
): Promise<void> {
|
|
79
|
+
if (!(await isCidForBytes(cid, bytes))) {
|
|
80
|
+
throw new Error(`Not a valid CID for bytes (${cid.toString()})`)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
80
83
|
|
|
81
84
|
/**
|
|
82
85
|
* @deprecated Use {@link cidForRawHash} from `@atproto/lex-cbor` instead.
|
|
@@ -84,13 +87,16 @@ export { verifyCidForBytesLegacy as verifyCidForBytes }
|
|
|
84
87
|
export const sha256RawToCid = cidForRawHash as (hash: Uint8Array) => CID
|
|
85
88
|
|
|
86
89
|
/**
|
|
87
|
-
* @deprecated Use {@link
|
|
90
|
+
* @deprecated Use {@link decodeCid} from `@atproto/lex-cbor` instead.
|
|
88
91
|
*/
|
|
89
|
-
|
|
90
|
-
|
|
92
|
+
export function parseCidFromBytes(bytes: Uint8Array): CID {
|
|
93
|
+
const cid = CID.asCID(decodeCid(bytes, { flavor: 'dasl' }))
|
|
94
|
+
if (!cid) throw new Error('Failed to parse CID from bytes')
|
|
95
|
+
return cid
|
|
96
|
+
}
|
|
91
97
|
|
|
92
98
|
export class VerifyCidTransform extends Transform {
|
|
93
|
-
constructor(public cid:
|
|
99
|
+
constructor(public cid: Cid) {
|
|
94
100
|
const hasher = createHash('sha256')
|
|
95
101
|
super({
|
|
96
102
|
transform(chunk, encoding, callback) {
|
|
@@ -118,8 +124,8 @@ const asError = (err: unknown): Error =>
|
|
|
118
124
|
|
|
119
125
|
export class VerifyCidError extends Error {
|
|
120
126
|
constructor(
|
|
121
|
-
public expected:
|
|
122
|
-
public actual:
|
|
127
|
+
public expected: Cid,
|
|
128
|
+
public actual: Cid,
|
|
123
129
|
) {
|
|
124
130
|
super('Bad cid check')
|
|
125
131
|
}
|