UNPKG

fast-brake

Version:

fast js feature detection with zero runtime dependencies; analyzes js to verify compatibility

1 lines 3.29 kB
import {a}from'./chunk-ZPNX4DSE.js';export{a as Detector,b as fastBrakeSync}from'./chunk-ZPNX4DSE.js';import {b,c,g}from'./chunk-4CPVD2CY.js';import'./chunk-O3M45IQU.js';import'./chunk-3TXHFKDN.js';import {existsSync,readdirSync,statSync}from'fs';import {join,extname}from'path';var h=class{scan(t,e={}){let{extensions:s=b,ignorePatterns:r=c,maxDepth:n=10,earlyExit:o=false,limit:i}=e;if(!existsSync(t))return [];let c$1=new Set(s),m={results:[],maxDepth:n,earlyExit:o,limit:i,shouldStop:false};return this.walkDirectory(t,0,m,r,c$1),m.results}findFirst(t,e={}){return this.scan(t,{...e,earlyExit:true,limit:1})[0]}walkDirectory(t,e,s,r,n){if(this.checkTermination(e,s))return;let i=this.readDirectorySafe(t);i&&this.processEntries(i,t,e,s,r,n);}checkTermination(t,e){return t>e.maxDepth||e.shouldStop}processEntries(t,e,s,r,n,o){for(let i of t){if(r.shouldStop)break;if(!this.shouldProcessEntry(i,n))continue;let c=join(e,i);this.processEntry(c,i,s,r,n,o);}}shouldProcessEntry(t,e){return !this.shouldIgnore(t,e)}processEntry(t,e,s,r,n,o){let i=this.getStatsSafe(t);if(!i)return;if(i.isDirectory()){this.walkDirectory(t,s+1,r,n,o);return}i.isFile()&&this.processFile(t,e,r,o);}processFile(t,e,s,r){let n=extname(e);r.has(n)&&(this.addResult(t,n,s),this.checkStopConditions(s));}addResult(t,e,s){let r={path:t,type:"file",extension:e};s.results.push(r);}checkStopConditions(t){let e=t.limit&&t.results.length>=t.limit;(t.earlyExit||e)&&(t.shouldStop=true);}readDirectorySafe(t){try{return readdirSync(t)}catch{return null}}getStatsSafe(t){try{return statSync(t)}catch{return null}}shouldIgnore(t,e){for(let s of e){let r=t===s,n=t.startsWith(s+"/");if(r||n)return true;if(!(g(s,"*")!==-1))continue;if(this.simpleGlobMatch(t,s))return true}return false}simpleGlobMatch(t,e){if(e==="*")return true;if(e.startsWith("*.")){let i=e.slice(1);return t.endsWith(i)}if(e.endsWith("*")){let i=e.slice(0,-1);return t.startsWith(i)}if(e.startsWith("*")){let i=e.slice(1);return t.endsWith(i)}return false}};var f=class{cache;ttl;constructor(t=6e4){this.cache=new Map,this.ttl=t;}get(t){let e=this.cache.get(t);if(e){if(Date.now()-e.timestamp>this.ttl){this.cache.delete(t);return}return e.value}}set(t,e){this.cache.set(t,{value:e,timestamp:Date.now()});}has(t){return this.get(t)!==void 0}invalidate(t){this.cache.delete(t);}clear(){this.cache.clear();}isStale(t){let e=this.cache.get(t);return e?Date.now()-e.timestamp>this.ttl:true}};var l=new a,x=false;async function p(){x||(await l.initialize(),x=true);}async function w(a$1){if(typeof a$1=="string"){await p();let s=l.detectFast(a$1);return !s.hasMatch||!s.firstMatch?[]:[{name:s.firstMatch.name,version:s.firstMatch.rule}]}let t=a$1,e=new a;return t.plugins&&t.plugins.length>0?await e.initialize(t.plugins[0]):await e.initialize(),{detect:async s=>{let r=e.detectFast(s);return !r.hasMatch||!r.firstMatch?[]:[{name:r.firstMatch.name,version:r.firstMatch.rule}]},check:async(s,r)=>{try{return e.check(s,r)}catch{return false}}}}async function W(a){await p();let t=l.detectFast(a);return t.firstMatch?[{name:t.firstMatch.name,version:t.firstMatch.rule}]:[]}async function R(a,t){await p();try{return l.check(a,t)}catch{return false}}var A=w;export{f as FastBrakeCache,h as Scanner,R as check,A as default,W as detect,w as fastBrake};