Moleculer v0.14.14 Release Notes

Release Date: 2021-06-27 // almost 3 years ago
  • 105 commits from 11 contributors.

    ๐Ÿ†• New CBOR serializer #905

    CBOR (cbor-x) is a new serializer but faster than any other serializers.

    Example

    // moleculer.config.js
    module.exports = {
        logger: true,
        serializer: "CBOR"
    };
    

    Benchmark

    Suite: Serialize packet with 10bytes
    โˆš JSON               509,217 rps
    โˆš Avro               308,711 rps
    โˆš MsgPack             79,932 rps
    โˆš ProtoBuf           435,183 rps
    โˆš Thrift              93,324 rps
    โˆš Notepack           530,121 rps
    โˆš CBOR             1,016,135 rps
    
       JSON (#)            0%        (509,217 rps)   (avg: 1ฮผs)
       Avro           -39.38%        (308,711 rps)   (avg: 3ฮผs)
       MsgPack         -84.3%         (79,932 rps)   (avg: 12ฮผs)
       ProtoBuf       -14.54%        (435,183 rps)   (avg: 2ฮผs)
       Thrift         -81.67%         (93,324 rps)   (avg: 10ฮผs)
       Notepack        +4.11%        (530,121 rps)   (avg: 1ฮผs)
       CBOR           +99.55%      (1,016,135 rps)   (avg: 984ns)
    

    settled option in broker.mcall

    The broker.mcall method has a new settled option to receive all Promise results. Without this option, if you make a multi-call and one call is rejected, the response will be a rejected Promise and you don't know how many (and which) calls were rejected.

    If settled: true, the method returns a resolved Promise in any case and the response contains the statuses and responses of all calls.

    Example

    const res = await broker.mcall([
        { action: "posts.find", params: { limit: 2, offset: 0 },
        { action: "users.find", params: { limit: 2, sort: "username" } },
        { action: "service.notfound", params: { notfound: 1 } }
    ], { settled: true });
    console.log(res);
    

    The res will be something similar to

    [
        { status: "fulfilled", value: [/*... response of `posts.find`...*/] },
        { status: "fulfilled", value: [/*... response of `users.find`...*/] },
        { status: "rejected", reason: {/*... Rejected response/Error`...*/} }
    ]
    

    ๐Ÿ†• New MOLECULER_CONFIG environment variable in Runner

    ๐Ÿ”ง In the Moleculer Runner, you can configure the configuration filename and path with the MOLECULER_CONFIG environment variable. It means, no need to specify the config file with --config argument.

    ๐Ÿ‘Œ Supporting [email protected] in NATS transporter

    ๐Ÿš€ The new nats 2.x.x version has a new breaking API which has locked the NATS transporter for [email protected] library. As of this release, the NATS transporter supports both major versions of the nats library.

    The transporter automatically detects the version of the library and uses the correct API.

    ๐Ÿ“‡ Async custom validator functions and ctx as metadata

    ๐Ÿ“‡ Since [email protected], the FastestValidator supports async custom validators and you can pass metadata for custom validator functions. ๐Ÿ“‡ In Moleculer, the FastestValidator passes the ctx as metadata. It means you can access to the current context, service, broker and you can make async calls (e.g calling another service) in custom checker functions.

    Example

    // posts.service.js
    module.exports = {
        name: "posts",
        actions: {
            params: {
                $$async: true,
                owner: { type: "string", custom: async (value, errors, schema, name, parent, context) => {
                    const ctx = context.meta;
    
                    const res = await ctx.call("users.isValid", { id: value });
                    if (res !== true)
                        errors.push({ type: "invalidOwner", field: "owner", actual: value });
                    return value;
                } }, 
            },
            /* ... */
        }
    }
    

    ๐Ÿ”„ Changes

    • ๐Ÿ›  fix node crash in encryption mode with TCP transporter. #849
    • ๐Ÿ”ฆ expose Utils in typescript definition. #909
    • other d.ts improvements. #920, #922, #934, #950
    • ๐Ÿ›  fix etcd3 discoverer lease-loss issue #922
    • catch errors in Compression and Encryption middlewares. #850
    • using optional peer dependencies. #911
    • โž• add relevant packet to to serialization and deserialization calls. #932
    • ๐Ÿ›  fix disabled balancer issue with external discoverer. #933