simplify attach addon
This commit is contained in:
@@ -54,7 +54,7 @@ describe('AttachAddon', () => {
|
|||||||
const server = new WebSocket.Server({ port });
|
const server = new WebSocket.Server({ port });
|
||||||
const data = new Uint8Array([102, 111, 111]);
|
const data = new Uint8Array([102, 111, 111]);
|
||||||
server.on('connection', socket => socket.send(data));
|
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');
|
assert.equal(await page.evaluate(`window.term.buffer.getLine(0).translateToString(true)`), 'foo');
|
||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,13 +9,11 @@ import { Terminal, IDisposable, ITerminalAddon } from 'xterm';
|
|||||||
|
|
||||||
interface IAttachOptions {
|
interface IAttachOptions {
|
||||||
bidirectional?: boolean;
|
bidirectional?: boolean;
|
||||||
inputUtf8?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AttachAddon implements ITerminalAddon {
|
export class AttachAddon implements ITerminalAddon {
|
||||||
private _socket: WebSocket;
|
private _socket: WebSocket;
|
||||||
private _bidirectional: boolean;
|
private _bidirectional: boolean;
|
||||||
private _utf8: boolean;
|
|
||||||
private _disposables: IDisposable[] = [];
|
private _disposables: IDisposable[] = [];
|
||||||
|
|
||||||
constructor(socket: WebSocket, options?: IAttachOptions) {
|
constructor(socket: WebSocket, options?: IAttachOptions) {
|
||||||
@@ -23,17 +21,17 @@ export class AttachAddon implements ITerminalAddon {
|
|||||||
// always set binary type to arraybuffer, we do not handle blobs
|
// always set binary type to arraybuffer, we do not handle blobs
|
||||||
this._socket.binaryType = 'arraybuffer';
|
this._socket.binaryType = 'arraybuffer';
|
||||||
this._bidirectional = (options && options.bidirectional === false) ? false : true;
|
this._bidirectional = (options && options.bidirectional === false) ? false : true;
|
||||||
this._utf8 = !!(options && options.inputUtf8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public activate(terminal: Terminal): void {
|
public activate(terminal: Terminal): void {
|
||||||
if (this._utf8) {
|
this._disposables.push(
|
||||||
this._disposables.push(addSocketListener(this._socket, 'message',
|
addSocketListener(this._socket, 'message',
|
||||||
(ev: MessageEvent | Event | CloseEvent) => terminal.writeUtf8(new Uint8Array((ev as any).data as ArrayBuffer))));
|
(ev: MessageEvent | Event | CloseEvent) => {
|
||||||
} else {
|
const data: ArrayBuffer | string = (ev as any).data;
|
||||||
this._disposables.push(addSocketListener(this._socket, 'message',
|
terminal.write(typeof data === 'string' ? data : new Uint8Array(data));
|
||||||
(ev: MessageEvent | Event | CloseEvent) => terminal.write((ev as any).data as string)));
|
}
|
||||||
}
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if (this._bidirectional) {
|
if (this._bidirectional) {
|
||||||
this._disposables.push(terminal.onData(data => this._sendData(data)));
|
this._disposables.push(terminal.onData(data => this._sendData(data)));
|
||||||
|
|||||||
@@ -11,14 +11,6 @@ declare module 'xterm-addon-attach' {
|
|||||||
* Whether input should be written to the backend. Defaults to `true`.
|
* Whether input should be written to the backend. Defaults to `true`.
|
||||||
*/
|
*/
|
||||||
bidirectional?: boolean;
|
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 {
|
export class AttachAddon implements ITerminalAddon {
|
||||||
|
|||||||
@@ -167,14 +167,7 @@ function createTerminal(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function runRealTerminal(): 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));
|
||||||
// term.loadAddon(new AttachAddon(socket, {inputUtf8: true}));
|
|
||||||
|
|
||||||
term._initialized = true;
|
term._initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-5
@@ -4,10 +4,9 @@ var os = require('os');
|
|||||||
var pty = require('node-pty');
|
var pty = require('node-pty');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether to use UTF8 binary transport.
|
* Whether to use binary transport.
|
||||||
* (Must also be switched in client.ts)
|
|
||||||
*/
|
*/
|
||||||
const USE_BINARY_UTF8 = false;
|
const USE_BINARY = true;
|
||||||
|
|
||||||
|
|
||||||
function startServer() {
|
function startServer() {
|
||||||
@@ -46,7 +45,7 @@ function startServer() {
|
|||||||
rows: rows || 24,
|
rows: rows || 24,
|
||||||
cwd: env.PWD,
|
cwd: env.PWD,
|
||||||
env: env,
|
env: env,
|
||||||
encoding: USE_BINARY_UTF8 ? null : 'utf8'
|
encoding: USE_BINARY ? null : 'utf8'
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Created terminal with PID: ' + term.pid);
|
console.log('Created terminal with PID: ' + term.pid);
|
||||||
@@ -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) {
|
term.on('data', function(data) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
+1
-1
@@ -1597,7 +1597,7 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
|
|||||||
public write(data: string | Uint8Array, callback?: () => void): void {
|
public write(data: string | Uint8Array, callback?: () => void): void {
|
||||||
this._writeBuffer.write(data, callback);
|
this._writeBuffer.write(data, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public writeSync(data: string | Uint8Array): void {
|
public writeSync(data: string | Uint8Array): void {
|
||||||
this._writeBuffer.writeSync(data);
|
this._writeBuffer.writeSync(data);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user