Skip to content

Commit b0d6ed3

Browse files
refactor(terminal): decouple terminal input from JSON RPC path (#4696)
1 parent e176173 commit b0d6ed3

File tree

8 files changed

+37
-27
lines changed

8 files changed

+37
-27
lines changed

packages/extension/__tests__/hosted/api/vscode/ext.host.terminal.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ describe('ext host terminal test', () => {
7878
detectAvailableProfiles() {
7979
return [];
8080
},
81+
input() {
82+
// no-op for tests
83+
},
84+
resize() {
85+
// no-op for tests
86+
},
8187
},
8288
},
8389
{

packages/terminal-next/__tests__/browser/inject.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ export const injector = new MockInjector([
194194
pid: 0,
195195
name: '123',
196196
}),
197+
input() {
198+
// no-op for tests
199+
},
200+
resize() {
201+
// no-op for tests
202+
},
197203
$resolveUnixShellPath(p) {
198204
return p;
199205
},

packages/terminal-next/__tests__/browser/terminal.service.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ describe('terminal service test cases', () => {
9090
(terminalService as any)?.$processChange(sessionId, 'zsh');
9191
});
9292
},
93+
input() {
94+
// no-op for tests
95+
},
96+
resize() {
97+
// no-op for tests
98+
},
9399
$resolveUnixShellPath(p) {
94100
return p;
95101
},

packages/terminal-next/__tests__/node/terminal.reconnect.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class MockTerminalClient implements ITerminalServiceClient {
9292
reconnectedIds: string[] = [];
9393
disconnectedIds: string[] = [];
9494
setConnectionClientId(): void {}
95+
input(): void {}
9596
onMessage(): void {}
9697
resize(): void {}
9798
disposeById(): void {}

packages/terminal-next/__tests__/node/terminal.service.client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('TerminalServiceClientImpl', () => {
6262
receiveData = receiveData + data;
6363
});
6464

65-
terminalServiceClient.onMessage(mockId, JSON.stringify({ data: 'message test' }));
65+
terminalServiceClient.input(mockId, 'message test');
6666
terminalServiceClient.resize(mockId, 400, 400);
6767

6868
await new Promise<void>((resolve) => {

packages/terminal-next/src/browser/terminal.service.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export interface EventMessage {
2727
}
2828
@Injectable()
2929
export class NodePtyTerminalService extends Disposable implements ITerminalService {
30-
static countId = 1;
31-
3230
private backendOs: OperatingSystem | undefined;
3331

3432
@Autowired(INJECTOR_TOKEN)
@@ -117,29 +115,12 @@ export class NodePtyTerminalService extends Disposable implements ITerminalServi
117115
this.logger.error(`${sessionId} cannot create ptyInstance`, ptyInstance);
118116
}
119117

120-
private _sendMessage(sessionId: string, json: any, requestId?: number) {
121-
const id = requestId || NodePtyTerminalService.countId++;
122-
123-
this.serviceClientRPC.onMessage(
124-
sessionId,
125-
JSON.stringify({
126-
id,
127-
...json,
128-
}),
129-
);
130-
}
131-
132118
async sendText(sessionId: string, message: string) {
133-
this._sendMessage(sessionId, {
134-
data: message,
135-
});
119+
this.serviceClientRPC.input(sessionId, message);
136120
}
137121

138122
async resize(sessionId: string, cols: number, rows: number) {
139-
this._sendMessage(sessionId, {
140-
method: 'resize',
141-
params: { cols, rows },
142-
});
123+
this.serviceClientRPC.resize(sessionId, rows, cols);
143124
}
144125

145126
async getCodePlatformKey(): Promise<'osx' | 'windows' | 'linux'> {

packages/terminal-next/src/common/pty.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ export interface ITerminalServiceClient {
328328
rows: number,
329329
launchConfig: IShellLaunchConfig,
330330
): Promise<INodePtyInstance | undefined>;
331+
input(id: string, data: string): void;
331332
onMessage(id: string, msg: string): void;
332333
resize(id: string, rows: number, cols: number): void;
333334
disposeById(id: string): void;

packages/terminal-next/src/node/terminal.service.client.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,24 @@ export class TerminalServiceClientImpl extends RPCService<IRPCTerminalService> i
155155
}
156156

157157
onMessage(id: string, msg: string): void {
158-
const { data, params, method } = JSON.parse(msg);
158+
try {
159+
const { data, params, method } = JSON.parse(msg);
159160

160-
if (method === 'resize') {
161-
this.resize(id, params.rows, params.cols);
162-
} else {
163-
this.terminalService.onMessage(id, data);
161+
if (method === 'resize') {
162+
this.resize(id, params.rows, params.cols);
163+
} else {
164+
this.terminalService.onMessage(id, data);
165+
}
166+
} catch {
167+
// Fallback for legacy/raw messages.
168+
this.terminalService.onMessage(id, msg);
164169
}
165170
}
166171

172+
input(id: string, data: string): void {
173+
this.terminalService.onMessage(id, data);
174+
}
175+
167176
resize(id: string, rows: number, cols: number) {
168177
this.terminalService.resize(id, rows, cols);
169178
}

0 commit comments

Comments
 (0)