import { Transform } from 'stream' ; param { String } splitChunk return { Transform } export function createSplitStream ( splitChunk ) { let unsplitBuffer = Buffer . alloc ( 0 ) ; return new Transform ( { writableObjectMode : false , readableObjectMode : true , transform ( chunk , enc , callback ) { try { let i ; let toSplit = Buffer . concat ( [ unsplitBuffer , chunk ] ) ; while ( ( i = toSplit . indexOf ( splitChunk ) ) !== - 1 ) { const slice = toSplit . slice ( 0 , i ) ; toSplit = toSplit . slice ( i + splitChunk . length ) ; this . push ( slice . toString ( 'utf8' ) ) ; } unsplitBuffer = toSplit ; callback ( ) ; } catch ( err ) { callback ( err ) ; } } , flush ( callback ) { try { this . push ( unsplitBuffer . toString ( 'utf8' ) ) ; callback ( ) ; } catch ( err ) { callback ( err ) ; } } , } ) ; }