Dicer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use strict'
  2. const WritableStream = require('node:stream').Writable
  3. const inherits = require('node:util').inherits
  4. const StreamSearch = require('../../streamsearch/sbmh')
  5. const PartStream = require('./PartStream')
  6. const HeaderParser = require('./HeaderParser')
  7. const DASH = 45
  8. const B_ONEDASH = Buffer.from('-')
  9. const B_CRLF = Buffer.from('\r\n')
  10. const EMPTY_FN = function () {}
  11. function Dicer (cfg) {
  12. if (!(this instanceof Dicer)) { return new Dicer(cfg) }
  13. WritableStream.call(this, cfg)
  14. if (!cfg || (!cfg.headerFirst && typeof cfg.boundary !== 'string')) { throw new TypeError('Boundary required') }
  15. if (typeof cfg.boundary === 'string') { this.setBoundary(cfg.boundary) } else { this._bparser = undefined }
  16. this._headerFirst = cfg.headerFirst
  17. this._dashes = 0
  18. this._parts = 0
  19. this._finished = false
  20. this._realFinish = false
  21. this._isPreamble = true
  22. this._justMatched = false
  23. this._firstWrite = true
  24. this._inHeader = true
  25. this._part = undefined
  26. this._cb = undefined
  27. this._ignoreData = false
  28. this._partOpts = { highWaterMark: cfg.partHwm }
  29. this._pause = false
  30. const self = this
  31. this._hparser = new HeaderParser(cfg)
  32. this._hparser.on('header', function (header) {
  33. self._inHeader = false
  34. self._part.emit('header', header)
  35. })
  36. }
  37. inherits(Dicer, WritableStream)
  38. Dicer.prototype.emit = function (ev) {
  39. if (ev === 'finish' && !this._realFinish) {
  40. if (!this._finished) {
  41. const self = this
  42. process.nextTick(function () {
  43. self.emit('error', new Error('Unexpected end of multipart data'))
  44. if (self._part && !self._ignoreData) {
  45. const type = (self._isPreamble ? 'Preamble' : 'Part')
  46. self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data'))
  47. self._part.push(null)
  48. process.nextTick(function () {
  49. self._realFinish = true
  50. self.emit('finish')
  51. self._realFinish = false
  52. })
  53. return
  54. }
  55. self._realFinish = true
  56. self.emit('finish')
  57. self._realFinish = false
  58. })
  59. }
  60. } else { WritableStream.prototype.emit.apply(this, arguments) }
  61. }
  62. Dicer.prototype._write = function (data, encoding, cb) {
  63. // ignore unexpected data (e.g. extra trailer data after finished)
  64. if (!this._hparser && !this._bparser) { return cb() }
  65. if (this._headerFirst && this._isPreamble) {
  66. if (!this._part) {
  67. this._part = new PartStream(this._partOpts)
  68. if (this.listenerCount('preamble') !== 0) { this.emit('preamble', this._part) } else { this._ignore() }
  69. }
  70. const r = this._hparser.push(data)
  71. if (!this._inHeader && r !== undefined && r < data.length) { data = data.slice(r) } else { return cb() }
  72. }
  73. // allows for "easier" testing
  74. if (this._firstWrite) {
  75. this._bparser.push(B_CRLF)
  76. this._firstWrite = false
  77. }
  78. this._bparser.push(data)
  79. if (this._pause) { this._cb = cb } else { cb() }
  80. }
  81. Dicer.prototype.reset = function () {
  82. this._part = undefined
  83. this._bparser = undefined
  84. this._hparser = undefined
  85. }
  86. Dicer.prototype.setBoundary = function (boundary) {
  87. const self = this
  88. this._bparser = new StreamSearch('\r\n--' + boundary)
  89. this._bparser.on('info', function (isMatch, data, start, end) {
  90. self._oninfo(isMatch, data, start, end)
  91. })
  92. }
  93. Dicer.prototype._ignore = function () {
  94. if (this._part && !this._ignoreData) {
  95. this._ignoreData = true
  96. this._part.on('error', EMPTY_FN)
  97. // we must perform some kind of read on the stream even though we are
  98. // ignoring the data, otherwise node's Readable stream will not emit 'end'
  99. // after pushing null to the stream
  100. this._part.resume()
  101. }
  102. }
  103. Dicer.prototype._oninfo = function (isMatch, data, start, end) {
  104. let buf; const self = this; let i = 0; let r; let shouldWriteMore = true
  105. if (!this._part && this._justMatched && data) {
  106. while (this._dashes < 2 && (start + i) < end) {
  107. if (data[start + i] === DASH) {
  108. ++i
  109. ++this._dashes
  110. } else {
  111. if (this._dashes) { buf = B_ONEDASH }
  112. this._dashes = 0
  113. break
  114. }
  115. }
  116. if (this._dashes === 2) {
  117. if ((start + i) < end && this.listenerCount('trailer') !== 0) { this.emit('trailer', data.slice(start + i, end)) }
  118. this.reset()
  119. this._finished = true
  120. // no more parts will be added
  121. if (self._parts === 0) {
  122. self._realFinish = true
  123. self.emit('finish')
  124. self._realFinish = false
  125. }
  126. }
  127. if (this._dashes) { return }
  128. }
  129. if (this._justMatched) { this._justMatched = false }
  130. if (!this._part) {
  131. this._part = new PartStream(this._partOpts)
  132. this._part._read = function (n) {
  133. self._unpause()
  134. }
  135. if (this._isPreamble && this.listenerCount('preamble') !== 0) {
  136. this.emit('preamble', this._part)
  137. } else if (this._isPreamble !== true && this.listenerCount('part') !== 0) {
  138. this.emit('part', this._part)
  139. } else {
  140. this._ignore()
  141. }
  142. if (!this._isPreamble) { this._inHeader = true }
  143. }
  144. if (data && start < end && !this._ignoreData) {
  145. if (this._isPreamble || !this._inHeader) {
  146. if (buf) { shouldWriteMore = this._part.push(buf) }
  147. shouldWriteMore = this._part.push(data.slice(start, end))
  148. if (!shouldWriteMore) { this._pause = true }
  149. } else if (!this._isPreamble && this._inHeader) {
  150. if (buf) { this._hparser.push(buf) }
  151. r = this._hparser.push(data.slice(start, end))
  152. if (!this._inHeader && r !== undefined && r < end) { this._oninfo(false, data, start + r, end) }
  153. }
  154. }
  155. if (isMatch) {
  156. this._hparser.reset()
  157. if (this._isPreamble) { this._isPreamble = false } else {
  158. if (start !== end) {
  159. ++this._parts
  160. this._part.on('end', function () {
  161. if (--self._parts === 0) {
  162. if (self._finished) {
  163. self._realFinish = true
  164. self.emit('finish')
  165. self._realFinish = false
  166. } else {
  167. self._unpause()
  168. }
  169. }
  170. })
  171. }
  172. }
  173. this._part.push(null)
  174. this._part = undefined
  175. this._ignoreData = false
  176. this._justMatched = true
  177. this._dashes = 0
  178. }
  179. }
  180. Dicer.prototype._unpause = function () {
  181. if (!this._pause) { return }
  182. this._pause = false
  183. if (this._cb) {
  184. const cb = this._cb
  185. this._cb = undefined
  186. cb()
  187. }
  188. }
  189. module.exports = Dicer