mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 08:39:09 -05:00
fd9b6e03af
Refs #7487
36 lines
929 B
TypeScript
36 lines
929 B
TypeScript
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
/** Create a `Writer` from a `WritablseStreamDefaultReader`. */
|
|
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;
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Create a `Reader` from a `ReadableSteramDefaultReader`. */
|
|
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);
|
|
},
|
|
};
|
|
}
|