mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
feat(std/io): add LimitedReader (#6026)
This commit is contained in:
parent
c9aded05a6
commit
b075f55d58
2 changed files with 59 additions and 1 deletions
|
@ -1,4 +1,10 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
// Based on https://github.com/golang/go/blob/0452f9460f50f0f0aba18df43dc2b31906fb66cc/src/io/io.go
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
type Reader = Deno.Reader;
|
||||
import { encode } from "../encoding/utf8.ts";
|
||||
|
||||
|
@ -40,3 +46,30 @@ export class MultiReader implements Reader {
|
|||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A `LimitedReader` 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`.
|
||||
*/
|
||||
export class LimitedReader implements Deno.Reader {
|
||||
constructor(public reader: Deno.Reader, public limit: number) {}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const { copy, test } = Deno;
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
import { MultiReader, StringReader } from "./readers.ts";
|
||||
import { LimitedReader, MultiReader, StringReader } from "./readers.ts";
|
||||
import { StringWriter } from "./writers.ts";
|
||||
import { copyN } from "./ioutil.ts";
|
||||
import { decode } from "../encoding/utf8.ts";
|
||||
|
@ -36,3 +36,28 @@ test("ioMultiReader", async function (): Promise<void> {
|
|||
await copy(r, w);
|
||||
assertEquals(w.toString(), "abcdef");
|
||||
});
|
||||
|
||||
test("ioLimitedReader", async function (): Promise<void> {
|
||||
let sr = new StringReader("abc");
|
||||
let r = new LimitedReader(sr, 2);
|
||||
let buffer = await Deno.readAll(r);
|
||||
assertEquals(decode(buffer), "ab");
|
||||
assertEquals(decode(await Deno.readAll(sr)), "c");
|
||||
sr = new StringReader("abc");
|
||||
r = new LimitedReader(sr, 3);
|
||||
buffer = await Deno.readAll(r);
|
||||
assertEquals(decode(buffer), "abc");
|
||||
assertEquals((await Deno.readAll(r)).length, 0);
|
||||
sr = new StringReader("abc");
|
||||
r = new LimitedReader(sr, 4);
|
||||
buffer = await Deno.readAll(r);
|
||||
assertEquals(decode(buffer), "abc");
|
||||
assertEquals((await Deno.readAll(r)).length, 0);
|
||||
});
|
||||
|
||||
test("ioLimitedReader", async function (): Promise<void> {
|
||||
const rb = new StringReader("abc");
|
||||
const wb = new StringWriter();
|
||||
await Deno.copy(new LimitedReader(rb, -1), wb);
|
||||
assertEquals(wb.toString(), "");
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue