function WebPageTest(server, key) {
if (!(this instanceof WebPageTest)) {
return new WebPageTest(server, key);
}
this.config = helper.normalizeServer(server || WebPageTest.defaultServer);
this.config.key = key;
}n/a
function scriptToString(data) {
var script = [];
data.forEach(function dataEach(step) {
var key, value;
if (typeof step === 'string') {
script.push(step);
} else if (typeof step === 'object') {
key = [Object.keys(step)[0]];
value = step[key];
if (value !== undefined && value !== null && value !== '') {
key = key.concat(value);
}
script.push(key.join(TAB));
}
});
return script.join(NEWLINE);
}...
#### Notes
* `getWaterfallImage` and `getScreenshotImage` callback function has a third parameter `info` which is an object with `{type:
x27;image/jpeg or png', encoding: 'utf8 or binary'}`
* `scriptToString` script array values 1-N are optional. e.g:
```javascript
var script = wpt.scriptToString([
{logData: 0},
{navigate: 'http://foo.com/login'},
{logData: 1},
{setValue: ['name=username', 'johndoe']},
{setValue: ['name=password', '12345']},
{submitForm: 'action=http://foo.com/main'},
'waitForComplete'
...function WPTAPIError(code, message) {
this.name = 'WPTAPIError';
this.code = code || 0;
this.message = message || this.name;
}n/a
function csvParser(data, callback) {
csv.parse(data.toString(), { columns: true }, function(err, data) {
if (err) {
callback.bind(this, err);
}
csv.transform(data, function(row) {
var key, value;
for (key in row) {
value = row[key].replace(/<b>|<\/b>/g, '');
row[key] = entities.decode(value, 2);
}
return row;
}, callback.bind(this));
});
}n/a
csvToObj = function () { [native code] }n/a
function dataURI(data) {
return new Buffer(data || '', 'binary').toString('base64');
}n/a
function dryRun(config, path) {
path = url.parse(path, true);
return {
url: url.format({
protocol: config.protocol,
hostname: config.hostname,
port: (config.port !== 80 && config.port !== 443 ?
config.port : undefined),
pathname: path.pathname,
query: path.query
})
};
}n/a
function localhost(local, defaultPort) {
// edge case when hostname:port is not informed via cli
if (local === undefined || local === 'true') {
local = [defaultPort];
} else {
local = local.toString().split(':');
}
return {
hostname: reIp.test(local[0]) || isNaN(parseInt(local[0], 10)) &&
local[0] ? local[0] : os.hostname(),
port: parseInt(local[1] || (!reIp.test(local[0]) && local[0]), 10) ||
defaultPort
};
}n/a
function netLogParser(data) {
data = (data || '{}').toString();
if (data.slice(data.length - 3) === ',\r\n') {
data = data.slice(0, data.length - 3) + ']}';
}
return JSON.parse(data);
}n/a
function normalizeServer(server) {
// normalize hostname
if (!reProtocol.test(server)) {
server = 'http://' + server;
}
server = url.parse(server);
return {
protocol: server.protocol,
hostname: server.hostname,
auth: server.auth,
pathname: server.pathname,
port: parseInt(server.port, 10) || (server.protocol === 'https:' ? 443 : 80)
};
}n/a
function scriptToString(data) {
var script = [];
data.forEach(function dataEach(step) {
var key, value;
if (typeof step === 'string') {
script.push(step);
} else if (typeof step === 'object') {
key = [Object.keys(step)[0]];
value = step[key];
if (value !== undefined && value !== null && value !== '') {
key = key.concat(value);
}
script.push(key.join(TAB));
}
});
return script.join(NEWLINE);
}...
#### Notes
* `getWaterfallImage` and `getScreenshotImage` callback function has a third parameter `info` which is an object with `{type:
x27;image/jpeg or png', encoding: 'utf8 or binary'}`
* `scriptToString` script array values 1-N are optional. e.g:
```javascript
var script = wpt.scriptToString([
{logData: 0},
{navigate: 'http://foo.com/login'},
{logData: 1},
{setValue: ['name=username', 'johndoe']},
{setValue: ['name=password', '12345']},
{submitForm: 'action=http://foo.com/main'},
'waitForComplete'
...function setQuery(map, options, query) {
query = query || {};
options = options || {};
map.options.forEach(function eachOpts(opt) {
Object.keys(opt).forEach(function eachOpt(key) {
var param = opt[key],
name = param.name,
value = options[name] || options[key];
if (value !== undefined && param.api) {
if (param.array) {
value = [].concat(value).join(' ');
}
query[param.api] = param.bool ? param.invert ?
(value ? 0 : 1) : (value ? 1 : 0) : value;
}
});
});
return query;
}n/a
tsvToObj = function () { [native code] }n/a
function xmlToObj(xml, callback) {
parser.parseString(xml, function (err, obj) {
if (err) {
callback(err);
}
normalizeObj(obj);
callback(undefined, obj);
});
}n/a
function setOptions(command, query) {
var count, opts;
command = commands[command];
if (!command) {
return;
}
opts = {};
count = Object.keys(query).length;
[options.common].concat(command.options).some(function someTypes(options) {
if (!options) {
return;
}
Object.keys(options).some(function someOptions(key) {
var valid = options[key].valid;
if (query.hasOwnProperty(key)) {
if (options[key].bool) {
opts[options[key].name] = !reBool.test(query[key]);
} else if (!valid || (valid && valid.test(query[key]))) {
opts[options[key].name] = decodeURIComponent(query[key]);
}
count -= 1;
}
return count === 0;
});
return count === 0;
});
return opts;
}...
if (method && method !== 'listen') {
// id, url or script
if (uri.id) {
args.push(decodeURIComponent(uri.id));
}
// options
args.push(mapping.setOptions(uri.command, uri.query));
// callback
args.push(output.bind(res, callback));
this[method].apply(this, args);
} else {
// help
...function listen(local, options, callback) {
var server;
if (fs.existsSync(options.key) && fs.existsSync(options.cert)) {
server = https.createServer({
key: fs.readFileSync(path.resolve(process.cwd(), options.key)),
cert: fs.readFileSync(path.resolve(process.cwd(), options.cert))
}, route.bind(this));
} else {
server = http.createServer(route.bind(this));
}
server.on('error', function listenError(err) {
if (typeof callback === 'function') {
callback(err);
}
}).on('listening', function listenError() {
if (typeof callback === 'function') {
callback(undefined, {
server: server,
protocol: server instanceof https.Server ? 'https' : 'http',
hostname: local.hostname,
port: local.port,
url: url.format({
protocol: server instanceof https.Server ? 'https' : 'http',
hostname: local.hostname,
port: local.port
})
});
}
}).listen(local.port);
buildHelp(url.format(this.config.hostname));
return server;
}...
#### Notes
* port _8080_ is optional, default port is _7791_
* `wpt.foo.com` is overriding the default `www.webpagetest.org` server but can still be overridden with `server` option
* when _--key_ and _--cert_ are provided, HTTPS is used instead of default HTTP server
### Module
```javascript
var server = wpt.listen(8080, function(err, data) {
if (err) throw err;
console.log('listening on ' + data.url);
}); // listen on port 8080 (optional), default port is 7791
setTimeout(function() {
server.close(function() {
console.log('server closed');
...