/
messages.go
130 lines (110 loc) · 2.82 KB
/
messages.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
package messages
import (
"encoding/json"
"fmt"
"reflect"
"github.com/dave/jsgo/server/servermsg"
"github.com/dave/services"
"github.com/dave/services/builder/buildermsg"
"github.com/dave/services/constor/constormsg"
"github.com/dave/services/deployer/deployermsg"
"github.com/dave/services/getter/gettermsg"
"github.com/gorilla/websocket"
)
var payloads = []interface{}{
// Progress messages:
servermsg.Queueing{},
gettermsg.Downloading{},
constormsg.Storing{},
buildermsg.Building{},
// Data messages:
servermsg.Error{},
ShareComplete{},
GetComplete{},
DeployComplete{},
deployermsg.Archive{},
deployermsg.ArchiveIndex{},
// Commands:
Update{},
Share{},
Get{},
Deploy{},
Initialise{},
}
type DeployComplete struct {
Main string
Index string
}
// Update is sent by the client to the server asking it to compile the source and return the archive
// files for all dependencies that are not found in the client cache.
type Update struct {
Source map[string]map[string]string // Source packages for this build: map[<package>]map[<filename>]<contents>
Tags []string // Build tags
Cache map[string]string // Map of path->hash of previously compiled dependencies to use if still in the cache
Minify bool
}
// Share is sent by the client to persist the setup on the server.
type Share struct {
Source map[string]map[string]string
Tags []string
}
type Deploy struct {
Main string
Imports []string
Source map[string]map[string]string // Source packages for this build: map[<package>]map[<filename>]<contents>
Tags []string
}
// Initialise is sent by the client to get the source at Path, and update.
type Initialise struct {
Path string
Minify bool
}
// Get is sent by the client to the server asking it to download a package and return the source.
type Get struct {
Path string
}
type GetComplete struct {
Source map[string]map[string]string
}
type ShareComplete struct {
Hash string
}
func Marshal(in services.Message) ([]byte, int, error) {
m := struct {
Type string
Message services.Message
}{
Type: reflect.TypeOf(in).Name(),
Message: in,
}
b, err := json.Marshal(m)
if err != nil {
return nil, 0, err
}
return b, websocket.TextMessage, nil
}
func Unmarshal(in []byte) (services.Message, error) {
var m struct {
Type string
Message json.RawMessage
}
if err := json.Unmarshal(in, &m); err != nil {
return nil, err
}
typ, ok := payloadTypes[m.Type]
if !ok {
return nil, fmt.Errorf("type not found: %s, %#v", m.Type, m)
}
pointer := reflect.New(typ)
if err := json.Unmarshal(m.Message, pointer.Interface()); err != nil {
return nil, err
}
return pointer.Elem().Interface(), nil
}
func init() {
for _, i := range payloads {
t := reflect.TypeOf(i)
payloadTypes[t.Name()] = t
}
}
var payloadTypes = make(map[string]reflect.Type)