1
- const jsdom = require ( 'jsdom' ) ;
2
- const argv = require ( 'yargs' ) . argv ;
3
- const fs = require ( 'fs' ) ;
4
- const _ = require ( 'lodash' ) ;
5
- const request = require ( 'request' ) ;
6
- const jsonfile = require ( 'jsonfile' ) ;
7
- const logger = require ( " ./utils/logger" ) ;
1
+ import jsdom from 'jsdom' ;
2
+ import yargs from 'yargs' ;
3
+ import fs from 'fs' ;
4
+ import _ from 'lodash' ;
5
+ import request from 'request' ;
6
+ import jsonfile from 'jsonfile' ;
7
+ import logger from ' ./utils/logger' ;
8
8
9
9
let multiplier = 0 ;
10
10
let IFTTTParams = { } ;
11
- let IFTTTTimers = { } ;
12
- const { endpoints, interval, ifttt, random } = argv ;
11
+ const IFTTTTimers = { } ;
12
+ const { endpoints, interval, ifttt, random } = yargs . argv ;
13
13
const options = {
14
- selector : 'body :not(script) ' ,
14
+ selector : 'body' ,
15
15
jQuerySrc : 'http://code.jquery.com/jquery.js' ,
16
16
defaultTimeout : 10 ,
17
17
defaultRandom : 20
18
18
} ;
19
19
20
20
// Ensure we have required arguments
21
- if ( ! _ . isString ( endpoints ) || ! _ . isInteger ( interval ) || interval < 0 ) {
22
- console . error ( '--endpoints and --interval are required' ) ;
21
+ if ( ! _ . isString ( endpoints ) || ! _ . isInteger ( interval ) || interval < 0 ) {
22
+ logger . error ( '--endpoints and --interval are required' ) ;
23
23
process . exit ( 1 ) ;
24
24
}
25
25
26
26
// Ensure list of endpoints is a file
27
- if ( ! fs . statSync ( endpoints ) . isFile ( ) ) {
28
- console . error ( '--endpoints should refer to a file (list of endpoints)' ) ;
27
+ if ( ! fs . statSync ( endpoints ) . isFile ( ) ) {
28
+ logger . error ( '--endpoints should refer to a file (list of endpoints)' ) ;
29
29
process . exit ( 2 ) ;
30
30
}
31
31
32
32
// Ensure IFTTT configuration is valid
33
- if ( ifttt ) {
34
- if ( ! fs . statSync ( ifttt ) . isFile ( ) ) {
35
- console . error ( '--ifttt should refer to a JSON file configuration' ) ;
33
+ if ( ifttt ) {
34
+ if ( ! fs . statSync ( ifttt ) . isFile ( ) ) {
35
+ logger . error ( '--ifttt should refer to a JSON file configuration' ) ;
36
36
process . exit ( 5 ) ;
37
37
}
38
38
39
39
const { key, eventName, bodyKey } = IFTTTParams = jsonfile . readFileSync ( ifttt ) ;
40
40
41
- if ( ! key || ! eventName || ! bodyKey || ! _ . isString ( key ) || ! _ . isString ( eventName ) || ! _ . isString ( bodyKey ) ) {
42
- console . error ( '--ifttt file is missing required data' ) ;
41
+ if ( ! _ . isString ( key ) || ! _ . isString ( eventName ) || ! _ . isString ( bodyKey ) ) {
42
+ logger . error ( '--ifttt file is missing required data' ) ;
43
43
process . exit ( 6 ) ;
44
44
}
45
45
}
46
46
47
47
// Ensure random argument is valid
48
- if ( random ) {
49
- if ( _ . isBoolean ( random ) ) {
48
+ if ( random ) {
49
+ if ( _ . isBoolean ( random ) ) {
50
50
multiplier = options . defaultRandom ;
51
- } else if ( ( _ . isInteger ( random ) && ( random < 0 || random > 99 ) || ! _ . isInteger ( random ) ) ) {
52
- console . error ( '--random must be an integer from 0 to 99' ) ;
51
+ } else if ( ( _ . isInteger ( random ) && ( random < 0 || random > 99 ) || ! _ . isInteger ( random ) ) ) {
52
+ logger . error ( '--random must be an integer from 0 to 99' ) ;
53
53
process . exit ( 7 ) ;
54
54
} else {
55
55
multiplier = random ;
@@ -58,20 +58,20 @@ if(random) {
58
58
59
59
// Make requests to endpoints
60
60
const makeRequests = ( urls , callback ) => {
61
- let responses = { } ;
61
+ const responses = { } ;
62
62
let complete = 0 ;
63
63
64
64
urls . forEach ( ( url ) => {
65
65
jsdom . env ( {
66
- url : url ,
66
+ url,
67
67
scripts : [ options . jQuerySrc ] ,
68
- done : ( err , window ) => {
69
- if ( ! window || ! window . $ || err ) {
70
- console . error ( `Resource data located at ${ url } failed to load` ) ;
68
+ done ( err , window ) {
69
+ if ( ! window || ! window . $ || err ) {
70
+ logger . error ( `Resource data located at ${ url } failed to load` ) ;
71
71
} else {
72
72
const $ = window . $ ;
73
73
74
- $ ( options . selector ) . each ( function ( ) {
74
+ $ ( options . selector ) . each ( ( ) => {
75
75
const responseText = $ ( this ) . text ( ) . replace ( / \W + / g, '' ) ;
76
76
77
77
responses [ url ] = responseText ;
@@ -80,7 +80,7 @@ const makeRequests = (urls, callback) => {
80
80
81
81
complete ++ ;
82
82
83
- if ( complete === urls . length ) {
83
+ if ( complete === urls . length ) {
84
84
callback ( responses ) ;
85
85
}
86
86
}
@@ -94,72 +94,73 @@ const postIFTTT = (data) => {
94
94
const timeout = ( _ . isInteger ( IFTTTParams . timeout ) ? IFTTTParams . timeout : options . defaultTimeout ) ;
95
95
96
96
// Ensure enough time has passed since last time an event was dispatched
97
- if ( ! IFTTTTimers [ data ] || now - IFTTTTimers [ data ] > timeout ) {
98
- let postData = { } ;
97
+ if ( ! IFTTTTimers [ data ] || now - IFTTTTimers [ data ] > timeout ) {
98
+ const postData = { } ;
99
99
100
100
postData [ IFTTTParams . bodyKey ] = data ;
101
101
IFTTTTimers [ data ] = now ;
102
102
103
103
request . post ( {
104
104
url : `https://maker.ifttt.com/trigger/${ IFTTTParams . eventName } /with/key/${ IFTTTParams . key } ` ,
105
105
form : postData
106
- } , ( err , response ) => {
107
- if ( err ) {
108
- console . log ( '- IFTTT event dispatch failed' ) ;
106
+ } , ( err ) => {
107
+ if ( err ) {
108
+ logger . info ( '- IFTTT event dispatch failed' ) ;
109
109
} else {
110
- console . log ( '- IFTTT event dispatched' ) ;
110
+ logger . info ( '- IFTTT event dispatched' ) ;
111
111
}
112
112
} ) ;
113
113
} else {
114
- console . log ( '- IFTTT event ignored due to timeout' ) ;
114
+ logger . info ( '- IFTTT event ignored due to timeout' ) ;
115
115
}
116
116
} ;
117
117
118
118
// Read endpoints file and create list of endpoints
119
119
fs . readFile ( endpoints , 'utf-8' , ( err , data ) => {
120
- if ( err ) {
121
- console . error ( '--endpoints file could not be read' ) ;
120
+ if ( err ) {
121
+ logger . error ( '--endpoints file could not be read' ) ;
122
122
process . exit ( 3 ) ;
123
123
}
124
124
125
- const endpointsList = _ . remove ( data . split ( '\n' ) , ( item ) => {
126
- return _ . isString ( item ) && ! _ . isEmpty ( item ) ;
127
- } ) ;
125
+ let endpointsList = data . split ( '\n' ) ;
126
+
127
+ endpointsList = _ . filter ( endpointsList , ( item ) => _ . isString ( item ) && ! _ . isEmpty ( item ) ) ;
128
128
129
- if ( ! endpointsList . length ) {
130
- console . error ( '--endpoints file does not contain any endpoints' ) ;
129
+ if ( ! endpointsList . length ) {
130
+ logger . error ( '--endpoints file does not contain any endpoints' ) ;
131
131
process . exit ( 4 ) ;
132
132
}
133
133
134
134
// Cache initial endpoint responses
135
- makeRequests ( endpointsList , ( responses ) => {
136
- const cache = responses ;
135
+ makeRequests ( endpointsList , ( initialResponses ) => {
136
+ const cache = initialResponses ;
137
137
138
- console . log ( `${ _ . keys ( responses ) . length } of ${ endpointsList . length } responses cached` ) ;
138
+ logger . info ( `${ _ . keys ( cache ) . length } of ${ endpointsList . length } responses cached` ) ;
139
139
140
140
( function loop ( ) {
141
- const random = ( Math . random ( ) * multiplier ) / 100 ;
141
+ // Apply randomness to timing
142
+ const rand = ( Math . random ( ) * multiplier ) / 100 ;
142
143
const plusOrMinus = ( Math . random ( ) < 0.5 ? - 1 : 1 ) ;
143
- const timer = Math . floor ( ( interval + plusOrMinus * ( random * interval ) ) * 1000 ) ;
144
+ const timer = Math . floor ( ( interval + plusOrMinus * ( rand * interval ) ) * 1000 ) ;
144
145
145
146
setTimeout ( ( ) => {
146
147
makeRequests ( endpointsList , ( responses ) => {
147
148
const differences = _ . difference ( _ . values ( responses ) , _ . values ( cache ) ) ;
148
149
149
- if ( differences . length ) {
150
+ if ( differences . length ) {
150
151
differences . forEach ( ( difference ) => {
151
152
const endpoint = _ . invert ( responses ) [ difference ] ;
152
153
153
154
cache [ endpoint ] = difference ;
154
155
155
- console . log ( `Difference identified within ${ endpoint } ` ) ;
156
+ logger . info ( `Difference identified within ${ endpoint } ` ) ;
156
157
157
- if ( ifttt ) {
158
+ if ( ifttt ) {
158
159
postIFTTT ( endpoint ) ;
159
160
}
160
161
} ) ;
161
162
} else {
162
- console . log ( `No differences identified for ${ _ . keys ( responses ) . length } responses` )
163
+ logger . info ( `No differences identified for ${ _ . keys ( responses ) . length } responses` ) ;
163
164
}
164
165
165
166
loop ( ) ;
0 commit comments