Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextDecoderStream() is not defined

I am learning from google codelab example Getting started with the Web Serial API

The example is on glitch so I remixed the code and follow the instructions and copied and pasted these code

let decoder = new TextDecoderStream();
inputDone = port.readable.pipeTo(decoder.writable);
inputStream = decoder.readable;

reader = inputStream.getReader();
readLoop();

Then I got 'TextDecoderStream' is not defined.

like image 284
user14084021 Avatar asked Oct 14 '25 19:10

user14084021


1 Answers

If it's not supported you can make a poly fill https://developer.mozilla.org/en-US/docs/Web/API/TransformStream

const tds = {
  start(){
    this.decoder = new TextDecoder(this.encoding, this.options)
  },
  transform(chunk, controller) {
    controller.enqueue(this.decoder.decode(chunk))
  }
}
let _jstds_wm = new WeakMap(); /* info holder */
class TextDecoderStream extends TransformStream {
  constructor(encoding = 'utf-8', {...options} = {}) {
    let t = {...tds, encoding, options}

    super(t)
    _jstds_wm.set(this, t)
  }
  get encoding() {return _jstds_wm.get(this).decoder.encoding}
  get fatal() {return _jstds_wm.get(this).decoder.fatal}
  get ignoreBOM() {return _jstds_wm.get(this).decoder.ignoreBOM}
}
like image 103
B''H Bi'ezras -- Boruch Hashem Avatar answered Oct 17 '25 09:10

B''H Bi'ezras -- Boruch Hashem