mirror of
https://github.com/denoland/deno.git
synced 2025-09-29 05:34:49 +00:00
feat(std/io): add fromStreamReader, fromStreamWriter (#5789)
This commit is contained in:
parent
a216bd06fc
commit
a829fa8f57
2 changed files with 168 additions and 0 deletions
34
std/io/streams.ts
Normal file
34
std/io/streams.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
export function fromStreamWriter(
|
||||
streamWriter: WritableStreamDefaultWriter<Uint8Array>
|
||||
): Deno.Writer {
|
||||
return {
|
||||
async write(p: Uint8Array): Promise<number> {
|
||||
await streamWriter.ready;
|
||||
await streamWriter.write(p);
|
||||
return p.length;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function fromStreamReader(
|
||||
streamReader: ReadableStreamDefaultReader<Uint8Array>
|
||||
): Deno.Reader {
|
||||
const buffer = new Deno.Buffer();
|
||||
|
||||
return {
|
||||
async read(p: Uint8Array): Promise<number | null> {
|
||||
if (buffer.empty()) {
|
||||
const res = await streamReader.read();
|
||||
if (res.done) {
|
||||
return null; // EOF
|
||||
}
|
||||
|
||||
await Deno.writeAll(buffer, res.value);
|
||||
}
|
||||
|
||||
return buffer.read(p);
|
||||
},
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue