Skip to content

Commit

Permalink
3.1.3 Add an interactive flag to PTY (#37)
Browse files Browse the repository at this point in the history
We want to be able to start processes that don't have a PTY stdin.

https://replit.slack.com/archives/C074CCJSS0M/p1721702561485369

This change adds that flag.
  • Loading branch information
lhchavez authored Jul 23, 2024
1 parent f77eab0 commit 9b65d88
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface PtyOptions {
envs?: Record<string, string>
dir?: string
size?: Size
interactive?: boolean
onExit: (err: null | Error, exitCode: number) => void
}
/** A size struct to pass to resize. */
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@replit/ruspty",
"version": "3.1.2",
"version": "3.1.3",
"main": "dist/wrapper.js",
"types": "dist/wrapper.d.ts",
"author": "Szymon Kaliski <[email protected]>",
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct PtyOptions {
pub envs: Option<HashMap<String, String>>,
pub dir: Option<String>,
pub size: Option<Size>,
pub interactive: Option<bool>,
#[napi(ts_type = "(err: null | Error, exitCode: number) => void")]
pub on_exit: JsFunction,
}
Expand Down Expand Up @@ -128,7 +129,11 @@ impl Pty {
set_nonblocking(controller_fd.as_raw_fd())?;

// duplicate pty user_fd to be the child's stdin, stdout, and stderr
cmd.stdin(Stdio::from(user_fd.try_clone()?));
if opts.interactive.unwrap_or(true) {
cmd.stdin(Stdio::from(user_fd.try_clone()?));
} else {
cmd.stdin(Stdio::null());
}
cmd.stderr(Stdio::from(user_fd.try_clone()?));
cmd.stdout(Stdio::from(user_fd.try_clone()?));

Expand Down
28 changes: 28 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ describe(
writeStream.end(EOT);
}));

test('can be started in non-interactive fashion', () =>
new Promise<void>((done) => {
const oldFds = getOpenFds();

let buffer = '';

const expectedResult = '\r\n';

const pty = new Pty({
command: '/bin/cat',
interactive: false,
onExit: (err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(0);
let result = buffer.toString();
expect(result.trim()).toStrictEqual(expectedResult.trim());
expect(getOpenFds()).toStrictEqual(oldFds);
done();
},
});

const readStream = pty.read;

readStream.on('data', (data) => {
buffer += data.toString();
});
}));

test('can be resized', () =>
new Promise<void>((done) => {
const oldFds = getOpenFds();
Expand Down

0 comments on commit 9b65d88

Please sign in to comment.