@atcute/car 3.0.5 → 3.1.0

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.
Files changed (78) hide show
  1. package/README.md +22 -7
  2. package/dist/v3/atproto-repo.d.ts +1 -0
  3. package/dist/v3/atproto-repo.js +2 -0
  4. package/dist/v3/atproto-repo.js.map +1 -0
  5. package/dist/v3/index.js.map +1 -0
  6. package/dist/v3/reader.d.ts +1 -0
  7. package/dist/v3/reader.js +2 -0
  8. package/dist/v3/reader.js.map +1 -0
  9. package/dist/v4/car-reader/index.d.ts +3 -0
  10. package/dist/v4/car-reader/index.js +4 -0
  11. package/dist/v4/car-reader/index.js.map +1 -0
  12. package/dist/v4/car-reader/stream-car-reader.d.ts +11 -0
  13. package/dist/v4/car-reader/stream-car-reader.js +178 -0
  14. package/dist/v4/car-reader/stream-car-reader.js.map +1 -0
  15. package/dist/v4/car-reader/sync-car-reader.d.ts +10 -0
  16. package/dist/{utilities → v4/car-reader}/sync-car-reader.js +63 -31
  17. package/dist/v4/car-reader/sync-car-reader.js.map +1 -0
  18. package/dist/{utilities/car.js → v4/car-reader/types.js} +1 -1
  19. package/dist/v4/car-reader/types.js.map +1 -0
  20. package/dist/v4/index.d.ts +2 -0
  21. package/dist/v4/index.js +3 -0
  22. package/dist/v4/index.js.map +1 -0
  23. package/dist/v4/repo-reader/index.d.ts +5 -0
  24. package/dist/v4/repo-reader/index.js +6 -0
  25. package/dist/v4/repo-reader/index.js.map +1 -0
  26. package/dist/v4/repo-reader/mst.d.ts +47 -0
  27. package/dist/v4/repo-reader/mst.js +62 -0
  28. package/dist/v4/repo-reader/mst.js.map +1 -0
  29. package/dist/v4/repo-reader/stream-repo-reader.d.ts +24 -0
  30. package/dist/v4/repo-reader/stream-repo-reader.js +137 -0
  31. package/dist/v4/repo-reader/stream-repo-reader.js.map +1 -0
  32. package/dist/v4/repo-reader/sync-blockmap.d.ts +29 -0
  33. package/dist/v4/repo-reader/sync-blockmap.js +58 -0
  34. package/dist/v4/repo-reader/sync-blockmap.js.map +1 -0
  35. package/dist/v4/repo-reader/sync-repo-reader.d.ts +2 -0
  36. package/dist/v4/repo-reader/sync-repo-reader.js +20 -0
  37. package/dist/v4/repo-reader/sync-repo-reader.js.map +1 -0
  38. package/dist/v4/repo-reader/types.d.ts +20 -0
  39. package/dist/v4/repo-reader/types.js +37 -0
  40. package/dist/v4/repo-reader/types.js.map +1 -0
  41. package/dist/v4/utils.d.ts +3 -0
  42. package/dist/v4/utils.js +6 -0
  43. package/dist/v4/utils.js.map +1 -0
  44. package/lib/v3/atproto-repo.ts +15 -0
  45. package/lib/v3/reader.ts +9 -0
  46. package/lib/v4/car-reader/index.ts +4 -0
  47. package/lib/v4/car-reader/stream-car-reader.ts +240 -0
  48. package/lib/{utilities → v4/car-reader}/sync-car-reader.ts +90 -40
  49. package/lib/v4/index.ts +2 -0
  50. package/lib/v4/repo-reader/index.ts +6 -0
  51. package/lib/v4/repo-reader/mst.ts +110 -0
  52. package/lib/v4/repo-reader/stream-repo-reader.ts +204 -0
  53. package/lib/v4/repo-reader/sync-blockmap.ts +85 -0
  54. package/lib/v4/repo-reader/sync-repo-reader.ts +27 -0
  55. package/lib/v4/repo-reader/types.ts +32 -0
  56. package/lib/v4/utils.ts +7 -0
  57. package/package.json +9 -4
  58. package/dist/atproto-repo.d.ts +0 -104
  59. package/dist/atproto-repo.js +0 -171
  60. package/dist/atproto-repo.js.map +0 -1
  61. package/dist/index.js.map +0 -1
  62. package/dist/reader.d.ts +0 -4
  63. package/dist/reader.js +0 -7
  64. package/dist/reader.js.map +0 -1
  65. package/dist/utilities/car.js.map +0 -1
  66. package/dist/utilities/sync-byte-reader.d.ts +0 -7
  67. package/dist/utilities/sync-byte-reader.js +0 -28
  68. package/dist/utilities/sync-byte-reader.js.map +0 -1
  69. package/dist/utilities/sync-car-reader.d.ts +0 -7
  70. package/dist/utilities/sync-car-reader.js.map +0 -1
  71. package/lib/atproto-repo.ts +0 -248
  72. package/lib/reader.ts +0 -10
  73. package/lib/utilities/sync-byte-reader.ts +0 -39
  74. /package/dist/{index.d.ts → v3/index.d.ts} +0 -0
  75. /package/dist/{index.js → v3/index.js} +0 -0
  76. /package/dist/{utilities/car.d.ts → v4/car-reader/types.d.ts} +0 -0
  77. /package/lib/{index.ts → v3/index.ts} +0 -0
  78. /package/lib/{utilities/car.ts → v4/car-reader/types.ts} +0 -0
@@ -0,0 +1,137 @@
1
+ import Queue from 'yocto-queue';
2
+ import * as CBOR from '@atcute/cbor';
3
+ import * as CID from '@atcute/cid';
4
+ import { decodeUtf8From } from '@atcute/uint8array';
5
+ import * as CarReader from '../car-reader/index.js';
6
+ import { assert } from '../utils.js';
7
+ import { isCommit, isMstNode } from './mst.js';
8
+ import { RepoEntry } from './types.js';
9
+ export const repoEntryTransform = () => {
10
+ const transform = new TransformStream();
11
+ let repo;
12
+ return {
13
+ readable: new ReadableStream({
14
+ async start(controller) {
15
+ repo = fromStream(transform.readable);
16
+ try {
17
+ for await (const entry of repo) {
18
+ controller.enqueue(entry);
19
+ }
20
+ await repo.dispose();
21
+ controller.close();
22
+ }
23
+ catch (err) {
24
+ controller.error(err);
25
+ }
26
+ },
27
+ async cancel() {
28
+ if (repo !== undefined) {
29
+ await repo.dispose();
30
+ }
31
+ },
32
+ }),
33
+ writable: transform.writable,
34
+ };
35
+ };
36
+ export const fromStream = (stream) => {
37
+ let missingBlocks = [];
38
+ return {
39
+ get missingBlocks() {
40
+ return missingBlocks;
41
+ },
42
+ async dispose() {
43
+ // does nothing for now
44
+ },
45
+ [Symbol.asyncDispose]() {
46
+ return this.dispose();
47
+ },
48
+ async *[Symbol.asyncIterator]() {
49
+ await using car = CarReader.fromStream(stream);
50
+ const pending = new Map();
51
+ const strays = new Map();
52
+ const queue = new Queue();
53
+ const request = (cid, meta) => {
54
+ const entry = strays.get(cid);
55
+ if (entry !== undefined) {
56
+ strays.delete(cid);
57
+ queue.enqueue({ c: cid, e: entry, m: meta });
58
+ }
59
+ else {
60
+ pending.set(cid, meta);
61
+ }
62
+ };
63
+ {
64
+ const roots = await car.roots();
65
+ assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
66
+ const rootCid = roots[0].$link;
67
+ request(rootCid, { t: 0 });
68
+ }
69
+ for await (const entry of car) {
70
+ const cid = CID.toString(entry.cid);
71
+ {
72
+ const meta = pending.get(cid);
73
+ if (meta !== undefined) {
74
+ pending.delete(cid);
75
+ queue.enqueue({ c: cid, e: entry, m: meta });
76
+ }
77
+ else {
78
+ strays.set(cid, entry);
79
+ }
80
+ }
81
+ let task;
82
+ while ((task = queue.dequeue())) {
83
+ const { c: cid, e: entry, m: meta } = task;
84
+ switch (meta.t) {
85
+ case 0: {
86
+ const commit = CBOR.decode(entry.bytes);
87
+ assert(isCommit(commit), `expected commit block; cid=${cid}`);
88
+ request(commit.data.$link, { t: 1 });
89
+ break;
90
+ }
91
+ case 1: {
92
+ const node = CBOR.decode(entry.bytes);
93
+ assert(isMstNode(node), `expected mst node block; cid=${cid}`);
94
+ const entries = node.e;
95
+ const left = node.l;
96
+ let lastKey = '';
97
+ if (left !== null) {
98
+ request(left.$link, meta);
99
+ }
100
+ for (let i = 0, il = entries.length; i < il; i++) {
101
+ const entry = entries[i];
102
+ const next = entry.t;
103
+ const key_str = decodeUtf8From(CBOR.fromBytes(entry.k));
104
+ const key = lastKey.slice(0, entry.p) + key_str;
105
+ lastKey = key;
106
+ request(entry.v.$link, { t: 2, k: key });
107
+ if (next !== null) {
108
+ request(next.$link, { t: 1 });
109
+ }
110
+ }
111
+ break;
112
+ }
113
+ case 2: {
114
+ const [collection, rkey] = meta.k.split('/');
115
+ yield new RepoEntry(collection, rkey, CID.toCidLink(entry.cid), entry);
116
+ break;
117
+ }
118
+ }
119
+ }
120
+ }
121
+ missingBlocks = Array.from(pending, ([cid, meta]) => {
122
+ switch (meta.t) {
123
+ case 0: {
124
+ return { cid, type: 'commit' };
125
+ }
126
+ case 1: {
127
+ return { cid, type: 'mst-node' };
128
+ }
129
+ case 2: {
130
+ return { cid, type: 'record', key: meta.k };
131
+ }
132
+ }
133
+ });
134
+ },
135
+ };
136
+ };
137
+ //# sourceMappingURL=stream-repo-reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-repo-reader.js","sourceRoot":"","sources":["../../../lib/v4/repo-reader/stream-repo-reader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,aAAa,CAAC;AAEhC,OAAO,KAAK,IAAI,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAsCvC,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAgD,EAAE;IACnF,MAAM,SAAS,GAAG,IAAI,eAAe,EAA0B,CAAC;IAChE,IAAI,IAAoC,CAAC;IAEzC,OAAO;QACN,QAAQ,EAAE,IAAI,cAAc,CAAC;YAC5B,KAAK,CAAC,KAAK,CAAC,UAAU;gBACrB,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;gBAEtC,IAAI,CAAC;oBACJ,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,IAAI,EAAE,CAAC;wBAChC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;oBAC3B,CAAC;oBAED,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;oBAErB,UAAU,CAAC,KAAK,EAAE,CAAC;gBACpB,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACd,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACvB,CAAC;Y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3
+ export type BlockMap = Map<string, CarReader.CarEntry>;
4
+ /**
5
+ * collects entries from a CAR archive into a mapping of CID string -> actual bytes
6
+ * @param iterator a generator that yields objects with a `cid` and `bytes` property
7
+ * @returns a mapping of CID string -> actual bytes
8
+ */
9
+ export declare const collectBlock: (iterator: Iterable<CarReader.CarEntry>) => BlockMap;
10
+ /**
11
+ * reads a block from the blockmap and validates it against the provided validation function
12
+ * @param map a mapping of CID string -> actual bytes
13
+ * @param link a CID link to read
14
+ * @param validate a validation function to validate the decoded data
15
+ * @returns the decoded and validated data
16
+ */
17
+ export declare const readBlock: <T>(map: BlockMap, link: CID.CidLink, validate: (value: unknown) => value is T) => T;
18
+ /** node entry object */
19
+ export interface NodeEntry {
20
+ key: string;
21
+ cid: CID.CidLink;
22
+ }
23
+ /**
24
+ * walks the entries of a Merkle Sorted Tree (MST) in a depth-first manner
25
+ * @param map a mapping of CID string -> actual bytes
26
+ * @param pointer a CID link to the root of the MST
27
+ * @returns a generator that yields the entries of the MST
28
+ */
29
+ export declare function walkMstEntries(map: BlockMap, pointer: CID.CidLink): Generator<NodeEntry>;
@@ -0,0 +1,58 @@
1
+ import * as CBOR from '@atcute/cbor';
2
+ import * as CID from '@atcute/cid';
3
+ import { decodeUtf8From } from '@atcute/uint8array';
4
+ import * as CarReader from '../car-reader/index.js';
5
+ import { assert } from '../utils.js';
6
+ import { isMstNode } from './mst.js';
7
+ /**
8
+ * collects entries from a CAR archive into a mapping of CID string -> actual bytes
9
+ * @param iterator a generator that yields objects with a `cid` and `bytes` property
10
+ * @returns a mapping of CID string -> actual bytes
11
+ */
12
+ export const collectBlock = (iterator) => {
13
+ const blockmap = new Map();
14
+ for (const entry of iterator) {
15
+ blockmap.set(CID.toString(entry.cid), entry);
16
+ }
17
+ return blockmap;
18
+ };
19
+ /**
20
+ * reads a block from the blockmap and validates it against the provided validation function
21
+ * @param map a mapping of CID string -> actual bytes
22
+ * @param link a CID link to read
23
+ * @param validate a validation function to validate the decoded data
24
+ * @returns the decoded and validated data
25
+ */
26
+ export const readBlock = (map, link, validate) => {
27
+ const cid = link.$link;
28
+ const entry = map.get(cid);
29
+ assert(entry != null, `cid not found in blockmap; cid=${cid}`);
30
+ const data = CBOR.decode(entry.bytes);
31
+ assert(validate(data), `validation failed for cid=${cid}`);
32
+ return data;
33
+ };
34
+ /**
35
+ * walks the entries of a Merkle Sorted Tree (MST) in a depth-first manner
36
+ * @param map a mapping of CID string -> actual bytes
37
+ * @param pointer a CID link to the root of the MST
38
+ * @returns a generator that yields the entries of the MST
39
+ */
40
+ export function* walkMstEntries(map, pointer) {
41
+ const data = readBlock(map, pointer, isMstNode);
42
+ const entries = data.e;
43
+ let lastKey = '';
44
+ if (data.l !== null) {
45
+ yield* walkMstEntries(map, data.l);
46
+ }
47
+ for (let i = 0, il = entries.length; i < il; i++) {
48
+ const entry = entries[i];
49
+ const key_str = decodeUtf8From(CBOR.fromBytes(entry.k));
50
+ const key = lastKey.slice(0, entry.p) + key_str;
51
+ lastKey = key;
52
+ yield { key: key, cid: entry.v };
53
+ if (entry.t !== null) {
54
+ yield* walkMstEntries(map, entry.t);
55
+ }
56
+ }
57
+ }
58
+ //# sourceMappingURL=sync-blockmap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-blockmap.js","sourceRoot":"","sources":["../../../lib/v4/repo-reader/sync-blockmap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAIrC;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,QAAsC,EAAY,EAAE;IAChF,MAAM,QAAQ,GA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,EAAE,CAAC;QAClD,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QAEzB,MAAM,OAAO,GAAG,cAAc,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,cAAc,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QACrC,CAAC;IACF,CAAC;AACF,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { RepoEntry } from './types.js';
2
+ export declare function fromUint8Array(buf: Uint8Array): Generator<RepoEntry>;
@@ -0,0 +1,20 @@
1
+ import * as CarReader from '../car-reader/index.js';
2
+ import { assert } from '../utils.js';
3
+ import { isCommit } from './mst.js';
4
+ import { collectBlock, readBlock, walkMstEntries } from './sync-blockmap.js';
5
+ import { RepoEntry } from './types.js';
6
+ export function* fromUint8Array(buf) {
7
+ const car = CarReader.fromUint8Array(buf);
8
+ const roots = car.roots;
9
+ assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
10
+ const blockmap = collectBlock(car);
11
+ assert(blockmap.size > 0, `expected at least 1 block in the archive; got=${blockmap.size}`);
12
+ const commit = readBlock(blockmap, roots[0], isCommit);
13
+ for (const { key, cid } of walkMstEntries(blockmap, commit.data)) {
14
+ const [collection, rkey] = key.split('/');
15
+ const carEntry = blockmap.get(cid.$link);
16
+ assert(carEntry != null, `cid not found in blockmap; cid=${cid}`);
17
+ yield new RepoEntry(collection, rkey, cid, carEntry);
18
+ }
19
+ }
20
+ //# sour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Q,IAAI,IAAI,EAAE,kCAAkC,GAAG,EAAE,CAAC,CAAC;QAElE,MAAM,IAAI,SAAS,CAAC,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IACtD,CAAC;AACF,CAAC"}
@@ -0,0 +1,20 @@
1
+ import * as CID from '@atcute/cid';
2
+ import { type CarEntry } from '../car-reader/index.js';
3
+ export declare class RepoEntry {
4
+ /** the collection this record belongs to */
5
+ readonly collection: string;
6
+ /** record key */
7
+ readonly rkey: string;
8
+ /** CID of this record */
9
+ readonly cid: CID.CidLink;
10
+ /** the associated CarEntry for this record */
11
+ readonly carEntry: CarEntry;
12
+ /**
13
+ * raw contents of this record
14
+ */
15
+ get bytes(): Uint8Array;
16
+ /**
17
+ * decoded contents of this record
18
+ */
19
+ get record(): unknown;
20
+ }
@@ -0,0 +1,37 @@
1
+ import * as CBOR from '@atcute/cbor';
2
+ import * as CID from '@atcute/cid';
3
+ import {} from '../car-reader/index.js';
4
+ export class RepoEntry {
5
+ collection;
6
+ rkey;
7
+ cid;
8
+ carEntry;
9
+ /** @internal */
10
+ constructor(
11
+ /** the collection this record belongs to */
12
+ collection,
13
+ /** record key */
14
+ rkey,
15
+ /** CID of this record */
16
+ cid,
17
+ /** the associated CarEntry for this record */
18
+ carEntry) {
19
+ this.collection = collection;
20
+ this.rkey = rkey;
21
+ this.cid = cid;
22
+ this.carEntry = carEntry;
23
+ }
24
+ /**
25
+ * raw contents of this record
26
+ */
27
+ get bytes() {
28
+ return this.carEntry.bytes;
29
+ }
30
+ /**
31
+ * decoded contents of this record
32
+ */
33
+ get record() {
34
+ return CBOR.decode(this.bytes);
35
+ }
36
+ }
37
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/v4/repo-reader/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,GAAG,MAAM,aAAa,CAAC;AAEnC,OAAO,EAAiB,MAAM,wBAAwB,CAAC;AAEvD,MAAM,OAAO,SAAS;IAIJ;IAEA;IAEA;IAEA;IATjB,gBAAgB;IAChB;IACC,4CAA4C;IAC5B,UAAkB;IAClC,iBAAiB;IACD,IAAY;IAC5B,yBAAyB;IACT,GAAgB;IAChC,8CAA8C;IAC9B,QAAkB;QANlB,eAAU,GAAV,UAAU,CAAQ;QAElB,SAAI,GAAJ,IAAI,CAAQ;QAEZ,QAAG,GAAH,GAAG,CAAa;QAEhB,aAAQ,GAAR,QAAQ,CAAU;IAChC,CAAC;IAEJ;;OAEG;IACH,IAAI,KAAK;QACR,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;IAC5B,CAAC;IAED;;OAEG;IACH,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC;CACD"}
@@ -0,0 +1,3 @@
1
+ export declare const assert: {
2
+ (condition: boolean, message: string): asserts condition;
3
+ };
@@ -0,0 +1,6 @@
1
+ export const assert = (condition, message) => {
2
+ if (!condition) {
3
+ throw new Error(message);
4
+ }
5
+ };
6
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../lib/v4/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,MAAM,GAEf,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE;IAC1B,IAAI,CAAC,SAAS,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC;AACF,CAAC,CAAC"}
@@ -0,0 +1,15 @@
1
+ export {
2
+ collectBlock,
3
+ isCommit,
4
+ isMstNode,
5
+ isTreeEntry,
6
+ fromUint8Array as iterateAtpRepo,
7
+ readBlock,
8
+ RepoEntry,
9
+ walkMstEntries,
10
+ type BlockMap,
11
+ type Commit,
12
+ type MstNode,
13
+ type NodeEntry,
14
+ type TreeEntry,
15
+ } from '../v4/repo-reader/index.js';
@@ -0,0 +1,9 @@
1
+ export {
2
+ isCarV1Header,
3
+ fromUint8Array as readCar,
4
+ type CarEntry,
5
+ type CarHeader,
6
+ type CarV1Header,
7
+ type StreamedCarReader,
8
+ type SyncCarReader,
9
+ } from '../v4/car-reader/index.js';
@@ -0,0 +1,4 @@
1
+ export * from './types.js';
2
+
3
+ export * from './stream-car-reader.js';
4
+ export * from './sync-car-reader.js';
@@ -0,0 +1,240 @@
1
+ import * as CBOR from '@atcute/cbor';
2
+ import * as CID from '@atcute/cid';
3
+ import { concat } from '@atcute/uint8array';
4
+
5
+ import { isCarV1Header, type CarEntry, type CarHeader } from './types.js';
6
+
7
+ export interface StreamedCarReader {
8
+ header(): Promise<CarHeader>;
9
+ roots(): Promise<CBOR.CidLink[]>;
10
+
11
+ dispose(): Promise<void>;
12
+
13
+ [Symbol.asyncDispose](): Promise<void>;
14
+ [Symbol.asyncIterator](): AsyncIterator<CarEntry>;
15
+ }
16
+
17
+ export const carEntryTransform = (): ReadableWritablePair<CarEntry, Uint8Array> => {
18
+ const transform = new TransformStream<Uint8Array, Uint8Array>();
19
+ let car: StreamedCarReader | undefined;
20
+
21
+ return {
22
+ readable: new ReadableStream({
23
+ async start(controller) {
24
+ car = fromStream(transform.readable);
25
+
26
+ try {
27
+ for await (const entry of car) {
28
+ controller.enqueue(entry);
29
+ }
30
+
31
+ await car.dispose();
32
+
33
+ controller.close();
34
+ } catch (err) {
35
+ controller.error(err);
36
+ }
37
+ },
38
+ async cancel() {
39
+ if (car !== undefined) {
40
+ await car.dispose();
41
+ }
42
+ },
43
+ }),
44
+ writable: transform.writable,
45
+ };
46
+ };
47
+
48
+ export const fromStream = (stream: ReadableStream<Uint8Array>): StreamedCarReader => {
49
+ let chunk = new Uint8Array(0) as Uint8Array; // annoying!
50
+ let offset = 0;
51
+
52
+ let _header: CarHeader | undefined;
53
+
54
+ const reader = stream.getReader();
55
+
56
+ const readVarint = async (): Promise<number> => {
57
+ let value = 0;
58
+ let shift = 0;
59
+
60
+ const MSB = 0x80;
61
+ const REST = 0x7f;
62
+
63
+ while (true) {
64
+ if (chunk.length === 0) {
65
+ const { value, done } = await reader.read();
66
+ if (done) {
67
+ throw new Error(`unexpected eof while decoding varint`);
68
+ }
69
+
70
+ chunk = value;
71
+ }
72
+
73
+ const byte = chunk[0];
74
+ chunk = chunk.subarray(1);
75
+
76
+ value += shift < 28 ? (byte & REST) << shift : (byte & REST) * 2 ** shift;
77
+ shift += 7;
78
+
79
+ offset++;
80
+
81
+ if ((byte & MSB) === 0) {
82
+ return value;
83
+ }
84
+ }
85
+ };
86
+
87
+ const readExact = async (n: number): Promise<Uint8Array> => {
88
+ const buffer = new Uint8Array(n);
89
+ let written = 0;
90
+
91
+ while (written < n) {
92
+ if (chunk.length === 0) {
93
+ const { value, done } = await reader.read();
94
+
95
+ if (done) {
96
+ throw new Error('unexpected eof while reading data');
97
+ }
98
+
99
+ chunk = value;
100
+ }
101
+
102
+ const taken = Math.min(n - written, chunk.length);
103
+ buffer.set(chunk.subarray(0, taken), written);
104
+
105
+ written += taken;
106
+ chunk = chunk.subarray(taken);
107
+ }
108
+
109
+ offset += n;
110
+ return buffer;
111
+ };
112
+
113
+ const readCid = async (): Promise<CID.Cid> => {
114
+ const head = await readExact(4);
115
+
116
+ const version = head[0];
117
+ const codec = head[1];
118
+ const digestType = head[2];
119
+ const digestSize = head[3];
120
+
121
+ if (version !== CID.CID_VERSION) {
122
+ throw new RangeError(`incorrect cid version (got v${version})`);
123
+ }
124
+
125
+ if (codec !== CID.CODEC_DCBOR && codec !== CID.CODEC_RAW) {
126
+ throw new RangeError(`incorrect cid codec (got 0x${codec.toString(16)})`);
127
+ }
128
+
129
+ if (digestType !== CID.HASH_SHA256) {
130
+ throw new RangeError(`incorrect cid digest type (got 0x${digestType.toString(16)})`);
131
+ }
132
+
133
+ if (digestSize !== 32 && digestSize !== 0) {
134
+ throw new RangeError(`incorrect cid digest size (got ${digestSize})`);
135
+ }
136
+
137
+ // concatenate and have digest refer back to this buffer
138
+ const bytes = concat([head, await readExact(digestSize)]);
139
+ const digest = bytes.subarray(4, 4 + digestSize);
140
+
141
+ const cid: CID.Cid = {
142
+ version: version,
143
+ codec: codec,
144
+ digest: {
145
+ codec: digestType,
146
+ contents: digest,
147
+ },
148
+ bytes: bytes,
149
+ };
150
+
151
+ return cid;
152
+ };
153
+
154
+ return {
155
+ [Symbol.asyncDispose]() {
156
+ return this.dispose();
157
+ },
158
+
159
+ async dispose() {
160
+ await reader.cancel();
161
+ },
162
+
163
+ async header(): Promise<CarHeader> {
164
+ if (_header !== undefined) {
165
+ return _header;
166
+ }
167
+
168
+ const headerStart = offset;
169
+
170
+ const headerSize = await readVarint();
171
+ if (headerSize === 0) {
172
+ throw new RangeError(`invalid car header; length=0`);
173
+ }
174
+
175
+ const dataStart = offset;
176
+
177
+ const raw = await readExact(headerSize);
178
+ const data = CBOR.decode(raw);
179
+ if (!isCarV1Header(data)) {
180
+ throw new TypeError(`expected a car v1 archive`);
181
+ }
182
+
183
+ const dataEnd = offset;
184
+ const headerEnd = offset;
185
+
186
+ return (_header = { data, headerStart, headerEnd, dataStart, dataEnd });
187
+ },
188
+
189
+ async roots(): Promise<CBOR.CidLink[]> {
190
+ const header = await this.header();
191
+
192
+ return header.data.roots;
193
+ },
194
+
195
+ async *[Symbol.asyncIterator](): AsyncGenerator<CarEntry> {
196
+ // ensure the header is read first
197
+ if (_header === undefined) {
198
+ await this.header();
199
+ }
200
+
201
+ while (true) {
202
+ if (chunk.length === 0) {
203
+ const { value, done } = await reader.read();
204
+
205
+ if (done) {
206
+ return;
207
+ }
208
+
209
+ chunk = value;
210
+ }
211
+
212
+ const entryStart = offset;
213
+ const entrySize = await readVarint();
214
+
215
+ const cidStart = offset;
216
+ const cid = await readCid();
217
+
218
+ const bytesStart = offset;
219
+ const bytesSize = entrySize - (bytesStart - cidStart);
220
+ const bytes = await readExact(bytesSize);
221
+
222
+ const cidEnd = bytesStart;
223
+ const bytesEnd = offset;
224
+ const entryEnd = bytesEnd;
225
+
226
+ yield {
227
+ cid,
228
+ bytes,
229
+
230
+ entryStart,
231
+ entryEnd,
232
+ cidStart,
233
+ cidEnd,
234
+ bytesStart,
235
+ bytesEnd,
236
+ };
237
+ }
238
+ },
239
+ };
240
+ };