mirror of
https://github.com/denoland/deno.git
synced 2025-01-05 05:49:20 -05:00
fix: Remove try-catch from Buffer.readFrom, readFromSync (#6161)
This commit is contained in:
parent
54c3f8e27f
commit
be8bacaaa4
3 changed files with 23 additions and 22 deletions
|
@ -158,38 +158,30 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
|
||||||
async readFrom(r: Reader): Promise<number> {
|
async readFrom(r: Reader): Promise<number> {
|
||||||
let n = 0;
|
let n = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
const i = this.#grow(MIN_READ);
|
||||||
const i = this.#grow(MIN_READ);
|
this.#reslice(i);
|
||||||
this.#reslice(i);
|
const fub = new Uint8Array(this.#buf.buffer, i);
|
||||||
const fub = new Uint8Array(this.#buf.buffer, i);
|
const nread = await r.read(fub);
|
||||||
const nread = await r.read(fub);
|
if (nread === null) {
|
||||||
if (nread === null) {
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
this.#reslice(i + nread);
|
|
||||||
n += nread;
|
|
||||||
} catch (e) {
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
this.#reslice(i + nread);
|
||||||
|
n += nread;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readFromSync(r: ReaderSync): number {
|
readFromSync(r: ReaderSync): number {
|
||||||
let n = 0;
|
let n = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
const i = this.#grow(MIN_READ);
|
||||||
const i = this.#grow(MIN_READ);
|
this.#reslice(i);
|
||||||
this.#reslice(i);
|
const fub = new Uint8Array(this.#buf.buffer, i);
|
||||||
const fub = new Uint8Array(this.#buf.buffer, i);
|
const nread = r.readSync(fub);
|
||||||
const nread = r.readSync(fub);
|
if (nread === null) {
|
||||||
if (nread === null) {
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
this.#reslice(i + nread);
|
|
||||||
n += nread;
|
|
||||||
} catch (e) {
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
this.#reslice(i + nread);
|
||||||
|
n += nread;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@ import {
|
||||||
assertEquals,
|
assertEquals,
|
||||||
assert,
|
assert,
|
||||||
assertStringContains,
|
assertStringContains,
|
||||||
|
assertThrows,
|
||||||
|
assertThrowsAsync,
|
||||||
unitTest,
|
unitTest,
|
||||||
} from "./test_util.ts";
|
} from "./test_util.ts";
|
||||||
|
|
||||||
|
@ -202,6 +204,9 @@ unitTest(async function bufferReadFrom(): Promise<void> {
|
||||||
const fub = new Uint8Array(testString.length);
|
const fub = new Uint8Array(testString.length);
|
||||||
await empty(b, s, fub);
|
await empty(b, s, fub);
|
||||||
}
|
}
|
||||||
|
assertThrowsAsync(async function () {
|
||||||
|
await new Buffer().readFrom(null!);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest(async function bufferReadFromSync(): Promise<void> {
|
unitTest(async function bufferReadFromSync(): Promise<void> {
|
||||||
|
@ -221,6 +226,9 @@ unitTest(async function bufferReadFromSync(): Promise<void> {
|
||||||
const fub = new Uint8Array(testString.length);
|
const fub = new Uint8Array(testString.length);
|
||||||
await empty(b, s, fub);
|
await empty(b, s, fub);
|
||||||
}
|
}
|
||||||
|
assertThrows(function () {
|
||||||
|
new Buffer().readFromSync(null!);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest(async function bufferTestGrow(): Promise<void> {
|
unitTest(async function bufferTestGrow(): Promise<void> {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { assert, assertEquals } from "../../../std/testing/asserts.ts";
|
||||||
export {
|
export {
|
||||||
assert,
|
assert,
|
||||||
assertThrows,
|
assertThrows,
|
||||||
|
assertThrowsAsync,
|
||||||
assertEquals,
|
assertEquals,
|
||||||
assertMatch,
|
assertMatch,
|
||||||
assertNotEquals,
|
assertNotEquals,
|
||||||
|
|
Loading…
Reference in a new issue