| 1 | package options |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/ipfs/boxo/namesys" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | DefaultNameValidTime = 24 * time.Hour |
| 11 | ) |
| 12 | |
| 13 | type NamePublishSettings struct { |
| 14 | ValidTime time.Duration |
| 15 | Key string |
| 16 | TTL *time.Duration |
| 17 | CompatibleWithV1 bool |
| 18 | AllowOffline bool |
| 19 | AllowDelegated bool |
| 20 | Sequence *uint64 |
| 21 | } |
| 22 | |
| 23 | type NameResolveSettings struct { |
| 24 | Cache bool |
| 25 | |
| 26 | ResolveOpts []namesys.ResolveOption |
| 27 | } |
| 28 | |
| 29 | type ( |
| 30 | NamePublishOption func(*NamePublishSettings) error |
| 31 | NameResolveOption func(*NameResolveSettings) error |
| 32 | ) |
| 33 | |
| 34 | func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) { |
| 35 | options := &NamePublishSettings{ |
| 36 | ValidTime: DefaultNameValidTime, |
| 37 | Key: "self", |
| 38 | |
| 39 | AllowOffline: false, |
| 40 | AllowDelegated: false, |
| 41 | } |
| 42 | |
| 43 | for _, opt := range opts { |
| 44 | err := opt(options) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return options, nil |
| 51 | } |
| 52 | |
| 53 | func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) { |
| 54 | options := &NameResolveSettings{ |
| 55 | Cache: true, |
| 56 | } |
| 57 | |
| 58 | for _, opt := range opts { |
| 59 | err := opt(options) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return options, nil |
| 66 | } |
| 67 | |
| 68 | type nameOpts struct{} |
| 69 | |
| 70 | var Name nameOpts |
| 71 | |
| 72 | // ValidTime is an option for Name.Publish which specifies for how long the |
| 73 | // entry will remain valid. Default value is 24h |
| 74 | func (nameOpts) ValidTime(validTime time.Duration) NamePublishOption { |
| 75 | return func(settings *NamePublishSettings) error { |
| 76 | settings.ValidTime = validTime |
| 77 | return nil |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Key is an option for Name.Publish which specifies the key to use for |
| 82 | // publishing. Default value is "self" which is the node's own PeerID. |
| 83 | // The key parameter must be either PeerID or keystore key alias. |
| 84 | // |
| 85 | // You can use KeyAPI to list and generate more names and their respective keys. |
| 86 | func (nameOpts) Key(key string) NamePublishOption { |
| 87 | return func(settings *NamePublishSettings) error { |
| 88 | settings.Key = key |
| 89 | return nil |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // AllowOffline is an option for Name.Publish which specifies whether to allow |
| 94 | // publishing when the node is offline. Default value is false |
| 95 | func (nameOpts) AllowOffline(allow bool) NamePublishOption { |
| 96 | return func(settings *NamePublishSettings) error { |
| 97 | settings.AllowOffline = allow |
| 98 | return nil |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // AllowDelegated is an option for Name.Publish which allows publishing without |
| 103 | // DHT connectivity, using local datastore and HTTP delegated publishers only. |
| 104 | // Default value is false |
| 105 | func (nameOpts) AllowDelegated(allowDelegated bool) NamePublishOption { |
| 106 | return func(settings *NamePublishSettings) error { |
| 107 | settings.AllowDelegated = allowDelegated |
| 108 | return nil |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // TTL is an option for Name.Publish which specifies the time duration the |
| 113 | // published record should be cached for (caution: experimental). |
| 114 | func (nameOpts) TTL(ttl time.Duration) NamePublishOption { |
| 115 | return func(settings *NamePublishSettings) error { |
| 116 | settings.TTL = &ttl |
| 117 | return nil |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Sequence is an option for Name.Publish which specifies the sequence number of |
| 122 | // a namesys record. |
| 123 | func (nameOpts) Sequence(seq uint64) NamePublishOption { |
| 124 | return func(settings *NamePublishSettings) error { |
| 125 | settings.Sequence = &seq |
| 126 | return nil |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // CompatibleWithV1 is an option for [Name.Publish] which specifies if the |
| 131 | // created record should be backwards compatible with V1 IPNS Records. |
| 132 | func (nameOpts) CompatibleWithV1(compatible bool) NamePublishOption { |
| 133 | return func(settings *NamePublishSettings) error { |
| 134 | settings.CompatibleWithV1 = compatible |
| 135 | return nil |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Cache is an option for Name.Resolve which specifies if cache should be used. |
| 140 | // Default value is true |
| 141 | func (nameOpts) Cache(cache bool) NameResolveOption { |
| 142 | return func(settings *NameResolveSettings) error { |
| 143 | settings.Cache = cache |
| 144 | return nil |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func (nameOpts) ResolveOption(opt namesys.ResolveOption) NameResolveOption { |
| 149 | return func(settings *NameResolveSettings) error { |
| 150 | settings.ResolveOpts = append(settings.ResolveOpts, opt) |
| 151 | return nil |
| 152 | } |
| 153 | } |