1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.callbackifyAll = exports.callbackify = exports.promisifyAll = exports.promisify = void 0;
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function promisify(originalMethod, options) {
|
14 | if (originalMethod.promisified_) {
|
15 | return originalMethod;
|
16 | }
|
17 | options = options || {};
|
18 | const slice = Array.prototype.slice;
|
19 |
|
20 | const wrapper = function () {
|
21 | let last;
|
22 | for (last = arguments.length - 1; last >= 0; last--) {
|
23 | const arg = arguments[last];
|
24 | if (typeof arg === 'undefined') {
|
25 | continue;
|
26 | }
|
27 | if (typeof arg !== 'function') {
|
28 | break;
|
29 | }
|
30 | return originalMethod.apply(this, arguments);
|
31 | }
|
32 |
|
33 | const args = slice.call(arguments, 0, last + 1);
|
34 |
|
35 | let PromiseCtor = Promise;
|
36 |
|
37 |
|
38 |
|
39 | if (this && this.Promise) {
|
40 | PromiseCtor = this.Promise;
|
41 | }
|
42 | return new PromiseCtor((resolve, reject) => {
|
43 |
|
44 | args.push((...args) => {
|
45 | const callbackArgs = slice.call(args);
|
46 | const err = callbackArgs.shift();
|
47 | if (err) {
|
48 | return reject(err);
|
49 | }
|
50 | if (options.singular && callbackArgs.length === 1) {
|
51 | resolve(callbackArgs[0]);
|
52 | }
|
53 | else {
|
54 | resolve(callbackArgs);
|
55 | }
|
56 | });
|
57 | originalMethod.apply(this, args);
|
58 | });
|
59 | };
|
60 | wrapper.promisified_ = true;
|
61 | return wrapper;
|
62 | }
|
63 | exports.promisify = promisify;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function promisifyAll(Class, options) {
|
73 | const exclude = (options && options.exclude) || [];
|
74 | const ownPropertyNames = Object.getOwnPropertyNames(Class.prototype);
|
75 | const methods = ownPropertyNames.filter(methodName => {
|
76 |
|
77 | return (!exclude.includes(methodName) &&
|
78 | typeof Class.prototype[methodName] === 'function' &&
|
79 | !/(^_|(Stream|_)|promise$)|^constructor$/.test(methodName)
|
80 | );
|
81 |
|
82 | });
|
83 | methods.forEach(methodName => {
|
84 | const originalMethod = Class.prototype[methodName];
|
85 | if (!originalMethod.promisified_) {
|
86 | Class.prototype[methodName] = exports.promisify(originalMethod, options);
|
87 | }
|
88 | });
|
89 | }
|
90 | exports.promisifyAll = promisifyAll;
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function callbackify(originalMethod) {
|
100 | if (originalMethod.callbackified_) {
|
101 | return originalMethod;
|
102 | }
|
103 |
|
104 | const wrapper = function () {
|
105 | if (typeof arguments[arguments.length - 1] !== 'function') {
|
106 | return originalMethod.apply(this, arguments);
|
107 | }
|
108 | const cb = Array.prototype.pop.call(arguments);
|
109 | originalMethod.apply(this, arguments).then(
|
110 |
|
111 | (res) => {
|
112 | res = Array.isArray(res) ? res : [res];
|
113 | cb(null, ...res);
|
114 | }, (err) => cb(err));
|
115 | };
|
116 | wrapper.callbackified_ = true;
|
117 | return wrapper;
|
118 | }
|
119 | exports.callbackify = callbackify;
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function callbackifyAll(
|
128 | // tslint:disable-next-line:variable-name
|
129 | Class, options) {
|
130 | const exclude = (options && options.exclude) || [];
|
131 | const ownPropertyNames = Object.getOwnPropertyNames(Class.prototype);
|
132 | const methods = ownPropertyNames.filter(methodName => {
|
133 |
|
134 | return (!exclude.includes(methodName) &&
|
135 | typeof Class.prototype[methodName] === 'function' &&
|
136 | !/^_|(Stream|_)|^constructor$/.test(methodName)
|
137 | );
|
138 |
|
139 | });
|
140 | methods.forEach(methodName => {
|
141 | const originalMethod = Class.prototype[methodName];
|
142 | if (!originalMethod.callbackified_) {
|
143 | Class.prototype[methodName] = exports.callbackify(originalMethod);
|
144 | }
|
145 | });
|
146 | }
|
147 | exports.callbackifyAll = callbackifyAll;
|
148 |
|
\ | No newline at end of file |