helia 0.0.0-031ca73
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/LICENSE +4 -0
- package/README.md +59 -0
- package/dist/index.min.js +3 -0
- package/dist/src/helia.d.ts +18 -0
- package/dist/src/helia.d.ts.map +1 -0
- package/dist/src/helia.js +101 -0
- package/dist/src/helia.js.map +1 -0
- package/dist/src/index.d.ts +77 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +38 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/pins.d.ts +17 -0
- package/dist/src/pins.d.ts.map +1 -0
- package/dist/src/pins.js +181 -0
- package/dist/src/pins.js.map +1 -0
- package/dist/src/storage.d.ts +66 -0
- package/dist/src/storage.d.ts.map +1 -0
- package/dist/src/storage.js +178 -0
- package/dist/src/storage.js.map +1 -0
- package/dist/src/utils/dag-walkers.d.ts +22 -0
- package/dist/src/utils/dag-walkers.d.ts.map +1 -0
- package/dist/src/utils/dag-walkers.js +145 -0
- package/dist/src/utils/dag-walkers.js.map +1 -0
- package/dist/src/utils/datastore-version.d.ts +3 -0
- package/dist/src/utils/datastore-version.d.ts.map +1 -0
- package/dist/src/utils/datastore-version.js +19 -0
- package/dist/src/utils/datastore-version.js.map +1 -0
- package/package.json +176 -0
- package/src/helia.ts +117 -0
- package/src/index.ts +94 -0
- package/src/pins.ts +238 -0
- package/src/storage.ts +214 -0
- package/src/utils/dag-walkers.ts +169 -0
- package/src/utils/datastore-version.ts +23 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _HeliaImpl_bitswap;
|
|
13
|
+
import { identity } from 'multiformats/hashes/identity';
|
|
14
|
+
import { sha256, sha512 } from 'multiformats/hashes/sha2';
|
|
15
|
+
import { createBitswap } from 'ipfs-bitswap';
|
|
16
|
+
import { BlockStorage } from './storage.js';
|
|
17
|
+
import { PinsImpl } from './pins.js';
|
|
18
|
+
import { assertDatastoreVersionIsCurrent } from './utils/datastore-version.js';
|
|
19
|
+
import drain from 'it-drain';
|
|
20
|
+
import { CustomProgressEvent } from 'progress-events';
|
|
21
|
+
import { MemoryDatastore } from 'datastore-core';
|
|
22
|
+
import { MemoryBlockstore } from 'blockstore-core';
|
|
23
|
+
export class HeliaImpl {
|
|
24
|
+
constructor(init) {
|
|
25
|
+
_HeliaImpl_bitswap.set(this, void 0);
|
|
26
|
+
const hashers = [
|
|
27
|
+
sha256,
|
|
28
|
+
sha512,
|
|
29
|
+
identity,
|
|
30
|
+
...(init.hashers ?? [])
|
|
31
|
+
];
|
|
32
|
+
const datastore = init.datastore ?? new MemoryDatastore();
|
|
33
|
+
const blockstore = init.blockstore ?? new MemoryBlockstore();
|
|
34
|
+
// @ts-expect-error incomplete libp2p implementation
|
|
35
|
+
const libp2p = init.libp2p ?? new Proxy({}, {
|
|
36
|
+
get(_, prop) {
|
|
37
|
+
const noop = () => { };
|
|
38
|
+
const noops = ['start', 'stop'];
|
|
39
|
+
if (noops.includes(prop.toString())) {
|
|
40
|
+
return noop;
|
|
41
|
+
}
|
|
42
|
+
if (prop === 'isProxy') {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
throw new Error('Please configure Helia with a libp2p instance');
|
|
46
|
+
},
|
|
47
|
+
set() {
|
|
48
|
+
throw new Error('Please configure Helia with a libp2p instance');
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
this.pins = new PinsImpl(datastore, blockstore, init.dagWalkers ?? []);
|
|
52
|
+
if (init.libp2p != null) {
|
|
53
|
+
__classPrivateFieldSet(this, _HeliaImpl_bitswap, createBitswap(libp2p, blockstore, {
|
|
54
|
+
hashLoader: {
|
|
55
|
+
getHasher: async (codecOrName) => {
|
|
56
|
+
const hasher = hashers.find(hasher => {
|
|
57
|
+
return hasher.code === codecOrName || hasher.name === codecOrName;
|
|
58
|
+
});
|
|
59
|
+
if (hasher != null) {
|
|
60
|
+
return await Promise.resolve(hasher);
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`Could not load hasher for code/name "${codecOrName}"`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}), "f");
|
|
66
|
+
}
|
|
67
|
+
this.libp2p = libp2p;
|
|
68
|
+
this.blockstore = new BlockStorage(blockstore, this.pins, __classPrivateFieldGet(this, _HeliaImpl_bitswap, "f"));
|
|
69
|
+
this.datastore = datastore;
|
|
70
|
+
}
|
|
71
|
+
async start() {
|
|
72
|
+
await assertDatastoreVersionIsCurrent(this.datastore);
|
|
73
|
+
__classPrivateFieldGet(this, _HeliaImpl_bitswap, "f")?.start();
|
|
74
|
+
await this.libp2p.start();
|
|
75
|
+
}
|
|
76
|
+
async stop() {
|
|
77
|
+
__classPrivateFieldGet(this, _HeliaImpl_bitswap, "f")?.stop();
|
|
78
|
+
await this.libp2p.stop();
|
|
79
|
+
}
|
|
80
|
+
async gc(options = {}) {
|
|
81
|
+
const releaseLock = await this.blockstore.lock.writeLock();
|
|
82
|
+
try {
|
|
83
|
+
const helia = this;
|
|
84
|
+
const blockstore = this.blockstore.unwrap();
|
|
85
|
+
await drain(blockstore.deleteMany((async function* () {
|
|
86
|
+
for await (const cid of blockstore.queryKeys({})) {
|
|
87
|
+
if (await helia.pins.isPinned(cid, options)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
yield cid;
|
|
91
|
+
options.onProgress?.(new CustomProgressEvent('helia:gc:deleted', cid));
|
|
92
|
+
}
|
|
93
|
+
}())));
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
releaseLock();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
_HeliaImpl_bitswap = new WeakMap();
|
|
101
|
+
//# sourceMappingURL=helia.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helia.js","sourceRoot":"","sources":["../../src/helia.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAGzD,OAAO,EAAW,aAAa,EAAE,MAAM,cAAc,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,+BAA+B,EAAE,MAAM,8BAA8B,CAAA;AAC9E,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAElD,MAAM,OAAO,SAAS;IAQpB,YAAa,IAAe;QAF5B,qCAAkB;QAGhB,MAAM,OAAO,GAAsB;YACjC,MAAM;YACN,MAAM;YACN,QAAQ;YACR,GAAG,CAAC,IAAI,CAAC,OAAO,IAAI,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C,EAAE;wBAC3C,SAAQ;qBACT;oBAED,MAAM,GAAG,CAAA;oBAET,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC,CAAA;iBACvE;YACH,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;SACP;gBAAS;YACR,WAAW,EAAE,CAAA;SACd;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Create a Helia node.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createLibp2p } from 'libp2p'
|
|
10
|
+
* import { MemoryDatastore } from 'datastore-core'
|
|
11
|
+
* import { MemoryBlockstore } from 'blockstore-core'
|
|
12
|
+
* import { createHelia } from 'helia'
|
|
13
|
+
* import { unixfs } from '@helia/unixfs'
|
|
14
|
+
* import { CID } from 'multiformats/cid'
|
|
15
|
+
*
|
|
16
|
+
* const node = await createHelia({
|
|
17
|
+
* blockstore: new MemoryBlockstore(),
|
|
18
|
+
* datastore: new MemoryDatastore(),
|
|
19
|
+
* libp2p: await createLibp2p({
|
|
20
|
+
* //... libp2p options
|
|
21
|
+
* })
|
|
22
|
+
* })
|
|
23
|
+
* const fs = unixfs(node)
|
|
24
|
+
* fs.cat(CID.parse('bafyFoo'))
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
import type { Helia } from '@helia/interface';
|
|
28
|
+
import type { Libp2p } from '@libp2p/interface-libp2p';
|
|
29
|
+
import type { Blockstore } from 'interface-blockstore';
|
|
30
|
+
import type { Datastore } from 'interface-datastore';
|
|
31
|
+
import type { CID } from 'multiformats/cid';
|
|
32
|
+
import type { MultihashHasher } from 'multiformats/hashes/interface';
|
|
33
|
+
/**
|
|
34
|
+
* DAGWalkers take a block and yield CIDs encoded in that block
|
|
35
|
+
*/
|
|
36
|
+
export interface DAGWalker {
|
|
37
|
+
codec: number;
|
|
38
|
+
walk: (block: Uint8Array) => AsyncGenerator<CID, void, undefined>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Options used to create a Helia node.
|
|
42
|
+
*/
|
|
43
|
+
export interface HeliaInit {
|
|
44
|
+
/**
|
|
45
|
+
* A libp2p node is required to perform network operations
|
|
46
|
+
*/
|
|
47
|
+
libp2p?: Libp2p;
|
|
48
|
+
/**
|
|
49
|
+
* The blockstore is where blocks are stored
|
|
50
|
+
*/
|
|
51
|
+
blockstore?: Blockstore;
|
|
52
|
+
/**
|
|
53
|
+
* The datastore is where data is stored
|
|
54
|
+
*/
|
|
55
|
+
datastore?: Datastore;
|
|
56
|
+
/**
|
|
57
|
+
* By default sha256, sha512 and identity hashes are supported for
|
|
58
|
+
* bitswap operations. To bitswap blocks with CIDs using other hashes
|
|
59
|
+
* pass appropriate MultihashHashers here.
|
|
60
|
+
*/
|
|
61
|
+
hashers?: MultihashHasher[];
|
|
62
|
+
/**
|
|
63
|
+
* In order to pin CIDs that correspond to a DAG, it's necessary to know
|
|
64
|
+
* how to traverse that DAG. DAGWalkers take a block and yield any CIDs
|
|
65
|
+
* encoded within that block.
|
|
66
|
+
*/
|
|
67
|
+
dagWalkers?: DAGWalker[];
|
|
68
|
+
/**
|
|
69
|
+
* Pass `false` to not start the helia node
|
|
70
|
+
*/
|
|
71
|
+
start?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create and return a Helia node
|
|
75
|
+
*/
|
|
76
|
+
export declare function createHelia(init?: HeliaInit): Promise<Helia>;
|
|
77
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAGpE;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC,CAAA;CAClE;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;OAEG;IACH,UAAU,CAAC,EAAE,UAAU,CAAA;IAEvB;;OAEG;IACH,SAAS,CAAC,EAAE,SAAS,CAAA;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,eAAe,EAAE,CAAA;IAE3B;;;;OAIG;IACH,UAAU,CAAC,EAAE,SAAS,EAAE,CAAA;IAExB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED;;GAEG;AACH,wBAAsB,WAAW,CAAE,IAAI,GAAE,SAAc,GAAG,OAAO,CAAC,KAAK,CAAC,CAQvE"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Create a Helia node.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createLibp2p } from 'libp2p'
|
|
10
|
+
* import { MemoryDatastore } from 'datastore-core'
|
|
11
|
+
* import { MemoryBlockstore } from 'blockstore-core'
|
|
12
|
+
* import { createHelia } from 'helia'
|
|
13
|
+
* import { unixfs } from '@helia/unixfs'
|
|
14
|
+
* import { CID } from 'multiformats/cid'
|
|
15
|
+
*
|
|
16
|
+
* const node = await createHelia({
|
|
17
|
+
* blockstore: new MemoryBlockstore(),
|
|
18
|
+
* datastore: new MemoryDatastore(),
|
|
19
|
+
* libp2p: await createLibp2p({
|
|
20
|
+
* //... libp2p options
|
|
21
|
+
* })
|
|
22
|
+
* })
|
|
23
|
+
* const fs = unixfs(node)
|
|
24
|
+
* fs.cat(CID.parse('bafyFoo'))
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
import { HeliaImpl } from './helia.js';
|
|
28
|
+
/**
|
|
29
|
+
* Create and return a Helia node
|
|
30
|
+
*/
|
|
31
|
+
export async function createHelia(init = {}) {
|
|
32
|
+
const helia = new HeliaImpl(init);
|
|
33
|
+
if (init.start !== false) {
|
|
34
|
+
await helia.start();
|
|
35
|
+
}
|
|
36
|
+
return helia;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAQH,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAiDtC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAE,OAAkB,EAAE;IACrD,MAAM,KAAK,GAAG,IAAI,SAAS,CAAC,IAAI,CAAC,CAAA;IAEjC,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK,EAAE;QACxB,MAAM,KAAK,CAAC,KAAK,EAAE,CAAA;KACpB;IAED,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AddOptions, IsPinnedOptions, LsOptions, Pin, Pins, RmOptions } from '@helia/interface/pins';
|
|
2
|
+
import { Datastore } from 'interface-datastore';
|
|
3
|
+
import { CID, Version } from 'multiformats/cid';
|
|
4
|
+
import type { Blockstore } from 'interface-blockstore';
|
|
5
|
+
import type { DAGWalker } from './index.js';
|
|
6
|
+
export declare class PinsImpl implements Pins {
|
|
7
|
+
#private;
|
|
8
|
+
private readonly datastore;
|
|
9
|
+
private readonly blockstore;
|
|
10
|
+
private dagWalkers;
|
|
11
|
+
constructor(datastore: Datastore, blockstore: Blockstore, dagWalkers: DAGWalker[]);
|
|
12
|
+
add(cid: CID<unknown, number, number, Version>, options?: AddOptions): Promise<Pin>;
|
|
13
|
+
rm(cid: CID<unknown, number, number, Version>, options?: RmOptions): Promise<Pin>;
|
|
14
|
+
ls(options?: LsOptions): AsyncGenerator<Pin, void, undefined>;
|
|
15
|
+
isPinned(cid: CID, options?: IsPinnedOptions): Promise<boolean>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=pins.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pins.d.ts","sourceRoot":"","sources":["../../src/pins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACzG,OAAO,EAAE,SAAS,EAAO,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAG/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAKtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AA6C3C,qBAAa,QAAS,YAAW,IAAI;;IACnC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IACrC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IACvC,OAAO,CAAC,UAAU,CAA2B;gBAEhC,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE;IAU5E,GAAG,CAAE,GAAG,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAE,UAAe,GAAG,OAAO,CAAC,GAAG,CAAC;IAwHxF,EAAE,CAAE,GAAG,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAE,SAAc,GAAG,OAAO,CAAC,GAAG,CAAC;IA4BpF,EAAE,CAAE,OAAO,GAAE,SAAc,GAAG,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC;IAcpE,QAAQ,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,OAAO,CAAC;CAK3E"}
|
package/dist/src/pins.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _PinsImpl_instances, _PinsImpl_walkDag, _PinsImpl_updatePinnedBlock;
|
|
7
|
+
import { Key } from 'interface-datastore';
|
|
8
|
+
import { CID } from 'multiformats/cid';
|
|
9
|
+
import * as cborg from 'cborg';
|
|
10
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
11
|
+
import PQueue from 'p-queue';
|
|
12
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
|
|
13
|
+
import defer from 'p-defer';
|
|
14
|
+
import { cborWalker, dagPbWalker, jsonWalker, rawWalker } from './utils/dag-walkers.js';
|
|
15
|
+
const DEFAULT_DAG_WALKERS = [
|
|
16
|
+
rawWalker,
|
|
17
|
+
dagPbWalker,
|
|
18
|
+
cborWalker,
|
|
19
|
+
jsonWalker
|
|
20
|
+
];
|
|
21
|
+
const DATASTORE_PIN_PREFIX = '/pin/';
|
|
22
|
+
const DATASTORE_BLOCK_PREFIX = '/pinned-block/';
|
|
23
|
+
const DATASTORE_ENCODING = base36;
|
|
24
|
+
// const DAG_WALK_MAX_QUEUE_LENGTH = 10
|
|
25
|
+
const DAG_WALK_QUEUE_CONCURRENCY = 1;
|
|
26
|
+
function toDSKey(cid) {
|
|
27
|
+
if (cid.version === 0) {
|
|
28
|
+
cid = cid.toV1();
|
|
29
|
+
}
|
|
30
|
+
return new Key(`${DATASTORE_PIN_PREFIX}${cid.toString(DATASTORE_ENCODING)}`);
|
|
31
|
+
}
|
|
32
|
+
export class PinsImpl {
|
|
33
|
+
constructor(datastore, blockstore, dagWalkers) {
|
|
34
|
+
_PinsImpl_instances.add(this);
|
|
35
|
+
this.datastore = datastore;
|
|
36
|
+
this.blockstore = blockstore;
|
|
37
|
+
this.dagWalkers = {};
|
|
38
|
+
[...DEFAULT_DAG_WALKERS, ...dagWalkers].forEach(dagWalker => {
|
|
39
|
+
this.dagWalkers[dagWalker.codec] = dagWalker;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async add(cid, options = {}) {
|
|
43
|
+
const pinKey = toDSKey(cid);
|
|
44
|
+
if (await this.datastore.has(pinKey)) {
|
|
45
|
+
throw new Error('Already pinned');
|
|
46
|
+
}
|
|
47
|
+
const depth = Math.round(options.depth ?? Infinity);
|
|
48
|
+
if (depth < 0) {
|
|
49
|
+
throw new Error('Depth must be greater than or equal to 0');
|
|
50
|
+
}
|
|
51
|
+
// use a queue to walk the DAG instead of recursion so we can traverse very large DAGs
|
|
52
|
+
const queue = new PQueue({
|
|
53
|
+
concurrency: DAG_WALK_QUEUE_CONCURRENCY
|
|
54
|
+
});
|
|
55
|
+
void queue.add(async () => {
|
|
56
|
+
await __classPrivateFieldGet(this, _PinsImpl_instances, "m", _PinsImpl_walkDag).call(this, cid, queue, (pinnedBlock) => {
|
|
57
|
+
// do not update pinned block if this block is already pinned by this CID
|
|
58
|
+
if (pinnedBlock.pinnedBy.find(c => uint8ArrayEquals(c, cid.bytes)) != null) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
pinnedBlock.pinCount++;
|
|
62
|
+
pinnedBlock.pinnedBy.push(cid.bytes);
|
|
63
|
+
}, {
|
|
64
|
+
...options,
|
|
65
|
+
depth
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
// if a job in the queue errors, throw that error
|
|
69
|
+
const deferred = defer();
|
|
70
|
+
queue.on('error', (err) => {
|
|
71
|
+
queue.clear();
|
|
72
|
+
deferred.reject(err);
|
|
73
|
+
});
|
|
74
|
+
// wait for the queue to complete or error
|
|
75
|
+
await Promise.race([
|
|
76
|
+
queue.onIdle(),
|
|
77
|
+
deferred.promise
|
|
78
|
+
]);
|
|
79
|
+
const pin = {
|
|
80
|
+
depth,
|
|
81
|
+
metadata: options.metadata ?? {}
|
|
82
|
+
};
|
|
83
|
+
await this.datastore.put(pinKey, cborg.encode(pin), options);
|
|
84
|
+
return {
|
|
85
|
+
cid,
|
|
86
|
+
...pin
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async rm(cid, options = {}) {
|
|
90
|
+
const pinKey = toDSKey(cid);
|
|
91
|
+
const buf = await this.datastore.get(pinKey, options);
|
|
92
|
+
const pin = cborg.decode(buf);
|
|
93
|
+
await this.datastore.delete(pinKey, options);
|
|
94
|
+
// use a queue to walk the DAG instead of recursion so we can traverse very large DAGs
|
|
95
|
+
const queue = new PQueue({
|
|
96
|
+
concurrency: DAG_WALK_QUEUE_CONCURRENCY
|
|
97
|
+
});
|
|
98
|
+
void queue.add(async () => {
|
|
99
|
+
await __classPrivateFieldGet(this, _PinsImpl_instances, "m", _PinsImpl_walkDag).call(this, cid, queue, (pinnedBlock) => {
|
|
100
|
+
pinnedBlock.pinCount--;
|
|
101
|
+
pinnedBlock.pinnedBy = pinnedBlock.pinnedBy.filter(c => uint8ArrayEquals(c, cid.bytes));
|
|
102
|
+
}, {
|
|
103
|
+
...options,
|
|
104
|
+
depth: pin.depth
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
await queue.onIdle();
|
|
108
|
+
return {
|
|
109
|
+
cid,
|
|
110
|
+
...pin
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
async *ls(options = {}) {
|
|
114
|
+
for await (const { key, value } of this.datastore.query({
|
|
115
|
+
prefix: DATASTORE_PIN_PREFIX + (options.cid != null ? `${options.cid.toString(base36)}` : '')
|
|
116
|
+
}, options)) {
|
|
117
|
+
const cid = CID.parse(key.toString().substring(5), base36);
|
|
118
|
+
const pin = cborg.decode(value);
|
|
119
|
+
yield {
|
|
120
|
+
cid,
|
|
121
|
+
...pin
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async isPinned(cid, options = {}) {
|
|
126
|
+
const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`);
|
|
127
|
+
return await this.datastore.has(blockKey, options);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
_PinsImpl_instances = new WeakSet(), _PinsImpl_walkDag =
|
|
131
|
+
/**
|
|
132
|
+
* Walk the DAG behind the passed CID, ensure all blocks are present in the blockstore
|
|
133
|
+
* and update the pin count for them
|
|
134
|
+
*/
|
|
135
|
+
async function _PinsImpl_walkDag(cid, queue, withPinnedBlock, options) {
|
|
136
|
+
if (options.depth === -1) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const dagWalker = this.dagWalkers[cid.code];
|
|
140
|
+
if (dagWalker == null) {
|
|
141
|
+
throw new Error(`No dag walker found for cid codec ${cid.code}`);
|
|
142
|
+
}
|
|
143
|
+
const block = await this.blockstore.get(cid);
|
|
144
|
+
await __classPrivateFieldGet(this, _PinsImpl_instances, "m", _PinsImpl_updatePinnedBlock).call(this, cid, withPinnedBlock, options);
|
|
145
|
+
// walk dag, ensure all blocks are present
|
|
146
|
+
for await (const cid of dagWalker.walk(block)) {
|
|
147
|
+
void queue.add(async () => {
|
|
148
|
+
await __classPrivateFieldGet(this, _PinsImpl_instances, "m", _PinsImpl_walkDag).call(this, cid, queue, withPinnedBlock, {
|
|
149
|
+
...options,
|
|
150
|
+
depth: options.depth - 1
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}, _PinsImpl_updatePinnedBlock =
|
|
155
|
+
/**
|
|
156
|
+
* Update the pin count for the CID
|
|
157
|
+
*/
|
|
158
|
+
async function _PinsImpl_updatePinnedBlock(cid, withPinnedBlock, options) {
|
|
159
|
+
const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`);
|
|
160
|
+
let pinnedBlock = {
|
|
161
|
+
pinCount: 0,
|
|
162
|
+
pinnedBy: []
|
|
163
|
+
};
|
|
164
|
+
try {
|
|
165
|
+
pinnedBlock = cborg.decode(await this.datastore.get(blockKey, options));
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
if (err.code !== 'ERR_NOT_FOUND') {
|
|
169
|
+
throw err;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
withPinnedBlock(pinnedBlock);
|
|
173
|
+
if (pinnedBlock.pinCount === 0) {
|
|
174
|
+
if (await this.datastore.has(blockKey)) {
|
|
175
|
+
await this.datastore.delete(blockKey);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
await this.datastore.put(blockKey, cborg.encode(pinnedBlock), options);
|
|
180
|
+
};
|
|
181
|
+
//# sourceMappingURL=pins.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pins.js","sourceRoot":"","sources":["../../src/pins.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAa,GAAG,EAA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AAI;QACF,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAA;KACxE;IAAC,OAAO,GAAQ,EAAE;QACjB,IAAI,GAAG,CAAC,IAAI,KAAK,eAAe,EAAE;YAChC,MAAM,GAAG,CAAA;SACV;KACF;IAED,eAAe,CAAC,WAAW,CAAC,CAAA;IAE5B,IAAI,WAAW,CAAC,QAAQ,KAAK,CAAC,EAAE;QAC9B,IAAI,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YACtC,MAAM,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YACrC,OAAM;SACP;KACF;IAED,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC,CAAA;AACxE,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { BaseBlockstore } from 'blockstore-core';
|
|
2
|
+
import type { Blockstore, KeyQuery, Query } from 'interface-blockstore';
|
|
3
|
+
import type { Bitswap } from 'ipfs-bitswap';
|
|
4
|
+
import type { CID } from 'multiformats/cid';
|
|
5
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
6
|
+
import type { AwaitIterable } from 'interface-store';
|
|
7
|
+
import type { Mortice } from 'mortice';
|
|
8
|
+
import type { Pins } from '@helia/interface/pins';
|
|
9
|
+
export interface BlockStorageOptions extends AbortOptions {
|
|
10
|
+
progress?: (evt: Event) => void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* BlockStorage is a hybrid blockstore that puts/gets blocks from a configured
|
|
14
|
+
* blockstore (that may be on disk, s3, or something else). If the blocks are
|
|
15
|
+
* not present Bitswap will be used to fetch them from network peers.
|
|
16
|
+
*/
|
|
17
|
+
export declare class BlockStorage extends BaseBlockstore implements Blockstore {
|
|
18
|
+
lock: Mortice;
|
|
19
|
+
private readonly child;
|
|
20
|
+
private readonly bitswap?;
|
|
21
|
+
private readonly pins;
|
|
22
|
+
/**
|
|
23
|
+
* Create a new BlockStorage
|
|
24
|
+
*/
|
|
25
|
+
constructor(blockstore: Blockstore, pins: Pins, bitswap?: Bitswap);
|
|
26
|
+
open(): Promise<void>;
|
|
27
|
+
close(): Promise<void>;
|
|
28
|
+
unwrap(): Blockstore;
|
|
29
|
+
/**
|
|
30
|
+
* Put a block to the underlying datastore
|
|
31
|
+
*/
|
|
32
|
+
put(cid: CID, block: Uint8Array, options?: AbortOptions): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Put a multiple blocks to the underlying datastore
|
|
35
|
+
*/
|
|
36
|
+
putMany(blocks: AwaitIterable<{
|
|
37
|
+
key: CID;
|
|
38
|
+
value: Uint8Array;
|
|
39
|
+
}>, options?: AbortOptions): AsyncGenerator<{
|
|
40
|
+
key: CID;
|
|
41
|
+
value: Uint8Array;
|
|
42
|
+
}, void, undefined>;
|
|
43
|
+
/**
|
|
44
|
+
* Get a block by cid
|
|
45
|
+
*/
|
|
46
|
+
get(cid: CID, options?: BlockStorageOptions): Promise<Uint8Array>;
|
|
47
|
+
/**
|
|
48
|
+
* Get multiple blocks back from an array of cids
|
|
49
|
+
*/
|
|
50
|
+
getMany(cids: AwaitIterable<CID>, options?: BlockStorageOptions): AsyncGenerator<Uint8Array, void, undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* Delete a block from the blockstore
|
|
53
|
+
*/
|
|
54
|
+
delete(cid: CID, options?: AbortOptions): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Delete multiple blocks from the blockstore
|
|
57
|
+
*/
|
|
58
|
+
deleteMany(cids: AwaitIterable<CID>, options?: AbortOptions): AsyncGenerator<CID, void, undefined>;
|
|
59
|
+
has(cid: CID, options?: AbortOptions): Promise<boolean>;
|
|
60
|
+
query(q: Query, options?: AbortOptions): AsyncGenerator<{
|
|
61
|
+
key: CID;
|
|
62
|
+
value: Uint8Array;
|
|
63
|
+
}, void, undefined>;
|
|
64
|
+
queryKeys(q: KeyQuery, options?: AbortOptions): AsyncGenerator<CID, void, undefined>;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAIhD,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAA;AACvE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEtC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAEjD,MAAM,WAAW,mBAAoB,SAAQ,YAAY;IACvD,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,CAAA;CAChC;AAED;;;;GAIG;AACH,qBAAa,YAAa,SAAQ,cAAe,YAAW,UAAU;IAC7D,IAAI,EAAE,OAAO,CAAA;IACpB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAY;IAClC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAM;IAE3B;;OAEG;gBACU,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,OAAO;IAS5D,IAAI,IAAK,OAAO,CAAC,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