deno.land / std@0.224.0 / io / copy_n.ts

copy_n.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { assert } from "../assert/assert.ts";import type { Reader, Writer } from "./types.ts";
const DEFAULT_BUFFER_SIZE = 32 * 1024;
/** * Copy N size at the most. If read size is lesser than N, then returns nread * @param r Reader * @param dest Writer * @param size Read size * * @deprecated This will be removed in 1.0.0. Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead. */export async function copyN( r: Reader, dest: Writer, size: number,): Promise<number> { let bytesRead = 0; let buf = new Uint8Array(DEFAULT_BUFFER_SIZE); while (bytesRead < size) { if (size - bytesRead < DEFAULT_BUFFER_SIZE) { buf = new Uint8Array(size - bytesRead); } const result = await r.read(buf); const nread = result ?? 0; bytesRead += nread; if (nread > 0) { let n = 0; while (n < nread) { n += await dest.write(buf.slice(n, nread)); } assert(n === nread, "could not write"); } if (result === null) { break; } } return bytesRead;}
std

Version Info

Tagged at
6 months ago