Skip to content

Commit c446d01

Browse files
ready...
1 parent c471b06 commit c446d01

File tree

6 files changed

+66
-29
lines changed

6 files changed

+66
-29
lines changed

src/SandboxClient.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class SandboxClient {
4545
id: this.defaultTemplateId,
4646
});
4747

48-
const client = await sandbox.connect(
48+
const session = await sandbox.connect(
4949
// We do not want users to pass gitAccessToken on global user, because it
5050
// can be read by other users
5151
opts.gitAccessToken
@@ -56,7 +56,7 @@ export class SandboxClient {
5656
: undefined
5757
);
5858

59-
await client.shells.run(
59+
await session.shells.run(
6060
[
6161
"rm -rf .git",
6262
"git init",
@@ -67,7 +67,9 @@ export class SandboxClient {
6767
].join("&&")
6868
);
6969

70-
client.disconnect();
70+
await opts.setup?.(session);
71+
72+
session.disconnect();
7173

7274
return sandbox;
7375
}
@@ -206,9 +208,6 @@ export class SandboxClient {
206208
case "git": {
207209
return this.createGitSandbox(opts);
208210
}
209-
case "files": {
210-
throw new Error("Not implemented");
211-
}
212211
case "template": {
213212
return this.createTemplateSandbox(opts);
214213
}

src/bin/commands/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const buildCommand: yargs.CommandModule<
143143
const spinner = createSpinner();
144144

145145
function createSpinnerMessage(message: string, sandboxId?: string) {
146-
return `[cluster: ${slug}, sandbox: ${
146+
return `[cluster: ${slug}, sandboxId: ${
147147
sandboxId || "-"
148148
}]: ${message}`;
149149
}

src/previews/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { Preview } from "./Preview";
2-
import { InjectFunction } from "./types";
2+
import {
3+
BaseMessageFromPreview,
4+
BaseMessageToPreview,
5+
InjectFunction,
6+
Message,
7+
} from "./types";
38

49
export { Preview, InjectFunction };
510

6-
export function createPreview(src: string) {
7-
return new Preview(src);
11+
export function createPreview<
12+
MessageToPreview extends Message = BaseMessageToPreview,
13+
MessageFromPreview extends Message = BaseMessageFromPreview
14+
>(src: string) {
15+
return new Preview<MessageToPreview, MessageFromPreview>(src);
816
}

src/sessions/WebSocketSession/tasks.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Emitter,
3+
IDisposable,
34
type IPitcherClient,
45
type protocol,
56
} from "@codesandbox/pitcher-client";
@@ -151,6 +152,34 @@ export class Task {
151152

152153
return this.openedShell.output.join("\n");
153154
}
155+
async waitForPort(timeout: number = 30_000) {
156+
if (this.ports.length) {
157+
return this.ports[0];
158+
}
159+
160+
let disposer: IDisposable | undefined;
161+
162+
return Promise.all([
163+
new Promise<protocol.port.Port>((resolve) => {
164+
disposer = this.pitcherClient.clients.task.onTaskUpdate((task) => {
165+
if (task.id !== this.id) {
166+
return;
167+
}
168+
169+
if (task.ports.length) {
170+
disposer?.dispose();
171+
resolve(task.ports[0]);
172+
}
173+
});
174+
}),
175+
new Promise<protocol.port.Port>((resolve, reject) => {
176+
setTimeout(() => {
177+
disposer?.dispose();
178+
reject(new Error("Timeout waiting for port"));
179+
}, timeout);
180+
}),
181+
]);
182+
}
154183
async run() {
155184
await this.pitcherClient.clients.task.runTask(this.id);
156185
}

src/sessions/WebSocketSession/terminals.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ export class Terminals {
3333
return new Terminal(shell, this.pitcherClient);
3434
}
3535

36-
/**
37-
* Opens an existing terminal.
38-
*/
39-
async open(
40-
shellId: string,
41-
dimensions = DEFAULT_SHELL_SIZE
42-
): Promise<Terminal> {
43-
const shell = await this.pitcherClient.clients.shell.open(
44-
shellId as Id,
45-
dimensions
46-
);
36+
get(shellId: string) {
37+
const shell = this.pitcherClient.clients.shell
38+
.getShells()
39+
.find((shell) => shell.shellId === shellId);
40+
41+
if (!shell) {
42+
return;
43+
}
44+
4745
return new Terminal(shell, this.pitcherClient);
4846
}
4947

@@ -101,7 +99,14 @@ export class Terminal {
10199
);
102100
}
103101

104-
getOutput(): string {
102+
async open(dimensions = DEFAULT_SHELL_SIZE): Promise<string> {
103+
const shell = await this.pitcherClient.clients.shell.open(
104+
this.shell.shellId,
105+
dimensions
106+
);
107+
108+
this.output = shell.buffer;
109+
105110
return this.output.join("\n");
106111
}
107112

src/types.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PitcherManagerResponse } from "@codesandbox/pitcher-client";
22
import { VMTier } from "./VMTier";
3+
import type { WebSocketSession } from "./sessions/WebSocketSession";
34

45
export interface SystemMetricsStatus {
56
cpu: {
@@ -206,17 +207,12 @@ export type CreateSandboxGitSourceOpts = CreateSandboxBaseOpts & {
206207
url: string;
207208
branch: string;
208209
gitAccessToken?: string;
209-
};
210-
211-
export type CreateSandboxFilesSourceOpts = CreateSandboxBaseOpts & {
212-
source: "files";
213-
files: Record<string, string>;
210+
setup?: (session: WebSocketSession) => Promise<void>;
214211
};
215212

216213
export type CreateSandboxOpts =
217214
| CreateSandboxTemplateSourceOpts
218-
| CreateSandboxGitSourceOpts
219-
| CreateSandboxFilesSourceOpts;
215+
| CreateSandboxGitSourceOpts;
220216

221217
export type SandboxOpts = {
222218
id: string;

0 commit comments

Comments
 (0)