index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var bytes = require('bytes')
  2. var iconv = require('iconv-lite')
  3. module.exports = function (stream, options, done) {
  4. if (options === true || typeof options === 'string') {
  5. // short cut for encoding
  6. options = {
  7. encoding: options
  8. }
  9. }
  10. options = options || {}
  11. if (typeof options === 'function') {
  12. done = options
  13. options = {}
  14. }
  15. // get encoding
  16. var encoding = options.encoding !== true
  17. ? options.encoding
  18. : 'utf-8'
  19. // convert the limit to an integer
  20. var limit = null
  21. if (typeof options.limit === 'number')
  22. limit = options.limit
  23. if (typeof options.limit === 'string')
  24. limit = bytes(options.limit)
  25. // convert the expected length to an integer
  26. var length = null
  27. if (options.length != null && !isNaN(options.length))
  28. length = parseInt(options.length, 10)
  29. // check the length and limit options.
  30. // note: we intentionally leave the stream paused,
  31. // so users should handle the stream themselves.
  32. if (limit !== null && length !== null && length > limit) {
  33. if (typeof stream.pause === 'function')
  34. stream.pause()
  35. process.nextTick(function () {
  36. var err = makeError('request entity too large', 'entity.too.large')
  37. err.status = err.statusCode = 413
  38. err.length = err.expected = length
  39. err.limit = limit
  40. done(err)
  41. })
  42. return defer
  43. }
  44. // streams1: assert request encoding is buffer.
  45. // streams2+: assert the stream encoding is buffer.
  46. // stream._decoder: streams1
  47. // state.encoding: streams2
  48. // state.decoder: streams2, specifically < 0.10.6
  49. var state = stream._readableState
  50. if (stream._decoder || (state && (state.encoding || state.decoder))) {
  51. if (typeof stream.pause === 'function')
  52. stream.pause()
  53. process.nextTick(function () {
  54. var err = makeError('stream encoding should not be set',
  55. 'stream.encoding.set')
  56. // developer error
  57. err.status = err.statusCode = 500
  58. done(err)
  59. })
  60. return defer
  61. }
  62. var received = 0
  63. var decoder
  64. try {
  65. decoder = getDecoder(encoding)
  66. } catch (err) {
  67. if (typeof stream.pause === 'function')
  68. stream.pause()
  69. process.nextTick(function () {
  70. done(err)
  71. })
  72. return defer
  73. }
  74. var buffer = decoder
  75. ? ''
  76. : []
  77. stream.on('data', onData)
  78. stream.once('end', onEnd)
  79. stream.once('error', onEnd)
  80. stream.once('close', cleanup)
  81. return defer
  82. // yieldable support
  83. function defer(fn) {
  84. done = fn
  85. }
  86. function onData(chunk) {
  87. received += chunk.length
  88. decoder
  89. ? buffer += decoder.write(chunk)
  90. : buffer.push(chunk)
  91. if (limit !== null && received > limit) {
  92. if (typeof stream.pause === 'function')
  93. stream.pause()
  94. var err = makeError('request entity too large', 'entity.too.large')
  95. err.status = err.statusCode = 413
  96. err.received = received
  97. err.limit = limit
  98. done(err)
  99. cleanup()
  100. }
  101. }
  102. function onEnd(err) {
  103. if (err) {
  104. if (typeof stream.pause === 'function')
  105. stream.pause()
  106. done(err)
  107. } else if (length !== null && received !== length) {
  108. err = makeError('request size did not match content length',
  109. 'request.size.invalid')
  110. err.status = err.statusCode = 400
  111. err.received = received
  112. err.length = err.expected = length
  113. done(err)
  114. } else {
  115. done(null, decoder
  116. ? buffer + (decoder.end() || '')
  117. : Buffer.concat(buffer)
  118. )
  119. }
  120. cleanup()
  121. }
  122. function cleanup() {
  123. received = buffer = null
  124. stream.removeListener('data', onData)
  125. stream.removeListener('end', onEnd)
  126. stream.removeListener('error', onEnd)
  127. stream.removeListener('close', cleanup)
  128. }
  129. }
  130. function getDecoder(encoding) {
  131. if (!encoding) return null
  132. try {
  133. return iconv.getCodec(encoding).decoder()
  134. } catch (e) {
  135. var err = makeError('specified encoding unsupported', 'encoding.unsupported')
  136. err.status = err.statusCode = 415
  137. err.encoding = encoding
  138. throw err
  139. }
  140. }
  141. // to create serializable errors you must re-set message so
  142. // that it is enumerable and you must re configure the type
  143. // property so that is writable and enumerable
  144. function makeError(message, type) {
  145. var error = new Error()
  146. error.message = message
  147. Object.defineProperty(error, 'type', {
  148. value: type,
  149. enumerable: true,
  150. writable: true,
  151. configurable: true
  152. })
  153. return error
  154. }