@atcute/car 2.0.2 → 2.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.
@@ -1,3 +1,4 @@
1
+ import * as CBOR from '@atcute/cbor';
1
2
  import * as CID from '@atcute/cid';
2
3
  export declare class RepoEntry {
3
4
  readonly collection: string;
@@ -9,5 +10,78 @@ export declare class RepoEntry {
9
10
  get record(): unknown;
10
11
  }
11
12
  export declare function iterateAtpRepo(buf: Uint8Array): Generator<RepoEntry>;
12
- type BlockMap = Map<string, Uint8Array>;
13
- export {};
13
+ /**
14
+ * collects entries from a CAR archive into a mapping of CID string -> actual bytes
15
+ * @param iterator a generator that yields objects with a `cid` and `bytes` property
16
+ * @returns a mapping of CID string -> actual bytes
17
+ */
18
+ export declare function collectBlock(iterator: Generator<{
19
+ cid: CID.Cid;
20
+ bytes: Uint8Array;
21
+ }>): BlockMap;
22
+ /**
23
+ * reads a block from the blockmap and validates it against the provided validation function
24
+ * @param map a mapping of CID string -> actual bytes
25
+ * @param link a CID link to read
26
+ * @param validate a validation function to validate the decoded data
27
+ * @returns the decoded and validated data
28
+ */
29
+ export declare function readBlock<T>(map: BlockMap, link: CID.CidLink, validate: (value: unknown) => value is T): T;
30
+ /** node entry object */
31
+ export interface NodeEntry {
32
+ key: string;
33
+ cid: CID.CidLink;
34
+ }
35
+ /**
36
+ * walks the entries of a Merkle Sorted Tree (MST) in a depth-first manner
37
+ * @param map a mapping of CID string -> actual bytes
38
+ * @param pointer a CID link to the root of the MST
39
+ * @returns a generator that yields the entries of the MST
40
+ */
41
+ export declare function walkMstEntries(map: BlockMap, pointer: CID.CidLink): Generator<NodeEntry>;
42
+ export type BlockMap = Map<string, Uint8Array>;
43
+ /** commit object */
44
+ export interface Commit {
45
+ version: 3;
46
+ did: string;
47
+ data: CID.CidLink;
48
+ rev: string;
49
+ prev: CID.CidLink | null;
50
+ sig: CBOR.Bytes;
51
+ }
52
+ /**
53
+ * checks if a value is a valid commit object
54
+ * @param value the value to check
55
+ * @returns true if the value is a valid commit object, false otherwise
56
+ */
57
+ export declare const isCommit: (value: unknown) => value is Commit;
58
+ /** mst tree entry object */
59
+ export interface TreeEntry {
60
+ /** count of bytes shared with previous TreeEntry in this Node (if any) */
61
+ p: number;
62
+ /** remainder of key for this TreeEntry, after "prefixlen" have been removed */
63
+ k: CBOR.Bytes;
64
+ /** 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) */
65
+ v: CID.CidLink;
66
+ /** next subtree (to the right of leaf) */
67
+ t: CID.CidLink | null;
68
+ }
69
+ /**
70
+ * checks if a value is a valid mst tree entry object
71
+ * @param value the value to check
72
+ * @returns true if the value is a valid mst tree entry object, false otherwise
73
+ */
74
+ export declare const isTreeEntry: (value: unknown) => value is TreeEntry;
75
+ /** mst node object */
76
+ export interface MstNode {
77
+ /** link to sub-tree Node on a lower level and with all keys sorting before keys at this node */
78
+ l: CID.CidLink | null;
79
+ /** ordered list of TreeEntry objects */
80
+ e: TreeEntry[];
81
+ }
82
+ /**
83
+ * checks if a value is a valid mst node object
84
+ * @param value the value to check
85
+ * @returns true if the value is a valid mst node object, false otherwise
86
+ */
87
+ export declare const isMstNode: (value: unknown) => value is MstNode;
@@ -26,31 +26,53 @@ export class RepoEntry {
26
26
  export function* iterateAtpRepo(buf) {
27
27
  const { roots, iterate } = readCar(buf);
28
28
  assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
29
- // Collect all archive entries into a mapping of CID string -> actual bytes
30
- const blockmap = new Map();
31
- for (const entry of iterate()) {
32
- blockmap.set(CID.toString(entry.cid), entry.bytes);
33
- }
34
- // Read the head, then walk through the MST tree from there.
35
- const commit = readObject(blockmap, roots[0]);
36
- for (const { key, cid } of walkEntries(blockmap, commit.data)) {
29
+ const blockmap = collectBlock(iterate());
30
+ assert(blockmap.size > 0, `expected at least 1 block in the archive; got=${blockmap.size}`);
31
+ const commit = readBlock(blockmap, roots[0], isCommit);
32
+ for (const { key, cid } of walkMstEntries(blockmap, commit.data)) {
37
33
  const [collection, rkey] = key.split('/');
38
34
  yield new RepoEntry(collection, rkey, cid, blockmap);
39
35
  }
40
36
  }
41
- function readObject(map, link) {
37
+ /**
38
+ * collects entries from a CAR archive into a mapping of CID string -> actual bytes
39
+ * @param iterator a generator that yields objects with a `cid` and `bytes` property
40
+ * @returns a mapping of CID string -> actual bytes
41
+ */
42
+ export function collectBlock(iterator) {
43
+ const blockmap = new Map();
44
+ for (const { cid, bytes } of iterator) {
45
+ blockmap.set(CID.toString(cid), bytes);
46
+ }
47
+ return blockmap;
48
+ }
49
+ /**
50
+ * reads a block from the blockmap and validates it against the provided validation function
51
+ * @param map a mapping of CID string -> actual bytes
52
+ * @param link a CID link to read
53
+ * @param validate a validation function to validate the decoded data
54
+ * @returns the decoded and validated data
55
+ */
56
+ export function readBlock(map, link, validate) {
42
57
  const cid = link.$link;
43
58
  const bytes = map.get(cid);
44
59
  assert(bytes != null, `cid not found in blockmap; cid=${cid}`);
45
60
  const data = CBOR.decode(bytes);
61
+ assert(validate(data), `validation failed for cid=${cid}`);
46
62
  return data;
47
63
  }
48
- function* walkEntries(map, pointer) {
49
- const data = readObject(map, pointer);
64
+ /**
65
+ * walks the entries of a Merkle Sorted Tree (MST) in a depth-first manner
66
+ * @param map a mapping of CID string -> actual bytes
67
+ * @param pointer a CID link to the root of the MST
68
+ * @returns a generator that yields the entries of the MST
69
+ */
70
+ export function* walkMstEntries(map, pointer) {
71
+ const data = readBlock(map, pointer, isMstNode);
50
72
  const entries = data.e;
51
73
  let lastKey = '';
52
74
  if (data.l !== null) {
53
- yield* walkEntries(map, data.l);
75
+ yield* walkMstEntries(map, data.l);
54
76
  }
55
77
  for (let i = 0, il = entries.length; i < il; i++) {
56
78
  const entry = entries[i];
@@ -59,7 +81,7 @@ function* walkEntries(map, pointer) {
59
81
  lastKey = key;
60
82
  yield { key: key, cid: entry.v };
61
83
  if (entry.t !== null) {
62
- yield* walkEntries(map, entry.t);
84
+ yield* walkMstEntries(map, entry.t);
63
85
  }
64
86
  }
65
87
  }
@@ -68,4 +90,63 @@ function assert(condition, message) {
68
90
  throw new Error(message);
69
91
  }
70
92
  }
93
+ const isCidLink = (value) => {
94
+ if (value instanceof CID.CidLinkWrapper) {
95
+ return true;
96
+ }
97
+ if (value === null || typeof value !== 'object') {
98
+ return false;
99
+ }
100
+ return '$link' in value && typeof value.$link === 'string';
101
+ };
102
+ const isBytes = (value) => {
103
+ if (value instanceof CBOR.BytesWrapper) {
104
+ return true;
105
+ }
106
+ if (value === null || typeof value !== 'object') {
107
+ return false;
108
+ }
109
+ return '$bytes' in value && typeof value.$bytes === 'string';
110
+ };
111
+ /**
112
+ * checks if a value is a valid commit object
113
+ * @param value the value to check
114
+ * @returns true if the value is a valid commit object, false otherwise
115
+ */
116
+ export const isCommit = (value) => {
117
+ if (value === null || typeof value !== 'object') {
118
+ return false;
119
+ }
120
+ const obj = value;
121
+ return (obj.version === 3 &&
122
+ typeof obj.did === 'string' &&
123
+ isCidLink(obj.data) &&
124
+ typeof obj.rev === 'string' &&
125
+ (obj.prev === null || isCidLink(obj.prev)) &&
126
+ isBytes(obj.sig));
127
+ };
128
+ /**
129
+ * checks if a value is a valid mst tree entry object
130
+ * @param value the value to check
131
+ * @returns true if the value is a valid mst tree entry object, false otherwise
132
+ */
133
+ export const isTreeEntry = (value) => {
134
+ if (value === null || typeof value !== 'object') {
135
+ return false;
136
+ }
137
+ const obj = value;
138
+ return (typeof obj.p === 'number' && isBytes(obj.k) && isCidLink(obj.v) && (obj.t === null || isCidLink(obj.t)));
139
+ };
140
+ /**
141
+ * checks if a value is a valid mst node object
142
+ * @param value the value to check
143
+ * @returns true if the value is a valid mst node object, false otherwise
144
+ */
145
+ export const isMstNode = (value) => {
146
+ if (value === null || typeof value !== 'object') {
147
+ return false;
148
+ }
149
+ const obj = value;
150
+ return (obj.l === null || isCidLink(obj.l)) && Array.isArray(obj.e) && obj.e.every(isTreeEntry);
151
+ };
71
152
  //# sourceMappingURL=atproto-repo.js.map
@@ -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,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,KAAK;QACR,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;QAE3B,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACrC,MAAM,CAAC,KAAK,IAAI,IAAI,EAAE,kCAAkC,GAAG,EAAE,CAAC,CAAC;QAE/D,OAAO,KAAK,CAAC;IACd,CAAC;IAED,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC;CACD;AAED,MAAM,SAAS,CAAC,CAAC,cAAc,CAAC,GAAe;IAC9C,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACxC,MAAM,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,gDAAgD,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;IAE3F,2EAA2E;IAC3E,MAAM,QAAQ,GAAa,IAAI,GAAG,EAAE,CAAC;IACrC,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AC,CAAC,KAAK,IAAI,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CACvG,CAAC;AACH,CAAC,CAAC;AAUF;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAc,EAAoB,EAAE;IAC7D,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACjD,OAAO,KAAK,CAAC;IACd,CAAC;IAED,MAAM,GAAG,GAAG,KAAgC,CAAC;IAE7C,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACjG,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { iterateAtpRepo, type RepoEntry } from './atproto-repo.js';
2
- export { readCar } from './reader.js';
1
+ export * from './atproto-repo.js';
2
+ export * from './reader.js';
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { iterateAtpRepo } from './atproto-repo.js';
2
- export { readCar } from './reader.js';
1
+ export * from './atproto-repo.js';
2
+ export * 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,cAAc,EAAkB,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC"}
@@ -31,40 +31,70 @@ export function* iterateAtpRepo(buf: Uint8Array): Generator<RepoEntry> {
31
31
  const { roots, iterate } = readCar(buf);
32
32
  assert(roots.length === 1, `expected only 1 root in the car archive; got=${roots.length}`);
33
33
 
34
- // Collect all archive entries into a mapping of CID string -> actual bytes
35
- const blockmap: BlockMap = new Map();
36
- for (const entry of iterate()) {
37
- blockmap.set(CID.toString(entry.cid), entry.bytes);
38
- }
34
+ const blockmap = collectBlock(iterate());
35
+ assert(blockmap.size > 0, `expected at least 1 block in the archive; got=${blockmap.size}`);
39
36
 
40
- // Read the head, then walk through the MST tree from there.
41
- const commit = readObject(blockmap, roots[0]) as Commit;
42
- for (const { key, cid } of walkEntries(blockmap, commit.data)) {
37
+ const commit = readBlock(blockmap, roots[0], isCommit);
38
+ for (const { key, cid } of walkMstEntries(blockmap, commit.data)) {
43
39
  const [collection, rkey] = key.split('/');
44
40
 
45
41
  yield new RepoEntry(collection, rkey, cid, blockmap);
46
42
  }
47
43
  }
48
44
 
49
- function readObject(map: BlockMap, link: CID.CidLink): unknown {
45
+ /**
46
+ * collects entries from a CAR archive into a mapping of CID string -> actual bytes
47
+ * @param iterator a generator that yields objects with a `cid` and `bytes` property
48
+ * @returns a mapping of CID string -> actual bytes
49
+ */
50
+ export function collectBlock(iterator: Generator<{ cid: CID.Cid; bytes: Uint8Array }>): BlockMap {
51
+ const blockmap: BlockMap = new Map();
52
+ for (const { cid, bytes } of iterator) {
53
+ blockmap.set(CID.toString(cid), bytes);
54
+ }
55
+
56
+ return blockmap;
57
+ }
58
+
59
+ /**
60
+ * reads a block from the blockmap and validates it against the provided validation function
61
+ * @param map a mapping of CID string -> actual bytes
62
+ * @param link a CID link to read
63
+ * @param validate a validation function to validate the decoded data
64
+ * @returns the decoded and validated data
65
+ */
66
+ export function readBlock<T>(map: BlockMap, link: CID.CidLink, validate: (value: unknown) => value is T): T {
50
67
  const cid = link.$link;
51
68
 
52
69
  const bytes = map.get(cid);
53
70
  assert(bytes != null, `cid not found in blockmap; cid=${cid}`);
54
71
 
55
72
  const data = CBOR.decode(bytes);
73
+ assert(validate(data), `validation failed for cid=${cid}`);
56
74
 
57
75
  return data;
58
76
  }
59
77
 
60
- function* walkEntries(map: BlockMap, pointer: CID.CidLink): Generator<NodeEntry> {
61
- const data = readObject(map, pointer) as MstNode;
78
+ /** node entry object */
79
+ export interface NodeEntry {
80
+ key: string;
81
+ cid: CID.CidLink;
82
+ }
83
+
84
+ /**
85
+ * walks the entries of a Merkle Sorted Tree (MST) in a depth-first manner
86
+ * @param map a mapping of CID string -> actual bytes
87
+ * @param pointer a CID link to the root of the MST
88
+ * @returns a generator that yields the entries of the MST
89
+ */
90
+ export function* walkMstEntries(map: BlockMap, pointer: CID.CidLink): Generator<NodeEntry> {
91
+ const data = readBlock(map, pointer, isMstNode);
62
92
  const entries = data.e;
63
93
 
64
94
  let lastKey = '';
65
95
 
66
96
  if (data.l !== null) {
67
- yield* walkEntries(map, data.l);
97
+ yield* walkMstEntries(map, data.l);
68
98
  }
69
99
 
70
100
  for (let i = 0, il = entries.length; i < il; i++) {
@@ -78,7 +108,7 @@ function* walkEntries(map: BlockMap, pointer: CID.CidLink): Generator<NodeEntry>
78
108
  yield { key: key, cid: entry.v };
79
109
 
80
110
  if (entry.t !== null) {
81
- yield* walkEntries(map, entry.t);
111
+ yield* walkMstEntries(map, entry.t);
82
112
  }
83
113
  }
84
114
  }
@@ -89,9 +119,34 @@ function assert(condition: boolean, message: string): asserts condition {
89
119
  }
90
120
  }
91
121
 
92
- type BlockMap = Map<string, Uint8Array>;
122
+ export type BlockMap = Map<string, Uint8Array>;
123
+
124
+ const isCidLink = (value: unknown): value is CID.CidLink => {
125
+ if (value instanceof CID.CidLinkWrapper) {
126
+ return true;
127
+ }
128
+
129
+ if (value === null || typeof value !== 'object') {
130
+ return false;
131
+ }
132
+
133
+ return '$link' in value && typeof value.$link === 'string';
134
+ };
135
+
136
+ const isBytes = (value: unknown): value is CBOR.Bytes => {
137
+ if (value instanceof CBOR.BytesWrapper) {
138
+ return true;
139
+ }
93
140
 
94
- interface Commit {
141
+ if (value === null || typeof value !== 'object') {
142
+ return false;
143
+ }
144
+
145
+ return '$bytes' in value && typeof value.$bytes === 'string';
146
+ };
147
+
148
+ /** commit object */
149
+ export interface Commit {
95
150
  version: 3;
96
151
  did: string;
97
152
  data: CID.CidLink;
@@ -100,7 +155,30 @@ interface Commit {
100
155
  sig: CBOR.Bytes;
101
156
  }
102
157
 
103
- interface TreeEntry {
158
+ /**
159
+ * checks if a value is a valid commit object
160
+ * @param value the value to check
161
+ * @returns true if the value is a valid commit object, false otherwise
162
+ */
163
+ export const isCommit = (value: unknown): value is Commit => {
164
+ if (value === null || typeof value !== 'object') {
165
+ return false;
166
+ }
167
+
168
+ const obj = value as Record<string, unknown>;
169
+
170
+ return (
171
+ obj.version === 3 &&
172
+ typeof obj.did === 'string' &&
173
+ isCidLink(obj.data) &&
174
+ typeof obj.rev === 'string' &&
175
+ (obj.prev === null || isCidLink(obj.prev)) &&
176
+ isBytes(obj.sig)
177
+ );
178
+ };
179
+
180
+ /** mst tree entry object */
181
+ export interface TreeEntry {
104
182
  /** count of bytes shared with previous TreeEntry in this Node (if any) */
105
183
  p: number;
106
184
  /** remainder of key for this TreeEntry, after "prefixlen" have been removed */
@@ -111,14 +189,42 @@ interface TreeEntry {
111
189
  t: CID.CidLink | null;
112
190
  }
113
191
 
114
- interface MstNode {
192
+ /**
193
+ * checks if a value is a valid mst tree entry object
194
+ * @param value the value to check
195
+ * @returns true if the value is a valid mst tree entry object, false otherwise
196
+ */
197
+ export const isTreeEntry = (value: unknown): value is TreeEntry => {
198
+ if (value === null || typeof value !== 'object') {
199
+ return false;
200
+ }
201
+
202
+ const obj = value as Record<string, unknown>;
203
+
204
+ return (
205
+ typeof obj.p === 'number' && isBytes(obj.k) && isCidLink(obj.v) && (obj.t === null || isCidLink(obj.t))
206
+ );
207
+ };
208
+
209
+ /** mst node object */
210
+ export interface MstNode {
115
211
  /** link to sub-tree Node on a lower level and with all keys sorting before keys at this node */
116
212
  l: CID.CidLink | null;
117
213
  /** ordered list of TreeEntry objects */
118
214
  e: TreeEntry[];
119
215
  }
120
216
 
121
- interface NodeEntry {
122
- key: string;
123
- cid: CID.CidLink;
124
- }
217
+ /**
218
+ * checks if a value is a valid mst node object
219
+ * @param value the value to check
220
+ * @returns true if the value is a valid mst node object, false otherwise
221
+ */
222
+ export const isMstNode = (value: unknown): value is MstNode => {
223
+ if (value === null || typeof value !== 'object') {
224
+ return false;
225
+ }
226
+
227
+ const obj = value as Record<string, unknown>;
228
+
229
+ return (obj.l === null || isCidLink(obj.l)) && Array.isArray(obj.e) && obj.e.every(isTreeEntry);
230
+ };
package/lib/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { iterateAtpRepo, type RepoEntry } from './atproto-repo.js';
2
- export { readCar } from './reader.js';
1
+ export * from './atproto-repo.js';
2
+ export * from './reader.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@atcute/car",
4
- "version": "2.0.2",
4
+ "version": "2.1.0",
5
5
  "description": "lightweight DASL CAR and atproto repository decoder for AT Protocol.",
6
6
  "keywords": [
7
7
  "atproto",
@@ -28,9 +28,9 @@
28
28
  "@atcute/multibase": "^1.1.2"
29
29
  },
30
30
  "dependencies": {
31
- "@atcute/varint": "^1.0.2",
32
- "@atcute/cbor": "^2.1.2",
33
- "@atcute/cid": "^2.1.0"
31
+ "@atcute/cbor": "^2.1.3",
32
+ "@atcute/cid": "^2.1.0",
33
+ "@atcute/varint": "^1.0.2"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsc --project tsconfig.build.json",