@browserbasehq/cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/cli.js +54 -0
- package/dist/commands/browse.js +27 -0
- package/dist/commands/contexts.js +42 -0
- package/dist/commands/dashboard.js +11 -0
- package/dist/commands/extensions.js +35 -0
- package/dist/commands/fetch.js +35 -0
- package/dist/commands/functions.js +70 -0
- package/dist/commands/projects.js +28 -0
- package/dist/commands/sessions.js +99 -0
- package/dist/lib/command.js +162 -0
- package/dist/lib/functions/dev.js +331 -0
- package/dist/lib/functions/init.js +102 -0
- package/dist/lib/functions/invoke.js +28 -0
- package/dist/lib/functions/publish.js +131 -0
- package/dist/lib/functions/shared.js +116 -0
- package/dist/lib/open.js +29 -0
- package/dist/lib/process.js +45 -0
- package/dist/main.js +4 -0
- package/package.json +54 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import Browserbase from "@browserbasehq/sdk";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import { createServer } from "node:http";
|
|
5
|
+
import { mkdir, readdir, readFile } from "node:fs/promises";
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
import { fail } from "../command.js";
|
|
10
|
+
import { resolveEntrypoint, resolveFunctionsProjectConfig, } from "./shared.js";
|
|
11
|
+
class InvocationBridge {
|
|
12
|
+
nextConnection = null;
|
|
13
|
+
invokeConnection = null;
|
|
14
|
+
currentRequestId = null;
|
|
15
|
+
currentSessionId = null;
|
|
16
|
+
cleanupSessionCallback = null;
|
|
17
|
+
runtimeConnected = false;
|
|
18
|
+
setCleanupSessionCallback(callback) {
|
|
19
|
+
this.cleanupSessionCallback = callback;
|
|
20
|
+
}
|
|
21
|
+
holdNextConnection(response) {
|
|
22
|
+
this.runtimeConnected = true;
|
|
23
|
+
if (this.nextConnection) {
|
|
24
|
+
this.nextConnection.response.writeHead(503, {
|
|
25
|
+
"content-type": "application/json",
|
|
26
|
+
});
|
|
27
|
+
this.nextConnection.response.end(JSON.stringify({ error: "Another runtime process connected." }));
|
|
28
|
+
}
|
|
29
|
+
this.nextConnection = { response };
|
|
30
|
+
}
|
|
31
|
+
isRuntimeConnected() {
|
|
32
|
+
return this.runtimeConnected && this.nextConnection !== null;
|
|
33
|
+
}
|
|
34
|
+
hasActiveInvocation() {
|
|
35
|
+
return this.invokeConnection !== null;
|
|
36
|
+
}
|
|
37
|
+
async completeWithSuccess(requestId, payload) {
|
|
38
|
+
if (requestId !== this.currentRequestId || !this.invokeConnection) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
this.invokeConnection.response.writeHead(200, {
|
|
42
|
+
"content-type": "application/json",
|
|
43
|
+
});
|
|
44
|
+
this.invokeConnection.response.end(JSON.stringify(payload ?? {}));
|
|
45
|
+
await this.cleanupSession();
|
|
46
|
+
this.reset();
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
async completeWithError(requestId, payload) {
|
|
50
|
+
if (requestId !== this.currentRequestId || !this.invokeConnection) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
this.invokeConnection.response.writeHead(500, {
|
|
54
|
+
"content-type": "application/json",
|
|
55
|
+
});
|
|
56
|
+
this.invokeConnection.response.end(JSON.stringify({ error: payload }));
|
|
57
|
+
await this.cleanupSession();
|
|
58
|
+
this.reset();
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
triggerInvocation(functionName, params, context, response) {
|
|
62
|
+
if (!this.nextConnection || this.invokeConnection) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const requestId = randomUUID();
|
|
66
|
+
this.currentRequestId = requestId;
|
|
67
|
+
this.currentSessionId = context.session.id;
|
|
68
|
+
this.invokeConnection = { response };
|
|
69
|
+
this.nextConnection.response.writeHead(200, {
|
|
70
|
+
"content-type": "application/json",
|
|
71
|
+
"Lambda-Runtime-Aws-Request-Id": requestId,
|
|
72
|
+
"Lambda-Runtime-Deadline-Ms": String(Date.now() + 300_000),
|
|
73
|
+
"Lambda-Runtime-Invoked-Function-Arn": `arn:aws:lambda:local:function:${functionName}`,
|
|
74
|
+
});
|
|
75
|
+
this.nextConnection.response.end(JSON.stringify({
|
|
76
|
+
functionName,
|
|
77
|
+
params,
|
|
78
|
+
context,
|
|
79
|
+
}));
|
|
80
|
+
this.nextConnection = null;
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
async cleanupSession() {
|
|
84
|
+
if (this.cleanupSessionCallback && this.currentSessionId) {
|
|
85
|
+
await this.cleanupSessionCallback(this.currentSessionId);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
reset() {
|
|
89
|
+
this.invokeConnection = null;
|
|
90
|
+
this.currentRequestId = null;
|
|
91
|
+
this.currentSessionId = null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
class BrowserSessionManager {
|
|
95
|
+
client;
|
|
96
|
+
projectId;
|
|
97
|
+
constructor(apiKey, projectId, baseUrl) {
|
|
98
|
+
this.client = new Browserbase({
|
|
99
|
+
apiKey,
|
|
100
|
+
baseURL: baseUrl,
|
|
101
|
+
});
|
|
102
|
+
this.projectId = projectId;
|
|
103
|
+
}
|
|
104
|
+
async createSession(sessionConfig) {
|
|
105
|
+
const session = await this.client.sessions.create({
|
|
106
|
+
projectId: this.projectId,
|
|
107
|
+
...sessionConfig,
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
id: session.id,
|
|
111
|
+
connectUrl: session.connectUrl,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async closeSession(sessionId) {
|
|
115
|
+
await this.client.sessions.update(sessionId, {
|
|
116
|
+
projectId: this.projectId,
|
|
117
|
+
status: "REQUEST_RELEASE",
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
class ManifestStore {
|
|
122
|
+
manifestsPath = join(process.cwd(), ".browserbase", "functions", "manifests");
|
|
123
|
+
manifests = new Map();
|
|
124
|
+
async load() {
|
|
125
|
+
this.manifests.clear();
|
|
126
|
+
if (!existsSync(this.manifestsPath)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const entries = await readdir(this.manifestsPath);
|
|
130
|
+
for (const entry of entries) {
|
|
131
|
+
if (!entry.endsWith(".json")) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const manifest = JSON.parse(await readFile(join(this.manifestsPath, entry), "utf8"));
|
|
135
|
+
this.manifests.set(manifest.name, manifest);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
get(name) {
|
|
139
|
+
return this.manifests.get(name);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
class RuntimeProcess {
|
|
143
|
+
process = null;
|
|
144
|
+
entrypoint;
|
|
145
|
+
runtimeApi;
|
|
146
|
+
verbose;
|
|
147
|
+
constructor(entrypoint, runtimeApi, verbose) {
|
|
148
|
+
this.entrypoint = entrypoint;
|
|
149
|
+
this.runtimeApi = runtimeApi;
|
|
150
|
+
this.verbose = verbose;
|
|
151
|
+
}
|
|
152
|
+
async start() {
|
|
153
|
+
const require = createRequire(import.meta.url);
|
|
154
|
+
const tsxCli = require.resolve("tsx/cli");
|
|
155
|
+
this.process = spawn(process.execPath, [tsxCli, "watch", "--clear-screen=false", this.entrypoint], {
|
|
156
|
+
cwd: process.cwd(),
|
|
157
|
+
env: {
|
|
158
|
+
...process.env,
|
|
159
|
+
AWS_LAMBDA_RUNTIME_API: this.runtimeApi,
|
|
160
|
+
BB_FUNCTIONS_PHASE: "runtime",
|
|
161
|
+
NODE_ENV: "local",
|
|
162
|
+
},
|
|
163
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
164
|
+
});
|
|
165
|
+
this.process.stdout?.on("data", (chunk) => {
|
|
166
|
+
const text = chunk.toString().trim();
|
|
167
|
+
if (!text) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
console.log(this.verbose ? `[runtime] ${text}` : text);
|
|
171
|
+
});
|
|
172
|
+
this.process.stderr?.on("data", (chunk) => {
|
|
173
|
+
const text = chunk.toString().trim();
|
|
174
|
+
if (!text) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
console.error(this.verbose ? `[runtime:error] ${text}` : text);
|
|
178
|
+
});
|
|
179
|
+
this.process.on("error", (error) => {
|
|
180
|
+
fail(`Failed to start functions runtime: ${error.message}`);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
async stop() {
|
|
184
|
+
if (!this.process) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
await new Promise((resolve) => {
|
|
188
|
+
const child = this.process;
|
|
189
|
+
child.once("exit", () => resolve());
|
|
190
|
+
child.kill("SIGTERM");
|
|
191
|
+
});
|
|
192
|
+
this.process = null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
export async function startFunctionsDevServer(options) {
|
|
196
|
+
const entrypoint = await resolveEntrypoint(options.entrypoint);
|
|
197
|
+
if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65_535) {
|
|
198
|
+
fail("Port must be an integer between 1 and 65535.");
|
|
199
|
+
}
|
|
200
|
+
const config = resolveFunctionsProjectConfig(options);
|
|
201
|
+
const runtimeApi = `${options.host}:${options.port}`;
|
|
202
|
+
const bridge = new InvocationBridge();
|
|
203
|
+
const sessionManager = new BrowserSessionManager(config.apiKey, config.projectId, config.apiUrl);
|
|
204
|
+
const manifestStore = new ManifestStore();
|
|
205
|
+
bridge.setCleanupSessionCallback(async (sessionId) => {
|
|
206
|
+
await sessionManager.closeSession(sessionId);
|
|
207
|
+
});
|
|
208
|
+
await mkdir(join(process.cwd(), ".browserbase", "functions", "manifests"), {
|
|
209
|
+
recursive: true,
|
|
210
|
+
});
|
|
211
|
+
const server = await startServer(options.host, options.port, bridge, manifestStore, sessionManager);
|
|
212
|
+
const runtime = new RuntimeProcess(entrypoint, runtimeApi, options.verbose);
|
|
213
|
+
await runtime.start();
|
|
214
|
+
await waitForRuntime(bridge, manifestStore);
|
|
215
|
+
console.log(`Functions dev server running at http://${options.host}:${options.port}`);
|
|
216
|
+
const shutdown = async () => {
|
|
217
|
+
await runtime.stop();
|
|
218
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
219
|
+
};
|
|
220
|
+
process.on("SIGINT", async () => {
|
|
221
|
+
await shutdown();
|
|
222
|
+
process.exit(0);
|
|
223
|
+
});
|
|
224
|
+
process.on("SIGTERM", async () => {
|
|
225
|
+
await shutdown();
|
|
226
|
+
process.exit(0);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
async function startServer(host, port, bridge, manifestStore, sessionManager) {
|
|
230
|
+
const server = createServer(async (request, response) => {
|
|
231
|
+
await routeRequest(request, response, bridge, manifestStore, sessionManager);
|
|
232
|
+
});
|
|
233
|
+
await new Promise((resolve, reject) => {
|
|
234
|
+
server.listen(port, host, () => resolve());
|
|
235
|
+
server.on("error", reject);
|
|
236
|
+
});
|
|
237
|
+
return server;
|
|
238
|
+
}
|
|
239
|
+
async function waitForRuntime(bridge, manifestStore) {
|
|
240
|
+
const deadline = Date.now() + 10_000;
|
|
241
|
+
while (Date.now() < deadline) {
|
|
242
|
+
if (bridge.isRuntimeConnected()) {
|
|
243
|
+
await manifestStore.load();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
247
|
+
}
|
|
248
|
+
await manifestStore.load();
|
|
249
|
+
}
|
|
250
|
+
async function routeRequest(request, response, bridge, manifestStore, sessionManager) {
|
|
251
|
+
const method = request.method || "GET";
|
|
252
|
+
const url = new URL(request.url || "/", `http://${request.headers.host || "127.0.0.1"}`);
|
|
253
|
+
const path = url.pathname;
|
|
254
|
+
if (method === "GET" && path === "/") {
|
|
255
|
+
sendJson(response, 200, { ok: true });
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (method === "GET" && path === "/2018-06-01/runtime/invocation/next") {
|
|
259
|
+
bridge.holdNextConnection(response);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const invokeMatch = path.match(/^\/v1\/functions\/([^/]+)\/invoke$/);
|
|
263
|
+
if (method === "POST" && invokeMatch?.[1]) {
|
|
264
|
+
await manifestStore.load();
|
|
265
|
+
const functionName = invokeMatch[1];
|
|
266
|
+
const manifest = manifestStore.get(functionName);
|
|
267
|
+
if (!manifest) {
|
|
268
|
+
sendJson(response, 404, {
|
|
269
|
+
error: `Function "${functionName}" was not found in .browserbase/functions/manifests.`,
|
|
270
|
+
});
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (bridge.hasActiveInvocation()) {
|
|
274
|
+
sendJson(response, 503, { error: "Another invocation is already in progress." });
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const body = await readJsonBody(request);
|
|
278
|
+
const params = body && typeof body === "object" && body !== null && !Array.isArray(body)
|
|
279
|
+
? (body.params || {})
|
|
280
|
+
: {};
|
|
281
|
+
const session = await sessionManager.createSession(manifest.config?.sessionConfig);
|
|
282
|
+
const accepted = bridge.triggerInvocation(functionName, params, { session }, response);
|
|
283
|
+
if (!accepted) {
|
|
284
|
+
await sessionManager.closeSession(session.id);
|
|
285
|
+
sendJson(response, 503, { error: "No runtime is connected yet." });
|
|
286
|
+
}
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const responseMatch = path.match(/^\/2018-06-01\/runtime\/invocation\/([^/]+)\/response$/);
|
|
290
|
+
if (method === "POST" && responseMatch?.[1]) {
|
|
291
|
+
const payload = await readJsonBody(request);
|
|
292
|
+
const completed = await bridge.completeWithSuccess(responseMatch[1], payload);
|
|
293
|
+
sendJson(response, completed ? 202 : 400, completed ? { ok: true } : { error: "Request ID mismatch." });
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const errorMatch = path.match(/^\/2018-06-01\/runtime\/invocation\/([^/]+)\/error$/);
|
|
297
|
+
if (method === "POST" && errorMatch?.[1]) {
|
|
298
|
+
const payload = (await readJsonBody(request));
|
|
299
|
+
const completed = await bridge.completeWithError(errorMatch[1], {
|
|
300
|
+
errorMessage: payload?.errorMessage || "Unknown runtime error",
|
|
301
|
+
errorType: payload?.errorType || "RuntimeError",
|
|
302
|
+
stackTrace: Array.isArray(payload?.stackTrace) ? payload.stackTrace : [],
|
|
303
|
+
});
|
|
304
|
+
sendJson(response, completed ? 202 : 400, completed ? { ok: true } : { error: "Request ID mismatch." });
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
sendJson(response, 404, { error: "Not found." });
|
|
308
|
+
}
|
|
309
|
+
async function readJsonBody(request) {
|
|
310
|
+
const chunks = [];
|
|
311
|
+
for await (const chunk of request) {
|
|
312
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
313
|
+
}
|
|
314
|
+
if (chunks.length === 0) {
|
|
315
|
+
return {};
|
|
316
|
+
}
|
|
317
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
318
|
+
if (!text) {
|
|
319
|
+
return {};
|
|
320
|
+
}
|
|
321
|
+
return JSON.parse(text);
|
|
322
|
+
}
|
|
323
|
+
function sendJson(response, statusCode, payload) {
|
|
324
|
+
response.writeHead(statusCode, {
|
|
325
|
+
"content-type": "application/json",
|
|
326
|
+
"access-control-allow-origin": "*",
|
|
327
|
+
"access-control-allow-methods": "GET, POST, OPTIONS",
|
|
328
|
+
"access-control-allow-headers": "content-type",
|
|
329
|
+
});
|
|
330
|
+
response.end(JSON.stringify(payload));
|
|
331
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { fail } from "../command.js";
|
|
6
|
+
import { ensureCommand } from "./shared.js";
|
|
7
|
+
const envTemplate = `# Browserbase Configuration
|
|
8
|
+
# Get your API key and project ID from https://browserbase.com/settings
|
|
9
|
+
|
|
10
|
+
BROWSERBASE_API_KEY=your_api_key_here
|
|
11
|
+
BROWSERBASE_PROJECT_ID=your_project_id_here
|
|
12
|
+
`;
|
|
13
|
+
const gitignoreTemplate = `node_modules/
|
|
14
|
+
.env
|
|
15
|
+
.env.local
|
|
16
|
+
dist/
|
|
17
|
+
*.log
|
|
18
|
+
.DS_Store
|
|
19
|
+
`;
|
|
20
|
+
const starterFunctionTemplate = `import { defineFn } from "@browserbasehq/sdk-functions";
|
|
21
|
+
import { chromium } from "playwright-core";
|
|
22
|
+
|
|
23
|
+
defineFn("my-function", async (context) => {
|
|
24
|
+
const browser = await chromium.connectOverCDP(context.session.connectUrl);
|
|
25
|
+
const page = browser.contexts()[0]!.pages()[0]!;
|
|
26
|
+
|
|
27
|
+
await page.goto("https://news.ycombinator.com");
|
|
28
|
+
await page.waitForSelector(".athing", { timeout: 30_000 });
|
|
29
|
+
|
|
30
|
+
const titles = await page.$$eval(".athing .titleline > a", (elements) =>
|
|
31
|
+
elements.slice(0, 3).map((element) => element.textContent),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
message: "Fetched top Hacker News titles",
|
|
36
|
+
titles,
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
`;
|
|
40
|
+
const tsconfigTemplate = `{
|
|
41
|
+
"compilerOptions": {
|
|
42
|
+
"target": "ES2022",
|
|
43
|
+
"module": "NodeNext",
|
|
44
|
+
"moduleResolution": "NodeNext",
|
|
45
|
+
"strict": true,
|
|
46
|
+
"skipLibCheck": true,
|
|
47
|
+
"esModuleInterop": true
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
`;
|
|
51
|
+
export async function initFunctionsProject({ projectName, packageManager, }) {
|
|
52
|
+
if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(projectName)) {
|
|
53
|
+
fail(`Invalid project name "${projectName}". Use a leading letter, then letters, numbers, hyphens, or underscores.`);
|
|
54
|
+
}
|
|
55
|
+
ensureCommand(packageManager);
|
|
56
|
+
const projectRoot = resolve(projectName);
|
|
57
|
+
if (existsSync(projectRoot)) {
|
|
58
|
+
fail(`Directory already exists: ${projectRoot}`);
|
|
59
|
+
}
|
|
60
|
+
await mkdir(projectRoot, { recursive: true });
|
|
61
|
+
const packageJson = {
|
|
62
|
+
name: projectName,
|
|
63
|
+
private: true,
|
|
64
|
+
type: "module",
|
|
65
|
+
scripts: {
|
|
66
|
+
dev: "bb functions dev index.ts",
|
|
67
|
+
publish: "bb functions publish index.ts",
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
await writeFile(join(projectRoot, "package.json"), JSON.stringify(packageJson, null, 2) + "\n");
|
|
71
|
+
await writeFile(join(projectRoot, ".env"), envTemplate);
|
|
72
|
+
await writeFile(join(projectRoot, ".gitignore"), gitignoreTemplate);
|
|
73
|
+
await writeFile(join(projectRoot, "index.ts"), starterFunctionTemplate);
|
|
74
|
+
await writeFile(join(projectRoot, "tsconfig.json"), tsconfigTemplate);
|
|
75
|
+
const install = packageManager === "pnpm" ? ["add"] : ["install"];
|
|
76
|
+
const installDev = packageManager === "pnpm" ? ["add", "-D"] : ["install", "--save-dev"];
|
|
77
|
+
const dependencies = ["@browserbasehq/sdk-functions", "playwright-core"];
|
|
78
|
+
const devDependencies = ["typescript", "@types/node"];
|
|
79
|
+
runPackageManager(packageManager, [...install, ...dependencies], projectRoot);
|
|
80
|
+
runPackageManager(packageManager, [...installDev, ...devDependencies], projectRoot);
|
|
81
|
+
if (!existsSync(join(projectRoot, ".git"))) {
|
|
82
|
+
spawnSync("git", ["init"], {
|
|
83
|
+
cwd: projectRoot,
|
|
84
|
+
stdio: "ignore",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
console.log(`Initialized Browserbase Functions project in ${projectRoot}`);
|
|
88
|
+
console.log("");
|
|
89
|
+
console.log("Next steps:");
|
|
90
|
+
console.log(` cd ${projectName}`);
|
|
91
|
+
console.log(" Edit .env with your Browserbase credentials");
|
|
92
|
+
console.log(` ${packageManager === "pnpm" ? "pnpm" : "npm run"} dev`);
|
|
93
|
+
}
|
|
94
|
+
function runPackageManager(packageManager, args, cwd) {
|
|
95
|
+
const result = spawnSync(packageManager, args, {
|
|
96
|
+
cwd,
|
|
97
|
+
stdio: "inherit",
|
|
98
|
+
});
|
|
99
|
+
if (result.error || result.status !== 0) {
|
|
100
|
+
fail(`Failed to install dependencies with ${packageManager}.`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { fail, parseOptionalJsonValueArg } from "../command.js";
|
|
2
|
+
import { functionsGet, functionsPost, pollUntil, resolveFunctionsApiConfig, } from "./shared.js";
|
|
3
|
+
export async function invokeFunction(options) {
|
|
4
|
+
const config = resolveFunctionsApiConfig(options);
|
|
5
|
+
if (options.checkStatus) {
|
|
6
|
+
const status = await functionsGet(config, `/v1/functions/invocations/${options.checkStatus}`);
|
|
7
|
+
console.log(JSON.stringify(status, null, 2));
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (!options.functionId) {
|
|
11
|
+
fail("functionId is required unless --check-status is used.");
|
|
12
|
+
}
|
|
13
|
+
const params = parseOptionalJsonValueArg(options.params, "params");
|
|
14
|
+
const invocation = await functionsPost(config, `/v1/functions/${options.functionId}/invoke`, { params });
|
|
15
|
+
if (options.noWait) {
|
|
16
|
+
console.log(JSON.stringify(invocation, null, 2));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const finalStatus = await pollUntil(() => functionsGet(config, `/v1/functions/invocations/${invocation.id}`), {
|
|
20
|
+
done: (result) => !["PENDING", "RUNNING"].includes(result.status),
|
|
21
|
+
intervalMs: 1_000,
|
|
22
|
+
maxAttempts: 900,
|
|
23
|
+
});
|
|
24
|
+
console.log(JSON.stringify(finalStatus, null, 2));
|
|
25
|
+
if (finalStatus.status === "FAILED") {
|
|
26
|
+
process.exitCode = 1;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import archiver from "archiver";
|
|
2
|
+
import ignore from "ignore";
|
|
3
|
+
import { createWriteStream, existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join, relative } from "node:path";
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { fail } from "../command.js";
|
|
9
|
+
import { functionsGet, parseErrorMessage, pollUntil, resolveEntrypoint, resolveFunctionsProjectConfig, } from "./shared.js";
|
|
10
|
+
const defaultIgnorePatterns = [
|
|
11
|
+
"node_modules/",
|
|
12
|
+
".git/",
|
|
13
|
+
".env",
|
|
14
|
+
".env.*",
|
|
15
|
+
"*.log",
|
|
16
|
+
".DS_Store",
|
|
17
|
+
"dist/",
|
|
18
|
+
"build/",
|
|
19
|
+
"*.zip",
|
|
20
|
+
"*.tar",
|
|
21
|
+
"*.tar.gz",
|
|
22
|
+
".vscode/",
|
|
23
|
+
".idea/",
|
|
24
|
+
];
|
|
25
|
+
export async function publishFunction(options) {
|
|
26
|
+
const entrypoint = await resolveEntrypoint(options.entrypoint);
|
|
27
|
+
const config = resolveFunctionsProjectConfig(options);
|
|
28
|
+
const archivePath = await createArchive(process.cwd(), options.dryRun);
|
|
29
|
+
const metadata = {
|
|
30
|
+
entrypoint: relative(process.cwd(), entrypoint),
|
|
31
|
+
projectId: config.projectId,
|
|
32
|
+
};
|
|
33
|
+
if (options.dryRun) {
|
|
34
|
+
console.log(JSON.stringify({
|
|
35
|
+
dryRun: true,
|
|
36
|
+
apiUrl: config.apiUrl,
|
|
37
|
+
projectId: config.projectId,
|
|
38
|
+
entrypoint: metadata.entrypoint,
|
|
39
|
+
archivePath,
|
|
40
|
+
}, null, 2));
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const formData = new FormData();
|
|
44
|
+
formData.append("metadata", JSON.stringify(metadata));
|
|
45
|
+
formData.append("archive", new Blob([await readFile(archivePath)], {
|
|
46
|
+
type: "application/gzip",
|
|
47
|
+
}), "archive.tar.gz");
|
|
48
|
+
const uploadResponse = await fetch(`${config.apiUrl}/v1/functions/builds`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"x-bb-api-key": config.apiKey,
|
|
52
|
+
},
|
|
53
|
+
body: formData,
|
|
54
|
+
});
|
|
55
|
+
if (!uploadResponse.ok) {
|
|
56
|
+
fail(await parseErrorMessage(uploadResponse));
|
|
57
|
+
}
|
|
58
|
+
const uploaded = (await uploadResponse.json());
|
|
59
|
+
if (!uploaded.id) {
|
|
60
|
+
fail("Build upload completed without returning a build ID.");
|
|
61
|
+
}
|
|
62
|
+
const build = await pollUntil(() => functionsGet(config, `/v1/functions/builds/${uploaded.id}`), {
|
|
63
|
+
done: (result) => !["PENDING", "RUNNING"].includes(result.status),
|
|
64
|
+
intervalMs: 2_000,
|
|
65
|
+
maxAttempts: 100,
|
|
66
|
+
});
|
|
67
|
+
console.log(JSON.stringify(build, null, 2));
|
|
68
|
+
if (build.status === "FAILED") {
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function createArchive(root, dryRun) {
|
|
73
|
+
const archivePath = join(tmpdir(), `browserbase-functions-${randomUUID()}.tar.gz`);
|
|
74
|
+
const ignoreMatcher = await loadIgnoreMatcher(root);
|
|
75
|
+
const entries = await listArchiveEntries(root, root, ignoreMatcher);
|
|
76
|
+
if (dryRun) {
|
|
77
|
+
console.log(JSON.stringify({ files: entries }, null, 2));
|
|
78
|
+
}
|
|
79
|
+
await new Promise((resolve, reject) => {
|
|
80
|
+
const output = createWriteStream(archivePath);
|
|
81
|
+
const archive = archiver("tar", {
|
|
82
|
+
gzip: true,
|
|
83
|
+
gzipOptions: { level: 9 },
|
|
84
|
+
});
|
|
85
|
+
archive.on("error", reject);
|
|
86
|
+
archive.on("warning", (warning) => {
|
|
87
|
+
if (warning.code === "ENOENT") {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
reject(warning);
|
|
91
|
+
});
|
|
92
|
+
output.on("close", () => resolve());
|
|
93
|
+
output.on("error", reject);
|
|
94
|
+
archive.pipe(output);
|
|
95
|
+
for (const entry of entries) {
|
|
96
|
+
archive.file(join(root, entry), { name: entry });
|
|
97
|
+
}
|
|
98
|
+
archive.finalize().catch(reject);
|
|
99
|
+
});
|
|
100
|
+
return archivePath;
|
|
101
|
+
}
|
|
102
|
+
async function loadIgnoreMatcher(root) {
|
|
103
|
+
const matcher = ignore();
|
|
104
|
+
matcher.add(defaultIgnorePatterns);
|
|
105
|
+
const gitignorePath = join(root, ".gitignore");
|
|
106
|
+
if (existsSync(gitignorePath)) {
|
|
107
|
+
matcher.add(readFileSync(gitignorePath, "utf8"));
|
|
108
|
+
}
|
|
109
|
+
return matcher;
|
|
110
|
+
}
|
|
111
|
+
async function listArchiveEntries(root, current, matcher) {
|
|
112
|
+
const entries = await readdir(current, { withFileTypes: true });
|
|
113
|
+
const files = [];
|
|
114
|
+
for (const entry of entries) {
|
|
115
|
+
const absolutePath = join(current, entry.name);
|
|
116
|
+
const relativePath = relative(root, absolutePath) || ".";
|
|
117
|
+
const ignorePath = entry.isDirectory() ? `${relativePath}/` : relativePath;
|
|
118
|
+
if (relativePath !== "." && matcher.ignores(ignorePath)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (entry.isDirectory()) {
|
|
122
|
+
files.push(...(await listArchiveEntries(root, absolutePath, matcher)));
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const fileStats = await stat(absolutePath);
|
|
126
|
+
if (fileStats.isFile()) {
|
|
127
|
+
files.push(relativePath);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return files.sort();
|
|
131
|
+
}
|