Skip to content

Commit 375489e

Browse files
authored
New Components - browserbase (#16714)
* browserbase init * [Components] browserbase #15425 Sources - New Session Actions - Create Session - Create Context - List Projects * pnpm update * pnpm update * pnpm update
1 parent 28b1d83 commit 375489e

File tree

11 files changed

+372
-6
lines changed

11 files changed

+372
-6
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import browserbase from "../../browserbase.app.mjs";
2+
3+
export default {
4+
key: "browserbase-create-context",
5+
name: "Create Context",
6+
description: "Creates a new context in Browserbase. [See the documentation](https://docs.browserbase.com/reference/api/create-a-context)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
browserbase,
11+
projectId: {
12+
propDefinition: [
13+
browserbase,
14+
"projectId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.browserbase.createContext({
20+
$,
21+
data: {
22+
projectId: this.projectId,
23+
},
24+
});
25+
26+
$.export("$summary", `Successfully created context with ID: ${response.id}`);
27+
28+
return response;
29+
},
30+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import browserbase from "../../browserbase.app.mjs";
2+
import { REGION_OPTIONS } from "../../common/constants.mjs";
3+
import { parseObject } from "../../common/utils.mjs";
4+
5+
export default {
6+
key: "browserbase-create-session",
7+
name: "Create Session",
8+
description: "Creates a new browser session with specified settings. [See the documentation](https://docs.browserbase.com/reference/api/create-a-session)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
browserbase,
13+
projectId: {
14+
propDefinition: [
15+
browserbase,
16+
"projectId",
17+
],
18+
},
19+
extensionId: {
20+
type: "string",
21+
label: "Extension ID",
22+
description: "The uploaded Extension ID",
23+
optional: true,
24+
},
25+
browserSettings: {
26+
type: "object",
27+
label: "Browser Settings",
28+
description: "An object with the settings for the session. [See the documentation](https://docs.browserbase.com/reference/api/create-a-session#body-browser-settings) for more details.",
29+
optional: true,
30+
},
31+
timeout: {
32+
type: "integer",
33+
label: "Timeout",
34+
description: "Duration in seconds after which the session will automatically end.",
35+
min: 60,
36+
max: 21600,
37+
optional: true,
38+
},
39+
keepAlive: {
40+
type: "boolean",
41+
label: "Keep Alive",
42+
description: "Set to true to keep the session alive even after disconnections.",
43+
optional: true,
44+
},
45+
proxies: {
46+
type: "string[]",
47+
label: "Proxies",
48+
description: "An array of objects with proxy configuration. [See the documentation](https://docs.browserbase.com/reference/api/create-a-session#body-proxies) for more details.",
49+
optional: true,
50+
},
51+
region: {
52+
type: "string",
53+
label: "Region",
54+
description: "The region where the session should run.",
55+
options: REGION_OPTIONS,
56+
optional: true,
57+
},
58+
userMetadata: {
59+
type: "object",
60+
label: "User Metadata",
61+
description: "Arbitrary user metadata to attach to the session. To learn more about user metadata, see [User Metadata](https://docs.browserbase.com/features/sessions#user-metadata).",
62+
optional: true,
63+
},
64+
},
65+
async run({ $ }) {
66+
const response = await this.browserbase.createSession({
67+
$,
68+
data: {
69+
projectId: this.projectId,
70+
extensionId: this.extensionId,
71+
browserSettings: parseObject(this.browserSettings),
72+
timeout: this.timeout,
73+
keepAlive: this.keepAlive,
74+
proxies: parseObject(this.proxies),
75+
region: this.region,
76+
userMetadata: parseObject(this.userMetadata),
77+
},
78+
});
79+
80+
$.export("$summary", `Session created successfully with ID: ${response.id}`);
81+
return response;
82+
},
83+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import browserbase from "../../browserbase.app.mjs";
2+
3+
export default {
4+
key: "browserbase-list-projects",
5+
name: "List Projects",
6+
description: "Lists all projects. [See the documentation](https://docs.browserbase.com/reference/api/list-projects)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
browserbase,
11+
},
12+
async run({ $ }) {
13+
const projects = await this.browserbase.listProjects({
14+
$,
15+
});
16+
$.export("$summary", `Successfully listed ${projects.length} project(s).`);
17+
return projects;
18+
},
19+
};
Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "browserbase",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
projectId: {
8+
type: "string",
9+
label: "Project ID",
10+
description: "The ID of the project",
11+
async options() {
12+
const projects = await this.listProjects();
13+
return projects.map((project) => ({
14+
value: project.id,
15+
label: project.name,
16+
}));
17+
},
18+
},
19+
},
520
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
21+
_baseUrl() {
22+
return "https://api.browserbase.com/v1";
23+
},
24+
_headers() {
25+
return {
26+
"x-bb-api-key": `${this.$auth.api_key}`,
27+
"Content-Type": "application/json",
28+
};
29+
},
30+
_makeRequest({
31+
$ = this, path, ...opts
32+
}) {
33+
return axios($, {
34+
url: this._baseUrl() + path,
35+
headers: this._headers(),
36+
...opts,
37+
});
38+
},
39+
listProjects(opts = {}) {
40+
return this._makeRequest({
41+
path: "/projects",
42+
...opts,
43+
});
44+
},
45+
listSessions(opts = {}) {
46+
return this._makeRequest({
47+
path: "/sessions",
48+
...opts,
49+
});
50+
},
51+
createSession(opts = {}) {
52+
return this._makeRequest({
53+
method: "POST",
54+
path: "/sessions",
55+
...opts,
56+
});
57+
},
58+
createContext(opts = {}) {
59+
return this._makeRequest({
60+
method: "POST",
61+
path: "/contexts",
62+
...opts,
63+
});
964
},
1065
},
1166
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const REGION_OPTIONS = [
2+
{
3+
label: "US West 2",
4+
value: "us-west-2",
5+
},
6+
{
7+
label: "US East 1",
8+
value: "us-east-1",
9+
},
10+
{
11+
label: "EU Central 1",
12+
value: "eu-central-1",
13+
},
14+
{
15+
label: "AP Southeast 1",
16+
value: "ap-southeast-1",
17+
},
18+
];
19+
20+
export const STATUS_OPTIONS = [
21+
"RUNNING",
22+
"ERROR",
23+
"TIMED_OUT",
24+
"COMPLETED",
25+
];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/browserbase/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/browserbase",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Browserbase Components",
55
"main": "browserbase.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
1518
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import browserbase from "../../browserbase.app.mjs";
3+
4+
export default {
5+
props: {
6+
browserbase,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
_getLastDate() {
17+
return this.db.get("lastDate") || 0;
18+
},
19+
_setLastDate(lastDate) {
20+
this.db.set("lastDate", lastDate);
21+
},
22+
getParams() {
23+
return {};
24+
},
25+
async emitEvent(maxResults = false) {
26+
const lastDate = this._getLastDate();
27+
const fn = this.getFunction();
28+
29+
const response = await fn({
30+
params: this.getParams(),
31+
});
32+
33+
let responseArray = response.filter((item) => {
34+
return Date.parse(item.createdAt) > lastDate;
35+
});
36+
37+
if (responseArray.length) {
38+
if (maxResults && (responseArray.length > maxResults)) {
39+
responseArray.length = maxResults;
40+
}
41+
this._setLastDate(Date.parse(responseArray[0].createdAt));
42+
}
43+
44+
for (const item of responseArray.reverse()) {
45+
this.$emit(item, {
46+
id: item.id,
47+
summary: this.getSummary(item),
48+
ts: Date.parse(item.createdAt),
49+
});
50+
}
51+
},
52+
},
53+
hooks: {
54+
async deploy() {
55+
await this.emitEvent(25);
56+
},
57+
},
58+
async run() {
59+
await this.emitEvent();
60+
},
61+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { STATUS_OPTIONS } from "../../common/constants.mjs";
2+
import common from "../common/base.mjs";
3+
import sampleEmit from "./test-event.mjs";
4+
5+
export default {
6+
...common,
7+
key: "browserbase-new-session",
8+
name: "New Session Created",
9+
description: "Emit new event when a new session is created. [See the documentation](https://docs.browserbase.com/reference/api/list-sessions)",
10+
version: "0.0.1",
11+
type: "source",
12+
dedupe: "unique",
13+
props: {
14+
...common.props,
15+
status: {
16+
type: "string",
17+
label: "Status",
18+
description: "The status of the session.",
19+
options: STATUS_OPTIONS,
20+
optional: true,
21+
},
22+
q: {
23+
type: "string",
24+
label: "Query",
25+
description: "Query sessions by user metadata. See [Querying Sessions by User Metadata](https://docs.browserbase.com/features/sessions#querying-sessions-by-user-metadata) for the schema of this query.",
26+
optional: true,
27+
},
28+
},
29+
methods: {
30+
...common.methods,
31+
getFunction() {
32+
return this.browserbase.listSessions;
33+
},
34+
getParams() {
35+
return {
36+
status: this.status,
37+
q: this.q || null,
38+
};
39+
},
40+
getSummary(item) {
41+
return `New session: ${item.id}`;
42+
},
43+
},
44+
sampleEmit,
45+
};

0 commit comments

Comments
 (0)