Skip to content

Add Node.js Eval extension - #340

Merged
fe9lix merged 4 commits into
raycast:mainfrom
selfish:feat/js-eval-extension
Mar 7, 2022
Merged

fe9lix merged 4 commits into
raycast:mainfrom
selfish:feat/js-eval-extension

Conversation

@selfish

@selfish selfish commented Nov 12, 2021 •

Copy link
Copy Markdown
Contributor

Description

Adds Node.js Evaluate extension.
This extension will quickly evaluate Javascript code right inside Raycast, and allow copying of the code or evaluation.
This idea is based on a similar alfred extension

Type of change

  • New extension

Screencast

Settings screen:

image

Default evaluation mode:

image image

Fast mode:

Screen.Recording.2021-11-12.at.2.15.02.AM.mov

Checklist

@fe9lix fe9lix left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @selfish! Thanks for the contribution.
Evaluating arbitrary JS through the entire Node.js API (giving you access to any system-level operations) in typeahead-style is a bit scary. I'd suggest to at least make the evaluation explicit, i.e. after running the primary action in the action panel. That way, the user can also first finish typing the entire expression without intermediary errors and then run it through the shortcut.
(In the future it might be a good idea to show an alert on the first run but not yet possible through the API).

@fe9lix fe9lix self-assigned this Nov 16, 2021
@mattisssa mattisssa added the new extension Label for PRs with new extensions label Nov 16, 2021
@selfish

selfish commented Nov 17, 2021

Copy link
Copy Markdown
Contributor Author

Hi @fe9lix, thanks for your input!
I understand your concern, but I have to admit that for me, part of the charm is precisely this type of speedy behavior, where you see real-time evaluation, even while typing.
As mentioned in the description, this matches the feel of an Alfred workflow, which became a core part of my daily routine when I have to make those small "does this behave like I think it does?" or "what will happen if I use this/that trick??" while coding.

I feel like the security aspect won't be improved by delaying the evaluation (I agree that the fact that eval is used is scary) - would you mind reconsidering the request?

By the way, this was my first contribution to Raycast and was intended to be part of a series, which will similarly add a ruby interpreter, python, golang, and maybe more 🙏🏻

As for alerting the user, I would be more than happy to do that. Maybe even abuse the preferences API to force the user to read some text and enable the evaluation explicitly.

Come to think of it, would you accept it if I implemented your suggestion, but allowed an alternative typehead option in the settings?
I'm more than happy to make your suggestion the default behavior, and allow people like myself the option to see every error on every broken input while typing.

Again, thanks for the feedback!

@fe9lix

fe9lix commented Nov 17, 2021 •

Copy link
Copy Markdown
Contributor

Hey @selfish – I understand your point. I still believe that at least it prevents situations where somebody doesn't know exactly what's happening or even situations where a typo would lead to dangerous side effects. That being said I get that you like the realtime feedback, so your suggestion with making this a preference sounds good to me. (Please default to the explicit behavior of triggering evaluation by shortcut and make the preference non-required, so users who wish to enable the realtime feedback, should go to prefs and enable this...)

@fe9lix

fe9lix commented Nov 17, 2021

Copy link
Copy Markdown
Contributor

Btw, I can think of another "scary" scenario if the typeahead behavior was enabled by default: Raycast allows you to set up an alias for a command and then directly jump into the command by typing the alias, pressing the space key and continue typing (or pasting from the clipboard...). So definitely better to make this an explicit option.

@selfish

selfish commented Nov 18, 2021

Copy link
Copy Markdown
Contributor Author

Thanks, @fe9lix!
I agree with the feedback (and the concerns).
I'll implement the changes in the next couple of days.

@FezVrasta

FezVrasta commented Nov 18, 2021 •

Copy link
Copy Markdown
Contributor

Could the evaluation be limited to pure-js with no access to node apis? That should really help with security.

Maybe using smth like this https://github.com/NeilFraser/JS-Interpreter

@selfish

selfish commented Nov 20, 2021

Copy link
Copy Markdown
Contributor Author

@FezVrasta I reviewed the interpreter, and I'd rather not include the whole thing. It gets heavy.
Honestly, I expect anyone who's able to understand what a node interpreter is, to understand the risk as well, wdyt?

@selfish

selfish commented Nov 20, 2021

Copy link
Copy Markdown
Contributor Author

@fe9lix Updated with a new default mode as we spoke.
Note that the PR description was also updated with screenshots of the new mode, and the evaluation indeed does not occur until the action is triggered. :)

@selfish
selfish requested a review from fe9lix November 20, 2021 02:16

@fe9lix fe9lix left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the option. One thing that I notice is that now on every keypress (regardless of the mode), the Action Panel primary action flashes, which is visually quite distracting. I guess it's because of the new list item entry that changes with the entered text and the list re-rendering on each keypress? Not sure if the issue was also present in the previous version.

@selfish

selfish commented Nov 25, 2021

Copy link
Copy Markdown
Contributor Author

Extra references and conversation regarding solutions here:
https://raycastcommunity.slack.com/archives/C028DTXTW3Y/p1637768174267500

I'll take a couple of days to rewrite this in a more correct way.
Meanwhile, I'm leaving the PR open to retain history

@selfish

selfish commented Dec 15, 2021

Copy link
Copy Markdown
Contributor Author

Updating that this hasn't fallen stale. I've had to replace my mac and I'm pending a new one, so the delay is due to a lack of a development environment, but I intend to complete this.

@pernielsentikaer

Copy link
Copy Markdown
Contributor

Thanks for the update @selfish, sorry for the very late reply 😄

@fe9lix

fe9lix commented Jan 10, 2022

Copy link
Copy Markdown
Contributor

Should we still leave this open @selfish? (Alternative we can close it for now and you can reopen it later...)

@franzwilhelm

Copy link
Copy Markdown
Contributor

@selfish cool extension. Hope to see this reach the store when your new mac arrives :)

@selfish

selfish commented Feb 5, 2022

Copy link
Copy Markdown
Contributor Author

@fe9lix sorry to have missed your question.
I just got my new mac, setting up my Raycast and dev env again, and will get to the rewrite.
I don't mind closing, but I expect to have it completed soon. :)

@selfish

selfish commented Feb 16, 2022

Copy link
Copy Markdown
Contributor Author

Ok, I see that It's taking me longer than I expected to finish.
closing this PR for now and will reopen once ready.

@selfish selfish closed this Feb 16, 2022
@selfish

selfish commented Feb 24, 2022

Copy link
Copy Markdown
Contributor Author

Okay, finally did it.
I think it's ready.
Also, the code is much more concise I feel.
Let me know what do you think :)

@selfish selfish reopened this Feb 24, 2022
@raycastbot

Copy link
Copy Markdown
Collaborator

Congratulation on your new Raycast extension! 🚀

We will review it shortly. Once the PR is approved and merged, the extension will be available on the Store.

@selfish
selfish force-pushed the feat/js-eval-extension branch from edf01c2 to f827a6e Compare February 24, 2022 22:04
@selfish
selfish force-pushed the feat/js-eval-extension branch from 160f406 to 09a97e1 Compare February 24, 2022 22:22
@selfish
selfish requested a review from fe9lix February 24, 2022 22:23
@fe9lix

fe9lix commented Feb 25, 2022

Copy link
Copy Markdown
Contributor

@selfish Thanks for the updates! Just quickly tested and when I type console.log("hello"), I receive this error after pressing enter:

TypeError: Cannot read properties of undefined (reading '__proto__')

  return object.__proto__.constructor.name;
                ^

inferType:util.ts:2:17

---
1: export function inferType(object: { __proto__: { constructor: { name: string } } }) {
2:   return object.__proto__.constructor.name;
3: }
4: 
---

EvalResult:slow-eval.tsx:54:57
renderWithHooks:react-reconciler.development.js:5637:22
mountIndeterminateComponent:react-reconciler.development.js:7757:17
beginWork:react-reconciler.development.js:8544:18
Object.invokeGuardedCallbackProd:react-reconciler.development.js:9535:14
invokeGuardedCallback:react-reconciler.development.js:9622:35
beginWork$1:react-reconciler.development.js:12460:11
performUnitOfWork:react-reconciler.development.js:11686:16
workLoopSync:react-reconciler.development.js:11637:9

@selfish

selfish commented Mar 5, 2022 •

Copy link
Copy Markdown
Contributor Author

@fe9lix great catch, thanks!
After some interesting research, I think it's now bulletproof.

Unfortunately, both evals and type inference are terrible to deal with in JS.

@selfish
selfish force-pushed the feat/js-eval-extension branch 2 times, most recently from 1d8d7b9 to 2c16d5d Compare March 5, 2022 14:21
@fe9lix
fe9lix merged commit 55ec89b into raycast:main Mar 7, 2022
@raycastbot

Copy link
Copy Markdown
Collaborator

@selfish
selfish deleted the feat/js-eval-extension branch March 9, 2022 14:07
@selfish

selfish commented Mar 9, 2022

Copy link
Copy Markdown
Contributor Author

🎉
Thanks everyone for the support bringing this to the store :)

@FezVrasta

FezVrasta commented Mar 9, 2022 •

Copy link
Copy Markdown
Contributor

I was testing it with 2 / 0 and I would expect it to return Infinity but instead it returns null (of type number)

@selfish

selfish commented Mar 10, 2022 •

Copy link
Copy Markdown
Contributor Author

@FezVrasta fixed in #1062 - thanks for reporting!

raycastbot added a commit that referenced this pull request May 27, 2026