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.
Files changed (62) hide show
  1. package/dist/index.min.js +39 -61
  2. package/dist/src/helia-p2p.d.ts +18 -0
  3. package/dist/src/helia-p2p.d.ts.map +1 -0
  4. package/dist/src/helia-p2p.js +22 -0
  5. package/dist/src/helia-p2p.js.map +1 -0
  6. package/dist/src/index.d.ts +9 -5
  7. package/dist/src/index.d.ts.map +1 -1
  8. package/dist/src/index.js +18 -6
  9. package/dist/src/index.js.map +1 -1
  10. package/dist/src/utils/libp2p-defaults.browser.d.ts +0 -2
  11. package/dist/src/utils/libp2p-defaults.browser.d.ts.map +1 -1
  12. package/dist/src/utils/libp2p-defaults.browser.js +1 -3
  13. package/dist/src/utils/libp2p-defaults.browser.js.map +1 -1
  14. package/dist/src/utils/libp2p-defaults.d.ts +0 -2
  15. package/dist/src/utils/libp2p-defaults.d.ts.map +1 -1
  16. package/dist/src/utils/libp2p-defaults.js +0 -2
  17. package/dist/src/utils/libp2p-defaults.js.map +1 -1
  18. package/dist/src/utils/libp2p.d.ts.map +1 -1
  19. package/dist/src/utils/libp2p.js +14 -6
  20. package/dist/src/utils/libp2p.js.map +1 -1
  21. package/dist/src/version.d.ts +1 -1
  22. package/dist/src/version.d.ts.map +1 -1
  23. package/dist/src/version.js +1 -1
  24. package/dist/src/version.js.map +1 -1
  25. package/package.json +17 -27
  26. package/src/helia-p2p.ts +37 -0
  27. package/src/index.ts +30 -10
  28. package/src/utils/libp2p-defaults.browser.ts +1 -5
  29. package/src/utils/libp2p-defaults.ts +0 -4
  30. package/src/utils/libp2p.ts +17 -8
  31. package/src/version.ts +1 -1
  32. package/dist/src/helia.d.ts +0 -26
  33. package/dist/src/helia.d.ts.map +0 -1
  34. package/dist/src/helia.js +0 -81
  35. package/dist/src/helia.js.map +0 -1
  36. package/dist/src/pins.d.ts +0 -17
  37. package/dist/src/pins.d.ts.map +0 -1
  38. package/dist/src/pins.js +0 -166
  39. package/dist/src/pins.js.map +0 -1
  40. package/dist/src/storage.d.ts +0 -63
  41. package/dist/src/storage.d.ts.map +0 -1
  42. package/dist/src/storage.js +0 -140
  43. package/dist/src/storage.js.map +0 -1
  44. package/dist/src/utils/dag-walkers.d.ts +0 -27
  45. package/dist/src/utils/dag-walkers.d.ts.map +0 -1
  46. package/dist/src/utils/dag-walkers.js +0 -157
  47. package/dist/src/utils/dag-walkers.js.map +0 -1
  48. package/dist/src/utils/datastore-version.d.ts +0 -3
  49. package/dist/src/utils/datastore-version.d.ts.map +0 -1
  50. package/dist/src/utils/datastore-version.js +0 -19
  51. package/dist/src/utils/datastore-version.js.map +0 -1
  52. package/dist/src/utils/default-hashers.d.ts +0 -3
  53. package/dist/src/utils/default-hashers.d.ts.map +0 -1
  54. package/dist/src/utils/default-hashers.js +0 -11
  55. package/dist/src/utils/default-hashers.js.map +0 -1
  56. package/dist/typedoc-urls.json +0 -10
  57. package/src/helia.ts +0 -107
  58. package/src/pins.ts +0 -239
  59. package/src/storage.ts +0 -172
  60. package/src/utils/dag-walkers.ts +0 -181
  61. package/src/utils/datastore-version.ts +0 -23
  62. package/src/utils/default-hashers.ts +0 -12
package/src/storage.ts DELETED
@@ -1,172 +0,0 @@
1
- import { start, stop } from '@libp2p/interface'
2
- import createMortice from 'mortice'
3
- import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions } from '@helia/interface/blocks'
4
- import type { Pins } from '@helia/interface/pins'
5
- import type { AbortOptions, Startable } from '@libp2p/interface'
6
- import type { Blockstore } from 'interface-blockstore'
7
- import type { AwaitIterable } from 'interface-store'
8
- import type { Mortice } from 'mortice'
9
- import type { CID } from 'multiformats/cid'
10
- import type { ProgressOptions } from 'progress-events'
11
-
12
- export interface BlockStorageInit {
13
- holdGcLock?: boolean
14
- }
15
-
16
- export interface GetOptions extends AbortOptions {
17
- progress?(evt: Event): void
18
- }
19
-
20
- /**
21
- * BlockStorage is a hybrid blockstore that puts/gets blocks from a configured
22
- * blockstore (that may be on disk, s3, or something else). If the blocks are
23
- * not present Bitswap will be used to fetch them from network peers.
24
- */
25
- export class BlockStorage implements Blocks, Startable {
26
- public lock: Mortice
27
- private readonly child: Blockstore
28
- private readonly pins: Pins
29
- private started: boolean
30
-
31
- /**
32
- * Create a new BlockStorage
33
- */
34
- constructor (blockstore: Blockstore, pins: Pins, options: BlockStorageInit = {}) {
35
- this.child = blockstore
36
- this.pins = pins
37
- this.lock = createMortice({
38
- singleProcess: options.holdGcLock
39
- })
40
- this.started = false
41
- }
42
-
43
- isStarted (): boolean {
44
- return this.started
45
- }
46
-
47
- async start (): Promise<void> {
48
- await start(this.child)
49
- this.started = true
50
- }
51
-
52
- async stop (): Promise<void> {
53
- await stop(this.child)
54
- this.started = false
55
- }
56
-
57
- unwrap (): Blockstore {
58
- return this.child
59
- }
60
-
61
- /**
62
- * Put a block to the underlying datastore
63
- */
64
- async put (cid: CID, block: Uint8Array, options: AbortOptions & ProgressOptions<PutBlockProgressEvents> = {}): Promise<CID> {
65
- const releaseLock = await this.lock.readLock()
66
-
67
- try {
68
- return await this.child.put(cid, block, options)
69
- } finally {
70
- releaseLock()
71
- }
72
- }
73
-
74
- /**
75
- * Put a multiple blocks to the underlying datastore
76
- */
77
- async * putMany (blocks: AwaitIterable<{ cid: CID, block: Uint8Array }>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
78
- const releaseLock = await this.lock.readLock()
79
-
80
- try {
81
- yield * this.child.putMany(blocks, options)
82
- } finally {
83
- releaseLock()
84
- }
85
- }
86
-
87
- /**
88
- * Get a block by cid
89
- */
90
- async get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): Promise<Uint8Array> {
91
- const releaseLock = await this.lock.readLock()
92
-
93
- try {
94
- return await this.child.get(cid, options)
95
- } finally {
96
- releaseLock()
97
- }
98
- }
99
-
100
- /**
101
- * Get multiple blocks back from an (async) iterable of cids
102
- */
103
- async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncIterable<Pair> {
104
- const releaseLock = await this.lock.readLock()
105
-
106
- try {
107
- yield * this.child.getMany(cids, options)
108
- } finally {
109
- releaseLock()
110
- }
111
- }
112
-
113
- /**
114
- * Delete a block from the blockstore
115
- */
116
- async delete (cid: CID, options: AbortOptions & ProgressOptions<DeleteBlockProgressEvents> = {}): Promise<void> {
117
- const releaseLock = await this.lock.writeLock()
118
-
119
- try {
120
- if (await this.pins.isPinned(cid)) {
121
- throw new Error('CID was pinned')
122
- }
123
-
124
- await this.child.delete(cid, options)
125
- } finally {
126
- releaseLock()
127
- }
128
- }
129
-
130
- /**
131
- * Delete multiple blocks from the blockstore
132
- */
133
- async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
134
- const releaseLock = await this.lock.writeLock()
135
-
136
- try {
137
- const storage = this
138
-
139
- yield * this.child.deleteMany((async function * (): AsyncGenerator<CID> {
140
- for await (const cid of cids) {
141
- if (await storage.pins.isPinned(cid)) {
142
- throw new Error('CID was pinned')
143
- }
144
-
145
- yield cid
146
- }
147
- }()), options)
148
- } finally {
149
- releaseLock()
150
- }
151
- }
152
-
153
- async has (cid: CID, options: AbortOptions = {}): Promise<boolean> {
154
- const releaseLock = await this.lock.readLock()
155
-
156
- try {
157
- return await this.child.has(cid, options)
158
- } finally {
159
- releaseLock()
160
- }
161
- }
162
-
163
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncIterable<Pair> {
164
- const releaseLock = await this.lock.readLock()
165
-
166
- try {
167
- yield * this.child.getAll(options)
168
- } finally {
169
- releaseLock()
170
- }
171
- }
172
- }
@@ -1,181 +0,0 @@
1
- /* eslint max-depth: ["error", 7] */
2
-
3
- import * as dagCbor from '@ipld/dag-cbor'
4
- import * as dagJson from '@ipld/dag-json'
5
- import * as dagPb from '@ipld/dag-pb'
6
- import * as cborg from 'cborg'
7
- import { Type, Token } from 'cborg'
8
- import * as cborgJson from 'cborg/json'
9
- import { CID } from 'multiformats'
10
- import { base64 } from 'multiformats/bases/base64'
11
- import * as json from 'multiformats/codecs/json'
12
- import * as raw from 'multiformats/codecs/raw'
13
- import type { DAGWalker } from '../index.js'
14
-
15
- /**
16
- * Dag walker for dag-pb CIDs
17
- */
18
- export const dagPbWalker: DAGWalker = {
19
- codec: dagPb.code,
20
- * walk (block) {
21
- const node = dagPb.decode(block)
22
-
23
- yield * node.Links.map(l => l.Hash)
24
- }
25
- }
26
-
27
- /**
28
- * Dag walker for raw CIDs
29
- */
30
- export const rawWalker: DAGWalker = {
31
- codec: raw.code,
32
- * walk () {
33
- // no embedded CIDs in a raw block
34
- }
35
- }
36
-
37
- // https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
38
- const CID_TAG = 42
39
-
40
- /**
41
- * Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
42
- * all we are interested in is extracting the the CIDs from the block
43
- * so we can just use cborg for that.
44
- */
45
- export const dagCborWalker: DAGWalker = {
46
- codec: dagCbor.code,
47
- * walk (block) {
48
- const cids: CID[] = []
49
- const tags: cborg.TagDecoder[] = []
50
- tags[CID_TAG] = (bytes) => {
51
- if (bytes[0] !== 0) {
52
- throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
53
- }
54
-
55
- const cid = CID.decode(bytes.subarray(1)) // ignore leading 0x00
56
-
57
- cids.push(cid)
58
-
59
- return cid
60
- }
61
-
62
- cborg.decode(block, {
63
- tags
64
- })
65
-
66
- yield * cids
67
- }
68
- }
69
-
70
- /**
71
- * Borrowed from @ipld/dag-json
72
- */
73
- class DagJsonTokenizer extends cborgJson.Tokenizer {
74
- private readonly tokenBuffer: cborg.Token[]
75
-
76
- constructor (data: Uint8Array, options?: cborg.DecodeOptions) {
77
- super(data, options)
78
-
79
- this.tokenBuffer = []
80
- }
81
-
82
- done (): boolean {
83
- return this.tokenBuffer.length === 0 && super.done()
84
- }
85
-
86
- _next (): cborg.Token {
87
- if (this.tokenBuffer.length > 0) {
88
- // @ts-expect-error https://github.com/Microsoft/TypeScript/issues/30406
89
- return this.tokenBuffer.pop()
90
- }
91
- return super.next()
92
- }
93
-
94
- /**
95
- * Implements rules outlined in https://github.com/ipld/specs/pull/356
96
- */
97
- next (): cborg.Token {
98
- const token = this._next()
99
-
100
- if (token.type === Type.map) {
101
- const keyToken = this._next()
102
- if (keyToken.type === Type.string && keyToken.value === '/') {
103
- const valueToken = this._next()
104
- if (valueToken.type === Type.string) { // *must* be a CID
105
- const breakToken = this._next() // swallow the end-of-map token
106
- if (breakToken.type !== Type.break) {
107
- throw new Error('Invalid encoded CID form')
108
- }
109
- this.tokenBuffer.push(valueToken) // CID.parse will pick this up after our tag token
110
- return new Token(Type.tag, 42, 0)
111
- }
112
- if (valueToken.type === Type.map) {
113
- const innerKeyToken = this._next()
114
- if (innerKeyToken.type === Type.string && innerKeyToken.value === 'bytes') {
115
- const innerValueToken = this._next()
116
- if (innerValueToken.type === Type.string) { // *must* be Bytes
117
- for (let i = 0; i < 2; i++) {
118
- const breakToken = this._next() // swallow two end-of-map tokens
119
- if (breakToken.type !== Type.break) {
120
- throw new Error('Invalid encoded Bytes form')
121
- }
122
- }
123
- const bytes = base64.decode(`m${innerValueToken.value}`)
124
- return new Token(Type.bytes, bytes, innerValueToken.value.length)
125
- }
126
- this.tokenBuffer.push(innerValueToken) // bail
127
- }
128
- this.tokenBuffer.push(innerKeyToken) // bail
129
- }
130
- this.tokenBuffer.push(valueToken) // bail
131
- }
132
- this.tokenBuffer.push(keyToken) // bail
133
- }
134
- return token
135
- }
136
- }
137
-
138
- /**
139
- * Dag walker for dag-json CIDs. Does not actually use dag-json since
140
- * all we are interested in is extracting the the CIDs from the block
141
- * so we can just use cborg/json for that.
142
- */
143
- export const dagJsonWalker: DAGWalker = {
144
- codec: dagJson.code,
145
- * walk (block) {
146
- const cids: CID[] = []
147
- const tags: cborg.TagDecoder[] = []
148
- tags[CID_TAG] = (string) => {
149
- const cid = CID.parse(string)
150
-
151
- cids.push(cid)
152
-
153
- return cid
154
- }
155
-
156
- cborgJson.decode(block, {
157
- tags,
158
- tokenizer: new DagJsonTokenizer(block, {
159
- tags,
160
- allowIndefinite: true,
161
- allowUndefined: true,
162
- allowNaN: true,
163
- allowInfinity: true,
164
- allowBigInt: true,
165
- strict: false,
166
- rejectDuplicateMapKeys: false
167
- })
168
- })
169
-
170
- yield * cids
171
- }
172
- }
173
-
174
- /**
175
- * Dag walker for json CIDs. JSON has no facility for linking to
176
- * external blocks so the walker is a no-op.
177
- */
178
- export const jsonWalker: DAGWalker = {
179
- codec: json.code,
180
- * walk () {}
181
- }
@@ -1,23 +0,0 @@
1
- import { type Datastore, Key } from 'interface-datastore'
2
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
3
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
-
5
- const DS_VERSION_KEY = new Key('/version')
6
- const CURRENT_VERSION = 1
7
-
8
- export async function assertDatastoreVersionIsCurrent (datastore: Datastore): Promise<void> {
9
- if (!(await datastore.has(DS_VERSION_KEY))) {
10
- await datastore.put(DS_VERSION_KEY, uint8ArrayFromString(`${CURRENT_VERSION}`))
11
-
12
- return
13
- }
14
-
15
- const buf = await datastore.get(DS_VERSION_KEY)
16
- const str = uint8ArrayToString(buf)
17
- const version = parseInt(str, 10)
18
-
19
- if (version !== CURRENT_VERSION) {
20
- // TODO: write migrations when we break compatibility - for an example, see https://github.com/ipfs/js-ipfs-repo/tree/master/packages/ipfs-repo-migrations
21
- throw new Error('Unknown datastore version, a datastore migration may be required')
22
- }
23
- }
@@ -1,12 +0,0 @@
1
- import { identity } from 'multiformats/hashes/identity'
2
- import { sha256, sha512 } from 'multiformats/hashes/sha2'
3
- import type { MultihashHasher } from 'multiformats/hashes/interface'
4
-
5
- export function defaultHashers (hashers: MultihashHasher[] = []): MultihashHasher[] {
6
- return [
7
- sha256,
8
- sha512,
9
- identity,
10
- ...hashers
11
- ]
12
- }