ssh2-streams icon indicating copy to clipboard operation
ssh2-streams copied to clipboard

Corrupt json file

Open janajri opened this issue 7 years ago • 12 comments

Attempting to download a JSON file from a Cerberus FTP server (version 5). Upon inspection, it appears that a quote is being dropped before the second key. I was able to pull the file with no issues on my linux install using sftp command and validate it as valid JSON.

While I understand this might be related to the server's protocol it is in fact outside of my control. Any help would be greatly appreciated

"stat":0.0905846153846,top_ftrxavg_wkc13":0.37284615384'

Attempting to JSON.parse the raw downloaded string results in the following error:

SyntaxError: Unexpected token t in JSON at position 65535

janajri avatar Sep 05 '17 21:09 janajri

How are you downloading the file?

mscdex avatar Sep 05 '17 21:09 mscdex

  host: CLIENT_SERVER,
  user: CLIENT_USERNAME,
  username: CLIENT_USERNAME,
  password: CLIENT_PASSWORD,
  algorithms: {
    compress: ["none"],
     cipher: [
       'aes128-ctr',
       'aes192-ctr',
       'aes256-ctr',
       'aes128-gcm',
       '[email protected]',
       'aes256-gcm',
       '[email protected]',
       'aes256-cbc'
     ]
   }
}

const file = await sftp.get(rootPath + '/' + next.name, null, 'ascii')
file.pipe(fs.createWriteStream('./test.json'))```


Sorry if I didn't clarify, but I'm using one of your other libraries where this is a dependency.

janajri avatar Sep 05 '17 21:09 janajri

Have you tried using ssh2 directly? It seems you are using a third party module that perhaps builds on top of ssh2, since ssh2 does not export any Promises.

mscdex avatar Sep 05 '17 21:09 mscdex

My bad, yes -- I am using this: https://github.com/jyu213/ssh2-sftp-client/blob/master/src/index.js

But the issue doesn't seem to exist here as I was able to validate the appropriate inputs.

I will retry with ssh2 directly and report back -- it's been a week since I last attempted.

janajri avatar Sep 05 '17 21:09 janajri


var conn = new Client();
conn.on('ready', function() {
  console.log('Client :: ready');
  conn.sftp(function(err, sftp) {
    if (err) throw err;
    sftp.readdir('/backups', function(err, list) {
      if (err) throw err;
      const [next] = list.filter(s => s.filename.endsWith('json'))
      const s = sftp.createReadStream('backups/' + next.filename, { encoding: 'ascii' })
      s.pipe(fs.createWriteStream('./test.json'))
      // conn.end();
    });
  });
}).connect(config);

f = require('./test.json') SyntaxError: /test.json: Unexpected token t in JSON at position 65535

I can confirm this is still an issue.

janajri avatar Sep 05 '17 21:09 janajri

Out of curiosity, does it work if you use sftp.fastGet() instead?

mscdex avatar Sep 05 '17 21:09 mscdex

Woah -- that did work! Any thoughts on where the issue might lie while streaming the file?

janajri avatar Sep 05 '17 22:09 janajri

Not offhand, but that might help narrow down the issue if sftp.fastGet() works every time. Would it be possible to obtain a JSON (or any other kind) file that you're having problems with, so that I can see if I can duplicate it on my end? These kinds of issues are typically pretty difficult to duplicate...

mscdex avatar Sep 05 '17 22:09 mscdex

I have the problem as reported above but it's not just JSON files. I'm using the vscode-sftp extension in Visual Studio Code and during download/diff of large UTF8 text files (200kb+) every 64101st character is removed. I have not experienced this corruption with files smaller than 64100 characters (UTF8 encoding).

I'm using a Mac/OSX 10.13.6 with node v8.11.3, npm 5.10.0, ssh2 ^0.6.1 (0.6.1 currently)

Please let me know if you need anymore information.

vitamindck avatar Jul 19 '18 17:07 vitamindck

@vitamindck Try replacing createReadStream() usage in vscode-sftp with fastGet() and see if the problem persists.

mscdex avatar Jul 19 '18 19:07 mscdex

https://github.com/mscdex/ssh2-streams/issues/43 this is the same issue I'm having, and coincidentally(? or not..) I'm also using Cerberus FTP

vitamindck avatar Feb 15 '19 00:02 vitamindck

I can confirm the issue still persists. With fastGet it works for me everytime

jnsbal avatar Jul 30 '19 16:07 jnsbal