question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JavaScript proxy in additional context

See original GitHub issue

Is it possible for additionalJsContext to capture a generic getter or setter from a proxy?

I’m providing:

const additionalJsContext = new Proxy({}, {
  set(obj, prop, value) {
    debug('set', {prop, value})
    obj[prop] = value
    return true
  },

  get(obj, prop) {
    const value = obj[prop]
    debug('get', {prop, value})
    return value === undefined ? null : value
  }
})

Then merge is used in this example [ref]:

  const sandbox: { [ind: string]: any } = merge(
    ctx.jsSandbox || {},
    {
      __code__: code,
      __result__: undefined,
    },
    data,
    ctx.options.additionalJsContext
  );

The generic “get” and “set” are not being merged into the sandbox object. If I put static properties in the 1st argument to Proxy it works or if I test my proxy alone additionalJsContext.anyprop === null it works. I’m not concerned about Proxy immutability, I can handle that with the set.

I’m have asked on the timm project too: https://github.com/guigrpa/timm/issues/52

This may be better handled here. Looks like using data instead will not help either. I’m also after the async support in additionalJsContext. Also, if you have a suggestion I don’t mind forking making changes and testing. I anticipate that I’m going to need to do this anyways.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mathe42commented, Feb 20, 2022

Just looked at the timm code and a work around should be:

const additionalJsContext = {
  key: new Proxy({}, {
    set(obj, prop, value) {
      debug('set', {prop, value})
      obj[prop] = value
      return true
    },
  
    get(obj, prop) {
      const value = obj[prop]
      debug('get', {prop, value})
      return value === undefined ? null : value
    }
  })
}

Where the key is not present in data. (and is not code nor result)

[edit] Sorry I just got what you want…

0reactions
jcalfeecommented, Feb 21, 2022

I tested v.__isProxy !== undefined ref and it works when I replace the two v instanceof Proxy checks . It errors err: Error: TypeError: eval is not a function. Probably this line: https://github.com/guigrpa/docx-templates/blob/28496607960efab8619ca006256d1d50fe5c0c7e/src/jsSandbox.ts#L49

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Javascript Proxy for isolated context intercommunication
Isolated Javascript contexts are independent Javascript execution context that lives aside each other. Most of the time they are sandboxed, ...
Read more >
How to pass a Proxy object as context to a closure in javascript
The issue is that some javascript methods in the application invoke closures and pass "this" parameters. When "this" parameters are wrapped in a ......
Read more >
Proxy - JavaScript - MDN Web Docs
The Proxy object enables you to create a proxy for another object, which can intercept and redefine fundamental operations for that object.
Read more >
Proxy and Reflect - The Modern JavaScript Tutorial
A Proxy object wraps another object and intercepts operations, like reading/writing properties and others, optionally handling them on its own, ...
Read more >
JavaScript Proxy Explained Clearly By Practical Examples
A JavaScript Proxy is an object that wraps another object (target) and intercepts the fundamental operations of the target object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found