File size: 2,153 Bytes
d4b85c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Vendored from Deno - 
// https://github.com/denoland/deno_std/blob/main/streams/delimiter.ts
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

/** Transform a stream into a stream where each chunk is divided by a newline,
 * be it `\n` or `\r\n`. `\r` can be enabled via the `allowCR` option.
 *
 * ```ts
 * import { TextLineStream } from "./delimiter.ts";
 * const res = await fetch("https://example.com");
 * const lines = res.body!
 *   .pipeThrough(new TextDecoderStream())
 *   .pipeThrough(new TextLineStream());
 * ```
 */
class TextLineStream extends TransformStream {
  #buf = "";
  #allowCR = false;
  #returnEmptyLines = false;
  #mapperFun = line => line;
  
  constructor(options) {
    super({
      transform: (chunk, controller) => this.#handle(chunk, controller),
      flush: (controller) => this.#handle("\r\n", controller),
    });
    
    this.#allowCR = options?.allowCR ?? false;
    this.#returnEmptyLines = options?.returnEmptyLines ?? false;
    this.#mapperFun = options?.mapperFun ?? this.#mapperFun;
  }

  #handle(chunk, controller) {
    chunk = this.#buf + chunk;

    for (;;) {
      const lfIndex = chunk.indexOf("\n");

      if (this.#allowCR) {
        const crIndex = chunk.indexOf("\r");

        if (
          crIndex !== -1 && crIndex !== (chunk.length - 1) &&
          (lfIndex === -1 || (lfIndex - 1) > crIndex)
        ) {
          const curChunk = this.#mapperFun(chunk.slice(0, crOrLfIndex));
          if (this.#returnEmptyLines || curChunk) {            
            controller.enqueue(curChunk);
          }
          chunk = chunk.slice(crIndex + 1);
          continue;
        }
      }

      if (lfIndex !== -1) {
        let crOrLfIndex = lfIndex;
        if (chunk[lfIndex - 1] === "\r") {
          crOrLfIndex--;
        }
        const curChunk = this.#mapperFun(chunk.slice(0, crOrLfIndex));
        if (this.#returnEmptyLines || curChunk) {          
          controller.enqueue(curChunk);
        }
        chunk = chunk.slice(lfIndex + 1);
        continue;
      }

      break;
    }

    this.#buf = chunk;
  }
}

export { TextLineStream as default };