FGasper / zmodemjs

zmodem.js - ZMODEM in JavaScript
Apache License 2.0
129 stars 25 forks source link

Unhandled header! #23

Closed NeroSolomon closed 3 years ago

NeroSolomon commented 3 years ago

Describe the bug I use zmodemjs in xterm.js which version in 3.14.5, when enter sz, console get error: Uncaught Error: Unhandled header: ZRQINIT image

What happens when you follow README.md’s TROUBLESHOOTING steps?

  1. when i create an empty file and use sz to download, console get error.
  2. Transfer a small file, get same error.
  3. Transfer a file that contains all possible octets, get same error.

To Reproduce Steps to reproduce the behavior:

  1. apply plugin
    import 'zmodem.js/dist/zmodem.devel'
    import * as zmodemAddOn from 'xterm/dist/addons/zmodem/zmodem'
    // ....
    Terminal.applyAddon(zmodemAddOn)
  2. attach zmode after websocket connected
    // onOpen
    () => {
    this.xterm.zmodemAttach(this.webSocket, {
        noTerminalWriteOutsideSession: true,
    })
    }
  3. listen zmodemDetect after terminal inited

    
    this.xterm.on('zmodemDetect', (detection) => {
    try {
        this.xterm.detach()
    } catch (e) {
        console.error(e)
    }
    
    const zsession = detection.confirm()
    
    let promise
    if (zsession.type === 'receive') {
        promise = this.downloadFile(zsession)
    } else {
        promise = this.uploadFile(zsession)
    }
    
     promise
        .then(() => {
            try {
                term.detach()
            } catch (e) {
              console.error(e);
            }
    
           try {
                term.attach(this.webSocket)
            } catch (e) {
              console.error(e);
            }
        })
        .catch((err: Error) => {
            console.error(err)
        })
    })

// download function downloadFile = (zsession: any) => { const self = this zsession.on('offer', (xfer: any) => { function on_form_submit() { const FILE_BUFFER: any[] = [] xfer.on('input', (payload: any) => { self.updateProgress(xfer) FILE_BUFFER.push(new Uint8Array(payload)) })

  xfer
    .accept()
    .then(() => {
      self.saveFile(xfer, FILE_BUFFER)
      this.xterm.write('\r\n')
    })
    .catch((err: Error) => {
      console.error(err)
    })
}

on_form_submit()

})

const promise = new Promise((res) => { zsession.on('session_end', () => { console.log('-----zession close----') res('') }) })

zsession.start() return promise }

// save file function saveFile = (xfer, buffer) => { return Zmodem.Browser.save_to_disk(buffer, xfer.get_details().name) }


3. enter sz empty.bin
4. See error

**Expected behavior**
download success

**Screenshots**
![image](https://user-images.githubusercontent.com/28894970/115228388-460c6e80-a144-11eb-80c5-6b3d594cbc74.png)
![image](https://user-images.githubusercontent.com/28894970/115240994-e4073580-a152-11eb-8623-1cf132815e14.png)
![image](https://user-images.githubusercontent.com/28894970/115241670-ac4cbd80-a153-11eb-8456-cdabc18bd9e9.png)

**Environment(s):**
 - OS: MacOS
 - JS engine : Chrome

**Additional context**
I found the same problem in issues, but I can't find the solution. 

I understand that may not be a bug, maybe it's my usage problem, but i have no idea if this is a back-end problem or a front-end problem. 

I though this is a back-end problem because the header order is wrong, but my companion told me, he can download file with terminal app from the same machine. 

My code is copy from gowebssh, and i check it  for many time, but I still can't solve this problem. So, can you give me some suggestions?
FGasper commented 3 years ago

Hi!

I’m going to close this since it’s not really a bug report so much as a request for assistance, which I unfortunately don’t have the time to provide.

I did just create a discussion for this repo, as this is a not-infrequent issue that folks report. (It’s generally been, as you suspect, a usage problem rather than a bug in zmodem.js.)

Note that the distribution does include a bin/ directory that includes a CLI demonstration of the workflow you’re asking about.

The only suggestion I can offer is to ensure that your WebSocket connection is using binary messages. Text messages don’t work.

NeroSolomon commented 3 years ago

@FGasper Thanks a lot!