-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
270 lines (232 loc) · 6.69 KB
/
index.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
var express = require('express')
var _ = require('lodash')
var serveStatic = require('serve-static')
var path = require('path')
var defaultFilter = {
runtimes: ['Chrome (CDP 1.3)', 'Edge 0.1', 'Edge 0.2 (preview)', 'Node (CDP 1.3)', 'Safari iOS 10.0']
}
var runtimes = [
{
name: 'Chrome (CDP 1.3)',
protocol: require('./protocols/chrome/protocol_13.json'),
icon: 'chrome.svg'
},
{
name: 'Chrome (CDP 1.2)',
protocol: require('./protocols/chrome/protocol_12.json'),
icon: 'chrome.svg'
},
{
name: 'Chrome (CDP 1.1)',
protocol: require('./protocols/chrome/protocol_11.json'),
icon: 'chrome.svg'
},
{
name: 'Edge 0.1',
protocol: require('./protocols/edge/protocol.01.json'),
icon: 'edge.svg'
},
{
name: 'Edge 0.2 (preview)',
protocol: require('./protocols/edge/protocol.02.json'),
icon: 'edge.svg'
},
{
name: 'Node (CDP 1.3)',
protocol: require('./protocols/node/protocol_13.json'),
icon: 'nodejs.svg'
},
{
name: 'Node (CDP 1.2)',
protocol: require('./protocols/node/protocol_12.json'),
icon: 'nodejs.svg'
},
{
name: 'Safari iOS 10.0',
protocol: require('./protocols/webkit/iOS-10.0.json'),
icon: 'safari-ios.svg'
},
{
name: 'Safari iOS 9.3',
protocol: require('./protocols/webkit/iOS-9.3.json'),
icon: 'safari-ios.svg'
},
{
name: 'Safari iOS 9.0',
protocol: require('./protocols/webkit/iOS-9.0.json'),
icon: 'safari-ios.svg'
},
{
name: 'Safari iOS 8.0',
protocol: require('./protocols/webkit/iOS-8.0.json'),
icon: 'safari-ios.svg'
},
{
name: 'Safari iOS 7.0',
protocol: require('./protocols/webkit/iOS-7.0.json'),
icon: 'safari-ios.svg'
}
]
var app = express()
app.engine('ejs', require('ejs-locals'))
app.set('port', process.env.PORT || 8080)
app.set('views', path.join(__dirname, '/views'))
app.set('view engine', 'ejs')
app.use(serveStatic(path.join(__dirname, '/assets')))
var handleFilters = function (req, res, next) {
req.filter = Object.assign({}, defaultFilter)
if (req.query.runtimes) {
req.filter.isDefault = false
req.filter.runtimes = req.query.runtimes.split(',')
} else {
req.filter.isDefault = true
}
next()
}
app.get('/', handleFilters, function (req, res) {
var domains = getDomains().map(d => getDomainInfo(d.name))
res.render('index', {
_layoutFile: 'layout',
domains: domains,
runtimes: runtimes,
filter: req.filter
})
})
app.get('/:domain', handleFilters, function (req, res) {
var domain = getDomainInfo(req.params.domain)
if (!domain) {
return res.render('404')
}
res.render('domain', {
_layoutFile: 'layout',
filter: req.filter,
domain: domain,
runtimes: runtimes,
commands: generateCompatibilityPairs(domain, 'commands', 'name'),
events: generateCompatibilityPairs(domain, 'events', 'name'),
types: generateCompatibilityPairs(domain, 'types', 'id')
})
})
app.get('/:domain/:runtime/:type/:object', handleFilters, function (req, res) {
var domain = getDomainInfo(req.params.domain)
if (!domain) {
return res.render('404')
}
var runtime = getRuntimeByName(req.params.runtime)
if (!runtime) {
return res.render('404')
}
var protocol = _.find(domain.runtimes, i => i.name === req.params.runtime).protocol
if (!protocol) {
return res.render('404')
}
var subProtocol = protocol[req.params.type]
if (!subProtocol) {
return res.render('404')
}
var object = _.find(subProtocol, i => i.name === req.params.object || i.id === req.params.object)
if (!object) {
return res.render('404')
}
res.render('object', {
_layoutFile: 'layout',
filter: req.filter,
domain: domain,
runtime: runtime,
object: object
})
})
app.listen(app.get('port'), function () {
console.log('Express server listening on port %d in %s mode', app.get('port'), app.settings.env)
})
function isPartOfCore (commands) {
return commands[0].runtime === 'RemoteDebug Core'
}
function generateCompatibilityPairs (domain, type, propertyKey) {
var runtimes = domain.runtimes || []
var runtimesCount = runtimes ? runtimes.length : 0
var commands = new Map()
runtimes.forEach(function (runtime, index) {
if (runtime.protocol && runtime.protocol[type]) {
runtime.protocol[type]
.sort(function (a, b) {
var x = a[propertyKey]
var y = b[propertyKey]
return ((x < y) ? -1 : ((x > y) ? 1 : 0))
})
.forEach(function (command) {
var cItem = {
runtime: runtime.name,
command: command
}
if (commands.has(command[propertyKey])) {
var entry = commands.get(command[propertyKey])
entry.push(cItem)
} else {
commands.set(command[propertyKey], [cItem])
}
})
}
})
var commandPairs = []
commands.forEach(function (command, key) {
var pair = _.fill(Array(runtimesCount), null)
command.forEach(function (item) {
var runtime = _.find(runtimes, r => r.name === item.runtime)
var runtimeIndex = runtimes.indexOf(runtime)
pair[runtimeIndex] = {
name: item.command[propertyKey],
object: item.command
}
})
commandPairs.push({
isPartOfCore: isPartOfCore(command, propertyKey),
pair: pair,
flat: _.compact(pair)[0]
})
})
return commandPairs
}
function getDomains () {
// Create unique list of domains
var domains = []
runtimes.forEach(function (runtime) {
var rDomains = runtime.protocol.domains.map(d => d.domain)
domains = domains.concat(rDomains)
})
domains = _.uniq(domains)
// Return collection of domains mapped to runtime protocol section
return domains.map(function (domainName) {
return {
name: domainName,
runtimes: runtimes.map(function (runtime) {
return {
name: runtime.name,
icon: runtime.icon,
protocol: getDomainForRuntime(runtime.protocol, domainName)
}
})
}
}).sort((a, b) => {
return a.name.localeCompare(b.name)
})
}
function getDomainInfo (domainName) {
var domain = getDomainByName(domainName)
var info = domain || {}
var protocols = domain && domain.runtimes && domain.runtimes.length ? _.compact(domain.runtimes.map(r => r.protocol)) : []
if (protocols.length) {
info.description = protocols[0].description
info.isExperimental = protocols[0].experimental | protocols[0].hidden
}
return info
}
function getDomainByName (name) {
return _.find(getDomains(), i => i.name === name)
}
function getRuntimeByName (name) {
return _.find(runtimes, i => i.name === name)
}
function getDomainForRuntime (protocol, name) {
return _.find(protocol.domains, item => item.domain === name)
}