index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const ObjectGetOwnPropertyDescriptors = require('object.getownpropertydescriptors');
  3. const util = require('util');
  4. const timers = require('timers');
  5. const kCustomPromisifiedSymbol = util.promisify && util.promisify.custom || Symbol('util.promisify.custom');
  6. //const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
  7. function promisify(orig) {
  8. if (typeof orig !== 'function') {
  9. //const errors = require('internal/errors');
  10. //throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'function');
  11. var err = TypeError(`The "original" argument must be of type function`);
  12. err.code = 'ERR_INVALID_ARG_TYPE';
  13. err.name = `TypeError [${err.code}]`;
  14. throw err
  15. }
  16. if (orig === timers.setTimeout || orig === timers.setImmediate) {
  17. const _orig = orig
  18. orig = function () {
  19. var args = [];
  20. for (var i = 0; i < arguments.length; i ++) args.push(arguments[i]);
  21. const _cb = args.pop()
  22. const cb = function () {
  23. var args = [];
  24. for (var i = 0; i < arguments.length; i ++) args.push(arguments[i]);
  25. _cb.apply(null, [null].concat(args))
  26. }
  27. _orig.apply(timers, [cb].concat(args))
  28. }
  29. }
  30. if (orig[kCustomPromisifiedSymbol]) {
  31. const fn = orig[kCustomPromisifiedSymbol];
  32. if (typeof fn !== 'function') {
  33. throw new TypeError('The [util.promisify.custom] property must be ' +
  34. 'a function');
  35. }
  36. Object.defineProperty(fn, kCustomPromisifiedSymbol, {
  37. value: fn, enumerable: false, writable: false, configurable: true
  38. });
  39. return fn;
  40. }
  41. // Names to create an object from in case the callback receives multiple
  42. // arguments, e.g. ['stdout', 'stderr'] for child_process.exec.
  43. //const argumentNames = orig[kCustomPromisifyArgsSymbol];
  44. function fn() {
  45. var args = [];
  46. for (var i = 0; i < arguments.length; i ++) args.push(arguments[i]);
  47. let resolve, reject;
  48. const promise = new Promise(function (_resolve, _reject) {
  49. resolve = _resolve;
  50. reject = _reject;
  51. });
  52. try {
  53. orig.apply(this, args.concat(function (err) {
  54. var values = [];
  55. for (var i = 1; i < arguments.length; i++) values.push(arguments[i]);
  56. if (err) {
  57. reject(err);
  58. //} else if (argumentNames !== undefined && values.length > 1) {
  59. // const obj = {};
  60. // for (var i = 0; i < argumentNames.length; i++)
  61. // obj[argumentNames[i]] = values[i];
  62. // resolve(obj);
  63. } else {
  64. resolve(values[0]);
  65. }
  66. }));
  67. } catch (err) {
  68. reject(err);
  69. }
  70. return promise;
  71. }
  72. Object.setPrototypeOf(fn, Object.getPrototypeOf(orig));
  73. Object.defineProperty(fn, kCustomPromisifiedSymbol, {
  74. value: fn, enumerable: false, writable: false, configurable: true
  75. });
  76. return Object.defineProperties(fn, ObjectGetOwnPropertyDescriptors(orig));
  77. }
  78. promisify.custom = kCustomPromisifiedSymbol;
  79. module.exports = promisify;