-
Notifications
You must be signed in to change notification settings - Fork 4
/
wpt-utils.js
142 lines (130 loc) · 3.9 KB
/
wpt-utils.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
const path = require('path');
const fs = require('fs-extra');
const webpagetest = require('webpagetest');
const {NODE_ENV} = process.env;
if (NODE_ENV == "development") {
require('dotenv').config();
}
const defaultServer = process.env.WPT_SERVER;
const wptAPIkey = process.env.WPT_APIKEY;
getStatus = function(testId) {
return new Promise((resolve)=>{
const wpt = new webpagetest();
wpt.getTestStatus(testId, {request: 12345}, (err,data) => {
if (err) throw new Error(err);
return resolve(data);
});
});
};
submitTest = function(url,server=null,location=null,opts=null) {
return new Promise((resolve) => {
server = (server==null?defaultServer:server);
let iq = 100;
const wpt = new webpagetest(server);
if (opts) {
let iq = (opts.iq==undefined?100:opts.iq);
}
wpt.runTest(url, {
location:location,
disableOptimization:true,
video:true,
key:wptAPIkey,
firstViewOnly:true,
runs:3,
medianVideo:true,
timeline:true
}, (err, data) => {
if (err) throw new Error(err);
return resolve(data);
});
});
};
submitBlockTest = function(url,block,server=null,location=null,opts=null) {
return new Promise((resolve) => {
server = (server==null?defaultServer:server);
let iq = 100;
const wpt = new webpagetest(server);
if (opts) {
let iq = (opts.iq==undefined?100:opts.iq);
}
wpt.runTest(url, {
location:location,
disableOptimization:true,
video:true,
key:wptAPIkey,
firstViewOnly:true,
runs:3,
block:block,
label:"blocked: "+block,
medianVideo:true,
timeline:true
}, (err, data) => {
if (err) throw new Error(err);
return resolve({"blocked":block,"testId":data.data.testId});
});
});
};
combinate = function(arr,sep=" ") {
var combi = [];
var temp = "";
var combiLen = Math.pow(2, arr.length);
for (var i = 0; i < combiLen ; i++){
temp = "";
for (var j=0;j<arr.length;j++) {
if ((i & Math.pow(2,j))){
temp += arr[j] + sep;
}
}
if (temp !== "") {
combi.push(temp);
}
}
return combi;
};
submitBlockTests = function(data) {
server = (data.server==null?defaultServer:data.server);
if (typeof(data["selected-domains"]) !== "object") {
data["selected-domains"] = [data["selected-domains"]];
}
//var combi = combinate(data["selected-domains"]);
var combi = data["selected-domains"];
var tests = [];
combi.forEach((domain) => {
tests.push(submitBlockTest(data["original-url"],domain,server,data.location,null));
});
return Promise.all(tests);
};
getResultSummary = function(testId,server=null,requests=1) {
return new Promise((resolve) => {
let opts = {};
opts.requests = requests;
server = (server==null?defaultServer:server);
const wpt = new webpagetest(server);
wpt.getTestResults(testId, opts=opts, (err,data) => {
if (err) throw new Error(err);
return resolve(data);
});
});
};
getLocations = function(server) {
return new Promise((resolve) => {
const wpt = new webpagetest(server);
wpt.getLocations((err,data) => {
if (err) throw new Error(err);
return resolve(data);
});
});
};
analyzeTestData = function(testId,testDir,server=null) {
return new Promise((resolve,reject) => {
return resolve(true);
});
};
return module.exports = {
getStatus: getStatus,
submitTest: submitTest,
getLocations: getLocations,
getResultSummary,
analyzeTestData,
submitBlockTests
};