Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,21 @@ describe("resolveConfig", () => {
expect(config.deviceSecret).toBe("secret123");
expect(config.port).toBe(4010);
});

it("reads previewPath from legacy spawndock.config.json", () => {
const dir = mkdtempSync(join(tmpdir(), "spawndock-config-"));
writeFileSync(
join(dir, "spawndock.config.json"),
JSON.stringify({
controlPlaneUrl: "http://localhost:8787",
projectSlug: "my-app",
deviceSecret: "secret123",
localPort: 4010,
previewPath: "/preview/my-app",
}),
);

const config = resolveConfig([], dir);
expect(config.previewPath).toBe("/preview/my-app");
});
});
18 changes: 18 additions & 0 deletions src/__tests__/tunnel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";

import { buildWsUrl } from "../tunnel.js";

describe("buildWsUrl", () => {
it("includes protocol version and token query params", () => {
const url = buildWsUrl({
controlPlane: "https://api.example.com",
projectSlug: "demo",
deviceSecret: "secret123",
port: 3000,
});

expect(url).toBe(
"wss://api.example.com/tunnel/connect?protocolVersion=1&token=secret123",
);
});
});
8 changes: 6 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface TunnelConfig {
projectSlug: string;
deviceSecret: string;
port: number;
previewPath?: string;
}

const PRIMARY_CONFIG_FILE = "spawndock.dev-tunnel.json";
Expand Down Expand Up @@ -46,8 +47,10 @@ function normalizeConfig(data: unknown): Partial<TunnelConfig> {
: typeof record.localPort === "number"
? record.localPort
: undefined;
const previewPath =
typeof record.previewPath === "string" ? record.previewPath : undefined;

return { controlPlane, projectSlug, deviceSecret, port };
return { controlPlane, projectSlug, deviceSecret, port, previewPath };
}

function readConfigFile(dir: string): Partial<TunnelConfig> {
Expand Down Expand Up @@ -134,10 +137,11 @@ export function resolveConfig(
const projectSlug = args.projectSlug ?? env.projectSlug ?? file.projectSlug;
const deviceSecret = args.deviceSecret ?? env.deviceSecret ?? file.deviceSecret;
const port = args.port ?? env.port ?? file.port ?? 3000;
const previewPath = file.previewPath;

if (!controlPlane) throw new Error("Missing --control-plane or SPAWNDOCK_CONTROL_PLANE");
if (!projectSlug) throw new Error("Missing --project-slug or SPAWNDOCK_PROJECT_SLUG");
if (!deviceSecret) throw new Error("Missing --device-secret or SPAWNDOCK_DEVICE_SECRET");

return { controlPlane, projectSlug, deviceSecret, port };
return { controlPlane, projectSlug, deviceSecret, port, previewPath };
}
24 changes: 23 additions & 1 deletion src/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ export function createTunnel(config: TunnelConfig): void {
connect(wsUrl, config, localOrigin);
}

function resolveForwardedPath(previewPath: string | undefined, path: string): string {
if (!previewPath || previewPath.length === 0) {
return path;
}

const normalizedPreviewPath = previewPath.replace(/\/$/, "");
const [pathname, query = ""] = path.split("?");

const forwardedPath =
pathname === "/"
? `${normalizedPreviewPath}/`
: `${normalizedPreviewPath}${pathname}`;

return query.length > 0 ? `${forwardedPath}?${query}` : forwardedPath;
}

export function buildWsUrl(config: TunnelConfig): string {
if (!URL.canParse(config.controlPlane)) {
throw new Error(`Invalid control plane URL: ${config.controlPlane}`);
Expand Down Expand Up @@ -64,7 +80,13 @@ function connect(wsUrl: string, config: TunnelConfig, localOrigin: string): void

if (msg.type === "http-request") {
try {
const response = await proxyRequest(msg.request, localOrigin);
const response = await proxyRequest(
{
...msg.request,
path: resolveForwardedPath(config.previewPath, msg.request.path),
},
localOrigin,
);
ws.send(serialize({ type: "http-response", response }));
} catch (err: any) {
ws.send(serialize({
Expand Down