Git Product home page Git Product logo

Comments (2)

gdaws avatar gdaws commented on July 30, 2024

I tested your code by transferring a PNG file and found that the consumer output corrupted data. The problem is the consumer is reading chunks and appending the data to a string and the string operation is performing some kind of character decoding.

The solution is to store the chunk data in a Buffer object.

let body = Buffer.from([]);

let read = function () {
    let chunk;
    while (null !== (chunk = message.read())) {
        body = Buffer.concat([body, chunk], body.length + chunk.length);
    }
};

Or you could avoid the hassle of writing your own read logic and pipe the message into file stream.

message.pipe(file);

from stompit.

chocronr avatar chocronr commented on July 30, 2024

Many thanks!
Indeed, consuming the file into a string corrupted the data.
My solution was to mark in the message header if this is a file, and in this case, consume it using file stream:

`subscribe(queueName, callback) {
stompit.connect(this.connectParams, function (error, client) {

        if (error) {
            console.log('Unable to connect: ' + error.message);
            callback(error, null);
        }

        let subscribeParams = {
            'destination': '/queue/' + queueName,
            'ack': 'client-individual'
        };

        client.subscribe(subscribeParams, function (error, message) {

            let correlationId = message['headers']['correlation-id'];
            let type = message['headers']['type'];
            let hash = message['headers']['hash'];
            let ws, body, filePath;

            if (type === "file") {
                let id = uuidv1();
                filePath = config.downloadPath + "/" + id;
                ws = fs.createWriteStream(filePath);
            }
            else {
                body = "";
            }

            let read = function () {
                let chunk;
                while (null !== (chunk = message.read())) {

                    if (type === "file") {
                        ws.write(chunk);
                    }
                    else {
                        body += chunk;
                    }
                }
            };

            message.on('readable', read);

            message.on('end', function () {
                let obj = {'message': body, 'id': correlationId, 'type': type};
                client.ack(message);
                if (type !== "file") {
                    callback(null, obj);
                }
                else {
                    ws.end();
                    ws.on('finish', function () {
                        let obj = {'message': filePath, 'id': correlationId, 'type': type, 'hash': hash};
                        callback(null, obj);
                    })
                }
            });

        });
    });
}`

from stompit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.