|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +function callbackifyOnRejected(reason: any, cb: any) { |
| 23 | + // `!reason` guard inspired by bluebird (Ref: https://goo.gl/t5IS6M). |
| 24 | + // Because `null` is a special error value in callbacks which means "no error |
| 25 | + // occurred", we error-wrap so the callback consumer can distinguish between |
| 26 | + // "the promise rejected with null" or "the promise fulfilled with undefined". |
| 27 | + if (!reason) { |
| 28 | + const newReason = new Error('Promise was rejected with a falsy value') as any |
| 29 | + newReason.reason = reason |
| 30 | + reason = newReason |
| 31 | + } |
| 32 | + return cb(reason) |
| 33 | +} |
| 34 | + |
| 35 | +export function callbackify(original: any): any { |
| 36 | + if (typeof original !== 'function') { |
| 37 | + throw new TypeError('The "original" argument must be of type Function') |
| 38 | + } |
| 39 | + |
| 40 | + // We DO NOT return the promise as it gives the user a false sense that |
| 41 | + // the promise is actually somehow related to the callback's execution |
| 42 | + // and that the callback throwing will reject the promise. |
| 43 | + function callbackified(this: any) { |
| 44 | + const args = [] |
| 45 | + for (let i = 0; i < arguments.length; i++) { |
| 46 | + args.push(arguments[i]) |
| 47 | + } |
| 48 | + |
| 49 | + const maybeCb = args.pop() |
| 50 | + if (typeof maybeCb !== 'function') { |
| 51 | + throw new TypeError('The last argument must be of type Function') |
| 52 | + } |
| 53 | + |
| 54 | + //tslint:disable-next-line no-invalid-this |
| 55 | + const self = this |
| 56 | + const cb = function() { |
| 57 | + return maybeCb.apply(self, arguments) |
| 58 | + } |
| 59 | + |
| 60 | + // In true node style we process the callback on `nextTick` with all the |
| 61 | + // implications (stack, `uncaughtException`, `async_hooks`) |
| 62 | + //tslint:disable-next-line no-invalid-this |
| 63 | + original.apply(this, args).then( |
| 64 | + function(ret: any) { |
| 65 | + process.nextTick(cb.bind(null, null, ret)) |
| 66 | + }, |
| 67 | + function(rej: any) { |
| 68 | + process.nextTick(callbackifyOnRejected.bind(null, rej, cb)) |
| 69 | + }, |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original)) |
| 74 | + Object.defineProperties(callbackified, Object.getOwnPropertyDescriptors(original)) |
| 75 | + |
| 76 | + return callbackified |
| 77 | +} |
0 commit comments