Giter Club home page Giter Club logo

Comments (1)

zxlin avatar zxlin commented on May 25, 2024

@apocas would it be wise for us to consider exposing this as a utility function for users of the lib to call so that they may explicitly pass this into the constructor rather than it being auto-populated? This can avoid some ambiguity with the options being passed in and how they're manipulated. Or if a null or undefined is passed as the constructor, it'll use the defaults, but an empty object {} should be treated as user wants to supply their own configs and if any critical configs are missing, then we should throw an error from the constructor.

Examples

new Docker(); // uses the defaultOpts function
new Docker({}); // does not use defaultOpts function, will throw an error since neither socketPath nor host/port is defined
new Docker({ host: '10.0.0.123' }); // host is defined, defaultOpts should not be called, but will throw error for missing port parameter

This will result in a major revision since this is technical breaking changes as we're going to be modifying the default behavior. If we can come to a consensus here, I can start a PR with these changes.

I understand that defaultOpts function does a few more things, we'll likely want to move those out of the function to set up the initial states.

Thanks for all the hard work! Let me know what you and other folks think is the best way forward.

Lines in question here for reference:

var defaultOpts = function() {
var host;
var opts = {};
if (!process.env.DOCKER_HOST) {
// Windows socket path: //./pipe/docker_engine ( Windows 10 )
// Linux & Darwin socket path: /var/run/docker.sock
opts.socketPath = isWin ? '//./pipe/docker_engine' : '/var/run/docker.sock';
} else if (process.env.DOCKER_HOST.indexOf('unix://') === 0) {
// Strip off unix://, fall back to default of /var/run/docker.sock if
// unix:// was passed without a path
opts.socketPath = process.env.DOCKER_HOST.substring(7) || '/var/run/docker.sock';
} else if (process.env.DOCKER_HOST.indexOf('npipe://') === 0) {
// Strip off npipe://, fall back to default of //./pipe/docker_engine if
// npipe:// was passed without a path
opts.socketPath = process.env.DOCKER_HOST.substring(8) || '//./pipe/docker_engine';
} else {
var hostStr = process.env.DOCKER_HOST;
if(hostStr.indexOf('\/\/') < 0) {
hostStr = 'tcp://' + hostStr;
}
try {
host = new url.URL(hostStr);
} catch(err) {
throw new Error('DOCKER_HOST env variable should be something like tcp://localhost:1234');
}
opts.port = host.port;
if (process.env.DOCKER_TLS_VERIFY === '1' || opts.port === '2376') {
opts.protocol = 'https';
} else if (host.protocol === 'ssh:') {
opts.protocol = 'ssh';
opts.username = host.username;
opts.sshAuthAgent = process.env.SSH_AUTH_SOCK;
} else {
opts.protocol = 'http';
}
opts.host = host.hostname;
if (process.env.DOCKER_CERT_PATH) {
opts.ca = splitca(path.join(process.env.DOCKER_CERT_PATH, 'ca.pem'));
opts.cert = fs.readFileSync(path.join(process.env.DOCKER_CERT_PATH, 'cert.pem'));
opts.key = fs.readFileSync(path.join(process.env.DOCKER_CERT_PATH, 'key.pem'));
}
if (process.env.DOCKER_CLIENT_TIMEOUT) {
opts.timeout = parseInt(process.env.DOCKER_CLIENT_TIMEOUT, 10);
}
}
return opts;
};

from docker-modem.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.