(_self, _opts)
| 21 | // of the passed `options` (second) param, because when you decide |
| 22 | // to test the plugin you can pass custom `this` context to it (and so `this.options`) |
| 23 | async function init(_self, _opts) { |
| 24 | this.type = octetStreamType; |
| 25 | const originalFilename = this.headers['x-file-name']; |
| 26 | const mimetype = this.headers['content-type']; |
| 27 | |
| 28 | const thisPart = { |
| 29 | originalFilename, |
| 30 | mimetype, |
| 31 | }; |
| 32 | const newFilename = this._getNewName(thisPart); |
| 33 | const filepath = this._joinDirectoryName(newFilename); |
| 34 | const file = await this._newFile({ |
| 35 | newFilename, |
| 36 | filepath, |
| 37 | originalFilename, |
| 38 | mimetype, |
| 39 | }); |
| 40 | |
| 41 | this.emit('fileBegin', originalFilename, file); |
| 42 | file.open(); |
| 43 | this.openedFiles.push(file); |
| 44 | this._flushing += 1; |
| 45 | |
| 46 | this._parser = new OctetStreamParser(this.options); |
| 47 | |
| 48 | // Keep track of writes that haven't finished so we don't emit the file before it's done being written |
| 49 | let outstandingWrites = 0; |
| 50 | |
| 51 | this._parser.on('data', (buffer) => { |
| 52 | this.pause(); |
| 53 | outstandingWrites += 1; |
| 54 | |
| 55 | file.write(buffer, () => { |
| 56 | outstandingWrites -= 1; |
| 57 | this.resume(); |
| 58 | |
| 59 | if (this.ended) { |
| 60 | this._parser.emit('doneWritingFile'); |
| 61 | } |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | this._parser.on('end', () => { |
| 66 | this._flushing -= 1; |
| 67 | this.ended = true; |
| 68 | |
| 69 | const done = () => { |
| 70 | file.end(() => { |
| 71 | this.emit('file', 'file', file); |
| 72 | this._maybeEnd(); |
| 73 | }); |
| 74 | }; |
| 75 | |
| 76 | if (outstandingWrites === 0) { |
| 77 | done(); |
| 78 | } else { |
| 79 | this._parser.once('doneWritingFile', done); |
| 80 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…