Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

javascript - Strange ghost properties being sent to Proxy get method

This is the code that's causing the error:

console.log(objectWithProxyProperty);

the error that I am dealing with is either,

TypeError: Cannot read property 'apply' of undefined

or

Error: The assertion library used does not have a 'inspect' property or method.

depending on which checks I use, which is demostrated in the code below. What's clear is that the 'inspect' property is being sent to the get method, but 'inspect' is not a Symbol AND 'inspect' cannot be read with prop in chaiAssert either.

objectWithProxyProperty looks like:

  const objectWithProxyProperty = {};

  const assrt = <Partial<AssertStatic>>  function () {
    try {
      return chaiAssert.apply(chaiAssert, arguments);
    }
    catch (e) {
      return handleError(e);
    }
  };

  // the assert property on objectWithProxyProperty is a Proxy 
  objectWithProxyProperty.assert = new Proxy(assrt, {
    get: function (target, prop) {

      if (typeof prop === 'symbol') {
        return Reflect.get(...arguments);
      }

      if (!(prop in chaiAssert)) {
        return handleError(
          // new Error(`The assertion library used does not have property or method.`)
          new Error(`The assertion library used does not have a '${prop}' property or method.`)
        );
      }

      return function () {
        try {
          return chaiAssert[prop].apply(chaiAssert, arguments);
        }
        catch (e) {
          return handleError(e);
        }
      }
    }
  });

What appears to be happening, is when I log this object with a proxy property called "assert", the proxy itself receives strange properties to the get method.

Specifically these properties are called 'inspect' and 'constructor', they do not seem to be Symbols.

So what I have to to "solve the problem" is add a check like so:

   const objectWithProxyProperty = {};

  const assrt = <Partial<AssertStatic>>  function () {
    try {
      return chaiAssert.apply(chaiAssert, arguments);
    }
    catch (e) {
      return handleError(e);
    }
  };

  let badProps =  {
     inspect: true,
     constructor: true
  };

  objectWithProxyProperty.assert = new Proxy(assrt, {
    get: function (target, prop) {

      if (typeof prop === 'symbol') {
        return Reflect.get(...arguments);
      }

      if (badProps[String(prop)]) {  // ! added this !
        return Reflect.get(...arguments);
      }

      if (!(prop in chaiAssert)) {
        return handleError(
          // new Error(`The assertion library used does not have property or method.`)
          new Error(`The assertion library used does not have a '${prop}' property or method.`)
        );
      }

      return function () {
        try {
          return chaiAssert[prop].apply(chaiAssert, arguments);
        }
        catch (e) {
          return handleError(e);
        }
      }
    }
  });

What are these properties that do not exist on chaiAssert yet are not Symbols?

So far these ghost properties have been 'inspect' and 'constructor', but the reason I ask the question, is because I need to find out what other ones there might be, so that I can handle them ahead of time!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...