@google-cloud/promisify
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -34,22 +34,18 @@ /** | ||
} | ||
declare class Util { | ||
/** | ||
* Wraps a callback style function to conditionally return a promise. | ||
* | ||
* @param {function} originalMethod - The method to promisify. | ||
* @param {object=} options - Promise options. | ||
* @param {boolean} options.singular - Resolve the promise with single arg instead of an array. | ||
* @return {function} wrapped | ||
*/ | ||
promisify(originalMethod: PromiseMethod, options?: PromisifyOptions): any; | ||
/** | ||
* Promisifies certain Class methods. This will not promisify private or | ||
* streaming methods. | ||
* | ||
* @param {module:common/service} Class - Service class. | ||
* @param {object=} options - Configuration object. | ||
*/ | ||
promisifyAll(Class: Function, options?: PromisifyAllOptions): void; | ||
} | ||
declare const util: Util; | ||
export { util }; | ||
/** | ||
* Wraps a callback style function to conditionally return a promise. | ||
* | ||
* @param {function} originalMethod - The method to promisify. | ||
* @param {object=} options - Promise options. | ||
* @param {boolean} options.singular - Resolve the promise with single arg instead of an array. | ||
* @return {function} wrapped | ||
*/ | ||
export declare function promisify(originalMethod: PromiseMethod, options?: PromisifyOptions): any; | ||
/** | ||
* Promisifies certain Class methods. This will not promisify private or | ||
* streaming methods. | ||
* | ||
* @param {module:common/service} Class - Service class. | ||
* @param {object=} options - Configuration object. | ||
*/ | ||
export declare function promisifyAll(Class: Function, options?: PromisifyAllOptions): void; |
@@ -18,97 +18,92 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Util = /** @class */ (function () { | ||
function Util() { | ||
/** | ||
* Wraps a callback style function to conditionally return a promise. | ||
* | ||
* @param {function} originalMethod - The method to promisify. | ||
* @param {object=} options - Promise options. | ||
* @param {boolean} options.singular - Resolve the promise with single arg instead of an array. | ||
* @return {function} wrapped | ||
*/ | ||
function promisify(originalMethod, options) { | ||
if (originalMethod.promisified_) { | ||
return originalMethod; | ||
} | ||
/** | ||
* Wraps a callback style function to conditionally return a promise. | ||
* | ||
* @param {function} originalMethod - The method to promisify. | ||
* @param {object=} options - Promise options. | ||
* @param {boolean} options.singular - Resolve the promise with single arg instead of an array. | ||
* @return {function} wrapped | ||
*/ | ||
Util.prototype.promisify = function (originalMethod, options) { | ||
if (originalMethod.promisified_) { | ||
return originalMethod; | ||
options = options || {}; | ||
var slice = Array.prototype.slice; | ||
// tslint:disable-next-line:no-any | ||
var wrapper = function () { | ||
var context = this; | ||
var last; | ||
for (last = arguments.length - 1; last >= 0; last--) { | ||
var arg = arguments[last]; | ||
if (typeof arg === 'undefined') { | ||
continue; // skip trailing undefined. | ||
} | ||
if (typeof arg !== 'function') { | ||
break; // non-callback last argument found. | ||
} | ||
return originalMethod.apply(context, arguments); | ||
} | ||
options = options || {}; | ||
var slice = Array.prototype.slice; | ||
// tslint:disable-next-line:no-any | ||
var wrapper = function () { | ||
var context = this; | ||
var last; | ||
for (last = arguments.length - 1; last >= 0; last--) { | ||
var arg = arguments[last]; | ||
if (typeof arg === 'undefined') { | ||
continue; // skip trailing undefined. | ||
// peel trailing undefined. | ||
var args = slice.call(arguments, 0, last + 1); | ||
// tslint:disable-next-line:variable-name | ||
var PromiseCtor = Promise; | ||
// Because dedupe will likely create a single install of | ||
// @google-cloud/common to be shared amongst all modules, we need to | ||
// localize it at the Service level. | ||
if (context && context.Promise) { | ||
PromiseCtor = context.Promise; | ||
} | ||
return new PromiseCtor(function (resolve, reject) { | ||
// tslint:disable-next-line:no-any | ||
args.push(function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
if (typeof arg !== 'function') { | ||
break; // non-callback last argument found. | ||
var callbackArgs = slice.call(args); | ||
var err = callbackArgs.shift(); | ||
if (err) { | ||
return reject(err); | ||
} | ||
return originalMethod.apply(context, arguments); | ||
} | ||
// peel trailing undefined. | ||
var args = slice.call(arguments, 0, last + 1); | ||
// tslint:disable-next-line:variable-name | ||
var PromiseCtor = Promise; | ||
// Because dedupe will likely create a single install of | ||
// @google-cloud/common to be shared amongst all modules, we need to | ||
// localize it at the Service level. | ||
if (context && context.Promise) { | ||
PromiseCtor = context.Promise; | ||
} | ||
return new PromiseCtor(function (resolve, reject) { | ||
// tslint:disable-next-line:no-any | ||
args.push(function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var callbackArgs = slice.call(args); | ||
var err = callbackArgs.shift(); | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (options.singular && callbackArgs.length === 1) { | ||
resolve(callbackArgs[0]); | ||
} | ||
else { | ||
resolve(callbackArgs); | ||
} | ||
}); | ||
originalMethod.apply(context, args); | ||
if (options.singular && callbackArgs.length === 1) { | ||
resolve(callbackArgs[0]); | ||
} | ||
else { | ||
resolve(callbackArgs); | ||
} | ||
}); | ||
}; | ||
wrapper.promisified_ = true; | ||
return wrapper; | ||
}; | ||
/** | ||
* Promisifies certain Class methods. This will not promisify private or | ||
* streaming methods. | ||
* | ||
* @param {module:common/service} Class - Service class. | ||
* @param {object=} options - Configuration object. | ||
*/ | ||
// tslint:disable-next-line:variable-name | ||
Util.prototype.promisifyAll = function (Class, options) { | ||
var exclude = (options && options.exclude) || []; | ||
var ownPropertyNames = Object.getOwnPropertyNames(Class.prototype); | ||
var methods = ownPropertyNames.filter(function (methodName) { | ||
// clang-format off | ||
return (typeof Class.prototype[methodName] === 'function' && // is it a function? | ||
!/(^_|(Stream|_)|promise$)|^constructor$/.test(methodName) && // is it promisable? | ||
exclude.indexOf(methodName) === -1); // is it blacklisted? | ||
// clang-format on | ||
originalMethod.apply(context, args); | ||
}); | ||
methods.forEach(function (methodName) { | ||
var originalMethod = Class.prototype[methodName]; | ||
if (!originalMethod.promisified_) { | ||
Class.prototype[methodName] = util.promisify(originalMethod, options); | ||
} | ||
}); | ||
}; | ||
return Util; | ||
}()); | ||
var util = new Util(); | ||
exports.util = util; | ||
wrapper.promisified_ = true; | ||
return wrapper; | ||
} | ||
exports.promisify = promisify; | ||
/** | ||
* Promisifies certain Class methods. This will not promisify private or | ||
* streaming methods. | ||
* | ||
* @param {module:common/service} Class - Service class. | ||
* @param {object=} options - Configuration object. | ||
*/ | ||
// tslint:disable-next-line:variable-name | ||
function promisifyAll(Class, options) { | ||
var exclude = (options && options.exclude) || []; | ||
var ownPropertyNames = Object.getOwnPropertyNames(Class.prototype); | ||
var methods = ownPropertyNames.filter(function (methodName) { | ||
// clang-format off | ||
return (typeof Class.prototype[methodName] === 'function' && // is it a function? | ||
!/(^_|(Stream|_)|promise$)|^constructor$/.test(methodName) && // is it promisable? | ||
exclude.indexOf(methodName) === -1); // is it blacklisted? | ||
// clang-format on | ||
}); | ||
methods.forEach(function (methodName) { | ||
var originalMethod = Class.prototype[methodName]; | ||
if (!originalMethod.promisified_) { | ||
Class.prototype[methodName] = exports.promisify(originalMethod, options); | ||
} | ||
}); | ||
} | ||
exports.promisifyAll = promisifyAll; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@google-cloud/promisify", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "A simple utility for promisifying functions and classes.", | ||
@@ -8,3 +8,5 @@ "main": "build/src/index.js", | ||
"scripts": { | ||
"test": "nyc mocha build/test", | ||
"test": "npm run test-only", | ||
"test-only": "nyc mocha build/test", | ||
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json", | ||
"check": "gts check", | ||
@@ -26,3 +28,5 @@ "clean": "gts clean", | ||
"@types/mocha": "^5.2.4", | ||
"@types/node": "^10.5.2", | ||
"@types/sinon": "^5.0.1", | ||
"codecov": "^3.0.4", | ||
"gts": "^0.7.1", | ||
@@ -40,6 +44,3 @@ "hard-rejection": "^1.0.0", | ||
] | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
@@ -6,2 +6,28 @@ <img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/> | ||
Google Cloud Common node.js module contains stuff used by other Cloud API modules. | ||
* [github.com/googlecloudplatform/google-cloud-node](https://github.com/googlecloudplatform/google-cloud-node) | ||
Read more about the client libraries for Cloud APIs, including the older | ||
Google APIs Client Libraries, in [Client Libraries Explained][explained]. | ||
[explained]: https://cloud.google.com/apis/docs/client-libraries-explained | ||
### Installing the package | ||
It's unlikely you will need to install this package directly, as it will be | ||
installed as a dependency when you install other `@google-cloud` packages. | ||
## Versioning | ||
This library follows [Semantic Versioning](http://semver.org/). | ||
This library is considered to be in **alpha**. This means it is still a | ||
work-in-progress and under active development. Any release is subject to | ||
backwards-incompatible changes at any time. | ||
More Information: [Google Cloud Platform Launch Stages][launch_stages] | ||
[launch_stages]: https://cloud.google.com/terms/launch-stages | ||
## Contributing | ||
@@ -16,2 +42,1 @@ | ||
See [LICENSE](https://github.com/googlecloudplatform/google-cloud-node/blob/master/LICENSE) | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22840
7
41
11
157