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

Cleanup _write method and API #311

Merged
merged 4 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 7 additions & 13 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,26 @@ export interface ITerminal {
once(eventName: string, listener: (...args: any[]) => any): void;
}

export interface IPtyForkOptions {
interface IBasePtyForkOptions {
name?: string;
cols?: number;
rows?: number;
cwd?: string;
env?: { [key: string]: string };
uid?: number;
gid?: number;
encoding?: string;
handleFlowControl?: boolean;
flowControlPause?: string;
flowControlResume?: string;
}

export interface IWindowsPtyForkOptions {
name?: string;
cols?: number;
rows?: number;
cwd?: string;
env?: { [key: string]: string };
encoding?: string;
export interface IPtyForkOptions extends IBasePtyForkOptions {
uid?: number;
gid?: number;
}

export interface IWindowsPtyForkOptions extends IBasePtyForkOptions {
experimentalUseConpty?: boolean;
conptyInheritCursor?: boolean;
handleFlowControl?: boolean;
flowControlPause?: string;
flowControlResume?: string;
}

export interface IPtyOpenOptions {
Expand Down
5 changes: 3 additions & 2 deletions src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export abstract class Terminal implements ITerminal {
protected _writable: boolean;

protected _internalee: EventEmitter;
protected _writeMethod: (data: string) => void;
private _flowControlPause: string;
private _flowControlResume: string;
public handleFlowControl: boolean;
Expand Down Expand Up @@ -75,6 +74,8 @@ export abstract class Terminal implements ITerminal {
this._flowControlResume = opt.flowControlResume || FLOW_CONTROL_RESUME;
}

protected abstract _write(data: string): void;

public write(data: string): void {
if (this.handleFlowControl) {
// PAUSE/RESUME messages are not forwarded to the pty
Expand All @@ -88,7 +89,7 @@ export abstract class Terminal implements ITerminal {
}
}
// everything else goes to the real pty
this._writeMethod(data);
this._write(data);
}

protected _forwardEvents(): void {
Expand Down
5 changes: 3 additions & 2 deletions src/unixTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ export class UnixTerminal extends Terminal {
});

this._forwardEvents();
}

// attach write method
this._writeMethod = (data: string) => this._socket.write(data);
protected _write(data: string): void {
this._socket.write(data);
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/windowsTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ export class WindowsTerminal extends Terminal {
this._writable = true;

this._forwardEvents();
}

protected _write(data: string): void {
this._defer(this._doWrite, data);
jerch marked this conversation as resolved.
Show resolved Hide resolved
}

// attach write method
this._writeMethod = (data: string) => this._defer(() => this._agent.inSocket.write(data));
private _doWrite(data: string): void {
this._agent.inSocket.write(data);
}

/**
Expand Down Expand Up @@ -163,7 +168,7 @@ export class WindowsTerminal extends Terminal {
});
}

private _defer(deferredFn: Function): void {
private _defer(deferredFn: Function, arg?: any): void {
Tyriar marked this conversation as resolved.
Show resolved Hide resolved

// Ensure that this method is only used within Terminal class.
if (!(this instanceof WindowsTerminal)) {
Tyriar marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -172,13 +177,13 @@ export class WindowsTerminal extends Terminal {

// If the terminal is ready, execute.
if (this._isReady) {
deferredFn.apply(this, null);
deferredFn.call(this, arg);
return;
}

// Queue until terminal is ready.
this._deferreds.push({
run: () => deferredFn.apply(this, null)
run: () => deferredFn.call(this, arg)
});
}

Expand Down
76 changes: 54 additions & 22 deletions typings/node-pty.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,41 @@ declare module 'node-pty' {
*/
export function spawn(file: string, args: string[] | string, options: IPtyForkOptions | IWindowsPtyForkOptions): IPty;

export interface IPtyForkOptions {
name?: string;
cols?: number;
rows?: number;
cwd?: string;
env?: { [key: string]: string };
uid?: number;
gid?: number;
encoding?: string;
}
export interface IBasePtyForkOptions {

export interface IWindowsPtyForkOptions {
/**
* Name of the terminal to be set in environment ($TERM variable).
*/
name?: string;

/**
* Number of intial cols of the pty.
*/
cols?: number;

/**
* Number of initial rows of the pty.
*/
rows?: number;

/**
* Working directory to be set for the slave program.
*/
cwd?: string;
env?: { [key: string]: string };
encoding?: string;

/**
* Whether to use the experimental ConPTY system on Windows. When this is not set, ConPTY will
* be used when the Windows build number is >= 18309 (it's available in 17134 and 17692 but is
* too unstable to enable by default).
*
* This setting does nothing on non-Windows.
* Environment to be set for the slave program.
*/
experimentalUseConpty?: boolean;
env?: { [key: string]: string };

/**
* Whether to use PSEUDOCONSOLE_INHERIT_CURSOR in conpty.
* @see https://docs.microsoft.com/en-us/windows/console/createpseudoconsole
* String encoding of the underlying pty.
* If set, incoming data will be decoded to strings and outgoing strings to bytes applying this encoding.
* If unset, incoming data will be delivered as raw bytes (Buffer type).
* By default 'utf8' is assumed, to unset it explicitly set it to `null`.
*/
conptyInheritCursor?: boolean;
encoding?: string;

/**
* Whether to enable flow control handling (false by default). If enabled a message of `flowControlPause`
* will pause the socket and thus blocking the slave program execution due to buffer back pressure.
Expand All @@ -57,16 +61,44 @@ declare module 'node-pty' {
* the underlying pseudoterminal.
*/
handleFlowControl?: boolean;

/**
* The string that should pause the pty when `handleFlowControl` is true. Default is XOFF ('\x13').
*/
flowControlPause?: string;

/**
* The string that should resume the pty when `handleFlowControl` is true. Default is XON ('\x11').
*/
flowControlResume?: string;
}

export interface IPtyForkOptions extends IBasePtyForkOptions {
/**
* Security warning: use this option with great caution, as opened file descriptors
* with higher privileges might leak to the slave program.
*/
uid?: number;
gid?: number;
}

export interface IWindowsPtyForkOptions extends IBasePtyForkOptions {
/**
* Whether to use the experimental ConPTY system on Windows. When this is not set, ConPTY will
* be used when the Windows build number is >= 18309 (it's available in 17134 and 17692 but is
* too unstable to enable by default).
*
* This setting does nothing on non-Windows.
*/
experimentalUseConpty?: boolean;

/**
* Whether to use PSEUDOCONSOLE_INHERIT_CURSOR in conpty.
* @see https://docs.microsoft.com/en-us/windows/console/createpseudoconsole
*/
conptyInheritCursor?: boolean;
}

/**
* An interface representing a pseudoterminal, on Windows this is emulated via the winpty library.
*/
Expand Down