summaryrefslogtreecommitdiff
path: root/ext/io/12_io.js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-03-04 23:37:37 -0400
committerGitHub <noreply@github.com>2023-03-05 03:37:37 +0000
commit888ceac7fdc6e9b96d860f183dd2f31df3f3d601 (patch)
treed56e78c736ca50ebf6d091e2804c4e468326befb /ext/io/12_io.js
parent17574f1ef7adf4aea91ba497759ac3e3fed50f1a (diff)
refactor(runtime): remove 40_files.js, 40_write_file.js and 40_read_file.js (#18018)
JavaScript APIs from "runtime/js/40_files.js" combined abstractions for stdio streams ("Stdout", "Stderr", "Stdin") and file system file ("File", "FsFile"). APIs from "runtime/js/40_read_file.js" and "runtime/js/40_write_file.js" were implemented using ops from "runtime/ops/fs.rs". This file was removed and relevant APIs were moved to "deno_io/12_io.js" and "runtime/js/30_fs.js". This work is meant to enable factoring out "deno_fs" crate.
Diffstat (limited to 'ext/io/12_io.js')
-rw-r--r--ext/io/12_io.js106
1 files changed, 106 insertions, 0 deletions
diff --git a/ext/io/12_io.js b/ext/io/12_io.js
index b9ff1190b..d1f3b8b1b 100644
--- a/ext/io/12_io.js
+++ b/ext/io/12_io.js
@@ -7,6 +7,10 @@
const core = globalThis.Deno.core;
const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
+import {
+ readableStreamForRid,
+ writableStreamForRid,
+} from "internal:deno_web/06_streams.js";
const {
Uint8Array,
ArrayPrototypePush,
@@ -221,6 +225,105 @@ async function readAllInnerSized(r, size, options) {
}
}
+class Stdin {
+ #readable;
+
+ constructor() {
+ }
+
+ get rid() {
+ return 0;
+ }
+
+ read(p) {
+ return read(this.rid, p);
+ }
+
+ readSync(p) {
+ return readSync(this.rid, p);
+ }
+
+ close() {
+ core.close(this.rid);
+ }
+
+ get readable() {
+ if (this.#readable === undefined) {
+ this.#readable = readableStreamForRid(this.rid);
+ }
+ return this.#readable;
+ }
+
+ setRaw(mode, options = {}) {
+ const cbreak = !!(options.cbreak ?? false);
+ ops.op_stdin_set_raw(mode, cbreak);
+ }
+}
+
+class Stdout {
+ #writable;
+
+ constructor() {
+ }
+
+ get rid() {
+ return 1;
+ }
+
+ write(p) {
+ return write(this.rid, p);
+ }
+
+ writeSync(p) {
+ return writeSync(this.rid, p);
+ }
+
+ close() {
+ core.close(this.rid);
+ }
+
+ get writable() {
+ if (this.#writable === undefined) {
+ this.#writable = writableStreamForRid(this.rid);
+ }
+ return this.#writable;
+ }
+}
+
+class Stderr {
+ #writable;
+
+ constructor() {
+ }
+
+ get rid() {
+ return 2;
+ }
+
+ write(p) {
+ return write(this.rid, p);
+ }
+
+ writeSync(p) {
+ return writeSync(this.rid, p);
+ }
+
+ close() {
+ core.close(this.rid);
+ }
+
+ get writable() {
+ if (this.#writable === undefined) {
+ this.#writable = writableStreamForRid(this.rid);
+ }
+ return this.#writable;
+ }
+}
+
+const stdin = new Stdin();
+const stdout = new Stdout();
+const stderr = new Stderr();
+
export {
copy,
iter,
@@ -233,6 +336,9 @@ export {
readAllSyncSized,
readSync,
SeekMode,
+ stderr,
+ stdin,
+ stdout,
write,
writeSync,
};