summaryrefslogtreecommitdiff
path: root/runtime/js
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2022-02-15 13:35:22 +0100
committerGitHub <noreply@github.com>2022-02-15 13:35:22 +0100
commitbdc8006a362b4f95107a25ca816dcdedb7f44e4a (patch)
tree928d7c08e1d16302af03404f37ce84b8d39e4a40 /runtime/js
parent7b893bd57f2f013c4a11e1e9f0ba435a3cfc96c0 (diff)
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.
Diffstat (limited to 'runtime/js')
-rw-r--r--runtime/js/40_files.js48
1 files changed, 47 insertions, 1 deletions
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();