From e17ba8c35108c2a03e1e788931cc551fe05676c8 Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Sun, 4 Jul 2021 17:11:21 +0200 Subject: [PATCH] move createSession() into session --- tabby-serial/src/api.ts | 54 ++++++++++++++- .../src/components/serialTab.component.ts | 8 +-- tabby-serial/src/profiles.ts | 7 +- tabby-serial/src/services/serial.service.ts | 66 +------------------ tabby-ssh/src/api.ts | 4 +- tabby-ssh/src/components/sshTab.component.ts | 4 +- tabby-ssh/src/services/ssh.service.ts | 6 -- tabby-terminal/src/api/streamProcessing.ts | 4 +- 8 files changed, 69 insertions(+), 84 deletions(-) diff --git a/tabby-serial/src/api.ts b/tabby-serial/src/api.ts index 94f90b72..a3e23f2b 100644 --- a/tabby-serial/src/api.ts +++ b/tabby-serial/src/api.ts @@ -1,7 +1,8 @@ import stripAnsi from 'strip-ansi' -import { SerialPort } from 'serialport' -import { Logger, Profile } from 'tabby-core' +import SerialPort from 'serialport' +import { Logger, LogService, NotificationsService, Profile } from 'tabby-core' import { Subject, Observable } from 'rxjs' +import { Injector, NgZone } from '@angular/core' import { BaseSession, StreamProcessingOptions, TerminalStreamProcessor } from 'tabby-terminal' export interface LoginScript { @@ -46,9 +47,16 @@ export class SerialSession extends BaseSession { get serviceMessage$ (): Observable { return this.serviceMessage } private serviceMessage = new Subject() private streamProcessor: TerminalStreamProcessor + private zone: NgZone + private notifications: NotificationsService - constructor (public profile: SerialProfile) { + constructor (injector: Injector, public profile: SerialProfile) { super() + + this.logger = injector.get(LogService).create(`serial-${profile.options.port}`) + this.zone = injector.get(NgZone) + this.notifications = injector.get(NotificationsService) + this.scripts = profile.options.scripts ?? [] this.streamProcessor = new TerminalStreamProcessor(profile.options) this.streamProcessor.outputToSession$.subscribe(data => { @@ -102,6 +110,46 @@ export class SerialSession extends BaseSession { } async start (): Promise { + this.serial = new SerialPort(this.profile.options.port, { + autoOpen: false, + baudRate: parseInt(this.profile.options.baudrate as any), + dataBits: this.profile.options.databits ?? 8, + stopBits: this.profile.options.stopbits ?? 1, + parity: this.profile.options.parity ?? 'none', + rtscts: this.profile.options.rtscts ?? false, + xon: this.profile.options.xon ?? false, + xoff: this.profile.options.xoff ?? false, + xany: this.profile.options.xany ?? false, + }) + let connected = false + await new Promise(async (resolve, reject) => { + this.serial.on('open', () => { + connected = true + this.zone.run(resolve) + }) + this.serial.on('error', error => { + this.zone.run(() => { + if (connected) { + this.notifications.error(error.toString()) + } else { + reject(error) + } + this.destroy() + }) + }) + this.serial.on('close', () => { + this.emitServiceMessage('Port closed') + this.destroy() + }) + + try { + this.serial.open() + } catch (e) { + this.notifications.error(e.message) + reject(e) + } + }) + this.open = true this.serial.on('readable', () => { diff --git a/tabby-serial/src/components/serialTab.component.ts b/tabby-serial/src/components/serialTab.component.ts index 243f5ade..b68670ea 100644 --- a/tabby-serial/src/components/serialTab.component.ts +++ b/tabby-serial/src/components/serialTab.component.ts @@ -5,7 +5,6 @@ import { Component, Injector } from '@angular/core' import { first } from 'rxjs/operators' import { SelectorService } from 'tabby-core' import { BaseTerminalTabComponent } from 'tabby-terminal' -import { SerialService } from '../services/serial.service' import { SerialSession, BAUD_RATES, SerialProfile } from '../api' /** @hidden */ @@ -19,7 +18,6 @@ export class SerialTabComponent extends BaseTerminalTabComponent { profile?: SerialProfile session: SerialSession|null = null serialPort: any - private serialService: SerialService // eslint-disable-next-line @typescript-eslint/no-useless-constructor constructor ( @@ -27,7 +25,6 @@ export class SerialTabComponent extends BaseTerminalTabComponent { private selector: SelectorService, ) { super(injector) - this.serialService = injector.get(SerialService) } ngOnInit () { @@ -67,7 +64,7 @@ export class SerialTabComponent extends BaseTerminalTabComponent { return } - const session = this.serialService.createSession(this.profile) + const session = new SerialSession(this.injector, this.profile) this.setSession(session) this.write(`Connecting to `) @@ -81,7 +78,7 @@ export class SerialTabComponent extends BaseTerminalTabComponent { spinner.start() try { - this.serialPort = await this.serialService.connectSession(this.session!) + await this.session!.start() spinner.stop(true) session.emitServiceMessage('Port opened') } catch (e) { @@ -89,7 +86,6 @@ export class SerialTabComponent extends BaseTerminalTabComponent { this.write(colors.black.bgRed(' X ') + ' ' + colors.red(e.message) + '\r\n') return } - await this.session!.start() this.session!.resize(this.size.columns, this.size.rows) } diff --git a/tabby-serial/src/profiles.ts b/tabby-serial/src/profiles.ts index 5e1130b4..b3cf04df 100644 --- a/tabby-serial/src/profiles.ts +++ b/tabby-serial/src/profiles.ts @@ -2,6 +2,7 @@ import slugify from 'slugify' import deepClone from 'clone-deep' import { Injectable } from '@angular/core' import { ProfileProvider, NewTabParameters, SelectorService } from 'tabby-core' +import { InputMode, NewlineMode } from 'tabby-terminal' import { SerialProfileSettingsComponent } from './components/serialProfileSettings.component' import { SerialTabComponent } from './components/serialTab.component' import { SerialService } from './services/serial.service' @@ -34,10 +35,10 @@ export class SerialProfilesService extends ProfileProvider { xany: false, xoff: false, xon: false, - inputMode: null, + inputMode: 'local-echo' as InputMode, outputMode: null, inputNewlines: null, - outputNewlines: null, + outputNewlines: 'crlf' as NewlineMode, }, isBuiltin: true, isTemplate: true, @@ -50,6 +51,8 @@ export class SerialProfilesService extends ProfileProvider { isBuiltin: true, options: { port: p.name, + inputMode: 'local-echo' as InputMode, + outputNewlines: 'crlf' as NewlineMode, }, })), ] diff --git a/tabby-serial/src/services/serial.service.ts b/tabby-serial/src/services/serial.service.ts index a4155a73..d71922c4 100644 --- a/tabby-serial/src/services/serial.service.ts +++ b/tabby-serial/src/services/serial.service.ts @@ -1,17 +1,13 @@ -import { Injectable, NgZone } from '@angular/core' +import { Injectable } from '@angular/core' import SerialPort from 'serialport' -import { LogService, NotificationsService, SelectorService, ProfilesService } from 'tabby-core' -import { SerialSession, SerialPortInfo, BAUD_RATES, SerialProfile } from '../api' +import { ProfilesService } from 'tabby-core' +import { SerialPortInfo, SerialProfile } from '../api' import { SerialTabComponent } from '../components/serialTab.component' @Injectable({ providedIn: 'root' }) export class SerialService { private constructor ( - private log: LogService, - private zone: NgZone, - private notifications: NotificationsService, private profilesService: ProfilesService, - private selector: SelectorService, ) { } async listPorts (): Promise { @@ -21,55 +17,6 @@ export class SerialService { })) } - createSession (profile: SerialProfile): SerialSession { - const session = new SerialSession(profile) - session.logger = this.log.create(`serial-${profile.options.port}`) - return session - } - - async connectSession (session: SerialSession): Promise { - const serial = new SerialPort(session.profile.options.port, { - autoOpen: false, - baudRate: parseInt(session.profile.options.baudrate as any), - dataBits: session.profile.options.databits, - stopBits: session.profile.options.stopbits, - parity: session.profile.options.parity, - rtscts: session.profile.options.rtscts, - xon: session.profile.options.xon, - xoff: session.profile.options.xoff, - xany: session.profile.options.xany, - }) - session.serial = serial - let connected = false - await new Promise(async (resolve, reject) => { - serial.on('open', () => { - connected = true - this.zone.run(resolve) - }) - serial.on('error', error => { - this.zone.run(() => { - if (connected) { - this.notifications.error(error.toString()) - } else { - reject(error) - } - }) - }) - serial.on('close', () => { - session.emitServiceMessage('Port closed') - session.destroy() - }) - - try { - serial.open() - } catch (e) { - this.notifications.error(e.message) - reject(e) - } - }) - return serial - } - quickConnect (query: string): Promise { let path = query let baudrate = 115200 @@ -95,11 +42,4 @@ export class SerialService { window.localStorage.lastSerialConnection = JSON.stringify(profile) return this.profilesService.openNewTabForProfile(profile) as Promise } - - async connectFoundPort (port: SerialPortInfo): Promise { - const rate = await this.selector.show('Baud rate', BAUD_RATES.map(x => ({ - name: x.toString(), result: x, - }))) - return this.quickConnect(`${port.name}@${rate}`) - } } diff --git a/tabby-ssh/src/api.ts b/tabby-ssh/src/api.ts index e42d0e99..5f09dbdd 100644 --- a/tabby-ssh/src/api.ts +++ b/tabby-ssh/src/api.ts @@ -10,7 +10,7 @@ import stripAnsi from 'strip-ansi' import socksv5 from 'socksv5' import { Injector, NgZone } from '@angular/core' import { NgbModal } from '@ng-bootstrap/ng-bootstrap' -import { ConfigService, FileProvidersService, HostAppService, Logger, NotificationsService, Platform, PlatformService, wrapPromise, PromptModalComponent, Profile } from 'tabby-core' +import { ConfigService, FileProvidersService, HostAppService, Logger, NotificationsService, Platform, PlatformService, wrapPromise, PromptModalComponent, Profile, LogService } from 'tabby-core' import { BaseSession } from 'tabby-terminal' import { Server, Socket, createServer, createConnection } from 'net' import { Client, ClientChannel, SFTPWrapper } from 'ssh2' @@ -287,6 +287,8 @@ export class SSHSession extends BaseSession { public profile: SSHProfile, ) { super() + this.logger = injector.get(LogService).create(`ssh-${profile.options.host}-${profile.options.port}`) + this.passwordStorage = injector.get(PasswordStorageService) this.ngbModal = injector.get(NgbModal) this.hostApp = injector.get(HostAppService) diff --git a/tabby-ssh/src/components/sshTab.component.ts b/tabby-ssh/src/components/sshTab.component.ts index 9c638bf3..8f5c07c7 100644 --- a/tabby-ssh/src/components/sshTab.component.ts +++ b/tabby-ssh/src/components/sshTab.component.ts @@ -90,7 +90,7 @@ export class SSHTabComponent extends BaseTerminalTabComponent { throw new Error(`${session.profile.options.host}: jump host "${session.profile.options.jumpHost}" not found in your config`) } - const jumpSession = this.ssh.createSession(jumpConnection) + const jumpSession = new SSHSession(this.injector, jumpConnection) await this.setupOneSession(jumpSession) @@ -173,7 +173,7 @@ export class SSHTabComponent extends BaseTerminalTabComponent { return } - const session = this.ssh.createSession(this.profile) + const session = new SSHSession(this.injector, this.profile) this.setSession(session) try { diff --git a/tabby-ssh/src/services/ssh.service.ts b/tabby-ssh/src/services/ssh.service.ts index 1a04875d..368ba70b 100644 --- a/tabby-ssh/src/services/ssh.service.ts +++ b/tabby-ssh/src/services/ssh.service.ts @@ -32,12 +32,6 @@ export class SSHService { } } - createSession (profile: SSHProfile): SSHSession { - const session = new SSHSession(this.injector, profile) - session.logger = this.log.create(`ssh-${profile.options.host}-${profile.options.port}`) - return session - } - async connectSession (session: SSHSession): Promise { const log = (s: any) => session.emitServiceMessage(s) diff --git a/tabby-terminal/src/api/streamProcessing.ts b/tabby-terminal/src/api/streamProcessing.ts index bb774761..03fdc6c0 100644 --- a/tabby-terminal/src/api/streamProcessing.ts +++ b/tabby-terminal/src/api/streamProcessing.ts @@ -87,7 +87,9 @@ export class TerminalStreamProcessor { } resize (): void { - this.inputReadlineOutStream.emit('resize') + if (this.options.inputMode?.startsWith('readline')) { + this.inputReadlineOutStream.emit('resize') + } } close (): void {