@browserbasehq/sdk
Advanced tools
Comparing version 1.0.1 to 1.1.0
/// <reference types="node" resolution-mode="require"/> | ||
export type BrowserbaseLoadOptions = { | ||
export type ClientOptions = { | ||
apiKey?: string; | ||
projectId?: string; | ||
baseURL?: string; | ||
}; | ||
export type LoadOptions = { | ||
sessionId?: string; | ||
textContent?: boolean; | ||
}; | ||
export type BrowserbaseScreenshotOptions = { | ||
export type ScreenshotOptions = { | ||
fullPage?: boolean; | ||
}; | ||
export type CreateSessionOptions = { | ||
projectId?: string; | ||
extensionId?: string; | ||
fingerprint?: { | ||
browserListQuery?: string; | ||
httpVersion?: 1 | 2; | ||
browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>; | ||
devices?: Array<'desktop' | 'mobile'>; | ||
locales?: string[]; | ||
operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>; | ||
screen?: { | ||
maxHeight?: number; | ||
maxWidth?: number; | ||
minHeight?: number; | ||
minWidth?: number; | ||
}; | ||
}; | ||
}; | ||
export type Session = { | ||
id: string; | ||
createdAt: string; | ||
startedAt: string; | ||
endedAt: string; | ||
projectId: string; | ||
status: 'NEW' | 'CREATED' | 'ERROR' | 'RUNNING' | 'REQUEST_RELEASE' | 'RELEASING'; | ||
taskId: string; | ||
proxyBytes: number; | ||
expiresAt: string; | ||
avg_cpu_usage: number; | ||
memory_usage: number; | ||
details: string; | ||
logs: string; | ||
}; | ||
export type UpdateSessionOptions = { | ||
projectId?: string; | ||
status: Session['status']; | ||
}; | ||
export type SessionRecording = { | ||
type: number; | ||
timestamp: number; | ||
data: object; | ||
}; | ||
export type SessionLog = { | ||
sessionId: string; | ||
id: string; | ||
timestamp: string; | ||
method: string; | ||
request: { | ||
timestamp: string; | ||
params: object; | ||
rawBody: string; | ||
}; | ||
response: { | ||
timestamp: string; | ||
result: object; | ||
rawBody: string; | ||
}; | ||
pageId: string; | ||
}; | ||
export type DebugConnectionURLs = { | ||
debuggerFullscreenUrl: string; | ||
debuggerUrl: string; | ||
wsUrl: string; | ||
}; | ||
export default class Browserbase { | ||
private apiKey; | ||
constructor(apiKey?: string); | ||
load(url: string | string[], options?: BrowserbaseLoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>; | ||
loadURL(url: string, options?: BrowserbaseLoadOptions): Promise<string>; | ||
loadURLs(urls: string[], options?: BrowserbaseLoadOptions): AsyncGenerator<string>; | ||
screenshot(url: string, options?: BrowserbaseScreenshotOptions): Promise<Buffer>; | ||
private projectId; | ||
private baseURL; | ||
constructor(options?: ClientOptions); | ||
listSessions(): Promise<Session[]>; | ||
createSession(options?: CreateSessionOptions): Promise<Session>; | ||
getSession(sessionId: string): Promise<Session>; | ||
updateSession(sessionId: string, options: UpdateSessionOptions): Promise<Session>; | ||
getSessionRecording(sessionId: string): Promise<SessionRecording[]>; | ||
getSessionLogs(sessionId: string): Promise<SessionLog[]>; | ||
getSessionDownloads(sessionId: string, filePath: string, retryInterval?: number, retryCount?: number): Promise<void>; | ||
getDebugConnectionURLs(sessionId: string): Promise<DebugConnectionURLs>; | ||
load(url: string | string[], options?: LoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>; | ||
loadURL(url: string, options?: LoadOptions): Promise<string>; | ||
loadURLs(urls: string[], options?: LoadOptions): AsyncGenerator<string>; | ||
screenshot(url: string, options?: ScreenshotOptions): Promise<Buffer>; | ||
} | ||
export { Browserbase }; |
import { chromium } from 'playwright'; | ||
import fs from 'fs/promises'; | ||
export default class Browserbase { | ||
apiKey; | ||
constructor(apiKey) { | ||
this.apiKey = apiKey || process.env.BROWSERBASE_API_KEY; | ||
projectId; | ||
baseURL; | ||
constructor(options = {}) { | ||
this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY; | ||
this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID; | ||
this.baseURL = options.baseURL || 'https://www.browserbase.com'; | ||
} | ||
async listSessions() { | ||
const response = await fetch(`${this.baseURL}/v1/sessions`, { | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
return await response.json(); | ||
} | ||
async createSession(options) { | ||
const response = await fetch(`${this.baseURL}/v1/sessions`, { | ||
method: 'POST', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ projectId: this.projectId, ...options }), | ||
}); | ||
return await response.json(); | ||
} | ||
async getSession(sessionId) { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, { | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
return await response.json(); | ||
} | ||
async updateSession(sessionId, options) { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, { | ||
method: 'POST', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ projectId: this.projectId, ...options }), | ||
}); | ||
return await response.json(); | ||
} | ||
async getSessionRecording(sessionId) { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/recording`, { | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
return await response.json(); | ||
} | ||
async getSessionLogs(sessionId) { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/logs`, { | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
return await response.json(); | ||
} | ||
async getSessionDownloads(sessionId, filePath, retryInterval = 2000, retryCount = 2) { | ||
return new Promise((resolve, reject) => { | ||
const fetchDownload = async () => { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/downloads`, { | ||
method: 'GET', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
}, | ||
}); | ||
const arrayBuffer = await response.arrayBuffer(); | ||
if (arrayBuffer.byteLength > 0) { | ||
const buffer = Buffer.from(arrayBuffer); | ||
await fs.writeFile(filePath, buffer); | ||
resolve(); | ||
} | ||
else { | ||
retryCount--; | ||
if (retryCount <= 0) { | ||
reject(); | ||
} | ||
setTimeout(fetchDownload, retryInterval); | ||
} | ||
}; | ||
fetchDownload(); | ||
}); | ||
} | ||
async getDebugConnectionURLs(sessionId) { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/debug`, { | ||
method: 'GET', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
const json = await response.json(); | ||
return json; | ||
} | ||
load(url, options = {}) { | ||
@@ -8,0 +108,0 @@ if (typeof url === 'string') { |
import { z } from 'zod'; | ||
import Browserbase, { BrowserbaseLoadOptions } from '../index.js'; | ||
export default function BrowserbaseAISDK(b: Browserbase, options?: BrowserbaseLoadOptions): { | ||
import Browserbase, { LoadOptions } from '../index.js'; | ||
export default function BrowserbaseAISDK(b: Browserbase, options?: LoadOptions): { | ||
description: string; | ||
@@ -5,0 +5,0 @@ parameters: z.ZodObject<{ |
{ | ||
"type": "module", | ||
"name": "@browserbasehq/sdk", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Browserbase JS SDK", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
241
src/index.ts
import { chromium } from 'playwright' | ||
import fs from 'fs/promises' | ||
export type BrowserbaseLoadOptions = { | ||
export type ClientOptions = { | ||
apiKey?: string | ||
projectId?: string | ||
baseURL?: string | ||
} | ||
export type LoadOptions = { | ||
sessionId?: string | ||
textContent?: boolean | ||
} | ||
export type BrowserbaseScreenshotOptions = { | ||
export type ScreenshotOptions = { | ||
fullPage?: boolean | ||
} | ||
export type CreateSessionOptions = { | ||
projectId?: string | ||
extensionId?: string | ||
fingerprint?: { | ||
browserListQuery?: string | ||
httpVersion?: 1 | 2 | ||
browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'> | ||
devices?: Array<'desktop' | 'mobile'> | ||
locales?: string[] | ||
operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'> | ||
screen?: { | ||
maxHeight?: number | ||
maxWidth?: number | ||
minHeight?: number | ||
minWidth?: number | ||
} | ||
} | ||
} | ||
export type Session = { | ||
id: string | ||
createdAt: string | ||
startedAt: string | ||
endedAt: string | ||
projectId: string | ||
status: | ||
| 'NEW' | ||
| 'CREATED' | ||
| 'ERROR' | ||
| 'RUNNING' | ||
| 'REQUEST_RELEASE' | ||
| 'RELEASING' | ||
taskId: string | ||
proxyBytes: number | ||
expiresAt: string | ||
avg_cpu_usage: number | ||
memory_usage: number | ||
details: string | ||
logs: string | ||
} | ||
export type UpdateSessionOptions = { | ||
projectId?: string | ||
status: Session['status'] | ||
} | ||
export type SessionRecording = { | ||
type: number | ||
timestamp: number | ||
data: object | ||
} | ||
export type SessionLog = { | ||
sessionId: string | ||
id: string | ||
timestamp: string | ||
method: string | ||
request: { | ||
timestamp: string | ||
params: object | ||
rawBody: string | ||
} | ||
response: { | ||
timestamp: string | ||
result: object | ||
rawBody: string | ||
} | ||
pageId: string | ||
} | ||
export type DebugConnectionURLs = { | ||
debuggerFullscreenUrl: string | ||
debuggerUrl: string | ||
wsUrl: string | ||
} | ||
export default class Browserbase { | ||
private apiKey: string | ||
private projectId: string | ||
private baseURL: string | ||
constructor(apiKey?: string) { | ||
this.apiKey = apiKey || process.env.BROWSERBASE_API_KEY! | ||
constructor(options: ClientOptions = {}) { | ||
this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY! | ||
this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID! | ||
this.baseURL = options.baseURL || 'https://www.browserbase.com' | ||
} | ||
load(url: string | string[], options: BrowserbaseLoadOptions = {}) { | ||
async listSessions(): Promise<Session[]> { | ||
const response = await fetch(`${this.baseURL}/v1/sessions`, { | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
return await response.json() | ||
} | ||
async createSession(options?: CreateSessionOptions): Promise<Session> { | ||
const response = await fetch(`${this.baseURL}/v1/sessions`, { | ||
method: 'POST', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ projectId: this.projectId, ...options }), | ||
}) | ||
return await response.json() | ||
} | ||
async getSession(sessionId: string): Promise<Session> { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, { | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
return await response.json() | ||
} | ||
async updateSession( | ||
sessionId: string, | ||
options: UpdateSessionOptions | ||
): Promise<Session> { | ||
const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, { | ||
method: 'POST', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ projectId: this.projectId, ...options }), | ||
}) | ||
return await response.json() | ||
} | ||
async getSessionRecording(sessionId: string): Promise<SessionRecording[]> { | ||
const response = await fetch( | ||
`${this.baseURL}/v1/sessions/${sessionId}/recording`, | ||
{ | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
) | ||
return await response.json() | ||
} | ||
async getSessionLogs(sessionId: string): Promise<SessionLog[]> { | ||
const response = await fetch( | ||
`${this.baseURL}/v1/sessions/${sessionId}/logs`, | ||
{ | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
) | ||
return await response.json() | ||
} | ||
async getSessionDownloads( | ||
sessionId: string, | ||
filePath: string, | ||
retryInterval: number = 2000, | ||
retryCount: number = 2 | ||
) { | ||
return new Promise<void>((resolve, reject) => { | ||
const fetchDownload = async () => { | ||
const response = await fetch( | ||
`${this.baseURL}/v1/sessions/${sessionId}/downloads`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
}, | ||
} | ||
) | ||
const arrayBuffer = await response.arrayBuffer() | ||
if (arrayBuffer.byteLength > 0) { | ||
const buffer = Buffer.from(arrayBuffer) | ||
await fs.writeFile(filePath, buffer) | ||
resolve() | ||
} else { | ||
retryCount-- | ||
if (retryCount <= 0) { | ||
reject() | ||
} | ||
setTimeout(fetchDownload, retryInterval) | ||
} | ||
} | ||
fetchDownload() | ||
}) | ||
} | ||
async getDebugConnectionURLs( | ||
sessionId: string | ||
): Promise<DebugConnectionURLs> { | ||
const response = await fetch( | ||
`${this.baseURL}/v1/sessions/${sessionId}/debug`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
'x-bb-api-key': this.apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
) | ||
const json = await response.json() | ||
return json | ||
} | ||
load(url: string | string[], options: LoadOptions = {}) { | ||
if (typeof url === 'string') { | ||
@@ -28,6 +250,3 @@ return this.loadURL(url, options) | ||
async loadURL( | ||
url: string, | ||
options: BrowserbaseLoadOptions = {} | ||
): Promise<string> { | ||
async loadURL(url: string, options: LoadOptions = {}): Promise<string> { | ||
if (!url) { | ||
@@ -62,3 +281,3 @@ throw new Error('Page URL was not provided') | ||
urls: string[], | ||
options: BrowserbaseLoadOptions = {} | ||
options: LoadOptions = {} | ||
): AsyncGenerator<string> { | ||
@@ -98,3 +317,3 @@ if (!urls.length) { | ||
url: string, | ||
options: BrowserbaseScreenshotOptions = { fullPage: false } | ||
options: ScreenshotOptions = { fullPage: false } | ||
): Promise<Buffer> { | ||
@@ -101,0 +320,0 @@ if (!url) { |
import { z } from 'zod' | ||
import Browserbase, { BrowserbaseLoadOptions } from '../index.js' | ||
import Browserbase, { LoadOptions } from '../index.js' | ||
export default function BrowserbaseAISDK( | ||
b: Browserbase, | ||
options: BrowserbaseLoadOptions = {} | ||
options: LoadOptions = {} | ||
) { | ||
@@ -8,0 +8,0 @@ return { |
@@ -13,2 +13,29 @@ import { describe, it, beforeEach } from 'node:test' | ||
it('shoud create and retrieve session', async () => { | ||
const { id } = await browserbase.createSession() | ||
const session = await browserbase.getSession(id) | ||
expect(session.status).to.equal('RUNNING') | ||
expect(session.id).to.equal(id) | ||
}) | ||
it('shoud create and update session', async () => { | ||
const { id } = await browserbase.createSession() | ||
const updated = await browserbase.updateSession(id, { status: 'RELEASING' }) | ||
expect(updated.status).to.equal('COMPLETED') | ||
}) | ||
it('shoud create a session and retrieve a recording', async () => { | ||
const { id } = await browserbase.createSession() | ||
const recording = await browserbase.getSessionRecording(id) | ||
expect(recording.length).to.equal(0) | ||
}) | ||
it('shoud create a session and get debug url', async () => { | ||
const { id } = await browserbase.createSession() | ||
const debug = await browserbase.getDebugConnectionURLs(id) | ||
}) | ||
it('should load a webpage', async () => { | ||
@@ -23,2 +50,3 @@ const result = await browserbase.load('https://example.com') | ||
}) | ||
expect(result).contain('Example Domain') | ||
@@ -25,0 +53,0 @@ }) |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
25267
677
3
8