helia 3.0.1 → 4.0.0-e554493
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/dist/index.min.js +39 -61
- package/dist/src/helia-p2p.d.ts +18 -0
- package/dist/src/helia-p2p.d.ts.map +1 -0
- package/dist/src/helia-p2p.js +22 -0
- package/dist/src/helia-p2p.js.map +1 -0
- package/dist/src/index.d.ts +9 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +18 -6
- package/dist/src/index.js.map +1 -1
- package/dist/src/utils/libp2p-defaults.browser.d.ts +0 -2
- package/dist/src/utils/libp2p-defaults.browser.d.ts.map +1 -1
- package/dist/src/utils/libp2p-defaults.browser.js +1 -3
- package/dist/src/utils/libp2p-defaults.browser.js.map +1 -1
- package/dist/src/utils/libp2p-defaults.d.ts +0 -2
- package/dist/src/utils/libp2p-defaults.d.ts.map +1 -1
- package/dist/src/utils/libp2p-defaults.js +0 -2
- package/dist/src/utils/libp2p-defaults.js.map +1 -1
- package/dist/src/utils/libp2p.d.ts.map +1 -1
- package/dist/src/utils/libp2p.js +14 -6
- package/dist/src/utils/libp2p.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/package.json +17 -27
- package/src/helia-p2p.ts +37 -0
- package/src/index.ts +30 -10
- package/src/utils/libp2p-defaults.browser.ts +1 -5
- package/src/utils/libp2p-defaults.ts +0 -4
- package/src/utils/libp2p.ts +17 -8
- package/src/version.ts +1 -1
- package/dist/src/helia.d.ts +0 -26
- package/dist/src/helia.d.ts.map +0 -1
- package/dist/src/helia.js +0 -81
- package/dist/src/helia.js.map +0 -1
- package/dist/src/pins.d.ts +0 -17
- package/dist/src/pins.d.ts.map +0 -1
- package/dist/src/pins.js +0 -166
- package/dist/src/pins.js.map +0 -1
- package/dist/src/storage.d.ts +0 -63
- package/dist/src/storage.d.ts.map +0 -1
- package/dist/src/storage.js +0 -140
- package/dist/src/storage.js.map +0 -1
- package/dist/src/utils/dag-walkers.d.ts +0 -27
- package/dist/src/utils/dag-walkers.d.ts.map +0 -1
- package/dist/src/utils/dag-walkers.js +0 -157
- package/dist/src/utils/dag-walkers.js.map +0 -1
- package/dist/src/utils/datastore-version.d.ts +0 -3
- package/dist/src/utils/datastore-version.d.ts.map +0 -1
- package/dist/src/utils/datastore-version.js +0 -19
- package/dist/src/utils/datastore-version.js.map +0 -1
- package/dist/src/utils/default-hashers.d.ts +0 -3
- package/dist/src/utils/default-hashers.d.ts.map +0 -1
- package/dist/src/utils/default-hashers.js +0 -11
- package/dist/src/utils/default-hashers.js.map +0 -1
- package/dist/typedoc-urls.json +0 -10
- package/src/helia.ts +0 -107
- package/src/pins.ts +0 -239
- package/src/storage.ts +0 -172
- package/src/utils/dag-walkers.ts +0 -181
- package/src/utils/datastore-version.ts +0 -23
- package/src/utils/default-hashers.ts +0 -12
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/* eslint max-depth: ["error", 7] */
|
|
2
|
-
import * as dagCbor from '@ipld/dag-cbor';
|
|
3
|
-
import * as dagJson from '@ipld/dag-json';
|
|
4
|
-
import * as dagPb from '@ipld/dag-pb';
|
|
5
|
-
import * as cborg from 'cborg';
|
|
6
|
-
import { Type, Token } from 'cborg';
|
|
7
|
-
import * as cborgJson from 'cborg/json';
|
|
8
|
-
import { CID } from 'multiformats';
|
|
9
|
-
import { base64 } from 'multiformats/bases/base64';
|
|
10
|
-
import * as json from 'multiformats/codecs/json';
|
|
11
|
-
import * as raw from 'multiformats/codecs/raw';
|
|
12
|
-
/**
|
|
13
|
-
* Dag walker for dag-pb CIDs
|
|
14
|
-
*/
|
|
15
|
-
export const dagPbWalker = {
|
|
16
|
-
codec: dagPb.code,
|
|
17
|
-
*walk(block) {
|
|
18
|
-
const node = dagPb.decode(block);
|
|
19
|
-
yield* node.Links.map(l => l.Hash);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Dag walker for raw CIDs
|
|
24
|
-
*/
|
|
25
|
-
export const rawWalker = {
|
|
26
|
-
codec: raw.code,
|
|
27
|
-
*walk() {
|
|
28
|
-
// no embedded CIDs in a raw block
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
|
|
32
|
-
const CID_TAG = 42;
|
|
33
|
-
/**
|
|
34
|
-
* Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
|
|
35
|
-
* all we are interested in is extracting the the CIDs from the block
|
|
36
|
-
* so we can just use cborg for that.
|
|
37
|
-
*/
|
|
38
|
-
export const dagCborWalker = {
|
|
39
|
-
codec: dagCbor.code,
|
|
40
|
-
*walk(block) {
|
|
41
|
-
const cids = [];
|
|
42
|
-
const tags = [];
|
|
43
|
-
tags[CID_TAG] = (bytes) => {
|
|
44
|
-
if (bytes[0] !== 0) {
|
|
45
|
-
throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00');
|
|
46
|
-
}
|
|
47
|
-
const cid = CID.decode(bytes.subarray(1)); // ignore leading 0x00
|
|
48
|
-
cids.push(cid);
|
|
49
|
-
return cid;
|
|
50
|
-
};
|
|
51
|
-
cborg.decode(block, {
|
|
52
|
-
tags
|
|
53
|
-
});
|
|
54
|
-
yield* cids;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Borrowed from @ipld/dag-json
|
|
59
|
-
*/
|
|
60
|
-
class DagJsonTokenizer extends cborgJson.Tokenizer {
|
|
61
|
-
tokenBuffer;
|
|
62
|
-
constructor(data, options) {
|
|
63
|
-
super(data, options);
|
|
64
|
-
this.tokenBuffer = [];
|
|
65
|
-
}
|
|
66
|
-
done() {
|
|
67
|
-
return this.tokenBuffer.length === 0 && super.done();
|
|
68
|
-
}
|
|
69
|
-
_next() {
|
|
70
|
-
if (this.tokenBuffer.length > 0) {
|
|
71
|
-
// @ts-expect-error https://github.com/Microsoft/TypeScript/issues/30406
|
|
72
|
-
return this.tokenBuffer.pop();
|
|
73
|
-
}
|
|
74
|
-
return super.next();
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Implements rules outlined in https://github.com/ipld/specs/pull/356
|
|
78
|
-
*/
|
|
79
|
-
next() {
|
|
80
|
-
const token = this._next();
|
|
81
|
-
if (token.type === Type.map) {
|
|
82
|
-
const keyToken = this._next();
|
|
83
|
-
if (keyToken.type === Type.string && keyToken.value === '/') {
|
|
84
|
-
const valueToken = this._next();
|
|
85
|
-
if (valueToken.type === Type.string) { // *must* be a CID
|
|
86
|
-
const breakToken = this._next(); // swallow the end-of-map token
|
|
87
|
-
if (breakToken.type !== Type.break) {
|
|
88
|
-
throw new Error('Invalid encoded CID form');
|
|
89
|
-
}
|
|
90
|
-
this.tokenBuffer.push(valueToken); // CID.parse will pick this up after our tag token
|
|
91
|
-
return new Token(Type.tag, 42, 0);
|
|
92
|
-
}
|
|
93
|
-
if (valueToken.type === Type.map) {
|
|
94
|
-
const innerKeyToken = this._next();
|
|
95
|
-
if (innerKeyToken.type === Type.string && innerKeyToken.value === 'bytes') {
|
|
96
|
-
const innerValueToken = this._next();
|
|
97
|
-
if (innerValueToken.type === Type.string) { // *must* be Bytes
|
|
98
|
-
for (let i = 0; i < 2; i++) {
|
|
99
|
-
const breakToken = this._next(); // swallow two end-of-map tokens
|
|
100
|
-
if (breakToken.type !== Type.break) {
|
|
101
|
-
throw new Error('Invalid encoded Bytes form');
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const bytes = base64.decode(`m${innerValueToken.value}`);
|
|
105
|
-
return new Token(Type.bytes, bytes, innerValueToken.value.length);
|
|
106
|
-
}
|
|
107
|
-
this.tokenBuffer.push(innerValueToken); // bail
|
|
108
|
-
}
|
|
109
|
-
this.tokenBuffer.push(innerKeyToken); // bail
|
|
110
|
-
}
|
|
111
|
-
this.tokenBuffer.push(valueToken); // bail
|
|
112
|
-
}
|
|
113
|
-
this.tokenBuffer.push(keyToken); // bail
|
|
114
|
-
}
|
|
115
|
-
return token;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Dag walker for dag-json CIDs. Does not actually use dag-json since
|
|
120
|
-
* all we are interested in is extracting the the CIDs from the block
|
|
121
|
-
* so we can just use cborg/json for that.
|
|
122
|
-
*/
|
|
123
|
-
export const dagJsonWalker = {
|
|
124
|
-
codec: dagJson.code,
|
|
125
|
-
*walk(block) {
|
|
126
|
-
const cids = [];
|
|
127
|
-
const tags = [];
|
|
128
|
-
tags[CID_TAG] = (string) => {
|
|
129
|
-
const cid = CID.parse(string);
|
|
130
|
-
cids.push(cid);
|
|
131
|
-
return cid;
|
|
132
|
-
};
|
|
133
|
-
cborgJson.decode(block, {
|
|
134
|
-
tags,
|
|
135
|
-
tokenizer: new DagJsonTokenizer(block, {
|
|
136
|
-
tags,
|
|
137
|
-
allowIndefinite: true,
|
|
138
|
-
allowUndefined: true,
|
|
139
|
-
allowNaN: true,
|
|
140
|
-
allowInfinity: true,
|
|
141
|
-
allowBigInt: true,
|
|
142
|
-
strict: false,
|
|
143
|
-
rejectDuplicateMapKeys: false
|
|
144
|
-
})
|
|
145
|
-
});
|
|
146
|
-
yield* cids;
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
/**
|
|
150
|
-
* Dag walker for json CIDs. JSON has no facility for linking to
|
|
151
|
-
* external blocks so the walker is a no-op.
|
|
152
|
-
*/
|
|
153
|
-
export const jsonWalker = {
|
|
154
|
-
codec: json.code,
|
|
155
|
-
*walk() { }
|
|
156
|
-
};
|
|
157
|
-
//# sourceMappingURL=dag-walkers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dag-walkers.js","sourceRoot":"","sources":["../../../src/utils/dag-walkers.ts"],"names":[],"mappings":"AAAA,oCAAoC;AAEpC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO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B,EAAE,KAAK;aAC9B,CAAC;SACH,CAAC,CAAA;QAEF,KAAM,CAAC,CAAC,IAAI,CAAA;IACd,CAAC;CACF,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAc;IACnC,KAAK,EAAE,IAAI,CAAC,IAAI;IAChB,CAAE,IAAI,KAAK,CAAC;CACb,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"datastore-version.d.ts","sourceRoot":"","sources":["../../../src/utils/datastore-version.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAO,MAAM,qBAAqB,CAAA;AAOzD,wBAAsB,+BAA+B,CAAE,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAe1F"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Key } from 'interface-datastore';
|
|
2
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
3
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
4
|
-
const DS_VERSION_KEY = new Key('/version');
|
|
5
|
-
const CURRENT_VERSION = 1;
|
|
6
|
-
export async function assertDatastoreVersionIsCurrent(datastore) {
|
|
7
|
-
if (!(await datastore.has(DS_VERSION_KEY))) {
|
|
8
|
-
await datastore.put(DS_VERSION_KEY, uint8ArrayFromString(`${CURRENT_VERSION}`));
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
const buf = await datastore.get(DS_VERSION_KEY);
|
|
12
|
-
const str = uint8ArrayToString(buf);
|
|
13
|
-
const version = parseInt(str, 10);
|
|
14
|
-
if (version !== CURRENT_VERSION) {
|
|
15
|
-
// TODO: write migrations when we break compatibility - for an example, see https://github.com/ipfs/js-ipfs-repo/tree/master/packages/ipfs-repo-migrations
|
|
16
|
-
throw new Error('Unknown datastore version, a datastore migration may be required');
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=datastore-version.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"datastore-version.js","sourceRoot":"","sources":["../../../src/utils/datastore-version.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,GAAG,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAEtE,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,CAAA;AAC1C,MAAM,eAAe,GAAG,CAAC,CAAA;AAEzB,MAAM,CAAC,KAAK,UAAU,+BAA+B,CAAE,SAAoB;IACzE,IAAI,CAAC,CAAC,MAAM,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC;QAC3C,MAAM,SAAS,CAAC,GAAG,CAAC,cAAc,EAAE,oBAAoB,CAAC,GAAG,eAAe,EAAE,CAAC,CAAC,CAAA;QAE/E,OAAM;IACR,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;IAC/C,MAAM,GAAG,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAA;IACnC,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;IAEjC,IAAI,OAAO,KAAK,eAAe,EAAE,CAAC;QAChC,0JAA0J;QAC1J,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAA;IACrF,CAAC;AACH,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"default-hashers.d.ts","sourceRoot":"","sources":["../../../src/utils/default-hashers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAEpE,wBAAgB,cAAc,CAAE,OAAO,GAAE,eAAe,EAAO,GAAG,eAAe,EAAE,CAOlF"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { identity } from 'multiformats/hashes/identity';
|
|
2
|
-
import { sha256, sha512 } from 'multiformats/hashes/sha2';
|
|
3
|
-
export function defaultHashers(hashers = []) {
|
|
4
|
-
return [
|
|
5
|
-
sha256,
|
|
6
|
-
sha512,
|
|
7
|
-
identity,
|
|
8
|
-
...hashers
|
|
9
|
-
];
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=default-hashers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"default-hashers.js","sourceRoot":"","sources":["../../../src/utils/default-hashers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAGzD,MAAM,UAAU,cAAc,CAAE,UAA6B,EAAE;IAC7D,OAAO;QACL,MAAM;QACN,MAAM;QACN,QAAQ;QACR,GAAG,OAAO;KACX,CAAA;AACH,CAAC"}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"DAGWalker": "https://ipfs.github.io/helia/interfaces/helia.DAGWalker.html",
|
|
3
|
-
".:DAGWalker": "https://ipfs.github.io/helia/interfaces/helia.DAGWalker.html",
|
|
4
|
-
"DefaultLibp2pServices": "https://ipfs.github.io/helia/interfaces/helia.DefaultLibp2pServices.html",
|
|
5
|
-
"HeliaInit": "https://ipfs.github.io/helia/interfaces/helia.HeliaInit.html",
|
|
6
|
-
".:HeliaInit": "https://ipfs.github.io/helia/interfaces/helia.HeliaInit.html",
|
|
7
|
-
"createHelia": "https://ipfs.github.io/helia/functions/helia.createHelia.html",
|
|
8
|
-
".:createHelia": "https://ipfs.github.io/helia/functions/helia.createHelia.html",
|
|
9
|
-
"libp2pDefaults": "https://ipfs.github.io/helia/functions/helia.libp2pDefaults.html"
|
|
10
|
-
}
|
package/src/helia.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { bitswap, trustlessGateway, NetworkedStorage } from '@helia/block-brokers'
|
|
2
|
-
import { start, stop } from '@libp2p/interface'
|
|
3
|
-
import drain from 'it-drain'
|
|
4
|
-
import { CustomProgressEvent } from 'progress-events'
|
|
5
|
-
import { PinsImpl } from './pins.js'
|
|
6
|
-
import { BlockStorage } from './storage.js'
|
|
7
|
-
import { assertDatastoreVersionIsCurrent } from './utils/datastore-version.js'
|
|
8
|
-
import { defaultHashers } from './utils/default-hashers.js'
|
|
9
|
-
import type { HeliaInit } from '.'
|
|
10
|
-
import type { GCOptions, Helia } from '@helia/interface'
|
|
11
|
-
import type { Pins } from '@helia/interface/pins'
|
|
12
|
-
import type { ComponentLogger, Libp2p, Logger } from '@libp2p/interface'
|
|
13
|
-
import type { Blockstore } from 'interface-blockstore'
|
|
14
|
-
import type { Datastore } from 'interface-datastore'
|
|
15
|
-
import type { CID } from 'multiformats/cid'
|
|
16
|
-
|
|
17
|
-
interface HeliaImplInit<T extends Libp2p = Libp2p> extends HeliaInit<T> {
|
|
18
|
-
libp2p: T
|
|
19
|
-
blockstore: Blockstore
|
|
20
|
-
datastore: Datastore
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class HeliaImpl implements Helia {
|
|
24
|
-
public libp2p: Libp2p
|
|
25
|
-
public blockstore: BlockStorage
|
|
26
|
-
public datastore: Datastore
|
|
27
|
-
public pins: Pins
|
|
28
|
-
public logger: ComponentLogger
|
|
29
|
-
private readonly log: Logger
|
|
30
|
-
|
|
31
|
-
constructor (init: HeliaImplInit) {
|
|
32
|
-
this.logger = init.libp2p.logger
|
|
33
|
-
this.log = this.logger.forComponent('helia')
|
|
34
|
-
const hashers = defaultHashers(init.hashers)
|
|
35
|
-
|
|
36
|
-
const components = {
|
|
37
|
-
blockstore: init.blockstore,
|
|
38
|
-
datastore: init.datastore,
|
|
39
|
-
libp2p: init.libp2p,
|
|
40
|
-
hashers,
|
|
41
|
-
logger: init.libp2p.logger
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const blockBrokers = init.blockBrokers?.map((fn) => {
|
|
45
|
-
return fn(components)
|
|
46
|
-
}) ?? [
|
|
47
|
-
bitswap()(components),
|
|
48
|
-
trustlessGateway()(components)
|
|
49
|
-
]
|
|
50
|
-
|
|
51
|
-
const networkedStorage = new NetworkedStorage(components, {
|
|
52
|
-
blockBrokers,
|
|
53
|
-
hashers
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
this.pins = new PinsImpl(init.datastore, networkedStorage, init.dagWalkers ?? [])
|
|
57
|
-
|
|
58
|
-
this.libp2p = init.libp2p
|
|
59
|
-
this.blockstore = new BlockStorage(networkedStorage, this.pins, {
|
|
60
|
-
holdGcLock: init.holdGcLock
|
|
61
|
-
})
|
|
62
|
-
this.datastore = init.datastore
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async start (): Promise<void> {
|
|
66
|
-
await assertDatastoreVersionIsCurrent(this.datastore)
|
|
67
|
-
await start(this.blockstore)
|
|
68
|
-
await this.libp2p.start()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async stop (): Promise<void> {
|
|
72
|
-
await this.libp2p.stop()
|
|
73
|
-
await stop(this.blockstore)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async gc (options: GCOptions = {}): Promise<void> {
|
|
77
|
-
const releaseLock = await this.blockstore.lock.writeLock()
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
const helia = this
|
|
81
|
-
const blockstore = this.blockstore.unwrap()
|
|
82
|
-
|
|
83
|
-
this.log('gc start')
|
|
84
|
-
|
|
85
|
-
await drain(blockstore.deleteMany((async function * (): AsyncGenerator<CID> {
|
|
86
|
-
for await (const { cid } of blockstore.getAll()) {
|
|
87
|
-
try {
|
|
88
|
-
if (await helia.pins.isPinned(cid, options)) {
|
|
89
|
-
continue
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
yield cid
|
|
93
|
-
|
|
94
|
-
options.onProgress?.(new CustomProgressEvent<CID>('helia:gc:deleted', cid))
|
|
95
|
-
} catch (err) {
|
|
96
|
-
helia.log.error('Error during gc', err)
|
|
97
|
-
options.onProgress?.(new CustomProgressEvent<Error>('helia:gc:error', err))
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}())))
|
|
101
|
-
} finally {
|
|
102
|
-
releaseLock()
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
this.log('gc finished')
|
|
106
|
-
}
|
|
107
|
-
}
|
package/src/pins.ts
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import { Queue } from '@libp2p/utils/queue'
|
|
2
|
-
import * as cborg from 'cborg'
|
|
3
|
-
import { type Datastore, Key } from 'interface-datastore'
|
|
4
|
-
import { base36 } from 'multiformats/bases/base36'
|
|
5
|
-
import { CID, type Version } from 'multiformats/cid'
|
|
6
|
-
import { CustomProgressEvent, type ProgressOptions } from 'progress-events'
|
|
7
|
-
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
8
|
-
import { dagCborWalker, dagJsonWalker, dagPbWalker, jsonWalker, rawWalker } from './utils/dag-walkers.js'
|
|
9
|
-
import type { DAGWalker, GetBlockProgressEvents } from './index.js'
|
|
10
|
-
import type { AddOptions, AddPinEvents, IsPinnedOptions, LsOptions, Pin, Pins, RmOptions } from '@helia/interface/pins'
|
|
11
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
12
|
-
import type { Blockstore } from 'interface-blockstore'
|
|
13
|
-
|
|
14
|
-
const DEFAULT_DAG_WALKERS = [
|
|
15
|
-
rawWalker,
|
|
16
|
-
dagPbWalker,
|
|
17
|
-
dagCborWalker,
|
|
18
|
-
dagJsonWalker,
|
|
19
|
-
jsonWalker
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
interface DatastorePin {
|
|
23
|
-
/**
|
|
24
|
-
* 0 for a direct pin or an arbitrary (+ve, whole) number or Infinity
|
|
25
|
-
*/
|
|
26
|
-
depth: number
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* User-specific metadata for the pin
|
|
30
|
-
*/
|
|
31
|
-
metadata: Record<string, string | number | boolean>
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface DatastorePinnedBlock {
|
|
35
|
-
pinCount: number
|
|
36
|
-
pinnedBy: Uint8Array[]
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Callback for updating a {@link DatastorePinnedBlock}'s properties when
|
|
41
|
-
* calling `#updatePinnedBlock`
|
|
42
|
-
*
|
|
43
|
-
* The callback should return `false` to prevent any pinning modifications to
|
|
44
|
-
* the block, and true in all other cases.
|
|
45
|
-
*/
|
|
46
|
-
interface WithPinnedBlockCallback {
|
|
47
|
-
(pinnedBlock: DatastorePinnedBlock): boolean
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const DATASTORE_PIN_PREFIX = '/pin/'
|
|
51
|
-
const DATASTORE_BLOCK_PREFIX = '/pinned-block/'
|
|
52
|
-
const DATASTORE_ENCODING = base36
|
|
53
|
-
const DAG_WALK_QUEUE_CONCURRENCY = 1
|
|
54
|
-
|
|
55
|
-
interface WalkDagOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents | AddPinEvents> {
|
|
56
|
-
depth: number
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function toDSKey (cid: CID): Key {
|
|
60
|
-
if (cid.version === 0) {
|
|
61
|
-
cid = cid.toV1()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return new Key(`${DATASTORE_PIN_PREFIX}${cid.toString(DATASTORE_ENCODING)}`)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export class PinsImpl implements Pins {
|
|
68
|
-
private readonly datastore: Datastore
|
|
69
|
-
private readonly blockstore: Blockstore
|
|
70
|
-
private dagWalkers: Record<number, DAGWalker>
|
|
71
|
-
|
|
72
|
-
constructor (datastore: Datastore, blockstore: Blockstore, dagWalkers: DAGWalker[]) {
|
|
73
|
-
this.datastore = datastore
|
|
74
|
-
this.blockstore = blockstore
|
|
75
|
-
this.dagWalkers = {}
|
|
76
|
-
|
|
77
|
-
;[...DEFAULT_DAG_WALKERS, ...dagWalkers].forEach(dagWalker => {
|
|
78
|
-
this.dagWalkers[dagWalker.codec] = dagWalker
|
|
79
|
-
})
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async * add (cid: CID<unknown, number, number, Version>, options: AddOptions = {}): AsyncGenerator<CID, void, undefined> {
|
|
83
|
-
const pinKey = toDSKey(cid)
|
|
84
|
-
|
|
85
|
-
if (await this.datastore.has(pinKey)) {
|
|
86
|
-
throw new Error('Already pinned')
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const depth = Math.round(options.depth ?? Infinity)
|
|
90
|
-
|
|
91
|
-
if (depth < 0) {
|
|
92
|
-
throw new Error('Depth must be greater than or equal to 0')
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// use a queue to walk the DAG instead of recursion so we can traverse very large DAGs
|
|
96
|
-
const queue = new Queue<AsyncGenerator<CID>>({
|
|
97
|
-
concurrency: DAG_WALK_QUEUE_CONCURRENCY
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
for await (const childCid of this.#walkDag(cid, queue, {
|
|
101
|
-
...options,
|
|
102
|
-
depth
|
|
103
|
-
})) {
|
|
104
|
-
await this.#updatePinnedBlock(childCid, (pinnedBlock: DatastorePinnedBlock) => {
|
|
105
|
-
// do not update pinned block if this block is already pinned by this CID
|
|
106
|
-
if (pinnedBlock.pinnedBy.find(c => uint8ArrayEquals(c, cid.bytes)) != null) {
|
|
107
|
-
return false
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
pinnedBlock.pinCount++
|
|
111
|
-
pinnedBlock.pinnedBy.push(cid.bytes)
|
|
112
|
-
return true
|
|
113
|
-
}, options)
|
|
114
|
-
|
|
115
|
-
yield childCid
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const pin: DatastorePin = {
|
|
119
|
-
depth,
|
|
120
|
-
metadata: options.metadata ?? {}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
await this.datastore.put(pinKey, cborg.encode(pin), options)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Walk a DAG in an iterable fashion
|
|
128
|
-
*/
|
|
129
|
-
async * #walkDag (cid: CID, queue: Queue<AsyncGenerator<CID>>, options: WalkDagOptions): AsyncGenerator<CID> {
|
|
130
|
-
if (options.depth === -1) {
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const dagWalker = this.dagWalkers[cid.code]
|
|
135
|
-
|
|
136
|
-
if (dagWalker == null) {
|
|
137
|
-
throw new Error(`No dag walker found for cid codec ${cid.code}`)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const block = await this.blockstore.get(cid, options)
|
|
141
|
-
|
|
142
|
-
yield cid
|
|
143
|
-
|
|
144
|
-
// walk dag, ensure all blocks are present
|
|
145
|
-
for await (const cid of dagWalker.walk(block)) {
|
|
146
|
-
yield * await queue.add(async () => {
|
|
147
|
-
return this.#walkDag(cid, queue, {
|
|
148
|
-
...options,
|
|
149
|
-
depth: options.depth - 1
|
|
150
|
-
})
|
|
151
|
-
})
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Update the pin count for the CID
|
|
157
|
-
*/
|
|
158
|
-
async #updatePinnedBlock (cid: CID, withPinnedBlock: WithPinnedBlockCallback, options: AddOptions): Promise<void> {
|
|
159
|
-
const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`)
|
|
160
|
-
|
|
161
|
-
let pinnedBlock: DatastorePinnedBlock = {
|
|
162
|
-
pinCount: 0,
|
|
163
|
-
pinnedBy: []
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
try {
|
|
167
|
-
pinnedBlock = cborg.decode(await this.datastore.get(blockKey, options))
|
|
168
|
-
} catch (err: any) {
|
|
169
|
-
if (err.code !== 'ERR_NOT_FOUND') {
|
|
170
|
-
throw err
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const shouldContinue = withPinnedBlock(pinnedBlock)
|
|
175
|
-
|
|
176
|
-
if (!shouldContinue) {
|
|
177
|
-
return
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (pinnedBlock.pinCount === 0) {
|
|
181
|
-
if (await this.datastore.has(blockKey)) {
|
|
182
|
-
await this.datastore.delete(blockKey)
|
|
183
|
-
return
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
await this.datastore.put(blockKey, cborg.encode(pinnedBlock), options)
|
|
188
|
-
options.onProgress?.(new CustomProgressEvent<CID>('helia:pin:add', cid))
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async * rm (cid: CID<unknown, number, number, Version>, options: RmOptions = {}): AsyncGenerator<CID, void, undefined> {
|
|
192
|
-
const pinKey = toDSKey(cid)
|
|
193
|
-
const buf = await this.datastore.get(pinKey, options)
|
|
194
|
-
const pin = cborg.decode(buf)
|
|
195
|
-
|
|
196
|
-
await this.datastore.delete(pinKey, options)
|
|
197
|
-
|
|
198
|
-
// use a queue to walk the DAG instead of recursion so we can traverse very large DAGs
|
|
199
|
-
const queue = new Queue<AsyncGenerator<CID>>({
|
|
200
|
-
concurrency: DAG_WALK_QUEUE_CONCURRENCY
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
for await (const childCid of this.#walkDag(cid, queue, {
|
|
204
|
-
...options,
|
|
205
|
-
depth: pin.depth
|
|
206
|
-
})) {
|
|
207
|
-
await this.#updatePinnedBlock(childCid, (pinnedBlock): boolean => {
|
|
208
|
-
pinnedBlock.pinCount--
|
|
209
|
-
pinnedBlock.pinnedBy = pinnedBlock.pinnedBy.filter(c => uint8ArrayEquals(c, cid.bytes))
|
|
210
|
-
return true
|
|
211
|
-
}, {
|
|
212
|
-
...options,
|
|
213
|
-
depth: pin.depth
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
yield childCid
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async * ls (options: LsOptions = {}): AsyncGenerator<Pin, void, undefined> {
|
|
221
|
-
for await (const { key, value } of this.datastore.query({
|
|
222
|
-
prefix: DATASTORE_PIN_PREFIX + (options.cid != null ? `${options.cid.toString(base36)}` : '')
|
|
223
|
-
}, options)) {
|
|
224
|
-
const cid = CID.parse(key.toString().substring(5), base36)
|
|
225
|
-
const pin = cborg.decode(value)
|
|
226
|
-
|
|
227
|
-
yield {
|
|
228
|
-
cid,
|
|
229
|
-
...pin
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async isPinned (cid: CID, options: IsPinnedOptions = {}): Promise<boolean> {
|
|
235
|
-
const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`)
|
|
236
|
-
|
|
237
|
-
return this.datastore.has(blockKey, options)
|
|
238
|
-
}
|
|
239
|
-
}
|