index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*!
  2. * on-finished
  3. * Copyright(c) 2013 Jonathan Ong
  4. * Copyright(c) 2014 Douglas Christopher Wilson
  5. * MIT Licensed
  6. */
  7. 'use strict'
  8. /**
  9. * Module exports.
  10. * @public
  11. */
  12. module.exports = onFinished
  13. module.exports.isFinished = isFinished
  14. /**
  15. * Module dependencies.
  16. * @private
  17. */
  18. var asyncHooks = tryRequireAsyncHooks()
  19. var first = require('ee-first')
  20. /**
  21. * Variables.
  22. * @private
  23. */
  24. /* istanbul ignore next */
  25. var defer = typeof setImmediate === 'function'
  26. ? setImmediate
  27. : function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) }
  28. /**
  29. * Invoke callback when the response has finished, useful for
  30. * cleaning up resources afterwards.
  31. *
  32. * @param {object} msg
  33. * @param {function} listener
  34. * @return {object}
  35. * @public
  36. */
  37. function onFinished (msg, listener) {
  38. if (isFinished(msg) !== false) {
  39. defer(listener, null, msg)
  40. return msg
  41. }
  42. // attach the listener to the message
  43. attachListener(msg, wrap(listener))
  44. return msg
  45. }
  46. /**
  47. * Determine if message is already finished.
  48. *
  49. * @param {object} msg
  50. * @return {boolean}
  51. * @public
  52. */
  53. function isFinished (msg) {
  54. var socket = msg.socket
  55. if (typeof msg.finished === 'boolean') {
  56. // OutgoingMessage
  57. return Boolean(msg.finished || (socket && !socket.writable))
  58. }
  59. if (typeof msg.complete === 'boolean') {
  60. // IncomingMessage
  61. return Boolean(msg.upgrade || !socket || !socket.readable || (msg.complete && !msg.readable))
  62. }
  63. // don't know
  64. return undefined
  65. }
  66. /**
  67. * Attach a finished listener to the message.
  68. *
  69. * @param {object} msg
  70. * @param {function} callback
  71. * @private
  72. */
  73. function attachFinishedListener (msg, callback) {
  74. var eeMsg
  75. var eeSocket
  76. var finished = false
  77. function onFinish (error) {
  78. eeMsg.cancel()
  79. eeSocket.cancel()
  80. finished = true
  81. callback(error)
  82. }
  83. // finished on first message event
  84. eeMsg = eeSocket = first([[msg, 'end', 'finish']], onFinish)
  85. function onSocket (socket) {
  86. // remove listener
  87. msg.removeListener('socket', onSocket)
  88. if (finished) return
  89. if (eeMsg !== eeSocket) return
  90. // finished on first socket event
  91. eeSocket = first([[socket, 'error', 'close']], onFinish)
  92. }
  93. if (msg.socket) {
  94. // socket already assigned
  95. onSocket(msg.socket)
  96. return
  97. }
  98. // wait for socket to be assigned
  99. msg.on('socket', onSocket)
  100. if (msg.socket === undefined) {
  101. // istanbul ignore next: node.js 0.8 patch
  102. patchAssignSocket(msg, onSocket)
  103. }
  104. }
  105. /**
  106. * Attach the listener to the message.
  107. *
  108. * @param {object} msg
  109. * @return {function}
  110. * @private
  111. */
  112. function attachListener (msg, listener) {
  113. var attached = msg.__onFinished
  114. // create a private single listener with queue
  115. if (!attached || !attached.queue) {
  116. attached = msg.__onFinished = createListener(msg)
  117. attachFinishedListener(msg, attached)
  118. }
  119. attached.queue.push(listener)
  120. }
  121. /**
  122. * Create listener on message.
  123. *
  124. * @param {object} msg
  125. * @return {function}
  126. * @private
  127. */
  128. function createListener (msg) {
  129. function listener (err) {
  130. if (msg.__onFinished === listener) msg.__onFinished = null
  131. if (!listener.queue) return
  132. var queue = listener.queue
  133. listener.queue = null
  134. for (var i = 0; i < queue.length; i++) {
  135. queue[i](err, msg)
  136. }
  137. }
  138. listener.queue = []
  139. return listener
  140. }
  141. /**
  142. * Patch ServerResponse.prototype.assignSocket for node.js 0.8.
  143. *
  144. * @param {ServerResponse} res
  145. * @param {function} callback
  146. * @private
  147. */
  148. // istanbul ignore next: node.js 0.8 patch
  149. function patchAssignSocket (res, callback) {
  150. var assignSocket = res.assignSocket
  151. if (typeof assignSocket !== 'function') return
  152. // res.on('socket', callback) is broken in 0.8
  153. res.assignSocket = function _assignSocket (socket) {
  154. assignSocket.call(this, socket)
  155. callback(socket)
  156. }
  157. }
  158. /**
  159. * Try to require async_hooks
  160. * @private
  161. */
  162. function tryRequireAsyncHooks () {
  163. try {
  164. return require('async_hooks')
  165. } catch (e) {
  166. return {}
  167. }
  168. }
  169. /**
  170. * Wrap function with async resource, if possible.
  171. * AsyncResource.bind static method backported.
  172. * @private
  173. */
  174. function wrap (fn) {
  175. var res
  176. // create anonymous resource
  177. if (asyncHooks.AsyncResource) {
  178. res = new asyncHooks.AsyncResource(fn.name || 'bound-anonymous-fn')
  179. }
  180. // incompatible node.js
  181. if (!res || !res.runInAsyncScope) {
  182. return fn
  183. }
  184. // return bound function
  185. return res.runInAsyncScope.bind(res, fn, null)
  186. }