@freestyle-sh/with-bun 0.1.0

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/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # @freestyle-sh/with-bun
2
+
3
+ Bun runtime for [Freestyle](https://freestyle.sh) VMs.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @freestyle-sh/with-bun freestyle-sandboxes
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { freestyle } from "freestyle-sandboxes";
15
+ import { VmBun } from "@freestyle-sh/with-bun";
16
+
17
+ const { vm } = await freestyle.vms.create({
18
+ with: {
19
+ js: new VmBun(),
20
+ },
21
+ });
22
+
23
+ const res = await vm.js.runCode(
24
+ "console.log(JSON.stringify({ hello: 'world' }));"
25
+ );
26
+
27
+ console.log(res);
28
+ // { result: { hello: 'world' }, stdout: '{"hello":"world"}\n', statusCode: 0 }
29
+ ```
30
+
31
+ ## Options
32
+
33
+ ```typescript
34
+ new VmBun({
35
+ version: "1.1.0", // Optional: specific Bun version (default: latest)
36
+ })
37
+ ```
38
+
39
+ | Option | Type | Default | Description |
40
+ |--------|------|---------|-------------|
41
+ | `version` | `string` | `undefined` | Bun version to install. If not specified, installs the latest version. |
42
+
43
+ ## API
44
+
45
+ ### `vm.js.runCode(code: string)`
46
+
47
+ Executes JavaScript/TypeScript code in the Bun runtime.
48
+
49
+ **Returns:** `Promise<RunCodeResponse>`
50
+
51
+ ```typescript
52
+ type RunCodeResponse<Result> = {
53
+ result: Result; // Parsed JSON from stdout (if valid JSON)
54
+ stdout?: string; // Raw stdout output
55
+ stderr?: string; // Raw stderr output
56
+ statusCode?: number; // Exit code
57
+ };
58
+ ```
59
+
60
+ ## Documentation
61
+
62
+ - [Freestyle Documentation](https://docs.freestyle.sh)
63
+ - [Bun Documentation](https://bun.sh/docs)
64
+
@@ -0,0 +1,25 @@
1
+ import { VmWith, VmWithInstance, CreateVmOptions } from 'freestyle-sandboxes';
2
+ import { VmJavaScriptRuntimeInstance, JSONValue, RunCodeResponse, VmJavaScriptRuntime } from '@freestyle-sh/with-type-js';
3
+
4
+ type BunJsOptions = {
5
+ version?: string;
6
+ workdir?: string;
7
+ };
8
+ type BunJsResolvedOptions = {
9
+ version?: string;
10
+ workdir?: string;
11
+ };
12
+ declare class VmBun extends VmWith<VmBunInstance> implements VmJavaScriptRuntime<VmJavaScriptRuntimeInstance> {
13
+ options: BunJsResolvedOptions;
14
+ constructor(options?: BunJsOptions);
15
+ configure(existingConfig: CreateVmOptions): CreateVmOptions | Promise<CreateVmOptions>;
16
+ createInstance(): VmBunInstance;
17
+ installServiceName(): string;
18
+ }
19
+ declare class VmBunInstance extends VmWithInstance implements VmJavaScriptRuntimeInstance {
20
+ builder: VmBun;
21
+ constructor(builder: VmBun);
22
+ runCode<Result extends JSONValue = any>(code: string): Promise<RunCodeResponse<Result>>;
23
+ }
24
+
25
+ export { VmBun };
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
1
+ import { VmWith, VmTemplate, VmWithInstance } from 'freestyle-sandboxes';
2
+
3
+ class VmBun extends VmWith {
4
+ options;
5
+ constructor(options) {
6
+ super();
7
+ this.options = {
8
+ version: options?.version,
9
+ workdir: options?.workdir
10
+ };
11
+ }
12
+ configure(existingConfig) {
13
+ const versionArg = this.options.version ? ` -s "bun-v${this.options.version}"` : "";
14
+ const installScript = `#!/bin/bash
15
+ set -e
16
+ export BUN_INSTALL="/opt/bun"
17
+ curl -fsSL https://bun.sh/install | bash${versionArg}
18
+ $BUN_INSTALL/bin/bun --version
19
+ `;
20
+ const bunInit = `export BUN_INSTALL="/opt/bun"
21
+ export PATH="$BUN_INSTALL/bin:$PATH"
22
+ `;
23
+ const bunConfig = {
24
+ template: new VmTemplate({
25
+ additionalFiles: {
26
+ "/opt/install-bun.sh": {
27
+ content: installScript
28
+ },
29
+ "/etc/profile.d/bun.sh": {
30
+ content: bunInit
31
+ }
32
+ },
33
+ systemd: {
34
+ services: [
35
+ {
36
+ name: "install-bun",
37
+ mode: "oneshot",
38
+ deleteAfterSuccess: true,
39
+ exec: ["bash /opt/install-bun.sh"],
40
+ timeoutSec: 300
41
+ }
42
+ ]
43
+ }
44
+ })
45
+ };
46
+ return this.compose(existingConfig, bunConfig);
47
+ }
48
+ createInstance() {
49
+ return new VmBunInstance(this);
50
+ }
51
+ installServiceName() {
52
+ return "install-bun.service";
53
+ }
54
+ }
55
+ class VmBunInstance extends VmWithInstance {
56
+ builder;
57
+ constructor(builder) {
58
+ super();
59
+ this.builder = builder;
60
+ }
61
+ async runCode(code) {
62
+ const result = await this.vm.exec({
63
+ command: `/opt/bun/bin/bun -e "${code.replace(/"/g, '\\"')}"`
64
+ });
65
+ let parsedResult = void 0;
66
+ if (result.stdout) {
67
+ try {
68
+ parsedResult = JSON.parse(result.stdout);
69
+ } catch (e) {
70
+ }
71
+ }
72
+ return {
73
+ result: parsedResult,
74
+ stdout: result.stdout ?? void 0,
75
+ stderr: result.stderr ?? void 0,
76
+ statusCode: result.statusCode ?? -1
77
+ };
78
+ }
79
+ }
80
+
81
+ export { VmBun };
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@freestyle-sh/with-bun",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "dependencies": {
6
+ "freestyle-sandboxes": "^0.1.2",
7
+ "@freestyle-sh/with-type-js": "^0.1.0"
8
+ },
9
+ "type": "module",
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ }
17
+ },
18
+ "source": "./src/index.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "pkgroll"
24
+ }
25
+ }