mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00
fix: Make std/io copyN write the whole read buffer (#4978)
This commit is contained in:
parent
0703431ec2
commit
ec41fb69cc
2 changed files with 18 additions and 2 deletions
|
@ -24,7 +24,10 @@ export async function copyN(
|
||||||
const nread = result ?? 0;
|
const nread = result ?? 0;
|
||||||
bytesRead += nread;
|
bytesRead += nread;
|
||||||
if (nread > 0) {
|
if (nread > 0) {
|
||||||
const n = await dest.write(buf.slice(0, nread));
|
let n = 0;
|
||||||
|
while (n < nread) {
|
||||||
|
n += await dest.write(buf.slice(n, nread));
|
||||||
|
}
|
||||||
assert(n === nread, "could not write");
|
assert(n === nread, "could not write");
|
||||||
}
|
}
|
||||||
if (result === null) {
|
if (result === null) {
|
||||||
|
|
|
@ -10,7 +10,8 @@ import {
|
||||||
sliceLongToBytes,
|
sliceLongToBytes,
|
||||||
} from "./ioutil.ts";
|
} from "./ioutil.ts";
|
||||||
import { BufReader } from "./bufio.ts";
|
import { BufReader } from "./bufio.ts";
|
||||||
import { stringsReader } from "./util.ts";
|
import { stringsReader, tempFile } from "./util.ts";
|
||||||
|
import * as path from "../path/mod.ts";
|
||||||
|
|
||||||
class BinaryReader implements Reader {
|
class BinaryReader implements Reader {
|
||||||
index = 0;
|
index = 0;
|
||||||
|
@ -85,3 +86,15 @@ Deno.test("testCopyN2", async function (): Promise<void> {
|
||||||
assertEquals(n, 10);
|
assertEquals(n, 10);
|
||||||
assertEquals(w.toString(), "abcdefghij");
|
assertEquals(w.toString(), "abcdefghij");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("copyNWriteAllData", async function (): Promise<void> {
|
||||||
|
const { filepath, file } = await tempFile(path.resolve("io"));
|
||||||
|
const size = 16 * 1024 + 1;
|
||||||
|
const data = "a".repeat(32 * 1024);
|
||||||
|
const r = stringsReader(data);
|
||||||
|
const n = await copyN(r, file, size); // Over max file possible buffer
|
||||||
|
file.close();
|
||||||
|
await Deno.remove(filepath);
|
||||||
|
|
||||||
|
assertEquals(n, size);
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue