forked from matthewmueller/joy
/
connection.go
170 lines (147 loc) · 3.51 KB
/
connection.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package livereload
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"golang.org/x/sync/errgroup"
"github.com/apex/log"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
type cmd struct {
Command string `json:"command"`
Protocols []string `json:"protocols"`
}
var protocols = []string{
"http://livereload.com/protocols/official-7",
"http://livereload.com/protocols/official-8",
"http://livereload.com/protocols/official-9",
"http://livereload.com/protocols/2.x-origin-version-negotiation",
"http://livereload.com/protocols/2.x-remote-control",
}
// Upgrade the connection
func upgrade(ctx context.Context, w http.ResponseWriter, r *http.Request) (*conn, error) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return nil, err
}
eg, ctx := errgroup.WithContext(ctx)
conn := &conn{
conn: c,
actionc: make(chan func() (json.RawMessage, error)),
ctx: ctx,
eg: eg,
ready: false,
}
// read from the browser
eg.Go(func() error {
for {
msgType, reader, err := c.NextReader()
if err != nil {
return err
}
// Close if binary instead of text
if msgType == websocket.BinaryMessage {
return errors.New("text message required recieved a binary message")
}
// Close if it's not JSON
command := &cmd{}
err = json.NewDecoder(reader).Decode(command)
if err != nil {
return errors.Wrapf(err, "error decoding command")
}
// ignore empty commands
if command.Command == "" {
return fmt.Errorf("server command shouldn't be empty")
}
// ensure we're ready to go
if !conn.ready {
if valid := validateHello(command); !valid {
return fmt.Errorf("invalid handshake")
}
conn.ready = true
log.Debugf("ready!")
}
}
})
// write to the browser
eg.Go(func() error {
for {
select {
case fn := <-conn.actionc:
// if !conn.ready {
// return fmt.Errorf("bad handshake")
// }
buf, err := fn()
if err != nil {
return errors.Wrapf(err, "error running fn")
}
log.Debugf("sending: %s", buf)
c.SetWriteDeadline(time.Now().Add(5 * time.Second))
if err := c.WriteJSON(buf); err != nil {
return errors.Wrapf(err, "error writing JSON")
}
case <-ctx.Done():
return ctx.Err()
}
}
})
// kick us off
conn.actionc <- func() (json.RawMessage, error) {
return json.Marshal(struct {
Command string `json:"command"`
Protocols []string `json:"protocols"`
ServerName string `json:"serverName"`
}{
Command: "hello",
Protocols: protocols,
ServerName: "joy",
})
}
return conn, nil
}
// conn is a single websocket connection
type conn struct {
actionc chan func() (json.RawMessage, error)
conn *websocket.Conn
ctx context.Context
eg *errgroup.Group
ready bool
}
// Reload a file
func (c *conn) Reload(file string) {
c.actionc <- func() (json.RawMessage, error) {
return json.Marshal(struct {
Command string `json:"command,omitempty"`
Path string `json:"path,omitempty"`
LiveCSS bool `json:"live_css,omitempty"`
}{
Command: "reload",
Path: file,
LiveCSS: true,
})
}
}
// Wait until the connection is over
func (c *conn) Wait() error {
return c.eg.Wait()
}
// validate the client's handshake
func validateHello(cmd *cmd) bool {
if cmd.Command != "hello" {
return false
}
for _, c := range cmd.Protocols {
for _, s := range protocols {
if c == s {
return true
}
}
}
return false
}