Skip to content

Commit

Permalink
commands: fix a bunch of tiny commands-lib issues
Browse files Browse the repository at this point in the history
* Always check errors returned by emit. Otherwise, we may not notice when the
  client goes away.
* Make sure to use EmitOnce instead of Emit when appropriate. Otherwise, we
  break javascript.

(thanks Magik6k for finding this before we cut the release...)

License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
  • Loading branch information
Stebalien committed Oct 30, 2018
1 parent f8375ec commit 9a52ca1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions core/commands/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ format.
}
out = final
}
return res.Emit(&out)
return cmds.EmitOnce(res, &out)
},
}

Expand Down Expand Up @@ -219,7 +219,7 @@ var DagResolveCmd = &cmds.Command{
return err
}

return res.Emit(&ResolveOutput{
return cmds.EmitOnce(res, &ResolveOutput{
Cid: lastCid,
RemPath: path.Join(rem),
})
Expand Down
2 changes: 1 addition & 1 deletion core/commands/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The resolver can recursively resolve:
if err != nil {
return err
}
return res.Emit(&ncmd.ResolvedPath{Path: output})
return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: output})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ncmd.ResolvedPath) error {
Expand Down
6 changes: 4 additions & 2 deletions core/commands/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ This command outputs data in the following encodings:
return err
}

res.Emit(&pubsubMessage{
if err := res.Emit(&pubsubMessage{
Data: msg.Data(),
From: []byte(msg.From()),
Seqno: msg.Seq(),
TopicIDs: msg.Topics(),
})
}); err != nil {
return err
}
}
},
Encoders: cmds.EncoderMap{
Expand Down
14 changes: 10 additions & 4 deletions core/commands/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,28 @@ Example:
for {
if pfound {
stats := nd.Reporter.GetBandwidthForPeer(pid)
res.Emit(&stats)
if err := res.Emit(&stats); err != nil {
return err
}
} else if tfound {
protoId := protocol.ID(tstr)
stats := nd.Reporter.GetBandwidthForProtocol(protoId)
res.Emit(&stats)
if err := res.Emit(&stats); err != nil {
return err
}
} else {
totals := nd.Reporter.GetBandwidthTotals()
res.Emit(&totals)
if err := res.Emit(&totals); err != nil {
return err
}
}
if !doPoll {
return nil
}
select {
case <-time.After(interval):
case <-req.Context.Done():
return nil
return req.Context.Err()
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions core/commands/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ represent it.
c := node.Cid()

fi.FileName()
return res.Emit(&coreiface.AddEvent{
return cmds.EmitOnce(&coreiface.AddEvent{
Name: fi.FileName(),
Hash: c.String(),
})
Expand Down Expand Up @@ -108,6 +108,6 @@ var tarCatCmd = &cmds.Command{
return err
}

return res.Emit(r)
return cmds.EmitOnce(res, r)
},
}
2 changes: 1 addition & 1 deletion core/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var VersionCmd = &cmds.Command{
cmdkit.BoolOption(versionAllOptionName, "Show all version information"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
return res.Emit(&VersionOutput{
return cmds.EmitOnce(res, &VersionOutput{
Version: version.CurrentVersionNumber,
Commit: version.CurrentCommit,
Repo: fmt.Sprint(fsrepo.RepoVersion),
Expand Down

0 comments on commit 9a52ca1

Please sign in to comment.