[, , src, dest] = process.argv const srcStream = createReadStream(src) const destStream = createWriteStream(dest) srcStream.on('data', data => { const canContinue = destStream.write(data) if (!canContinue) { // we are overflowing the destination, we should pause srcStream.pause() // we will resume when the destination stream is drained destStream.once('drain', () => srcStream.resume()) } }) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 const canContinue = destStream.write(data) // stream-copy-safe.js 1 2 const { createReadStream, createWriteStream } = require('fs') 3 4 const [, , src, dest] = process.argv 5 const srcStream = createReadStream(src) 6 const destStream = createWriteStream(dest) 7 8 srcStream.on('data', data => { 9 10 if (!canContinue) { 11 // we are overflowing the destination, we should pause 12 srcStream.pause() 13 // we will resume when the destination stream is drained 14 destStream.once('drain', () => srcStream.resume()) 15 } 16 }) 17 if (!canContinue) { } // stream-copy-safe.js 1 2 const { createReadStream, createWriteStream } = require('fs') 3 4 const [, , src, dest] = process.argv 5 const srcStream = createReadStream(src) 6 const destStream = createWriteStream(dest) 7 8 srcStream.on('data', data => { 9 const canContinue = destStream.write(data) 10 11 // we are overflowing the destination, we should pause 12 srcStream.pause() 13 // we will resume when the destination stream is drained 14 destStream.once('drain', () => srcStream.resume()) 15 16 }) 17 // we are overflowing the destination, we should pause srcStream.pause() // stream-copy-safe.js 1 2 const { createReadStream, createWriteStream } = require('fs') 3 4 const [, , src, dest] = process.argv 5 const srcStream = createReadStream(src) 6 const destStream = createWriteStream(dest) 7 8 srcStream.on('data', data => { 9 const canContinue = destStream.write(data) 10 if (!canContinue) { 11 12 13 // we will resume when the destination stream is drained 14 destStream.once('drain', () => srcStream.resume()) 15 } 16 }) 17 // we will resume when the destination stream is drained destStream.once('drain', () => srcStream.resume()) // stream-copy-safe.js 1 2 const { createReadStream, createWriteStream } = require('fs') 3 4 const [, , src, dest] = process.argv 5 const srcStream = createReadStream(src) 6 const destStream = createWriteStream(dest) 7 8 srcStream.on('data', data => { 9 const canContinue = destStream.write(data) 10 if (!canContinue) { 11 // we are overflowing the destination, we should pause 12 srcStream.pause() 13 14 15 } 16 }) 17 const canContinue = destStream.write(data) if (!canContinue) { // we are overflowing the destination, we should pause srcStream.pause() // we will resume when the destination stream is drained destStream.once('drain', () => srcStream.resume()) } // stream-copy-safe.js 1 2 const { createReadStream, createWriteStream } = require('fs') 3 4 const [, , src, dest] = process.argv 5 const srcStream = createReadStream(src) 6 const destStream = createWriteStream(dest) 7 8 srcStream.on('data', data => { 9 10 11 12 13 14 15 16 }) 17 @loige 45