BREAKING(io): remove LimitedReader (#6058)

This commit is contained in:
Asher Gomez 2024-09-26 11:51:20 +10:00 committed by GitHub
parent 33915afbb4
commit 6c75ccb6ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 0 additions and 148 deletions

View File

@ -6,7 +6,6 @@
"./buffer": "./buffer.ts",
"./copy": "./copy.ts",
"./iterate-reader": "./iterate_reader.ts",
"./limited-reader": "./limited_reader.ts",
"./multi-reader": "./multi_reader.ts",
"./read-all": "./read_all.ts",
"./reader-from-stream-reader": "./reader_from_stream_reader.ts",

View File

@ -1,113 +0,0 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import type { Reader } from "./types.ts";
/**
* Reads from `reader` but limits the amount of data returned to just `limit` bytes.
* Each call to `read` updates `limit` to reflect the new amount remaining.
* `read` returns `null` when `limit` <= `0` or
* when the underlying `reader` returns `null`.
*
* @example Usage
* ```ts
* import { StringReader } from "@std/io/string-reader";
* import { LimitedReader } from "@std/io/limited-reader";
* import { readAll } from "@std/io/read-all";
* import { assertEquals } from "@std/assert/equals";
*
* const r = new StringReader("hello world");
* const lr = new LimitedReader(r, 5);
* const res = await readAll(lr);
*
* assertEquals(new TextDecoder().decode(res), "hello");
* ```
*
* @deprecated Pipe the readable through a
* {@linkcode https://jsr.io/@std/streams/doc/limited-bytes-transform-stream/~/LimitedBytesTransformStream | LimitedBytesTransformStream}
* instead. This will be removed in 0.225.0.
*/
export class LimitedReader implements Reader {
/**
* The reader to read from
*
* @example Usage
* ```ts
* import { StringReader } from "@std/io/string-reader";
* import { LimitedReader } from "@std/io/limited-reader";
* import { assertEquals } from "@std/assert/equals";
*
* const r = new StringReader("hello world");
* const lr = new LimitedReader(r, 5);
*
* assertEquals(lr.reader, r);
* ```
*/
reader: Reader;
/**
* The number of bytes to limit the reader to
*
* @example Usage
* ```ts
* import { StringReader } from "@std/io/string-reader";
* import { LimitedReader } from "@std/io/limited-reader";
* import { assertEquals } from "@std/assert/equals";
*
* const r = new StringReader("hello world");
* const lr = new LimitedReader(r, 5);
*
* assertEquals(lr.limit, 5);
* ```
*/
limit: number;
/**
* Construct a new instance.
*
* @param reader The reader to read from.
* @param limit The number of bytes to limit the reader to.
*/
constructor(reader: Reader, limit: number) {
this.reader = reader;
this.limit = limit;
}
/**
* Reads data from the reader.
*
* @example Usage
* ```ts
* import { StringReader } from "@std/io/string-reader";
* import { LimitedReader } from "@std/io/limited-reader";
* import { assertEquals } from "@std/assert/equals";
*
* const r = new StringReader("hello world");
* const lr = new LimitedReader(r, 5);
*
* const data = new Uint8Array(5);
* const res = await lr.read(data);
*
* assertEquals(res, 5);
* assertEquals(new TextDecoder().decode(data), "hello");
* ```
*
* @param p The buffer to read data into.
* @returns The number of bytes read.
*/
async read(p: Uint8Array): Promise<number | null> {
if (this.limit <= 0) {
return null;
}
if (p.length > this.limit) {
p = p.subarray(0, this.limit);
}
const n = await this.reader.read(p);
if (n === null) {
return null;
}
this.limit -= n;
return n;
}
}

View File

@ -1,33 +0,0 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import { LimitedReader } from "./limited_reader.ts";
import { StringWriter } from "./string_writer.ts";
import { copy } from "./copy.ts";
import { readAll } from "./read_all.ts";
import { StringReader } from "./string_reader.ts";
Deno.test("ioLimitedReader", async function () {
const decoder = new TextDecoder();
let sr = new StringReader("abc");
let r = new LimitedReader(sr, 2);
let buffer = await readAll(r);
assertEquals(decoder.decode(buffer), "ab");
assertEquals(decoder.decode(await readAll(sr)), "c");
sr = new StringReader("abc");
r = new LimitedReader(sr, 3);
buffer = await readAll(r);
assertEquals(decoder.decode(buffer), "abc");
assertEquals((await readAll(r)).length, 0);
sr = new StringReader("abc");
r = new LimitedReader(sr, 4);
buffer = await readAll(r);
assertEquals(decoder.decode(buffer), "abc");
assertEquals((await readAll(r)).length, 0);
});
Deno.test("ioLimitedReader", async function () {
const rb = new StringReader("abc");
const wb = new StringWriter();
await copy(new LimitedReader(rb, -1), wb);
assertEquals(wb.toString(), "");
});

View File

@ -19,7 +19,6 @@
export * from "./buffer.ts";
export * from "./copy.ts";
export * from "./iterate_reader.ts";
export * from "./limited_reader.ts";
export * from "./multi_reader.ts";
export * from "./read_all.ts";
export * from "./reader_from_stream_reader.ts";