
Show HN: A library to use JavaScript as the query language for your API - yosbelms
https://dev.to/yosbelms/the-library-that-allows-to-use-javascript-as-the-query-language-for-your-api-j9m
======
lioeters
I like it - sounds crazy but (could be :) brilliant.

I suppose there's nothing stopping me from sending an infinite loop "query"?

    
    
      func(async () => while(true) {})

~~~
yosbelms
Yes theres is, its name is watchdog, see [https://github.com/yosbelms/remote-
func/blob/master/server/w...](https://github.com/yosbelms/remote-
func/blob/master/server/watchdog.ts)

~~~
lioeters
Ah, I see it - watching on tick/setImmediate for functions that take too long.
Nice!

It's an intriguing concept, I'll be sure to study what you've created and
explore its possibilities. There are many aspects I like about it, like being
able to use modern JavaScript to fetch/query/filter from the client.

~~~
yosbelms
Watchdog algorithm explained:

The Runner receivers JS code, I wrote a babel plugin [1] to insert watchdog
checks inside each loop (for, while, dowhile), also in each arrow function,
that checks each time a function executes.

If the check scope is async, the watchdog awaits for a Promise that resolves
immediately to give the opportunity to other functions in the thread, this
function collaborative, does not hijack nodejs thread, so, it is allowed to
run 10 minutes default.

If the scope is not async, the nodejs thread can be hijacked, so, it is
allowed to run 100 milliseconds.

The watchdog stops the function by throwing a TimeoutError if the time is
consumed in each case.

Finally, a check in inserted on each `catch`, and `finally` statements for the
following cases:

    
    
        try {
          // infinite loop
        } catch(e) {
          // infinite loop
        } finally {
          // infinite loop
        }
    

[1] [https://github.com/yosbelms/remote-
func/blob/master/server/f...](https://github.com/yosbelms/remote-
func/blob/master/server/function-transform.ts)

~~~
lioeters
Wow! Thank you so much for the explanation. This is indeed treating it as a
query _language_.

I had originally thought this library is basically eval + Proxy, an approach I
had explored before a bit, for running user-written scripts. Funny enough,
I've also gone down the road of developing a mini-language, which has a check
on each parsed instruction, to have detailed control of its execution.

I didn't realize the "remote function" gets parsed and transpiled on the
server.

Using a custom Babel plugin to transform the source, placing a check on each
step, that is genius.

Oh, and I see there's an in-memory cache, using the source itself as a Map
hash. Practical.

Seeing the depth of thought that's gone into it, I really think you are onto
something with this. It deserves more attention than it's gotten so far. I'm
sure many people love this kind of meta-level language wizardry.

