helia 0.0.0-270bb98
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 +19 -0
- package/dist/src/helia.d.ts.map +1 -0
- package/dist/src/helia.js +88 -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 +174 -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 +100 -0
- package/src/index.ts +94 -0
- package/src/pins.ts +238 -0
- package/src/storage.ts +209 -0
- package/src/utils/dag-walkers.ts +169 -0
- package/src/utils/datastore-version.ts +23 -0
|
@@ -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,EAAE,MAAM,CAAA;IAEd;;OAEG;IACH,UAAU,EAAE,UAAU,CAAA;IAEtB;;OAEG;IACH,SAAS,EAAE,SAAS,CAAA;IAEpB;;;;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,EAAE,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,CAQlE"}
|
|
@@ -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,IAAe;IAChD,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,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,GAAG,EAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAElD,OAAO,MAAM,MAAM,SAAS,CAAA;AAE5B,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,KAAK,MAAM,SAAS,CAAA;AAE3B,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAEvF,MAAM,mBAAmB,GAAG;IAC1B,SAAS;IACT,WAAW;IACX,UAAU;IACV,UAAU;CACX,CAAA;AAmBD,MAAM,oBAAoB,GAAG,OAAO,CAAA;AACpC,MAAM,sBAAsB,GAAG,gBAAgB,CAAA;AAC/C,MAAM,kB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,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, bitswap: Bitswap, pins: Pins);
|
|
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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AEG;IACK,OAAO,CAAE,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,OAAO,GAAE,mBAAwB,GAAG,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,SAAS,CAAC;IA+B1H;;OAEG;IACG,MAAM,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAclE;;OAEG;IACK,UAAU,CAAE,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,OAAO,GAAE,YAAiB,GAAG,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC;IAoBzG,GAAG,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,OAAO,CAAC;IAU1D,KAAK,CAAE,CAAC,EAAE,KAAK,EAAE,OAAO,GAAE,YAAiB,GAAG,cAAc,CAAC;QAAE,GAAG,EAAE,GAAG,CAAC;QAAC,KAAK,EAAE,UAAU,CAAA;KAAE,EAAE,IAAI,EAAE,SAAS,CAAC;IAU9G,SAAS,CAAE,CAAC,EAAE,QAAQ,EAAE,OAAO,GAAE,YAAiB,GAAG,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC;CASlG"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { BaseBlockstore } from 'blockstore-core';
|
|
2
|
+
import merge from 'it-merge';
|
|
3
|
+
import { pushable } from 'it-pushable';
|
|
4
|
+
import filter from 'it-filter';
|
|
5
|
+
import createMortice from 'mortice';
|
|
6
|
+
/**
|
|
7
|
+
* BlockStorage is a hybrid blockstore that puts/gets blocks from a configured
|
|
8
|
+
* blockstore (that may be on disk, s3, or something else). If the blocks are
|
|
9
|
+
* not present Bitswap will be used to fetch them from network peers.
|
|
10
|
+
*/
|
|
11
|
+
export class BlockStorage extends BaseBlockstore {
|
|
12
|
+
/**
|
|
13
|
+
* Create a new BlockStorage
|
|
14
|
+
*/
|
|
15
|
+
constructor(blockstore, bitswap, pins) {
|
|
16
|
+
super();
|
|
17
|
+
this.child = blockstore;
|
|
18
|
+
this.bitswap = bitswap;
|
|
19
|
+
this.pins = pins;
|
|
20
|
+
this.lock = createMortice();
|
|
21
|
+
}
|
|
22
|
+
async open() {
|
|
23
|
+
await this.child.open();
|
|
24
|
+
}
|
|
25
|
+
async close() {
|
|
26
|
+
await this.child.close();
|
|
27
|
+
}
|
|
28
|
+
unwrap() {
|
|
29
|
+
return this.child;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Put a block to the underlying datastore
|
|
33
|
+
*/
|
|
34
|
+
async put(cid, block, options = {}) {
|
|
35
|
+
const releaseLock = await this.lock.writeLock();
|
|
36
|
+
try {
|
|
37
|
+
if (this.bitswap.isStarted()) {
|
|
38
|
+
await this.bitswap.put(cid, block, options);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
await this.child.put(cid, block, options);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
releaseLock();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Put a multiple blocks to the underlying datastore
|
|
50
|
+
*/
|
|
51
|
+
async *putMany(blocks, options = {}) {
|
|
52
|
+
const releaseLock = await this.lock.writeLock();
|
|
53
|
+
try {
|
|
54
|
+
const missingBlocks = filter(blocks, async ({ key }) => { return !(await this.child.has(key)); });
|
|
55
|
+
if (this.bitswap.isStarted()) {
|
|
56
|
+
yield* this.bitswap.putMany(missingBlocks, options);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
yield* this.child.putMany(missingBlocks, options);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
releaseLock();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get a block by cid
|
|
68
|
+
*/
|
|
69
|
+
async get(cid, options = {}) {
|
|
70
|
+
const releaseLock = await this.lock.readLock();
|
|
71
|
+
try {
|
|
72
|
+
if (!(await this.has(cid)) && this.bitswap.isStarted()) {
|
|
73
|
+
return await this.bitswap.get(cid, options);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return await this.child.get(cid, options);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
releaseLock();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get multiple blocks back from an array of cids
|
|
85
|
+
*/
|
|
86
|
+
async *getMany(cids, options = {}) {
|
|
87
|
+
const releaseLock = await this.lock.readLock();
|
|
88
|
+
try {
|
|
89
|
+
const getFromBitswap = pushable({ objectMode: true });
|
|
90
|
+
const getFromChild = pushable({ objectMode: true });
|
|
91
|
+
void Promise.resolve().then(async () => {
|
|
92
|
+
for await (const cid of cids) {
|
|
93
|
+
if (!(await this.has(cid)) && this.bitswap.isStarted()) {
|
|
94
|
+
getFromBitswap.push(cid);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
getFromChild.push(cid);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
getFromBitswap.end();
|
|
101
|
+
getFromChild.end();
|
|
102
|
+
}).catch(err => {
|
|
103
|
+
getFromBitswap.throw(err);
|
|
104
|
+
});
|
|
105
|
+
yield* merge(this.bitswap.getMany(getFromBitswap, options), this.child.getMany(getFromChild, options));
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
releaseLock();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Delete a block from the blockstore
|
|
113
|
+
*/
|
|
114
|
+
async delete(cid, options = {}) {
|
|
115
|
+
const releaseLock = await this.lock.writeLock();
|
|
116
|
+
try {
|
|
117
|
+
if (await this.pins.isPinned(cid)) {
|
|
118
|
+
throw new Error('CID was pinned');
|
|
119
|
+
}
|
|
120
|
+
await this.child.delete(cid, options);
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
releaseLock();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Delete multiple blocks from the blockstore
|
|
128
|
+
*/
|
|
129
|
+
async *deleteMany(cids, options = {}) {
|
|
130
|
+
const releaseLock = await this.lock.writeLock();
|
|
131
|
+
try {
|
|
132
|
+
const storage = this;
|
|
133
|
+
yield* this.child.deleteMany((async function* () {
|
|
134
|
+
for await (const cid of cids) {
|
|
135
|
+
if (await storage.pins.isPinned(cid)) {
|
|
136
|
+
throw new Error('CID was pinned');
|
|
137
|
+
}
|
|
138
|
+
yield cid;
|
|
139
|
+
}
|
|
140
|
+
}()), options);
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
releaseLock();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async has(cid, options = {}) {
|
|
147
|
+
const releaseLock = await this.lock.readLock();
|
|
148
|
+
try {
|
|
149
|
+
return await this.child.has(cid, options);
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
releaseLock();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async *query(q, options = {}) {
|
|
156
|
+
const releaseLock = await this.lock.readLock();
|
|
157
|
+
try {
|
|
158
|
+
yield* this.child.query(q, options);
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
releaseLock();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async *queryKeys(q, options = {}) {
|
|
165
|
+
const releaseLock = await this.lock.readLock();
|
|
166
|
+
try {
|
|
167
|
+
yield* this.child.queryKeys(q, options);
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
releaseLock();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE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AAQ,EAAE,CAAA;QAE9C,IAAI;YACF,KAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAA;SACrC;gBAAS;YACR,WAAW,EAAE,CAAA;SACd;IACH,CAAC;IAED,KAAK,CAAC,CAAE,SAAS,CAAE,CAAW,EAAE,UAAwB,EAAE;QACxD,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAA;QAE9C,IAAI;YACF,KAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,CAAA;SACzC;gBAAS;YACR,WAAW,EAAE,CAAA;SACd;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DAGWalker } from '../index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Dag walker for dag-pb CIDs
|
|
4
|
+
*/
|
|
5
|
+
export declare const dagPbWalker: DAGWalker;
|
|
6
|
+
/**
|
|
7
|
+
* Dag walker for raw CIDs
|
|
8
|
+
*/
|
|
9
|
+
export declare const rawWalker: DAGWalker;
|
|
10
|
+
/**
|
|
11
|
+
* Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
|
|
12
|
+
* all we are interested in is extracting the the CIDs from the block
|
|
13
|
+
* so we can just use cborg for that.
|
|
14
|
+
*/
|
|
15
|
+
export declare const cborWalker: DAGWalker;
|
|
16
|
+
/**
|
|
17
|
+
* Dag walker for dag-json CIDs. Does not actually use dag-json since
|
|
18
|
+
* all we are interested in is extracting the the CIDs from the block
|
|
19
|
+
* so we can just use cborg/json for that.
|
|
20
|
+
*/
|
|
21
|
+
export declare const jsonWalker: DAGWalker;
|
|
22
|
+
//# sourceMappingURL=dag-walkers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dag-walkers.d.ts","sourceRoot":"","sources":["../../../src/utils/dag-walkers.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAK5C;;GAEG;AACH,eAAO,MAAM,WAAW,EAAE,SAOzB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,SAKvB,CAAA;AAKD;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,SAuBxB,CAAA;AAsED;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,SA6BxB,CAAA"}
|