Created
February 13, 2021 22:20
-
-
Save eemeli/2436bda974549eb24ee0696bd8f6368b to your computer and use it in GitHub Desktop.
yaml@2 Node.js Transform stream implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Transform } from 'stream' | |
import { StringDecoder } from 'string_decoder' | |
import { Composer, Parser } from 'yaml' | |
/** | |
* A Transform stream that accepts either strings or Buffers as input, and | |
* emits YAML Documents. | |
* | |
* Calling the stream's `end()` method may be required to emit the last | |
* document. | |
* | |
* ```js | |
* import { createReadStream } from 'fs' | |
* const source = createReadStream('input.yaml') | |
* const docStream = new DocStream() | |
* docStream.on('data', doc => console.log(doc.toJS())) | |
* source.pipe(docStream) | |
* ``` | |
*/ | |
export class DocStream extends Transform { | |
constructor(options = {}) { | |
super({ | |
...options, | |
decodeStrings: false, | |
emitClose: true, | |
objectMode: true | |
}) | |
this.composer = new Composer(doc => this.push(doc)) | |
this.decoder = new StringDecoder(options.defaultEncoding || 'utf8') | |
this.parser = new Parser(this.composer.next) | |
} | |
_flush(done) { | |
this.parser.parse('', false) | |
this.composer.end() | |
done() | |
} | |
_transform(chunk, _, done) { | |
try { | |
const src = Buffer.isBuffer(chunk) ? this.decoder.write(chunk) : chunk | |
this.parser.parse(src, true) | |
done() | |
} catch (error) { | |
done(error) // should never happen | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment