@ceki/sdk 1.9.2 → 1.11.0

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.cts CHANGED
@@ -6,7 +6,7 @@ interface ConnectOptions {
6
6
  reconnect?: boolean;
7
7
  }
8
8
  interface BrowserOption {
9
- schedule_id: number;
9
+ browser_id: number;
10
10
  user_id?: number | null;
11
11
  geo?: string | null;
12
12
  language?: string | null;
@@ -23,7 +23,7 @@ interface BrowserOption {
23
23
  }
24
24
  interface Match {
25
25
  session_id: string;
26
- schedule_id: number;
26
+ browser_id: number;
27
27
  event_id?: string | null;
28
28
  chat_topic_id?: string | null;
29
29
  provider_user_id?: number | null;
@@ -91,7 +91,7 @@ interface ChatHistoryOptions {
91
91
  }
92
92
  interface SessionInfo {
93
93
  id: number;
94
- schedule_id: number;
94
+ browser_id: number;
95
95
  started_at: string | null;
96
96
  ended_at: string | null;
97
97
  status: string;
@@ -208,7 +208,6 @@ type UserEventHandler = (events: Record<string, unknown>[]) => void;
208
208
  declare class Browser {
209
209
  readonly sessionId: string;
210
210
  readonly browserId: number;
211
- readonly scheduleId: number;
212
211
  readonly chatTopicId: string | null;
213
212
  readonly browserInfo: Record<string, unknown>;
214
213
  readonly providerUserId: number | null;
@@ -254,7 +253,8 @@ declare class Browser {
254
253
  data: string;
255
254
  } | Buffer>;
256
255
  snapshot(): Promise<Snapshot>;
257
- upload(selector: string, source: string | Buffer, filename?: string): Promise<{
256
+ private static _detectMime;
257
+ upload(selector: string, source: string | Buffer, filename?: string, mime?: string): Promise<{
258
258
  ok: boolean;
259
259
  filename: string;
260
260
  size: number;
@@ -334,7 +334,7 @@ declare class Client {
334
334
  limit?: number;
335
335
  }): Promise<SessionInfo[]>;
336
336
  myBrowsers(): Promise<BrowserOption[]>;
337
- rent(scheduleId: number, opts?: RentOptions): Promise<Browser>;
337
+ rent(browserId: number, opts?: RentOptions): Promise<Browser>;
338
338
  resume(sessionId: string, opts?: RentOptions): Promise<Browser>;
339
339
  close(): Promise<void>;
340
340
  disconnect(): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ interface ConnectOptions {
6
6
  reconnect?: boolean;
7
7
  }
8
8
  interface BrowserOption {
9
- schedule_id: number;
9
+ browser_id: number;
10
10
  user_id?: number | null;
11
11
  geo?: string | null;
12
12
  language?: string | null;
@@ -23,7 +23,7 @@ interface BrowserOption {
23
23
  }
24
24
  interface Match {
25
25
  session_id: string;
26
- schedule_id: number;
26
+ browser_id: number;
27
27
  event_id?: string | null;
28
28
  chat_topic_id?: string | null;
29
29
  provider_user_id?: number | null;
@@ -91,7 +91,7 @@ interface ChatHistoryOptions {
91
91
  }
92
92
  interface SessionInfo {
93
93
  id: number;
94
- schedule_id: number;
94
+ browser_id: number;
95
95
  started_at: string | null;
96
96
  ended_at: string | null;
97
97
  status: string;
@@ -208,7 +208,6 @@ type UserEventHandler = (events: Record<string, unknown>[]) => void;
208
208
  declare class Browser {
209
209
  readonly sessionId: string;
210
210
  readonly browserId: number;
211
- readonly scheduleId: number;
212
211
  readonly chatTopicId: string | null;
213
212
  readonly browserInfo: Record<string, unknown>;
214
213
  readonly providerUserId: number | null;
@@ -254,7 +253,8 @@ declare class Browser {
254
253
  data: string;
255
254
  } | Buffer>;
256
255
  snapshot(): Promise<Snapshot>;
257
- upload(selector: string, source: string | Buffer, filename?: string): Promise<{
256
+ private static _detectMime;
257
+ upload(selector: string, source: string | Buffer, filename?: string, mime?: string): Promise<{
258
258
  ok: boolean;
259
259
  filename: string;
260
260
  size: number;
@@ -334,7 +334,7 @@ declare class Client {
334
334
  limit?: number;
335
335
  }): Promise<SessionInfo[]>;
336
336
  myBrowsers(): Promise<BrowserOption[]>;
337
- rent(scheduleId: number, opts?: RentOptions): Promise<Browser>;
337
+ rent(browserId: number, opts?: RentOptions): Promise<Browser>;
338
338
  resume(sessionId: string, opts?: RentOptions): Promise<Browser>;
339
339
  close(): Promise<void>;
340
340
  disconnect(): Promise<void>;
package/dist/index.js CHANGED
@@ -133,6 +133,9 @@ var ChatSendFailed = class extends CekiBrowserError {
133
133
  }
134
134
  };
135
135
 
136
+ // src/browser.ts
137
+ import mime from "mime-types";
138
+
136
139
  // src/chat.ts
137
140
  import * as crypto from "crypto";
138
141
  import * as fs from "fs";
@@ -198,7 +201,7 @@ var BrowserChat = class {
198
201
  if (buf.length > MAX_IMAGE_SIZE) {
199
202
  throw new Error(`Image too large: ${buf.length} bytes (max ${MAX_IMAGE_SIZE})`);
200
203
  }
201
- const { mime, ext } = detectMime(buf);
204
+ const { mime: mime2, ext } = detectMime(buf);
202
205
  if (!filename.includes(".")) {
203
206
  filename = `${filename}.${ext}`;
204
207
  }
@@ -209,7 +212,7 @@ var BrowserChat = class {
209
212
  session_id: this._browser.sessionId,
210
213
  client_msg_id: clientMsgId,
211
214
  filename,
212
- mime,
215
+ mime: mime2,
213
216
  data_b64
214
217
  };
215
218
  if (text) msg.text = text;
@@ -558,10 +561,9 @@ function keymapForChar(char) {
558
561
  }
559
562
 
560
563
  // src/browser.ts
561
- var Browser = class {
564
+ var Browser = class _Browser {
562
565
  sessionId;
563
566
  browserId;
564
- scheduleId;
565
567
  chatTopicId;
566
568
  browserInfo;
567
569
  providerUserId;
@@ -607,8 +609,7 @@ var Browser = class {
607
609
  constructor(client, match, humanizer) {
608
610
  this._client = client;
609
611
  this.sessionId = match.session_id;
610
- this.browserId = match.schedule_id;
611
- this.scheduleId = match.schedule_id;
612
+ this.browserId = match.browser_id;
612
613
  this.chatTopicId = match.chat_topic_id ?? null;
613
614
  this.browserInfo = match.browser_info ?? {};
614
615
  this.providerUserId = match.provider_user_id ?? null;
@@ -623,7 +624,7 @@ var Browser = class {
623
624
  saveSession(this.sessionId, {
624
625
  session_id: this.sessionId,
625
626
  chat_topic_id: this.chatTopicId,
626
- schedule_id: this.scheduleId,
627
+ browser_id: this.browserId,
627
628
  last_seen_ts: this._lastSeenTs
628
629
  });
629
630
  }
@@ -798,7 +799,10 @@ var Browser = class {
798
799
  ts: /* @__PURE__ */ new Date()
799
800
  };
800
801
  }
801
- async upload(selector, source, filename) {
802
+ static _detectMime(filename) {
803
+ return mime.lookup(filename) || "application/octet-stream";
804
+ }
805
+ async upload(selector, source, filename, mime2) {
802
806
  let buf;
803
807
  let resolvedFilename;
804
808
  if (typeof source === "string") {
@@ -810,6 +814,8 @@ var Browser = class {
810
814
  buf = Buffer.isBuffer(source) ? source : Buffer.from(source);
811
815
  resolvedFilename = filename ?? "file";
812
816
  }
817
+ const mimeType = mime2 ?? _Browser._detectMime(resolvedFilename);
818
+ console.info(`upload: file=${resolvedFilename} mime=${mimeType} size=${buf.length}`);
813
819
  const b64 = buf.toString("base64");
814
820
  const size = buf.length;
815
821
  const expression = `
@@ -820,7 +826,7 @@ var Browser = class {
820
826
  var binary = atob(b64);
821
827
  var bytes = new Uint8Array(binary.length);
822
828
  for (var i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
823
- var file = new File([bytes], ${JSON.stringify(resolvedFilename)}, {type: 'application/octet-stream'});
829
+ var file = new File([bytes], ${JSON.stringify(resolvedFilename)}, {type: ${JSON.stringify(mimeType)}});
824
830
  var dt = new DataTransfer();
825
831
  dt.items.add(file);
826
832
  input.files = dt.files;
@@ -833,6 +839,17 @@ var Browser = class {
833
839
  params: { expression, returnByValue: true }
834
840
  });
835
841
  const resultObj = result?.result;
842
+ try {
843
+ await this.send({
844
+ method: "Input.dispatchKeyEvent",
845
+ params: { type: "keyDown", key: "Escape", code: "Escape", windowsVirtualKeyCode: 27, nativeVirtualKeyCode: 27 }
846
+ });
847
+ await this.send({
848
+ method: "Input.dispatchKeyEvent",
849
+ params: { type: "keyUp", key: "Escape", code: "Escape", windowsVirtualKeyCode: 27, nativeVirtualKeyCode: 27 }
850
+ });
851
+ } catch {
852
+ }
836
853
  if (resultObj?.value) {
837
854
  return JSON.parse(String(resultObj.value));
838
855
  }
@@ -1376,7 +1393,7 @@ var Client = class _Client {
1376
1393
  _pongTimer = null;
1377
1394
  _lastPongAt = 0;
1378
1395
  _pendingRents = /* @__PURE__ */ new Map();
1379
- // keyed by `rent:<scheduleId>` or eventId
1396
+ // keyed by `rent:<browserId>` or eventId
1380
1397
  _pendingResumes = /* @__PURE__ */ new Map();
1381
1398
  // keyed by sessionId
1382
1399
  _connectResolve = null;
@@ -1467,18 +1484,18 @@ var Client = class _Client {
1467
1484
  const items = body.browsers ?? body.data ?? body;
1468
1485
  return Array.isArray(items) ? items : [];
1469
1486
  }
1470
- async rent(scheduleId, opts) {
1471
- const rentMsg = { type: "rent", browser_id: scheduleId };
1487
+ async rent(browserId, opts) {
1488
+ const rentMsg = { type: "rent", browser_id: browserId };
1472
1489
  if (opts?.mode) rentMsg.mode = opts.mode;
1473
1490
  this._wsSend(rentMsg);
1474
- const key = `rent:${scheduleId}`;
1491
+ const key = `rent:${browserId}`;
1475
1492
  return new Promise((resolve, reject) => {
1476
1493
  const timer = setTimeout(() => {
1477
1494
  this._pendingRents.delete(key);
1478
1495
  reject(new TimeoutError("Rent timed out after 90s"));
1479
1496
  }, 9e4);
1480
1497
  this._pendingRents.set(key, {
1481
- scheduleId,
1498
+ browserId,
1482
1499
  eventId: null,
1483
1500
  opts,
1484
1501
  resolve: (match) => {
@@ -1783,7 +1800,7 @@ var Client = class _Client {
1783
1800
  }
1784
1801
  _onMatch(msg) {
1785
1802
  const eventId = String(msg.event_id ?? "");
1786
- const scheduleId = Number(msg.schedule_id ?? 0);
1803
+ const browserId = Number(msg.browser_id ?? 0);
1787
1804
  const sessionId = String(msg.session_id ?? "");
1788
1805
  if (msg.requires_ack) {
1789
1806
  try {
@@ -1793,15 +1810,15 @@ var Client = class _Client {
1793
1810
  }
1794
1811
  let pending = this._pendingRents.get(`event:${eventId}`);
1795
1812
  if (!pending) {
1796
- pending = this._pendingRents.get(`rent:${scheduleId}`);
1813
+ pending = this._pendingRents.get(`rent:${browserId}`);
1797
1814
  }
1798
1815
  if (pending) {
1799
1816
  clearTimeout(pending.timer);
1800
- const key = pending.eventId ? `event:${pending.eventId}` : `rent:${pending.scheduleId}`;
1817
+ const key = pending.eventId ? `event:${pending.eventId}` : `rent:${pending.browserId}`;
1801
1818
  this._pendingRents.delete(key);
1802
1819
  const match = {
1803
1820
  session_id: sessionId,
1804
- schedule_id: scheduleId,
1821
+ browser_id: browserId,
1805
1822
  event_id: eventId || null,
1806
1823
  chat_topic_id: msg.chat_topic_id ? String(msg.chat_topic_id) : null,
1807
1824
  provider_user_id: msg.provider_user_id != null ? Number(msg.provider_user_id) : null,
@@ -1843,7 +1860,7 @@ var Client = class _Client {
1843
1860
  const match = {
1844
1861
  session_id: sessionId,
1845
1862
  event_id: msg.event_id != null ? String(msg.event_id) : null,
1846
- schedule_id: Number(msg.schedule_id ?? 0),
1863
+ browser_id: Number(msg.browser_id ?? 0),
1847
1864
  chat_topic_id: msg.chat_topic_id ? String(msg.chat_topic_id) : null,
1848
1865
  provider_user_id: msg.provider_user_id != null ? Number(msg.provider_user_id) : null,
1849
1866
  started_at: Date.now(),