2017-08-30 16:31:51 +03:00
|
|
|
const Server = require('karma').Server;
|
|
|
|
const cfg = require('karma').config;
|
2017-08-28 16:27:39 +03:00
|
|
|
const path = require('path');
|
2017-09-04 18:36:19 +03:00
|
|
|
const proxy = require('html2canvas-proxy');
|
2017-08-30 16:31:51 +03:00
|
|
|
const karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'));
|
|
|
|
const server = new Server(karmaConfig, (exitCode) => {
|
|
|
|
console.log('Karma has exited with ' + exitCode);
|
|
|
|
process.exit(exitCode)
|
|
|
|
});
|
|
|
|
|
2017-08-28 16:27:39 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
2017-08-30 16:31:51 +03:00
|
|
|
const cors = require('cors');
|
2017-08-28 16:27:39 +03:00
|
|
|
const filenamifyUrl = require('filenamify-url');
|
|
|
|
|
2019-04-07 09:07:52 +03:00
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
const screenshotFolder = './tmp/reftests';
|
2019-04-13 09:17:23 +03:00
|
|
|
const metadataFolder = './tmp/reftests/metadata';
|
2019-04-07 09:07:52 +03:00
|
|
|
|
|
|
|
mkdirp.sync(path.resolve(__dirname, screenshotFolder));
|
2019-04-13 09:17:23 +03:00
|
|
|
mkdirp.sync(path.resolve(__dirname, metadataFolder));
|
2019-04-07 09:07:52 +03:00
|
|
|
|
2017-09-03 16:25:06 +03:00
|
|
|
const CORS_PORT = 8081;
|
|
|
|
const corsApp = express();
|
2017-09-04 18:36:19 +03:00
|
|
|
corsApp.use('/proxy', proxy());
|
|
|
|
corsApp.use('/cors', cors(), express.static(path.resolve(__dirname)));
|
|
|
|
corsApp.use('/', express.static(path.resolve(__dirname, '/tests')));
|
2017-09-03 16:25:06 +03:00
|
|
|
corsApp.use((error, req, res, next) => {
|
|
|
|
console.error(error);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err) => {
|
|
|
|
if(err.errno === 'EADDRINUSE') {
|
|
|
|
console.warn(err);
|
|
|
|
} else {
|
|
|
|
console.log(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
corsApp.listen(CORS_PORT, () => {
|
|
|
|
console.log(`CORS server running on port ${CORS_PORT}`);
|
|
|
|
});
|
|
|
|
|
2017-08-28 16:27:39 +03:00
|
|
|
const app = express();
|
2017-08-30 16:31:51 +03:00
|
|
|
app.use(cors());
|
2017-09-03 16:25:06 +03:00
|
|
|
app.use((req, res, next) => {
|
2017-08-30 16:31:51 +03:00
|
|
|
// IE9 doesn't set headers for cross-domain ajax requests
|
|
|
|
if(typeof(req.headers['content-type']) === 'undefined'){
|
|
|
|
req.headers['content-type'] = "application/json";
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
2017-08-28 16:27:39 +03:00
|
|
|
app.use(
|
|
|
|
bodyParser.json({
|
|
|
|
limit: '15mb',
|
|
|
|
type: '*/*'
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const prefix = 'data:image/png;base64,';
|
|
|
|
|
|
|
|
const writeScreenshot = (buffer, body) => {
|
|
|
|
const filename = `${filenamifyUrl(
|
|
|
|
body.test.replace(/^\/tests\/reftests\//, '').replace(/\.html$/, ''),
|
|
|
|
{replacement: '-'}
|
2019-04-13 09:17:23 +03:00
|
|
|
)}!${[process.env.TARGET_BROWSER, body.platform.name, body.platform.version].join('-')}`;
|
2017-08-28 16:27:39 +03:00
|
|
|
|
2019-04-13 09:17:23 +03:00
|
|
|
fs.writeFileSync(path.resolve(__dirname, screenshotFolder, `${filename}.png`), buffer);
|
|
|
|
return filename;
|
2017-08-28 16:27:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
app.post('/screenshot', (req, res) => {
|
|
|
|
if (!req.body || !req.body.screenshot) {
|
|
|
|
return res.sendStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
const buffer = new Buffer(req.body.screenshot.substring(prefix.length), 'base64');
|
2019-04-13 09:17:23 +03:00
|
|
|
const filename = writeScreenshot(buffer, req.body);
|
|
|
|
fs.writeFileSync(path.resolve(__dirname, metadataFolder, `${filename}.json`), JSON.stringify({
|
|
|
|
windowWidth: req.body.windowWidth,
|
|
|
|
windowHeight: req.body.windowHeight,
|
|
|
|
platform: req.body.platform,
|
|
|
|
devicePixelRatio: req.body.devicePixelRatio,
|
|
|
|
test: req.body.test,
|
|
|
|
id: process.env.TARGET_BROWSER,
|
|
|
|
screenshot: filename
|
|
|
|
}));
|
2017-08-28 16:27:39 +03:00
|
|
|
return res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use((error, req, res, next) => {
|
|
|
|
console.error(error);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2017-09-03 16:25:06 +03:00
|
|
|
const listener = app.listen(8000, () => {
|
2017-08-30 16:31:51 +03:00
|
|
|
server.start();
|
2017-08-28 16:27:39 +03:00
|
|
|
});
|
2017-08-30 16:31:51 +03:00
|
|
|
|
|
|
|
|