zmodem fixes (fixes #1949)

This commit is contained in:
Eugene Pankov 2020-01-14 11:41:05 +01:00
parent 472b421484
commit 5507171fee

View File

@ -15,7 +15,6 @@ const SPACER = ' '
export class ZModemDecorator extends TerminalDecorator { export class ZModemDecorator extends TerminalDecorator {
private subscriptions: Subscription[] = [] private subscriptions: Subscription[] = []
private logger: Logger private logger: Logger
private sentry
private activeSession: any = null private activeSession: any = null
constructor ( constructor (
@ -28,8 +27,12 @@ export class ZModemDecorator extends TerminalDecorator {
} }
attach (terminal: TerminalTabComponent): void { attach (terminal: TerminalTabComponent): void {
this.sentry = new ZModem.Sentry({ const sentry = new ZModem.Sentry({
to_terminal: () => null, to_terminal: data => {
if (!terminal.enablePassthrough) {
terminal.write(data)
}
},
sender: data => terminal.session.write(Buffer.from(data)), sender: data => terminal.session.write(Buffer.from(data)),
on_detect: async detection => { on_detect: async detection => {
try { try {
@ -49,7 +52,7 @@ export class ZModemDecorator extends TerminalDecorator {
const chunkSize = 1024 const chunkSize = 1024
for (let i = 0; i <= Math.floor(data.length / chunkSize); i++) { for (let i = 0; i <= Math.floor(data.length / chunkSize); i++) {
try { try {
this.sentry.consume(data.subarray(i * chunkSize, (i + 1) * chunkSize)) sentry.consume(data.subarray(i * chunkSize, (i + 1) * chunkSize))
} catch (e) { } catch (e) {
this.logger.error('protocol error', e) this.logger.error('protocol error', e)
this.activeSession.abort() this.activeSession.abort()
@ -109,7 +112,7 @@ export class ZModemDecorator extends TerminalDecorator {
private async receiveFile (terminal, xfer) { private async receiveFile (terminal, xfer) {
const details = xfer.get_details() const details = xfer.get_details()
this.showMessage(terminal, `🟡 Offered ${details.name}`, true) this.showMessage(terminal, `🟡 Offered ${details.name}`, true)
this.logger.info('offered', xfer) this.logger.info('offered', xfer)
const result = await this.electron.dialog.showSaveDialog( const result = await this.electron.dialog.showSaveDialog(
this.hostApp.getWindow(), this.hostApp.getWindow(),
@ -118,7 +121,7 @@ export class ZModemDecorator extends TerminalDecorator {
}, },
) )
if (!result.filePath) { if (!result.filePath) {
this.showMessage(terminal, `🔴 Rejected ${details.name}`) this.showMessage(terminal, `🔴 Rejected ${details.name}`)
xfer.skip() xfer.skip()
return return
} }
@ -128,10 +131,10 @@ export class ZModemDecorator extends TerminalDecorator {
on_input: chunk => { on_input: chunk => {
stream.write(Buffer.from(chunk)) stream.write(Buffer.from(chunk))
bytesSent += chunk.length bytesSent += chunk.length
this.showMessage(terminal, `🟡 Receiving ${details.name}: ${Math.round(100 * bytesSent / details.size)}%`, true) this.showMessage(terminal, `🟡 Receiving ${details.name}: ${Math.round(100 * bytesSent / details.size)}%`, true)
}, },
}) })
this.showMessage(terminal, `Received ${details.name}`) this.showMessage(terminal, ` Received ${details.name}`)
stream.end() stream.end()
} }
@ -146,7 +149,7 @@ export class ZModemDecorator extends TerminalDecorator {
bytes_remaining: stat.size, bytes_remaining: stat.size,
} }
this.logger.info('offering', offer) this.logger.info('offering', offer)
this.showMessage(terminal, `🟡 Offering ${offer.name}`, true) this.showMessage(terminal, `🟡 Offering ${offer.name}`, true)
const xfer = await zsession.send_offer(offer) const xfer = await zsession.send_offer(offer)
if (xfer) { if (xfer) {
@ -155,14 +158,14 @@ export class ZModemDecorator extends TerminalDecorator {
stream.on('data', chunk => { stream.on('data', chunk => {
xfer.send(chunk) xfer.send(chunk)
bytesSent += chunk.length bytesSent += chunk.length
this.showMessage(terminal, `🟡 Sending ${offer.name}: ${Math.round(100 * bytesSent / offer.size)}%`, true) this.showMessage(terminal, `🟡 Sending ${offer.name}: ${Math.round(100 * bytesSent / offer.size)}%`, true)
}) })
await new Promise(resolve => stream.on('end', resolve)) await new Promise(resolve => stream.on('end', resolve))
await xfer.end() await xfer.end()
stream.close() stream.close()
this.showMessage(terminal, `Sent ${offer.name}`) this.showMessage(terminal, ` Sent ${offer.name}`)
} else { } else {
this.showMessage(terminal, `🔴 Other side rejected ${offer.name}`) this.showMessage(terminal, `🔴 Other side rejected ${offer.name}`)
this.logger.warn('rejected by the other side') this.logger.warn('rejected by the other side')
} }
} }