其他分享
首页 > 其他分享> > js: TransformStream 浏览器上的流转换

js: TransformStream 浏览器上的流转换

作者:互联网

class UpperCaseTransformStream {
  constructor() {
    return new TransformStream({
      transform(chunk, controller) {
        controller.enqueue(chunk.toUpperCase());
      },
    });
  }
}

(async () => {
  const response = await fetch("/index.js");
  const readableStream = response.body
    .pipeThrough(new TextDecoderStream()) // 将 uint8 转换为 text
    .pipeThrough(new UpperCaseTransformStream());

  const reader = readableStream.getReader();
  let out = "";
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      break;
    }
    out += value;
  }
  console.assert(out === 'CONSOLE.LOG(THIS);');
})();

See also:

标签:const,chunk,value,js,TransformStream,done,new,浏览器,out
来源: https://www.cnblogs.com/ajanuw/p/16527499.html