std/io/multi_reader.ts
Asher Gomez e2de4d4e49
deprecation(io/unstable): deprecate MultiReader (#6023)
* deprecation(io/unstable): deprecate `MultiReader`

* update
2024-09-23 20:30:40 +09:00

84 lines
2.3 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import type { Reader } from "./types.ts";
/**
* Reader utility for combining multiple readers.
*
* @example Usage
* ```ts
* import { MultiReader } from "@std/io/multi-reader";
* import { StringReader } from "@std/io/string-reader";
* import { readAll } from "@std/io/read-all";
* import { assertEquals } from "@std/assert/equals";
*
* const r1 = new StringReader("hello");
* const r2 = new StringReader("world");
* const mr = new MultiReader([r1, r2]);
*
* const res = await readAll(mr);
*
* assertEquals(new TextDecoder().decode(res), "helloworld");
* ```
*
* @deprecated Use
* {@linkcode https://jsr.io/@std/streams/doc/merge-readable-streams/~/mergeReadableStreams | mergeReadableStreams}
* on readable streams instead. This will be removed in 0.225.0.
*/
export class MultiReader implements Reader {
readonly #readers: Reader[];
#currentIndex = 0;
/**
* Construct a new instance.
*
* @param readers The readers to combine.
*/
constructor(readers: Reader[]) {
this.#readers = [...readers];
}
/**
* Reads data from the readers.
*
* @example Usage
* ```ts
* import { MultiReader } from "@std/io/multi-reader";
* import { StringReader } from "@std/io/string-reader";
* import { readAll } from "@std/io/read-all";
* import { assertEquals } from "@std/assert/equals";
*
* const r1 = new StringReader("hello");
* const r2 = new StringReader("world");
* const mr = new MultiReader([r1, r2]);
*
* const data = new Uint8Array(5);
* const res = await mr.read(data);
*
* assertEquals(res, 5);
* assertEquals(new TextDecoder().decode(data), "hello");
*
* const res2 = await mr.read(data);
* assertEquals(res2, 0);
*
* const res3 = await mr.read(data);
* assertEquals(res3, 5);
* assertEquals(new TextDecoder().decode(data), "world");
* ```
*
* @param p The buffer to read data into.
* @returns The number of bytes read.
*/
async read(p: Uint8Array): Promise<number | null> {
const r = this.#readers[this.#currentIndex];
if (!r) return null;
const result = await r.read(p);
if (result === null) {
this.#currentIndex++;
return 0;
}
return result;
}
}