Skip to content

Commit

Permalink
fix: call fs.createReadStream lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
mbargiel committed Feb 17, 2024
1 parent 13f0378 commit e344eb5
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ module.exports = class Interceptor {
if (!fs) {
throw new Error('No fs')
}
const readStream = fs.createReadStream(filePath)
readStream.pause()
this.filePath = filePath
return this.reply(statusCode, readStream, headers)
return this.reply(statusCode, () => {
const readStream = fs.createReadStream(filePath)
readStream.pause()
return readStream
}, headers)
}

// Also match request headers
Expand Down Expand Up @@ -453,15 +455,6 @@ module.exports = class Interceptor {
markConsumed() {
this.interceptionCounter++

if (
(this.scope.shouldPersist() || this.counter > 0) &&
this.interceptionCounter > 1 &&
this.filePath
) {
this.body = fs.createReadStream(this.filePath)
this.body.pause()
}

remove(this)

if (!this.scope.shouldPersist() && this.counter < 1) {
Expand Down

0 comments on commit e344eb5

Please sign in to comment.