Skip to content

Commit edfad0b

Browse files
fix preview tokens
1 parent 43d378d commit edfad0b

File tree

8 files changed

+57
-32
lines changed

8 files changed

+57
-32
lines changed

src/bin/commands/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { getDefaultTemplateId, handleResponse } from "../../utils/api";
2020
import { BASE_URL, getApiKey } from "../utils/constants";
2121
import { hashDirectory } from "../utils/hash";
22-
import { startVm } from "../../SandboxClient";
22+
import { startVm } from "../../Sandboxes";
2323

2424
export type BuildCommandArgs = {
2525
directory: string;

src/browser.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,27 @@ export { createPreview, Preview } from "./previews";
1010
* 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.
1111
*/
1212
export async function connectToSandbox(options: {
13-
id: string;
13+
session: SandboxBrowserSession;
1414
getSession: (id: string) => Promise<SandboxBrowserSession>;
1515
onFocusChange?: (cb: (isFocused: boolean) => void) => () => void;
1616
initStatusCb?: (event: protocol.system.InitStatus) => void;
1717
}): Promise<WebSocketSession> {
18-
let env: Record<string, string> = {};
19-
18+
let hasConnected = false;
2019
const pitcherClient = await initPitcherClient(
2120
{
2221
appId: "sdk",
23-
instanceId: options.id,
22+
instanceId: options.session.id,
2423
onFocusChange:
2524
options.onFocusChange ||
2625
(() => {
2726
return () => {};
2827
}),
2928
requestPitcherInstance: async (id) => {
30-
const session = await options.getSession(id);
29+
const session = hasConnected
30+
? await options.getSession(id)
31+
: options.session;
3132

32-
if (session.env) {
33-
env = session.env;
34-
}
33+
hasConnected = true;
3534

3635
return session;
3736
},
@@ -40,5 +39,8 @@ export async function connectToSandbox(options: {
4039
options.initStatusCb || (() => {})
4140
);
4241

43-
return new WebSocketSession(pitcherClient, () => env);
42+
return new WebSocketSession(pitcherClient, {
43+
env: options.session.env,
44+
previewToken: options.session.previewToken,
45+
});
4446
}

src/sandbox.ts

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

2525
export class Sandbox {
2626
/**
@@ -146,6 +146,10 @@ export class Sandbox {
146146
customSession?: SessionCreateOptions
147147
): Promise<WebSocketSession> {
148148
let hasConnected = false;
149+
const session = customSession
150+
? await this.createSession(customSession)
151+
: this.globalSession;
152+
149153
const pitcherClient = await initPitcherClient(
150154
{
151155
appId: "sdk",
@@ -162,10 +166,6 @@ export class Sandbox {
162166
);
163167
}
164168

165-
const session = customSession
166-
? await this.createSession(customSession)
167-
: this.globalSession;
168-
169169
const headers = this.apiClient.getConfig().headers as Headers;
170170

171171
if (headers.get("x-pitcher-manager-url")) {
@@ -206,7 +206,10 @@ export class Sandbox {
206206
() => {}
207207
);
208208

209-
return new WebSocketSession(pitcherClient, () => customSession?.env ?? {});
209+
return new WebSocketSession(pitcherClient, {
210+
env: customSession?.env,
211+
previewToken: customSession?.previewToken,
212+
});
210213
}
211214

212215
/**

src/sessions/WebSocketSession/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Commands {
2626
constructor(
2727
sessionDisposable: Disposable,
2828
private pitcherClient: IPitcherClient,
29-
private getEnv: () => Record<string, string>
29+
private env: Record<string, string> = {}
3030
) {
3131
sessionDisposable.onWillDispose(() => {
3232
this.disposable.dispose();
@@ -43,7 +43,7 @@ export class Commands {
4343

4444
command = Array.isArray(command) ? command.join(" && ") : command;
4545

46-
const allEnv = Object.assign(this.getEnv(), opts?.env ?? {});
46+
const allEnv = Object.assign(this.env, opts?.env ?? {});
4747

4848
// TODO: use a new shell API that natively supports cwd & env
4949
let commandWithEnv = Object.keys(allEnv).length

src/sessions/WebSocketSession/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Interpreters } from "./interpreters";
1212
import { Terminals } from "./terminals";
1313
import { Commands } from "./commands";
1414
import { Git } from "./git";
15+
import { PreviewToken } from "../../PreviewTokens";
1516

1617
export * from "./filesystem";
1718
export * from "./ports";
@@ -53,7 +54,7 @@ export class WebSocketSession {
5354
/**
5455
* Namespace for managing ports on this Sandbox
5556
*/
56-
public readonly ports = new Ports(this.disposable, this.pitcherClient);
57+
public readonly ports: Ports;
5758

5859
/**
5960
* Namespace for the setup that runs when the Sandbox starts from scratch.
@@ -67,7 +68,10 @@ export class WebSocketSession {
6768

6869
constructor(
6970
protected pitcherClient: IPitcherClient,
70-
getEnv: () => Record<string, string>
71+
{
72+
env,
73+
previewToken,
74+
}: { env?: Record<string, string>; previewToken?: PreviewToken }
7175
) {
7276
// TODO: Bring this back once metrics polling does not reset inactivity
7377
// const metricsDisposable = {
@@ -76,8 +80,9 @@ export class WebSocketSession {
7680
// };
7781

7882
// this.addDisposable(metricsDisposable);
79-
this.terminals = new Terminals(this.disposable, this.pitcherClient, getEnv);
80-
this.commands = new Commands(this.disposable, this.pitcherClient, getEnv);
83+
this.terminals = new Terminals(this.disposable, this.pitcherClient, env);
84+
this.commands = new Commands(this.disposable, this.pitcherClient, env);
85+
this.ports = new Ports(this.disposable, this.pitcherClient, previewToken);
8186
this.interpreters = new Interpreters(this.disposable, this.commands);
8287
this.disposable.addDisposable(this.pitcherClient);
8388
}

src/sessions/WebSocketSession/ports.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import type { IPitcherClient } from "@codesandbox/pitcher-client";
22

33
import { Disposable } from "../../utils/disposable";
44
import { Emitter } from "../../utils/event";
5-
6-
export type Port = {
7-
port: number;
8-
host: string;
9-
};
5+
import { PreviewToken } from "../../PreviewTokens";
106

117
export class Ports {
128
private disposable = new Disposable();
@@ -27,7 +23,8 @@ export class Ports {
2723

2824
constructor(
2925
sessionDisposable: Disposable,
30-
private pitcherClient: IPitcherClient
26+
private pitcherClient: IPitcherClient,
27+
private previewToken?: PreviewToken
3128
) {
3229
sessionDisposable.onWillDispose(() => {
3330
this.disposable.dispose();
@@ -49,7 +46,9 @@ export class Ports {
4946

5047
if (openedPorts.length) {
5148
for (const port of openedPorts) {
52-
this.onDidPortOpenEmitter.fire({ port: port.port, host: port.url });
49+
this.onDidPortOpenEmitter.fire(
50+
new Port(port.port, port.url, this.previewToken)
51+
);
5352
}
5453
}
5554

@@ -71,7 +70,7 @@ export class Ports {
7170
getOpenedPorts(): Port[] {
7271
return this.pitcherClient.clients.port
7372
.getPorts()
74-
.map(({ port, url }) => ({ host: url, port }));
73+
.map(({ port, url }) => new Port(port, url, this.previewToken));
7574
}
7675

7776
/**
@@ -125,3 +124,16 @@ export class Ports {
125124
});
126125
}
127126
}
127+
128+
class Port {
129+
constructor(
130+
public readonly port: number,
131+
public readonly host: string,
132+
private previewToken?: PreviewToken
133+
) {}
134+
getPreviewUrl() {
135+
return `https://${this.host}${
136+
this.previewToken ? `?preview_token=${this.previewToken.token}` : ""
137+
}`;
138+
}
139+
}

src/sessions/WebSocketSession/terminals.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class Terminals {
1313
constructor(
1414
sessionDisposable: Disposable,
1515
private pitcherClient: IPitcherClient,
16-
private getEnv: () => Record<string, string>
16+
private env: Record<string, string> = {}
1717
) {
1818
sessionDisposable.onWillDispose(() => {
1919
this.disposable.dispose();
@@ -24,7 +24,7 @@ export class Terminals {
2424
command: "bash" | "zsh" | "fish" | "ksh" | "dash" = "bash",
2525
opts?: ShellRunOpts
2626
): Promise<Terminal> {
27-
const allEnv = Object.assign(this.getEnv(), opts?.env ?? {});
27+
const allEnv = Object.assign(this.env, opts?.env ?? {});
2828

2929
// TODO: use a new shell API that natively supports cwd & env
3030
let commandWithEnv = Object.keys(allEnv).length

src/types.ts

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

56
export interface SystemMetricsStatus {
67
cpu: {
@@ -189,6 +190,7 @@ export interface SessionCreateOptions {
189190
name?: string;
190191
};
191192
env?: Record<string, string>;
193+
previewToken?: PreviewToken;
192194
}
193195

194196
export type SandboxSession = {
@@ -235,4 +237,5 @@ export type SandboxOpts = {
235237
export type SandboxBrowserSession = PitcherManagerResponse & {
236238
id: string;
237239
env?: Record<string, string>;
240+
previewToken?: PreviewToken;
238241
};

0 commit comments

Comments
 (0)