Skip to content

Commit

Permalink
refactor: upgrade node-pty to 1.0.0 (opensumi#3187)
Browse files Browse the repository at this point in the history
  • Loading branch information
erha19 authored Dec 4, 2023
1 parent 1a3ba76 commit 6e9cc7e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/terminal-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@opensumi/ide-core-node": "workspace:*",
"@opensumi/ide-file-service": "workspace:*",
"event-kit": "^2.5.3",
"node-pty": "0.11.0-beta19",
"node-pty": "1.0.0",
"os-locale": "^4.0.0",
"xterm": "5.2.0",
"xterm-addon-canvas": "0.4.0",
Expand Down
13 changes: 6 additions & 7 deletions packages/terminal-next/src/common/pty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ export interface IPtyProxyRPCService {
*/
$onExit(callId: number, pid: number): void;

/**
* pty数据on回调代理
* @param callId 指定callId的方法回调注册
* @param pid pty进程的pid,用于辨识pty进程
*/
$on(callId: number, pid: number, event: any): void;

/**
* resize方法RPC转发
* @param pid pty进程的pid,用于辨识pty进程
Expand Down Expand Up @@ -120,6 +113,12 @@ export interface IPtyProxyRPCService {
*/
$resume(pid: number): void;

/**
* 清理 pty
* @param pid pty进程的pid,用于辨识pty进程
*/
$clear(pid: number): void;

/**
* 实时性通过Pid获取ProcessName
* @param pid pty进程的pid,用于辨识pty进程
Expand Down
25 changes: 9 additions & 16 deletions packages/terminal-next/src/node/pty.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ export interface IPtyServiceManager {
// 因为 PtyServiceManage 是 PtyClient 端统筹所有 Pty 的管理类,因此每一个具体方法的调用都需要传入 pid 来对指定 pid 做某些操作
onData(pid: number, listener: (e: string) => any): pty.IDisposable;
onExit(pid: number, listener: (e: { exitCode: number; signal?: number }) => any): pty.IDisposable;
on(pid: number, event: 'data', listener: (data: string) => void): void;
on(pid: number, event: 'exit', listener: (exitCode: number, signal?: number) => void): void;
on(pid: number, event: any, listener: (data: any) => void): void;
resize(pid: number, columns: number, rows: number): void;
write(pid: number, data: string): void;
pause(pid: number): void;
resume(pid: number): void;
clear(pid: number): void;
kill(pid: number, signal?: string): void;
getProcess(pid: number): Promise<string>;
getCwd(pid: number): Promise<string | undefined>;
Expand Down Expand Up @@ -127,23 +125,21 @@ export class PtyServiceManager implements IPtyServiceManager {
this.ptyServiceProxy.$onData(callId, pid);
return disposable;
}

onExit(pid: number, listener: (e: { exitCode: number; signal?: number }) => any): pty.IDisposable {
const { callId, disposable } = this.addNewCallback(pid, listener);
this.ptyServiceProxy.$onExit(callId, pid);
return disposable;
}

on(pid: number, event: 'data', listener: (data: string) => void): void;
on(pid: number, event: 'exit', listener: (exitCode: number, signal?: number) => void): void;
on(pid: number, event: any, listener: (data: any) => void): void {
const { callId } = this.addNewCallback(pid, listener);
this.ptyServiceProxy.$on(callId, pid, event);
}

resize(pid: number, columns: number, rows: number): void {
this.ptyServiceProxy.$resize(pid, columns, rows);
}

clear(pid: number) {
this.ptyServiceProxy.$clear(pid);
}

write(pid: number, data: string): void {
timeCaptureTmp = new Date().getTime();
if (data.includes('=')) {
Expand Down Expand Up @@ -220,12 +216,6 @@ class PtyProcessProxy implements IPtyProcessProxy {
onData: pty.IEvent<string>;
onExit: pty.IEvent<{ exitCode: number; signal?: number }>;

// 将 pid 维护到对象内部,对外暴露 NodePty 的标准 api,因此在调用的时候不需要显式传入 pid
on(event: 'data', listener: (data: string) => void): void;
on(event: 'exit', listener: (exitCode: number, signal?: number) => void): void;
on(event: any, listener: any): void {
this.ptyServiceManager.on(this.pid, event, listener);
}
resize(columns: number, rows: number): void {
this.ptyServiceManager.resize(this.pid, columns, rows);
}
Expand All @@ -241,4 +231,7 @@ class PtyProcessProxy implements IPtyProcessProxy {
resume(): void {
this.ptyServiceManager.resume(this.pid);
}
clear(): void {
this.ptyServiceManager.clear(this.pid);
}
}
11 changes: 4 additions & 7 deletions packages/terminal-next/src/node/pty.proxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import childProcess from 'child_process';
import fs from 'fs';
import net, { ListenOptions, Server } from 'net';
import os from 'os';
import { promisify } from 'util';

import * as pty from 'node-pty';
Expand Down Expand Up @@ -201,16 +200,14 @@ export class PtyServiceProxy implements IPtyProxyRPCService {
}
}

$on(callId: number, pid: number, event: any): void {
$resize(pid: number, columns: number, rows: number): void {
const ptyInstance = this.ptyInstanceMap.get(pid);
ptyInstance?.on(event, (e) => {
this.$callback(callId, e);
});
ptyInstance?.resize(columns, rows);
}

$resize(pid: number, columns: number, rows: number): void {
$clear(pid: number): void {
const ptyInstance = this.ptyInstanceMap.get(pid);
ptyInstance?.resize(columns, rows);
ptyInstance?.clear();
}

$write(pid: number, data: string): void {
Expand Down
2 changes: 1 addition & 1 deletion tools/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@opensumi/vscode-ripgrep": "^1.4.0",
"@parcel/watcher": "2.1.0",
"node-gyp": "^9.3.0",
"node-pty": "0.11.0-beta19",
"node-pty": "1.0.0",
"spdlog": "^0.9.0",
"vscode-oniguruma": "1.6.1"
},
Expand Down
24 changes: 16 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3548,7 +3548,7 @@ __metadata:
"@types/http-proxy": ^1.17.2
event-kit: ^2.5.3
http-proxy: ^1.18.0
node-pty: 0.11.0-beta19
node-pty: 1.0.0
os-locale: ^4.0.0
xterm: 5.2.0
xterm-addon-canvas: 0.4.0
Expand Down Expand Up @@ -15797,6 +15797,15 @@ __metadata:
languageName: node
linkType: hard

"nan@npm:^2.17.0":
version: 2.18.0
resolution: "nan@npm:2.18.0"
dependencies:
node-gyp: latest
checksum: 4fe42f58456504eab3105c04a5cffb72066b5f22bd45decf33523cb17e7d6abc33cca2a19829407b9000539c5cb25f410312d4dc5b30220167a3594896ea6a0a
languageName: node
linkType: hard

"nanoid@npm:3.3.4, nanoid@npm:^3.3.4":
version: 3.3.4
resolution: "nanoid@npm:3.3.4"
Expand Down Expand Up @@ -16062,14 +16071,13 @@ __metadata:
languageName: node
linkType: hard

"node-pty@npm:0.11.0-beta19":
version: 0.11.0-beta19
resolution: "node-pty@npm:0.11.0-beta19"
"node-pty@npm:1.0.0":
version: 1.0.0
resolution: "node-pty@npm:1.0.0"
dependencies:
nan: ^2.14.0
peerDependencies:
node-gyp: ^8.3.0
checksum: 1bad0f546bf446e84e850e3f437e38c8eefc6fcf6667cfc191e99bdb25929e6d8996b09a6c8ecb57ae4157a036858579d56a9167466cffea2c59ee3266bac4a3
nan: ^2.17.0
node-gyp: latest
checksum: 861b1708d81030da4f1f8cf06f367737de4e2e88ee6144bd0d1aadb57bdf1c09bd4c481453e462d2c9af9cc122d9643c7a8ac9c4a9ef1462c0486d741b220d09
languageName: node
linkType: hard

Expand Down

0 comments on commit 6e9cc7e

Please sign in to comment.