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

Can't use readableStream's on "data" ? #44

Open
akermabon opened this issue May 6, 2021 · 1 comment
Open

Can't use readableStream's on "data" ? #44

akermabon opened this issue May 6, 2021 · 1 comment

Comments

@akermabon
Copy link

hello,

I'm trying to get a file content from ftp, without having to write it on disk.
I'm doing the following, but it seems the "data" event is never triggered...
I'm using latest version (v1.3.5).
Code to reproduce:

const Ftp = require('promise-ftp')
const client = new Ftp();
client.connect()
    .then(() => client.get('path-to-file'))
    .then((stream) => {
        return new Promise( (resolve, reject) => {
            let buffer = '';
            stream.once('close', () => resolve(buffer));
            stream.once('error', reject);
            stream.on('data', function (chunk) {
                console.log(chunk.toString());
                buffer += chunk;
            });
        });
    })
    .then(() => client.end());

Am I missing something ?

@t0bs000r
Copy link

t0bs000r commented Oct 1, 2021

I ran in the same problem. The 'data'-Event is never fired, because the stream is never written.
You need to add a Writeable.
Try this:

const Ftp = require('promise-ftp')
const client = new Ftp();
client.connect()
    .then(() => client.get('path-to-file'))
    .then((stream) => {
        return new Promise( (resolve, reject) => {

            stream.pipe(new Writable({
                write(chunk, encoding, callback: (error?) => void) {
                    callback();
                }
            }));

            let buffer = '';
            stream.once('close', () => resolve(buffer));
            stream.once('error', reject);
            stream.on('data', function (chunk) {
                console.log(chunk.toString());
                buffer += chunk;
            });
        });
    })
    .then(() => client.end());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants