Files
rappaurio-sae501_502/app/node_modules/hyntax/lib/stream-tokenizer.js
2023-09-25 09:41:55 +02:00

52 lines
956 B
JavaScript

const { Transform } = require('stream')
const tokenize = require('./tokenize')
class StreamTokenizer extends Transform {
constructor (options) {
super(Object.assign(
{},
options,
{
decodeStrings: false,
readableObjectMode: true
}
))
this.currentTokenizerState = undefined
this.setDefaultEncoding('utf8')
}
_transform (chunk, encoding, callback) {
let chunkString = chunk
if (Buffer.isBuffer(chunk)) {
chunkString = chunk.toString()
}
const { state, tokens } = tokenize(
chunkString,
this.currentTokenizerState,
{ isFinalChunk: false }
)
this.currentTokenizerState = state
callback(null, tokens)
}
_flush (callback) {
const tokenizeResults = tokenize(
'',
this.currentTokenizerState,
{ isFinalChunk: true }
)
this.push(tokenizeResults.tokens)
callback()
}
}
module.exports = StreamTokenizer