text-line-transform-stream.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
  2. // This module is browser compatible.
  3. // Modified by Sukka (https://skk.moe) to increase compatibility and performance with Bun.
  4. interface TextLineStreamOptions {
  5. /** Allow splitting by solo \r */
  6. allowCR: boolean
  7. }
  8. /** Transform a stream into a stream where each chunk is divided by a newline,
  9. * be it `\n` or `\r\n`. `\r` can be enabled via the `allowCR` option.
  10. *
  11. * ```ts
  12. * import { TextLineStream } from 'https://deno.land/std@$STD_VERSION/streams/text_line_stream.ts';
  13. * const res = await fetch('https://example.com');
  14. * const lines = res.body!
  15. * .pipeThrough(new TextDecoderStream())
  16. * .pipeThrough(new TextLineStream());
  17. * ```
  18. */
  19. export class TextLineStream extends TransformStream<string, string> {
  20. // private __buf = '';
  21. constructor(options?: TextLineStreamOptions) {
  22. const allowCR = options?.allowCR ?? false;
  23. let __buf = '';
  24. super({
  25. transform(chunk, controller) {
  26. chunk = __buf + chunk;
  27. for (; ;) {
  28. const lfIndex = chunk.indexOf('\n');
  29. if (allowCR) {
  30. const crIndex = chunk.indexOf('\r');
  31. if (
  32. crIndex !== -1 && crIndex !== (chunk.length - 1)
  33. && (lfIndex === -1 || (lfIndex - 1) > crIndex)
  34. ) {
  35. controller.enqueue(chunk.slice(0, crIndex));
  36. chunk = chunk.slice(crIndex + 1);
  37. continue;
  38. }
  39. }
  40. if (lfIndex !== -1) {
  41. let crOrLfIndex = lfIndex;
  42. if (chunk[lfIndex - 1] === '\r') {
  43. crOrLfIndex--;
  44. }
  45. controller.enqueue(chunk.slice(0, crOrLfIndex));
  46. chunk = chunk.slice(lfIndex + 1);
  47. continue;
  48. }
  49. break;
  50. }
  51. __buf = chunk;
  52. },
  53. flush(controller) {
  54. if (__buf.length > 0) {
  55. // eslint-disable-next-line sukka-ts/string/prefer-string-starts-ends-with -- performance
  56. if (allowCR && __buf[__buf.length - 1] === '\r') {
  57. controller.enqueue(__buf.slice(0, -1));
  58. } else {
  59. controller.enqueue(__buf);
  60. }
  61. }
  62. }
  63. });
  64. }
  65. }