Skip to content

Commit

Permalink
fix(middleware): prevent any tries reading from the same stream twice (
Browse files Browse the repository at this point in the history
…#2)

BREAKING CHANGE: `stream` property is replaced with `toStream` method that allows the user to create a new readable stream from the temp file each time.
  • Loading branch information
derevnjuk authored Mar 9, 2020
1 parent 167c0eb commit d9134cd
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 82 deletions.
6 changes: 2 additions & 4 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const is = require('type-is')
const { createReadStream, unlink } = require('fs')
const { createReadStream } = require('fs')
const appendField = require('append-field')
const { createFileAppender } = require('./file-appender')
const readBody = require('./read-body')
Expand All @@ -17,9 +17,7 @@ const handleRequest = async (setup, req) => {
const fileAppender = createFileAppender(options.fileStrategy, req, options.fields)

for (const file of result.files) {
file.stream = createReadStream(file.path)
file.stream.once('open', () => unlink(file.path, () => {}))

file.toStream = () => createReadStream(file.path)
fileAppender.append(file)
}
}
Expand Down
Loading

0 comments on commit d9134cd

Please sign in to comment.