Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node/process): null is not returned when reaching end-of-file in stdin #3113

Merged
merged 24 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion node/_process/streams.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const stdout = stdio.stdout = createWritableStdioStream(

/** https://nodejs.org/api/process.html#process_process_stdin */
export const stdin = stdio.stdin = new Readable({
highWaterMark: 0,
highWaterMark: Deno.isatty?.(Deno.stdin?.rid) ? 0 : 64 * 1024,
emitClose: false,
read(size) {
const p = Buffer.alloc(size || 16 * 1024);
Expand Down
60 changes: 60 additions & 0 deletions node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,66 @@ Deno.test({
},
});

Deno.test({
name: "process.stdin readable with a TTY",
ignore: !Deno.isatty(Deno.stdin.rid),
PolarETech marked this conversation as resolved.
Show resolved Hide resolved
async fn() {
const promise = deferred();
const expected = ["foo", "bar", null, "end"];
const data: (string | null)[] = [];

process.stdin.setEncoding("utf8");
process.stdin.on("readable", () => {
data.push(process.stdin.read());
});
process.stdin.on("end", () => {
data.push("end");
});

process.stdin.push("foo");
process.nextTick(() => {
process.stdin.push("bar");
process.nextTick(() => {
process.stdin.push(null);
promise.resolve();
});
});

await promise;
assertEquals(process.stdin.readableHighWaterMark, 0);
assertEquals(data, expected);
},
});

Deno.test({
name: "process.stdin readable with piping a file",
async fn() {
const expected = ["65536", "foo", "bar", "null", "end"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In node.js, the first number becomes 16384. Is this difference intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My confirmation was lacking.
As I mentioned in the issue, the highWaterMark is 65536 with piping a file, But when running the code in child_process with piping stdin, the highWaterMark is a different value.

  • tty(tty.ReadStream): 0
  • piping files(fs.ReadStream): 65536
  • piping stdin(Stream?): 16384

I'll look into it further.
Thank you for pointing this out.

const scriptPath = "./node/testdata/process_stdin.ts";
const file = await Deno.readFile(`./node/testdata/process_stdin_dummy.txt`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can replace this with inline Uint8Array like: new TextEncoder().encode("foo\nbar")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was divided into two tests, one for file and one for pipe. And I used inline Uint8Array in the test for pipe.
https://github.com/denoland/deno_std/pull/3113/files#diff-f27eacddc948ff5c5ce7c851fccf64c00f313b8edc28862b5066c211e2b14d72R402


const command = new Deno.Command(Deno.execPath(), {
args: ["run", scriptPath],
stdin: "piped",
stdout: "piped",
stderr: "null",
});
const child = command.spawn();

const writer = await child.stdin.getWriter();
writer.ready
.then(() => writer.write(file))
.then(() => writer.releaseLock())
.then(() => child.stdin.close());

const { stdout } = await child.output();

const decoder = new TextDecoder();
const data = decoder.decode(stdout).trim().split("\n");
assertEquals(data, expected);
},
});

Deno.test({
name: "process.stdout",
fn() {
Expand Down
11 changes: 11 additions & 0 deletions node/testdata/process_stdin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import process from "../process.ts";

console.log(process.stdin.readableHighWaterMark);

process.stdin.setEncoding("utf8");
process.stdin.on("readable", () => {
console.log(process.stdin.read());
});
process.stdin.on("end", () => {
console.log("end");
});
2 changes: 2 additions & 0 deletions node/testdata/process_stdin_dummy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
bar