forked from GoogleChromeLabs/psi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·31 lines (27 loc) · 877 Bytes
/
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
'use strict';
var Promise = require('pinkie-promise');
var googleapis = require('googleapis');
var prependHttp = require('prepend-http');
var objectAssign = require('object-assign');
var pify = require('pify');
var pagespeed = pify(googleapis.pagespeedonline('v2').pagespeedapi.runpagespeed, Promise);
var output = require('./lib/output');
function handleOpts(url, opts) {
opts = objectAssign({strategy: 'mobile'}, opts);
opts.nokey = opts.key === undefined;
opts.url = prependHttp(url);
return opts;
}
var psi = module.exports = function (url, opts) {
return Promise.resolve().then(function () {
if (!url) {
throw new Error('URL required');
}
return pagespeed(handleOpts(url, opts));
});
};
module.exports.output = function (url, opts) {
return psi(url, opts).then(function (data) {
return output(handleOpts(url, opts), data);
});
};