-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (104 loc) · 2.98 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
const express = require('express');
const http = require('http');
const bb = require('express-busboy');
const startRun = require('./lib/startRun');
const share = require('./lib/share');
const WebSocket = require('ws');
const mustacheExpress = require('mustache-express');
const defaults = require('./default-test');
const archiver = require('archiver');
const createTempDirectory = require('./lib/createTempDirectory');
const app = express();
app.engine('mustache', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
bb.extend(app);
function sendMsg (ws, data) {
let msg = data.toString('utf8')
if (msg.indexOf('Starting docker_selenium') === -1) {
ws.send(data.toString('utf8'));
}
}
async function runTest (testDeets, ws) {
if (testDeets.baseUrl.match(/file:\/\//g)) {
ws.send('Error: Attempting to access file:// resources.');
ws.send('Tests completed!');
return;
}
try {
let {runId, run} = await startRun(testDeets.baseUrl, testDeets.code);
run.stdout.on('data', sendMsg.bind(this, ws));
run.stderr.on('data', sendMsg.bind(this, ws));
run.on('close', (code) => {
if (code !== 0) {
// save broken screenshot?
}
// save data to database?
if (ws) {
ws.send('Tests completed!');
}
});
ws.send('Tests started');
ws.onclose = () => {
console.log('client connection closed');
run.stdin.pause();
run.stderr.pause();
run.kill();
ws = null;
}
} catch (err) {
console.log(err.stack);
}
}
// save/share link
app.post('/save', async function (req, res) {
testDeets = {
baseUrl: req.body.baseUrl,
file: req.body.code
}
// store to DB
let id = await share.create(testDeets);
// respond with ID of store
res.send(id)
});
app.post('/download', async function (req, res) {
testDeets = {
baseUrl: req.body.baseUrl,
file: req.body.code
}
// generate directory
let dir = createTempDirectory(req.body.baseUrl, req.body.code);
const archive = archiver('zip');
archive.on('error', function(err) {
res.status(500).send({error: err.message});
});
res.attachment('wdio.zip');
archive.pipe(res);
// zip directory
archive.directory(dir, false);
archive.finalize();
})
app.get('/', async function(req, res, next) {
res.render('index', defaults);
});
app.get('/share/:tag', async function(req, res, next) {
// // load json data from db via ID
let content = await share.load(req.params.tag);
res.render('index', content[0]);
});
app.use(express.static('public'))
const server = http.createServer(app);
const wss = new WebSocket.Server({ server: server });
wss.on('connection', (ws, req) => {
ws.on('message', (message) => {
// is request to run test?
let testDeets = JSON.parse(message);
if (testDeets.baseUrl && testDeets.code) {
runTest(testDeets, ws);
}
});
});
//
// Start the server.
//
server.listen(3333, () => console.log('Listening on http://localhost:3333'));