From bdc8006a362b4f95107a25ca816dcdedb7f44e4a Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Tue, 15 Feb 2022 13:35:22 +0100 Subject: feat(runtime): web streams in fs & net APIs (#13615) This commit adds `readable` and `writable` properties to `Deno.File` and `Deno.Conn`. This makes it very simple to use files and network sockets with fetch or the native HTTP server. --- runtime/js/40_files.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'runtime/js/40_files.js') diff --git a/runtime/js/40_files.js b/runtime/js/40_files.js index d7768375b..b0d651d5c 100644 --- a/runtime/js/40_files.js +++ b/runtime/js/40_files.js @@ -6,10 +6,12 @@ const { read, readSync, write, writeSync } = window.__bootstrap.io; const { ftruncate, ftruncateSync, fstat, fstatSync } = window.__bootstrap.fs; const { pathFromURL } = window.__bootstrap.util; + const { readableStreamForRid, writableStreamForRid } = + window.__bootstrap.streamUtils; const { + ArrayPrototypeFilter, Error, ObjectValues, - ArrayPrototypeFilter, } = window.__bootstrap.primordials; function seekSync( @@ -77,6 +79,9 @@ class File { #rid = 0; + #readable; + #writable; + constructor(rid) { this.#rid = rid; } @@ -128,9 +133,25 @@ close() { core.close(this.rid); } + + get readable() { + if (this.#readable === undefined) { + this.#readable = readableStreamForRid(this.rid); + } + return this.#readable; + } + + get writable() { + if (this.#writable === undefined) { + this.#writable = writableStreamForRid(this.rid); + } + return this.#writable; + } } class Stdin { + #readable; + constructor() { } @@ -149,9 +170,18 @@ close() { core.close(this.rid); } + + get readable() { + if (this.#readable === undefined) { + this.#readable = readableStreamForRid(this.rid); + } + return this.#readable; + } } class Stdout { + #writable; + constructor() { } @@ -170,9 +200,18 @@ close() { core.close(this.rid); } + + get writable() { + if (this.#writable === undefined) { + this.#writable = writableStreamForRid(this.rid); + } + return this.#writable; + } } class Stderr { + #writable; + constructor() { } @@ -191,6 +230,13 @@ close() { core.close(this.rid); } + + get writable() { + if (this.#writable === undefined) { + this.#writable = writableStreamForRid(this.rid); + } + return this.#writable; + } } const stdin = new Stdin(); -- cgit v1.2.3