-
Notifications
You must be signed in to change notification settings - Fork 2
/
carstream.go
66 lines (57 loc) · 1.8 KB
/
carstream.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
package frisbii
import (
"bytes"
"context"
"io"
// codecs we care about
_ "github.com/ipld/go-ipld-prime/codec/cbor"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
_ "github.com/ipld/go-ipld-prime/codec/json"
_ "github.com/ipld/go-ipld-prime/codec/raw"
"github.com/ipld/go-trustless-utils/traversal"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/storage/deferred"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
trustlessutils "github.com/ipld/go-trustless-utils"
)
// StreamCar streams a DAG in CARv1 format to the given writer, using the given
// selector.
func StreamCar(
ctx context.Context,
requestLsys linking.LinkSystem,
out io.Writer,
request trustlessutils.Request,
) error {
carWriter := deferred.NewDeferredCarWriterForStream(out, []cid.Cid{request.Root}, car.AllowDuplicatePuts(request.Duplicates))
requestLsys.StorageReadOpener = carPipe(requestLsys.StorageReadOpener, carWriter)
cfg := traversal.Config{Root: request.Root, Selector: request.Selector()}
lastPath, err := cfg.Traverse(ctx, requestLsys, nil)
if err != nil {
return err
}
if err := traversal.CheckPath(datamodel.ParsePath(request.Path), lastPath); err != nil {
logger.Warn(err)
}
return nil
}
func carPipe(orig linking.BlockReadOpener, car *deferred.DeferredCarWriter) linking.BlockReadOpener {
return func(lc linking.LinkContext, lnk datamodel.Link) (io.Reader, error) {
r, err := orig(lc, lnk)
if err != nil {
return nil, err
}
byts, err := io.ReadAll(r)
if err != nil {
return nil, err
}
err = car.Put(lc.Ctx, lnk.(cidlink.Link).Cid.KeyString(), byts)
if err != nil {
return nil, err
}
return bytes.NewReader(byts), nil
}
}