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

3.1.3 Add an interactive flag to PTY #37

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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