Skip to content

Commit 43d378d

Browse files
documenting
1 parent f472813 commit 43d378d

File tree

11 files changed

+144
-151
lines changed

11 files changed

+144
-151
lines changed

src/PreviewTokens.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,16 @@ export class PreviewTokens extends Disposable {
3535

3636
/**
3737
* Get a signed preview URL for a port using a preview token.
38-
*
39-
* @param port - The port to get a signed preview URL for
40-
* @param token - The preview token to sign the URL with
41-
* @returns The signed preview URL, or undefined if the port is not open
4238
*/
43-
getSignedPreviewUrl(token: PreviewToken, port: number): string {
39+
getSignedPreviewUrl(
40+
token: { sandboxId: string; token: string },
41+
port: number
42+
): string {
4443
return `https://${token.sandboxId}-${port}.csb.app?preview_token=${token.token}`;
4544
}
4645

4746
/**
48-
* Generate a new preview token that can be used to access private sandbox previews.
49-
*
50-
* @param opts - Options
51-
* @param opts.expiresAt - Optional expiration date for the preview token
52-
* @returns A preview token that can be used with Ports.getSignedPreviewUrl
47+
* Generate a new preview token that can be used to access private sandbox previews. By default the token never expires.
5348
*/
5449
async create(
5550
sandboxId: string,
@@ -87,8 +82,6 @@ export class PreviewTokens extends Disposable {
8782

8883
/**
8984
* List all active preview tokens for this sandbox.
90-
*
91-
* @returns A list of preview tokens
9285
*/
9386
async list(sandboxId: string): Promise<PreviewTokenInfo[]> {
9487
const response = handleResponse(
@@ -115,8 +108,6 @@ export class PreviewTokens extends Disposable {
115108

116109
/**
117110
* Revoke a single preview token for this sandbox.
118-
*
119-
* @param tokenId - The ID of the token to revoke
120111
*/
121112
async revoke(sandboxId: string, tokenId: string): Promise<void> {
122113
handleResponse(
@@ -153,10 +144,6 @@ export class PreviewTokens extends Disposable {
153144

154145
/**
155146
* Update a preview token's expiration date.
156-
*
157-
* @param tokenId - The ID of the token to update
158-
* @param expiresAt - The new expiration date for the token (null for no expiration)
159-
* @returns The updated preview token info
160147
*/
161148
async update(
162149
sandboxId: string,

src/SandboxClient.ts renamed to src/Sandboxes.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export async function startVm(
5656
return getStartResponse(response);
5757
}
5858

59-
export class SandboxClient {
59+
/**
60+
* This class provides methods for creating and managing sandboxes.
61+
*/
62+
export class Sandboxes {
6063
get defaultTemplateId() {
6164
return getDefaultTemplateId(this.apiClient);
6265
}
@@ -139,7 +142,13 @@ export class SandboxClient {
139142
}
140143

141144
/**
145+
* Resume a sandbox.
142146
*
147+
* - Hibernated with snapshot: It wakes up and continues within 2-3 seconds
148+
* - Hibernated with expired snapshot: It will start from scratch (CLEAN bootup)
149+
* - Shutdown: It will start from scratch (CLEAN bootup)
150+
*
151+
* Note! On CLEAN bootups the setup will run again. When hibernated a new snapshot will be created.
143152
*/
144153
async resume(sandboxId: string) {
145154
const startResponse = await startVm(this.apiClient, sandboxId);
@@ -148,8 +157,6 @@ export class SandboxClient {
148157

149158
/**
150159
* Shuts down a sandbox. Files will be saved, and the sandbox will be stopped.
151-
*
152-
* @param sandboxId The ID of the sandbox to shutdown
153160
*/
154161
async shutdown(sandboxId: string): Promise<void> {
155162
const response = await vmShutdown({
@@ -166,7 +173,7 @@ export class SandboxClient {
166173
* Restart the sandbox. This will shutdown the sandbox, and then start it again. Files in
167174
* the project directory (`/project/sandbox`) will be preserved.
168175
*
169-
* Will resolve once the sandbox is rebooted.
176+
* Will resolve once the sandbox is restarted with its setup running.
170177
*/
171178
public async restart(sandboxId: string, opts?: StartSandboxOpts) {
172179
await this.shutdown(sandboxId);
@@ -177,9 +184,7 @@ export class SandboxClient {
177184

178185
/**
179186
* Hibernates a sandbox. Files will be saved, and the sandbox will be put to sleep. Next time
180-
* you start the sandbox it will be resumed from the last state it was in.
181-
*
182-
* @param sandboxId The ID of the sandbox to hibernate
187+
* you resume the sandbox it will continue from the last state it was in.
183188
*/
184189
async hibernate(sandboxId: string): Promise<void> {
185190
const response = await vmHibernate({
@@ -193,16 +198,7 @@ export class SandboxClient {
193198
}
194199

195200
/**
196-
* Creates a sandbox by forking a template. You can pass in any template or sandbox id (from
197-
* any sandbox/template created on codesandbox.io, even your own templates) or don't pass
198-
* in anything and we'll use the default universal template.
199-
*
200-
* This function will also start & connect to the VM of the created sandbox with a global session, and return a {@link Sandbox}
201-
* that allows you to control the VM. Pass "autoConnect: false" to only return the session data.
202-
*
203-
* @param opts Additional options for creating the sandbox
204-
*
205-
* @returns A promise that resolves to a {@link Sandbox}, which you can use to control the VM
201+
* Create a sandbox from a template or git repository. By default we will create a sandbox from the default template.
206202
*/
207203
async create(
208204
opts: CreateSandboxOpts & StartSandboxOpts = { source: "template" }

src/browser.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,7 @@ export * from "./sessions/WebSocketSession";
77
export { createPreview, Preview } from "./previews";
88

99
/**
10-
* With this function you can connect to a sandbox from the browser.
11-
*
12-
* ## Why does this exist?
13-
*
14-
* The CodeSandbox API is a REST API that you can use to control sandboxes. However, it
15-
* requires your CodeSandbox API token to be sent with every request. This makes it
16-
* unsafe to use from the browser, where you don't want to expose your API token.
17-
*
18-
* With this helper function, you can generate a sandbox on the server, and then share a single-use
19-
* token that can be used to create a connection to that sandbox from the browser.
20-
*
21-
* ## Example
22-
*
23-
* To use this function, you first need to start a sandbox on the server:
24-
*
25-
* ```ts
26-
* import { CodeSandbox } from "@codesandbox/sdk";
27-
*
28-
* const client = new CodeSandbox(apiToken);
29-
*
30-
* const startData = await client.sandbox.start("my-sandbox-id");
31-
* ```
32-
*
33-
* Then you can start a sandbox using this start data in the browser:
34-
*
35-
* ```ts
36-
* import { connectToSandbox } from "@codesandbox/sdk/browser";
37-
*
38-
* // Get the start data from the server
39-
* const startData = ...;
40-
*
41-
* const sandbox = await connectToSandbox(startData);
42-
* ```
10+
* Connect to a Sandbox from the browser and automatically reconnect. `getSession` requires and endpoint that resumes the Sandbox. `onFocusChange` can be used to notify when a reconnect should happen.
4311
*/
4412
export async function connectToSandbox(options: {
4513
id: string;

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { SandboxClient } from "./SandboxClient";
1+
import { Sandboxes } from "./Sandboxes";
22
import { ClientOpts } from "./types";
33

4-
export { SandboxClient };
4+
export { Sandboxes as SandboxClient };
55

66
export { VMTier } from "./VMTier";
77

@@ -24,11 +24,11 @@ function ensure<T>(value: T | undefined, message: string): T {
2424
}
2525

2626
export class CodeSandbox {
27-
public readonly sandboxes: SandboxClient;
27+
public readonly sandboxes: Sandboxes;
2828
/**
2929
* @deprecated Use `sandboxes` instead
3030
*/
31-
public readonly sandbox: SandboxClient;
31+
public readonly sandbox: Sandboxes;
3232

3333
/**
3434
* Provider for generating preview tokens. These tokens can be used to generate signed
@@ -67,7 +67,7 @@ export class CodeSandbox {
6767
})
6868
);
6969

70-
this.sandboxes = new SandboxClient(apiClient);
70+
this.sandboxes = new Sandboxes(apiClient);
7171
this.sandbox = this.sandboxes;
7272
this.previewTokens = new PreviewTokens(apiClient);
7373
}

src/previews/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88

99
export { Preview, InjectFunction };
1010

11+
/**
12+
* Create a preview that you can interact with. By default you can interact with navigation, but you can also inject your own code and do custom message passing. Append the `iframe` to your DOM to display the preview.
13+
*/
1114
export function createPreview<
1215
MessageToPreview extends Message = BaseMessageToPreview,
1316
MessageFromPreview extends Message = BaseMessageFromPreview

src/sandbox.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,31 @@ import { handleResponse } from "./utils/api";
2020
import { VMTier } from "./VMTier";
2121
import { WebSocketSession } from "./sessions/WebSocketSession";
2222
import { RestSession } from "./sessions/RestSession";
23-
import { SandboxClient, startVm } from "./SandboxClient";
23+
import { Sandboxes, startVm } from "./Sandboxes";
2424

2525
export class Sandbox {
26+
/**
27+
* How the Sandbox booted up:
28+
* - RUNNING: Already running
29+
* - RESUME: Resumes from hibernation
30+
* - CLEAN: Clean bootup, no hibernation snapshot or shutdown
31+
* - FORK: When the sandbox was created from a template
32+
*/
2633
get bootupType() {
2734
return this.pitcherManagerResponse.bootupType;
2835
}
36+
37+
/**
38+
* The cluster the Sandbox is running on.
39+
*/
2940
get cluster() {
3041
return this.pitcherManagerResponse.cluster;
3142
}
43+
44+
/**
45+
* Whether the Sandbox Agent version is up to date. Use "restart" to
46+
* update the agent.
47+
*/
3248
get isUpToDate() {
3349
return (
3450
this.pitcherManagerResponse.latestPitcherVersion ===
@@ -53,9 +69,6 @@ export class Sandbox {
5369
* Updates the specs that this sandbox runs on. It will dynamically scale the sandbox to the
5470
* new specs without a reboot. Be careful when scaling specs down, if the VM is using more memory
5571
* than it can scale down to, it can become very slow.
56-
*
57-
* @param id The ID of the sandbox to update
58-
* @param tier The new VM tier
5972
*/
6073
async updateTier(sandboxId: string, tier: VMTier): Promise<void> {
6174
const response = await vmUpdateSpecs({
@@ -69,6 +82,10 @@ export class Sandbox {
6982
handleResponse(response, `Failed to update sandbox tier ${sandboxId}`);
7083
}
7184

85+
/**
86+
* Updates the hibernation timeout for this sandbox. This is the amount of seconds the sandbox
87+
* will be kept alive without activity before it is automatically hibernated. Activity can be sessions or interactions with any endpoints exposed by the Sandbox.
88+
*/
7289
async updateHibernationTimeout(
7390
sandboxId: string,
7491
timeoutSeconds: number
@@ -122,6 +139,9 @@ export class Sandbox {
122139
return session;
123140
}
124141

142+
/**
143+
* Connects to the Sandbox using a WebSocket connection, allowing you to interact with it. You can pass a custom session to connect to a specific user workspace, controlling permissions, git credentials and environment variables.
144+
*/
125145
async connect(
126146
customSession?: SessionCreateOptions
127147
): Promise<WebSocketSession> {
@@ -189,6 +209,9 @@ export class Sandbox {
189209
return new WebSocketSession(pitcherClient, () => customSession?.env ?? {});
190210
}
191211

212+
/**
213+
* Returns a REST API client connected to this Sandbox, allowing you to interact with it. You can pass a custom session to connect to a specific user workspace, controlling permissions, git credentials and environment variables.
214+
*/
192215
async createRestSession(customSession?: SessionCreateOptions) {
193216
const session = customSession
194217
? await this.createSession(customSession)
@@ -197,6 +220,9 @@ export class Sandbox {
197220
return new RestSession(session);
198221
}
199222

223+
/**
224+
* Returns a browser session connected to this Sandbox, allowing you to interact with it. You can pass a custom session to connect to a specific user workspace, controlling permissions, git credentials and environment variables.
225+
*/
200226
async createBrowserSession(
201227
customSession?: SessionCreateOptions
202228
): Promise<SandboxBrowserSession> {

0 commit comments

Comments
 (0)