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

write callback support #2422

Merged
merged 19 commits into from
Sep 26, 2019
Merged
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
2 changes: 1 addition & 1 deletion addons/xterm-addon-attach/src/AttachAddon.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('AttachAddon', () => {
const server = new WebSocket.Server({ port });
const data = new Uint8Array([102, 111, 111]);
server.on('connection', socket => socket.send(data));
await page.evaluate(`window.term.loadAddon(new window.AttachAddon(new WebSocket('ws://localhost:${port}'), { inputUtf8: true }))`);
await page.evaluate(`window.term.loadAddon(new window.AttachAddon(new WebSocket('ws://localhost:${port}')))`);
assert.equal(await page.evaluate(`window.term.buffer.getLine(0).translateToString(true)`), 'foo');
server.close();
});
Expand Down
18 changes: 7 additions & 11 deletions addons/xterm-addon-attach/src/AttachAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,27 @@ import { Terminal, IDisposable, ITerminalAddon } from 'xterm';

interface IAttachOptions {
bidirectional?: boolean;
inputUtf8?: boolean;
}

export class AttachAddon implements ITerminalAddon {
private _socket: WebSocket;
private _bidirectional: boolean;
private _utf8: boolean;
private _disposables: IDisposable[] = [];

constructor(socket: WebSocket, options?: IAttachOptions) {
this._socket = socket;
// always set binary type to arraybuffer, we do not handle blobs
this._socket.binaryType = 'arraybuffer';
this._bidirectional = (options && options.bidirectional === false) ? false : true;
this._utf8 = !!(options && options.inputUtf8);
}

public activate(terminal: Terminal): void {
if (this._utf8) {
this._disposables.push(addSocketListener(this._socket, 'message',
(ev: MessageEvent | Event | CloseEvent) => terminal.writeUtf8(new Uint8Array((ev as any).data as ArrayBuffer))));
} else {
this._disposables.push(addSocketListener(this._socket, 'message',
(ev: MessageEvent | Event | CloseEvent) => terminal.write((ev as any).data as string)));
}
this._disposables.push(
addSocketListener(this._socket, 'message', ev => {
const data: ArrayBuffer | string = ev.data;
terminal.write(typeof data === 'string' ? data : new Uint8Array(data));
})
);

if (this._bidirectional) {
this._disposables.push(terminal.onData(data => this._sendData(data)));
Expand All @@ -57,7 +53,7 @@ export class AttachAddon implements ITerminalAddon {
}
}

function addSocketListener(socket: WebSocket, type: string, handler: (this: WebSocket, ev: MessageEvent | Event | CloseEvent) => any): IDisposable {
function addSocketListener<K extends keyof WebSocketEventMap>(socket: WebSocket, type: K, handler: (this: WebSocket, ev: WebSocketEventMap[K]) => any): IDisposable {
socket.addEventListener(type, handler);
return {
dispose: () => {
Expand Down
8 changes: 0 additions & 8 deletions addons/xterm-addon-attach/typings/xterm-addon-attach.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ declare module 'xterm-addon-attach' {
* Whether input should be written to the backend. Defaults to `true`.
*/
bidirectional?: boolean;

/**
* Whether to use UTF8 binary transport for incoming messages. Defaults to `false`.
* Note: This must be in line with the server side of the websocket.
* Always send string messages from the backend if this options is false,
* otherwise always binary UTF8 data.
*/
inputUtf8?: boolean;
}

export class AttachAddon implements ITerminalAddon {
Expand Down
7 changes: 1 addition & 6 deletions addons/xterm-addon-search/src/SearchAddon.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ async function openTerminal(options: ITerminalOptions = {}): Promise<void> {
}

async function writeSync(data: string): Promise<void> {
await page.evaluate(`window.term.write('${data}');`);
while (true) {
if (await page.evaluate(`window.term._core.writeBuffer.length === 0`)) {
break;
}
}
return page.evaluate(`new Promise(resolve => window.term.write('${data}', resolve))`);
}

function makeData(length: number): string {
Expand Down
7 changes: 1 addition & 6 deletions addons/xterm-addon-webgl/src/WebglRenderer.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ async function openTerminal(options: ITerminalOptions = {}): Promise<void> {
}

async function writeSync(data: string): Promise<void> {
await page.evaluate(`window.term.write('${data}');`);
while (true) {
if (await page.evaluate(`window.term._core.writeBuffer.length === 0`)) {
break;
}
}
return page.evaluate(`new Promise(resolve => window.term.write('${data}', resolve))`);
}

async function getCellColor(col: number, row: number): Promise<number[]> {
Expand Down
7 changes: 0 additions & 7 deletions demo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,7 @@ function createTerminal(): void {
}

function runRealTerminal(): void {
/**
* The demo defaults to string transport by default.
* To run it with UTF8 binary transport, swap comment on
* the lines below. (Must also be switched in server.js)
*/
term.loadAddon(new AttachAddon(socket));
// term.loadAddon(new AttachAddon(socket, {inputUtf8: true}));

term._initialized = true;
}

Expand Down
9 changes: 4 additions & 5 deletions demo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ var os = require('os');
var pty = require('node-pty');

/**
* Whether to use UTF8 binary transport.
* (Must also be switched in client.ts)
* Whether to use binary transport.
*/
const USE_BINARY_UTF8 = false;
const USE_BINARY = true;


function startServer() {
Expand Down Expand Up @@ -46,7 +45,7 @@ function startServer() {
rows: rows || 24,
cwd: env.PWD,
env: env,
encoding: USE_BINARY_UTF8 ? null : 'utf8'
encoding: USE_BINARY ? null : 'utf8'
});

console.log('Created terminal with PID: ' + term.pid);
Expand Down Expand Up @@ -108,7 +107,7 @@ function startServer() {
}
};
}
const send = USE_BINARY_UTF8 ? bufferUtf8(ws, 5) : buffer(ws, 5);
const send = USE_BINARY ? bufferUtf8(ws, 5) : buffer(ws, 5);

term.on('data', function(data) {
try {
Expand Down
27 changes: 7 additions & 20 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class InputHandler extends Disposable implements IInputHandler {
super.dispose();
}

public parse(data: string): void {
public parse(data: string | Uint8Array): void {
let buffer = this._bufferService.buffer;
const cursorStartX = buffer.x;
const cursorStartY = buffer.y;
Expand All @@ -334,30 +334,17 @@ export class InputHandler extends Disposable implements IInputHandler {
if (this._parseBuffer.length < data.length) {
this._parseBuffer = new Uint32Array(data.length);
}
this._parser.parse(this._parseBuffer, this._stringDecoder.decode(data, this._parseBuffer));

buffer = this._bufferService.buffer;
if (buffer.x !== cursorStartX || buffer.y !== cursorStartY) {
this._onCursorMove.fire();
}
}

public parseUtf8(data: Uint8Array): void {
let buffer = this._bufferService.buffer;
const cursorStartX = buffer.x;
const cursorStartY = buffer.y;

this._logService.debug('parsing data', data);

if (this._parseBuffer.length < data.length) {
this._parseBuffer = new Uint32Array(data.length);
}
this._parser.parse(this._parseBuffer, this._utf8Decoder.decode(data, this._parseBuffer));
this._parser.parse(this._parseBuffer,
(typeof data === 'string')
? this._stringDecoder.decode(data, this._parseBuffer)
: this._utf8Decoder.decode(data, this._parseBuffer)
);

buffer = this._bufferService.buffer;
if (buffer.x !== cursorStartX || buffer.y !== cursorStartY) {
this._onCursorMove.fire();
}
this._terminal.refresh(this._dirtyRowService.start, this._dirtyRowService.end);
}

public print(data: Uint32Array, start: number, end: number): void {
Expand Down
Loading