Skip to content

Commit eb6e162

Browse files
committed
refactoring, bug fixes, improvements
1 parent c4c6c07 commit eb6e162

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

lib/modem.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ var querystring = require('querystring'),
88
debug = require('debug')('modem'),
99
util = require('util');
1010

11+
1112
var Modem = function(opts) {
1213
this.socketPath = opts.socketPath;
1314
this.host = opts.host;
15+
this.protocol = opts.protocol || 'http';
1416
this.port = opts.port;
1517
this.version = opts.version;
1618
};
1719

18-
1920
Modem.prototype.dial = function(options, callback) {
20-
var opts, address, data, datastream;
21+
var opts, address, data;
2122
var self = this;
2223

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

3132
if(this.host) {
32-
address = url.resolve(this.host + ':' + this.port, options.path);
33+
var parsed = url.parse(self.host);
34+
address = url.format({
35+
'protocol': parsed.protocol || self.protocol,
36+
'hostname': parsed.hostname || self.host,
37+
'port': self.port
38+
});
39+
address = url.resolve(address, options.path);
3340
} else {
3441
address = options.path;
3542
}
@@ -50,31 +57,25 @@ Modem.prototype.dial = function(options, callback) {
5057
};
5158

5259
if(options.authconfig) {
53-
function base64(s) {
54-
new Buffer(s).toString('base64');
55-
}
56-
57-
optionsf.headers['X-Registry-Auth'] = base64(JSON.stringify(options.authconfig));
60+
optionsf.headers['X-Registry-Auth'] = new Buffer(JSON.stringify(options.authconfig)).toString('base64');
5861
}
5962

6063
if(options.file) {
6164
if (typeof options.file === 'string') {
6265
data = fs.readFileSync(p.resolve(options.file));
6366
} else {
64-
datastream = options.file;
67+
data = options.file;
6568
}
6669
optionsf.headers['Content-Type'] = 'application/tar';
6770
} else if(opts && options.method === 'POST') {
6871
data = JSON.stringify(opts);
6972
optionsf.headers['Content-Type'] = 'application/json';
7073
}
7174

72-
if(data) {
73-
if(typeof data === "string") {
74-
optionsf.headers['Content-Length'] = Buffer.byteLength(data);
75-
} else {
76-
optionsf.headers['Content-Length'] = data.length;
77-
}
75+
if(typeof data === "string") {
76+
optionsf.headers['Content-Length'] = Buffer.byteLength(data);
77+
} else if(Buffer.isBuffer(data) === true) {
78+
optionsf.headers['Content-Length'] = data.length;
7879
}
7980

8081
if(this.socketPath) {
@@ -84,19 +85,20 @@ Modem.prototype.dial = function(options, callback) {
8485
optionsf.hostname = urlp.hostname;
8586
optionsf.port = urlp.port;
8687
optionsf.path = urlp.path;
87-
if(opts && options.method === 'GET') {
88-
optionsf.path = optionsf.path + "?" + querystring.stringify(opts);
89-
optionsf.headers['Content-Type'] = "application/json";
90-
}
9188
}
9289

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

95-
var req = http.request(optionsf, function() {});
93+
Modem.prototype.buildRequest = function(options, context, data, callback) {
94+
var self = this;
95+
var req = http.request(options, function() {});
96+
97+
debug('Sending: %s', util.inspect(options, { showHidden: true, depth: null }));
9698

9799
req.on('response', function(res) {
98-
if (options.isStream) {
99-
self.buildPayload(null, options.isStream, options.statusCodes, options.openStdin, req, res, null, callback);
100+
if (context.isStream) {
101+
self.buildPayload(null, context.isStream, context.statusCodes, context.openStdin, req, res, null, callback);
100102
} else {
101103
var chunks = '';
102104
res.on('data', function(chunk) {
@@ -112,21 +114,22 @@ Modem.prototype.dial = function(options, callback) {
112114
} catch(e) {
113115
json = chunks;
114116
}
115-
self.buildPayload(null, options.isStream, options.statusCodes, false, req, res, json, callback);
117+
self.buildPayload(null, context.isStream, context.statusCodes, false, req, res, json, callback);
116118
});
117119
}
118120
});
119121

120122
req.on('error', function(error) {
121-
self.buildPayload(error, options.isStream, options.statusCodes, false, {}, {}, null, callback);
123+
self.buildPayload(error, context.isStream, context.statusCodes, false, {}, {}, null, callback);
122124
});
123125

124-
if(data) {
126+
if(typeof data === "string" || Buffer.isBuffer(data)) {
125127
req.write(data);
128+
} else if(data) {
129+
data.pipe(req);
126130
}
127-
if (datastream) {
128-
datastream.pipe(req);
129-
} else if (!options.openStdin) {
131+
132+
if (!context.openStdin && (typeof data === "string" || data === undefined || Buffer.isBuffer(data))) {
130133
req.end();
131134
}
132135
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "docker-modem",
33
"description": "Docker remote API network layer module.",
4-
"version": "0.1.16",
4+
"version": "0.1.17",
55
"author": "Pedro Dias <[email protected]>",
66
"maintainers": [
77
"apocas <[email protected]>"

0 commit comments

Comments
 (0)