Skip to content

Commit

Permalink
fix: support readable() and writable() properties on FsFile and stdio (
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 29, 2024
1 parent 30c7a69 commit 5db6429
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
12 changes: 0 additions & 12 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions packages/shim-deno/src/deno/stable/classes/FsFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
///<reference path="../lib.deno.d.ts" />

import * as fs from "fs";
import * as fs from "node:fs";
import * as stream from "node:stream";
import { fstat } from "../functions/fstat.js";
import { fstatSync } from "../functions/fstatSync.js";
import { ftruncate } from "../functions/ftruncate.js";
Expand Down Expand Up @@ -95,12 +96,28 @@ export class FsFile implements Deno.FsFile {
fs.closeSync(this.rid);
}

#readableStream: ReadableStream<Uint8Array> | undefined;
get readable(): ReadableStream<Uint8Array> {
throw new Error("Not implemented.");
if (this.#readableStream == null) {
const nodeStream = fs.createReadStream(null as any, {
fd: this.rid,
autoClose: false,
});
this.#readableStream = stream.Readable.toWeb(nodeStream);
}
return this.#readableStream;
}

#writableStream: WritableStream<Uint8Array> | undefined;
get writable(): WritableStream<Uint8Array> {
throw new Error("Not implemented.");
if (this.#writableStream == null) {
const nodeStream = fs.createWriteStream(null as any, {
fd: this.rid,
autoClose: false,
});
this.#writableStream = stream.Writable.toWeb(nodeStream);
}
return this.#writableStream;
}
}

Expand Down
20 changes: 17 additions & 3 deletions packages/shim-deno/src/deno/stable/variables/std.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
///<reference path="../lib.deno.d.ts" />

import stream from "node:stream";
import tty from "node:tty";
import { readSync } from "../functions/readSync.js";
import { writeSync } from "../functions/writeSync.js";
Expand All @@ -21,6 +22,7 @@ function chain<T extends (...args: any[]) => Promise<any>>(
} as T;
}

let stdinReadable: ReadableStream<Uint8Array> | undefined;
export const stdin: typeof Deno.stdin = {
rid: 0,
isTerminal() {
Expand Down Expand Up @@ -50,7 +52,10 @@ export const stdin: typeof Deno.stdin = {
() => process.stdin.pause(),
),
get readable(): ReadableStream<Uint8Array> {
throw new Error("Not implemented.");
if (stdinReadable == null) {
stdinReadable = stream.Readable.toWeb(process.stdin);
}
return stdinReadable;
},
readSync(buffer: Uint8Array) {
return readSync(this.rid, buffer);
Expand All @@ -65,6 +70,8 @@ export const stdin: typeof Deno.stdin = {
process.stdin.setRawMode(mode);
},
};

let stdoutWritable: WritableStream<Uint8Array> | undefined;
export const stdout: typeof Deno.stdout = {
rid: 1,
isTerminal() {
Expand All @@ -81,7 +88,10 @@ export const stdout: typeof Deno.stdout = {
});
}),
get writable(): WritableStream<Uint8Array> {
throw new Error("Not implemented.");
if (stdoutWritable == null) {
stdoutWritable = stream.Writable.toWeb(process.stdout);
}
return stdoutWritable;
},
writeSync(data: Uint8Array) {
return writeSync(this.rid, data);
Expand All @@ -90,6 +100,7 @@ export const stdout: typeof Deno.stdout = {
process.stdout.destroy();
},
};
let stderrWritable: WritableStream<Uint8Array> | undefined;
export const stderr: typeof Deno.stderr = {
rid: 2,
isTerminal() {
Expand All @@ -106,7 +117,10 @@ export const stderr: typeof Deno.stderr = {
});
}),
get writable(): WritableStream<Uint8Array> {
throw new Error("Not implemented.");
if (stderrWritable == null) {
stderrWritable = stream.Writable.toWeb(process.stderr);
}
return stderrWritable;
},
writeSync(data: Uint8Array) {
return writeSync(this.rid, data);
Expand Down
3 changes: 0 additions & 3 deletions packages/shim-deno/tools/run_tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const testsToSkip = new Set([
"openSyncNotFound", // includes full path in node.js
"openNotFound", // includes full path in node.js
"openModeWriteRead", // not implemented
"readableStream", // not implemented
"readableStreamTextEncoderPipe", // not implemented
"readFileIsDirectoryErrorCode", // todo(https://github.com/denoland/deno/issues/18629): re-enable
"seekStart", // not implemented
"seekSyncStart", // not implemented
Expand All @@ -61,7 +59,6 @@ const testsToSkip = new Set([
"seekEnd", // not implemented
"seekSyncEnd", // not implemented
"seekMode", // not implemented
"writableStream", // not implemented

// mkdir_test
"mkdirMode", // depends on Deno.umask
Expand Down

0 comments on commit 5db6429

Please sign in to comment.