UNPKG

automation-client

Version:

automation-client

130 lines (94 loc) 3.23 kB
"use strict" var querystring = require('querystring'); var httpSync = require("urllib-sync"); var stack = require("callsite"); var CookieManager = require('cookie-manager'); var contextSimulator = { protocol : 'http', port : '8080', path : '/sample/automation', service : '127.0.0.1', device : '127.0.0.1', timeout : 300000 }; var contextCanon = { protocol : 'http', port : '8000', path : '/automation', service : '192.168.38.100', device : '192.168.38.100', timeout : 300000 }; var defaultContext = contextSimulator; function command( name, parameters, optionals ) { console.log(stack()[1].getFunctionName()); for ( var name in optionals ) { parameters[name] = optionals[name]; } var trace = stack()[1]; parameters['ADDRESS'] = this.currentContext.device; parameters['COMMAND'] = name; parameters['FUNCTION'] = trace.getFunctionName(); parameters['FILE'] = trace.getFileName().split('\\').pop(); parameters['LINE'] = trace.getLineNumber(); parameters['STACK'] = new Error().stack; var response = request.call(this, this.currentContext, parameters); if ( response.length > 0 ) console.log(response); return response; } function isRedirection(status) { return /^3/.test(status+''); } function requestCore( url, options ) { var headers = options.headers; if(typeof headers['Cookie'] == 'undefined') { headers['Cookie'] = this.cm.prepare( url ); } if( !headers['Cookie'] ) { delete headers['Cookie']; } var response = httpSync.request(url, options); if(typeof response.headers['set-cookie'] != 'undefined'){ this.cm.store( url, response.headers['set-cookie'] ); } if ( isRedirection( response.status ) ) { return requestCore.call(this, response.headers.location, options); } return response; } function request( context, parameters ) { var options = this.options || {}; options.method = options.method || 'GET'; options.headers = options.headers || {}; var query = querystring.stringify(parameters); var url = context.url.call( context, query ); var response = requestCore.call( this, url, options ) return response.data.toString('UTF-8'); } function AutomationClient( currentContext ) { this.currentContext = currentContext == undefined ? defaultContext : currentContext; this.currentContext.url = function( query ) { return this.protocol + '://' + this.service + ':' + this.port + this.path + '?' + query; } this.automationClient = this; this.cm = new CookieManager(); this.options = null; this.context = function() { return this.currentContext; } this.click = function( target, optionals ) { var parameters = { 'TARGET' : target }; return command.call( this, 'CLICK', parameters, optionals ); } this.stop = function(optionals) { return command.call( this, 'STOP', {}, optionals ); } this.help = function(command, optionals) { var parameters = { 'ITEM' : command }; return command.call( this, 'HELP', parameters, optionals ); } } exports.AutomationClient = AutomationClient; exports.contextSimulator = contextSimulator; exports.contextCanon = contextCanon;