fast-brake
Version:
fast js feature detection with zero runtime dependencies; analyzes js to verify compatibility
1 lines • 5.12 kB
JavaScript
import {g,f as f$1,d,e}from'./chunk-4CPVD2CY.js';import {b}from'./chunk-O3M45IQU.js';import {readFileSync}from'fs';var f=class{compiledPatterns;featureStrings;featureExcludes;plugin=null;initialized=false;constructor(){this.compiledPatterns=new Map,this.featureStrings={},this.featureExcludes={};}async initialize(t){this.initialized||(t?this.plugin=t:this.plugin=await b("esversion"),this.plugin&&(this.loadPlugin(this.plugin),this.initialized=true));}loadPlugin(t){this.compiledPatterns.clear(),this.featureStrings={},this.featureExcludes={};for(let[e,n]of Object.entries(t.spec.matches)){if("strings"in n&&n.strings&&(this.featureStrings[e]=n.strings),"patterns"in n&&n.patterns)for(let s of n.patterns)this.compiledPatterns.set(e,s.pattern);"exclude"in n&&n.exclude&&(this.featureExcludes[e]=n.exclude);}}getPluginRule(t){if(!this.plugin)return null;let e=this.plugin.spec.matches[t];return e?e.rule:null}detectBoolean(t){return this.checkStrings(t)?true:this.shouldRunPatternDetection(t)?this.checkPatterns(t):false}detectFast(t){let e=this.findFirstStringMatch(t);if(e)return {hasMatch:true,mode:"fast",firstMatch:e};if(!this.shouldRunPatternDetection(t))return {hasMatch:false,mode:"fast"};let s=this.findFirstPatternMatch(t);return s?{hasMatch:true,mode:"fast",firstMatch:s}:{hasMatch:false,mode:"fast"}}detectDetailed(t){let e=this.findFirstStringMatchDetailed(t);if(e)return {hasMatch:true,mode:"detailed",firstMatch:e};if(!this.shouldRunPatternDetection(t))return {hasMatch:false,mode:"detailed"};let s=this.findFirstPatternMatchDetailed(t);return s?{hasMatch:true,mode:"detailed",firstMatch:s}:{hasMatch:false,mode:"detailed"}}detect(t,e="fast"){switch(e){case "boolean":return {hasMatch:this.detectBoolean(t),mode:"boolean"};case "fast":return this.detectFast(t);case "detailed":return this.detectDetailed(t)}}detectFile(t,e="fast"){try{let n=readFileSync(t,"utf-8");return this.detect(n,e)}catch{return {hasMatch:false,mode:e}}}checkStrings(t){for(let e of Object.values(this.featureStrings))for(let n of e)if(g(t,n)!==-1)return true;return false}checkPatterns(t){for(let e of this.compiledPatterns.values())if(f$1(e).test(t))return true;return false}isExcluded(t,e,n){let s=this.featureExcludes[n];if(!s||s.length===0)return false;let i=Math.max(0,e-20),r=t.substring(i,e);for(let c of s)if(r.endsWith(c))return true;return false}findFirstValidIndex(t,e,n){let s=0,i=[];for(;(s=t.indexOf(e,s))!==-1;)i.push(s),s+=1;let r=i.find(c=>!this.isExcluded(t,c,n));return r!==void 0?r:-1}checkPatternMatch(t,e,n){let s=this.findFirstValidIndex(t,n,e);if(s===-1)return null;if(this.plugin){let i=this.getPluginRule(e);return i?{name:e,match:n,spec:this.plugin.name,rule:i,index:s}:null}return {name:e,match:n,spec:"legacy",rule:e,index:s}}findFirstStringMatch(t){let e=Object.entries(this.featureStrings),n=([a,u])=>u.map(h=>({featureName:a,pattern:h})),i=e.map(n).flat(),r=a=>this.checkPatternMatch(t,a.featureName,a.pattern),l=i.map(r).find(a=>a!==null);return l!==void 0?l:null}findFirstPatternMatch(t){for(let[e,n]of this.compiledPatterns.entries()){let i=f$1(n).exec(t);if(i)if(this.plugin){let r=this.getPluginRule(e);if(!r)continue;return {name:e,match:i[0],spec:this.plugin.name,rule:r,index:i.index}}else return {name:e,match:i[0],spec:"legacy",rule:e,index:i.index}}return null}findFirstStringMatchDetailed(t){let e=Object.entries(this.featureStrings),n=([a,u])=>u.map(h=>({featureName:a,pattern:h})),i=e.map(n).flat(),r=a=>this.checkPatternMatch(t,a.featureName,a.pattern),l=i.map(r).find(a=>a!==null);return l!==void 0?l:null}findFirstPatternMatchDetailed(t){for(let[e,n]of this.compiledPatterns.entries()){let i=f$1(n).exec(t);if(i)if(this.plugin){let r=this.getPluginRule(e);if(!r)continue;return {name:e,match:i[0],spec:this.plugin.name,rule:r,index:i.index}}else return {name:e,match:i[0],spec:"legacy",rule:e,index:i.index}}return null}shouldRunPatternDetection(t){return this.hasComplexityIndicators(t)?true:!(t.length<d)}hasComplexityIndicators(t){for(let e$1 of e)if(g(t,e$1)!==-1)return true;return false}check(t,e){let n=e.orderedRules;if(!n||!this.plugin)return !this.detectFast(t).hasMatch;let s=n.indexOf(e.target);if(s===-1)return false;let i=this.plugin,r={};for(let[a,u]of Object.entries(this.plugin.spec.matches))n.indexOf(u.rule)>s&&(r[a]=u);let c={...this.plugin,spec:{...this.plugin.spec,matches:r}};this.plugin=c,this.loadPlugin(c);let l=this.detectFast(t);return this.plugin=i,this.loadPlugin(i),!l.hasMatch}};function k(o={}){let t=new f;if(o.plugins&&o.plugins.length>0){let n=o.plugins[0];t.plugin=n,t.loadPlugin(n),t.initialized=true;}let e=o.extensions||[];return {detect:n=>{let s=t.detectFast(n);if(!s.hasMatch||!s.firstMatch)return [];let i={name:s.firstMatch.name,version:s.firstMatch.rule};for(let r of e){let c=r.process({code:n,result:{name:s.firstMatch.name,match:s.firstMatch.match,spec:{},rule:s.firstMatch.rule,index:s.firstMatch.index}});c.spec&&Object.assign(i,c.spec);}return [i]},check:(n,s)=>{try{let i={...s};return t.plugin?.spec?.orderedRules&&(i.orderedRules=t.plugin.spec.orderedRules),t.check(n,i)}catch{return false}}}}export{f as a,k as b};