Skip to content

Falsy Values Incorrectly Evaluated in Option Checks #354

Open

Description

@momoAmch

Hello,

I've encountered an issue in multiple functions where certain properties in the options object are checked using a simple if (options.someProp). This causes problems when someProp is a number and equal to 0, as it is treated as false, even though 0 is a valid value.

For example, with Axis cameras, the token of a video encoder can be 0. When calling setVideoEncoderConfiguration, I receive an incorrect response stating that the token is mandatory, even though I provided it. The same issue occurs with other parameters, such as bitrate.

Would it be possible to update these checks to properly handle 0 as a valid value? I’m happy to create a PR, but the fix will require multiple changes across media.js. If you're open to it, I can proceed with the PR.

Thank you!

Activity

  1. agsh commented on Apr 9, 2025

    @agsh
    Owner

    @momoAmch hello!
    Oh... media.js.. my headache... Written a long-long time ago in ECMAScript5 and not enough tested yet. Right now I'm rewriting it in the typescript and still make this stupid mistake (

    }${configuration.useCount ? `<UseCount xmlns="http://www.onvif.org/ver10/schema">${configuration.useCount}</UseCount>` : ''
    ) 🤦
    I can offer you a temporary solution to replace a number as value to string when passing options to methods:

    function zeroes(object) {
      if (object === 0) return '0';
      if (object === null) return null;
      if (Array.isArray(object)) return object.map(zeroes);
      if (typeof object === 'object') {
        return Object.fromEntries(Object
          .entries(object)
          .map(([key, value]) => [key, zeroes(value)]));
      } 
      return object;
    }
    
    console.log(
      zeroes({
        a : null,
        b : undefined,
        c : 0,
        d : [0,1,{e : 0,f : 1}],
        g : {h : 0, i : 1, j : {k : 0}},
      }),
    );

    But this is of course not a solution. So I'm glad you want to do a PR. I'll be happy to review it 👍
    Seem that this regex covers quite a lot of this falsy cases (45 in media.js): options.[\S\)]+[\n\s]*\?

  2. RogerHardiman commented on Nov 10, 2025

    @RogerHardiman
    Collaborator

    I'd be happy to look at a PR for the 'falsy' stuff.
    I've also got 4 Axis cameras in my collection of IP cameras (plus Bosch, Avigion, Hanwha, Sony) etc so can do some tests too.

    I'm just catching up on a year of agsh/onvif posts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions