Skip to content

Instantly share code, notes, and snippets.

@watson
Last active September 1, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watson/8f9ce58b77ea4fa193c56f48474bb9b5 to your computer and use it in GitHub Desktop.
Save watson/8f9ce58b77ea4fa193c56f48474bb9b5 to your computer and use it in GitHub Desktop.
Testing write on corked stream in combination with destroy for readable-stream@3
'use strict'
const { Writable } = require('readable-stream')
let stream
console.log('write')
stream = getStream()
stream.write('foo')
console.log('write + destroy')
stream = getStream()
stream.write('foo')
stream.destroy()
console.log('cork + write + uncork')
stream = getStream()
stream.cork()
stream.write('foo')
stream.uncork()
console.log('cork + write + uncork + destroy')
stream = getStream()
stream.cork()
stream.write('foo')
stream.uncork()
stream.destroy()
console.log('cork + write + destroy + uncork')
stream = getStream()
stream.cork()
stream.write('foo')
stream.destroy()
stream.uncork()
function getStream () {
if (!getStream.n) getStream.n = 0
const n = ++getStream.n
const stream = new Writable({
write (chunk, enc, cb) {
console.log(` ${n}: write:`, chunk.toString())
cb()
}
})
stream.on('error', err => {
console.log(` ${n}: error event:`, err.message)
})
return stream
}
write
1: write: foo
write + destroy
2: write: foo
cork + write + uncork
3: write: foo
cork + write + uncork + destroy
4: write: foo
cork + write + destroy + uncork
5: error event: Cannot call write after a stream was destroyed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment