The class was already implemented in "https://deno.land/std@0.144.0/streams/conversion.ts". So, I litterally lost 1 day of work! I hate myself.
32 lines
865 B
TypeScript
32 lines
865 B
TypeScript
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* 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`.
|
|
*/
|
|
import type { Reader } from "../types.d.ts";
|
|
|
|
export class LimitedReader implements Reader {
|
|
constructor(public reader: 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;
|
|
}
|
|
}
|