@browserbasehq/sdk 1.1.3 → 1.1.5

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/dist/index.d.ts CHANGED
@@ -53,8 +53,8 @@ export type UpdateSessionOptions = {
53
53
  status: Session['status'];
54
54
  };
55
55
  export type SessionRecording = {
56
- type?: number;
57
- timestamp?: number;
56
+ type?: string;
57
+ time?: string;
58
58
  data?: object;
59
59
  };
60
60
  export type SessionLog = {
@@ -85,15 +85,15 @@ export default class Browserbase {
85
85
  private baseAPIURL;
86
86
  private baseConnectURL;
87
87
  constructor(options?: ClientOptions);
88
- connectURL({ sessionId, proxy }?: ConnectOptions): string;
88
+ getConnectURL({ sessionId, proxy }?: ConnectOptions): string;
89
89
  listSessions(): Promise<Session[]>;
90
90
  createSession(options?: CreateSessionOptions): Promise<Session>;
91
91
  getSession(sessionId: string): Promise<Session>;
92
92
  updateSession(sessionId: string, options: UpdateSessionOptions): Promise<Session>;
93
93
  getSessionRecording(sessionId: string): Promise<SessionRecording[]>;
94
- getSessionLogs(sessionId: string): Promise<SessionLog[]>;
95
- getSessionDownloads(sessionId: string, filePath: string, retryInterval?: number, retryCount?: number): Promise<void>;
94
+ getSessionDownloads(sessionId: string, retryInterval?: number, retryCount?: number): Promise<Buffer>;
96
95
  getDebugConnectionURLs(sessionId: string): Promise<DebugConnectionURLs>;
96
+ getSessionLogs(sessionId: string): Promise<SessionLog[]>;
97
97
  load(url: string | string[], options?: LoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>;
98
98
  loadURL(url: string, options?: LoadOptions): Promise<string>;
99
99
  loadURLs(urls: string[], options?: LoadOptions): AsyncGenerator<string>;
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { chromium } from 'playwright';
2
- import fs from 'fs/promises';
3
2
  export default class Browserbase {
4
3
  apiKey;
5
4
  projectId;
@@ -12,7 +11,7 @@ export default class Browserbase {
12
11
  this.baseConnectURL =
13
12
  options.baseConnectURL || 'wss://connect.browserbase.com';
14
13
  }
15
- connectURL({ sessionId, proxy = false } = {}) {
14
+ getConnectURL({ sessionId, proxy = false } = {}) {
16
15
  return `${this.baseConnectURL}?apiKey=${this.apiKey}${sessionId ? `&sessionId=${sessionId}` : ''}${proxy ? `&enableProxy=true` : ''}`;
17
16
  }
18
17
  async listSessions() {
@@ -64,16 +63,7 @@ export default class Browserbase {
64
63
  });
65
64
  return await response.json();
66
65
  }
67
- async getSessionLogs(sessionId) {
68
- const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}/logs`, {
69
- headers: {
70
- 'x-bb-api-key': this.apiKey,
71
- 'Content-Type': 'application/json',
72
- },
73
- });
74
- return await response.json();
75
- }
76
- async getSessionDownloads(sessionId, filePath, retryInterval = 2000, retryCount = 2) {
66
+ async getSessionDownloads(sessionId, retryInterval = 2000, retryCount = 2) {
77
67
  return new Promise((resolve, reject) => {
78
68
  const fetchDownload = async () => {
79
69
  const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}/downloads`, {
@@ -84,9 +74,7 @@ export default class Browserbase {
84
74
  });
85
75
  const arrayBuffer = await response.arrayBuffer();
86
76
  if (arrayBuffer.byteLength > 0) {
87
- const buffer = Buffer.from(arrayBuffer);
88
- await fs.writeFile(filePath, buffer);
89
- resolve();
77
+ resolve(Buffer.from(arrayBuffer));
90
78
  }
91
79
  else {
92
80
  retryCount--;
@@ -110,6 +98,15 @@ export default class Browserbase {
110
98
  const json = await response.json();
111
99
  return json;
112
100
  }
101
+ async getSessionLogs(sessionId) {
102
+ const response = await fetch(`${this.baseAPIURL}/v1/sessions/${sessionId}/logs`, {
103
+ headers: {
104
+ 'x-bb-api-key': this.apiKey,
105
+ 'Content-Type': 'application/json',
106
+ },
107
+ });
108
+ return await response.json();
109
+ }
113
110
  load(url, options = {}) {
114
111
  if (typeof url === 'string') {
115
112
  return this.loadURL(url, options);
@@ -125,7 +122,7 @@ export default class Browserbase {
125
122
  if (!url) {
126
123
  throw new Error('Page URL was not provided');
127
124
  }
128
- const browser = await chromium.connectOverCDP(this.connectURL({ sessionId: options.sessionId, proxy: options.proxy }));
125
+ const browser = await chromium.connectOverCDP(this.getConnectURL({ sessionId: options.sessionId, proxy: options.proxy }));
129
126
  const defaultContext = browser.contexts()[0];
130
127
  const page = defaultContext.pages()[0];
131
128
  await page.goto(url);
@@ -146,7 +143,7 @@ export default class Browserbase {
146
143
  if (!urls.length) {
147
144
  throw new Error('Page URLs were not provided');
148
145
  }
149
- const browser = await chromium.connectOverCDP(this.connectURL({ sessionId: options.sessionId, proxy: options.proxy }));
146
+ const browser = await chromium.connectOverCDP(this.getConnectURL({ sessionId: options.sessionId, proxy: options.proxy }));
150
147
  const defaultContext = browser.contexts()[0];
151
148
  const page = defaultContext.pages()[0];
152
149
  for (const url of urls) {
@@ -169,7 +166,7 @@ export default class Browserbase {
169
166
  if (!url) {
170
167
  throw new Error('Page URL was not provided');
171
168
  }
172
- const browser = await chromium.connectOverCDP(this.connectURL({ sessionId: options.sessionId, proxy: options.proxy }));
169
+ const browser = await chromium.connectOverCDP(this.getConnectURL({ sessionId: options.sessionId, proxy: options.proxy }));
173
170
  const page = await browser.newPage();
174
171
  await page.goto(url);
175
172
  const screenshot = await page.screenshot({ fullPage: options.fullPage });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@browserbasehq/sdk",
4
- "version": "1.1.3",
4
+ "version": "1.1.5",
5
5
  "description": "Browserbase JS SDK",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { chromium } from 'playwright'
2
- import fs from 'fs/promises'
3
2
 
4
3
  export type ClientOptions = {
5
4
  apiKey?: string
@@ -69,8 +68,8 @@ export type UpdateSessionOptions = {
69
68
  }
70
69
 
71
70
  export type SessionRecording = {
72
- type?: number
73
- timestamp?: number
71
+ type?: string
72
+ time?: string
74
73
  data?: object
75
74
  }
76
75
 
@@ -112,7 +111,7 @@ export default class Browserbase {
112
111
  options.baseConnectURL || 'wss://connect.browserbase.com'
113
112
  }
114
113
 
115
- connectURL({ sessionId, proxy = false }: ConnectOptions = {}): string {
114
+ getConnectURL({ sessionId, proxy = false }: ConnectOptions = {}): string {
116
115
  return `${this.baseConnectURL}?apiKey=${this.apiKey}${
117
116
  sessionId ? `&sessionId=${sessionId}` : ''
118
117
  }${proxy ? `&enableProxy=true` : ''}`
@@ -189,27 +188,12 @@ export default class Browserbase {
189
188
  return await response.json()
190
189
  }
191
190
 
192
- async getSessionLogs(sessionId: string): Promise<SessionLog[]> {
193
- const response = await fetch(
194
- `${this.baseAPIURL}/v1/sessions/${sessionId}/logs`,
195
- {
196
- headers: {
197
- 'x-bb-api-key': this.apiKey,
198
- 'Content-Type': 'application/json',
199
- },
200
- }
201
- )
202
-
203
- return await response.json()
204
- }
205
-
206
191
  async getSessionDownloads(
207
192
  sessionId: string,
208
- filePath: string,
209
193
  retryInterval: number = 2000,
210
194
  retryCount: number = 2
211
195
  ) {
212
- return new Promise<void>((resolve, reject) => {
196
+ return new Promise<Buffer>((resolve, reject) => {
213
197
  const fetchDownload = async () => {
214
198
  const response = await fetch(
215
199
  `${this.baseAPIURL}/v1/sessions/${sessionId}/downloads`,
@@ -223,9 +207,7 @@ export default class Browserbase {
223
207
 
224
208
  const arrayBuffer = await response.arrayBuffer()
225
209
  if (arrayBuffer.byteLength > 0) {
226
- const buffer = Buffer.from(arrayBuffer)
227
- await fs.writeFile(filePath, buffer)
228
- resolve()
210
+ resolve(Buffer.from(arrayBuffer))
229
211
  } else {
230
212
  retryCount--
231
213
  if (retryCount <= 0) {
@@ -258,6 +240,20 @@ export default class Browserbase {
258
240
  return json
259
241
  }
260
242
 
243
+ async getSessionLogs(sessionId: string): Promise<SessionLog[]> {
244
+ const response = await fetch(
245
+ `${this.baseAPIURL}/v1/sessions/${sessionId}/logs`,
246
+ {
247
+ headers: {
248
+ 'x-bb-api-key': this.apiKey,
249
+ 'Content-Type': 'application/json',
250
+ },
251
+ }
252
+ )
253
+
254
+ return await response.json()
255
+ }
256
+
261
257
  load(url: string | string[], options: LoadOptions = {}) {
262
258
  if (typeof url === 'string') {
263
259
  return this.loadURL(url, options)
@@ -274,7 +270,7 @@ export default class Browserbase {
274
270
  }
275
271
 
276
272
  const browser = await chromium.connectOverCDP(
277
- this.connectURL({ sessionId: options.sessionId, proxy: options.proxy })
273
+ this.getConnectURL({ sessionId: options.sessionId, proxy: options.proxy })
278
274
  )
279
275
  const defaultContext = browser.contexts()[0]
280
276
  const page = defaultContext.pages()[0]
@@ -305,7 +301,7 @@ export default class Browserbase {
305
301
  }
306
302
 
307
303
  const browser = await chromium.connectOverCDP(
308
- this.connectURL({ sessionId: options.sessionId, proxy: options.proxy })
304
+ this.getConnectURL({ sessionId: options.sessionId, proxy: options.proxy })
309
305
  )
310
306
  const defaultContext = browser.contexts()[0]
311
307
  const page = defaultContext.pages()[0]
@@ -340,7 +336,7 @@ export default class Browserbase {
340
336
  }
341
337
 
342
338
  const browser = await chromium.connectOverCDP(
343
- this.connectURL({ sessionId: options.sessionId, proxy: options.proxy })
339
+ this.getConnectURL({ sessionId: options.sessionId, proxy: options.proxy })
344
340
  )
345
341
  const page = await browser.newPage()
346
342
  await page.goto(url)
package/test/lib.test.ts CHANGED
@@ -10,6 +10,11 @@ describe('Browserbase', () => {
10
10
  browserbase = new Browserbase()
11
11
  })
12
12
 
13
+ it('shoud list sessions', async () => {
14
+ const session = await browserbase.listSessions()
15
+ expect(session[0].status).to.equal('COMPLETED')
16
+ })
17
+
13
18
  it('shoud create and retrieve session', async () => {
14
19
  const { id } = await browserbase.createSession()
15
20
  const session = await browserbase.getSession(id)