Skip to content

Commit

Permalink
Merge pull request #630 from pimterry/abort
Browse files Browse the repository at this point in the history
Allow aborting all API requests by passing an abortSignal
  • Loading branch information
apocas authored Sep 7, 2021
2 parents 8c721a0 + 323ba36 commit 9a50c26
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 72 deletions.
22 changes: 13 additions & 9 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ Config.prototype[require('util').inspect.custom] = function() { return this; };

/**
* Inspect
*
* @param {Object} opts Options (optional)
* @param {Function} callback Callback, if specified Docker will be queried.
* @return {Object} Name only if callback isn't specified.
*/
Config.prototype.inspect = function(callback) {
Config.prototype.inspect = function(opts, callback) {
var self = this;
var args = util.processArgs(opts, callback);

var optsf = {
path: '/configs/' + this.id,
method: 'GET',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
404: 'config not found',
Expand All @@ -31,7 +35,7 @@ Config.prototype.inspect = function(callback) {
}
};

if(callback === undefined) {
if(args.callback === undefined) {
return new this.modem.Promise(function(resolve, reject) {
self.modem.dial(optsf, function(err, data) {
if (err) {
Expand All @@ -42,7 +46,7 @@ Config.prototype.inspect = function(callback) {
});
} else {
this.modem.dial(optsf, function(err, data) {
callback(err, data);
args.callback(err, data);
});
}
};
Expand All @@ -55,23 +59,22 @@ Config.prototype.inspect = function(callback) {
*/
Config.prototype.update = function(opts, callback) {
var self = this;
if (!callback && typeof opts === 'function') {
callback = opts;
}
var args = util.processArgs(opts, callback);

var optsf = {
path: '/configs/' + this.id + '/update?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
404: 'config not found',
500: 'server error',
503: 'node is not part of a swarm'
},
options: opts
options: args.opts
};

if(callback === undefined) {
if(args.callback === undefined) {
return new this.modem.Promise(function(resolve, reject) {
self.modem.dial(optsf, function(err, data) {
if (err) {
Expand All @@ -82,7 +85,7 @@ Config.prototype.update = function(opts, callback) {
});
} else {
this.modem.dial(optsf, function(err, data) {
callback(err, data);
args.callback(err, data);
});
}
};
Expand All @@ -100,6 +103,7 @@ Config.prototype.remove = function(opts, callback) {
var optsf = {
path: '/configs/' + this.id,
method: 'DELETE',
abortSignal: opts.abortSignal,
statusCodes: {
200: true,
204: true,
Expand Down
45 changes: 39 additions & 6 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Container.prototype.inspect = function(opts, callback) {
path: '/containers/' + this.id + '/json?',
method: 'GET',
options: args.opts,
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
404: 'no such container',
Expand Down Expand Up @@ -87,6 +88,7 @@ Container.prototype.rename = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/rename?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
204: true,
Expand Down Expand Up @@ -124,6 +126,7 @@ Container.prototype.update = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/update',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
204: true,
Expand Down Expand Up @@ -162,6 +165,7 @@ Container.prototype.top = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/top?',
method: 'GET',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
404: 'no such container',
Expand All @@ -188,21 +192,25 @@ Container.prototype.top = function(opts, callback) {

/**
* Containers changes
* @param {Object} Options
* @param {Function} callback Callback
*/
Container.prototype.changes = function(callback) {
Container.prototype.changes = function(opts, callback) {
var self = this;
var args = util.processArgs(opts, callback);

var optsf = {
path: '/containers/' + this.id + '/changes',
method: 'GET',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
404: 'no such container',
500: 'server error'
}
};

if(callback === undefined) {
if(args.callback === undefined) {
return new this.modem.Promise(function(resolve, reject) {
self.modem.dial(optsf, function(err, data) {
if (err) {
Expand All @@ -213,7 +221,7 @@ Container.prototype.changes = function(callback) {
});
} else {
this.modem.dial(optsf, function(err, data) {
callback(err, data);
args.callback(err, data);
});
}
};
Expand All @@ -230,6 +238,7 @@ Container.prototype.listCheckpoint = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/checkpoints?',
method: 'GET',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
404: 'no such container',
Expand Down Expand Up @@ -267,6 +276,7 @@ Container.prototype.deleteCheckpoint = function(checkpoint, opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/checkpoints/' + checkpoint + '?',
method: 'DELETE',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -304,6 +314,7 @@ Container.prototype.createCheckpoint = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/checkpoints',
method: 'POST',
abortSignal: args.opts.abortSignal,
allowEmpty: true,
statusCodes: {
200: true, //unofficial, but proxies may return it
Expand Down Expand Up @@ -333,13 +344,17 @@ Container.prototype.createCheckpoint = function(opts, callback) {

/**
* Export
* @param {Object} opts Options (optional)
* @param {Function} callback Callback with the octet-stream.
*/
Container.prototype.export = function(callback) {
Container.prototype.export = function(opts, callback) {
var self = this;
var args = util.processArgs(opts, callback);

var optsf = {
path: '/containers/' + this.id + '/export',
method: 'GET',
abortSignal: args.opts.abortSignal,
isStream: true,
statusCodes: {
200: true,
Expand All @@ -348,7 +363,7 @@ Container.prototype.export = function(callback) {
}
};

if(callback === undefined) {
if(args.callback === undefined) {
return new this.modem.Promise(function(resolve, reject) {
self.modem.dial(optsf, function(err, data) {
if (err) {
Expand All @@ -359,7 +374,7 @@ Container.prototype.export = function(callback) {
});
} else {
this.modem.dial(optsf, function(err, data) {
callback(err, data);
args.callback(err, data);
});
}
};
Expand All @@ -376,6 +391,7 @@ Container.prototype.start = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/start?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -414,6 +430,7 @@ Container.prototype.pause = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/pause',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -450,6 +467,7 @@ Container.prototype.unpause = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/unpause',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -488,6 +506,7 @@ Container.prototype.exec = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/exec',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
201: true,
Expand Down Expand Up @@ -530,6 +549,7 @@ Container.prototype.commit = function(opts, callback) {
var optsf = {
path: '/commit?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
201: true,
Expand Down Expand Up @@ -567,6 +587,7 @@ Container.prototype.stop = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/stop?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -605,6 +626,7 @@ Container.prototype.restart = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/restart?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -642,6 +664,7 @@ Container.prototype.kill = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/kill?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -679,6 +702,7 @@ Container.prototype.resize = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/resize?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
400: 'bad parameter',
Expand Down Expand Up @@ -716,6 +740,7 @@ Container.prototype.attach = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/attach?',
method: 'POST',
abortSignal: args.opts.abortSignal,
isStream: true,
hijack: args.opts.hijack,
openStdin: args.opts.stdin,
Expand Down Expand Up @@ -755,6 +780,7 @@ Container.prototype.wait = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/wait?',
method: 'POST',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true,
400: 'bad parameter',
Expand Down Expand Up @@ -792,6 +818,7 @@ Container.prototype.remove = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '?',
method: 'DELETE',
abortSignal: args.opts.abortSignal,
statusCodes: {
200: true, // unofficial, but proxies may return it
204: true,
Expand Down Expand Up @@ -831,6 +858,7 @@ Container.prototype.copy = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/copy',
method: 'POST',
abortSignal: args.opts.abortSignal,
isStream: true,
statusCodes: {
200: true,
Expand Down Expand Up @@ -868,6 +896,7 @@ Container.prototype.getArchive = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/archive?',
method: 'GET',
abortSignal: args.opts.abortSignal,
isStream: true,
statusCodes: {
200: true,
Expand Down Expand Up @@ -906,6 +935,7 @@ Container.prototype.infoArchive = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/archive?',
method: 'HEAD',
abortSignal: args.opts.abortSignal,
isStream: true,
statusCodes: {
200: true,
Expand Down Expand Up @@ -945,6 +975,7 @@ Container.prototype.putArchive = function(file, opts, callback) {
path: '/containers/' + this.id + '/archive?',
method: 'PUT',
file: file,
abortSignal: args.opts.abortSignal,
isStream: true,
statusCodes: {
200: true,
Expand Down Expand Up @@ -984,6 +1015,7 @@ Container.prototype.logs = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/logs?',
method: 'GET',
abortSignal: args.opts.abortSignal,
isStream: args.opts.follow || false,
statusCodes: {
200: true,
Expand Down Expand Up @@ -1024,6 +1056,7 @@ Container.prototype.stats = function(opts, callback) {
var optsf = {
path: '/containers/' + this.id + '/stats?',
method: 'GET',
abortSignal: args.opts.abortSignal,
isStream: isStream,
statusCodes: {
200: true,
Expand Down
Loading

0 comments on commit 9a50c26

Please sign in to comment.