Skip to content

Commit

Permalink
refactoring, bug fixes, improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Jul 26, 2014
1 parent c4c6c07 commit eb6e162
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
61 changes: 32 additions & 29 deletions lib/modem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ var querystring = require('querystring'),
debug = require('debug')('modem'),
util = require('util');


var Modem = function(opts) {
this.socketPath = opts.socketPath;
this.host = opts.host;
this.protocol = opts.protocol || 'http';
this.port = opts.port;
this.version = opts.version;
};


Modem.prototype.dial = function(options, callback) {
var opts, address, data, datastream;
var opts, address, data;
var self = this;

if (options.options) {
Expand All @@ -29,7 +30,13 @@ Modem.prototype.dial = function(options, callback) {
}

if(this.host) {
address = url.resolve(this.host + ':' + this.port, options.path);
var parsed = url.parse(self.host);
address = url.format({
'protocol': parsed.protocol || self.protocol,
'hostname': parsed.hostname || self.host,
'port': self.port
});
address = url.resolve(address, options.path);
} else {
address = options.path;
}
Expand All @@ -50,31 +57,25 @@ Modem.prototype.dial = function(options, callback) {
};

if(options.authconfig) {
function base64(s) {
new Buffer(s).toString('base64');
}

optionsf.headers['X-Registry-Auth'] = base64(JSON.stringify(options.authconfig));
optionsf.headers['X-Registry-Auth'] = new Buffer(JSON.stringify(options.authconfig)).toString('base64');
}

if(options.file) {
if (typeof options.file === 'string') {
data = fs.readFileSync(p.resolve(options.file));
} else {
datastream = options.file;
data = options.file;
}
optionsf.headers['Content-Type'] = 'application/tar';
} else if(opts && options.method === 'POST') {
data = JSON.stringify(opts);
optionsf.headers['Content-Type'] = 'application/json';
}

if(data) {
if(typeof data === "string") {
optionsf.headers['Content-Length'] = Buffer.byteLength(data);
} else {
optionsf.headers['Content-Length'] = data.length;
}
if(typeof data === "string") {
optionsf.headers['Content-Length'] = Buffer.byteLength(data);
} else if(Buffer.isBuffer(data) === true) {
optionsf.headers['Content-Length'] = data.length;
}

if(this.socketPath) {
Expand All @@ -84,19 +85,20 @@ Modem.prototype.dial = function(options, callback) {
optionsf.hostname = urlp.hostname;
optionsf.port = urlp.port;
optionsf.path = urlp.path;
if(opts && options.method === 'GET') {
optionsf.path = optionsf.path + "?" + querystring.stringify(opts);
optionsf.headers['Content-Type'] = "application/json";
}
}

debug('Sending: %s', util.inspect(optionsf, { showHidden: true, depth: null }));
this.buildRequest(optionsf, options, data, callback);
};

var req = http.request(optionsf, function() {});
Modem.prototype.buildRequest = function(options, context, data, callback) {
var self = this;
var req = http.request(options, function() {});

debug('Sending: %s', util.inspect(options, { showHidden: true, depth: null }));

req.on('response', function(res) {
if (options.isStream) {
self.buildPayload(null, options.isStream, options.statusCodes, options.openStdin, req, res, null, callback);
if (context.isStream) {
self.buildPayload(null, context.isStream, context.statusCodes, context.openStdin, req, res, null, callback);
} else {
var chunks = '';
res.on('data', function(chunk) {
Expand All @@ -112,21 +114,22 @@ Modem.prototype.dial = function(options, callback) {
} catch(e) {
json = chunks;
}
self.buildPayload(null, options.isStream, options.statusCodes, false, req, res, json, callback);
self.buildPayload(null, context.isStream, context.statusCodes, false, req, res, json, callback);
});
}
});

req.on('error', function(error) {
self.buildPayload(error, options.isStream, options.statusCodes, false, {}, {}, null, callback);
self.buildPayload(error, context.isStream, context.statusCodes, false, {}, {}, null, callback);
});

if(data) {
if(typeof data === "string" || Buffer.isBuffer(data)) {
req.write(data);
} else if(data) {
data.pipe(req);
}
if (datastream) {
datastream.pipe(req);
} else if (!options.openStdin) {

if (!context.openStdin && (typeof data === "string" || data === undefined || Buffer.isBuffer(data))) {
req.end();
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "docker-modem",
"description": "Docker remote API network layer module.",
"version": "0.1.16",
"version": "0.1.17",
"author": "Pedro Dias <[email protected]>",
"maintainers": [
"apocas <[email protected]>"
Expand Down

0 comments on commit eb6e162

Please sign in to comment.