-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
64 lines (54 loc) · 1.74 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// <reference types="node" />
import { chromium } from "playwright-core";
import Browserbase from "@browserbasehq/sdk";
const bb = new Browserbase({
apiKey: process.env.BROWSERBASE_API_KEY as string,
});
interface Session {
id: string;
connectUrl: string;
}
async function createSession(): Promise<Session> {
const session = await bb.sessions.create({
projectId: process.env.BROWSERBASE_PROJECT_ID as string,
});
return session;
}
export async function processBrowserbaseTasks<R>(
tasks: ((url: string) => Promise<R>)[]
): Promise<R[]> {
const tasksQueue = tasks.slice();
const resultsQueue: R[] = [];
const NUM_PARALLEL_SESSIONS = 5;
const sessions: Session[] = [];
// Create sessions upfront
for (let i = 0; i < NUM_PARALLEL_SESSIONS; i++) {
sessions.push(await createSession());
}
const processTasksWithSession = async (session: Session) => {
const browser = await chromium.connectOverCDP(session.connectUrl);
const defaultContext = browser.contexts()[0];
const page = defaultContext?.pages()[0];
if (!page) {
throw new Error('No page available in the browser context');
}
try {
while (tasksQueue.length > 0) {
const task = tasksQueue.shift();
if (task) {
try {
const result = await task(page.url());
resultsQueue.push(result);
} catch (error: unknown) {
console.error(`Task failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
}
} finally {
await browser.close();
console.log(`Session complete! View replay at https://browserbase.com/sessions/${session.id}`);
}
};
await Promise.all(sessions.map(processTasksWithSession));
return resultsQueue;
}