@atcute/car 1.1.1 → 2.0.1
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/README.md +9 -6
- package/dist/atproto-repo.d.ts +3 -5
- package/dist/atproto-repo.js +2 -4
- package/dist/atproto-repo.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/reader.d.ts +2 -10
- package/dist/reader.js +0 -2
- package/dist/reader.js.map +1 -1
- package/dist/utilities/sync-car-reader.d.ts +2 -2
- package/dist/utilities/sync-car-reader.js +23 -24
- package/dist/utilities/sync-car-reader.js.map +1 -1
- package/lib/atproto-repo.ts +11 -14
- package/lib/index.ts +2 -2
- package/lib/reader.ts +0 -3
- package/lib/utilities/sync-car-reader.ts +29 -30
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
# @atcute/car
|
|
2
2
|
|
|
3
|
-
CAR (content-addressable
|
|
3
|
+
lightweight [DASL CAR (content-addressable archives)][dasl-car] and atproto repository decoder
|
|
4
|
+
library for AT Protocol.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
// convenient iterator for reading through an AT Protocol CAR repository
|
|
7
|
-
for (const { collection, rkey, record } of iterateAtpRepo(buf)) {
|
|
8
|
-
// ...
|
|
9
|
-
}
|
|
6
|
+
[dasl-car]: https://dasl.ing/car.html
|
|
10
7
|
|
|
8
|
+
```ts
|
|
11
9
|
// read through a CAR archive
|
|
12
10
|
const { roots, iterate } = readCar(buf);
|
|
13
11
|
|
|
14
12
|
for (const { cid, bytes } of iterate()) {
|
|
15
13
|
// ...
|
|
16
14
|
}
|
|
15
|
+
|
|
16
|
+
// convenient iterator for reading through an AT Protocol CAR repository
|
|
17
|
+
for (const { collection, rkey, record } of iterateAtpRepo(buf)) {
|
|
18
|
+
// ...
|
|
19
|
+
}
|
|
17
20
|
```
|
package/dist/atproto-repo.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as CID from '@atcute/cid';
|
|
2
2
|
export declare class RepoEntry {
|
|
3
3
|
readonly collection: string;
|
|
4
4
|
readonly rkey: string;
|
|
5
|
-
readonly cid:
|
|
5
|
+
readonly cid: CID.CidLink;
|
|
6
6
|
private blockmap;
|
|
7
|
-
constructor(collection: string, rkey: string, cid:
|
|
7
|
+
constructor(collection: string, rkey: string, cid: CID.CidLink, blockmap: BlockMap);
|
|
8
8
|
get record(): unknown;
|
|
9
9
|
}
|
|
10
10
|
export declare function iterateAtpRepo(buf: Uint8Array): Generator<RepoEntry>;
|
|
11
|
-
/** @deprecated Use `iterateAtpRepo` instead */
|
|
12
|
-
export declare const iterateAtpCar: typeof iterateAtpRepo;
|
|
13
11
|
type BlockMap = Map<string, Uint8Array>;
|
|
14
12
|
export {};
|
package/dist/atproto-repo.js
CHANGED
|
@@ -18,12 +18,12 @@ export class RepoEntry {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
export function* iterateAtpRepo(buf) {
|
|
21
|
-
const { roots, iterate } = readCar(
|
|
21
|
+
const { roots, iterate } = readCar(buf);
|
|
22
22
|
assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
|
|
23
23
|
// Collect all archive entries into a mapping of CID string -> actual bytes
|
|
24
24
|
const blockmap = new Map();
|
|
25
25
|
for (const entry of iterate()) {
|
|
26
|
-
blockmap.set(CID.
|
|
26
|
+
blockmap.set(CID.toString(entry.cid), entry.bytes);
|
|
27
27
|
}
|
|
28
28
|
// Read the head, then walk through the MST tree from there.
|
|
29
29
|
const commit = readObject(blockmap, roots[0]);
|
|
@@ -32,8 +32,6 @@ export function* iterateAtpRepo(buf) {
|
|
|
32
32
|
yield new RepoEntry(collection, rkey, cid, blockmap);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
-
/** @deprecated Use `iterateAtpRepo` instead */
|
|
36
|
-
export const iterateAtpCar = iterateAtpRepo;
|
|
37
35
|
function readObject(map, link) {
|
|
38
36
|
const cid = link.$link;
|
|
39
37
|
const bytes = map.get(cid);
|
package/dist/atproto-repo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atproto-repo.js","sourceRoot":"","sources":["../lib/atproto-repo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC;AAEnC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC,MAAM,OAAO,SAAS;IAEJ;IACA;IACA;IACR;IAJT,YACiB,UAAkB,EAClB,IAAY,EACZ,
|
|
1
|
+
{"version":3,"file":"atproto-repo.js","sourceRoot":"","sources":["../lib/atproto-repo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC;AAEnC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC,MAAM,OAAO,SAAS;IAEJ;IACA;IACA;IACR;IAJT,YACiB,UAAkB,EAClB,IAAY,EACZ,GAAgB,EACxB,QAAkB;QAHV,eAAU,GAAV,UAAU,CAAQ;QAClB,SAAI,GAAJ,IAAI,CAAQ;QACZ,QAAG,GAAH,GAAG,CAAa;QACxB,aAAQ,GAAR,QAAQ,CAAU;IACxB,CAAC;IAEJ,IAAI,MAAM;QACT,OAAO,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IAC5C,CAAC;CACD;AAED,MAAM,SAAS,CAAC,CAAC,cAAc,CAAC,GAAe;IAC9C,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACx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OAAO,GAAG,IAAI,CAAC,CAAC,CAAC;IAEvB,IAAI,OAAO,GAAG,EAAE,CAAC;IAEjB,IAAI,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;QACrB,KAAK,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IACjC,CAAC;IAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;QAClD,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QAEzB,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACxD,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;QAEhD,OAAO,GAAG,GAAG,CAAC;QAEd,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC;QAEjC,IAAI,KAAK,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;YACtB,KAAK,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QAClC,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,MAAM,CAAC,SAAkB,EAAE,OAAe;IAClD,IAAI,CAAC,SAAS,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC;AACF,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { iterateAtpRepo, type RepoEntry } from './atproto-repo.js';
|
|
2
|
+
export { readCar } from './reader.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { iterateAtpRepo } from './atproto-repo.js';
|
|
2
|
+
export { readCar } from './reader.js';
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAkB,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/reader.d.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
export declare const readCar: (buffer: Uint8Array) => {
|
|
2
|
-
roots: import("@atcute/
|
|
2
|
+
roots: import("@atcute/cid").CidLink[];
|
|
3
3
|
iterate(): Generator<{
|
|
4
|
-
cid: import("@atcute/cid").
|
|
5
|
-
bytes: Uint8Array;
|
|
6
|
-
}>;
|
|
7
|
-
};
|
|
8
|
-
/** @deprecated Use `readCar` instead */
|
|
9
|
-
export declare const fromUint8Array: (buffer: Uint8Array) => {
|
|
10
|
-
roots: import("@atcute/cbor").CIDLink[];
|
|
11
|
-
iterate(): Generator<{
|
|
12
|
-
cid: import("@atcute/cid").CID;
|
|
4
|
+
cid: import("@atcute/cid").Cid;
|
|
13
5
|
bytes: Uint8Array;
|
|
14
6
|
}>;
|
|
15
7
|
};
|
package/dist/reader.js
CHANGED
package/dist/reader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reader.js","sourceRoot":"","sources":["../lib/reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAEjE,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,MAAkB,EAAE,EAAE;IAC7C,MAAM,MAAM,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;IACzC,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC,CAAC
|
|
1
|
+
{"version":3,"file":"reader.js","sourceRoot":"","sources":["../lib/reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAEjE,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,MAAkB,EAAE,EAAE;IAC7C,MAAM,MAAM,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;IACzC,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC,CAAC"}
|
|
@@ -2,9 +2,9 @@ import * as CBOR from '@atcute/cbor';
|
|
|
2
2
|
import * as CID from '@atcute/cid';
|
|
3
3
|
import type { SyncByteReader } from './byte-reader.js';
|
|
4
4
|
export declare const createCarReader: (reader: SyncByteReader) => {
|
|
5
|
-
roots: CBOR.
|
|
5
|
+
roots: CBOR.CidLink[];
|
|
6
6
|
iterate(): Generator<{
|
|
7
|
-
cid: CID.
|
|
7
|
+
cid: CID.Cid;
|
|
8
8
|
bytes: Uint8Array;
|
|
9
9
|
}>;
|
|
10
10
|
};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import * as CBOR from '@atcute/cbor';
|
|
2
|
-
import * as varint from '@atcute/varint';
|
|
3
2
|
import * as CID from '@atcute/cid';
|
|
3
|
+
import * as varint from '@atcute/varint';
|
|
4
4
|
const isCarV1Header = (value) => {
|
|
5
5
|
if (value === null || typeof value !== 'object') {
|
|
6
6
|
return false;
|
|
7
7
|
}
|
|
8
8
|
const { version, roots } = value;
|
|
9
|
-
return version === 1 && Array.isArray(roots) && roots.every((root) => root instanceof CBOR.
|
|
9
|
+
return version === 1 && Array.isArray(roots) && roots.every((root) => root instanceof CBOR.CidLinkWrapper);
|
|
10
10
|
};
|
|
11
11
|
const readVarint = (reader, size) => {
|
|
12
12
|
const buf = reader.upto(size);
|
|
@@ -29,32 +29,31 @@ const readHeader = (reader) => {
|
|
|
29
29
|
}
|
|
30
30
|
return header;
|
|
31
31
|
};
|
|
32
|
-
const readMultihashDigest = (reader) => {
|
|
33
|
-
const first = reader.upto(8);
|
|
34
|
-
const [code, codeOffset] = varint.decode(first);
|
|
35
|
-
const [size, sizeOffset] = varint.decode(first.subarray(codeOffset));
|
|
36
|
-
const offset = codeOffset + sizeOffset;
|
|
37
|
-
const bytes = reader.exactly(offset + size, true);
|
|
38
|
-
const digest = bytes.subarray(offset);
|
|
39
|
-
return {
|
|
40
|
-
code: code,
|
|
41
|
-
size: size,
|
|
42
|
-
digest: digest,
|
|
43
|
-
bytes: bytes,
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
32
|
const readCid = (reader) => {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
33
|
+
const head = reader.upto(3 + 4);
|
|
34
|
+
const version = head[0];
|
|
35
|
+
const codec = head[1];
|
|
36
|
+
const digestCodec = head[2];
|
|
37
|
+
if (version !== CID.CID_VERSION) {
|
|
38
|
+
throw new RangeError(`incorrect cid version (got v${version})`);
|
|
39
|
+
}
|
|
40
|
+
if (codec !== CID.CODEC_DCBOR && codec !== CID.CODEC_RAW) {
|
|
41
|
+
throw new RangeError(`incorrect cid codec (got 0x${codec.toString(16)})`);
|
|
42
|
+
}
|
|
43
|
+
if (digestCodec !== CID.HASH_SHA256) {
|
|
44
|
+
throw new RangeError(`incorrect cid hash type (got 0x${digestCodec.toString(16)})`);
|
|
50
45
|
}
|
|
51
|
-
const
|
|
52
|
-
const
|
|
46
|
+
const [digestSize, digestLebSize] = varint.decode(head, 3);
|
|
47
|
+
const bytes = reader.exactly(3 + digestLebSize + digestSize, true);
|
|
48
|
+
const digest = bytes.subarray(3 + digestLebSize);
|
|
53
49
|
const cid = {
|
|
54
50
|
version: version,
|
|
55
|
-
|
|
56
|
-
digest:
|
|
57
|
-
|
|
51
|
+
codec: codec,
|
|
52
|
+
digest: {
|
|
53
|
+
codec: digestCodec,
|
|
54
|
+
contents: digest,
|
|
55
|
+
},
|
|
56
|
+
bytes: bytes,
|
|
58
57
|
};
|
|
59
58
|
return cid;
|
|
60
59
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync-car-reader.js","sourceRoot":"","sources":["../../lib/utilities/sync-car-reader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"sync-car-reader.js","sourceRoot":"","sources":["../../lib/utilities/sync-car-reader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC;AACnC,OAAO,KAAK,MAAM,MAAM,gBAAgB,CAAC;AASzC,MAAM,aAAa,GAAG,CAAC,KAAc,EAAwB,EAAE;IAC9D,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACjD,OAAO,KAAK,CAAC;IACd,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,KAAoB,CAAC;IAChD,OAAO,OAAO,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,YAAY,IAAI,CAAC,cAAc,CAAC,CAAC;AAC5G,CAAC,CAAC;AAEF,MAAM,UAAU,GAAG,CAAC,MAAsB,EAAE,IAAY,EAAU,EAAE;IACnE,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC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AAM,CAAC,MAAM,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC;IAEpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;AAC3B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAsB,EAAE,EAAE;IACzD,MAAM,EAAE,KAAK,EAAE,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IAErC,OAAO;QACN,KAAK;QACL,CAAC,OAAO;YACP,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAClC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;gBACnD,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBAE9C,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;YACtB,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC,CAAC"}
|
package/lib/atproto-repo.ts
CHANGED
|
@@ -9,7 +9,7 @@ export class RepoEntry {
|
|
|
9
9
|
constructor(
|
|
10
10
|
public readonly collection: string,
|
|
11
11
|
public readonly rkey: string,
|
|
12
|
-
public readonly cid:
|
|
12
|
+
public readonly cid: CID.CidLink,
|
|
13
13
|
private blockmap: BlockMap,
|
|
14
14
|
) {}
|
|
15
15
|
|
|
@@ -19,13 +19,13 @@ export class RepoEntry {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export function* iterateAtpRepo(buf: Uint8Array): Generator<RepoEntry> {
|
|
22
|
-
const { roots, iterate } = readCar(
|
|
22
|
+
const { roots, iterate } = readCar(buf);
|
|
23
23
|
assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
|
|
24
24
|
|
|
25
25
|
// Collect all archive entries into a mapping of CID string -> actual bytes
|
|
26
26
|
const blockmap: BlockMap = new Map();
|
|
27
27
|
for (const entry of iterate()) {
|
|
28
|
-
blockmap.set(CID.
|
|
28
|
+
blockmap.set(CID.toString(entry.cid), entry.bytes);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
// Read the head, then walk through the MST tree from there.
|
|
@@ -37,10 +37,7 @@ export function* iterateAtpRepo(buf: Uint8Array): Generator<RepoEntry> {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
export const iterateAtpCar = iterateAtpRepo;
|
|
42
|
-
|
|
43
|
-
function readObject(map: BlockMap, link: CBOR.CIDLink): unknown {
|
|
40
|
+
function readObject(map: BlockMap, link: CID.CidLink): unknown {
|
|
44
41
|
const cid = link.$link;
|
|
45
42
|
|
|
46
43
|
const bytes = map.get(cid);
|
|
@@ -51,7 +48,7 @@ function readObject(map: BlockMap, link: CBOR.CIDLink): unknown {
|
|
|
51
48
|
return data;
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
function* walkEntries(map: BlockMap, pointer:
|
|
51
|
+
function* walkEntries(map: BlockMap, pointer: CID.CidLink): Generator<NodeEntry> {
|
|
55
52
|
const data = readObject(map, pointer) as MstNode;
|
|
56
53
|
const entries = data.e;
|
|
57
54
|
|
|
@@ -88,9 +85,9 @@ type BlockMap = Map<string, Uint8Array>;
|
|
|
88
85
|
interface Commit {
|
|
89
86
|
version: 3;
|
|
90
87
|
did: string;
|
|
91
|
-
data:
|
|
88
|
+
data: CID.CidLink;
|
|
92
89
|
rev: string;
|
|
93
|
-
prev:
|
|
90
|
+
prev: CID.CidLink | null;
|
|
94
91
|
sig: CBOR.Bytes;
|
|
95
92
|
}
|
|
96
93
|
|
|
@@ -100,19 +97,19 @@ interface TreeEntry {
|
|
|
100
97
|
/** remainder of key for this TreeEntry, after "prefixlen" have been removed */
|
|
101
98
|
k: CBOR.Bytes;
|
|
102
99
|
/** link to a sub-tree Node at a lower level which has keys sorting after this TreeEntry's key (to the "right"), but before the next TreeEntry's key in this Node (if any) */
|
|
103
|
-
v:
|
|
100
|
+
v: CID.CidLink;
|
|
104
101
|
/** next subtree (to the right of leaf) */
|
|
105
|
-
t:
|
|
102
|
+
t: CID.CidLink | null;
|
|
106
103
|
}
|
|
107
104
|
|
|
108
105
|
interface MstNode {
|
|
109
106
|
/** link to sub-tree Node on a lower level and with all keys sorting before keys at this node */
|
|
110
|
-
l:
|
|
107
|
+
l: CID.CidLink | null;
|
|
111
108
|
/** ordered list of TreeEntry objects */
|
|
112
109
|
e: TreeEntry[];
|
|
113
110
|
}
|
|
114
111
|
|
|
115
112
|
interface NodeEntry {
|
|
116
113
|
key: string;
|
|
117
|
-
cid:
|
|
114
|
+
cid: CID.CidLink;
|
|
118
115
|
}
|
package/lib/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { iterateAtpRepo, type RepoEntry } from './atproto-repo.js';
|
|
2
|
+
export { readCar } from './reader.js';
|
package/lib/reader.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import * as CBOR from '@atcute/cbor';
|
|
2
|
-
import * as varint from '@atcute/varint';
|
|
3
2
|
import * as CID from '@atcute/cid';
|
|
3
|
+
import * as varint from '@atcute/varint';
|
|
4
4
|
|
|
5
5
|
import type { SyncByteReader } from './byte-reader.js';
|
|
6
6
|
|
|
7
7
|
interface CarV1Header {
|
|
8
8
|
version: 1;
|
|
9
|
-
roots:
|
|
9
|
+
roots: CID.CidLink[];
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
const isCarV1Header = (value: unknown): value is CarV1Header => {
|
|
@@ -15,7 +15,7 @@ const isCarV1Header = (value: unknown): value is CarV1Header => {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
const { version, roots } = value as CarV1Header;
|
|
18
|
-
return version === 1 && Array.isArray(roots) && roots.every((root) => root instanceof CBOR.
|
|
18
|
+
return version === 1 && Array.isArray(roots) && roots.every((root) => root instanceof CBOR.CidLinkWrapper);
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
const readVarint = (reader: SyncByteReader, size: number): number => {
|
|
@@ -45,45 +45,44 @@ const readHeader = (reader: SyncByteReader): CarV1Header => {
|
|
|
45
45
|
return header;
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
const [code, codeOffset] = varint.decode(first);
|
|
52
|
-
const [size, sizeOffset] = varint.decode(first.subarray(codeOffset));
|
|
48
|
+
const readCid = (reader: SyncByteReader): CID.Cid => {
|
|
49
|
+
const head = reader.upto(3 + 4);
|
|
53
50
|
|
|
54
|
-
const
|
|
51
|
+
const version = head[0];
|
|
52
|
+
const codec = head[1];
|
|
53
|
+
const digestCodec = head[2];
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
if (version !== CID.CID_VERSION) {
|
|
56
|
+
throw new RangeError(`incorrect cid version (got v${version})`);
|
|
57
|
+
}
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
digest: digest,
|
|
63
|
-
bytes: bytes,
|
|
64
|
-
};
|
|
65
|
-
};
|
|
59
|
+
if (codec !== CID.CODEC_DCBOR && codec !== CID.CODEC_RAW) {
|
|
60
|
+
throw new RangeError(`incorrect cid codec (got 0x${codec.toString(16)})`);
|
|
61
|
+
}
|
|
66
62
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (version !== 1) {
|
|
70
|
-
throw new Error(`expected a cidv1`);
|
|
63
|
+
if (digestCodec !== CID.HASH_SHA256) {
|
|
64
|
+
throw new RangeError(`incorrect cid hash type (got 0x${digestCodec.toString(16)})`);
|
|
71
65
|
}
|
|
72
66
|
|
|
73
|
-
const
|
|
74
|
-
const digest = readMultihashDigest(reader);
|
|
67
|
+
const [digestSize, digestLebSize] = varint.decode(head, 3);
|
|
75
68
|
|
|
76
|
-
const
|
|
69
|
+
const bytes = reader.exactly(3 + digestLebSize + digestSize, true);
|
|
70
|
+
const digest = bytes.subarray(3 + digestLebSize);
|
|
71
|
+
|
|
72
|
+
const cid: CID.Cid = {
|
|
77
73
|
version: version,
|
|
78
|
-
|
|
79
|
-
digest:
|
|
80
|
-
|
|
74
|
+
codec: codec,
|
|
75
|
+
digest: {
|
|
76
|
+
codec: digestCodec,
|
|
77
|
+
contents: digest,
|
|
78
|
+
},
|
|
79
|
+
bytes: bytes,
|
|
81
80
|
};
|
|
82
81
|
|
|
83
82
|
return cid;
|
|
84
83
|
};
|
|
85
84
|
|
|
86
|
-
const readBlockHeader = (reader: SyncByteReader): { cid: CID.
|
|
85
|
+
const readBlockHeader = (reader: SyncByteReader): { cid: CID.Cid; blockSize: number } => {
|
|
87
86
|
const start = reader.pos;
|
|
88
87
|
|
|
89
88
|
let size = readVarint(reader, 8);
|
|
@@ -104,7 +103,7 @@ export const createCarReader = (reader: SyncByteReader) => {
|
|
|
104
103
|
|
|
105
104
|
return {
|
|
106
105
|
roots,
|
|
107
|
-
*iterate(): Generator<{ cid: CID.
|
|
106
|
+
*iterate(): Generator<{ cid: CID.Cid; bytes: Uint8Array }> {
|
|
108
107
|
while (reader.upto(8).length > 0) {
|
|
109
108
|
const { cid, blockSize } = readBlockHeader(reader);
|
|
110
109
|
const bytes = reader.exactly(blockSize, true);
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@atcute/car",
|
|
4
|
-
"version": "
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "2.0.1",
|
|
5
|
+
"description": "lightweight DASL CAR and atproto repository decoder for AT Protocol.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"atproto",
|
|
8
|
+
"dasl",
|
|
9
|
+
"car"
|
|
10
|
+
],
|
|
6
11
|
"license": "MIT",
|
|
7
12
|
"repository": {
|
|
8
13
|
"url": "https://github.com/mary-ext/atcute",
|
|
@@ -19,12 +24,12 @@
|
|
|
19
24
|
},
|
|
20
25
|
"sideEffects": false,
|
|
21
26
|
"devDependencies": {
|
|
22
|
-
"@types/bun": "^1.1.
|
|
27
|
+
"@types/bun": "^1.1.14"
|
|
23
28
|
},
|
|
24
29
|
"dependencies": {
|
|
25
|
-
"@atcute/cbor": "^
|
|
26
|
-
"@atcute/
|
|
27
|
-
"@atcute/
|
|
30
|
+
"@atcute/cbor": "^2.0.1",
|
|
31
|
+
"@atcute/cid": "^2.0.1",
|
|
32
|
+
"@atcute/varint": "^1.0.2"
|
|
28
33
|
},
|
|
29
34
|
"scripts": {
|
|
30
35
|
"build": "tsc --project tsconfig.build.json",
|