@stacksjs/dtsx
Version:
A modern, fast .d.ts generation tool, powered by Bun.
4 lines (3 loc) • 4.47 kB
JavaScript
// @bun
import{createReadStream as Q,createWriteStream as U,statSync as V}from"fs";import{createInterface as X}from"readline";class N{config;profiles=[];cleanupTimer=null;constructor(q={}){this.config={maxMemoryMB:q.maxMemoryMB??512,chunkSize:q.chunkSize??65536,aggressiveGC:q.aggressiveGC??!0,maxDeclarationsInMemory:q.maxDeclarationsInMemory??1e4,profile:q.profile??!1,cleanupInterval:q.cleanupInterval??5000}}startMonitoring(){if(this.cleanupTimer=setInterval(()=>{this.checkMemoryAndCleanup()},this.config.cleanupInterval),this.cleanupTimer&&typeof this.cleanupTimer==="object"&&"unref"in this.cleanupTimer)this.cleanupTimer.unref()}stopMonitoring(){if(this.cleanupTimer)clearInterval(this.cleanupTimer),this.cleanupTimer=null}checkMemoryAndCleanup(){if(this.getMemoryStats().heapUsedMB>this.config.maxMemoryMB)this.triggerCleanup()}triggerCleanup(){if(this.config.aggressiveGC&&global.gc)global.gc()}getMemoryStats(){let q=process.memoryUsage();return{heapUsed:q.heapUsed,heapTotal:q.heapTotal,external:q.external,arrayBuffers:q.arrayBuffers,rss:q.rss,heapUsedMB:Math.ceil(q.heapUsed/1024/1024),percentUsed:Math.min(100,Math.round(q.heapUsed/q.heapTotal*100))}}async profile(q,D){if(!this.config.profile)return D();let E=this.getMemoryStats(),F=Date.now();try{return await D()}finally{let H=this.getMemoryStats(),J=Date.now()-F;this.profiles.push({timestamp:Date.now(),operation:q,memoryBefore:E,memoryAfter:H,duration:J})}}getProfiles(){return[...this.profiles]}clearProfiles(){this.profiles=[]}async*streamFile(q){let D=Q(q,{encoding:"utf-8",highWaterMark:this.config.chunkSize}),E=X({input:D,crlfDelay:1/0});for await(let F of E)yield F}async processFileInChunks(q,D,E=1000){let F=[],H=[];for await(let J of this.streamFile(q))if(H.push(J),H.length>=E){let K=await D(H);F.push(...K),H=[],this.checkMemoryAndCleanup()}if(H.length>0){let J=await D(H);F.push(...J)}return F}async streamWrite(q,D){let E=U(q,{encoding:"utf-8"});return new Promise((F,H)=>{let J=async()=>{try{for await(let K of D)if(!E.write(K))await new Promise((O)=>E.once("drain",()=>O()));E.end(),E.once("finish",F)}catch(K){H(K)}};E.once("error",H),J()})}}class Y{declarations=new Map;registry=new FinalizationRegistry((q)=>{this.declarations.delete(q)});maxSize;constructor(q=1e4){this.maxSize=q}add(q,D){if(this.declarations.size>=this.maxSize)this.evictOldest();let E=new WeakRef(D);this.declarations.set(q,E),this.registry.register(D,q)}get(q){return this.declarations.get(q)?.deref()}has(q){return this.declarations.get(q)?.deref()!==void 0}delete(q){this.declarations.delete(q)}clear(){this.declarations.clear()}get size(){return this.declarations.size}evictOldest(){let q=Math.ceil(this.maxSize*0.2),D=Array.from(this.declarations.keys());for(let E=0;E<q&&E<D.length;E++)this.declarations.delete(D[E])}}class Z{value=null;loaded=!1;loader;constructor(q){this.loader=q}async get(){if(!this.loaded)this.value=await this.loader(),this.loaded=!0;return this.value}isLoaded(){return this.loaded}unload(){this.value=null,this.loaded=!1}}class _{strings=new Map;maxSize;constructor(q=50000){this.maxSize=q}intern(q){if(q.length>100)return q;let D=this.strings.get(q);if(D!==void 0)return D;if(this.strings.size>=this.maxSize){let E=Array.from(this.strings.keys());for(let F=0;F<E.length/2;F++)this.strings.delete(E[F])}return this.strings.set(q,q),q}get size(){return this.strings.size}clear(){this.strings.clear()}}class ${pool=[];factory;reset;maxSize;constructor(q,D,E=1000){this.factory=q,this.reset=D,this.maxSize=E}acquire(){if(this.pool.length>0)return this.pool.pop();return this.factory()}release(q){if(this.pool.length<this.maxSize)this.reset(q),this.pool.push(q)}get size(){return this.pool.length}clear(){this.pool=[]}}function W(q,D={}){return{...q,memory:{maxMemoryMB:D.maxMemoryMB??512,chunkSize:D.chunkSize??65536,aggressiveGC:D.aggressiveGC??!0,maxDeclarationsInMemory:D.maxDeclarationsInMemory??1e4,profile:D.profile??!1,cleanupInterval:D.cleanupInterval??5000}}}function G(q){let E=V(q).size/1024/1024,F=E*4,H=65536;if(E>10)H=32768;if(E>50)H=16384;return{fileSizeMB:Math.round(E*100)/100,estimatedMemoryMB:Math.round(F*100)/100,recommendedChunkSize:H}}function L(q){return[`Heap: ${q.heapUsedMB}MB / ${Math.round(q.heapTotal/1024/1024)}MB (${q.percentUsed}%)`,`RSS: ${Math.round(q.rss/1024/1024)}MB`,`External: ${Math.round(q.external/1024/1024)}MB`].join(" | ")}function v(q={}){return new N(q)}
export{N as Na,Y as Oa,Z as Pa,_ as Qa,$ as Ra,W as Sa,G as Ta,L as Ua,v as Va};