nothing-browser 0.0.20 → 0.1.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.
Files changed (76) hide show
  1. package/LICENSE +1 -1
  2. package/dist/human/index.js +25 -1
  3. package/dist/launch/detect.js +13 -0
  4. package/dist/launch/spawn.js +13 -0
  5. package/dist/piggy/captcha/index.d.ts +39 -0
  6. package/dist/piggy/captcha/index.d.ts.map +1 -0
  7. package/dist/piggy/capture/index.d.ts +48 -0
  8. package/dist/piggy/capture/index.d.ts.map +1 -0
  9. package/dist/piggy/dialog/index.d.ts +28 -0
  10. package/dist/piggy/dialog/index.d.ts.map +1 -0
  11. package/dist/piggy/export/index.d.ts +62 -0
  12. package/dist/piggy/export/index.d.ts.map +1 -0
  13. package/dist/piggy/find/index.d.ts +90 -0
  14. package/dist/piggy/find/index.d.ts.map +1 -0
  15. package/dist/piggy/http/index.d.ts +14 -0
  16. package/dist/piggy/http/index.d.ts.map +1 -0
  17. package/dist/piggy/human/index.d.ts +36 -4
  18. package/dist/piggy/human/index.d.ts.map +1 -1
  19. package/dist/piggy/iframe/index.d.ts +53 -0
  20. package/dist/piggy/iframe/index.d.ts.map +1 -0
  21. package/dist/piggy/interactions/index.d.ts +24 -0
  22. package/dist/piggy/interactions/index.d.ts.map +1 -0
  23. package/dist/piggy/launch/detect.d.ts +1 -1
  24. package/dist/piggy/launch/detect.d.ts.map +1 -1
  25. package/dist/piggy/launch/spawn.d.ts.map +1 -1
  26. package/dist/piggy/media/index.d.ts +11 -0
  27. package/dist/piggy/media/index.d.ts.map +1 -0
  28. package/dist/piggy/navigation/index.d.ts +16 -0
  29. package/dist/piggy/navigation/index.d.ts.map +1 -0
  30. package/dist/piggy/provide/index.d.ts +81 -0
  31. package/dist/piggy/provide/index.d.ts.map +1 -0
  32. package/dist/piggy/proxy/index.d.ts +94 -0
  33. package/dist/piggy/proxy/index.d.ts.map +1 -0
  34. package/dist/piggy/register/index.d.ts.map +1 -1
  35. package/dist/piggy/router/index.d.ts +38 -0
  36. package/dist/piggy/router/index.d.ts.map +1 -0
  37. package/dist/piggy/session/index.d.ts +27 -0
  38. package/dist/piggy/session/index.d.ts.map +1 -0
  39. package/dist/piggy/tabs/index.d.ts +10 -0
  40. package/dist/piggy/tabs/index.d.ts.map +1 -0
  41. package/dist/piggy/wait/index.d.ts +28 -0
  42. package/dist/piggy/wait/index.d.ts.map +1 -0
  43. package/dist/piggy.d.ts.map +1 -1
  44. package/dist/piggy.js +914 -181
  45. package/dist/register/index.js +39 -86
  46. package/package.json +1 -1
  47. package/piggy/captcha/index.d.ts +35 -0
  48. package/piggy/captcha/index.ts +93 -0
  49. package/piggy/capture/index.ts +76 -0
  50. package/piggy/dialog/index.d.ts +29 -0
  51. package/piggy/dialog/index.ts +85 -0
  52. package/piggy/export/index.d.ts +117 -0
  53. package/piggy/export/index.ts +147 -0
  54. package/piggy/find/index.d.ts +79 -0
  55. package/piggy/find/index.ts +165 -0
  56. package/piggy/http/index.ts +65 -0
  57. package/piggy/human/index.ts +115 -53
  58. package/piggy/iframe/index.ts +79 -0
  59. package/piggy/interactions/index.ts +79 -0
  60. package/piggy/launch/detect.ts +19 -2
  61. package/piggy/launch/spawn.ts +1 -8
  62. package/piggy/media/index.ts +46 -0
  63. package/piggy/navigation/index.ts +52 -0
  64. package/piggy/provide/index.ts +144 -0
  65. package/piggy/proxy/index.ts +154 -0
  66. package/piggy/register/index.ts +41 -59
  67. package/piggy/router/index.ts +69 -0
  68. package/piggy/session/index.ts +76 -0
  69. package/piggy/tabs/index.ts +22 -0
  70. package/piggy/wait/index.ts +90 -0
  71. package/piggy.ts +94 -39
  72. package/dist/piggy/open/index.d.ts +0 -4
  73. package/dist/piggy/open/index.d.ts.map +0 -1
  74. package/piggy/open/index.d.ts +0 -4
  75. package/piggy/open/index.d.ts.map +0 -1
  76. package/piggy/open/index.ts +0 -5
package/dist/piggy.js CHANGED
@@ -6871,6 +6871,19 @@ var BINARY_NAMES = {
6871
6871
  };
6872
6872
  function detectBinary(mode = "headless") {
6873
6873
  const cwd = process.cwd();
6874
+ if (mode !== "headless" && mode !== "headful") {
6875
+ if (existsSync(mode)) {
6876
+ logger_default.success(`Binary found (custom path): ${mode}`);
6877
+ return mode;
6878
+ }
6879
+ const abs = join(cwd, mode);
6880
+ if (existsSync(abs)) {
6881
+ logger_default.success(`Binary found (custom path): ${abs}`);
6882
+ return abs;
6883
+ }
6884
+ logger_default.error(`Binary not found at custom path: ${mode}`);
6885
+ return null;
6886
+ }
6874
6887
  const name = BINARY_NAMES[mode];
6875
6888
  if (process.platform === "win32") {
6876
6889
  const p2 = join(cwd, `${name}.exe`);
@@ -7555,6 +7568,29 @@ class PiggyClient {
7555
7568
  }
7556
7569
  }
7557
7570
 
7571
+ // piggy/human/index.ts
7572
+ class HumanClient {
7573
+ client;
7574
+ constructor(client) {
7575
+ this.client = client;
7576
+ }
7577
+ set(opts, tabId = "default") {
7578
+ return this.client.send("human.set", { ...opts, tabId });
7579
+ }
7580
+ get(tabId = "default") {
7581
+ return this.client.send("human.get", { tabId });
7582
+ }
7583
+ type(opts, tabId = "default") {
7584
+ return this.client.send("human.type", { ...opts, tabId });
7585
+ }
7586
+ click(opts, tabId = "default") {
7587
+ return this.client.send("human.click", { ...opts, tabId });
7588
+ }
7589
+ }
7590
+ function createHumanAPI(client) {
7591
+ return new HumanClient(client);
7592
+ }
7593
+
7558
7594
  // node_modules/memoirist/dist/index.mjs
7559
7595
  var createNode = (part, inert) => {
7560
7596
  const inertMap = inert?.length ? {} : null;
@@ -28284,61 +28320,6 @@ function stopServer() {
28284
28320
  _app = null;
28285
28321
  }
28286
28322
 
28287
- // piggy/human/index.ts
28288
- function randomDelay(min, max) {
28289
- return new Promise((r) => setTimeout(r, Math.floor(Math.random() * (max - min + 1)) + min));
28290
- }
28291
- function humanTypeSequence(text) {
28292
- const adjacent = {
28293
- a: ["q", "w", "s", "z"],
28294
- b: ["v", "g", "h", "n"],
28295
- c: ["x", "d", "f", "v"],
28296
- d: ["s", "e", "r", "f", "c", "x"],
28297
- e: ["w", "r", "d", "s"],
28298
- f: ["d", "r", "t", "g", "v", "c"],
28299
- g: ["f", "t", "y", "h", "b", "v"],
28300
- h: ["g", "y", "u", "j", "n", "b"],
28301
- i: ["u", "o", "k", "j"],
28302
- j: ["h", "u", "i", "k", "m", "n"],
28303
- k: ["j", "i", "o", "l", "m"],
28304
- l: ["k", "o", "p"],
28305
- m: ["n", "j", "k"],
28306
- n: ["b", "h", "j", "m"],
28307
- o: ["i", "p", "l", "k"],
28308
- p: ["o", "l"],
28309
- q: ["w", "a"],
28310
- r: ["e", "t", "f", "d"],
28311
- s: ["a", "w", "e", "d", "x", "z"],
28312
- t: ["r", "y", "g", "f"],
28313
- u: ["y", "i", "h", "j"],
28314
- v: ["c", "f", "g", "b"],
28315
- w: ["q", "e", "a", "s"],
28316
- x: ["z", "s", "d", "c"],
28317
- y: ["t", "u", "g", "h"],
28318
- z: ["a", "s", "x"]
28319
- };
28320
- const actions = [];
28321
- const typoIndices = new Set;
28322
- if (text.length > 4) {
28323
- let tries = 0;
28324
- while (typoIndices.size < 2 && tries < 20) {
28325
- typoIndices.add(Math.floor(Math.random() * text.length));
28326
- tries++;
28327
- }
28328
- }
28329
- for (let i = 0;i < text.length; i++) {
28330
- if (typoIndices.has(i)) {
28331
- const ch = text[i].toLowerCase();
28332
- const neighbors = adjacent[ch];
28333
- const typo = neighbors ? neighbors[Math.floor(Math.random() * neighbors.length)] ?? ch : ch;
28334
- actions.push(typo);
28335
- actions.push("BACKSPACE");
28336
- }
28337
- actions.push(text[i]);
28338
- }
28339
- return actions;
28340
- }
28341
-
28342
28323
  // piggy/intercept/scripts.ts
28343
28324
  function buildRespondScript(pattern, status2, contentType, body) {
28344
28325
  const safePattern = pattern.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
@@ -28749,7 +28730,7 @@ function createSiteObject(name, registeredUrl, client, tabId, pool) {
28749
28730
  },
28750
28731
  click: (selector, opts) => withErrScreen(() => withTab((t2) => retry(name, async () => {
28751
28732
  if (humanMode)
28752
- await randomDelay(80, 220);
28733
+ await new Promise((r) => setTimeout(r, 80 + Math.random() * 140));
28753
28734
  await client.waitForSelector(selector, opts?.timeout ?? 15000, t2);
28754
28735
  const ok = await client.click(selector, t2);
28755
28736
  if (!ok)
@@ -28759,48 +28740,36 @@ function createSiteObject(name, registeredUrl, client, tabId, pool) {
28759
28740
  }, opts?.retries ?? 2)), `click(${selector})`),
28760
28741
  doubleClick: (selector) => withErrScreen(() => withTab(async (t2) => {
28761
28742
  if (humanMode)
28762
- await randomDelay(80, 200);
28743
+ await new Promise((r) => setTimeout(r, 80 + Math.random() * 120));
28763
28744
  return client.doubleClick(selector, t2);
28764
28745
  }), `dblclick(${selector})`),
28765
28746
  hover: (selector) => withErrScreen(() => withTab(async (t2) => {
28766
28747
  if (humanMode)
28767
- await randomDelay(50, 150);
28748
+ await new Promise((r) => setTimeout(r, 50 + Math.random() * 100));
28768
28749
  return client.hover(selector, t2);
28769
28750
  }), `hover(${selector})`),
28770
28751
  type: (selector, text, opts) => withErrScreen(() => withTab(async (t2) => {
28771
28752
  await client.waitForSelector(selector, 30000, t2);
28772
- if (humanMode && !opts?.fact) {
28773
- const seq = humanTypeSequence(text);
28774
- let current = "";
28775
- for (const action of seq) {
28776
- if (action === "BACKSPACE")
28777
- current = current.slice(0, -1);
28778
- else
28779
- current += action;
28753
+ if (humanMode) {
28754
+ const human = new HumanClient(client);
28755
+ await human.type({
28756
+ selector,
28757
+ text,
28758
+ clear: opts?.clear ?? false,
28759
+ speed: opts?.speed
28760
+ }, t2);
28761
+ } else {
28762
+ if (opts?.clear) {
28780
28763
  await client.evaluate(`
28781
- (function() {
28782
- const el = document.querySelector('${selector}');
28783
- const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
28784
- nativeSetter.call(el, '${current.replace(/'/g, "\\'")}');
28785
- el.dispatchEvent(new Event('input', { bubbles: true }));
28786
- el.dispatchEvent(new Event('change', { bubbles: true }));
28787
- })()
28788
- `, t2);
28789
- const wpm = opts?.wpm ?? 120;
28790
- const msPerChar = Math.round(60000 / (wpm * 5));
28791
- await randomDelay(msPerChar * 0.5, msPerChar * 1.8);
28764
+ const el = document.querySelector('${selector.replace(/'/g, "\\'")}');
28765
+ if (el) { el.value = ''; el.dispatchEvent(new Event('input', { bubbles: true })); }
28766
+ `, t2);
28792
28767
  }
28793
- } else if (opts?.delay) {
28794
- for (const ch of text) {
28795
- await client.type(selector, ch, t2);
28796
- await new Promise((r) => setTimeout(r, opts.delay));
28797
- }
28798
- } else {
28799
28768
  await client.type(selector, text, t2);
28800
28769
  }
28801
28770
  await client.evaluate(`
28802
- document.querySelector('${selector}').dispatchEvent(new Event('blur', { bubbles: true }))
28803
- `, t2);
28771
+ document.querySelector('${selector.replace(/'/g, "\\'")}')?.dispatchEvent(new Event('blur', { bubbles: true }));
28772
+ `, t2);
28804
28773
  logger_default.success(`[${name}] typed into: ${selector}`);
28805
28774
  return true;
28806
28775
  }), `type(${selector})`),
@@ -28825,7 +28794,7 @@ function createSiteObject(name, registeredUrl, client, tabId, pool) {
28825
28794
  const chunk = px / steps;
28826
28795
  for (let i = 0;i < steps; i++) {
28827
28796
  await client.scrollBy(chunk, t2);
28828
- await randomDelay(30, 80);
28797
+ await new Promise((r) => setTimeout(r, 30 + Math.random() * 50));
28829
28798
  }
28830
28799
  } else {
28831
28800
  await client.scrollBy(px, t2);
@@ -29127,11 +29096,788 @@ class TabPool {
29127
29096
  }
29128
29097
  }
29129
29098
 
29099
+ // piggy/capture/index.ts
29100
+ class CaptureClient {
29101
+ client;
29102
+ constructor(client) {
29103
+ this.client = client;
29104
+ }
29105
+ start(tabId = "default") {
29106
+ return this.client.send("capture.start", { tabId });
29107
+ }
29108
+ stop(tabId = "default") {
29109
+ return this.client.send("capture.stop", { tabId });
29110
+ }
29111
+ requests(tabId = "default") {
29112
+ return this.client.send("capture.requests", { tabId });
29113
+ }
29114
+ ws(tabId = "default") {
29115
+ return this.client.send("capture.ws", { tabId });
29116
+ }
29117
+ cookies(tabId = "default") {
29118
+ return this.client.send("capture.cookies", { tabId });
29119
+ }
29120
+ storage(tabId = "default") {
29121
+ return this.client.send("capture.storage", { tabId });
29122
+ }
29123
+ clear(tabId = "default") {
29124
+ return this.client.send("capture.clear", { tabId });
29125
+ }
29126
+ }
29127
+ function createCaptureAPI(client) {
29128
+ return new CaptureClient(client);
29129
+ }
29130
+
29131
+ // piggy/captcha/index.ts
29132
+ class CaptchaClient {
29133
+ client;
29134
+ constructor(client) {
29135
+ this.client = client;
29136
+ }
29137
+ status(tabId = "default") {
29138
+ return this.client.send("captcha.status", { tabId });
29139
+ }
29140
+ resolve(tabId = "default") {
29141
+ return this.client.send("captcha.resolve", { tabId });
29142
+ }
29143
+ pause(tabId = "default") {
29144
+ return this.client.send("captcha.pause", { tabId });
29145
+ }
29146
+ check(tabId = "default") {
29147
+ return this.client.send("captcha.check", { tabId });
29148
+ }
29149
+ setAutoRetry(enabled) {
29150
+ return this.client.send("captcha.autoRetry", { enabled });
29151
+ }
29152
+ blockStatus(tabId = "default") {
29153
+ return this.client.send("block.status", { tabId });
29154
+ }
29155
+ blockRetry(tabId = "default") {
29156
+ return this.client.send("block.retry", { tabId });
29157
+ }
29158
+ onCaptcha(tabId, handler) {
29159
+ return this.client.onEvent("captcha", tabId, handler);
29160
+ }
29161
+ onCaptchaResolved(tabId, handler) {
29162
+ return this.client.onEvent("captcha:resolved", tabId, handler);
29163
+ }
29164
+ onBlocked(tabId, handler) {
29165
+ return this.client.onEvent("blocked", tabId, handler);
29166
+ }
29167
+ onBlockRetry(tabId, handler) {
29168
+ return this.client.onEvent("block:retry", tabId, handler);
29169
+ }
29170
+ waitForResolution(tabId = "default", timeoutMs = 300000) {
29171
+ return new Promise((resolve2, reject) => {
29172
+ const timer = setTimeout(() => {
29173
+ unsub();
29174
+ reject(new Error(`captcha.waitForResolution: timed out after ${timeoutMs}ms`));
29175
+ }, timeoutMs);
29176
+ const unsub = this.onCaptchaResolved(tabId, () => {
29177
+ clearTimeout(timer);
29178
+ unsub();
29179
+ resolve2();
29180
+ });
29181
+ logger_default.warn(`[captcha] waiting for manual resolution on tab ${tabId}…`);
29182
+ });
29183
+ }
29184
+ }
29185
+ function createCaptchaAPI(client) {
29186
+ return new CaptchaClient(client);
29187
+ }
29188
+
29189
+ // piggy/dialog/index.ts
29190
+ class DialogClient {
29191
+ client;
29192
+ constructor(client) {
29193
+ this.client = client;
29194
+ }
29195
+ accept(tabId = "default", text) {
29196
+ return this.client.send("dialog.accept", { tabId, ...text !== undefined ? { text } : {} });
29197
+ }
29198
+ dismiss(tabId = "default") {
29199
+ return this.client.send("dialog.dismiss", { tabId });
29200
+ }
29201
+ status(tabId = "default") {
29202
+ return this.client.send("dialog.status", { tabId });
29203
+ }
29204
+ setAutoAction(tabId = "default", action) {
29205
+ return this.client.send("dialog.onDialog", { tabId, action });
29206
+ }
29207
+ upload(selector, filePath, tabId = "default") {
29208
+ return this.client.send("upload", { selector, path: filePath, tabId });
29209
+ }
29210
+ onDialog(tabId, handler) {
29211
+ return this.client.onEvent("dialog", tabId, handler);
29212
+ }
29213
+ waitAndAccept(tabId = "default", text, timeoutMs = 30000) {
29214
+ return new Promise((resolve2, reject) => {
29215
+ const timer = setTimeout(() => {
29216
+ unsub();
29217
+ reject(new Error(`dialog.waitAndAccept: timed out after ${timeoutMs}ms`));
29218
+ }, timeoutMs);
29219
+ const unsub = this.onDialog(tabId, async (data) => {
29220
+ clearTimeout(timer);
29221
+ unsub();
29222
+ await this.accept(tabId, text);
29223
+ resolve2(data);
29224
+ });
29225
+ });
29226
+ }
29227
+ waitAndDismiss(tabId = "default", timeoutMs = 30000) {
29228
+ return new Promise((resolve2, reject) => {
29229
+ const timer = setTimeout(() => {
29230
+ unsub();
29231
+ reject(new Error(`dialog.waitAndDismiss: timed out after ${timeoutMs}ms`));
29232
+ }, timeoutMs);
29233
+ const unsub = this.onDialog(tabId, async (data) => {
29234
+ clearTimeout(timer);
29235
+ unsub();
29236
+ await this.dismiss(tabId);
29237
+ resolve2(data);
29238
+ });
29239
+ });
29240
+ }
29241
+ }
29242
+ function createDialogAPI(client) {
29243
+ return new DialogClient(client);
29244
+ }
29245
+
29246
+ // piggy/export/index.ts
29247
+ class ExportClient {
29248
+ client;
29249
+ constructor(client) {
29250
+ this.client = client;
29251
+ }
29252
+ searchCss(query, tabId = "default") {
29253
+ return this.client.send("search.css", { query, tabId });
29254
+ }
29255
+ searchId(query, tabId = "default") {
29256
+ return this.client.send("search.id", { query, tabId });
29257
+ }
29258
+ setCookie(opts, tabId = "default") {
29259
+ return this.client.send("cookie.set", { ...opts, tabId });
29260
+ }
29261
+ deleteCookie(opts, tabId = "default") {
29262
+ return this.client.send("cookie.delete", { ...opts, tabId });
29263
+ }
29264
+ sessionReload(tabId = "default") {
29265
+ return this.client.send("session.reload", { tabId });
29266
+ }
29267
+ cookiesPath() {
29268
+ return this.client.send("session.cookies.path", {});
29269
+ }
29270
+ profilePath() {
29271
+ return this.client.send("session.profile.path", {});
29272
+ }
29273
+ wsPath() {
29274
+ return this.client.send("session.ws.path", {});
29275
+ }
29276
+ pingsPath() {
29277
+ return this.client.send("session.pings.path", {});
29278
+ }
29279
+ sessionPaths() {
29280
+ return this.client.send("session.paths", {});
29281
+ }
29282
+ setWsSave(enabled) {
29283
+ return this.client.send("session.ws.save", { enabled });
29284
+ }
29285
+ setPingsSave(enabled) {
29286
+ return this.client.send("session.pings.save", { enabled });
29287
+ }
29288
+ addInterceptRule(rule, tabId = "default") {
29289
+ return this.client.send("intercept.rule.add", { ...rule, tabId });
29290
+ }
29291
+ clearInterceptRules(tabId = "default") {
29292
+ return this.client.send("intercept.rule.clear", { tabId });
29293
+ }
29294
+ async exportSession(tabId = "default") {
29295
+ const raw = await this.client.send("session.export", { tabId });
29296
+ return typeof raw === "string" ? JSON.parse(raw) : raw;
29297
+ }
29298
+ importSession(data, tabId = "default") {
29299
+ return this.client.send("session.import", {
29300
+ data: JSON.stringify(data),
29301
+ tabId
29302
+ });
29303
+ }
29304
+ exposeFunction(name, tabId = "default") {
29305
+ return this.client.send("expose.function", { name, tabId });
29306
+ }
29307
+ resolveExposed(callId, result, isError = false, tabId = "default") {
29308
+ return this.client.send("exposed.result", { callId, result, isError, tabId });
29309
+ }
29310
+ addInitScript(js, tabId = "default") {
29311
+ return this.client.send("addInitScript", { js, tabId });
29312
+ }
29313
+ onExposedFunctionCalled(tabId, handler) {
29314
+ return this.client.onEvent("exposed_call", tabId, handler);
29315
+ }
29316
+ }
29317
+ function createExportAPI(client) {
29318
+ return new ExportClient(client);
29319
+ }
29320
+
29321
+ // piggy/find/index.ts
29322
+ class FindClient {
29323
+ client;
29324
+ constructor(client) {
29325
+ this.client = client;
29326
+ }
29327
+ css(selector, tabId = "default") {
29328
+ return this.client.send("find.css", { selector, tabId });
29329
+ }
29330
+ all(selector, tabId = "default") {
29331
+ return this.client.send("find.all", { selector, tabId });
29332
+ }
29333
+ first(selector, tabId = "default") {
29334
+ return this.client.send("find.first", { selector, tabId });
29335
+ }
29336
+ byText(opts, tabId = "default") {
29337
+ return this.client.send("find.byText", { ...opts, tabId });
29338
+ }
29339
+ byAttr(opts, tabId = "default") {
29340
+ return this.client.send("find.byAttr", { ...opts, tabId });
29341
+ }
29342
+ byTag(tag, tabId = "default") {
29343
+ return this.client.send("find.byTag", { tag, tabId });
29344
+ }
29345
+ byPlaceholder(text, tabId = "default") {
29346
+ return this.client.send("find.byPlaceholder", { text, tabId });
29347
+ }
29348
+ byRole(opts, tabId = "default") {
29349
+ return this.client.send("find.byRole", { ...opts, tabId });
29350
+ }
29351
+ children(selector, tabId = "default") {
29352
+ return this.client.send("find.children", { selector, tabId });
29353
+ }
29354
+ filter(opts, tabId = "default") {
29355
+ return this.client.send("find.filter", { ...opts, tabId });
29356
+ }
29357
+ closest(opts, tabId = "default") {
29358
+ return this.client.send("find.closest", { ...opts, tabId });
29359
+ }
29360
+ parent(selector, tabId = "default") {
29361
+ return this.client.send("find.parent", { selector, tabId });
29362
+ }
29363
+ count(selector, tabId = "default") {
29364
+ return this.client.send("find.count", { selector, tabId });
29365
+ }
29366
+ exists(selector, tabId = "default") {
29367
+ return this.client.send("find.exists", { selector, tabId });
29368
+ }
29369
+ visible(selector, tabId = "default") {
29370
+ return this.client.send("find.visible", { selector, tabId });
29371
+ }
29372
+ enabled(selector, tabId = "default") {
29373
+ return this.client.send("find.enabled", { selector, tabId });
29374
+ }
29375
+ checked(selector, tabId = "default") {
29376
+ return this.client.send("find.checked", { selector, tabId });
29377
+ }
29378
+ }
29379
+ function createFindAPI(client) {
29380
+ return new FindClient(client);
29381
+ }
29382
+
29383
+ // piggy/iframe/index.ts
29384
+ class IframeClient {
29385
+ client;
29386
+ constructor(client) {
29387
+ this.client = client;
29388
+ }
29389
+ list(tabId = "default") {
29390
+ return this.client.send("iframe.list", { tabId });
29391
+ }
29392
+ evaluate(opts, tabId = "default") {
29393
+ return this.client.send("iframe.evaluate", { ...opts, tabId });
29394
+ }
29395
+ click(opts, tabId = "default") {
29396
+ return this.client.send("iframe.click", { ...opts, tabId });
29397
+ }
29398
+ type(opts, tabId = "default") {
29399
+ return this.client.send("iframe.type", { ...opts, tabId });
29400
+ }
29401
+ text(opts, tabId = "default") {
29402
+ return this.client.send("iframe.text", { ...opts, tabId });
29403
+ }
29404
+ html(opts, tabId = "default") {
29405
+ return this.client.send("iframe.html", { ...opts, tabId });
29406
+ }
29407
+ waitSel(opts, tabId = "default") {
29408
+ return this.client.send("iframe.waitSel", { ...opts, tabId });
29409
+ }
29410
+ }
29411
+ function createIframeAPI(client) {
29412
+ return new IframeClient(client);
29413
+ }
29414
+
29415
+ // piggy/interactions/index.ts
29416
+ class InteractionsClient {
29417
+ client;
29418
+ constructor(client) {
29419
+ this.client = client;
29420
+ }
29421
+ click(selector, tabId = "default") {
29422
+ return this.client.send("click", { selector, tabId });
29423
+ }
29424
+ dblclick(selector, tabId = "default") {
29425
+ return this.client.send("dblclick", { selector, tabId });
29426
+ }
29427
+ hover(selector, tabId = "default") {
29428
+ return this.client.send("hover", { selector, tabId });
29429
+ }
29430
+ type(selector, text, tabId = "default") {
29431
+ return this.client.send("type", { selector, text, tabId });
29432
+ }
29433
+ typeClear(selector, text, tabId = "default") {
29434
+ return this.client.send("type", { selector, text, clear: true, tabId });
29435
+ }
29436
+ select(selector, value, tabId = "default") {
29437
+ return this.client.send("select", { selector, value, tabId });
29438
+ }
29439
+ scrollTo(selector, tabId = "default") {
29440
+ return this.client.send("scroll.to", { selector, tabId });
29441
+ }
29442
+ scrollBy(px, tabId = "default") {
29443
+ return this.client.send("scroll.by", { px, tabId });
29444
+ }
29445
+ keyPress(key, tabId = "default") {
29446
+ return this.client.send("keyboard.press", { key, tabId });
29447
+ }
29448
+ keyCombo(combo, tabId = "default") {
29449
+ return this.client.send("keyboard.combo", { combo, tabId });
29450
+ }
29451
+ mouseMove(x, y, tabId = "default") {
29452
+ return this.client.send("mouse.move", { x, y, tabId });
29453
+ }
29454
+ mouseDrag(from, to, tabId = "default") {
29455
+ return this.client.send("mouse.drag", { from, to, tabId });
29456
+ }
29457
+ evaluate(js, tabId = "default") {
29458
+ return this.client.send("evaluate", { js, tabId });
29459
+ }
29460
+ }
29461
+ function createInteractionsAPI(client) {
29462
+ return new InteractionsClient(client);
29463
+ }
29464
+
29465
+ // piggy/media/index.ts
29466
+ import { writeFileSync as writeFileSync3, mkdirSync as mkdirSync3 } from "fs";
29467
+ import { dirname as dirname3 } from "path";
29468
+
29469
+ class MediaClient {
29470
+ client;
29471
+ constructor(client) {
29472
+ this.client = client;
29473
+ }
29474
+ async screenshot(filePath, tabId = "default") {
29475
+ const b64 = await this.client.send("screenshot", { tabId });
29476
+ if (filePath) {
29477
+ mkdirSync3(dirname3(filePath), { recursive: true });
29478
+ writeFileSync3(filePath, Buffer.from(b64, "base64"));
29479
+ return filePath;
29480
+ }
29481
+ return b64;
29482
+ }
29483
+ async pdf(filePath, tabId = "default") {
29484
+ const b64 = await this.client.send("pdf", { tabId });
29485
+ if (filePath) {
29486
+ mkdirSync3(dirname3(filePath), { recursive: true });
29487
+ writeFileSync3(filePath, Buffer.from(b64, "base64"));
29488
+ return filePath;
29489
+ }
29490
+ return b64;
29491
+ }
29492
+ blockImages(tabId = "default") {
29493
+ return this.client.send("intercept.block.images", { tabId });
29494
+ }
29495
+ unblockImages(tabId = "default") {
29496
+ return this.client.send("intercept.unblock.images", { tabId });
29497
+ }
29498
+ }
29499
+ function createMediaAPI(client) {
29500
+ return new MediaClient(client);
29501
+ }
29502
+
29503
+ // piggy/navigation/index.ts
29504
+ class NavigationClient {
29505
+ client;
29506
+ constructor(client) {
29507
+ this.client = client;
29508
+ }
29509
+ navigate(url2, tabId = "default") {
29510
+ return this.client.send("navigate", { url: url2, tabId });
29511
+ }
29512
+ reload(tabId = "default") {
29513
+ return this.client.send("reload", { tabId });
29514
+ }
29515
+ goBack(tabId = "default") {
29516
+ return this.client.send("go.back", { tabId });
29517
+ }
29518
+ goForward(tabId = "default") {
29519
+ return this.client.send("go.forward", { tabId });
29520
+ }
29521
+ url(tabId = "default") {
29522
+ return this.client.send("page.url", { tabId });
29523
+ }
29524
+ title(tabId = "default") {
29525
+ return this.client.send("page.title", { tabId });
29526
+ }
29527
+ content(tabId = "default") {
29528
+ return this.client.send("page.content", { tabId });
29529
+ }
29530
+ waitForNavigation(tabId = "default") {
29531
+ return this.client.send("wait.navigation", { tabId });
29532
+ }
29533
+ waitForSelector(selector, timeout = 1e4, tabId = "default") {
29534
+ return this.client.send("wait.selector", { selector, timeout, tabId });
29535
+ }
29536
+ }
29537
+ function createNavigationAPI(client) {
29538
+ return new NavigationClient(client);
29539
+ }
29540
+
29541
+ // piggy/provide/index.ts
29542
+ class ProvideClient {
29543
+ client;
29544
+ constructor(client) {
29545
+ this.client = client;
29546
+ }
29547
+ text(selector, tabId = "default") {
29548
+ return this.client.send("provide.text", { selector, tabId });
29549
+ }
29550
+ textAll(selector, tabId = "default") {
29551
+ return this.client.send("provide.textAll", { selector, tabId });
29552
+ }
29553
+ attr(selector, attr, tabId = "default") {
29554
+ return this.client.send("provide.attr", { selector, attr, tabId });
29555
+ }
29556
+ attrAll(selector, attr, tabId = "default") {
29557
+ return this.client.send("provide.attrAll", { selector, attr, tabId });
29558
+ }
29559
+ html(selector, tabId = "default") {
29560
+ return this.client.send("provide.html", { selector, tabId });
29561
+ }
29562
+ table(selector, tabId = "default") {
29563
+ return this.client.send("provide.table", { selector, tabId });
29564
+ }
29565
+ list(selector, itemSel, tabId = "default") {
29566
+ return this.client.send("provide.list", { selector, itemSel, tabId });
29567
+ }
29568
+ links(selector, tabId = "default") {
29569
+ return this.client.send("provide.links", { selector, tabId });
29570
+ }
29571
+ images(selector, tabId = "default") {
29572
+ return this.client.send("provide.images", { selector, tabId });
29573
+ }
29574
+ form(selector, tabId = "default") {
29575
+ return this.client.send("provide.form", { selector, tabId });
29576
+ }
29577
+ page(tabId = "default") {
29578
+ return this.client.send("provide.page", { tabId });
29579
+ }
29580
+ div(selector, tabId = "default") {
29581
+ return this.client.send("provide.div", { selector, tabId });
29582
+ }
29583
+ meta(tabId = "default") {
29584
+ return this.client.send("provide.meta", { tabId });
29585
+ }
29586
+ select(selector, tabId = "default") {
29587
+ return this.client.send("provide.select", { selector, tabId });
29588
+ }
29589
+ json(selector, tabId = "default") {
29590
+ return this.client.send("provide.json", { selector, tabId });
29591
+ }
29592
+ }
29593
+ function createProvideAPI(client) {
29594
+ return new ProvideClient(client);
29595
+ }
29596
+
29597
+ // piggy/proxy/index.ts
29598
+ class ProxyClient {
29599
+ client;
29600
+ constructor(client) {
29601
+ this.client = client;
29602
+ }
29603
+ load(path) {
29604
+ return this.client.send("proxy.load", { path });
29605
+ }
29606
+ fetch(url2) {
29607
+ return this.client.send("proxy.fetch", { url: url2 });
29608
+ }
29609
+ ovpn(path) {
29610
+ return this.client.send("proxy.ovpn", { path });
29611
+ }
29612
+ set(opts) {
29613
+ return this.client.send("proxy.set", opts);
29614
+ }
29615
+ test() {
29616
+ return this.client.send("proxy.test", {});
29617
+ }
29618
+ testStop() {
29619
+ return this.client.send("proxy.test.stop", {});
29620
+ }
29621
+ next() {
29622
+ return this.client.send("proxy.next", {});
29623
+ }
29624
+ rotate() {
29625
+ return this.client.send("proxy.rotate", {});
29626
+ }
29627
+ disable() {
29628
+ return this.client.send("proxy.disable", {});
29629
+ }
29630
+ enable() {
29631
+ return this.client.send("proxy.enable", {});
29632
+ }
29633
+ current() {
29634
+ return this.client.send("proxy.current", {});
29635
+ }
29636
+ stats() {
29637
+ return this.client.send("proxy.stats", {});
29638
+ }
29639
+ list(limit) {
29640
+ return this.client.send("proxy.list", { limit });
29641
+ }
29642
+ rotation(mode, interval) {
29643
+ return this.client.send("proxy.rotation", { mode, interval });
29644
+ }
29645
+ config(opts) {
29646
+ return this.client.send("proxy.config", opts);
29647
+ }
29648
+ save(path, filter) {
29649
+ return this.client.send("proxy.save", { path, filter });
29650
+ }
29651
+ }
29652
+ function createProxyAPI(client) {
29653
+ return new ProxyClient(client);
29654
+ }
29655
+
29656
+ // piggy/session/index.ts
29657
+ class SessionClient {
29658
+ client;
29659
+ constructor(client) {
29660
+ this.client = client;
29661
+ }
29662
+ reload(tabId = "default") {
29663
+ return this.client.send("session.reload", { tabId });
29664
+ }
29665
+ paths() {
29666
+ return this.client.send("session.paths", {});
29667
+ }
29668
+ cookiesPath() {
29669
+ return this.client.send("session.cookies.path", {});
29670
+ }
29671
+ profilePath() {
29672
+ return this.client.send("session.profile.path", {});
29673
+ }
29674
+ wsPath() {
29675
+ return this.client.send("session.ws.path", {});
29676
+ }
29677
+ pingsPath() {
29678
+ return this.client.send("session.pings.path", {});
29679
+ }
29680
+ setWsSave(enabled) {
29681
+ return this.client.send("session.ws.save", { enabled });
29682
+ }
29683
+ setPingsSave(enabled) {
29684
+ return this.client.send("session.pings.save", { enabled });
29685
+ }
29686
+ async export(tabId = "default") {
29687
+ const raw = await this.client.send("session.export", { tabId });
29688
+ return typeof raw === "string" ? JSON.parse(raw) : raw;
29689
+ }
29690
+ import(data, tabId = "default") {
29691
+ return this.client.send("session.import", {
29692
+ data: JSON.stringify(data),
29693
+ tabId
29694
+ });
29695
+ }
29696
+ setCookie(opts, tabId = "default") {
29697
+ return this.client.send("cookie.set", { ...opts, tabId });
29698
+ }
29699
+ deleteCookie(opts, tabId = "default") {
29700
+ return this.client.send("cookie.delete", { ...opts, tabId });
29701
+ }
29702
+ }
29703
+ function createSessionAPI(client) {
29704
+ return new SessionClient(client);
29705
+ }
29706
+
29707
+ // piggy/tabs/index.ts
29708
+ class TabsClient {
29709
+ client;
29710
+ constructor(client) {
29711
+ this.client = client;
29712
+ }
29713
+ new() {
29714
+ return this.client.send("tab.new", {});
29715
+ }
29716
+ close(tabId) {
29717
+ return this.client.send("tab.close", { tabId });
29718
+ }
29719
+ list() {
29720
+ return this.client.send("tab.list", {});
29721
+ }
29722
+ }
29723
+ function createTabsAPI(client) {
29724
+ return new TabsClient(client);
29725
+ }
29726
+
29727
+ // piggy/wait/index.ts
29728
+ class WaitClient {
29729
+ client;
29730
+ constructor(client) {
29731
+ this.client = client;
29732
+ }
29733
+ function(js, timeout = 1e4, tabId = "default") {
29734
+ return this.client.send("wait.function", { js, timeout, tabId });
29735
+ }
29736
+ selector(selector, state = "attached", timeout = 1e4, tabId = "default") {
29737
+ return this.client.send("wait.selector", { selector, state, timeout, tabId });
29738
+ }
29739
+ }
29740
+
29741
+ class EvaluateClient {
29742
+ client;
29743
+ constructor(client) {
29744
+ this.client = client;
29745
+ }
29746
+ run(js, timeout, tabId = "default") {
29747
+ return this.client.send("evaluate", {
29748
+ js,
29749
+ tabId,
29750
+ ...timeout !== undefined ? { timeout } : {}
29751
+ });
29752
+ }
29753
+ }
29754
+
29755
+ class FetchClient {
29756
+ client;
29757
+ constructor(client) {
29758
+ this.client = client;
29759
+ }
29760
+ text(selector, tabId = "default") {
29761
+ return this.client.send("fetch.text", { query: selector, tabId });
29762
+ }
29763
+ textAll(selector, tabId = "default") {
29764
+ return this.client.send("fetch.textAll", { selector, tabId });
29765
+ }
29766
+ attr(selector, attr, tabId = "default") {
29767
+ return this.client.send("fetch.attr", { selector, attr, tabId });
29768
+ }
29769
+ attrAll(selector, attr, tabId = "default") {
29770
+ return this.client.send("fetch.attrAll", { selector, attr, tabId });
29771
+ }
29772
+ links(selector, tabId = "default") {
29773
+ return this.client.send("fetch.links", { query: selector, tabId });
29774
+ }
29775
+ linksAll(tabId = "default") {
29776
+ return this.client.send("fetch.links.all", { tabId });
29777
+ }
29778
+ images(selector, tabId = "default") {
29779
+ return this.client.send("fetch.image", { query: selector, tabId });
29780
+ }
29781
+ }
29782
+ function createWaitAPI(client) {
29783
+ return new WaitClient(client);
29784
+ }
29785
+ function createEvaluateAPI(client) {
29786
+ return new EvaluateClient(client);
29787
+ }
29788
+ function createFetchAPI(client) {
29789
+ return new FetchClient(client);
29790
+ }
29791
+
29792
+ // piggy/router/index.ts
29793
+ function createRouter(client) {
29794
+ return {
29795
+ client,
29796
+ tabs: createTabsAPI(client),
29797
+ navigation: createNavigationAPI(client),
29798
+ interactions: createInteractionsAPI(client),
29799
+ media: createMediaAPI(client),
29800
+ capture: createCaptureAPI(client),
29801
+ find: createFindAPI(client),
29802
+ provide: createProvideAPI(client),
29803
+ wait: createWaitAPI(client),
29804
+ evaluate: createEvaluateAPI(client),
29805
+ fetch: createFetchAPI(client),
29806
+ proxy: createProxyAPI(client),
29807
+ captcha: createCaptchaAPI(client),
29808
+ dialog: createDialogAPI(client),
29809
+ human: createHumanAPI(client),
29810
+ iframe: createIframeAPI(client),
29811
+ session: createSessionAPI(client),
29812
+ export: createExportAPI(client)
29813
+ };
29814
+ }
29815
+
29816
+ // piggy/http/index.ts
29817
+ class PiggyHttpClient {
29818
+ baseUrl;
29819
+ key;
29820
+ constructor(opts) {
29821
+ const host = opts.host ?? "localhost";
29822
+ const port = opts.port ?? 2005;
29823
+ this.baseUrl = `http://${host}:${port}`;
29824
+ this.key = opts.key;
29825
+ }
29826
+ async ping() {
29827
+ try {
29828
+ const res = await fetch(this.baseUrl, {
29829
+ method: "POST",
29830
+ headers: {
29831
+ "Content-Type": "application/json",
29832
+ "X-Piggy-Key": this.key
29833
+ },
29834
+ body: "hello"
29835
+ });
29836
+ const text = await res.text();
29837
+ return text.includes("active");
29838
+ } catch {
29839
+ return false;
29840
+ }
29841
+ }
29842
+ async send(cmd, payload = {}) {
29843
+ const res = await fetch(this.baseUrl, {
29844
+ method: "POST",
29845
+ headers: {
29846
+ "Content-Type": "application/json",
29847
+ "X-Piggy-Key": this.key
29848
+ },
29849
+ body: JSON.stringify({ id: String(Date.now()), cmd, payload })
29850
+ });
29851
+ if (res.status === 401)
29852
+ throw new Error("Unauthorized — invalid X-Piggy-Key");
29853
+ if (res.status === 400)
29854
+ throw new Error("Bad request — invalid JSON");
29855
+ if (!res.ok)
29856
+ throw new Error(`HTTP ${res.status}`);
29857
+ const data = await res.json();
29858
+ if (!data.ok)
29859
+ throw new Error(String(data.data) ?? "command failed");
29860
+ return data.data;
29861
+ }
29862
+ }
29863
+ function createHttpClient(opts) {
29864
+ return new PiggyHttpClient(opts);
29865
+ }
29866
+
29130
29867
  // piggy.ts
29131
29868
  var _client = null;
29869
+ var _router = null;
29132
29870
  var _tabMode = "tab";
29133
29871
  var _extraProcs = [];
29134
- var _sites = [];
29872
+ var _sites = {};
29873
+ function guardClient() {
29874
+ if (!_client)
29875
+ throw new Error("No client. Call piggy.launch() or piggy.connect() first.");
29876
+ return _client;
29877
+ }
29878
+ function buildAPIs(client) {
29879
+ _router = createRouter(client);
29880
+ }
29135
29881
  var piggy = {
29136
29882
  launch: async (opts) => {
29137
29883
  _tabMode = opts?.mode ?? "tab";
@@ -29141,6 +29887,7 @@ var piggy = {
29141
29887
  _client = new PiggyClient;
29142
29888
  await _client.connect();
29143
29889
  setClient(_client);
29890
+ buildAPIs(_client);
29144
29891
  logger_default.info(`[piggy] launched — tab mode: "${_tabMode}", binary: "${binaryMode}"`);
29145
29892
  return piggy;
29146
29893
  },
@@ -29149,27 +29896,28 @@ var piggy = {
29149
29896
  _client = new PiggyClient({ host: opts.host, key: opts.key });
29150
29897
  await _client.connect();
29151
29898
  setClient(_client);
29899
+ buildAPIs(_client);
29152
29900
  logger_default.info(`[piggy] connected (HTTP) → ${opts.host}`);
29153
29901
  return piggy;
29154
29902
  },
29903
+ http: (opts) => createHttpClient(opts),
29155
29904
  register: async (name, url2, opts) => {
29156
29905
  if (!url2?.trim())
29157
29906
  throw new Error(`No URL for site "${name}"`);
29158
29907
  const binaryMode = opts?.binary ?? "headless";
29159
29908
  const poolSize = opts?.pool ?? 0;
29160
29909
  if (_tabMode === "tab") {
29161
- if (!_client)
29162
- throw new Error("No client. Call piggy.launch() or piggy.connect() first.");
29910
+ const client = guardClient();
29163
29911
  if (poolSize > 1) {
29164
- const pool = new TabPool(_client, poolSize, url2, name);
29912
+ const pool = new TabPool(client, poolSize, url2, name);
29165
29913
  await pool.init();
29166
- const siteObj = createSiteObject(name, url2, _client, "default", pool);
29914
+ const siteObj = createSiteObject(name, url2, client, "default", pool);
29167
29915
  _sites[name] = siteObj;
29168
29916
  piggy[name] = siteObj;
29169
29917
  logger_default.success(`[${name}] registered with pool of ${poolSize} tabs`);
29170
29918
  } else {
29171
- const tabId = await _client.newTab();
29172
- const siteObj = createSiteObject(name, url2, _client, tabId);
29919
+ const tabId = await client.newTab();
29920
+ const siteObj = createSiteObject(name, url2, client, tabId);
29173
29921
  _sites[name] = siteObj;
29174
29922
  piggy[name] = siteObj;
29175
29923
  logger_default.success(`[${name}] registered as tab ${tabId}`);
@@ -29188,6 +29936,76 @@ var piggy = {
29188
29936
  }
29189
29937
  return piggy;
29190
29938
  },
29939
+ get tabs() {
29940
+ return _router?.tabs ?? createTabsAPI(guardClient());
29941
+ },
29942
+ get navigation() {
29943
+ return _router?.navigation ?? createNavigationAPI(guardClient());
29944
+ },
29945
+ get interactions() {
29946
+ return _router?.interactions ?? createInteractionsAPI(guardClient());
29947
+ },
29948
+ get media() {
29949
+ return _router?.media ?? createMediaAPI(guardClient());
29950
+ },
29951
+ get capture() {
29952
+ return _router?.capture ?? createCaptureAPI(guardClient());
29953
+ },
29954
+ get find() {
29955
+ return _router?.find ?? createFindAPI(guardClient());
29956
+ },
29957
+ get provide() {
29958
+ return _router?.provide ?? createProvideAPI(guardClient());
29959
+ },
29960
+ get wait() {
29961
+ return _router?.wait ?? createWaitAPI(guardClient());
29962
+ },
29963
+ get evaluate() {
29964
+ return _router?.evaluate ?? createEvaluateAPI(guardClient());
29965
+ },
29966
+ get fetch() {
29967
+ return _router?.fetch ?? createFetchAPI(guardClient());
29968
+ },
29969
+ get captcha() {
29970
+ return _router?.captcha ?? createCaptchaAPI(guardClient());
29971
+ },
29972
+ get dialog() {
29973
+ return _router?.dialog ?? createDialogAPI(guardClient());
29974
+ },
29975
+ get human() {
29976
+ return _router?.human ?? createHumanAPI(guardClient());
29977
+ },
29978
+ get iframe() {
29979
+ return _router?.iframe ?? createIframeAPI(guardClient());
29980
+ },
29981
+ get session() {
29982
+ return _router?.session ?? createSessionAPI(guardClient());
29983
+ },
29984
+ get export() {
29985
+ return _router?.export ?? createExportAPI(guardClient());
29986
+ },
29987
+ get proxy() {
29988
+ const api = _router?.proxy ?? createProxyAPI(guardClient());
29989
+ return {
29990
+ load: (path) => api.load(path),
29991
+ fetch: (url2) => api.fetch(url2),
29992
+ ovpn: (path) => api.ovpn(path),
29993
+ set: (opts) => api.set(opts),
29994
+ test: () => api.test(),
29995
+ testStop: () => api.testStop(),
29996
+ next: () => api.next(),
29997
+ rotate: () => api.rotate(),
29998
+ disable: () => api.disable(),
29999
+ enable: () => api.enable(),
30000
+ current: () => api.current(),
30001
+ stats: () => api.stats(),
30002
+ list: (limit) => api.list(limit),
30003
+ rotation: (mode, interval) => api.rotation(mode, interval),
30004
+ config: (opts) => api.config(opts),
30005
+ save: (path, filter) => api.save(path, filter),
30006
+ on: (event, handler) => guardClient().onProxyEvent(event, handler)
30007
+ };
30008
+ },
29191
30009
  actHuman: (enable) => {
29192
30010
  setHumanMode(enable);
29193
30011
  logger_default.info(`[piggy] actHuman: ${enable}`);
@@ -29198,101 +30016,15 @@ var piggy = {
29198
30016
  return piggy;
29199
30017
  },
29200
30018
  expose: async (name, handler, tabId = "default") => {
29201
- if (!_client)
29202
- throw new Error("No client. Call piggy.launch() or piggy.connect() first.");
29203
- await _client.exposeFunction(name, handler, tabId);
30019
+ await guardClient().exposeFunction(name, handler, tabId);
29204
30020
  logger_default.success(`[piggy] exposed global function: ${name}`);
29205
30021
  return piggy;
29206
30022
  },
29207
30023
  unexpose: async (name, tabId = "default") => {
29208
- if (!_client)
29209
- throw new Error("No client. Call piggy.launch() or piggy.connect() first.");
29210
- await _client.unexposeFunction(name, tabId);
30024
+ await guardClient().unexposeFunction(name, tabId);
29211
30025
  logger_default.info(`[piggy] unexposed function: ${name}`);
29212
30026
  return piggy;
29213
30027
  },
29214
- proxy: {
29215
- load: (path) => {
29216
- if (!_client)
29217
- throw new Error("No client");
29218
- return _client.proxyLoad(path);
29219
- },
29220
- fetch: (url2) => {
29221
- if (!_client)
29222
- throw new Error("No client");
29223
- return _client.proxyFetch(url2);
29224
- },
29225
- ovpn: (path) => {
29226
- if (!_client)
29227
- throw new Error("No client");
29228
- return _client.proxyOvpn(path);
29229
- },
29230
- set: (opts) => {
29231
- if (!_client)
29232
- throw new Error("No client");
29233
- return _client.proxySet(opts);
29234
- },
29235
- test: () => {
29236
- if (!_client)
29237
- throw new Error("No client");
29238
- return _client.proxyTest();
29239
- },
29240
- testStop: () => {
29241
- if (!_client)
29242
- throw new Error("No client");
29243
- return _client.proxyTestStop();
29244
- },
29245
- next: () => {
29246
- if (!_client)
29247
- throw new Error("No client");
29248
- return _client.proxyNext();
29249
- },
29250
- disable: () => {
29251
- if (!_client)
29252
- throw new Error("No client");
29253
- return _client.proxyDisable();
29254
- },
29255
- enable: () => {
29256
- if (!_client)
29257
- throw new Error("No client");
29258
- return _client.proxyEnable();
29259
- },
29260
- current: () => {
29261
- if (!_client)
29262
- throw new Error("No client");
29263
- return _client.proxyCurrent();
29264
- },
29265
- stats: () => {
29266
- if (!_client)
29267
- throw new Error("No client");
29268
- return _client.proxyStats();
29269
- },
29270
- list: (limit) => {
29271
- if (!_client)
29272
- throw new Error("No client");
29273
- return _client.proxyList(limit);
29274
- },
29275
- rotation: (mode, interval) => {
29276
- if (!_client)
29277
- throw new Error("No client");
29278
- return _client.proxyRotation(mode, interval);
29279
- },
29280
- config: (opts) => {
29281
- if (!_client)
29282
- throw new Error("No client");
29283
- return _client.proxyConfig(opts);
29284
- },
29285
- save: (path, filter) => {
29286
- if (!_client)
29287
- throw new Error("No client");
29288
- return _client.proxySave(path, filter);
29289
- },
29290
- on: (event, handler) => {
29291
- if (!_client)
29292
- throw new Error("No client");
29293
- return _client.onProxyEvent(event, handler);
29294
- }
29295
- },
29296
30028
  serve: (port, opts) => startServer(port, opts?.hostname, opts),
29297
30029
  stopServer,
29298
30030
  routes: () => Array.from(routeRegistry.entries()).map(([key, cfg]) => {
@@ -29334,6 +30066,7 @@ var piggy = {
29334
30066
  _extraProcs.length = 0;
29335
30067
  _client?.disconnect();
29336
30068
  _client = null;
30069
+ _router = null;
29337
30070
  setClient(null);
29338
30071
  killBrowser();
29339
30072
  }