@helia/interop 3.0.0 → 3.0.1-19bf9ce

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 (123) hide show
  1. package/.aegir.js +46 -0
  2. package/README.md +11 -0
  3. package/dist/src/bin.d.ts +3 -0
  4. package/dist/src/bin.d.ts.map +1 -0
  5. package/dist/src/bin.js +20 -0
  6. package/dist/src/bin.js.map +1 -0
  7. package/dist/src/car.spec.d.ts +2 -0
  8. package/dist/src/car.spec.d.ts.map +1 -0
  9. package/dist/src/car.spec.js +77 -0
  10. package/dist/src/car.spec.js.map +1 -0
  11. package/dist/src/dag-cbor.spec.d.ts +2 -0
  12. package/dist/src/dag-cbor.spec.d.ts.map +1 -0
  13. package/dist/src/dag-cbor.spec.js +49 -0
  14. package/dist/src/dag-cbor.spec.js.map +1 -0
  15. package/dist/src/dag-json.spec.d.ts +2 -0
  16. package/dist/src/dag-json.spec.d.ts.map +1 -0
  17. package/dist/src/dag-json.spec.js +49 -0
  18. package/dist/src/dag-json.spec.js.map +1 -0
  19. package/dist/src/fixtures/connect.d.ts +7 -0
  20. package/dist/src/fixtures/connect.d.ts.map +1 -0
  21. package/dist/src/fixtures/connect.js +17 -0
  22. package/dist/src/fixtures/connect.js.map +1 -0
  23. package/dist/src/fixtures/create-helia.browser.d.ts +4 -0
  24. package/dist/src/fixtures/create-helia.browser.d.ts.map +1 -0
  25. package/dist/src/fixtures/create-helia.browser.js +55 -0
  26. package/dist/src/fixtures/create-helia.browser.js.map +1 -0
  27. package/dist/src/fixtures/create-helia.d.ts +4 -0
  28. package/dist/src/fixtures/create-helia.d.ts.map +1 -0
  29. package/dist/src/fixtures/create-helia.js +45 -0
  30. package/dist/src/fixtures/create-helia.js.map +1 -0
  31. package/dist/src/fixtures/create-kubo.browser.d.ts +3 -0
  32. package/dist/src/fixtures/create-kubo.browser.d.ts.map +1 -0
  33. package/dist/src/fixtures/create-kubo.browser.js +21 -0
  34. package/dist/src/fixtures/create-kubo.browser.js.map +1 -0
  35. package/dist/src/fixtures/create-kubo.d.ts +3 -0
  36. package/dist/src/fixtures/create-kubo.d.ts.map +1 -0
  37. package/dist/src/fixtures/create-kubo.js +23 -0
  38. package/dist/src/fixtures/create-kubo.js.map +1 -0
  39. package/dist/src/fixtures/create-peer-ids.d.ts +14 -0
  40. package/dist/src/fixtures/create-peer-ids.d.ts.map +1 -0
  41. package/dist/src/fixtures/create-peer-ids.js +37 -0
  42. package/dist/src/fixtures/create-peer-ids.js.map +1 -0
  43. package/dist/src/fixtures/key-types.d.ts +3 -0
  44. package/dist/src/fixtures/key-types.d.ts.map +1 -0
  45. package/dist/src/fixtures/key-types.js +6 -0
  46. package/dist/src/fixtures/key-types.js.map +1 -0
  47. package/dist/src/fixtures/memory-car.d.ts +7 -0
  48. package/dist/src/fixtures/memory-car.d.ts.map +1 -0
  49. package/dist/src/fixtures/memory-car.js +26 -0
  50. package/dist/src/fixtures/memory-car.js.map +1 -0
  51. package/dist/src/fixtures/wait-for.d.ts +7 -0
  52. package/dist/src/fixtures/wait-for.d.ts.map +1 -0
  53. package/dist/src/fixtures/wait-for.js +17 -0
  54. package/dist/src/fixtures/wait-for.js.map +1 -0
  55. package/dist/src/helia-blockstore.spec.d.ts +2 -0
  56. package/dist/src/helia-blockstore.spec.d.ts.map +1 -0
  57. package/dist/src/helia-blockstore.spec.js +48 -0
  58. package/dist/src/helia-blockstore.spec.js.map +1 -0
  59. package/dist/src/helia-hashes.spec.d.ts +2 -0
  60. package/dist/src/helia-hashes.spec.d.ts.map +1 -0
  61. package/dist/src/helia-hashes.spec.js +50 -0
  62. package/dist/src/helia-hashes.spec.js.map +1 -0
  63. package/dist/src/helia-pins.spec.d.ts +2 -0
  64. package/dist/src/helia-pins.spec.d.ts.map +1 -0
  65. package/dist/src/helia-pins.spec.js +48 -0
  66. package/dist/src/helia-pins.spec.js.map +1 -0
  67. package/dist/src/index.d.ts +12 -0
  68. package/dist/src/index.d.ts.map +1 -1
  69. package/dist/src/index.js +12 -0
  70. package/dist/src/index.js.map +1 -1
  71. package/dist/src/ipns-pubsub.spec.d.ts +2 -0
  72. package/dist/src/ipns-pubsub.spec.d.ts.map +1 -0
  73. package/dist/src/ipns-pubsub.spec.js +146 -0
  74. package/dist/src/ipns-pubsub.spec.js.map +1 -0
  75. package/dist/src/ipns.spec.d.ts +2 -0
  76. package/dist/src/ipns.spec.d.ts.map +1 -0
  77. package/dist/src/ipns.spec.js +146 -0
  78. package/dist/src/ipns.spec.js.map +1 -0
  79. package/dist/src/json.spec.d.ts +2 -0
  80. package/dist/src/json.spec.d.ts.map +1 -0
  81. package/dist/src/json.spec.js +49 -0
  82. package/dist/src/json.spec.js.map +1 -0
  83. package/dist/src/mfs.spec.d.ts +2 -0
  84. package/dist/src/mfs.spec.d.ts.map +1 -0
  85. package/dist/src/mfs.spec.js +85 -0
  86. package/dist/src/mfs.spec.js.map +1 -0
  87. package/dist/src/strings.spec.d.ts +2 -0
  88. package/dist/src/strings.spec.d.ts.map +1 -0
  89. package/dist/src/strings.spec.js +51 -0
  90. package/dist/src/strings.spec.js.map +1 -0
  91. package/dist/src/unixfs-bitswap.spec.d.ts +2 -0
  92. package/dist/src/unixfs-bitswap.spec.d.ts.map +1 -0
  93. package/dist/src/unixfs-bitswap.spec.js +65 -0
  94. package/dist/src/unixfs-bitswap.spec.js.map +1 -0
  95. package/dist/src/unixfs-files.spec.d.ts +2 -0
  96. package/dist/src/unixfs-files.spec.d.ts.map +1 -0
  97. package/dist/src/unixfs-files.spec.js +69 -0
  98. package/dist/src/unixfs-files.spec.js.map +1 -0
  99. package/package.json +22 -19
  100. package/src/bin.ts +25 -0
  101. package/src/car.spec.ts +102 -0
  102. package/src/dag-cbor.spec.ts +65 -0
  103. package/src/dag-json.spec.ts +65 -0
  104. package/src/fixtures/connect.ts +19 -0
  105. package/src/fixtures/create-helia.browser.ts +64 -0
  106. package/src/fixtures/create-helia.ts +51 -0
  107. package/src/fixtures/create-kubo.browser.ts +21 -0
  108. package/src/fixtures/create-kubo.ts +23 -0
  109. package/src/fixtures/create-peer-ids.ts +46 -0
  110. package/src/fixtures/key-types.ts +7 -0
  111. package/src/fixtures/memory-car.ts +33 -0
  112. package/src/fixtures/wait-for.ts +26 -0
  113. package/src/helia-blockstore.spec.ts +59 -0
  114. package/src/helia-hashes.spec.ts +61 -0
  115. package/src/helia-pins.spec.ts +64 -0
  116. package/src/index.ts +13 -0
  117. package/src/ipns-pubsub.spec.ts +180 -0
  118. package/src/ipns.spec.ts +183 -0
  119. package/src/json.spec.ts +65 -0
  120. package/src/mfs.spec.ts +105 -0
  121. package/src/strings.spec.ts +67 -0
  122. package/src/unixfs-bitswap.spec.ts +86 -0
  123. package/src/unixfs-files.spec.ts +89 -0
@@ -0,0 +1,64 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { expect } from 'aegir/chai'
4
+ import all from 'it-all'
5
+ import drain from 'it-drain'
6
+ import { CID } from 'multiformats/cid'
7
+ import * as raw from 'multiformats/codecs/raw'
8
+ import { sha256 } from 'multiformats/hashes/sha2'
9
+ import { createHeliaNode } from './fixtures/create-helia.js'
10
+ import { createKuboNode } from './fixtures/create-kubo.js'
11
+ import type { HeliaLibp2p } from 'helia'
12
+ import type { Controller } from 'ipfsd-ctl'
13
+
14
+ describe('helia - pins', () => {
15
+ let helia: HeliaLibp2p
16
+ let kubo: Controller
17
+
18
+ beforeEach(async () => {
19
+ helia = await createHeliaNode()
20
+ kubo = await createKuboNode()
21
+
22
+ // connect the two nodes
23
+ await helia.libp2p.dial(kubo.peer.addresses)
24
+ })
25
+
26
+ afterEach(async () => {
27
+ if (helia != null) {
28
+ await helia.stop()
29
+ }
30
+
31
+ if (kubo != null) {
32
+ await kubo.stop()
33
+ }
34
+ })
35
+
36
+ it('pinning on kubo should pull from helia', async () => {
37
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
38
+ const digest = await sha256.digest(input)
39
+ const cid = CID.createV1(raw.code, digest)
40
+
41
+ expect((await all(kubo.api.refs.local())).map(r => r.ref)).to.not.include(cid.toString())
42
+
43
+ await helia.blockstore.put(cid, input)
44
+
45
+ const pinned = await kubo.api.pin.add(cid)
46
+ expect(pinned.toString()).to.equal(cid.toString())
47
+
48
+ expect((await all(kubo.api.refs.local())).map(r => r.ref)).to.include(cid.toString())
49
+ })
50
+
51
+ it('pinning on helia should pull from kubo', async () => {
52
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
53
+ const { cid } = await kubo.api.add({ content: input }, {
54
+ cidVersion: 1,
55
+ rawLeaves: true
56
+ })
57
+
58
+ await expect(helia.blockstore.has(CID.parse(cid.toString()))).to.eventually.be.false()
59
+
60
+ await drain(helia.pins.add(CID.parse(cid.toString())))
61
+
62
+ await expect(helia.blockstore.has(CID.parse(cid.toString()))).to.eventually.be.true()
63
+ })
64
+ })
package/src/index.ts CHANGED
@@ -1 +1,14 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Runs interop tests between Helia and Kubo.
5
+ *
6
+ * @example Testing a new Kubo release
7
+ *
8
+ * ```console
9
+ * $ npm i @helia/interop
10
+ * $ KUBO_BINARY=/path/to/kubo helia-interop
11
+ * ```
12
+ */
13
+
1
14
  export {}
@@ -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,183 @@
1
+ /* eslint-env mocha */
2
+
3
+ import { ipns } from '@helia/ipns'
4
+ import { peerIdFromString } from '@libp2p/peer-id'
5
+ import { createEd25519PeerId, createRSAPeerId, createSecp256k1PeerId } from '@libp2p/peer-id-factory'
6
+ import { expect } from 'aegir/chai'
7
+ import last from 'it-last'
8
+ import { CID } from 'multiformats/cid'
9
+ import * as raw from 'multiformats/codecs/raw'
10
+ import { sha256 } from 'multiformats/hashes/sha2'
11
+ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
12
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
13
+ import { isElectronMain } from 'wherearewe'
14
+ import { connect } from './fixtures/connect.js'
15
+ import { createHeliaNode } from './fixtures/create-helia.js'
16
+ import { createKuboNode } from './fixtures/create-kubo.js'
17
+ import { sortClosestPeers } from './fixtures/create-peer-ids.js'
18
+ import { keyTypes } from './fixtures/key-types.js'
19
+ import { waitFor } from './fixtures/wait-for.js'
20
+ import type { IPNS } from '@helia/ipns'
21
+ import type { PeerId } from '@libp2p/interface'
22
+ import type { HeliaLibp2p } from 'helia'
23
+ import type { Controller } from 'ipfsd-ctl'
24
+
25
+ keyTypes.forEach(type => {
26
+ describe(`@helia/ipns - default routing with ${type} keys`, () => {
27
+ let helia: HeliaLibp2p
28
+ let kubo: Controller
29
+ let name: IPNS
30
+
31
+ // the CID we are going to publish
32
+ let value: CID
33
+
34
+ // the public key we will use to publish the value
35
+ let key: PeerId
36
+
37
+ /**
38
+ * Ensure that for the CID we are going to publish, the resolver has a peer ID that
39
+ * is KAD-closer to the routing key so we can predict the the resolver will receive
40
+ * the DHT record containing the IPNS record
41
+ */
42
+ async function createNodes (resolver: 'kubo' | 'helia'): Promise<void> {
43
+ const input = Uint8Array.from([0, 1, 2, 3, 4])
44
+ const digest = await sha256.digest(input)
45
+ value = CID.createV1(raw.code, digest)
46
+
47
+ helia = await createHeliaNode()
48
+ kubo = await createKuboNode()
49
+
50
+ // find a PeerId that is KAD-closer to the resolver than the publisher when used as an IPNS key
51
+ while (true) {
52
+ if (type === 'Ed25519') {
53
+ key = await createEd25519PeerId()
54
+ } else if (type === 'secp256k1') {
55
+ key = await createSecp256k1PeerId()
56
+ } else {
57
+ key = await createRSAPeerId()
58
+ }
59
+
60
+ const routingKey = uint8ArrayConcat([
61
+ uint8ArrayFromString('/ipns/'),
62
+ key.toBytes()
63
+ ])
64
+
65
+ const [closest] = await sortClosestPeers(routingKey, [
66
+ helia.libp2p.peerId,
67
+ peerIdFromString(kubo.peer.id.toString())
68
+ ])
69
+
70
+ if (resolver === 'kubo' && closest.equals(peerIdFromString(kubo.peer.id.toString()))) {
71
+ break
72
+ }
73
+
74
+ if (resolver === 'helia' && closest.equals(helia.libp2p.peerId)) {
75
+ break
76
+ }
77
+ }
78
+
79
+ // connect the two nodes over the KAD-DHT protocol, this should ensure
80
+ // both nodes have each other in their KAD buckets
81
+ await connect(helia, kubo, '/ipfs/lan/kad/1.0.0')
82
+
83
+ await waitFor(async () => {
84
+ let found = false
85
+
86
+ for await (const event of helia.libp2p.services.dht.findPeer(peerIdFromString(kubo.peer.id.toString()))) {
87
+ if (event.name === 'FINAL_PEER') {
88
+ found = true
89
+ }
90
+ }
91
+
92
+ return found
93
+ }, {
94
+ timeout: 30000,
95
+ delay: 1000,
96
+ message: 'Helia could not find Kubo on the DHT'
97
+ })
98
+
99
+ await waitFor(async () => {
100
+ let found = false
101
+
102
+ // @ts-expect-error kubo deps are out of date
103
+ for await (const event of kubo.api.dht.findPeer(helia.libp2p.peerId)) {
104
+ if (event.name === 'FINAL_PEER') {
105
+ found = true
106
+ }
107
+ }
108
+
109
+ return found
110
+ }, {
111
+ timeout: 30000,
112
+ delay: 1000,
113
+ message: 'Kubo could not find Helia on the DHT'
114
+ })
115
+
116
+ name = ipns(helia)
117
+ }
118
+
119
+ afterEach(async () => {
120
+ if (helia != null) {
121
+ await helia.stop()
122
+ }
123
+
124
+ if (kubo != null) {
125
+ await kubo.stop()
126
+ }
127
+ })
128
+
129
+ it(`should publish on helia and resolve on kubo using a ${type} key`, async () => {
130
+ await createNodes('kubo')
131
+
132
+ const keyName = 'my-ipns-key'
133
+ await helia.libp2p.services.keychain.importPeer(keyName, key)
134
+
135
+ await name.publish(key, value)
136
+
137
+ const resolved = await last(kubo.api.name.resolve(key.toString()))
138
+
139
+ if (resolved == null) {
140
+ throw new Error('kubo failed to resolve name')
141
+ }
142
+
143
+ expect(resolved).to.equal(`/ipfs/${value.toString()}`)
144
+ })
145
+
146
+ it('should publish on kubo and resolve on helia', async function () {
147
+ if (isElectronMain) {
148
+ // electron main does not have fetch, FormData or Blob APIs
149
+ // can revisit when kubo-rpc-client supports the key.import API
150
+ return this.skip()
151
+ }
152
+
153
+ if (type === 'secp256k1') {
154
+ // Kubo cannot import secp256k1 keys
155
+ return this.skip()
156
+ }
157
+
158
+ await createNodes('helia')
159
+
160
+ const keyName = 'my-ipns-key'
161
+ const { cid } = await kubo.api.add(Uint8Array.from([0, 1, 2, 3, 4]))
162
+
163
+ // ensure the key is in the kubo keychain so we can use it to publish the IPNS record
164
+ const body = new FormData()
165
+ body.append('key', new Blob([key.privateKey ?? new Uint8Array(0)]))
166
+
167
+ // can't use the kubo-rpc-api for this call yet
168
+ const response = await fetch(`http://${kubo.api.apiHost}:${kubo.api.apiPort}/api/v0/key/import?arg=${keyName}`, {
169
+ method: 'POST',
170
+ body
171
+ })
172
+
173
+ expect(response).to.have.property('status', 200)
174
+
175
+ await kubo.api.name.publish(cid, {
176
+ key: keyName
177
+ })
178
+
179
+ const resolvedCid = await name.resolve(key)
180
+ expect(resolvedCid.toString()).to.equal(cid.toString())
181
+ })
182
+ })
183
+ })
@@ -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
+ })