@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.cjs CHANGED
@@ -192,6 +192,9 @@ var ChatSendFailed = class extends CekiBrowserError {
192
192
  }
193
193
  };
194
194
 
195
+ // src/browser.ts
196
+ var import_mime_types = __toESM(require("mime-types"), 1);
197
+
195
198
  // src/chat.ts
196
199
  var crypto = __toESM(require("crypto"), 1);
197
200
  var fs = __toESM(require("fs"), 1);
@@ -257,7 +260,7 @@ var BrowserChat = class {
257
260
  if (buf.length > MAX_IMAGE_SIZE) {
258
261
  throw new Error(`Image too large: ${buf.length} bytes (max ${MAX_IMAGE_SIZE})`);
259
262
  }
260
- const { mime, ext } = detectMime(buf);
263
+ const { mime: mime2, ext } = detectMime(buf);
261
264
  if (!filename.includes(".")) {
262
265
  filename = `${filename}.${ext}`;
263
266
  }
@@ -268,7 +271,7 @@ var BrowserChat = class {
268
271
  session_id: this._browser.sessionId,
269
272
  client_msg_id: clientMsgId,
270
273
  filename,
271
- mime,
274
+ mime: mime2,
272
275
  data_b64
273
276
  };
274
277
  if (text) msg.text = text;
@@ -617,10 +620,9 @@ function keymapForChar(char) {
617
620
  }
618
621
 
619
622
  // src/browser.ts
620
- var Browser = class {
623
+ var Browser = class _Browser {
621
624
  sessionId;
622
625
  browserId;
623
- scheduleId;
624
626
  chatTopicId;
625
627
  browserInfo;
626
628
  providerUserId;
@@ -666,8 +668,7 @@ var Browser = class {
666
668
  constructor(client, match, humanizer) {
667
669
  this._client = client;
668
670
  this.sessionId = match.session_id;
669
- this.browserId = match.schedule_id;
670
- this.scheduleId = match.schedule_id;
671
+ this.browserId = match.browser_id;
671
672
  this.chatTopicId = match.chat_topic_id ?? null;
672
673
  this.browserInfo = match.browser_info ?? {};
673
674
  this.providerUserId = match.provider_user_id ?? null;
@@ -682,7 +683,7 @@ var Browser = class {
682
683
  saveSession(this.sessionId, {
683
684
  session_id: this.sessionId,
684
685
  chat_topic_id: this.chatTopicId,
685
- schedule_id: this.scheduleId,
686
+ browser_id: this.browserId,
686
687
  last_seen_ts: this._lastSeenTs
687
688
  });
688
689
  }
@@ -857,7 +858,10 @@ var Browser = class {
857
858
  ts: /* @__PURE__ */ new Date()
858
859
  };
859
860
  }
860
- async upload(selector, source, filename) {
861
+ static _detectMime(filename) {
862
+ return import_mime_types.default.lookup(filename) || "application/octet-stream";
863
+ }
864
+ async upload(selector, source, filename, mime2) {
861
865
  let buf;
862
866
  let resolvedFilename;
863
867
  if (typeof source === "string") {
@@ -869,6 +873,8 @@ var Browser = class {
869
873
  buf = Buffer.isBuffer(source) ? source : Buffer.from(source);
870
874
  resolvedFilename = filename ?? "file";
871
875
  }
876
+ const mimeType = mime2 ?? _Browser._detectMime(resolvedFilename);
877
+ console.info(`upload: file=${resolvedFilename} mime=${mimeType} size=${buf.length}`);
872
878
  const b64 = buf.toString("base64");
873
879
  const size = buf.length;
874
880
  const expression = `
@@ -879,7 +885,7 @@ var Browser = class {
879
885
  var binary = atob(b64);
880
886
  var bytes = new Uint8Array(binary.length);
881
887
  for (var i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
882
- var file = new File([bytes], ${JSON.stringify(resolvedFilename)}, {type: 'application/octet-stream'});
888
+ var file = new File([bytes], ${JSON.stringify(resolvedFilename)}, {type: ${JSON.stringify(mimeType)}});
883
889
  var dt = new DataTransfer();
884
890
  dt.items.add(file);
885
891
  input.files = dt.files;
@@ -892,6 +898,17 @@ var Browser = class {
892
898
  params: { expression, returnByValue: true }
893
899
  });
894
900
  const resultObj = result?.result;
901
+ try {
902
+ await this.send({
903
+ method: "Input.dispatchKeyEvent",
904
+ params: { type: "keyDown", key: "Escape", code: "Escape", windowsVirtualKeyCode: 27, nativeVirtualKeyCode: 27 }
905
+ });
906
+ await this.send({
907
+ method: "Input.dispatchKeyEvent",
908
+ params: { type: "keyUp", key: "Escape", code: "Escape", windowsVirtualKeyCode: 27, nativeVirtualKeyCode: 27 }
909
+ });
910
+ } catch {
911
+ }
895
912
  if (resultObj?.value) {
896
913
  return JSON.parse(String(resultObj.value));
897
914
  }
@@ -1435,7 +1452,7 @@ var Client = class _Client {
1435
1452
  _pongTimer = null;
1436
1453
  _lastPongAt = 0;
1437
1454
  _pendingRents = /* @__PURE__ */ new Map();
1438
- // keyed by `rent:<scheduleId>` or eventId
1455
+ // keyed by `rent:<browserId>` or eventId
1439
1456
  _pendingResumes = /* @__PURE__ */ new Map();
1440
1457
  // keyed by sessionId
1441
1458
  _connectResolve = null;
@@ -1526,18 +1543,18 @@ var Client = class _Client {
1526
1543
  const items = body.browsers ?? body.data ?? body;
1527
1544
  return Array.isArray(items) ? items : [];
1528
1545
  }
1529
- async rent(scheduleId, opts) {
1530
- const rentMsg = { type: "rent", browser_id: scheduleId };
1546
+ async rent(browserId, opts) {
1547
+ const rentMsg = { type: "rent", browser_id: browserId };
1531
1548
  if (opts?.mode) rentMsg.mode = opts.mode;
1532
1549
  this._wsSend(rentMsg);
1533
- const key = `rent:${scheduleId}`;
1550
+ const key = `rent:${browserId}`;
1534
1551
  return new Promise((resolve, reject) => {
1535
1552
  const timer = setTimeout(() => {
1536
1553
  this._pendingRents.delete(key);
1537
1554
  reject(new TimeoutError("Rent timed out after 90s"));
1538
1555
  }, 9e4);
1539
1556
  this._pendingRents.set(key, {
1540
- scheduleId,
1557
+ browserId,
1541
1558
  eventId: null,
1542
1559
  opts,
1543
1560
  resolve: (match) => {
@@ -1842,7 +1859,7 @@ var Client = class _Client {
1842
1859
  }
1843
1860
  _onMatch(msg) {
1844
1861
  const eventId = String(msg.event_id ?? "");
1845
- const scheduleId = Number(msg.schedule_id ?? 0);
1862
+ const browserId = Number(msg.browser_id ?? 0);
1846
1863
  const sessionId = String(msg.session_id ?? "");
1847
1864
  if (msg.requires_ack) {
1848
1865
  try {
@@ -1852,15 +1869,15 @@ var Client = class _Client {
1852
1869
  }
1853
1870
  let pending = this._pendingRents.get(`event:${eventId}`);
1854
1871
  if (!pending) {
1855
- pending = this._pendingRents.get(`rent:${scheduleId}`);
1872
+ pending = this._pendingRents.get(`rent:${browserId}`);
1856
1873
  }
1857
1874
  if (pending) {
1858
1875
  clearTimeout(pending.timer);
1859
- const key = pending.eventId ? `event:${pending.eventId}` : `rent:${pending.scheduleId}`;
1876
+ const key = pending.eventId ? `event:${pending.eventId}` : `rent:${pending.browserId}`;
1860
1877
  this._pendingRents.delete(key);
1861
1878
  const match = {
1862
1879
  session_id: sessionId,
1863
- schedule_id: scheduleId,
1880
+ browser_id: browserId,
1864
1881
  event_id: eventId || null,
1865
1882
  chat_topic_id: msg.chat_topic_id ? String(msg.chat_topic_id) : null,
1866
1883
  provider_user_id: msg.provider_user_id != null ? Number(msg.provider_user_id) : null,
@@ -1902,7 +1919,7 @@ var Client = class _Client {
1902
1919
  const match = {
1903
1920
  session_id: sessionId,
1904
1921
  event_id: msg.event_id != null ? String(msg.event_id) : null,
1905
- schedule_id: Number(msg.schedule_id ?? 0),
1922
+ browser_id: Number(msg.browser_id ?? 0),
1906
1923
  chat_topic_id: msg.chat_topic_id ? String(msg.chat_topic_id) : null,
1907
1924
  provider_user_id: msg.provider_user_id != null ? Number(msg.provider_user_id) : null,
1908
1925
  started_at: Date.now(),