@isdk/web-fetcher 0.3.0 → 0.3.2

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 (88) hide show
  1. package/README.action.cn.md +77 -312
  2. package/README.action.extract.cn.md +263 -0
  3. package/README.action.extract.md +263 -0
  4. package/README.action.md +76 -311
  5. package/README.cn.md +12 -2
  6. package/README.engine.cn.md +22 -1
  7. package/README.engine.md +22 -1
  8. package/README.md +10 -1
  9. package/dist/index.d.mts +245 -1
  10. package/dist/index.d.ts +245 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.mjs +1 -1
  13. package/docs/README.md +10 -1
  14. package/docs/_media/README.action.md +76 -311
  15. package/docs/_media/README.cn.md +12 -2
  16. package/docs/_media/README.engine.md +22 -1
  17. package/docs/classes/CheerioFetchEngine.md +312 -88
  18. package/docs/classes/ClickAction.md +23 -23
  19. package/docs/classes/EvaluateAction.md +23 -23
  20. package/docs/classes/ExtractAction.md +23 -23
  21. package/docs/classes/FetchAction.md +29 -23
  22. package/docs/classes/FetchEngine.md +286 -86
  23. package/docs/classes/FetchSession.md +13 -13
  24. package/docs/classes/FillAction.md +23 -23
  25. package/docs/classes/GetContentAction.md +23 -23
  26. package/docs/classes/GotoAction.md +23 -23
  27. package/docs/classes/KeyboardPressAction.md +533 -0
  28. package/docs/classes/KeyboardTypeAction.md +533 -0
  29. package/docs/classes/MouseClickAction.md +533 -0
  30. package/docs/classes/MouseMoveAction.md +533 -0
  31. package/docs/classes/MouseWheelAction.md +533 -0
  32. package/docs/classes/PauseAction.md +23 -23
  33. package/docs/classes/PlaywrightFetchEngine.md +472 -88
  34. package/docs/classes/ScrollIntoViewAction.md +533 -0
  35. package/docs/classes/SubmitAction.md +23 -23
  36. package/docs/classes/TrimAction.md +23 -23
  37. package/docs/classes/WaitForAction.md +23 -23
  38. package/docs/classes/WebFetcher.md +5 -5
  39. package/docs/enumerations/FetchActionResultStatus.md +4 -4
  40. package/docs/functions/fetchWeb.md +2 -2
  41. package/docs/functions/getRandomDelay.md +25 -0
  42. package/docs/globals.md +13 -0
  43. package/docs/interfaces/BaseFetchActionProperties.md +12 -12
  44. package/docs/interfaces/BaseFetchCollectorActionProperties.md +16 -16
  45. package/docs/interfaces/BaseFetcherProperties.md +31 -27
  46. package/docs/interfaces/Cookie.md +14 -14
  47. package/docs/interfaces/DispatchedEngineAction.md +4 -4
  48. package/docs/interfaces/EvaluateActionOptions.md +3 -3
  49. package/docs/interfaces/ExtractActionProperties.md +12 -12
  50. package/docs/interfaces/FetchActionInContext.md +15 -15
  51. package/docs/interfaces/FetchActionProperties.md +13 -13
  52. package/docs/interfaces/FetchActionResult.md +6 -6
  53. package/docs/interfaces/FetchContext.md +41 -37
  54. package/docs/interfaces/FetchEngineContext.md +36 -32
  55. package/docs/interfaces/FetchMetadata.md +5 -5
  56. package/docs/interfaces/FetchResponse.md +14 -14
  57. package/docs/interfaces/FetchReturnTypeRegistry.md +7 -7
  58. package/docs/interfaces/FetchSite.md +34 -30
  59. package/docs/interfaces/FetcherOptions.md +33 -29
  60. package/docs/interfaces/GotoActionOptions.md +14 -6
  61. package/docs/interfaces/KeyboardPressParams.md +25 -0
  62. package/docs/interfaces/KeyboardTypeParams.md +25 -0
  63. package/docs/interfaces/MouseClickParams.md +49 -0
  64. package/docs/interfaces/MouseMoveParams.md +41 -0
  65. package/docs/interfaces/MouseWheelParams.md +69 -0
  66. package/docs/interfaces/PendingEngineRequest.md +3 -3
  67. package/docs/interfaces/ScrollIntoViewParams.md +17 -0
  68. package/docs/interfaces/StorageOptions.md +5 -5
  69. package/docs/interfaces/SubmitActionOptions.md +2 -2
  70. package/docs/interfaces/TrimActionOptions.md +3 -3
  71. package/docs/interfaces/WaitForActionOptions.md +5 -5
  72. package/docs/type-aliases/BaseFetchActionOptions.md +1 -1
  73. package/docs/type-aliases/BaseFetchCollectorOptions.md +1 -1
  74. package/docs/type-aliases/BrowserEngine.md +1 -1
  75. package/docs/type-aliases/FetchActionCapabilities.md +1 -1
  76. package/docs/type-aliases/FetchActionCapabilityMode.md +1 -1
  77. package/docs/type-aliases/FetchActionOptions.md +1 -1
  78. package/docs/type-aliases/FetchEngineAction.md +2 -2
  79. package/docs/type-aliases/FetchEngineType.md +1 -1
  80. package/docs/type-aliases/FetchReturnType.md +1 -1
  81. package/docs/type-aliases/FetchReturnTypeFor.md +1 -1
  82. package/docs/type-aliases/OnFetchPauseCallback.md +1 -1
  83. package/docs/type-aliases/ResourceType.md +1 -1
  84. package/docs/type-aliases/TrimPreset.md +1 -1
  85. package/docs/variables/DefaultFetcherProperties.md +1 -1
  86. package/docs/variables/FetcherOptionKeys.md +1 -1
  87. package/docs/variables/TRIM_PRESETS.md +1 -1
  88. package/package.json +10 -10
package/dist/index.d.ts CHANGED
@@ -1094,6 +1094,7 @@ interface GotoActionOptions {
1094
1094
  headers?: Record<string, string>;
1095
1095
  waitUntil?: 'load' | 'domcontentloaded' | 'networkidle' | 'commit';
1096
1096
  timeoutMs?: number;
1097
+ simulate?: boolean;
1097
1098
  }
1098
1099
  /**
1099
1100
  * Options for the {@link FetchEngine.waitFor} action, specifying conditions to wait for before continuing.
@@ -1202,6 +1203,51 @@ type FetchEngineAction = {
1202
1203
  type: 'fill';
1203
1204
  selector: string;
1204
1205
  value: string;
1206
+ } | {
1207
+ type: 'mouseMove';
1208
+ params: {
1209
+ x?: number;
1210
+ y?: number;
1211
+ selector?: string;
1212
+ steps?: number;
1213
+ };
1214
+ } | {
1215
+ type: 'mouseClick';
1216
+ params: {
1217
+ x?: number;
1218
+ y?: number;
1219
+ button?: 'left' | 'right' | 'middle';
1220
+ clickCount?: number;
1221
+ delay?: number;
1222
+ steps?: number;
1223
+ };
1224
+ } | {
1225
+ type: 'mouseWheel';
1226
+ params: {
1227
+ x?: number;
1228
+ y?: number;
1229
+ selector?: string;
1230
+ deltaX?: number;
1231
+ deltaY?: number;
1232
+ steps?: number;
1233
+ };
1234
+ } | {
1235
+ type: 'keyboardType';
1236
+ params: {
1237
+ text: string;
1238
+ delay?: number;
1239
+ };
1240
+ } | {
1241
+ type: 'keyboardPress';
1242
+ params: {
1243
+ key: string;
1244
+ delay?: number;
1245
+ };
1246
+ } | {
1247
+ type: 'scrollIntoView';
1248
+ params: {
1249
+ selector: string;
1250
+ };
1205
1251
  } | {
1206
1252
  type: 'waitFor';
1207
1253
  options?: WaitForActionOptions;
@@ -1555,6 +1601,64 @@ declare abstract class FetchEngine<TContext extends CrawlingContext = any, TCraw
1555
1601
  * @throws {Error} When no active page context exists
1556
1602
  */
1557
1603
  click(selector: string): Promise<void>;
1604
+ /**
1605
+ * Moves mouse to specified position or element.
1606
+ *
1607
+ * @param params - Move parameters (x, y, selector, steps)
1608
+ */
1609
+ mouseMove(params: {
1610
+ x?: number;
1611
+ y?: number;
1612
+ selector?: string;
1613
+ steps?: number;
1614
+ }): Promise<void>;
1615
+ /**
1616
+ * Clicks at current position or specified position.
1617
+ *
1618
+ * @param params - Click parameters (x, y, button, clickCount, delay)
1619
+ */
1620
+ mouseClick(params: {
1621
+ x?: number;
1622
+ y?: number;
1623
+ button?: 'left' | 'right' | 'middle';
1624
+ clickCount?: number;
1625
+ delay?: number;
1626
+ }): Promise<void>;
1627
+ /**
1628
+ * Scrolls the mouse wheel.
1629
+ *
1630
+ * @param params - Wheel parameters (x, y, selector, deltaX, deltaY, steps)
1631
+ */
1632
+ mouseWheel(params: {
1633
+ x?: number;
1634
+ y?: number;
1635
+ selector?: string;
1636
+ deltaX?: number;
1637
+ deltaY?: number;
1638
+ steps?: number;
1639
+ }): Promise<void>;
1640
+ /**
1641
+ * Scrolls the element into view.
1642
+ *
1643
+ * @param params - Scroll parameters (selector)
1644
+ */
1645
+ scrollIntoView(params: {
1646
+ selector: string;
1647
+ }): Promise<void>;
1648
+ /**
1649
+ * Types text into current focused element.
1650
+ *
1651
+ * @param text - Text to type
1652
+ * @param delay - Delay between key presses
1653
+ */
1654
+ keyboardType(text: string, delay?: number): Promise<void>;
1655
+ /**
1656
+ * Presses specified key.
1657
+ *
1658
+ * @param key - Key to press
1659
+ * @param delay - Delay after key press
1660
+ */
1661
+ keyboardPress(key: string, delay?: number): Promise<void>;
1558
1662
  /**
1559
1663
  * Fills input element with specified value.
1560
1664
  *
@@ -1778,6 +1882,7 @@ declare abstract class FetchEngine<TContext extends CrawlingContext = any, TCraw
1778
1882
  */
1779
1883
  dispose(): Promise<void>;
1780
1884
  }
1885
+ declare function getRandomDelay(base: number, variance?: number): number;
1781
1886
 
1782
1887
  type FetchReturnType = 'response' | 'context' | 'outputs' | 'any' | 'none';
1783
1888
  interface FetchReturnTypeRegistry {
@@ -2007,6 +2112,38 @@ declare class PlaywrightFetchEngine extends FetchEngine<PlaywrightCrawlingContex
2007
2112
  _extractValue(schema: ExtractValueSchema, scope: Locator): Promise<any>;
2008
2113
  protected _getInitialElementScope(context: PlaywrightCrawlingContext): FetchElementScope;
2009
2114
  protected _waitForNavigation(context: PlaywrightCrawlingContext, oldUrl: string, actionType: string): Promise<void>;
2115
+ protected currentMousePos: {
2116
+ x: number;
2117
+ y: number;
2118
+ };
2119
+ protected _sharedRequestHandler(context: PlaywrightCrawlingContext): Promise<void>;
2120
+ protected mouseInitialized: boolean;
2121
+ protected _initializeMousePos(page: Page): Promise<void>;
2122
+ protected _getTrajectory(start: {
2123
+ x: number;
2124
+ y: number;
2125
+ }, end: {
2126
+ x: number;
2127
+ y: number;
2128
+ }, steps?: number): {
2129
+ x: number;
2130
+ y: number;
2131
+ }[];
2132
+ protected _moveToPos(context: PlaywrightCrawlingContext, target: {
2133
+ x: number;
2134
+ y: number;
2135
+ }, steps?: number): Promise<{
2136
+ x: number;
2137
+ y: number;
2138
+ }>;
2139
+ protected _ensureVisible(context: PlaywrightCrawlingContext, selector: string): Promise<{
2140
+ x: number;
2141
+ y: number;
2142
+ }>;
2143
+ protected _moveToSelector(context: PlaywrightCrawlingContext, selector: string, steps?: number): Promise<{
2144
+ x: number;
2145
+ y: number;
2146
+ }>;
2010
2147
  protected executeAction(context: PlaywrightCrawlingContext, action: FetchEngineAction): Promise<any>;
2011
2148
  protected _createCrawler(options: PlaywrightCrawlerOptions, config?: Configuration): PlaywrightCrawler;
2012
2149
  protected _getSpecificCrawlerOptions(ctx: FetchEngineContext): Promise<Partial<PlaywrightCrawlerOptions>>;
@@ -2195,6 +2332,7 @@ interface BaseFetcherProperties {
2195
2332
  engine?: BrowserEngine;
2196
2333
  headless?: boolean;
2197
2334
  waitUntil?: 'load' | 'domcontentloaded' | 'networkidle' | 'commit';
2335
+ launchOptions?: Record<string, any>;
2198
2336
  };
2199
2337
  http?: {
2200
2338
  method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
@@ -2526,6 +2664,112 @@ declare class EvaluateAction extends FetchAction {
2526
2664
  onExecute(context: FetchContext, options?: BaseFetchActionProperties): Promise<any>;
2527
2665
  }
2528
2666
 
2667
+ interface MouseMoveParams {
2668
+ x?: number;
2669
+ y?: number;
2670
+ selector?: string;
2671
+ steps?: number;
2672
+ }
2673
+ declare class MouseMoveAction extends FetchAction {
2674
+ static id: string;
2675
+ static returnType: "none";
2676
+ static capabilities: {
2677
+ http: "noop";
2678
+ browser: "native";
2679
+ };
2680
+ onExecute(context: FetchContext, options?: BaseFetchActionProperties): Promise<void>;
2681
+ }
2682
+ interface MouseClickParams {
2683
+ x?: number;
2684
+ y?: number;
2685
+ button?: 'left' | 'right' | 'middle';
2686
+ clickCount?: number;
2687
+ delay?: number;
2688
+ }
2689
+ declare class MouseClickAction extends FetchAction {
2690
+ static id: string;
2691
+ static returnType: "none";
2692
+ static capabilities: {
2693
+ http: "noop";
2694
+ browser: "native";
2695
+ };
2696
+ onExecute(context: FetchContext, options?: BaseFetchActionProperties): Promise<void>;
2697
+ }
2698
+ interface ScrollIntoViewParams {
2699
+ selector: string;
2700
+ }
2701
+ declare class ScrollIntoViewAction extends FetchAction {
2702
+ static id: string;
2703
+ static returnType: "none";
2704
+ static capabilities: {
2705
+ http: "noop";
2706
+ browser: "native";
2707
+ };
2708
+ onExecute(context: FetchContext, options?: BaseFetchActionProperties): Promise<void>;
2709
+ }
2710
+ interface MouseWheelParams {
2711
+ /**
2712
+ * Target X coordinate for the mouse wheel event.
2713
+ */
2714
+ x?: number;
2715
+ /**
2716
+ * Target Y coordinate for the mouse wheel event.
2717
+ */
2718
+ y?: number;
2719
+ /**
2720
+ * Selector for the element to scroll. If provided, mouse will move to this element before scrolling.
2721
+ */
2722
+ selector?: string;
2723
+ /**
2724
+ * Horizontal scroll delta.
2725
+ */
2726
+ deltaX?: number;
2727
+ /**
2728
+ * Vertical scroll delta.
2729
+ */
2730
+ deltaY?: number;
2731
+ /**
2732
+ * Number of steps to split the scroll into for simulating human-like behavior.
2733
+ */
2734
+ steps?: number;
2735
+ }
2736
+ declare class MouseWheelAction extends FetchAction {
2737
+ static id: string;
2738
+ static returnType: "none";
2739
+ static capabilities: {
2740
+ http: "noop";
2741
+ browser: "native";
2742
+ };
2743
+ onExecute(context: FetchContext, options?: BaseFetchActionProperties): Promise<void>;
2744
+ }
2745
+
2746
+ interface KeyboardTypeParams {
2747
+ text: string;
2748
+ delay?: number;
2749
+ }
2750
+ declare class KeyboardTypeAction extends FetchAction {
2751
+ static id: string;
2752
+ static returnType: "none";
2753
+ static capabilities: {
2754
+ http: "noop";
2755
+ browser: "native";
2756
+ };
2757
+ onExecute(context: FetchContext, options?: BaseFetchActionProperties): Promise<void>;
2758
+ }
2759
+ interface KeyboardPressParams {
2760
+ key: string;
2761
+ delay?: number;
2762
+ }
2763
+ declare class KeyboardPressAction extends FetchAction {
2764
+ static id: string;
2765
+ static returnType: "none";
2766
+ static capabilities: {
2767
+ http: "noop";
2768
+ browser: "native";
2769
+ };
2770
+ onExecute(context: FetchContext, options?: BaseFetchActionProperties): Promise<void>;
2771
+ }
2772
+
2529
2773
  declare function fetchWeb(options: FetcherOptions): Promise<{
2530
2774
  result: FetchResponse | undefined;
2531
2775
  outputs: Record<string, any>;
@@ -2535,4 +2779,4 @@ declare function fetchWeb(url: string, options?: FetcherOptions): Promise<{
2535
2779
  outputs: Record<string, any>;
2536
2780
  }>;
2537
2781
 
2538
- export { type BaseFetchActionOptions, type BaseFetchActionProperties, type BaseFetchCollectorActionProperties, type BaseFetchCollectorOptions, type BaseFetcherProperties, type BrowserEngine, CheerioFetchEngine, ClickAction, DefaultFetcherProperties, type DispatchedEngineAction, EvaluateAction, type EvaluateActionOptions, ExtractAction, type ExtractActionProperties, FetchAction, type FetchActionCapabilities, type FetchActionCapabilityMode, type FetchActionInContext, type FetchActionOptions, type FetchActionProperties, type FetchActionResult, FetchActionResultStatus, type FetchContext, FetchEngine, type FetchEngineAction, type FetchEngineContext, type FetchEngineType, type FetchMetadata, type FetchResponse, type FetchReturnType, type FetchReturnTypeFor, type FetchReturnTypeRegistry, FetchSession, type FetchSite, FetcherOptionKeys, type FetcherOptions, FillAction, GetContentAction, GotoAction, type GotoActionOptions, type OnFetchPauseCallback, PauseAction, type PendingEngineRequest, PlaywrightFetchEngine, type ResourceType, type StorageOptions, SubmitAction, type SubmitActionOptions, TRIM_PRESETS, TrimAction, type TrimActionOptions, type TrimPreset, WaitForAction, type WaitForActionOptions, WebFetcher, fetchWeb };
2782
+ export { type BaseFetchActionOptions, type BaseFetchActionProperties, type BaseFetchCollectorActionProperties, type BaseFetchCollectorOptions, type BaseFetcherProperties, type BrowserEngine, CheerioFetchEngine, ClickAction, DefaultFetcherProperties, type DispatchedEngineAction, EvaluateAction, type EvaluateActionOptions, ExtractAction, type ExtractActionProperties, FetchAction, type FetchActionCapabilities, type FetchActionCapabilityMode, type FetchActionInContext, type FetchActionOptions, type FetchActionProperties, type FetchActionResult, FetchActionResultStatus, type FetchContext, FetchEngine, type FetchEngineAction, type FetchEngineContext, type FetchEngineType, type FetchMetadata, type FetchResponse, type FetchReturnType, type FetchReturnTypeFor, type FetchReturnTypeRegistry, FetchSession, type FetchSite, FetcherOptionKeys, type FetcherOptions, FillAction, GetContentAction, GotoAction, type GotoActionOptions, KeyboardPressAction, type KeyboardPressParams, KeyboardTypeAction, type KeyboardTypeParams, MouseClickAction, type MouseClickParams, MouseMoveAction, type MouseMoveParams, MouseWheelAction, type MouseWheelParams, type OnFetchPauseCallback, PauseAction, type PendingEngineRequest, PlaywrightFetchEngine, type ResourceType, ScrollIntoViewAction, type ScrollIntoViewParams, type StorageOptions, SubmitAction, type SubmitActionOptions, TRIM_PRESETS, TrimAction, type TrimActionOptions, type TrimPreset, WaitForAction, type WaitForActionOptions, WebFetcher, fetchWeb, getRandomDelay };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var t,e=Object.create,i=Object.defineProperty,n=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,r=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,a=(t,e,r,a)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let c of s(e))o.call(t,c)||c===r||i(t,c,{get:()=>e[c],enumerable:!(a=n(e,c))||a.enumerable});return t},c={};((t,e)=>{for(var n in e)i(t,n,{get:e[n],enumerable:!0})})(c,{CheerioFetchEngine:()=>at,ClickAction:()=>dt,DefaultFetcherProperties:()=>l,EvaluateAction:()=>$t,ExtractAction:()=>xt,FetchAction:()=>d,FetchActionResultStatus:()=>h,FetchEngine:()=>G,FetchSession:()=>z,FetcherOptionKeys:()=>u,FillAction:()=>wt,GetContentAction:()=>pt,GotoAction:()=>yt,PauseAction:()=>bt,PlaywrightFetchEngine:()=>ft,SubmitAction:()=>mt,TRIM_PRESETS:()=>V,TrimAction:()=>vt,WaitForAction:()=>gt,WebFetcher:()=>J,fetchWeb:()=>_t}),module.exports=(t=c,a(i({},"__esModule",{value:!0}),t));var l={engine:"auto",enableSmart:!0,useSiteRegistry:!0,antibot:!1,debug:!1,headers:{},cookies:[],throwHttpErrors:void 0,output:{cookies:!0,sessionState:!0},proxy:[],blockResources:[],storage:{purge:!0},ignoreSslErrors:!0,browser:{engine:"playwright",headless:!0,waitUntil:"domcontentloaded"},http:{method:"GET"},timeoutMs:6e4,requestHandlerTimeoutSecs:void 0,maxConcurrency:1,maxRequestsPerMinute:1e3,delayBetweenRequestsMs:0,retries:0,sites:[]},u=Object.keys(l).concat(["actions","onPause"]),h=(t=>(t[t.Failed=0]="Failed",t[t.Success=1]="Success",t[t.Skipped=2]="Skipped",t))(h||{}),f=class t{static register(t){const e=t.id;if(!e)throw new Error("FetchAction.register: actionClass.id is required");this.registry.set(e,t)}static get(t){return this.registry.get(t)}static create(e){const i="string"==typeof e?e:e.id||e.name||e.action;if(!i)throw new Error("Action must have id, name or action");const n=i instanceof t?i.constructor:this.registry.get(i);return n?new n:void 0}static has(t){return this.registry.has(t)}static list(){return Array.from(this.registry.keys())}static getCapability(t){return this.capabilities[t]??"noop"}getCapability(t){return this.constructor.getCapability(t)}get id(){return this.constructor.id}get returnType(){return this.constructor.returnType}get capabilities(){return this.constructor.capabilities}async delegateToEngine(t,e,...i){const n=t.internal.engine;if(!n)throw new Error("No engine available");if("function"!=typeof n[e])throw new Error(`Engine does not have a method named '${String(e)}'`);return await n[e](...i)}installCollectors(e,i){const n=i?.collectors;if(!n?.length)return;const s=[],r=new Set;for(const i of n){const n=w(i.activateOn),o=w(i.collectOn),a=w(i.deactivateOn),c=!(i.background??!0),l=t.create(i);if(!l)continue;let u=!1,h=!1,f=0;const d=async t=>{if(!u&&!h){u=!0;try{await(l.onBeforeExec?.(e,i))}catch(t){e.eventBus.emit("collector:error",{action:this.id,collector:l.id,phase:"before",error:t})}}},m=async(t,n)=>{if(!h){u||await d(n);try{const s=Promise.resolve(l.onExecute?.(e,i,n)).then(n=>{var s,r;if(i.storeAs){((s=e.outputs)[r=i.storeAs]||(s[r]=[])).push(n)}return e.eventBus.emit("collector:result",{action:this.id,collector:i.id||i.name,event:t,result:n}),n}).catch(n=>{e.eventBus.emit("collector:error",{action:this.id,collector:i.id||i.name,event:t,phase:"exec",error:n})}).finally(()=>{f++});c&&(r.add(s),s.finally(()=>r.delete(s)))}catch(i){e.eventBus.emit("collector:error",{action:this.id,collector:l.id,event:t,phase:"exec",error:i})}}},g=async()=>{if(!h){0===f&&m("collector:after"),h=!0;try{await(l.onAfterExec?.(e,i))}catch(t){e.eventBus.emit("collector:error",{action:this.id,collector:i.id||i.name,phase:"after",error:t})}finally{e.eventBus.emit("collector:end",{action:this.id,collector:i.id||i.name}),b.forEach(t=>t())}}},x=p(e,n,d),b=y(e,o,m),v=p(e,a,g);if(s.push(...x,...b,...v),!n.length&&!o.length&&!a.length){const t=()=>{g()};e.eventBus.once(`action:${this.id}.end`,t),s.push(()=>e.eventBus.off("fetcher:action:end",t))}}return s.length||r.size>0?{cleanup:()=>s.forEach(t=>t()),awaitExecPendings:async()=>{r.size>0&&await Promise.allSettled(Array.from(r))}}:void 0}async beforeExec(t,e){t.internal.actionStack||(t.internal.actionStack=[]);const i=t.internal.actionStack,n=i.length,s=i.length>0?i[i.length-1].id:void 0,r={...e,id:this.id,depth:n,parent:s};i.push(r),t.currentAction=r;const o={action:this,context:t,options:e,index:e?.index,depth:n,stack:[...i]};t.eventBus.emit(`action:${this.id}.start`,o),t.eventBus.emit("action:start",o),await(this.onBeforeExec?.(t,e));return{entry:o,collectors:this.installCollectors(t,e)}}async afterExec(t,e,i,n){const s=t.internal.actionStack,r=s.length-1,o=n?.collectors;try{await(o?.awaitExecPendings()),t.lastResult=i,"response"!==i?.returnType||i.error||(t.lastResponse=i.result),e?.storeAs&&(t.outputs[e.storeAs]=i?.result),i?.error&&(t.currentAction.error=i.error),await(this.onAfterExec?.(t,e));const n={action:this,context:t,options:e,result:i,depth:r,stack:[...s]};i?.error&&(n.error=i.error);try{t.eventBus.emit(`action:${this.id}.end`,n)}catch(t){}try{t.eventBus.emit("action:end",n)}catch(t){}}finally{try{o?.cleanup()}finally{s.pop();const e=s.length;t.currentAction=e>0?s[e-1]:void 0}}}async execute(t,e){e?.args&&!e.params&&(e.params=e.args);const i=await this.beforeExec(t,e),n=e?.failOnError??!0;let s;try{return t.throwHttpErrors=n,s=await this.onExecute(t,e),s&&s.returnType||(s={status:1,returnType:this.returnType??"any",result:s}),s}catch(e){if(s={status:0,error:e,meta:{id:this.id,engineType:t.engine,capability:this.getCapability(t.engine)}},n)throw e;return s}finally{await this.afterExec(t,e,s,i)}}};f.registry=new Map,f.returnType="any",f.capabilities={http:"noop",browser:"noop"};var d=f;function w(t){return t?Array.isArray(t)?t:[t]:[]}function p(t,e,i){const n=[];for(const s of e)if("string"==typeof s||s instanceof RegExp){const e=(...t)=>{i(t[0])};t.eventBus.once(s,e),n.push(()=>t.eventBus.off(s,e))}return n}function y(t,e,i){const n=[];for(const s of e)if("string"==typeof s||s instanceof RegExp){const e=t=>i(s,t);t.eventBus.on(s,e),n.push(()=>t.eventBus.off(s,e))}return n}var m=require("events-ex");var g=require("lodash-es"),x=(0,require("nanoid").customAlphabet)("0123456789abcdefghijklmnopqrstuvwxyz",12);function b(t,e,...i){if(!t)return;const{prefix:n,id:s,category:r}=e;if(!0===t||t===r||Array.isArray(t)&&t.includes(r)){const t=s?`:${s}`:"";console.log(`[${n}${t}:${r}]`,...i)}}var v=require("lodash-es"),$=require("events-ex"),_=require("@isdk/common-error"),q=require("crawlee"),S=require("@isdk/common-error"),E=new Set(["string","number","boolean","html","object","array"]),C=new Set(["selector","has","exclude","required","strict","relativeTo","order","anchor","depth"]);function k(t){if("string"==typeof t)return{type:"string",selector:t,mode:"text"};if(!t||"object"!=typeof t)return{type:"string",mode:"text"};let e={...t};if(function(t){if(!t||"object"!=typeof t)return!1;if(Array.isArray(t))return!1;if("type"in t)return"string"!=typeof t.type||!E.has(t.type);const e=Object.keys(t);if(0===e.length)return!1;for(const t of e)if(!["selector","attribute","has","exclude","mode","required","strict","relativeTo","order","anchor","depth"].includes(t))return!0;return!1}(e)){const t={};for(const i of Object.keys(e))C.has(i)||(t[i]=k(e[i]),delete e[i]);e.type="object",e.properties=t}if(e.type||(e.type="string"),"object"===e.type){const t=e;t.properties||(t.properties={});for(const e in t.properties)t.properties[e]=k(t.properties[e]);delete t.mode,delete t.items,delete t.attribute}else if("array"===e.type){const t=e;t.attribute&&!t.items&&(t.items={type:"string",attribute:t.attribute,mode:"text"},delete t.attribute),t.items||(t.items={type:"string",mode:"text"}),t.items=k(t.items),"string"==typeof t.mode&&(t.mode={type:t.mode})}else{const t=e;t.mode||("html"===t.type?t.mode="html":t.mode="text")}if(e.selector&&(e.has||e.exclude)){const{selector:t,has:i,exclude:n}=e,s=t.split(",").map(t=>t.trim()).map(t=>{let e=t;return i&&(e+=`:has(${i})`),n&&(e+=`:not(${n})`),e});e.selector=s.join(", "),delete e.has,delete e.exclude}return e}async function A(t,e,i){const n=k(t);return R.call(this,n,e,i)}async function R(t,e,i){const n=t.type,s=t.selector,r=t.strict??i;if(!e)return this._logDebug("extract",`_extract: No scope for selector "${s||""}", type "${n||"value"}"`),"array"===n?[]:null;switch(n){case"object":return j.call(this,t,e,r);case"array":return O.call(this,t,e,r);default:return P.call(this,t,e,r)}}async function j(t,e,i){const{selector:n,properties:s,strict:r}=t,o=r??i,a=t._skipSelector;let c=e;if(n&&!a){const t=await this._querySelectorAll(e,n);c=t.length>0?t[0]:null,this._logDebug("extract",`_extractObject: selector "${n}" found ${t.length} elements`)}if(!c){if(this._logDebug("extract",`_extractObject: scope not found for selector "${n||""}"`),o&&t.required)throw new S.CommonError(`Required object "${n||""}" is missing.`,"extract");return null}let l=t.depth??0;const u=l;for(;;){const{result:i,hasValue:r,missingRequired:h}=await T.call(this,t,c,o);if(0===h.length)return!n&&!r&&Object.keys(s).length>0?(this._logDebug("extract","_extractObject result: null"),null):(this._logDebug("extract","_extractObject result:",i),i);let f=!1;if(l>0)if(a)f=!0;else{const t=await this._isSameElement(c,e),i=await this._contains(e,c);f=!t&&i}if(f){const t=await this._parentElement(c);if(t){let i=!0;if(a||(i=await this._isSameElement(e,t)||await this._contains(e,t)),i){this._logDebug("extract",`_extractObject: missing required fields [${h.join(", ")}], bubbling up from depth ${u-l} to ${u-l+1}`),c=t,l--;continue}}}if(o)throw new S.CommonError(`Required property "${h[0]}" is missing.`,"extract");return null}}async function T(t,e,i){const{properties:n,relativeTo:s,order:r}=t,o={},a=[];let c=!1;const l=r||Object.keys(n);let u=e;const h=new Map,f="previous"===s;for(const t of l){const s=n[t];if(!s)continue;this._logDebug("extract",`_extractObject: extracting property "${t}"`);let r,l=u;if(s.anchor){const r=await M.call(this,s.anchor,n,h,e,f,s.depth);if(!r){if(i)throw new S.CommonError(`Anchor "${s.anchor}" not found or out of scope.`,"extract");o[t]=null,s.required&&a.push(t);continue}l=r.scopeForField,f&&(u=l)}let d=null;const w=s.selector,p="array"===s.type;if(w){let t=await this._querySelectorAll(l,w);if(t.length>0){void 0!==s.depth&&"object"!==s.type&&(t=await Promise.all(t.map(t=>L.call(this,t,l,s.depth)))),d=t[0];const e={...s,_skipSelector:!0};if(r=await R.call(this,e,p?t:d,i),f&&!s.anchor){const e=p&&Array.isArray(r)?t[t.length-1]:d;u=await B.call(this,e,u)}p&&(d=t[t.length-1])}else r=null}else r=await R.call(this,s,l,i),null!==r&&(d=Array.isArray(l)?l[0]:l);d&&h.set(t,d),null===r&&s.required&&a.push(t),null!==r&&(c=!0),o[t]=r}return{result:o,hasValue:c,missingRequired:a}}async function O(t,e,i){const{selector:n,items:s,mode:r,strict:o}=t,a=o??i,c=t._skipSelector;let l=n&&!c?await this._querySelectorAll(e,n):Array.isArray(e)?e:[e];n&&!c&&void 0!==t.depth&&(l=await Promise.all(l.map(i=>L.call(this,i,e,t.depth)))),this._logDebug("extract",`_extractArray: selector "${n||""}" found ${l.length} elements`);const u=F.call(this,r);if(void 0!==a&&void 0===u.strict&&(u.strict=a),(!r||"columnar"===u.type)&&1===l.length&&s){this._logDebug("extract","_extractArray: trying columnar extraction");const t=await U.call(this,s,l[0],u);if(t)return t}if("segmented"===u.type&&s){this._logDebug("extract",`_extractArray: trying segmented extraction for ${l.length} containers`);const t=[];let e=!1;for(const i of l){const n=await H.call(this,s,i,u);n&&(e=!0,t.push(...n))}if(e)return t}return this._logDebug("extract",`_extractArray: using nested extraction for ${l.length} elements`),N.call(this,s,l,{strict:u.strict})}async function P(t,e,i){const{selector:n}=t,s=t._skipSelector,r=t.strict??i;let o=e;if(n&&!s){const i=await this._querySelectorAll(e,n);o=i.length>0?i[0]:null,o&&void 0!==t.depth&&(o=await L.call(this,o,e,t.depth)),this._logDebug("extract",`_extractValue: selector "${n}" found ${i.length} elements`)}else Array.isArray(e)&&(o=e.length>0?e[0]:null);if(!o){if(this._logDebug("extract",`_extractValue: element not found for selector "${n||""}"`),r&&t.required)throw new S.CommonError(`Required value "${n||""}" is missing.`,"extract");return null}const a=await this._extractValue(t,o);return this._logDebug("extract",`_extractValue: extracted for selector "${n||""}":`,a),a}function F(t){return t?"string"==typeof t?{type:t}:t:{type:"nested"}}async function N(t,e,i){const n=[],s=t.required,r=!0===i?.strict,o="object"===t.type||"array"===t.type;for(const i of e){const e=await R.call(this,t,i,r);if(null!==e)n.push(e);else{if(s&&r)throw new S.CommonError("Required item is missing in array.","extract");s||o||n.push(null)}}return n}async function U(t,e,i){const n="object"===t.type,s=!0===i?.strict,r=!0===i?.inference;if(n){const i=t.properties,n=Object.keys(i);if(0===n.length)return null;const o={},a={};let c=null,l=0,u=[];for(const t of n){const n=i[t];if("array"===n.type||"object"===n.type)return this._logDebug("extract",`_extractColumnar: field "${t}" has nested structure, columnar not supported`),null;const h=n;let f=[];f=h.selector?await this._querySelectorAll(e,h.selector):[e],a[t]=f;const d=f.length;if(this._logDebug("extract",`_extractColumnar: field "${t}" with selector "${h.selector||""}" found ${d} matches`),d>l&&(l=d,u=f),h.selector)if(null===c)c=d,this._logDebug("extract",`_extractColumnar: set commonCount to ${c}`);else if(c!==d)if(this._logDebug("extract",`_extractColumnar: count mismatch for field "${t}": ${d} vs ${c}`),r&&l>1)c=-1,this._logDebug("extract","_extractColumnar: mismatch marked for inference");else if(s){if(!(1===d&&await this._isSameElement(f[0],e))){if(n.required&&d<c)throw new S.CommonError(`Required field "${t}" is missing at index ${d}.`,"extract");throw new S.CommonError(`Columnar extraction mismatch: field "${t}" has ${d} matches, but expected ${c}.`,"extract")}}const w=await Promise.all(f.map(t=>this._extractValue(h,t)));this._logDebug("extract",`_extractColumnar: field "${t}" values:`,w),o[t]=w}if(r&&-1===c&&l>1&&u.length>0){const i=[];for(const t of u){const n=await this._findContainerChild(t,e);n&&i.push(n)}const n=[];for(const t of i){await this._findClosestAncestor(t,n)||n.push(t)}if(n.length>1)return N.call(this,t,n,{strict:s})}if(l<=1)return null;if(-1===c&&s)return null;const h=s&&-1!==c?c:l,f={};if(h>1)for(const t of n){if(1===o[t].length){(!i[t].selector||await this._isSameElement(a[t][0],e))&&(f[t]=!0)}}const d=[];for(let t=0;t<h;t++){const e={};let r=!1;for(const a of n){const n=o[a],c=i[a];let l=n[t];if(f[a]&&(l=n[0]),void 0===l&&(l=null),null===l&&c.required){if(this._logDebug("extract",`_extractColumnar: skipping row ${t} because required field "${a}" is null`),s)throw new S.CommonError(`Required field "${a}" is missing at index ${t}.`,"extract");r=!0;break}e[a]=l}r||d.push(e)}return d}{const i=t;if(!i.selector)return null;const n=await this._querySelectorAll(e,i.selector);if(n.length<=1)return null;const s=await Promise.all(n.map(t=>this._extractValue(i,t)));return i.required?s.filter(t=>null!==t):s}}async function H(t,e,i){if(!("object"===t.type))return null;const n=t.properties,s=Object.keys(n);if(0===s.length)return null;let r;if(i?.anchor)r=n[i.anchor]?.selector||i.anchor;else for(const t of s)if(n[t].selector){r=n[t].selector;break}if(!r)return this._logDebug("extract","_extractSegmented: no anchor selector found, falling back to nested"),null;const o=await this._querySelectorAll(e,r);if(this._logDebug("extract",`_extractSegmented: anchor selector "${r}" found ${o.length} elements`),0===o.length){if(i?.strict)throw new S.CommonError(`Segmented extraction failed: no elements found for anchor selector "${r}".`,"extract");return[]}const a=[];for(let n=0;n<o.length;n++){const s=o[n],c=n>0?o[n-1]:null,l=n<o.length-1?o[n+1]:null;let u,h=s,f=null;if(c&&(f=await this._findCommonAncestor(s,c)),!f&&l)f=await this._findCommonAncestor(s,l);else if(f&&l){const t=await this._findCommonAncestor(s,l);t&&await this._contains(f,t)&&(f=t)}if(f){const t=await L.call(this,s,f,i?.depth);t&&!await this._isSameElement(t,s)&&(h=t)}else{const t=await L.call(this,s,e,i?.depth);t&&(h=t)}if(await this._isSameElement(h,s)){u=[s,...await this._nextSiblingsUntil(s,r)],this._logDebug("extract",`_extractSegmented: segment ${n} (flat) created with ${u.length} elements`)}else u=h,this._logDebug("extract",`_extractSegmented: segment ${n} (nested) identified as container element`);const d={...t};i?.relativeTo&&!d.relativeTo&&(d.relativeTo=i.relativeTo);const w=await R.call(this,d,u,i?.strict),p=t.required,y="object"===t.type||"array"===t.type;if(null!==w)a.push(w);else{if(p&&i?.strict)throw new S.CommonError("Required item is missing in array.","extract");p||y||a.push(null)}}return a}async function L(t,e,i){const n=Array.isArray(e),s=n?e:[e],r=n?await this._findClosestAncestor(t,s):await this._findContainerChild(t,e);if(void 0===i||!r)return r;let o=t;for(let t=0;t<i&&!await this._isSameElement(o,r);t++){const t=await this._parentElement(o);if(!t||!await this._contains(r,t))break;o=t}return o}async function M(t,e,i,n,s,r){let o=null;if(e.hasOwnProperty(t))o=i.get(t)||null;else{const e=await this._querySelectorAll(n,t);e.length>0&&(o=e[0])}if(o){const t=[];let e=o,i=0;const s=void 0!==r?r:1e3;for(;e&&i<=s;){const s=await this._nextSiblingsUntil(e);t.push(...s);const r=await this._parentElement(e);if(!r)break;if(Array.isArray(n)?null!==await this._findClosestAncestor(r,n):await this._isSameElement(r,n))break;e=r,i++}if(t.length>0||void 0!==r)return{scopeForField:t}}return null}async function B(t,e){const i=await L.call(this,t,e);if(i){if(!Array.isArray(e))return this._nextSiblingsUntil(i);{let t=e.indexOf(i);if(-1===t)for(let n=0;n<e.length;n++)if(await this._isSameElement(e[n],i)){t=n;break}if(-1!==t)return e.slice(t+1)}}return Array.isArray(e)?e:[e]}function I(){let t=()=>{};const e=new Promise(e=>{t=e});return e.release=t,e}q.Configuration.getGlobalConfig().set("persistStorage",!1);var V={scripts:["script"],styles:["style",'link[rel="stylesheet"]'],svgs:["svg"],images:["img","picture","canvas"],hidden:["[hidden]",'[style*="display:none"]','[style*="display: none"]']},G=class{constructor(){this.hdrs={},this._initializedSessions=new Set,this.pendingRequests=new Map,this.requestCounter=0,this.actionEmitter=new $.EventEmitter,this.isPageActive=!1,this.isEngineDisposed=!1,this.navigationLock=function(){const t=I();return t.release(),t}(),this.isExecutingAction=!1,this.actionQueue=[],this.isProcessingActionLoop=!1,this.blockedTypes=new Set}static register(t){const e=t.id;if(!e)throw new Error("Engine must define static id");if(this.registry.has(e))throw new Error(`Engine id duplicated: ${e}`);this.registry.set(e,t)}static get(t){return this.registry.get(t)}static getByMode(t){for(const[e,i]of this.registry.entries())if(i.mode===t)return i}static async create(t,e){const i=(0,v.defaultsDeep)(e,t,l),n=i.engine??t.engine,s=n?this.get(n)??this.getByMode(n):null;if(s){const e=new s;return await e.initialize(t,i),e}}_logDebug(t,...e){b(this.opts?.debug,{prefix:"FetchEngine",id:this.id,category:t},...e)}_getTrimInfo(t){let{selectors:e=[],presets:i=[]}=t;"string"==typeof e&&(e=[e]),"string"==typeof i&&(i=[i]);const n=i.includes("all"),s=[...e];for(const[t,e]of Object.entries(V))(n||i.includes(t))&&s.push(...e);return{selectors:s,removeComments:n||i.includes("comments"),removeHidden:n||i.includes("hidden")}}async _extract(t,e,i){return R.call(this,t,e,i)}_normalizeArrayMode(t){return F.call(this,t)}async _extractNested(t,e,i){return N.call(this,t,e,i)}async _extractColumnar(t,e,i){return U.call(this,t,e,i)}async _extractSegmented(t,e,i){return H.call(this,t,e,i)}async buildResponse(t){const e=await this._buildResponse(t),i=e.headers["content-type"]||"";return e.contentType=i.split(";")[0].trim(),!1!==this.opts?.output?.cookies?!e.cookies&&t.session&&(e.cookies=t.session.getCookies(t.request.url)):delete e.cookies,!1!==this.opts?.output?.sessionState?this.crawler?.sessionPool&&(e.sessionState=await this.crawler.sessionPool.getState()):delete e.sessionState,this.opts?.debug&&(e.metadata={...e.metadata,mode:this.mode,engine:this.id,proxy:t.proxyInfo?.url||("string"==typeof this.opts.proxy?this.opts.proxy:Array.isArray(this.opts.proxy)?this.opts.proxy[0]:void 0)}),e}waitFor(t){return this.dispatchAction({type:"waitFor",options:t})}click(t){return this.dispatchAction({type:"click",selector:t})}fill(t,e){return this.dispatchAction({type:"fill",selector:t,value:e})}submit(t,e){return this.dispatchAction({type:"submit",selector:t,options:e})}trim(t){return this.dispatchAction({type:"trim",options:t})}pause(t){return this.dispatchAction({type:"pause",message:t})}evaluate(t){return this.dispatchAction({type:"evaluate",params:t})}extract(t){t&&"object"==typeof t&&t.schema&&(t=t.schema);const e=k(t);return this.dispatchAction({type:"extract",schema:e})}get id(){return this.constructor.id}async getState(){return{cookies:await this.cookies(),sessionState:await(this.crawler?.sessionPool?.getState())}}get mode(){return this.constructor.mode}get context(){return this.ctx}async initialize(t,e){if(this.ctx)return;(0,v.merge)(t,e),this.ctx=t,this.opts=t,this.hdrs=function(t){const e={};if(t&&"object"==typeof t)for(const[i,n]of Object.entries(t))e[i.toLowerCase()]=n;return e}(t.headers),this._initialCookies=[...t.cookies??[]],t.internal||(t.internal={}),t.internal.engine=this,t.engine=this.mode,this.actionEmitter.setMaxListeners(100);const i=t.storage||{},n=i.persist??!1,s=this.config=new q.Configuration({persistStorage:n,storageClientOptions:{persistStorage:n,...i.config},...i.config}),r=i.id||t.id;this.requestQueue=await q.RequestQueue.open(r,{config:s});const o=this.opts?.proxy?"string"==typeof this.opts.proxy?[this.opts.proxy]:this.opts.proxy:void 0;o?.length&&(this.proxyConfiguration=new q.ProxyConfiguration({proxyUrls:o}));const a=await this._getSpecificCrawlerOptions(t),c=(0,v.defaultsDeep)({persistenceOptions:{enable:!0,storeId:r},persistStateKeyValueStoreId:r},t.sessionPoolOptions,{maxPoolSize:1,sessionOptions:{maxUsageCount:1e3,maxErrorScore:3}});t.sessionState&&t.cookies&&t.cookies.length>0&&console.warn('[FetchEngine] Warning: Both "sessionState" and "cookies" are provided. Explicit "cookies" will override any conflicting cookies restored from "sessionState".');const l={...(0,v.defaultsDeep)(a,{requestQueue:this.requestQueue,maxConcurrency:1,minConcurrency:1,useSessionPool:!0,persistCookiesPerSession:!0,sessionPoolOptions:c}),requestHandler:this._requestHandler.bind(this),errorHandler:this._failedRequestHandler.bind(this),failedRequestHandler:this._failedRequestHandler.bind(this)};l.preNavigationHooks||(l.preNavigationHooks=[]),l.preNavigationHooks.unshift(({crawler:t,session:e,request:i},n)=>{if(this.currentSession=e,e&&!this._initializedSessions.has(e.id)){if(this._initialCookies&&this._initialCookies.length>0){const t=this._initialCookies.map(t=>{const e={...t};return"no_restriction"===e.sameSite&&(e.sameSite="None"),e});e.setCookies(t,i.url)}this._initializedSessions.add(e.id)}});const u=this.crawler=this._createCrawler(l,s),h=this.kvStore=await q.KeyValueStore.open(r,{config:s}),f=await h.getValue(q.PERSIST_STATE_KEY);!t.sessionState||f&&!t.overrideSessionState||await h.setValue(q.PERSIST_STATE_KEY,t.sessionState),this.isCrawlerReady=!0,this.crawlerRunPromise=u.run(),this.crawlerRunPromise.finally(()=>{this.isCrawlerReady=!1}).catch(t=>{console.error("Crawler background error:",t)})}async cleanup(){await(this._cleanup?.()),await this._commonCleanup();const t=this.ctx;t&&t.internal?.engine===this&&(t.internal.engine=void 0),this.ctx=void 0,this.opts=void 0}async _processAction(t,e){switch(this._logDebug(e.type,"Executing action:",e),e.type){case"extract":return A.call(this,e.schema,this._getInitialElementScope(t));case"pause":return this._handlePause(e);case"getContent":return this.buildResponse(t);case"waitFor":return e.options?.ms&&1===Object.keys(e.options).length?void await new Promise(t=>setTimeout(t,e.options.ms)):this.executeAction(t,e);default:return this.executeAction(t,e)}}async _handlePause(t){const e=this.ctx?.onPause;e?(console.info(t.message||"Execution paused for manual intervention."),await e({message:t.message}),console.info("Resuming execution...")):console.warn("[PauseAction] was called, but no `onPause` handler was provided in fetchWeb options. Skipped.")}async _executePendingActions(t){if(this.isEngineDisposed)return;this.activeContext=t;const e=async()=>{if(!this.isProcessingActionLoop){this.isProcessingActionLoop=!0;try{for(;this.actionQueue.length>0&&this.isPageActive&&!this.isEngineDisposed;){const e=this.actionQueue.shift();try{if("dispose"===e.action.type){this.actionEmitter.emit("dispose"),e.resolve();continue}this.isExecutingAction=!0;const i=await this._processAction(t,e.action);e.resolve(i)}catch(t){e.reject(t)}finally{this.isExecutingAction=!1}}}finally{this.isProcessingActionLoop=!1}}};await new Promise(t=>{const i=t=>{this.actionQueue.push(t),e()},n=()=>{this.actionEmitter.removeListener("dispatch",i),this.activeContext=void 0,t()};this.actionEmitter.on("dispatch",i),this.actionEmitter.once("dispose",n),e(),this.isEngineDisposed&&(n(),this.actionEmitter.removeListener("dispose",n))})}async _sharedRequestHandler(t){const{request:e}=t;this._logDebug("request",`Processing request: ${e.url}`);try{this.currentSession=t.session,this.isPageActive=!0;const i=this.pendingRequests.get(e.userData.requestId);if(i){const n=await this.buildResponse(t),s=!n.statusCode||n.statusCode>=400;if(this.ctx?.throwHttpErrors&&s){const t=new _.CommonError(`Request for ${n.finalUrl} failed with status ${n.statusCode||"N/A"}`,"request",n.statusCode);i.reject(t)}else this.lastResponse=n,i.resolve(n);this.pendingRequests.delete(e.userData.requestId)}await this._executePendingActions(t)}finally{if(this.currentSession){const t=this.currentSession.getCookies(e.url);t&&(this._initialCookies=t)}this.isPageActive=!1,this.navigationLock.release()}}async _sharedFailedRequestHandler(t,e){const{request:i}=t,n=this.pendingRequests.get(i.userData.requestId);if(n&&e&&this.ctx?.throwHttpErrors){this.pendingRequests.delete(i.userData.requestId);const t=e.response,s=t?.statusCode||500,r=t?.url?t.url:i.url,o=new _.CommonError(`Request${r?" for "+r:""} failed: ${e.message}`,"request",s);n.reject(o)}return this._sharedRequestHandler(t)}async dispatchAction(t){if(!this.isPageActive)throw new Error("No active page. Call goto() before performing actions.");return this.isExecutingAction&&this.activeContext?(this._logDebug(t.type,"Re-entrant action execution:",t),await this._processAction(this.activeContext,t)):new Promise((e,i)=>{this.actionEmitter.emit("dispatch",{action:t,resolve:e,reject:i})})}async _requestHandler(t){await this._sharedRequestHandler(t)}async _failedRequestHandler(t,e){await this._sharedFailedRequestHandler(t,e)}async _commonCleanup(){if(this.isEngineDisposed=!0,this._initializedSessions.clear(),this.actionEmitter.emit("dispose"),this.navigationLock?.release(),this.pendingRequests.size>0){for(const[,t]of this.pendingRequests)t.reject(new Error("Cleanup:Request cancelled"));this.pendingRequests.clear()}if(this.crawler){try{await(this.crawler.teardown?.())}catch(t){console.error("crawler teardown error:",t)}this.crawler=void 0}this.crawlerRunPromise=void 0,this.isCrawlerReady=void 0;const t=(this.opts?.storage||{}).purge??!0;this.requestQueue&&(t&&await this.requestQueue.drop().catch(t=>console.error("Error dropping requestQueue:",t)),this.requestQueue=void 0),this.kvStore&&(t&&await this.kvStore.drop().catch(t=>console.error("Error dropping kvStore:",t)),this.kvStore=void 0),this.actionEmitter.removeAllListeners(),this.pendingRequests.clear(),this.actionQueue=[],this.config=void 0}async blockResources(t,e){return e&&this.blockedTypes.clear(),t.forEach(t=>this.blockedTypes.add(t)),t.length}getContent(){return this.lastResponse?Promise.resolve(this.lastResponse):Promise.reject(new Error("No content fetched yet. Call goto() first."))}async headers(t,e){if(void 0===t)return{...this.hdrs};if("string"==typeof t&&void 0===e)return this.hdrs[t.toLowerCase()]||"";if(null!==t&&"object"==typeof t){const i={};for(const[e,n]of Object.entries(t))i[e.toLowerCase()]=String(n);return this.hdrs=!0===e?i:{...this.hdrs,...i},!0}return"string"==typeof t&&("string"==typeof e?this.hdrs[t.toLowerCase()]=e:null===e&&delete this.hdrs[t.toLowerCase()],!0)}async cookies(t){const e=this.lastResponse?.url||"";if(Array.isArray(t))return this.currentSession?this.currentSession.setCookies(t,e):this._initialCookies=[...t],!0;if(null===t)return this.currentSession,this._initialCookies=[],!0;if(this.currentSession){return this.currentSession.getCookies(e)}return[...this._initialCookies||[]]}async dispose(){await this.cleanup()}};async function D(t,e){let i;const n=e?.engine||t.engine;if(n&&"auto"!==n){if(i=await G.create(t,{engine:n}),!i)throw new Error(`Engine "${n}" is not available or failed to initialize.`);return i}const s=function(t,e){if(!t||!e?.length)return null;const i=new URL(t);let n=e.find(t=>t.domain===i.hostname);n||(n=e.find(t=>i.hostname.endsWith(t.domain)));if(!n)return null;if(n.pathScope?.length){if(!n.pathScope.some(t=>i.pathname.startsWith(t)))return null}return n}(e?.url||t.url,t.sites);if(s?.engine&&"auto"!==s.engine&&(i=await G.create(t,{engine:s.engine}),i))return i;if(i=await G.create(t,{engine:"http"}),!i)throw new Error("Failed to create default http engine");return i}G.registry=new Map;var z=class{constructor(t={}){this.options=t,this.closed=!1,this.id=x(),this.context=this.createContext(t)}_logDebug(t,...e){b(this.context.debug,{prefix:"FetchSession",id:this.id.slice(0,8),category:t},...e)}async execute(t,e=this.context){const i=t.id||t.name||t.action;this._logDebug("execute",`Executing action: ${i}`,t.params);const n=t.index??(e.internal.actionIndex||0);e.internal.actionIndex=n+1,await this.ensureEngine(t,e);const s=d.create(t);if(!s)throw new Error(`Unknown action: ${t.id||t.name}`);const r={...t,index:n};let o,a;e.currentAction={...r,startedAt:Date.now()};try{return o=await s.execute(e,r),o}catch(t){throw a=t,a}finally{e.currentAction=void 0}}async executeAll(t,e){this._logDebug("executeAll",`Total actions: ${t.length}`,t.map(t=>t.id||t.name||t.action));const i=e?{...this.context,...e,id:this.context.id,eventBus:this.context.eventBus,outputs:this.context.outputs,execute:this.context.execute,action:this.context.action}:this.context;let n=e?.index??0;try{for(;n<t.length;){const e=t[n];await this.execute({...e,index:n},i),n++}const e=await this.execute({id:"getContent",index:n},i);return{result:e?.result,outputs:this.getOutputs()}}catch(t){throw t.actionIndex=n,t}}getOutputs(){return this.context.outputs}async getState(){return this.context.internal.engine?.getState()}async dispose(){if(this.closed)return;const t=this.context.eventBus;t.emit("session:closing",{sessionId:this.id});try{await(this.context.internal.engine?.dispose())}finally{this.closed=!0}t.emit("session:closed",{sessionId:this.id})}async ensureEngine(t,e){if(this.closed)throw new Error("Session is closed");if(!e.internal.engine){const i=t?.params?.url??e.url,n=await D(e,{url:i});if(!n)throw new Error("No engine found");e.internal.engine=n}}createContext(t=this.options){const e=new m.EventEmitter;return(0,g.defaultsDeep)({...t,id:this.id,eventBus:e,outputs:{},internal:{},execute:async t=>this.execute(t),action:async function(t,e,i){return this.execute({name:t,params:e,...i})}},l)}},J=class{constructor(t={}){this.defaults=t}async createSession(t){const e={...this.defaults,...t||{}};return new z(e)}async fetch(t,e){"string"!=typeof t&&(t=(e=t).url);const i=await this.createSession(e);try{const n=e?.actions||[];t&&0!==n.findIndex(e=>("goto"===e.id||"goto"===e.name)&&e.params?.url===t)&&n.unshift({id:"goto",params:{url:t}});return await i.executeAll(n)}finally{await i.dispose()}}},W=require("crawlee"),K=((t,n,s)=>(s=null!=t?e(r(t)):{},a(!n&&t&&t.__esModule?s:i(s,"default",{value:t,enumerable:!0}),t)))(require("cheerio")),Q=require("util-ex"),Z=require("@isdk/common-error"),X="___BR___",Y="___BLOCK___",tt="___P___",et=/\s+/g,it=new RegExp(` *(${X}|${Y}|${tt}) *`,"g"),nt=new RegExp(`(?:${Y}|${tt})+`,"g");var st={"&amp;":"&amp;","&lt;":"&lt;","&gt;":"&gt;"},rt={"&quot;":'"',"&apos;":"'","&nbsp;":" ","&copy;":"©","&reg;":"®","&trade;":"™","&sect;":"§","&para;":"¶","&bull;":"•","&hellip;":"…","&euro;":"€","&pound;":"£","&yen;":"¥","&cent;":"¢","&curren;":"¤","&brvbar;":"¦","&uml;":"¨","&ordf;":"ª","&laquo;":"«","&raquo;":"»","&not;":"¬","&shy;":"","&macr;":"¯","&deg;":"°","&plusmn;":"±","&sup2;":"²","&sup3;":"³","&acute;":"´","&micro;":"µ","&middot;":"·","&cedil;":"¸","&sup1;":"¹","&ordm;":"º","&iquest;":"¿","&times;":"×","&divide;":"÷","&ndash;":"–","&mdash;":"—","&lsquo;":"‘","&rsquo;":"’","&sbquo;":"‚","&ldquo;":"“","&rdquo;":"”","&bdquo;":"„","&dagger;":"†","&Dagger;":"‡","&permil;":"‰","&lsaquo;":"‹","&rsaquo;":"›"};function ot(t){return t?t.replace(/&(#?[a-zA-Z0-9]+);/g,t=>{const e=t.toLowerCase();if(st[e])return t;if(rt[e])return rt[e];if(t.startsWith("&#")){const e=t.startsWith("&#x")?parseInt(t.slice(3,-1),16):parseInt(t.slice(2,-1),10);if(!isNaN(e)){if(160===e)return" ";try{return String.fromCodePoint(e)}catch(e){return t}}}return t}):t}var at=class extends G{_ensureCheerioContext(t){if(!t.$&&t.body){let e="string"==typeof t.body?t.body:Buffer.isBuffer(t.body)?t.body.toString("utf-8"):JSON.stringify(t.body);e.trim().startsWith("<")||(e=`<html><body><pre>${e}</pre></body></html>`),t.$=K.load(e)}}async _buildResponse(t){this._ensureCheerioContext(t);const{request:e,response:i,body:n,$:s}=t,r=s?.html();let o="string"==typeof n?n:Buffer.isBuffer(n)?n.toString("utf-8"):String(n??"");r&&r!==o&&(o=r);let a=i?.headers;if(!a&&i?.rawHeaders){a={};const t=i.rawHeaders;for(let e=0;e<t.length;e+=2)a[t[e].toLowerCase()]=t[e+1]}const c={url:e.url,finalUrl:e.loadedUrl||e.url,statusCode:i?.statusCode??200,statusText:i?.statusMessage,headers:a||{},body:n,html:ot(o),text:o};if(this.opts?.debug&&i?.timings){const t=i.timings;c.metadata={timings:{start:t.start,total:t.phases?.total,ttfb:t.phases?.firstByte,dns:t.phases?.dns,tcp:t.phases?.tcp,download:t.phases?.download}}}return c}async _querySelectorAll(t,e){if(Array.isArray(t)){if(0===t.length)return[];const{$:i}=t[0],n=t.map(t=>t.el[0]).filter(Boolean),s=i(n);return s.find(e).add(s.filter(e)).toArray().map(t=>({$:i,el:i(t)}))}const{$:i,el:n}=t;return":scope"===e?[{$:i,el:n}]:n.find(e).add(n.filter(e)).toArray().map(t=>({$:i,el:i(t)}))}async _nextSiblingsUntil(t,e){const{$:i,el:n}=t;return(e?n.nextUntil(e):n.nextAll()).toArray().map(t=>({$:i,el:i(t)}))}async _parentElement(t){const{$:e,el:i}=t,n=i.parent();return 0===n.length?null:{$:e,el:n}}async _isSameElement(t,e){return t.el[0]===e.el[0]}async _findClosestAncestor(t,e){if(0===e.length)return null;const i=new Set(e.map(t=>t.el[0])),{$:n,el:s}=t;let r=s;for(;r.length>0;){if(i.has(r[0]))return{$:n,el:r};r=r.parent()}return null}async _contains(t,e){const i=t.el[0],n=e.el[0];if(i===n)return!0;const s=t.$;return"function"==typeof s.contains?s.contains(i,n):t.el.find(e.el).length>0}async _findCommonAncestor(t,e){const{$:i,el:n}=t,{el:s}=e;if(n[0]===s[0])return t;if(await this._contains(t,e))return t;if(await this._contains(e,t))return e;const r=n.parents().toArray(),o=s.parents().toArray(),a=new Set(o);for(const t of r)if(a.has(t))return{$:i,el:i(t)};return null}async _findContainerChild(t,e){const{$:i,el:n}=t,s=e.el[0];let r=n;if(r[0]===s)return t;const o=r.parents().toArray();for(let t=0;t<o.length;t++)if(o[t]===s){return{$:i,el:i(t>0?o[t-1]:n[0])}}if(s===i.root()[0]){return{$:i,el:i(o.length>0?o[o.length-1]:n[0])}}return null}async _extractValue(t,e){const{$:i,el:n}=e,{attribute:s,type:r="string",mode:o="text"}=t;if(this._logDebug("extract",`_extractValue: el.length=${n.length} schema=${JSON.stringify(t)}`),0===n.length)return null;let a="";if(s?a=n.attr(s)??null:"html"===r||"html"===o||"outerHTML"===o?(a="outerHTML"===o?i.html(n):n.html()??("html"===r?"":null),a&&(a=ot(a.trim()))):a="innerText"===o?function(t){const e=t.clone();e.find("script, style, noscript, template").remove(),e.find("[hidden]").remove(),e.find("br").replaceWith(X),e.find("p").before(tt).after(tt),e.find("div, h1, h2, h3, h4, h5, h6, li, ul, ol, tr, dl, dt, dd, blockquote, pre, form, table, article, section, header, footer, nav, main, aside, hr, address, fieldset, figure, figcaption, details, summary").before(Y).after(Y);let i=e.text();return i=i.replace(et," "),i=i.replace(it,"$1"),i=i.replace(nt,t=>t.includes(tt)?tt:Y),i=i.replaceAll(X,"\n"),i=i.replaceAll(tt,"\n\n"),i=i.replaceAll(Y,"\n"),i.trim()}(n):n.text().trim(),null===a)return null;switch(r){case"number":return parseFloat(a.replace(/[^0-9.-]+/g,""))||null;case"boolean":const t=a.toLowerCase();return"true"===t||"1"===t;default:return a}}_getInitialElementScope(t){const{$:e}=t;return e?{$:e,el:e.root()}:null}async executeAction(t,e){const{$:i}=t;switch(e.type){case"dispose":return;case"navigate":{const{url:i,opts:n}=e;this._logDebug("navigate",`Navigating to: ${i}`);const s=await this._requestWithRedirects(t,{url:i,method:"GET",headers:{...this.hdrs,...n?.headers}});return await this._updateStateAfterNavigation(t,s),this.lastResponse}case"click":{if(!i)throw new Z.CommonError(`Cheerio context not available for action: ${e.type}`,"click");const n=e.selector,s=i(n).first();let r;if(0===s.length)try{r=new URL(n,t.request.loadedUrl||t.request.url).href}catch{throw new Z.CommonError(`click: selector not found or invalid URL: ${n}`,"click")}else{if(!s.is("a")||!s.attr("href")){if(s.is('input[type="submit"], button[type="submit"], button, input')){const e=s.closest("form");return e.length?this.executeAction(t,{type:"submit",selector:e}):void this._logDebug("click","Button/input clicked but no form found and no JS support in http mode. Ignoring.")}throw new Z.CommonError(`click: unsupported element for http simulate. Selector: ${n}`,"click")}{const e=s.attr("href");r=new URL(e,t.request.loadedUrl||t.request.url).href}}const o=await t.sendRequest({url:r});return void await this._updateStateAfterNavigation(t,o)}case"fill":{if(!i)throw new Z.CommonError(`Cheerio context not available for action: ${e.type}`),"fill";const n=i(e.selector).first();if(0===n.length)throw new Z.CommonError(`fill: selector not found: ${e.selector}`);if(!n.is("input, textarea, select"))throw new Z.CommonError(`fill: not a form field: ${e.selector}`);return n.val(e.value),void(this.lastResponse=await this.buildResponse(t))}case"trim":{if(!i)throw new Z.CommonError(`Cheerio context not available for action: ${e.type}`,"trim");const{selectors:n,removeComments:s}=this._getTrimInfo(e.options);return n.forEach(t=>i(t).remove()),s&&i("*").contents().filter((t,e)=>"comment"===e.type).remove(),void(this.lastResponse=await this.buildResponse(t))}case"waitFor":return void(e.options?.ms&&await new Promise(t=>setTimeout(t,e.options.ms)));case"submit":{if(!i)throw new Z.CommonError(`Cheerio context not available for action: ${e.type}`,"submit");const n="string"==typeof e.selector?i(e.selector).first():null!=e.selector?e.selector:i("form").first();if(0===n.length)throw new Z.NotFoundError(e.selector,"submit");const s=n.attr("action")||t.request.loadedUrl||t.request.url,r=(n.attr("method")||"GET").toUpperCase(),o=new URL(s,t.request.loadedUrl||t.request.url).href,a={};let c;if(n.find("input, select, textarea").each((t,e)=>{const n=i(e),s=n.attr("name");if(!s)return;const r=n.val();null!=r&&(a[s]=String(r))}),"GET"===r){const e=new URL(o);Object.entries(a).forEach(([t,i])=>e.searchParams.set(t,i)),c=await this._requestWithRedirects(t,{url:e.href,method:"GET"})}else{const i=e.options?.enctype||n.attr("enctype")||"application/x-www-form-urlencoded";let s;const r={};"application/json"===i?(s=JSON.stringify(a),r["Content-Type"]="application/json"):(s=new URLSearchParams(a).toString(),r["Content-Type"]="application/x-www-form-urlencoded"),this._logDebug("submit","Submitting POST to:",o,"enctype:",i),c=await this._requestWithRedirects(t,{url:o,method:"POST",body:s,headers:r})}return await this._updateStateAfterNavigation(t,c),void this._logDebug("submit","Submit finished. Current URL:",t.request.loadedUrl||t.request.url)}case"evaluate":{const{fn:n,args:s=[]}=e.params,r=t.request.loadedUrl||t.request.url;let o=null;const a=t=>t&&0!==t.length?{textContent:t.text(),innerHTML:t.html(),outerHTML:i.html(t),getAttribute:e=>t.attr(e),matches:e=>t.is(e)}:null,c=this,l={location:{_href:r,get href(){return this._href},set href(t){if(t&&t!==this._href){this._href=t;const e=new URL(t,r).href;o=c.goto(e)}},assign(t){this.href=t},replace(t){this.href=t}}},u={getElementById:t=>a(i(`#${t}`).first()),querySelector:t=>a(i(t).first()),querySelectorAll:t=>i(t).toArray().map(t=>a(i(t))),getElementsByClassName:t=>i(`.${t}`).toArray().map(t=>a(i(t))),getElementsByTagName:t=>i(t).toArray().map(t=>a(i(t))),get body(){return a(i("body").first())},get title(){return i("title").text()}};l.document=u;const h={window:l,document:u,$:i,console:console};let f;const d=(0,Q.newFunction)(n,h);return f="function"==typeof d?await d(s):d,o?await o:l.location.href===r&&(this.lastResponse=await this.buildResponse(t)),f}default:throw new Z.CommonError(`Unknown action type: ${e.type}`,"CheerioFetchEngine.executeAction",Z.ErrorCode.NotSupported)}}async _requestWithRedirects(t,e){let{url:i,method:n,body:s,headers:r={}}=e,o=0;let a;for(;o<=5;){if(t.session){const e=t.session.getCookieString(i);e&&(r={...r,cookie:e})}if(a=await t.sendRequest({url:i,method:n,body:s,headers:r,followRedirect:!1}),!a)break;const e=a.statusCode,c=a.headers||a.req?.res?.headers||a.res?.headers||{};if(t.session&&c["set-cookie"]&&t.session.setCookies(c["set-cookie"],i),[301,302,303,307,308].includes(e)){const t=c.location;if(!t)break;if(i=new URL(t,i).href,o++,[301,302,303].includes(e)){this._logDebug("http",`Redirect ${e} (method conversion to GET):`,i),n="GET",s=void 0;const{"content-type":t,"Content-Type":o,"content-length":a,"Content-Length":c,...l}=r;r=l}else this._logDebug("http",`Redirect ${e} (method preserved):`,i);continue}break}return a}async _updateStateAfterNavigation(t,e){const i=e;t.response=i,t.body=i.body,t.$=void 0,i.url&&(t.request.loadedUrl=i.url),this.lastResponse=await this.buildResponse(t)}_createCrawler(t,e){return new W.CheerioCrawler(t,e)}_getSpecificCrawlerOptions(t){return{additionalMimeTypes:["text/plain"],maxRequestRetries:1,requestHandlerTimeoutSecs:t.requestHandlerTimeoutSecs,proxyConfiguration:this.proxyConfiguration,preNavigationHooks:[({session:e,request:i},n)=>{n.throwHttpErrors=t.throwHttpErrors,this.opts?.timeoutMs&&(n.timeout={request:this.opts.timeoutMs})}]}}async goto(t,e){if(this.isPageActive)return this.dispatchAction({type:"navigate",url:t,opts:e});const i="req-"+ ++this.requestCounter,n=new Promise((t,n)=>{const s=e?.timeoutMs||this.opts?.timeoutMs||3e4,r=setTimeout(()=>{this.pendingRequests.delete(i),this.navigationLock.release(),n(new Z.CommonError(`goto timed out after ${s}ms.`,"gotoTimeout",Z.ErrorCode.RequestTimeout))},s);this.pendingRequests.set(i,{resolve:e=>{clearTimeout(r),t(e)},reject:t=>{clearTimeout(r),n(t)}})});return this.requestQueue.addRequest({...e,url:t,headers:{...this.hdrs,...e?.headers},userData:{requestId:i},uniqueKey:`${t}-${i}`}).catch(t=>{const e=this.pendingRequests.get(i);e&&(this.pendingRequests.delete(i),this.navigationLock.release(),e.reject(t))}),await this.navigationLock,this.navigationLock=I(),n}};at.id="cheerio",at.mode="http",G.register(at);var ct=require("crawlee"),lt=require("playwright"),ut=require("@isdk/common-error"),ht=3e4,ft=class extends G{async _buildResponse(t){const{page:e,response:i,request:n,session:s}=t;if(!e||e.isClosed())return{url:n.url,finalUrl:n.loadedUrl||n.url,statusCode:i?.status(),statusText:i?.statusText(),headers:await(i?.allHeaders())||{},body:"",html:"",text:""};const r=await e.content(),o=await e.textContent("body"),a=await e.context().cookies();s&&s.setCookies(a,n.url);const c={url:e.url(),finalUrl:e.url(),statusCode:i?.status(),statusText:i?.statusText(),headers:await(i?.allHeaders())||{},body:r,html:r,text:o||""};if(this.opts?.debug&&i){const t="function"==typeof i.request?i.request():i.request;if(t&&"function"==typeof t.timing){const e=t.timing();c.metadata={timings:{start:e.startTime,total:e.responseEnd-e.startTime,ttfb:e.responseStart-e.requestStart,dns:e.domainLookupEnd-e.domainLookupStart,tcp:e.connectEnd-e.connectStart,download:e.responseEnd-e.responseStart}}}}return!1!==this.opts?.output?.cookies&&(c.cookies=a),c}async _querySelectorAll(t,e){const i=Array.isArray(t)?t:[t],n=[];for(const t of i){const i=await t.locator(e).all();n.push(...i);try{await t.evaluate((t,e)=>t.matches(e),e)}catch(t){}}const s=[];for(const t of i){let i=!1;try{i=await t.evaluate((t,e)=>t.matches(e),e)}catch{}i&&s.push(t);const n=await t.locator(e).all();s.push(...n)}return s}async _nextSiblingsUntil(t,e){const i=await t.locator("xpath=following-sibling::*").all();if(!e)return i;const n=[];for(const t of i){if(await t.evaluate((t,e)=>t.matches(e),e))break;n.push(t)}return n}async _parentElement(t){const e=t.locator("xpath=..");return 0===await e.count()?null:e.first()}async _isSameElement(t,e){const i=await t.elementHandle(),n=await e.elementHandle();if(!i||!n)return!1;try{return await i.evaluate((t,e)=>t===e,n)}finally{await i.dispose(),await n.dispose()}}async _findClosestAncestor(t,e){if(0===e.length)return null;const i=await t.elementHandle();if(!i)return null;const n=await Promise.all(e.map(t=>t.elementHandle()));try{const t=await i.evaluate((t,e)=>{const i=new Set(e);let n=t;for(;n;){if(i.has(n))return e.indexOf(n);n=n.parentElement}return-1},n);return-1!==t?e[t]:null}finally{await i.dispose(),await Promise.all(n.map(t=>t?.dispose()))}}async _contains(t,e){const i=await t.elementHandle(),n=await e.elementHandle();if(!i||!n)return!1;try{return await i.evaluate((t,e)=>t.contains(e),n)}finally{await i.dispose(),await n.dispose()}}async _findCommonAncestor(t,e){const i=await t.elementHandle(),n=await e.elementHandle();if(!i||!n)return null;try{const e=await i.evaluateHandle((t,e)=>{let i=null;if(t===e)i=t;else if(t.contains(e))i=t;else if(e.contains(t))i=e;else{const n=new Set;let s=e.parentElement;for(;s;)n.add(s),s=s.parentElement;for(s=t.parentElement;s;){if(n.has(s)){i=s;break}s=s.parentElement}}return i&&1===i.nodeType?function t(e){if(e.id)return`//*[@id="${e.id}"]`;if(e===document.body)return"/html/body";if(e===document.documentElement)return"/html";let i=0;const n=e.parentNode?e.parentNode.childNodes:[];for(let s=0;s<n.length;s++){const r=n[s];if(r===e)return t(e.parentNode)+"/"+e.tagName.toLowerCase()+"["+(i+1)+"]";1===r.nodeType&&r.tagName===e.tagName&&i++}return""}(i):null},n);if(!e)return null;const s=await e.jsonValue();return"string"==typeof s&&s?t.page().locator(`xpath=${s}`):null}finally{await i.dispose(),await n.dispose()}}async _findContainerChild(t,e){const i=await t.elementHandle(),n=await e.elementHandle();if(!i||!n)return null;try{const e=await i.evaluateHandle((t,e)=>{let i=null;if(t===e)i=t;else{let n=t;for(;n;){if(n.parentElement===e){i=n;break}n=n.parentElement}}return i&&1===i.nodeType?function t(e){if(e.id)return`//*[@id="${e.id}"]`;if(e===document.body)return"/html/body";if(e===document.documentElement)return"/html";let i=0;const n=e.parentNode?e.parentNode.childNodes:[];for(let s=0;s<n.length;s++){const r=n[s];if(r===e)return t(e.parentNode)+"/"+e.tagName.toLowerCase()+"["+(i+1)+"]";1===r.nodeType&&r.tagName===e.tagName&&i++}return""}(i):null},n);if(!e)return null;const s=await e.jsonValue();return"string"==typeof s&&s?t.page().locator(`xpath=${s}`):null}finally{await i.dispose(),await n.dispose()}}async _extractValue(t,e){const{attribute:i,type:n="string",mode:s="text"}=t,r=await e.count();if(this._logDebug("extract",`_extractValue: count=${r} schema=${JSON.stringify(t)}`),0===r)return null;let o="";if(i?o=await e.getAttribute(i):"html"===n||"html"===s||"outerHTML"===s?(o="outerHTML"===s?await e.evaluate(t=>t.outerHTML):await e.innerHTML(),o&&(o=ot(o))):o="innerText"===s?await e.innerText():await e.textContent(),null===o)return null;switch(o=o.trim(),n){case"number":return parseFloat(o.replace(/[^0-9.-]+/g,""))||null;case"boolean":const t=o.toLowerCase();return"true"===t||"1"===t;default:return o}}_getInitialElementScope(t){const{page:e}=t;return e?e.locator(":root"):null}async _waitForNavigation(t,e,i){const{page:n}=t,s=this.opts?.timeoutMs||ht;try{await n.waitForURL(t=>t.href!==e,{waitUntil:"domcontentloaded",timeout:5e3}),this._logDebug(i,"URL changed to:",n.url())}catch(t){this._logDebug(i,"No URL change detected within 5s")}await n.waitForLoadState("networkidle",{timeout:s}),this.lastResponse=await this.buildResponse(t)}async executeAction(t,e){const{page:i}=t,n=this.opts?.timeoutMs||ht;switch(e.type){case"dispose":return;case"navigate":{this._logDebug("navigate",`Navigating to: ${e.url}`);const n=await i.goto(e.url,{waitUntil:e.opts?.waitUntil||"domcontentloaded",timeout:this.opts?.timeoutMs||ht});n&&(t={...t,response:n},this._logDebug("navigate",`Navigation status: ${n.status()} for ${n.url()}`));const s=await this.buildResponse(t);return this.lastResponse=s,s}case"click":{this._logDebug("click","Clicking selector:",e.selector);const s=i.url();return await i.click(e.selector,{timeout:n}),void await this._waitForNavigation(t,s,"click")}case"fill":await i.fill(e.selector,e.value,{timeout:n});const s=await this.buildResponse(t);return void(this.lastResponse=s);case"trim":{const n=this._getTrimInfo(e.options);return await i.evaluate(t=>{const{selectors:e,removeComments:i,removeHidden:n}=t;if(e.forEach(t=>{document.querySelectorAll(t).forEach(t=>t.remove())}),n){const t=[];document.querySelectorAll("*").forEach(e=>{const i=window.getComputedStyle(e);"none"!==i.display&&"hidden"!==i.visibility||t.push(e)}),t.forEach(t=>t.remove())}if(i){const t=document.createNodeIterator(document,NodeFilter.SHOW_COMMENT),e=[];let i;for(;i=t.nextNode();)e.push(i);e.forEach(t=>t.parentElement?.removeChild(t))}},n),void(this.lastResponse=await this.buildResponse(t))}case"waitFor":try{e.options?.selector&&await i.waitForSelector(e.options.selector,{timeout:n}),e.options?.networkIdle&&await i.waitForLoadState("networkidle",{timeout:n})}catch(t){if(!1!==e.options?.failOnTimeout)throw t}return void(e.options?.ms&&await i.waitForTimeout(e.options.ms));case"submit":{const n=e.selector||"form",s=i.locator(n).first();if(0===await s.count())throw new ut.NotFoundError(n,"submit");if("application/json"===(e.options?.enctype||"application/x-www-form-urlencoded")){const t=await s.elementHandle();if(!t)throw new ut.CommonError(`submit: could not get form handle for ${n}`,"submit");const e=await t.evaluate(async t=>{const e=new FormData(t),i={};e.forEach((t,e)=>{i[e]=t.toString()});const n=await fetch(t.action,{method:t.method,headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}),s=await n.text();return{status:n.status,statusText:n.statusText,headers:Object.fromEntries(n.headers.entries()),body:s,html:s,text:s,url:t.action,finalUrl:n.url}});return await t.dispose(),await i.setContent(e.html),void(this.lastResponse=e)}{this._logDebug("submit","Submitting form...");const e=i.url();return await s.evaluate(t=>t.submit()),void await this._waitForNavigation(t,e,"submit")}}case"evaluate":{const{fn:s,args:r=[]}=e.params,o=i.url();let a;if(a="function"==typeof s?await i.evaluate(s,r):await i.evaluate(([t,e])=>{const i=(0,eval)(`(${t})`);return"function"==typeof i?i(e):i},[s,r]),i.url()!==o)await i.waitForLoadState("domcontentloaded",{timeout:n}).catch(()=>{}),this.lastResponse=await this.buildResponse(t);else try{this.lastResponse=await this.buildResponse(t)}catch(e){await i.waitForLoadState("domcontentloaded",{timeout:n}).catch(()=>{}),this.lastResponse=await this.buildResponse(t)}return a}default:throw new ut.CommonError(`Unknown action type: ${e.type}`,"PlaywrightFetchEngine.executeAction",ut.ErrorCode.NotSupported)}}_createCrawler(t,e){return new ct.PlaywrightCrawler(t,e)}async _getSpecificCrawlerOptions(t){const e=t.browser?.headless??!0,i={maxRequestRetries:t.retries||3,headless:e,proxyConfiguration:this.proxyConfiguration,requestHandlerTimeoutSecs:t.requestHandlerTimeoutSecs,preNavigationHooks:[async({page:e,request:i},n)=>{n.throwHttpErrors=t.throwHttpErrors;const s=this.blockedTypes;s.size>0&&await e.route("**/*",t=>{s.has(t.request().resourceType())?t.abort():t.continue()})}]};if(this.opts?.antibot){i.browserPoolOptions={useFingerprints:!1};const{launchOptions:t}=await import("camoufox-js"),n=await t({headless:e});i.launchContext={launcher:lt.firefox,launchOptions:n},i.postNavigationHooks=[async({page:t,handleCloudflareChallenge:e})=>{await e()}]}return i}async goto(t,e){if(this.isPageActive)return this.dispatchAction({type:"navigate",url:t,opts:e});if(!this.requestQueue)throw new ut.CommonError("RequestQueue not initialized","goto");const i="req-"+ ++this.requestCounter,n=new Promise((t,e)=>{this.pendingRequests.set(i,{resolve:t,reject:e})});return await this.requestQueue.addRequest({url:t,headers:this.hdrs,userData:{requestId:i,waitUntil:e?.waitUntil||"domcontentloaded"},uniqueKey:`${t}-${i}`}),n}};ft.id="playwright",ft.mode="browser",G.register(ft);var dt=class extends d{async onExecute(t,e){const{selector:i,...n}=e?.params||{};if(!i)throw new Error("Selector is required for click action");await this.delegateToEngine(t,"click",i,n)}};dt.id="click",dt.returnType="none",dt.capabilities={http:"simulate",browser:"native"},d.register(dt);var wt=class extends d{async onExecute(t,e){const{selector:i,value:n,...s}=e?.params||{};if(!i)throw new Error("Selector is required for fill action");if(void 0===n)throw new Error("Value is required for fill action");await this.delegateToEngine(t,"fill",i,n,s)}};wt.id="fill",wt.returnType="none",wt.capabilities={http:"simulate",browser:"native"},d.register(wt);var pt=class extends d{async onExecute(t,e){return await this.delegateToEngine(t,"getContent",e?.params)}};pt.id="getContent",pt.returnType="response",pt.capabilities={http:"native",browser:"native"},d.register(pt);var yt=class extends d{async onExecute(t,e,i){const n=e?.params,s=n?.url||t.url;if(!s)throw new Error("URL is required for goto action");const r=t.internal.engine;if(!r)throw new Error("No engine available");t.url=s;return await r.goto(s,n)}};yt.id="goto",yt.returnType="response",yt.capabilities={http:"native",browser:"native"},d.register(yt);var mt=class extends d{async onExecute(t,e){const{selector:i,...n}=e?.params||{};await this.delegateToEngine(t,"submit",i,n)}};mt.id="submit",mt.returnType="none",mt.capabilities={http:"simulate",browser:"native"},d.register(mt);var gt=class extends d{async onExecute(t,e){const i=t.internal.engine;if(!i)throw new Error("No engine available");await i.waitFor(e?.params)}};gt.id="waitFor",gt.returnType="none",gt.capabilities={http:"native",browser:"native"},d.register(gt);var xt=class extends d{async onExecute(t,e){const i=e?.params;if(!i)throw new Error("Schema is required for extract action");return this.delegateToEngine(t,"extract",i)}};xt.id="extract",xt.returnType="any",xt.capabilities={http:"native",browser:"native"},d.register(xt);var bt=class extends d{async onExecute(t,e){const{selector:i,message:n,attribute:s}=e?.params||{},r=t.internal.engine;if("browser"===r?.mode){if(i){if(!await(r?.extract({selector:i,attribute:s})))return}r&&"pause"in r?await r.pause(n):console.warn("[PauseAction] was called, but the current engine does not support `pause`. Skipped.")}else console.warn("[PauseAction] can only run in browser engine. Skipped.")}};bt.id="pause",bt.capabilities={http:"native",browser:"native"},bt.returnType="none",d.register(bt);var vt=class extends d{async onExecute(t,e){const i=e?.params||{};await this.delegateToEngine(t,"trim",i)}};vt.id="trim",vt.returnType="none",vt.capabilities={http:"simulate",browser:"native"},d.register(vt);var $t=class extends d{async onExecute(t,e){const i=e?.params;if(!i)throw new Error("evaluate action: params is required");return await this.delegateToEngine(t,"evaluate",i)}};async function _t(t,e){return(new J).fetch(t,e)}$t.id="evaluate",$t.returnType="any",$t.capabilities={http:"simulate",browser:"native"},d.register($t);
1
+ "use strict";var t,e=Object.create,i=Object.defineProperty,s=Object.getOwnPropertyDescriptor,n=Object.getOwnPropertyNames,r=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,a=(t,e,r,a)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let c of n(e))o.call(t,c)||c===r||i(t,c,{get:()=>e[c],enumerable:!(a=s(e,c))||a.enumerable});return t},c={};((t,e)=>{for(var s in e)i(t,s,{get:e[s],enumerable:!0})})(c,{CheerioFetchEngine:()=>ct,ClickAction:()=>wt,DefaultFetcherProperties:()=>l,EvaluateAction:()=>_t,ExtractAction:()=>bt,FetchAction:()=>d,FetchActionResultStatus:()=>h,FetchEngine:()=>W,FetchSession:()=>G,FetcherOptionKeys:()=>u,FillAction:()=>yt,GetContentAction:()=>pt,GotoAction:()=>mt,KeyboardPressAction:()=>At,KeyboardTypeAction:()=>Ct,MouseClickAction:()=>kt,MouseMoveAction:()=>qt,MouseWheelAction:()=>St,PauseAction:()=>vt,PlaywrightFetchEngine:()=>dt,ScrollIntoViewAction:()=>Et,SubmitAction:()=>gt,TRIM_PRESETS:()=>B,TrimAction:()=>$t,WaitForAction:()=>xt,WebFetcher:()=>K,fetchWeb:()=>Mt,getRandomDelay:()=>z}),module.exports=(t=c,a(i({},"__esModule",{value:!0}),t));var l={engine:"auto",enableSmart:!0,useSiteRegistry:!0,antibot:!1,debug:!1,headers:{},cookies:[],throwHttpErrors:void 0,output:{cookies:!0,sessionState:!0},proxy:[],blockResources:[],storage:{purge:!0},ignoreSslErrors:!0,browser:{engine:"playwright",headless:!0,waitUntil:"domcontentloaded"},http:{method:"GET"},timeoutMs:6e4,requestHandlerTimeoutSecs:void 0,maxConcurrency:1,maxRequestsPerMinute:1e3,delayBetweenRequestsMs:0,retries:0,sites:[]},u=Object.keys(l).concat(["actions","onPause"]),h=(t=>(t[t.Failed=0]="Failed",t[t.Success=1]="Success",t[t.Skipped=2]="Skipped",t))(h||{}),f=class t{static register(t){const e=t.id;if(!e)throw new Error("FetchAction.register: actionClass.id is required");this.registry.set(e,t)}static get(t){return this.registry.get(t)}static create(e){const i="string"==typeof e?e:e.id||e.name||e.action;if(!i)throw new Error("Action must have id, name or action");const s=i instanceof t?i.constructor:this.registry.get(i);return s?new s:void 0}static has(t){return this.registry.has(t)}static list(){return Array.from(this.registry.keys())}static getCapability(t){return this.capabilities[t]??"noop"}getCapability(t){return this.constructor.getCapability(t)}get id(){return this.constructor.id}get returnType(){return this.constructor.returnType}get capabilities(){return this.constructor.capabilities}async delegateToEngine(t,e,...i){const s=t.internal.engine;if(!s)throw new Error("No engine available");if("function"!=typeof s[e])throw new Error(`Engine does not have a method named '${String(e)}'`);return await s[e](...i)}installCollectors(e,i){const s=i?.collectors;if(!s?.length)return;const n=[],r=new Set;for(const i of s){const s=w(i.activateOn),o=w(i.collectOn),a=w(i.deactivateOn),c=!(i.background??!0),l=t.create(i);if(!l)continue;let u=!1,h=!1,f=0;const d=async t=>{if(!u&&!h){u=!0;try{await(l.onBeforeExec?.(e,i))}catch(t){e.eventBus.emit("collector:error",{action:this.id,collector:l.id,phase:"before",error:t})}}},m=async(t,s)=>{if(!h){u||await d(s);try{const n=Promise.resolve(l.onExecute?.(e,i,s)).then(s=>{var n,r;if(i.storeAs){((n=e.outputs)[r=i.storeAs]||(n[r]=[])).push(s)}return e.eventBus.emit("collector:result",{action:this.id,collector:i.id||i.name,event:t,result:s}),s}).catch(s=>{e.eventBus.emit("collector:error",{action:this.id,collector:i.id||i.name,event:t,phase:"exec",error:s})}).finally(()=>{f++});c&&(r.add(n),n.finally(()=>r.delete(n)))}catch(i){e.eventBus.emit("collector:error",{action:this.id,collector:l.id,event:t,phase:"exec",error:i})}}},g=async()=>{if(!h){0===f&&m("collector:after"),h=!0;try{await(l.onAfterExec?.(e,i))}catch(t){e.eventBus.emit("collector:error",{action:this.id,collector:i.id||i.name,phase:"after",error:t})}finally{e.eventBus.emit("collector:end",{action:this.id,collector:i.id||i.name}),b.forEach(t=>t())}}},x=y(e,s,d),b=p(e,o,m),v=y(e,a,g);if(n.push(...x,...b,...v),!s.length&&!o.length&&!a.length){const t=()=>{g()};e.eventBus.once(`action:${this.id}.end`,t),n.push(()=>e.eventBus.off("fetcher:action:end",t))}}return n.length||r.size>0?{cleanup:()=>n.forEach(t=>t()),awaitExecPendings:async()=>{r.size>0&&await Promise.allSettled(Array.from(r))}}:void 0}async beforeExec(t,e){t.internal.actionStack||(t.internal.actionStack=[]);const i=t.internal.actionStack,s=i.length,n=i.length>0?i[i.length-1].id:void 0,r={...e,id:this.id,depth:s,parent:n};i.push(r),t.currentAction=r;const o={action:this,context:t,options:e,index:e?.index,depth:s,stack:[...i]};t.eventBus.emit(`action:${this.id}.start`,o),t.eventBus.emit("action:start",o),await(this.onBeforeExec?.(t,e));return{entry:o,collectors:this.installCollectors(t,e)}}async afterExec(t,e,i,s){const n=t.internal.actionStack,r=n.length-1,o=s?.collectors;try{if(await(o?.awaitExecPendings()),t.lastResult=i,"response"!==i?.returnType||i.error||(t.lastResponse=i.result),e?.storeAs){const s=t.outputs[e.storeAs],n=i?.result;"object"!=typeof s||null===s||"object"!=typeof n||null===n||Array.isArray(s)||Array.isArray(n)?t.outputs[e.storeAs]=n:t.outputs[e.storeAs]={...s,...n}}i?.error&&(t.currentAction.error=i.error),await(this.onAfterExec?.(t,e));const s={action:this,context:t,options:e,result:i,depth:r,stack:[...n]};i?.error&&(s.error=i.error);try{t.eventBus.emit(`action:${this.id}.end`,s)}catch(t){}try{t.eventBus.emit("action:end",s)}catch(t){}}finally{try{o?.cleanup()}finally{n.pop();const e=n.length;t.currentAction=e>0?n[e-1]:void 0}}}async execute(t,e){e?.args&&!e.params&&(e.params=e.args);const i=await this.beforeExec(t,e),s=e?.failOnError??!0;let n;try{return t.throwHttpErrors=s,n=await this.onExecute(t,e),n&&n.returnType||(n={status:1,returnType:this.returnType??"any",result:n}),n}catch(e){if(n={status:0,error:e,meta:{id:this.id,engineType:t.engine,capability:this.getCapability(t.engine)}},s)throw e;return n}finally{await this.afterExec(t,e,n,i)}}};f.registry=new Map,f.returnType="any",f.capabilities={http:"noop",browser:"noop"};var d=f;function w(t){return t?Array.isArray(t)?t:[t]:[]}function y(t,e,i){const s=[];for(const n of e)if("string"==typeof n||n instanceof RegExp){const e=(...t)=>{i(t[0])};t.eventBus.once(n,e),s.push(()=>t.eventBus.off(n,e))}return s}function p(t,e,i){const s=[];for(const n of e)if("string"==typeof n||n instanceof RegExp){const e=t=>i(n,t);t.eventBus.on(n,e),s.push(()=>t.eventBus.off(n,e))}return s}var m=require("events-ex");var g=require("lodash-es"),x=(0,require("nanoid").customAlphabet)("0123456789abcdefghijklmnopqrstuvwxyz",12);function b(t,e,...i){if(!t)return;const{prefix:s,id:n,category:r}=e;if(!0===t||t===r||Array.isArray(t)&&t.includes(r)){const t=n?`:${n}`:"";console.log(`[${s}${t}:${r}]`,...i)}}var v=require("lodash-es"),$=require("events-ex"),_=require("@isdk/common-error"),q=require("crawlee"),k=require("@isdk/common-error"),E=new Set(["string","number","boolean","html","object","array"]),S=new Set(["selector","has","exclude","required","strict","relativeTo","order","anchor","depth"]);function C(t){if("string"==typeof t)return{type:"string",selector:t,mode:"text"};if(!t||"object"!=typeof t)return{type:"string",mode:"text"};let e={...t};if(function(t){if(!t||"object"!=typeof t)return!1;if(Array.isArray(t))return!1;if("type"in t)return"string"!=typeof t.type||!E.has(t.type);const e=Object.keys(t);if(0===e.length)return!1;for(const t of e)if(!["selector","attribute","has","exclude","mode","required","strict","relativeTo","order","anchor","depth"].includes(t))return!0;return!1}(e)){const t={};for(const i of Object.keys(e))S.has(i)||(t[i]=C(e[i]),delete e[i]);e.type="object",e.properties=t}if(e.type||(e.type="string"),"object"===e.type){const t=e;t.properties||(t.properties={});for(const e in t.properties)t.properties[e]=C(t.properties[e]);delete t.mode,delete t.items,delete t.attribute}else if("array"===e.type){const t=e;t.attribute&&!t.items&&(t.items={type:"string",attribute:t.attribute,mode:"text"},delete t.attribute),t.items||(t.items={type:"string",mode:"text"}),t.items=C(t.items),"string"==typeof t.mode&&(t.mode={type:t.mode})}else{const t=e;t.mode||("html"===t.type?t.mode="html":t.mode="text")}if(e.selector&&(e.has||e.exclude)){const{selector:t,has:i,exclude:s}=e,n=t.split(",").map(t=>t.trim()).map(t=>{let e=t;return i&&(e+=`:has(${i})`),s&&(e+=`:not(${s})`),e});e.selector=n.join(", "),delete e.has,delete e.exclude}return e}async function A(t,e,i){const s=C(t);return M.call(this,s,e,i)}async function M(t,e,i){const s=t.type,n=t.selector,r=t.strict??i;if(!e)return this._logDebug("extract",`_extract: No scope for selector "${n||""}", type "${s||"value"}"`),"array"===s?[]:null;switch(s){case"object":return R.call(this,t,e,r);case"array":return j.call(this,t,e,r);default:return P.call(this,t,e,r)}}async function R(t,e,i){const{selector:s,properties:n,strict:r}=t,o=r??i,a=t._skipSelector;let c=e;if(s&&!a){const t=await this._querySelectorAll(e,s);c=t.length>0?t[0]:null,this._logDebug("extract",`_extractObject: selector "${s}" found ${t.length} elements`)}if(!c){if(this._logDebug("extract",`_extractObject: scope not found for selector "${s||""}"`),o&&t.required)throw new k.CommonError(`Required object "${s||""}" is missing.`,"extract");return null}let l=t.depth??0;const u=l;for(;;){const{result:i,hasValue:r,missingRequired:h}=await T.call(this,t,c,o);if(0===h.length)return!s&&!r&&Object.keys(n).length>0?(this._logDebug("extract","_extractObject result: null"),null):(this._logDebug("extract","_extractObject result:",i),i);let f=!1;if(l>0)if(a)f=!0;else{const t=await this._isSameElement(c,e),i=await this._contains(e,c);f=!t&&i}if(f){const t=await this._parentElement(c);if(t){let i=!0;if(a||(i=await this._isSameElement(e,t)||await this._contains(e,t)),i){this._logDebug("extract",`_extractObject: missing required fields [${h.join(", ")}], bubbling up from depth ${u-l} to ${u-l+1}`),c=t,l--;continue}}}if(o)throw new k.CommonError(`Required property "${h[0]}" is missing.`,"extract");return null}}async function T(t,e,i){const{properties:s,relativeTo:n,order:r}=t,o={},a=[];let c=!1;const l=r||Object.keys(s);let u=e;const h=new Map,f="previous"===n;for(const t of l){const n=s[t];if(!n)continue;this._logDebug("extract",`_extractObject: extracting property "${t}"`);let r,l=u;if(n.anchor){const r=await I.call(this,n.anchor,s,h,e,f,n.depth);if(!r){if(i)throw new k.CommonError(`Anchor "${n.anchor}" not found or out of scope.`,"extract");o[t]=null,n.required&&a.push(t);continue}l=r.scopeForField,f&&(u=l)}let d=null;const w=n.selector,y="array"===n.type;if(w){let t=await this._querySelectorAll(l,w);if(t.length>0){void 0!==n.depth&&"object"!==n.type&&(t=await Promise.all(t.map(t=>H.call(this,t,l,n.depth)))),d=t[0];const e={...n,_skipSelector:!0};if(r=await M.call(this,e,y?t:d,i),f&&!n.anchor){const e=y&&Array.isArray(r)?t[t.length-1]:d;u=await L.call(this,e,u)}y&&(d=t[t.length-1])}else r=null}else r=await M.call(this,n,l,i),null!==r&&(d=Array.isArray(l)?l[0]:l);d&&h.set(t,d),null===r&&n.required&&a.push(t),null!==r&&(c=!0),o[t]=r}return{result:o,hasValue:c,missingRequired:a}}async function j(t,e,i){const{selector:s,items:n,mode:r,strict:o}=t,a=o??i,c=t._skipSelector;let l=s&&!c?await this._querySelectorAll(e,s):Array.isArray(e)?e:[e];s&&!c&&void 0!==t.depth&&(l=await Promise.all(l.map(i=>H.call(this,i,e,t.depth)))),this._logDebug("extract",`_extractArray: selector "${s||""}" found ${l.length} elements`);const u=O.call(this,r);if(void 0!==a&&void 0===u.strict&&(u.strict=a),(!r||"columnar"===u.type)&&1===l.length&&n){this._logDebug("extract","_extractArray: trying columnar extraction");const t=await U.call(this,n,l[0],u);if(t)return t}if("segmented"===u.type&&n){this._logDebug("extract",`_extractArray: trying segmented extraction for ${l.length} containers`);const t=[];let e=!1;for(const i of l){const s=await N.call(this,n,i,u);s&&(e=!0,t.push(...s))}if(e)return t}return this._logDebug("extract",`_extractArray: using nested extraction for ${l.length} elements`),F.call(this,n,l,{strict:u.strict})}async function P(t,e,i){const{selector:s}=t,n=t._skipSelector,r=t.strict??i;let o=e;if(s&&!n){const i=await this._querySelectorAll(e,s);o=i.length>0?i[0]:null,o&&void 0!==t.depth&&(o=await H.call(this,o,e,t.depth)),this._logDebug("extract",`_extractValue: selector "${s}" found ${i.length} elements`)}else Array.isArray(e)&&(o=e.length>0?e[0]:null);if(!o){if(this._logDebug("extract",`_extractValue: element not found for selector "${s||""}"`),r&&t.required)throw new k.CommonError(`Required value "${s||""}" is missing.`,"extract");return null}const a=await this._extractValue(t,o);return this._logDebug("extract",`_extractValue: extracted for selector "${s||""}":`,a),a}function O(t){return t?"string"==typeof t?{type:t}:t:{type:"nested"}}async function F(t,e,i){const s=[],n=t.required,r=!0===i?.strict,o="object"===t.type||"array"===t.type;for(const i of e){const e=await M.call(this,t,i,r);if(null!==e)s.push(e);else{if(n&&r)throw new k.CommonError("Required item is missing in array.","extract");n||o||s.push(null)}}return s}async function U(t,e,i){const s="object"===t.type,n=!0===i?.strict,r=!0===i?.inference;if(s){const i=t.properties,s=Object.keys(i);if(0===s.length)return null;const o={},a={};let c=null,l=0,u=[];for(const t of s){const s=i[t];if("array"===s.type||"object"===s.type)return this._logDebug("extract",`_extractColumnar: field "${t}" has nested structure, columnar not supported`),null;const h=s;let f=[];f=h.selector?await this._querySelectorAll(e,h.selector):[e],a[t]=f;const d=f.length;if(this._logDebug("extract",`_extractColumnar: field "${t}" with selector "${h.selector||""}" found ${d} matches`),d>l&&(l=d,u=f),h.selector)if(null===c)c=d,this._logDebug("extract",`_extractColumnar: set commonCount to ${c}`);else if(c!==d)if(this._logDebug("extract",`_extractColumnar: count mismatch for field "${t}": ${d} vs ${c}`),r&&l>1)c=-1,this._logDebug("extract","_extractColumnar: mismatch marked for inference");else if(n){if(!(1===d&&await this._isSameElement(f[0],e))){if(s.required&&d<c)throw new k.CommonError(`Required field "${t}" is missing at index ${d}.`,"extract");throw new k.CommonError(`Columnar extraction mismatch: field "${t}" has ${d} matches, but expected ${c}.`,"extract")}}const w=await Promise.all(f.map(t=>this._extractValue(h,t)));this._logDebug("extract",`_extractColumnar: field "${t}" values:`,w),o[t]=w}if(r&&-1===c&&l>1&&u.length>0){const i=[];for(const t of u){const s=await this._findContainerChild(t,e);s&&i.push(s)}const s=[];for(const t of i){await this._findClosestAncestor(t,s)||s.push(t)}if(s.length>1)return F.call(this,t,s,{strict:n})}if(l<=1)return null;if(-1===c&&n)return null;const h=n&&-1!==c?c:l,f={};if(h>1)for(const t of s){if(1===o[t].length){(!i[t].selector||await this._isSameElement(a[t][0],e))&&(f[t]=!0)}}const d=[];for(let t=0;t<h;t++){const e={};let r=!1;for(const a of s){const s=o[a],c=i[a];let l=s[t];if(f[a]&&(l=s[0]),void 0===l&&(l=null),null===l&&c.required){if(this._logDebug("extract",`_extractColumnar: skipping row ${t} because required field "${a}" is null`),n)throw new k.CommonError(`Required field "${a}" is missing at index ${t}.`,"extract");r=!0;break}e[a]=l}r||d.push(e)}return d}{const i=t;if(!i.selector)return null;const s=await this._querySelectorAll(e,i.selector);if(s.length<=1)return null;const n=await Promise.all(s.map(t=>this._extractValue(i,t)));return i.required?n.filter(t=>null!==t):n}}async function N(t,e,i){if(!("object"===t.type))return null;const s=t.properties,n=Object.keys(s);if(0===n.length)return null;let r;if(i?.anchor)r=s[i.anchor]?.selector||i.anchor;else for(const t of n)if(s[t].selector){r=s[t].selector;break}if(!r)return this._logDebug("extract","_extractSegmented: no anchor selector found, falling back to nested"),null;const o=await this._querySelectorAll(e,r);if(this._logDebug("extract",`_extractSegmented: anchor selector "${r}" found ${o.length} elements`),0===o.length){if(i?.strict)throw new k.CommonError(`Segmented extraction failed: no elements found for anchor selector "${r}".`,"extract");return[]}const a=[];for(let s=0;s<o.length;s++){const n=o[s],c=s>0?o[s-1]:null,l=s<o.length-1?o[s+1]:null;let u,h=n,f=null;if(c&&(f=await this._findCommonAncestor(n,c)),!f&&l)f=await this._findCommonAncestor(n,l);else if(f&&l){const t=await this._findCommonAncestor(n,l);t&&await this._contains(f,t)&&(f=t)}if(f){const t=await H.call(this,n,f,i?.depth);t&&!await this._isSameElement(t,n)&&(h=t)}else{const t=await H.call(this,n,e,i?.depth);t&&(h=t)}if(await this._isSameElement(h,n)){u=[n,...await this._nextSiblingsUntil(n,r)],this._logDebug("extract",`_extractSegmented: segment ${s} (flat) created with ${u.length} elements`)}else u=h,this._logDebug("extract",`_extractSegmented: segment ${s} (nested) identified as container element`);const d={...t};i?.relativeTo&&!d.relativeTo&&(d.relativeTo=i.relativeTo);const w=await M.call(this,d,u,i?.strict),y=t.required,p="object"===t.type||"array"===t.type;if(null!==w)a.push(w);else{if(y&&i?.strict)throw new k.CommonError("Required item is missing in array.","extract");y||p||a.push(null)}}return a}async function H(t,e,i){const s=Array.isArray(e),n=s?e:[e],r=s?await this._findClosestAncestor(t,n):await this._findContainerChild(t,e);if(void 0===i||!r)return r;let o=t;for(let t=0;t<i&&!await this._isSameElement(o,r);t++){const t=await this._parentElement(o);if(!t||!await this._contains(r,t))break;o=t}return o}async function I(t,e,i,s,n,r){let o=null;if(e.hasOwnProperty(t))o=i.get(t)||null;else{const e=await this._querySelectorAll(s,t);e.length>0&&(o=e[0])}if(o){const t=[];let e=o,i=0;const n=void 0!==r?r:1e3;for(;e&&i<=n;){const n=await this._nextSiblingsUntil(e);t.push(...n);const r=await this._parentElement(e);if(!r)break;if(Array.isArray(s)?null!==await this._findClosestAncestor(r,s):await this._isSameElement(r,s))break;e=r,i++}if(t.length>0||void 0!==r)return{scopeForField:t}}return null}async function L(t,e){const i=await H.call(this,t,e);if(i){if(!Array.isArray(e))return this._nextSiblingsUntil(i);{let t=e.indexOf(i);if(-1===t)for(let s=0;s<e.length;s++)if(await this._isSameElement(e[s],i)){t=s;break}if(-1!==t)return e.slice(t+1)}}return Array.isArray(e)?e:[e]}function V(){let t=()=>{};const e=new Promise(e=>{t=e});return e.release=t,e}q.Configuration.getGlobalConfig().set("persistStorage",!1);var B={scripts:["script"],styles:["style",'link[rel="stylesheet"]'],svgs:["svg"],images:["img","picture","canvas"],hidden:["[hidden]",'[style*="display:none"]','[style*="display: none"]']},W=class{constructor(){this.hdrs={},this._initializedSessions=new Set,this.pendingRequests=new Map,this.requestCounter=0,this.actionEmitter=new $.EventEmitter,this.isPageActive=!1,this.isEngineDisposed=!1,this.navigationLock=function(){const t=V();return t.release(),t}(),this.isExecutingAction=!1,this.actionQueue=[],this.isProcessingActionLoop=!1,this.blockedTypes=new Set}static register(t){const e=t.id;if(!e)throw new Error("Engine must define static id");if(this.registry.has(e))throw new Error(`Engine id duplicated: ${e}`);this.registry.set(e,t)}static get(t){return this.registry.get(t)}static getByMode(t){for(const[e,i]of this.registry.entries())if(i.mode===t)return i}static async create(t,e){const i=(0,v.defaultsDeep)(e,t,l),s=i.engine??t.engine,n=s?this.get(s)??this.getByMode(s):null;if(n){const e=new n;return await e.initialize(t,i),e}}_logDebug(t,...e){b(this.opts?.debug,{prefix:"FetchEngine",id:this.id,category:t},...e)}_getTrimInfo(t){let{selectors:e=[],presets:i=[]}=t;"string"==typeof e&&(e=[e]),"string"==typeof i&&(i=[i]);const s=i.includes("all"),n=[...e];for(const[t,e]of Object.entries(B))(s||i.includes(t))&&n.push(...e);return{selectors:n,removeComments:s||i.includes("comments"),removeHidden:s||i.includes("hidden")}}async _extract(t,e,i){return M.call(this,t,e,i)}_normalizeArrayMode(t){return O.call(this,t)}async _extractNested(t,e,i){return F.call(this,t,e,i)}async _extractColumnar(t,e,i){return U.call(this,t,e,i)}async _extractSegmented(t,e,i){return N.call(this,t,e,i)}async buildResponse(t){const e=await this._buildResponse(t),i=e.headers["content-type"]||"";return e.contentType=i.split(";")[0].trim(),!1!==this.opts?.output?.cookies?!e.cookies&&t.session&&(e.cookies=t.session.getCookies(t.request.url)):delete e.cookies,!1!==this.opts?.output?.sessionState?this.crawler?.sessionPool&&(e.sessionState=await this.crawler.sessionPool.getState()):delete e.sessionState,this.opts?.debug&&(e.metadata={...e.metadata,mode:this.mode,engine:this.id,proxy:t.proxyInfo?.url||("string"==typeof this.opts.proxy?this.opts.proxy:Array.isArray(this.opts.proxy)?this.opts.proxy[0]:void 0)}),e}waitFor(t){return this.dispatchAction({type:"waitFor",options:t})}click(t){return this.dispatchAction({type:"click",selector:t})}mouseMove(t){return this.dispatchAction({type:"mouseMove",params:t})}mouseClick(t){return this.dispatchAction({type:"mouseClick",params:t})}mouseWheel(t){return this.dispatchAction({type:"mouseWheel",params:t})}scrollIntoView(t){return this.dispatchAction({type:"scrollIntoView",params:t})}keyboardType(t,e){return this.dispatchAction({type:"keyboardType",params:{text:t,delay:e}})}keyboardPress(t,e){return this.dispatchAction({type:"keyboardPress",params:{key:t,delay:e}})}fill(t,e){return this.dispatchAction({type:"fill",selector:t,value:e})}submit(t,e){return this.dispatchAction({type:"submit",selector:t,options:e})}trim(t){return this.dispatchAction({type:"trim",options:t})}pause(t){return this.dispatchAction({type:"pause",message:t})}evaluate(t){return this.dispatchAction({type:"evaluate",params:t})}extract(t){t&&"object"==typeof t&&t.schema&&(t=t.schema);const e=C(t);return this.dispatchAction({type:"extract",schema:e})}get id(){return this.constructor.id}async getState(){return{cookies:await this.cookies(),sessionState:await(this.crawler?.sessionPool?.getState())}}get mode(){return this.constructor.mode}get context(){return this.ctx}async initialize(t,e){if(this.ctx)return;(0,v.merge)(t,e),this.ctx=t,this.opts=t,this.hdrs=function(t){const e={};if(t&&"object"==typeof t)for(const[i,s]of Object.entries(t))e[i.toLowerCase()]=s;return e}(t.headers),this._initialCookies=[...t.cookies??[]],t.internal||(t.internal={}),t.internal.engine=this,t.engine=this.mode,this.actionEmitter.setMaxListeners(100);const i=t.storage||{},s=i.persist??!1,n=this.config=new q.Configuration({persistStorage:s,storageClientOptions:{persistStorage:s,...i.config},...i.config}),r=i.id||t.id;this.requestQueue=await q.RequestQueue.open(r,{config:n});const o=this.opts?.proxy?"string"==typeof this.opts.proxy?[this.opts.proxy]:this.opts.proxy:void 0;o?.length&&(this.proxyConfiguration=new q.ProxyConfiguration({proxyUrls:o}));const a=await this._getSpecificCrawlerOptions(t),c=(0,v.defaultsDeep)({persistenceOptions:{enable:!0,storeId:r},persistStateKeyValueStoreId:r},t.sessionPoolOptions,{maxPoolSize:1,sessionOptions:{maxUsageCount:1e3,maxErrorScore:3}});t.sessionState&&t.cookies&&t.cookies.length>0&&console.warn('[FetchEngine] Warning: Both "sessionState" and "cookies" are provided. Explicit "cookies" will override any conflicting cookies restored from "sessionState".');const l={...(0,v.defaultsDeep)(a,{requestQueue:this.requestQueue,maxConcurrency:1,minConcurrency:1,useSessionPool:!0,persistCookiesPerSession:!0,sessionPoolOptions:c}),requestHandler:this._requestHandler.bind(this),errorHandler:this._failedRequestHandler.bind(this),failedRequestHandler:this._failedRequestHandler.bind(this)};l.preNavigationHooks||(l.preNavigationHooks=[]),l.preNavigationHooks.unshift(({crawler:t,session:e,request:i},s)=>{if(this.currentSession=e,e&&!this._initializedSessions.has(e.id)){if(this._initialCookies&&this._initialCookies.length>0){const t=this._initialCookies.map(t=>{const e={...t};return"no_restriction"===e.sameSite&&(e.sameSite="None"),e});e.setCookies(t,i.url)}this._initializedSessions.add(e.id)}});const u=this.crawler=this._createCrawler(l,n),h=this.kvStore=await q.KeyValueStore.open(r,{config:n}),f=await h.getValue(q.PERSIST_STATE_KEY);!t.sessionState||f&&!t.overrideSessionState||await h.setValue(q.PERSIST_STATE_KEY,t.sessionState),this.isCrawlerReady=!0,this.crawlerRunPromise=u.run(),this.crawlerRunPromise.finally(()=>{this.isCrawlerReady=!1}).catch(t=>{console.error("Crawler background error:",t)})}async cleanup(){await(this._cleanup?.()),await this._commonCleanup();const t=this.ctx;t&&t.internal?.engine===this&&(t.internal.engine=void 0),this.ctx=void 0,this.opts=void 0}async _processAction(t,e){switch(this._logDebug(e.type,"Executing action:",e),e.type){case"extract":return A.call(this,e.schema,this._getInitialElementScope(t));case"pause":return this._handlePause(e);case"getContent":return this.buildResponse(t);case"waitFor":return e.options?.ms&&1===Object.keys(e.options).length?void await new Promise(t=>setTimeout(t,e.options.ms)):this.executeAction(t,e);default:return this.executeAction(t,e)}}async _handlePause(t){const e=this.ctx?.onPause;e?(console.info(t.message||"Execution paused for manual intervention."),await e({message:t.message}),console.info("Resuming execution...")):console.warn("[PauseAction] was called, but no `onPause` handler was provided in fetchWeb options. Skipped.")}async _executePendingActions(t){if(this.isEngineDisposed)return;this.activeContext=t;const e=async()=>{if(!this.isProcessingActionLoop){this.isProcessingActionLoop=!0,this._logDebug("action-loop",`Action loop started. Current queue size: ${this.actionQueue.length}`);try{for(;this.actionQueue.length>0&&this.isPageActive&&!this.isEngineDisposed;){const e=this.actionQueue.shift();this._logDebug("action-loop",`Processing action: ${e.action.type}`,e.action);try{if("dispose"===e.action.type){this.actionEmitter.emit("dispose"),e.resolve();continue}this.isExecutingAction=!0;const i=await this._processAction(t,e.action);this._logDebug("action-loop",`Action completed: ${e.action.type}`),e.resolve(i)}catch(t){this._logDebug("action-loop",`Action failed: ${e.action.type}`,t),e.reject(t)}finally{this.isExecutingAction=!1,await new Promise(t=>setImmediate(t))}}}finally{this.isProcessingActionLoop=!1,this._logDebug("action-loop","Action loop paused/finished.")}}};await new Promise(t=>{const i=t=>{this.actionQueue.push(t),e()},s=()=>{this.actionEmitter.removeListener("dispatch",i),this.activeContext=void 0,t()};this.actionEmitter.on("dispatch",i),this.actionEmitter.once("dispose",s),e(),this.isEngineDisposed&&(s(),this.actionEmitter.removeListener("dispose",s))})}async _sharedRequestHandler(t){const{request:e}=t;this._logDebug("request",`Processing request: ${e.url}`);try{this.currentSession=t.session,this.isPageActive=!0;const i=this.pendingRequests.get(e.userData.requestId);if(i){const s=await this.buildResponse(t),n=!s.statusCode||s.statusCode>=400;if(this.ctx?.throwHttpErrors&&n){const t=new _.CommonError(`Request for ${s.finalUrl} failed with status ${s.statusCode||"N/A"}`,"request",s.statusCode);i.reject(t)}else this.lastResponse=s,i.resolve(s);this.pendingRequests.delete(e.userData.requestId)}await this._executePendingActions(t)}finally{if(this.currentSession){const t=this.currentSession.getCookies(e.url);t&&(this._initialCookies=t)}this.isPageActive=!1,this.navigationLock.release()}}async _sharedFailedRequestHandler(t,e){const{request:i}=t,s=this.pendingRequests.get(i.userData.requestId);if(s&&e&&this.ctx?.throwHttpErrors){this.pendingRequests.delete(i.userData.requestId);const t=e.response,n=t?.statusCode||(e.message.includes("timed out")?_.ErrorCode.RequestTimeout:_.ErrorCode.InternalError),r=t?.url?t.url:i.url,o=n===_.ErrorCode.RequestTimeout?r+" "+e.message:`Request${r?" for "+r:""} failed: ${e.message}`,a=new _.CommonError(o,"request",n);s.reject(a)}return this._sharedRequestHandler(t)}async dispatchAction(t){if(!this.isPageActive)throw new Error("No active page. Call goto() before performing actions.");return this.isExecutingAction&&this.activeContext?(this._logDebug(t.type,"Re-entrant action execution:",t),await this._processAction(this.activeContext,t)):new Promise((e,i)=>{this.actionEmitter.emit("dispatch",{action:t,resolve:e,reject:i})})}async _requestHandler(t){await this._sharedRequestHandler(t)}async _failedRequestHandler(t,e){await this._sharedFailedRequestHandler(t,e)}async _commonCleanup(){if(this.isEngineDisposed=!0,this._initializedSessions.clear(),this.actionEmitter.emit("dispose"),this.navigationLock?.release(),this.pendingRequests.size>0){for(const[,t]of this.pendingRequests)t.reject(new Error("Cleanup:Request cancelled"));this.pendingRequests.clear()}if(this.crawler){try{await(this.crawler.teardown?.())}catch(t){console.error("crawler teardown error:",t)}this.crawler=void 0}this.crawlerRunPromise=void 0,this.isCrawlerReady=void 0;const t=(this.opts?.storage||{}).purge??!0;this.requestQueue&&(t&&await this.requestQueue.drop().catch(t=>console.error("Error dropping requestQueue:",t)),this.requestQueue=void 0),this.kvStore&&(t&&await this.kvStore.drop().catch(t=>console.error("Error dropping kvStore:",t)),this.kvStore=void 0),this.actionEmitter.removeAllListeners(),this.pendingRequests.clear(),this.actionQueue=[],this.config=void 0}async blockResources(t,e){return e&&this.blockedTypes.clear(),t.forEach(t=>this.blockedTypes.add(t)),t.length}getContent(){return this.lastResponse?Promise.resolve(this.lastResponse):Promise.reject(new Error("No content fetched yet. Call goto() first."))}async headers(t,e){if(void 0===t)return{...this.hdrs};if("string"==typeof t&&void 0===e)return this.hdrs[t.toLowerCase()]||"";if(null!==t&&"object"==typeof t){const i={};for(const[e,s]of Object.entries(t))i[e.toLowerCase()]=String(s);return this.hdrs=!0===e?i:{...this.hdrs,...i},!0}return"string"==typeof t&&("string"==typeof e?this.hdrs[t.toLowerCase()]=e:null===e&&delete this.hdrs[t.toLowerCase()],!0)}async cookies(t){const e=this.lastResponse?.url||"";if(Array.isArray(t))return this.currentSession?this.currentSession.setCookies(t,e):this._initialCookies=[...t],!0;if(null===t)return this.currentSession,this._initialCookies=[],!0;if(this.currentSession){return this.currentSession.getCookies(e)}return[...this._initialCookies||[]]}async dispose(){await this.cleanup()}};function z(t,e=.3){const i=t*(1-e),s=t*(1+e);return Math.floor(Math.random()*(s-i+1)+i)}async function D(t,e){let i;const s=e?.engine||t.engine;if(s&&"auto"!==s){if(i=await W.create(t,{engine:s}),!i)throw new Error(`Engine "${s}" is not available or failed to initialize.`);return i}const n=function(t,e){if(!t||!e?.length)return null;const i=new URL(t);let s=e.find(t=>t.domain===i.hostname);s||(s=e.find(t=>i.hostname.endsWith(t.domain)));if(!s)return null;if(s.pathScope?.length){if(!s.pathScope.some(t=>i.pathname.startsWith(t)))return null}return s}(e?.url||t.url,t.sites);if(n?.engine&&"auto"!==n.engine&&(i=await W.create(t,{engine:n.engine}),i))return i;if(i=await W.create(t,{engine:"http"}),!i)throw new Error("Failed to create default http engine");return i}W.registry=new Map;var G=class{constructor(t={}){this.options=t,this.closed=!1,this.id=x(),this.context=this.createContext(t)}_logDebug(t,...e){b(this.context.debug,{prefix:"FetchSession",id:this.id.slice(0,8),category:t},...e)}async execute(t,e=this.context){const i=t.id||t.name||t.action;this._logDebug("execute",`Executing action: ${i}`,t.params);const s=t.index??(e.internal.actionIndex||0);e.internal.actionIndex=s+1,await this.ensureEngine(t,e);const n=d.create(t);if(!n)throw new Error(`Unknown action: ${t.id||t.name}`);const r={...t,index:s};let o,a;e.currentAction={...r,startedAt:Date.now()};try{return o=await n.execute(e,r),o}catch(t){throw a=t,a}finally{e.currentAction=void 0}}async executeAll(t,e){this._logDebug("executeAll",`Total actions: ${t.length}`,t.map(t=>t.id||t.name||t.action));const i=this.context.internal,s=e?(0,g.defaultsDeep)({id:this.context.id,eventBus:this.context.eventBus,outputs:this.context.outputs,execute:this.context.execute,action:this.context.action,internal:i},e,this.context):this.context;let n=e?.index??0;try{for(;n<t.length;){const e=t[n];await this.execute({...e,index:n},s),n++}const e=await this.execute({id:"getContent",index:n},s);return{result:e?.result,outputs:this.getOutputs()}}catch(t){throw t.actionIndex=n,t}}getOutputs(){return this.context.outputs}async getState(){return this.context.internal.engine?.getState()}async dispose(){if(this.closed)return;const t=this.context.eventBus;t.emit("session:closing",{sessionId:this.id});try{await(this.context.internal.engine?.dispose())}finally{this.closed=!0}t.emit("session:closed",{sessionId:this.id})}async ensureEngine(t,e){if(this.closed)throw new Error("Session is closed");if(!e.internal.engine){const i=t?.params?.url??e.url,s=await D(e,{url:i});if(!s)throw new Error("No engine found");e.internal.engine=s}}createContext(t=this.options){const e=new m.EventEmitter;return(0,g.defaultsDeep)({...t,id:this.id,eventBus:e,outputs:{},internal:{},execute:async t=>this.execute(t),action:async function(t,e,i){return this.execute({name:t,params:e,...i})}},l)}},K=class{constructor(t={}){this.defaults=t}async createSession(t){const e={...this.defaults,...t||{}};return new G(e)}async fetch(t,e){"string"!=typeof t&&(t=(e=t).url);const i=await this.createSession(e);try{const s=e?.actions||[];t&&0!==s.findIndex(e=>("goto"===e.id||"goto"===e.name)&&e.params?.url===t)&&s.unshift({id:"goto",params:{url:t}});return await i.executeAll(s)}finally{await i.dispose()}}},J=require("crawlee"),Q=((t,s,n)=>(n=null!=t?e(r(t)):{},a(!s&&t&&t.__esModule?n:i(n,"default",{value:t,enumerable:!0}),t)))(require("cheerio")),X=require("util-ex"),Y=require("@isdk/common-error"),Z="___BR___",tt="___BLOCK___",et="___P___",it=/\s+/g,st=new RegExp(` *(${Z}|${tt}|${et}) *`,"g"),nt=new RegExp(`(?:${tt}|${et})+`,"g");var rt={"&amp;":"&amp;","&lt;":"&lt;","&gt;":"&gt;"},ot={"&quot;":'"',"&apos;":"'","&nbsp;":" ","&copy;":"©","&reg;":"®","&trade;":"™","&sect;":"§","&para;":"¶","&bull;":"•","&hellip;":"…","&euro;":"€","&pound;":"£","&yen;":"¥","&cent;":"¢","&curren;":"¤","&brvbar;":"¦","&uml;":"¨","&ordf;":"ª","&laquo;":"«","&raquo;":"»","&not;":"¬","&shy;":"","&macr;":"¯","&deg;":"°","&plusmn;":"±","&sup2;":"²","&sup3;":"³","&acute;":"´","&micro;":"µ","&middot;":"·","&cedil;":"¸","&sup1;":"¹","&ordm;":"º","&iquest;":"¿","&times;":"×","&divide;":"÷","&ndash;":"–","&mdash;":"—","&lsquo;":"‘","&rsquo;":"’","&sbquo;":"‚","&ldquo;":"“","&rdquo;":"”","&bdquo;":"„","&dagger;":"†","&Dagger;":"‡","&permil;":"‰","&lsaquo;":"‹","&rsaquo;":"›"};function at(t){return t?t.replace(/&(#?[a-zA-Z0-9]+);/g,t=>{const e=t.toLowerCase();if(rt[e])return t;if(ot[e])return ot[e];if(t.startsWith("&#")){const e=t.startsWith("&#x")?parseInt(t.slice(3,-1),16):parseInt(t.slice(2,-1),10);if(!isNaN(e)){if(160===e)return" ";try{return String.fromCodePoint(e)}catch(e){return t}}}return t}):t}var ct=class extends W{_ensureCheerioContext(t){if(!t.$&&t.body){let e="string"==typeof t.body?t.body:Buffer.isBuffer(t.body)?t.body.toString("utf-8"):JSON.stringify(t.body);e.trim().startsWith("<")||(e=`<html><body><pre>${e}</pre></body></html>`),t.$=Q.load(e)}}async _buildResponse(t){this._ensureCheerioContext(t);const{request:e,response:i,body:s,$:n}=t,r=n?.html();let o="string"==typeof s?s:Buffer.isBuffer(s)?s.toString("utf-8"):String(s??"");r&&r!==o&&(o=r);let a=i?.headers;if(!a&&i?.rawHeaders){a={};const t=i.rawHeaders;for(let e=0;e<t.length;e+=2)a[t[e].toLowerCase()]=t[e+1]}const c={url:e.url,finalUrl:e.loadedUrl||e.url,statusCode:i?.statusCode??200,statusText:i?.statusMessage,headers:a||{},body:s,html:at(o),text:o};if(this.opts?.debug&&i?.timings){const t=i.timings;c.metadata={timings:{start:t.start,total:t.phases?.total,ttfb:t.phases?.firstByte,dns:t.phases?.dns,tcp:t.phases?.tcp,download:t.phases?.download}}}return c}async _querySelectorAll(t,e){if(Array.isArray(t)){if(0===t.length)return[];const{$:i}=t[0],s=t.map(t=>t.el[0]).filter(Boolean),n=i(s);return n.find(e).add(n.filter(e)).toArray().map(t=>({$:i,el:i(t)}))}const{$:i,el:s}=t;return":scope"===e?[{$:i,el:s}]:s.find(e).add(s.filter(e)).toArray().map(t=>({$:i,el:i(t)}))}async _nextSiblingsUntil(t,e){const{$:i,el:s}=t;return(e?s.nextUntil(e):s.nextAll()).toArray().map(t=>({$:i,el:i(t)}))}async _parentElement(t){const{$:e,el:i}=t,s=i.parent();return 0===s.length?null:{$:e,el:s}}async _isSameElement(t,e){return t.el[0]===e.el[0]}async _findClosestAncestor(t,e){if(0===e.length)return null;const i=new Set(e.map(t=>t.el[0])),{$:s,el:n}=t;let r=n;for(;r.length>0;){if(i.has(r[0]))return{$:s,el:r};r=r.parent()}return null}async _contains(t,e){const i=t.el[0],s=e.el[0];if(i===s)return!0;const n=t.$;return"function"==typeof n.contains?n.contains(i,s):t.el.find(e.el).length>0}async _findCommonAncestor(t,e){const{$:i,el:s}=t,{el:n}=e;if(s[0]===n[0])return t;if(await this._contains(t,e))return t;if(await this._contains(e,t))return e;const r=s.parents().toArray(),o=n.parents().toArray(),a=new Set(o);for(const t of r)if(a.has(t))return{$:i,el:i(t)};return null}async _findContainerChild(t,e){const{$:i,el:s}=t,n=e.el[0];let r=s;if(r[0]===n)return t;const o=r.parents().toArray();for(let t=0;t<o.length;t++)if(o[t]===n){return{$:i,el:i(t>0?o[t-1]:s[0])}}if(n===i.root()[0]){return{$:i,el:i(o.length>0?o[o.length-1]:s[0])}}return null}async _extractValue(t,e){const{$:i,el:s}=e,{attribute:n,type:r="string",mode:o="text"}=t;if(this._logDebug("extract",`_extractValue: el.length=${s.length} schema=${JSON.stringify(t)}`),0===s.length)return null;let a="";if(n?a=s.attr(n)??null:"html"===r||"html"===o||"outerHTML"===o?(a="outerHTML"===o?i.html(s):s.html()??("html"===r?"":null),a&&(a=at(a.trim()))):a="innerText"===o?function(t){const e=t.clone();e.find("script, style, noscript, template").remove(),e.find("[hidden]").remove(),e.find("br").replaceWith(Z),e.find("p").before(et).after(et),e.find("div, h1, h2, h3, h4, h5, h6, li, ul, ol, tr, dl, dt, dd, blockquote, pre, form, table, article, section, header, footer, nav, main, aside, hr, address, fieldset, figure, figcaption, details, summary").before(tt).after(tt);let i=e.text();return i=i.replace(it," "),i=i.replace(st,"$1"),i=i.replace(nt,t=>t.includes(et)?et:tt),i=i.replaceAll(Z,"\n"),i=i.replaceAll(et,"\n\n"),i=i.replaceAll(tt,"\n"),i.trim()}(s):s.text().trim(),null===a)return null;switch(r){case"number":return parseFloat(a.replace(/[^0-9.-]+/g,""))||null;case"boolean":const t=a.toLowerCase();return"true"===t||"1"===t;default:return a}}_getInitialElementScope(t){const{$:e}=t;return e?{$:e,el:e.root()}:null}async executeAction(t,e){const{$:i}=t;switch(e.type){case"dispose":return;case"navigate":{const{url:i,opts:s}=e;this._logDebug("navigate",`Navigating to: ${i}`);const n=await this._requestWithRedirects(t,{url:i,method:"GET",headers:{...this.hdrs,...s?.headers}});return await this._updateStateAfterNavigation(t,n),this.lastResponse}case"mouseMove":case"mouseClick":case"mouseWheel":case"keyboardType":case"keyboardPress":case"scrollIntoView":throw new Y.CommonError(`Action "${e.type}" is only supported in browser engine mode.`,e.type,"not_supported");case"click":{if(!i)throw new Y.CommonError(`Cheerio context not available for action: ${e.type}`,"click");const s=e.selector,n=i(s).first();let r;if(0===n.length)try{r=new URL(s,t.request.loadedUrl||t.request.url).href}catch{throw new Y.CommonError(`click: selector not found or invalid URL: ${s}`,"click")}else{if(!n.is("a")||!n.attr("href")){if(n.is('input[type="submit"], button[type="submit"], button, input')){const e=n.closest("form");return e.length?this.executeAction(t,{type:"submit",selector:e}):void this._logDebug("click","Button/input clicked but no form found and no JS support in http mode. Ignoring.")}throw new Y.CommonError(`click: unsupported element for http simulate. Selector: ${s}`,"click")}{const e=n.attr("href");r=new URL(e,t.request.loadedUrl||t.request.url).href}}const o=await t.sendRequest({url:r});return void await this._updateStateAfterNavigation(t,o)}case"fill":{if(!i)throw new Y.CommonError(`Cheerio context not available for action: ${e.type}`),"fill";const s=i(e.selector).first();if(0===s.length)throw new Y.CommonError(`fill: selector not found: ${e.selector}`);if(!s.is("input, textarea, select"))throw new Y.CommonError(`fill: not a form field: ${e.selector}`);return s.val(e.value),void(this.lastResponse=await this.buildResponse(t))}case"trim":{if(!i)throw new Y.CommonError(`Cheerio context not available for action: ${e.type}`,"trim");const{selectors:s,removeComments:n}=this._getTrimInfo(e.options);return s.forEach(t=>i(t).remove()),n&&i("*").contents().filter((t,e)=>"comment"===e.type).remove(),void(this.lastResponse=await this.buildResponse(t))}case"waitFor":return void(e.options?.ms&&await new Promise(t=>setTimeout(t,e.options.ms)));case"submit":{if(!i)throw new Y.CommonError(`Cheerio context not available for action: ${e.type}`,"submit");const s="string"==typeof e.selector?i(e.selector).first():null!=e.selector?e.selector:i("form").first();if(0===s.length)throw new Y.NotFoundError(e.selector,"submit");const n=s.attr("action")||t.request.loadedUrl||t.request.url,r=(s.attr("method")||"GET").toUpperCase(),o=new URL(n,t.request.loadedUrl||t.request.url).href,a={};let c;if(s.find("input, select, textarea").each((t,e)=>{const s=i(e),n=s.attr("name");if(!n)return;const r=s.val();null!=r&&(a[n]=String(r))}),"GET"===r){const e=new URL(o);Object.entries(a).forEach(([t,i])=>e.searchParams.set(t,i)),c=await this._requestWithRedirects(t,{url:e.href,method:"GET"})}else{const i=e.options?.enctype||s.attr("enctype")||"application/x-www-form-urlencoded";let n;const r={};"application/json"===i?(n=JSON.stringify(a),r["Content-Type"]="application/json"):(n=new URLSearchParams(a).toString(),r["Content-Type"]="application/x-www-form-urlencoded"),this._logDebug("submit","Submitting POST to:",o,"enctype:",i),c=await this._requestWithRedirects(t,{url:o,method:"POST",body:n,headers:r})}return await this._updateStateAfterNavigation(t,c),void this._logDebug("submit","Submit finished. Current URL:",t.request.loadedUrl||t.request.url)}case"evaluate":{const{fn:s,args:n=[]}=e.params,r=t.request.loadedUrl||t.request.url;let o=null;const a=t=>t&&0!==t.length?{textContent:t.text(),innerHTML:t.html(),outerHTML:i.html(t),getAttribute:e=>t.attr(e),matches:e=>t.is(e)}:null,c=this,l={location:{_href:r,get href(){return this._href},set href(t){if(t&&t!==this._href){this._href=t;const e=new URL(t,r).href;o=c.goto(e)}},assign(t){this.href=t},replace(t){this.href=t}}},u={getElementById:t=>a(i(`#${t}`).first()),querySelector:t=>a(i(t).first()),querySelectorAll:t=>i(t).toArray().map(t=>a(i(t))),getElementsByClassName:t=>i(`.${t}`).toArray().map(t=>a(i(t))),getElementsByTagName:t=>i(t).toArray().map(t=>a(i(t))),get body(){return a(i("body").first())},get title(){return i("title").text()}};l.document=u;const h={window:l,document:u,$:i,console:console};let f;const d=(0,X.newFunction)(s,h);return f="function"==typeof d?await d(n):d,o?await o:l.location.href===r&&(this.lastResponse=await this.buildResponse(t)),f}default:throw new Y.CommonError(`Unknown action type: ${e.type}`,"CheerioFetchEngine.executeAction",Y.ErrorCode.NotSupported)}}async _requestWithRedirects(t,e){let{url:i,method:s,body:n,headers:r={}}=e,o=0;let a;for(;o<=5;){if(t.session){const e=t.session.getCookieString(i);e&&(r={...r,cookie:e})}if(a=await t.sendRequest({url:i,method:s,body:n,headers:r,followRedirect:!1}),!a)break;const e=a.statusCode,c=a.headers||a.req?.res?.headers||a.res?.headers||{};if(t.session&&c["set-cookie"]&&t.session.setCookies(c["set-cookie"],i),[301,302,303,307,308].includes(e)){const t=c.location;if(!t)break;if(i=new URL(t,i).href,o++,[301,302,303].includes(e)){this._logDebug("http",`Redirect ${e} (method conversion to GET):`,i),s="GET",n=void 0;const{"content-type":t,"Content-Type":o,"content-length":a,"Content-Length":c,...l}=r;r=l}else this._logDebug("http",`Redirect ${e} (method preserved):`,i);continue}break}return a}async _updateStateAfterNavigation(t,e){const i=e;t.response=i,t.body=i.body,t.$=void 0,i.url&&(t.request.loadedUrl=i.url),this.lastResponse=await this.buildResponse(t)}_createCrawler(t,e){return new J.CheerioCrawler(t,e)}_getSpecificCrawlerOptions(t){return{additionalMimeTypes:["text/plain"],maxRequestRetries:1,requestHandlerTimeoutSecs:t.requestHandlerTimeoutSecs,proxyConfiguration:this.proxyConfiguration,preNavigationHooks:[({session:e,request:i},s)=>{s.throwHttpErrors=t.throwHttpErrors,this.opts?.timeoutMs&&(s.timeout={request:this.opts.timeoutMs})}]}}async goto(t,e){if(this.isPageActive)return this.dispatchAction({type:"navigate",url:t,opts:e});const i="req-"+ ++this.requestCounter,s=new Promise((t,s)=>{const n=e?.timeoutMs||this.opts?.timeoutMs||3e4,r=setTimeout(()=>{this.pendingRequests.delete(i),this.navigationLock.release(),s(new Y.CommonError(`goto timed out after ${n}ms.`,"gotoTimeout",Y.ErrorCode.RequestTimeout))},n);this.pendingRequests.set(i,{resolve:e=>{clearTimeout(r),t(e)},reject:t=>{clearTimeout(r),s(t)}})});return this.requestQueue.addRequest({...e,url:t,headers:{...this.hdrs,...e?.headers},userData:{requestId:i},uniqueKey:`${t}-${i}`}).catch(t=>{const e=this.pendingRequests.get(i);e&&(this.pendingRequests.delete(i),this.navigationLock.release(),e.reject(t))}),await this.navigationLock,this.navigationLock=V(),s}};ct.id="cheerio",ct.mode="http",W.register(ct);var lt=require("crawlee"),ut=require("playwright"),ht=require("@isdk/common-error"),ft=3e4,dt=class extends W{constructor(){super(...arguments),this.currentMousePos={x:0,y:0},this.mouseInitialized=!1}async _buildResponse(t){const{page:e,response:i,request:s,session:n}=t;if(!e||e.isClosed())return{url:s.url,finalUrl:s.loadedUrl||s.url,statusCode:i?.status(),statusText:i?.statusText(),headers:await(i?.allHeaders())||{},body:"",html:"",text:""};const r=await e.content(),o=await e.textContent("body"),a=await e.context().cookies();n&&n.setCookies(a,s.url);const c={url:e.url(),finalUrl:e.url(),statusCode:i?.status(),statusText:i?.statusText(),headers:await(i?.allHeaders())||{},body:r,html:r,text:o||""};if(this.opts?.debug&&i){const t="function"==typeof i.request?i.request():i.request;if(t&&"function"==typeof t.timing){const e=t.timing();c.metadata={timings:{start:e.startTime,total:e.responseEnd-e.startTime,ttfb:e.responseStart-e.requestStart,dns:e.domainLookupEnd-e.domainLookupStart,tcp:e.connectEnd-e.connectStart,download:e.responseEnd-e.responseStart}}}}return!1!==this.opts?.output?.cookies&&(c.cookies=a),c}async _querySelectorAll(t,e){const i=Array.isArray(t)?t:[t],s=[];for(const t of i){const i=await t.locator(e).all();s.push(...i);try{await t.evaluate((t,e)=>t.matches(e),e)}catch(t){}}const n=[];for(const t of i){let i=!1;try{i=await t.evaluate((t,e)=>t.matches(e),e)}catch{}i&&n.push(t);const s=await t.locator(e).all();n.push(...s)}return n}async _nextSiblingsUntil(t,e){const i=await t.locator("xpath=following-sibling::*").all();if(!e)return i;const s=[];for(const t of i){if(await t.evaluate((t,e)=>t.matches(e),e))break;s.push(t)}return s}async _parentElement(t){const e=t.locator("xpath=..");return 0===await e.count()?null:e.first()}async _isSameElement(t,e){const i=await t.elementHandle(),s=await e.elementHandle();if(!i||!s)return!1;try{return await i.evaluate((t,e)=>t===e,s)}finally{await i.dispose(),await s.dispose()}}async _findClosestAncestor(t,e){if(0===e.length)return null;const i=await t.elementHandle();if(!i)return null;const s=await Promise.all(e.map(t=>t.elementHandle()));try{const t=await i.evaluate((t,e)=>{const i=new Set(e);let s=t;for(;s;){if(i.has(s))return e.indexOf(s);s=s.parentElement}return-1},s);return-1!==t?e[t]:null}finally{await i.dispose(),await Promise.all(s.map(t=>t?.dispose()))}}async _contains(t,e){const i=await t.elementHandle(),s=await e.elementHandle();if(!i||!s)return!1;try{return await i.evaluate((t,e)=>t.contains(e),s)}finally{await i.dispose(),await s.dispose()}}async _findCommonAncestor(t,e){const i=await t.elementHandle(),s=await e.elementHandle();if(!i||!s)return null;try{const e=await i.evaluateHandle((t,e)=>{let i=null;if(t===e)i=t;else if(t.contains(e))i=t;else if(e.contains(t))i=e;else{const s=new Set;let n=e.parentElement;for(;n;)s.add(n),n=n.parentElement;for(n=t.parentElement;n;){if(s.has(n)){i=n;break}n=n.parentElement}}return i&&1===i.nodeType?function t(e){if(e.id)return`//*[@id="${e.id}"]`;if(e===document.body)return"/html/body";if(e===document.documentElement)return"/html";let i=0;const s=e.parentNode?e.parentNode.childNodes:[];for(let n=0;n<s.length;n++){const r=s[n];if(r===e)return t(e.parentNode)+"/"+e.tagName.toLowerCase()+"["+(i+1)+"]";1===r.nodeType&&r.tagName===e.tagName&&i++}return""}(i):null},s);if(!e)return null;const n=await e.jsonValue();return"string"==typeof n&&n?t.page().locator(`xpath=${n}`):null}finally{await i.dispose(),await s.dispose()}}async _findContainerChild(t,e){const i=await t.elementHandle(),s=await e.elementHandle();if(!i||!s)return null;try{const e=await i.evaluateHandle((t,e)=>{let i=null;if(t===e)i=t;else{let s=t;for(;s;){if(s.parentElement===e){i=s;break}s=s.parentElement}}return i&&1===i.nodeType?function t(e){if(e.id)return`//*[@id="${e.id}"]`;if(e===document.body)return"/html/body";if(e===document.documentElement)return"/html";let i=0;const s=e.parentNode?e.parentNode.childNodes:[];for(let n=0;n<s.length;n++){const r=s[n];if(r===e)return t(e.parentNode)+"/"+e.tagName.toLowerCase()+"["+(i+1)+"]";1===r.nodeType&&r.tagName===e.tagName&&i++}return""}(i):null},s);if(!e)return null;const n=await e.jsonValue();return"string"==typeof n&&n?t.page().locator(`xpath=${n}`):null}finally{await i.dispose(),await s.dispose()}}async _extractValue(t,e){const{attribute:i,type:s="string",mode:n="text"}=t,r=await e.count();if(this._logDebug("extract",`_extractValue: count=${r} schema=${JSON.stringify(t)}`),0===r)return null;let o="";if(i?o=await e.getAttribute(i):"html"===s||"html"===n||"outerHTML"===n?(o="outerHTML"===n?await e.evaluate(t=>t.outerHTML):await e.innerHTML(),o&&(o=at(o))):o="innerText"===n?await e.innerText():await e.textContent(),null===o)return null;switch(o=o.trim(),s){case"number":return parseFloat(o.replace(/[^0-9.-]+/g,""))||null;case"boolean":const t=o.toLowerCase();return"true"===t||"1"===t;default:return o}}_getInitialElementScope(t){const{page:e}=t;return e?e.locator(":root"):null}async _waitForNavigation(t,e,i){const{page:s}=t,n=this.opts?.timeoutMs||ft;try{await s.waitForURL(t=>t.href!==e,{waitUntil:"domcontentloaded",timeout:5e3}),this._logDebug(i,"URL changed to:",s.url())}catch(t){this._logDebug(i,"No URL change detected within 5s")}await s.waitForLoadState("networkidle",{timeout:n}),this.lastResponse=await this.buildResponse(t)}async _sharedRequestHandler(t){const{page:e}=t;return e&&!this.mouseInitialized&&await this._initializeMousePos(e),super._sharedRequestHandler(t)}async _initializeMousePos(t){if(this.mouseInitialized||0!==this.currentMousePos.x||0!==this.currentMousePos.y)return void(this.mouseInitialized=!0);let e=0,i=0;Math.random()>.5?(e=0,i=Math.floor(600*Math.random())+100):(e=Math.floor(800*Math.random())+100,i=0),this.currentMousePos={x:e,y:i};try{await t.mouse.move(e,i),this.mouseInitialized=!0}catch(t){}}_getTrajectory(t,e,i=-1){const s=[],n=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2));-1===i&&(i=Math.max(2,Math.min(3,Math.floor(n/400)+2)));const r=t.x+(e.x-t.x)/2,o=t.y+(e.y-t.y)/2,a=r+(Math.random()-.5)*n*.1,c=o+(Math.random()-.5)*n*.1,l=t=>t<.5?4*t*t*t:1-Math.pow(-2*t+2,3)/2;for(let n=1;n<=i;n++){const r=l(n/i),o=(1-r)*(1-r)*t.x+2*(1-r)*r*a+r*r*e.x,u=(1-r)*(1-r)*t.y+2*(1-r)*r*c+r*r*e.y;s.push({x:o,y:u})}return s}async _moveToPos(t,e,i=-1){const{page:s}=t,n={...this.currentMousePos};e.x<0&&(e.x=Math.floor(Math.random()*z(Math.abs(e.x)))+(n.x||0)),e.y<0&&(e.y=Math.floor(Math.random()*z(Math.abs(e.y)))+(n.y||0));const r=s.viewportSize();r&&(e.x=Math.max(0,Math.min(e.x,r.width-1)),e.y=Math.max(0,Math.min(e.y,r.height-1)));const o=this._getTrajectory(n,e,i),a=Math.sqrt(Math.pow(e.x-n.x,2)+Math.pow(e.y-n.y,2)),c=Math.max(1,Math.min(3,a/500+1));let l=0,u=n;for(const t of o){const e=Math.sqrt(Math.pow(t.x-u.x,2)+Math.pow(t.y-u.y,2));e>l&&(l=e),u=t}const h=Math.max(5,Math.floor(l/c));for(const t of o)await s.mouse.move(t.x,t.y,{steps:h});return this.currentMousePos=e,this.currentMousePos}async _ensureVisible(t,e){const{page:i}=t,s=i.locator(e).first();await s.scrollIntoViewIfNeeded();const n=await s.boundingBox();if(!n)throw new ht.CommonError(`Selector not found or not visible: ${e}`,"ensureVisible");return{x:n.x+n.width/2,y:n.y+n.height/2}}async _moveToSelector(t,e,i=-1){const s=await this._ensureVisible(t,e);return this._moveToPos(t,s,i)}async executeAction(t,e){const{page:i}=t,s=this.opts?.timeoutMs||ft;switch(e.type){case"dispose":return;case"navigate":{const s=await i.goto(e.url,{waitUntil:e.opts?.waitUntil||"domcontentloaded",timeout:this.opts?.timeoutMs||ft});s&&(t={...t,response:s},this._logDebug("navigate",`Navigation status: ${s.status()} for ${s.url()}`));const n=await this.buildResponse(t);return this.lastResponse=n,n}case"mouseMove":{const{x:i,y:s,selector:n,steps:r=-1}=e.params;return void(n?(await this._moveToSelector(t,n,r),this.lastResponse=await this.buildResponse(t)):void 0!==i&&void 0!==s&&await this._moveToPos(t,{x:i,y:s},r))}case"mouseClick":{const{x:s,y:n,selector:r,button:o="left",clickCount:a=1,delay:c=0,steps:l=-1}=e.params;return r?await this._moveToSelector(t,r,l):void 0!==s&&void 0!==n&&await this._moveToPos(t,{x:s,y:n},l),await i.mouse.click(this.currentMousePos.x,this.currentMousePos.y,{button:o,clickCount:a,delay:z(c||50,.2)}),await i.waitForTimeout(z(100,.5)),void(this.lastResponse=await this.buildResponse(t))}case"mouseWheel":{const{x:s,y:n,selector:r,deltaX:o=0,deltaY:a=0,steps:c=1}=e.params;if(r){const e=await this._ensureVisible(t,r);await i.mouse.move(e.x,e.y),this.currentMousePos=e}else void 0!==s&&void 0!==n&&await this._moveToPos(t,{x:s,y:n});if(c>1){const t=o/c,e=a/c;for(let s=0;s<c;s++)await i.mouse.wheel(t,e)}else await i.mouse.wheel(o,a);return await i.waitForTimeout(z(100,.5)),void(this.lastResponse=await this.buildResponse(t))}case"scrollIntoView":{const{selector:i}=e.params;return await this._ensureVisible(t,i),void(this.lastResponse=await this.buildResponse(t))}case"keyboardType":{const{text:s,delay:n=150}=e.params;return await i.keyboard.type(s,{delay:z(n)}),void(this.lastResponse=await this.buildResponse(t))}case"keyboardPress":{const{key:s,delay:n=50}=e.params;return await i.keyboard.press(s,{delay:z(n)}),void(this.lastResponse=await this.buildResponse(t))}case"click":{const n=i.url();return await i.click(e.selector,{timeout:s}),void await this._waitForNavigation(t,n,"click")}case"fill":await i.fill(e.selector,e.value,{timeout:s});const n=await this.buildResponse(t);return void(this.lastResponse=n);case"trim":{const s=this._getTrimInfo(e.options);return await i.evaluate(t=>{const{selectors:e,removeComments:i,removeHidden:s}=t;if(e.forEach(t=>{document.querySelectorAll(t).forEach(t=>t.remove())}),s){const t=[];document.querySelectorAll("*").forEach(e=>{const i=window.getComputedStyle(e);"none"!==i.display&&"hidden"!==i.visibility||t.push(e)}),t.forEach(t=>t.remove())}if(i){const t=document.createNodeIterator(document,NodeFilter.SHOW_COMMENT),e=[];let i;for(;i=t.nextNode();)e.push(i);e.forEach(t=>t.parentElement?.removeChild(t))}},s),void(this.lastResponse=await this.buildResponse(t))}case"waitFor":try{e.options?.selector&&await i.waitForSelector(e.options.selector,{timeout:s}),e.options?.networkIdle&&await i.waitForLoadState("networkidle",{timeout:s}),e.options?.ms&&await i.waitForTimeout(z(e.options.ms,.1))}catch(t){if(!1!==e.options?.failOnTimeout)throw t}return;case"submit":{const s=e.selector||"form",n=i.locator(s).first();if(0===await n.count())throw new ht.NotFoundError(s,"submit");if("application/json"===(e.options?.enctype||"application/x-www-form-urlencoded")){const t=await n.elementHandle();if(!t)throw new ht.CommonError(`submit: could not get form handle for ${s}`,"submit");const e=await t.evaluate(async t=>{const e=new FormData(t),i={};e.forEach((t,e)=>{i[e]=t.toString()});const s=await fetch(t.action,{method:t.method,headers:{"Content-Type":"application/json"},body:JSON.stringify(i)}),n=await s.text();return{status:s.status,statusText:s.statusText,headers:Object.fromEntries(s.headers.entries()),body:n,html:n,text:n,url:t.action,finalUrl:s.url}});return await t.dispose(),await i.setContent(e.html),void(this.lastResponse=e)}{this._logDebug("submit","Submitting form by form.submit()...");const e=i.url();return await n.evaluate(t=>t.submit()),void await this._waitForNavigation(t,e,"submit")}}case"evaluate":{const{fn:n,args:r=[]}=e.params,o=i.url();let a;if(a="function"==typeof n?await i.evaluate(n,r):await i.evaluate(([t,e])=>{const i=(0,eval)(`(${t})`);return"function"==typeof i?i(e):i},[n,r]),i.url()!==o)await i.waitForLoadState("domcontentloaded",{timeout:s}).catch(()=>{}),this.lastResponse=await this.buildResponse(t);else try{this.lastResponse=await this.buildResponse(t)}catch(e){await i.waitForLoadState("domcontentloaded",{timeout:s}).catch(()=>{}),this.lastResponse=await this.buildResponse(t)}return a}default:throw new ht.CommonError(`Unknown action type: ${e.type}`,"PlaywrightFetchEngine.executeAction",ht.ErrorCode.NotSupported)}}_createCrawler(t,e){return new lt.PlaywrightCrawler(t,e)}async _getSpecificCrawlerOptions(t){const e=t.browser?.headless??!0,i={maxRequestRetries:t.retries||3,headless:e,proxyConfiguration:this.proxyConfiguration,requestHandlerTimeoutSecs:t.requestHandlerTimeoutSecs,preNavigationHooks:[async({page:e,request:i},s)=>{s.throwHttpErrors=t.throwHttpErrors;const n=this.blockedTypes;n.size>0&&await e.route("**/*",t=>{n.has(t.request().resourceType())?t.abort():t.continue()})}]},s=t.browser?.launchOptions||{};if(this.opts?.antibot){i.browserPoolOptions={useFingerprints:!1};const{launchOptions:t}=await import("camoufox-js"),n=await t({headless:e,...s});i.launchContext={launcher:ut.firefox,launchOptions:n},i.postNavigationHooks=[async({page:t,handleCloudflareChallenge:e})=>{await e()}]}else Object.keys(s).length>0&&(i.launchContext={launchOptions:s});return i}async goto(t,e){if(this.isPageActive)return this.dispatchAction({type:"navigate",url:t,opts:e});if(!this.requestQueue)throw new ht.CommonError("RequestQueue not initialized","goto");const i="req-"+ ++this.requestCounter,s=new Promise((t,e)=>{this.pendingRequests.set(i,{resolve:t,reject:e})});return await this.requestQueue.addRequest({url:t,headers:this.hdrs,userData:{requestId:i,waitUntil:e?.waitUntil||"domcontentloaded"},uniqueKey:`${t}-${i}`}),s}};dt.id="playwright",dt.mode="browser",W.register(dt);var wt=class extends d{async onExecute(t,e){const{selector:i,...s}=e?.params||{};if(!i)throw new Error("Selector is required for click action");await this.delegateToEngine(t,"click",i,s)}};wt.id="click",wt.returnType="none",wt.capabilities={http:"simulate",browser:"native"},d.register(wt);var yt=class extends d{async onExecute(t,e){const{selector:i,value:s,...n}=e?.params||{};if(!i)throw new Error("Selector is required for fill action");if(void 0===s)throw new Error("Value is required for fill action");await this.delegateToEngine(t,"fill",i,s,n)}};yt.id="fill",yt.returnType="none",yt.capabilities={http:"simulate",browser:"native"},d.register(yt);var pt=class extends d{async onExecute(t,e){return await this.delegateToEngine(t,"getContent",e?.params)}};pt.id="getContent",pt.returnType="response",pt.capabilities={http:"native",browser:"native"},d.register(pt);var mt=class extends d{async onExecute(t,e,i){const s=e?.params,n=s?.url||t.url;if(!n)throw new Error("URL is required for goto action");const r=t.internal.engine;if(!r)throw new Error("No engine available");t.url=n;return await r.goto(n,s)}};mt.id="goto",mt.returnType="response",mt.capabilities={http:"native",browser:"native"},d.register(mt);var gt=class extends d{async onExecute(t,e){const{selector:i,...s}=e?.params||{};await this.delegateToEngine(t,"submit",i,s)}};gt.id="submit",gt.returnType="none",gt.capabilities={http:"simulate",browser:"native"},d.register(gt);var xt=class extends d{async onExecute(t,e){const i=t.internal.engine;if(!i)throw new Error("No engine available");await i.waitFor(e?.params)}};xt.id="waitFor",xt.returnType="none",xt.capabilities={http:"native",browser:"native"},d.register(xt);var bt=class extends d{async onExecute(t,e){const i=e?.params;if(!i)throw new Error("Schema is required for extract action");return this.delegateToEngine(t,"extract",i)}};bt.id="extract",bt.returnType="any",bt.capabilities={http:"native",browser:"native"},d.register(bt);var vt=class extends d{async onExecute(t,e){const{selector:i,message:s,attribute:n}=e?.params||{},r=t.internal.engine;if("browser"===r?.mode){if(i){if(!await(r?.extract({selector:i,attribute:n})))return}r&&"pause"in r?await r.pause(s):console.warn("[PauseAction] was called, but the current engine does not support `pause`. Skipped.")}else console.warn("[PauseAction] can only run in browser engine. Skipped.")}};vt.id="pause",vt.capabilities={http:"native",browser:"native"},vt.returnType="none",d.register(vt);var $t=class extends d{async onExecute(t,e){const i=e?.params||{};await this.delegateToEngine(t,"trim",i)}};$t.id="trim",$t.returnType="none",$t.capabilities={http:"simulate",browser:"native"},d.register($t);var _t=class extends d{async onExecute(t,e){const i=e?.params;if(!i)throw new Error("evaluate action: params is required");return await this.delegateToEngine(t,"evaluate",i)}};_t.id="evaluate",_t.returnType="any",_t.capabilities={http:"simulate",browser:"native"},d.register(_t);var qt=class extends d{async onExecute(t,e){const i=e?.params;await this.delegateToEngine(t,"mouseMove",i)}};qt.id="mouseMove",qt.returnType="none",qt.capabilities={http:"noop",browser:"native"};var kt=class extends d{async onExecute(t,e){const i=e?.params;await this.delegateToEngine(t,"mouseClick",i)}};kt.id="mouseClick",kt.returnType="none",kt.capabilities={http:"noop",browser:"native"};var Et=class extends d{async onExecute(t,e){const i=e?.params;await this.delegateToEngine(t,"scrollIntoView",i)}};Et.id="scrollIntoView",Et.returnType="none",Et.capabilities={http:"noop",browser:"native"};var St=class extends d{async onExecute(t,e){const i=e?.params;await this.delegateToEngine(t,"mouseWheel",i)}};St.id="mouseWheel",St.returnType="none",St.capabilities={http:"noop",browser:"native"},d.register(qt),d.register(kt),d.register(Et),d.register(St);var Ct=class extends d{async onExecute(t,e){const i=e?.params;if(!i?.text)throw new Error("text is required for keyboardType action");await this.delegateToEngine(t,"keyboardType",i.text,i.delay)}};Ct.id="keyboardType",Ct.returnType="none",Ct.capabilities={http:"noop",browser:"native"};var At=class extends d{async onExecute(t,e){const i=e?.params;if(!i?.key)throw new Error("key is required for keyboardPress action");await this.delegateToEngine(t,"keyboardPress",i.key,i.delay)}};async function Mt(t,e){return(new K).fetch(t,e)}At.id="keyboardPress",At.returnType="none",At.capabilities={http:"noop",browser:"native"},d.register(Ct),d.register(At);