summaryrefslogtreecommitdiff
path: root/std/io/streams.ts
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2020-06-27 22:55:01 +0200
committerGitHub <noreply@github.com>2020-06-27 16:55:01 -0400
commita829fa8f57a2063492aab564ec1f15da21eb851c (patch)
treeb1516b8a6eeeedabc8dd754d98a83c5b35cddeab /std/io/streams.ts
parenta216bd06fc7dfb4a136e9fc04ae119c2e4801b6e (diff)
feat(std/io): add fromStreamReader, fromStreamWriter (#5789)
Diffstat (limited to 'std/io/streams.ts')
-rw-r--r--std/io/streams.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/std/io/streams.ts b/std/io/streams.ts
new file mode 100644
index 000000000..3969746ef
--- /dev/null
+++ b/std/io/streams.ts
@@ -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);
+ },
+ };
+}