index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. const { promisify, callbackify } = require('node:util')
  2. const pLimit = require('p-limit')
  3. module.exports = {
  4. promisify,
  5. promisifyAll,
  6. promisifyClass,
  7. promisifyMultiResult,
  8. callbackify,
  9. callbackifyAll,
  10. callbackifyClass,
  11. callbackifyMultiResult,
  12. expressify,
  13. expressifyErrorHandler,
  14. promiseMapWithLimit,
  15. promiseMapSettledWithLimit,
  16. }
  17. /**
  18. * Promisify all functions in a module.
  19. *
  20. * This is meant to be used only when all functions in the module are async
  21. * callback-style functions.
  22. *
  23. * It's very much tailored to our current module structure. In particular, it
  24. * binds `this` to the module when calling the function in order not to break
  25. * modules that call sibling functions using `this`.
  26. *
  27. * This will not magically fix all modules. Special cases should be promisified
  28. * manually.
  29. *
  30. * The second argument is a bag of options:
  31. *
  32. * - without: an array of function names that shouldn't be promisified
  33. *
  34. * - multiResult: an object whose keys are function names and values are lists
  35. * of parameter names. This is meant for functions that invoke their callbacks
  36. * with more than one result in separate parameters. The promisifed function
  37. * will return these results as a single object, with each result keyed under
  38. * the corresponding parameter name.
  39. */
  40. function promisifyAll(module, opts = {}) {
  41. const { without = [], multiResult = {} } = opts
  42. const promises = {}
  43. for (const propName of Object.getOwnPropertyNames(module)) {
  44. if (without.includes(propName)) {
  45. continue
  46. }
  47. const propValue = module[propName]
  48. if (typeof propValue !== 'function') {
  49. continue
  50. }
  51. if (multiResult[propName] != null) {
  52. promises[propName] = promisifyMultiResult(
  53. propValue,
  54. multiResult[propName]
  55. ).bind(module)
  56. } else {
  57. promises[propName] = promisify(propValue).bind(module)
  58. }
  59. }
  60. return promises
  61. }
  62. /**
  63. * Promisify all methods in a class.
  64. *
  65. * Options are the same as for promisifyAll
  66. */
  67. function promisifyClass(cls, opts = {}) {
  68. const promisified = class extends cls {}
  69. const { without = [], multiResult = {} } = opts
  70. for (const propName of Object.getOwnPropertyNames(cls.prototype)) {
  71. if (propName === 'constructor' || without.includes(propName)) {
  72. continue
  73. }
  74. const propValue = cls.prototype[propName]
  75. if (typeof propValue !== 'function') {
  76. continue
  77. }
  78. if (multiResult[propName] != null) {
  79. promisified.prototype[propName] = promisifyMultiResult(
  80. propValue,
  81. multiResult[propName]
  82. )
  83. } else {
  84. promisified.prototype[propName] = promisify(propValue)
  85. }
  86. }
  87. return promisified
  88. }
  89. /**
  90. * Promisify a function that returns multiple results via additional callback
  91. * parameters.
  92. *
  93. * The promisified function returns the results in a single object whose keys
  94. * are the names given in the array `resultNames`.
  95. *
  96. * Example:
  97. *
  98. * function f(callback) {
  99. * return callback(null, 1, 2, 3)
  100. * }
  101. *
  102. * const g = promisifyMultiResult(f, ['a', 'b', 'c'])
  103. *
  104. * const result = await g() // returns {a: 1, b: 2, c: 3}
  105. */
  106. function promisifyMultiResult(fn, resultNames) {
  107. function promisified(...args) {
  108. return new Promise((resolve, reject) => {
  109. try {
  110. fn.bind(this)(...args, (err, ...results) => {
  111. if (err != null) {
  112. return reject(err)
  113. }
  114. const promiseResult = {}
  115. for (let i = 0; i < resultNames.length; i++) {
  116. promiseResult[resultNames[i]] = results[i]
  117. }
  118. resolve(promiseResult)
  119. })
  120. } catch (err) {
  121. reject(err)
  122. }
  123. })
  124. }
  125. return promisified
  126. }
  127. /**
  128. * Reverse of `promisifyAll`.
  129. *
  130. * Callbackify all async functions in a module and return them in an object. In
  131. * contrast with `promisifyAll`, all other exports from the module are added to
  132. * the result.
  133. *
  134. * This is meant to be used like this:
  135. *
  136. * const MyPromisifiedModule = {...}
  137. * module.exports = {
  138. * ...callbackifyAll(MyPromisifiedModule),
  139. * promises: MyPromisifiedModule
  140. * }
  141. *
  142. * @param {Object} module - The module to callbackify
  143. * @param {Object} opts - Options
  144. * @param {Array<string>} opts.without - Array of method names to exclude from
  145. * being callbackified
  146. * @param {Object} opts.multiResult - Spec of methods to be callbackified with
  147. * callbackifyMultiResult()
  148. */
  149. function callbackifyAll(module, opts = {}) {
  150. const { without = [], multiResult = {} } = opts
  151. const callbacks = {}
  152. for (const propName of Object.getOwnPropertyNames(module)) {
  153. if (without.includes(propName)) {
  154. continue
  155. }
  156. const propValue = module[propName]
  157. if (typeof propValue === 'function') {
  158. if (propValue.constructor.name === 'AsyncFunction') {
  159. if (multiResult[propName] != null) {
  160. callbacks[propName] = callbackifyMultiResult(
  161. propValue,
  162. multiResult[propName]
  163. ).bind(module)
  164. } else {
  165. callbacks[propName] = callbackify(propValue).bind(module)
  166. }
  167. } else {
  168. callbacks[propName] = propValue.bind(module)
  169. }
  170. } else {
  171. callbacks[propName] = propValue
  172. }
  173. }
  174. return callbacks
  175. }
  176. /**
  177. * Callbackify all methods in a class.
  178. *
  179. * Options are the same as for callbackifyAll
  180. */
  181. function callbackifyClass(cls, opts = {}) {
  182. const callbackified = class extends cls {}
  183. const { without = [], multiResult = {} } = opts
  184. for (const propName of Object.getOwnPropertyNames(cls.prototype)) {
  185. if (propName === 'constructor' || without.includes(propName)) {
  186. continue
  187. }
  188. const propValue = cls.prototype[propName]
  189. if (typeof propValue !== 'function') {
  190. continue
  191. }
  192. if (multiResult[propName] != null) {
  193. callbackified.prototype[propName] = callbackifyMultiResult(
  194. propValue,
  195. multiResult[propName]
  196. )
  197. } else {
  198. callbackified.prototype[propName] = callbackify(propValue)
  199. }
  200. }
  201. return callbackified
  202. }
  203. /**
  204. * Reverse the effect of `promisifyMultiResult`.
  205. *
  206. * This is meant for providing a temporary backward compatible callback
  207. * interface while we migrate to promises.
  208. */
  209. function callbackifyMultiResult(fn, resultNames) {
  210. function callbackified(...args) {
  211. const [callback] = args.splice(-1)
  212. fn.apply(this, args)
  213. .then(result => {
  214. const cbResults = resultNames.map(resultName => result[resultName])
  215. callback(null, ...cbResults)
  216. })
  217. .catch(err => {
  218. callback(err)
  219. })
  220. }
  221. return callbackified
  222. }
  223. /**
  224. * Transform an async function into an Express middleware
  225. *
  226. * Any error will be passed to the error middlewares via `next()`
  227. */
  228. function expressify(fn) {
  229. return (req, res, next) => {
  230. return fn(req, res, next).catch(next)
  231. }
  232. }
  233. /**
  234. * Transform an async function into an Error Handling Express middleware
  235. *
  236. * Any error will be passed to the error middlewares via `next()`
  237. */
  238. function expressifyErrorHandler(fn) {
  239. return (err, req, res, next) => {
  240. fn(err, req, res, next).catch(next)
  241. }
  242. }
  243. /**
  244. * Map values in `array` with the async function `fn`
  245. *
  246. * Limit the number of unresolved promises to `concurrency`.
  247. * @template T
  248. * @template V
  249. * @param {number} concurrency
  250. * @param {Array<T>} array
  251. * @param {(arg: T) => Promise<V>} fn
  252. * @return {Promise<Array<Awaited<V>>>}
  253. */
  254. async function promiseMapWithLimit(concurrency, array, fn) {
  255. const limit = pLimit(concurrency)
  256. return await Promise.all(array.map(x => limit(() => fn(x))))
  257. }
  258. /**
  259. * Map values in `array` with the async function `fn`
  260. *
  261. * Limit the number of unresolved promises to `concurrency`.
  262. *
  263. * @template T, U
  264. * @param {number} concurrency
  265. * @param {Array<T>} array
  266. * @param {(T) => Promise<U>} fn
  267. * @return {Promise<Array<PromiseSettledResult<U>>>}
  268. */
  269. function promiseMapSettledWithLimit(concurrency, array, fn) {
  270. const limit = pLimit(concurrency)
  271. return Promise.allSettled(array.map(x => limit(() => fn(x))))
  272. }