@atcute/car 3.1.2 → 5.0.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 (95) hide show
  1. package/README.md +16 -48
  2. package/dist/index.d.ts +5 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +5 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/{v4/car-reader/sync-car-reader.d.ts → reader.d.ts} +3 -3
  7. package/dist/reader.d.ts.map +1 -0
  8. package/dist/{v4/car-reader/sync-car-reader.js → reader.js} +1 -1
  9. package/dist/reader.js.map +1 -0
  10. package/dist/{v4/car-reader/stream-car-reader.d.ts → streamed-reader.d.ts} +3 -3
  11. package/dist/streamed-reader.d.ts.map +1 -0
  12. package/dist/{v4/car-reader/stream-car-reader.js → streamed-reader.js} +1 -1
  13. package/dist/streamed-reader.js.map +1 -0
  14. package/dist/{v4/car-reader/types.d.ts → types.d.ts} +12 -3
  15. package/dist/types.d.ts.map +1 -0
  16. package/dist/{v4/car-reader/types.js → types.js} +2 -3
  17. package/dist/types.js.map +1 -0
  18. package/dist/utils.d.ts.map +1 -0
  19. package/dist/utils.js.map +1 -0
  20. package/dist/writer.d.ts +26 -0
  21. package/dist/writer.d.ts.map +1 -0
  22. package/dist/writer.js +77 -0
  23. package/dist/writer.js.map +1 -0
  24. package/lib/index.ts +6 -0
  25. package/lib/{v4/car-reader/sync-car-reader.ts → reader.ts} +2 -1
  26. package/lib/{v4/car-reader/stream-car-reader.ts → streamed-reader.ts} +5 -4
  27. package/lib/{v4/car-reader/types.ts → types.ts} +14 -5
  28. package/lib/writer.ts +92 -0
  29. package/package.json +8 -11
  30. package/dist/v3/atproto-repo.d.ts +0 -2
  31. package/dist/v3/atproto-repo.d.ts.map +0 -1
  32. package/dist/v3/atproto-repo.js +0 -2
  33. package/dist/v3/atproto-repo.js.map +0 -1
  34. package/dist/v3/index.d.ts +0 -3
  35. package/dist/v3/index.d.ts.map +0 -1
  36. package/dist/v3/index.js +0 -3
  37. package/dist/v3/index.js.map +0 -1
  38. package/dist/v3/reader.d.ts +0 -2
  39. package/dist/v3/reader.d.ts.map +0 -1
  40. package/dist/v3/reader.js +0 -2
  41. package/dist/v3/reader.js.map +0 -1
  42. package/dist/v4/car-reader/index.d.ts +0 -4
  43. package/dist/v4/car-reader/index.d.ts.map +0 -1
  44. package/dist/v4/car-reader/index.js +0 -4
  45. package/dist/v4/car-reader/index.js.map +0 -1
  46. package/dist/v4/car-reader/stream-car-reader.d.ts.map +0 -1
  47. package/dist/v4/car-reader/stream-car-reader.js.map +0 -1
  48. package/dist/v4/car-reader/sync-car-reader.d.ts.map +0 -1
  49. package/dist/v4/car-reader/sync-car-reader.js.map +0 -1
  50. package/dist/v4/car-reader/types.d.ts.map +0 -1
  51. package/dist/v4/car-reader/types.js.map +0 -1
  52. package/dist/v4/index.d.ts +0 -3
  53. package/dist/v4/index.d.ts.map +0 -1
  54. package/dist/v4/index.js +0 -3
  55. package/dist/v4/index.js.map +0 -1
  56. package/dist/v4/repo-reader/index.d.ts +0 -6
  57. package/dist/v4/repo-reader/index.d.ts.map +0 -1
  58. package/dist/v4/repo-reader/index.js +0 -6
  59. package/dist/v4/repo-reader/index.js.map +0 -1
  60. package/dist/v4/repo-reader/mst.d.ts +0 -48
  61. package/dist/v4/repo-reader/mst.d.ts.map +0 -1
  62. package/dist/v4/repo-reader/mst.js +0 -62
  63. package/dist/v4/repo-reader/mst.js.map +0 -1
  64. package/dist/v4/repo-reader/stream-repo-reader.d.ts +0 -25
  65. package/dist/v4/repo-reader/stream-repo-reader.d.ts.map +0 -1
  66. package/dist/v4/repo-reader/stream-repo-reader.js +0 -143
  67. package/dist/v4/repo-reader/stream-repo-reader.js.map +0 -1
  68. package/dist/v4/repo-reader/sync-blockmap.d.ts +0 -30
  69. package/dist/v4/repo-reader/sync-blockmap.d.ts.map +0 -1
  70. package/dist/v4/repo-reader/sync-blockmap.js +0 -58
  71. package/dist/v4/repo-reader/sync-blockmap.js.map +0 -1
  72. package/dist/v4/repo-reader/sync-repo-reader.d.ts +0 -3
  73. package/dist/v4/repo-reader/sync-repo-reader.d.ts.map +0 -1
  74. package/dist/v4/repo-reader/sync-repo-reader.js +0 -20
  75. package/dist/v4/repo-reader/sync-repo-reader.js.map +0 -1
  76. package/dist/v4/repo-reader/types.d.ts +0 -21
  77. package/dist/v4/repo-reader/types.d.ts.map +0 -1
  78. package/dist/v4/repo-reader/types.js +0 -37
  79. package/dist/v4/repo-reader/types.js.map +0 -1
  80. package/dist/v4/utils.d.ts.map +0 -1
  81. package/dist/v4/utils.js.map +0 -1
  82. package/lib/v3/atproto-repo.ts +0 -15
  83. package/lib/v3/index.ts +0 -2
  84. package/lib/v3/reader.ts +0 -9
  85. package/lib/v4/car-reader/index.ts +0 -4
  86. package/lib/v4/index.ts +0 -2
  87. package/lib/v4/repo-reader/index.ts +0 -6
  88. package/lib/v4/repo-reader/mst.ts +0 -110
  89. package/lib/v4/repo-reader/stream-repo-reader.ts +0 -209
  90. package/lib/v4/repo-reader/sync-blockmap.ts +0 -85
  91. package/lib/v4/repo-reader/sync-repo-reader.ts +0 -27
  92. package/lib/v4/repo-reader/types.ts +0 -32
  93. /package/dist/{v4/utils.d.ts → utils.d.ts} +0 -0
  94. /package/dist/{v4/utils.js → utils.js} +0 -0
  95. /package/lib/{v4/utils.ts → utils.ts} +0 -0
@@ -1,37 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../lib/v4/utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM,EAAE;IACpB,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;CAKzD,CAAC"}
@@ -1 +0,0 @@
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"}
@@ -1,15 +0,0 @@
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';
package/lib/v3/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './atproto-repo.js';
2
- export * from './reader.js';
package/lib/v3/reader.ts DELETED
@@ -1,9 +0,0 @@
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';
@@ -1,4 +0,0 @@
1
- export * from './types.js';
2
-
3
- export * from './stream-car-reader.js';
4
- export * from './sync-car-reader.js';
package/lib/v4/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * as CarReader from './car-reader/index.js';
2
- export * as RepoReader from './repo-reader/index.js';
@@ -1,6 +0,0 @@
1
- export * from './types.js';
2
-
3
- export * from './mst.js';
4
- export * from './stream-repo-reader.js';
5
- export * from './sync-blockmap.js';
6
- export * from './sync-repo-reader.js';
@@ -1,110 +0,0 @@
1
- import * as CBOR from '@atcute/cbor';
2
- import * as CID from '@atcute/cid';
3
-
4
- const isCidLink = (value: unknown): value is CID.CidLink => {
5
- if (value instanceof CID.CidLinkWrapper) {
6
- return true;
7
- }
8
-
9
- if (value === null || typeof value !== 'object') {
10
- return false;
11
- }
12
-
13
- return '$link' in value && typeof value.$link === 'string';
14
- };
15
-
16
- const isBytes = (value: unknown): value is CBOR.Bytes => {
17
- if (value instanceof CBOR.BytesWrapper) {
18
- return true;
19
- }
20
-
21
- if (value === null || typeof value !== 'object') {
22
- return false;
23
- }
24
-
25
- return '$bytes' in value && typeof value.$bytes === 'string';
26
- };
27
-
28
- /** commit object */
29
- export interface Commit {
30
- version: 3;
31
- did: string;
32
- data: CID.CidLink;
33
- rev: string;
34
- prev: CID.CidLink | null;
35
- sig: CBOR.Bytes;
36
- }
37
-
38
- /**
39
- * checks if a value is a valid commit object
40
- * @param value the value to check
41
- * @returns true if the value is a valid commit object, false otherwise
42
- */
43
- export const isCommit = (value: unknown): value is Commit => {
44
- if (value === null || typeof value !== 'object') {
45
- return false;
46
- }
47
-
48
- const obj = value as Record<string, unknown>;
49
-
50
- return (
51
- obj.version === 3 &&
52
- typeof obj.did === 'string' &&
53
- isCidLink(obj.data) &&
54
- typeof obj.rev === 'string' &&
55
- (obj.prev === null || isCidLink(obj.prev)) &&
56
- isBytes(obj.sig)
57
- );
58
- };
59
-
60
- /** mst tree entry object */
61
- export interface TreeEntry {
62
- /** count of bytes shared with previous TreeEntry in this Node (if any) */
63
- p: number;
64
- /** remainder of key for this TreeEntry, after "prefixlen" have been removed */
65
- k: CBOR.Bytes;
66
- /** 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) */
67
- v: CID.CidLink;
68
- /** next subtree (to the right of leaf) */
69
- t: CID.CidLink | null;
70
- }
71
-
72
- /**
73
- * checks if a value is a valid mst tree entry object
74
- * @param value the value to check
75
- * @returns true if the value is a valid mst tree entry object, false otherwise
76
- */
77
- export const isTreeEntry = (value: unknown): value is TreeEntry => {
78
- if (value === null || typeof value !== 'object') {
79
- return false;
80
- }
81
-
82
- const obj = value as Record<string, unknown>;
83
-
84
- return (
85
- typeof obj.p === 'number' && isBytes(obj.k) && isCidLink(obj.v) && (obj.t === null || isCidLink(obj.t))
86
- );
87
- };
88
-
89
- /** mst node object */
90
- export interface MstNode {
91
- /** link to sub-tree Node on a lower level and with all keys sorting before keys at this node */
92
- l: CID.CidLink | null;
93
- /** ordered list of TreeEntry objects */
94
- e: TreeEntry[];
95
- }
96
-
97
- /**
98
- * checks if a value is a valid mst node object
99
- * @param value the value to check
100
- * @returns true if the value is a valid mst node object, false otherwise
101
- */
102
- export const isMstNode = (value: unknown): value is MstNode => {
103
- if (value === null || typeof value !== 'object') {
104
- return false;
105
- }
106
-
107
- const obj = value as Record<string, unknown>;
108
-
109
- return (obj.l === null || isCidLink(obj.l)) && Array.isArray(obj.e) && obj.e.every(isTreeEntry);
110
- };
@@ -1,209 +0,0 @@
1
- import Queue from 'yocto-queue';
2
-
3
- import * as CBOR from '@atcute/cbor';
4
- import * as CID from '@atcute/cid';
5
- import { decodeUtf8From } from '@atcute/uint8array';
6
-
7
- import * as CarReader from '../car-reader/index.js';
8
- import { assert } from '../utils.js';
9
-
10
- import { isCommit, isMstNode } from './mst.js';
11
- import { RepoEntry } from './types.js';
12
-
13
- type EntryMeta = { t: 0 } | { t: 1 } | { t: 2; k: string };
14
-
15
- type Task = {
16
- c: string;
17
- e: CarReader.CarEntry;
18
- m: EntryMeta;
19
- };
20
-
21
- export type MissingBlockEntry =
22
- | {
23
- cid: string;
24
- type: 'record';
25
- key: string;
26
- }
27
- | {
28
- cid: string;
29
- type: 'mst-node';
30
- }
31
- | {
32
- cid: string;
33
- type: 'commit';
34
- };
35
-
36
- export interface StreamedRepoReader {
37
- /**
38
- * list of blocks that were referenced but not found in the repository.
39
- * blocks may be reported as missing if multiple records share the same CID.
40
- */
41
- readonly missingBlocks: readonly MissingBlockEntry[];
42
-
43
- dispose(): Promise<void>;
44
-
45
- [Symbol.asyncDispose](): Promise<void>;
46
- [Symbol.asyncIterator](): AsyncIterator<RepoEntry>;
47
- }
48
-
49
- export const repoEntryTransform = (): ReadableWritablePair<RepoEntry, Uint8Array> => {
50
- const transform = new TransformStream<Uint8Array, Uint8Array>();
51
- let repo: StreamedRepoReader | undefined;
52
-
53
- return {
54
- readable: new ReadableStream({
55
- async start(controller) {
56
- repo = fromStream(transform.readable);
57
-
58
- try {
59
- for await (const entry of repo) {
60
- controller.enqueue(entry);
61
- }
62
-
63
- await repo.dispose();
64
-
65
- controller.close();
66
- } catch (err) {
67
- controller.error(err);
68
- }
69
- },
70
- async cancel() {
71
- if (repo !== undefined) {
72
- await repo.dispose();
73
- }
74
- },
75
- }),
76
- writable: transform.writable,
77
- };
78
- };
79
-
80
- export const fromStream = (stream: ReadableStream<Uint8Array>): StreamedRepoReader => {
81
- let missingBlocks: MissingBlockEntry[] = [];
82
-
83
- return {
84
- get missingBlocks() {
85
- return missingBlocks;
86
- },
87
-
88
- async dispose() {
89
- // does nothing for now
90
- },
91
-
92
- [Symbol.asyncDispose]() {
93
- return this.dispose();
94
- },
95
- async *[Symbol.asyncIterator]() {
96
- // await using car = CarReader.fromStream(stream);
97
- const car = CarReader.fromStream(stream);
98
-
99
- try {
100
- const pending = new Map<string, EntryMeta>();
101
- const strays = new Map<string, CarReader.CarEntry>();
102
-
103
- const queue = new Queue<Task>();
104
-
105
- const request = (cid: string, meta: EntryMeta): void => {
106
- const entry = strays.get(cid);
107
-
108
- if (entry !== undefined) {
109
- strays.delete(cid);
110
- queue.enqueue({ c: cid, e: entry, m: meta });
111
- } else {
112
- pending.set(cid, meta);
113
- }
114
- };
115
-
116
- {
117
- const roots = await car.roots();
118
- assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
119
-
120
- const rootCid = roots[0].$link;
121
- request(rootCid, { t: 0 });
122
- }
123
-
124
- for await (const entry of car) {
125
- const cid = CID.toString(entry.cid);
126
-
127
- {
128
- const meta = pending.get(cid);
129
-
130
- if (meta !== undefined) {
131
- pending.delete(cid);
132
- queue.enqueue({ c: cid, e: entry, m: meta });
133
- } else {
134
- strays.set(cid, entry);
135
- }
136
- }
137
-
138
- let task: Task | undefined;
139
- while ((task = queue.dequeue())) {
140
- const { c: cid, e: entry, m: meta } = task;
141
-
142
- switch (meta.t) {
143
- case 0: {
144
- const commit = CBOR.decode(entry.bytes);
145
- assert(isCommit(commit), `expected commit block; cid=${cid}`);
146
-
147
- request(commit.data.$link, { t: 1 });
148
- break;
149
- }
150
- case 1: {
151
- const node = CBOR.decode(entry.bytes);
152
- assert(isMstNode(node), `expected mst node block; cid=${cid}`);
153
-
154
- const entries = node.e;
155
- const left = node.l;
156
-
157
- let lastKey = '';
158
-
159
- if (left !== null) {
160
- request(left.$link, meta);
161
- }
162
-
163
- for (let i = 0, il = entries.length; i < il; i++) {
164
- const entry = entries[i];
165
- const next = entry.t;
166
-
167
- const key_str = decodeUtf8From(CBOR.fromBytes(entry.k));
168
- const key = lastKey.slice(0, entry.p) + key_str;
169
-
170
- lastKey = key;
171
-
172
- request(entry.v.$link, { t: 2, k: key });
173
-
174
- if (next !== null) {
175
- request(next.$link, { t: 1 });
176
- }
177
- }
178
-
179
- break;
180
- }
181
- case 2: {
182
- const [collection, rkey] = meta.k.split('/');
183
-
184
- yield new RepoEntry(collection, rkey, CID.toCidLink(entry.cid), entry);
185
- break;
186
- }
187
- }
188
- }
189
- }
190
-
191
- missingBlocks = Array.from(pending, ([cid, meta]): MissingBlockEntry => {
192
- switch (meta.t) {
193
- case 0: {
194
- return { cid, type: 'commit' };
195
- }
196
- case 1: {
197
- return { cid, type: 'mst-node' };
198
- }
199
- case 2: {
200
- return { cid, type: 'record', key: meta.k };
201
- }
202
- }
203
- });
204
- } finally {
205
- await car.dispose();
206
- }
207
- },
208
- };
209
- };
@@ -1,85 +0,0 @@
1
- import * as CBOR from '@atcute/cbor';
2
- import * as CID from '@atcute/cid';
3
- import { decodeUtf8From } from '@atcute/uint8array';
4
-
5
- import * as CarReader from '../car-reader/index.js';
6
- import { assert } from '../utils.js';
7
-
8
- import { isMstNode } from './mst.js';
9
-
10
- export type BlockMap = Map<string, CarReader.CarEntry>;
11
-
12
- /**
13
- * collects entries from a CAR archive into a mapping of CID string -> actual bytes
14
- * @param iterator a generator that yields objects with a `cid` and `bytes` property
15
- * @returns a mapping of CID string -> actual bytes
16
- */
17
- export const collectBlock = (iterator: Iterable<CarReader.CarEntry>): BlockMap => {
18
- const blockmap: BlockMap = new Map();
19
- for (const entry of iterator) {
20
- blockmap.set(CID.toString(entry.cid), entry);
21
- }
22
-
23
- return blockmap;
24
- };
25
-
26
- /**
27
- * reads a block from the blockmap and validates it against the provided validation function
28
- * @param map a mapping of CID string -> actual bytes
29
- * @param link a CID link to read
30
- * @param validate a validation function to validate the decoded data
31
- * @returns the decoded and validated data
32
- */
33
- export const readBlock = <T>(
34
- map: BlockMap,
35
- link: CID.CidLink,
36
- validate: (value: unknown) => value is T,
37
- ): T => {
38
- const cid = link.$link;
39
-
40
- const entry = map.get(cid);
41
- assert(entry != null, `cid not found in blockmap; cid=${cid}`);
42
-
43
- const data = CBOR.decode(entry.bytes);
44
- assert(validate(data), `validation failed for cid=${cid}`);
45
-
46
- return data;
47
- };
48
-
49
- /** node entry object */
50
- export interface NodeEntry {
51
- key: string;
52
- cid: CID.CidLink;
53
- }
54
-
55
- /**
56
- * walks the entries of a Merkle Sorted Tree (MST) in a depth-first manner
57
- * @param map a mapping of CID string -> actual bytes
58
- * @param pointer a CID link to the root of the MST
59
- * @returns a generator that yields the entries of the MST
60
- */
61
- export function* walkMstEntries(map: BlockMap, pointer: CID.CidLink): Generator<NodeEntry> {
62
- const data = readBlock(map, pointer, isMstNode);
63
- const entries = data.e;
64
-
65
- let lastKey = '';
66
-
67
- if (data.l !== null) {
68
- yield* walkMstEntries(map, data.l);
69
- }
70
-
71
- for (let i = 0, il = entries.length; i < il; i++) {
72
- const entry = entries[i];
73
-
74
- const key_str = decodeUtf8From(CBOR.fromBytes(entry.k));
75
- const key = lastKey.slice(0, entry.p) + key_str;
76
-
77
- lastKey = key;
78
-
79
- yield { key: key, cid: entry.v };
80
-
81
- if (entry.t !== null) {
82
- yield* walkMstEntries(map, entry.t);
83
- }
84
- }
85
- }
@@ -1,27 +0,0 @@
1
- import * as CarReader from '../car-reader/index.js';
2
- import { assert } from '../utils.js';
3
-
4
- import { isCommit } from './mst.js';
5
- import { collectBlock, readBlock, walkMstEntries } from './sync-blockmap.js';
6
- import { RepoEntry } from './types.js';
7
-
8
- export function* fromUint8Array(buf: Uint8Array): Generator<RepoEntry> {
9
- const car = CarReader.fromUint8Array(buf);
10
- const roots = car.roots;
11
-
12
- assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
13
-
14
- const blockmap = collectBlock(car);
15
- assert(blockmap.size > 0, `expected at least 1 block in the archive; got=${blockmap.size}`);
16
-
17
- const commit = readBlock(blockmap, roots[0], isCommit);
18
-
19
- for (const { key, cid } of walkMstEntries(blockmap, commit.data)) {
20
- const [collection, rkey] = key.split('/');
21
-
22
- const carEntry = blockmap.get(cid.$link);
23
- assert(carEntry != null, `cid not found in blockmap; cid=${cid}`);
24
-
25
- yield new RepoEntry(collection, rkey, cid, carEntry);
26
- }
27
- }
@@ -1,32 +0,0 @@
1
- import * as CBOR from '@atcute/cbor';
2
- import * as CID from '@atcute/cid';
3
-
4
- import { type CarEntry } from '../car-reader/index.js';
5
-
6
- export class RepoEntry {
7
- /** @internal */
8
- constructor(
9
- /** the collection this record belongs to */
10
- public readonly collection: string,
11
- /** record key */
12
- public readonly rkey: string,
13
- /** CID of this record */
14
- public readonly cid: CID.CidLink,
15
- /** the associated CarEntry for this record */
16
- public readonly carEntry: CarEntry,
17
- ) {}
18
-
19
- /**
20
- * raw contents of this record
21
- */
22
- get bytes(): Uint8Array {
23
- return this.carEntry.bytes;
24
- }
25
-
26
- /**
27
- * decoded contents of this record
28
- */
29
- get record(): unknown {
30
- return CBOR.decode(this.bytes);
31
- }
32
- }
File without changes
File without changes
File without changes