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.

Let developers to call JS methods on limited JS objects

See original GitHub issue

Is your feature request related to a problem? If yes, please describe the problem.

It would be great if we could pass over some libraries - take the lodash to AskQL and let the developers use all the functions provided, without the need to pack every call with a painstakingly created resource (current case). It could be pretty much useful for using the external services APIs like commercetools sdk or ORMs generated from tools like Prisma

To keep it safe we can let call just the top-level functions, or add whitelisting + only on the explicitly passed resources like:

const vmContext = { 
customResources:  [ 
 "lodash": jsModuleInterop(_, ["sort", "unique"])
]

where the jsModuleInterop is a wrapper over the JS object accepting the object (in this case _ - lodash module, plus a whitelist of methods)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
mhagmajercommented, May 18, 2021

@pkarw seems like this can be done in user space with a resource factory that accepts an object, for example:

  function myResourceFactory(module) {
    const res = {}
    Object.keys(module).forEach((k) => {
      res[k] = resource({
        type: any,
        async resolver(...args: any) {
          return module[k](...args);
        },
      });
    });
    return res;
  }

  const result = await askql.runUntyped(
    {
      resources: {
        ...myResourceFactory(lodash),
        ...askql.resources,
      }
    },
    askql.parse("ask { 'hello world!' }")
  );

The design decision with AskQL was to have a separate computing environment from JavaScript (or any other host programming language) and therefore it would be troublesome to do a direct linking. All callable host methods would need to go through an explicit wrapper which allows for security measures. With the method above it perhaps becomes easier to add multiple resources at the same time.

Although, for full support of what you’re asking for with namespaces would need to solve https://github.com/CatchTheTornado/askql/issues/579 first.

0reactions
pkarwcommented, May 18, 2021

Perfect fit @mhagmajer. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Functions - JavaScript - MDN Web Docs
In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object.
Read more >
The 10 Most Common JavaScript Issues Developers Face
JavaScript Issue #8: Creating Incorrect References to Instance Methods. Let's define a simple object, and create an instance of it, as follows:
Read more >
The Secret Life of Objects :: Eloquent JavaScript
Usually a method needs to do something with the object it was called on. When a function is called as a method—looked up...
Read more >
Method vs Functions, and other questions - javascript
So it could happen that a function A, being an object, has properties and methods, one of which happens to be another function...
Read more >
JavaScript best practices - W3C Wiki
Call things by their name — easy, short and readable variable and function names ... Furthermore, not every JavaScript developer is proficient or...
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