GithubHelp home page GithubHelp logo

Comments (8)

koolfunky avatar koolfunky commented on August 10, 2024

Ok I put this in my TODO

from arsenal.

koolfunky avatar koolfunky commented on August 10, 2024

Something like that ?

    _parse(data, callback) {
        const version = data.readInt8(0);
        const pbMsgLen = data.readInt32BE(1);
        const chunkLen = data.readInt32BE(5);

        if (version !== getVersion()) {
            return callback(new Error('Version Failure'));
        }

        try {
            this._cmd = protoBuild.Message.decode(data.slice(9, 9 + pbMsgLen));
            this.setProtobuf(protoBuild.Command.decode(this._cmd.commandBytes));
        } catch (e) {
            if (e.decoded) {
                this.setProtobuf(e.decoded);
            } else {
                return callback(new Error('Error in decode' + e));
            }
        }
        this.setChunk(data.slice(pbMsgLen + 9, chunkLen + pbMsgLen + 9));

        if (this.getChunkSize() !== chunkLen) {
            return callback(new Error('data error - different len'));
        }
        if (this._cmd.authType === 1 &&
                !this.hmacIntegrity(this.getSlice(this._cmd.hmacAuth.hmac)))
            return callback(new Error('Hmac fail'));
        return callback(null);
    }

with a constructor like this :

    constructor(input, callback) {
        /*
         * From Kinetic official documentation, a Kinetic Protocol Data Unit
         * (PDU) is composed of:
         * - message -- a Protocol Buffer (protobuf) message, containing
         *              operation metadata & key-value metadata,
         * - chunk   -- the value to store/read.
         */
        this._message = undefined;
        this._chunk = undefined;

        if (input !== undefined) {
            if (!Buffer.isBuffer(input))
                return callback(new Error("input is not a buffer"));
            this._parse(input, (err) => {
                if (err){
                    return callback(new Error("could not parse input buffer (" + err + ")"));
                } else {
                    return callback(null, this);
                }
            });
        }
    }

from arsenal.

adrienverge avatar adrienverge commented on August 10, 2024

Since both _parse and constructor are synchronous, I wouldn't use callbacks here.

Instead, they could throw exceptions, explicitely say it their jsdoc, and all code calling them should check for exceptions.

Example:

_parse(data) {
    if (version !== getVersion()) {
        const err = new Error('Version Failure');
        err.badVersion = true;
        throw err;
    }
}

Client code:

try {
    const pdu = new Kinetic.PDU(new Buffer("sdkfljsdf"));
} catch (e) {
    if (e.badVersion) {
        log.error("bad version!");
    }
}

from arsenal.

koolfunky avatar koolfunky commented on August 10, 2024

Okok

And for send ? I tried something like that but i'm not really good on error handling, so I don't really know what is the best way

        try {
            if (this.getChunk() !== undefined)
                sock.write(Buffer.concat(
                    [pduHeader, this._message.toBuffer(), this.getChunk()]));
            else
                sock.write(Buffer.concat([pduHeader,
                    this._message.toBuffer()]));
        } catch (e) {
            return callback(e);
        }
        return callback(null);

I did this before, I just forgot to update my comment X)

from arsenal.

koolfunky avatar koolfunky commented on August 10, 2024

I tried to do a better error throwing/handling on the branch dev/CLEANUP/error_return
There are some tests on the branch

from arsenal.

adrienverge avatar adrienverge commented on August 10, 2024

Thanks @AntoninCoulibaly. The branch you created brings lots of enhancements, can you create a pull request for it? I have a few comments to make, but mostly 👍

from arsenal.

koolfunky avatar koolfunky commented on August 10, 2024

Ok no problem I will

from arsenal.

 avatar commented on August 10, 2024

Closing, since this is not relevant in this repo anymore.

from arsenal.

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.