wtt
2024-11-11 6467554bd4fd68cd6c95cd9dd1f8f0265021fc53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 将原本可能截断成多个字符串的内容按顺序合并一个字符串
class LineBreakTransformer {
    constructor() {
        // 保存流数据直到新行出现的容器
        this.container = "";
    }
 
    transform(chunk, controller) {
        // 将新块追加到现有块。
        this.container += chunk;
        // 对于每一行分段,将解析后的行发送出去。
        const lines = this.container.split("\r");
        this.container = lines.pop();
        lines.forEach((line) => controller.enqueue(line));
    }
 
    flush(controller) {
        // 当流关闭时,清除所有剩余的块。
        controller.enqueue(this.container);
    }
}