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

[WIP] feat(unstable): add Deno.openPty() #14535

Closed
wants to merge 9 commits into from
Closed
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
21 changes: 17 additions & 4 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ declare namespace Deno {
*/
export function umask(mask?: number): number;

export interface ConsoleSize {
columns: number;
rows: number;
}

/** **UNSTABLE**: New API, yet to be vetted.
*
* Gets the size of the console as columns/rows.
Expand All @@ -203,10 +208,7 @@ declare namespace Deno {
*/
export function consoleSize(
rid: number,
): {
columns: number;
rows: number;
};
): ConsoleSize;

/** **Unstable** There are questions around which permission this needs. And
* maybe should be renamed (loadAverage?)
Expand Down Expand Up @@ -1026,6 +1028,7 @@ declare namespace Deno {
stdout?: "piped" | "inherit" | "null";
/** Defaults to "piped". */
stderr?: "piped" | "inherit" | "null";
pty?: Pty;
}

/**
Expand Down Expand Up @@ -1072,6 +1075,16 @@ declare namespace Deno {
/** Kills the process with given Signal. Defaults to SIGTERM. */
kill(signo?: Signal): void;
}

export class Pty {
rid: number;
readable: ReadableStream<Uint8Array>;
writable: WritableStream<Uint8Array>;
close(): void;
resize(size: ConsoleSize): void;
}

export function openPty(size: ConsoleSize): Pty;

/**
* Executes a subprocess, waiting for it to finish and
Expand Down
51 changes: 51 additions & 0 deletions runtime/js/40_pty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";
((window) => {
const core = window.Deno.core;
const { pathFromURL } = window.__bootstrap.util;
const { illegalConstructorKey } = window.__bootstrap.webUtil;
const {
ArrayPrototypeMap,
ObjectEntries,
String,
TypeError,
Uint8Array,
PromiseAll,
} = window.__bootstrap.primordials;
const { readableStreamForRid, writableStreamForRid } =
window.__bootstrap.streamUtils;

class Pty {
#rid;
get rid() {
return this.#rid;
}

constructor(key = null, rid) {
if (key !== illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
}

this.#rid = rid;

this.readable = readableStreamForRid(rid);
this.writable = writableStreamForRid(rid);
}
}

function openPty({
rows,
columns,
}) {
const pty = core.opSync("op_pty_open", {
rows,
columns,
});
return new Pty(illegalConstructorKey, pty);
}

window.__bootstrap.pty = {
Pty,
openPty,
};
})(this);
3 changes: 3 additions & 0 deletions runtime/js/40_spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
stdin,
stdout,
stderr,
pty,
});
return new Child(illegalConstructorKey, {
...child,
Expand Down Expand Up @@ -192,6 +193,7 @@
stdin = "null",
stdout = "piped",
stderr = "piped",
pty = undefined,
} = {}) {
if (stdin === "piped") {
throw new TypeError(
Expand All @@ -209,6 +211,7 @@
stdin,
stdout,
stderr,
pty,
});
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,7 @@
spawnChild: __bootstrap.spawn.spawnChild,
spawn: __bootstrap.spawn.spawn,
spawnSync: __bootstrap.spawn.spawnSync,
openPty: __bootstrap.pty.openPty,
Pty: __bootstrap.pty.Pty,
};
})(this);
1 change: 1 addition & 0 deletions runtime/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod io;
pub mod os;
pub mod permissions;
pub mod process;
pub mod pty;
pub mod runtime;
pub mod signal;
pub mod spawn;
Expand Down
Loading