版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

stream-tokenizer.js 956 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const { Transform } = require('stream')
  2. const tokenize = require('./tokenize')
  3. class StreamTokenizer extends Transform {
  4. constructor (options) {
  5. super(Object.assign(
  6. {},
  7. options,
  8. {
  9. decodeStrings: false,
  10. readableObjectMode: true
  11. }
  12. ))
  13. this.currentTokenizerState = undefined
  14. this.setDefaultEncoding('utf8')
  15. }
  16. _transform (chunk, encoding, callback) {
  17. let chunkString = chunk
  18. if (Buffer.isBuffer(chunk)) {
  19. chunkString = chunk.toString()
  20. }
  21. const { state, tokens } = tokenize(
  22. chunkString,
  23. this.currentTokenizerState,
  24. { isFinalChunk: false }
  25. )
  26. this.currentTokenizerState = state
  27. callback(null, tokens)
  28. }
  29. _flush (callback) {
  30. const tokenizeResults = tokenize(
  31. '',
  32. this.currentTokenizerState,
  33. { isFinalChunk: true }
  34. )
  35. this.push(tokenizeResults.tokens)
  36. callback()
  37. }
  38. }
  39. module.exports = StreamTokenizer