Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If a subfolder does not exist, this.mkdir fails #304

Closed
ururk opened this issue May 19, 2020 · 4 comments
Closed

If a subfolder does not exist, this.mkdir fails #304

ururk opened this issue May 19, 2020 · 4 comments
Labels

Comments

@ururk
Copy link

ururk commented May 19, 2020

Node 10.16.3
node-ssh 10.0.1
Firebase cloud function environment

I'm running into an issue putting a file into a subdirectory that doesn't exist. Looking at the code, https://github.com/steelbrain/node-ssh/blob/master/src/index.ts#L475, it looks like it is supposed to handle creating subdirectories that don't exist on the target share. Also, I suspect there are test cases for this. Regardless, I am running into trouble.

When trying to put a file:

await ssh.putFile('/tmp/file.txt', '/subfolders/that/do/not/exist/file.txt');

I get this error:

resolve(this.mkdir(path_1.default.dirname(remoteFile), 'sftp', sftp).then(() => putFile(false)));
>                                       ^
>  
>  TypeError: Cannot read property 'mkdir' of undefined
>      at sftp.fastPut.err ... functions/node_modules/node-ssh/lib/cjs/index.js:380:38

this is undefined, but sftp is not. Should it be binding this in the promise call?

This fixes it for me:

    const putFile = (retry: boolean) => {
      return new Promise(function(resolve, reject) {
        sftp.fastPut(localFile, unixifyPath(remoteFile), transferOptions || {}, err => {
          if (err == null) {
            resolve()
            return
          }

          if (err.message === 'No such file' && retry) {
            resolve(this.mkdir(fsPath.dirname(remoteFile), 'sftp', sftp).then(() => putFile(false)))
          } else {
            reject(err)
          }
        })
      }.bind(this))
    }

There are other calls where this.mkdir is being wrapped by a promise, but I'm not using those methods, they may behave similarly.

@ururk
Copy link
Author

ururk commented May 19, 2020

One other point - I cannot imagine that this is broken like this and no one else has posted a bug report. Could I be doing something wrong, or is the share I'm connecting to somehow non-standard?

@ururk
Copy link
Author

ururk commented May 19, 2020

Another thought - if this were an arrow function, I imagine it would bind to the context of the parent:

      return new Promise((resolve, reject) => {

@steelbrain
Copy link
Owner

Fixing, gimme ten

@steelbrain
Copy link
Owner

Fixed in node-ssh@10.0.2

steelbrain pushed a commit that referenced this issue May 19, 2020
Fixes #304
Thanks @ururk for reporting
@steelbrain steelbrain added the bug label May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants