| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | var utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | var _ = utils._; | 
					
						
							|  |  |  | var _s = utils._s; | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | var setDefaults = function(req) { | 
					
						
							|  |  |  |     req.clipRectangle = checkObject('clipRectangle', req.clipRectangle, {top: 0, left: 0, width: 0, height: 0}); | 
					
						
							|  |  |  |     req.cookies = checkArray('cookies', req.cookies, []); | 
					
						
							|  |  |  |     req.customRequestHeaders = checkArray('customRequestHeaders', req.customRequestHeaders, []); | 
					
						
							|  |  |  |     req.delayTime = checkNumber('delayTime', req.delayTime, 0, 300000, 0); | 
					
						
							|  |  |  |     req.execScripts = checkObject('execScripts', req.execScripts, {preInjected: [], postInjected: []}); | 
					
						
							|  |  |  |     req.injectedScripts = checkArray('injectedScripts', req.injectedScripts, []); | 
					
						
							|  |  |  |     req.isDebug = checkBoolean('isDebug', req.isDebug, false); | 
					
						
							|  |  |  |     req.loadImages = checkBoolean('loadImages', req.loadImages, false); | 
					
						
							|  |  |  |     req.outputAsJson = checkBoolean('outputAsJson', req.outputAsJson, true); //FIXME: it is ignored
 | 
					
						
							| 
									
										
										
										
											2014-12-30 11:31:19 +08:00
										 |  |  |     req.requestId = checkString('requestId', utils.guid()); // ignore user input
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  |     req.requestType = checkString('requestType', req.requestType, 'text'); | 
					
						
							|  |  |  |     req.resourceUrlBlacklist = checkArray('resourceUrlBlacklist', req.resourceUrlBlacklist, []); //HINT: set it to ['.*\.css.*'] to block stylesheet
 | 
					
						
							|  |  |  |     req.resourceUrlWhitelist = checkArray('resourceUrlWhitelist', req.resourceUrlWhitelist, []); //FIXME: not implemented
 | 
					
						
							|  |  |  |     req.scrollTo = checkObject('scrollTo', req.scrollTo, {top: 0, left: 0}); //FIXME: it doesn't work as expected
 | 
					
						
							|  |  |  |     req.targetContent = checkString('targetContent', req.targetContent, ''); | 
					
						
							|  |  |  |     req.targetUrl = checkString('targetUrl', req.targetUrl); // required
 | 
					
						
							|  |  |  |     req.timeout = checkNumber('timeout', req.timeout, 1000, 300000, 60000); | 
					
						
							| 
									
										
										
										
											2015-02-03 14:49:35 +08:00
										 |  |  |     req.userAgent = checkString('userAgent', req.userAgent, 'Mozilla/5.0 WebKit'); | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  |     req.viewportSize = checkObject('viewportSize', req.viewportSize, {width: 800, height: 600, zoomFactor: 1}); | 
					
						
							| 
									
										
										
										
											2015-01-15 13:01:04 +08:00
										 |  |  |     req.renderQuality = checkNumber('renderQuality', req.renderQuality, 0, 100, 50); | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  |     return req; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | function checkNumber(name, value, min, max, defaultValue) { | 
					
						
							|  |  |  |     if(_.isUndefined(value)) { | 
					
						
							|  |  |  |         if(_.isUndefined(defaultValue)) { | 
					
						
							|  |  |  |             throw _s.sprintf('ValueError: `%s` is undefined', name); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return defaultValue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else if(!_.isNumber(value)) { | 
					
						
							|  |  |  |         throw _s.sprintf('TypeError: `%s` is not a number', name); | 
					
						
							|  |  |  |     } else if(value < min || value > max) { | 
					
						
							| 
									
										
										
										
											2015-01-15 13:01:04 +08:00
										 |  |  |         throw _s.sprintf('ValueError: `%s` is out of range [%d, %d]', name, min, max); | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         return value; | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | function checkBoolean(name, value, defaultValue) { | 
					
						
							|  |  |  |     if(_.isUndefined(value)) { | 
					
						
							|  |  |  |         if(_.isUndefined(defaultValue)) { | 
					
						
							|  |  |  |             throw _s.sprintf('ValueError: `%s` is undefined', name); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return defaultValue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else if(!_.isBoolean(value)) { | 
					
						
							|  |  |  |         throw _s.sprintf('TypeError: `%s` is not a boolean', name); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return value; | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | function checkString(name, value, defaultValue) { | 
					
						
							|  |  |  |     if(_.isUndefined(value)) { | 
					
						
							|  |  |  |         if(_.isUndefined(defaultValue)) { | 
					
						
							|  |  |  |             throw _s.sprintf('ValueError: `%s` is undefined', name); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return defaultValue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else if(!_.isString(value)) { | 
					
						
							|  |  |  |         throw _s.sprintf('TypeError: `%s` is not a string', name); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return value; | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | function checkArray(name, value, defaultValue) { | 
					
						
							|  |  |  |     if(_.isUndefined(value)) { | 
					
						
							|  |  |  |         if(_.isUndefined(defaultValue)) { | 
					
						
							|  |  |  |             throw _s.sprintf('ValueError: `%s` is undefined', name); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return defaultValue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else if(!_.isArray(value)) { | 
					
						
							|  |  |  |         throw _s.sprintf('TypeError: `%s` is not a array', name); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         return value; | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | function checkObject(name, value, defaultValue) { | 
					
						
							|  |  |  |     if(_.isUndefined(value)) { | 
					
						
							|  |  |  |         if(_.isUndefined(defaultValue)) { | 
					
						
							|  |  |  |             throw _s.sprintf('ValueError: `%s` is undefined', name); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return defaultValue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else if(!_.isObject(value)) { | 
					
						
							|  |  |  |         throw _s.sprintf('TypeError: `%s` is not a object', name); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         _.defaults(value, defaultValue); | 
					
						
							|  |  |  |         return value; | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 14:58:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-24 19:30:14 +08:00
										 |  |  | module.exports.setDefaults = setDefaults; | 
					
						
							|  |  |  | 
 |