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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 |
x1 x1 x1 x1 x1 x2 x1 x1 x1 x1 x2 x1 x1 x1 x1 x1 x1 x1 x1 x1 x2 x1 x1 x1 x1 x1 x1 x1 x1 x1 x2 x1 x1 x1 x1 x2 x1 x1 x1 x1 x1 x1 x1 x1 x1 x2 x1 x1 x1 x1 x2 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 |
/**
* Utilities for working with the
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Streams API}.
*
* Includes buffering and conversion.
*
* ```ts
* import { toText } from "@std/streams";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["Hello, world!"]);
* const text = await toText(stream);
*
* assertEquals(text, "Hello, world!");
* ```
*
* @module
*/
import type { BufferBytesOptions as _interface_BufferBytesOptions } from "jsr:@std/[email protected]"
/**
* Options for {@linkcode Buffer.bytes}.
*/
interface BufferBytesOptions extends _interface_BufferBytesOptions {}
export type { BufferBytesOptions }
import { Buffer as _class_Buffer } from "jsr:@std/[email protected]"
/**
* A variable-sized buffer of bytes with `readable` and `writable` getters that
* allows you to work with {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API}.
*
* Buffer is almost always used with some I/O like files and sockets. It allows
* one to buffer up a download from a socket. Buffer grows and shrinks as
* necessary.
*
* Buffer is NOT the same thing as Node's Buffer. Node's Buffer was created in
* 2009 before JavaScript had the concept of ArrayBuffers. It's simply a
* non-standard ArrayBuffer.
*
* ArrayBuffer is a fixed memory allocation. Buffer is implemented on top of
* ArrayBuffer.
*
* Based on {@link https://golang.org/pkg/bytes/#Buffer | Go Buffer}.
*
* @example Buffer input bytes and convert it to a string
* ```ts
* import { Buffer } from "@std/streams/buffer";
* import { toText } from "@std/streams/to-text";
* import { assert } from "@std/assert";
* import { assertEquals } from "@std/assert";
*
* // Create a new buffer
* const buf = new Buffer();
* assertEquals(buf.capacity, 0);
* assertEquals(buf.length, 0);
*
* // Dummy input stream
* const inputStream = ReadableStream.from([
* "hello, ",
* "world",
* "!",
* ]);
*
* // Pipe the input stream to the buffer
* await inputStream.pipeThrough(new TextEncoderStream()).pipeTo(buf.writable);
* assert(buf.capacity > 0);
* assert(buf.length > 0);
*
* // Convert the buffered bytes to a string
* const result = await toText(buf.readable);
* assertEquals(result, "hello, world!");
* assert(buf.empty());
* ```
*/
class Buffer extends _class_Buffer {}
export { Buffer }
import { ByteSliceStream as _class_ByteSliceStream } from "jsr:@std/[email protected]"
/**
* A transform stream that only transforms from the zero-indexed `start` and
* `end` bytes (both inclusive).
*
* @example Basic usage
* ```ts
* import { ByteSliceStream } from "@std/streams/byte-slice-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* new Uint8Array([0, 1]),
* new Uint8Array([2, 3, 4]),
* ]);
* const slicedStream = stream.pipeThrough(new ByteSliceStream(1, 3));
*
* assertEquals(
* await Array.fromAsync(slicedStream),
* [new Uint8Array([1]), new Uint8Array([2, 3])]
* );
* ```
*
* @example Get a range of bytes from a fetch response body
* ```ts
* import { ByteSliceStream } from "@std/streams/byte-slice-stream";
* import { assertEquals } from "@std/assert";
*
* const response = await fetch("https://example.com");
* const rangedStream = response.body!
* .pipeThrough(new ByteSliceStream(3, 8));
* const collected = await Array.fromAsync(rangedStream);
* assertEquals(collected[0]?.length, 6);
* ```
*/
class ByteSliceStream extends _class_ByteSliceStream {}
export { ByteSliceStream }
import { concatReadableStreams as _function_concatReadableStreams } from "jsr:@std/[email protected]"
/**
* Concatenates multiple `ReadableStream`s into a single ordered
* `ReadableStream`.
*
* Cancelling the resulting stream will cancel all the input streams.
*
* @template T The type of the chunks in the streams.
* @param streams An iterable of `ReadableStream`s to concat.
* @return A `ReadableStream` that will emit the concatenated chunks.
*
* @example Usage
* ```ts
* import { concatReadableStreams } from "@std/streams/concat-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from([1, 2, 3]);
* const stream2 = ReadableStream.from([4, 5, 6]);
* const stream3 = ReadableStream.from([7, 8, 9]);
*
* assertEquals(
* await Array.fromAsync(concatReadableStreams(stream1, stream2, stream3)),
* [1, 2, 3, 4, 5, 6, 7, 8, 9],
* );
* ```
*/
const concatReadableStreams = _function_concatReadableStreams as typeof _function_concatReadableStreams
export { concatReadableStreams }
import type { DelimiterDisposition as _typeAlias_DelimiterDisposition } from "jsr:@std/[email protected]"
/**
* Disposition of the delimiter for {@linkcode DelimiterStreamOptions}.
*/
type DelimiterDisposition = _typeAlias_DelimiterDisposition
export type { DelimiterDisposition }
import type { DelimiterStreamOptions as _interface_DelimiterStreamOptions } from "jsr:@std/[email protected]"
/**
* Options for {@linkcode DelimiterStream}.
*/
interface DelimiterStreamOptions extends _interface_DelimiterStreamOptions {}
export type { DelimiterStreamOptions }
import { DelimiterStream as _class_DelimiterStream } from "jsr:@std/[email protected]"
/**
* Divide a stream into chunks delimited by a given byte sequence.
*
* If you are working with a stream of `string`, consider using {@linkcode TextDelimiterStream}.
*
* @example Divide a CSV stream by commas, discarding the commas:
* ```ts
* import { DelimiterStream } from "@std/streams/delimiter-stream";
* import { assertEquals } from "@std/assert";
*
* const inputStream = ReadableStream.from(["foo,bar", ",baz"]);
*
* const transformed = inputStream.pipeThrough(new TextEncoderStream())
* .pipeThrough(new DelimiterStream(new TextEncoder().encode(",")))
* .pipeThrough(new TextDecoderStream());
*
* assertEquals(await Array.fromAsync(transformed), ["foo", "bar", "baz"]);
* ```
*
* @example Divide a stream after semi-colons, keeping the semicolons in the output:
* ```ts
* import { DelimiterStream } from "@std/streams/delimiter-stream";
* import { assertEquals } from "@std/assert";
*
* const inputStream = ReadableStream.from(["foo;", "bar;baz", ";"]);
*
* const transformed = inputStream.pipeThrough(new TextEncoderStream())
* .pipeThrough(
* new DelimiterStream(new TextEncoder().encode(";"), {
* disposition: "suffix",
* }),
* ).pipeThrough(new TextDecoderStream());
*
* assertEquals(await Array.fromAsync(transformed), ["foo;", "bar;", "baz;"]);
* ```
*/
class DelimiterStream extends _class_DelimiterStream {}
export { DelimiterStream }
import { earlyZipReadableStreams as _function_earlyZipReadableStreams } from "jsr:@std/[email protected]"
/**
* Merge multiple streams into a single one, taking order into account, and each
* stream will wait for a chunk to enqueue before the next stream can append
* another chunk.
*
* If a stream ends before other ones, the others will be cancelled after the
* last chunk of said stream is read. See the examples below for more
* comprehensible information. If you want to continue reading the other streams
* even after one of them ends, use {@linkcode zipReadableStreams}.
*
* @template T The type of the chunks in the input streams.
* @return A `ReadableStream` that will emit the zipped chunks
*
* @example Zip 2 streams with the same length
* ```ts
* import { earlyZipReadableStreams } from "@std/streams/early-zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1", "2", "3"]);
* const stream2 = ReadableStream.from(["a", "b", "c"]);
* const zippedStream = earlyZipReadableStreams(stream1, stream2);
*
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "2", "b", "3", "c"],
* );
* ```
*
* @example Zip 2 streams with different length (first one is shorter)
* ```ts
* import { earlyZipReadableStreams } from "@std/streams/early-zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1", "2"]);
* const stream2 = ReadableStream.from(["a", "b", "c", "d"]);
* const zippedStream = earlyZipReadableStreams(stream1, stream2);
*
* // The first stream ends before the second one. When the first stream ends,
* // the second one is cancelled and no more data is read or added to the
* // zipped stream.
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "2", "b"],
* );
* ```
*
* @example Zip 2 streams with different length (first one is longer)
* ```ts
* import { earlyZipReadableStreams } from "@std/streams/early-zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1", "2", "3", "4"]);
* const stream2 = ReadableStream.from(["a", "b"]);
* const zippedStream = earlyZipReadableStreams(stream1, stream2);
*
* // The second stream ends before the first one. When the second stream ends,
* // the first one is cancelled, but the chunk of "3" is already read so it
* // is added to the zipped stream.
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "2", "b", "3"],
* );
* ```
*
* @example Zip 3 streams
* ```ts
* import { earlyZipReadableStreams } from "@std/streams/early-zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1"]);
* const stream2 = ReadableStream.from(["a", "b"]);
* const stream3 = ReadableStream.from(["A", "B", "C"]);
* const zippedStream = earlyZipReadableStreams(stream1, stream2, stream3);
*
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "A"],
* );
* ```
*/
const earlyZipReadableStreams = _function_earlyZipReadableStreams as typeof _function_earlyZipReadableStreams
export { earlyZipReadableStreams }
import type { LimitedBytesTransformStreamOptions as _interface_LimitedBytesTransformStreamOptions } from "jsr:@std/[email protected]"
/**
* Options for {@linkcode LimitedBytesTransformStream}.
*/
interface LimitedBytesTransformStreamOptions extends _interface_LimitedBytesTransformStreamOptions {}
export type { LimitedBytesTransformStreamOptions }
import { LimitedBytesTransformStream as _class_LimitedBytesTransformStream } from "jsr:@std/[email protected]"
/**
* A {@linkcode TransformStream} that will only read & enqueue chunks until the
* total amount of enqueued data exceeds `size`. The last chunk that would
* exceed the limit will NOT be enqueued, in which case a {@linkcode RangeError}
* is thrown when `options.error` is set to true, otherwise the stream is just
* terminated.
*
* @example `size` is equal to the total byte length of the chunks
* ```ts
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["1234", "5678"]);
* const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
* new LimitedBytesTransformStream(8),
* ).pipeThrough(new TextDecoderStream());
*
* assertEquals(
* await Array.fromAsync(transformed),
* ["1234", "5678"],
* );
* ```
*
* @example `size` is less than the total byte length of the chunks, and at the
* boundary of the chunks
* ```ts
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["1234", "5678"]);
* const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
* // `4` is the boundary of the chunks
* new LimitedBytesTransformStream(4),
* ).pipeThrough(new TextDecoderStream());
*
* assertEquals(
* await Array.fromAsync(transformed),
* // The first chunk was read, but the second chunk was not
* ["1234"],
* );
* ```
*
* @example `size` is less than the total byte length of the chunks, and not at
* the boundary of the chunks
* ```ts
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["1234", "5678"]);
* const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
* // `5` is not the boundary of the chunks
* new LimitedBytesTransformStream(5),
* ).pipeThrough(new TextDecoderStream());
*
* assertEquals(
* await Array.fromAsync(transformed),
* // The second chunk was not read because it would exceed the specified size
* ["1234"],
* );
* ```
*
* @example Throw error when the total byte length of the chunks exceeds the
* specified size
*
* To do so, set `options.error` to `true`.
*
* ```ts
* import { LimitedBytesTransformStream } from "@std/streams/limited-bytes-transform-stream";
* import { assertRejects } from "@std/assert";
*
* const stream = ReadableStream.from(["1234", "5678"]);
* const transformed = stream.pipeThrough(new TextEncoderStream()).pipeThrough(
* new LimitedBytesTransformStream(5, { error: true }),
* ).pipeThrough(new TextDecoderStream());
*
* await assertRejects(async () => {
* await Array.fromAsync(transformed);
* }, RangeError);
* ```
*/
class LimitedBytesTransformStream extends _class_LimitedBytesTransformStream {}
export { LimitedBytesTransformStream }
import type { LimitedTransformStreamOptions as _interface_LimitedTransformStreamOptions } from "jsr:@std/[email protected]"
/**
* Options for {@linkcode LimitedTransformStream}
*/
interface LimitedTransformStreamOptions extends _interface_LimitedTransformStreamOptions {}
export type { LimitedTransformStreamOptions }
import { LimitedTransformStream as _class_LimitedTransformStream } from "jsr:@std/[email protected]"
/**
* A {@linkcode TransformStream} that will only read & enqueue `size` amount of
* chunks.
*
* If `options.error` is set, then instead of terminating the stream,
* a {@linkcode RangeError} will be thrown when the total number of enqueued
* chunks is about to exceed the specified size.
*
* @template T The type the chunks in the stream.
*
* @example `size` is equal to the total number of chunks
* ```ts
* import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["1234", "5678"]);
* const transformed = stream.pipeThrough(
* new LimitedTransformStream(2),
* );
*
* // All chunks were read
* assertEquals(
* await Array.fromAsync(transformed),
* ["1234", "5678"],
* );
* ```
*
* @example `size` is less than the total number of chunks
* ```ts
* import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["1234", "5678"]);
* const transformed = stream.pipeThrough(
* new LimitedTransformStream(1),
* );
*
* // Only the first chunk was read
* assertEquals(
* await Array.fromAsync(transformed),
* ["1234"],
* );
* ```
*
* @example Throw a {@linkcode RangeError} when the total number of chunks is
* about to exceed the specified limit
*
* Do this by setting `options.error` to `true`.
*
* ```ts
* import { LimitedTransformStream } from "@std/streams/limited-transform-stream";
* import { assertRejects } from "@std/assert";
*
* const stream = ReadableStream.from(["1234", "5678"]);
* const transformed = stream.pipeThrough(
* new LimitedTransformStream(1, { error: true }),
* );
*
* await assertRejects(async () => {
* await Array.fromAsync(transformed);
* }, RangeError);
* ```
*/
class LimitedTransformStream<T> extends _class_LimitedTransformStream<T> {}
export { LimitedTransformStream }
import { mergeReadableStreams as _function_mergeReadableStreams } from "jsr:@std/[email protected]"
/**
* Merge multiple streams into a single one, not taking order into account.
* If a stream ends before other ones, the other will continue adding data,
* and the finished one will not add any more data.
*
* @template T The type of the chunks in the input/output streams.
* @param streams An iterable of `ReadableStream`s to merge.
* @return A `ReadableStream` that will emit the merged chunks.
*
* @example Merge 2 streams
* ```ts
* import { mergeReadableStreams } from "@std/streams/merge-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from([1, 2]);
* const stream2 = ReadableStream.from([3, 4, 5]);
*
* const mergedStream = mergeReadableStreams(stream1, stream2);
* const merged = await Array.fromAsync(mergedStream);
* assertEquals(merged.toSorted(), [1, 2, 3, 4, 5]);
* ```
*
* @example Merge 3 streams
* ```ts
* import { mergeReadableStreams } from "@std/streams/merge-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from([1, 2]);
* const stream2 = ReadableStream.from([3, 4, 5]);
* const stream3 = ReadableStream.from([6]);
*
* const mergedStream = mergeReadableStreams(stream1, stream2, stream3);
* const merged = await Array.fromAsync(mergedStream);
* assertEquals(merged.toSorted(), [1, 2, 3, 4, 5, 6]);
* ```
*/
const mergeReadableStreams = _function_mergeReadableStreams as typeof _function_mergeReadableStreams
export { mergeReadableStreams }
import { TextDelimiterStream as _class_TextDelimiterStream } from "jsr:@std/[email protected]"
/**
* Transform a stream `string` into a stream where each chunk is divided by a
* given delimiter.
*
* If you are working with a stream of `Uint8Array`, consider using {@linkcode DelimiterStream}.
*
* If you want to split by a newline, consider using {@linkcode TextLineStream}.
*
* @example Comma-separated values
* ```ts
* import { TextDelimiterStream } from "@std/streams/text-delimiter-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* "alice,20,",
* ",US,",
* ]);
*
* const valueStream = stream.pipeThrough(new TextDelimiterStream(","));
*
* assertEquals(
* await Array.fromAsync(valueStream),
* ["alice", "20", "", "US", ""],
* );
* ```
*
* @example Semicolon-separated values with suffix disposition
* ```ts
* import { TextDelimiterStream } from "@std/streams/text-delimiter-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* "const a = 42;;let b =",
* " true;",
* ]);
*
* const valueStream = stream.pipeThrough(
* new TextDelimiterStream(";", { disposition: "suffix" }),
* );
*
* assertEquals(
* await Array.fromAsync(valueStream),
* ["const a = 42;", ";", "let b = true;", ""],
* );
* ```
*/
class TextDelimiterStream extends _class_TextDelimiterStream {}
export { TextDelimiterStream }
import type { TextLineStreamOptions as _interface_TextLineStreamOptions } from "jsr:@std/[email protected]"
/**
* Options for {@linkcode TextLineStream}.
*/
interface TextLineStreamOptions extends _interface_TextLineStreamOptions {}
export type { TextLineStreamOptions }
import { TextLineStream as _class_TextLineStream } from "jsr:@std/[email protected]"
/**
* 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.
*
* If you want to split by a custom delimiter, consider using {@linkcode TextDelimiterStream}.
*
* @example JSON Lines
* ```ts
* import { TextLineStream } from "@std/streams/text-line-stream";
* import { toTransformStream } from "@std/streams/to-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* '{"name": "Alice", "age": ',
* '30}\n{"name": "Bob", "age"',
* ": 25}\n",
* ]);
*
* type Person = { name: string; age: number };
*
* // Split the stream by newline and parse each line as a JSON object
* const jsonStream = stream.pipeThrough(new TextLineStream())
* .pipeThrough(toTransformStream(async function* (src) {
* for await (const chunk of src) {
* if (chunk.trim().length === 0) {
* continue;
* }
* yield JSON.parse(chunk) as Person;
* }
* }));
*
* assertEquals(
* await Array.fromAsync(jsonStream),
* [{ "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 }],
* );
* ```
*
* @example Allow splitting by `\r`
*
* ```ts
* import { TextLineStream } from "@std/streams/text-line-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* "CR\rLF",
* "\nCRLF\r\ndone",
* ]).pipeThrough(new TextLineStream({ allowCR: true }));
*
* const lines = await Array.fromAsync(stream);
*
* assertEquals(lines, ["CR", "LF", "CRLF", "done"]);
* ```
*/
class TextLineStream extends _class_TextLineStream {}
export { TextLineStream }
import { toArrayBuffer as _function_toArrayBuffer } from "jsr:@std/[email protected]"
/**
* Converts a {@linkcode ReadableStream} of {@linkcode Uint8Array}s to an
* {@linkcode ArrayBuffer}. Works the same as {@linkcode Response.arrayBuffer}.
*
* @param readableStream A `ReadableStream` of `Uint8Array`s to convert into an `ArrayBuffer`.
* @return A promise that resolves with the `ArrayBuffer` containing all the data from the stream.
*
* @example Basic usage
* ```ts
* import { toArrayBuffer } from "@std/streams/to-array-buffer";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* new Uint8Array([1, 2]),
* new Uint8Array([3, 4, 5]),
* ]);
* const buf = await toArrayBuffer(stream);
* assertEquals(buf.byteLength, 5);
* ```
*/
const toArrayBuffer = _function_toArrayBuffer as typeof _function_toArrayBuffer
export { toArrayBuffer }
import { toBlob as _function_toBlob } from "jsr:@std/[email protected]"
/**
* Converts a {@linkcode ReadableStream} of {@linkcode Uint8Array}s to a
* {@linkcode Blob}. Works the same as {@linkcode Response.blob}.
*
* @param stream A `ReadableStream` of `Uint8Array`s to convert into a `Blob`.
* @return A `Promise` that resolves to the `Blob`.
*
* @example Basic usage
* ```ts
* import { toBlob } from "@std/streams/to-blob";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* new Uint8Array([1, 2]),
* new Uint8Array([3, 4, 5]),
* ]);
* const blob = await toBlob(stream);
* assertEquals(blob.size, 5);
* ```
*/
const toBlob = _function_toBlob as typeof _function_toBlob
export { toBlob }
import { toJson as _function_toJson } from "jsr:@std/[email protected]"
/**
* Converts a
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON}-formatted
* {@linkcode ReadableSteam} of strings or {@linkcode Uint8Array}s to an object.
* Works the same as {@linkcode Response.json} and {@linkcode Request.json}, but
* also extends to support streams of strings.
*
* @param stream A `ReadableStream` whose chunks compose a JSON.
* @return A promise that resolves to the parsed JSON.
*
* @example Usage with a stream of strings
* ```ts
* import { toJson } from "@std/streams/to-json";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* "[1, true",
* ', [], {}, "hello',
* '", null]',
* ]);
* assertEquals(await toJson(stream), [1, true, [], {}, "hello", null]);
* ```
*
* @example Usage with a stream of `Uint8Array`s
* ```ts
* import { toJson } from "@std/streams/to-json";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* "[1, true",
* ', [], {}, "hello',
* '", null]',
* ]).pipeThrough(new TextEncoderStream());
* assertEquals(await toJson(stream), [1, true, [], {}, "hello", null]);
* ```
*/
const toJson = _function_toJson as typeof _function_toJson
export { toJson }
import { toText as _function_toText } from "jsr:@std/[email protected]"
/**
* Converts a {@linkcode ReadableSteam} of strings or {@linkcode Uint8Array}s
* to a single string. Works the same as {@linkcode Response.text} and
* {@linkcode Request.text}, but also extends to support streams of strings.
*
* @param stream A `ReadableStream` to convert into a `string`.
* @return A `Promise` that resolves to the `string`.
*
* @example Basic usage with a stream of strings
* ```ts
* import { toText } from "@std/streams/to-text";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["Hello, ", "world!"]);
* assertEquals(await toText(stream), "Hello, world!");
* ```
*
* @example Basic usage with a stream of `Uint8Array`s
* ```ts
* import { toText } from "@std/streams/to-text";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from(["Hello, ", "world!"])
* .pipeThrough(new TextEncoderStream());
* assertEquals(await toText(stream), "Hello, world!");
* ```
*/
const toText = _function_toText as typeof _function_toText
export { toText }
import { toTransformStream as _function_toTransformStream } from "jsr:@std/[email protected]"
/**
* Convert the generator function into a {@linkcode TransformStream}.
*
* @template I The type of the chunks in the source stream.
* @template O The type of the chunks in the transformed stream.
* @param transformer A function to transform.
* @param writableStrategy An object that optionally defines a queuing strategy for the stream.
* @param readableStrategy An object that optionally defines a queuing strategy for the stream.
* @return A {@linkcode TransformStream} that transforms the source stream as defined by the provided transformer.
*
* @example Build a transform stream that multiplies each value by 100
* ```ts
* import { toTransformStream } from "@std/streams/to-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([0, 1, 2])
* .pipeThrough(toTransformStream(async function* (src) {
* for await (const chunk of src) {
* yield chunk * 100;
* }
* }));
*
* assertEquals(
* await Array.fromAsync(stream),
* [0, 100, 200],
* );
* ```
*
* @example JSON Lines
* ```ts
* import { TextLineStream } from "@std/streams/text-line-stream";
* import { toTransformStream } from "@std/streams/to-transform-stream";
* import { assertEquals } from "@std/assert";
*
* const stream = ReadableStream.from([
* '{"name": "Alice", "age": ',
* '30}\n{"name": "Bob", "age"',
* ": 25}\n",
* ]);
*
* type Person = { name: string; age: number };
*
* // Split the stream by newline and parse each line as a JSON object
* const jsonStream = stream.pipeThrough(new TextLineStream())
* .pipeThrough(toTransformStream(async function* (src) {
* for await (const chunk of src) {
* if (chunk.trim().length === 0) {
* continue;
* }
* yield JSON.parse(chunk) as Person;
* }
* }));
*
* assertEquals(
* await Array.fromAsync(jsonStream),
* [{ "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 }],
* );
* ```
*/
const toTransformStream = _function_toTransformStream as typeof _function_toTransformStream
export { toTransformStream }
import { zipReadableStreams as _function_zipReadableStreams } from "jsr:@std/[email protected]"
/**
* Merge multiple streams into a single one, taking order into account, and
* each stream will wait for a chunk to enqueue before the next stream can
* append another chunk.
*
* If a stream ends before other ones, the others will continue adding data in
* order, and the finished one will not add any more data. If you want to cancel
* the other streams when one of them ends, use {@linkcode earlyZipReadableStreams}.
*
* @template T The type of the chunks in the input/output streams.
* @return A `ReadableStream` that will emit the zipped chunks.
*
* @example Zip 2 streams with the same length
* ```ts
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1", "2", "3"]);
* const stream2 = ReadableStream.from(["a", "b", "c"]);
* const zippedStream = zipReadableStreams(stream1, stream2);
*
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "2", "b", "3", "c"],
* );
* ```
*
* @example Zip 2 streams with different length (first one is shorter)
* ```ts
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1", "2"]);
* const stream2 = ReadableStream.from(["a", "b", "c", "d"]);
* const zippedStream = zipReadableStreams(stream1, stream2);
*
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "2", "b", "c", "d"],
* );
* ```
*
* @example Zip 2 streams with different length (first one is longer)
* ```ts
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1", "2", "3", "4"]);
* const stream2 = ReadableStream.from(["a", "b"]);
* const zippedStream = zipReadableStreams(stream1, stream2);
*
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "2", "b", "3", "4"],
* );
* ```
*
* @example Zip 3 streams
* ```ts
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
* import { assertEquals } from "@std/assert";
*
* const stream1 = ReadableStream.from(["1"]);
* const stream2 = ReadableStream.from(["a", "b"]);
* const stream3 = ReadableStream.from(["A", "B", "C"]);
* const zippedStream = zipReadableStreams(stream1, stream2, stream3);
*
* assertEquals(
* await Array.fromAsync(zippedStream),
* ["1", "a", "A", "b", "B", "C"],
* );
* ```
*/
const zipReadableStreams = _function_zipReadableStreams as typeof _function_zipReadableStreams
export { zipReadableStreams }
|