generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cleanup isDNSLinkName mess
- Loading branch information
Showing
7 changed files
with
171 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,11 @@ type API interface { | |
// exist due to a missing link, it should return an error of type: | ||
// https://pkg.go.dev/github.com/ipfs/[email protected]/resolver#ErrNoLink | ||
ResolvePath(context.Context, path.Path) (path.Resolved, error) | ||
|
||
// HasDNSLinkRecord returns if the provided path has a DNSLink TXT record. | ||
// It does not perform any validation, only checks for the existence of | ||
// a DNSLink TXT record. | ||
HasDNSLinkRecord(context.Context, string) bool | ||
} | ||
|
||
// A helper function to clean up a set of headers: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package gateway | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-libipfs/blocks" | ||
"github.com/ipfs/go-libipfs/files" | ||
"github.com/ipfs/go-namesys" | ||
path "github.com/ipfs/go-path" | ||
iface "github.com/ipfs/interface-go-ipfs-core" | ||
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
"github.com/libp2p/go-libp2p/core/crypto" | ||
) | ||
|
||
type mockNamesys map[string]path.Path | ||
|
||
func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) { | ||
cfg := nsopts.DefaultResolveOpts() | ||
for _, o := range opts { | ||
o(&cfg) | ||
} | ||
depth := cfg.Depth | ||
if depth == nsopts.UnlimitedDepth { | ||
// max uint | ||
depth = ^uint(0) | ||
} | ||
for strings.HasPrefix(name, "/ipns/") { | ||
if depth == 0 { | ||
return value, namesys.ErrResolveRecursion | ||
} | ||
depth-- | ||
|
||
var ok bool | ||
value, ok = m[name] | ||
if !ok { | ||
return "", namesys.ErrResolveFailed | ||
} | ||
name = value.String() | ||
} | ||
return value, nil | ||
} | ||
|
||
func (m mockNamesys) ResolveAsync(ctx context.Context, name string, opts ...nsopts.ResolveOpt) <-chan namesys.Result { | ||
out := make(chan namesys.Result, 1) | ||
v, err := m.Resolve(ctx, name, opts...) | ||
out <- namesys.Result{Path: v, Err: err} | ||
close(out) | ||
return out | ||
} | ||
|
||
func (m mockNamesys) Publish(ctx context.Context, name crypto.PrivKey, value path.Path, opts ...nsopts.PublishOption) error { | ||
return errors.New("not implemented for mockNamesys") | ||
} | ||
|
||
func (m mockNamesys) GetResolver(subs string) (namesys.Resolver, bool) { | ||
return nil, false | ||
} | ||
|
||
type mockApi struct { | ||
ns mockNamesys | ||
} | ||
|
||
func newMockApi() *mockApi { | ||
return &mockApi{ | ||
ns: mockNamesys{}, | ||
} | ||
} | ||
|
||
func (m *mockApi) GetUnixFsNode(context.Context, ipath.Resolved) (files.Node, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
|
||
func (m *mockApi) LsUnixFsDir(context.Context, ipath.Resolved) (<-chan iface.DirEntry, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
|
||
func (m *mockApi) GetBlock(context.Context, cid.Cid) (blocks.Block, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
|
||
func (m *mockApi) GetIPNSRecord(context.Context, cid.Cid) ([]byte, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
|
||
func (m *mockApi) IsCached(context.Context, ipath.Path) bool { | ||
return false | ||
} | ||
|
||
func (m *mockApi) ResolvePath(context.Context, ipath.Path) (ipath.Resolved, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
|
||
func (m *mockApi) HasDNSLinkRecord(ctx context.Context, hostname string) bool { | ||
_, err := m.ns.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1)) | ||
return err == nil || err == namesys.ErrResolveRecursion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters