@freestyle-sh/with-bun 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -21,6 +21,9 @@ declare class VmBunInstance extends VmWithInstance implements VmJavaScriptRuntim
21
21
  constructor(builder: VmBun);
22
22
  runCode<Result extends JSONValue = any>(args: string | {
23
23
  code: string;
24
+ argv?: string[];
25
+ env?: Record<string, string>;
26
+ workdir?: string;
24
27
  }): Promise<RunCodeResponse<Result>>;
25
28
  install(options?: InstallOptions): Promise<InstallResult>;
26
29
  }
package/dist/index.js CHANGED
@@ -59,15 +59,25 @@ class VmBunInstance extends VmWithInstance {
59
59
  this.builder = builder;
60
60
  }
61
61
  async runCode(args) {
62
- const code = typeof args === "string" ? args : args.code;
62
+ const options = typeof args === "string" ? { code: args } : args;
63
+ const { code, argv, env, workdir } = options;
64
+ const shellEscape = (value) => `'${value.replace(/'/g, "'\\''")}'`;
65
+ const argvArgs = argv?.map(shellEscape).join(" ");
66
+ const envPrefix = env ? `${Object.entries(env).map(([key, value]) => `${key}=${shellEscape(value)}`).join(" ")} ` : "";
67
+ const cdPrefix = workdir ? `cd ${shellEscape(workdir)} && ` : "";
68
+ const command = `${cdPrefix}${envPrefix}/opt/bun/bin/bun -e "${code.replace(/"/g, '\\"')}"${argvArgs ? ` -- ${argvArgs}` : ""}`;
63
69
  const result = await this.vm.exec({
64
- command: `/opt/bun/bin/bun -e "${code.replace(/"/g, '\\"')}"`
70
+ command
65
71
  });
66
72
  let parsedResult = void 0;
67
73
  if (result.stdout) {
68
- try {
69
- parsedResult = JSON.parse(result.stdout);
70
- } catch (e) {
74
+ const lines = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
75
+ const lastLine = lines[lines.length - 1];
76
+ if (lastLine) {
77
+ try {
78
+ parsedResult = JSON.parse(lastLine);
79
+ } catch (e) {
80
+ }
71
81
  }
72
82
  }
73
83
  return {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-bun",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "private": false,
5
5
  "dependencies": {
6
6
  "freestyle-sandboxes": "^0.1.8",
7
- "@freestyle-sh/with-type-js": "^0.2.1"
7
+ "@freestyle-sh/with-type-js": "^0.2.2"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "./dist/index.js",