Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Created July 25, 2017 14:29
Show Gist options
  • Save jazzyjackson/0a378d6386e8fcd96776e130c1ce8353 to your computer and use it in GitHub Desktop.
Save jazzyjackson/0a378d6386e8fcd96776e130c1ce8353 to your computer and use it in GitHub Desktop.
consumeStream(reader, contentType = 'application/json'){
if(!reader) return null // consumeStream will exit if the text was consumed already
this.streambuffer || (this.streambuffer = '') //if streambuffer is undefined, create it
/* recursively call consumeStream. reader.read() is a promise that resolves as soon as a chunk of data is available */
return reader.read().then(sample => {
if(sample.value){
this.streambuffer += textDecoder.decode(sample.value)
console.log(this.streambuffer)
// if the last character of a chunk of data is a closing bracket, parse the JSON. Otherwise, keep consuming stream until it hits a closing bracket.
// this leaves the very unfortunate possible bug of a chunk of data coming in with an escaped bracket at the end, and to detect this condition we'd have to pay attention to opening and closing quotes, except for escaped qutoes
if(contentType = 'application/json' && this.streambuffer.match(/}\s*$/)){
console.log('gonna parse')
this.streambuffer.split(/\n(?={)/g).forEach(JSONchunk => this.output = JSON.parse(JSONchunk))
delete this.streambuffer
} else if(contentType == 'plain/text'){
this.output = this.streambuffer
delete this.streambuffer
}
return this.consumeStream(reader)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment