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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

The Browserbase Functions SDK lets you define, develop, and deploy serverless browser automation functions on [Browserbase](https://browserbase.com). Each function gets a managed browser session — write your automation logic, test it locally, and publish it to the cloud.

The full documentation can be found on [docs.browserbase.com](https://docs.browserbase.com).
The full documentation can be found on [docs.browserbase.com](https://docs.browserbase.com/functions/quickstart).

## Installation

Expand Down Expand Up @@ -103,7 +103,7 @@ defineFn(

### Custom Browser Configuration

Pass `sessionConfig` to customize the browser session (uses the same options as the [Browserbase SDK session create params](https://docs.browserbase.com)):
Pass `sessionConfig` to customize the browser session (uses the same options as the [Browserbase SDK session create params](https://docs.browserbase.com/reference/api/create-a-session)):

```ts
import { defineFn } from "@browserbasehq/sdk-functions";
Expand Down
26 changes: 10 additions & 16 deletions src/cli/invoke/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import chalk from "chalk";

import { loadBaseConfig, apiGet, apiPost, pollUntil } from "../shared/index.js";

export type InvocationStatus =
| "PENDING"
| "RUNNING"
| "COMPLETED"
| "FAILED"
| "TIMEOUT";
import {
loadBaseConfig,
apiGet,
apiPost,
pollUntil,
type InvocationStatus,
isTerminalInvocationStatus,
} from "../shared/index.js";

export interface InvocationResponse {
id: string;
Expand All @@ -32,10 +32,6 @@ export interface InvokeOptions {
checkStatus?: string;
}

function isTerminalStatus(status: InvocationStatus): boolean {
return status !== "RUNNING" && status !== "PENDING";
}

function displayInvocationResult(invocation: InvocationResponse): void {
console.log(chalk.bold.cyan("\n📋 Invocation Details"));
console.log(chalk.gray("─".repeat(50)));
Expand Down Expand Up @@ -163,7 +159,7 @@ export async function invoke(options: InvokeOptions): Promise<void> {
config,
`/v1/functions/invocations/${result.data.id}`,
),
(status) => isTerminalStatus(status.status),
(status) => isTerminalInvocationStatus(status.status),
(status) => status.status,
{
intervalMs: 1000,
Expand All @@ -183,13 +179,11 @@ export async function invoke(options: InvokeOptions): Promise<void> {
console.log(chalk.green("✓ Invocation completed successfully"));
} else if (finalStatus.status === "FAILED") {
console.error(chalk.red("✗ Invocation failed"));
} else if (finalStatus.status === "TIMEOUT") {
console.error(chalk.red("✗ Invocation timed out"));
}

displayInvocationResult(finalStatus);

if (finalStatus.status === "FAILED" || finalStatus.status === "TIMEOUT") {
if (finalStatus.status === "FAILED") {
process.exit(1);
}
} catch (error: unknown) {
Expand Down
11 changes: 7 additions & 4 deletions src/cli/publish/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import chalk from "chalk";

import { type PublishConfig } from "./config.js";
import { parseErrorResponse, pollUntil } from "../shared/index.js";
import {
parseErrorResponse,
pollUntil,
type BuildStatus,
isTerminalBuildStatus,
} from "../shared/index.js";

export interface BuildMetadata {
entrypoint: string;
Expand All @@ -14,8 +19,6 @@ export interface UploadResult {
message?: string;
}

export type BuildStatus = "RUNNING" | "COMPLETED" | "FAILED";

export interface FunctionCreatedVersion {
id: string;
projectId: string;
Expand Down Expand Up @@ -212,7 +215,7 @@ export async function pollBuildStatus(

const result = await pollUntil(
() => getBuildStatus(config, buildId),
(status) => status.status !== "RUNNING",
(status) => isTerminalBuildStatus(status.status),
(status) => status.status,
{
intervalMs: options?.intervalMs ?? 2000,
Expand Down
12 changes: 12 additions & 0 deletions src/cli/shared/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ export async function apiPost<T>(
}
}

export type BuildStatus = "PENDING" | "RUNNING" | "COMPLETED" | "FAILED";

export type InvocationStatus = "PENDING" | "RUNNING" | "COMPLETED" | "FAILED";

export function isTerminalBuildStatus(status: BuildStatus): boolean {
return status !== "PENDING" && status !== "RUNNING";
}

export function isTerminalInvocationStatus(status: InvocationStatus): boolean {
return status !== "PENDING" && status !== "RUNNING";
}
Comment on lines +124 to +130
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is it easier/better to define these this way (excluding what's not terminal) than the opposite? Feels like we're less likely to add another terminal status than a non-terminal

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fair, although I think I'll punt that to if/when we do that refactoring


export interface PollOptions {
/** Polling interval in milliseconds */
intervalMs?: number;
Expand Down
4 changes: 4 additions & 0 deletions src/cli/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export {
apiPost,
pollUntil,
type PollOptions,
type BuildStatus,
type InvocationStatus,
isTerminalBuildStatus,
isTerminalInvocationStatus,
} from "./api-client.js";