UNPKG

@incubrain/helpers

Version:
13 lines (12 loc) 9.57 kB
var U=(r,e)=>r.filter(t=>{let o=t.url.toLowerCase();if(!t.title)return!e.some(i=>o.includes(i.toLowerCase()));let n=t.title.toLowerCase();return!e.some(i=>n.includes(i.toLowerCase())||o.includes(i.toLowerCase()))});function z(r){return r.filter((e,t,o)=>e.title?t===o.findIndex(n=>n.title===e.title||n.url===e.url):t===o.findIndex(n=>n.url===e.url))}async function K({items:r,batchSize:e,processBatch:t}){let o=0;for(let n=0;n<r.length;n+=e){let i=r.slice(n,n+e);await t(i,o),o++}}function M(r,e){let t=[];for(let o=0;o<r.length;o+=e)t.push(r.slice(o,o+e));return t}import d from"node:os";function J(){let r=d.cpus().length,e=d.totalmem(),t=d.freemem(),o=Math.floor(r*(t/e));return o=Math.max(1,Math.min(o,10)),o}function V(r,e){let t=a=>{if(typeof e=="function")return e(a);if(typeof e=="string")return String(a[e]);if(Array.isArray(e))return e.map(l=>String(a[l])).join("|");throw new Error("Invalid key generator")},o=new Map,n={},i=0;return r.forEach(a=>{let l=t(a);o.has(l)?(n[l]++,i++):(o.set(l,a),n[l]=0)}),{items:Array.from(o.values()),duplicateCounts:n,totalDuplicates:i}}function Y(r){let e=new Set;return r.forEach(t=>{e.add(t)}),Array.from(e)}import c from"path";import u from"fs";import{fileURLToPath as R}from"url";var b=class{root;isDocker;appName;constructor(e){this.isDocker=e.isDocker,this.appName=e.appName,this.isDocker?this.root="/app":this.root=e.projectRoot||this.determineProjectRoot()}determineProjectRoot(){let e=typeof import.meta<"u",t,o;e?(t=R(import.meta.url),o=c.dirname(t)):(t=__filename,o=__dirname);let n=o;for(;n!==c.dirname(n);){if(u.existsSync(c.join(n,"package.json")))return n;n=c.dirname(n)}return n}get paths(){return{root:this.root,logs:c.join(this.root,"logs",this.appName),config:c.join(this.root,"config",this.appName),data:c.join(this.root,"data",this.appName),temp:c.join(this.root,"temp",this.appName),cache:c.join(this.root,"cache",this.appName)}}ensureDirectories(){Object.values(this.paths).forEach(e=>{try{if(!u.existsSync(e)){let t=c.dirname(e);try{u.accessSync(t,u.constants.W_OK)}catch{console.error(`\u274C Permission denied: Cannot write to ${t}`);return}u.mkdirSync(e,{recursive:!0,mode:493}),console.info(`\u2705 Created directory: ${e}`)}}catch(t){t.code==="EACCES"?console.error(`\u274C Permission denied: Cannot create directory ${e}`):console.error(`\u274C Unexpected error creating directory ${e}:`,t)}})}resolvePath(...e){return c.join(this.root,...e)}static isRunningInDocker(){try{return u.existsSync("/.dockerenv")}catch{return!1}}};function re(r,e){return Object.fromEntries(Object.entries(r).filter(([t])=>e.includes(t)))}var s=[];for(let r=0;r<256;++r)s.push((r+256).toString(16).slice(1));function v(r,e=0){return(s[r[e+0]]+s[r[e+1]]+s[r[e+2]]+s[r[e+3]]+"-"+s[r[e+4]]+s[r[e+5]]+"-"+s[r[e+6]]+s[r[e+7]]+"-"+s[r[e+8]]+s[r[e+9]]+"-"+s[r[e+10]]+s[r[e+11]]+s[r[e+12]]+s[r[e+13]]+s[r[e+14]]+s[r[e+15]]).toLowerCase()}import{randomFillSync as j}from"crypto";var m=new Uint8Array(256),p=m.length;function y(){return p>m.length-16&&(j(m),p=0),m.slice(p,p+=16)}import{randomUUID as $}from"crypto";var x={randomUUID:$};function C(r,e,t){if(x.randomUUID&&!e&&!r)return x.randomUUID();r=r||{};let o=r.random??r.rng?.()??y();if(o.length<16)throw new Error("Random bytes length must be >= 16");if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,e){if(t=t||0,t<0||t+16>e.length)throw new RangeError(`UUID byte range ${t}:${t+15} is out of buffer bounds`);for(let n=0;n<16;++n)e[t+n]=o[n];return e}return v(o)}var g=C;var D=["research_citations","research_figures","research_tables","research_math","research_notes"],N=new RegExp(`\\[(${D.join("|")})\\((.*?)\\)\\]`,"g");function fe(r,e=g()){return`[${r}(${e})]`}function de(){return g()}function ye(r){let e=[],t=0,o=r;try{o=r.replace(N,(n,i)=>{let a=`${i.split("_")[1].charAt(0)}(${t})`;return e.push({original:n,simplified:a}),t++,a})}catch(n){console.error("Error extracting placeholders:",{error:n}),o=r}return{cleanedText:o,placeholders:e}}function xe(r,e){let t=r;try{e.forEach(o=>{try{t=t.replace(o.simplified,o.original)}catch(n){console.error("Error replacing placeholder:",{error:n})}})}catch(o){console.error("Error re-adding placeholders:",{error:o})}return t.trim()}import{createHash as O}from"node:crypto";function be(r){return O("sha256").update(r,"utf8").digest("hex")}function Pe(r){let e=["the","be","to","of","and","a","in","that","have","I","it","for","not","on","with","he","as","you","do","at"];return r.split(/\s+/).reduce((n,i)=>(e.includes(i.toLowerCase())&&(n+=1),n),0)>10}var Ee=(r,e)=>{let t=e.split("/").filter(Boolean).length;return r.split("/").filter(Boolean).length-t};async function je(r,e,t){let o=0,n=t;for(;o<e;)try{return await r()}catch(i){if(o++,o>=e)throw i;console.warn(`Attempt ${o} failed. Retrying in ${n}ms...`),await new Promise(a=>setTimeout(a,n)),n*=2}throw new Error("Should not reach here")}function Ce(r){return r/4}import{statSync as I,existsSync as S,mkdirSync as P}from"node:fs";import{createWriteStream as _}from"node:fs";import h from"node:path";var f=class{constructor(e,t={},o="default"){this.baseFilePath=e;this.options=t;this.logName=o,this.options={maxFileSize:5*1024*1024,maxFiles:5,timestamp:!0,format:"json",...t};let n=h.dirname(e);S(n)||P(n,{recursive:!0}),this.initStream()}currentFile=0;writeStream=null;currentSize=0;isRotating=!1;logName;getCurrentFilePath(){return`${this.baseFilePath}.${this.currentFile}.log`}initStream(){try{let e=this.getCurrentFilePath();if(this.writeStream=_(e,{flags:"a"}),S(e)){let t=I(e);this.currentSize=t.size}else this.currentSize=0;this.writeStream.on("error",t=>{console.error(`Error writing to log file [${this.logName}]:`,t)})}catch(e){console.error(`Failed to initialize log stream [${this.logName}]:`,e)}}async rotateFiles(){if(!this.isRotating){this.isRotating=!0;try{this.writeStream&&await new Promise(e=>{this.writeStream.end(()=>{this.writeStream.destroy(),e()})}),this.currentFile=(this.currentFile+1)%(this.options.maxFiles||5),this.initStream(),this.currentSize=0}catch(e){console.error(`Error rotating log files [${this.logName}]:`,e)}finally{this.isRotating=!1}}}formatLogEntry(e){let t={...e,...this.options.timestamp&&{timestamp:new Date().toISOString()},logger:this.logName};try{return this.options.format==="jsonsl"?JSON.stringify(t)+` `:this.options.pretty?JSON.stringify(t,null,2)+` `:JSON.stringify(t)+` `}catch{return`${new Date().toISOString()} ERROR_FORMATTING_LOG [${this.logName}]: ${String(e)} `}}async write(e,t="info"){if(e)try{let o=F(e);for(let n of o){let i=typeof n=="object"?{...n,level:t}:{message:n,level:t},a=this.formatLogEntry(i),l=Buffer.from(a).length;if(this.currentSize+l>(this.options.maxFileSize||5*1024*1024)&&await this.rotateFiles(),this.writeStream&&!this.isRotating){let T=this.writeStream.write(a);this.currentSize+=l,T||await new Promise(E=>this.writeStream.once("drain",E))}}}catch(o){console.error(`Error writing log entry [${this.logName}]:`,o)}}async info(e){return this.write(e,"info")}async warn(e){return this.write(e,"warn")}async error(e){return this.write(e,"error")}async debug(e){return this.write(e,"debug")}},F=r=>{switch(typeof r){case"string":return[{message:r}];case"number":case"bigint":return[{message:String(r)}];case"object":if(r===null)throw new TypeError("Cannot log `null` as an entry.");return Array.isArray(r)?r:[r];default:throw new TypeError(`Unhandled log entry type: ${typeof r}`)}},w={};function k(r,e={},t=r){try{let o=h.join(process.cwd(),"logs");S(o)||P(o,{recursive:!0});let n=h.join(o,r),i=`${n}-${t}`;return w[i]||(w[i]=new f(n,e,t)),w[i]}catch(o){console.error("Error creating logger:",o);let n=h.join(process.cwd(),"logs","emergency");return new f(n,e,"emergency")}}async function Fe(r,e,t={}){try{await k(r,t).write(e)}catch(o){console.error("Error writing to log file:",o)}}function Le(r="ts"){let e="//";switch(r){case"sql":e="--";break;case"js":e="//";break;case"ts":e="//";break;default:e="//"}return` ${e} ----------------------------------------------------------------------- ${e} WARNING: This file is auto-generated. DO NOT MODIFY IT MANUALLY. ${e} Any changes you make will be overwritten the next time the file ${e} is generated. ${e} ----------------------------------------------------------------------- `}function Ae(r){return r?r.split("_").map(e=>e[0].toUpperCase()+e.slice(1)).join(""):(console.warn("Empty string provided to capitalCase helper"),"")}function Ue(r){return r.replace(/[-_](\w)/g,(e,t)=>t.toUpperCase())}function ze(r){return r.endsWith("s")?r:r+"s"}function We(r){return r.endsWith("ies")?r.slice(0,-3)+"y":r.endsWith("s")?r.slice(0,-1):r}import{readFileSync as L}from"fs";var A=r=>new RegExp(`^${r}(/.*)?$`);function Be(r){try{let e=JSON.parse(L(r,"utf-8")),t={...e.dependencies,...e.devDependencies,...e.peerDependencies},n=Object.keys(t).map(A);return[/^node:/,...n,/^[^./]|^\.[^./]|^\.\.[^/]/]}catch(e){return console.warn("Failed to read package.json, falling back to defaults:",e),[/^node:/,/^@opentelemetry/,/^@incubrain/,/^[^./]|^\.[^./]|^\.\.[^/]/]}}export{f as FileLogger,b as PathManager,M as batchArray,K as batchProcess,J as calculateConcurrency,Ce as calculateTokens,Ee as calculateUrlDepth,Ue as camelCase,Ae as capitalCase,k as createFileLogger,V as dedupeItems,Y as dedupeStrings,ye as extractPlaceholders,re as filterObject,de as generateId,fe as generatePlaceholder,Be as getExternalPackages,Le as getWarningComment,be as hashContent,Pe as isEnglish,Fe as logFile,ze as pluralize,xe as reAddPlaceholders,z as removeDuplicates,U as removeItemsByKeywords,je as retry,We as singularize}; //# sourceMappingURL=index.js.map