index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*!
  2. * raw-body
  3. * Copyright(c) 2013-2014 Jonathan Ong
  4. * Copyright(c) 2014-2015 Douglas Christopher Wilson
  5. * MIT Licensed
  6. */
  7. 'use strict'
  8. /**
  9. * Module dependencies.
  10. * @private
  11. */
  12. var bytes = require('bytes')
  13. var iconv = require('iconv-lite')
  14. var unpipe = require('unpipe')
  15. /**
  16. * Module exports.
  17. * @public
  18. */
  19. module.exports = getRawBody
  20. /**
  21. * Module variables.
  22. * @private
  23. */
  24. var iconvEncodingMessageRegExp = /^Encoding not recognized: /
  25. /**
  26. * Get the decoder for a given encoding.
  27. *
  28. * @param {string} encoding
  29. * @private
  30. */
  31. function getDecoder (encoding) {
  32. if (!encoding) return null
  33. try {
  34. return iconv.getDecoder(encoding)
  35. } catch (e) {
  36. // error getting decoder
  37. if (!iconvEncodingMessageRegExp.test(e.message)) throw e
  38. // the encoding was not found
  39. throw createError(415, 'specified encoding unsupported', 'encoding.unsupported', {
  40. encoding: encoding
  41. })
  42. }
  43. }
  44. /**
  45. * Get the raw body of a stream (typically HTTP).
  46. *
  47. * @param {object} stream
  48. * @param {object|string|function} [options]
  49. * @param {function} [callback]
  50. * @public
  51. */
  52. function getRawBody (stream, options, callback) {
  53. var done = callback
  54. var opts = options || {}
  55. if (options === true || typeof options === 'string') {
  56. // short cut for encoding
  57. opts = {
  58. encoding: options
  59. }
  60. }
  61. if (typeof options === 'function') {
  62. done = options
  63. opts = {}
  64. }
  65. // validate callback is a function, if provided
  66. if (done !== undefined && typeof done !== 'function') {
  67. throw new TypeError('argument callback must be a function')
  68. }
  69. // require the callback without promises
  70. if (!done && !global.Promise) {
  71. throw new TypeError('argument callback is required')
  72. }
  73. // get encoding
  74. var encoding = opts.encoding !== true
  75. ? opts.encoding
  76. : 'utf-8'
  77. // convert the limit to an integer
  78. var limit = bytes.parse(opts.limit)
  79. // convert the expected length to an integer
  80. var length = opts.length != null && !isNaN(opts.length)
  81. ? parseInt(opts.length, 10)
  82. : null
  83. if (done) {
  84. // classic callback style
  85. return readStream(stream, encoding, length, limit, done)
  86. }
  87. return new Promise(function executor (resolve, reject) {
  88. readStream(stream, encoding, length, limit, function onRead (err, buf) {
  89. if (err) return reject(err)
  90. resolve(buf)
  91. })
  92. })
  93. }
  94. /**
  95. * Halt a stream.
  96. *
  97. * @param {Object} stream
  98. * @private
  99. */
  100. function halt (stream) {
  101. // unpipe everything from the stream
  102. unpipe(stream)
  103. // pause stream
  104. if (typeof stream.pause === 'function') {
  105. stream.pause()
  106. }
  107. }
  108. /**
  109. * Make a serializable error object.
  110. *
  111. * To create serializable errors you must re-set message so
  112. * that it is enumerable and you must re configure the type
  113. * property so that is writable and enumerable.
  114. *
  115. * @param {number} status
  116. * @param {string} message
  117. * @param {string} type
  118. * @param {object} props
  119. * @private
  120. */
  121. function createError (status, message, type, props) {
  122. var error = new Error()
  123. // capture stack trace
  124. Error.captureStackTrace(error, createError)
  125. // set free-form properties
  126. for (var prop in props) {
  127. error[prop] = props[prop]
  128. }
  129. // set message
  130. error.message = message
  131. // set status
  132. error.status = status
  133. error.statusCode = status
  134. // set type
  135. Object.defineProperty(error, 'type', {
  136. value: type,
  137. enumerable: true,
  138. writable: true,
  139. configurable: true
  140. })
  141. return error
  142. }
  143. /**
  144. * Read the data from the stream.
  145. *
  146. * @param {object} stream
  147. * @param {string} encoding
  148. * @param {number} length
  149. * @param {number} limit
  150. * @param {function} callback
  151. * @public
  152. */
  153. function readStream (stream, encoding, length, limit, callback) {
  154. var complete = false
  155. var sync = true
  156. // check the length and limit options.
  157. // note: we intentionally leave the stream paused,
  158. // so users should handle the stream themselves.
  159. if (limit !== null && length !== null && length > limit) {
  160. return done(createError(413, 'request entity too large', 'entity.too.large', {
  161. expected: length,
  162. length: length,
  163. limit: limit
  164. }))
  165. }
  166. // streams1: assert request encoding is buffer.
  167. // streams2+: assert the stream encoding is buffer.
  168. // stream._decoder: streams1
  169. // state.encoding: streams2
  170. // state.decoder: streams2, specifically < 0.10.6
  171. var state = stream._readableState
  172. if (stream._decoder || (state && (state.encoding || state.decoder))) {
  173. // developer error
  174. return done(createError(500, 'stream encoding should not be set', 'stream.encoding.set'))
  175. }
  176. var received = 0
  177. var decoder
  178. try {
  179. decoder = getDecoder(encoding)
  180. } catch (err) {
  181. return done(err)
  182. }
  183. var buffer = decoder
  184. ? ''
  185. : []
  186. // attach listeners
  187. stream.on('aborted', onAborted)
  188. stream.on('close', cleanup)
  189. stream.on('data', onData)
  190. stream.on('end', onEnd)
  191. stream.on('error', onEnd)
  192. // mark sync section complete
  193. sync = false
  194. function done () {
  195. var args = new Array(arguments.length)
  196. // copy arguments
  197. for (var i = 0; i < args.length; i++) {
  198. args[i] = arguments[i]
  199. }
  200. // mark complete
  201. complete = true
  202. if (sync) {
  203. process.nextTick(invokeCallback)
  204. } else {
  205. invokeCallback()
  206. }
  207. function invokeCallback () {
  208. cleanup()
  209. if (args[0]) {
  210. // halt the stream on error
  211. halt(stream)
  212. }
  213. callback.apply(null, args)
  214. }
  215. }
  216. function onAborted () {
  217. if (complete) return
  218. done(createError(400, 'request aborted', 'request.aborted', {
  219. code: 'ECONNABORTED',
  220. expected: length,
  221. length: length,
  222. received: received
  223. }))
  224. }
  225. function onData (chunk) {
  226. if (complete) return
  227. received += chunk.length
  228. decoder
  229. ? buffer += decoder.write(chunk)
  230. : buffer.push(chunk)
  231. if (limit !== null && received > limit) {
  232. done(createError(413, 'request entity too large', 'entity.too.large', {
  233. limit: limit,
  234. received: received
  235. }))
  236. }
  237. }
  238. function onEnd (err) {
  239. if (complete) return
  240. if (err) return done(err)
  241. if (length !== null && received !== length) {
  242. done(createError(400, 'request size did not match content length', 'request.size.invalid', {
  243. expected: length,
  244. length: length,
  245. received: received
  246. }))
  247. } else {
  248. var string = decoder
  249. ? buffer + (decoder.end() || '')
  250. : Buffer.concat(buffer)
  251. done(null, string)
  252. }
  253. }
  254. function cleanup () {
  255. buffer = null
  256. stream.removeListener('aborted', onAborted)
  257. stream.removeListener('data', onData)
  258. stream.removeListener('end', onEnd)
  259. stream.removeListener('error', onEnd)
  260. stream.removeListener('close', cleanup)
  261. }
  262. }