This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetipns.go
93 lines (81 loc) · 1.68 KB
/
getipns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package client
import (
"context"
ipns "github.com/ipfs/boxo/ipns"
"github.com/ipfs/go-delegated-routing/gen/proto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
)
func (fp *Client) GetIPNS(ctx context.Context, id []byte) ([]byte, error) {
resps, err := fp.GetIPNSAsync(ctx, id)
if err != nil {
return nil, err
}
records := [][]byte{}
for resp := range resps {
if resp.Err == nil {
records = append(records, resp.Record)
}
}
if len(records) == 0 {
return nil, routing.ErrNotFound
}
best, err := fp.validator.Select(ipns.RecordKey(peer.ID(id)), records)
if err != nil {
return nil, err
}
return records[best], nil
}
type GetIPNSAsyncResult struct {
Record []byte
Err error
}
func (fp *Client) GetIPNSAsync(ctx context.Context, id []byte) (<-chan GetIPNSAsyncResult, error) {
ch0, err := fp.client.GetIPNS_Async(ctx, &proto.GetIPNSRequest{ID: id})
if err != nil {
return nil, err
}
ch1 := make(chan GetIPNSAsyncResult, 1)
go func() {
defer close(ch1)
for {
select {
case <-ctx.Done():
return
case r0, ok := <-ch0:
if !ok {
return
}
var r1 GetIPNSAsyncResult
if r0.Err != nil {
r1.Err = r0.Err
select {
case <-ctx.Done():
return
case ch1 <- r1:
}
continue
}
if r0.Resp == nil {
continue
}
if err = fp.validator.Validate(ipns.RecordKey(peer.ID(id)), r0.Resp.Record); err != nil {
r1.Err = err
select {
case <-ctx.Done():
return
case ch1 <- r1:
}
continue
}
r1.Record = r0.Resp.Record
select {
case <-ctx.Done():
return
case ch1 <- r1:
}
}
}
}()
return ch1, nil
}