@je-es/server 0.0.5 → 0.0.6

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 CHANGED
@@ -8,7 +8,7 @@
8
8
  </div>
9
9
 
10
10
  <div align="center">
11
- <img src="https://img.shields.io/badge/v-0.0.5-black"/>
11
+ <img src="https://img.shields.io/badge/v-0.0.6-black"/>
12
12
  <a href="https://github.com/maysara-elshewehy">
13
13
  </a>
14
14
  <a href="https://github.com/je-es/server"> <img src="https://img.shields.io/badge/🔥-@je--es/server-black"/> </a>
package/dist/main.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  'use strict';Object.defineProperty(exports,'__esModule',{value:true});var bun_sqlite=require('bun:sqlite'),ce=require('crypto');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var ce__default=/*#__PURE__*/_interopDefault(ce);var q=class{constructor(e=":memory:"){this.schemas=new Map;this.currentQuery="";this.currentParams=[];this.db=new bun_sqlite.Database(e),this.db.exec("PRAGMA foreign_keys = ON");}close(){this.db.close();}defineSchema(e){this.schemas.set(e.name,e);let r=this.generateCreateTableSQL(e);if(this.db.exec(r),e.indexes)for(let t of e.indexes){let a=`CREATE ${t.unique?"UNIQUE":""} INDEX IF NOT EXISTS ${t.name} ON ${e.name} (${t.columns.join(", ")})`;this.db.exec(a);}}getSchema(e){return this.schemas.get(e)}listTables(){return this.db.query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'").all().map(r=>r.name)}dropTable(e){this.db.exec(`DROP TABLE IF EXISTS ${e}`),this.schemas.delete(e);}query(){return this.reset(),this.createQueryBuilder()}find(e,r){let t=Object.entries(r).map(([n,a])=>({column:n,operator:"=",value:a}));return this.query().select().from(e).where(t).execute()}findOne(e,r){return this.query().select().from(e).where(Object.entries(r).map(([t,n])=>({column:t,operator:"=",value:n}))).limit(1).executeOne()}findById(e,r){return this.findOne(e,{id:r})}all(e){return this.query().select().from(e).execute()}insert(e,r){this.query().insert(e,r).execute();let t=this.db.query("SELECT last_insert_rowid() as id").get();return this.findById(e,t.id)}update(e,r,t){return this.query().update(e,t).where({column:"id",operator:"=",value:r}).execute(),this.findById(e,r)}delete(e,r){this.query().delete(e).where({column:"id",operator:"=",value:r}).execute();return true}transaction(e){this.db.exec("BEGIN TRANSACTION");try{e(this),this.db.exec("COMMIT");}catch(r){throw this.db.exec("ROLLBACK"),r}}exec(e){this.db.exec(e);}raw(e,r=[]){return this.db.query(e).all(...r)}rawOne(e,r=[]){return this.db.query(e).get(...r)}reset(){this.currentQuery="",this.currentParams=[];}createQueryBuilder(){let e={_select:["*"],_from:"",_where:[],_orderBy:"",_limit:null,_offset:null,_isInsert:false,_isUpdate:false,_isDelete:false,_insertData:null,_updateData:null},r=this;return e.select=function(t){return this._select=t||["*"],this},e.from=function(t){return this._from=t,this},e.where=function(t){let a=(Array.isArray(t)?t:[t]).map(u=>{if(u.operator==="IS NULL"||u.operator==="IS NOT NULL")return `${u.column} ${u.operator}`;if(u.operator==="IN"&&Array.isArray(u.value)){let i=u.value.map(()=>"?").join(", ");return u.value.forEach(p=>{r.currentParams.push(p);}),`${u.column} IN (${i})`}else return r.currentParams.push(u.value),`${u.column} ${u.operator} ?`});return this._where.push(...a),this},e.and=function(t){return this.where(t)},e.or=function(t){if(t.operator==="IS NULL"||t.operator==="IS NOT NULL")this._where.push(`OR ${t.column} ${t.operator}`);else if(t.operator==="IN"&&Array.isArray(t.value)){let n=t.value.map(()=>"?").join(", ");t.value.forEach(a=>{r.currentParams.push(a);}),this._where.push(`OR ${t.column} IN (${n})`);}else r.currentParams.push(t.value),this._where.push(`OR ${t.column} ${t.operator} ?`);return this},e.orderBy=function(t,n="ASC"){return this._orderBy=`ORDER BY ${t} ${n}`,this},e.limit=function(t){return this._limit=t,this},e.offset=function(t){return this._offset=t,this},e.insert=function(t,n){return this._isInsert=true,this._from=t,this._insertData=n,this},e.update=function(t,n){return this._isUpdate=true,this._from=t,this._updateData=n,this},e.delete=function(t){return this._isDelete=true,this._from=t,this},e.raw=function(t,n=[]){return r.currentQuery=t,r.currentParams=n,this},e.execute=function(){let t="";if(this._isInsert&&this._insertData){let u=Object.keys(this._insertData),i=u.map(()=>"?").join(", ");t=`INSERT INTO ${this._from} (${u.join(", ")}) VALUES (${i})`,r.currentParams=Object.values(this._insertData);}else if(this._isUpdate&&this._updateData){let u=Object.keys(this._updateData).map(p=>`${p} = ?`),i=Object.values(this._updateData);r.currentParams=[...i,...r.currentParams],t=`UPDATE ${this._from} SET ${u.join(", ")}`,this._where.length>0&&(t+=` WHERE ${this._where.join(" AND ")}`);}else this._isDelete?(t=`DELETE FROM ${this._from}`,this._where.length>0&&(t+=` WHERE ${this._where.join(" AND ")}`)):(t=`SELECT ${this._select.join(", ")} FROM ${this._from}`,this._where.length>0&&(t+=` WHERE ${this._where.join(" AND ")}`),this._orderBy&&(t+=` ${this._orderBy}`),this._limit!==null&&(t+=` LIMIT ${this._limit}`),this._offset!==null&&(t+=` OFFSET ${this._offset}`));!t&&r.currentQuery&&(t=r.currentQuery);let a=r.db.query(t).all(...r.currentParams);return r.reset(),a},e.executeOne=function(){let t=this.execute();return t.length>0?t[0]:null},e.executeRaw=function(t,n=[]){return r.db.query(t).all(...n)},e}generateCreateTableSQL(e){let r=e.columns.map(t=>{let n=`${t.name} ${t.type}`;return t.primaryKey&&(n+=" PRIMARY KEY",t.autoIncrement&&(n+=" AUTOINCREMENT")),t.notNull&&!t.primaryKey&&(n+=" NOT NULL"),t.unique&&(n+=" UNIQUE"),t.default!==void 0&&(typeof t.default=="string"?n+=` DEFAULT '${t.default}'`:t.default===null?n+=" DEFAULT NULL":n+=` DEFAULT ${t.default}`),t.references&&(n+=` REFERENCES ${t.references.table}(${t.references.column})`),n});return `CREATE TABLE IF NOT EXISTS ${e.name} (${r.join(", ")})`}};function Y(s,e){return {name:s,columns:e}}function Z(s,e){return {name:s,type:e}}function ee(s){return {name:s,type:"INTEGER"}}function te(s){return {name:s,type:"TEXT"}}function re(s){return {name:s,type:"REAL"}}function ne(s){return {name:s,type:"BLOB"}}function se(s){return {name:s,type:"NUMERIC"}}function oe(s,e=false){return {...s,primaryKey:true,autoIncrement:e}}function ie(s){return {...s,notNull:true}}function ae(s){return {...s,unique:true}}function ue(s,e){return {...s,default:e}}function le(s,e,r){return {...s,references:{table:e,column:r}}}var _=class{constructor(){this.routes=new Map;this.regexRoutes=[];}match(e,r){let t=`${e}:${r}`;if(this.routes.has(t))return {handler:this.routes.get(t),params:{}};for(let n of this.regexRoutes)if(n.method===e){let a=r.match(n.pattern);if(a?.groups)return {handler:n.handler,params:a.groups}}return null}getAll(){let e=Array.from(this.routes.entries()).map(([t,n])=>{let a=t.indexOf(":"),u=t.substring(0,a),i=t.substring(a+1);return {method:u,path:i,handler:n}}),r=this.regexRoutes.map(t=>{let n=t.key.indexOf(":");return {method:t.method,path:t.key.substring(n+1),handler:t.handler}});return [...e,...r]}clear(){this.routes.clear(),this.regexRoutes=[];}remove(e,r){let t=`${e}:${r}`;if(this.routes.has(t))return this.routes.delete(t),true;let n=this.regexRoutes.findIndex(a=>a.key===t);return n>=0?(this.regexRoutes.splice(n,1),true):false}register(e,r,t,n={}){let a=`${e}:${r}`;if(r.includes(":")){let u=this.pathToRegex(r),i=this.regexRoutes.findIndex(b=>b.key===a),p={pattern:u,method:e,handler:t,key:a};i>=0?this.regexRoutes[i]=p:this.regexRoutes.push(p);}else this.routes.set(a,t);}pathToRegex(e){let t=e.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/:(\w+)/g,"(?<$1>[^/]+)");return new RegExp(`^${t}$`)}};var N=class{constructor(){this.rateLimitStore=new Map;this.csrfTokens=new Map;this.requestLog=new Map;this.MAX_REQUEST_LOG_SIZE=1e3;}checkRateLimit(e,r,t){let n=Date.now(),a=this.rateLimitStore.get(e);return a?n<a.reset?a.count>=r?false:(a.count++,true):(this.rateLimitStore.set(e,{count:1,reset:n+t}),true):(this.rateLimitStore.set(e,{count:1,reset:n+t}),true)}cleanupRateLimit(){let e=Date.now();for(let[r,t]of this.rateLimitStore.entries())e>t.reset&&this.rateLimitStore.delete(r);}generateCsrfToken(e,r=36e5){let t=ce__default.default.randomBytes(32).toString("hex");return this.csrfTokens.set(t,{sessionId:e,expires:Date.now()+r}),t}validateCsrfToken(e,r){let t=this.csrfTokens.get(e);return t?Date.now()>t.expires?(this.csrfTokens.delete(e),false):t.sessionId===r?(this.csrfTokens.delete(e),true):false:false}cleanupCsrfTokens(){let e=Date.now();for(let[r,t]of this.csrfTokens.entries())e>t.expires&&this.csrfTokens.delete(r);}sanitizeHtml(e){return e?e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;").replace(/\//g,"&#x2F;"):""}sanitizeSql(e){return e?e.replace(/\\/g,"\\\\").replace(/;/g,"").replace(/'/g,"''").replace(/"/g,'\\"').replace(/\x00/g,""):""}logRequest(e,r,t,n,a,u){if(this.requestLog.set(e,{timestamp:new Date().toISOString(),method:r,path:t,ip:n,status:a,duration:u}),this.requestLog.size>this.MAX_REQUEST_LOG_SIZE){let{value:i}=this.requestLog.keys().next()||{value:null};i&&this.requestLog.delete(i);}}getRequestLog(e){return this.requestLog.get(e)}getAllRequestLogs(){return Array.from(this.requestLog.values())}clearAll(){this.rateLimitStore.clear(),this.csrfTokens.clear(),this.requestLog.clear();}getStats(){return {rateLimitEntries:this.rateLimitStore.size,csrfTokens:this.csrfTokens.size,requestLogs:this.requestLog.size}}};var $=class{constructor(e="info",r=false){this.level=1;this.pretty=false;this.levels={debug:0,info:1,warn:2,error:3,fatal:4};this.level=this.levels[e]??1,this.pretty=r;}debug(e,r){this.log("debug",this.levels.debug,e,r);}info(e,r){this.log("info",this.levels.info,e,r);}warn(e,r){this.log("warn",this.levels.warn,e,r);}error(e,r){this.log("error",this.levels.error,e,r);}fatal(e,r){this.log("fatal",this.levels.fatal,e,r),process.env.NODE_ENV;}log(e,r,t,n){if(r<this.level)return;let a=new Date().toISOString(),u=t??{},i={timestamp:a,level:e.toUpperCase(),message:n||"No message",...u},p=this.pretty?`[${a}] ${e.toUpperCase()} ${n||"No message"}
2
- ${JSON.stringify(u,null,2)}`:JSON.stringify(i);e==="error"||e==="fatal"?console.error(p):e==="warn"?console.warn(p):console.log(p);}};var w=class extends Error{constructor(r,t=500,n){super(r);this.message=r;this.statusCode=t;this.code=n;this.name="AppError";}},E=class extends w{constructor(r,t){super(r,400,"VALIDATION_ERROR");this.issues=t;this.name="ValidationError";}},W=class extends w{constructor(e){super(e,500,"DATABASE_ERROR"),this.name="DatabaseError";}},k=class extends w{constructor(e="Request timeout"){super(e,408,"TIMEOUT_ERROR"),this.name="TimeoutError";}},G=class extends w{constructor(e="Too many requests"){super(e,429,"RATE_LIMIT_ERROR"),this.name="RateLimitError";}};var P=new N,L=new _;function de(s={}){let e=s.port||3e3,r=s.hostname||"localhost",t=s.maxRequestSize||10*1024*1024,n=s.requestTimeout||3e4,a=s.gracefulShutdownTimeout||1e4,u=typeof s.logging=="object"?s.logging:{},i=s.logging?new $(u.level||"info",u.pretty):null,p=new Map,b=[],x=new Set,T=setInterval(()=>{P.cleanupRateLimit(),P.cleanupCsrfTokens();},120*1e3);async function c(o){let m=Date.now(),l=crypto.randomUUID(),R=new URL(o.url).pathname,A=o.method.toUpperCase(),f=he(o);x.add(l);try{let y=o.headers.get("content-length");if(y&&parseInt(y)>t)return i?.warn({requestId:l,size:y,ip:f},"Request too large"),new Response(JSON.stringify({error:"Payload too large"}),{status:413,headers:{"Content-Type":"application/json"}});let I=fe(o,s);if(A==="OPTIONS")return new Response(null,{status:204,headers:I});if(s.security&&typeof s.security=="object"&&s.security.rateLimit){let C=typeof s.security.rateLimit=="object"?s.security.rateLimit:{},v=C.max||100,j=C.windowMs||6e4,F=C.keyGenerator?C.keyGenerator({request:o,ip:f}):f;if(!P.checkRateLimit(F,v,j))return i?.warn({requestId:l,ip:f,key:F},"Rate limit exceeded"),new Response(JSON.stringify({error:C.message||"Too many requests"}),{status:429,headers:{"Content-Type":"application/json"}})}let M=null;["POST","PUT","PATCH"].includes(A)&&(M=await ge(o,i,t));let H=p.get("default"),B=L.match(A,R);if(!B){let C=X(o,{},H,i,l);return i?.warn({requestId:l,method:A,path:R,ip:f},"Route not found"),C.json({error:"Not Found",path:R},404)}let U=X(o,B.params||{},H,i,l);U.body=M,U.request=o;let Q=new AbortController,K=new Promise((C,v)=>{let j=setTimeout(()=>{Q.abort(),v(new k("Request timeout"));},n);Q.signal.addEventListener("abort",()=>clearTimeout(j));}),D=await Promise.race([B.handler(U),K]),S=new Headers(D.headers);I.forEach((C,v)=>{S.has(v)||S.set(v,C);}),S.set("X-Request-ID",l),S.set("X-Content-Type-Options","nosniff"),S.set("X-Frame-Options","DENY"),S.set("X-XSS-Protection","1; mode=block"),S.set("Referrer-Policy","strict-origin-when-cross-origin");let z=Date.now()-m;return P.logRequest(l,A,R,f,D.status,z),i?.info({requestId:l,method:A,path:R,status:D.status,duration:z,ip:f},"Request completed"),new Response(D.body,{status:D.status,headers:S})}catch(y){if(y instanceof w)return i?.warn({error:y.message,requestId:l,ip:f},`App error: ${y.message}`),new Response(JSON.stringify({error:y.message,code:y.code,requestId:l}),{status:y.statusCode,headers:{"Content-Type":"application/json"}});i?.error({error:String(y),requestId:l,ip:f},"Unhandled error");let I=process.env.NODE_ENV==="production"?"Internal Server Error":y.message;return new Response(JSON.stringify({error:I,requestId:l}),{status:500,headers:{"Content-Type":"application/json"}})}finally{x.delete(l);}}let d={method:"GET",path:"/health",handler:o=>o.json({status:"healthy",timestamp:new Date().toISOString(),uptime:process.uptime(),activeRequests:x.size})},g={method:"GET",path:"/readiness",handler:o=>{let m=p.size>0,l=m||p.size===0;return o.json({ready:l,checks:{database:m?"connected":"not configured",activeRequests:x.size},timestamp:new Date().toISOString()},l?200:503)}};s.routes&&s.routes.forEach(o=>{b.push(o),(Array.isArray(o.method)?o.method:[o.method]).forEach(l=>{L.register(l,o.path,o.handler,o);});}),b.push(d,g),L.register("GET","/health",d.handler,d),L.register("GET","/readiness",g.handler,g);let h=null,V={app:null,logger:i,db:p,bunServer:null,async start(){if(s.database){let m=Array.isArray(s.database)?s.database:[s.database];for(let l of m){let O=l.name||"default";try{if(typeof l.connection=="string"){let R=new q(l.connection);if(l.schema&&typeof l.schema=="object")for(let[A,f]of Object.entries(l.schema))f&&typeof f=="object"&&R.defineSchema(f);p.set(O,R),i?.info({name:O,connection:l.connection},"\u2705 Database connected");}else throw new Error(`Database connection must be a string path (got ${typeof l.connection})`)}catch(R){throw i?.error({error:String(R),name:O},"Failed to connect to database"),R}}}h=Bun.serve({port:e,hostname:r,fetch:c}),V.bunServer=h;let o=`http://${r}:${e}`;console.log(`\u2192 URL: ${o}
2
+ ${JSON.stringify(u,null,2)}`:JSON.stringify(i);e==="error"||e==="fatal"?console.error(p):e==="warn"?console.warn(p):console.log(p);}};var w=class extends Error{constructor(r,t=500,n){super(r);this.message=r;this.statusCode=t;this.code=n;this.name="AppError";}},E=class extends w{constructor(r,t){super(r,400,"VALIDATION_ERROR");this.issues=t;this.name="ValidationError";}},W=class extends w{constructor(e){super(e,500,"DATABASE_ERROR"),this.name="DatabaseError";}},k=class extends w{constructor(e="Request timeout"){super(e,408,"TIMEOUT_ERROR"),this.name="TimeoutError";}},G=class extends w{constructor(e="Too many requests"){super(e,429,"RATE_LIMIT_ERROR"),this.name="RateLimitError";}};var P=new N,L=new _;function de(s={}){let e=Number(s.port)||3e3,r=s.hostname||"localhost",t=s.maxRequestSize||10*1024*1024,n=s.requestTimeout||3e4,a=s.gracefulShutdownTimeout||1e4,u=typeof s.logging=="object"?s.logging:{},i=s.logging?new $(u.level||"info",u.pretty):null,p=new Map,b=[],x=new Set,T=setInterval(()=>{P.cleanupRateLimit(),P.cleanupCsrfTokens();},120*1e3);async function c(o){let m=Date.now(),l=crypto.randomUUID(),R=new URL(o.url).pathname,A=o.method.toUpperCase(),f=he(o);x.add(l);try{let y=o.headers.get("content-length");if(y&&parseInt(y)>t)return i?.warn({requestId:l,size:y,ip:f},"Request too large"),new Response(JSON.stringify({error:"Payload too large"}),{status:413,headers:{"Content-Type":"application/json"}});let I=fe(o,s);if(A==="OPTIONS")return new Response(null,{status:204,headers:I});if(s.security&&typeof s.security=="object"&&s.security.rateLimit){let C=typeof s.security.rateLimit=="object"?s.security.rateLimit:{},v=C.max||100,j=C.windowMs||6e4,F=C.keyGenerator?C.keyGenerator({request:o,ip:f}):f;if(!P.checkRateLimit(F,v,j))return i?.warn({requestId:l,ip:f,key:F},"Rate limit exceeded"),new Response(JSON.stringify({error:C.message||"Too many requests"}),{status:429,headers:{"Content-Type":"application/json"}})}let M=null;["POST","PUT","PATCH"].includes(A)&&(M=await ge(o,i,t));let H=p.get("default"),B=L.match(A,R);if(!B){let C=X(o,{},H,i,l);return i?.warn({requestId:l,method:A,path:R,ip:f},"Route not found"),C.json({error:"Not Found",path:R},404)}let U=X(o,B.params||{},H,i,l);U.body=M,U.request=o;let Q=new AbortController,K=new Promise((C,v)=>{let j=setTimeout(()=>{Q.abort(),v(new k("Request timeout"));},n);Q.signal.addEventListener("abort",()=>clearTimeout(j));}),D=await Promise.race([B.handler(U),K]),S=new Headers(D.headers);I.forEach((C,v)=>{S.has(v)||S.set(v,C);}),S.set("X-Request-ID",l),S.set("X-Content-Type-Options","nosniff"),S.set("X-Frame-Options","DENY"),S.set("X-XSS-Protection","1; mode=block"),S.set("Referrer-Policy","strict-origin-when-cross-origin");let z=Date.now()-m;return P.logRequest(l,A,R,f,D.status,z),i?.info({requestId:l,method:A,path:R,status:D.status,duration:z,ip:f},"Request completed"),new Response(D.body,{status:D.status,headers:S})}catch(y){if(y instanceof w)return i?.warn({error:y.message,requestId:l,ip:f},`App error: ${y.message}`),new Response(JSON.stringify({error:y.message,code:y.code,requestId:l}),{status:y.statusCode,headers:{"Content-Type":"application/json"}});i?.error({error:String(y),requestId:l,ip:f},"Unhandled error");let I=process.env.NODE_ENV==="production"?"Internal Server Error":y.message;return new Response(JSON.stringify({error:I,requestId:l}),{status:500,headers:{"Content-Type":"application/json"}})}finally{x.delete(l);}}let d={method:"GET",path:"/health",handler:o=>o.json({status:"healthy",timestamp:new Date().toISOString(),uptime:process.uptime(),activeRequests:x.size})},g={method:"GET",path:"/readiness",handler:o=>{let m=p.size>0,l=m||p.size===0;return o.json({ready:l,checks:{database:m?"connected":"not configured",activeRequests:x.size},timestamp:new Date().toISOString()},l?200:503)}};s.routes&&s.routes.forEach(o=>{b.push(o),(Array.isArray(o.method)?o.method:[o.method]).forEach(l=>{L.register(l,o.path,o.handler,o);});}),b.push(d,g),L.register("GET","/health",d.handler,d),L.register("GET","/readiness",g.handler,g);let h=null,V={app:null,logger:i,db:p,bunServer:null,async start(){if(s.database){let m=Array.isArray(s.database)?s.database:[s.database];for(let l of m){let O=l.name||"default";try{if(typeof l.connection=="string"){let R=new q(l.connection);if(l.schema&&typeof l.schema=="object")for(let[A,f]of Object.entries(l.schema))f&&typeof f=="object"&&R.defineSchema(f);p.set(O,R),i?.info({name:O,connection:l.connection},"\u2705 Database connected");}else throw new Error(`Database connection must be a string path (got ${typeof l.connection})`)}catch(R){throw i?.error({error:String(R),name:O},"Failed to connect to database"),R}}}h=Bun.serve({port:e,hostname:r,fetch:c}),V.bunServer=h;let o=`http://${r}:${e}`;console.log(`\u2192 URL: ${o}
3
3
  \u2192 Environment: ${process.env.NODE_ENV||"development"}
4
4
  \u2192 Routes: ${b.length.toString()}
5
5
  \u2192 Database: ${p.size>0?"\u2705 Connected":"\u274C None"}