@computesdk/codesandbox 1.5.45 → 1.5.46

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.mts CHANGED
@@ -1,23 +1,16 @@
1
1
  import * as _computesdk_provider from '@computesdk/provider';
2
- import { Runtime } from '@computesdk/provider';
3
2
  import { Sandbox } from '@codesandbox/sdk';
4
3
 
5
- /**
6
- * Codesandbox-specific configuration options
7
- */
8
4
  interface CodesandboxConfig {
9
5
  /** CodeSandbox API key - if not provided, will fallback to CSB_API_KEY environment variable */
10
6
  apiKey?: string;
11
7
  /** Template to use for new sandboxes */
12
8
  templateId?: string;
13
- /** Default runtime environment */
14
- runtime?: Runtime;
9
+ /** Default runtime environment (e.g. 'node', 'python') */
10
+ runtime?: string;
15
11
  /** Execution timeout in milliseconds */
16
12
  timeout?: number;
17
13
  }
18
- /**
19
- * Create a Codesandbox provider instance using the factory pattern
20
- */
21
14
  declare const codesandbox: (config: CodesandboxConfig) => _computesdk_provider.Provider<Sandbox, any, any>;
22
15
 
23
16
  export { type CodesandboxConfig, codesandbox };
package/dist/index.d.ts CHANGED
@@ -1,23 +1,16 @@
1
1
  import * as _computesdk_provider from '@computesdk/provider';
2
- import { Runtime } from '@computesdk/provider';
3
2
  import { Sandbox } from '@codesandbox/sdk';
4
3
 
5
- /**
6
- * Codesandbox-specific configuration options
7
- */
8
4
  interface CodesandboxConfig {
9
5
  /** CodeSandbox API key - if not provided, will fallback to CSB_API_KEY environment variable */
10
6
  apiKey?: string;
11
7
  /** Template to use for new sandboxes */
12
8
  templateId?: string;
13
- /** Default runtime environment */
14
- runtime?: Runtime;
9
+ /** Default runtime environment (e.g. 'node', 'python') */
10
+ runtime?: string;
15
11
  /** Execution timeout in milliseconds */
16
12
  timeout?: number;
17
13
  }
18
- /**
19
- * Create a Codesandbox provider instance using the factory pattern
20
- */
21
14
  declare const codesandbox: (config: CodesandboxConfig) => _computesdk_provider.Provider<Sandbox, any, any>;
22
15
 
23
16
  export { type CodesandboxConfig, codesandbox };
package/dist/index.js CHANGED
@@ -29,13 +29,10 @@ var codesandbox = (0, import_provider.defineProvider)({
29
29
  name: "codesandbox",
30
30
  methods: {
31
31
  sandbox: {
32
- // Collection operations (compute.sandbox.*)
33
32
  create: async (config, options) => {
34
33
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
35
34
  if (!apiKey) {
36
- throw new Error(
37
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`
38
- );
35
+ throw new Error(`Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`);
39
36
  }
40
37
  const sdk = new import_sdk.CodeSandbox(apiKey);
41
38
  try {
@@ -46,7 +43,6 @@ var codesandbox = (0, import_provider.defineProvider)({
46
43
  sandboxId = options.snapshotId;
47
44
  } else {
48
45
  const {
49
- runtime: _runtime,
50
46
  timeout: _timeout,
51
47
  envs,
52
48
  name: _name,
@@ -58,72 +54,43 @@ var codesandbox = (0, import_provider.defineProvider)({
58
54
  directory: _directory,
59
55
  ...providerOptions
60
56
  } = options || {};
61
- const createOptions = {
62
- ...providerOptions
63
- // Spread provider-specific options
64
- };
57
+ const createOptions = { ...providerOptions };
65
58
  const templateId = optTemplateId || config.templateId;
66
- if (templateId) {
67
- createOptions.id = templateId;
68
- }
69
- if (envs && Object.keys(envs).length > 0) {
70
- createOptions.envVars = envs;
71
- }
59
+ if (templateId) createOptions.id = templateId;
60
+ if (envs && Object.keys(envs).length > 0) createOptions.envVars = envs;
72
61
  sandbox = await sdk.sandboxes.create(createOptions);
73
62
  sandboxId = sandbox.id;
74
63
  }
75
- return {
76
- sandbox,
77
- sandboxId
78
- };
64
+ return { sandbox, sandboxId };
79
65
  } catch (error) {
80
66
  if (error instanceof Error) {
81
67
  if (error.message.includes("unauthorized") || error.message.includes("API key")) {
82
- throw new Error(
83
- `CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`
84
- );
68
+ throw new Error(`CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable.`);
85
69
  }
86
70
  if (error.message.includes("quota") || error.message.includes("limit")) {
87
- throw new Error(
88
- `CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`
89
- );
71
+ throw new Error(`CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`);
90
72
  }
91
73
  }
92
- throw new Error(
93
- `Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`
94
- );
74
+ throw new Error(`Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`);
95
75
  }
96
76
  },
97
77
  getById: async (config, sandboxId) => {
98
78
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
99
- if (!apiKey) {
100
- throw new Error(
101
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
102
- );
103
- }
79
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
104
80
  const sdk = new import_sdk.CodeSandbox(apiKey);
105
81
  try {
106
82
  const sandbox = await sdk.sandboxes.resume(sandboxId);
107
- return {
108
- sandbox,
109
- sandboxId
110
- };
111
- } catch (error) {
83
+ return { sandbox, sandboxId };
84
+ } catch {
112
85
  return null;
113
86
  }
114
87
  },
115
88
  list: async (_config) => {
116
- throw new Error(
117
- `CodeSandbox provider does not support listing sandboxes. CodeSandbox SDK does not provide a native list API. Consider using the CodeSandbox dashboard or implement your own tracking system.`
118
- );
89
+ throw new Error(`CodeSandbox provider does not support listing sandboxes.`);
119
90
  },
120
91
  destroy: async (config, sandboxId) => {
121
92
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
122
- if (!apiKey) {
123
- throw new Error(
124
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
125
- );
126
- }
93
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
127
94
  const sdk = new import_sdk.CodeSandbox(apiKey);
128
95
  try {
129
96
  await sdk.sandboxes.shutdown(sandboxId);
@@ -138,7 +105,6 @@ var codesandbox = (0, import_provider.defineProvider)({
138
105
  }
139
106
  }
140
107
  },
141
- // Instance operations (sandbox.*)
142
108
  runCommand: async (sandbox, command, options) => {
143
109
  const startTime = Date.now();
144
110
  try {
@@ -148,53 +114,30 @@ var codesandbox = (0, import_provider.defineProvider)({
148
114
  const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}="${(0, import_provider.escapeShellArg)(v)}"`).join(" ");
149
115
  fullCommand = `${envPrefix} ${fullCommand}`;
150
116
  }
151
- if (options?.cwd) {
152
- fullCommand = `cd "${(0, import_provider.escapeShellArg)(options.cwd)}" && ${fullCommand}`;
153
- }
154
- if (options?.background) {
155
- fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;
156
- }
117
+ if (options?.cwd) fullCommand = `cd "${(0, import_provider.escapeShellArg)(options.cwd)}" && ${fullCommand}`;
118
+ if (options?.background) fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;
157
119
  const output = await client.commands.run(fullCommand);
158
- return {
159
- stdout: output,
160
- stderr: "",
161
- exitCode: 0,
162
- durationMs: Date.now() - startTime
163
- };
120
+ return { stdout: output, stderr: "", exitCode: 0, durationMs: Date.now() - startTime };
164
121
  } catch (error) {
165
- return {
166
- stdout: "",
167
- stderr: error instanceof Error ? error.message : String(error),
168
- exitCode: 127,
169
- durationMs: Date.now() - startTime
170
- };
122
+ return { stdout: "", stderr: error instanceof Error ? error.message : String(error), exitCode: 127, durationMs: Date.now() - startTime };
171
123
  }
172
124
  },
173
- getInfo: async (sandbox) => {
174
- return {
175
- id: sandbox.id,
176
- provider: "codesandbox",
177
- runtime: "node",
178
- // CodeSandbox default
179
- status: "running",
180
- createdAt: /* @__PURE__ */ new Date(),
181
- timeout: 3e5,
182
- metadata: {
183
- cluster: sandbox.cluster,
184
- bootupType: sandbox.bootupType,
185
- isUpToDate: sandbox.isUpToDate
186
- }
187
- };
188
- },
125
+ getInfo: async (sandbox) => ({
126
+ id: sandbox.id,
127
+ provider: "codesandbox",
128
+ status: "running",
129
+ createdAt: /* @__PURE__ */ new Date(),
130
+ timeout: 3e5,
131
+ metadata: { cluster: sandbox.cluster, bootupType: sandbox.bootupType, isUpToDate: sandbox.isUpToDate }
132
+ }),
189
133
  getUrl: async (sandbox, options) => {
190
134
  const protocol = options.protocol || "https";
191
135
  return `${protocol}://${sandbox.id}.${sandbox.cluster}.csb.app:${options.port}`;
192
136
  },
193
- // Filesystem operations using CodeSandbox client.fs API
194
137
  filesystem: {
195
138
  readFile: async (sandbox, path) => {
196
139
  const client = await sandbox.connect();
197
- return await client.fs.readTextFile(path);
140
+ return client.fs.readTextFile(path);
198
141
  },
199
142
  writeFile: async (sandbox, path, content) => {
200
143
  const client = await sandbox.connect();
@@ -228,74 +171,43 @@ var codesandbox = (0, import_provider.defineProvider)({
228
171
  await client.fs.remove(path);
229
172
  }
230
173
  },
231
- // Provider-specific typed getInstance method
232
- getInstance: (sandbox) => {
233
- return sandbox;
234
- }
174
+ getInstance: (sandbox) => sandbox
235
175
  },
236
176
  snapshot: {
237
177
  create: async (config, sandboxId, options) => {
238
178
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
239
- if (!apiKey) {
240
- throw new Error(
241
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
242
- );
243
- }
179
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
244
180
  const sdk = new import_sdk.CodeSandbox(apiKey);
245
181
  try {
246
182
  const sandbox = await sdk.sandboxes.resume(sandboxId);
247
- const hibernateSandbox = sandbox;
248
- await hibernateSandbox.hibernate();
249
- return {
250
- id: sandbox.id,
251
- provider: "codesandbox",
252
- createdAt: /* @__PURE__ */ new Date(),
253
- metadata: {
254
- name: options?.name,
255
- bootupType: sandbox.bootupType
256
- }
257
- };
183
+ await sandbox.hibernate();
184
+ return { id: sandbox.id, provider: "codesandbox", createdAt: /* @__PURE__ */ new Date(), metadata: { name: options?.name, bootupType: sandbox.bootupType } };
258
185
  } catch (error) {
259
- throw new Error(
260
- `Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`
261
- );
186
+ throw new Error(`Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`);
262
187
  }
263
188
  },
264
189
  list: async (_config) => {
265
- throw new Error(
266
- `CodeSandbox provider does not support listing snapshots. Use the dashboard to manage snapshots.`
267
- );
190
+ throw new Error(`CodeSandbox provider does not support listing snapshots.`);
268
191
  },
269
192
  delete: async (config, snapshotId) => {
270
193
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
271
- if (!apiKey) {
272
- throw new Error(
273
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
274
- );
275
- }
194
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
276
195
  const sdk = new import_sdk.CodeSandbox(apiKey);
277
196
  try {
278
197
  await sdk.sandboxes.shutdown(snapshotId);
279
- } catch (error) {
198
+ } catch {
280
199
  }
281
200
  }
282
201
  },
283
- // Templates in CodeSandbox are handled via templateId on create
284
202
  template: {
285
203
  create: async (_config, _options) => {
286
- throw new Error(
287
- `CodeSandbox templates must be created via the CodeSandbox dashboard or CLI. Use templateId in sandbox.create() to specify a template.`
288
- );
204
+ throw new Error(`CodeSandbox templates must be created via the dashboard.`);
289
205
  },
290
206
  list: async (_config) => {
291
- throw new Error(
292
- `CodeSandbox provider does not support listing templates via API. Use the dashboard to manage templates.`
293
- );
207
+ throw new Error(`CodeSandbox provider does not support listing templates via API.`);
294
208
  },
295
209
  delete: async (_config, _templateId) => {
296
- throw new Error(
297
- `CodeSandbox templates must be deleted via the CodeSandbox dashboard or CLI.`
298
- );
210
+ throw new Error(`CodeSandbox templates must be deleted via the dashboard.`);
299
211
  }
300
212
  }
301
213
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Codesandbox Provider - Factory-based Implementation\n * \n * Full-featured provider with filesystem support using the factory pattern.\n */\n\nimport { CodeSandbox } from '@codesandbox/sdk';\nimport type { Sandbox as CodesandboxSandbox } from '@codesandbox/sdk';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\n\nimport type { Runtime, CommandResult, SandboxInfo, CreateSandboxOptions, FileEntry, RunCommandOptions } from '@computesdk/provider';\n\ntype HibernateCapableSandbox = CodesandboxSandbox & {\n hibernate: () => Promise<void>;\n};\n\n/**\n * Codesandbox-specific configuration options\n */\nexport interface CodesandboxConfig {\n /** CodeSandbox API key - if not provided, will fallback to CSB_API_KEY environment variable */\n apiKey?: string;\n /** Template to use for new sandboxes */\n templateId?: string;\n /** Default runtime environment */\n runtime?: Runtime;\n /** Execution timeout in milliseconds */\n timeout?: number;\n}\n\n/**\n * Create a Codesandbox provider instance using the factory pattern\n */\nexport const codesandbox = defineProvider<CodesandboxSandbox, CodesandboxConfig, any, any>({\n name: 'codesandbox',\n methods: {\n sandbox: {\n // Collection operations (compute.sandbox.*)\n create: async (config: CodesandboxConfig, options?: CreateSandboxOptions) => {\n // Validate API key\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n let sandbox: CodesandboxSandbox;\n let sandboxId: string;\n\n if (options?.snapshotId) {\n // Resume from snapshot - in CodeSandbox, snapshots are hibernated sandboxes\n sandbox = await sdk.sandboxes.resume(options.snapshotId);\n sandboxId = options.snapshotId;\n } else {\n // Destructure known ComputeSDK fields, collect the rest for passthrough\n const {\n runtime: _runtime,\n timeout: _timeout,\n envs,\n name: _name,\n metadata: _metadata,\n templateId: optTemplateId,\n snapshotId: _snapshotId,\n sandboxId: _sandboxId,\n namespace: _namespace,\n directory: _directory,\n ...providerOptions\n } = options || {};\n\n // Create new CodeSandbox using sdk.sandboxes.create()\n const createOptions: any = {\n ...providerOptions, // Spread provider-specific options\n };\n\n // options.templateId takes precedence over config.templateId\n const templateId = optTemplateId || config.templateId;\n if (templateId) {\n createOptions.id = templateId;\n }\n\n // Remap envs to envVars\n if (envs && Object.keys(envs).length > 0) {\n createOptions.envVars = envs;\n }\n\n sandbox = await sdk.sandboxes.create(createOptions);\n sandboxId = sandbox.id;\n }\n\n return {\n sandbox,\n sandboxId\n };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('API key')) {\n throw new Error(\n `CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`\n );\n }\n if (error.message.includes('quota') || error.message.includes('limit')) {\n throw new Error(\n `CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`\n );\n }\n }\n throw new Error(\n `Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n getById: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n // Resume existing sandbox using sdk.sandboxes.resume()\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n\n return {\n sandbox,\n sandboxId\n };\n } catch (error) {\n // Sandbox doesn't exist or can't be accessed\n return null;\n }\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(\n `CodeSandbox provider does not support listing sandboxes. CodeSandbox SDK does not provide a native list API. Consider using the CodeSandbox dashboard or implement your own tracking system.`\n );\n },\n\n destroy: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n await sdk.sandboxes.shutdown(sandboxId);\n } catch {\n // Ignore — may already be stopped\n }\n\n try {\n await sdk.sandboxes.delete(sandboxId);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n if (!message.includes('not found') && !message.includes('404')) {\n throw new Error(`Failed to delete CodeSandbox sandbox \"${sandboxId}\": ${message}`);\n }\n }\n },\n\n // Instance operations (sandbox.*)\n\n runCommand: async (sandbox: CodesandboxSandbox, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n\n try {\n // Connect to the sandbox client using sandbox.connect()\n const client = await sandbox.connect();\n\n // Build command with options\n let fullCommand = command;\n\n // Handle environment variables\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env)\n .map(([k, v]) => `${k}=\"${escapeShellArg(v)}\"`)\n .join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n\n // Handle working directory\n if (options?.cwd) {\n fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n }\n\n // Handle background execution\n if (options?.background) {\n fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n }\n\n // Execute command using CodeSandbox client.commands.run()\n const output = await client.commands.run(fullCommand);\n\n return {\n stdout: output,\n stderr: '',\n exitCode: 0,\n durationMs: Date.now() - startTime\n };\n } catch (error) {\n return {\n stdout: '',\n stderr: error instanceof Error ? error.message : String(error),\n exitCode: 127,\n durationMs: Date.now() - startTime\n };\n }\n },\n\n getInfo: async (sandbox: CodesandboxSandbox): Promise<SandboxInfo> => {\n return {\n id: sandbox.id,\n provider: 'codesandbox',\n runtime: 'node', // CodeSandbox default\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: {\n cluster: sandbox.cluster,\n bootupType: sandbox.bootupType,\n isUpToDate: sandbox.isUpToDate\n }\n };\n },\n\n getUrl: async (sandbox: CodesandboxSandbox, options: { port: number; protocol?: string }): Promise<string> => {\n const protocol = options.protocol || 'https';\n // CodeSandbox provides URLs in the format: https://{sandbox-id}.{cluster}.csb.app:{port}\n // Use the actual CodeSandbox URL format\n return `${protocol}://${sandbox.id}.${sandbox.cluster}.csb.app:${options.port}`;\n },\n\n // Filesystem operations using CodeSandbox client.fs API\n filesystem: {\n readFile: async (sandbox: CodesandboxSandbox, path: string): Promise<string> => {\n // Connect to the sandbox client and use client.fs.readTextFile()\n const client = await sandbox.connect();\n return await client.fs.readTextFile(path);\n },\n\n writeFile: async (sandbox: CodesandboxSandbox, path: string, content: string): Promise<void> => {\n // Connect to the sandbox client and use client.fs.writeTextFile()\n const client = await sandbox.connect();\n await client.fs.writeTextFile(path, content);\n },\n\n mkdir: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n // CodeSandbox doesn't have a direct mkdir API, use commands to create directory\n const client = await sandbox.connect();\n await client.commands.run(`mkdir -p \"${path}\"`);\n },\n\n readdir: async (sandbox: CodesandboxSandbox, path: string): Promise<FileEntry[]> => {\n // Connect to the sandbox client and use client.fs.readdir()\n const client = await sandbox.connect();\n const entries = await client.fs.readdir(path);\n\n return entries.map((entry: any) => ({\n name: entry.name,\n type: entry.isDirectory ? 'directory' as const : 'file' as const,\n size: entry.size || 0,\n modified: entry.lastModified ? new Date(entry.lastModified) : new Date()\n }));\n },\n\n exists: async (sandbox: CodesandboxSandbox, path: string): Promise<boolean> => {\n // CodeSandbox doesn't have a direct exists API, use ls command to check\n const client = await sandbox.connect();\n try {\n await client.commands.run(`ls \"${path}\"`);\n return true;\n } catch {\n return false;\n }\n },\n\n remove: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n // Connect to the sandbox client and use client.fs.remove()\n const client = await sandbox.connect();\n await client.fs.remove(path);\n }\n },\n\n // Provider-specific typed getInstance method\n getInstance: (sandbox: CodesandboxSandbox): CodesandboxSandbox => {\n return sandbox;\n },\n\n },\n\n snapshot: {\n create: async (config: CodesandboxConfig, sandboxId: string, options?: { name?: string }) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n // Resume the sandbox first, then hibernate to create a snapshot\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n\n // Hibernate creates a checkpoint/snapshot of the sandbox\n const hibernateSandbox = sandbox as HibernateCapableSandbox;\n await hibernateSandbox.hibernate();\n\n // The hibernated sandbox becomes a snapshot we can fork\n return {\n id: sandbox.id,\n provider: 'codesandbox',\n createdAt: new Date(),\n metadata: {\n name: options?.name,\n bootupType: sandbox.bootupType\n }\n };\n } catch (error) {\n throw new Error(\n `Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(\n `CodeSandbox provider does not support listing snapshots. Use the dashboard to manage snapshots.`\n );\n },\n\n delete: async (config: CodesandboxConfig, snapshotId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n // Shutdown the snapshot (hibernated sandbox)\n await sdk.sandboxes.shutdown(snapshotId);\n } catch (error) {\n // Ignore if not found\n }\n }\n },\n\n // Templates in CodeSandbox are handled via templateId on create\n template: {\n create: async (_config: CodesandboxConfig, _options: { name: string }) => {\n throw new Error(\n `CodeSandbox templates must be created via the CodeSandbox dashboard or CLI. Use templateId in sandbox.create() to specify a template.`\n );\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(\n `CodeSandbox provider does not support listing templates via API. Use the dashboard to manage templates.`\n );\n },\n\n delete: async (_config: CodesandboxConfig, _templateId: string) => {\n throw new Error(\n `CodeSandbox templates must be deleted via the CodeSandbox dashboard or CLI.`\n );\n }\n }\n }\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,iBAA4B;AAE5B,sBAA+C;AAyBxC,IAAM,kBAAc,gCAAgE;AAAA,EACzF,MAAM;AAAA,EACN,SAAS;AAAA,IACP,SAAS;AAAA;AAAA,MAEP,QAAQ,OAAO,QAA2B,YAAmC;AAE3E,cAAM,SAAS,OAAO,UAAW,OAAO,YAAY,eAAe,QAAQ,KAAK,eAAgB;AAEhG,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,cAAM,MAAM,IAAI,uBAAY,MAAM;AAElC,YAAI;AACF,cAAI;AACJ,cAAI;AAEJ,cAAI,SAAS,YAAY;AAEvB,sBAAU,MAAM,IAAI,UAAU,OAAO,QAAQ,UAAU;AACvD,wBAAY,QAAQ;AAAA,UACtB,OAAO;AAEL,kBAAM;AAAA,cACJ,SAAS;AAAA,cACT,SAAS;AAAA,cACT;AAAA,cACA,MAAM;AAAA,cACN,UAAU;AAAA,cACV,YAAY;AAAA,cACZ,YAAY;AAAA,cACZ,WAAW;AAAA,cACX,WAAW;AAAA,cACX,WAAW;AAAA,cACX,GAAG;AAAA,YACL,IAAI,WAAW,CAAC;AAGhB,kBAAM,gBAAqB;AAAA,cACzB,GAAG;AAAA;AAAA,YACL;AAGA,kBAAM,aAAa,iBAAiB,OAAO;AAC3C,gBAAI,YAAY;AACd,4BAAc,KAAK;AAAA,YACrB;AAGA,gBAAI,QAAQ,OAAO,KAAK,IAAI,EAAE,SAAS,GAAG;AACxC,4BAAc,UAAU;AAAA,YAC1B;AAEA,sBAAU,MAAM,IAAI,U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M,OAAO,YAA+B;AAC1C,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,MAEA,QAAQ,OAAO,QAA2B,eAAuB;AAC/D,cAAM,SAAS,OAAO,UAAW,OAAO,YAAY,eAAe,QAAQ,KAAK,eAAgB;AAEhG,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,cAAM,MAAM,IAAI,uBAAY,MAAM;AAElC,YAAI;AAEF,gBAAM,IAAI,UAAU,SAAS,UAAU;AAAA,QACzC,SAAS,OAAO;AAAA,QAEhB;AAAA,MACF;AAAA,IACF;AAAA;AAAA,IAGA,UAAU;AAAA,MACR,QAAQ,OAAO,SAA4B,aAA+B;AACxE,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,MAEA,MAAM,OAAO,YAA+B;AAC1C,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,MAEA,QAAQ,OAAO,SAA4B,gBAAwB;AACjE,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Codesandbox Provider - Factory-based Implementation\n */\n\nimport { CodeSandbox } from '@codesandbox/sdk';\nimport type { Sandbox as CodesandboxSandbox } from '@codesandbox/sdk';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\n\nimport type { CommandResult, SandboxInfo, CreateSandboxOptions, FileEntry, RunCommandOptions } from '@computesdk/provider';\n\ntype HibernateCapableSandbox = CodesandboxSandbox & { hibernate: () => Promise<void>; };\n\nexport interface CodesandboxConfig {\n /** CodeSandbox API key - if not provided, will fallback to CSB_API_KEY environment variable */\n apiKey?: string;\n /** Template to use for new sandboxes */\n templateId?: string;\n /** Default runtime environment (e.g. 'node', 'python') */\n runtime?: string;\n /** Execution timeout in milliseconds */\n timeout?: number;\n}\n\nexport const codesandbox = defineProvider<CodesandboxSandbox, CodesandboxConfig, any, any>({\n name: 'codesandbox',\n methods: {\n sandbox: {\n create: async (config: CodesandboxConfig, options?: CreateSandboxOptions) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) {\n throw new Error(`Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`);\n }\n const sdk = new CodeSandbox(apiKey);\n try {\n let sandbox: CodesandboxSandbox;\n let sandboxId: string;\n if (options?.snapshotId) {\n sandbox = await sdk.sandboxes.resume(options.snapshotId);\n sandboxId = options.snapshotId;\n } else {\n const {\n timeout: _timeout, envs, name: _name, metadata: _metadata,\n templateId: optTemplateId, snapshotId: _snapshotId, sandboxId: _sandboxId,\n namespace: _namespace, directory: _directory, ...providerOptions\n } = options || {};\n const createOptions: any = { ...providerOptions };\n const templateId = optTemplateId || config.templateId;\n if (templateId) createOptions.id = templateId;\n if (envs && Object.keys(envs).length > 0) createOptions.envVars = envs;\n sandbox = await sdk.sandboxes.create(createOptions);\n sandboxId = sandbox.id;\n }\n return { sandbox, sandboxId };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('API key')) {\n throw new Error(`CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable.`);\n }\n if (error.message.includes('quota') || error.message.includes('limit')) {\n throw new Error(`CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`);\n }\n }\n throw new Error(`Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`);\n }\n },\n\n getById: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try {\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n return { sandbox, sandboxId };\n } catch { return null; }\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(`CodeSandbox provider does not support listing sandboxes.`);\n },\n\n destroy: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try { await sdk.sandboxes.shutdown(sandboxId); } catch { /* ignore */ }\n try {\n await sdk.sandboxes.delete(sandboxId);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n if (!message.includes('not found') && !message.includes('404')) {\n throw new Error(`Failed to delete CodeSandbox sandbox \"${sandboxId}\": ${message}`);\n }\n }\n },\n\n runCommand: async (sandbox: CodesandboxSandbox, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n try {\n const client = await sandbox.connect();\n let fullCommand = command;\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}=\"${escapeShellArg(v)}\"`).join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n if (options?.cwd) fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n if (options?.background) fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n const output = await client.commands.run(fullCommand);\n return { stdout: output, stderr: '', exitCode: 0, durationMs: Date.now() - startTime };\n } catch (error) {\n return { stdout: '', stderr: error instanceof Error ? error.message : String(error), exitCode: 127, durationMs: Date.now() - startTime };\n }\n },\n\n getInfo: async (sandbox: CodesandboxSandbox): Promise<SandboxInfo> => ({\n id: sandbox.id,\n provider: 'codesandbox',\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: { cluster: sandbox.cluster, bootupType: sandbox.bootupType, isUpToDate: sandbox.isUpToDate }\n }),\n\n getUrl: async (sandbox: CodesandboxSandbox, options: { port: number; protocol?: string }): Promise<string> => {\n const protocol = options.protocol || 'https';\n return `${protocol}://${sandbox.id}.${sandbox.cluster}.csb.app:${options.port}`;\n },\n\n filesystem: {\n readFile: async (sandbox: CodesandboxSandbox, path: string): Promise<string> => {\n const client = await sandbox.connect();\n return client.fs.readTextFile(path);\n },\n writeFile: async (sandbox: CodesandboxSandbox, path: string, content: string): Promise<void> => {\n const client = await sandbox.connect();\n await client.fs.writeTextFile(path, content);\n },\n mkdir: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n const client = await sandbox.connect();\n await client.commands.run(`mkdir -p \"${path}\"`);\n },\n readdir: async (sandbox: CodesandboxSandbox, path: string): Promise<FileEntry[]> => {\n const client = await sandbox.connect();\n const entries = await client.fs.readdir(path);\n return entries.map((entry: any) => ({\n name: entry.name,\n type: entry.isDirectory ? 'directory' as const : 'file' as const,\n size: entry.size || 0,\n modified: entry.lastModified ? new Date(entry.lastModified) : new Date()\n }));\n },\n exists: async (sandbox: CodesandboxSandbox, path: string): Promise<boolean> => {\n const client = await sandbox.connect();\n try { await client.commands.run(`ls \"${path}\"`); return true; } catch { return false; }\n },\n remove: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n const client = await sandbox.connect();\n await client.fs.remove(path);\n }\n },\n\n getInstance: (sandbox: CodesandboxSandbox): CodesandboxSandbox => sandbox,\n },\n\n snapshot: {\n create: async (config: CodesandboxConfig, sandboxId: string, options?: { name?: string }) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try {\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n await (sandbox as HibernateCapableSandbox).hibernate();\n return { id: sandbox.id, provider: 'codesandbox', createdAt: new Date(), metadata: { name: options?.name, bootupType: sandbox.bootupType } };\n } catch (error) {\n throw new Error(`Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`);\n }\n },\n list: async (_config: CodesandboxConfig) => { throw new Error(`CodeSandbox provider does not support listing snapshots.`); },\n delete: async (config: CodesandboxConfig, snapshotId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try { await sdk.sandboxes.shutdown(snapshotId); } catch { /* ignore */ }\n }\n },\n\n template: {\n create: async (_config: CodesandboxConfig, _options: { name: string }) => {\n throw new Error(`CodeSandbox templates must be created via the dashboard.`);\n },\n list: async (_config: CodesandboxConfig) => { throw new Error(`CodeSandbox provider does not support listing templates via API.`); },\n delete: async (_config: CodesandboxConfig, _templateId: string) => { throw new Error(`CodeSandbox templates must be deleted via the dashboard.`); }\n }\n }\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,iBAA4B;AAE5B,sBAA+C;AAiBxC,IAAM,kBAAc,gCAAgE;AAAA,EACzF,MAAM;AAAA,EACN,SAAS;AAAA,IACP,SAAS;AAAA,MACP,QAAQ,OAAO,QAA2B,YAAmC;AAC3E,cAAM,SAAS,OAAO,UAAW,OAAO,YAAY,eAAe,QAAQ,KAAK,eAAgB;AAChG,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI,MAAM,kGAAkG;AAAA,QACpH;AACA,cAAM,MAAM,IAAI,uBAAY,MAAM;AAClC,YAAI;AACF,cAAI;AACJ,cAAI;AACJ,cAAI,SAAS,YAAY;AACvB,sBAAU,MAAM,IAAI,UAAU,OAAO,QAAQ,UAAU;AACvD,wBAAY,QAAQ;AAAA,UACtB,OAAO;AACL,kBAAM;AAAA,cACJ,SAAS;AAAA,cAAU;AAAA,cAAM,MAAM;AAAA,cAAO,UAAU;AAAA,cAChD,YAAY;AAAA,cAAe,YAAY;AAAA,cAAa,WAAW;AAAA,cAC/D,WAAW;AAAA,cAAY,WAAW;AAAA,cAAY,GAAG;AAAA,YACnD,IAAI,WAAW,CAAC;AAChB,kBAAM,gBAAqB,EAAE,GAAG,gBAAgB;AAChD,kBAAM,aAAa,i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AAE,gBAAM,IAAI,UAAU,SAAS,UAAU;AAAA,QAAG,QAAQ;AAAA,QAAe;AAAA,MACzE;AAAA,IACF;AAAA,IAEA,UAAU;AAAA,MACR,QAAQ,OAAO,SAA4B,aAA+B;AACxE,cAAM,IAAI,MAAM,0DAA0D;AAAA,MAC5E;AAAA,MACA,MAAM,OAAO,YAA+B;AAAE,cAAM,IAAI,MAAM,kEAAkE;AAAA,MAAG;AAAA,MACnI,QAAQ,OAAO,SAA4B,gBAAwB;AAAE,cAAM,IAAI,MAAM,0DAA0D;AAAA,MAAG;AAAA,IACpJ;AAAA,EACF;AACF,CAAC;","names":[]}
package/dist/index.mjs CHANGED
@@ -5,13 +5,10 @@ var codesandbox = defineProvider({
5
5
  name: "codesandbox",
6
6
  methods: {
7
7
  sandbox: {
8
- // Collection operations (compute.sandbox.*)
9
8
  create: async (config, options) => {
10
9
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
11
10
  if (!apiKey) {
12
- throw new Error(
13
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`
14
- );
11
+ throw new Error(`Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`);
15
12
  }
16
13
  const sdk = new CodeSandbox(apiKey);
17
14
  try {
@@ -22,7 +19,6 @@ var codesandbox = defineProvider({
22
19
  sandboxId = options.snapshotId;
23
20
  } else {
24
21
  const {
25
- runtime: _runtime,
26
22
  timeout: _timeout,
27
23
  envs,
28
24
  name: _name,
@@ -34,72 +30,43 @@ var codesandbox = defineProvider({
34
30
  directory: _directory,
35
31
  ...providerOptions
36
32
  } = options || {};
37
- const createOptions = {
38
- ...providerOptions
39
- // Spread provider-specific options
40
- };
33
+ const createOptions = { ...providerOptions };
41
34
  const templateId = optTemplateId || config.templateId;
42
- if (templateId) {
43
- createOptions.id = templateId;
44
- }
45
- if (envs && Object.keys(envs).length > 0) {
46
- createOptions.envVars = envs;
47
- }
35
+ if (templateId) createOptions.id = templateId;
36
+ if (envs && Object.keys(envs).length > 0) createOptions.envVars = envs;
48
37
  sandbox = await sdk.sandboxes.create(createOptions);
49
38
  sandboxId = sandbox.id;
50
39
  }
51
- return {
52
- sandbox,
53
- sandboxId
54
- };
40
+ return { sandbox, sandboxId };
55
41
  } catch (error) {
56
42
  if (error instanceof Error) {
57
43
  if (error.message.includes("unauthorized") || error.message.includes("API key")) {
58
- throw new Error(
59
- `CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`
60
- );
44
+ throw new Error(`CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable.`);
61
45
  }
62
46
  if (error.message.includes("quota") || error.message.includes("limit")) {
63
- throw new Error(
64
- `CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`
65
- );
47
+ throw new Error(`CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`);
66
48
  }
67
49
  }
68
- throw new Error(
69
- `Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`
70
- );
50
+ throw new Error(`Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`);
71
51
  }
72
52
  },
73
53
  getById: async (config, sandboxId) => {
74
54
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
75
- if (!apiKey) {
76
- throw new Error(
77
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
78
- );
79
- }
55
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
80
56
  const sdk = new CodeSandbox(apiKey);
81
57
  try {
82
58
  const sandbox = await sdk.sandboxes.resume(sandboxId);
83
- return {
84
- sandbox,
85
- sandboxId
86
- };
87
- } catch (error) {
59
+ return { sandbox, sandboxId };
60
+ } catch {
88
61
  return null;
89
62
  }
90
63
  },
91
64
  list: async (_config) => {
92
- throw new Error(
93
- `CodeSandbox provider does not support listing sandboxes. CodeSandbox SDK does not provide a native list API. Consider using the CodeSandbox dashboard or implement your own tracking system.`
94
- );
65
+ throw new Error(`CodeSandbox provider does not support listing sandboxes.`);
95
66
  },
96
67
  destroy: async (config, sandboxId) => {
97
68
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
98
- if (!apiKey) {
99
- throw new Error(
100
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
101
- );
102
- }
69
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
103
70
  const sdk = new CodeSandbox(apiKey);
104
71
  try {
105
72
  await sdk.sandboxes.shutdown(sandboxId);
@@ -114,7 +81,6 @@ var codesandbox = defineProvider({
114
81
  }
115
82
  }
116
83
  },
117
- // Instance operations (sandbox.*)
118
84
  runCommand: async (sandbox, command, options) => {
119
85
  const startTime = Date.now();
120
86
  try {
@@ -124,53 +90,30 @@ var codesandbox = defineProvider({
124
90
  const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}="${escapeShellArg(v)}"`).join(" ");
125
91
  fullCommand = `${envPrefix} ${fullCommand}`;
126
92
  }
127
- if (options?.cwd) {
128
- fullCommand = `cd "${escapeShellArg(options.cwd)}" && ${fullCommand}`;
129
- }
130
- if (options?.background) {
131
- fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;
132
- }
93
+ if (options?.cwd) fullCommand = `cd "${escapeShellArg(options.cwd)}" && ${fullCommand}`;
94
+ if (options?.background) fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;
133
95
  const output = await client.commands.run(fullCommand);
134
- return {
135
- stdout: output,
136
- stderr: "",
137
- exitCode: 0,
138
- durationMs: Date.now() - startTime
139
- };
96
+ return { stdout: output, stderr: "", exitCode: 0, durationMs: Date.now() - startTime };
140
97
  } catch (error) {
141
- return {
142
- stdout: "",
143
- stderr: error instanceof Error ? error.message : String(error),
144
- exitCode: 127,
145
- durationMs: Date.now() - startTime
146
- };
98
+ return { stdout: "", stderr: error instanceof Error ? error.message : String(error), exitCode: 127, durationMs: Date.now() - startTime };
147
99
  }
148
100
  },
149
- getInfo: async (sandbox) => {
150
- return {
151
- id: sandbox.id,
152
- provider: "codesandbox",
153
- runtime: "node",
154
- // CodeSandbox default
155
- status: "running",
156
- createdAt: /* @__PURE__ */ new Date(),
157
- timeout: 3e5,
158
- metadata: {
159
- cluster: sandbox.cluster,
160
- bootupType: sandbox.bootupType,
161
- isUpToDate: sandbox.isUpToDate
162
- }
163
- };
164
- },
101
+ getInfo: async (sandbox) => ({
102
+ id: sandbox.id,
103
+ provider: "codesandbox",
104
+ status: "running",
105
+ createdAt: /* @__PURE__ */ new Date(),
106
+ timeout: 3e5,
107
+ metadata: { cluster: sandbox.cluster, bootupType: sandbox.bootupType, isUpToDate: sandbox.isUpToDate }
108
+ }),
165
109
  getUrl: async (sandbox, options) => {
166
110
  const protocol = options.protocol || "https";
167
111
  return `${protocol}://${sandbox.id}.${sandbox.cluster}.csb.app:${options.port}`;
168
112
  },
169
- // Filesystem operations using CodeSandbox client.fs API
170
113
  filesystem: {
171
114
  readFile: async (sandbox, path) => {
172
115
  const client = await sandbox.connect();
173
- return await client.fs.readTextFile(path);
116
+ return client.fs.readTextFile(path);
174
117
  },
175
118
  writeFile: async (sandbox, path, content) => {
176
119
  const client = await sandbox.connect();
@@ -204,74 +147,43 @@ var codesandbox = defineProvider({
204
147
  await client.fs.remove(path);
205
148
  }
206
149
  },
207
- // Provider-specific typed getInstance method
208
- getInstance: (sandbox) => {
209
- return sandbox;
210
- }
150
+ getInstance: (sandbox) => sandbox
211
151
  },
212
152
  snapshot: {
213
153
  create: async (config, sandboxId, options) => {
214
154
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
215
- if (!apiKey) {
216
- throw new Error(
217
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
218
- );
219
- }
155
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
220
156
  const sdk = new CodeSandbox(apiKey);
221
157
  try {
222
158
  const sandbox = await sdk.sandboxes.resume(sandboxId);
223
- const hibernateSandbox = sandbox;
224
- await hibernateSandbox.hibernate();
225
- return {
226
- id: sandbox.id,
227
- provider: "codesandbox",
228
- createdAt: /* @__PURE__ */ new Date(),
229
- metadata: {
230
- name: options?.name,
231
- bootupType: sandbox.bootupType
232
- }
233
- };
159
+ await sandbox.hibernate();
160
+ return { id: sandbox.id, provider: "codesandbox", createdAt: /* @__PURE__ */ new Date(), metadata: { name: options?.name, bootupType: sandbox.bootupType } };
234
161
  } catch (error) {
235
- throw new Error(
236
- `Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`
237
- );
162
+ throw new Error(`Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`);
238
163
  }
239
164
  },
240
165
  list: async (_config) => {
241
- throw new Error(
242
- `CodeSandbox provider does not support listing snapshots. Use the dashboard to manage snapshots.`
243
- );
166
+ throw new Error(`CodeSandbox provider does not support listing snapshots.`);
244
167
  },
245
168
  delete: async (config, snapshotId) => {
246
169
  const apiKey = config.apiKey || typeof process !== "undefined" && process.env?.CSB_API_KEY || "";
247
- if (!apiKey) {
248
- throw new Error(
249
- `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`
250
- );
251
- }
170
+ if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);
252
171
  const sdk = new CodeSandbox(apiKey);
253
172
  try {
254
173
  await sdk.sandboxes.shutdown(snapshotId);
255
- } catch (error) {
174
+ } catch {
256
175
  }
257
176
  }
258
177
  },
259
- // Templates in CodeSandbox are handled via templateId on create
260
178
  template: {
261
179
  create: async (_config, _options) => {
262
- throw new Error(
263
- `CodeSandbox templates must be created via the CodeSandbox dashboard or CLI. Use templateId in sandbox.create() to specify a template.`
264
- );
180
+ throw new Error(`CodeSandbox templates must be created via the dashboard.`);
265
181
  },
266
182
  list: async (_config) => {
267
- throw new Error(
268
- `CodeSandbox provider does not support listing templates via API. Use the dashboard to manage templates.`
269
- );
183
+ throw new Error(`CodeSandbox provider does not support listing templates via API.`);
270
184
  },
271
185
  delete: async (_config, _templateId) => {
272
- throw new Error(
273
- `CodeSandbox templates must be deleted via the CodeSandbox dashboard or CLI.`
274
- );
186
+ throw new Error(`CodeSandbox templates must be deleted via the dashboard.`);
275
187
  }
276
188
  }
277
189
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Codesandbox Provider - Factory-based Implementation\n * \n * Full-featured provider with filesystem support using the factory pattern.\n */\n\nimport { CodeSandbox } from '@codesandbox/sdk';\nimport type { Sandbox as CodesandboxSandbox } from '@codesandbox/sdk';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\n\nimport type { Runtime, CommandResult, SandboxInfo, CreateSandboxOptions, FileEntry, RunCommandOptions } from '@computesdk/provider';\n\ntype HibernateCapableSandbox = CodesandboxSandbox & {\n hibernate: () => Promise<void>;\n};\n\n/**\n * Codesandbox-specific configuration options\n */\nexport interface CodesandboxConfig {\n /** CodeSandbox API key - if not provided, will fallback to CSB_API_KEY environment variable */\n apiKey?: string;\n /** Template to use for new sandboxes */\n templateId?: string;\n /** Default runtime environment */\n runtime?: Runtime;\n /** Execution timeout in milliseconds */\n timeout?: number;\n}\n\n/**\n * Create a Codesandbox provider instance using the factory pattern\n */\nexport const codesandbox = defineProvider<CodesandboxSandbox, CodesandboxConfig, any, any>({\n name: 'codesandbox',\n methods: {\n sandbox: {\n // Collection operations (compute.sandbox.*)\n create: async (config: CodesandboxConfig, options?: CreateSandboxOptions) => {\n // Validate API key\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n let sandbox: CodesandboxSandbox;\n let sandboxId: string;\n\n if (options?.snapshotId) {\n // Resume from snapshot - in CodeSandbox, snapshots are hibernated sandboxes\n sandbox = await sdk.sandboxes.resume(options.snapshotId);\n sandboxId = options.snapshotId;\n } else {\n // Destructure known ComputeSDK fields, collect the rest for passthrough\n const {\n runtime: _runtime,\n timeout: _timeout,\n envs,\n name: _name,\n metadata: _metadata,\n templateId: optTemplateId,\n snapshotId: _snapshotId,\n sandboxId: _sandboxId,\n namespace: _namespace,\n directory: _directory,\n ...providerOptions\n } = options || {};\n\n // Create new CodeSandbox using sdk.sandboxes.create()\n const createOptions: any = {\n ...providerOptions, // Spread provider-specific options\n };\n\n // options.templateId takes precedence over config.templateId\n const templateId = optTemplateId || config.templateId;\n if (templateId) {\n createOptions.id = templateId;\n }\n\n // Remap envs to envVars\n if (envs && Object.keys(envs).length > 0) {\n createOptions.envVars = envs;\n }\n\n sandbox = await sdk.sandboxes.create(createOptions);\n sandboxId = sandbox.id;\n }\n\n return {\n sandbox,\n sandboxId\n };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('API key')) {\n throw new Error(\n `CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable. Get your API key from https://codesandbox.io/t/api`\n );\n }\n if (error.message.includes('quota') || error.message.includes('limit')) {\n throw new Error(\n `CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`\n );\n }\n }\n throw new Error(\n `Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n getById: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n // Resume existing sandbox using sdk.sandboxes.resume()\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n\n return {\n sandbox,\n sandboxId\n };\n } catch (error) {\n // Sandbox doesn't exist or can't be accessed\n return null;\n }\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(\n `CodeSandbox provider does not support listing sandboxes. CodeSandbox SDK does not provide a native list API. Consider using the CodeSandbox dashboard or implement your own tracking system.`\n );\n },\n\n destroy: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n await sdk.sandboxes.shutdown(sandboxId);\n } catch {\n // Ignore — may already be stopped\n }\n\n try {\n await sdk.sandboxes.delete(sandboxId);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n if (!message.includes('not found') && !message.includes('404')) {\n throw new Error(`Failed to delete CodeSandbox sandbox \"${sandboxId}\": ${message}`);\n }\n }\n },\n\n // Instance operations (sandbox.*)\n\n runCommand: async (sandbox: CodesandboxSandbox, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n\n try {\n // Connect to the sandbox client using sandbox.connect()\n const client = await sandbox.connect();\n\n // Build command with options\n let fullCommand = command;\n\n // Handle environment variables\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env)\n .map(([k, v]) => `${k}=\"${escapeShellArg(v)}\"`)\n .join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n\n // Handle working directory\n if (options?.cwd) {\n fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n }\n\n // Handle background execution\n if (options?.background) {\n fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n }\n\n // Execute command using CodeSandbox client.commands.run()\n const output = await client.commands.run(fullCommand);\n\n return {\n stdout: output,\n stderr: '',\n exitCode: 0,\n durationMs: Date.now() - startTime\n };\n } catch (error) {\n return {\n stdout: '',\n stderr: error instanceof Error ? error.message : String(error),\n exitCode: 127,\n durationMs: Date.now() - startTime\n };\n }\n },\n\n getInfo: async (sandbox: CodesandboxSandbox): Promise<SandboxInfo> => {\n return {\n id: sandbox.id,\n provider: 'codesandbox',\n runtime: 'node', // CodeSandbox default\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: {\n cluster: sandbox.cluster,\n bootupType: sandbox.bootupType,\n isUpToDate: sandbox.isUpToDate\n }\n };\n },\n\n getUrl: async (sandbox: CodesandboxSandbox, options: { port: number; protocol?: string }): Promise<string> => {\n const protocol = options.protocol || 'https';\n // CodeSandbox provides URLs in the format: https://{sandbox-id}.{cluster}.csb.app:{port}\n // Use the actual CodeSandbox URL format\n return `${protocol}://${sandbox.id}.${sandbox.cluster}.csb.app:${options.port}`;\n },\n\n // Filesystem operations using CodeSandbox client.fs API\n filesystem: {\n readFile: async (sandbox: CodesandboxSandbox, path: string): Promise<string> => {\n // Connect to the sandbox client and use client.fs.readTextFile()\n const client = await sandbox.connect();\n return await client.fs.readTextFile(path);\n },\n\n writeFile: async (sandbox: CodesandboxSandbox, path: string, content: string): Promise<void> => {\n // Connect to the sandbox client and use client.fs.writeTextFile()\n const client = await sandbox.connect();\n await client.fs.writeTextFile(path, content);\n },\n\n mkdir: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n // CodeSandbox doesn't have a direct mkdir API, use commands to create directory\n const client = await sandbox.connect();\n await client.commands.run(`mkdir -p \"${path}\"`);\n },\n\n readdir: async (sandbox: CodesandboxSandbox, path: string): Promise<FileEntry[]> => {\n // Connect to the sandbox client and use client.fs.readdir()\n const client = await sandbox.connect();\n const entries = await client.fs.readdir(path);\n\n return entries.map((entry: any) => ({\n name: entry.name,\n type: entry.isDirectory ? 'directory' as const : 'file' as const,\n size: entry.size || 0,\n modified: entry.lastModified ? new Date(entry.lastModified) : new Date()\n }));\n },\n\n exists: async (sandbox: CodesandboxSandbox, path: string): Promise<boolean> => {\n // CodeSandbox doesn't have a direct exists API, use ls command to check\n const client = await sandbox.connect();\n try {\n await client.commands.run(`ls \"${path}\"`);\n return true;\n } catch {\n return false;\n }\n },\n\n remove: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n // Connect to the sandbox client and use client.fs.remove()\n const client = await sandbox.connect();\n await client.fs.remove(path);\n }\n },\n\n // Provider-specific typed getInstance method\n getInstance: (sandbox: CodesandboxSandbox): CodesandboxSandbox => {\n return sandbox;\n },\n\n },\n\n snapshot: {\n create: async (config: CodesandboxConfig, sandboxId: string, options?: { name?: string }) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n // Resume the sandbox first, then hibernate to create a snapshot\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n\n // Hibernate creates a checkpoint/snapshot of the sandbox\n const hibernateSandbox = sandbox as HibernateCapableSandbox;\n await hibernateSandbox.hibernate();\n\n // The hibernated sandbox becomes a snapshot we can fork\n return {\n id: sandbox.id,\n provider: 'codesandbox',\n createdAt: new Date(),\n metadata: {\n name: options?.name,\n bootupType: sandbox.bootupType\n }\n };\n } catch (error) {\n throw new Error(\n `Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(\n `CodeSandbox provider does not support listing snapshots. Use the dashboard to manage snapshots.`\n );\n },\n\n delete: async (config: CodesandboxConfig, snapshotId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n\n if (!apiKey) {\n throw new Error(\n `Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`\n );\n }\n\n const sdk = new CodeSandbox(apiKey);\n\n try {\n // Shutdown the snapshot (hibernated sandbox)\n await sdk.sandboxes.shutdown(snapshotId);\n } catch (error) {\n // Ignore if not found\n }\n }\n },\n\n // Templates in CodeSandbox are handled via templateId on create\n template: {\n create: async (_config: CodesandboxConfig, _options: { name: string }) => {\n throw new Error(\n `CodeSandbox templates must be created via the CodeSandbox dashboard or CLI. Use templateId in sandbox.create() to specify a template.`\n );\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(\n `CodeSandbox provider does not support listing templates via API. Use the dashboard to manage templates.`\n );\n },\n\n delete: async (_config: CodesandboxConfig, _templateId: string) => {\n throw new Error(\n `CodeSandbox templates must be deleted via the CodeSandbox dashboard or CLI.`\n );\n }\n }\n }\n});\n"],"mappings":";AAMA,SAAS,mBAAmB;AAE5B,SAAS,gBAAgB,sBAAsB;AAyBxC,IAAM,cAAc,eAAgE;AAAA,EACzF,MAAM;AAAA,EACN,SAAS;AAAA,IACP,SAAS;AAAA;AAAA,MAEP,QAAQ,OAAO,QAA2B,YAAmC;AAE3E,cAAM,SAAS,OAAO,UAAW,OAAO,YAAY,eAAe,QAAQ,KAAK,eAAgB;AAEhG,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,cAAM,MAAM,IAAI,YAAY,MAAM;AAElC,YAAI;AACF,cAAI;AACJ,cAAI;AAEJ,cAAI,SAAS,YAAY;AAEvB,sBAAU,MAAM,IAAI,UAAU,OAAO,QAAQ,UAAU;AACvD,wBAAY,QAAQ;AAAA,UACtB,OAAO;AAEL,kBAAM;AAAA,cACJ,SAAS;AAAA,cACT,SAAS;AAAA,cACT;AAAA,cACA,MAAM;AAAA,cACN,UAAU;AAAA,cACV,YAAY;AAAA,cACZ,YAAY;AAAA,cACZ,WAAW;AAAA,cACX,WAAW;AAAA,cACX,WAAW;AAAA,cACX,GAAG;AAAA,YACL,IAAI,WAAW,CAAC;AAGhB,kBAAM,gBAAqB;AAAA,cACzB,GAAG;AAAA;AAAA,YACL;AAGA,kBAAM,aAAa,iBAAiB,OA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AA,UACF;AAAA,QACF,SAAS,OAAO;AACd,gBAAM,IAAI;AAAA,YACR,0CAA0C,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AAAA,UAClG;AAAA,QACF;AAAA,MACF;AAAA,MAEA,MAAM,OAAO,YAA+B;AAC1C,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,MAEA,QAAQ,OAAO,QAA2B,eAAuB;AAC/D,cAAM,SAAS,OAAO,UAAW,OAAO,YAAY,eAAe,QAAQ,KAAK,eAAgB;AAEhG,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF;AAEA,cAAM,MAAM,IAAI,YAAY,MAAM;AAElC,YAAI;AAEF,gBAAM,IAAI,UAAU,SAAS,UAAU;AAAA,QACzC,SAAS,OAAO;AAAA,QAEhB;AAAA,MACF;AAAA,IACF;AAAA;AAAA,IAGA,UAAU;AAAA,MACR,QAAQ,OAAO,SAA4B,aAA+B;AACxE,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,MAEA,MAAM,OAAO,YAA+B;AAC1C,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,MAEA,QAAQ,OAAO,SAA4B,gBAAwB;AACjE,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Codesandbox Provider - Factory-based Implementation\n */\n\nimport { CodeSandbox } from '@codesandbox/sdk';\nimport type { Sandbox as CodesandboxSandbox } from '@codesandbox/sdk';\nimport { defineProvider, escapeShellArg } from '@computesdk/provider';\n\nimport type { CommandResult, SandboxInfo, CreateSandboxOptions, FileEntry, RunCommandOptions } from '@computesdk/provider';\n\ntype HibernateCapableSandbox = CodesandboxSandbox & { hibernate: () => Promise<void>; };\n\nexport interface CodesandboxConfig {\n /** CodeSandbox API key - if not provided, will fallback to CSB_API_KEY environment variable */\n apiKey?: string;\n /** Template to use for new sandboxes */\n templateId?: string;\n /** Default runtime environment (e.g. 'node', 'python') */\n runtime?: string;\n /** Execution timeout in milliseconds */\n timeout?: number;\n}\n\nexport const codesandbox = defineProvider<CodesandboxSandbox, CodesandboxConfig, any, any>({\n name: 'codesandbox',\n methods: {\n sandbox: {\n create: async (config: CodesandboxConfig, options?: CreateSandboxOptions) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) {\n throw new Error(`Missing CodeSandbox API key. Provide 'apiKey' in config or set CSB_API_KEY environment variable.`);\n }\n const sdk = new CodeSandbox(apiKey);\n try {\n let sandbox: CodesandboxSandbox;\n let sandboxId: string;\n if (options?.snapshotId) {\n sandbox = await sdk.sandboxes.resume(options.snapshotId);\n sandboxId = options.snapshotId;\n } else {\n const {\n timeout: _timeout, envs, name: _name, metadata: _metadata,\n templateId: optTemplateId, snapshotId: _snapshotId, sandboxId: _sandboxId,\n namespace: _namespace, directory: _directory, ...providerOptions\n } = options || {};\n const createOptions: any = { ...providerOptions };\n const templateId = optTemplateId || config.templateId;\n if (templateId) createOptions.id = templateId;\n if (envs && Object.keys(envs).length > 0) createOptions.envVars = envs;\n sandbox = await sdk.sandboxes.create(createOptions);\n sandboxId = sandbox.id;\n }\n return { sandbox, sandboxId };\n } catch (error) {\n if (error instanceof Error) {\n if (error.message.includes('unauthorized') || error.message.includes('API key')) {\n throw new Error(`CodeSandbox authentication failed. Please check your CSB_API_KEY environment variable.`);\n }\n if (error.message.includes('quota') || error.message.includes('limit')) {\n throw new Error(`CodeSandbox quota exceeded. Please check your usage at https://codesandbox.io/dashboard`);\n }\n }\n throw new Error(`Failed to create CodeSandbox sandbox: ${error instanceof Error ? error.message : String(error)}`);\n }\n },\n\n getById: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try {\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n return { sandbox, sandboxId };\n } catch { return null; }\n },\n\n list: async (_config: CodesandboxConfig) => {\n throw new Error(`CodeSandbox provider does not support listing sandboxes.`);\n },\n\n destroy: async (config: CodesandboxConfig, sandboxId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try { await sdk.sandboxes.shutdown(sandboxId); } catch { /* ignore */ }\n try {\n await sdk.sandboxes.delete(sandboxId);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n if (!message.includes('not found') && !message.includes('404')) {\n throw new Error(`Failed to delete CodeSandbox sandbox \"${sandboxId}\": ${message}`);\n }\n }\n },\n\n runCommand: async (sandbox: CodesandboxSandbox, command: string, options?: RunCommandOptions): Promise<CommandResult> => {\n const startTime = Date.now();\n try {\n const client = await sandbox.connect();\n let fullCommand = command;\n if (options?.env && Object.keys(options.env).length > 0) {\n const envPrefix = Object.entries(options.env).map(([k, v]) => `${k}=\"${escapeShellArg(v)}\"`).join(' ');\n fullCommand = `${envPrefix} ${fullCommand}`;\n }\n if (options?.cwd) fullCommand = `cd \"${escapeShellArg(options.cwd)}\" && ${fullCommand}`;\n if (options?.background) fullCommand = `nohup ${fullCommand} > /dev/null 2>&1 &`;\n const output = await client.commands.run(fullCommand);\n return { stdout: output, stderr: '', exitCode: 0, durationMs: Date.now() - startTime };\n } catch (error) {\n return { stdout: '', stderr: error instanceof Error ? error.message : String(error), exitCode: 127, durationMs: Date.now() - startTime };\n }\n },\n\n getInfo: async (sandbox: CodesandboxSandbox): Promise<SandboxInfo> => ({\n id: sandbox.id,\n provider: 'codesandbox',\n status: 'running',\n createdAt: new Date(),\n timeout: 300000,\n metadata: { cluster: sandbox.cluster, bootupType: sandbox.bootupType, isUpToDate: sandbox.isUpToDate }\n }),\n\n getUrl: async (sandbox: CodesandboxSandbox, options: { port: number; protocol?: string }): Promise<string> => {\n const protocol = options.protocol || 'https';\n return `${protocol}://${sandbox.id}.${sandbox.cluster}.csb.app:${options.port}`;\n },\n\n filesystem: {\n readFile: async (sandbox: CodesandboxSandbox, path: string): Promise<string> => {\n const client = await sandbox.connect();\n return client.fs.readTextFile(path);\n },\n writeFile: async (sandbox: CodesandboxSandbox, path: string, content: string): Promise<void> => {\n const client = await sandbox.connect();\n await client.fs.writeTextFile(path, content);\n },\n mkdir: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n const client = await sandbox.connect();\n await client.commands.run(`mkdir -p \"${path}\"`);\n },\n readdir: async (sandbox: CodesandboxSandbox, path: string): Promise<FileEntry[]> => {\n const client = await sandbox.connect();\n const entries = await client.fs.readdir(path);\n return entries.map((entry: any) => ({\n name: entry.name,\n type: entry.isDirectory ? 'directory' as const : 'file' as const,\n size: entry.size || 0,\n modified: entry.lastModified ? new Date(entry.lastModified) : new Date()\n }));\n },\n exists: async (sandbox: CodesandboxSandbox, path: string): Promise<boolean> => {\n const client = await sandbox.connect();\n try { await client.commands.run(`ls \"${path}\"`); return true; } catch { return false; }\n },\n remove: async (sandbox: CodesandboxSandbox, path: string): Promise<void> => {\n const client = await sandbox.connect();\n await client.fs.remove(path);\n }\n },\n\n getInstance: (sandbox: CodesandboxSandbox): CodesandboxSandbox => sandbox,\n },\n\n snapshot: {\n create: async (config: CodesandboxConfig, sandboxId: string, options?: { name?: string }) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try {\n const sandbox = await sdk.sandboxes.resume(sandboxId);\n await (sandbox as HibernateCapableSandbox).hibernate();\n return { id: sandbox.id, provider: 'codesandbox', createdAt: new Date(), metadata: { name: options?.name, bootupType: sandbox.bootupType } };\n } catch (error) {\n throw new Error(`Failed to create CodeSandbox snapshot: ${error instanceof Error ? error.message : String(error)}`);\n }\n },\n list: async (_config: CodesandboxConfig) => { throw new Error(`CodeSandbox provider does not support listing snapshots.`); },\n delete: async (config: CodesandboxConfig, snapshotId: string) => {\n const apiKey = config.apiKey || (typeof process !== 'undefined' && process.env?.CSB_API_KEY) || '';\n if (!apiKey) throw new Error(`Missing CodeSandbox API key.`);\n const sdk = new CodeSandbox(apiKey);\n try { await sdk.sandboxes.shutdown(snapshotId); } catch { /* ignore */ }\n }\n },\n\n template: {\n create: async (_config: CodesandboxConfig, _options: { name: string }) => {\n throw new Error(`CodeSandbox templates must be created via the dashboard.`);\n },\n list: async (_config: CodesandboxConfig) => { throw new Error(`CodeSandbox provider does not support listing templates via API.`); },\n delete: async (_config: CodesandboxConfig, _templateId: string) => { throw new Error(`CodeSandbox templates must be deleted via the dashboard.`); }\n }\n }\n});\n"],"mappings":";AAIA,SAAS,mBAAmB;AAE5B,SAAS,gBAAgB,sBAAsB;AAiBxC,IAAM,cAAc,eAAgE;AAAA,EACzF,MAAM;AAAA,EACN,SAAS;AAAA,IACP,SAAS;AAAA,MACP,QAAQ,OAAO,QAA2B,YAAmC;AAC3E,cAAM,SAAS,OAAO,UAAW,OAAO,YAAY,eAAe,QAAQ,KAAK,eAAgB;AAChG,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI,MAAM,kGAAkG;AAAA,QACpH;AACA,cAAM,MAAM,IAAI,YAAY,MAAM;AAClC,YAAI;AACF,cAAI;AACJ,cAAI;AACJ,cAAI,SAAS,YAAY;AACvB,sBAAU,MAAM,IAAI,UAAU,OAAO,QAAQ,UAAU;AACvD,wBAAY,QAAQ;AAAA,UACtB,OAAO;AACL,kBAAM;AAAA,cACJ,SAAS;AAAA,cAAU;AAAA,cAAM,MAAM;AAAA,cAAO,UAAU;AAAA,cAChD,YAAY;AAAA,cAAe,YAAY;AAAA,cAAa,WAAW;AAAA,cAC/D,WAAW;AA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Q,KAAK,eAAgB;AAChG,YAAI,CAAC,OAAQ,OAAM,IAAI,MAAM,8BAA8B;AAC3D,cAAM,MAAM,IAAI,YAAY,MAAM;AAClC,YAAI;AAAE,gBAAM,IAAI,UAAU,SAAS,UAAU;AAAA,QAAG,QAAQ;AAAA,QAAe;AAAA,MACzE;AAAA,IACF;AAAA,IAEA,UAAU;AAAA,MACR,QAAQ,OAAO,SAA4B,aAA+B;AACxE,cAAM,IAAI,MAAM,0DAA0D;AAAA,MAC5E;AAAA,MACA,MAAM,OAAO,YAA+B;AAAE,cAAM,IAAI,MAAM,kEAAkE;AAAA,MAAG;AAAA,MACnI,QAAQ,OAAO,SAA4B,gBAAwB;AAAE,cAAM,IAAI,MAAM,0DAA0D;AAAA,MAAG;AAAA,IACpJ;AAAA,EACF;AACF,CAAC;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@computesdk/codesandbox",
3
- "version": "1.5.45",
3
+ "version": "1.5.46",
4
4
  "description": "CodeSandbox provider for ComputeSDK - fast browser-compatible sandboxes with npm and Python support",
5
5
  "author": "Garrison",
6
6
  "license": "MIT",
@@ -19,8 +19,8 @@
19
19
  ],
20
20
  "dependencies": {
21
21
  "@codesandbox/sdk": "^2.4.2",
22
- "@computesdk/provider": "1.4.0",
23
- "computesdk": "3.0.0"
22
+ "@computesdk/provider": "2.0.0",
23
+ "computesdk": "4.0.0"
24
24
  },
25
25
  "keywords": [
26
26
  "computesdk",
@@ -51,7 +51,7 @@
51
51
  "tsup": "^8.0.0",
52
52
  "typescript": "^5.0.0",
53
53
  "vitest": "^1.0.0",
54
- "@computesdk/test-utils": "1.6.1"
54
+ "@computesdk/test-utils": "2.0.0"
55
55
  },
56
56
  "scripts": {
57
57
  "build": "tsup",