Added a very complicated AsyncIterableReader class.
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.
This commit is contained in:
1 parent
17a1b4bed3
commit
0d627c9051
125 files changed
+26943
-83
No files matched your search
+41
@@ -0,0 +1,41 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
import { assert } from "../_util/asserts.ts";
|
||||
import type { Reader, Writer } from "../types.d.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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user