@atproto/repo 0.9.1 → 0.10.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.
- package/CHANGELOG.md +22 -0
- package/dist/block-map.js +10 -19
- package/dist/block-map.js.map +1 -1
- package/dist/car.d.ts +2 -2
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +30 -104
- package/dist/car.js.map +1 -1
- package/dist/cid-set.js +4 -14
- package/dist/cid-set.js.map +1 -1
- package/dist/data-diff.d.ts +3 -3
- package/dist/data-diff.d.ts.map +1 -1
- package/dist/data-diff.js +11 -45
- package/dist/data-diff.js.map +1 -1
- package/dist/error.js +11 -53
- package/dist/error.js.map +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -27
- package/dist/index.js.map +1 -1
- package/dist/logger.js +3 -6
- package/dist/logger.js.map +1 -1
- package/dist/mst/diff.d.ts +2 -2
- package/dist/mst/diff.d.ts.map +1 -1
- package/dist/mst/diff.js +9 -14
- package/dist/mst/diff.js.map +1 -1
- package/dist/mst/index.d.ts +4 -4
- package/dist/mst/index.d.ts.map +1 -1
- package/dist/mst/index.js +4 -43
- package/dist/mst/index.js.map +1 -1
- package/dist/mst/mst.d.ts +14 -14
- package/dist/mst/mst.d.ts.map +1 -1
- package/dist/mst/mst.js +41 -118
- package/dist/mst/mst.js.map +1 -1
- package/dist/mst/util.d.ts +2 -2
- package/dist/mst/util.d.ts.map +1 -1
- package/dist/mst/util.js +29 -47
- package/dist/mst/util.js.map +1 -1
- package/dist/mst/walker.d.ts +1 -1
- package/dist/mst/walker.d.ts.map +1 -1
- package/dist/mst/walker.js +3 -23
- package/dist/mst/walker.js.map +1 -1
- package/dist/parse.d.ts +1 -1
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +12 -18
- package/dist/parse.js.map +1 -1
- package/dist/readable-repo.d.ts +3 -3
- package/dist/readable-repo.d.ts.map +1 -1
- package/dist/readable-repo.js +13 -77
- package/dist/readable-repo.js.map +1 -1
- package/dist/repo.d.ts +6 -6
- package/dist/repo.d.ts.map +1 -1
- package/dist/repo.js +31 -77
- package/dist/repo.js.map +1 -1
- package/dist/storage/index.d.ts +4 -4
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +4 -20
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/memory-blockstore.d.ts +4 -4
- package/dist/storage/memory-blockstore.d.ts.map +1 -1
- package/dist/storage/memory-blockstore.js +7 -27
- package/dist/storage/memory-blockstore.js.map +1 -1
- package/dist/storage/readable-blockstore.d.ts +1 -1
- package/dist/storage/readable-blockstore.d.ts.map +1 -1
- package/dist/storage/readable-blockstore.js +9 -13
- package/dist/storage/readable-blockstore.js.map +1 -1
- package/dist/storage/sync-storage.d.ts +2 -2
- package/dist/storage/sync-storage.d.ts.map +1 -1
- package/dist/storage/sync-storage.js +5 -19
- package/dist/storage/sync-storage.js.map +1 -1
- package/dist/storage/types.d.ts +2 -2
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/storage/types.js +1 -5
- package/dist/storage/types.js.map +1 -1
- package/dist/sync/consumer.d.ts +3 -3
- package/dist/sync/consumer.d.ts.map +1 -1
- package/dist/sync/consumer.js +32 -75
- package/dist/sync/consumer.js.map +1 -1
- package/dist/sync/index.d.ts +2 -2
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +2 -18
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/provider.d.ts +2 -2
- package/dist/sync/provider.d.ts.map +1 -1
- package/dist/sync/provider.js +17 -55
- package/dist/sync/provider.js.map +1 -1
- package/dist/types.d.ts +39 -39
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +41 -44
- package/dist/types.js.map +1 -1
- package/dist/util.d.ts +2 -2
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +28 -75
- package/dist/util.js.map +1 -1
- package/jest.config.cjs +24 -0
- package/package.json +19 -14
- package/src/car.ts +5 -4
- package/src/data-diff.ts +3 -3
- package/src/index.ts +11 -11
- package/src/mst/diff.ts +3 -3
- package/src/mst/index.ts +4 -4
- package/src/mst/mst.ts +7 -7
- package/src/mst/util.ts +3 -3
- package/src/mst/walker.ts +1 -1
- package/src/parse.ts +3 -3
- package/src/readable-repo.ts +7 -7
- package/src/repo.ts +9 -9
- package/src/storage/index.ts +4 -4
- package/src/storage/memory-blockstore.ts +4 -4
- package/src/storage/readable-blockstore.ts +4 -4
- package/src/storage/sync-storage.ts +2 -2
- package/src/storage/types.ts +2 -2
- package/src/sync/consumer.ts +12 -8
- package/src/sync/index.ts +2 -2
- package/src/sync/provider.ts +7 -7
- package/src/types.ts +7 -7
- package/src/util.ts +2 -2
- package/tests/_util.ts +4 -4
- package/tests/car.test.ts +2 -2
- package/tests/commit-data.test.ts +7 -2
- package/tests/commit-proofs.test.ts +4 -4
- package/tests/covering-proofs.test.ts +4 -4
- package/tests/mst.test.ts +5 -5
- package/tests/proofs.test.ts +4 -4
- package/tests/repo.test.ts +4 -4
- package/tests/sync.test.ts +4 -4
- package/tsconfig.build.tsbuildinfo +1 -1
- package/jest.config.js +0 -7
package/dist/sync/consumer.js
CHANGED
|
@@ -1,73 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.RepoVerificationError = exports.verifyRecords = exports.verifyProofs = exports.verifyDiff = exports.verifyDiffCar = exports.verifyRepo = exports.verifyRepoCar = void 0;
|
|
37
|
-
const car_1 = require("../car");
|
|
38
|
-
const data_diff_1 = require("../data-diff");
|
|
39
|
-
const mst_1 = require("../mst");
|
|
40
|
-
const readable_repo_1 = require("../readable-repo");
|
|
41
|
-
const storage_1 = require("../storage");
|
|
42
|
-
const types_1 = require("../types");
|
|
43
|
-
const util = __importStar(require("../util"));
|
|
44
|
-
const verifyRepoCar = async (carBytes, did, signingKey) => {
|
|
45
|
-
const car = await (0, car_1.readCarWithRoot)(carBytes);
|
|
46
|
-
return (0, exports.verifyRepo)(car.blocks, car.root, did, signingKey);
|
|
1
|
+
import { readCarWithRoot } from '../car.js';
|
|
2
|
+
import { DataDiff } from '../data-diff.js';
|
|
3
|
+
import { MST } from '../mst/index.js';
|
|
4
|
+
import { ReadableRepo } from '../readable-repo.js';
|
|
5
|
+
import { MemoryBlockstore, SyncStorage, } from '../storage/index.js';
|
|
6
|
+
import { def, } from '../types.js';
|
|
7
|
+
import * as util from '../util.js';
|
|
8
|
+
export const verifyRepoCar = async (carBytes, did, signingKey) => {
|
|
9
|
+
const car = await readCarWithRoot(carBytes);
|
|
10
|
+
return verifyRepo(car.blocks, car.root, did, signingKey);
|
|
47
11
|
};
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const diff = await (0, exports.verifyDiff)(null, blocks, head, did, signingKey, opts);
|
|
12
|
+
export const verifyRepo = async (blocks, head, did, signingKey, opts) => {
|
|
13
|
+
const diff = await verifyDiff(null, blocks, head, did, signingKey, opts);
|
|
51
14
|
const creates = util.ensureCreates(diff.writes);
|
|
52
15
|
return {
|
|
53
16
|
creates,
|
|
54
17
|
commit: diff.commit,
|
|
55
18
|
};
|
|
56
19
|
};
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
return (0, exports.verifyDiff)(repo, car.blocks, car.root, did, signingKey, opts);
|
|
20
|
+
export const verifyDiffCar = async (repo, carBytes, did, signingKey, opts) => {
|
|
21
|
+
const car = await readCarWithRoot(carBytes);
|
|
22
|
+
return verifyDiff(repo, car.blocks, car.root, did, signingKey, opts);
|
|
61
23
|
};
|
|
62
|
-
|
|
63
|
-
const verifyDiff = async (repo, updateBlocks, updateRoot, did, signingKey, opts) => {
|
|
24
|
+
export const verifyDiff = async (repo, updateBlocks, updateRoot, did, signingKey, opts) => {
|
|
64
25
|
const { ensureLeaves = true } = opts ?? {};
|
|
65
|
-
const stagedStorage = new
|
|
26
|
+
const stagedStorage = new MemoryBlockstore(updateBlocks);
|
|
66
27
|
const updateStorage = repo
|
|
67
|
-
? new
|
|
28
|
+
? new SyncStorage(stagedStorage, repo.storage)
|
|
68
29
|
: stagedStorage;
|
|
69
30
|
const updated = await verifyRepoRoot(updateStorage, updateRoot, did, signingKey);
|
|
70
|
-
const diff = await
|
|
31
|
+
const diff = await DataDiff.of(updated.data, repo?.data ?? null);
|
|
71
32
|
const writes = await util.diffToWriteDescripts(diff);
|
|
72
33
|
const newBlocks = diff.newMstBlocks;
|
|
73
34
|
const leaves = updateBlocks.getMany(diff.newLeafCids.toList());
|
|
@@ -99,10 +60,9 @@ const verifyDiff = async (repo, updateBlocks, updateRoot, did, signingKey, opts)
|
|
|
99
60
|
},
|
|
100
61
|
};
|
|
101
62
|
};
|
|
102
|
-
exports.verifyDiff = verifyDiff;
|
|
103
63
|
// @NOTE only verifies the root, not the repo contents
|
|
104
64
|
const verifyRepoRoot = async (storage, head, did, signingKey) => {
|
|
105
|
-
const repo = await
|
|
65
|
+
const repo = await ReadableRepo.load(storage, head);
|
|
106
66
|
if (did !== undefined && repo.did !== did) {
|
|
107
67
|
throw new RepoVerificationError(`Invalid repo did: ${repo.did}`);
|
|
108
68
|
}
|
|
@@ -114,10 +74,10 @@ const verifyRepoRoot = async (storage, head, did, signingKey) => {
|
|
|
114
74
|
}
|
|
115
75
|
return repo;
|
|
116
76
|
};
|
|
117
|
-
const verifyProofs = async (proofs, claims, did, didKey) => {
|
|
118
|
-
const car = await
|
|
119
|
-
const blockstore = new
|
|
120
|
-
const commit = await blockstore.readObj(car.root,
|
|
77
|
+
export const verifyProofs = async (proofs, claims, did, didKey) => {
|
|
78
|
+
const car = await readCarWithRoot(proofs);
|
|
79
|
+
const blockstore = new MemoryBlockstore(car.blocks);
|
|
80
|
+
const commit = await blockstore.readObj(car.root, def.commit);
|
|
121
81
|
if (commit.did !== did) {
|
|
122
82
|
throw new RepoVerificationError(`Invalid repo did: ${commit.did}`);
|
|
123
83
|
}
|
|
@@ -125,12 +85,12 @@ const verifyProofs = async (proofs, claims, did, didKey) => {
|
|
|
125
85
|
if (!validSig) {
|
|
126
86
|
throw new RepoVerificationError(`Invalid signature on commit: ${car.root.toString()}`);
|
|
127
87
|
}
|
|
128
|
-
const mst =
|
|
88
|
+
const mst = MST.load(blockstore, commit.data);
|
|
129
89
|
const verified = [];
|
|
130
90
|
const unverified = [];
|
|
131
91
|
for (const claim of claims) {
|
|
132
92
|
const found = await mst.get(util.formatDataKey(claim.collection, claim.rkey));
|
|
133
|
-
const record = found ? await blockstore.readObj(found,
|
|
93
|
+
const record = found ? await blockstore.readObj(found, def.map) : null;
|
|
134
94
|
if (claim.cid === null) {
|
|
135
95
|
if (record === null) {
|
|
136
96
|
verified.push(claim);
|
|
@@ -150,11 +110,10 @@ const verifyProofs = async (proofs, claims, did, didKey) => {
|
|
|
150
110
|
}
|
|
151
111
|
return { verified, unverified };
|
|
152
112
|
};
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
const
|
|
156
|
-
const
|
|
157
|
-
const commit = await blockstore.readObj(car.root, types_1.def.commit);
|
|
113
|
+
export const verifyRecords = async (proofs, did, signingKey) => {
|
|
114
|
+
const car = await readCarWithRoot(proofs);
|
|
115
|
+
const blockstore = new MemoryBlockstore(car.blocks);
|
|
116
|
+
const commit = await blockstore.readObj(car.root, def.commit);
|
|
158
117
|
if (commit.did !== did) {
|
|
159
118
|
throw new RepoVerificationError(`Invalid repo did: ${commit.did}`);
|
|
160
119
|
}
|
|
@@ -162,7 +121,7 @@ const verifyRecords = async (proofs, did, signingKey) => {
|
|
|
162
121
|
if (!validSig) {
|
|
163
122
|
throw new RepoVerificationError(`Invalid signature on commit: ${car.root.toString()}`);
|
|
164
123
|
}
|
|
165
|
-
const mst =
|
|
124
|
+
const mst = MST.load(blockstore, commit.data);
|
|
166
125
|
const records = [];
|
|
167
126
|
const leaves = await mst.reachableLeaves();
|
|
168
127
|
for (const leaf of leaves) {
|
|
@@ -178,8 +137,6 @@ const verifyRecords = async (proofs, did, signingKey) => {
|
|
|
178
137
|
}
|
|
179
138
|
return records;
|
|
180
139
|
};
|
|
181
|
-
|
|
182
|
-
class RepoVerificationError extends Error {
|
|
140
|
+
export class RepoVerificationError extends Error {
|
|
183
141
|
}
|
|
184
|
-
exports.RepoVerificationError = RepoVerificationError;
|
|
185
142
|
//# sourceMappingURL=consumer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consumer.js","sourceRoot":"","sources":["../../src/sync/consumer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,gCAAwC;AACxC,4CAAuC;AACvC,gCAA4B;AAC5B,oDAA+C;AAC/C,wCAA8E;AAC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,IAAI,CAAC,CAAA;IAE7C,MAAM,OAAO,GAAkB,EAAE,CAAA;IACjC,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,eAAe,EAAE,CAAA;IAC1C,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,CAAC;QAC1B,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QACxD,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC7D,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,CAAC,IAAI,CAAC;gBACX,UAAU;gBACV,IAAI;gBACJ,MAAM;aACP,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA;AAjCY,QAAA,aAAa,iBAiCzB;AAED,MAAa,qBAAsB,SAAQ,KAAK;CAAG;AAAnD,sDAAmD","sourcesContent":["import { Cid } from '@atproto/lex-data'\nimport { BlockMap } from '../block-map'\nimport { readCarWithRoot } from '../car'\nimport { DataDiff } from '../data-diff'\nimport { MST } from '../mst'\nimport { ReadableRepo } from '../readable-repo'\nimport { MemoryBlockstore, ReadableBlockstore, SyncStorage } from '../storage'\nimport {\n RecordCidClaim,\n RecordClaim,\n VerifiedDiff,\n VerifiedRepo,\n def,\n} from '../types'\nimport * as util from '../util'\n\nexport const verifyRepoCar = async (\n carBytes: Uint8Array,\n did?: string,\n signingKey?: string,\n): Promise<VerifiedRepo> => {\n const car = await readCarWithRoot(carBytes)\n return verifyRepo(car.blocks, car.root, did, signingKey)\n}\n\nexport const verifyRepo = async (\n blocks: BlockMap,\n head: Cid,\n did?: string,\n signingKey?: string,\n opts?: { ensureLeaves?: boolean },\n): Promise<VerifiedRepo> => {\n const diff = await verifyDiff(null, blocks, head, did, signingKey, opts)\n const creates = util.ensureCreates(diff.writes)\n return {\n creates,\n commit: diff.commit,\n }\n}\n\nexport const verifyDiffCar = async (\n repo: ReadableRepo | null,\n carBytes: Uint8Array,\n did?: string,\n signingKey?: string,\n opts?: { ensureLeaves?: boolean },\n): Promise<VerifiedDiff> => {\n const car = await readCarWithRoot(carBytes)\n return verifyDiff(repo, car.blocks, car.root, did, signingKey, opts)\n}\n\nexport const verifyDiff = async (\n repo: ReadableRepo | null,\n updateBlocks: BlockMap,\n updateRoot: Cid,\n did?: string,\n signingKey?: string,\n opts?: { ensureLeaves?: boolean },\n): Promise<VerifiedDiff> => {\n const { ensureLeaves = true } = opts ?? {}\n const stagedStorage = new MemoryBlockstore(updateBlocks)\n const updateStorage = repo\n ? new SyncStorage(stagedStorage, repo.storage)\n : stagedStorage\n const updated = await verifyRepoRoot(\n updateStorage,\n updateRoot,\n did,\n signingKey,\n )\n const diff = await DataDiff.of(updated.data, repo?.data ?? null)\n const writes = await util.diffToWriteDescripts(diff)\n const newBlocks = diff.newMstBlocks\n const leaves = updateBlocks.getMany(diff.newLeafCids.toList())\n if (leaves.missing.length > 0 && ensureLeaves) {\n throw new Error(`missing leaf blocks: ${leaves.missing}`)\n }\n newBlocks.addMap(leaves.blocks)\n const removedCids = diff.removedCids\n const commitCid = await newBlocks.add(updated.commit)\n // ensure the commit cid actually changed\n if (repo) {\n if (commitCid.equals(repo.cid)) {\n newBlocks.delete(commitCid)\n } else {\n removedCids.add(repo.cid)\n }\n }\n return {\n writes,\n commit: {\n cid: updated.cid,\n rev: updated.commit.rev,\n prev: repo?.cid ?? null,\n since: repo?.commit.rev ?? null,\n newBlocks,\n relevantBlocks: newBlocks,\n removedCids,\n },\n }\n}\n\n// @NOTE only verifies the root, not the repo contents\nconst verifyRepoRoot = async (\n storage: ReadableBlockstore,\n head: Cid,\n did?: string,\n signingKey?: string,\n): Promise<ReadableRepo> => {\n const repo = await ReadableRepo.load(storage, head)\n if (did !== undefined && repo.did !== did) {\n throw new RepoVerificationError(`Invalid repo did: ${repo.did}`)\n }\n if (signingKey !== undefined) {\n const validSig = await util.verifyCommitSig(repo.commit, signingKey)\n if (!validSig) {\n throw new RepoVerificationError(\n `Invalid signature on commit: ${repo.cid.toString()}`,\n )\n }\n }\n return repo\n}\n\nexport const verifyProofs = async (\n proofs: Uint8Array,\n claims: RecordCidClaim[],\n did: string,\n didKey: string,\n): Promise<{ verified: RecordCidClaim[]; unverified: RecordCidClaim[] }> => {\n const car = await readCarWithRoot(proofs)\n const blockstore = new MemoryBlockstore(car.blocks)\n const commit = await blockstore.readObj(car.root, def.commit)\n if (commit.did !== did) {\n throw new RepoVerificationError(`Invalid repo did: ${commit.did}`)\n }\n const validSig = await util.verifyCommitSig(commit, didKey)\n if (!validSig) {\n throw new RepoVerificationError(\n `Invalid signature on commit: ${car.root.toString()}`,\n )\n }\n const mst = MST.load(blockstore, commit.data)\n const verified: RecordCidClaim[] = []\n const unverified: RecordCidClaim[] = []\n for (const claim of claims) {\n const found = await mst.get(\n util.formatDataKey(claim.collection, claim.rkey),\n )\n const record = found ? await blockstore.readObj(found, def.map) : null\n if (claim.cid === null) {\n if (record === null) {\n verified.push(claim)\n } else {\n unverified.push(claim)\n }\n } else {\n if (found?.equals(claim.cid)) {\n verified.push(claim)\n } else {\n unverified.push(claim)\n }\n }\n }\n return { verified, unverified }\n}\n\nexport const verifyRecords = async (\n proofs: Uint8Array,\n did: string,\n signingKey: string,\n): Promise<RecordClaim[]> => {\n const car = await readCarWithRoot(proofs)\n const blockstore = new MemoryBlockstore(car.blocks)\n const commit = await blockstore.readObj(car.root, def.commit)\n if (commit.did !== did) {\n throw new RepoVerificationError(`Invalid repo did: ${commit.did}`)\n }\n const validSig = await util.verifyCommitSig(commit, signingKey)\n if (!validSig) {\n throw new RepoVerificationError(\n `Invalid signature on commit: ${car.root.toString()}`,\n )\n }\n const mst = MST.load(blockstore, commit.data)\n\n const records: RecordClaim[] = []\n const leaves = await mst.reachableLeaves()\n for (const leaf of leaves) {\n const { collection, rkey } = util.parseDataKey(leaf.key)\n const record = await blockstore.attemptReadRecord(leaf.value)\n if (record) {\n records.push({\n collection,\n rkey,\n record,\n })\n }\n }\n return records\n}\n\nexport class RepoVerificationError extends Error {}\n"]}
|
|
1
|
+
{"version":3,"file":"consumer.js","sourceRoot":"","sources":["../../src/sync/consumer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC1C,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,EACL,gBAAgB,EAEhB,WAAW,GACZ,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAKL,GAAG,GACJ,MAAM,aAAa,CAAA;AACpB,OAAO,KAAK,IAAI,MAAM,YAAY,CAAA;AAElC,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAChC,QAAoB,EACpB,GAAY,EACZ,UAAmB,EACI,EAAE;IACzB,MAAM,GAAG,GAAG,MAAM,eAAe,CAAC,QAAQ,CAAC,CAAA;IAC3C,OAAO,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,UAAU,CAAC,CAAA;AAC1D,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAC7B,MAAgB,EAChB,IAAS,EACT,GAAY,EACZ,UAAmB,EACnB,IAAiC,EACV,EAAE;IACzB,MAAM,IAAI,GAAG,MAAM,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA;IACxE,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAC/C,OAAO;QACL,OAAO;QACP,MAAM,EAAE,IAAI,CAAC,MAAM;KACpB,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,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AC;QACvB,MAAM,IAAI,qBAAqB,CAAC,qBAAqB,MAAM,CAAC,GAAG,EAAE,CAAC,CAAA;IACpE,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IAC/D,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,qBAAqB,CAC7B,gCAAgC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CACtD,CAAA;IACH,CAAC;IACD,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,CAAA;IAE7C,MAAM,OAAO,GAAkB,EAAE,CAAA;IACjC,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,eAAe,EAAE,CAAA;IAC1C,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,CAAC;QAC1B,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QACxD,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC7D,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,CAAC,IAAI,CAAC;gBACX,UAAU;gBACV,IAAI;gBACJ,MAAM;aACP,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA;AAED,MAAM,OAAO,qBAAsB,SAAQ,KAAK;CAAG","sourcesContent":["import { Cid } from '@atproto/lex-data'\nimport { BlockMap } from '../block-map.js'\nimport { readCarWithRoot } from '../car.js'\nimport { DataDiff } from '../data-diff.js'\nimport { MST } from '../mst/index.js'\nimport { ReadableRepo } from '../readable-repo.js'\nimport {\n MemoryBlockstore,\n ReadableBlockstore,\n SyncStorage,\n} from '../storage/index.js'\nimport {\n RecordCidClaim,\n RecordClaim,\n VerifiedDiff,\n VerifiedRepo,\n def,\n} from '../types.js'\nimport * as util from '../util.js'\n\nexport const verifyRepoCar = async (\n carBytes: Uint8Array,\n did?: string,\n signingKey?: string,\n): Promise<VerifiedRepo> => {\n const car = await readCarWithRoot(carBytes)\n return verifyRepo(car.blocks, car.root, did, signingKey)\n}\n\nexport const verifyRepo = async (\n blocks: BlockMap,\n head: Cid,\n did?: string,\n signingKey?: string,\n opts?: { ensureLeaves?: boolean },\n): Promise<VerifiedRepo> => {\n const diff = await verifyDiff(null, blocks, head, did, signingKey, opts)\n const creates = util.ensureCreates(diff.writes)\n return {\n creates,\n commit: diff.commit,\n }\n}\n\nexport const verifyDiffCar = async (\n repo: ReadableRepo | null,\n carBytes: Uint8Array,\n did?: string,\n signingKey?: string,\n opts?: { ensureLeaves?: boolean },\n): Promise<VerifiedDiff> => {\n const car = await readCarWithRoot(carBytes)\n return verifyDiff(repo, car.blocks, car.root, did, signingKey, opts)\n}\n\nexport const verifyDiff = async (\n repo: ReadableRepo | null,\n updateBlocks: BlockMap,\n updateRoot: Cid,\n did?: string,\n signingKey?: string,\n opts?: { ensureLeaves?: boolean },\n): Promise<VerifiedDiff> => {\n const { ensureLeaves = true } = opts ?? {}\n const stagedStorage = new MemoryBlockstore(updateBlocks)\n const updateStorage = repo\n ? new SyncStorage(stagedStorage, repo.storage)\n : stagedStorage\n const updated = await verifyRepoRoot(\n updateStorage,\n updateRoot,\n did,\n signingKey,\n )\n const diff = await DataDiff.of(updated.data, repo?.data ?? null)\n const writes = await util.diffToWriteDescripts(diff)\n const newBlocks = diff.newMstBlocks\n const leaves = updateBlocks.getMany(diff.newLeafCids.toList())\n if (leaves.missing.length > 0 && ensureLeaves) {\n throw new Error(`missing leaf blocks: ${leaves.missing}`)\n }\n newBlocks.addMap(leaves.blocks)\n const removedCids = diff.removedCids\n const commitCid = await newBlocks.add(updated.commit)\n // ensure the commit cid actually changed\n if (repo) {\n if (commitCid.equals(repo.cid)) {\n newBlocks.delete(commitCid)\n } else {\n removedCids.add(repo.cid)\n }\n }\n return {\n writes,\n commit: {\n cid: updated.cid,\n rev: updated.commit.rev,\n prev: repo?.cid ?? null,\n since: repo?.commit.rev ?? null,\n newBlocks,\n relevantBlocks: newBlocks,\n removedCids,\n },\n }\n}\n\n// @NOTE only verifies the root, not the repo contents\nconst verifyRepoRoot = async (\n storage: ReadableBlockstore,\n head: Cid,\n did?: string,\n signingKey?: string,\n): Promise<ReadableRepo> => {\n const repo = await ReadableRepo.load(storage, head)\n if (did !== undefined && repo.did !== did) {\n throw new RepoVerificationError(`Invalid repo did: ${repo.did}`)\n }\n if (signingKey !== undefined) {\n const validSig = await util.verifyCommitSig(repo.commit, signingKey)\n if (!validSig) {\n throw new RepoVerificationError(\n `Invalid signature on commit: ${repo.cid.toString()}`,\n )\n }\n }\n return repo\n}\n\nexport const verifyProofs = async (\n proofs: Uint8Array,\n claims: RecordCidClaim[],\n did: string,\n didKey: string,\n): Promise<{ verified: RecordCidClaim[]; unverified: RecordCidClaim[] }> => {\n const car = await readCarWithRoot(proofs)\n const blockstore = new MemoryBlockstore(car.blocks)\n const commit = await blockstore.readObj(car.root, def.commit)\n if (commit.did !== did) {\n throw new RepoVerificationError(`Invalid repo did: ${commit.did}`)\n }\n const validSig = await util.verifyCommitSig(commit, didKey)\n if (!validSig) {\n throw new RepoVerificationError(\n `Invalid signature on commit: ${car.root.toString()}`,\n )\n }\n const mst = MST.load(blockstore, commit.data)\n const verified: RecordCidClaim[] = []\n const unverified: RecordCidClaim[] = []\n for (const claim of claims) {\n const found = await mst.get(\n util.formatDataKey(claim.collection, claim.rkey),\n )\n const record = found ? await blockstore.readObj(found, def.map) : null\n if (claim.cid === null) {\n if (record === null) {\n verified.push(claim)\n } else {\n unverified.push(claim)\n }\n } else {\n if (found?.equals(claim.cid)) {\n verified.push(claim)\n } else {\n unverified.push(claim)\n }\n }\n }\n return { verified, unverified }\n}\n\nexport const verifyRecords = async (\n proofs: Uint8Array,\n did: string,\n signingKey: string,\n): Promise<RecordClaim[]> => {\n const car = await readCarWithRoot(proofs)\n const blockstore = new MemoryBlockstore(car.blocks)\n const commit = await blockstore.readObj(car.root, def.commit)\n if (commit.did !== did) {\n throw new RepoVerificationError(`Invalid repo did: ${commit.did}`)\n }\n const validSig = await util.verifyCommitSig(commit, signingKey)\n if (!validSig) {\n throw new RepoVerificationError(\n `Invalid signature on commit: ${car.root.toString()}`,\n )\n }\n const mst = MST.load(blockstore, commit.data)\n\n const records: RecordClaim[] = []\n const leaves = await mst.reachableLeaves()\n for (const leaf of leaves) {\n const { collection, rkey } = util.parseDataKey(leaf.key)\n const record = await blockstore.attemptReadRecord(leaf.value)\n if (record) {\n records.push({\n collection,\n rkey,\n record,\n })\n }\n }\n return records\n}\n\nexport class RepoVerificationError extends Error {}\n"]}
|
package/dist/sync/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './consumer';
|
|
2
|
-
export * from './provider';
|
|
1
|
+
export * from './consumer.js';
|
|
2
|
+
export * from './provider.js';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/sync/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sync/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sync/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA"}
|
package/dist/sync/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./consumer"), exports);
|
|
18
|
-
__exportStar(require("./provider"), exports);
|
|
1
|
+
export * from './consumer.js';
|
|
2
|
+
export * from './provider.js';
|
|
19
3
|
//# sourceMappingURL=index.js.map
|
package/dist/sync/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sync/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sync/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA","sourcesContent":["export * from './consumer.js'\nexport * from './provider.js'\n"]}
|
package/dist/sync/provider.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Cid } from '@atproto/lex-data';
|
|
2
|
-
import { ReadableBlockstore, RepoStorage } from '../storage';
|
|
3
|
-
import { RecordPath } from '../types';
|
|
2
|
+
import { ReadableBlockstore, RepoStorage } from '../storage/index.js';
|
|
3
|
+
import { RecordPath } from '../types.js';
|
|
4
4
|
export declare const getFullRepo: (storage: RepoStorage, commitCid: Cid) => AsyncIterable<Uint8Array>;
|
|
5
5
|
export declare const getRecords: (storage: ReadableBlockstore, commitCid: Cid, paths: RecordPath[]) => AsyncIterable<Uint8Array>;
|
|
6
6
|
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/sync/provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AAKvC,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/sync/provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AAKvC,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACrE,OAAO,EAAY,UAAU,EAAO,MAAM,aAAa,CAAA;AAMvD,eAAO,MAAM,WAAW,GACtB,SAAS,WAAW,EACpB,WAAW,GAAG,KACb,aAAa,CAAC,UAAU,CAE1B,CAAA;AAiBD,eAAO,MAAM,UAAU,GACrB,SAAS,kBAAkB,EAC3B,WAAW,GAAG,EACd,OAAO,UAAU,EAAE,KAClB,aAAa,CAAC,UAAU,CAK1B,CAAA"}
|
package/dist/sync/provider.js
CHANGED
|
@@ -1,76 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.getRecords = exports.getFullRepo = void 0;
|
|
37
|
-
const car_1 = require("../car");
|
|
38
|
-
const cid_set_1 = require("../cid-set");
|
|
39
|
-
const error_1 = require("../error");
|
|
40
|
-
const mst_1 = require("../mst");
|
|
41
|
-
const types_1 = require("../types");
|
|
42
|
-
const util = __importStar(require("../util"));
|
|
1
|
+
import { writeCarStream } from '../car.js';
|
|
2
|
+
import { CidSet } from '../cid-set.js';
|
|
3
|
+
import { MissingBlocksError } from '../error.js';
|
|
4
|
+
import { MST } from '../mst/index.js';
|
|
5
|
+
import { def } from '../types.js';
|
|
6
|
+
import * as util from '../util.js';
|
|
43
7
|
// Full Repo
|
|
44
8
|
// -------------
|
|
45
|
-
const getFullRepo = (storage, commitCid) => {
|
|
46
|
-
return
|
|
9
|
+
export const getFullRepo = (storage, commitCid) => {
|
|
10
|
+
return writeCarStream(commitCid, iterateFullRepo(storage, commitCid));
|
|
47
11
|
};
|
|
48
|
-
exports.getFullRepo = getFullRepo;
|
|
49
12
|
async function* iterateFullRepo(storage, commitCid) {
|
|
50
|
-
const commit = await storage.readObjAndBytes(commitCid,
|
|
13
|
+
const commit = await storage.readObjAndBytes(commitCid, def.commit);
|
|
51
14
|
yield { cid: commitCid, bytes: commit.bytes };
|
|
52
|
-
const mst =
|
|
15
|
+
const mst = MST.load(storage, commit.obj.data);
|
|
53
16
|
for await (const block of mst.carBlockStream()) {
|
|
54
17
|
yield block;
|
|
55
18
|
}
|
|
56
19
|
}
|
|
57
20
|
// Narrow slices
|
|
58
21
|
// -------------
|
|
59
|
-
const getRecords = (storage, commitCid, paths) => {
|
|
60
|
-
return
|
|
22
|
+
export const getRecords = (storage, commitCid, paths) => {
|
|
23
|
+
return writeCarStream(commitCid, iterateRecordBlocks(storage, commitCid, paths));
|
|
61
24
|
};
|
|
62
|
-
exports.getRecords = getRecords;
|
|
63
25
|
async function* iterateRecordBlocks(storage, commitCid, paths) {
|
|
64
|
-
const commit = await storage.readObjAndBytes(commitCid,
|
|
26
|
+
const commit = await storage.readObjAndBytes(commitCid, def.commit);
|
|
65
27
|
yield { cid: commitCid, bytes: commit.bytes };
|
|
66
|
-
const mst =
|
|
28
|
+
const mst = MST.load(storage, commit.obj.data);
|
|
67
29
|
const cidsForPaths = await Promise.all(paths.map((p) => mst.cidsForPath(util.formatDataKey(p.collection, p.rkey))));
|
|
68
30
|
const allCids = cidsForPaths.reduce((acc, cur) => {
|
|
69
|
-
return acc.addSet(new
|
|
70
|
-
}, new
|
|
31
|
+
return acc.addSet(new CidSet(cur));
|
|
32
|
+
}, new CidSet());
|
|
71
33
|
const found = await storage.getBlocks(allCids.toList());
|
|
72
34
|
if (found.missing.length > 0) {
|
|
73
|
-
throw new
|
|
35
|
+
throw new MissingBlocksError('writeRecordsToCarStream', found.missing);
|
|
74
36
|
}
|
|
75
37
|
for (const block of found.blocks.entries()) {
|
|
76
38
|
yield block;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/sync/provider.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/sync/provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,C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,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAA;IAC7C,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IAC9C,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,GAAG,CACpC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAC5E,CAAA;IACD,MAAM,OAAO,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC/C,OAAO,GAAG,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;IACpC,CAAC,EAAE,IAAI,MAAM,EAAE,CAAC,CAAA;IAChB,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;IACvD,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,kBAAkB,CAAC,yBAAyB,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IACxE,CAAC;IACD,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;QAC3C,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC","sourcesContent":["import { Cid } from '@atproto/lex-data'\nimport { writeCarStream } from '../car.js'\nimport { CidSet } from '../cid-set.js'\nimport { MissingBlocksError } from '../error.js'\nimport { MST } from '../mst/index.js'\nimport { ReadableBlockstore, RepoStorage } from '../storage/index.js'\nimport { CarBlock, RecordPath, def } from '../types.js'\nimport * as util from '../util.js'\n\n// Full Repo\n// -------------\n\nexport const getFullRepo = (\n storage: RepoStorage,\n commitCid: Cid,\n): AsyncIterable<Uint8Array> => {\n return writeCarStream(commitCid, iterateFullRepo(storage, commitCid))\n}\n\nasync function* iterateFullRepo(\n storage: RepoStorage,\n commitCid: Cid,\n): AsyncGenerator<CarBlock> {\n const commit = await storage.readObjAndBytes(commitCid, def.commit)\n yield { cid: commitCid, bytes: commit.bytes }\n const mst = MST.load(storage, commit.obj.data)\n for await (const block of mst.carBlockStream()) {\n yield block\n }\n}\n\n// Narrow slices\n// -------------\n\nexport const getRecords = (\n storage: ReadableBlockstore,\n commitCid: Cid,\n paths: RecordPath[],\n): AsyncIterable<Uint8Array> => {\n return writeCarStream(\n commitCid,\n iterateRecordBlocks(storage, commitCid, paths),\n )\n}\n\nasync function* iterateRecordBlocks(\n storage: ReadableBlockstore,\n commitCid: Cid,\n paths: RecordPath[],\n): AsyncGenerator<CarBlock> {\n const commit = await storage.readObjAndBytes(commitCid, def.commit)\n yield { cid: commitCid, bytes: commit.bytes }\n const mst = MST.load(storage, commit.obj.data)\n const cidsForPaths = await Promise.all(\n paths.map((p) => mst.cidsForPath(util.formatDataKey(p.collection, p.rkey))),\n )\n const allCids = cidsForPaths.reduce((acc, cur) => {\n return acc.addSet(new CidSet(cur))\n }, new CidSet())\n const found = await storage.getBlocks(allCids.toList())\n if (found.missing.length > 0) {\n throw new MissingBlocksError('writeRecordsToCarStream', found.missing)\n }\n for (const block of found.blocks.entries()) {\n yield block\n }\n}\n"]}
|