|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + |
| 8 | + cid "github.com/ipfs/go-cid" |
| 9 | + "github.com/ipfs/go-libipfs/blocks" |
| 10 | + "github.com/ipfs/go-libipfs/files" |
| 11 | + "github.com/ipfs/go-namesys" |
| 12 | + path "github.com/ipfs/go-path" |
| 13 | + iface "github.com/ipfs/interface-go-ipfs-core" |
| 14 | + nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys" |
| 15 | + ipath "github.com/ipfs/interface-go-ipfs-core/path" |
| 16 | + "github.com/libp2p/go-libp2p/core/crypto" |
| 17 | +) |
| 18 | + |
| 19 | +type mockNamesys map[string]path.Path |
| 20 | + |
| 21 | +func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) { |
| 22 | + cfg := nsopts.DefaultResolveOpts() |
| 23 | + for _, o := range opts { |
| 24 | + o(&cfg) |
| 25 | + } |
| 26 | + depth := cfg.Depth |
| 27 | + if depth == nsopts.UnlimitedDepth { |
| 28 | + // max uint |
| 29 | + depth = ^uint(0) |
| 30 | + } |
| 31 | + for strings.HasPrefix(name, "/ipns/") { |
| 32 | + if depth == 0 { |
| 33 | + return value, namesys.ErrResolveRecursion |
| 34 | + } |
| 35 | + depth-- |
| 36 | + |
| 37 | + var ok bool |
| 38 | + value, ok = m[name] |
| 39 | + if !ok { |
| 40 | + return "", namesys.ErrResolveFailed |
| 41 | + } |
| 42 | + name = value.String() |
| 43 | + } |
| 44 | + return value, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (m mockNamesys) ResolveAsync(ctx context.Context, name string, opts ...nsopts.ResolveOpt) <-chan namesys.Result { |
| 48 | + out := make(chan namesys.Result, 1) |
| 49 | + v, err := m.Resolve(ctx, name, opts...) |
| 50 | + out <- namesys.Result{Path: v, Err: err} |
| 51 | + close(out) |
| 52 | + return out |
| 53 | +} |
| 54 | + |
| 55 | +func (m mockNamesys) Publish(ctx context.Context, name crypto.PrivKey, value path.Path, opts ...nsopts.PublishOption) error { |
| 56 | + return errors.New("not implemented for mockNamesys") |
| 57 | +} |
| 58 | + |
| 59 | +func (m mockNamesys) GetResolver(subs string) (namesys.Resolver, bool) { |
| 60 | + return nil, false |
| 61 | +} |
| 62 | + |
| 63 | +type mockApi struct { |
| 64 | + ns mockNamesys |
| 65 | +} |
| 66 | + |
| 67 | +func newMockApi() *mockApi { |
| 68 | + return &mockApi{ |
| 69 | + ns: mockNamesys{}, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func (m *mockApi) GetUnixFsNode(context.Context, ipath.Resolved) (files.Node, error) { |
| 74 | + return nil, errors.New("not implemented") |
| 75 | +} |
| 76 | + |
| 77 | +func (m *mockApi) LsUnixFsDir(context.Context, ipath.Resolved) (<-chan iface.DirEntry, error) { |
| 78 | + return nil, errors.New("not implemented") |
| 79 | +} |
| 80 | + |
| 81 | +func (m *mockApi) GetBlock(context.Context, cid.Cid) (blocks.Block, error) { |
| 82 | + return nil, errors.New("not implemented") |
| 83 | +} |
| 84 | + |
| 85 | +func (m *mockApi) GetIPNSRecord(context.Context, cid.Cid) ([]byte, error) { |
| 86 | + return nil, errors.New("not implemented") |
| 87 | +} |
| 88 | + |
| 89 | +func (m *mockApi) GetDNSLinkRecord(ctx context.Context, hostname string) (ipath.Path, error) { |
| 90 | + p, err := m.ns.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1)) |
| 91 | + if err == namesys.ErrResolveRecursion { |
| 92 | + err = nil |
| 93 | + } |
| 94 | + return ipath.New(p.String()), err |
| 95 | +} |
| 96 | + |
| 97 | +func (m *mockApi) IsCached(context.Context, ipath.Path) bool { |
| 98 | + return false |
| 99 | +} |
| 100 | + |
| 101 | +func (m *mockApi) ResolvePath(context.Context, ipath.Path) (ipath.Resolved, error) { |
| 102 | + return nil, errors.New("not implemented") |
| 103 | +} |
0 commit comments