Skip to content

Commit 42af4f8

Browse files
committed
refactor!: namesys package
1 parent 546e33d commit 42af4f8

40 files changed

+1567
-1611
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ The following emojis are used to highlight certain changes:
1616

1717
### Added
1818

19+
* The gateway now sets a `Cache-Control` header for requests under the `/ipns/` namespace
20+
if the TTL for the corresponding IPNS Records or DNSLink entities is known.
21+
1922
### Changed
2023

2124
* 🛠 The `path` package has been massively refactored. With this refactor, we have
@@ -24,6 +27,18 @@ The following emojis are used to highlight certain changes:
2427
for more details on how to use the new package.
2528
* 🛠 The signature of `CoreAPI.ResolvePath` in `coreiface` has changed to now return
2629
the remainder segments as a second return value, matching the signature of `resolver.ResolveToLastNode`.
30+
* 🛠 The `namesys` package has been refactored. The following are the largest modifications:
31+
* The options in `coreiface/options/namesys` have been moved to `namesys` and their names
32+
have been made more consistent.
33+
* Many of the exported structs and functions have been renamed in order to be consistent with
34+
the remaining packages.
35+
* `namesys.Resolver.Resolve` now returns a TTL, in addition to the resolved path. If the
36+
TTL is unknown, 0 is returned. `IPNSResolver` is able to resolve a TTL, while `DNSResolver`
37+
is not.
38+
* `namesys/resolver.ResolveIPNS` has been moved to `namesys.ResolveIPNS` and now returns a TTL
39+
in addition to the resolved path.
40+
* 🛠 The `gateway`'s `IPFSBackend.ResolveMutable` is now expected to return a TTL in addition to
41+
the resolved path. If the TTL is unknown, 0 should be returned.
2742

2843
### Removed
2944

coreiface/options/name.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package options
33
import (
44
"time"
55

6-
ropts "github.com/ipfs/boxo/coreiface/options/namesys"
6+
"github.com/ipfs/boxo/namesys"
77
)
88

99
const (
@@ -21,7 +21,7 @@ type NamePublishSettings struct {
2121
type NameResolveSettings struct {
2222
Cache bool
2323

24-
ResolveOpts []ropts.ResolveOpt
24+
ResolveOpts []namesys.ResolveOption
2525
}
2626

2727
type (
@@ -123,7 +123,7 @@ func (nameOpts) Cache(cache bool) NameResolveOption {
123123
}
124124
}
125125

126-
func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption {
126+
func (nameOpts) ResolveOption(opt namesys.ResolveOption) NameResolveOption {
127127
return func(settings *NameResolveSettings) error {
128128
settings.ResolveOpts = append(settings.ResolveOpts, opt)
129129
return nil

coreiface/options/namesys/opts.go

Lines changed: 0 additions & 131 deletions
This file was deleted.

gateway/blocks_backend.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import (
88
"io"
99
"net/http"
1010
"strings"
11+
"time"
1112

1213
"github.com/ipfs/boxo/blockservice"
1314
blockstore "github.com/ipfs/boxo/blockstore"
14-
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
1515
"github.com/ipfs/boxo/fetcher"
1616
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
1717
"github.com/ipfs/boxo/files"
1818
"github.com/ipfs/boxo/ipld/merkledag"
1919
ufile "github.com/ipfs/boxo/ipld/unixfs/file"
2020
uio "github.com/ipfs/boxo/ipld/unixfs/io"
21+
"github.com/ipfs/boxo/ipns"
2122
"github.com/ipfs/boxo/namesys"
22-
"github.com/ipfs/boxo/namesys/resolve"
2323
"github.com/ipfs/boxo/path"
2424
"github.com/ipfs/boxo/path/resolver"
2525
blocks "github.com/ipfs/go-block-format"
@@ -39,7 +39,6 @@ import (
3939
"github.com/ipld/go-ipld-prime/traversal/selector"
4040
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
4141
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
42-
"github.com/libp2p/go-libp2p/core/peer"
4342
"github.com/libp2p/go-libp2p/core/routing"
4443
mc "github.com/multiformats/go-multicodec"
4544

@@ -562,18 +561,23 @@ func (bb *BlocksBackend) getPathRoots(ctx context.Context, contentPath path.Immu
562561
return pathRoots, lastPath, remainder, nil
563562
}
564563

565-
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, error) {
564+
func (bb *BlocksBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, time.Duration, time.Time, error) {
566565
switch p.Namespace() {
567566
case path.IPNSNamespace:
568-
p, err := resolve.ResolveIPNS(ctx, bb.namesys, p)
567+
res, err := namesys.Resolve(ctx, bb.namesys, p)
569568
if err != nil {
570-
return nil, err
569+
return nil, 0, time.Time{}, err
570+
}
571+
ip, err := path.NewImmutablePath(res.Path)
572+
if err != nil {
573+
return nil, 0, time.Time{}, err
571574
}
572-
return path.NewImmutablePath(p)
575+
return ip, res.TTL, res.LastMod, nil
573576
case path.IPFSNamespace:
574-
return path.NewImmutablePath(p)
577+
ip, err := path.NewImmutablePath(p)
578+
return ip, 0, time.Time{}, err
575579
default:
576-
return nil, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)
580+
return nil, 0, time.Time{}, NewErrorStatusCode(fmt.Errorf("unsupported path namespace: %s", p.Namespace()), http.StatusNotImplemented)
577581
}
578582
}
579583

@@ -582,28 +586,25 @@ func (bb *BlocksBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte,
582586
return nil, NewErrorStatusCode(errors.New("IPNS Record responses are not supported by this gateway"), http.StatusNotImplemented)
583587
}
584588

585-
// Fails fast if the CID is not an encoded Libp2p Key, avoids wasteful
586-
// round trips to the remote routing provider.
587-
if mc.Code(c.Type()) != mc.Libp2pKey {
588-
return nil, NewErrorStatusCode(errors.New("cid codec must be libp2p-key"), http.StatusBadRequest)
589-
}
590-
591-
// The value store expects the key itself to be encoded as a multihash.
592-
id, err := peer.FromCid(c)
589+
name, err := ipns.NameFromCid(c)
593590
if err != nil {
594-
return nil, err
591+
return nil, NewErrorStatusCode(err, http.StatusBadRequest)
595592
}
596593

597-
return bb.routing.GetValue(ctx, "/ipns/"+string(id))
594+
return bb.routing.GetValue(ctx, string(name.RoutingKey()))
598595
}
599596

600597
func (bb *BlocksBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (path.Path, error) {
601598
if bb.namesys != nil {
602-
p, err := bb.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
599+
p, err := path.NewPath("/ipns/" + hostname)
600+
if err != nil {
601+
return nil, err
602+
}
603+
res, err := bb.namesys.Resolve(ctx, p, namesys.ResolveWithDepth(1))
603604
if err == namesys.ErrResolveRecursion {
604605
err = nil
605606
}
606-
return p, err
607+
return res.Path, err
607608
}
608609

609610
return nil, NewErrorStatusCode(errors.New("not implemented"), http.StatusNotImplemented)
@@ -635,10 +636,11 @@ func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePat
635636
func (bb *BlocksBackend) resolvePath(ctx context.Context, p path.Path) (path.ImmutablePath, []string, error) {
636637
var err error
637638
if p.Namespace() == path.IPNSNamespace {
638-
p, err = resolve.ResolveIPNS(ctx, bb.namesys, p)
639+
res, err := namesys.Resolve(ctx, bb.namesys, p)
639640
if err != nil {
640641
return nil, nil, err
641642
}
643+
p = res.Path
642644
}
643645

644646
if p.Namespace() != path.IPFSNamespace {

gateway/gateway.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sort"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"github.com/ipfs/boxo/files"
1314
"github.com/ipfs/boxo/gateway/assets"
@@ -309,11 +310,11 @@ type IPFSBackend interface {
309310
GetIPNSRecord(context.Context, cid.Cid) ([]byte, error)
310311

311312
// ResolveMutable takes a mutable path and resolves it into an immutable one. This means recursively resolving any
312-
// DNSLink or IPNS records.
313+
// DNSLink or IPNS records. It should also return a TTL. If the TTL is unknown, 0 should be returned.
313314
//
314315
// For example, given a mapping from `/ipns/dnslink.tld -> /ipns/ipns-id/mydirectory` and `/ipns/ipns-id` to
315316
// `/ipfs/some-cid`, the result of passing `/ipns/dnslink.tld/myfile` would be `/ipfs/some-cid/mydirectory/myfile`.
316-
ResolveMutable(context.Context, path.Path) (path.ImmutablePath, error)
317+
ResolveMutable(context.Context, path.Path) (path.ImmutablePath, time.Duration, time.Time, error)
317318

318319
// GetDNSLinkRecord returns the DNSLink TXT record for the provided FQDN.
319320
// Unlike ResolvePath, it does not perform recursive resolution. It only

0 commit comments

Comments
 (0)