-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (51 loc) · 1.29 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
'use strict';
const psi = require('psi');
const ngrok = require('ngrok');
const url = require('url');
let config = {
strategies: ['mobile', 'desktop'],
pages: ['/'],
onBeforeConnect: Function.prototype,
onSuccess: Function.prototype,
onError: Function.prototype,
port: 8000,
options: {}
};
const psiNgrok = function() {
return Promise.resolve()
.then(() => {
return Promise.resolve().then(config.onBeforeConnect);
})
.then(ngrokConnect)
.then(runPSIWithStrategy)
.then(() => { ngrok.kill() });
};
const ngrokConnect = function() {
return new Promise((resolve, reject) => {
ngrok.connect(config.port, (err, url) => {
if (err !== null) {
reject(err);
}
config.url = url;
resolve(url);
});
});
};
const runPSIWithStrategy = function() {
return Promise.all(config.strategies.map(strategy => {
return runPSI(Object.assign({strategy: strategy}, config.options));
}));
};
const runPSI = function(options) {
return Promise.all(config.pages.map(page => {
return psi.output(url.resolve(config.url, page), options).then((data) => {
config.onSuccess(data);
}).catch(err => {
config.onError(err.message);
});
}));
};
module.exports = function(params) {
Object.assign(config, params);
return psiNgrok();
};