preact-missing-hooks 4.5.0 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +144 -17
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.modern.mjs +1 -1
- package/dist/index.modern.mjs.map +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/react.js +77 -0
- package/dist/usePrefetch.d.ts +44 -0
- package/docs/README.md +107 -0
- package/docs/main.js +34 -0
- package/llm.package.json +105 -216
- package/llm.package.txt +29 -47
- package/package.json +8 -2
- package/src/index.ts +1 -0
- package/src/usePrefetch.ts +92 -0
- package/tests/usePrefetch.test.tsx +107 -0
package/dist/index.modern.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useState as e,useCallback as t,useEffect as n,useMemo as r,useRef as o}from"preact/hooks";import{isValidElement as i,cloneElement as s}from"preact";import{useRef as c,useEffect as u}from"preact/compat";function a(){const[n,r]=e(!1);return[t(e=>{r(!0),Promise.resolve().then(()=>{e(),r(!1)})},[]),n]}function l(e,t,r){n(()=>{const n=e.current;if(!n)return;const o=new MutationObserver(t);return o.observe(n,r),()=>o.disconnect()},[e,t,r])}const d=new Map;function f(){return{emit:t((e,...t)=>{const n=d.get(e);n&&n.forEach(e=>e(...t))},[]),on:t((e,t)=>{let n=d.get(e);return n||(n=new Set,d.set(e,n)),n.add(t),()=>{n.delete(t),0===n.size&&d.delete(e)}},[])}}function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},p.apply(null,arguments)}function m(e,t,n="preserve"){return r(()=>{if(!e)return e;const r=e=>{if(!i(e))return e;const r=e.props||{};let o;o="override"===n?p({},r,t):p({},t,r);const c=null==r?void 0:r.style,u=null==t?void 0:t.style;return c&&u&&"object"==typeof c&&"object"==typeof u&&(o.style="override"===n?p({},c,u):p({},u,c)),s(e,o)};return Array.isArray(e)?e.map(r):r(e)},[e,t,n])}function g(){const[t,r]=e(()=>{if("undefined"==typeof window)return"no-preference";const e=window.matchMedia("(prefers-color-scheme: dark)"),t=window.matchMedia("(prefers-color-scheme: light)");return e.matches?"dark":t.matches?"light":"no-preference"});return n(()=>{if("undefined"==typeof window)return;const e=window.matchMedia("(prefers-color-scheme: dark)"),t=e=>{r(e.matches?"dark":"light")},n=()=>{const e=window.matchMedia("(prefers-color-scheme: dark)"),t=window.matchMedia("(prefers-color-scheme: light)");r(e.matches?"dark":t.matches?"light":"no-preference")};e.addEventListener("change",t);const o=window.matchMedia("(prefers-color-scheme: light)");return o.addEventListener("change",n),()=>{e.removeEventListener("change",t),o.removeEventListener("change",n)}},[]),t}function y(){if("undefined"==typeof navigator)return{online:!0};const e={online:navigator.onLine},t=navigator.connection;return t&&(void 0!==t.effectiveType&&(e.effectiveType=t.effectiveType),void 0!==t.downlink&&(e.downlink=t.downlink),void 0!==t.rtt&&(e.rtt=t.rtt),void 0!==t.saveData&&(e.saveData=t.saveData),void 0!==t.type&&(e.connectionType=t.type)),e}function v(){const[t,r]=e(y);return n(()=>{if("undefined"==typeof window)return;const e=()=>r(y());window.addEventListener("online",e),window.addEventListener("offline",e);const t=navigator.connection;return null!=t&&t.addEventListener&&t.addEventListener("change",e),()=>{window.removeEventListener("online",e),window.removeEventListener("offline",e),null!=t&&t.removeEventListener&&t.removeEventListener("change",e)}},[]),t}function w(n={}){const{resetDelay:r=2e3}=n,[o,i]=e(!1),[s,c]=e(null),u=t(()=>{i(!1),c(null)},[]);return{copy:t(async e=>{if(c(null),"undefined"==typeof navigator||!navigator.clipboard){const e=new Error("Clipboard API is not available");return c(e),!1}try{return await navigator.clipboard.writeText(e),i(!0),r>0&&setTimeout(()=>i(!1),r),!0}catch(e){const t=e instanceof Error?e:new Error(String(e));return c(t),!1}},[r]),paste:t(async()=>{if(c(null),"undefined"==typeof navigator||!navigator.clipboard){const e=new Error("Clipboard API is not available");return c(e),""}try{return await navigator.clipboard.readText()}catch(e){const t=e instanceof Error?e:new Error(String(e));return c(t),""}},[]),copied:o,error:s,reset:u}}function h(e,t){const{onRageClick:r,threshold:i=5,timeWindow:s=1e3,distanceThreshold:c=30}=t,u=o(r);u.current=r;const a=o([]);n(()=>{const t=e.current;if(!t)return;const n=e=>{const t=Date.now(),n={time:t,x:e.clientX,y:e.clientY},r=t-s,o=a.current.filter(e=>e.time>=r);if(o.push(n),Infinity!==c){const t=o.filter(e=>{return t=e,r=n,Math.hypot(r.x-t.x,r.y-t.y)<=c;var t,r});if(t.length>=i)return u.current({count:t.length,event:e}),void(a.current=[])}else if(o.length>=i)return u.current({count:o.length,event:e}),void(a.current=[]);a.current=o};return t.addEventListener("click",n),()=>t.removeEventListener("click",n)},[e,i,s,c])}function b(r,i){const{mode:s,concurrency:c=4}=i,u="sequential"===s?1:Math.max(1,c),[a,l]=e(!1),[d,f]=e(void 0),[p,m]=e(void 0),[g,y]=e(0),v=o([]),w=o(0),h=o(0),b=o(!1),S=o(r);S.current=r;const k=t(()=>{y(v.current.length+h.current)},[]),E=t(()=>{if(b.current)return;if(h.current>=u)return;if(0===v.current.length)return 0===h.current&&l(!1),void k();v.current.sort((e,t)=>e.priority!==t.priority?e.priority-t.priority:e.sequence-t.sequence);const e=v.current.shift();h.current+=1,l(!0),k(),(0,S.current)(e.data).then(t=>{f(t),m(void 0),e.resolve(t)}).catch(t=>{m(t),e.reject(t)}).finally(()=>{h.current-=1,k(),E()}),v.current.length>0&&h.current<u&&E()},[u,k]),x=t((e,t)=>{var n;if(b.current)return Promise.reject(new Error("Worker is terminated"));const r=null!=(n=null==t?void 0:t.priority)?n:1,o=++w.current,i=new Promise((t,n)=>{v.current.push({data:e,priority:r,sequence:o,resolve:t,reject:n})});return k(),l(!0),queueMicrotask(E),i},[E,k]),I=t(()=>{const e=v.current;v.current=[],e.forEach(e=>e.reject(new Error("Task cleared from queue"))),k(),0===h.current&&l(!1)},[k]),T=t(()=>{b.current=!0,I()},[I]);return n(()=>()=>{b.current=!0},[]),{run:x,loading:a,result:d,error:p,queueSize:g,clearQueue:I,terminate:T}}const S=new Map;function k(e){return new Promise((t,n)=>{e.onsuccess=()=>t(e.result),e.onerror=()=>{var t;return n(null!=(t=e.error)?t:new DOMException("Unknown IndexedDB error"))}})}function E(e,t){return t?e.then(e=>(null==t.onSuccess||t.onSuccess(e),e)).catch(e=>{throw null==t.onError||t.onError(e),e}):e}function x(t){const[r,i]=e(null),[s,c]=e(null),[u,a]=e(!1),l=o(t);return l.current=t,n(()=>{let e=!1;c(null),a(!1),i(null);const{name:t,version:n,tables:r}=l.current;return function(e){const t=`${e.name}_v${e.version}`;let n=S.get(t);return n||(n=function(e){return new Promise((t,n)=>{const r=indexedDB.open(e.name,e.version);r.onerror=()=>{var e;return n(null!=(e=r.error)?e:new DOMException("Failed to open database"))},r.onsuccess=()=>t(r.result),r.onupgradeneeded=t=>{const n=t.target.result,r=e.tables;for(const e of Object.keys(r)){const t=r[e];if(!n.objectStoreNames.contains(e)){var o;const r=n.createObjectStore(e,{keyPath:t.keyPath,autoIncrement:null!=(o=t.autoIncrement)&&o});if(t.indexes)for(const e of t.indexes)r.createIndex(e,e,{unique:!1})}}}})}(e),S.set(t,n),n)}({name:t,version:n,tables:r}).then(t=>{if(e)return void t.close();const n=function(e){return{get db(){return e},hasTable:t=>e.objectStoreNames.contains(t),table:t=>function(e,t){return function(e,t){function n(n){return e.transaction([t],n).objectStore(t)}return{insert:(e,t)=>E(k(n("readwrite").add(e)),t),update(e,t,r){const o=n("readwrite");return E(k(o.get(e)).then(e=>{if(void 0===e)throw new DOMException("Key not found","NotFoundError");const n=p({},e,t);return k(o.put(n))}).then(()=>{}),r)},delete:(e,t)=>E(k(n("readwrite").delete(e)).then(()=>{}),t),exists:e=>k(n("readonly").getKey(e)).then(e=>void 0!==e),query(e,t){const r=n("readonly").openCursor(),o=[];return E(new Promise((t,n)=>{r.onsuccess=()=>{const n=r.result;n?(e(n.value)&&o.push(n.value),n.continue()):t(o)},r.onerror=()=>{var e;return n(null!=(e=r.error)?e:new DOMException("Unknown error"))}}),t)},upsert:(e,t)=>E(k(n("readwrite").put(e)),t),bulkInsert(e,t){const r=n("readwrite"),o=[];if(0===e.length)return E(Promise.resolve(o),t);let i=0;return E(new Promise((t,n)=>{e.forEach((s,c)=>{const u=r.add(s);u.onsuccess=()=>{o[c]=u.result,i++,i===e.length&&t(o)},u.onerror=()=>{var e;return n(null!=(e=u.error)?e:new DOMException("Unknown error"))}})}),t)},clear:e=>E(k(n("readwrite").clear()).then(()=>{}),e),count:e=>E(k(n("readonly").count()),null!=e?e:{})}}(e,t)}(e,t),transaction(t,n,r,o){const i=e.transaction(t,n),s={table:e=>function(e,t){return function(e,t){function n(){return e.objectStore(t)}return{insert:(e,t)=>E(k(n().add(e)),t),update(e,t,r){const o=n();return E(k(o.get(e)).then(e=>{if(void 0===e)throw new DOMException("Key not found","NotFoundError");const n=p({},e,t);return k(o.put(n))}).then(()=>{}),r)},delete:(e,t)=>E(k(n().delete(e)).then(()=>{}),t),exists:e=>k(n().getKey(e)).then(e=>void 0!==e),query(e,t){const r=n().openCursor(),o=[];return E(new Promise((t,n)=>{r.onsuccess=()=>{const n=r.result;n?(e(n.value)&&o.push(n.value),n.continue()):t(o)},r.onerror=()=>{var e;return n(null!=(e=r.error)?e:new DOMException("Unknown error"))}}),t)},upsert:(e,t)=>E(k(n().put(e)),t),bulkInsert(e,t){const r=n(),o=[];if(0===e.length)return E(Promise.resolve(o),t);let i=0;return E(new Promise((t,n)=>{e.forEach((s,c)=>{const u=r.add(s);u.onsuccess=()=>{o[c]=u.result,i++,i===e.length&&t(o)},u.onerror=()=>{var e;return n(null!=(e=u.error)?e:new DOMException("Unknown error"))}})}),t)},clear:e=>E(k(n().clear()).then(()=>{}),e),count:e=>E(k(n().count()),null!=e?e:{})}}(e,t)}(i,e)},c=new Promise((e,t)=>{i.oncomplete=()=>e(),i.onerror=()=>{var e;return t(null!=(e=i.error)?e:new DOMException("Transaction failed"))}}),u=r(s);return function(e,t){return t?e.then(()=>null==t.onSuccess?void 0:t.onSuccess()).catch(e=>{throw null==t.onError||t.onError(e),e}):e}(Promise.resolve(u).then(()=>c),o)}}}(t);i(n),a(!0)}).catch(t=>{e||c(t)}),()=>{e=!0}},[t.name,t.version]),{db:r,isReady:u,error:s}}const I=/\b(?:25[0-5]|2[0-4]\d|1?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|1?\d{1,2})){3}\b/g,T=["stun:stun.l.google.com:19302"],A=3e3;function O(t={}){const{stunServers:r=T,timeout:i=A,onDetect:s}=t,[c,u]=e([]),[a,l]=e(!0),[d,f]=e(null),p=o(null),m=o(null),g=o(new Set),y=o(s);return y.current=s,n(()=>{if("undefined"==typeof window)return l(!1),void f("WebRTC IP detection is not available during SSR");if("undefined"==typeof RTCPeerConnection)return l(!1),void f("RTCPeerConnection is not available");const e=new Set;g.current=e;const t=()=>{m.current&&(clearTimeout(m.current),m.current=null),p.current&&(p.current.close(),p.current=null),l(!1)},n=t=>{e.has(t)||(e.add(t),u(e=>[...e,t]),null==y.current||y.current(t))};try{const e=new RTCPeerConnection({iceServers:[{urls:r}]});p.current=e,e.onicecandidate=e=>{const t=e.candidate;t&&t.candidate&&function(e){const t=e.match(I);return t?[...t]:[]}(t.candidate).forEach(n)},e.createDataChannel(""),e.createOffer().then(t=>e.setLocalDescription(t)).catch(e=>{f(e instanceof Error?e.message:"Failed to create offer"),t()}),m.current=setTimeout(()=>t(),i)}catch(e){f(e instanceof Error?e.message:"WebRTC setup failed"),t()}return()=>{t()}},[r.join(","),i]),{ips:c,loading:a,error:d}}function M(r){const{wasmUrl:i,exportName:s="compute",workerUrl:c,importObject:u}=r,[a,l]=e(void 0),[d,f]=e(!0),[p,m]=e(null),[g,y]=e(!1),v=o(null),w=o(null),h=o(null);return n(()=>{if("undefined"==typeof window)return m("useWasmCompute is not available during SSR"),void f(!1);if("undefined"==typeof Worker)return m("Worker is not supported in this environment"),void f(!1);if("undefined"==typeof WebAssembly||"function"!=typeof WebAssembly.instantiate)return m("WebAssembly is not supported in this environment"),void f(!1);m(null),y(!1);const e=function(e){if(e)return new Worker(e);const t=new Blob(["\nself.onmessage = async (e) => {\n const d = e.data;\n if (d.type === 'init') {\n try {\n const res = await fetch(d.wasmUrl);\n const buf = await res.arrayBuffer();\n const mod = await WebAssembly.instantiate(buf, d.importObject || {});\n self.wasmInstance = mod.instance;\n self.exportName = d.exportName || 'compute';\n self.postMessage({ type: 'ready' });\n } catch (err) {\n self.postMessage({ type: 'error', error: (err && err.message) || String(err) });\n }\n return;\n }\n if (d.type === 'compute') {\n try {\n const fn = self.wasmInstance.exports[self.exportName];\n if (typeof fn !== 'function') {\n self.postMessage({ type: 'error', error: 'Export \"' + self.exportName + '\" is not a function' });\n return;\n }\n const result = fn(d.input);\n self.postMessage({ type: 'result', result: result });\n } catch (err) {\n self.postMessage({ type: 'error', error: (err && err.message) || String(err) });\n }\n }\n};\n"],{type:"application/javascript"}),n=URL.createObjectURL(t),r=new Worker(n);return URL.revokeObjectURL(n),r}(c);v.current=e;const t=e=>{var t;const{type:n,result:r,error:o}=null!=(t=e.data)?t:{};return"ready"===n?(y(!0),void f(!1)):"error"===n?(m(null!=o?o:"Unknown error"),f(!1),void(h.current&&(h.current(new Error(o)),w.current=null,h.current=null))):void("result"===n&&(l(r),f(!1),w.current&&(w.current(r),w.current=null,h.current=null)))};return e.addEventListener("message",t),e.postMessage({type:"init",wasmUrl:i,exportName:s,importObject:null!=u?u:{}}),()=>{e.removeEventListener("message",t),e.terminate(),v.current=null,h.current&&(h.current(new Error("Worker terminated")),w.current=null,h.current=null)}},[i,s,c,u]),{compute:t(e=>new Promise((t,n)=>{v.current&&g?p?n(new Error(p)):(w.current=t,h.current=n,f(!0),v.current.postMessage({type:"compute",input:e})):n(new Error("WASM not ready"))}),[g,p]),result:a,loading:d,error:p,ready:g}}function L(t,i={}){const{maxHistory:s=100,throughputWindowMs:c=1e3}=i,[u,a]=e([]),[l,d]=e(0),[f,p]=e(0),[m,g]=e([]),[y,v]=e(0),w=o([]),h=o(0),b=o(0);n(()=>{if(!t)return;const e=e=>{const t=function(e){if(null==e||"object"!=typeof e)return null;const t=e.type;return"task_start"!==t&&"task_end"!==t&&"task_fail"!==t&&"queue_size"!==t?null:{type:t,taskId:"string"==typeof e.taskId?e.taskId:void 0,duration:"number"==typeof e.duration?e.duration:void 0,error:"string"==typeof e.error?e.error:void 0,size:"number"==typeof e.size?e.size:void 0,timestamp:Date.now()}}(e.data);if(t)if(g(e=>[...e,t].slice(-s)),"task_start"===t.type&&t.taskId)a(e=>e.includes(t.taskId)?e:[...e,t.taskId]);else if("task_end"===t.type){t.taskId&&a(e=>e.filter(e=>e!==t.taskId)),d(e=>e+1);const e=Date.now()-c;w.current=[...w.current.filter(t=>t>=e),t.timestamp],"number"==typeof t.duration&&(h.current+=t.duration,b.current+=1)}else"task_fail"===t.type?(t.taskId&&a(e=>e.filter(e=>e!==t.taskId)),p(e=>e+1)):"queue_size"===t.type&&"number"==typeof t.size&&v(t.size)};return t.addEventListener("message",e),()=>t.removeEventListener("message",e)},[t,s]);const S=r(()=>{const e=b.current;return e>0?h.current/e:0},[m]),k=r(()=>{const e=Date.now()-c;return w.current.filter(t=>t>=e).length/(c/1e3)},[m,c]),E=r(()=>({runningTasks:u,completedCount:l,failedCount:f,averageDurationMs:S,throughputPerSecond:k,currentQueueSize:y,totalProcessed:l+f,recentEventCount:m.length}),[u,l,f,S,k,y,m.length]);return{runningTasks:u,completedCount:l,failedCount:f,eventHistory:m,averageDurationMs:S,throughputPerSecond:k,currentQueueSize:y,progress:E}}function C(e,t){if(null==e)return"";const n=("string"==typeof e?e:String(e)).trim();return n.length>t?n.slice(0,t):n}function j(e){if(!Array.isArray(e))return[];const t=[];for(let n=0;n<e.length&&t.length<50;n++){const r=C(e[n],100);r&&t.push(r)}return t}function D(e){const t=C(e,2048);if(!t)return"";try{const e=new URL(t);if("http:"===e.protocol||"https:"===e.protocol)return t}catch(e){}return""}function P(e){try{if("undefined"==typeof window)return!1;const t=window.getComputedStyle(e);return"none"!==t.display&&"hidden"!==t.visibility&&"0"!==t.opacity}catch(e){return!1}}function N(e){const t={};try{for(const n of Object.keys(e).slice(0,20)){const r=C(n,50);if(!r)continue;const o=e[n];"string"==typeof o?t[r]=o.slice(0,500):"number"==typeof o&&Number.isFinite(o)||"boolean"==typeof o?t[r]=o:Array.isArray(o)&&(t[r]=o.map(e=>C(e,200)).filter(Boolean).slice(0,20))}}catch(e){}return t}function U(){try{if("undefined"==typeof document||!document.querySelectorAll)return;document.querySelectorAll('script[data-llm="true"]').forEach(e=>e.remove())}catch(e){}}function R(e){const t=c(null);u(()=>{try{if("undefined"==typeof window)return;const n=function(e){return null==e||"object"!=typeof e?{route:"/"}:{route:C(e.route,2048)||"/",mode:"auto-extract"===e.mode?"auto-extract":"manual",title:void 0!==e.title?C(e.title,200):void 0,description:void 0!==e.description?C(e.description,2e3):void 0,tags:void 0!==e.tags?j(e.tags):void 0,canonicalUrl:void 0!==e.canonicalUrl?D(e.canonicalUrl):void 0,language:void 0!==e.language?C(e.language,20):void 0,ogType:void 0!==e.ogType?C(e.ogType,50):void 0,ogImage:void 0!==e.ogImage?D(e.ogImage):void 0,ogImageAlt:void 0!==e.ogImageAlt?C(e.ogImageAlt,200):void 0,siteName:void 0!==e.siteName?C(e.siteName,100):void 0,author:void 0!==e.author?C(e.author,200):void 0,publishedTime:void 0!==e.publishedTime?C(e.publishedTime,50):void 0,modifiedTime:void 0!==e.modifiedTime?C(e.modifiedTime,50):void 0,robots:void 0!==e.robots?C(e.robots,100):void 0,extra:void 0===e.extra||"object"!=typeof e.extra||Array.isArray(e.extra)?void 0:N(e.extra)}}(e),r=function(e){try{const r=(new Date).toISOString(),o={route:e.route,generatedAt:r};if("auto-extract"===e.mode){var t,n;const r=function(){const e={title:"",outline:[],description:""};try{if("undefined"==typeof document)return e;const t=C(document.title,200),n=[];let r="";try{const e=document.querySelectorAll("nav, footer, [role='navigation'], [role='contentinfo'], script, style, noscript"),t=t=>{for(const n of e)if(n.contains(t))return!0;return!1},o=document.querySelectorAll("h1, h2");for(const e of o){if(n.length>=50)break;if(P(e)&&!t(e)){const t=C(e.textContent,300);t&&n.push(t)}}const i=document.querySelectorAll("p"),s=[];for(const e of i){if(s.length>=3)break;if(P(e)&&!t(e)){const t=C(e.textContent,1e3);t&&s.push(t)}}r=s.join(" ").trim().slice(0,2e3)||""}catch(e){}return{title:t,outline:n,description:r}}catch(t){return e}}();o.title=(null!=(t=e.title)?t:r.title)||void 0,o.description=(null!=(n=e.description)?n:r.description)||void 0,r.outline.length>0&&(o.outline=r.outline)}else void 0!==e.title&&""!==e.title&&(o.title=e.title),void 0!==e.description&&""!==e.description&&(o.description=e.description);return e.tags&&e.tags.length>0&&(o.tags=e.tags),e.canonicalUrl&&(o.canonicalUrl=e.canonicalUrl),e.language&&(o.language=e.language),e.ogType&&(o.ogType=e.ogType),e.ogImage&&(o.ogImage=e.ogImage),e.ogImageAlt&&(o.ogImageAlt=e.ogImageAlt),e.siteName&&(o.siteName=e.siteName),e.author&&(o.author=e.author),e.publishedTime&&(o.publishedTime=e.publishedTime),e.modifiedTime&&(o.modifiedTime=e.modifiedTime),e.robots&&(o.robots=e.robots),e.extra&&Object.keys(e.extra).length>0&&(o.extra=e.extra),o}catch(t){var r;return{route:null!=(r=null==e?void 0:e.route)?r:"/",generatedAt:(new Date).toISOString()}}}(n),o=JSON.stringify(r);if(t.current===o)return;return t.current=o,U(),function(e){try{if("undefined"==typeof document||!document.head)return;const t=document.createElement("script");t.type="application/llm+json",t.setAttribute("data-llm","true"),t.textContent=JSON.stringify(e),document.head.appendChild(t)}catch(e){}}(r),()=>{U(),t.current=null}}catch(e){}},[null==e?void 0:e.route,null==e?void 0:e.mode,null==e?void 0:e.title,null==e?void 0:e.description,null==e?void 0:e.tags,null==e?void 0:e.canonicalUrl,null==e?void 0:e.language,null==e?void 0:e.ogType,null==e?void 0:e.ogImage,null==e?void 0:e.ogImageAlt,null==e?void 0:e.siteName,null==e?void 0:e.author,null==e?void 0:e.publishedTime,null==e?void 0:e.modifiedTime,null==e?void 0:e.robots])}const q="use-ref-print-target",W="use-ref-print-styles",z=`\n@media print {\n body * {\n visibility: hidden;\n }\n .${q},\n .${q} * {\n visibility: visible;\n }\n .${q} {\n position: absolute !important;\n left: 0 !important;\n top: 0 !important;\n width: 100% !important;\n }\n}\n`;function _(e,n={}){const{documentTitle:r}=n,i=o("");return{print:t(()=>{const t=e.current;if(!t||"undefined"==typeof window||!window.print)return;let n=document.getElementById(W);n||(n=document.createElement("style"),n.id=W,n.textContent=z,document.head.appendChild(n)),t.classList.add(q),r&&(i.current=document.title,document.title=r);const o=()=>{t.classList.remove(q),r&&void 0!==i.current&&(document.title=i.current)};if("onafterprint"in window){const e=()=>{o(),window.removeEventListener("afterprint",e)};window.addEventListener("afterprint",e)}window.print(),setTimeout(o,1e3)},[e,r])}}function J(e,t){if("undefined"==typeof window)return null;const n="localStorage"===e?window.localStorage:window.sessionStorage;try{const e=n.getItem(t);if(null==e)return null;const r=JSON.parse(e);return r&&"object"==typeof r?r:null}catch(e){return null}}function B(e,t){if("undefined"==typeof window)return[];const n="localStorage"===e?window.localStorage:window.sessionStorage;try{const e=n.getItem(t);if(null==e)return[];const r=JSON.parse(e);return Array.isArray(r)?r.filter(e=>"string"==typeof e):[]}catch(e){return[]}}function F(e,t){return t.filter(t=>t.condition(e)).map(e=>e.role)}function $(e,t){const n=new Set;for(const r of e){const e=t[r];if(Array.isArray(e))for(const t of e)n.add(t)}return Array.from(n)}function K(r){const{userSource:i}=r,s=o(r);s.current=r;const[c,u]=e(null),[a,l]=e([]),[d,f]=e([]),[p,m]=e(!1),[g,y]=e(null),v=t(async()=>{const{userSource:e,roleDefinitions:t,roleCapabilities:n,capabilitiesOverride:r}=s.current;y(null);let o=null,i=[],c=[];try{if("localStorage"===e.type)o=J("localStorage",e.key),i=F(o,t),c=$(i,n);else if("sessionStorage"===e.type)o=J("sessionStorage",e.key),i=F(o,t),c=$(i,n);else if("api"===e.type)o=await e.fetch(),i=F(o,t),c=$(i,n);else if("memory"===e.type)o=e.getUser(),i=F(o,t),c=$(i,n);else if("custom"===e.type){var a,d,p;const r=await Promise.resolve(e.getAuth());o=null!=(a=r.user)?a:null,i=null!=(d=r.roles)?d:F(o,t),c=null!=(p=r.capabilities)?p:$(i,n)}if(r)if("localStorage"===r.type){const e=B("localStorage",r.key);e.length>0&&(c=e)}else if("sessionStorage"===r.type){const e=B("sessionStorage",r.key);e.length>0&&(c=e)}else"api"===r.type&&(c=await r.fetch());u(o),l(i),f(c)}catch(e){y(e instanceof Error?e:new Error(String(e))),u(null),l([]),f([])}finally{m(!0)}},[]);n(()=>{v()},[v]),n(()=>{if("undefined"==typeof window)return;const e="localStorage"===i.type||"sessionStorage"===i.type?i.key:null;if(!e)return;const t=t=>{t.key===e&&v()};return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)},[i.type,"localStorage"===i.type||"sessionStorage"===i.type?i.key:"",v]);const w=t(e=>a.includes(e),[a]),h=t(e=>function(e,t){return!!e.includes("*")||e.includes(t)}(d,e),[d]),b=h,S=t(()=>v(),[v]),k=t((e,t,n)=>{if("undefined"==typeof window)return;const r="localStorage"===t?window.localStorage:window.sessionStorage;null==e?r.removeItem(n):r.setItem(n,JSON.stringify(e)),("localStorage"===i.type&&"localStorage"===t&&i.key===n||"sessionStorage"===i.type&&"sessionStorage"===t&&i.key===n)&&v()},[i,v]),E=t((e,t,n)=>{"undefined"!=typeof window&&("localStorage"===t?window.localStorage:window.sessionStorage).setItem(n,JSON.stringify(e))},[]),x=t((e,t,n)=>{"undefined"!=typeof window&&("localStorage"===t?window.localStorage:window.sessionStorage).setItem(n,JSON.stringify(e))},[]);return{user:c,roles:a,capabilities:d,isReady:p,error:g,hasRole:w,hasCapability:h,can:b,refetch:S,setUserInStorage:k,setRolesInStorage:E,setCapabilitiesInStorage:x}}export{w as useClipboard,f as useEventBus,x as useIndexedDB,R as useLLMMetadata,l as useMutationObserver,v as useNetworkState,g as usePreferredTheme,K as useRBAC,h as useRageClick,_ as useRefPrint,b as useThreadedWorker,a as useTransition,M as useWasmCompute,O as useWebRTCIP,L as useWorkerNotifications,m as useWrappedChildren};
|
|
1
|
+
import{useState as e,useCallback as t,useEffect as n,useMemo as r,useRef as o}from"preact/hooks";import{isValidElement as i,cloneElement as s}from"preact";import{useRef as c,useEffect as u}from"preact/compat";function a(){const[n,r]=e(!1);return[t(e=>{r(!0),Promise.resolve().then(()=>{e(),r(!1)})},[]),n]}function l(e,t,r){n(()=>{const n=e.current;if(!n)return;const o=new MutationObserver(t);return o.observe(n,r),()=>o.disconnect()},[e,t,r])}const d=new Map;function f(){return{emit:t((e,...t)=>{const n=d.get(e);n&&n.forEach(e=>e(...t))},[]),on:t((e,t)=>{let n=d.get(e);return n||(n=new Set,d.set(e,n)),n.add(t),()=>{n.delete(t),0===n.size&&d.delete(e)}},[])}}function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},p.apply(null,arguments)}function m(e,t,n="preserve"){return r(()=>{if(!e)return e;const r=e=>{if(!i(e))return e;const r=e.props||{};let o;o="override"===n?p({},r,t):p({},t,r);const c=null==r?void 0:r.style,u=null==t?void 0:t.style;return c&&u&&"object"==typeof c&&"object"==typeof u&&(o.style="override"===n?p({},c,u):p({},u,c)),s(e,o)};return Array.isArray(e)?e.map(r):r(e)},[e,t,n])}function g(){const[t,r]=e(()=>{if("undefined"==typeof window)return"no-preference";const e=window.matchMedia("(prefers-color-scheme: dark)"),t=window.matchMedia("(prefers-color-scheme: light)");return e.matches?"dark":t.matches?"light":"no-preference"});return n(()=>{if("undefined"==typeof window)return;const e=window.matchMedia("(prefers-color-scheme: dark)"),t=e=>{r(e.matches?"dark":"light")},n=()=>{const e=window.matchMedia("(prefers-color-scheme: dark)"),t=window.matchMedia("(prefers-color-scheme: light)");r(e.matches?"dark":t.matches?"light":"no-preference")};e.addEventListener("change",t);const o=window.matchMedia("(prefers-color-scheme: light)");return o.addEventListener("change",n),()=>{e.removeEventListener("change",t),o.removeEventListener("change",n)}},[]),t}function y(){if("undefined"==typeof navigator)return{online:!0};const e={online:navigator.onLine},t=navigator.connection;return t&&(void 0!==t.effectiveType&&(e.effectiveType=t.effectiveType),void 0!==t.downlink&&(e.downlink=t.downlink),void 0!==t.rtt&&(e.rtt=t.rtt),void 0!==t.saveData&&(e.saveData=t.saveData),void 0!==t.type&&(e.connectionType=t.type)),e}function v(){const[t,r]=e(y);return n(()=>{if("undefined"==typeof window)return;const e=()=>r(y());window.addEventListener("online",e),window.addEventListener("offline",e);const t=navigator.connection;return null!=t&&t.addEventListener&&t.addEventListener("change",e),()=>{window.removeEventListener("online",e),window.removeEventListener("offline",e),null!=t&&t.removeEventListener&&t.removeEventListener("change",e)}},[]),t}function h(n={}){const{resetDelay:r=2e3}=n,[o,i]=e(!1),[s,c]=e(null),u=t(()=>{i(!1),c(null)},[]);return{copy:t(async e=>{if(c(null),"undefined"==typeof navigator||!navigator.clipboard){const e=new Error("Clipboard API is not available");return c(e),!1}try{return await navigator.clipboard.writeText(e),i(!0),r>0&&setTimeout(()=>i(!1),r),!0}catch(e){const t=e instanceof Error?e:new Error(String(e));return c(t),!1}},[r]),paste:t(async()=>{if(c(null),"undefined"==typeof navigator||!navigator.clipboard){const e=new Error("Clipboard API is not available");return c(e),""}try{return await navigator.clipboard.readText()}catch(e){const t=e instanceof Error?e:new Error(String(e));return c(t),""}},[]),copied:o,error:s,reset:u}}function w(e,t){const{onRageClick:r,threshold:i=5,timeWindow:s=1e3,distanceThreshold:c=30}=t,u=o(r);u.current=r;const a=o([]);n(()=>{const t=e.current;if(!t)return;const n=e=>{const t=Date.now(),n={time:t,x:e.clientX,y:e.clientY},r=t-s,o=a.current.filter(e=>e.time>=r);if(o.push(n),Infinity!==c){const t=o.filter(e=>{return t=e,r=n,Math.hypot(r.x-t.x,r.y-t.y)<=c;var t,r});if(t.length>=i)return u.current({count:t.length,event:e}),void(a.current=[])}else if(o.length>=i)return u.current({count:o.length,event:e}),void(a.current=[]);a.current=o};return t.addEventListener("click",n),()=>t.removeEventListener("click",n)},[e,i,s,c])}function b(r,i){const{mode:s,concurrency:c=4}=i,u="sequential"===s?1:Math.max(1,c),[a,l]=e(!1),[d,f]=e(void 0),[p,m]=e(void 0),[g,y]=e(0),v=o([]),h=o(0),w=o(0),b=o(!1),S=o(r);S.current=r;const k=t(()=>{y(v.current.length+w.current)},[]),E=t(()=>{if(b.current)return;if(w.current>=u)return;if(0===v.current.length)return 0===w.current&&l(!1),void k();v.current.sort((e,t)=>e.priority!==t.priority?e.priority-t.priority:e.sequence-t.sequence);const e=v.current.shift();w.current+=1,l(!0),k(),(0,S.current)(e.data).then(t=>{f(t),m(void 0),e.resolve(t)}).catch(t=>{m(t),e.reject(t)}).finally(()=>{w.current-=1,k(),E()}),v.current.length>0&&w.current<u&&E()},[u,k]),x=t((e,t)=>{var n;if(b.current)return Promise.reject(new Error("Worker is terminated"));const r=null!=(n=null==t?void 0:t.priority)?n:1,o=++h.current,i=new Promise((t,n)=>{v.current.push({data:e,priority:r,sequence:o,resolve:t,reject:n})});return k(),l(!0),queueMicrotask(E),i},[E,k]),I=t(()=>{const e=v.current;v.current=[],e.forEach(e=>e.reject(new Error("Task cleared from queue"))),k(),0===w.current&&l(!1)},[k]),T=t(()=>{b.current=!0,I()},[I]);return n(()=>()=>{b.current=!0},[]),{run:x,loading:a,result:d,error:p,queueSize:g,clearQueue:I,terminate:T}}const S=new Map;function k(e){return new Promise((t,n)=>{e.onsuccess=()=>t(e.result),e.onerror=()=>{var t;return n(null!=(t=e.error)?t:new DOMException("Unknown IndexedDB error"))}})}function E(e,t){return t?e.then(e=>(null==t.onSuccess||t.onSuccess(e),e)).catch(e=>{throw null==t.onError||t.onError(e),e}):e}function x(t){const[r,i]=e(null),[s,c]=e(null),[u,a]=e(!1),l=o(t);return l.current=t,n(()=>{let e=!1;c(null),a(!1),i(null);const{name:t,version:n,tables:r}=l.current;return function(e){const t=`${e.name}_v${e.version}`;let n=S.get(t);return n||(n=function(e){return new Promise((t,n)=>{const r=indexedDB.open(e.name,e.version);r.onerror=()=>{var e;return n(null!=(e=r.error)?e:new DOMException("Failed to open database"))},r.onsuccess=()=>t(r.result),r.onupgradeneeded=t=>{const n=t.target.result,r=e.tables;for(const e of Object.keys(r)){const t=r[e];if(!n.objectStoreNames.contains(e)){var o;const r=n.createObjectStore(e,{keyPath:t.keyPath,autoIncrement:null!=(o=t.autoIncrement)&&o});if(t.indexes)for(const e of t.indexes)r.createIndex(e,e,{unique:!1})}}}})}(e),S.set(t,n),n)}({name:t,version:n,tables:r}).then(t=>{if(e)return void t.close();const n=function(e){return{get db(){return e},hasTable:t=>e.objectStoreNames.contains(t),table:t=>function(e,t){return function(e,t){function n(n){return e.transaction([t],n).objectStore(t)}return{insert:(e,t)=>E(k(n("readwrite").add(e)),t),update(e,t,r){const o=n("readwrite");return E(k(o.get(e)).then(e=>{if(void 0===e)throw new DOMException("Key not found","NotFoundError");const n=p({},e,t);return k(o.put(n))}).then(()=>{}),r)},delete:(e,t)=>E(k(n("readwrite").delete(e)).then(()=>{}),t),exists:e=>k(n("readonly").getKey(e)).then(e=>void 0!==e),query(e,t){const r=n("readonly").openCursor(),o=[];return E(new Promise((t,n)=>{r.onsuccess=()=>{const n=r.result;n?(e(n.value)&&o.push(n.value),n.continue()):t(o)},r.onerror=()=>{var e;return n(null!=(e=r.error)?e:new DOMException("Unknown error"))}}),t)},upsert:(e,t)=>E(k(n("readwrite").put(e)),t),bulkInsert(e,t){const r=n("readwrite"),o=[];if(0===e.length)return E(Promise.resolve(o),t);let i=0;return E(new Promise((t,n)=>{e.forEach((s,c)=>{const u=r.add(s);u.onsuccess=()=>{o[c]=u.result,i++,i===e.length&&t(o)},u.onerror=()=>{var e;return n(null!=(e=u.error)?e:new DOMException("Unknown error"))}})}),t)},clear:e=>E(k(n("readwrite").clear()).then(()=>{}),e),count:e=>E(k(n("readonly").count()),null!=e?e:{})}}(e,t)}(e,t),transaction(t,n,r,o){const i=e.transaction(t,n),s={table:e=>function(e,t){return function(e,t){function n(){return e.objectStore(t)}return{insert:(e,t)=>E(k(n().add(e)),t),update(e,t,r){const o=n();return E(k(o.get(e)).then(e=>{if(void 0===e)throw new DOMException("Key not found","NotFoundError");const n=p({},e,t);return k(o.put(n))}).then(()=>{}),r)},delete:(e,t)=>E(k(n().delete(e)).then(()=>{}),t),exists:e=>k(n().getKey(e)).then(e=>void 0!==e),query(e,t){const r=n().openCursor(),o=[];return E(new Promise((t,n)=>{r.onsuccess=()=>{const n=r.result;n?(e(n.value)&&o.push(n.value),n.continue()):t(o)},r.onerror=()=>{var e;return n(null!=(e=r.error)?e:new DOMException("Unknown error"))}}),t)},upsert:(e,t)=>E(k(n().put(e)),t),bulkInsert(e,t){const r=n(),o=[];if(0===e.length)return E(Promise.resolve(o),t);let i=0;return E(new Promise((t,n)=>{e.forEach((s,c)=>{const u=r.add(s);u.onsuccess=()=>{o[c]=u.result,i++,i===e.length&&t(o)},u.onerror=()=>{var e;return n(null!=(e=u.error)?e:new DOMException("Unknown error"))}})}),t)},clear:e=>E(k(n().clear()).then(()=>{}),e),count:e=>E(k(n().count()),null!=e?e:{})}}(e,t)}(i,e)},c=new Promise((e,t)=>{i.oncomplete=()=>e(),i.onerror=()=>{var e;return t(null!=(e=i.error)?e:new DOMException("Transaction failed"))}}),u=r(s);return function(e,t){return t?e.then(()=>null==t.onSuccess?void 0:t.onSuccess()).catch(e=>{throw null==t.onError||t.onError(e),e}):e}(Promise.resolve(u).then(()=>c),o)}}}(t);i(n),a(!0)}).catch(t=>{e||c(t)}),()=>{e=!0}},[t.name,t.version]),{db:r,isReady:u,error:s}}const I=/\b(?:25[0-5]|2[0-4]\d|1?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|1?\d{1,2})){3}\b/g,T=["stun:stun.l.google.com:19302"],A=3e3;function O(t={}){const{stunServers:r=T,timeout:i=A,onDetect:s}=t,[c,u]=e([]),[a,l]=e(!0),[d,f]=e(null),p=o(null),m=o(null),g=o(new Set),y=o(s);return y.current=s,n(()=>{if("undefined"==typeof window)return l(!1),void f("WebRTC IP detection is not available during SSR");if("undefined"==typeof RTCPeerConnection)return l(!1),void f("RTCPeerConnection is not available");const e=new Set;g.current=e;const t=()=>{m.current&&(clearTimeout(m.current),m.current=null),p.current&&(p.current.close(),p.current=null),l(!1)},n=t=>{e.has(t)||(e.add(t),u(e=>[...e,t]),null==y.current||y.current(t))};try{const e=new RTCPeerConnection({iceServers:[{urls:r}]});p.current=e,e.onicecandidate=e=>{const t=e.candidate;t&&t.candidate&&function(e){const t=e.match(I);return t?[...t]:[]}(t.candidate).forEach(n)},e.createDataChannel(""),e.createOffer().then(t=>e.setLocalDescription(t)).catch(e=>{f(e instanceof Error?e.message:"Failed to create offer"),t()}),m.current=setTimeout(()=>t(),i)}catch(e){f(e instanceof Error?e.message:"WebRTC setup failed"),t()}return()=>{t()}},[r.join(","),i]),{ips:c,loading:a,error:d}}function M(r){const{wasmUrl:i,exportName:s="compute",workerUrl:c,importObject:u}=r,[a,l]=e(void 0),[d,f]=e(!0),[p,m]=e(null),[g,y]=e(!1),v=o(null),h=o(null),w=o(null);return n(()=>{if("undefined"==typeof window)return m("useWasmCompute is not available during SSR"),void f(!1);if("undefined"==typeof Worker)return m("Worker is not supported in this environment"),void f(!1);if("undefined"==typeof WebAssembly||"function"!=typeof WebAssembly.instantiate)return m("WebAssembly is not supported in this environment"),void f(!1);m(null),y(!1);const e=function(e){if(e)return new Worker(e);const t=new Blob(["\nself.onmessage = async (e) => {\n const d = e.data;\n if (d.type === 'init') {\n try {\n const res = await fetch(d.wasmUrl);\n const buf = await res.arrayBuffer();\n const mod = await WebAssembly.instantiate(buf, d.importObject || {});\n self.wasmInstance = mod.instance;\n self.exportName = d.exportName || 'compute';\n self.postMessage({ type: 'ready' });\n } catch (err) {\n self.postMessage({ type: 'error', error: (err && err.message) || String(err) });\n }\n return;\n }\n if (d.type === 'compute') {\n try {\n const fn = self.wasmInstance.exports[self.exportName];\n if (typeof fn !== 'function') {\n self.postMessage({ type: 'error', error: 'Export \"' + self.exportName + '\" is not a function' });\n return;\n }\n const result = fn(d.input);\n self.postMessage({ type: 'result', result: result });\n } catch (err) {\n self.postMessage({ type: 'error', error: (err && err.message) || String(err) });\n }\n }\n};\n"],{type:"application/javascript"}),n=URL.createObjectURL(t),r=new Worker(n);return URL.revokeObjectURL(n),r}(c);v.current=e;const t=e=>{var t;const{type:n,result:r,error:o}=null!=(t=e.data)?t:{};return"ready"===n?(y(!0),void f(!1)):"error"===n?(m(null!=o?o:"Unknown error"),f(!1),void(w.current&&(w.current(new Error(o)),h.current=null,w.current=null))):void("result"===n&&(l(r),f(!1),h.current&&(h.current(r),h.current=null,w.current=null)))};return e.addEventListener("message",t),e.postMessage({type:"init",wasmUrl:i,exportName:s,importObject:null!=u?u:{}}),()=>{e.removeEventListener("message",t),e.terminate(),v.current=null,w.current&&(w.current(new Error("Worker terminated")),h.current=null,w.current=null)}},[i,s,c,u]),{compute:t(e=>new Promise((t,n)=>{v.current&&g?p?n(new Error(p)):(h.current=t,w.current=n,f(!0),v.current.postMessage({type:"compute",input:e})):n(new Error("WASM not ready"))}),[g,p]),result:a,loading:d,error:p,ready:g}}function C(t,i={}){const{maxHistory:s=100,throughputWindowMs:c=1e3}=i,[u,a]=e([]),[l,d]=e(0),[f,p]=e(0),[m,g]=e([]),[y,v]=e(0),h=o([]),w=o(0),b=o(0);n(()=>{if(!t)return;const e=e=>{const t=function(e){if(null==e||"object"!=typeof e)return null;const t=e.type;return"task_start"!==t&&"task_end"!==t&&"task_fail"!==t&&"queue_size"!==t?null:{type:t,taskId:"string"==typeof e.taskId?e.taskId:void 0,duration:"number"==typeof e.duration?e.duration:void 0,error:"string"==typeof e.error?e.error:void 0,size:"number"==typeof e.size?e.size:void 0,timestamp:Date.now()}}(e.data);if(t)if(g(e=>[...e,t].slice(-s)),"task_start"===t.type&&t.taskId)a(e=>e.includes(t.taskId)?e:[...e,t.taskId]);else if("task_end"===t.type){t.taskId&&a(e=>e.filter(e=>e!==t.taskId)),d(e=>e+1);const e=Date.now()-c;h.current=[...h.current.filter(t=>t>=e),t.timestamp],"number"==typeof t.duration&&(w.current+=t.duration,b.current+=1)}else"task_fail"===t.type?(t.taskId&&a(e=>e.filter(e=>e!==t.taskId)),p(e=>e+1)):"queue_size"===t.type&&"number"==typeof t.size&&v(t.size)};return t.addEventListener("message",e),()=>t.removeEventListener("message",e)},[t,s]);const S=r(()=>{const e=b.current;return e>0?w.current/e:0},[m]),k=r(()=>{const e=Date.now()-c;return h.current.filter(t=>t>=e).length/(c/1e3)},[m,c]),E=r(()=>({runningTasks:u,completedCount:l,failedCount:f,averageDurationMs:S,throughputPerSecond:k,currentQueueSize:y,totalProcessed:l+f,recentEventCount:m.length}),[u,l,f,S,k,y,m.length]);return{runningTasks:u,completedCount:l,failedCount:f,eventHistory:m,averageDurationMs:S,throughputPerSecond:k,currentQueueSize:y,progress:E}}function L(e,t){if(null==e)return"";const n=("string"==typeof e?e:String(e)).trim();return n.length>t?n.slice(0,t):n}function j(e){if(!Array.isArray(e))return[];const t=[];for(let n=0;n<e.length&&t.length<50;n++){const r=L(e[n],100);r&&t.push(r)}return t}function P(e){const t=L(e,2048);if(!t)return"";try{const e=new URL(t);if("http:"===e.protocol||"https:"===e.protocol)return t}catch(e){}return""}function D(e){try{if("undefined"==typeof window)return!1;const t=window.getComputedStyle(e);return"none"!==t.display&&"hidden"!==t.visibility&&"0"!==t.opacity}catch(e){return!1}}function N(e){const t={};try{for(const n of Object.keys(e).slice(0,20)){const r=L(n,50);if(!r)continue;const o=e[n];"string"==typeof o?t[r]=o.slice(0,500):"number"==typeof o&&Number.isFinite(o)||"boolean"==typeof o?t[r]=o:Array.isArray(o)&&(t[r]=o.map(e=>L(e,200)).filter(Boolean).slice(0,20))}}catch(e){}return t}function U(){try{if("undefined"==typeof document||!document.querySelectorAll)return;document.querySelectorAll('script[data-llm="true"]').forEach(e=>e.remove())}catch(e){}}function q(e){const t=c(null);u(()=>{try{if("undefined"==typeof window)return;const n=function(e){return null==e||"object"!=typeof e?{route:"/"}:{route:L(e.route,2048)||"/",mode:"auto-extract"===e.mode?"auto-extract":"manual",title:void 0!==e.title?L(e.title,200):void 0,description:void 0!==e.description?L(e.description,2e3):void 0,tags:void 0!==e.tags?j(e.tags):void 0,canonicalUrl:void 0!==e.canonicalUrl?P(e.canonicalUrl):void 0,language:void 0!==e.language?L(e.language,20):void 0,ogType:void 0!==e.ogType?L(e.ogType,50):void 0,ogImage:void 0!==e.ogImage?P(e.ogImage):void 0,ogImageAlt:void 0!==e.ogImageAlt?L(e.ogImageAlt,200):void 0,siteName:void 0!==e.siteName?L(e.siteName,100):void 0,author:void 0!==e.author?L(e.author,200):void 0,publishedTime:void 0!==e.publishedTime?L(e.publishedTime,50):void 0,modifiedTime:void 0!==e.modifiedTime?L(e.modifiedTime,50):void 0,robots:void 0!==e.robots?L(e.robots,100):void 0,extra:void 0===e.extra||"object"!=typeof e.extra||Array.isArray(e.extra)?void 0:N(e.extra)}}(e),r=function(e){try{const r=(new Date).toISOString(),o={route:e.route,generatedAt:r};if("auto-extract"===e.mode){var t,n;const r=function(){const e={title:"",outline:[],description:""};try{if("undefined"==typeof document)return e;const t=L(document.title,200),n=[];let r="";try{const e=document.querySelectorAll("nav, footer, [role='navigation'], [role='contentinfo'], script, style, noscript"),t=t=>{for(const n of e)if(n.contains(t))return!0;return!1},o=document.querySelectorAll("h1, h2");for(const e of o){if(n.length>=50)break;if(D(e)&&!t(e)){const t=L(e.textContent,300);t&&n.push(t)}}const i=document.querySelectorAll("p"),s=[];for(const e of i){if(s.length>=3)break;if(D(e)&&!t(e)){const t=L(e.textContent,1e3);t&&s.push(t)}}r=s.join(" ").trim().slice(0,2e3)||""}catch(e){}return{title:t,outline:n,description:r}}catch(t){return e}}();o.title=(null!=(t=e.title)?t:r.title)||void 0,o.description=(null!=(n=e.description)?n:r.description)||void 0,r.outline.length>0&&(o.outline=r.outline)}else void 0!==e.title&&""!==e.title&&(o.title=e.title),void 0!==e.description&&""!==e.description&&(o.description=e.description);return e.tags&&e.tags.length>0&&(o.tags=e.tags),e.canonicalUrl&&(o.canonicalUrl=e.canonicalUrl),e.language&&(o.language=e.language),e.ogType&&(o.ogType=e.ogType),e.ogImage&&(o.ogImage=e.ogImage),e.ogImageAlt&&(o.ogImageAlt=e.ogImageAlt),e.siteName&&(o.siteName=e.siteName),e.author&&(o.author=e.author),e.publishedTime&&(o.publishedTime=e.publishedTime),e.modifiedTime&&(o.modifiedTime=e.modifiedTime),e.robots&&(o.robots=e.robots),e.extra&&Object.keys(e.extra).length>0&&(o.extra=e.extra),o}catch(t){var r;return{route:null!=(r=null==e?void 0:e.route)?r:"/",generatedAt:(new Date).toISOString()}}}(n),o=JSON.stringify(r);if(t.current===o)return;return t.current=o,U(),function(e){try{if("undefined"==typeof document||!document.head)return;const t=document.createElement("script");t.type="application/llm+json",t.setAttribute("data-llm","true"),t.textContent=JSON.stringify(e),document.head.appendChild(t)}catch(e){}}(r),()=>{U(),t.current=null}}catch(e){}},[null==e?void 0:e.route,null==e?void 0:e.mode,null==e?void 0:e.title,null==e?void 0:e.description,null==e?void 0:e.tags,null==e?void 0:e.canonicalUrl,null==e?void 0:e.language,null==e?void 0:e.ogType,null==e?void 0:e.ogImage,null==e?void 0:e.ogImageAlt,null==e?void 0:e.siteName,null==e?void 0:e.author,null==e?void 0:e.publishedTime,null==e?void 0:e.modifiedTime,null==e?void 0:e.robots])}const R="use-ref-print-target",W="use-ref-print-styles",z=`\n@media print {\n body * {\n visibility: hidden;\n }\n .${R},\n .${R} * {\n visibility: visible;\n }\n .${R} {\n position: absolute !important;\n left: 0 !important;\n top: 0 !important;\n width: 100% !important;\n }\n}\n`;function _(e,n={}){const{documentTitle:r}=n,i=o("");return{print:t(()=>{const t=e.current;if(!t||"undefined"==typeof window||!window.print)return;let n=document.getElementById(W);n||(n=document.createElement("style"),n.id=W,n.textContent=z,document.head.appendChild(n)),t.classList.add(R),r&&(i.current=document.title,document.title=r);const o=()=>{t.classList.remove(R),r&&void 0!==i.current&&(document.title=i.current)};if("onafterprint"in window){const e=()=>{o(),window.removeEventListener("afterprint",e)};window.addEventListener("afterprint",e)}window.print(),setTimeout(o,1e3)},[e,r])}}function J(e,t){if("undefined"==typeof window)return null;const n="localStorage"===e?window.localStorage:window.sessionStorage;try{const e=n.getItem(t);if(null==e)return null;const r=JSON.parse(e);return r&&"object"==typeof r?r:null}catch(e){return null}}function B(e,t){if("undefined"==typeof window)return[];const n="localStorage"===e?window.localStorage:window.sessionStorage;try{const e=n.getItem(t);if(null==e)return[];const r=JSON.parse(e);return Array.isArray(r)?r.filter(e=>"string"==typeof e):[]}catch(e){return[]}}function F(e,t){return t.filter(t=>t.condition(e)).map(e=>e.role)}function $(e,t){const n=new Set;for(const r of e){const e=t[r];if(Array.isArray(e))for(const t of e)n.add(t)}return Array.from(n)}function K(r){const{userSource:i}=r,s=o(r);s.current=r;const[c,u]=e(null),[a,l]=e([]),[d,f]=e([]),[p,m]=e(!1),[g,y]=e(null),v=t(async()=>{const{userSource:e,roleDefinitions:t,roleCapabilities:n,capabilitiesOverride:r}=s.current;y(null);let o=null,i=[],c=[];try{if("localStorage"===e.type)o=J("localStorage",e.key),i=F(o,t),c=$(i,n);else if("sessionStorage"===e.type)o=J("sessionStorage",e.key),i=F(o,t),c=$(i,n);else if("api"===e.type)o=await e.fetch(),i=F(o,t),c=$(i,n);else if("memory"===e.type)o=e.getUser(),i=F(o,t),c=$(i,n);else if("custom"===e.type){var a,d,p;const r=await Promise.resolve(e.getAuth());o=null!=(a=r.user)?a:null,i=null!=(d=r.roles)?d:F(o,t),c=null!=(p=r.capabilities)?p:$(i,n)}if(r)if("localStorage"===r.type){const e=B("localStorage",r.key);e.length>0&&(c=e)}else if("sessionStorage"===r.type){const e=B("sessionStorage",r.key);e.length>0&&(c=e)}else"api"===r.type&&(c=await r.fetch());u(o),l(i),f(c)}catch(e){y(e instanceof Error?e:new Error(String(e))),u(null),l([]),f([])}finally{m(!0)}},[]);n(()=>{v()},[v]),n(()=>{if("undefined"==typeof window)return;const e="localStorage"===i.type||"sessionStorage"===i.type?i.key:null;if(!e)return;const t=t=>{t.key===e&&v()};return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)},[i.type,"localStorage"===i.type||"sessionStorage"===i.type?i.key:"",v]);const h=t(e=>a.includes(e),[a]),w=t(e=>function(e,t){return!!e.includes("*")||e.includes(t)}(d,e),[d]),b=w,S=t(()=>v(),[v]),k=t((e,t,n)=>{if("undefined"==typeof window)return;const r="localStorage"===t?window.localStorage:window.sessionStorage;null==e?r.removeItem(n):r.setItem(n,JSON.stringify(e)),("localStorage"===i.type&&"localStorage"===t&&i.key===n||"sessionStorage"===i.type&&"sessionStorage"===t&&i.key===n)&&v()},[i,v]),E=t((e,t,n)=>{"undefined"!=typeof window&&("localStorage"===t?window.localStorage:window.sessionStorage).setItem(n,JSON.stringify(e))},[]),x=t((e,t,n)=>{"undefined"!=typeof window&&("localStorage"===t?window.localStorage:window.sessionStorage).setItem(n,JSON.stringify(e))},[]);return{user:c,roles:a,capabilities:d,isReady:p,error:g,hasRole:h,hasCapability:w,can:b,refetch:S,setUserInStorage:k,setRolesInStorage:E,setCapabilitiesInStorage:x}}function Q(){const n=o(new Set),[,r]=e(0);return{prefetch:t((e,t)=>{var o;const i=null==e?void 0:e.trim();i&&(n.current.has(i)||("document"===(null!=(o=null==t?void 0:t.as)?o:"document")?function(e){if("undefined"==typeof document)return;const t=document.querySelectorAll('link[rel="prefetch"]');for(let n=0;n<t.length;n++)if(t[n].href===e)return;const n=document.createElement("link");n.rel="prefetch",n.href=e,document.head.appendChild(n)}(i):function(e){"undefined"!=typeof fetch&&fetch(e,{method:"GET",mode:"cors"}).catch(()=>{})}(i),n.current.add(i),r(e=>e+1)))},[]),isPrefetched:t(e=>{var t;return n.current.has(null!=(t=null==e?void 0:e.trim())?t:"")},[])}}export{h as useClipboard,f as useEventBus,x as useIndexedDB,q as useLLMMetadata,l as useMutationObserver,v as useNetworkState,g as usePreferredTheme,Q as usePrefetch,K as useRBAC,w as useRageClick,_ as useRefPrint,b as useThreadedWorker,a as useTransition,M as useWasmCompute,O as useWebRTCIP,C as useWorkerNotifications,m as useWrappedChildren};
|
|
2
2
|
//# sourceMappingURL=index.modern.mjs.map
|