@helia/interop 3.0.0 → 3.0.1-3477b27

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 (122) hide show
  1. package/README.md +11 -0
  2. package/dist/src/bin.d.ts +3 -0
  3. package/dist/src/bin.d.ts.map +1 -0
  4. package/dist/src/bin.js +14 -0
  5. package/dist/src/bin.js.map +1 -0
  6. package/dist/src/car.spec.d.ts +2 -0
  7. package/dist/src/car.spec.d.ts.map +1 -0
  8. package/dist/src/car.spec.js +77 -0
  9. package/dist/src/car.spec.js.map +1 -0
  10. package/dist/src/dag-cbor.spec.d.ts +2 -0
  11. package/dist/src/dag-cbor.spec.d.ts.map +1 -0
  12. package/dist/src/dag-cbor.spec.js +49 -0
  13. package/dist/src/dag-cbor.spec.js.map +1 -0
  14. package/dist/src/dag-json.spec.d.ts +2 -0
  15. package/dist/src/dag-json.spec.d.ts.map +1 -0
  16. package/dist/src/dag-json.spec.js +49 -0
  17. package/dist/src/dag-json.spec.js.map +1 -0
  18. package/dist/src/fixtures/connect.d.ts +7 -0
  19. package/dist/src/fixtures/connect.d.ts.map +1 -0
  20. package/dist/src/fixtures/connect.js +17 -0
  21. package/dist/src/fixtures/connect.js.map +1 -0
  22. package/dist/src/fixtures/create-helia.browser.d.ts +4 -0
  23. package/dist/src/fixtures/create-helia.browser.d.ts.map +1 -0
  24. package/dist/src/fixtures/create-helia.browser.js +55 -0
  25. package/dist/src/fixtures/create-helia.browser.js.map +1 -0
  26. package/dist/src/fixtures/create-helia.d.ts +4 -0
  27. package/dist/src/fixtures/create-helia.d.ts.map +1 -0
  28. package/dist/src/fixtures/create-helia.js +45 -0
  29. package/dist/src/fixtures/create-helia.js.map +1 -0
  30. package/dist/src/fixtures/create-kubo.browser.d.ts +3 -0
  31. package/dist/src/fixtures/create-kubo.browser.d.ts.map +1 -0
  32. package/dist/src/fixtures/create-kubo.browser.js +21 -0
  33. package/dist/src/fixtures/create-kubo.browser.js.map +1 -0
  34. package/dist/src/fixtures/create-kubo.d.ts +3 -0
  35. package/dist/src/fixtures/create-kubo.d.ts.map +1 -0
  36. package/dist/src/fixtures/create-kubo.js +23 -0
  37. package/dist/src/fixtures/create-kubo.js.map +1 -0
  38. package/dist/src/fixtures/create-peer-ids.d.ts +14 -0
  39. package/dist/src/fixtures/create-peer-ids.d.ts.map +1 -0
  40. package/dist/src/fixtures/create-peer-ids.js +37 -0
  41. package/dist/src/fixtures/create-peer-ids.js.map +1 -0
  42. package/dist/src/fixtures/key-types.d.ts +3 -0
  43. package/dist/src/fixtures/key-types.d.ts.map +1 -0
  44. package/dist/src/fixtures/key-types.js +6 -0
  45. package/dist/src/fixtures/key-types.js.map +1 -0
  46. package/dist/src/fixtures/memory-car.d.ts +7 -0
  47. package/dist/src/fixtures/memory-car.d.ts.map +1 -0
  48. package/dist/src/fixtures/memory-car.js +26 -0
  49. package/dist/src/fixtures/memory-car.js.map +1 -0
  50. package/dist/src/fixtures/wait-for.d.ts +7 -0
  51. package/dist/src/fixtures/wait-for.d.ts.map +1 -0
  52. package/dist/src/fixtures/wait-for.js +17 -0
  53. package/dist/src/fixtures/wait-for.js.map +1 -0
  54. package/dist/src/helia-blockstore.spec.d.ts +2 -0
  55. package/dist/src/helia-blockstore.spec.d.ts.map +1 -0
  56. package/dist/src/helia-blockstore.spec.js +48 -0
  57. package/dist/src/helia-blockstore.spec.js.map +1 -0
  58. package/dist/src/helia-hashes.spec.d.ts +2 -0
  59. package/dist/src/helia-hashes.spec.d.ts.map +1 -0
  60. package/dist/src/helia-hashes.spec.js +50 -0
  61. package/dist/src/helia-hashes.spec.js.map +1 -0
  62. package/dist/src/helia-pins.spec.d.ts +2 -0
  63. package/dist/src/helia-pins.spec.d.ts.map +1 -0
  64. package/dist/src/helia-pins.spec.js +48 -0
  65. package/dist/src/helia-pins.spec.js.map +1 -0
  66. package/dist/src/index.d.ts +12 -0
  67. package/dist/src/index.d.ts.map +1 -1
  68. package/dist/src/index.js +12 -0
  69. package/dist/src/index.js.map +1 -1
  70. package/dist/src/ipns-libp2p.spec.d.ts +2 -0
  71. package/dist/src/ipns-libp2p.spec.d.ts.map +1 -0
  72. package/dist/src/ipns-libp2p.spec.js +151 -0
  73. package/dist/src/ipns-libp2p.spec.js.map +1 -0
  74. package/dist/src/ipns-pubsub.spec.d.ts +2 -0
  75. package/dist/src/ipns-pubsub.spec.d.ts.map +1 -0
  76. package/dist/src/ipns-pubsub.spec.js +146 -0
  77. package/dist/src/ipns-pubsub.spec.js.map +1 -0
  78. package/dist/src/json.spec.d.ts +2 -0
  79. package/dist/src/json.spec.d.ts.map +1 -0
  80. package/dist/src/json.spec.js +49 -0
  81. package/dist/src/json.spec.js.map +1 -0
  82. package/dist/src/mfs.spec.d.ts +2 -0
  83. package/dist/src/mfs.spec.d.ts.map +1 -0
  84. package/dist/src/mfs.spec.js +85 -0
  85. package/dist/src/mfs.spec.js.map +1 -0
  86. package/dist/src/strings.spec.d.ts +2 -0
  87. package/dist/src/strings.spec.d.ts.map +1 -0
  88. package/dist/src/strings.spec.js +51 -0
  89. package/dist/src/strings.spec.js.map +1 -0
  90. package/dist/src/unixfs-bitswap.spec.d.ts +2 -0
  91. package/dist/src/unixfs-bitswap.spec.d.ts.map +1 -0
  92. package/dist/src/unixfs-bitswap.spec.js +65 -0
  93. package/dist/src/unixfs-bitswap.spec.js.map +1 -0
  94. package/dist/src/unixfs-files.spec.d.ts +2 -0
  95. package/dist/src/unixfs-files.spec.d.ts.map +1 -0
  96. package/dist/src/unixfs-files.spec.js +69 -0
  97. package/dist/src/unixfs-files.spec.js.map +1 -0
  98. package/package.json +19 -17
  99. package/src/bin.ts +18 -0
  100. package/src/car.spec.ts +102 -0
  101. package/src/dag-cbor.spec.ts +65 -0
  102. package/src/dag-json.spec.ts +65 -0
  103. package/src/fixtures/connect.ts +19 -0
  104. package/src/fixtures/create-helia.browser.ts +64 -0
  105. package/src/fixtures/create-helia.ts +51 -0
  106. package/src/fixtures/create-kubo.browser.ts +21 -0
  107. package/src/fixtures/create-kubo.ts +23 -0
  108. package/src/fixtures/create-peer-ids.ts +46 -0
  109. package/src/fixtures/key-types.ts +7 -0
  110. package/src/fixtures/memory-car.ts +33 -0
  111. package/src/fixtures/wait-for.ts +26 -0
  112. package/src/helia-blockstore.spec.ts +59 -0
  113. package/src/helia-hashes.spec.ts +61 -0
  114. package/src/helia-pins.spec.ts +64 -0
  115. package/src/index.ts +13 -0
  116. package/src/ipns-libp2p.spec.ts +188 -0
  117. package/src/ipns-pubsub.spec.ts +180 -0
  118. package/src/json.spec.ts +65 -0
  119. package/src/mfs.spec.ts +105 -0
  120. package/src/strings.spec.ts +67 -0
  121. package/src/unixfs-bitswap.spec.ts +86 -0
  122. package/src/unixfs-files.spec.ts +89 -0
@@ -0,0 +1,188 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { ipns } from '@helia/ipns'
4
+ import { libp2p } from '@helia/ipns/routing'
5
+ import { peerIdFromString } from '@libp2p/peer-id'
6
+ import { createEd25519PeerId, createRSAPeerId, createSecp256k1PeerId } from '@libp2p/peer-id-factory'
7
+ import { expect } from 'aegir/chai'
8
+ import last from 'it-last'
9
+ import { CID } from 'multiformats/cid'
10
+ import * as raw from 'multiformats/codecs/raw'
11
+ import { sha256 } from 'multiformats/hashes/sha2'
12
+ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
13
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
14
+ import { isElectronMain } from 'wherearewe'
15
+ import { connect } from './fixtures/connect.js'
16
+ import { createHeliaNode } from './fixtures/create-helia.js'
17
+ import { createKuboNode } from './fixtures/create-kubo.js'
18
+ import { sortClosestPeers } from './fixtures/create-peer-ids.js'
19
+ import { keyTypes } from './fixtures/key-types.js'
20
+ import { waitFor } from './fixtures/wait-for.js'
21
+ import type { IPNS } from '@helia/ipns'
22
+ import type { PeerId } from '@libp2p/interface'
23
+ import type { HeliaLibp2p } from 'helia'
24
+ import type { Controller } from 'ipfsd-ctl'
25
+
26
+ keyTypes.forEach(type => {
27
+ describe(`@helia/ipns - libp2p routing with ${type} keys`, () => {
28
+ let helia: HeliaLibp2p
29
+ let kubo: Controller
30
+ let name: IPNS
31
+
32
+ // the CID we are going to publish
33
+ let value: CID
34
+
35
+ // the public key we will use to publish the value
36
+ let key: PeerId
37
+
38
+ /**
39
+ * Ensure that for the CID we are going to publish, the resolver has a peer ID that
40
+ * is KAD-closer to the routing key so we can predict the the resolver will receive
41
+ * the DHT record containing the IPNS record
42
+ */
43
+ async function createNodes (resolver: 'kubo' | 'helia'): Promise<void> {
44
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
45
+ const digest = await sha256.digest(input)
46
+ value = CID.createV1(raw.code, digest)
47
+
48
+ helia = await createHeliaNode()
49
+ kubo = await createKuboNode()
50
+
51
+ // find a PeerId that is KAD-closer to the resolver than the publisher when used as an IPNS key
52
+ while (true) {
53
+ if (type === 'Ed25519') {
54
+ key = await createEd25519PeerId()
55
+ } else if (type === 'secp256k1') {
56
+ key = await createSecp256k1PeerId()
57
+ } else {
58
+ key = await createRSAPeerId()
59
+ }
60
+
61
+ const routingKey = uint8ArrayConcat([
62
+ uint8ArrayFromString('/ipns/'),
63
+ key.toBytes()
64
+ ])
65
+
66
+ const [closest] = await sortClosestPeers(routingKey, [
67
+ helia.libp2p.peerId,
68
+ peerIdFromString(kubo.peer.id.toString())
69
+ ])
70
+
71
+ if (resolver === 'kubo' && closest.equals(peerIdFromString(kubo.peer.id.toString()))) {
72
+ break
73
+ }
74
+
75
+ if (resolver === 'helia' && closest.equals(helia.libp2p.peerId)) {
76
+ break
77
+ }
78
+ }
79
+
80
+ // connect the two nodes over the KAD-DHT protocol, this should ensure
81
+ // both nodes have each other in their KAD buckets
82
+ await connect(helia, kubo, '/ipfs/lan/kad/1.0.0')
83
+
84
+ await waitFor(async () => {
85
+ let found = false
86
+
87
+ for await (const event of helia.libp2p.services.dht.findPeer(peerIdFromString(kubo.peer.id.toString()))) {
88
+ if (event.name === 'FINAL_PEER') {
89
+ found = true
90
+ }
91
+ }
92
+
93
+ return found
94
+ }, {
95
+ timeout: 30000,
96
+ delay: 1000,
97
+ message: 'Helia could not find Kubo on the DHT'
98
+ })
99
+
100
+ await waitFor(async () => {
101
+ let found = false
102
+
103
+ // @ts-expect-error kubo deps are out of date
104
+ for await (const event of kubo.api.dht.findPeer(helia.libp2p.peerId)) {
105
+ if (event.name === 'FINAL_PEER') {
106
+ found = true
107
+ }
108
+ }
109
+
110
+ return found
111
+ }, {
112
+ timeout: 30000,
113
+ delay: 1000,
114
+ message: 'Kubo could not find Helia on the DHT'
115
+ })
116
+
117
+ name = ipns(helia, {
118
+ routers: [
119
+ libp2p(helia)
120
+ ]
121
+ })
122
+ }
123
+
124
+ afterEach(async () => {
125
+ if (helia != null) {
126
+ await helia.stop()
127
+ }
128
+
129
+ if (kubo != null) {
130
+ await kubo.stop()
131
+ }
132
+ })
133
+
134
+ it(`should publish on helia and resolve on kubo using a ${type} key`, async () => {
135
+ await createNodes('kubo')
136
+
137
+ const keyName = 'my-ipns-key'
138
+ await helia.libp2p.services.keychain.importPeer(keyName, key)
139
+
140
+ await name.publish(key, value)
141
+
142
+ const resolved = await last(kubo.api.name.resolve(key.toString()))
143
+
144
+ if (resolved == null) {
145
+ throw new Error('kubo failed to resolve name')
146
+ }
147
+
148
+ expect(resolved).to.equal(`/ipfs/${value.toString()}`)
149
+ })
150
+
151
+ it('should publish on kubo and resolve on helia', async function () {
152
+ if (isElectronMain) {
153
+ // electron main does not have fetch, FormData or Blob APIs
154
+ // can revisit when kubo-rpc-client supports the key.import API
155
+ return this.skip()
156
+ }
157
+
158
+ if (type === 'secp256k1') {
159
+ // Kubo cannot import secp256k1 keys
160
+ return this.skip()
161
+ }
162
+
163
+ await createNodes('helia')
164
+
165
+ const keyName = 'my-ipns-key'
166
+ const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))
167
+
168
+ // ensure the key is in the kubo keychain so we can use it to publish the IPNS record
169
+ const body = new FormData()
170
+ body.append('key', new Blob([key.privateKey ?? new Uint8Array(0)]))
171
+
172
+ // can't use the kubo-rpc-api for this call yet
173
+ const response = await fetch(`http://${kubo.api.apiHost}:${kubo.api.apiPort}/api/v0/key/import?arg=${keyName}`, {
174
+ method: 'POST',
175
+ body
176
+ })
177
+
178
+ expect(response).to.have.property('status', 200)
179
+
180
+ await kubo.api.name.publish(cid, {
181
+ key: keyName
182
+ })
183
+
184
+ const resolvedCid = await name.resolve(key)
185
+ expect(resolvedCid.toString()).to.equal(cid.toString())
186
+ })
187
+ })
188
+ })
@@ -0,0 +1,180 @@
1
+ /* eslint-env mocha */
2
+ /* eslint max-nested-callbacks: ["error", 5] */
3
+
4
+ import { ipns } from '@helia/ipns'
5
+ import { pubsub } from '@helia/ipns/routing'
6
+ import { peerIdFromKeys } from '@libp2p/peer-id'
7
+ import { expect } from 'aegir/chai'
8
+ import last from 'it-last'
9
+ import { base36 } from 'multiformats/bases/base36'
10
+ import { CID } from 'multiformats/cid'
11
+ import * as raw from 'multiformats/codecs/raw'
12
+ import { identity } from 'multiformats/hashes/identity'
13
+ import { sha256 } from 'multiformats/hashes/sha2'
14
+ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
15
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
16
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
17
+ import { connect } from './fixtures/connect.js'
18
+ import { createHeliaNode } from './fixtures/create-helia.js'
19
+ import { createKuboNode } from './fixtures/create-kubo.js'
20
+ import { keyTypes } from './fixtures/key-types.js'
21
+ import { waitFor } from './fixtures/wait-for.js'
22
+ import type { IPNS } from '@helia/ipns'
23
+ import type { HeliaLibp2p } from 'helia'
24
+ import type { Controller } from 'ipfsd-ctl'
25
+
26
+ const LIBP2P_KEY_CODEC = 0x72
27
+
28
+ // skip RSA tests because we need the DHT enabled to find the public key
29
+ // component of the keypair, but that means we can't test pubsub
30
+ // resolution because Kubo will use the DHT as well
31
+ keyTypes.filter(keyType => keyType !== 'RSA').forEach(keyType => {
32
+ describe(`@helia/ipns - pubsub routing with ${keyType} keys`, () => {
33
+ let helia: HeliaLibp2p
34
+ let kubo: Controller
35
+ let name: IPNS
36
+
37
+ beforeEach(async () => {
38
+ helia = await createHeliaNode()
39
+ kubo = await createKuboNode()
40
+
41
+ // connect the two nodes
42
+ await connect(helia, kubo, '/meshsub/1.1.0')
43
+
44
+ name = ipns(helia, {
45
+ routers: [
46
+ pubsub(helia)
47
+ ]
48
+ })
49
+ })
50
+
51
+ afterEach(async () => {
52
+ if (helia != null) {
53
+ await helia.stop()
54
+ }
55
+
56
+ if (kubo != null) {
57
+ await kubo.stop()
58
+ }
59
+ })
60
+
61
+ it('should publish on helia and resolve on kubo', async () => {
62
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
63
+ const digest = await sha256.digest(input)
64
+ const cid = CID.createV1(raw.code, digest)
65
+
66
+ const keyName = 'my-ipns-key'
67
+ await helia.libp2p.services.keychain.createKey(keyName, keyType)
68
+ const peerId = await helia.libp2p.services.keychain.exportPeerId(keyName)
69
+
70
+ if (peerId.publicKey == null) {
71
+ throw new Error('No public key present')
72
+ }
73
+
74
+ // first publish should fail because kubo isn't subscribed to key update channel
75
+ await expect(name.publish(peerId, cid)).to.eventually.be.rejected()
76
+ .with.property('message', 'PublishError.InsufficientPeers')
77
+
78
+ // should fail to resolve the first time as kubo was not subscribed to the pubsub channel
79
+ // @ts-expect-error kubo deps are out of date
80
+ await expect(last(kubo.api.name.resolve(peerId, {
81
+ timeout: 100
82
+ }))).to.eventually.be.undefined()
83
+
84
+ // magic pubsub subscription name
85
+ const subscriptionName = `/ipns/${CID.createV1(LIBP2P_KEY_CODEC, identity.digest(peerId.publicKey)).toString(base36)}`
86
+
87
+ // wait for kubo to be subscribed to updates
88
+ await waitFor(async () => {
89
+ const subs = await kubo.api.name.pubsub.subs()
90
+
91
+ return subs.includes(subscriptionName)
92
+ }, {
93
+ timeout: 30000
94
+ })
95
+
96
+ // publish should now succeed
97
+ await name.publish(peerId, cid)
98
+
99
+ // kubo should now be able to resolve IPNS name
100
+ // @ts-expect-error kubo deps are out of date
101
+ const resolved = await last(kubo.api.name.resolve(peerId, {
102
+ timeout: 100
103
+ }))
104
+
105
+ expect(resolved).to.equal(`/ipfs/${cid.toString()}`)
106
+ })
107
+
108
+ it('should publish on kubo and resolve on helia', async function () {
109
+ if (keyType === 'secp256k1') {
110
+ // Kubo cannot generate secp256k1 keys
111
+ return this.skip()
112
+ }
113
+
114
+ const keyName = 'my-ipns-key'
115
+ const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))
116
+ const result = await kubo.api.key.gen(keyName, {
117
+ // @ts-expect-error kubo needs this in lower case
118
+ type: keyType.toLowerCase()
119
+ })
120
+
121
+ // the generated id is libp2p-key CID with the public key as an identity multihash
122
+ const peerCid = CID.parse(result.id, base36)
123
+ const peerId = await peerIdFromKeys(peerCid.multihash.digest)
124
+
125
+ // first call to pubsub resolver should fail but we should now be subscribed for updates
126
+ await expect(name.resolve(peerId)).to.eventually.be.rejected()
127
+
128
+ // actual pubsub subscription name
129
+ const subscriptionName = `/record/${uint8ArrayToString(uint8ArrayConcat([
130
+ uint8ArrayFromString('/ipns/'),
131
+ peerId.toBytes()
132
+ ]), 'base64url')}`
133
+
134
+ // wait for helia to be subscribed to the topic for record updates
135
+ await waitFor(async () => {
136
+ return helia.libp2p.services.pubsub.getTopics().includes(subscriptionName)
137
+ }, {
138
+ timeout: 30000,
139
+ message: 'Helia did not register for record updates'
140
+ })
141
+
142
+ // wait for kubo to see that helia is subscribed to the topic for record updates
143
+ await waitFor(async () => {
144
+ const peers = await kubo.api.pubsub.peers(subscriptionName)
145
+
146
+ return peers.map(p => p.toString()).includes(helia.libp2p.peerId.toString())
147
+ }, {
148
+ timeout: 30000,
149
+ message: 'Kubo did not see that Helia was registered for record updates'
150
+ })
151
+
152
+ // now publish, this should cause a pubsub message on the topic for record updates
153
+ await kubo.api.name.publish(cid, {
154
+ key: keyName
155
+ })
156
+
157
+ let resolvedCid: CID | undefined
158
+
159
+ // we should get an update eventually
160
+ await waitFor(async () => {
161
+ try {
162
+ resolvedCid = await name.resolve(peerId)
163
+
164
+ return true
165
+ } catch {
166
+ return false
167
+ }
168
+ }, {
169
+ timeout: 10000,
170
+ message: 'Helia could not resolve the IPNS record'
171
+ })
172
+
173
+ if (resolvedCid == null) {
174
+ throw new Error('Failed to resolve CID')
175
+ }
176
+
177
+ expect(resolvedCid.toString()).to.equal(cid.toString())
178
+ })
179
+ })
180
+ })
@@ -0,0 +1,65 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { json, type JSON, type AddOptions } from '@helia/json'
4
+ import { expect } from 'aegir/chai'
5
+ import { CID } from 'multiformats/cid'
6
+ import * as jsonCodec from 'multiformats/codecs/json'
7
+ import { createHeliaNode } from './fixtures/create-helia.js'
8
+ import { createKuboNode } from './fixtures/create-kubo.js'
9
+ import type { HeliaLibp2p } from 'helia'
10
+ import type { PutOptions as KuboAddOptions } from 'ipfs-core-types/src/block/index.js'
11
+ import type { Controller } from 'ipfsd-ctl'
12
+
13
+ describe('@helia/json', () => {
14
+ let helia: HeliaLibp2p
15
+ let j: JSON
16
+ let kubo: Controller
17
+
18
+ async function expectSameCid (data: () => any, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = { format: 'json' }): Promise<void> {
19
+ const heliaCid = await j.add(data(), heliaOpts)
20
+ const kuboCid = await kubo.api.block.put(jsonCodec.encode(data()), kuboOpts)
21
+
22
+ expect(heliaCid.toString()).to.equal(kuboCid.toString())
23
+ }
24
+
25
+ beforeEach(async () => {
26
+ helia = await createHeliaNode()
27
+ j = json(helia)
28
+ kubo = await createKuboNode()
29
+
30
+ await helia.libp2p.dial((await (kubo.api.id())).addresses)
31
+ })
32
+
33
+ afterEach(async () => {
34
+ if (helia != null) {
35
+ await helia.stop()
36
+ }
37
+
38
+ if (kubo != null) {
39
+ await kubo.stop()
40
+ }
41
+ })
42
+
43
+ it('should create the same CID for a string', async () => {
44
+ const candidate = (): any => ({ hello: 'world' })
45
+
46
+ await expectSameCid(candidate)
47
+ })
48
+
49
+ it('should add to helia and fetch from kubo', async () => {
50
+ const input = { hello: 'world' }
51
+ const cid = await j.add(input)
52
+ const block = await kubo.api.block.get(cid)
53
+ const output = jsonCodec.decode(block)
54
+
55
+ expect(output).to.deep.equal(input)
56
+ })
57
+
58
+ it('should add to kubo and fetch from helia', async () => {
59
+ const input = { hello: 'world' }
60
+ const cid = await kubo.api.block.put(jsonCodec.encode(input))
61
+ const output = await j.get(CID.parse(cid.toString()))
62
+
63
+ expect(output).to.deep.equal(input)
64
+ })
65
+ })
@@ -0,0 +1,105 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { type MFS, mfs } from '@helia/mfs'
4
+ import { expect } from 'aegir/chai'
5
+ import { createHeliaNode } from './fixtures/create-helia.js'
6
+ import { createKuboNode } from './fixtures/create-kubo.js'
7
+ import type { HeliaLibp2p } from 'helia'
8
+ import type { Controller } from 'ipfsd-ctl'
9
+
10
+ describe('@helia/mfs', () => {
11
+ let helia: HeliaLibp2p
12
+ let fs: MFS
13
+ let kubo: Controller
14
+
15
+ beforeEach(async () => {
16
+ helia = await createHeliaNode()
17
+ fs = mfs(helia)
18
+ kubo = await createKuboNode()
19
+ })
20
+
21
+ afterEach(async () => {
22
+ if (helia != null) {
23
+ await helia.stop()
24
+ }
25
+
26
+ if (kubo != null) {
27
+ await kubo.stop()
28
+ }
29
+ })
30
+
31
+ it('should have the same CID initially', async () => {
32
+ const heliaStat = await fs.stat('/')
33
+ const kuboStat = await kubo.api.files.stat('/')
34
+
35
+ expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
36
+ })
37
+
38
+ it('should have the same CID after creating a directory', async () => {
39
+ const dirPath = '/foo'
40
+ await fs.mkdir(dirPath)
41
+ await kubo.api.files.mkdir(dirPath, {
42
+ cidVersion: 1
43
+ })
44
+
45
+ const heliaStat = await fs.stat('/')
46
+ const kuboStat = await kubo.api.files.stat('/')
47
+
48
+ expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
49
+ })
50
+
51
+ it('should have the same CID after removing a directory', async () => {
52
+ const dirPath = '/foo'
53
+ await fs.mkdir(dirPath)
54
+ await fs.rm(dirPath)
55
+ await kubo.api.files.mkdir(dirPath, {
56
+ cidVersion: 1
57
+ })
58
+ await kubo.api.files.rm(dirPath, {
59
+ recursive: true
60
+ })
61
+
62
+ const heliaStat = await fs.stat('/')
63
+ const kuboStat = await kubo.api.files.stat('/')
64
+
65
+ expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
66
+ })
67
+
68
+ it('should have the same CID after creating a file', async () => {
69
+ const filePath = '/foo.txt'
70
+ const fileData = Uint8Array.from([0, 1, 2, 3, 4])
71
+ await fs.writeBytes(fileData, filePath, {
72
+ rawLeaves: true,
73
+ reduceSingleLeafToSelf: false
74
+ })
75
+ await kubo.api.files.write(filePath, fileData, {
76
+ cidVersion: 1,
77
+ create: true
78
+ })
79
+
80
+ const heliaStat = await fs.stat('/')
81
+ const kuboStat = await kubo.api.files.stat('/')
82
+
83
+ expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
84
+ })
85
+
86
+ it('should have the same CID after removing a file', async () => {
87
+ const filePath = '/foo.txt'
88
+ const fileData = Uint8Array.from([0, 1, 2, 3, 4])
89
+ await fs.writeBytes(fileData, filePath, {
90
+ rawLeaves: true,
91
+ reduceSingleLeafToSelf: false
92
+ })
93
+ await fs.rm(filePath)
94
+ await kubo.api.files.write(filePath, fileData, {
95
+ cidVersion: 1,
96
+ create: true
97
+ })
98
+ await kubo.api.files.rm(filePath)
99
+
100
+ const heliaStat = await fs.stat('/')
101
+ const kuboStat = await kubo.api.files.stat('/')
102
+
103
+ expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
104
+ })
105
+ })
@@ -0,0 +1,67 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { strings, type Strings, type AddOptions } from '@helia/strings'
4
+ import { expect } from 'aegir/chai'
5
+ import { CID } from 'multiformats/cid'
6
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
+ import { createHeliaNode } from './fixtures/create-helia.js'
9
+ import { createKuboNode } from './fixtures/create-kubo.js'
10
+ import type { HeliaLibp2p } from 'helia'
11
+ import type { PutOptions as KuboAddOptions } from 'ipfs-core-types/src/block/index.js'
12
+ import type { Controller } from 'ipfsd-ctl'
13
+
14
+ describe('@helia/strings', () => {
15
+ let helia: HeliaLibp2p
16
+ let str: Strings
17
+ let kubo: Controller
18
+
19
+ async function expectSameCid (data: () => string, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = {}): Promise<void> {
20
+ const heliaCid = await str.add(data(), heliaOpts)
21
+ const kuboCid = await kubo.api.block.put(uint8ArrayFromString(data()), kuboOpts)
22
+
23
+ expect(heliaCid.toString()).to.equal(kuboCid.toString())
24
+ }
25
+
26
+ beforeEach(async () => {
27
+ helia = await createHeliaNode()
28
+ str = strings(helia)
29
+ kubo = await createKuboNode()
30
+
31
+ const id = await kubo.api.id()
32
+ await helia.libp2p.dial(id.addresses)
33
+ })
34
+
35
+ afterEach(async () => {
36
+ if (helia != null) {
37
+ await helia.stop()
38
+ }
39
+
40
+ if (kubo != null) {
41
+ await kubo.stop()
42
+ }
43
+ })
44
+
45
+ it('should create the same CID for a string', async () => {
46
+ const candidate = (): string => 'hello world'
47
+
48
+ await expectSameCid(candidate)
49
+ })
50
+
51
+ it('should add to helia and fetch from kubo', async () => {
52
+ const input = 'hello world'
53
+ const cid = await str.add(input)
54
+ const block = await kubo.api.block.get(cid)
55
+ const output = uint8ArrayToString(block)
56
+
57
+ expect(output).to.equal(input)
58
+ })
59
+
60
+ it('should add to kubo and fetch from helia', async () => {
61
+ const input = 'hello world'
62
+ const cid = await kubo.api.block.put(uint8ArrayFromString(input))
63
+ const output = await str.get(CID.parse(cid.toString()))
64
+
65
+ expect(output).to.equal(input)
66
+ })
67
+ })
@@ -0,0 +1,86 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { type UnixFS, unixfs } from '@helia/unixfs'
4
+ import { expect } from 'aegir/chai'
5
+ import toBuffer from 'it-to-buffer'
6
+ import { CID } from 'multiformats/cid'
7
+ import { createHeliaNode } from './fixtures/create-helia.js'
8
+ import { createKuboNode } from './fixtures/create-kubo.js'
9
+ import type { HeliaLibp2p } from 'helia'
10
+ import type { FileCandidate } from 'ipfs-unixfs-importer'
11
+ import type { Controller } from 'ipfsd-ctl'
12
+
13
+ describe('@helia/unixfs - bitswap', () => {
14
+ let helia: HeliaLibp2p
15
+ let unixFs: UnixFS
16
+ let kubo: Controller
17
+
18
+ beforeEach(async () => {
19
+ helia = await createHeliaNode()
20
+ unixFs = unixfs(helia)
21
+ kubo = await createKuboNode()
22
+
23
+ // connect helia to kubo
24
+ await helia.libp2p.dial(kubo.peer.addresses)
25
+ })
26
+
27
+ afterEach(async () => {
28
+ if (helia != null) {
29
+ await helia.stop()
30
+ }
31
+
32
+ if (kubo != null) {
33
+ await kubo.stop()
34
+ }
35
+ })
36
+
37
+ it('should add a large file to helia and fetch it from kubo', async () => {
38
+ const chunkSize = 1024 * 1024
39
+ const size = chunkSize * 10
40
+ const input: Uint8Array[] = []
41
+
42
+ const candidate: FileCandidate = {
43
+ content: (async function * () {
44
+ for (let i = 0; i < size; i += chunkSize) {
45
+ const buf = new Uint8Array(chunkSize)
46
+ input.push(buf)
47
+
48
+ yield buf
49
+ }
50
+ }())
51
+ }
52
+
53
+ const cid = await unixFs.addFile(candidate)
54
+
55
+ const output: Uint8Array[] = []
56
+
57
+ for await (const b of kubo.api.cat(cid)) {
58
+ output.push(b)
59
+ }
60
+
61
+ expect(toBuffer(output)).to.equalBytes(toBuffer(input))
62
+ })
63
+
64
+ it('should add a large file to kubo and fetch it from helia', async () => {
65
+ const chunkSize = 1024 * 1024
66
+ const size = chunkSize * 10
67
+ const input: Uint8Array[] = []
68
+
69
+ const candidate: FileCandidate = {
70
+ content: (async function * () {
71
+ for (let i = 0; i < size; i += chunkSize) {
72
+ const buf = new Uint8Array(chunkSize)
73
+ input.push(buf)
74
+
75
+ yield buf
76
+ }
77
+ }())
78
+ }
79
+
80
+ const { cid } = await kubo.api.add(candidate.content)
81
+
82
+ const bytes = await toBuffer(unixFs.cat(CID.parse(cid.toString())))
83
+
84
+ expect(bytes).to.equalBytes(toBuffer(input))
85
+ })
86
+ })