mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
BREAKING(io): remove MultiReader
(#6059)
This commit is contained in:
parent
6c75ccb6ed
commit
45cef56ce9
24
archive/_multi_reader.ts
Normal file
24
archive/_multi_reader.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
// This module is browser compatible.
|
||||||
|
|
||||||
|
import type { Reader } from "@std/io/types";
|
||||||
|
|
||||||
|
export class MultiReader implements Reader {
|
||||||
|
readonly #readers: Reader[];
|
||||||
|
#currentIndex = 0;
|
||||||
|
|
||||||
|
constructor(readers: Reader[]) {
|
||||||
|
this.#readers = [...readers];
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -35,7 +35,7 @@ import {
|
|||||||
USTAR_STRUCTURE,
|
USTAR_STRUCTURE,
|
||||||
} from "./_common.ts";
|
} from "./_common.ts";
|
||||||
import type { Reader } from "@std/io/types";
|
import type { Reader } from "@std/io/types";
|
||||||
import { MultiReader } from "@std/io/multi-reader";
|
import { MultiReader } from "./_multi_reader.ts";
|
||||||
import { Buffer } from "@std/io/buffer";
|
import { Buffer } from "@std/io/buffer";
|
||||||
import { HEADER_LENGTH } from "./_common.ts";
|
import { HEADER_LENGTH } from "./_common.ts";
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
"./buffer": "./buffer.ts",
|
"./buffer": "./buffer.ts",
|
||||||
"./copy": "./copy.ts",
|
"./copy": "./copy.ts",
|
||||||
"./iterate-reader": "./iterate_reader.ts",
|
"./iterate-reader": "./iterate_reader.ts",
|
||||||
"./multi-reader": "./multi_reader.ts",
|
|
||||||
"./read-all": "./read_all.ts",
|
"./read-all": "./read_all.ts",
|
||||||
"./reader-from-stream-reader": "./reader_from_stream_reader.ts",
|
"./reader-from-stream-reader": "./reader_from_stream_reader.ts",
|
||||||
"./string-reader": "./string_reader.ts",
|
"./string-reader": "./string_reader.ts",
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
export * from "./buffer.ts";
|
export * from "./buffer.ts";
|
||||||
export * from "./copy.ts";
|
export * from "./copy.ts";
|
||||||
export * from "./iterate_reader.ts";
|
export * from "./iterate_reader.ts";
|
||||||
export * from "./multi_reader.ts";
|
|
||||||
export * from "./read_all.ts";
|
export * from "./read_all.ts";
|
||||||
export * from "./reader_from_stream_reader.ts";
|
export * from "./reader_from_stream_reader.ts";
|
||||||
export * from "./string_reader.ts";
|
export * from "./string_reader.ts";
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
||||||
|
|
||||||
import { assertEquals } from "@std/assert";
|
|
||||||
import { MultiReader } from "./multi_reader.ts";
|
|
||||||
import { StringWriter } from "./string_writer.ts";
|
|
||||||
import { copy } from "./copy.ts";
|
|
||||||
import { StringReader } from "./string_reader.ts";
|
|
||||||
|
|
||||||
Deno.test("ioMultiReader", async function () {
|
|
||||||
const r = new MultiReader([new StringReader("abc"), new StringReader("def")]);
|
|
||||||
const w = new StringWriter();
|
|
||||||
await copy(r, w);
|
|
||||||
assertEquals(w.toString(), "abcdef");
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user