@isdk/web-fetcher 0.3.0 → 0.3.1

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 (83) hide show
  1. package/README.action.cn.md +53 -312
  2. package/README.action.extract.cn.md +263 -0
  3. package/README.action.extract.md +263 -0
  4. package/README.action.md +53 -311
  5. package/README.cn.md +10 -2
  6. package/README.engine.cn.md +22 -1
  7. package/README.engine.md +22 -1
  8. package/README.md +8 -1
  9. package/dist/index.d.mts +147 -1
  10. package/dist/index.d.ts +147 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.mjs +1 -1
  13. package/docs/README.md +8 -1
  14. package/docs/_media/README.action.md +53 -311
  15. package/docs/_media/README.cn.md +10 -2
  16. package/docs/_media/README.engine.md +22 -1
  17. package/docs/classes/CheerioFetchEngine.md +236 -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 +27 -23
  22. package/docs/classes/FetchEngine.md +218 -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/PauseAction.md +23 -23
  32. package/docs/classes/PlaywrightFetchEngine.md +337 -87
  33. package/docs/classes/SubmitAction.md +23 -23
  34. package/docs/classes/TrimAction.md +23 -23
  35. package/docs/classes/WaitForAction.md +23 -23
  36. package/docs/classes/WebFetcher.md +5 -5
  37. package/docs/enumerations/FetchActionResultStatus.md +4 -4
  38. package/docs/functions/fetchWeb.md +2 -2
  39. package/docs/globals.md +8 -0
  40. package/docs/interfaces/BaseFetchActionProperties.md +12 -12
  41. package/docs/interfaces/BaseFetchCollectorActionProperties.md +16 -16
  42. package/docs/interfaces/BaseFetcherProperties.md +31 -27
  43. package/docs/interfaces/Cookie.md +14 -14
  44. package/docs/interfaces/DispatchedEngineAction.md +4 -4
  45. package/docs/interfaces/EvaluateActionOptions.md +3 -3
  46. package/docs/interfaces/ExtractActionProperties.md +12 -12
  47. package/docs/interfaces/FetchActionInContext.md +15 -15
  48. package/docs/interfaces/FetchActionProperties.md +13 -13
  49. package/docs/interfaces/FetchActionResult.md +6 -6
  50. package/docs/interfaces/FetchContext.md +41 -37
  51. package/docs/interfaces/FetchEngineContext.md +36 -32
  52. package/docs/interfaces/FetchMetadata.md +5 -5
  53. package/docs/interfaces/FetchResponse.md +14 -14
  54. package/docs/interfaces/FetchReturnTypeRegistry.md +7 -7
  55. package/docs/interfaces/FetchSite.md +34 -30
  56. package/docs/interfaces/FetcherOptions.md +33 -29
  57. package/docs/interfaces/GotoActionOptions.md +14 -6
  58. package/docs/interfaces/KeyboardPressParams.md +25 -0
  59. package/docs/interfaces/KeyboardTypeParams.md +25 -0
  60. package/docs/interfaces/MouseClickParams.md +49 -0
  61. package/docs/interfaces/MouseMoveParams.md +41 -0
  62. package/docs/interfaces/PendingEngineRequest.md +3 -3
  63. package/docs/interfaces/StorageOptions.md +5 -5
  64. package/docs/interfaces/SubmitActionOptions.md +2 -2
  65. package/docs/interfaces/TrimActionOptions.md +3 -3
  66. package/docs/interfaces/WaitForActionOptions.md +5 -5
  67. package/docs/type-aliases/BaseFetchActionOptions.md +1 -1
  68. package/docs/type-aliases/BaseFetchCollectorOptions.md +1 -1
  69. package/docs/type-aliases/BrowserEngine.md +1 -1
  70. package/docs/type-aliases/FetchActionCapabilities.md +1 -1
  71. package/docs/type-aliases/FetchActionCapabilityMode.md +1 -1
  72. package/docs/type-aliases/FetchActionOptions.md +1 -1
  73. package/docs/type-aliases/FetchEngineAction.md +2 -2
  74. package/docs/type-aliases/FetchEngineType.md +1 -1
  75. package/docs/type-aliases/FetchReturnType.md +1 -1
  76. package/docs/type-aliases/FetchReturnTypeFor.md +1 -1
  77. package/docs/type-aliases/OnFetchPauseCallback.md +1 -1
  78. package/docs/type-aliases/ResourceType.md +1 -1
  79. package/docs/type-aliases/TrimPreset.md +1 -1
  80. package/docs/variables/DefaultFetcherProperties.md +1 -1
  81. package/docs/variables/FetcherOptionKeys.md +1 -1
  82. package/docs/variables/TRIM_PRESETS.md +1 -1
  83. package/package.json +10 -10
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:()=>at,ClickAction:()=>dt,DefaultFetcherProperties:()=>l,EvaluateAction:()=>$t,ExtractAction:()=>xt,FetchAction:()=>d,FetchActionResultStatus:()=>h,FetchEngine:()=>D,FetchSession:()=>z,FetcherOptionKeys:()=>u,FillAction:()=>wt,GetContentAction:()=>yt,GotoAction:()=>pt,KeyboardPressAction:()=>Et,KeyboardTypeAction:()=>qt,MouseClickAction:()=>kt,MouseMoveAction:()=>_t,PauseAction:()=>bt,PlaywrightFetchEngine:()=>ft,SubmitAction:()=>mt,TRIM_PRESETS:()=>V,TrimAction:()=>vt,WaitForAction:()=>gt,WebFetcher:()=>K,fetchWeb:()=>St}),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"),k=require("crawlee"),q=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 R.call(this,s,e,i)}async function R(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 T.call(this,t,e,r);case"array":return P.call(this,t,e,r);default:return O.call(this,t,e,r)}}async function T(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 q.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 j.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 q.CommonError(`Required property "${h[0]}" is missing.`,"extract");return null}}async function j(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 L.call(this,n.anchor,s,h,e,f,n.depth);if(!r){if(i)throw new q.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 R.call(this,e,y?t:d,i),f&&!n.anchor){const e=y&&Array.isArray(r)?t[t.length-1]:d;u=await I.call(this,e,u)}y&&(d=t[t.length-1])}else r=null}else r=await R.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 P(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=M.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 N.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 U.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 O(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 q.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 M(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 R.call(this,t,i,r);if(null!==e)s.push(e);else{if(n&&r)throw new q.CommonError("Required item is missing in array.","extract");n||o||s.push(null)}}return s}async function N(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 q.CommonError(`Required field "${t}" is missing at index ${d}.`,"extract");throw new q.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 q.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 U(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 q.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 R.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 q.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 L(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 I(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 B(){let t=()=>{};const e=new Promise(e=>{t=e});return e.release=t,e}k.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"]']},D=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=B();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(V))(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 R.call(this,t,e,i)}_normalizeArrayMode(t){return M.call(this,t)}async _extractNested(t,e,i){return F.call(this,t,e,i)}async _extractColumnar(t,e,i){return N.call(this,t,e,i)}async _extractSegmented(t,e,i){return U.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})}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 k.Configuration({persistStorage:s,storageClientOptions:{persistStorage:s,...i.config},...i.config}),r=i.id||t.id;this.requestQueue=await k.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 k.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 k.KeyValueStore.open(r,{config:n}),f=await h.getValue(k.PERSIST_STATE_KEY);!t.sessionState||f&&!t.overrideSessionState||await h.setValue(k.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||500,r=t?.url?t.url:i.url,o=new _.CommonError(`Request${r?" for "+r:""} failed: ${e.message}`,"request",n);s.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,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()}};async function G(t,e){let i;const s=e?.engine||t.engine;if(s&&"auto"!==s){if(i=await D.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 D.create(t,{engine:n.engine}),i))return i;if(i=await D.create(t,{engine:"http"}),!i)throw new Error("Failed to create default http engine");return i}D.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 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=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 s=e?.index??0;try{for(;s<t.length;){const e=t[s];await this.execute({...e,index:s},i),s++}const e=await this.execute({id:"getContent",index:s},i);return{result:e?.result,outputs:this.getOutputs()}}catch(t){throw t.actionIndex=s,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 G(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 z(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"),W=((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")),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"),st=new RegExp(`(?:${Y}|${tt})+`,"g");var nt={"&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(nt[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 D{_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.$=W.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: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],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=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(st,t=>t.includes(tt)?tt:Y),i=i.replaceAll(X,"\n"),i=i.replaceAll(tt,"\n\n"),i=i.replaceAll(Y,"\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"keyboardType":case"keyboardPress":throw new Z.CommonError(`Action "${e.type}" is only supported in browser engine mode.`,e.type,"not_supported");case"click":{if(!i)throw new Z.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 Z.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 Z.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 Z.CommonError(`Cheerio context not available for action: ${e.type}`),"fill";const s=i(e.selector).first();if(0===s.length)throw new Z.CommonError(`fill: selector not found: ${e.selector}`);if(!s.is("input, textarea, select"))throw new Z.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 Z.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 Z.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 Z.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,Q.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 Z.CommonError(`Unknown action type: ${e.type}`,"CheerioFetchEngine.executeAction",Z.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 Z.CommonError(`goto timed out after ${n}ms.`,"gotoTimeout",Z.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=B(),s}};at.id="cheerio",at.mode="http",D.register(at);var ct=require("crawlee"),lt=require("playwright"),ut=require("@isdk/common-error"),ht=3e4,ft=class extends D{constructor(){super(...arguments),this.currentMousePos={x:0,y:0}}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=ot(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||ht;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)}_getRandomDelay(t,e=.3){const i=t*(1-e),s=t*(1+e);return Math.floor(Math.random()*(s-i+1)+i)}_getTrajectory(t,e,i=-1){const s=[];if(-1===i){const s=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),n=10*Math.random()+5;i=Math.max(5,Math.floor(s/n))}const n=t.x+(e.x-t.x)/2,r=t.y+(e.y-t.y)/2,o=n+100*(Math.random()-.5),a=r+100*(Math.random()-.5);this._logDebug("mouseMove",`Trajectory: start(${t.x},${t.y}) -> end(${e.x},${e.y}), cp(${o},${a}), steps: ${i}`);for(let n=1;n<=i;n++){const r=n/i;let c=(1-r)*(1-r)*t.x+2*(1-r)*r*o+r*r*e.x,l=(1-r)*(1-r)*t.y+2*(1-r)*r*a+r*r*e.y;n<i&&(c+=1.5*(Math.random()-.5),l+=1.5*(Math.random()-.5)),s.push({x:c,y:l})}return s}async _moveToSelector(t,e,i=-1){const{page:s}=t,n=s.locator(e).first(),r=await n.boundingBox();if(!r)throw new ut.CommonError(`Selector not found or not visible for mouse movement: ${e}`,"mouseMove");const o=r.x+r.width/2,a=r.y+r.height/2,c=this._getTrajectory(this.currentMousePos,{x:o,y:a},i);for(const t of c)await s.mouse.move(t.x,t.y),(i>1||-1===i)&&await s.waitForTimeout(15*Math.random()+5);return this.currentMousePos={x:o,y:a},this.currentMousePos}async executeAction(t,e){const{page:i}=t,s=this.opts?.timeoutMs||ht;switch(e.type){case"dispose":return;case"navigate":{this._logDebug("navigate",`Navigating to: ${e.url}`);const s=await i.goto(e.url,{waitUntil:e.opts?.waitUntil||"domcontentloaded",timeout:this.opts?.timeoutMs||ht});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:s,y:n,selector:r,steps:o=-1}=e.params;if(r)await this._moveToSelector(t,r,o);else if(void 0!==s&&void 0!==n){const t=this._getTrajectory(this.currentMousePos,{x:s,y:n},o);for(const e of t)await i.mouse.move(e.x,e.y),(o>1||-1===o)&&await i.waitForTimeout(15*Math.random()+5);this.currentMousePos={x:s,y:n}}return}case"mouseClick":{const{x:s,y:n,selector:r,button:o="left",clickCount:a=1,delay:c=0}=e.params;return r?(await this._moveToSelector(t,r,-1),await i.mouse.click(this.currentMousePos.x,this.currentMousePos.y,{button:o,clickCount:a,delay:this._getRandomDelay(c||50,.2)})):void 0!==s&&void 0!==n?(await i.mouse.click(s,n,{button:o,clickCount:a,delay:this._getRandomDelay(c||50,.2)}),this.currentMousePos={x:s,y:n}):await i.mouse.click(this.currentMousePos.x,this.currentMousePos.y,{button:o,clickCount:a,delay:this._getRandomDelay(c||50,.2)}),await i.waitForTimeout(this._getRandomDelay(100,.5)),void(this.lastResponse=await this.buildResponse(t))}case"keyboardType":{const{text:s,delay:n=150}=e.params;return await i.keyboard.type(s,{delay:this._getRandomDelay(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:this._getRandomDelay(n)}),void(this.lastResponse=await this.buildResponse(t))}case"click":{this._logDebug("click","Clicking selector:",e.selector);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(this._getRandomDelay(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 ut.NotFoundError(s,"submit");if("application/json"===(e.options?.enctype||"application/x-www-form-urlencoded")){const t=await n.elementHandle();if(!t)throw new ut.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...");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 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},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:lt.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 ut.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}};ft.id="playwright",ft.mode="browser",D.register(ft);var dt=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)}};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: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)}};wt.id="fill",wt.returnType="none",wt.capabilities={http:"simulate",browser:"native"},d.register(wt);var yt=class extends d{async onExecute(t,e){return await this.delegateToEngine(t,"getContent",e?.params)}};yt.id="getContent",yt.returnType="response",yt.capabilities={http:"native",browser:"native"},d.register(yt);var pt=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)}};pt.id="goto",pt.returnType="response",pt.capabilities={http:"native",browser:"native"},d.register(pt);var mt=class extends d{async onExecute(t,e){const{selector:i,...s}=e?.params||{};await this.delegateToEngine(t,"submit",i,s)}};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: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.")}};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)}};$t.id="evaluate",$t.returnType="any",$t.capabilities={http:"simulate",browser:"native"},d.register($t);var _t=class extends d{async onExecute(t,e){const i=e?.params;await this.delegateToEngine(t,"mouseMove",i)}};_t.id="mouseMove",_t.returnType="none",_t.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"},d.register(_t),d.register(kt);var qt=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)}};qt.id="keyboardType",qt.returnType="none",qt.capabilities={http:"noop",browser:"native"};var Et=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 St(t,e){return(new K).fetch(t,e)}Et.id="keyboardPress",Et.returnType="none",Et.capabilities={http:"noop",browser:"native"},d.register(qt),d.register(Et);