fixed zmodem corruption issues (#10587)

This commit is contained in:
Eugene
2025-07-23 09:31:01 +02:00
committed by GitHub
parent 7e1905c32c
commit 3c17654180

View File

@@ -27,7 +27,7 @@ class ZModemMiddleware extends SessionMiddleware {
this.logger = log.create('zmodem') this.logger = log.create('zmodem')
this.sentry = new ZModem.Sentry({ this.sentry = new ZModem.Sentry({
to_terminal: data => { to_terminal: data => {
if (this.isActive) { if (this.isActive && this.activeSession) {
this.outputToTerminal.next(Buffer.from(data)) this.outputToTerminal.next(Buffer.from(data))
} }
}, },
@@ -42,25 +42,32 @@ class ZModemMiddleware extends SessionMiddleware {
}, },
on_retract: () => { on_retract: () => {
this.showMessage('transfer cancelled') this.showMessage('transfer cancelled')
this.activeSession = null
this.isActive = false
}, },
}) })
} }
feedFromSession (data: Buffer): void { feedFromSession (data: Buffer): void {
const chunkSize = 1024 if (this.isActive || this.activeSession) {
for (let i = 0; i <= Math.floor(data.length / chunkSize); i++) {
try { try {
this.sentry.consume(Buffer.from(data.slice(i * chunkSize, (i + 1) * chunkSize))) this.sentry.consume(data)
} catch (e) { } catch (e) {
this.showMessage(colors.bgRed.black(' Error ') + ' ' + e) this.showMessage(colors.bgRed.black(' Error ') + ' ' + e)
this.logger.error('protocol error', e) this.logger.error('protocol error', e)
this.activeSession.abort() this.activeSession?.abort()
this.activeSession = null this.activeSession = null
this.isActive = false this.isActive = false
// Don't forward the problematic data to terminal
return return
} }
} else {
try {
this.sentry.consume(data)
} catch (e) {
this.logger.error('zmodem detection error', e)
} }
if (!this.isActive) {
this.outputToTerminal.next(data) this.outputToTerminal.next(data)
} }
} }
@@ -73,6 +80,7 @@ class ZModemMiddleware extends SessionMiddleware {
this.activeSession = zsession this.activeSession = zsession
this.logger.info('new session', zsession) this.logger.info('new session', zsession)
try {
if (zsession.type === 'send') { if (zsession.type === 'send') {
const transfers = await this.platform.startUpload({ multiple: true }) const transfers = await this.platform.startUpload({ multiple: true })
let filesRemaining = transfers.length let filesRemaining = transfers.length
@@ -82,7 +90,6 @@ class ZModemMiddleware extends SessionMiddleware {
filesRemaining-- filesRemaining--
sizeRemaining -= transfer.getSize() sizeRemaining -= transfer.getSize()
} }
this.activeSession = null
await zsession.close() await zsession.close()
} else { } else {
zsession.on('offer', xfer => { zsession.on('offer', xfer => {
@@ -92,6 +99,16 @@ class ZModemMiddleware extends SessionMiddleware {
zsession.start() zsession.start()
await new Promise(resolve => zsession.on('session_end', resolve)) await new Promise(resolve => zsession.on('session_end', resolve))
}
this.showMessage(colors.bgBlue.black(' ZMODEM ') + ' Complete')
} catch (error) {
this.logger.error('ZMODEM session error', error)
this.showMessage(colors.bgRed.black(' ZMODEM ') + ` Session failed: ${error.message}`)
try {
zsession.abort()
} catch { }
} finally {
this.activeSession = null this.activeSession = null
} }
} }