JCMais / node-libcurl

libcurl bindings for Node.js
https://npmjs.org/package/node-libcurl
MIT License
660 stars 117 forks source link

Promisify the writefunction callback (Curly) #372

Closed Ookamini95 closed 1 year ago

Ookamini95 commented 1 year ago

Is it possible to use async on the writefunction, so as to use fs.promises to write onto a file? Something like

const fsp = require('fs').promises;

const writeResponse = (buff, nmemb, size) => {
    let written;
    if (fileOut) {
        try {
            console.log('inside good 1')
            written = fsp.writeFile(fileOut, buff); // I'd like to use await here
        } catch (error) {
            console.log(error)
        }
    } else {
        /* listing output */
        process.stdout.write(buff.toString())
    }
    written = size * nmemb
    return written;
}

async function runCurly() {
    const fileOutPath = path.join(process.cwd(), `temp.mp3`);
    fileOut = await fsp.open(`${fileOutPath}`, 'w+');
    try {
        const { statusCode, data: stream, headers } = await curly.get(`ftp://url/samp.mp3`, {
            username: `admin`,
            password: `admin`,
            port: `40404`,
            ftpSkipPasvIp: `1`,
            ftpResponseTimeout: `20`,
            writeFunction: writeResponse
        });
    } catch (error) {
        console.log(error, error.code)
    }

If I use await, error code 23 is shown (function expects an int, while a promise is returned). Also if I try to write onto the file directly in the runCurly function nothing is written (buffer is empty).

JCMais commented 1 year ago

@Ookamini95 async is not possible due to the way libcurl works, we must return something to libcurl as soon as they call us. You can use writeFileSync inside the writeResponse.

Keep in mind this may (and probably will) get called more than once, as the response is retrieved in chunks.

You are supposed to read nmemb * size bytes of the buffer, and write those. Example here: https://github.com/JCMais/node-libcurl/blob/1563bb7699eb9b22989058a0d018c59a460aaf37/examples/16-ftp-download.js#L24-L36

Ookamini95 commented 1 year ago

Thanks for the response. Too bad though, I'll have to find another way.

JCMais commented 1 year ago

If the file is not big, you can append the chunks to an array and then create a single buffer later to then write to a file.

Closing this as the original question has been answered.