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