Skip to content

Commit 43eb4e8

Browse files
committed
Revert "chore: remove deprecated api process.binding (microsoft#653)"
This reverts commit 3913479.
1 parent 36af101 commit 43eb4e8

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/unixTerminal.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
import * as net from 'net';
77
import * as path from 'path';
8-
import * as tty from 'tty';
98
import { Terminal, DEFAULT_COLS, DEFAULT_ROWS } from './terminal';
109
import { IProcessEnv, IPtyForkOptions, IPtyOpenOptions } from './interfaces';
1110
import { ArgvOrCommandLine } from './types';
@@ -114,7 +113,7 @@ export class UnixTerminal extends Terminal {
114113
// fork
115114
const term = pty.fork(file, args, parsedEnv, cwd, this._cols, this._rows, uid, gid, (encoding === 'utf8'), helperPath, onexit);
116115

117-
this._socket = new tty.ReadStream(term.fd);
116+
this._socket = new PipeSocket(term.fd);
118117
if (encoding !== null) {
119118
this._socket.setEncoding(encoding);
120119
}
@@ -204,13 +203,13 @@ export class UnixTerminal extends Terminal {
204203
// open
205204
const term: IUnixOpenProcess = pty.open(cols, rows);
206205

207-
self._master = new tty.ReadStream(term.master);
206+
self._master = new PipeSocket(<number>term.master);
208207
if (encoding !== null) {
209208
self._master.setEncoding(encoding);
210209
}
211210
self._master.resume();
212211

213-
self._slave = new tty.ReadStream(term.slave);
212+
self._slave = new PipeSocket(term.slave);
214213
if (encoding !== null) {
215214
self._slave.setEncoding(encoding);
216215
}
@@ -305,3 +304,18 @@ export class UnixTerminal extends Terminal {
305304
delete env['LINES'];
306305
}
307306
}
307+
308+
/**
309+
* Wraps net.Socket to force the handle type "PIPE" by temporarily overwriting
310+
* tty_wrap.guessHandleType.
311+
* See: https://github.com/chjj/pty.js/issues/103
312+
*/
313+
class PipeSocket extends net.Socket {
314+
constructor(fd: number) {
315+
const pipeWrap = (<any>process).binding('pipe_wrap'); // tslint:disable-line
316+
// @types/node has fd as string? https://github.com/DefinitelyTyped/DefinitelyTyped/pull/18275
317+
const handle = new pipeWrap.Pipe(pipeWrap.constants.SOCKET);
318+
handle.open(fd);
319+
super(<any>{ handle });
320+
}
321+
}

0 commit comments

Comments
 (0)