-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcapabilities.go
187 lines (168 loc) · 3.69 KB
/
capabilities.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package prpl
import (
"strconv"
"strings"
"github.com/ua-parser/uap-go/uaparser"
)
type (
userAgentPredicate func(client *uaparser.Client) bool
userAgentCapabilities struct {
es2015 userAgentPredicate
push userAgentPredicate
serviceworker userAgentPredicate
}
)
var browserPredicates = map[string]userAgentCapabilities{
"Chrome": {
es2015: since(49),
push: since(41),
serviceworker: since(45),
},
"Chromium": {
es2015: since(49),
push: since(41),
serviceworker: since(45),
},
"OPR": {
es2015: since(36),
push: since(28),
serviceworker: since(32),
},
"Vivaldi": {
es2015: since(1),
push: since(1),
serviceworker: since(1),
},
"Mobile Safari": {
es2015: since(10),
push: since(9, 2),
serviceworker: notyet,
},
"Safari": {
es2015: since(10),
push: func(client *uaparser.Client) bool {
return versionAtLeast(parseVersion(client.UserAgent), 9) &&
// HTTP/2 on desktop Safari requires macOS 10.11 according to
// caniuse.com.
versionAtLeast(parseVersion(client.Os), 10, 11)
},
// https://webkit.org/status/#specification-service-workers
serviceworker: notyet,
},
"Edge": {
// Edge versions before 15.15063 may contain a JIT bug affecting ES6
// constructors (https://github.com/Microsoft/ChakraCore/issues/1496).
es2015: since(15, 15063),
push: since(12),
// https://developer.microsoft.com/en-us/microsoft-edge/platform/status/serviceworker/
serviceworker: notyet,
},
"Firefox": {
es2015: since(51),
push: since(36),
serviceworker: since(44),
},
}
type capability int
const (
es2015 capability = 1 << iota
push
serviceworker
)
func newCapabilities(browserCapabilities []string) capability {
var capability capability
for _, c := range browserCapabilities {
switch c {
case "es2015":
capability += es2015
case "push":
capability += push
case "serviceworker":
capability += serviceworker
}
}
return capability
}
func (c capability) size() int {
size := 0
if c&es2015 == es2015 {
size++
}
if c&push == push {
size++
}
if c&serviceworker == serviceworker {
size++
}
return size
}
func (c capability) String() string {
val := []string{}
if c&es2015 == es2015 {
val = append(val, "es2015")
}
if c&push == push {
val = append(val, "push")
}
if c&serviceworker == serviceworker {
val = append(val, "serviceworker")
}
return strings.Join(val, ", ")
}
func (p *prpl) browserCapabilities(userAgentString string) capability {
client := p.parser.Parse(userAgentString)
predicate, ok := browserPredicates[client.UserAgent.Family]
if !ok {
return 0
}
var capabilities capability
if predicate.es2015(client) {
capabilities += es2015
}
if predicate.push(client) {
capabilities += push
}
if predicate.serviceworker(client) {
capabilities += serviceworker
}
return capabilities
}
var notyet = func(client *uaparser.Client) bool {
return false
}
func since(atLeast ...int) userAgentPredicate {
return func(client *uaparser.Client) bool {
version := parseVersion(client.UserAgent)
return versionAtLeast(version, atLeast...)
}
}
func versionAtLeast(version []int, atLeast ...int) bool {
for i, r := range atLeast {
var v int
if len(version) > i {
v = version[i]
}
if v > r {
return true
}
if v < r {
return false
}
}
return true
}
type versioned interface {
ToVersionString() string
}
func parseVersion(version versioned) []int {
parts := strings.Split(version.ToVersionString(), ".")
values := make([]int, len(parts))
for i, part := range parts {
value, err := strconv.Atoi(part)
if err != nil {
value = -1
}
values[i] = value
}
return values
}