teambition / gulp-ssh

SSH and SFTP tasks for gulp
184 stars 38 forks source link

gulp-ssh.dest doesn't support merge-streams #32

Closed Rommero closed 8 years ago

Rommero commented 8 years ago

Gulp fails when trying to merge multiple streams of gulp-ssh.dest. Error "no writecb in Transform class" occurs when trying to run the following gulp-task (merging 1 stream for simplicity)

gulp.task("copy-to-remote", function() {
     var t = gulp
        .src(src)
        .pipe(gulpSSH.dest(dest))
    return merge(t)
})

Here's the traceback.

[15:53:06] gulp-ssh :: Connect...
[15:53:06] gulp-ssh :: Ready
[15:53:06] Preparing to write "/home/support/1/1.test"
[15:53:06] Writing '/home/support/1/1.test'
[15:53:06] Finished writing '/home/support/1/1.test'
[15:53:06] Preparing to write "/home/support/1/123/55.txt"
[15:53:06] Writing '/home/support/1/123/55.txt'
[15:53:06] Finished writing '/home/support/1/123/55.txt'

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: no writecb in Transform class
    at afterTransform (/home/support/project/node_modules/gulp-ssh/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:75:33)
    at TransformState.afterTransform (/home/support/project/node_modules/gulp-ssh/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:59:12)
    at end (/home/support/project/node_modules/gulp-ssh/index.js:259:9)
    at WriteStream.done (/home/support/project/node_modules/gulp-ssh/index.js:281:37)
    at WriteStream.emit (events.js:95:17)
    at /home/support/project/node_modules/gulp-ssh/node_modules/ssh2/node_modules/ssh2-streams/lib/sftp.js:2656:14
    at SFTPStream._cleanup (/home/support/project/node_modules/gulp-ssh/node_modules/ssh2/node_modules/ssh2-streams/lib/sftp.js:193:38)
    at SFTPStream.onFinish (/home/support/project/node_modules/gulp-ssh/node_modules/ssh2/node_modules/ssh2-streams/lib/sftp.js:159:10)
    at SFTPStream.emit (events.js:117:20)
    at finishMaybe (_stream_writable.js:359:12)

I suppose there's a kinda bug in through2 library usage. I'll try to figure it out and report you via PR

Rommero commented 8 years ago

The investigation showed that the issue is causeb by multiple callback calls in dest function:

return through.obj(function (file, encoding, callback) {

I'll inform of any new info on the issue when i find out smth new

zensh commented 8 years ago

Thank you, I will fixed it tomorrow.

Rommero commented 8 years ago

Some people say it's caused by the new version of ssh2 library. But the previous ones doesn't seem to help.

file.pipe(sftp.createWriteStream(outPath, options))
          .on('error', done)
          .on('finish', done)

        function done (err) {
          if (err) return end(err, callback)
          gutil.log('Finished writing \'' + gutil.colors.cyan(outPath) + '\'')
          callback()
        }

The thing is the error event is emitted after the 'finish' one and is caused by gulp completing all the tasks (or by calling sftpClient.end() or ssh.end() manually). Therefore, calling done second time causes the callback to be called twice resulting in an error. Possible fix is checking whether the callback was called. I haven't find out yet why ending sftp session via end() method causes the error

Rommero commented 8 years ago

I found out that ss2-streams library of ssh2 submodules caused this issue. Starting from v0.0.15 ssh2-streams has a bug, which causes such failures in other libs apart from gulp-ssh. So, the solution is to lock on ssh2-streams@0.0.14

zensh commented 8 years ago

@Rommero ssh2-streams released 0.0.17

Rommero commented 8 years ago

@zensh 0.0.17 still contains the bug

Rommero commented 8 years ago

@TXChetG "So, the solution is to lock on ssh2-streams@0.0.14" . I haven't tested gulp-ssh with the latest ssh2-streams@0.0.18 and ssh2-streams@0.0.19

TXChetG commented 8 years ago

Thanks @Rommero I didn't quite know what the meant until I read in another issue. I locked on to 0.0.15 and that alleviated my error. Maybe I'll give the two newer ones you suggested a try too.

Thanks again.