Skip to content

Commit 28e46d8

Browse files
authored
chore: using existing provider method (#2826)
1 parent fe4b1fd commit 28e46d8

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

apps/web/client/src/server/api/routers/project/sandbox.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,10 @@ export const sandboxRouter = createTRPCRouter({
156156

157157
for (let attempt = 1; attempt <= MAX_RETRY_ATTEMPTS; attempt++) {
158158
try {
159-
const { CodeSandbox } = await import('@codesandbox/sdk');
160-
const sdk = new CodeSandbox();
161-
162-
const sandbox = await sdk.sandboxes.create({
163-
source: 'git',
164-
url: input.repoUrl,
159+
const CodesandboxProvider = await getStaticCodeProvider(CodeProvider.CodeSandbox);
160+
const sandbox = await CodesandboxProvider.createProjectFromGit({
161+
repoUrl: input.repoUrl,
165162
branch: input.branch,
166-
async setup(session) {
167-
await session.setup.run();
168-
},
169163
});
170164

171165
const previewUrl = getSandboxPreviewUrl(sandbox.id, DEFAULT_PORT);

packages/code-provider/src/providers/codesandbox/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,33 @@ export class CodesandboxProvider extends Provider {
171171
};
172172
}
173173

174+
static async createProjectFromGit(input: {
175+
repoUrl: string;
176+
branch: string;
177+
}): Promise<CreateProjectOutput> {
178+
const sdk = new CodeSandbox();
179+
const TIMEOUT_MS = 30000;
180+
181+
const createPromise = sdk.sandboxes.create({
182+
source: 'git',
183+
url: input.repoUrl,
184+
branch: input.branch,
185+
async setup(session) {
186+
await session.setup.run();
187+
},
188+
});
189+
190+
const timeoutPromise = new Promise<never>((_, reject) => {
191+
setTimeout(() => reject(new Error('Repository access timeout')), TIMEOUT_MS);
192+
});
193+
194+
const newSandbox = await Promise.race([createPromise, timeoutPromise]);
195+
196+
return {
197+
id: newSandbox.id,
198+
};
199+
}
200+
174201
async pauseProject(input: PauseProjectInput): Promise<PauseProjectOutput> {
175202
if (this.sandbox && this.options.sandboxId) {
176203
const sdk = new CodeSandbox();

packages/code-provider/src/providers/nodefs/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ export class NodeFsProvider extends Provider {
177177
};
178178
}
179179

180+
static async createProjectFromGit(input: {
181+
repoUrl: string;
182+
branch: string;
183+
}): Promise<CreateProjectOutput> {
184+
throw new Error('createProjectFromGit not implemented for NodeFs provider');
185+
}
186+
180187
async pauseProject(input: PauseProjectInput): Promise<PauseProjectOutput> {
181188
return {};
182189
}

packages/code-provider/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ export abstract class Provider {
220220
static createProject(input: CreateProjectInput): Promise<CreateProjectOutput> {
221221
throw new Error('createProject must be implemented by subclass');
222222
}
223+
static createProjectFromGit(input: {
224+
repoUrl: string;
225+
branch: string;
226+
}): Promise<CreateProjectOutput> {
227+
throw new Error('createProjectFromGit must be implemented by subclass');
228+
}
223229
abstract pauseProject(input: PauseProjectInput): Promise<PauseProjectOutput>;
224230
abstract stopProject(input: StopProjectInput): Promise<StopProjectOutput>;
225231
abstract listProjects(input: ListProjectsInput): Promise<ListProjectsOutput>;

0 commit comments

Comments
 (0)