-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (74 loc) · 2 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
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
/*
* bouncer
* https://github.com/3rd-party-bouncer/bouncer
*
* Licensed under the MIT license.
*/
'use strict';
var Runner = require( './lib/runner' );
var EventEmitter = require( 'events' ).EventEmitter;
var util = require( 'util' );
var parseDomain = require( 'parse-domain' );
/**
* Events to be emitted to the outside
* @type {Array}
*/
var events = [ 'msg', 'error', 'debug', 'data' ];
/**
* Constructor
* @param {Object} options options
*
* @tested
*/
var Bouncer = function( options ) {
options = options || {};
if ( !options.url ) {
throw new Error( ' -> No url to run against defined <- ' );
}
options.allowedDomains = options.allowedDomains || [];
options.allowedDomains = options.allowedDomains.map( parseDomain );
this.options = {
runner : {
allowedDomains : options.allowedDomains,
url : options.url,
domain : parseDomain( options.url )
},
wpt : {
key : options.key || '',
location : options.location,
pollResults : 10,
requests : true,
runs : +options.runs || 1,
server : options.server || 'www.webpagetest.org',
video : true
}
};
this.runner = new Runner( this.options );
// pipe all events to the outer world
events.forEach( function( eventName ) {
var bouncerName = 'bouncer:' + eventName;
this.runner.on( bouncerName, function( msg ) {
this.emit( bouncerName, msg );
}.bind( this ) );
}, this );
};
util.inherits( Bouncer, EventEmitter );
/**
* Kick of the bouncer
*
* @param {Function} callback callback
*
* @tested
*/
Bouncer.prototype.run = function( callback ) {
this.emit( 'msg', 'Starting to bounce' );
this.emit( 'msg', 'Set WPT server -> ' + this.options.wpt.server );
this.emit( 'msg', 'Set URL -> ' + this.options.runner.url );
this.runner.run( function( err, data ) {
if ( err ) {
return callback( err );
}
callback( null, data );
} );
};
module.exports = Bouncer;