nl_tokens: Intercepts the write and end methods of the given writable stream and buffers the values written to them instead . When the stream ends the buffered value is returned and the original write and end methods are restored .
pl_tokens: function interceptWriteStream ( writeStream ) { return new Promise ( ( resolve , reject ) => { let response = "" ; let writeBkp = writeStream . write ; let sendBkp = writeStream . send ; // For express response streams let endBkp = writeStream . end ; writeStream . write = function ( chunk , encoding , callback ) { response += chunk ; if ( typeof encoding == "function" ) { encoding ( ) ; } else if ( callback ) { callback ( ) ; } return true ; } ; writeStream . send = function ( body ) { writeStream . end ( body ) ; } ; writeStream . end = function ( chunk , encoding , callback ) { if ( chunk ) { response += chunk ; } writeStream . write = writeBkp ; writeStream . send = sendBkp ; writeStream . end = endBkp ; if ( typeof encoding == "function" ) { writeStream . once ( "finish" , encoding ) ; } else if ( <mask> ) { writeStream . once ( "finish" , encoding ) ; } resolve ( response ) ; } ; } ) ; }