winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.98k stars 196 forks source link

Cannot use `ngrok` in extern #2084

Open Chriscbr opened 1 year ago

Chriscbr commented 1 year ago

I tried this

npm i ngrok

// repro.w
bring cloud;

resource Utils {
  init() {}
  extern "./repro.js" inflight start_ngrok(): str;
}

let utils = new Utils();

new cloud.Function(inflight () => {
  utils.start_ngrok();
}) as "test";
// repro.js
exports.start_ngrok = async function() {
    const ngrok = require('ngrok');
    const url = await ngrok.connect(4567);
    console.log(`ngrok url: ${url}`);
    return url;
}

I expected this:

A url to be printed

Instead, this happened

wing test src/repro.w
✔ Compiling to sim...
fail ┌ repro.wsim » root/env0/test
     │ TypeError [ERR_INVALID_ARG_TYPE]: The "buf" argument must be an instance of ArrayBuffer or ArrayBufferView. Received an instance of Object
     │     at new NodeError (node:internal/errors:399:5)
     │     at Object.randomFillSync (node:internal/crypto/random:121:11)
     │     at ReadOnlyHandler.apply (/Users/chrisr/.volta/tools/image/packages/winglang/lib/node_modules/winglang/node_modules/@winglang/sdk/node_modules/vm2/lib/bridge.js:485:11)
     │     at rng (/Users/chrisr/dev/wing-webhook/node_modules/uuid/dist/rng.js:18:21)
     │     at Object.v4 (/Users/chrisr/dev/wing-webhook/node_modules/uuid/dist/v4.js:17:63)
     │     at connectRetry (/Users/chrisr/dev/wing-webhook/node_modules/ngrok/index.js:27:40)
     │     at Object.connect (/Users/chrisr/dev/wing-webhook/node_modules/ngrok/index.js:23:10)
     │     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
     │     at async exports.start_ngrok (/Users/chrisr/dev/wing-webhook/src/repro.js:3:17)
     │     at async Handler.handle (/Users/chrisr/dev/wing-webhook/src/target/test/repro.wsim/.wing/test_c8c85f80.js:11:4)
     └     at async exports.handler (/Users/chrisr/dev/wing-webhook/src/target/test/repro.wsim/.wing/test_c8c85f80.js:2:10)

Is there a workaround?

Shell out to ngrok by calling process.execSync in the extern code instead

Component

Compiler

Wing Version

0.11.2

Wing Console Version

No response

Node.js Version

18.14.1

Platform(s)

MacOS

Anything else?

If I try calling the extern code outside of the Wing simulator, it works fine

Screenshot 2023-04-14 at 4 55 02 PM

Based on the error message it looks like some kind of issue with vm2

Community Notes

MarkMcCulloh commented 1 year ago

https://github.com/patriksimek/vm2/issues/484

Adding Uint8Array to the global sandbox might resolve this

staycoolcall911 commented 1 year ago

I've marked this as p2, because there's a workaround (thanks @Chriscbr 🙏). However @MarkMcCulloh's comment makes this a good first issue to quickly see if we can solve this here.

github-actions[bot] commented 1 year ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] commented 1 year ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

skyrpex commented 10 months ago

Related to https://github.com/winglang/wing/issues/4965.

I think we need some sort of LocalService or SimService that only runs in the sim, and allows executing functions without being bundled.

Maybe https://github.com/unjs/jiti could be useful to run arbitrary code.

github-actions[bot] commented 5 months ago

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] commented 2 months ago

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!