diff --git a/lib/modem.js b/lib/modem.js index 4122d13..9062eb7 100644 --- a/lib/modem.js +++ b/lib/modem.js @@ -232,9 +232,7 @@ Modem.prototype.dial = function (options, callback) { if (this.socketPath) { // SocketPath may be a function that can return a promise: - var socketPathValue = typeof this.socketPath === 'function' - ? this.socketPath() : this.socketPath; - Promise.resolve(socketPathValue).then((socketPath) => { + this.getSocketPath().then((socketPath) => { optionsf.socketPath = socketPath; this.buildRequest(optionsf, options, data, callback); }); @@ -248,6 +246,15 @@ Modem.prototype.dial = function (options, callback) { } }; +Modem.prototype.getSocketPath = function () { + if (!this.socketPath) return; + + var socketPathValue = typeof this.socketPath === 'function' + ? this.socketPath() : this.socketPath; + + return Promise.resolve(socketPathValue); +} + Modem.prototype.buildRequest = function (options, context, data, callback) { var self = this; var connectionTimeoutTimer; diff --git a/test/modem_test.js b/test/modem_test.js index 2e4c77e..d12e5a4 100644 --- a/test/modem_test.js +++ b/test/modem_test.js @@ -7,15 +7,6 @@ var Modem = require('../lib/modem'); var unixDefaultSocketPaths = ['/var/run/docker.sock', path.join(os.homedir(), '.docker/run/docker.sock')] var defaultSocketPaths = os.type() === 'Windows_NT' ? ['//./pipe/docker_engine'] : unixDefaultSocketPaths; -function resolveSocketPath(socketPathField) { - if (typeof socketPathField === 'function') { - return Promise.resolve(socketPathField()); - } else { - // Return it as a promise, just for consistency - return Promise.resolve(socketPathField); - } -} - describe('Modem', function () { beforeEach(function () { delete process.env.DOCKER_HOST; @@ -24,7 +15,7 @@ describe('Modem', function () { it('should default to default socket path', function () { var modem = new Modem(); - return resolveSocketPath(modem.socketPath).then((socketPath) => { + return modem.getSocketPath().then((socketPath) => { assert.ok(socketPath); assert.ok(defaultSocketPaths.includes(socketPath)); }); @@ -68,7 +59,7 @@ describe('Modem', function () { assert.ok(modem.headers); assert.strictEqual(modem.headers, customHeaders); - return resolveSocketPath(modem.socketPath).then((socketPath) => { + return modem.getSocketPath().then((socketPath) => { assert.ok(socketPath); assert.ok(defaultSocketPaths.includes(socketPath)); }); @@ -86,7 +77,7 @@ describe('Modem', function () { process.env.DOCKER_HOST = 'unix://'; var modem = new Modem(); - return resolveSocketPath(modem.socketPath).then((socketPath) => { + return modem.getSocketPath().then((socketPath) => { assert.ok(socketPath); assert.ok(unixDefaultSocketPaths.includes(socketPath)); });