Skip to content

Commit

Permalink
Add getSocketPath method to easily read the detected socket path
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Sep 13, 2023
1 parent 0c65c9a commit a04e0ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
13 changes: 10 additions & 3 deletions lib/modem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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;
Expand Down
15 changes: 3 additions & 12 deletions test/modem_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
});
Expand Down Expand Up @@ -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));
});
Expand All @@ -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));
});
Expand Down

0 comments on commit a04e0ef

Please sign in to comment.