blob: 58ab5a78413708984467b270c2221267734c99c3 [file] [log] [blame]
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
var http = require('http');
var url = require('url');
var Q = require('q');
var AgentKeepAlive = require('agentkeepalive');
var PushSession = require('./pushsession');
var httpAgent = new AgentKeepAlive({
maxSockets: 5,
maxFreeSockets: 5,
keepAliveMsecs: 30000
});
function doRequest(method, target, action, options) {
var deferred = Q.defer();
var targetParts = target.split(':');
var host = targetParts[0];
var port = +(targetParts[1] || 2424);
options = options || {};
var queryParams = {};
if (options.query) {
Object.keys(options.query).forEach(function(k) {
queryParams[k] = options.query[k];
});
}
if (options.appId) {
queryParams['appId'] = options.appId;
}
if (options.appType) {
queryParams['appType'] = options.appType;
}
var uri = url.format({
protocol: 'http',
hostname: host,
port: port,
pathname: action,
query: queryParams
});
var pathWithQuery = uri.replace(/^.*?\/\/.*?\//, '/');
process.stdout.write(method + ' ' + uri);
var headers = {
'Connection': 'keep-alive'
};
var body = null;
if (method == 'POST' || method == 'PUT') {
body = options.body || '';
if (options.json) {
body = JSON.stringify(options.json);
headers['Content-Type'] = 'application/json';
}
headers['Content-Length'] = body.length;
}
var startTime = new Date();
var req = http.request({
hostname: host,
port: port,
method: method,
path: pathWithQuery,
headers: headers,
agent: httpAgent
});
req.on('error', function(e) {
deferred.reject(e);
});
if (body) {
req.write(body);
}
req.end();
req.on('response', function(res) {
process.stdout.write(' ==> ' + res.statusCode);
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
process.stdout.write(' (' + (new Date() - startTime) + ')\n');
if (res.statusCode != 200) {
deferred.reject(new Error('Server returned status code: ' + res.statusCode + '\n\n' + body));
return;
}
try {
body = options.expectJson ? JSON.parse(body) : body;
} catch (e) {
deferred.reject(new Error('Invalid JSON: ' + body.slice(500)));
return;
}
deferred.resolve({res:res, body:body});
});
});
return deferred.promise;
}
function HarnessClient(target) {
this.target = target || '127.0.0.1';
}
HarnessClient.prototype.info = function() {
return doRequest('GET', this.target, '/info', { expectJson: true });
};
HarnessClient.prototype.assetmanifest = function(/* optional */ appId) {
return doRequest('GET', this.target, '/assetmanifest', {expectJson: true, appId: appId});
};
HarnessClient.prototype.menu = function() {
return doRequest('POST', this.target, '/menu');
};
HarnessClient.prototype.evalJs = function(someJs) {
return doRequest('POST', this.target, '/exec', { query: {code: someJs} });
};
HarnessClient.prototype.launch = function(/* optional */ appId) {
return doRequest('POST', this.target, '/launch', { appId: appId});
};
HarnessClient.prototype.deleteAllApps = function() {
return doRequest('POST', this.target, '/deleteapp', { query: {'all': 1} });
};
HarnessClient.prototype.deleteApp = function(/* optional */ appId) {
return doRequest('POST', this.target, '/deleteapp', { appId: appId});
};
HarnessClient.prototype.pushZip = function(appId, appType, zipData, /* optional */ totalPushBytes, /* optional */ manifestEtag) {
var query = {};
if (totalPushBytes) {
query['expectBytes'] = totalPushBytes;
}
if (manifestEtag) {
query['manifestEtag'] = manifestEtag;
}
return doRequest('POST', this.target, '/zippush', {
appId: appId,
appType: appType,
body: zipData,
query: query
});
};
HarnessClient.prototype.pushFile = function(appId, appType, payload, etag, remotePath, /* optional */ totalPushBytes, /* optional */ manifestEtag) {
var query = {
'path': remotePath,
'etag': etag
};
if (totalPushBytes) {
query['expectBytes'] = totalPushBytes;
}
if (manifestEtag) {
query['manifestEtag'] = manifestEtag;
}
return doRequest('PUT', this.target, '/putfile', {
appId: appId,
appType: appType,
body: payload,
query: query
});
};
HarnessClient.prototype.deleteFiles = function(appId, deleteList, /* optional */ manifestEtag) {
var query = {};
if (manifestEtag) {
query['manifestEtag'] = manifestEtag;
}
return doRequest('POST', this.target, 'deletefiles', { appId: appId, json: {'paths': deleteList}, query: query});
};
HarnessClient.prototype.createPushSession = function(dir) {
return new PushSession(this, dir);
};
module.exports = HarnessClient;