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

reader_from_stream_reader.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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { Buffer } from "./buffer.ts";import { writeAll } from "./write_all.ts";import type { Reader } from "./types.ts";
/** * Create a {@linkcode Reader} from a {@linkcode ReadableStreamDefaultReader}. * * @example * ```ts * import { copy } from "https://deno.land/std@$STD_VERSION/io/copy.ts"; * import { readerFromStreamReader } from "https://deno.land/std@$STD_VERSION/io/reader_from_stream_reader.ts"; * * const res = await fetch("https://deno.land"); * using file = await Deno.open("./deno.land.html", { create: true, write: true }); * * const reader = readerFromStreamReader(res.body!.getReader()); * await copy(reader, file); * ``` */export function readerFromStreamReader( streamReader: ReadableStreamDefaultReader<Uint8Array>,): Reader { const buffer = new Buffer();
return { async read(p: Uint8Array): Promise<number | null> { if (buffer.empty()) { const res = await streamReader.read(); if (res.done) { return null; // EOF }
await writeAll(buffer, res.value); }
return buffer.read(p); }, };}
std

Version Info

Tagged at
6 months ago