diff --git a/lib/modem.js b/lib/modem.js index 63a4b9b..b5f4cfd 100644 --- a/lib/modem.js +++ b/lib/modem.js @@ -164,7 +164,7 @@ Modem.prototype.dial = function (options, callback) { address = address.substring(0, address.length - 1); } } - + var optionsf = { path: address, method: options.method, @@ -340,7 +340,7 @@ Modem.prototype.buildRequest = function (options, context, data, callback) { debug('Received: %s', result); - var json = utils.parseJSON(result) || stream.Readable.from(buffer); + var json = utils.parseJSON(result) || result; if (finished === false) { finished = true; self.buildPayload(null, context.isStream, context.statusCodes, false, req, res, json, callback); @@ -376,6 +376,9 @@ Modem.prototype.buildPayload = function (err, isStream, statusCodes, openStdin, if (statusCodes[res.statusCode] !== true) { getCause(isStream, res, json, function (err, cause) { + if (err) { + return cb(err, null); + } var msg = new Error( '(HTTP code ' + res.statusCode + ') ' + (statusCodes[res.statusCode] || 'unexpected') + ' - ' + @@ -398,16 +401,32 @@ Modem.prototype.buildPayload = function (err, isStream, statusCodes, openStdin, function getCause(isStream, res, json, callback) { var chunks = ''; + var done = false; + if (isStream) { res.on('data', function (chunk) { chunks += chunk; }); + res.on('error', function (err) { + handler(err, null); + }); res.on('end', function () { - callback(null, utils.parseJSON(chunks) || chunks); + handler(null, utils.parseJSON(chunks) || chunks) }); } else { callback(null, json); } + + function handler(err, data) { + if (done === false) { + if (err) { + callback(err); + } else { + callback(null, data); + } + } + done = true; + } } }; diff --git a/package-lock.json b/package-lock.json index 0fb9388..2af4603 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "docker-modem", - "version": "4.0.1", + "version": "5.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "docker-modem", - "version": "4.0.1", + "version": "5.0.0", "license": "Apache-2.0", "dependencies": { "debug": "^4.1.1", diff --git a/package.json b/package.json index 054e11d..ddb2d82 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "docker-modem", "description": "Docker remote API network layer module.", - "version": "4.0.1", + "version": "5.0.0", "author": "Pedro Dias ", "maintainers": [ "apocas "