@atproto/repo 0.9.0 → 0.10.0-next.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 +34 -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 +17 -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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @atproto/repo
|
|
2
2
|
|
|
3
|
+
## 0.10.0-next.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#4929](https://github.com/bluesky-social/atproto/pull/4929) [`bb7491c`](https://github.com/bluesky-social/atproto/commit/bb7491c29e06181e1d2f8cf6eb454f9bb8ab961b) Thanks [@devinivy](https://github.com/devinivy)! - **BREAKING:** Drop support for Node.js 18 and 20. Node.js 22 is now the minimum supported version. Docker images now use Node.js 24.
|
|
8
|
+
|
|
9
|
+
- [#4943](https://github.com/bluesky-social/atproto/pull/4943) [`07ae5d4`](https://github.com/bluesky-social/atproto/commit/07ae5d4452df51e045e0239da7a04cf0bc154028) Thanks [@devinivy](https://github.com/devinivy)! - **BREAKING:** Convert to pure ESM. All packages now ship `"type": "module"` with ES module output and Node16 module resolution.
|
|
10
|
+
|
|
11
|
+
Node.js 22's `require()` compatibility layer can still load these packages in CommonJS code.
|
|
12
|
+
|
|
13
|
+
- [#4930](https://github.com/bluesky-social/atproto/pull/4930) [`042df15`](https://github.com/bluesky-social/atproto/commit/042df15087c0e62cd1e715fcbf58852fab875af9) Thanks [@devinivy](https://github.com/devinivy)! - Build with TypeScript 6.0. Emitted `.d.ts` files now use TypeScript 6's stricter `Uint8Array<ArrayBuffer>` typing in places where Web/Node APIs require buffer-backed (not shared-memory) byte arrays. Consumers compiling against these types on older TypeScript should see no runtime impact, but may need to widen or cast in spots that previously relied on `Uint8Array` defaulting to `<ArrayBufferLike>`.
|
|
14
|
+
|
|
15
|
+
Internal: tsconfig `moduleResolution: "node"` is silenced via `ignoreDeprecations: "6.0"` for now; the proper migration to `node16`/`bundler` resolution is deferred.
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [[`bb7491c`](https://github.com/bluesky-social/atproto/commit/bb7491c29e06181e1d2f8cf6eb454f9bb8ab961b), [`07ae5d4`](https://github.com/bluesky-social/atproto/commit/07ae5d4452df51e045e0239da7a04cf0bc154028), [`042df15`](https://github.com/bluesky-social/atproto/commit/042df15087c0e62cd1e715fcbf58852fab875af9)]:
|
|
20
|
+
- @atproto/common@0.6.0-next.0
|
|
21
|
+
- @atproto/common-web@0.5.0-next.0
|
|
22
|
+
- @atproto/crypto@0.5.0-next.0
|
|
23
|
+
- @atproto/lex-cbor@0.1.0-next.0
|
|
24
|
+
- @atproto/lex-data@0.1.0-next.0
|
|
25
|
+
- @atproto/syntax@0.6.0-next.0
|
|
26
|
+
|
|
27
|
+
## 0.9.1
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- Updated dependencies [[`c62651d`](https://github.com/bluesky-social/atproto/commit/c62651dd69f1e18bd854b66e499b91fee9eaa856), [`f6f100c`](https://github.com/bluesky-social/atproto/commit/f6f100c33700a7ff58a1458109cc7420131feed0), [`c62651d`](https://github.com/bluesky-social/atproto/commit/c62651dd69f1e18bd854b66e499b91fee9eaa856), [`c62651d`](https://github.com/bluesky-social/atproto/commit/c62651dd69f1e18bd854b66e499b91fee9eaa856)]:
|
|
32
|
+
- @atproto/lex-data@0.0.15
|
|
33
|
+
- @atproto/common@0.5.16
|
|
34
|
+
- @atproto/common-web@0.4.20
|
|
35
|
+
- @atproto/lex-cbor@0.0.16
|
|
36
|
+
|
|
3
37
|
## 0.9.0
|
|
4
38
|
|
|
5
39
|
### Minor Changes
|
package/dist/block-map.js
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const lex_cbor_1 = require("@atproto/lex-cbor");
|
|
5
|
-
const lex_data_1 = require("@atproto/lex-data");
|
|
6
|
-
class BlockMap {
|
|
1
|
+
import { encode } from '@atproto/lex-cbor';
|
|
2
|
+
import { cidForCbor, parseCid, ui8Equals, } from '@atproto/lex-data';
|
|
3
|
+
export class BlockMap {
|
|
7
4
|
constructor(entries) {
|
|
8
|
-
|
|
9
|
-
enumerable: true,
|
|
10
|
-
configurable: true,
|
|
11
|
-
writable: true,
|
|
12
|
-
value: new Map()
|
|
13
|
-
});
|
|
5
|
+
this.map = new Map();
|
|
14
6
|
if (entries) {
|
|
15
7
|
for (const [cid, bytes] of entries) {
|
|
16
8
|
this.set(cid, bytes);
|
|
@@ -18,8 +10,8 @@ class BlockMap {
|
|
|
18
10
|
}
|
|
19
11
|
}
|
|
20
12
|
async add(value) {
|
|
21
|
-
const bytes =
|
|
22
|
-
const cid = await
|
|
13
|
+
const bytes = encode(value);
|
|
14
|
+
const cid = await cidForCbor(bytes);
|
|
23
15
|
this.set(cid, bytes);
|
|
24
16
|
return cid;
|
|
25
17
|
}
|
|
@@ -86,7 +78,7 @@ class BlockMap {
|
|
|
86
78
|
const otherBytes = other.get(cid);
|
|
87
79
|
if (!otherBytes)
|
|
88
80
|
return false;
|
|
89
|
-
if (!
|
|
81
|
+
if (!ui8Equals(bytes, otherBytes)) {
|
|
90
82
|
return false;
|
|
91
83
|
}
|
|
92
84
|
}
|
|
@@ -94,7 +86,7 @@ class BlockMap {
|
|
|
94
86
|
}
|
|
95
87
|
*keys() {
|
|
96
88
|
for (const key of this.map.keys()) {
|
|
97
|
-
yield
|
|
89
|
+
yield parseCid(key);
|
|
98
90
|
}
|
|
99
91
|
}
|
|
100
92
|
*values() {
|
|
@@ -102,13 +94,12 @@ class BlockMap {
|
|
|
102
94
|
}
|
|
103
95
|
*[Symbol.iterator]() {
|
|
104
96
|
for (const [key, value] of this.map) {
|
|
105
|
-
yield [
|
|
97
|
+
yield [parseCid(key), value];
|
|
106
98
|
}
|
|
107
99
|
}
|
|
108
100
|
}
|
|
109
|
-
exports.BlockMap = BlockMap;
|
|
110
101
|
function toEntry([cid, bytes]) {
|
|
111
102
|
return { cid, bytes };
|
|
112
103
|
}
|
|
113
|
-
|
|
104
|
+
export default BlockMap;
|
|
114
105
|
//# sourceMappingURL=block-map.js.map
|
package/dist/block-map.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-map.js","sourceRoot":"","sources":["../src/block-map.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"block-map.js","sourceRoot":"","sources":["../src/block-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,EAGL,UAAU,EACV,QAAQ,EACR,SAAS,GACV,MAAM,mBAAmB,CAAA;AAE1B,MAAM,OAAO,QAAQ;IAGnB,YAAY,OAA0D;QAF9D,QAAG,GAA4B,IAAI,GAAG,EAAE,CAAA;QAG9C,IAAI,OAAO,EAAE,CAAC;YACZ,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,OAAO,EAAE,CAAC;gBACnC,IAAI,CAA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AA;YACjC,IAAI,CAAC,UAAU;gBAAE,OAAO,KAAK,CAAA;YAC7B,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;gBAClC,OAAO,KAAK,CAAA;YACd,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,CAAC,IAAI;QACH,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;YAClC,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAED,CAAC,MAAM;QACL,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAA;IAC1B,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;YACpC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAA;QAC9B,CAAC;IACH,CAAC;CACF;AAED,SAAS,OAAO,CAAC,CAAC,GAAG,EAAE,KAAK,CAA6B;IACvD,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,CAAA;AACvB,CAAC;AAOD,eAAe,QAAQ,CAAA","sourcesContent":["import { encode } from '@atproto/lex-cbor'\nimport {\n Cid,\n LexValue,\n cidForCbor,\n parseCid,\n ui8Equals,\n} from '@atproto/lex-data'\n\nexport class BlockMap implements Iterable<[cid: Cid, bytes: Uint8Array]> {\n private map: Map<string, Uint8Array> = new Map()\n\n constructor(entries?: Iterable<readonly [cid: Cid, bytes: Uint8Array]>) {\n if (entries) {\n for (const [cid, bytes] of entries) {\n this.set(cid, bytes)\n }\n }\n }\n\n async add(value: LexValue): Promise<Cid> {\n const bytes = encode(value)\n const cid = await cidForCbor(bytes)\n this.set(cid, bytes)\n return cid\n }\n\n set(cid: Cid, bytes: Uint8Array): BlockMap {\n this.map.set(cid.toString(), bytes)\n return this\n }\n\n get(cid: Cid): Uint8Array | undefined {\n return this.map.get(cid.toString())\n }\n\n delete(cid: Cid): BlockMap {\n this.map.delete(cid.toString())\n return this\n }\n\n getMany(cids: Cid[]): { blocks: BlockMap; missing: Cid[] } {\n const missing: Cid[] = []\n const blocks = new BlockMap()\n for (const cid of cids) {\n const got = this.map.get(cid.toString())\n if (got) {\n blocks.set(cid, got)\n } else {\n missing.push(cid)\n }\n }\n return { blocks, missing }\n }\n\n has(cid: Cid): boolean {\n return this.map.has(cid.toString())\n }\n\n clear(): void {\n this.map.clear()\n }\n\n forEach(cb: (bytes: Uint8Array, cid: Cid) => void): void {\n for (const [cid, bytes] of this) cb(bytes, cid)\n }\n\n entries(): Entry[] {\n return Array.from(this, toEntry)\n }\n\n cids(): Cid[] {\n return Array.from(this.keys())\n }\n\n addMap(toAdd: BlockMap): BlockMap {\n for (const [cid, bytes] of toAdd) this.set(cid, bytes)\n return this\n }\n\n get size(): number {\n return this.map.size\n }\n\n get byteSize(): number {\n let size = 0\n for (const bytes of this.values()) size += bytes.length\n return size\n }\n\n equals(other: BlockMap): boolean {\n if (this.size !== other.size) {\n return false\n }\n for (const [cid, bytes] of this) {\n const otherBytes = other.get(cid)\n if (!otherBytes) return false\n if (!ui8Equals(bytes, otherBytes)) {\n return false\n }\n }\n return true\n }\n\n *keys(): Generator<Cid, void, unknown> {\n for (const key of this.map.keys()) {\n yield parseCid(key)\n }\n }\n\n *values(): Generator<Uint8Array, void, unknown> {\n yield* this.map.values()\n }\n\n *[Symbol.iterator](): Generator<[Cid, Uint8Array], void, unknown> {\n for (const [key, value] of this.map) {\n yield [parseCid(key), value]\n }\n }\n}\n\nfunction toEntry([cid, bytes]: readonly [Cid, Uint8Array]): Entry {\n return { cid, bytes }\n}\n\ntype Entry = {\n cid: Cid\n bytes: Uint8Array\n}\n\nexport default BlockMap\n"]}
|
package/dist/car.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Cid } from '@atproto/lex-data';
|
|
2
|
-
import { BlockMap } from './block-map';
|
|
3
|
-
import { CarBlock } from './types';
|
|
2
|
+
import { BlockMap } from './block-map.js';
|
|
3
|
+
import { CarBlock } from './types.js';
|
|
4
4
|
export declare function writeCarStream(root: Cid | null, blocks: AsyncIterable<CarBlock>): AsyncIterable<Uint8Array>;
|
|
5
5
|
export declare function blocksToCarFile(root: Cid | null, blocks: BlockMap): Promise<Uint8Array>;
|
|
6
6
|
export declare const blocksToCarStream: (root: Cid | null, blocks: BlockMap) => AsyncIterable<Uint8Array>;
|
package/dist/car.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"car.d.ts","sourceRoot":"","sources":["../src/car.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"car.d.ts","sourceRoot":"","sources":["../src/car.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAA4B,MAAM,mBAAmB,CAAA;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAAE,QAAQ,EAAU,MAAM,YAAY,CAAA;AAG7C,wBAAuB,cAAc,CACnC,IAAI,EAAE,GAAG,GAAG,IAAI,EAChB,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,GAC9B,aAAa,CAAC,UAAU,CAAC,CAgB3B;AAED,wBAAsB,eAAe,CACnC,IAAI,EAAE,GAAG,GAAG,IAAI,EAChB,MAAM,EAAE,QAAQ,GACf,OAAO,CAAC,UAAU,CAAC,CAErB;AAED,eAAO,MAAM,iBAAiB,GAC5B,MAAM,GAAG,GAAG,IAAI,EAChB,QAAQ,QAAQ,KACf,aAAa,CAAC,UAAU,CAE1B,CAAA;AAQD,MAAM,MAAM,cAAc,GAAG;IAC3B;;OAEG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAA;CAC9B,CAAA;AAED,eAAO,MAAM,OAAO,GAClB,OAAO,UAAU,EACjB,OAAO,cAAc,K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|
|
8
|
-
|
|
9
|
-
|
|
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.readCarReader = exports.readCarStream = exports.readCarWithRoot = exports.readCar = exports.blocksToCarStream = void 0;
|
|
37
|
-
exports.writeCarStream = writeCarStream;
|
|
38
|
-
exports.blocksToCarFile = blocksToCarFile;
|
|
39
|
-
exports.verifyIncomingCarBlocks = verifyIncomingCarBlocks;
|
|
40
|
-
const promises_1 = require("node:timers/promises");
|
|
41
|
-
const varint = __importStar(require("varint"));
|
|
42
|
-
const cbor = __importStar(require("@atproto/lex-cbor"));
|
|
43
|
-
const lex_data_1 = require("@atproto/lex-data");
|
|
44
|
-
const block_map_1 = require("./block-map");
|
|
45
|
-
const types_1 = require("./types");
|
|
46
|
-
const util_1 = require("./util");
|
|
47
|
-
async function* writeCarStream(root, blocks) {
|
|
1
|
+
import { setImmediate } from 'node:timers/promises';
|
|
2
|
+
// eslint-disable-next-line import/default, import/no-named-as-default-member
|
|
3
|
+
import varint from 'varint';
|
|
4
|
+
import * as cbor from '@atproto/lex-cbor';
|
|
5
|
+
import { decodeCid, isCidForBytes } from '@atproto/lex-data';
|
|
6
|
+
import { BlockMap } from './block-map.js';
|
|
7
|
+
import { schema } from './types.js';
|
|
8
|
+
import { concatBytesAsync } from './util.js';
|
|
9
|
+
export async function* writeCarStream(root, blocks) {
|
|
48
10
|
const header = new Uint8Array(cbor.encode({
|
|
49
11
|
version: 1,
|
|
50
12
|
roots: root ? [root] : [],
|
|
@@ -57,29 +19,27 @@ async function* writeCarStream(root, blocks) {
|
|
|
57
19
|
yield block.bytes;
|
|
58
20
|
}
|
|
59
21
|
}
|
|
60
|
-
async function blocksToCarFile(root, blocks) {
|
|
61
|
-
return
|
|
22
|
+
export async function blocksToCarFile(root, blocks) {
|
|
23
|
+
return concatBytesAsync(blocksToCarStream(root, blocks));
|
|
62
24
|
}
|
|
63
|
-
const blocksToCarStream = (root, blocks) => {
|
|
25
|
+
export const blocksToCarStream = (root, blocks) => {
|
|
64
26
|
return writeCarStream(root, iterateBlocks(blocks));
|
|
65
27
|
};
|
|
66
|
-
exports.blocksToCarStream = blocksToCarStream;
|
|
67
28
|
async function* iterateBlocks(blocks) {
|
|
68
29
|
for (const entry of blocks.entries()) {
|
|
69
30
|
yield { cid: entry.cid, bytes: entry.bytes };
|
|
70
31
|
}
|
|
71
32
|
}
|
|
72
|
-
const readCar = async (bytes, opts) => {
|
|
73
|
-
const { roots, blocks } = await
|
|
74
|
-
const blockMap = new
|
|
33
|
+
export const readCar = async (bytes, opts) => {
|
|
34
|
+
const { roots, blocks } = await readCarReader(new Ui8Reader(bytes), opts);
|
|
35
|
+
const blockMap = new BlockMap();
|
|
75
36
|
for await (const block of blocks) {
|
|
76
37
|
blockMap.set(block.cid, block.bytes);
|
|
77
38
|
}
|
|
78
39
|
return { roots, blocks: blockMap };
|
|
79
40
|
};
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
const { roots, blocks } = await (0, exports.readCar)(bytes, opts);
|
|
41
|
+
export const readCarWithRoot = async (bytes, opts) => {
|
|
42
|
+
const { roots, blocks } = await readCar(bytes, opts);
|
|
83
43
|
if (roots.length !== 1) {
|
|
84
44
|
throw new Error(`Expected one root, got ${roots.length}`);
|
|
85
45
|
}
|
|
@@ -89,12 +49,10 @@ const readCarWithRoot = async (bytes, opts) => {
|
|
|
89
49
|
blocks,
|
|
90
50
|
};
|
|
91
51
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return (0, exports.readCarReader)(new BufferedReader(car), opts);
|
|
52
|
+
export const readCarStream = async (car, opts) => {
|
|
53
|
+
return readCarReader(new BufferedReader(car), opts);
|
|
95
54
|
};
|
|
96
|
-
|
|
97
|
-
const readCarReader = async (reader, opts) => {
|
|
55
|
+
export const readCarReader = async (reader, opts) => {
|
|
98
56
|
try {
|
|
99
57
|
const headerSize = await readVarint(reader);
|
|
100
58
|
if (headerSize === null) {
|
|
@@ -102,7 +60,7 @@ const readCarReader = async (reader, opts) => {
|
|
|
102
60
|
}
|
|
103
61
|
const headerBytes = await reader.read(headerSize);
|
|
104
62
|
const header = cbor.decode(headerBytes);
|
|
105
|
-
const result =
|
|
63
|
+
const result = schema.carHeader.safeParse(header);
|
|
106
64
|
if (!result.success) {
|
|
107
65
|
throw new Error('Could not parse CAR header', { cause: result.error });
|
|
108
66
|
}
|
|
@@ -116,7 +74,6 @@ const readCarReader = async (reader, opts) => {
|
|
|
116
74
|
throw err;
|
|
117
75
|
}
|
|
118
76
|
};
|
|
119
|
-
exports.readCarReader = readCarReader;
|
|
120
77
|
const readCarBlocksIter = (reader, opts) => {
|
|
121
78
|
let generator = readCarBlocksIterGenerator(reader);
|
|
122
79
|
if (!opts?.skipCidVerification) {
|
|
@@ -146,14 +103,14 @@ async function* readCarBlocksIterGenerator(reader) {
|
|
|
146
103
|
break;
|
|
147
104
|
}
|
|
148
105
|
const blockBytes = await reader.read(blockSize);
|
|
149
|
-
const cid =
|
|
106
|
+
const cid = decodeCid(blockBytes.subarray(0, 36));
|
|
150
107
|
const bytes = blockBytes.subarray(36);
|
|
151
108
|
yield { cid, bytes };
|
|
152
109
|
// yield to the event loop every 25 blocks
|
|
153
110
|
// in the case the incoming CAR is synchronous, this can end up jamming up the thread
|
|
154
111
|
blocks++;
|
|
155
112
|
if (blocks % 25 === 0) {
|
|
156
|
-
await
|
|
113
|
+
await setImmediate();
|
|
157
114
|
}
|
|
158
115
|
}
|
|
159
116
|
}
|
|
@@ -161,9 +118,9 @@ async function* readCarBlocksIterGenerator(reader) {
|
|
|
161
118
|
await reader.close();
|
|
162
119
|
}
|
|
163
120
|
}
|
|
164
|
-
async function* verifyIncomingCarBlocks(car) {
|
|
121
|
+
export async function* verifyIncomingCarBlocks(car) {
|
|
165
122
|
for await (const block of car) {
|
|
166
|
-
if (!(await
|
|
123
|
+
if (!(await isCidForBytes(block.cid, block.bytes))) {
|
|
167
124
|
throw new Error(`Not a valid CID for bytes (${block.cid.toString()})`);
|
|
168
125
|
}
|
|
169
126
|
yield block;
|
|
@@ -192,24 +149,9 @@ const readVarint = async (reader) => {
|
|
|
192
149
|
};
|
|
193
150
|
class Ui8Reader {
|
|
194
151
|
constructor(bytes) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
writable: true,
|
|
199
|
-
value: bytes
|
|
200
|
-
});
|
|
201
|
-
Object.defineProperty(this, "idx", {
|
|
202
|
-
enumerable: true,
|
|
203
|
-
configurable: true,
|
|
204
|
-
writable: true,
|
|
205
|
-
value: 0
|
|
206
|
-
});
|
|
207
|
-
Object.defineProperty(this, "isDone", {
|
|
208
|
-
enumerable: true,
|
|
209
|
-
configurable: true,
|
|
210
|
-
writable: true,
|
|
211
|
-
value: false
|
|
212
|
-
});
|
|
152
|
+
this.bytes = bytes;
|
|
153
|
+
this.idx = 0;
|
|
154
|
+
this.isDone = false;
|
|
213
155
|
}
|
|
214
156
|
async read(bytesToRead) {
|
|
215
157
|
const value = this.bytes.subarray(this.idx, this.idx + bytesToRead);
|
|
@@ -228,25 +170,9 @@ class Ui8Reader {
|
|
|
228
170
|
*/
|
|
229
171
|
class BufferedReader {
|
|
230
172
|
constructor(stream) {
|
|
231
|
-
|
|
232
|
-
enumerable: true,
|
|
233
|
-
configurable: true,
|
|
234
|
-
writable: true,
|
|
235
|
-
value: void 0
|
|
236
|
-
});
|
|
237
|
-
Object.defineProperty(this, "isDone", {
|
|
238
|
-
enumerable: true,
|
|
239
|
-
configurable: true,
|
|
240
|
-
writable: true,
|
|
241
|
-
value: false
|
|
242
|
-
});
|
|
173
|
+
this.isDone = false;
|
|
243
174
|
/** fifo list of chunks to consume */
|
|
244
|
-
|
|
245
|
-
enumerable: true,
|
|
246
|
-
configurable: true,
|
|
247
|
-
writable: true,
|
|
248
|
-
value: []
|
|
249
|
-
});
|
|
175
|
+
this.chunks = [];
|
|
250
176
|
this.iterator =
|
|
251
177
|
Symbol.asyncIterator in stream
|
|
252
178
|
? stream[Symbol.asyncIterator]()
|
package/dist/car.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"car.js","sourceRoot":"","sources":["../src/car.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,wCAmBC;AAED,0CAKC;AA4ID,0DASC;AAvLD,mDAAmD;AACnD,+CAAgC;AAChC,wDAAyC;AACzC,gDAAiE;AACjE,2CAAsC;AACtC,mCAA0C;AAC1C,iCAAyC;AAElC,KAAK,SAAS,CAAC,CAAC,cAAc,CACnC,IAAgB,EAChB,MAA+B;IAE/B,MAAM,MAAM,GAAG,IAAI,UAAU,CAC3B,IAAI,CAAC,MAAM,CAAC;QACV,OAAO,EAAE,CAAC;QACV,KAAK,EAAE,IAAI,CAAC,CAAC,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QACzC,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,CAAA;QAClC,IAAI,cAAc,GAAG,UAAU,CAAC,UAAU,EAAE,CAAC;YAC3C,uEAAuE;YACvE,mDAAmD;YAEnD,yEAAyE;YACzE,cAAc;YAEd,uDAAuD;YACvD,gDAAgD;YAEhD,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,UAAU,CAC7B,UAAU,CAAC,MAAM,EACjB,UAAU,CAAC,UAAU,GAAG,cAAc,EACtC,UAAU,CAAC,UAAU,GAAG,cAAc,CACvC,CAAA;YACD,OAAO,IAAI,UAAU,CACnB,UAAU,CAAC,MAAM,EACjB,UAAU,CAAC,UAAU,EACrB,cAAc,CACf,CAAA;QACH,CAAC;aAAM,CAAC;YACN,gDAAgD;YAChD,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAA;YACnB,OAAO,UAAU,CAAA;QACnB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC;YACH,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;gBACzC,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAA;YAC9B,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;YAClB,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAA;QACxB,CAAC;IACH,CAAC;CACF","sourcesContent":["import { setImmediate } from 'node:timers/promises'\nimport * as varint from 'varint'\nimport * as cbor from '@atproto/lex-cbor'\nimport { Cid, decodeCid, isCidForBytes } from '@atproto/lex-data'\nimport { BlockMap } from './block-map'\nimport { CarBlock, schema } from './types'\nimport { concatBytesAsync } from './util'\n\nexport async function* writeCarStream(\n root: Cid | null,\n blocks: AsyncIterable<CarBlock>,\n): AsyncIterable<Uint8Array> {\n const header = new Uint8Array(\n cbor.encode({\n version: 1,\n roots: root ? [root] : [],\n }),\n )\n yield new Uint8Array(varint.encode(header.byteLength))\n yield header\n for await (const block of blocks) {\n yield new Uint8Array(\n varint.encode(block.cid.bytes.byteLength + block.bytes.byteLength),\n )\n yield block.cid.bytes\n yield block.bytes\n }\n}\n\nexport async function blocksToCarFile(\n root: Cid | null,\n blocks: BlockMap,\n): Promise<Uint8Array> {\n return concatBytesAsync(blocksToCarStream(root, blocks))\n}\n\nexport const blocksToCarStream = (\n root: Cid | null,\n blocks: BlockMap,\n): AsyncIterable<Uint8Array> => {\n return writeCarStream(root, iterateBlocks(blocks))\n}\n\nasync function* iterateBlocks(blocks: BlockMap) {\n for (const entry of blocks.entries()) {\n yield { cid: entry.cid, bytes: entry.bytes }\n }\n}\n\nexport type ReadCarOptions = {\n /**\n * When true, does not verify CID-to-content mapping within CAR.\n */\n skipCidVerification?: boolean\n}\n\nexport const readCar = async (\n bytes: Uint8Array,\n opts?: ReadCarOptions,\n): Promise<{ roots: Cid[]; blocks: BlockMap }> => {\n const { roots, blocks } = await readCarReader(new Ui8Reader(bytes), opts)\n const blockMap = new BlockMap()\n for await (const block of blocks) {\n blockMap.set(block.cid, block.bytes)\n }\n return { roots, blocks: blockMap }\n}\n\nexport const readCarWithRoot = async (\n bytes: Uint8Array,\n opts?: ReadCarOptions,\n): Promise<{ root: Cid; blocks: BlockMap }> => {\n const { roots, blocks } = await readCar(bytes, opts)\n if (roots.length !== 1) {\n throw new Error(`Expected one root, got ${roots.length}`)\n }\n const root = roots[0]\n return {\n root,\n blocks,\n }\n}\nexport type CarBlockIterable = AsyncGenerator<CarBlock, void, unknown> & {\n dump: () => Promise<void>\n}\n\nexport const readCarStream = async (\n car: Iterable<Uint8Array> | AsyncIterable<Uint8Array>,\n opts?: ReadCarOptions,\n): Promise<{\n roots: Cid[]\n blocks: CarBlockIterable\n}> => {\n return readCarReader(new BufferedReader(car), opts)\n}\n\nexport const readCarReader = async (\n reader: BytesReader,\n opts?: ReadCarOptions,\n): Promise<{\n roots: Cid[]\n blocks: CarBlockIterable\n}> => {\n try {\n const headerSize = await readVarint(reader)\n if (headerSize === null) {\n throw new Error('Could not parse CAR header')\n }\n const headerBytes = await reader.read(headerSize)\n const header = cbor.decode(headerBytes)\n const result = schema.carHeader.safeParse(header)\n if (!result.success) {\n throw new Error('Could not parse CAR header', { cause: result.error })\n }\n return {\n roots: result.data.roots,\n blocks: readCarBlocksIter(reader, opts),\n }\n } catch (err) {\n await reader.close()\n throw err\n }\n}\n\nconst readCarBlocksIter = (\n reader: BytesReader,\n opts?: ReadCarOptions,\n): CarBlockIterable => {\n let generator = readCarBlocksIterGenerator(reader)\n if (!opts?.skipCidVerification) {\n generator = verifyIncomingCarBlocks(generator)\n }\n return Object.assign(generator, {\n async dump() {\n // try/finally to ensure that reader.close is called even if blocks.return throws.\n try {\n // Prevent the iterator from being started after this method is called.\n await generator.return()\n } finally {\n // @NOTE the \"finally\" block of the async generator won't be called\n // if the iteration was never started so we need to manually close here.\n await reader.close()\n }\n },\n })\n}\n\nasync function* readCarBlocksIterGenerator(\n reader: BytesReader,\n): AsyncGenerator<CarBlock, void, unknown> {\n let blocks = 0\n try {\n while (!reader.isDone) {\n const blockSize = await readVarint(reader)\n if (blockSize === null) {\n break\n }\n const blockBytes = await reader.read(blockSize)\n const cid = decodeCid(blockBytes.subarray(0, 36))\n const bytes = blockBytes.subarray(36)\n yield { cid, bytes }\n\n // yield to the event loop every 25 blocks\n // in the case the incoming CAR is synchronous, this can end up jamming up the thread\n blocks++\n if (blocks % 25 === 0) {\n await setImmediate()\n }\n }\n } finally {\n await reader.close()\n }\n}\n\nexport async function* verifyIncomingCarBlocks(\n car: AsyncIterable<CarBlock>,\n): AsyncGenerator<CarBlock, void, unknown> {\n for await (const block of car) {\n if (!(await isCidForBytes(block.cid, block.bytes))) {\n throw new Error(`Not a valid CID for bytes (${block.cid.toString()})`)\n }\n yield block\n }\n}\n\nconst readVarint = async (reader: BytesReader): Promise<number | null> => {\n let done = false\n const bytes: Uint8Array[] = []\n while (!done) {\n const byte = await reader.read(1)\n if (byte.byteLength === 0) {\n if (bytes.length > 0) {\n throw new Error('could not parse varint')\n } else {\n return null\n }\n }\n bytes.push(byte)\n if (byte[0] < 128) {\n done = true\n }\n }\n const concatted = Buffer.concat(bytes)\n return varint.decode(concatted)\n}\n\ninterface BytesReader {\n isDone: boolean\n read(bytesToRead: number): Promise<Uint8Array>\n close(): Promise<void>\n}\n\nclass Ui8Reader implements BytesReader {\n idx = 0\n isDone = false\n\n constructor(public bytes: Uint8Array) {}\n\n async read(bytesToRead: number): Promise<Uint8Array> {\n const value = this.bytes.subarray(this.idx, this.idx + bytesToRead)\n this.idx += bytesToRead\n if (this.idx >= this.bytes.length) {\n this.isDone = true\n }\n return value\n }\n\n async close(): Promise<void> {}\n}\n\n/**\n * This code was optimized for performance. See\n * {@link https://github.com/bluesky-social/atproto/pull/4729 #4729} for more details\n * and benchmarks.\n */\nclass BufferedReader implements BytesReader {\n iterator: Iterator<Uint8Array> | AsyncIterator<Uint8Array>\n isDone = false\n\n /** fifo list of chunks to consume */\n private chunks: Uint8Array[] = []\n\n constructor(stream: Iterable<Uint8Array> | AsyncIterable<Uint8Array>) {\n this.iterator =\n Symbol.asyncIterator in stream\n ? stream[Symbol.asyncIterator]()\n : stream[Symbol.iterator]()\n }\n\n /** Number of bytes currently buffered and available for reading */\n get bufferedByteLength() {\n let total = 0\n for (let i = 0; i < this.chunks.length; i++) {\n total += this.chunks[i].byteLength\n }\n return total\n }\n\n /**\n * @note concurrent reads are **NOT** supported by the current implementation\n * and would require call to readUntilBuffered to be using a fifo lock for\n * read()s to be processed in fifo order.\n */\n async read(bytesToRead: number): Promise<Uint8Array> {\n const bytesNeeded = bytesToRead - this.bufferedByteLength\n if (bytesNeeded > 0 && !this.isDone) {\n await this.readUntilBuffered(bytesNeeded)\n }\n\n const resultLength = Math.min(bytesToRead, this.bufferedByteLength)\n if (resultLength <= 0) return new Uint8Array()\n\n const firstChunk = this.consumeChunk(resultLength)\n if (firstChunk.byteLength === resultLength) {\n // If the data consumed from the first chunk contains all we need, return\n // it as-is. This allows to avoid any copy operation.\n return firstChunk\n }\n\n // The first chunk does not have all the data we need. We have to copy\n // multiple chunks into a larger buffer\n const result = new Uint8Array(resultLength)\n let resultWriteIndex = 0\n\n // Copy the first chunk into the result buffer\n result.set(firstChunk, resultWriteIndex)\n resultWriteIndex += firstChunk.byteLength\n\n // Copy more chunks as needed (we use do-while because we *know* we need\n // more than one chunk)\n do {\n const missingLength = resultLength - resultWriteIndex\n const currentChunk = this.consumeChunk(missingLength)\n\n result.set(currentChunk, resultWriteIndex)\n resultWriteIndex += currentChunk.byteLength\n } while (resultWriteIndex < resultLength)\n\n return result\n }\n\n private async readUntilBuffered(bytesNeeded: number) {\n let bytesRead = 0\n while (bytesRead < bytesNeeded) {\n const next = await this.iterator.next()\n if (next.done) {\n this.isDone = true\n break\n } else {\n this.chunks.push(next.value)\n bytesRead += next.value.byteLength\n }\n }\n return bytesRead\n }\n\n private consumeChunk(bytesToConsume: number) {\n const firstChunk = this.chunks[0]!\n if (bytesToConsume < firstChunk.byteLength) {\n // return a sub-view of the data being read and replace the first chunk\n // with a sub-view that does not contain that data.\n\n // @NOTE for some reason, subarray() revealed to be 7-8% slower in NodeJS\n // benchmarks.\n\n // this.chunks[0] = firstChunk.subarray(bytesToConsume)\n // return firstChunk.subarray(0, bytesToConsume)\n\n this.chunks[0] = new Uint8Array(\n firstChunk.buffer,\n firstChunk.byteOffset + bytesToConsume,\n firstChunk.byteLength - bytesToConsume,\n )\n return new Uint8Array(\n firstChunk.buffer,\n firstChunk.byteOffset,\n bytesToConsume,\n )\n } else {\n // First chunk is being read in full, discard it\n this.chunks.shift()\n return firstChunk\n }\n }\n\n async close(): Promise<void> {\n try {\n if (!this.isDone && this.iterator.return) {\n await this.iterator.return()\n }\n } finally {\n this.isDone = true\n this.chunks.length = 0\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"car.js","source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AQ,CAAC,MAAM,EAAE,CAAA;YAC9B,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;YAClB,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAA;QACxB,CAAC;IACH,CAAC;CACF","sourcesContent":["import { setImmediate } from 'node:timers/promises'\n// eslint-disable-next-line import/default, import/no-named-as-default-member\nimport varint from 'varint'\nimport * as cbor from '@atproto/lex-cbor'\nimport { Cid, decodeCid, isCidForBytes } from '@atproto/lex-data'\nimport { BlockMap } from './block-map.js'\nimport { CarBlock, schema } from './types.js'\nimport { concatBytesAsync } from './util.js'\n\nexport async function* writeCarStream(\n root: Cid | null,\n blocks: AsyncIterable<CarBlock>,\n): AsyncIterable<Uint8Array> {\n const header = new Uint8Array(\n cbor.encode({\n version: 1,\n roots: root ? [root] : [],\n }),\n )\n yield new Uint8Array(varint.encode(header.byteLength))\n yield header\n for await (const block of blocks) {\n yield new Uint8Array(\n varint.encode(block.cid.bytes.byteLength + block.bytes.byteLength),\n )\n yield block.cid.bytes\n yield block.bytes\n }\n}\n\nexport async function blocksToCarFile(\n root: Cid | null,\n blocks: BlockMap,\n): Promise<Uint8Array> {\n return concatBytesAsync(blocksToCarStream(root, blocks))\n}\n\nexport const blocksToCarStream = (\n root: Cid | null,\n blocks: BlockMap,\n): AsyncIterable<Uint8Array> => {\n return writeCarStream(root, iterateBlocks(blocks))\n}\n\nasync function* iterateBlocks(blocks: BlockMap) {\n for (const entry of blocks.entries()) {\n yield { cid: entry.cid, bytes: entry.bytes }\n }\n}\n\nexport type ReadCarOptions = {\n /**\n * When true, does not verify CID-to-content mapping within CAR.\n */\n skipCidVerification?: boolean\n}\n\nexport const readCar = async (\n bytes: Uint8Array,\n opts?: ReadCarOptions,\n): Promise<{ roots: Cid[]; blocks: BlockMap }> => {\n const { roots, blocks } = await readCarReader(new Ui8Reader(bytes), opts)\n const blockMap = new BlockMap()\n for await (const block of blocks) {\n blockMap.set(block.cid, block.bytes)\n }\n return { roots, blocks: blockMap }\n}\n\nexport const readCarWithRoot = async (\n bytes: Uint8Array,\n opts?: ReadCarOptions,\n): Promise<{ root: Cid; blocks: BlockMap }> => {\n const { roots, blocks } = await readCar(bytes, opts)\n if (roots.length !== 1) {\n throw new Error(`Expected one root, got ${roots.length}`)\n }\n const root = roots[0]\n return {\n root,\n blocks,\n }\n}\nexport type CarBlockIterable = AsyncGenerator<CarBlock, void, unknown> & {\n dump: () => Promise<void>\n}\n\nexport const readCarStream = async (\n car: Iterable<Uint8Array> | AsyncIterable<Uint8Array>,\n opts?: ReadCarOptions,\n): Promise<{\n roots: Cid[]\n blocks: CarBlockIterable\n}> => {\n return readCarReader(new BufferedReader(car), opts)\n}\n\nexport const readCarReader = async (\n reader: BytesReader,\n opts?: ReadCarOptions,\n): Promise<{\n roots: Cid[]\n blocks: CarBlockIterable\n}> => {\n try {\n const headerSize = await readVarint(reader)\n if (headerSize === null) {\n throw new Error('Could not parse CAR header')\n }\n const headerBytes = await reader.read(headerSize)\n const header = cbor.decode(headerBytes)\n const result = schema.carHeader.safeParse(header)\n if (!result.success) {\n throw new Error('Could not parse CAR header', { cause: result.error })\n }\n return {\n roots: result.data.roots,\n blocks: readCarBlocksIter(reader, opts),\n }\n } catch (err) {\n await reader.close()\n throw err\n }\n}\n\nconst readCarBlocksIter = (\n reader: BytesReader,\n opts?: ReadCarOptions,\n): CarBlockIterable => {\n let generator = readCarBlocksIterGenerator(reader)\n if (!opts?.skipCidVerification) {\n generator = verifyIncomingCarBlocks(generator)\n }\n return Object.assign(generator, {\n async dump() {\n // try/finally to ensure that reader.close is called even if blocks.return throws.\n try {\n // Prevent the iterator from being started after this method is called.\n await generator.return()\n } finally {\n // @NOTE the \"finally\" block of the async generator won't be called\n // if the iteration was never started so we need to manually close here.\n await reader.close()\n }\n },\n })\n}\n\nasync function* readCarBlocksIterGenerator(\n reader: BytesReader,\n): AsyncGenerator<CarBlock, void, unknown> {\n let blocks = 0\n try {\n while (!reader.isDone) {\n const blockSize = await readVarint(reader)\n if (blockSize === null) {\n break\n }\n const blockBytes = await reader.read(blockSize)\n const cid = decodeCid(blockBytes.subarray(0, 36))\n const bytes = blockBytes.subarray(36)\n yield { cid, bytes }\n\n // yield to the event loop every 25 blocks\n // in the case the incoming CAR is synchronous, this can end up jamming up the thread\n blocks++\n if (blocks % 25 === 0) {\n await setImmediate()\n }\n }\n } finally {\n await reader.close()\n }\n}\n\nexport async function* verifyIncomingCarBlocks(\n car: AsyncIterable<CarBlock>,\n): AsyncGenerator<CarBlock, void, unknown> {\n for await (const block of car) {\n if (!(await isCidForBytes(block.cid, block.bytes))) {\n throw new Error(`Not a valid CID for bytes (${block.cid.toString()})`)\n }\n yield block\n }\n}\n\nconst readVarint = async (reader: BytesReader): Promise<number | null> => {\n let done = false\n const bytes: Uint8Array[] = []\n while (!done) {\n const byte = await reader.read(1)\n if (byte.byteLength === 0) {\n if (bytes.length > 0) {\n throw new Error('could not parse varint')\n } else {\n return null\n }\n }\n bytes.push(byte)\n if (byte[0] < 128) {\n done = true\n }\n }\n const concatted = Buffer.concat(bytes)\n return varint.decode(concatted)\n}\n\ninterface BytesReader {\n isDone: boolean\n read(bytesToRead: number): Promise<Uint8Array>\n close(): Promise<void>\n}\n\nclass Ui8Reader implements BytesReader {\n idx = 0\n isDone = false\n\n constructor(public bytes: Uint8Array) {}\n\n async read(bytesToRead: number): Promise<Uint8Array> {\n const value = this.bytes.subarray(this.idx, this.idx + bytesToRead)\n this.idx += bytesToRead\n if (this.idx >= this.bytes.length) {\n this.isDone = true\n }\n return value\n }\n\n async close(): Promise<void> {}\n}\n\n/**\n * This code was optimized for performance. See\n * {@link https://github.com/bluesky-social/atproto/pull/4729 #4729} for more details\n * and benchmarks.\n */\nclass BufferedReader implements BytesReader {\n iterator: Iterator<Uint8Array> | AsyncIterator<Uint8Array>\n isDone = false\n\n /** fifo list of chunks to consume */\n private chunks: Uint8Array[] = []\n\n constructor(stream: Iterable<Uint8Array> | AsyncIterable<Uint8Array>) {\n this.iterator =\n Symbol.asyncIterator in stream\n ? stream[Symbol.asyncIterator]()\n : stream[Symbol.iterator]()\n }\n\n /** Number of bytes currently buffered and available for reading */\n get bufferedByteLength() {\n let total = 0\n for (let i = 0; i < this.chunks.length; i++) {\n total += this.chunks[i].byteLength\n }\n return total\n }\n\n /**\n * @note concurrent reads are **NOT** supported by the current implementation\n * and would require call to readUntilBuffered to be using a fifo lock for\n * read()s to be processed in fifo order.\n */\n async read(bytesToRead: number): Promise<Uint8Array> {\n const bytesNeeded = bytesToRead - this.bufferedByteLength\n if (bytesNeeded > 0 && !this.isDone) {\n await this.readUntilBuffered(bytesNeeded)\n }\n\n const resultLength = Math.min(bytesToRead, this.bufferedByteLength)\n if (resultLength <= 0) return new Uint8Array()\n\n const firstChunk = this.consumeChunk(resultLength)\n if (firstChunk.byteLength === resultLength) {\n // If the data consumed from the first chunk contains all we need, return\n // it as-is. This allows to avoid any copy operation.\n return firstChunk\n }\n\n // The first chunk does not have all the data we need. We have to copy\n // multiple chunks into a larger buffer\n const result = new Uint8Array(resultLength)\n let resultWriteIndex = 0\n\n // Copy the first chunk into the result buffer\n result.set(firstChunk, resultWriteIndex)\n resultWriteIndex += firstChunk.byteLength\n\n // Copy more chunks as needed (we use do-while because we *know* we need\n // more than one chunk)\n do {\n const missingLength = resultLength - resultWriteIndex\n const currentChunk = this.consumeChunk(missingLength)\n\n result.set(currentChunk, resultWriteIndex)\n resultWriteIndex += currentChunk.byteLength\n } while (resultWriteIndex < resultLength)\n\n return result\n }\n\n private async readUntilBuffered(bytesNeeded: number) {\n let bytesRead = 0\n while (bytesRead < bytesNeeded) {\n const next = await this.iterator.next()\n if (next.done) {\n this.isDone = true\n break\n } else {\n this.chunks.push(next.value)\n bytesRead += next.value.byteLength\n }\n }\n return bytesRead\n }\n\n private consumeChunk(bytesToConsume: number) {\n const firstChunk = this.chunks[0]!\n if (bytesToConsume < firstChunk.byteLength) {\n // return a sub-view of the data being read and replace the first chunk\n // with a sub-view that does not contain that data.\n\n // @NOTE for some reason, subarray() revealed to be 7-8% slower in NodeJS\n // benchmarks.\n\n // this.chunks[0] = firstChunk.subarray(bytesToConsume)\n // return firstChunk.subarray(0, bytesToConsume)\n\n this.chunks[0] = new Uint8Array(\n firstChunk.buffer,\n firstChunk.byteOffset + bytesToConsume,\n firstChunk.byteLength - bytesToConsume,\n )\n return new Uint8Array(\n firstChunk.buffer,\n firstChunk.byteOffset,\n bytesToConsume,\n )\n } else {\n // First chunk is being read in full, discard it\n this.chunks.shift()\n return firstChunk\n }\n }\n\n async close(): Promise<void> {\n try {\n if (!this.isDone && this.iterator.return) {\n await this.iterator.return()\n }\n } finally {\n this.isDone = true\n this.chunks.length = 0\n }\n }\n}\n"]}
|
package/dist/cid-set.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.CidSet = void 0;
|
|
4
|
-
const lex_data_1 = require("@atproto/lex-data");
|
|
5
|
-
class CidSet {
|
|
1
|
+
import { parseCid } from '@atproto/lex-data';
|
|
2
|
+
export class CidSet {
|
|
6
3
|
constructor(arr = []) {
|
|
7
|
-
Object.defineProperty(this, "set", {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
configurable: true,
|
|
10
|
-
writable: true,
|
|
11
|
-
value: void 0
|
|
12
|
-
});
|
|
13
4
|
const strArr = arr.map((c) => c.toString());
|
|
14
5
|
this.set = new Set(strArr);
|
|
15
6
|
}
|
|
@@ -46,10 +37,9 @@ class CidSet {
|
|
|
46
37
|
}
|
|
47
38
|
*[Symbol.iterator]() {
|
|
48
39
|
for (const c of this.set) {
|
|
49
|
-
yield
|
|
40
|
+
yield parseCid(c);
|
|
50
41
|
}
|
|
51
42
|
}
|
|
52
43
|
}
|
|
53
|
-
|
|
54
|
-
exports.default = CidSet;
|
|
44
|
+
export default CidSet;
|
|
55
45
|
//# sourceMappingURL=cid-set.js.map
|
package/dist/cid-set.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cid-set.js","sourceRoot":"","sources":["../src/cid-set.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cid-set.js","sourceRoot":"","sources":["../src/cid-set.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAEjD,MAAM,OAAO,MAAM;IAGjB,YAAY,MAAa,EAAE;QACzB,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC3C,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAA;IAC5B,CAAC;IAED,GAAG,CAAC,GAAQ;QACV,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC5B,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,CAAC,OAAe;QACpB,KAAK,MAAM,CAAC,IAAI,OAAO,CAAC,GAAG;YAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;QAC5C,OAAO,IAAI,CAAA;IACb,CAAC;IAED,WAAW,CAAC,UAAkB;QAC5B,KAAK,MAAM,CAAC,IAAI,UAAU,CAAC,GAAG;YAAE,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;QAClD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,CAAC,GAAQ;QACb,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC/B,OAAO,IAAI,CAAA;IACb,CAAC;IAED,GAAG,CAAC,GAAQ;QACV,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAA;IACrC,CAAC;IAED,IAAI;QACF,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAA;IACtB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;QAChB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM;QACJ,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACzB,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;YACzB,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAA;QACnB,CAAC;IACH,CAAC;CACF;AAED,eAAe,MAAM,CAAA","sourcesContent":["import { Cid, parseCid } from '@atproto/lex-data'\n\nexport class CidSet implements Iterable<Cid> {\n private set: Set<string>\n\n constructor(arr: Cid[] = []) {\n const strArr = arr.map((c) => c.toString())\n this.set = new Set(strArr)\n }\n\n add(cid: Cid): CidSet {\n this.set.add(cid.toString())\n return this\n }\n\n addSet(toMerge: CidSet): CidSet {\n for (const c of toMerge.set) this.set.add(c)\n return this\n }\n\n subtractSet(toSubtract: CidSet): CidSet {\n for (const c of toSubtract.set) this.set.delete(c)\n return this\n }\n\n delete(cid: Cid) {\n this.set.delete(cid.toString())\n return this\n }\n\n has(cid: Cid): boolean {\n return this.set.has(cid.toString())\n }\n\n size(): number {\n return this.set.size\n }\n\n clear(): CidSet {\n this.set.clear()\n return this\n }\n\n toList(): Cid[] {\n return Array.from(this)\n }\n\n *[Symbol.iterator](): Generator<Cid, void, unknown> {\n for (const c of this.set) {\n yield parseCid(c)\n }\n }\n}\n\nexport default CidSet\n"]}
|
package/dist/data-diff.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Cid } from '@atproto/lex-data';
|
|
2
|
-
import { BlockMap } from './block-map';
|
|
3
|
-
import { CidSet } from './cid-set';
|
|
4
|
-
import { MST, NodeEntry } from './mst';
|
|
2
|
+
import { BlockMap } from './block-map.js';
|
|
3
|
+
import { CidSet } from './cid-set.js';
|
|
4
|
+
import { MST, NodeEntry } from './mst/index.js';
|
|
5
5
|
export declare class DataDiff {
|
|
6
6
|
adds: Record<string, DataAdd>;
|
|
7
7
|
updates: Record<string, DataUpdate>;
|
package/dist/data-diff.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-diff.d.ts","sourceRoot":"","sources":["../src/data-diff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"data-diff.d.ts","sourceRoot":"","sources":["../src/data-diff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,GAAG,EAAE,SAAS,EAAW,MAAM,gBAAgB,CAAA;AAExD,qBAAa,QAAQ;IACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAK;IAClC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAK;IACxC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAK;IAExC,YAAY,EAAE,QAAQ,CAAiB;IACvC,WAAW,EAAE,MAAM,CAAe;IAClC,WAAW,EAAE,MAAM,CAAe;WAErB,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,GAAG,IAAI,GAAG,OAAO,CAAC,QAAQ,CAAC;IAIzD,OAAO,CAAC,IAAI,EAAE,SAAS;IASvB,UAAU,CAAC,IAAI,EAAE,SAAS;IAYhC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG;IAS7B,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;IAO3C,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG;IAShC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,UAAU;IAQnC,UAAU,CAAC,GAAG,EAAE,GAAG;IAQnB,OAAO,IAAI,OAAO,EAAE;IAIpB,UAAU,IAAI,UAAU,EAAE;IAI1B,UAAU,IAAI,UAAU,EAAE;IAI1B,WAAW,IAAI,MAAM,EAAE;CAQxB;AAED,MAAM,MAAM,OAAO,GAAG;IACpB,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,GAAG,CAAA;IACT,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,GAAG,CAAA;CACT,CAAA"}
|
package/dist/data-diff.js
CHANGED
|
@@ -1,50 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const cid_set_1 = require("./cid-set");
|
|
6
|
-
const mst_1 = require("./mst");
|
|
7
|
-
class DataDiff {
|
|
1
|
+
import { BlockMap } from './block-map.js';
|
|
2
|
+
import { CidSet } from './cid-set.js';
|
|
3
|
+
import { mstDiff } from './mst/index.js';
|
|
4
|
+
export class DataDiff {
|
|
8
5
|
constructor() {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Object.defineProperty(this, "updates", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
writable: true,
|
|
19
|
-
value: {}
|
|
20
|
-
});
|
|
21
|
-
Object.defineProperty(this, "deletes", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true,
|
|
25
|
-
value: {}
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "newMstBlocks", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: new block_map_1.BlockMap()
|
|
32
|
-
});
|
|
33
|
-
Object.defineProperty(this, "newLeafCids", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: new cid_set_1.CidSet()
|
|
38
|
-
});
|
|
39
|
-
Object.defineProperty(this, "removedCids", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
configurable: true,
|
|
42
|
-
writable: true,
|
|
43
|
-
value: new cid_set_1.CidSet()
|
|
44
|
-
});
|
|
6
|
+
this.adds = {};
|
|
7
|
+
this.updates = {};
|
|
8
|
+
this.deletes = {};
|
|
9
|
+
this.newMstBlocks = new BlockMap();
|
|
10
|
+
this.newLeafCids = new CidSet();
|
|
11
|
+
this.removedCids = new CidSet();
|
|
45
12
|
}
|
|
46
13
|
static async of(curr, prev) {
|
|
47
|
-
return
|
|
14
|
+
return mstDiff(curr, prev);
|
|
48
15
|
}
|
|
49
16
|
async nodeAdd(node) {
|
|
50
17
|
if (node.isLeaf()) {
|
|
@@ -126,5 +93,4 @@ class DataDiff {
|
|
|
126
93
|
return [...new Set(keys)];
|
|
127
94
|
}
|
|
128
95
|
}
|
|
129
|
-
exports.DataDiff = DataDiff;
|
|
130
96
|
//# sourceMappingURL=data-diff.js.map
|
package/dist/data-diff.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-diff.js","sourceRoot":"","sources":["../src/data-diff.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"data-diff.js","sourceRoot":"","sources":["../src/data-diff.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;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AC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC9B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC9B,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QACnC,CAAC;IACH,CAAC;IAED,UAAU,CAAC,GAAQ;QACjB,IAAI,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/B,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC/B,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;IAED,OAAO;QACL,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACjC,CAAC;IAED,UAAU;QACR,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACpC,CAAC;IAED,UAAU;QACR,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACpC,CAAC;IAED,WAAW;QACT,MAAM,IAAI,GAAG;YACX,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;YACzB,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;YAC5B,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;SAC7B,CAAA;QACD,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;IAC3B,CAAC;CACF","sourcesContent":["import { Cid } from '@atproto/lex-data'\nimport { BlockMap } from './block-map.js'\nimport { CidSet } from './cid-set.js'\nimport { MST, NodeEntry, mstDiff } from './mst/index.js'\n\nexport class DataDiff {\n adds: Record<string, DataAdd> = {}\n updates: Record<string, DataUpdate> = {}\n deletes: Record<string, DataDelete> = {}\n\n newMstBlocks: BlockMap = new BlockMap()\n newLeafCids: CidSet = new CidSet()\n removedCids: CidSet = new CidSet()\n\n static async of(curr: MST, prev: MST | null): Promise<DataDiff> {\n return mstDiff(curr, prev)\n }\n\n async nodeAdd(node: NodeEntry) {\n if (node.isLeaf()) {\n this.leafAdd(node.key, node.value)\n } else {\n const data = await node.serialize()\n this.treeAdd(data.cid, data.bytes)\n }\n }\n\n async nodeDelete(node: NodeEntry) {\n if (node.isLeaf()) {\n const key = node.key\n const cid = node.value\n this.deletes[key] = { key, cid }\n this.removedCids.add(cid)\n } else {\n const cid = await node.getPointer()\n this.treeDelete(cid)\n }\n }\n\n leafAdd(key: string, cid: Cid) {\n this.adds[key] = { key, cid }\n if (this.removedCids.has(cid)) {\n this.removedCids.delete(cid)\n } else {\n this.newLeafCids.add(cid)\n }\n }\n\n leafUpdate(key: string, prev: Cid, cid: Cid) {\n if (prev.equals(cid)) return\n this.updates[key] = { key, prev, cid }\n this.removedCids.add(prev)\n this.newLeafCids.add(cid)\n }\n\n leafDelete(key: string, cid: Cid) {\n this.deletes[key] = { key, cid }\n if (this.newLeafCids.has(cid)) {\n this.newLeafCids.delete(cid)\n } else {\n this.removedCids.add(cid)\n }\n }\n\n treeAdd(cid: Cid, bytes: Uint8Array) {\n if (this.removedCids.has(cid)) {\n this.removedCids.delete(cid)\n } else {\n this.newMstBlocks.set(cid, bytes)\n }\n }\n\n treeDelete(cid: Cid) {\n if (this.newMstBlocks.has(cid)) {\n this.newMstBlocks.delete(cid)\n } else {\n this.removedCids.add(cid)\n }\n }\n\n addList(): DataAdd[] {\n return Object.values(this.adds)\n }\n\n updateList(): DataUpdate[] {\n return Object.values(this.updates)\n }\n\n deleteList(): DataDelete[] {\n return Object.values(this.deletes)\n }\n\n updatedKeys(): string[] {\n const keys = [\n ...Object.keys(this.adds),\n ...Object.keys(this.updates),\n ...Object.keys(this.deletes),\n ]\n return [...new Set(keys)]\n }\n}\n\nexport type DataAdd = {\n key: string\n cid: Cid\n}\n\nexport type DataUpdate = {\n key: string\n prev: Cid\n cid: Cid\n}\n\nexport type DataDelete = {\n key: string\n cid: Cid\n}\n"]}
|