@helia/ipns 5.0.0 → 6.0.0

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/README.md CHANGED
@@ -21,7 +21,7 @@ IPNS operations using a Helia node
21
21
 
22
22
  With IPNSRouting routers:
23
23
 
24
- ```typescript
24
+ ```TypeScript
25
25
  import { createHelia } from 'helia'
26
26
  import { ipns } from '@helia/ipns'
27
27
  import { unixfs } from '@helia/unixfs'
@@ -41,7 +41,78 @@ const cid = await fs.add(Uint8Array.from([0, 1, 2, 3, 4]))
41
41
  await name.publish(peerId, cid)
42
42
 
43
43
  // resolve the name
44
- const cid = name.resolve(peerId)
44
+ const result = name.resolve(peerId)
45
+
46
+ console.info(result.cid, result.path)
47
+ ```
48
+
49
+ ## Example - Publishing a recursive record
50
+
51
+ A recursive record is a one that points to another record rather than to a
52
+ value.
53
+
54
+ ```TypeScript
55
+ import { createHelia } from 'helia'
56
+ import { ipns } from '@helia/ipns'
57
+ import { unixfs } from '@helia/unixfs'
58
+
59
+ const helia = await createHelia()
60
+ const name = ipns(helia)
61
+
62
+ // create a public key to publish as an IPNS name
63
+ const keyInfo = await helia.libp2p.services.keychain.createKey('my-key')
64
+ const peerId = await helia.libp2p.services.keychain.exportPeerId(keyInfo.name)
65
+
66
+ // store some data to publish
67
+ const fs = unixfs(helia)
68
+ const cid = await fs.add(Uint8Array.from([0, 1, 2, 3, 4]))
69
+
70
+ // publish the name
71
+ await name.publish(peerId, cid)
72
+
73
+ // create another public key to re-publish the original record
74
+ const recursiveKeyInfo = await helia.libp2p.services.keychain.createKey('my-recursive-key')
75
+ const recursivePeerId = await helia.libp2p.services.keychain.exportPeerId(recursiveKeyInfo.name)
76
+
77
+ // publish the recursive name
78
+ await name.publish(recursivePeerId, peerId)
79
+
80
+ // resolve the name recursively - it resolves until a CID is found
81
+ const result = name.resolve(recursivePeerId)
82
+ console.info(result.cid.toString() === cid.toString()) // true
83
+ ```
84
+
85
+ ## Example - Publishing a record with a path
86
+
87
+ It is possible to publish CIDs with an associated path.
88
+
89
+ ```TypeScript
90
+ import { createHelia } from 'helia'
91
+ import { ipns } from '@helia/ipns'
92
+ import { unixfs } from '@helia/unixfs'
93
+
94
+ const helia = await createHelia()
95
+ const name = ipns(helia)
96
+
97
+ // create a public key to publish as an IPNS name
98
+ const keyInfo = await helia.libp2p.services.keychain.createKey('my-key')
99
+ const peerId = await helia.libp2p.services.keychain.exportPeerId(keyInfo.name)
100
+
101
+ // store some data to publish
102
+ const fs = unixfs(helia)
103
+ const fileCid = await fs.add(Uint8Array.from([0, 1, 2, 3, 4]))
104
+
105
+ // store the file in a directory
106
+ const dirCid = await fs.mkdir()
107
+ const finalDirCid = await fs.cp(fileCid, dirCid, '/foo.txt')
108
+
109
+ // publish the name
110
+ await name.publish(peerId, `/ipfs/${finalDirCid}/foo.txt)
111
+
112
+ // resolve the name
113
+ const result = name.resolve(peerId)
114
+
115
+ console.info(result.cid, result.path) // QmFoo.. 'foo.txt'
45
116
  ```
46
117
 
47
118
  ## Example - Using custom PubSub router
@@ -60,7 +131,7 @@ This router is only suitable for networks where IPNS updates are frequent
60
131
  and multiple peers are listening on the topic(s), otherwise update messages
61
132
  may fail to be published with "Insufficient peers" errors.
62
133
 
63
- ```typescript
134
+ ```TypeScript
64
135
  import { createHelia, libp2pDefaults } from 'helia'
65
136
  import { ipns } from '@helia/ipns'
66
137
  import { pubsub } from '@helia/ipns/routing'
@@ -91,14 +162,14 @@ const cid = await fs.add(Uint8Array.from([0, 1, 2, 3, 4]))
91
162
  await name.publish(peerId, cid)
92
163
 
93
164
  // resolve the name
94
- const cid = name.resolve(peerId)
165
+ const { cid, path } = name.resolve(peerId)
95
166
  ```
96
167
 
97
168
  ## Example - Using custom DNS over HTTPS resolvers
98
169
 
99
170
  With default DNSResolver resolvers:
100
171
 
101
- ```typescript
172
+ ```TypeScript
102
173
  import { createHelia } from 'helia'
103
174
  import { ipns } from '@helia/ipns'
104
175
  import { unixfs } from '@helia/unixfs'
@@ -111,14 +182,14 @@ const name = ipns(helia, {
111
182
  ]
112
183
  })
113
184
 
114
- const cid = name.resolveDns('some-domain-with-dnslink-entry.com')
185
+ const { cid, path } = name.resolveDns('some-domain-with-dnslink-entry.com')
115
186
  ```
116
187
 
117
188
  ## Example - Resolving a domain with a dnslink entry
118
189
 
119
190
  Calling `resolveDns` with the `@helia/ipns` instance:
120
191
 
121
- ```typescript
192
+ ```TypeScript
122
193
  // resolve a CID from a TXT record in a DNS zone file, using the default
123
194
  // resolver for the current platform eg:
124
195
  // > dig _dnslink.ipfs.io TXT
@@ -128,7 +199,7 @@ Calling `resolveDns` with the `@helia/ipns` instance:
128
199
  // ;; ANSWER SECTION:
129
200
  // _dnslink.website.ipfs.io. 60 IN TXT "dnslink=/ipfs/QmWebsite"
130
201
 
131
- const cid = name.resolveDns('ipfs.io')
202
+ const { cid, path } = name.resolveDns('ipfs.io')
132
203
 
133
204
  console.info(cid)
134
205
  // QmWebsite
@@ -142,11 +213,11 @@ response which can increase browser bundle sizes.
142
213
 
143
214
  If this is a concern, use the DNS-JSON-Over-HTTPS resolver instead.
144
215
 
145
- ```typescript
216
+ ```TypeScript
146
217
  // use DNS-Over-HTTPS
147
218
  import { dnsOverHttps } from '@helia/ipns/dns-resolvers'
148
219
 
149
- const cid = name.resolveDns('ipfs.io', {
220
+ const { cid, path } = name.resolveDns('ipfs.io', {
150
221
  resolvers: [
151
222
  dnsOverHttps('https://mozilla.cloudflare-dns.com/dns-query')
152
223
  ]
@@ -158,11 +229,11 @@ const cid = name.resolveDns('ipfs.io', {
158
229
  DNS-JSON-Over-HTTPS resolvers use the RFC 8427 `application/dns-json` and can
159
230
  result in a smaller browser bundle due to the response being plain JSON.
160
231
 
161
- ```typescript
232
+ ```TypeScript
162
233
  // use DNS-JSON-Over-HTTPS
163
234
  import { dnsJsonOverHttps } from '@helia/ipns/dns-resolvers'
164
235
 
165
- const cid = name.resolveDns('ipfs.io', {
236
+ const { cid, path } = name.resolveDns('ipfs.io', {
166
237
  resolvers: [
167
238
  dnsJsonOverHttps('https://mozilla.cloudflare-dns.com/dns-query')
168
239
  ]