text-line-transform-stream.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. super({
  24. transform: (chunk, controller) => {
  25. chunk = this.__buf + chunk;
  26. for (; ;) {
  27. const lfIndex = chunk.indexOf('\n');
  28. if (allowCR) {
  29. const crIndex = chunk.indexOf('\r');
  30. if (
  31. crIndex !== -1 && crIndex !== (chunk.length - 1) &&
  32. (lfIndex === -1 || (lfIndex - 1) > crIndex)
  33. ) {
  34. controller.enqueue(chunk.slice(0, crIndex));
  35. chunk = chunk.slice(crIndex + 1);
  36. continue;
  37. }
  38. }
  39. if (lfIndex !== -1) {
  40. let crOrLfIndex = lfIndex;
  41. if (chunk[lfIndex - 1] === '\r') {
  42. crOrLfIndex--;
  43. }
  44. controller.enqueue(chunk.slice(0, crOrLfIndex));
  45. chunk = chunk.slice(lfIndex + 1);
  46. continue;
  47. }
  48. break;
  49. }
  50. this.__buf = chunk;
  51. },
  52. flush: (controller) => {
  53. if (this.__buf.length > 0) {
  54. if (allowCR && this.__buf[this.__buf.length - 1] === '\r') {
  55. controller.enqueue(this.__buf.slice(0, -1));
  56. } else {
  57. controller.enqueue(this.__buf);
  58. };
  59. }
  60. },
  61. });
  62. }
  63. }