@mateosuarezdev/brpc 1.0.5 → 1.0.7

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/dist/context.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { type BaseContext } from "./types";
1
+ import type { BaseContext } from "./types";
2
2
  export declare function createContext<C extends BaseContext>(contextCreator: (req: Request) => Promise<C>): (req: Request) => Promise<C>;
@@ -0,0 +1 @@
1
+ export * from "./BRPCError";
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  export * from "./types";
2
- export { createProcedure } from "./procedure";
3
- export { createRouter } from "./router";
4
- export { createContext } from "./context";
2
+ export * from "./procedure";
3
+ export * from "./router";
4
+ export * from "./context";
5
5
  export * from "@mateosuarezdev/headers-builder";
6
6
  export * from "./schemas";
7
7
  export * from "./middlewares";
8
- export * from "./BRPCError";
8
+ export * from "./errors";
9
+ export * from "./stream";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // @bun
2
- import{z as U}from"zod";class H{middlewares=[];constructor(F=[]){this.middlewares=F}use(F){return new H([...this.middlewares,F])}input(F){return new b(this.middlewares,F)}query(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"query",handler:F,middlewares:this.middlewares}}mutation(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"mutation",handler:F,middlewares:this.middlewares}}formMutation(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"formMutation",handler:F,middlewares:this.middlewares}}subscription(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"subscription",handler:F,middlewares:this.middlewares}}file(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"file",handler:F,middlewares:this.middlewares}}fileStream(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"fileStream",handler:F,middlewares:this.middlewares}}html(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"html",handler:F,middlewares:this.middlewares}}}class b{middlewares;inputSchema;constructor(F,J){this.middlewares=F;this.inputSchema=J}query(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"query",handler:F,middlewares:this.middlewares}}mutation(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"mutation",handler:F,middlewares:this.middlewares}}formMutation(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"formMutation",handler:F,middlewares:this.middlewares}}subscription(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"subscription",handler:F,middlewares:this.middlewares}}file(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"file",handler:F,middlewares:this.middlewares}}fileStream(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"fileStream",handler:F,middlewares:this.middlewares}}html(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"html",handler:F,middlewares:this.middlewares}}}function x(){return new H}import{ZodError as P}from"zod";class L extends Error{code;clientCode;httpStatus;data;cause;static STATUS_MAP={BAD_REQUEST:400,UNAUTHORIZED:401,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_SUPPORTED:405,TIMEOUT:408,CONFLICT:409,PRECONDITION_FAILED:412,PAYLOAD_TOO_LARGE:413,UNPROCESSABLE_CONTENT:422,TOO_MANY_REQUESTS:429,CLIENT_CLOSED_REQUEST:499,INTERNAL_SERVER_ERROR:500,NOT_IMPLEMENTED:501,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504};constructor(F){super(F.message);if(this.name="BRPCError",this.code=F.code,this.clientCode=F.clientCode,this.httpStatus=L.STATUS_MAP[F.code],this.data=F.data,this.cause=F.cause,Error.captureStackTrace)Error.captureStackTrace(this,L)}toJSON(){return{name:this.name,code:this.code,clientCode:this.clientCode,message:this.message,data:this.data,httpStatus:this.httpStatus}}static badRequest(F,J,_){return new L({code:"BAD_REQUEST",message:F,clientCode:J,data:_})}static unauthorized(F="Unauthorized",J,_){return new L({code:"UNAUTHORIZED",message:F,clientCode:J,data:_})}static forbidden(F="Forbidden",J,_){return new L({code:"FORBIDDEN",message:F,clientCode:J,data:_})}static notFound(F="Not Found",J,_){return new L({code:"NOT_FOUND",message:F,clientCode:J,data:_})}static preconditionFailed(F="Precondition failed",J,_){return new L({code:"NOT_FOUND",message:F,clientCode:J,data:_})}static conflict(F,J,_){return new L({code:"CONFLICT",message:F,clientCode:J,data:_})}static unprocessableContent(F,J,_){return new L({code:"UNPROCESSABLE_CONTENT",message:F,clientCode:J,data:_})}static tooManyRequests(F="Too many requests",J,_){return new L({code:"TOO_MANY_REQUESTS",message:F,clientCode:J,data:_})}static internalServerError(F="Internal Server Error",J,_){return new L({code:"INTERNAL_SERVER_ERROR",message:F,clientCode:J,data:_})}static timeout(F="Request timeout",J,_){return new L({code:"TIMEOUT",message:F,clientCode:J,data:_})}}class M{routeTree;routeCache;options;routeStats;constructor(F,J={}){if(this.options={enableCaching:!0,maxCacheSize:1000,debug:!1,...J},this.routeCache=new Map,this.routeStats=this.analyzeRoutes(F),this.routeTree=this.compileRoutes(F),this.options.debug)console.log("[ROUTEMATCHER]","initialized with stats:",this.routeStats)}match(F){if(this.options.enableCaching)return this.matchWithCache(F);return this.matchDirect(F)}matchWithCache(F){let J=F.join("/");if(this.routeCache.has(J)){let W=this.routeCache.get(J);if(this.options.debug&&W)console.log("[ROUTEMATCHER]",`\uD83C\uDFAF Cache hit for route: /${J}`);return W}let _=this.matchDirect(F);if(this.cacheResult(J,_),this.options.debug)console.log("[ROUTEMATCHER]",`\uD83D\uDCBE Cached route: /${J} -> ${_?"found":"not found"}`);return _}matchDirect(F){let J=this.options.debug?performance.now():0,_=this.traverseTree(this.routeTree,F,0,{});if(this.options.debug){let W=performance.now()-J,X=_?.route._type||"none";console.log("[ROUTEMATCHER]",`\u26A1 Route match: /${F.join("/")} (${X}) took ${W.toFixed(3)}ms`)}return _}traverseTree(F,J,_,W){if(_>=J.length){if(F.handler)return{params:W,route:F.handler.handler};if(F.index)return{params:W,route:F.index.handler};return null}let X=J[_],$=decodeURIComponent(X),Y=F.static.get($);if(Y){let A=this.traverseTree(Y,J,_+1,W);if(A)return A}if(F.param){let A={...W,[F.param.name]:$},j=this.traverseTree(F.param.node,J,_+1,A);if(j)return j}if(F.wildcard){let A=J.slice(_).join("/");return{params:{...W,wildcardPath:A},route:F.wildcard.handler}}return null}compileRoutes(F,J=[]){let _={static:new Map};for(let[W,X]of Object.entries(F)){if(W==="index"&&this.isProcedure(X)){_.index={handler:X,paramNames:[...J],hasWildcard:!1};continue}if(W==="*"){if(this.isProcedure(X))_.wildcard={handler:X,paramNames:[...J,"wildcardPath"],hasWildcard:!0,wildcardIndex:J.length};continue}if(this.isProcedure(X)){let $={handler:X,paramNames:[...J],hasWildcard:!1};if(W.startsWith(":")){let Y=W.slice(1),A={static:new Map,handler:$};_.param={name:Y,node:A}}else{let Y={static:new Map,handler:$};_.static.set(W,Y)}}else if(typeof X==="object"&&X!==null)if(W.startsWith(":")){let $=W.slice(1),Y=this.compileRoutes(X,[...J,$]);_.param={name:$,node:Y}}else{let $=this.compileRoutes(X,J);_.static.set(W,$)}}return _}isProcedure(F){return F&&typeof F==="object"&&"_type"in F&&"_input"in F}cacheResult(F,J){if(this.routeCache.size>=this.options.maxCacheSize){let _=this.routeCache.keys().next().value;if(_!==void 0){if(this.routeCache.delete(_),this.options.debug)console.log(`\uD83D\uDDD1\uFE0F Evicted oldest cache entry: ${_}`)}}this.routeCache.set(F,J)}analyzeRoutes(F){let J={totalRoutes:0,staticRoutes:0,paramRoutes:0,wildcardRoutes:0,indexRoutes:0,maxDepth:0,procedureTypes:{query:0,mutation:0,subscription:0,file:0,fileStream:0,streamQuery:0,streamMutation:0,formMutation:0,html:0}},_=(W,X=0)=>{J.maxDepth=Math.max(J.maxDepth,X);for(let[$,Y]of Object.entries(W))if(this.isProcedure(Y)){if(J.totalRoutes++,Y._type in J.procedureTypes)J.procedureTypes[Y._type]++;if($==="index")J.indexRoutes++;else if($.startsWith(":"))J.paramRoutes++;else if($==="*")J.wildcardRoutes++;else J.staticRoutes++}else if(typeof Y==="object"&&Y!==null)_(Y,X+1)};return _(F),J}clearCache(){if(this.routeCache.clear(),this.options.debug)console.log("[ROUTEMATCHER]","\uD83E\uDDF9 Route cache cleared")}getCacheStats(){return{size:this.routeCache.size,maxSize:this.options.maxCacheSize,routeStats:this.routeStats}}getOptimizationSuggestions(){let F=[],{routeStats:J}=this;if(J.paramRoutes>J.staticRoutes*2)F.push("\uD83D\uDE80 Consider grouping routes to reduce parameter route overhead");if(J.maxDepth>6)F.push("\uD83D\uDCCA Deep route nesting detected - consider flattening some routes");if(J.totalRoutes>200)F.push("\u26A1 Large route table - consider route splitting or lazy loading");if(!this.options.enableCaching&&J.totalRoutes>50)F.push("\uD83D\uDCBE Enable caching for better performance with many routes");if(J.procedureTypes.file>20)F.push("\uD83D\uDCC1 Many file routes detected - consider static file serving");if(J.procedureTypes.subscription>10)F.push("\uD83D\uDD04 Many subscription routes - ensure WebSocket optimization");return F}findRoutesByPattern(F){return[]}getRouteInfo(F){let J=F.split("/").filter(Boolean),_=J.join("/"),W=this.routeCache.has(_),X=performance.now(),$=this.matchDirect(J),Y=performance.now()-X;return{found:!!$,route:$?.route,params:$?.params,cached:W,matchTime:Y}}benchmark(F,J=1000){let _=F.map((G)=>G.split("/").filter(Boolean)),W=new Map(this.routeCache);this.routeCache.clear();let X=performance.now();for(let G=0;G<J;G++)for(let V of _)this.matchDirect(V);let Y=performance.now()-X,A=J*F.length,j=Y/A,I=A/Y*1000;return this.routeCache=W,{averageTime:j,totalTime:Y,routesPerSecond:I}}warmupCache(F){if(!this.options.enableCaching)return;let J=performance.now(),_=0;for(let X of F){let $=X.split("/").filter(Boolean),Y=this.matchDirect($);if(Y)this.cacheResult($.join("/"),Y),_++}let W=performance.now();if(this.options.debug)console.log("[ROUTEMATCHER]",`\uD83D\uDD25 Cache warmed up: ${_} routes in ${(W-J).toFixed(2)}ms`)}}var C=import.meta.require,q={NO_CACHE:{cacheControl:"no-cache, no-store, must-revalidate",expires:new Date(0).toUTCString()},ONE_YEAR:{cacheControl:"public, max-age=31536000",expires:new Date(Date.now()+31536000000).toUTCString()},IMMUTABLE:{cacheControl:"public, max-age=31536000, immutable",expires:new Date(Date.now()+31536000000).toUTCString()},ONE_MONTH:{cacheControl:"public, max-age=2592000"},ONE_WEEK:{cacheControl:"public, max-age=604800"},ONE_DAY:{cacheControl:"public, max-age=86400, must-revalidate"},ONE_HOUR:{cacheControl:"public, max-age=3600, must-revalidate"},FIVE_MINUTES:{cacheControl:"public, max-age=300"},API:{cacheControl:"public, max-age=300, s-maxage=3600"},STYLESHEET:{cacheControl:"public, max-age=2592000, must-revalidate"},JAVASCRIPT:{cacheControl:"public, max-age=2592000, must-revalidate"},HASHED_ASSET:{cacheControl:"public, max-age=31536000, immutable",expires:new Date(Date.now()+31536000000).toUTCString()},FONT:{cacheControl:"public, max-age=31536000",expires:new Date(Date.now()+31536000000).toUTCString()},IMAGE:{cacheControl:"public, max-age=2592000"},FAVICON:{cacheControl:"public, max-age=31536000",expires:new Date(Date.now()+31536000000).toUTCString()},HTML_PAGE:{cacheControl:"public, max-age=3600, must-revalidate"},API_RESPONSE:{cacheControl:"public, max-age=300, s-maxage=900"},FEED:{cacheControl:"public, max-age=3600"},SITEMAP:{cacheControl:"public, max-age=86400"},MANIFEST:{cacheControl:"public, max-age=86400, must-revalidate"},SERVICE_WORKER:{cacheControl:"no-cache, no-store, must-revalidate",expires:new Date(0).toUTCString()},MEDIA_STREAM:{cacheControl:"no-cache, no-store"},DOCUMENT:{cacheControl:"public, max-age=86400"},ARCHIVE:{cacheControl:"public, max-age=604800"}};function v(F){return q[F]}var y={html:"text/html; charset=utf-8",css:"text/css",javascript:"application/javascript",json:"application/json",xml:"application/xml",text:"text/plain; charset=utf-8",csv:"text/csv",markdown:"text/markdown; charset=utf-8",png:"image/png",jpeg:"image/jpeg",gif:"image/gif",svg:"image/svg+xml",webp:"image/webp",icon:"image/x-icon",bmp:"image/bmp",woff:"font/woff",woff2:"font/woff2",truetype:"font/ttf","embedded-opentype":"application/vnd.ms-fontobject",opentype:"font/otf",mp4:"video/mp4",webm:"video/webm",ogg:"video/ogg",mpeg:"audio/mpeg",wav:"audio/wav",flac:"audio/flac",pdf:"application/pdf",doc:"application/msword",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",zip:"application/zip",tar:"application/x-tar",gzip:"application/gzip","7z":"application/x-7z-compressed",js:"application/javascript",txt:"text/plain; charset=utf-8",jpg:"image/jpeg",ico:"image/x-icon",ttf:"font/ttf",eot:"application/vnd.ms-fontobject",otf:"font/otf",mp3:"audio/mpeg",gz:"application/gzip"};function K(F){return y[F]||"application/octet-stream"}function z(F){if(typeof Bun!=="undefined"&&Bun.hash)return Bun.hash(F).toString(16);try{let J=C("crypto"),_;if(typeof F==="string")_=Buffer.from(F);else if(F instanceof ArrayBuffer)_=Buffer.from(new Uint8Array(F));else _=Buffer.from(F);return J.createHash("md5").update(_).digest("hex")}catch{let J=typeof F==="string"?new TextEncoder().encode(F):F instanceof ArrayBuffer?new Uint8Array(F):F,_=0;for(let W=0;W<J.length;W++)_=(_<<5)-_+J[W],_=_&_;return Math.abs(_).toString(16).padStart(16,"0")}}function D(F,J,_){let W=F.headers.get("Origin"),X=!0;if((_?.allowAll??!0)&&W)return W;if(J.length===0)return W||"*";if(!W)return J[0];if(J.includes(W))return W;return J[0]}class B{headers={};contentType(F){return this.headers["Content-Type"]=K(F),this}filePath(F){let J=F.split(".").pop()?.toLowerCase()||"";return this.headers["Content-Type"]=K(J),this}mimeType(F){return this.headers["Content-Type"]=F,this}cache(F){let{cacheControl:J,expires:_}=v(F);if(this.headers["Cache-Control"]=J,_)this.headers.Expires=_;return this}eTag(F){if(!F)return this;let J;if(typeof F==="string"&&F.startsWith('"'))J=F;else J=`"${z(F)}"`;return this.headers.ETag=J,this}lastModified(F){return this.headers["Last-Modified"]=F.toUTCString(),this}contentLength(F){return this.headers["Content-Length"]=F.toString(),this}redirect(F,J=!1){return this.headers.Location=F,this.headers["X-Redirect-Type"]=J?"permanent":"temporary",this}cors({origin:F="*",methods:J=["GET","POST","PUT","DELETE","OPTIONS"],headers:_=["Content-Type","Authorization"],maxAge:W=86400,credentials:X}={}){let $=X??F!=="*";if($&&F==="*")throw new Error('CORS: Cannot use credentials with wildcard origin "*". Specify an exact origin or set credentials to false.');if(this.headers["Access-Control-Allow-Origin"]=F,this.headers["Access-Control-Allow-Methods"]=J.join(", "),this.headers["Access-Control-Allow-Headers"]=_.join(", "),$)this.headers["Access-Control-Allow-Credentials"]="true";return this.headers["Access-Control-Max-Age"]=W.toString(),this}security(F){let J={hsts:!0,noSniff:!0,frameOptions:"SAMEORIGIN",xssProtection:!0,...F};if(J.csp)this.headers["Content-Security-Policy"]=J.csp;if(J.hsts){let _=typeof J.hsts==="number"?J.hsts:31536000;this.headers["Strict-Transport-Security"]=`max-age=${_}; includeSubDomains`}if(J.noSniff)this.headers["X-Content-Type-Options"]="nosniff";if(J.frameOptions)this.headers["X-Frame-Options"]=J.frameOptions;if(J.xssProtection)this.headers["X-XSS-Protection"]="1; mode=block";return this}custom(F,J){return this.headers[F]=J,this}customHeaders(F){return Object.assign(this.headers,F),this}vary(...F){let J=this.headers.Vary,_=J?`${J}, ${F.join(", ")}`:F.join(", ");return this.headers.Vary=_,this}compress(F){if(F)this.headers["Content-Encoding"]=F;return this}build(F){let J={...this.headers};if(F&&!J["Content-Length"]){let _=typeof F==="string"?new TextEncoder().encode(F).length:F.byteLength;J["Content-Length"]=_.toString()}return J}buildHeaders(F){return new Headers(this.build(F))}}function Z(){return new B}function u(F,J){let _=Z().contentType(F);if(J)_.cache(J);return _.build()}var l={css:(F)=>Z().contentType("css").cache("STYLESHEET").eTag(F),javascript:(F)=>Z().contentType("javascript").cache("JAVASCRIPT").eTag(F),hashedAsset:(F,J)=>Z().filePath(F).cache("HASHED_ASSET").eTag(J),font:(F)=>Z().filePath(F).cache("FONT").cors({origin:"*"}),image:(F)=>Z().filePath(F).cache("IMAGE"),favicon:()=>Z().contentType("icon").cache("FAVICON"),api:()=>Z().contentType("json").cache("API_RESPONSE").cors(),expensiveApi:(F)=>Z().contentType("json").cache("API_RESPONSE").cors().eTag(F),realtime:()=>Z().contentType("json").cache("NO_CACHE").cors(),html:()=>Z().contentType("html").cache("HTML_PAGE").security(),expensiveHtml:(F)=>Z().contentType("html").cache("HTML_PAGE").security().eTag(F),manifest:(F)=>Z().contentType("json").cache("MANIFEST").eTag(F),serviceWorker:()=>Z().contentType("javascript").cache("SERVICE_WORKER"),sitemap:(F)=>Z().contentType("xml").cache("SITEMAP").eTag(F),feed:(F,J="rss")=>Z().contentType("xml").cache("FEED").eTag(F).mimeType(J==="rss"?"application/rss+xml":"application/atom+xml"),video:(F)=>Z().filePath(F).cache("MEDIA_STREAM").custom("Accept-Ranges","bytes"),audio:(F)=>Z().filePath(F).cache("MEDIA_STREAM").custom("Accept-Ranges","bytes"),pdf:(F)=>Z().contentType("pdf").cache("DOCUMENT").custom("Content-Disposition","inline").eTag(F),download:(F,J,_)=>Z().filePath(J).cache("ARCHIVE").custom("Content-Disposition",`attachment; filename="${F}"`).eTag(_),secure:()=>Z().security({csp:"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'",hsts:31536000}).custom("X-Powered-By","Bun"),permanentRedirect:(F)=>Z().redirect(F,!0),temporaryRedirect:(F)=>Z().redirect(F,!1)};async function S(F,J,_,W={}){let{maxChunkSize:X=2097152,cacheMaxAge:$=3600,acceptedExtensions:Y}=W;if(!await F.exists())return new Response("Media file not found",{status:404,headers:Z().contentType("text").cors({origin:D(J,_)}).build()});if(Y&&Y.length>0){let O=F.name?.toLowerCase().split(".").pop();if(!O||!Y.includes(O))return new Response("File type not supported",{status:415,headers:Z().contentType("text").cors({origin:D(J,_)}).build()})}let j=F.size,I=J.headers.get("Range");if(!I)return new Response(F,{headers:Z().filePath(F.name||"media").custom("Accept-Ranges","bytes").custom("Cache-Control",`public, max-age=${$}`).cors({origin:D(J,_)}).build()});let G=I.match(/bytes=(\d*)-(\d*)/);if(!G)return new Response("Invalid Range header",{status:416,headers:Z().contentType("text").cors({origin:D(J,_)}).custom("Content-Range",`bytes */${j}`).build()});let V=G[1]?parseInt(G[1]):0,N=G[2]?parseInt(G[2]):j-1,Q=Math.min(N,V+X-1,j-1);if(V>=j||Q>=j||V>Q)return new Response("Range not satisfiable",{status:416,headers:Z().contentType("text").cors({origin:D(J,_)}).custom("Content-Range",`bytes */${j}`).build()});return new Response(F.slice(V,Q+1),{status:206,headers:Z().filePath(F.name||"media").custom("Accept-Ranges","bytes").custom("Cache-Control",`public, max-age=${$}`).cors({origin:D(J,_)}).build()})}class E{routes;routeMatcher;contextCreator;prefix;globalMiddlewares;server=null;wsConfig;integrations;development=!0;debug=!1;allowedOrigins=[];activeConnections=new Set;maxConnections=parseInt(process.env.MAX_WS_CONNECTIONS??"1000");connectionTimeout=parseInt(process.env.WS_TIMEOUT??"30000");maxRequestSize=parseInt(process.env.MAX_REQUEST_SIZE??"10485760");requestTimeout=parseInt(process.env.REQUEST_TIMEOUT??"30000");constructor(F){if(this.contextCreator=F.context,this.prefix=F.prefix??"",this.routes=F.routes,this.globalMiddlewares=F.globalMiddlewares??[],this.wsConfig=F.websocket??{},this.integrations=F.integrations,this.debug=!!F.debug&&!0,this.routeMatcher=new M(F.routes,{enableCaching:!this.development,maxCacheSize:parseInt(process.env.ROUTE_CACHE_SIZE||"1000"),debug:this.debug}),!this.development)setInterval(()=>this.cleanupStaleConnections(),60000);if(this.debug)console.log("Router initialized with route matcher"),console.log("Route cache stats:",this.routeMatcher.getCacheStats())}findRoute(F){return this.routeMatcher.match(F)}async parseInput(F,J,_){let W={},X=new URL(F.url),$=Object.fromEntries(X.searchParams),Y=F.method;if(Y==="POST"){if(parseInt(F.headers.get("content-length")||"0")>this.maxRequestSize)throw new Error("Request payload too large")}if(Y==="GET"&&["query","streamQuery","file","fileStream","html"].includes(_))W=$;else if(Y==="POST"&&["mutation","formMutation","streamMutation"].includes(_)){let A=F.headers.get("content-type");if(A?.includes("application/json"))W=await F.json();else if(A?.includes("multipart/form-data")){let j=await F.formData();W=await this.parseFormData(j)}else throw new Error("Unsupported content type")}else throw new Error(`Unsupported request method ${Y} for procedure type ${_}`);return J.parse(W)}async parseFormData(F){let J={},_={};F.forEach(async(W,X)=>{if(W instanceof File){if(!_[X])_[X]=[];_[X].push(W)}else if(typeof W==="string")try{J[X]=JSON.parse(W)}catch{J[X]=W}});for(let[W,X]of Object.entries(_))J[W]=X.length===1?X[0]:X;return J}handleError(F,J){if(F?.errors&&Array.isArray(F.errors)&&F.name==="ZodError")return new Response(JSON.stringify({error:{name:"ValidationError",code:"BAD_REQUEST",clientCode:"VALIDATION_ERROR",message:"Input validation failed",data:{validationErrors:F.errors.map((_)=>({path:_.path.join("."),message:_.message,code:_.code,received:_.received}))},httpStatus:400}}),{status:400,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()});if(F instanceof L)return new Response(JSON.stringify({error:F.toJSON()}),{status:F.httpStatus,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()});if(F instanceof Error){let _=F.message==="Not Found"?404:F.message==="Unauthorized"?401:F.message==="Forbidden"?403:F.message==="Request payload too large"?413:F.message==="Request timeout"?408:500;return new Response(JSON.stringify({error:{code:_===404?"NOT_FOUND":_===401?"UNAUTHORIZED":_===403?"FORBIDDEN":_===413?"PAYLOAD_TOO_LARGE":_===408?"TIMEOUT":"INTERNAL_SERVER_ERROR",message:F.message}}),{status:_,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()})}return new Response(JSON.stringify({error:{code:"INTERNAL_SERVER_ERROR",message:"Internal Server Error"}}),{status:500,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()})}handleFileCacheControl(F){let J;if(F?.includes("text/html"))J="public, max-age=0, must-revalidate";else if(F?.includes("text/css")||F?.includes("text/javascript")||F?.includes("application/javascript"))J="public, max-age=31536000, immutable";else J="public, max-age=86400, stale-while-revalidate=604800";return J}async handleRequest(F,J){let _=new Promise((W,X)=>{setTimeout(()=>X(new Error("Request timeout")),this.requestTimeout)});try{return await Promise.race([this._handleRequest(F,J),_])}catch(W){return this.handleError(W,F)}}async _handleRequest(F,J){try{if(F.method==="OPTIONS")return new Response(null,{status:204,headers:Z().cors({origin:D(F,this.allowedOrigins)}).build()});let W=new URL(F.url).pathname;if(W=W.startsWith(this.prefix)?W.slice(this.prefix.length):W,W=W.startsWith("/")?W:"/"+W,W==="/ws"&&J){if(this.debug)console.log("\uD83D\uDD0C WebSocket upgrade requested");return this.handleWebSocketUpgrade(F,J)}let X=this.findRoute(W.split("/").filter(Boolean));if(!X)return new Response("Not Found",{status:404,headers:Z().contentType("text").cors({origin:D(F,this.allowedOrigins)}).build()});let{params:$,route:Y}=X;if(!Y.handler)return new Response("Handler not implemented",{status:501,headers:Z().contentType("text").cors({origin:D(F,this.allowedOrigins)}).build()});let A=await this.contextCreator(F);Object.assign(A,{params:$,publishToProcedure:async(G,V,N)=>{await this.publishToProcedure(G,V,A,N)}});for(let G of this.globalMiddlewares)await G(A);for(let G of Y.middlewares)await G(A);let j=await this.parseInput(F,Y._input,Y._type),I=await Y.handler({ctx:A,input:j});return await this.handleProcedureResponse(Y._type,I,Z().cors({origin:D(F,this.allowedOrigins)}).build())}catch(_){return this.handleError(_,F)}}async handleProcedureResponse(F,J,_){switch(F){case"file":if(J instanceof Response)return J;let W=J.type;return new Response(J,{headers:{..._,"Content-Type":W,"Cache-Control":this.development?"public, max-age=0, must-revalidate":this.handleFileCacheControl(W)}});case"fileStream":if(typeof J==="object"&&J!==null&&"type"in J&&J.type==="stream"){let $=J;return await S(J.file,J.request,this.allowedOrigins,$.options)}let X=J.type;return new Response(J,{headers:{..._,"Content-Type":X,"Cache-Control":this.development?"public, max-age=0, must-revalidate":this.handleFileCacheControl(X)}});case"html":if(typeof J==="string")return new Response(J,{headers:{..._,"Content-Type":"text/html"}});break;case"query":case"mutation":case"formMutation":case"streamQuery":case"streamMutation":case"subscription":default:return new Response(JSON.stringify({data:J}),{headers:{..._,"Content-Type":"application/json"}})}return new Response(JSON.stringify({data:J}),{headers:{..._,"Content-Type":"application/json"}})}cleanupStaleConnections(){let F=Date.now();for(let J of this.activeConnections)if(F-J.data.lastActivity>this.connectionTimeout)J.close(),this.activeConnections.delete(J)}async handleWebSocketUpgrade(F,J){if(this.activeConnections.size>=this.maxConnections)return new Response("Too many connections",{status:503});let _=await this.contextCreator(F);for(let X of this.globalMiddlewares)await X(_);if(J.upgrade(F,{data:{ctx:_,topics:new Set,lastActivity:Date.now()}}))return new Response(null,{status:101});return new Response("WebSocket upgrade failed",{status:500})}async handleAuthenticate(F,J,_){try{let W=J.startsWith("Bearer ")?J.substring(7):J;if(F.data.ctx.authToken=W,F.send(JSON.stringify({type:"auth_success",message:"Authentication successful"})),this.debug)console.log("[BRPC]","WebSocket authenticated successfully")}catch(W){if(F.send(JSON.stringify({type:"auth_error",error:W instanceof Error?W.message:"Authentication failed"})),this.debug)console.error("WebSocket authentication error:",W)}}async handleSubscribe(F,J,_){let W=this.findRoute(J.split("/").filter(Boolean));if(!W||W.route._type!=="subscription")throw new Error("Invalid subscription route");F.subscribe(J),F.data.topics.add(J)}async handleUnsubscribe(F,J,_){let W=this.findRoute(J.split("/").filter(Boolean));if(!W||W.route._type!=="subscription")throw new Error("Invalid subscription route");F.unsubscribe(J),F.data.topics.delete(J)}async handlePublish(F,J,_,W){let X=this.findRoute(J.split("/").filter(Boolean));if(!X)throw new Error("Subscription not found");let{params:$,route:Y}=X;if(Y._type!=="subscription"||!Y.handler)throw new Error("Invalid subscription route");Object.assign(W,{params:$});let A=Y._input.fields?Y._input.parse(_):_,j=await Y.handler({ctx:W,input:A}),I={topic:J,data:j,type:"publish"};if(this.debug)console.log(`Server will publish to ${J}`,I);this.server.publish(J,JSON.stringify(I))}async publishToProcedure(F,J,_,W){if(F._type!=="subscription"||!F.handler)throw new Error("Can only publish to subscription procedures");let X=this.findRoutePath(this.routes,F);if(!X)throw new Error("Subscription route not found");let $=X.split("/").map((j)=>{if(j.startsWith(":")){let I=j.slice(1),G=W[I];if(!G)throw new Error(`Missing parameter: ${I}`);return G}return j}).join("/"),Y=await F.handler({ctx:{..._,params:W},input:J}),A={topic:$,data:Y,type:"publish"};if(this.debug)console.log(`Server will publish to ${$}`,A);return this.server?.publish($,JSON.stringify(A)),Y}async publish(F,J,_){if(F._type!=="subscription"||!F.handler)throw new Error("Can only publish to subscription procedures");let W=this.findRoutePath(this.routes,F);if(!W)throw new Error("Subscription route not found");let X=W.split("/").map((A)=>{if(A.startsWith(":")){let j=A.slice(1),I=_?_[j]:null;if(!I)throw new Error(`Missing parameter: ${j}`);return I}return A}).join("/"),$=await F.handler({ctx:{params:_},input:J}),Y={topic:X,data:$,type:"publish"};if(this.debug)console.log(`Server will publish to ${X}`,Y);return this.server?.publish(X,JSON.stringify(Y)),$}findRoutePath(F,J,_=""){for(let[W,X]of Object.entries(F)){let $=_?`${_}/${W}`:W;if(X===J)return $;if(X&&typeof X==="object"&&!("_type"in X)){let Y=this.findRoutePath(X,J,$);if(Y)return Y}}return null}getIntegrationRoutes(){if(!this.integrations)return;let F={};if(this.integrations.betterAuth){let J=this.integrations.betterAuth.handler;if(!J)throw new Error("Please provide a handler for betterAuth integration");F["/api/auth/*"]=async(_)=>{return await J(_)}}if(this.integrations.rawRoutes)Object.keys(this.integrations.rawRoutes).forEach((J)=>{F[J]=this.integrations.rawRoutes[J]});return Object.keys(F).length>0?F:void 0}getRouteStats(){return{cache:this.routeMatcher.getCacheStats(),suggestions:this.routeMatcher.getOptimizationSuggestions()}}clearRouteCache(){if(this.routeMatcher.clearCache(),this.debug)console.log("[ROUTEMATCHER] Route cache cleared")}benchmarkRoutes(F=["/users/123","/posts/456/comments"],J=1000){if(this.debug){let _=this.routeMatcher.benchmark(F,J);return console.log("[ROUTEMATCHER]","Route matching benchmark:",_),_}}updateRoutes(F){if(this.development)this.routes=F,this.routeMatcher=new M(F,{enableCaching:!1,debug:this.debug}),console.log("[HMR]","Server Routes updated");else console.warn("Route updates are only allowed in development mode")}freePublish(F,J){let _={topic:F,data:J,type:"publish"};this.server?.publish(F,JSON.stringify(_))}listen(F,J){if(this.server=Bun.serve({port:F,routes:this.getIntegrationRoutes(),fetch:(_,W)=>this.handleRequest(_,W),websocket:{open:(_)=>{if(this.activeConnections.add(_),this.wsConfig?.onOpen)this.wsConfig.onOpen(_,_.data.ctx);if(this.debug)console.log("[BRPC]","WebSocket connection opened",{connections:this.activeConnections.size})},message:async(_,W)=>{try{_.data.lastActivity=Date.now();let{type:X,topic:$,token:Y,data:A}=JSON.parse(W),j=_.data.ctx;if(this.debug)console.log("[BRPC]","WebSocket message received:",{type:X,topic:$});switch(X){case"authenticate":await this.handleAuthenticate(_,Y,j);break;case"subscribe":await this.handleSubscribe(_,$,j);break;case"unsubscribe":await this.handleUnsubscribe(_,$,j);break;case"publish":await this.handlePublish(_,$,A,j);break;default:if(_.send(JSON.stringify({error:"Unknown message type"})),this.debug)console.error("Unknown WebSocket message type:",X)}}catch(X){if(this.debug)console.error("[BRPC]","WebSocket message error:",X);if(X instanceof P)_.send(JSON.stringify({error:"Invalid message format"}));else if(X instanceof Error)_.send(JSON.stringify({error:X.message}))}},close:(_,W,X)=>{if(this.activeConnections.delete(_),_.data.topics.forEach(($)=>{_.unsubscribe($)}),this.debug)console.log("[BRPC]","WebSocket connection closed",{code:W,reason:X,remainingConnections:this.activeConnections.size});if(this.wsConfig?.onClose)this.wsConfig.onClose(_,W,X,_.data.ctx)}}}),J)J()}}function f(F){return new E(F)}function w(F){return F}import{z as k}from"zod";var h={image:["image/jpeg","image/png","image/gif","image/webp","image/svg+xml"],video:["video/mp4","video/webm","video/ogg"],audio:["audio/mpeg","audio/ogg","audio/wav"],document:["application/pdf","application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]},T=(F,J="MB")=>{switch(J){case"MB":return F*1024*1024;case"KB":return F*1024;case"B":return F}},W9=({acceptedTypes:F,maxSize:J=1/0,minSize:_,messages:W={}}={})=>{let X=J?T(J):1/0,$=_?T(_):0,Y=F?Object.entries(F).flatMap(([j,I])=>{let G=j;if(I==="*")return h[G];if(Array.isArray(I))return I;return[]}):void 0,A=k.instanceof(File,{message:W.required??"File is required"});if(A=A.refine((j)=>j.size<=X,W.maxSize??`File size must be less than ${J}MB`).refine((j)=>j.size>=$,W.minSize??`File size must be at least ${_}MB`),Y?.length)A=A.refine((j)=>Y.includes(j.type),W.type??`File must be of type: ${Y.join(", ")}`);return A};var Y9=(F)=>{let J=F.paths.map((W)=>new RegExp(W)),_="Not Found";return async(W)=>{let $=new URL(W.req.url).pathname;if(J.some((Y)=>Y.test($)))throw new Error("Not Found")}};class R{requests=new Map;lastCleanupTime=Date.now();alertThreshold;config;constructor(F){this.config={windowMs:F.windowMs,maxRequests:F.maxRequests,maxEntries:F.maxEntries??1e4,cleanupIntervalMs:F.cleanupIntervalMs??60000,message:F.message??"Too Many Requests",statusCode:F.statusCode??429,headerPrefix:F.headerPrefix??"X-RateLimit"},this.alertThreshold=this.config.maxEntries*0.8}cleanup(){let F=Date.now(),J=0;for(let[_,W]of this.requests.entries()){let X=F-W.startTime>this.config.windowMs,$=F-W.lastAccessed>this.config.windowMs*2;if(X||$)this.requests.delete(_),J++}return this.lastCleanupTime=F,J}handleMaxEntries(F){let J=this.requests.size;if(Date.now()-this.lastCleanupTime>this.config.cleanupIntervalMs)this.cleanup();if(J>=this.alertThreshold)console.warn(`Rate limiter at ${Math.round(J/this.config.maxEntries*100)}% capacity`);if(this.requests.has(F))return!0;return J<this.config.maxEntries}check(F){if(!this.handleMaxEntries(F))return{isLimited:!0,remaining:0,resetTime:Date.now()+this.config.windowMs};let J=Date.now(),_=this.requests.get(F);if(!_||J-_.startTime>=this.config.windowMs)return this.requests.set(F,{count:1,startTime:J,lastAccessed:J}),{isLimited:!1,remaining:this.config.maxRequests-1,resetTime:J+this.config.windowMs};_.lastAccessed=J;let W=_.count>=this.config.maxRequests;if(!W)_.count++;return{isLimited:W,remaining:Math.max(0,this.config.maxRequests-_.count),resetTime:_.startTime+this.config.windowMs}}getConfig(){return this.config}}var A9=(F)=>{let J=new R(F);return async(_)=>{if(_.req.headers.get("range"))return;let W=_.req.headers.get("x-forwarded-for")?.split(",")[0]||_.req.headers.get("x-real-ip")||"unknown",{isLimited:X,remaining:$,resetTime:Y}=J.check(W),{headerPrefix:A}=J.getConfig();if(_.setHeader(`${A}-Remaining`,$.toString()),_.setHeader(`${A}-Reset`,Y.toString()),X)throw _.setHeader(`${A}-Exceeded`,"true"),new Error(J.getConfig().message)}};export{u as quickHeaders,z as hashContent,K as getMimeType,D as getCorsOrigin,f as createRouter,A9 as createRateLimiter,x as createProcedure,Y9 as createPathBlocker,W9 as createFileSchema,w as createContext,l as commonHeaders,Z as buildHeaders,L as BRPCError};
2
+ import{z as U}from"zod";class H{middlewares=[];constructor(F=[]){this.middlewares=F}use(F){return new H([...this.middlewares,F])}input(F){return new O(this.middlewares,F)}query(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"query",handler:F,middlewares:this.middlewares}}mutation(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"mutation",handler:F,middlewares:this.middlewares}}formMutation(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"formMutation",handler:F,middlewares:this.middlewares}}subscription(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"subscription",handler:F,middlewares:this.middlewares}}file(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"file",handler:F,middlewares:this.middlewares}}fileStream(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"fileStream",handler:F,middlewares:this.middlewares}}html(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"html",handler:F,middlewares:this.middlewares}}}class O{middlewares;inputSchema;constructor(F,J){this.middlewares=F;this.inputSchema=J}query(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"query",handler:F,middlewares:this.middlewares}}mutation(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"mutation",handler:F,middlewares:this.middlewares}}formMutation(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"formMutation",handler:F,middlewares:this.middlewares}}subscription(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"subscription",handler:F,middlewares:this.middlewares}}file(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"file",handler:F,middlewares:this.middlewares}}fileStream(F){return{_input:this.inputSchema,_output:null,_ctx:null,_type:"fileStream",handler:F,middlewares:this.middlewares}}html(F){return{_input:U.object({}),_output:null,_ctx:null,_type:"html",handler:F,middlewares:this.middlewares}}}function h(){return new H}import{ZodError as f}from"zod";class L extends Error{code;clientCode;httpStatus;data;cause;static STATUS_MAP={BAD_REQUEST:400,UNAUTHORIZED:401,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_SUPPORTED:405,TIMEOUT:408,CONFLICT:409,PRECONDITION_FAILED:412,PAYLOAD_TOO_LARGE:413,UNPROCESSABLE_CONTENT:422,TOO_MANY_REQUESTS:429,CLIENT_CLOSED_REQUEST:499,INTERNAL_SERVER_ERROR:500,NOT_IMPLEMENTED:501,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504};constructor(F){super(F.message);if(this.name="BRPCError",this.code=F.code,this.clientCode=F.clientCode,this.httpStatus=L.STATUS_MAP[F.code],this.data=F.data,this.cause=F.cause,Error.captureStackTrace)Error.captureStackTrace(this,L)}toJSON(){return{name:this.name,code:this.code,clientCode:this.clientCode,message:this.message,data:this.data,httpStatus:this.httpStatus}}static badRequest(F,J,_){return new L({code:"BAD_REQUEST",message:F,clientCode:J,data:_})}static unauthorized(F="Unauthorized",J,_){return new L({code:"UNAUTHORIZED",message:F,clientCode:J,data:_})}static forbidden(F="Forbidden",J,_){return new L({code:"FORBIDDEN",message:F,clientCode:J,data:_})}static notFound(F="Not Found",J,_){return new L({code:"NOT_FOUND",message:F,clientCode:J,data:_})}static preconditionFailed(F="Precondition failed",J,_){return new L({code:"NOT_FOUND",message:F,clientCode:J,data:_})}static conflict(F,J,_){return new L({code:"CONFLICT",message:F,clientCode:J,data:_})}static unprocessableContent(F,J,_){return new L({code:"UNPROCESSABLE_CONTENT",message:F,clientCode:J,data:_})}static tooManyRequests(F="Too many requests",J,_){return new L({code:"TOO_MANY_REQUESTS",message:F,clientCode:J,data:_})}static internalServerError(F="Internal Server Error",J,_){return new L({code:"INTERNAL_SERVER_ERROR",message:F,clientCode:J,data:_})}static timeout(F="Request timeout",J,_){return new L({code:"TIMEOUT",message:F,clientCode:J,data:_})}}class M{routeTree;routeCache;options;routeStats;constructor(F,J={}){if(this.options={enableCaching:!0,maxCacheSize:1000,debug:!1,...J},this.routeCache=new Map,this.routeStats=this.analyzeRoutes(F),this.routeTree=this.compileRoutes(F),this.options.debug)console.log("[ROUTEMATCHER]","initialized with stats:",this.routeStats)}match(F){if(this.options.enableCaching)return this.matchWithCache(F);return this.matchDirect(F)}matchWithCache(F){let J=F.join("/");if(this.routeCache.has(J)){let W=this.routeCache.get(J);if(this.options.debug&&W)console.log("[ROUTEMATCHER]",`\uD83C\uDFAF Cache hit for route: /${J}`);return W}let _=this.matchDirect(F);if(this.cacheResult(J,_),this.options.debug)console.log("[ROUTEMATCHER]",`\uD83D\uDCBE Cached route: /${J} -> ${_?"found":"not found"}`);return _}matchDirect(F){let J=this.options.debug?performance.now():0,_=this.traverseTree(this.routeTree,F,0,{});if(this.options.debug){let W=performance.now()-J,X=_?.route._type||"none";console.log("[ROUTEMATCHER]",`\u26A1 Route match: /${F.join("/")} (${X}) took ${W.toFixed(3)}ms`)}return _}traverseTree(F,J,_,W){if(_>=J.length){if(F.handler)return{params:W,route:F.handler.handler};if(F.index)return{params:W,route:F.index.handler};return null}let X=J[_],$=decodeURIComponent(X),Y=F.static.get($);if(Y){let A=this.traverseTree(Y,J,_+1,W);if(A)return A}if(F.param){let A={...W,[F.param.name]:$},j=this.traverseTree(F.param.node,J,_+1,A);if(j)return j}if(F.wildcard){let A=J.slice(_).join("/");return{params:{...W,wildcardPath:A},route:F.wildcard.handler}}return null}compileRoutes(F,J=[]){let _={static:new Map};for(let[W,X]of Object.entries(F)){if(W==="index"&&this.isProcedure(X)){_.index={handler:X,paramNames:[...J],hasWildcard:!1};continue}if(W==="*"){if(this.isProcedure(X))_.wildcard={handler:X,paramNames:[...J,"wildcardPath"],hasWildcard:!0,wildcardIndex:J.length};continue}if(this.isProcedure(X)){let $={handler:X,paramNames:[...J],hasWildcard:!1};if(W.startsWith(":")){let Y=W.slice(1),A={static:new Map,handler:$};_.param={name:Y,node:A}}else{let Y={static:new Map,handler:$};_.static.set(W,Y)}}else if(typeof X==="object"&&X!==null)if(W.startsWith(":")){let $=W.slice(1),Y=this.compileRoutes(X,[...J,$]);_.param={name:$,node:Y}}else{let $=this.compileRoutes(X,J);_.static.set(W,$)}}return _}isProcedure(F){return F&&typeof F==="object"&&"_type"in F&&"_input"in F}cacheResult(F,J){if(this.routeCache.size>=this.options.maxCacheSize){let _=this.routeCache.keys().next().value;if(_!==void 0){if(this.routeCache.delete(_),this.options.debug)console.log(`\uD83D\uDDD1\uFE0F Evicted oldest cache entry: ${_}`)}}this.routeCache.set(F,J)}analyzeRoutes(F){let J={totalRoutes:0,staticRoutes:0,paramRoutes:0,wildcardRoutes:0,indexRoutes:0,maxDepth:0,procedureTypes:{query:0,mutation:0,subscription:0,file:0,fileStream:0,streamQuery:0,streamMutation:0,formMutation:0,html:0}},_=(W,X=0)=>{J.maxDepth=Math.max(J.maxDepth,X);for(let[$,Y]of Object.entries(W))if(this.isProcedure(Y)){if(J.totalRoutes++,Y._type in J.procedureTypes)J.procedureTypes[Y._type]++;if($==="index")J.indexRoutes++;else if($.startsWith(":"))J.paramRoutes++;else if($==="*")J.wildcardRoutes++;else J.staticRoutes++}else if(typeof Y==="object"&&Y!==null)_(Y,X+1)};return _(F),J}clearCache(){if(this.routeCache.clear(),this.options.debug)console.log("[ROUTEMATCHER]","\uD83E\uDDF9 Route cache cleared")}getCacheStats(){return{size:this.routeCache.size,maxSize:this.options.maxCacheSize,routeStats:this.routeStats}}getOptimizationSuggestions(){let F=[],{routeStats:J}=this;if(J.paramRoutes>J.staticRoutes*2)F.push("\uD83D\uDE80 Consider grouping routes to reduce parameter route overhead");if(J.maxDepth>6)F.push("\uD83D\uDCCA Deep route nesting detected - consider flattening some routes");if(J.totalRoutes>200)F.push("\u26A1 Large route table - consider route splitting or lazy loading");if(!this.options.enableCaching&&J.totalRoutes>50)F.push("\uD83D\uDCBE Enable caching for better performance with many routes");if(J.procedureTypes.file>20)F.push("\uD83D\uDCC1 Many file routes detected - consider static file serving");if(J.procedureTypes.subscription>10)F.push("\uD83D\uDD04 Many subscription routes - ensure WebSocket optimization");return F}findRoutesByPattern(F){return[]}getRouteInfo(F){let J=F.split("/").filter(Boolean),_=J.join("/"),W=this.routeCache.has(_),X=performance.now(),$=this.matchDirect(J),Y=performance.now()-X;return{found:!!$,route:$?.route,params:$?.params,cached:W,matchTime:Y}}benchmark(F,J=1000){let _=F.map((G)=>G.split("/").filter(Boolean)),W=new Map(this.routeCache);this.routeCache.clear();let X=performance.now();for(let G=0;G<J;G++)for(let V of _)this.matchDirect(V);let Y=performance.now()-X,A=J*F.length,j=Y/A,I=A/Y*1000;return this.routeCache=W,{averageTime:j,totalTime:Y,routesPerSecond:I}}warmupCache(F){if(!this.options.enableCaching)return;let J=performance.now(),_=0;for(let X of F){let $=X.split("/").filter(Boolean),Y=this.matchDirect($);if(Y)this.cacheResult($.join("/"),Y),_++}let W=performance.now();if(this.options.debug)console.log("[ROUTEMATCHER]",`\uD83D\uDD25 Cache warmed up: ${_} routes in ${(W-J).toFixed(2)}ms`)}}var R=import.meta.require,q={NO_CACHE:{cacheControl:"no-cache, no-store, must-revalidate",expires:new Date(0).toUTCString()},ONE_YEAR:{cacheControl:"public, max-age=31536000",expires:new Date(Date.now()+31536000000).toUTCString()},IMMUTABLE:{cacheControl:"public, max-age=31536000, immutable",expires:new Date(Date.now()+31536000000).toUTCString()},ONE_MONTH:{cacheControl:"public, max-age=2592000"},ONE_WEEK:{cacheControl:"public, max-age=604800"},ONE_DAY:{cacheControl:"public, max-age=86400, must-revalidate"},ONE_HOUR:{cacheControl:"public, max-age=3600, must-revalidate"},FIVE_MINUTES:{cacheControl:"public, max-age=300"},API:{cacheControl:"public, max-age=300, s-maxage=3600"},STYLESHEET:{cacheControl:"public, max-age=2592000, must-revalidate"},JAVASCRIPT:{cacheControl:"public, max-age=2592000, must-revalidate"},HASHED_ASSET:{cacheControl:"public, max-age=31536000, immutable",expires:new Date(Date.now()+31536000000).toUTCString()},FONT:{cacheControl:"public, max-age=31536000",expires:new Date(Date.now()+31536000000).toUTCString()},IMAGE:{cacheControl:"public, max-age=2592000"},FAVICON:{cacheControl:"public, max-age=31536000",expires:new Date(Date.now()+31536000000).toUTCString()},HTML_PAGE:{cacheControl:"public, max-age=3600, must-revalidate"},API_RESPONSE:{cacheControl:"public, max-age=300, s-maxage=900"},FEED:{cacheControl:"public, max-age=3600"},SITEMAP:{cacheControl:"public, max-age=86400"},MANIFEST:{cacheControl:"public, max-age=86400, must-revalidate"},SERVICE_WORKER:{cacheControl:"no-cache, no-store, must-revalidate",expires:new Date(0).toUTCString()},MEDIA_STREAM:{cacheControl:"no-cache, no-store"},DOCUMENT:{cacheControl:"public, max-age=86400"},ARCHIVE:{cacheControl:"public, max-age=604800"}};function C(F){return q[F]}var v={html:"text/html; charset=utf-8",css:"text/css",javascript:"application/javascript",json:"application/json",xml:"application/xml",text:"text/plain; charset=utf-8",csv:"text/csv",markdown:"text/markdown; charset=utf-8",png:"image/png",jpeg:"image/jpeg",gif:"image/gif",svg:"image/svg+xml",webp:"image/webp",icon:"image/x-icon",bmp:"image/bmp",woff:"font/woff",woff2:"font/woff2",truetype:"font/ttf","embedded-opentype":"application/vnd.ms-fontobject",opentype:"font/otf",mp4:"video/mp4",webm:"video/webm",ogg:"video/ogg",mpeg:"audio/mpeg",wav:"audio/wav",flac:"audio/flac",pdf:"application/pdf",doc:"application/msword",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",zip:"application/zip",tar:"application/x-tar",gzip:"application/gzip","7z":"application/x-7z-compressed",js:"application/javascript",txt:"text/plain; charset=utf-8",jpg:"image/jpeg",ico:"image/x-icon",ttf:"font/ttf",eot:"application/vnd.ms-fontobject",otf:"font/otf",mp3:"audio/mpeg",gz:"application/gzip"};function K(F){return v[F]||"application/octet-stream"}function y(F){if(typeof Bun<"u"&&Bun.hash)return Bun.hash(F).toString(16);try{let J=R("crypto"),_;if(typeof F==="string")_=Buffer.from(F);else if(F instanceof ArrayBuffer)_=Buffer.from(new Uint8Array(F));else _=Buffer.from(F);return J.createHash("md5").update(_).digest("hex")}catch{let J=typeof F==="string"?new TextEncoder().encode(F):F instanceof ArrayBuffer?new Uint8Array(F):F,_=0;for(let W=0;W<J.length;W++)_=(_<<5)-_+J[W],_=_&_;return Math.abs(_).toString(16).padStart(16,"0")}}function D(F,J,_){let W=F.headers.get("Origin"),X=!0;if((_?.allowAll??!0)&&W)return W;if(J.length===0)return W||"*";if(!W)return J[0];if(J.includes(W))return W;return J[0]}class B{headers={};contentType(F){return this.headers["Content-Type"]=K(F),this}filePath(F){let J=F.split(".").pop()?.toLowerCase()||"";return this.headers["Content-Type"]=K(J),this}mimeType(F){return this.headers["Content-Type"]=F,this}cache(F){let{cacheControl:J,expires:_}=C(F);if(this.headers["Cache-Control"]=J,_)this.headers.Expires=_;return this}eTag(F){if(!F)return this;let J;if(typeof F==="string"&&F.startsWith('"'))J=F;else J=`"${y(F)}"`;return this.headers.ETag=J,this}lastModified(F){return this.headers["Last-Modified"]=F.toUTCString(),this}contentLength(F){return this.headers["Content-Length"]=F.toString(),this}redirect(F,J=!1){return this.headers.Location=F,this.headers["X-Redirect-Type"]=J?"permanent":"temporary",this}cors({origin:F="*",methods:J=["GET","POST","PUT","DELETE","OPTIONS"],headers:_=["Content-Type","Authorization"],maxAge:W=86400,credentials:X}={}){let $=X??F!=="*";if($&&F==="*")throw Error('CORS: Cannot use credentials with wildcard origin "*". Specify an exact origin or set credentials to false.');if(this.headers["Access-Control-Allow-Origin"]=F,this.headers["Access-Control-Allow-Methods"]=J.join(", "),this.headers["Access-Control-Allow-Headers"]=_.join(", "),$)this.headers["Access-Control-Allow-Credentials"]="true";return this.headers["Access-Control-Max-Age"]=W.toString(),this}security(F){let J={hsts:!0,noSniff:!0,frameOptions:"SAMEORIGIN",xssProtection:!0,...F};if(J.csp)this.headers["Content-Security-Policy"]=J.csp;if(J.hsts){let _=typeof J.hsts==="number"?J.hsts:31536000;this.headers["Strict-Transport-Security"]=`max-age=${_}; includeSubDomains`}if(J.noSniff)this.headers["X-Content-Type-Options"]="nosniff";if(J.frameOptions)this.headers["X-Frame-Options"]=J.frameOptions;if(J.xssProtection)this.headers["X-XSS-Protection"]="1; mode=block";return this}custom(F,J){return this.headers[F]=J,this}customHeaders(F){return Object.assign(this.headers,F),this}vary(...F){let J=this.headers.Vary,_=J?`${J}, ${F.join(", ")}`:F.join(", ");return this.headers.Vary=_,this}compress(F){if(F)this.headers["Content-Encoding"]=F;return this}build(F){let J={...this.headers};if(F&&!J["Content-Length"]){let _=typeof F==="string"?new TextEncoder().encode(F).length:F.byteLength;J["Content-Length"]=_.toString()}return J}buildHeaders(F){return new Headers(this.build(F))}}function Z(){return new B}function c(F,J){let _=Z().contentType(F);if(J)_.cache(J);return _.build()}var u={css:(F)=>Z().contentType("css").cache("STYLESHEET").eTag(F),javascript:(F)=>Z().contentType("javascript").cache("JAVASCRIPT").eTag(F),hashedAsset:(F,J)=>Z().filePath(F).cache("HASHED_ASSET").eTag(J),font:(F)=>Z().filePath(F).cache("FONT").cors({origin:"*"}),image:(F)=>Z().filePath(F).cache("IMAGE"),favicon:()=>Z().contentType("icon").cache("FAVICON"),api:()=>Z().contentType("json").cache("API_RESPONSE").cors(),expensiveApi:(F)=>Z().contentType("json").cache("API_RESPONSE").cors().eTag(F),realtime:()=>Z().contentType("json").cache("NO_CACHE").cors(),html:()=>Z().contentType("html").cache("HTML_PAGE").security(),expensiveHtml:(F)=>Z().contentType("html").cache("HTML_PAGE").security().eTag(F),manifest:(F)=>Z().contentType("json").cache("MANIFEST").eTag(F),serviceWorker:()=>Z().contentType("javascript").cache("SERVICE_WORKER"),sitemap:(F)=>Z().contentType("xml").cache("SITEMAP").eTag(F),feed:(F,J="rss")=>Z().contentType("xml").cache("FEED").eTag(F).mimeType(J==="rss"?"application/rss+xml":"application/atom+xml"),video:(F)=>Z().filePath(F).cache("MEDIA_STREAM").custom("Accept-Ranges","bytes"),audio:(F)=>Z().filePath(F).cache("MEDIA_STREAM").custom("Accept-Ranges","bytes"),pdf:(F)=>Z().contentType("pdf").cache("DOCUMENT").custom("Content-Disposition","inline").eTag(F),download:(F,J,_)=>Z().filePath(J).cache("ARCHIVE").custom("Content-Disposition",`attachment; filename="${F}"`).eTag(_),secure:()=>Z().security({csp:"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'",hsts:31536000}).custom("X-Powered-By","Bun"),permanentRedirect:(F)=>Z().redirect(F,!0),temporaryRedirect:(F)=>Z().redirect(F,!1)};async function S(F,J,_,W={}){let{maxChunkSize:X=2097152,cacheMaxAge:$=3600,acceptedExtensions:Y}=W;if(!await F.exists())return new Response("Media file not found",{status:404,headers:Z().contentType("text").cors({origin:D(J,_)}).build()});if(Y&&Y.length>0){let b=F.name?.toLowerCase().split(".").pop();if(!b||!Y.includes(b))return new Response("File type not supported",{status:415,headers:Z().contentType("text").cors({origin:D(J,_)}).build()})}let j=F.size,I=J.headers.get("Range");if(!I)return new Response(F,{headers:Z().filePath(F.name||"media").custom("Accept-Ranges","bytes").custom("Cache-Control",`public, max-age=${$}`).cors({origin:D(J,_)}).build()});let G=I.match(/bytes=(\d*)-(\d*)/);if(!G)return new Response("Invalid Range header",{status:416,headers:Z().contentType("text").cors({origin:D(J,_)}).custom("Content-Range",`bytes */${j}`).build()});let V=G[1]?parseInt(G[1]):0,N=G[2]?parseInt(G[2]):j-1,Q=Math.min(N,V+X-1,j-1);if(V>=j||Q>=j||V>Q)return new Response("Range not satisfiable",{status:416,headers:Z().contentType("text").cors({origin:D(J,_)}).custom("Content-Range",`bytes */${j}`).build()});return new Response(F.slice(V,Q+1),{status:206,headers:Z().filePath(F.name||"media").custom("Accept-Ranges","bytes").custom("Cache-Control",`public, max-age=${$}`).cors({origin:D(J,_)}).build()})}function z(F,J,_){return{type:"stream",file:F,request:J,options:_}}class E{routes;routeMatcher;contextCreator;prefix;globalMiddlewares;server=null;wsConfig;integrations;development=!0;debug=!1;allowedOrigins=[];activeConnections=new Set;maxConnections=parseInt(process.env.MAX_WS_CONNECTIONS??"1000");connectionTimeout=parseInt(process.env.WS_TIMEOUT??"30000");maxRequestSize=parseInt(process.env.MAX_REQUEST_SIZE??"10485760");requestTimeout=parseInt(process.env.REQUEST_TIMEOUT??"30000");constructor(F){if(this.contextCreator=F.context,this.prefix=F.prefix??"",this.routes=F.routes,this.globalMiddlewares=F.globalMiddlewares??[],this.wsConfig=F.websocket??{},this.integrations=F.integrations,this.debug=!!F.debug&&!0,this.routeMatcher=new M(F.routes,{enableCaching:!this.development,maxCacheSize:parseInt(process.env.ROUTE_CACHE_SIZE||"1000"),debug:this.debug}),!this.development)setInterval(()=>this.cleanupStaleConnections(),60000);if(this.debug)console.log("Router initialized with route matcher"),console.log("Route cache stats:",this.routeMatcher.getCacheStats())}findRoute(F){return this.routeMatcher.match(F)}async parseInput(F,J,_){let W={},X=new URL(F.url),$=Object.fromEntries(X.searchParams),Y=F.method;if(Y==="POST"){if(parseInt(F.headers.get("content-length")||"0")>this.maxRequestSize)throw Error("Request payload too large")}if(Y==="GET"&&["query","streamQuery","file","fileStream","html"].includes(_))W=$;else if(Y==="POST"&&["mutation","formMutation","streamMutation"].includes(_)){let A=F.headers.get("content-type");if(A?.includes("application/json"))W=await F.json();else if(A?.includes("multipart/form-data")){let j=await F.formData();W=await this.parseFormData(j)}else throw Error("Unsupported content type")}else throw Error(`Unsupported request method ${Y} for procedure type ${_}`);return J.parse(W)}async parseFormData(F){let J={},_={};F.forEach(async(W,X)=>{if(W instanceof File){if(!_[X])_[X]=[];_[X].push(W)}else if(typeof W==="string")try{J[X]=JSON.parse(W)}catch{J[X]=W}});for(let[W,X]of Object.entries(_))J[W]=X.length===1?X[0]:X;return J}handleError(F,J){if(F?.errors&&Array.isArray(F.errors)&&F.name==="ZodError")return new Response(JSON.stringify({error:{name:"ValidationError",code:"BAD_REQUEST",clientCode:"VALIDATION_ERROR",message:"Input validation failed",data:{validationErrors:F.errors.map((_)=>({path:_.path.join("."),message:_.message,code:_.code,received:_.received}))},httpStatus:400}}),{status:400,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()});if(F instanceof L)return new Response(JSON.stringify({error:F.toJSON()}),{status:F.httpStatus,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()});if(F instanceof Error){let _=F.message==="Not Found"?404:F.message==="Unauthorized"?401:F.message==="Forbidden"?403:F.message==="Request payload too large"?413:F.message==="Request timeout"?408:500;return new Response(JSON.stringify({error:{code:_===404?"NOT_FOUND":_===401?"UNAUTHORIZED":_===403?"FORBIDDEN":_===413?"PAYLOAD_TOO_LARGE":_===408?"TIMEOUT":"INTERNAL_SERVER_ERROR",message:F.message}}),{status:_,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()})}return new Response(JSON.stringify({error:{code:"INTERNAL_SERVER_ERROR",message:"Internal Server Error"}}),{status:500,headers:Z().contentType("json").cors({origin:D(J,this.allowedOrigins)}).build()})}handleFileCacheControl(F){let J;if(F?.includes("text/html"))J="public, max-age=0, must-revalidate";else if(F?.includes("text/css")||F?.includes("text/javascript")||F?.includes("application/javascript"))J="public, max-age=31536000, immutable";else J="public, max-age=86400, stale-while-revalidate=604800";return J}async handleRequest(F,J){let _=new Promise((W,X)=>{setTimeout(()=>X(Error("Request timeout")),this.requestTimeout)});try{return await Promise.race([this._handleRequest(F,J),_])}catch(W){return this.handleError(W,F)}}async _handleRequest(F,J){try{if(F.method==="OPTIONS")return new Response(null,{status:204,headers:Z().cors({origin:D(F,this.allowedOrigins)}).build()});let W=new URL(F.url).pathname;if(W=W.startsWith(this.prefix)?W.slice(this.prefix.length):W,W=W.startsWith("/")?W:"/"+W,W==="/ws"&&J){if(this.debug)console.log("\uD83D\uDD0C WebSocket upgrade requested");return this.handleWebSocketUpgrade(F,J)}let X=this.findRoute(W.split("/").filter(Boolean));if(!X)return new Response("Not Found",{status:404,headers:Z().contentType("text").cors({origin:D(F,this.allowedOrigins)}).build()});let{params:$,route:Y}=X;if(!Y.handler)return new Response("Handler not implemented",{status:501,headers:Z().contentType("text").cors({origin:D(F,this.allowedOrigins)}).build()});let A=await this.contextCreator(F);Object.assign(A,{params:$,publishToProcedure:async(G,V,N)=>{await this.publishToProcedure(G,V,A,N)}});for(let G of this.globalMiddlewares)await G(A);for(let G of Y.middlewares)await G(A);let j=await this.parseInput(F,Y._input,Y._type),I=await Y.handler({ctx:A,input:j});return await this.handleProcedureResponse(Y._type,I,Z().cors({origin:D(F,this.allowedOrigins)}).build())}catch(_){return this.handleError(_,F)}}async handleProcedureResponse(F,J,_){switch(F){case"file":if(J instanceof Response)return J;let W=J.type;return new Response(J,{headers:{..._,"Content-Type":W,"Cache-Control":this.development?"public, max-age=0, must-revalidate":this.handleFileCacheControl(W)}});case"fileStream":if(typeof J==="object"&&J!==null&&"type"in J&&J.type==="stream"){let $=J;return await S(J.file,J.request,this.allowedOrigins,$.options)}let X=J.type;return new Response(J,{headers:{..._,"Content-Type":X,"Cache-Control":this.development?"public, max-age=0, must-revalidate":this.handleFileCacheControl(X)}});case"html":if(typeof J==="string")return new Response(J,{headers:{..._,"Content-Type":"text/html"}});break;case"query":case"mutation":case"formMutation":case"streamQuery":case"streamMutation":case"subscription":default:return new Response(JSON.stringify({data:J}),{headers:{..._,"Content-Type":"application/json"}})}return new Response(JSON.stringify({data:J}),{headers:{..._,"Content-Type":"application/json"}})}cleanupStaleConnections(){let F=Date.now();for(let J of this.activeConnections)if(F-J.data.lastActivity>this.connectionTimeout)J.close(),this.activeConnections.delete(J)}async handleWebSocketUpgrade(F,J){if(this.activeConnections.size>=this.maxConnections)return new Response("Too many connections",{status:503});let _=await this.contextCreator(F);for(let X of this.globalMiddlewares)await X(_);if(J.upgrade(F,{data:{ctx:_,topics:new Set,lastActivity:Date.now()}}))return new Response(null,{status:101});return new Response("WebSocket upgrade failed",{status:500})}async handleAuthenticate(F,J,_){try{let W=J.startsWith("Bearer ")?J.substring(7):J;if(F.data.ctx.authToken=W,F.send(JSON.stringify({type:"auth_success",message:"Authentication successful"})),this.debug)console.log("[BRPC]","WebSocket authenticated successfully")}catch(W){if(F.send(JSON.stringify({type:"auth_error",error:W instanceof Error?W.message:"Authentication failed"})),this.debug)console.error("WebSocket authentication error:",W)}}async handleSubscribe(F,J,_){let W=this.findRoute(J.split("/").filter(Boolean));if(!W||W.route._type!=="subscription")throw Error("Invalid subscription route");F.subscribe(J),F.data.topics.add(J)}async handleUnsubscribe(F,J,_){let W=this.findRoute(J.split("/").filter(Boolean));if(!W||W.route._type!=="subscription")throw Error("Invalid subscription route");F.unsubscribe(J),F.data.topics.delete(J)}async handlePublish(F,J,_,W){let X=this.findRoute(J.split("/").filter(Boolean));if(!X)throw Error("Subscription not found");let{params:$,route:Y}=X;if(Y._type!=="subscription"||!Y.handler)throw Error("Invalid subscription route");Object.assign(W,{params:$});let A=Y._input.fields?Y._input.parse(_):_,j=await Y.handler({ctx:W,input:A}),I={topic:J,data:j,type:"publish"};if(this.debug)console.log(`Server will publish to ${J}`,I);this.server.publish(J,JSON.stringify(I))}async publishToProcedure(F,J,_,W){if(F._type!=="subscription"||!F.handler)throw Error("Can only publish to subscription procedures");let X=this.findRoutePath(this.routes,F);if(!X)throw Error("Subscription route not found");let $=X.split("/").map((j)=>{if(j.startsWith(":")){let I=j.slice(1),G=W[I];if(!G)throw Error(`Missing parameter: ${I}`);return G}return j}).join("/"),Y=await F.handler({ctx:{..._,params:W},input:J}),A={topic:$,data:Y,type:"publish"};if(this.debug)console.log(`Server will publish to ${$}`,A);return this.server?.publish($,JSON.stringify(A)),Y}async publish(F,J,_){if(F._type!=="subscription"||!F.handler)throw Error("Can only publish to subscription procedures");let W=this.findRoutePath(this.routes,F);if(!W)throw Error("Subscription route not found");let X=W.split("/").map((A)=>{if(A.startsWith(":")){let j=A.slice(1),I=_?_[j]:null;if(!I)throw Error(`Missing parameter: ${j}`);return I}return A}).join("/"),$=await F.handler({ctx:{params:_},input:J}),Y={topic:X,data:$,type:"publish"};if(this.debug)console.log(`Server will publish to ${X}`,Y);return this.server?.publish(X,JSON.stringify(Y)),$}findRoutePath(F,J,_=""){for(let[W,X]of Object.entries(F)){let $=_?`${_}/${W}`:W;if(X===J)return $;if(X&&typeof X==="object"&&!("_type"in X)){let Y=this.findRoutePath(X,J,$);if(Y)return Y}}return null}getIntegrationRoutes(){if(!this.integrations)return;let F={};if(this.integrations.betterAuth){let J=this.integrations.betterAuth.handler;if(!J)throw Error("Please provide a handler for betterAuth integration");F["/api/auth/*"]=async(_)=>{return await J(_)}}if(this.integrations.rawRoutes)Object.keys(this.integrations.rawRoutes).forEach((J)=>{F[J]=this.integrations.rawRoutes[J]});return Object.keys(F).length>0?F:void 0}getRouteStats(){return{cache:this.routeMatcher.getCacheStats(),suggestions:this.routeMatcher.getOptimizationSuggestions()}}clearRouteCache(){if(this.routeMatcher.clearCache(),this.debug)console.log("[ROUTEMATCHER] Route cache cleared")}benchmarkRoutes(F=["/users/123","/posts/456/comments"],J=1000){if(this.debug){let _=this.routeMatcher.benchmark(F,J);return console.log("[ROUTEMATCHER]","Route matching benchmark:",_),_}}updateRoutes(F){if(this.development)this.routes=F,this.routeMatcher=new M(F,{enableCaching:!1,debug:this.debug}),console.log("[HMR]","Server Routes updated");else console.warn("Route updates are only allowed in development mode")}freePublish(F,J){let _={topic:F,data:J,type:"publish"};this.server?.publish(F,JSON.stringify(_))}listen(F,J){if(this.server=Bun.serve({port:F,routes:this.getIntegrationRoutes(),fetch:(_,W)=>this.handleRequest(_,W),websocket:{open:(_)=>{if(this.activeConnections.add(_),this.wsConfig?.onOpen)this.wsConfig.onOpen(_,_.data.ctx);if(this.debug)console.log("[BRPC]","WebSocket connection opened",{connections:this.activeConnections.size})},message:async(_,W)=>{try{_.data.lastActivity=Date.now();let{type:X,topic:$,token:Y,data:A}=JSON.parse(W),j=_.data.ctx;if(this.debug)console.log("[BRPC]","WebSocket message received:",{type:X,topic:$});switch(X){case"authenticate":await this.handleAuthenticate(_,Y,j);break;case"subscribe":await this.handleSubscribe(_,$,j);break;case"unsubscribe":await this.handleUnsubscribe(_,$,j);break;case"publish":await this.handlePublish(_,$,A,j);break;default:if(_.send(JSON.stringify({error:"Unknown message type"})),this.debug)console.error("Unknown WebSocket message type:",X)}}catch(X){if(this.debug)console.error("[BRPC]","WebSocket message error:",X);if(X instanceof f)_.send(JSON.stringify({error:"Invalid message format"}));else if(X instanceof Error)_.send(JSON.stringify({error:X.message}))}},close:(_,W,X)=>{if(this.activeConnections.delete(_),_.data.topics.forEach(($)=>{_.unsubscribe($)}),this.debug)console.log("[BRPC]","WebSocket connection closed",{code:W,reason:X,remainingConnections:this.activeConnections.size});if(this.wsConfig?.onClose)this.wsConfig.onClose(_,W,X,_.data.ctx)}}}),J)J()}}function e(F){return new E(F)}function J9(F){return F}import{z as P}from"zod";var w={image:["image/jpeg","image/png","image/gif","image/webp","image/svg+xml"],video:["video/mp4","video/webm","video/ogg"],audio:["audio/mpeg","audio/ogg","audio/wav"],document:["application/pdf","application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]},T=(F,J="MB")=>{switch(J){case"MB":return F*1024*1024;case"KB":return F*1024;case"B":return F}},X9=({acceptedTypes:F,maxSize:J=1/0,minSize:_,messages:W={}}={})=>{let X=J?T(J):1/0,$=_?T(_):0,Y=F?Object.entries(F).flatMap(([j,I])=>{let G=j;if(I==="*")return w[G];if(Array.isArray(I))return I;return[]}):void 0,A=P.instanceof(File,{message:W.required??"File is required"});if(A=A.refine((j)=>j.size<=X,W.maxSize??`File size must be less than ${J}MB`).refine((j)=>j.size>=$,W.minSize??`File size must be at least ${_}MB`),Y?.length)A=A.refine((j)=>Y.includes(j.type),W.type??`File must be of type: ${Y.join(", ")}`);return A};var $9=(F)=>{let J=F.paths.map((W)=>new RegExp(W)),_="Not Found";return async(W)=>{let $=new URL(W.req.url).pathname;if(J.some((Y)=>Y.test($)))throw Error("Not Found")}};class x{requests=new Map;lastCleanupTime=Date.now();alertThreshold;config;constructor(F){this.config={windowMs:F.windowMs,maxRequests:F.maxRequests,maxEntries:F.maxEntries??1e4,cleanupIntervalMs:F.cleanupIntervalMs??60000,message:F.message??"Too Many Requests",statusCode:F.statusCode??429,headerPrefix:F.headerPrefix??"X-RateLimit"},this.alertThreshold=this.config.maxEntries*0.8}cleanup(){let F=Date.now(),J=0;for(let[_,W]of this.requests.entries()){let X=F-W.startTime>this.config.windowMs,$=F-W.lastAccessed>this.config.windowMs*2;if(X||$)this.requests.delete(_),J++}return this.lastCleanupTime=F,J}handleMaxEntries(F){let J=this.requests.size;if(Date.now()-this.lastCleanupTime>this.config.cleanupIntervalMs)this.cleanup();if(J>=this.alertThreshold)console.warn(`Rate limiter at ${Math.round(J/this.config.maxEntries*100)}% capacity`);if(this.requests.has(F))return!0;return J<this.config.maxEntries}check(F){if(!this.handleMaxEntries(F))return{isLimited:!0,remaining:0,resetTime:Date.now()+this.config.windowMs};let J=Date.now(),_=this.requests.get(F);if(!_||J-_.startTime>=this.config.windowMs)return this.requests.set(F,{count:1,startTime:J,lastAccessed:J}),{isLimited:!1,remaining:this.config.maxRequests-1,resetTime:J+this.config.windowMs};_.lastAccessed=J;let W=_.count>=this.config.maxRequests;if(!W)_.count++;return{isLimited:W,remaining:Math.max(0,this.config.maxRequests-_.count),resetTime:_.startTime+this.config.windowMs}}getConfig(){return this.config}}var Z9=(F)=>{let J=new x(F);return async(_)=>{if(_.req.headers.get("range"))return;let W=_.req.headers.get("x-forwarded-for")?.split(",")[0]||_.req.headers.get("x-real-ip")||"unknown",{isLimited:X,remaining:$,resetTime:Y}=J.check(W),{headerPrefix:A}=J.getConfig();if(_.setHeader(`${A}-Remaining`,$.toString()),_.setHeader(`${A}-Reset`,Y.toString()),X)throw _.setHeader(`${A}-Exceeded`,"true"),Error(J.getConfig().message)}};export{z as streamMedia,c as quickHeaders,y as hashContent,K as getMimeType,D as getCorsOrigin,e as createRouter,Z9 as createRateLimiter,h as createProcedure,$9 as createPathBlocker,X9 as createFileSchema,J9 as createContext,u as commonHeaders,Z as buildHeaders,E as Router,L as BRPCError};
3
3
 
4
- //# debugId=FC699260F69ADD2F64756E2164756E21
4
+ //# debugId=6214C7542F1273F264756E2164756E21
package/dist/index.js.map CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["..\\src\\procedure.ts", "..\\src\\router\\router.ts", "..\\src\\BRPCError.ts", "..\\src\\router\\RouteMatcher.ts", "..\\..\\..\\node_modules\\@mateosuarezdev\\headers-builder\\dist\\index.js", "..\\src\\stream\\media.ts", "..\\src\\context.ts", "..\\src\\schemas.ts", "..\\src\\middlewares\\pathblocker\\index.ts", "..\\src\\middlewares\\ratelimiter\\index.ts"],
3
+ "sources": ["..\\src\\procedure.ts", "..\\src\\router\\router.ts", "..\\src\\errors\\BRPCError.ts", "..\\src\\router\\RouteMatcher.ts", "..\\..\\..\\node_modules\\@mateosuarezdev\\headers-builder\\dist\\index.js", "..\\src\\stream\\media.ts", "..\\src\\context.ts", "..\\src\\schemas.ts", "..\\src\\middlewares\\pathblocker\\index.ts", "..\\src\\middlewares\\ratelimiter\\index.ts"],
4
4
  "sourcesContent": [
5
5
  "import { z } from \"zod\";\r\nimport type {\r\n BaseContext,\r\n Handler,\r\n InferInput,\r\n Procedure,\r\n StreamableResponse,\r\n} from \"./types\";\r\nimport type { BunFile } from \"bun\";\r\n\r\nclass ProcedureBuilder<C extends BaseContext> {\r\n private middlewares: ((ctx: C) => Promise<void>)[] = [];\r\n\r\n constructor(middlewares: ((ctx: C) => Promise<void>)[] = []) {\r\n this.middlewares = middlewares;\r\n }\r\n\r\n use(middleware: (ctx: C) => Promise<void>) {\r\n return new ProcedureBuilder<C>([...this.middlewares, middleware]);\r\n }\r\n\r\n input<I extends z.ZodType>(schema: I) {\r\n return new InputProcedureBuilder<C, I>(this.middlewares, schema);\r\n }\r\n\r\n query<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"query\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"query\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n mutation<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"mutation\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"mutation\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n formMutation<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"formMutation\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"formMutation\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n //TODO\r\n // formMutation\r\n // streamMutation\r\n // streamQuery? Is even possible?\r\n\r\n subscription<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"subscription\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"subscription\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n file(\r\n handler: Handler<C, {}, BunFile | Response>\r\n ): Procedure<C, z.ZodObject<{}>, BunFile | Response, \"file\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as BunFile | Response,\r\n _ctx: null as unknown as C,\r\n _type: \"file\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n // NEW: Video streaming file handler using existing Handler type\r\n fileStream(\r\n handler: Handler<C, {}, Blob | string>\r\n ): Procedure<\r\n C,\r\n z.ZodObject<{}>,\r\n StreamableResponse | Blob | string,\r\n \"fileStream\"\r\n > {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as StreamableResponse | Blob | string,\r\n _ctx: null as unknown as C,\r\n _type: \"fileStream\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n html(\r\n handler: Handler<C, {}, string>\r\n ): Procedure<C, z.ZodObject<{}>, string, \"html\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as string,\r\n _ctx: null as unknown as C,\r\n _type: \"html\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n}\r\n\r\nclass InputProcedureBuilder<C extends BaseContext, I extends z.ZodType> {\r\n constructor(\r\n private middlewares: ((ctx: C) => Promise<void>)[],\r\n private inputSchema: I\r\n ) {}\r\n\r\n query<O>(handler: Handler<C, InferInput<I>, O>): Procedure<C, I, O, \"query\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"query\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n mutation<O>(\r\n handler: Handler<C, InferInput<I>, O>\r\n ): Procedure<C, I, O, \"mutation\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"mutation\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n formMutation<O>(\r\n handler: Handler<C, InferInput<I>, O>\r\n ): Procedure<C, I, O, \"formMutation\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"formMutation\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n subscription<O>(\r\n handler: Handler<C, InferInput<I>, O>\r\n ): Procedure<C, I, O, \"subscription\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"subscription\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n file(\r\n handler: Handler<C, InferInput<I>, BunFile | Response>\r\n ): Procedure<C, I, BunFile | Response, \"file\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as BunFile | Response,\r\n _ctx: null as unknown as C,\r\n _type: \"file\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n // NEW: Video streaming file handler using existing Handler type\r\n fileStream(\r\n handler: Handler<C, InferInput<I>, StreamableResponse | Blob | string>\r\n ): Procedure<C, I, StreamableResponse | Blob | string, \"fileStream\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as StreamableResponse | Blob | string,\r\n _ctx: null as unknown as C,\r\n _type: \"fileStream\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n html(\r\n handler: Handler<C, {}, string>\r\n ): Procedure<C, z.ZodObject<{}>, string, \"html\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as string,\r\n _ctx: null as unknown as C,\r\n _type: \"html\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n}\r\n\r\nexport function createProcedure<C extends BaseContext>() {\r\n return new ProcedureBuilder<C>();\r\n}\r\n",
6
- "import type { BunFile, Server, ServerWebSocket, BunRequest } from \"bun\";\r\nimport type {\r\n BaseContext,\r\n InferInput,\r\n Procedure,\r\n ProcedureType,\r\n RouterConfig,\r\n Routes,\r\n} from \"../types\";\r\nimport { ZodError, z } from \"zod\";\r\nimport { BRPCError } from \"../BRPCError\";\r\nimport { type RouteMatch, RouteMatcher } from \"./RouteMatcher\";\r\nimport { buildHeaders, getCorsOrigin } from \"@mateosuarezdev/headers-builder\";\r\nimport {\r\n streamMediaInternal,\r\n type StreamableMediaResponse,\r\n} from \"../stream/media\";\r\n\r\ninterface WebSocketData {\r\n ctx: any;\r\n topics: Set<string>;\r\n lastActivity: number;\r\n}\r\n\r\n//? This is if I want to handle bun --hot (hot refresh mode)\r\n// declare global {\r\n// var __wsConnections: Set<ServerWebSocket<WebSocketData>> | undefined;\r\n// }\r\n\r\nexport class Router<C extends BaseContext> {\r\n private routes: Routes;\r\n private routeMatcher: RouteMatcher;\r\n private contextCreator: (req: Request) => Promise<C>;\r\n private prefix: string;\r\n private globalMiddlewares: ((ctx: C) => Promise<void>)[];\r\n private server: Server | null = null;\r\n private wsConfig: RouterConfig<C>[\"websocket\"];\r\n private integrations: RouterConfig<C>[\"integrations\"];\r\n private development = process.env.NODE_ENV !== \"production\";\r\n private debug = false;\r\n private allowedOrigins = [];\r\n\r\n // Connection management fields...\r\n private activeConnections: Set<ServerWebSocket<WebSocketData>> = new Set();\r\n //? This is if I want to handle bun --hot (hot refresh mode)\r\n // private activeConnections: Set<ServerWebSocket<WebSocketData>> =\r\n // (globalThis.__wsConnections ??= new Set());\r\n private readonly maxConnections: number = parseInt(\r\n process.env.MAX_WS_CONNECTIONS ?? \"1000\"\r\n );\r\n private readonly connectionTimeout: number = parseInt(\r\n process.env.WS_TIMEOUT ?? \"30000\"\r\n );\r\n private readonly maxRequestSize: number = parseInt(\r\n process.env.MAX_REQUEST_SIZE ?? \"10485760\"\r\n );\r\n private readonly requestTimeout: number = parseInt(\r\n process.env.REQUEST_TIMEOUT ?? \"30000\"\r\n );\r\n\r\n constructor(config: RouterConfig<C>) {\r\n this.contextCreator = config.context;\r\n this.prefix = config.prefix ?? \"\";\r\n this.routes = config.routes;\r\n this.globalMiddlewares = config.globalMiddlewares ?? [];\r\n this.wsConfig = config.websocket ?? {};\r\n this.integrations = config.integrations;\r\n this.debug = !!config.debug && process.env.NODE_ENV === \"development\";\r\n\r\n // Initialize the route matcher\r\n this.routeMatcher = new RouteMatcher(config.routes, {\r\n enableCaching: !this.development, // Disable cache in dev for hot reloading\r\n maxCacheSize: parseInt(process.env.ROUTE_CACHE_SIZE || \"1000\"),\r\n debug: this.debug,\r\n });\r\n\r\n // Start periodic cleanup\r\n if (!this.development) {\r\n setInterval(() => this.cleanupStaleConnections(), 60000);\r\n }\r\n\r\n if (this.debug) {\r\n console.log(\"Router initialized with route matcher\");\r\n console.log(\"Route cache stats:\", this.routeMatcher.getCacheStats());\r\n }\r\n }\r\n\r\n /**\r\n * Simplified route finding using RouteMatcher\r\n */\r\n private findRoute(pathParts: string[]): RouteMatch | null {\r\n return this.routeMatcher.match(pathParts);\r\n }\r\n\r\n /**\r\n * Parses and validates the request input\r\n */\r\n private async parseInput(\r\n req: Request,\r\n schema: z.ZodType,\r\n routeType: ProcedureType\r\n ): Promise<any> {\r\n let input: any = {};\r\n const url = new URL(req.url);\r\n const queryParams = Object.fromEntries(url.searchParams);\r\n const method = req.method;\r\n\r\n // Check request size for POST requests\r\n if (method === \"POST\") {\r\n const contentLength = parseInt(req.headers.get(\"content-length\") || \"0\");\r\n if (contentLength > this.maxRequestSize) {\r\n throw new Error(\"Request payload too large\");\r\n }\r\n }\r\n\r\n // Handle different procedure types and HTTP methods\r\n if (\r\n method === \"GET\" &&\r\n [\"query\", \"streamQuery\", \"file\", \"fileStream\", \"html\"].includes(routeType)\r\n ) {\r\n input = queryParams;\r\n } else if (\r\n method === \"POST\" &&\r\n [\"mutation\", \"formMutation\", \"streamMutation\"].includes(routeType)\r\n ) {\r\n const contentType = req.headers.get(\"content-type\");\r\n if (contentType?.includes(\"application/json\")) {\r\n input = await req.json();\r\n } else if (contentType?.includes(\"multipart/form-data\")) {\r\n const formData = await req.formData();\r\n //todo investigate about this deprecation\r\n //@ts-ignore\r\n input = await this.parseFormData(formData);\r\n } else {\r\n throw new Error(\"Unsupported content type\");\r\n }\r\n } else {\r\n throw new Error(\r\n `Unsupported request method ${method} for procedure type ${routeType}`\r\n );\r\n }\r\n\r\n return schema.parse(input);\r\n }\r\n\r\n /**\r\n * Helper method to parse form data\r\n */\r\n private async parseFormData(\r\n formData: FormData\r\n ): Promise<Record<string, any>> {\r\n const result: Record<string, any> = {};\r\n const fileGroups: Record<string, any[]> = {};\r\n\r\n formData.forEach(async (value, key) => {\r\n if (value instanceof File) {\r\n // Initialize array for this file key if it doesn't exist\r\n if (!fileGroups[key]) {\r\n fileGroups[key] = [];\r\n }\r\n\r\n // Add the file to its group\r\n // fileGroups[key].push({\r\n // name: value.name,\r\n // type: value.type,\r\n // size: value.size,\r\n // lastModified: value.lastModified,\r\n // blob: new Blob([await value.arrayBuffer()], { type: value.type }),\r\n // });\r\n //? new, not converting into a blob, directly passing the File instance\r\n fileGroups[key].push(value);\r\n } else if (typeof value === \"string\") {\r\n // Try to parse JSON strings\r\n try {\r\n result[key] = JSON.parse(value);\r\n } catch {\r\n // If not JSON, keep as string\r\n result[key] = value;\r\n }\r\n }\r\n });\r\n\r\n // Process file groups\r\n for (const [key, files] of Object.entries(fileGroups)) {\r\n // If there's only one file, store it directly\r\n // If there are multiple files, store them as an array\r\n result[key] = files.length === 1 ? files[0] : files;\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * handleError\r\n * @description global error handling for\r\n * each incoming request\r\n */\r\n private handleError(error: any, req: Request): Response {\r\n // const corsHeaders = getCorsHeaders(req);\r\n\r\n // Handle Zod validation errors\r\n if (\r\n error?.errors &&\r\n Array.isArray(error.errors) &&\r\n error.name === \"ZodError\"\r\n ) {\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n name: \"ValidationError\",\r\n code: \"BAD_REQUEST\",\r\n clientCode: \"VALIDATION_ERROR\",\r\n message: \"Input validation failed\",\r\n data: {\r\n validationErrors: error.errors.map((err: any) => ({\r\n path: err.path.join(\".\"),\r\n message: err.message,\r\n code: err.code,\r\n received: err.received,\r\n })),\r\n },\r\n httpStatus: 400,\r\n },\r\n }),\r\n {\r\n status: 400,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n }\r\n );\r\n }\r\n\r\n // Handle BRPCError\r\n if (error instanceof BRPCError) {\r\n return new Response(JSON.stringify({ error: error.toJSON() }), {\r\n status: error.httpStatus,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Handle generic Error instances with special messages (backward compatibility)\r\n if (error instanceof Error) {\r\n const status =\r\n error.message === \"Not Found\"\r\n ? 404\r\n : error.message === \"Unauthorized\"\r\n ? 401\r\n : error.message === \"Forbidden\"\r\n ? 403\r\n : error.message === \"Request payload too large\"\r\n ? 413\r\n : error.message === \"Request timeout\"\r\n ? 408\r\n : 500;\r\n\r\n const errorCode =\r\n status === 404\r\n ? \"NOT_FOUND\"\r\n : status === 401\r\n ? \"UNAUTHORIZED\"\r\n : status === 403\r\n ? \"FORBIDDEN\"\r\n : status === 413\r\n ? \"PAYLOAD_TOO_LARGE\"\r\n : status === 408\r\n ? \"TIMEOUT\"\r\n : \"INTERNAL_SERVER_ERROR\";\r\n\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n code: errorCode,\r\n message: error.message,\r\n },\r\n }),\r\n {\r\n status,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n }\r\n );\r\n }\r\n\r\n // Fallback for unknown errors\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n code: \"INTERNAL_SERVER_ERROR\",\r\n message: \"Internal Server Error\",\r\n },\r\n }),\r\n {\r\n status: 500,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n }\r\n );\r\n }\r\n\r\n /**\r\n * handles the default cache control headers for file routes\r\n */\r\n private handleFileCacheControl(contentType: string) {\r\n let cacheControl: string;\r\n if (contentType?.includes(\"text/html\")) {\r\n cacheControl = \"public, max-age=0, must-revalidate\";\r\n } else if (\r\n contentType?.includes(\"text/css\") ||\r\n contentType?.includes(\"text/javascript\") ||\r\n contentType?.includes(\"application/javascript\")\r\n ) {\r\n cacheControl = \"public, max-age=31536000, immutable\";\r\n } else {\r\n cacheControl = \"public, max-age=86400, stale-while-revalidate=604800\";\r\n }\r\n return cacheControl;\r\n }\r\n\r\n /**\r\n * Handles the behavior of each request\r\n */\r\n private async handleRequest(\r\n req: Request,\r\n server?: Server\r\n ): Promise<Response> {\r\n const timeoutPromise = new Promise<Response>((_, reject) => {\r\n setTimeout(\r\n () => reject(new Error(\"Request timeout\")),\r\n this.requestTimeout\r\n );\r\n });\r\n\r\n try {\r\n return await Promise.race([\r\n this._handleRequest(req, server),\r\n timeoutPromise,\r\n ]);\r\n } catch (error) {\r\n return this.handleError(error, req);\r\n }\r\n }\r\n\r\n private async _handleRequest(\r\n req: Request,\r\n server?: Server\r\n ): Promise<Response> {\r\n try {\r\n if (req.method === \"OPTIONS\") {\r\n return new Response(null, {\r\n status: 204,\r\n headers: buildHeaders()\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n const url = new URL(req.url);\r\n let path = url.pathname;\r\n path = path.startsWith(this.prefix)\r\n ? path.slice(this.prefix.length)\r\n : path;\r\n path = path.startsWith(\"/\") ? path : \"/\" + path;\r\n\r\n if (path === \"/ws\" && server) {\r\n if (this.debug) {\r\n console.log(\"🔌 WebSocket upgrade requested\");\r\n }\r\n return this.handleWebSocketUpgrade(req, server);\r\n }\r\n\r\n // Use the optimized route matcher\r\n const routeResult = this.findRoute(path.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult) {\r\n return new Response(\"Not Found\", {\r\n status: 404,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n const { params, route } = routeResult;\r\n\r\n if (!route.handler) {\r\n return new Response(\"Handler not implemented\", {\r\n status: 501,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Create context with all the properties from BaseContext\r\n const ctx = await this.contextCreator(req);\r\n Object.assign(ctx, {\r\n params,\r\n publishToProcedure: async <\r\n P extends Procedure<any, any, any, \"subscription\">\r\n >(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n params: Record<string, string>\r\n ) => {\r\n await this.publishToProcedure(procedure, input, ctx, params);\r\n },\r\n });\r\n\r\n // Run global middlewares\r\n for (const middleware of this.globalMiddlewares) {\r\n await middleware(ctx);\r\n }\r\n\r\n // Run procedure-specific middlewares\r\n for (const middleware of route.middlewares) {\r\n await middleware(ctx);\r\n }\r\n\r\n // Parse input based on procedure type\r\n const input = await this.parseInput(req, route._input, route._type);\r\n const result = await route.handler({ ctx, input });\r\n\r\n // Handle different response types based on procedure type\r\n return await this.handleProcedureResponse(\r\n route._type,\r\n result,\r\n buildHeaders()\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build()\r\n );\r\n } catch (error) {\r\n return this.handleError(error, req);\r\n }\r\n }\r\n\r\n /**\r\n * Handle responses based on procedure type\r\n */\r\n private async handleProcedureResponse(\r\n procedureType: ProcedureType,\r\n result: any,\r\n corsHeaders: Record<string, string>\r\n ): Promise<Response> {\r\n switch (procedureType) {\r\n case \"file\":\r\n if (result instanceof Response) {\r\n return result;\r\n }\r\n const contentType = (result as BunFile).type;\r\n return new Response(result, {\r\n headers: {\r\n ...corsHeaders,\r\n \"Content-Type\": contentType,\r\n \"Cache-Control\": this.development\r\n ? \"public, max-age=0, must-revalidate\"\r\n : this.handleFileCacheControl(contentType),\r\n },\r\n });\r\n\r\n case \"fileStream\":\r\n if (\r\n typeof result === \"object\" &&\r\n result !== null &&\r\n \"type\" in result &&\r\n result.type === \"stream\"\r\n ) {\r\n // const videoHandler = new VideoStreamingHandler();\r\n // return await videoHandler.handleVideoStream(\r\n // result.file,\r\n // result.request\r\n // );\r\n const mediaStreamResult = result as StreamableMediaResponse;\r\n return await streamMediaInternal(\r\n result.file,\r\n result.request,\r\n this.allowedOrigins,\r\n mediaStreamResult.options\r\n );\r\n }\r\n // Fallback to regular file response\r\n const streamContentType = (result as Blob).type;\r\n return new Response(result, {\r\n headers: {\r\n ...corsHeaders,\r\n \"Content-Type\": streamContentType,\r\n \"Cache-Control\": this.development\r\n ? \"public, max-age=0, must-revalidate\"\r\n : this.handleFileCacheControl(streamContentType),\r\n },\r\n });\r\n\r\n case \"html\":\r\n // Handle HTML rendering\r\n if (typeof result === \"string\") {\r\n return new Response(result, {\r\n headers: {\r\n ...corsHeaders,\r\n \"Content-Type\": \"text/html\",\r\n },\r\n });\r\n }\r\n break;\r\n\r\n case \"query\":\r\n case \"mutation\":\r\n case \"formMutation\":\r\n case \"streamQuery\":\r\n case \"streamMutation\":\r\n case \"subscription\":\r\n default:\r\n // Standard JSON response\r\n return new Response(JSON.stringify({ data: result }), {\r\n headers: { ...corsHeaders, \"Content-Type\": \"application/json\" },\r\n });\r\n }\r\n\r\n // Fallback\r\n return new Response(JSON.stringify({ data: result }), {\r\n headers: { ...corsHeaders, \"Content-Type\": \"application/json\" },\r\n });\r\n }\r\n\r\n //*Websockets\r\n\r\n private cleanupStaleConnections() {\r\n const now = Date.now();\r\n for (const ws of this.activeConnections) {\r\n if (now - ws.data.lastActivity > this.connectionTimeout) {\r\n ws.close();\r\n this.activeConnections.delete(ws);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * handleWebSocketUpgrade\r\n * @description upgrades the incoming request to\r\n * a websocket connection\r\n */\r\n private async handleWebSocketUpgrade(\r\n req: Request,\r\n server: Server\r\n ): Promise<Response> {\r\n if (this.activeConnections.size >= this.maxConnections) {\r\n return new Response(\"Too many connections\", { status: 503 });\r\n }\r\n\r\n const ctx = await this.contextCreator(req);\r\n for (const middleware of this.globalMiddlewares) {\r\n await middleware(ctx);\r\n }\r\n\r\n const success = server.upgrade<WebSocketData>(req, {\r\n data: {\r\n ctx,\r\n topics: new Set<string>(),\r\n lastActivity: Date.now(),\r\n },\r\n });\r\n\r\n if (success) {\r\n return new Response(null, { status: 101 }); // Switching Protocols\r\n }\r\n\r\n return new Response(\"WebSocket upgrade failed\", { status: 500 });\r\n }\r\n\r\n /**\r\n * Handles WebSocket authentication\r\n */\r\n private async handleAuthenticate(\r\n ws: ServerWebSocket<WebSocketData>,\r\n token: string,\r\n _ctx: C\r\n ) {\r\n try {\r\n // Extract token from \"Bearer xxx\" format if needed\r\n const finalToken = token.startsWith(\"Bearer \")\r\n ? token.substring(7)\r\n : token;\r\n\r\n // Store the token in the WebSocket context\r\n ws.data.ctx.authToken = finalToken;\r\n\r\n // Additionally, we can verify the token and update the session\r\n // For example, with Supabase:\r\n // const { data, error } = await supabase.auth.getUser(finalToken);\r\n // if (error) throw new Error(error.message);\r\n // ws.data.ctx.session = { user: data.user };\r\n\r\n // Send success response\r\n ws.send(\r\n JSON.stringify({\r\n type: \"auth_success\",\r\n message: \"Authentication successful\",\r\n })\r\n );\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket authenticated successfully\");\r\n }\r\n } catch (error) {\r\n ws.send(\r\n JSON.stringify({\r\n type: \"auth_error\",\r\n error:\r\n error instanceof Error ? error.message : \"Authentication failed\",\r\n })\r\n );\r\n\r\n if (this.debug) {\r\n console.error(\"WebSocket authentication error:\", error);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * handles the ws client topic subscription\r\n */\r\n private async handleSubscribe(\r\n ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n _ctx: C\r\n ) {\r\n // Find route first to validate subscription\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult || routeResult.route._type !== \"subscription\") {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n ws.subscribe(topic);\r\n ws.data.topics.add(topic);\r\n }\r\n\r\n /**\r\n * handles the ws client topic unsubscription\r\n */\r\n private async handleUnsubscribe(\r\n ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n _ctx: C\r\n ) {\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult || routeResult.route._type !== \"subscription\") {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n ws.unsubscribe(topic);\r\n ws.data.topics.delete(topic);\r\n }\r\n\r\n /**\r\n * handles the message publication of a certain ws client\r\n */\r\n private async handlePublish(\r\n _ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n message: any,\r\n ctx: C\r\n ) {\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult) {\r\n throw new Error(\"Subscription not found\");\r\n }\r\n\r\n const { params, route } = routeResult;\r\n\r\n if (route._type !== \"subscription\" || !route.handler) {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n Object.assign(ctx, { params });\r\n\r\n // Only validate if the procedure has defined input\r\n const validatedMessage = route._input.fields\r\n ? route._input.parse(message)\r\n : message;\r\n\r\n const publishMessage = await route.handler({\r\n ctx,\r\n input: validatedMessage,\r\n });\r\n\r\n const converted = {\r\n topic: topic,\r\n data: publishMessage,\r\n type: \"publish\",\r\n };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${topic}`, converted);\r\n }\r\n\r\n // this.server!.publish(topic, JSON.stringify(publishMessage));\r\n this.server!.publish(topic, JSON.stringify(converted));\r\n }\r\n\r\n private async publishToProcedure<\r\n P extends Procedure<any, any, any, \"subscription\">\r\n >(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n ctx: C,\r\n params: Record<string, string>\r\n ) {\r\n if (procedure._type !== \"subscription\" || !procedure.handler) {\r\n throw new Error(\"Can only publish to subscription procedures\");\r\n }\r\n\r\n const routePath = this.findRoutePath(this.routes, procedure);\r\n\r\n if (!routePath) {\r\n throw new Error(\"Subscription route not found\");\r\n }\r\n\r\n // Replace params in the path\r\n const finalPath = routePath\r\n .split(\"/\")\r\n .map((part) => {\r\n if (part.startsWith(\":\")) {\r\n const paramName = part.slice(1);\r\n const paramValue = params[paramName];\r\n if (!paramValue) {\r\n throw new Error(`Missing parameter: ${paramName}`);\r\n }\r\n return paramValue;\r\n }\r\n return part;\r\n })\r\n .join(\"/\");\r\n\r\n const result = await procedure.handler({\r\n ctx: { ...ctx, params },\r\n input,\r\n });\r\n\r\n const converted = { topic: finalPath, data: result, type: \"publish\" };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${finalPath}`, converted);\r\n }\r\n\r\n // this.server?.publish(finalPath, JSON.stringify(result));\r\n this.server?.publish(finalPath, JSON.stringify(converted));\r\n return result;\r\n }\r\n\r\n async publish<P extends Procedure<any, any, any, \"subscription\">>(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n params?: Record<string, string>\r\n ) {\r\n if (procedure._type !== \"subscription\" || !procedure.handler) {\r\n throw new Error(\"Can only publish to subscription procedures\");\r\n }\r\n\r\n const routePath = this.findRoutePath(this.routes, procedure);\r\n\r\n if (!routePath) {\r\n throw new Error(\"Subscription route not found\");\r\n }\r\n\r\n // Replace params in the path\r\n const finalPath = routePath\r\n .split(\"/\")\r\n .map((part) => {\r\n if (part.startsWith(\":\")) {\r\n const paramName = part.slice(1);\r\n const paramValue = params ? params[paramName] : null;\r\n if (!paramValue) {\r\n throw new Error(`Missing parameter: ${paramName}`);\r\n }\r\n return paramValue;\r\n }\r\n return part;\r\n })\r\n .join(\"/\");\r\n\r\n const result = await procedure.handler({\r\n ctx: { params },\r\n input,\r\n });\r\n\r\n const converted = { topic: finalPath, data: result, type: \"publish\" };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${finalPath}`, converted);\r\n }\r\n\r\n // this.server?.publish(finalPath, JSON.stringify(result));\r\n this.server?.publish(finalPath, JSON.stringify(converted));\r\n return result;\r\n }\r\n\r\n /**\r\n * findRoutePath\r\n * @description finds the full path to a procedure in the routes tree\r\n */\r\n private findRoutePath(\r\n routes: Routes,\r\n targetProcedure: Procedure<any, any, any, any>,\r\n prefix: string = \"\"\r\n ): string | null {\r\n for (const [key, value] of Object.entries(routes)) {\r\n const currentPath = prefix ? `${prefix}/${key}` : key;\r\n\r\n if (value === targetProcedure) {\r\n return currentPath;\r\n }\r\n\r\n if (value && typeof value === \"object\" && !(\"_type\" in value)) {\r\n const found = this.findRoutePath(\r\n value as Routes,\r\n targetProcedure,\r\n currentPath\r\n );\r\n if (found) return found;\r\n }\r\n }\r\n\r\n return null;\r\n }\r\n\r\n private getIntegrationRoutes() {\r\n if (!this.integrations) return undefined;\r\n\r\n let routes: any = {}; // Initialize the routes object\r\n\r\n if (this.integrations.betterAuth) {\r\n const handler = this.integrations.betterAuth.handler;\r\n if (!handler) {\r\n throw new Error(\"Please provide a handler for betterAuth integration\");\r\n }\r\n routes[\"/api/auth/*\"] = async (req: BunRequest<\"auth\">) => {\r\n return await handler(req);\r\n };\r\n }\r\n\r\n if (this.integrations.rawRoutes) {\r\n Object.keys(this.integrations.rawRoutes).forEach((val) => {\r\n routes[val] = this.integrations!.rawRoutes![val];\r\n });\r\n }\r\n\r\n return Object.keys(routes).length > 0 ? routes : undefined;\r\n }\r\n\r\n /**\r\n * New utility methods for route management\r\n */\r\n public getRouteStats() {\r\n return {\r\n cache: this.routeMatcher.getCacheStats(),\r\n suggestions: this.routeMatcher.getOptimizationSuggestions(),\r\n };\r\n }\r\n\r\n public clearRouteCache() {\r\n this.routeMatcher.clearCache();\r\n if (this.debug) {\r\n console.log(\"[ROUTEMATCHER] Route cache cleared\");\r\n }\r\n }\r\n\r\n public benchmarkRoutes(\r\n testPaths: string[] = [\"/users/123\", \"/posts/456/comments\"],\r\n iterations = 1000\r\n ) {\r\n if (this.debug) {\r\n const results = this.routeMatcher.benchmark(testPaths, iterations);\r\n console.log(\"[ROUTEMATCHER]\", \"Route matching benchmark:\", results);\r\n return results;\r\n }\r\n }\r\n\r\n /**\r\n * Hot reload support for development\r\n */\r\n public updateRoutes(newRoutes: Routes) {\r\n if (this.development) {\r\n this.routes = newRoutes;\r\n this.routeMatcher = new RouteMatcher(newRoutes, {\r\n enableCaching: false, // No caching in dev\r\n debug: this.debug,\r\n });\r\n console.log(\"[HMR]\", \"Server Routes updated\");\r\n } else {\r\n console.warn(\"Route updates are only allowed in development mode\");\r\n }\r\n }\r\n\r\n /**\r\n * publish\r\n * @description publish a message to a topic from\r\n * the server itself\r\n */\r\n // private publish(topic: string, message: string | Uint8Array) {\r\n // if (this.server) {\r\n // this.server.publish(topic, message);\r\n // } else {\r\n // //?debug\r\n // //console.warn(\"Server not initialized. Cannot publish message.\");\r\n // }\r\n // }\r\n public freePublish<T>(topic: string, message: T) {\r\n const converted = { topic: topic, data: message, type: \"publish\" };\r\n this.server?.publish(topic, JSON.stringify(converted));\r\n }\r\n\r\n //*start server\r\n\r\n /**\r\n * Listen\r\n * @description starts up the Bun http server\r\n */\r\n listen(port: number, callback?: () => void) {\r\n this.server = Bun.serve({\r\n port,\r\n routes: this.getIntegrationRoutes(),\r\n fetch: (req, server) => this.handleRequest(req, server),\r\n websocket: {\r\n open: (ws: ServerWebSocket<WebSocketData>) => {\r\n this.activeConnections.add(ws);\r\n if (this.wsConfig?.onOpen) {\r\n this.wsConfig.onOpen(ws, ws.data.ctx);\r\n }\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket connection opened\", {\r\n connections: this.activeConnections.size,\r\n });\r\n }\r\n },\r\n message: async (ws: ServerWebSocket<WebSocketData>, message: any) => {\r\n try {\r\n ws.data.lastActivity = Date.now();\r\n const { type, topic, token, data } = JSON.parse(message as string);\r\n // console.log(type, token);\r\n const ctx = ws.data.ctx;\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket message received:\", {\r\n type,\r\n topic,\r\n });\r\n }\r\n\r\n switch (type) {\r\n case \"authenticate\":\r\n await this.handleAuthenticate(ws, token, ctx);\r\n break;\r\n case \"subscribe\":\r\n await this.handleSubscribe(ws, topic, ctx);\r\n break;\r\n case \"unsubscribe\":\r\n await this.handleUnsubscribe(ws, topic, ctx);\r\n break;\r\n case \"publish\":\r\n await this.handlePublish(ws, topic, data, ctx);\r\n break;\r\n default:\r\n ws.send(JSON.stringify({ error: \"Unknown message type\" }));\r\n if (this.debug) {\r\n console.error(\"Unknown WebSocket message type:\", type);\r\n }\r\n }\r\n } catch (error) {\r\n if (this.debug) {\r\n console.error(\"[BRPC]\", \"WebSocket message error:\", error);\r\n }\r\n\r\n if (error instanceof ZodError) {\r\n ws.send(JSON.stringify({ error: \"Invalid message format\" }));\r\n } else if (error instanceof Error) {\r\n ws.send(JSON.stringify({ error: error.message }));\r\n }\r\n }\r\n },\r\n close: (\r\n ws: ServerWebSocket<WebSocketData>,\r\n code: number,\r\n reason: string\r\n ) => {\r\n this.activeConnections.delete(ws);\r\n ws.data.topics.forEach((topic) => {\r\n ws.unsubscribe(topic);\r\n });\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket connection closed\", {\r\n code,\r\n reason,\r\n remainingConnections: this.activeConnections.size,\r\n });\r\n }\r\n\r\n if (this.wsConfig?.onClose) {\r\n this.wsConfig.onClose(ws, code, reason, ws.data.ctx);\r\n }\r\n },\r\n },\r\n });\r\n\r\n if (callback) callback();\r\n }\r\n}\r\n\r\n//*export needed methods and types\r\n\r\n/**\r\n * createRouter\r\n * @description helper function to create a\r\n * Router instance\r\n */\r\nexport function createRouter<C extends BaseContext>(config: RouterConfig<C>) {\r\n return new Router(config);\r\n}\r\n\r\nexport type CreateRouter = ReturnType<typeof createRouter>;\r\n",
6
+ "import type { BunFile, Server, ServerWebSocket, BunRequest } from \"bun\";\r\nimport type {\r\n BaseContext,\r\n InferInput,\r\n Procedure,\r\n ProcedureType,\r\n RouterConfig,\r\n Routes,\r\n} from \"../types\";\r\nimport { ZodError, z } from \"zod\";\r\nimport { BRPCError } from \"../errors/BRPCError\";\r\nimport { type RouteMatch, RouteMatcher } from \"./RouteMatcher\";\r\nimport { buildHeaders, getCorsOrigin } from \"@mateosuarezdev/headers-builder\";\r\nimport {\r\n streamMediaInternal,\r\n type StreamableMediaResponse,\r\n} from \"../stream/media\";\r\n\r\ninterface WebSocketData {\r\n ctx: any;\r\n topics: Set<string>;\r\n lastActivity: number;\r\n}\r\n\r\n//? This is if I want to handle bun --hot (hot refresh mode)\r\n// declare global {\r\n// var __wsConnections: Set<ServerWebSocket<WebSocketData>> | undefined;\r\n// }\r\n\r\nexport class Router<C extends BaseContext> {\r\n private routes: Routes;\r\n private routeMatcher: RouteMatcher;\r\n private contextCreator: (req: Request) => Promise<C>;\r\n private prefix: string;\r\n private globalMiddlewares: ((ctx: C) => Promise<void>)[];\r\n private server: Server | null = null;\r\n private wsConfig: RouterConfig<C>[\"websocket\"];\r\n private integrations: RouterConfig<C>[\"integrations\"];\r\n private development = process.env.NODE_ENV !== \"production\";\r\n private debug = false;\r\n private allowedOrigins = [];\r\n\r\n // Connection management fields...\r\n private activeConnections: Set<ServerWebSocket<WebSocketData>> = new Set();\r\n //? This is if I want to handle bun --hot (hot refresh mode)\r\n // private activeConnections: Set<ServerWebSocket<WebSocketData>> =\r\n // (globalThis.__wsConnections ??= new Set());\r\n private readonly maxConnections: number = parseInt(\r\n process.env.MAX_WS_CONNECTIONS ?? \"1000\"\r\n );\r\n private readonly connectionTimeout: number = parseInt(\r\n process.env.WS_TIMEOUT ?? \"30000\"\r\n );\r\n private readonly maxRequestSize: number = parseInt(\r\n process.env.MAX_REQUEST_SIZE ?? \"10485760\"\r\n );\r\n private readonly requestTimeout: number = parseInt(\r\n process.env.REQUEST_TIMEOUT ?? \"30000\"\r\n );\r\n\r\n constructor(config: RouterConfig<C>) {\r\n this.contextCreator = config.context;\r\n this.prefix = config.prefix ?? \"\";\r\n this.routes = config.routes;\r\n this.globalMiddlewares = config.globalMiddlewares ?? [];\r\n this.wsConfig = config.websocket ?? {};\r\n this.integrations = config.integrations;\r\n this.debug = !!config.debug && process.env.NODE_ENV === \"development\";\r\n\r\n // Initialize the route matcher\r\n this.routeMatcher = new RouteMatcher(config.routes, {\r\n enableCaching: !this.development, // Disable cache in dev for hot reloading\r\n maxCacheSize: parseInt(process.env.ROUTE_CACHE_SIZE || \"1000\"),\r\n debug: this.debug,\r\n });\r\n\r\n // Start periodic cleanup\r\n if (!this.development) {\r\n setInterval(() => this.cleanupStaleConnections(), 60000);\r\n }\r\n\r\n if (this.debug) {\r\n console.log(\"Router initialized with route matcher\");\r\n console.log(\"Route cache stats:\", this.routeMatcher.getCacheStats());\r\n }\r\n }\r\n\r\n /**\r\n * Simplified route finding using RouteMatcher\r\n */\r\n private findRoute(pathParts: string[]): RouteMatch | null {\r\n return this.routeMatcher.match(pathParts);\r\n }\r\n\r\n /**\r\n * Parses and validates the request input\r\n */\r\n private async parseInput(\r\n req: Request,\r\n schema: z.ZodType,\r\n routeType: ProcedureType\r\n ): Promise<any> {\r\n let input: any = {};\r\n const url = new URL(req.url);\r\n const queryParams = Object.fromEntries(url.searchParams);\r\n const method = req.method;\r\n\r\n // Check request size for POST requests\r\n if (method === \"POST\") {\r\n const contentLength = parseInt(req.headers.get(\"content-length\") || \"0\");\r\n if (contentLength > this.maxRequestSize) {\r\n throw new Error(\"Request payload too large\");\r\n }\r\n }\r\n\r\n // Handle different procedure types and HTTP methods\r\n if (\r\n method === \"GET\" &&\r\n [\"query\", \"streamQuery\", \"file\", \"fileStream\", \"html\"].includes(routeType)\r\n ) {\r\n input = queryParams;\r\n } else if (\r\n method === \"POST\" &&\r\n [\"mutation\", \"formMutation\", \"streamMutation\"].includes(routeType)\r\n ) {\r\n const contentType = req.headers.get(\"content-type\");\r\n if (contentType?.includes(\"application/json\")) {\r\n input = await req.json();\r\n } else if (contentType?.includes(\"multipart/form-data\")) {\r\n const formData = await req.formData();\r\n //todo investigate about this deprecation\r\n //@ts-ignore\r\n input = await this.parseFormData(formData);\r\n } else {\r\n throw new Error(\"Unsupported content type\");\r\n }\r\n } else {\r\n throw new Error(\r\n `Unsupported request method ${method} for procedure type ${routeType}`\r\n );\r\n }\r\n\r\n return schema.parse(input);\r\n }\r\n\r\n /**\r\n * Helper method to parse form data\r\n */\r\n private async parseFormData(\r\n formData: FormData\r\n ): Promise<Record<string, any>> {\r\n const result: Record<string, any> = {};\r\n const fileGroups: Record<string, any[]> = {};\r\n\r\n formData.forEach(async (value, key) => {\r\n if (value instanceof File) {\r\n // Initialize array for this file key if it doesn't exist\r\n if (!fileGroups[key]) {\r\n fileGroups[key] = [];\r\n }\r\n\r\n // Add the file to its group\r\n // fileGroups[key].push({\r\n // name: value.name,\r\n // type: value.type,\r\n // size: value.size,\r\n // lastModified: value.lastModified,\r\n // blob: new Blob([await value.arrayBuffer()], { type: value.type }),\r\n // });\r\n //? new, not converting into a blob, directly passing the File instance\r\n fileGroups[key].push(value);\r\n } else if (typeof value === \"string\") {\r\n // Try to parse JSON strings\r\n try {\r\n result[key] = JSON.parse(value);\r\n } catch {\r\n // If not JSON, keep as string\r\n result[key] = value;\r\n }\r\n }\r\n });\r\n\r\n // Process file groups\r\n for (const [key, files] of Object.entries(fileGroups)) {\r\n // If there's only one file, store it directly\r\n // If there are multiple files, store them as an array\r\n result[key] = files.length === 1 ? files[0] : files;\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * handleError\r\n * @description global error handling for\r\n * each incoming request\r\n */\r\n private handleError(error: any, req: Request): Response {\r\n // const corsHeaders = getCorsHeaders(req);\r\n\r\n // Handle Zod validation errors\r\n if (\r\n error?.errors &&\r\n Array.isArray(error.errors) &&\r\n error.name === \"ZodError\"\r\n ) {\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n name: \"ValidationError\",\r\n code: \"BAD_REQUEST\",\r\n clientCode: \"VALIDATION_ERROR\",\r\n message: \"Input validation failed\",\r\n data: {\r\n validationErrors: error.errors.map((err: any) => ({\r\n path: err.path.join(\".\"),\r\n message: err.message,\r\n code: err.code,\r\n received: err.received,\r\n })),\r\n },\r\n httpStatus: 400,\r\n },\r\n }),\r\n {\r\n status: 400,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n }\r\n );\r\n }\r\n\r\n // Handle BRPCError\r\n if (error instanceof BRPCError) {\r\n return new Response(JSON.stringify({ error: error.toJSON() }), {\r\n status: error.httpStatus,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Handle generic Error instances with special messages (backward compatibility)\r\n if (error instanceof Error) {\r\n const status =\r\n error.message === \"Not Found\"\r\n ? 404\r\n : error.message === \"Unauthorized\"\r\n ? 401\r\n : error.message === \"Forbidden\"\r\n ? 403\r\n : error.message === \"Request payload too large\"\r\n ? 413\r\n : error.message === \"Request timeout\"\r\n ? 408\r\n : 500;\r\n\r\n const errorCode =\r\n status === 404\r\n ? \"NOT_FOUND\"\r\n : status === 401\r\n ? \"UNAUTHORIZED\"\r\n : status === 403\r\n ? \"FORBIDDEN\"\r\n : status === 413\r\n ? \"PAYLOAD_TOO_LARGE\"\r\n : status === 408\r\n ? \"TIMEOUT\"\r\n : \"INTERNAL_SERVER_ERROR\";\r\n\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n code: errorCode,\r\n message: error.message,\r\n },\r\n }),\r\n {\r\n status,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n }\r\n );\r\n }\r\n\r\n // Fallback for unknown errors\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n code: \"INTERNAL_SERVER_ERROR\",\r\n message: \"Internal Server Error\",\r\n },\r\n }),\r\n {\r\n status: 500,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n }\r\n );\r\n }\r\n\r\n /**\r\n * handles the default cache control headers for file routes\r\n */\r\n private handleFileCacheControl(contentType: string) {\r\n let cacheControl: string;\r\n if (contentType?.includes(\"text/html\")) {\r\n cacheControl = \"public, max-age=0, must-revalidate\";\r\n } else if (\r\n contentType?.includes(\"text/css\") ||\r\n contentType?.includes(\"text/javascript\") ||\r\n contentType?.includes(\"application/javascript\")\r\n ) {\r\n cacheControl = \"public, max-age=31536000, immutable\";\r\n } else {\r\n cacheControl = \"public, max-age=86400, stale-while-revalidate=604800\";\r\n }\r\n return cacheControl;\r\n }\r\n\r\n /**\r\n * Handles the behavior of each request\r\n */\r\n private async handleRequest(\r\n req: Request,\r\n server?: Server\r\n ): Promise<Response> {\r\n const timeoutPromise = new Promise<Response>((_, reject) => {\r\n setTimeout(\r\n () => reject(new Error(\"Request timeout\")),\r\n this.requestTimeout\r\n );\r\n });\r\n\r\n try {\r\n return await Promise.race([\r\n this._handleRequest(req, server),\r\n timeoutPromise,\r\n ]);\r\n } catch (error) {\r\n return this.handleError(error, req);\r\n }\r\n }\r\n\r\n private async _handleRequest(\r\n req: Request,\r\n server?: Server\r\n ): Promise<Response> {\r\n try {\r\n if (req.method === \"OPTIONS\") {\r\n return new Response(null, {\r\n status: 204,\r\n headers: buildHeaders()\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n const url = new URL(req.url);\r\n let path = url.pathname;\r\n path = path.startsWith(this.prefix)\r\n ? path.slice(this.prefix.length)\r\n : path;\r\n path = path.startsWith(\"/\") ? path : \"/\" + path;\r\n\r\n if (path === \"/ws\" && server) {\r\n if (this.debug) {\r\n console.log(\"🔌 WebSocket upgrade requested\");\r\n }\r\n return this.handleWebSocketUpgrade(req, server);\r\n }\r\n\r\n // Use the optimized route matcher\r\n const routeResult = this.findRoute(path.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult) {\r\n return new Response(\"Not Found\", {\r\n status: 404,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n const { params, route } = routeResult;\r\n\r\n if (!route.handler) {\r\n return new Response(\"Handler not implemented\", {\r\n status: 501,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Create context with all the properties from BaseContext\r\n const ctx = await this.contextCreator(req);\r\n Object.assign(ctx, {\r\n params,\r\n publishToProcedure: async <\r\n P extends Procedure<any, any, any, \"subscription\">\r\n >(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n params: Record<string, string>\r\n ) => {\r\n await this.publishToProcedure(procedure, input, ctx, params);\r\n },\r\n });\r\n\r\n // Run global middlewares\r\n for (const middleware of this.globalMiddlewares) {\r\n await middleware(ctx);\r\n }\r\n\r\n // Run procedure-specific middlewares\r\n for (const middleware of route.middlewares) {\r\n await middleware(ctx);\r\n }\r\n\r\n // Parse input based on procedure type\r\n const input = await this.parseInput(req, route._input, route._type);\r\n const result = await route.handler({ ctx, input });\r\n\r\n // Handle different response types based on procedure type\r\n return await this.handleProcedureResponse(\r\n route._type,\r\n result,\r\n buildHeaders()\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build()\r\n );\r\n } catch (error) {\r\n return this.handleError(error, req);\r\n }\r\n }\r\n\r\n /**\r\n * Handle responses based on procedure type\r\n */\r\n private async handleProcedureResponse(\r\n procedureType: ProcedureType,\r\n result: any,\r\n corsHeaders: Record<string, string>\r\n ): Promise<Response> {\r\n switch (procedureType) {\r\n case \"file\":\r\n if (result instanceof Response) {\r\n return result;\r\n }\r\n const contentType = (result as BunFile).type;\r\n return new Response(result, {\r\n headers: {\r\n ...corsHeaders,\r\n \"Content-Type\": contentType,\r\n \"Cache-Control\": this.development\r\n ? \"public, max-age=0, must-revalidate\"\r\n : this.handleFileCacheControl(contentType),\r\n },\r\n });\r\n\r\n case \"fileStream\":\r\n if (\r\n typeof result === \"object\" &&\r\n result !== null &&\r\n \"type\" in result &&\r\n result.type === \"stream\"\r\n ) {\r\n // const videoHandler = new VideoStreamingHandler();\r\n // return await videoHandler.handleVideoStream(\r\n // result.file,\r\n // result.request\r\n // );\r\n const mediaStreamResult = result as StreamableMediaResponse;\r\n return await streamMediaInternal(\r\n result.file,\r\n result.request,\r\n this.allowedOrigins,\r\n mediaStreamResult.options\r\n );\r\n }\r\n // Fallback to regular file response\r\n const streamContentType = (result as Blob).type;\r\n return new Response(result, {\r\n headers: {\r\n ...corsHeaders,\r\n \"Content-Type\": streamContentType,\r\n \"Cache-Control\": this.development\r\n ? \"public, max-age=0, must-revalidate\"\r\n : this.handleFileCacheControl(streamContentType),\r\n },\r\n });\r\n\r\n case \"html\":\r\n // Handle HTML rendering\r\n if (typeof result === \"string\") {\r\n return new Response(result, {\r\n headers: {\r\n ...corsHeaders,\r\n \"Content-Type\": \"text/html\",\r\n },\r\n });\r\n }\r\n break;\r\n\r\n case \"query\":\r\n case \"mutation\":\r\n case \"formMutation\":\r\n case \"streamQuery\":\r\n case \"streamMutation\":\r\n case \"subscription\":\r\n default:\r\n // Standard JSON response\r\n return new Response(JSON.stringify({ data: result }), {\r\n headers: { ...corsHeaders, \"Content-Type\": \"application/json\" },\r\n });\r\n }\r\n\r\n // Fallback\r\n return new Response(JSON.stringify({ data: result }), {\r\n headers: { ...corsHeaders, \"Content-Type\": \"application/json\" },\r\n });\r\n }\r\n\r\n //*Websockets\r\n\r\n private cleanupStaleConnections() {\r\n const now = Date.now();\r\n for (const ws of this.activeConnections) {\r\n if (now - ws.data.lastActivity > this.connectionTimeout) {\r\n ws.close();\r\n this.activeConnections.delete(ws);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * handleWebSocketUpgrade\r\n * @description upgrades the incoming request to\r\n * a websocket connection\r\n */\r\n private async handleWebSocketUpgrade(\r\n req: Request,\r\n server: Server\r\n ): Promise<Response> {\r\n if (this.activeConnections.size >= this.maxConnections) {\r\n return new Response(\"Too many connections\", { status: 503 });\r\n }\r\n\r\n const ctx = await this.contextCreator(req);\r\n for (const middleware of this.globalMiddlewares) {\r\n await middleware(ctx);\r\n }\r\n\r\n const success = server.upgrade<WebSocketData>(req, {\r\n data: {\r\n ctx,\r\n topics: new Set<string>(),\r\n lastActivity: Date.now(),\r\n },\r\n });\r\n\r\n if (success) {\r\n return new Response(null, { status: 101 }); // Switching Protocols\r\n }\r\n\r\n return new Response(\"WebSocket upgrade failed\", { status: 500 });\r\n }\r\n\r\n /**\r\n * Handles WebSocket authentication\r\n */\r\n private async handleAuthenticate(\r\n ws: ServerWebSocket<WebSocketData>,\r\n token: string,\r\n _ctx: C\r\n ) {\r\n try {\r\n // Extract token from \"Bearer xxx\" format if needed\r\n const finalToken = token.startsWith(\"Bearer \")\r\n ? token.substring(7)\r\n : token;\r\n\r\n // Store the token in the WebSocket context\r\n ws.data.ctx.authToken = finalToken;\r\n\r\n // Additionally, we can verify the token and update the session\r\n // For example, with Supabase:\r\n // const { data, error } = await supabase.auth.getUser(finalToken);\r\n // if (error) throw new Error(error.message);\r\n // ws.data.ctx.session = { user: data.user };\r\n\r\n // Send success response\r\n ws.send(\r\n JSON.stringify({\r\n type: \"auth_success\",\r\n message: \"Authentication successful\",\r\n })\r\n );\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket authenticated successfully\");\r\n }\r\n } catch (error) {\r\n ws.send(\r\n JSON.stringify({\r\n type: \"auth_error\",\r\n error:\r\n error instanceof Error ? error.message : \"Authentication failed\",\r\n })\r\n );\r\n\r\n if (this.debug) {\r\n console.error(\"WebSocket authentication error:\", error);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * handles the ws client topic subscription\r\n */\r\n private async handleSubscribe(\r\n ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n _ctx: C\r\n ) {\r\n // Find route first to validate subscription\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult || routeResult.route._type !== \"subscription\") {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n ws.subscribe(topic);\r\n ws.data.topics.add(topic);\r\n }\r\n\r\n /**\r\n * handles the ws client topic unsubscription\r\n */\r\n private async handleUnsubscribe(\r\n ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n _ctx: C\r\n ) {\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult || routeResult.route._type !== \"subscription\") {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n ws.unsubscribe(topic);\r\n ws.data.topics.delete(topic);\r\n }\r\n\r\n /**\r\n * handles the message publication of a certain ws client\r\n */\r\n private async handlePublish(\r\n _ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n message: any,\r\n ctx: C\r\n ) {\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult) {\r\n throw new Error(\"Subscription not found\");\r\n }\r\n\r\n const { params, route } = routeResult;\r\n\r\n if (route._type !== \"subscription\" || !route.handler) {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n Object.assign(ctx, { params });\r\n\r\n // Only validate if the procedure has defined input\r\n const validatedMessage = route._input.fields\r\n ? route._input.parse(message)\r\n : message;\r\n\r\n const publishMessage = await route.handler({\r\n ctx,\r\n input: validatedMessage,\r\n });\r\n\r\n const converted = {\r\n topic: topic,\r\n data: publishMessage,\r\n type: \"publish\",\r\n };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${topic}`, converted);\r\n }\r\n\r\n // this.server!.publish(topic, JSON.stringify(publishMessage));\r\n this.server!.publish(topic, JSON.stringify(converted));\r\n }\r\n\r\n private async publishToProcedure<\r\n P extends Procedure<any, any, any, \"subscription\">\r\n >(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n ctx: C,\r\n params: Record<string, string>\r\n ) {\r\n if (procedure._type !== \"subscription\" || !procedure.handler) {\r\n throw new Error(\"Can only publish to subscription procedures\");\r\n }\r\n\r\n const routePath = this.findRoutePath(this.routes, procedure);\r\n\r\n if (!routePath) {\r\n throw new Error(\"Subscription route not found\");\r\n }\r\n\r\n // Replace params in the path\r\n const finalPath = routePath\r\n .split(\"/\")\r\n .map((part) => {\r\n if (part.startsWith(\":\")) {\r\n const paramName = part.slice(1);\r\n const paramValue = params[paramName];\r\n if (!paramValue) {\r\n throw new Error(`Missing parameter: ${paramName}`);\r\n }\r\n return paramValue;\r\n }\r\n return part;\r\n })\r\n .join(\"/\");\r\n\r\n const result = await procedure.handler({\r\n ctx: { ...ctx, params },\r\n input,\r\n });\r\n\r\n const converted = { topic: finalPath, data: result, type: \"publish\" };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${finalPath}`, converted);\r\n }\r\n\r\n // this.server?.publish(finalPath, JSON.stringify(result));\r\n this.server?.publish(finalPath, JSON.stringify(converted));\r\n return result;\r\n }\r\n\r\n async publish<P extends Procedure<any, any, any, \"subscription\">>(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n params?: Record<string, string>\r\n ) {\r\n if (procedure._type !== \"subscription\" || !procedure.handler) {\r\n throw new Error(\"Can only publish to subscription procedures\");\r\n }\r\n\r\n const routePath = this.findRoutePath(this.routes, procedure);\r\n\r\n if (!routePath) {\r\n throw new Error(\"Subscription route not found\");\r\n }\r\n\r\n // Replace params in the path\r\n const finalPath = routePath\r\n .split(\"/\")\r\n .map((part) => {\r\n if (part.startsWith(\":\")) {\r\n const paramName = part.slice(1);\r\n const paramValue = params ? params[paramName] : null;\r\n if (!paramValue) {\r\n throw new Error(`Missing parameter: ${paramName}`);\r\n }\r\n return paramValue;\r\n }\r\n return part;\r\n })\r\n .join(\"/\");\r\n\r\n const result = await procedure.handler({\r\n ctx: { params },\r\n input,\r\n });\r\n\r\n const converted = { topic: finalPath, data: result, type: \"publish\" };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${finalPath}`, converted);\r\n }\r\n\r\n // this.server?.publish(finalPath, JSON.stringify(result));\r\n this.server?.publish(finalPath, JSON.stringify(converted));\r\n return result;\r\n }\r\n\r\n /**\r\n * findRoutePath\r\n * @description finds the full path to a procedure in the routes tree\r\n */\r\n private findRoutePath(\r\n routes: Routes,\r\n targetProcedure: Procedure<any, any, any, any>,\r\n prefix: string = \"\"\r\n ): string | null {\r\n for (const [key, value] of Object.entries(routes)) {\r\n const currentPath = prefix ? `${prefix}/${key}` : key;\r\n\r\n if (value === targetProcedure) {\r\n return currentPath;\r\n }\r\n\r\n if (value && typeof value === \"object\" && !(\"_type\" in value)) {\r\n const found = this.findRoutePath(\r\n value as Routes,\r\n targetProcedure,\r\n currentPath\r\n );\r\n if (found) return found;\r\n }\r\n }\r\n\r\n return null;\r\n }\r\n\r\n private getIntegrationRoutes() {\r\n if (!this.integrations) return undefined;\r\n\r\n let routes: any = {}; // Initialize the routes object\r\n\r\n if (this.integrations.betterAuth) {\r\n const handler = this.integrations.betterAuth.handler;\r\n if (!handler) {\r\n throw new Error(\"Please provide a handler for betterAuth integration\");\r\n }\r\n routes[\"/api/auth/*\"] = async (req: BunRequest<\"auth\">) => {\r\n return await handler(req);\r\n };\r\n }\r\n\r\n if (this.integrations.rawRoutes) {\r\n Object.keys(this.integrations.rawRoutes).forEach((val) => {\r\n routes[val] = this.integrations!.rawRoutes![val];\r\n });\r\n }\r\n\r\n return Object.keys(routes).length > 0 ? routes : undefined;\r\n }\r\n\r\n /**\r\n * New utility methods for route management\r\n */\r\n public getRouteStats() {\r\n return {\r\n cache: this.routeMatcher.getCacheStats(),\r\n suggestions: this.routeMatcher.getOptimizationSuggestions(),\r\n };\r\n }\r\n\r\n public clearRouteCache() {\r\n this.routeMatcher.clearCache();\r\n if (this.debug) {\r\n console.log(\"[ROUTEMATCHER] Route cache cleared\");\r\n }\r\n }\r\n\r\n public benchmarkRoutes(\r\n testPaths: string[] = [\"/users/123\", \"/posts/456/comments\"],\r\n iterations = 1000\r\n ) {\r\n if (this.debug) {\r\n const results = this.routeMatcher.benchmark(testPaths, iterations);\r\n console.log(\"[ROUTEMATCHER]\", \"Route matching benchmark:\", results);\r\n return results;\r\n }\r\n }\r\n\r\n /**\r\n * Hot reload support for development\r\n */\r\n public updateRoutes(newRoutes: Routes) {\r\n if (this.development) {\r\n this.routes = newRoutes;\r\n this.routeMatcher = new RouteMatcher(newRoutes, {\r\n enableCaching: false, // No caching in dev\r\n debug: this.debug,\r\n });\r\n console.log(\"[HMR]\", \"Server Routes updated\");\r\n } else {\r\n console.warn(\"Route updates are only allowed in development mode\");\r\n }\r\n }\r\n\r\n /**\r\n * publish\r\n * @description publish a message to a topic from\r\n * the server itself\r\n */\r\n // private publish(topic: string, message: string | Uint8Array) {\r\n // if (this.server) {\r\n // this.server.publish(topic, message);\r\n // } else {\r\n // //?debug\r\n // //console.warn(\"Server not initialized. Cannot publish message.\");\r\n // }\r\n // }\r\n public freePublish<T>(topic: string, message: T) {\r\n const converted = { topic: topic, data: message, type: \"publish\" };\r\n this.server?.publish(topic, JSON.stringify(converted));\r\n }\r\n\r\n //*start server\r\n\r\n /**\r\n * Listen\r\n * @description starts up the Bun http server\r\n */\r\n listen(port: number, callback?: () => void) {\r\n this.server = Bun.serve({\r\n port,\r\n routes: this.getIntegrationRoutes(),\r\n fetch: (req, server) => this.handleRequest(req, server),\r\n websocket: {\r\n open: (ws: ServerWebSocket<WebSocketData>) => {\r\n this.activeConnections.add(ws);\r\n if (this.wsConfig?.onOpen) {\r\n this.wsConfig.onOpen(ws, ws.data.ctx);\r\n }\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket connection opened\", {\r\n connections: this.activeConnections.size,\r\n });\r\n }\r\n },\r\n message: async (ws: ServerWebSocket<WebSocketData>, message: any) => {\r\n try {\r\n ws.data.lastActivity = Date.now();\r\n const { type, topic, token, data } = JSON.parse(message as string);\r\n // console.log(type, token);\r\n const ctx = ws.data.ctx;\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket message received:\", {\r\n type,\r\n topic,\r\n });\r\n }\r\n\r\n switch (type) {\r\n case \"authenticate\":\r\n await this.handleAuthenticate(ws, token, ctx);\r\n break;\r\n case \"subscribe\":\r\n await this.handleSubscribe(ws, topic, ctx);\r\n break;\r\n case \"unsubscribe\":\r\n await this.handleUnsubscribe(ws, topic, ctx);\r\n break;\r\n case \"publish\":\r\n await this.handlePublish(ws, topic, data, ctx);\r\n break;\r\n default:\r\n ws.send(JSON.stringify({ error: \"Unknown message type\" }));\r\n if (this.debug) {\r\n console.error(\"Unknown WebSocket message type:\", type);\r\n }\r\n }\r\n } catch (error) {\r\n if (this.debug) {\r\n console.error(\"[BRPC]\", \"WebSocket message error:\", error);\r\n }\r\n\r\n if (error instanceof ZodError) {\r\n ws.send(JSON.stringify({ error: \"Invalid message format\" }));\r\n } else if (error instanceof Error) {\r\n ws.send(JSON.stringify({ error: error.message }));\r\n }\r\n }\r\n },\r\n close: (\r\n ws: ServerWebSocket<WebSocketData>,\r\n code: number,\r\n reason: string\r\n ) => {\r\n this.activeConnections.delete(ws);\r\n ws.data.topics.forEach((topic) => {\r\n ws.unsubscribe(topic);\r\n });\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket connection closed\", {\r\n code,\r\n reason,\r\n remainingConnections: this.activeConnections.size,\r\n });\r\n }\r\n\r\n if (this.wsConfig?.onClose) {\r\n this.wsConfig.onClose(ws, code, reason, ws.data.ctx);\r\n }\r\n },\r\n },\r\n });\r\n\r\n if (callback) callback();\r\n }\r\n}\r\n\r\n//*export needed methods and types\r\n\r\n/**\r\n * createRouter\r\n * @description helper function to create a\r\n * Router instance\r\n */\r\nexport function createRouter<C extends BaseContext>(config: RouterConfig<C>) {\r\n return new Router(config);\r\n}\r\n\r\nexport type CreateRouter = ReturnType<typeof createRouter>;\r\n",
7
7
  "export type BRPCErrorCode =\r\n | \"BAD_REQUEST\"\r\n | \"UNAUTHORIZED\"\r\n | \"FORBIDDEN\"\r\n | \"NOT_FOUND\"\r\n | \"METHOD_NOT_SUPPORTED\"\r\n | \"TIMEOUT\"\r\n | \"CONFLICT\"\r\n | \"PRECONDITION_FAILED\"\r\n | \"PAYLOAD_TOO_LARGE\"\r\n | \"UNPROCESSABLE_CONTENT\"\r\n | \"TOO_MANY_REQUESTS\"\r\n | \"CLIENT_CLOSED_REQUEST\"\r\n | \"INTERNAL_SERVER_ERROR\"\r\n | \"NOT_IMPLEMENTED\"\r\n | \"BAD_GATEWAY\"\r\n | \"SERVICE_UNAVAILABLE\"\r\n | \"GATEWAY_TIMEOUT\";\r\n\r\nexport interface BRPCErrorOptions {\r\n code: BRPCErrorCode;\r\n message: string;\r\n clientCode?: string; // Custom code for client-side detection\r\n cause?: Error;\r\n data?: Record<string, any>;\r\n}\r\n\r\nexport class BRPCError extends Error {\r\n public readonly code: BRPCErrorCode;\r\n public readonly clientCode?: string;\r\n public readonly httpStatus: number;\r\n public readonly data?: Record<string, any>;\r\n public readonly cause?: Error;\r\n\r\n private static readonly STATUS_MAP: Record<BRPCErrorCode, number> = {\r\n BAD_REQUEST: 400,\r\n UNAUTHORIZED: 401,\r\n FORBIDDEN: 403,\r\n NOT_FOUND: 404,\r\n METHOD_NOT_SUPPORTED: 405,\r\n TIMEOUT: 408,\r\n CONFLICT: 409,\r\n PRECONDITION_FAILED: 412,\r\n PAYLOAD_TOO_LARGE: 413,\r\n UNPROCESSABLE_CONTENT: 422,\r\n TOO_MANY_REQUESTS: 429,\r\n CLIENT_CLOSED_REQUEST: 499,\r\n INTERNAL_SERVER_ERROR: 500,\r\n NOT_IMPLEMENTED: 501,\r\n BAD_GATEWAY: 502,\r\n SERVICE_UNAVAILABLE: 503,\r\n GATEWAY_TIMEOUT: 504,\r\n };\r\n\r\n constructor(options: BRPCErrorOptions) {\r\n super(options.message);\r\n\r\n this.name = \"BRPCError\";\r\n this.code = options.code;\r\n this.clientCode = options.clientCode;\r\n this.httpStatus = BRPCError.STATUS_MAP[options.code];\r\n this.data = options.data;\r\n this.cause = options.cause;\r\n\r\n // Maintains proper stack trace for where our error was thrown (only available on V8)\r\n if (Error.captureStackTrace) {\r\n Error.captureStackTrace(this, BRPCError);\r\n }\r\n }\r\n\r\n /**\r\n * Create a serializable object for sending to frontend\r\n */\r\n toJSON() {\r\n return {\r\n name: this.name,\r\n code: this.code,\r\n clientCode: this.clientCode,\r\n message: this.message,\r\n data: this.data,\r\n httpStatus: this.httpStatus,\r\n };\r\n }\r\n\r\n /**\r\n * Static factory methods for common errors\r\n */\r\n static badRequest(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"BAD_REQUEST\", message, clientCode, data });\r\n }\r\n\r\n static unauthorized(\r\n message: string = \"Unauthorized\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"UNAUTHORIZED\", message, clientCode, data });\r\n }\r\n\r\n static forbidden(\r\n message: string = \"Forbidden\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"FORBIDDEN\", message, clientCode, data });\r\n }\r\n\r\n static notFound(\r\n message: string = \"Not Found\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static preconditionFailed(\r\n message: string = \"Precondition failed\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static conflict(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"CONFLICT\", message, clientCode, data });\r\n }\r\n\r\n static unprocessableContent(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"UNPROCESSABLE_CONTENT\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static tooManyRequests(\r\n message: string = \"Too many requests\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"TOO_MANY_REQUESTS\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static internalServerError(\r\n message: string = \"Internal Server Error\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"INTERNAL_SERVER_ERROR\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static timeout(\r\n message: string = \"Request timeout\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"TIMEOUT\", message, clientCode, data });\r\n }\r\n}\r\n",
8
8
  "// Optimized route finding for BRPC\r\n\r\nimport type { Routes, Procedure, ProcedureType } from \"../types\";\r\n\r\ninterface RouteMatch {\r\n params: Record<string, string>;\r\n route: Procedure<any, any, any, any>;\r\n}\r\n\r\ninterface CompiledRoute {\r\n handler: Procedure<any, any, any, any>;\r\n paramNames: string[];\r\n hasWildcard: boolean;\r\n wildcardIndex?: number;\r\n}\r\n\r\ninterface RouteNode {\r\n // Static routes (exact match) - O(1) lookup with Map\r\n static: Map<string, RouteNode>;\r\n // Parameter route (single dynamic segment)\r\n param?: { name: string; node: RouteNode };\r\n // Wildcard route (catches everything)\r\n wildcard?: CompiledRoute;\r\n // Handler if this node is a terminal route\r\n handler?: CompiledRoute;\r\n // Index handler\r\n index?: CompiledRoute;\r\n}\r\n\r\ninterface RouteMatcherOptions {\r\n enableCaching?: boolean;\r\n maxCacheSize?: number;\r\n debug?: boolean;\r\n}\r\n\r\ninterface RouteStats {\r\n totalRoutes: number;\r\n staticRoutes: number;\r\n paramRoutes: number;\r\n wildcardRoutes: number;\r\n indexRoutes: number;\r\n maxDepth: number;\r\n procedureTypes: Record<ProcedureType, number>;\r\n}\r\n\r\nexport class RouteMatcher {\r\n private routeTree: RouteNode;\r\n private routeCache: Map<string, RouteMatch | null>;\r\n private readonly options: Required<RouteMatcherOptions>;\r\n private routeStats: RouteStats;\r\n\r\n constructor(routes: Routes, options: RouteMatcherOptions = {}) {\r\n this.options = {\r\n enableCaching: true,\r\n maxCacheSize: 1000,\r\n debug: false,\r\n ...options,\r\n };\r\n\r\n this.routeCache = new Map();\r\n this.routeStats = this.analyzeRoutes(routes);\r\n this.routeTree = this.compileRoutes(routes);\r\n\r\n if (this.options.debug) {\r\n console.log(\"[ROUTEMATCHER]\", \"initialized with stats:\", this.routeStats);\r\n }\r\n }\r\n\r\n /**\r\n * Main route matching method\r\n */\r\n public match(pathParts: string[]): RouteMatch | null {\r\n if (this.options.enableCaching) {\r\n return this.matchWithCache(pathParts);\r\n }\r\n return this.matchDirect(pathParts);\r\n }\r\n\r\n /**\r\n * Route matching with caching\r\n */\r\n private matchWithCache(pathParts: string[]): RouteMatch | null {\r\n const cacheKey = pathParts.join(\"/\");\r\n\r\n // Check cache first\r\n if (this.routeCache.has(cacheKey)) {\r\n const cached = this.routeCache.get(cacheKey)!;\r\n if (this.options.debug && cached) {\r\n console.log(\"[ROUTEMATCHER]\", `🎯 Cache hit for route: /${cacheKey}`);\r\n }\r\n return cached;\r\n }\r\n\r\n // Find route\r\n const result = this.matchDirect(pathParts);\r\n\r\n // Cache the result (including null results to avoid repeated failed lookups)\r\n this.cacheResult(cacheKey, result);\r\n\r\n if (this.options.debug) {\r\n console.log(\r\n \"[ROUTEMATCHER]\",\r\n `💾 Cached route: /${cacheKey} -> ${result ? \"found\" : \"not found\"}`\r\n );\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Direct route matching without caching\r\n */\r\n private matchDirect(pathParts: string[]): RouteMatch | null {\r\n const startTime = this.options.debug ? performance.now() : 0;\r\n\r\n const result = this.traverseTree(this.routeTree, pathParts, 0, {});\r\n\r\n if (this.options.debug) {\r\n const duration = performance.now() - startTime;\r\n const routeType = result?.route._type || \"none\";\r\n console.log(\r\n \"[ROUTEMATCHER]\",\r\n `⚡ Route match: /${pathParts.join(\r\n \"/\"\r\n )} (${routeType}) took ${duration.toFixed(3)}ms`\r\n );\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Traverse the compiled route tree\r\n */\r\n private traverseTree(\r\n node: RouteNode,\r\n pathParts: string[],\r\n index: number,\r\n params: Record<string, string>\r\n ): RouteMatch | null {\r\n // If we've consumed all path parts\r\n if (index >= pathParts.length) {\r\n // Try handler first, then index\r\n if (node.handler) {\r\n return { params, route: node.handler.handler };\r\n }\r\n if (node.index) {\r\n return { params, route: node.index.handler };\r\n }\r\n return null;\r\n }\r\n\r\n const segment = pathParts[index];\r\n const decodedSegment = decodeURIComponent(segment);\r\n\r\n // 1. Try static route first (O(1) lookup with Map)\r\n const staticChild = node.static.get(decodedSegment);\r\n if (staticChild) {\r\n const result = this.traverseTree(\r\n staticChild,\r\n pathParts,\r\n index + 1,\r\n params\r\n );\r\n if (result) return result;\r\n }\r\n\r\n // 2. Try parameter route\r\n if (node.param) {\r\n const newParams = { ...params, [node.param.name]: decodedSegment };\r\n const result = this.traverseTree(\r\n node.param.node,\r\n pathParts,\r\n index + 1,\r\n newParams\r\n );\r\n if (result) return result;\r\n }\r\n\r\n // 3. Try wildcard route (catches all remaining segments)\r\n if (node.wildcard) {\r\n const wildcardPath = pathParts.slice(index).join(\"/\");\r\n return {\r\n params: { ...params, wildcardPath },\r\n route: node.wildcard.handler,\r\n };\r\n }\r\n\r\n return null;\r\n }\r\n\r\n /**\r\n * Compile the routes object into an optimized tree structure\r\n */\r\n private compileRoutes(routes: Routes, paramNames: string[] = []): RouteNode {\r\n const node: RouteNode = {\r\n static: new Map(),\r\n };\r\n\r\n for (const [key, value] of Object.entries(routes)) {\r\n if (key === \"index\" && this.isProcedure(value)) {\r\n // Index route\r\n node.index = {\r\n handler: value,\r\n paramNames: [...paramNames],\r\n hasWildcard: false,\r\n };\r\n continue;\r\n }\r\n\r\n if (key === \"*\") {\r\n // Wildcard route\r\n if (this.isProcedure(value)) {\r\n node.wildcard = {\r\n handler: value,\r\n paramNames: [...paramNames, \"wildcardPath\"],\r\n hasWildcard: true,\r\n wildcardIndex: paramNames.length,\r\n };\r\n }\r\n continue;\r\n }\r\n\r\n if (this.isProcedure(value)) {\r\n // Terminal route with handler\r\n const compiledRoute: CompiledRoute = {\r\n handler: value,\r\n paramNames: [...paramNames],\r\n hasWildcard: false,\r\n };\r\n\r\n if (key.startsWith(\":\")) {\r\n // Parameter route\r\n const paramName = key.slice(1);\r\n const terminalNode: RouteNode = {\r\n static: new Map(),\r\n handler: compiledRoute,\r\n };\r\n node.param = { name: paramName, node: terminalNode };\r\n } else {\r\n // Static route\r\n const terminalNode: RouteNode = {\r\n static: new Map(),\r\n handler: compiledRoute,\r\n };\r\n node.static.set(key, terminalNode);\r\n }\r\n } else if (typeof value === \"object\" && value !== null) {\r\n // Nested routes\r\n if (key.startsWith(\":\")) {\r\n // Parameter segment with nested routes\r\n const paramName = key.slice(1);\r\n const childNode = this.compileRoutes(value as Routes, [\r\n ...paramNames,\r\n paramName,\r\n ]);\r\n node.param = { name: paramName, node: childNode };\r\n } else {\r\n // Static segment with nested routes\r\n const childNode = this.compileRoutes(value as Routes, paramNames);\r\n node.static.set(key, childNode);\r\n }\r\n }\r\n }\r\n\r\n return node;\r\n }\r\n\r\n /**\r\n * Type guard to check if value is a Procedure\r\n */\r\n private isProcedure(value: any): value is Procedure<any, any, any, any> {\r\n return (\r\n value &&\r\n typeof value === \"object\" &&\r\n \"_type\" in value &&\r\n \"_input\" in value\r\n );\r\n }\r\n\r\n /**\r\n * Cache management with LRU behavior\r\n */\r\n private cacheResult(key: string, result: RouteMatch | null): void {\r\n // Implement LRU: remove oldest entry if cache is full\r\n if (this.routeCache.size >= this.options.maxCacheSize) {\r\n const firstKey = this.routeCache.keys().next().value;\r\n if (firstKey !== undefined) {\r\n this.routeCache.delete(firstKey);\r\n\r\n if (this.options.debug) {\r\n console.log(`🗑️ Evicted oldest cache entry: ${firstKey}`);\r\n }\r\n }\r\n }\r\n\r\n this.routeCache.set(key, result);\r\n }\r\n\r\n /**\r\n * Analyze route structure for optimization insights and debugging\r\n */\r\n private analyzeRoutes(routes: Routes): RouteStats {\r\n const stats: RouteStats = {\r\n totalRoutes: 0,\r\n staticRoutes: 0,\r\n paramRoutes: 0,\r\n wildcardRoutes: 0,\r\n indexRoutes: 0,\r\n maxDepth: 0,\r\n procedureTypes: {\r\n query: 0,\r\n mutation: 0,\r\n subscription: 0,\r\n file: 0,\r\n fileStream: 0,\r\n streamQuery: 0,\r\n streamMutation: 0,\r\n formMutation: 0,\r\n html: 0,\r\n },\r\n };\r\n\r\n const traverse = (obj: any, depth = 0): void => {\r\n stats.maxDepth = Math.max(stats.maxDepth, depth);\r\n\r\n for (const [key, value] of Object.entries(obj)) {\r\n if (this.isProcedure(value)) {\r\n stats.totalRoutes++;\r\n\r\n // Count procedure types\r\n if (value._type in stats.procedureTypes) {\r\n stats.procedureTypes[value._type as ProcedureType]++;\r\n }\r\n\r\n // Count route patterns\r\n if (key === \"index\") {\r\n stats.indexRoutes++;\r\n } else if (key.startsWith(\":\")) {\r\n stats.paramRoutes++;\r\n } else if (key === \"*\") {\r\n stats.wildcardRoutes++;\r\n } else {\r\n stats.staticRoutes++;\r\n }\r\n } else if (typeof value === \"object\" && value !== null) {\r\n traverse(value, depth + 1);\r\n }\r\n }\r\n };\r\n\r\n traverse(routes);\r\n return stats;\r\n }\r\n\r\n /**\r\n * Utility methods\r\n */\r\n public clearCache(): void {\r\n this.routeCache.clear();\r\n if (this.options.debug) {\r\n console.log(\"[ROUTEMATCHER]\", \"🧹 Route cache cleared\");\r\n }\r\n }\r\n\r\n public getCacheStats(): {\r\n size: number;\r\n maxSize: number;\r\n hitRatio?: number;\r\n routeStats: RouteStats;\r\n } {\r\n return {\r\n size: this.routeCache.size,\r\n maxSize: this.options.maxCacheSize,\r\n routeStats: this.routeStats,\r\n };\r\n }\r\n\r\n /**\r\n * Get suggestions for route optimization\r\n */\r\n public getOptimizationSuggestions(): string[] {\r\n const suggestions: string[] = [];\r\n const { routeStats } = this;\r\n\r\n if (routeStats.paramRoutes > routeStats.staticRoutes * 2) {\r\n suggestions.push(\r\n \"🚀 Consider grouping routes to reduce parameter route overhead\"\r\n );\r\n }\r\n\r\n if (routeStats.maxDepth > 6) {\r\n suggestions.push(\r\n \"📊 Deep route nesting detected - consider flattening some routes\"\r\n );\r\n }\r\n\r\n if (routeStats.totalRoutes > 200) {\r\n suggestions.push(\r\n \"⚡ Large route table - consider route splitting or lazy loading\"\r\n );\r\n }\r\n\r\n if (!this.options.enableCaching && routeStats.totalRoutes > 50) {\r\n suggestions.push(\r\n \"💾 Enable caching for better performance with many routes\"\r\n );\r\n }\r\n\r\n if (routeStats.procedureTypes.file > 20) {\r\n suggestions.push(\r\n \"📁 Many file routes detected - consider static file serving\"\r\n );\r\n }\r\n\r\n if (routeStats.procedureTypes.subscription > 10) {\r\n suggestions.push(\r\n \"🔄 Many subscription routes - ensure WebSocket optimization\"\r\n );\r\n }\r\n\r\n return suggestions;\r\n }\r\n\r\n /**\r\n * Find all routes matching a pattern (useful for debugging)\r\n */\r\n public findRoutesByPattern(_pattern: string): string[] {\r\n const matchingRoutes: string[] = [];\r\n // const regex = new RegExp(pattern);\r\n\r\n // This would require storing all route paths during compilation\r\n // For now, return empty array - can be implemented if needed\r\n return matchingRoutes;\r\n }\r\n\r\n /**\r\n * Get route information for debugging\r\n */\r\n public getRouteInfo(path: string): {\r\n found: boolean;\r\n route?: Procedure<any, any, any, any>;\r\n params?: Record<string, string>;\r\n cached: boolean;\r\n matchTime?: number;\r\n } {\r\n const pathParts = path.split(\"/\").filter(Boolean);\r\n const cacheKey = pathParts.join(\"/\");\r\n const cached = this.routeCache.has(cacheKey);\r\n\r\n const startTime = performance.now();\r\n const result = this.matchDirect(pathParts);\r\n const matchTime = performance.now() - startTime;\r\n\r\n return {\r\n found: !!result,\r\n route: result?.route,\r\n params: result?.params,\r\n cached,\r\n matchTime,\r\n };\r\n }\r\n\r\n /**\r\n * Benchmark route matching performance\r\n */\r\n public benchmark(\r\n testPaths: string[],\r\n iterations = 1000\r\n ): {\r\n averageTime: number;\r\n totalTime: number;\r\n routesPerSecond: number;\r\n cacheHitRatio?: number;\r\n } {\r\n const pathSegments = testPaths.map((path) =>\r\n path.split(\"/\").filter(Boolean)\r\n );\r\n\r\n // Clear cache for accurate benchmarking\r\n const originalCache = new Map(this.routeCache);\r\n this.routeCache.clear();\r\n\r\n const startTime = performance.now();\r\n\r\n for (let i = 0; i < iterations; i++) {\r\n for (const segments of pathSegments) {\r\n this.matchDirect(segments); // Use direct matching for pure performance\r\n }\r\n }\r\n\r\n const endTime = performance.now();\r\n const totalTime = endTime - startTime;\r\n const totalMatches = iterations * testPaths.length;\r\n const averageTime = totalTime / totalMatches;\r\n const routesPerSecond = (totalMatches / totalTime) * 1000;\r\n\r\n // Restore cache\r\n this.routeCache = originalCache;\r\n\r\n return {\r\n averageTime,\r\n totalTime,\r\n routesPerSecond,\r\n };\r\n }\r\n\r\n /**\r\n * Warm up the cache with common routes\r\n */\r\n public warmupCache(commonPaths: string[]): void {\r\n if (!this.options.enableCaching) return;\r\n\r\n const startTime = performance.now();\r\n let warmedRoutes = 0;\r\n\r\n for (const path of commonPaths) {\r\n const pathParts = path.split(\"/\").filter(Boolean);\r\n const result = this.matchDirect(pathParts);\r\n if (result) {\r\n this.cacheResult(pathParts.join(\"/\"), result);\r\n warmedRoutes++;\r\n }\r\n }\r\n\r\n const endTime = performance.now();\r\n\r\n if (this.options.debug) {\r\n console.log(\r\n \"[ROUTEMATCHER]\",\r\n `🔥 Cache warmed up: ${warmedRoutes} routes in ${(\r\n endTime - startTime\r\n ).toFixed(2)}ms`\r\n );\r\n }\r\n }\r\n}\r\n\r\n// Export types for use in Router\r\nexport type { RouteMatch, RouteMatcherOptions, RouteStats };\r\n",
9
9
  "// @bun\nvar L=import.meta.require;var W={NO_CACHE:{cacheControl:\"no-cache, no-store, must-revalidate\",expires:new Date(0).toUTCString()},ONE_YEAR:{cacheControl:\"public, max-age=31536000\",expires:new Date(Date.now()+31536000000).toUTCString()},IMMUTABLE:{cacheControl:\"public, max-age=31536000, immutable\",expires:new Date(Date.now()+31536000000).toUTCString()},ONE_MONTH:{cacheControl:\"public, max-age=2592000\"},ONE_WEEK:{cacheControl:\"public, max-age=604800\"},ONE_DAY:{cacheControl:\"public, max-age=86400, must-revalidate\"},ONE_HOUR:{cacheControl:\"public, max-age=3600, must-revalidate\"},FIVE_MINUTES:{cacheControl:\"public, max-age=300\"},API:{cacheControl:\"public, max-age=300, s-maxage=3600\"},STYLESHEET:{cacheControl:\"public, max-age=2592000, must-revalidate\"},JAVASCRIPT:{cacheControl:\"public, max-age=2592000, must-revalidate\"},HASHED_ASSET:{cacheControl:\"public, max-age=31536000, immutable\",expires:new Date(Date.now()+31536000000).toUTCString()},FONT:{cacheControl:\"public, max-age=31536000\",expires:new Date(Date.now()+31536000000).toUTCString()},IMAGE:{cacheControl:\"public, max-age=2592000\"},FAVICON:{cacheControl:\"public, max-age=31536000\",expires:new Date(Date.now()+31536000000).toUTCString()},HTML_PAGE:{cacheControl:\"public, max-age=3600, must-revalidate\"},API_RESPONSE:{cacheControl:\"public, max-age=300, s-maxage=900\"},FEED:{cacheControl:\"public, max-age=3600\"},SITEMAP:{cacheControl:\"public, max-age=86400\"},MANIFEST:{cacheControl:\"public, max-age=86400, must-revalidate\"},SERVICE_WORKER:{cacheControl:\"no-cache, no-store, must-revalidate\",expires:new Date(0).toUTCString()},MEDIA_STREAM:{cacheControl:\"no-cache, no-store\"},DOCUMENT:{cacheControl:\"public, max-age=86400\"},ARCHIVE:{cacheControl:\"public, max-age=604800\"}};function J(k){return W[k]}var Y={html:\"text/html; charset=utf-8\",css:\"text/css\",javascript:\"application/javascript\",json:\"application/json\",xml:\"application/xml\",text:\"text/plain; charset=utf-8\",csv:\"text/csv\",markdown:\"text/markdown; charset=utf-8\",png:\"image/png\",jpeg:\"image/jpeg\",gif:\"image/gif\",svg:\"image/svg+xml\",webp:\"image/webp\",icon:\"image/x-icon\",bmp:\"image/bmp\",woff:\"font/woff\",woff2:\"font/woff2\",truetype:\"font/ttf\",\"embedded-opentype\":\"application/vnd.ms-fontobject\",opentype:\"font/otf\",mp4:\"video/mp4\",webm:\"video/webm\",ogg:\"video/ogg\",mpeg:\"audio/mpeg\",wav:\"audio/wav\",flac:\"audio/flac\",pdf:\"application/pdf\",doc:\"application/msword\",docx:\"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",zip:\"application/zip\",tar:\"application/x-tar\",gzip:\"application/gzip\",\"7z\":\"application/x-7z-compressed\",js:\"application/javascript\",txt:\"text/plain; charset=utf-8\",jpg:\"image/jpeg\",ico:\"image/x-icon\",ttf:\"font/ttf\",eot:\"application/vnd.ms-fontobject\",otf:\"font/otf\",mp3:\"audio/mpeg\",gz:\"application/gzip\"};function V(k){return Y[k]||\"application/octet-stream\"}function Z(k){if(typeof Bun!==\"undefined\"&&Bun.hash)return Bun.hash(k).toString(16);try{let z=L(\"crypto\"),K;if(typeof k===\"string\")K=Buffer.from(k);else if(k instanceof ArrayBuffer)K=Buffer.from(new Uint8Array(k));else K=Buffer.from(k);return z.createHash(\"md5\").update(K).digest(\"hex\")}catch{let z=typeof k===\"string\"?new TextEncoder().encode(k):k instanceof ArrayBuffer?new Uint8Array(k):k,K=0;for(let U=0;U<z.length;U++)K=(K<<5)-K+z[U],K=K&K;return Math.abs(K).toString(16).padStart(16,\"0\")}}function B(k,z,K){let U=k.headers.get(\"Origin\"),D=!0;if((K?.allowAll??!0)&&U)return U;if(z.length===0)return U||\"*\";if(!U)return z[0];if(z.includes(U))return U;return z[0]}class ${headers={};contentType(k){return this.headers[\"Content-Type\"]=V(k),this}filePath(k){let z=k.split(\".\").pop()?.toLowerCase()||\"\";return this.headers[\"Content-Type\"]=V(z),this}mimeType(k){return this.headers[\"Content-Type\"]=k,this}cache(k){let{cacheControl:z,expires:K}=J(k);if(this.headers[\"Cache-Control\"]=z,K)this.headers.Expires=K;return this}eTag(k){if(!k)return this;let z;if(typeof k===\"string\"&&k.startsWith('\"'))z=k;else z=`\"${Z(k)}\"`;return this.headers.ETag=z,this}lastModified(k){return this.headers[\"Last-Modified\"]=k.toUTCString(),this}contentLength(k){return this.headers[\"Content-Length\"]=k.toString(),this}redirect(k,z=!1){return this.headers.Location=k,this.headers[\"X-Redirect-Type\"]=z?\"permanent\":\"temporary\",this}cors({origin:k=\"*\",methods:z=[\"GET\",\"POST\",\"PUT\",\"DELETE\",\"OPTIONS\"],headers:K=[\"Content-Type\",\"Authorization\"],maxAge:U=86400,credentials:D}={}){let X=D??k!==\"*\";if(X&&k===\"*\")throw new Error('CORS: Cannot use credentials with wildcard origin \"*\". Specify an exact origin or set credentials to false.');if(this.headers[\"Access-Control-Allow-Origin\"]=k,this.headers[\"Access-Control-Allow-Methods\"]=z.join(\", \"),this.headers[\"Access-Control-Allow-Headers\"]=K.join(\", \"),X)this.headers[\"Access-Control-Allow-Credentials\"]=\"true\";return this.headers[\"Access-Control-Max-Age\"]=U.toString(),this}security(k){let z={hsts:!0,noSniff:!0,frameOptions:\"SAMEORIGIN\",xssProtection:!0,...k};if(z.csp)this.headers[\"Content-Security-Policy\"]=z.csp;if(z.hsts){let K=typeof z.hsts===\"number\"?z.hsts:31536000;this.headers[\"Strict-Transport-Security\"]=`max-age=${K}; includeSubDomains`}if(z.noSniff)this.headers[\"X-Content-Type-Options\"]=\"nosniff\";if(z.frameOptions)this.headers[\"X-Frame-Options\"]=z.frameOptions;if(z.xssProtection)this.headers[\"X-XSS-Protection\"]=\"1; mode=block\";return this}custom(k,z){return this.headers[k]=z,this}customHeaders(k){return Object.assign(this.headers,k),this}vary(...k){let z=this.headers.Vary,K=z?`${z}, ${k.join(\", \")}`:k.join(\", \");return this.headers.Vary=K,this}compress(k){if(k)this.headers[\"Content-Encoding\"]=k;return this}build(k){let z={...this.headers};if(k&&!z[\"Content-Length\"]){let K=typeof k===\"string\"?new TextEncoder().encode(k).length:k.byteLength;z[\"Content-Length\"]=K.toString()}return z}buildHeaders(k){return new Headers(this.build(k))}}function Q(){return new $}function F(k,z){let K=Q().contentType(k);if(z)K.cache(z);return K.build()}var G={css:(k)=>Q().contentType(\"css\").cache(\"STYLESHEET\").eTag(k),javascript:(k)=>Q().contentType(\"javascript\").cache(\"JAVASCRIPT\").eTag(k),hashedAsset:(k,z)=>Q().filePath(k).cache(\"HASHED_ASSET\").eTag(z),font:(k)=>Q().filePath(k).cache(\"FONT\").cors({origin:\"*\"}),image:(k)=>Q().filePath(k).cache(\"IMAGE\"),favicon:()=>Q().contentType(\"icon\").cache(\"FAVICON\"),api:()=>Q().contentType(\"json\").cache(\"API_RESPONSE\").cors(),expensiveApi:(k)=>Q().contentType(\"json\").cache(\"API_RESPONSE\").cors().eTag(k),realtime:()=>Q().contentType(\"json\").cache(\"NO_CACHE\").cors(),html:()=>Q().contentType(\"html\").cache(\"HTML_PAGE\").security(),expensiveHtml:(k)=>Q().contentType(\"html\").cache(\"HTML_PAGE\").security().eTag(k),manifest:(k)=>Q().contentType(\"json\").cache(\"MANIFEST\").eTag(k),serviceWorker:()=>Q().contentType(\"javascript\").cache(\"SERVICE_WORKER\"),sitemap:(k)=>Q().contentType(\"xml\").cache(\"SITEMAP\").eTag(k),feed:(k,z=\"rss\")=>Q().contentType(\"xml\").cache(\"FEED\").eTag(k).mimeType(z===\"rss\"?\"application/rss+xml\":\"application/atom+xml\"),video:(k)=>Q().filePath(k).cache(\"MEDIA_STREAM\").custom(\"Accept-Ranges\",\"bytes\"),audio:(k)=>Q().filePath(k).cache(\"MEDIA_STREAM\").custom(\"Accept-Ranges\",\"bytes\"),pdf:(k)=>Q().contentType(\"pdf\").cache(\"DOCUMENT\").custom(\"Content-Disposition\",\"inline\").eTag(k),download:(k,z,K)=>Q().filePath(z).cache(\"ARCHIVE\").custom(\"Content-Disposition\",`attachment; filename=\"${k}\"`).eTag(K),secure:()=>Q().security({csp:\"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'\",hsts:31536000}).custom(\"X-Powered-By\",\"Bun\"),permanentRedirect:(k)=>Q().redirect(k,!0),temporaryRedirect:(k)=>Q().redirect(k,!1)};export{F as quickHeaders,Z as hashContent,V as getMimeType,B as getCorsOrigin,G as commonHeaders,Q as buildHeaders};\n\n//# debugId=4EE16D0787D156E064756E2164756E21\n",
10
- "import type { BunFile } from \"bun\";\r\nimport { buildHeaders, getCorsOrigin } from \"@mateosuarezdev/headers-builder\"; // Adjust import path as needed\r\n\r\ninterface StreamMediaOptions {\r\n maxChunkSize?: number; // Default: 2MB\r\n cacheMaxAge?: number; // Default: 3600 (1 hour)\r\n acceptedExtensions?: string[]; // Optional whitelist\r\n}\r\n\r\n/**\r\n * Metadata for deferred media streaming\r\n * Used with router's streamFile procedure\r\n */\r\nexport interface StreamableMediaResponse {\r\n type: \"stream\";\r\n file: BunFile;\r\n request: Request;\r\n options?: StreamMediaOptions;\r\n}\r\n\r\n/**\r\n * Handle media streaming with automatic Range request support\r\n * Supports video, audio, and other streamable media formats\r\n * @internal - Used by router to process StreamableMediaResponse\r\n */\r\nexport async function streamMediaInternal(\r\n file: BunFile,\r\n request: Request,\r\n allowedOrigins: string[],\r\n options: StreamMediaOptions = {}\r\n): Promise<Response> {\r\n const {\r\n maxChunkSize = 2 * 1024 * 1024,\r\n cacheMaxAge = 3600,\r\n acceptedExtensions,\r\n } = options;\r\n\r\n // Check if file exists\r\n const fileExists = await file.exists();\r\n if (!fileExists) {\r\n return new Response(\"Media file not found\", {\r\n status: 404,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Validate file extension if acceptedExtensions provided\r\n if (acceptedExtensions && acceptedExtensions.length > 0) {\r\n const ext = file.name?.toLowerCase().split(\".\").pop();\r\n if (!ext || !acceptedExtensions.includes(ext)) {\r\n return new Response(\"File type not supported\", {\r\n status: 415, // Unsupported Media Type\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n }\r\n\r\n const fileSize = file.size;\r\n const range = request.headers.get(\"Range\");\r\n\r\n // No range request - send full file\r\n if (!range) {\r\n return new Response(file, {\r\n headers: buildHeaders()\r\n .filePath(file.name || \"media\")\r\n .custom(\"Accept-Ranges\", \"bytes\")\r\n .custom(\"Cache-Control\", `public, max-age=${cacheMaxAge}`)\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Parse range header\r\n const rangeMatch = range.match(/bytes=(\\d*)-(\\d*)/);\r\n if (!rangeMatch) {\r\n return new Response(\"Invalid Range header\", {\r\n status: 416,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .custom(\"Content-Range\", `bytes */${fileSize}`)\r\n .build(),\r\n });\r\n }\r\n\r\n // Calculate start and end positions\r\n const start = rangeMatch[1] ? parseInt(rangeMatch[1]) : 0;\r\n const requestedEnd = rangeMatch[2] ? parseInt(rangeMatch[2]) : fileSize - 1;\r\n\r\n // Apply maxChunkSize limit\r\n const end = Math.min(requestedEnd, start + maxChunkSize - 1, fileSize - 1);\r\n\r\n // Validate range\r\n if (start >= fileSize || end >= fileSize || start > end) {\r\n return new Response(\"Range not satisfiable\", {\r\n status: 416,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .custom(\"Content-Range\", `bytes */${fileSize}`)\r\n .build(),\r\n });\r\n }\r\n\r\n // Bun automatically sets Content-Range and Content-Length for slices\r\n return new Response(file.slice(start, end + 1), {\r\n status: 206,\r\n headers: buildHeaders()\r\n .filePath(file.name || \"media\")\r\n .custom(\"Accept-Ranges\", \"bytes\")\r\n .custom(\"Cache-Control\", `public, max-age=${cacheMaxAge}`)\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n}\r\n\r\n/**\r\n * Helper function to create a streamable media response\r\n * Returns metadata that the router will process with streamMediaInternal()\r\n *\r\n * @example\r\n * ```typescript\r\n * // Stream any media\r\n * return stream(file, ctx.request);\r\n *\r\n * // Stream with validation\r\n * return stream(file, ctx.request, {\r\n * acceptedExtensions: [\"mp4\", \"webm\", \"mov\"],\r\n * maxChunkSize: 5 * 1024 * 1024,\r\n * });\r\n * ```\r\n */\r\nexport function streamMedia(\r\n file: BunFile,\r\n request: Request,\r\n options?: StreamMediaOptions\r\n): StreamableMediaResponse {\r\n return {\r\n type: \"stream\",\r\n file,\r\n request,\r\n options,\r\n };\r\n}\r\n\r\n//? I don't need to bundle them, users can create their owns\r\n// /**\r\n// * Common media extension presets\r\n// */\r\n// export const MEDIA_EXTENSIONS = {\r\n// video: [\"mp4\", \"webm\", \"mov\", \"avi\", \"mkv\", \"ogg\", \"ogv\"],\r\n// audio: [\"mp3\", \"wav\", \"ogg\", \"m4a\", \"flac\", \"aac\", \"opus\"],\r\n// all: [\r\n// \"mp4\",\r\n// \"webm\",\r\n// \"mov\",\r\n// \"avi\",\r\n// \"mkv\",\r\n// \"ogg\",\r\n// \"ogv\",\r\n// \"mp3\",\r\n// \"wav\",\r\n// \"m4a\",\r\n// \"flac\",\r\n// \"aac\",\r\n// \"opus\",\r\n// ],\r\n// } as const;\r\n",
11
- "import { type BaseContext } from \"./types\";\r\n\r\nexport function createContext<C extends BaseContext>(\r\n contextCreator: (req: Request) => Promise<C>\r\n) {\r\n return contextCreator;\r\n}\r\n",
12
- "import { z } from \"zod\";\r\n\r\n// export const fileSchema = z.instanceof(File);\r\n\r\n// export const blobSchema = z.object({\r\n// name: z.string(),\r\n// type: z.string(),\r\n// size: z.number(),\r\n// lastModified: z.number(),\r\n// blob: z.instanceof(Blob),\r\n// });\r\n\r\n// Common MIME type groups\r\nconst MIME_TYPES = {\r\n image: [\r\n \"image/jpeg\",\r\n \"image/png\",\r\n \"image/gif\",\r\n \"image/webp\",\r\n \"image/svg+xml\",\r\n ],\r\n video: [\"video/mp4\", \"video/webm\", \"video/ogg\"],\r\n audio: [\"audio/mpeg\", \"audio/ogg\", \"audio/wav\"],\r\n document: [\r\n \"application/pdf\",\r\n \"application/msword\",\r\n \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\r\n \"application/vnd.ms-excel\",\r\n \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\r\n ],\r\n} as const;\r\n\r\ntype MimeTypeGroup = keyof typeof MIME_TYPES;\r\ntype MimeTypeValues<T extends MimeTypeGroup> = (typeof MIME_TYPES)[T][number];\r\n\r\n// Type for accepting either specific types or wildcard per group\r\ntype MimeTypeConfig = {\r\n [K in MimeTypeGroup]?: \"*\" | MimeTypeValues<K>[];\r\n};\r\n\r\n/** Options for creating a file validation schema */\r\ntype FileSchemaOptions = {\r\n /** Accepted MIME types configuration. Use \"*\" for all types in a category or specify an array of types\r\n * @example\r\n * {\r\n * image: \"*\", // All image types\r\n * audio: [\"audio/mpeg\"], // Only MP3s\r\n * document: [\"application/pdf\", \"application/msword\"] // PDFs and DOCs\r\n * }\r\n */\r\n acceptedTypes?: MimeTypeConfig;\r\n /** Maximum file size in megabytes */\r\n maxSize?: number;\r\n /** Minimum file size in megabytes */\r\n minSize?: number;\r\n /** Whether the file field is required */\r\n // required?: boolean;\r\n /** Custom error messages */\r\n messages?: {\r\n /** Custom message for type validation failure */\r\n type?: string;\r\n /** Custom message for maximum size validation failure */\r\n maxSize?: string;\r\n /** Custom message for minimum size validation failure */\r\n minSize?: string;\r\n /** Custom message for required field validation failure */\r\n required?: string;\r\n };\r\n};\r\n\r\nconst convertToBytes = (\r\n size: number,\r\n unit: \"MB\" | \"KB\" | \"B\" = \"MB\"\r\n): number => {\r\n switch (unit) {\r\n case \"MB\":\r\n return size * 1024 * 1024;\r\n case \"KB\":\r\n return size * 1024;\r\n case \"B\":\r\n return size;\r\n }\r\n};\r\n\r\n/**\r\n * Creates a Zod schema for file validation with support for MIME types and size constraints\r\n * you can extend it as any other zod schema after calling it\r\n * @param {Object} options - The configuration options for the file schema\r\n * @returns Zod schema for file validation\r\n */\r\nexport const createFileSchema = ({\r\n acceptedTypes,\r\n maxSize = Infinity,\r\n minSize,\r\n // required = true,\r\n messages = {},\r\n}: FileSchemaOptions = {}) => {\r\n // Convert MB to bytes\r\n const maxBytes = maxSize ? convertToBytes(maxSize) : Infinity;\r\n const minBytes = minSize ? convertToBytes(minSize) : 0;\r\n\r\n // Process accepted types\r\n const validMimeTypes = acceptedTypes\r\n ? Object.entries(acceptedTypes).flatMap(([category, types]) => {\r\n const cat = category as MimeTypeGroup;\r\n\r\n // Handle wildcard\r\n if (types === \"*\") {\r\n return MIME_TYPES[cat];\r\n }\r\n\r\n // Handle array of specific types\r\n if (Array.isArray(types)) {\r\n return types;\r\n }\r\n\r\n return [];\r\n })\r\n : undefined;\r\n\r\n // Base schema\r\n let schema = z.instanceof(File, {\r\n message: messages.required ?? \"File is required\",\r\n });\r\n\r\n // Add size refinement\r\n schema = schema\r\n .refine(\r\n (file) => file.size <= maxBytes,\r\n messages.maxSize ?? `File size must be less than ${maxSize}MB`\r\n )\r\n .refine(\r\n (file) => file.size >= minBytes,\r\n messages.minSize ?? `File size must be at least ${minSize}MB`\r\n );\r\n\r\n // Add type refinement if acceptedTypes is provided\r\n if (validMimeTypes?.length) {\r\n schema = schema.refine(\r\n (file) =>\r\n validMimeTypes.includes(file.type as MimeTypeValues<MimeTypeGroup>),\r\n messages.type ?? `File must be of type: ${validMimeTypes.join(\", \")}`\r\n );\r\n }\r\n\r\n return schema;\r\n};\r\n",
10
+ "import type { BunFile } from \"bun\";\r\nimport { buildHeaders, getCorsOrigin } from \"@mateosuarezdev/headers-builder\"; // Adjust import path as needed\r\n\r\ninterface StreamMediaOptions {\r\n maxChunkSize?: number; // Default: 2MB\r\n cacheMaxAge?: number; // Default: 3600 (1 hour)\r\n acceptedExtensions?: string[]; // Optional whitelist\r\n}\r\n\r\n/**\r\n * Metadata for deferred media streaming\r\n * Used with router's streamFile procedure\r\n */\r\nexport interface StreamableMediaResponse {\r\n type: \"stream\";\r\n file: BunFile;\r\n request: Request;\r\n options?: StreamMediaOptions;\r\n}\r\n\r\n/**\r\n * Handle media streaming with automatic Range request support\r\n * Supports video, audio, and other streamable media formats\r\n * @internal - Used by router to process StreamableMediaResponse\r\n */\r\nexport async function streamMediaInternal(\r\n file: BunFile,\r\n request: Request,\r\n allowedOrigins: string[],\r\n options: StreamMediaOptions = {}\r\n): Promise<Response> {\r\n const {\r\n maxChunkSize = 2 * 1024 * 1024,\r\n cacheMaxAge = 3600,\r\n acceptedExtensions,\r\n } = options;\r\n\r\n // Check if file exists\r\n const fileExists = await file.exists();\r\n if (!fileExists) {\r\n return new Response(\"Media file not found\", {\r\n status: 404,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Validate file extension if acceptedExtensions provided\r\n if (acceptedExtensions && acceptedExtensions.length > 0) {\r\n const ext = file.name?.toLowerCase().split(\".\").pop();\r\n if (!ext || !acceptedExtensions.includes(ext)) {\r\n return new Response(\"File type not supported\", {\r\n status: 415, // Unsupported Media Type\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n }\r\n\r\n const fileSize = file.size;\r\n const range = request.headers.get(\"Range\");\r\n\r\n // No range request - send full file\r\n if (!range) {\r\n return new Response(file, {\r\n headers: buildHeaders()\r\n .filePath(file.name || \"media\")\r\n .custom(\"Accept-Ranges\", \"bytes\")\r\n .custom(\"Cache-Control\", `public, max-age=${cacheMaxAge}`)\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Parse range header\r\n const rangeMatch = range.match(/bytes=(\\d*)-(\\d*)/);\r\n if (!rangeMatch) {\r\n return new Response(\"Invalid Range header\", {\r\n status: 416,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .custom(\"Content-Range\", `bytes */${fileSize}`)\r\n .build(),\r\n });\r\n }\r\n\r\n // Calculate start and end positions\r\n const start = rangeMatch[1] ? parseInt(rangeMatch[1]) : 0;\r\n const requestedEnd = rangeMatch[2] ? parseInt(rangeMatch[2]) : fileSize - 1;\r\n\r\n // Apply maxChunkSize limit\r\n const end = Math.min(requestedEnd, start + maxChunkSize - 1, fileSize - 1);\r\n\r\n // Validate range\r\n if (start >= fileSize || end >= fileSize || start > end) {\r\n return new Response(\"Range not satisfiable\", {\r\n status: 416,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .custom(\"Content-Range\", `bytes */${fileSize}`)\r\n .build(),\r\n });\r\n }\r\n\r\n // Bun automatically sets Content-Range and Content-Length for slices\r\n return new Response(file.slice(start, end + 1), {\r\n status: 206,\r\n headers: buildHeaders()\r\n .filePath(file.name || \"media\")\r\n .custom(\"Accept-Ranges\", \"bytes\")\r\n .custom(\"Cache-Control\", `public, max-age=${cacheMaxAge}`)\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n}\r\n\r\n/**\r\n * Helper function to create a streamable media response\r\n * Returns metadata that the router will process with streamMediaInternal()\r\n *\r\n * @example\r\n * ```typescript\r\n * // Stream any media\r\n * return streamMedia(file, ctx.request);\r\n *\r\n * // Stream with validation\r\n * return streamMedia(file, ctx.request, {\r\n * acceptedExtensions: [\"mp4\", \"webm\", \"mov\"],\r\n * maxChunkSize: 5 * 1024 * 1024,\r\n * });\r\n * ```\r\n */\r\nexport function streamMedia(\r\n file: BunFile,\r\n request: Request,\r\n options?: StreamMediaOptions\r\n): StreamableMediaResponse {\r\n return {\r\n type: \"stream\",\r\n file,\r\n request,\r\n options,\r\n };\r\n}\r\n\r\n//? I don't need to bundle them, users can create their owns\r\n// but just in case or if I want to add to docs\r\n// /**\r\n// * Common media extension presets\r\n// */\r\n// export const MEDIA_EXTENSIONS = {\r\n// video: [\"mp4\", \"webm\", \"mov\", \"avi\", \"mkv\", \"ogg\", \"ogv\"],\r\n// audio: [\"mp3\", \"wav\", \"ogg\", \"m4a\", \"flac\", \"aac\", \"opus\"],\r\n// all: [\r\n// \"mp4\",\r\n// \"webm\",\r\n// \"mov\",\r\n// \"avi\",\r\n// \"mkv\",\r\n// \"ogg\",\r\n// \"ogv\",\r\n// \"mp3\",\r\n// \"wav\",\r\n// \"m4a\",\r\n// \"flac\",\r\n// \"aac\",\r\n// \"opus\",\r\n// ],\r\n// } as const;\r\n",
11
+ "import type { BaseContext } from \"./types\";\r\n\r\nexport function createContext<C extends BaseContext>(\r\n contextCreator: (req: Request) => Promise<C>\r\n) {\r\n return contextCreator;\r\n}\r\n",
12
+ "import { z } from \"zod\";\r\n\r\n// Common MIME type groups\r\nconst MIME_TYPES = {\r\n image: [\r\n \"image/jpeg\",\r\n \"image/png\",\r\n \"image/gif\",\r\n \"image/webp\",\r\n \"image/svg+xml\",\r\n ],\r\n video: [\"video/mp4\", \"video/webm\", \"video/ogg\"],\r\n audio: [\"audio/mpeg\", \"audio/ogg\", \"audio/wav\"],\r\n document: [\r\n \"application/pdf\",\r\n \"application/msword\",\r\n \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\r\n \"application/vnd.ms-excel\",\r\n \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\r\n ],\r\n} as const;\r\n\r\ntype MimeTypeGroup = keyof typeof MIME_TYPES;\r\ntype MimeTypeValues<T extends MimeTypeGroup> = (typeof MIME_TYPES)[T][number];\r\n\r\n// Type for accepting either specific types or wildcard per group\r\ntype MimeTypeConfig = {\r\n [K in MimeTypeGroup]?: \"*\" | MimeTypeValues<K>[];\r\n};\r\n\r\n/** Options for creating a file validation schema */\r\ntype FileSchemaOptions = {\r\n /** Accepted MIME types configuration. Use \"*\" for all types in a category or specify an array of types\r\n * @example\r\n * {\r\n * image: \"*\", // All image types\r\n * audio: [\"audio/mpeg\"], // Only MP3s\r\n * document: [\"application/pdf\", \"application/msword\"] // PDFs and DOCs\r\n * }\r\n */\r\n acceptedTypes?: MimeTypeConfig;\r\n /** Maximum file size in megabytes */\r\n maxSize?: number;\r\n /** Minimum file size in megabytes */\r\n minSize?: number;\r\n /** Whether the file field is required */\r\n // required?: boolean;\r\n /** Custom error messages */\r\n messages?: {\r\n /** Custom message for type validation failure */\r\n type?: string;\r\n /** Custom message for maximum size validation failure */\r\n maxSize?: string;\r\n /** Custom message for minimum size validation failure */\r\n minSize?: string;\r\n /** Custom message for required field validation failure */\r\n required?: string;\r\n };\r\n};\r\n\r\nconst convertToBytes = (\r\n size: number,\r\n unit: \"MB\" | \"KB\" | \"B\" = \"MB\"\r\n): number => {\r\n switch (unit) {\r\n case \"MB\":\r\n return size * 1024 * 1024;\r\n case \"KB\":\r\n return size * 1024;\r\n case \"B\":\r\n return size;\r\n }\r\n};\r\n\r\n/**\r\n * Creates a Zod schema for file validation with support for MIME types and size constraints\r\n * you can extend it as any other zod schema after calling it\r\n * @param {Object} options - The configuration options for the file schema\r\n * @returns Zod schema for file validation\r\n */\r\nexport const createFileSchema = ({\r\n acceptedTypes,\r\n maxSize = Infinity,\r\n minSize,\r\n // required = true,\r\n messages = {},\r\n}: FileSchemaOptions = {}) => {\r\n // Convert MB to bytes\r\n const maxBytes = maxSize ? convertToBytes(maxSize) : Infinity;\r\n const minBytes = minSize ? convertToBytes(minSize) : 0;\r\n\r\n // Process accepted types\r\n const validMimeTypes = acceptedTypes\r\n ? Object.entries(acceptedTypes).flatMap(([category, types]) => {\r\n const cat = category as MimeTypeGroup;\r\n\r\n // Handle wildcard\r\n if (types === \"*\") {\r\n return MIME_TYPES[cat];\r\n }\r\n\r\n // Handle array of specific types\r\n if (Array.isArray(types)) {\r\n return types;\r\n }\r\n\r\n return [];\r\n })\r\n : undefined;\r\n\r\n // Base schema\r\n let schema = z.instanceof(File, {\r\n message: messages.required ?? \"File is required\",\r\n });\r\n\r\n // Add size refinement\r\n schema = schema\r\n .refine(\r\n (file) => file.size <= maxBytes,\r\n messages.maxSize ?? `File size must be less than ${maxSize}MB`\r\n )\r\n .refine(\r\n (file) => file.size >= minBytes,\r\n messages.minSize ?? `File size must be at least ${minSize}MB`\r\n );\r\n\r\n // Add type refinement if acceptedTypes is provided\r\n if (validMimeTypes?.length) {\r\n schema = schema.refine(\r\n (file) =>\r\n validMimeTypes.includes(file.type as MimeTypeValues<MimeTypeGroup>),\r\n messages.type ?? `File must be of type: ${validMimeTypes.join(\", \")}`\r\n );\r\n }\r\n\r\n return schema;\r\n};\r\n",
13
13
  "import type { BaseContext } from \"../..\";\r\n\r\n// blockPaths.ts\r\nexport type PathBlockerConfig = {\r\n paths: string[]; // Array of paths/patterns to block\r\n};\r\n\r\nexport const createPathBlocker = (config: PathBlockerConfig) => {\r\n const patterns = config.paths.map((path) => new RegExp(path));\r\n const message = \"Not Found\";\r\n\r\n return async <C extends BaseContext>(ctx: C): Promise<void> => {\r\n const url = new URL(ctx.req.url);\r\n const path = url.pathname;\r\n\r\n // Check if the path matches any blocked pattern\r\n if (patterns.some((pattern) => pattern.test(path))) {\r\n throw new Error(message);\r\n }\r\n };\r\n};\r\n",
14
14
  "import type { BaseContext } from \"../../types\";\r\n\r\nexport interface RateLimitConfig {\r\n windowMs: number;\r\n maxRequests: number;\r\n maxEntries?: number;\r\n cleanupIntervalMs?: number;\r\n message?: string;\r\n statusCode?: number;\r\n headerPrefix?: string;\r\n}\r\n\r\ninterface RequestWindow {\r\n count: number;\r\n startTime: number;\r\n lastAccessed: number;\r\n}\r\n\r\ninterface RateLimitResult {\r\n isLimited: boolean;\r\n remaining: number;\r\n resetTime: number;\r\n}\r\n\r\nclass RateLimiter {\r\n private requests: Map<string, RequestWindow> = new Map();\r\n private lastCleanupTime: number = Date.now();\r\n private readonly alertThreshold: number;\r\n private config: Required<RateLimitConfig>;\r\n\r\n constructor(config: RateLimitConfig) {\r\n this.config = {\r\n windowMs: config.windowMs,\r\n maxRequests: config.maxRequests,\r\n maxEntries: config.maxEntries ?? 10000,\r\n cleanupIntervalMs: config.cleanupIntervalMs ?? 60000,\r\n message: config.message ?? \"Too Many Requests\",\r\n statusCode: config.statusCode ?? 429,\r\n headerPrefix: config.headerPrefix ?? \"X-RateLimit\",\r\n };\r\n this.alertThreshold = this.config.maxEntries * 0.8;\r\n\r\n // Start periodic cleanup\r\n if (process.env.NODE_ENV !== \"development\") {\r\n setInterval(() => this.cleanup(), this.config.cleanupIntervalMs);\r\n }\r\n }\r\n\r\n private cleanup(): number {\r\n const now = Date.now();\r\n let deletedCount = 0;\r\n\r\n for (const [ip, window] of this.requests.entries()) {\r\n const windowExpired = now - window.startTime > this.config.windowMs;\r\n const inactive = now - window.lastAccessed > this.config.windowMs * 2;\r\n\r\n if (windowExpired || inactive) {\r\n this.requests.delete(ip);\r\n deletedCount++;\r\n }\r\n }\r\n\r\n this.lastCleanupTime = now;\r\n return deletedCount;\r\n }\r\n\r\n private handleMaxEntries(ip: string): boolean {\r\n const currentSize = this.requests.size;\r\n\r\n // Regular cleanup if interval passed\r\n if (Date.now() - this.lastCleanupTime > this.config.cleanupIntervalMs) {\r\n this.cleanup();\r\n }\r\n\r\n // Alert if nearing capacity\r\n if (currentSize >= this.alertThreshold) {\r\n console.warn(\r\n `Rate limiter at ${Math.round(\r\n (currentSize / this.config.maxEntries) * 100\r\n )}% capacity`\r\n );\r\n }\r\n\r\n // If IP is already tracked, allow it\r\n if (this.requests.has(ip)) {\r\n return true;\r\n }\r\n\r\n // Reject if at max capacity\r\n return currentSize < this.config.maxEntries;\r\n }\r\n\r\n check(ip: string): RateLimitResult {\r\n // Check system capacity first\r\n if (!this.handleMaxEntries(ip)) {\r\n return {\r\n isLimited: true,\r\n remaining: 0,\r\n resetTime: Date.now() + this.config.windowMs,\r\n };\r\n }\r\n\r\n const now = Date.now();\r\n const window = this.requests.get(ip);\r\n\r\n // Create new window if none exists or current one expired\r\n if (!window || now - window.startTime >= this.config.windowMs) {\r\n this.requests.set(ip, {\r\n count: 1,\r\n startTime: now,\r\n lastAccessed: now,\r\n });\r\n\r\n return {\r\n isLimited: false,\r\n remaining: this.config.maxRequests - 1,\r\n resetTime: now + this.config.windowMs,\r\n };\r\n }\r\n\r\n // Update existing window\r\n window.lastAccessed = now;\r\n const isLimited = window.count >= this.config.maxRequests;\r\n\r\n if (!isLimited) {\r\n window.count++;\r\n }\r\n\r\n return {\r\n isLimited,\r\n remaining: Math.max(0, this.config.maxRequests - window.count),\r\n resetTime: window.startTime + this.config.windowMs,\r\n };\r\n }\r\n\r\n getConfig() {\r\n return this.config;\r\n }\r\n}\r\n\r\n// Middleware factory\r\nexport const createRateLimiter = (config: RateLimitConfig) => {\r\n const limiter = new RateLimiter(config);\r\n\r\n return async <C extends BaseContext>(ctx: C): Promise<void> => {\r\n // Skip rate limiting for video range requests\r\n if (ctx.req.headers.get(\"range\")) {\r\n return; // Don't apply rate limiting to video chunks\r\n }\r\n\r\n const ip =\r\n ctx.req.headers.get(\"x-forwarded-for\")?.split(\",\")[0] || // Get first IP if multiple\r\n ctx.req.headers.get(\"x-real-ip\") ||\r\n \"unknown\";\r\n\r\n const { isLimited, remaining, resetTime } = limiter.check(ip);\r\n\r\n const { headerPrefix } = limiter.getConfig();\r\n ctx.setHeader(`${headerPrefix}-Remaining`, remaining.toString());\r\n ctx.setHeader(`${headerPrefix}-Reset`, resetTime.toString());\r\n\r\n if (isLimited) {\r\n ctx.setHeader(`${headerPrefix}-Exceeded`, \"true\");\r\n throw new Error(limiter.getConfig().message);\r\n }\r\n };\r\n};\r\n\r\n//example usage with more options\r\n// const rateLimiter = createRateLimiter({\r\n// windowMs: 60 * 1000, // 1 minute\r\n// maxRequests: 100, // 100 requests per minute\r\n// maxEntries: 10000, // Maximum number of IPs to track\r\n// cleanupIntervalMs: 30000, // Cleanup every 30 seconds\r\n// message: 'Rate limit exceeded. Please try again later.',\r\n// headerPrefix: 'X-RateLimit'\r\n// });\r\n\r\n// const router = createRouter({\r\n// context: createContext,\r\n// routes: appRoutes,\r\n// globalMiddlewares: [rateLimiter],\r\n// // ... other config\r\n// });\r\n"
15
15
  ],
16
- "mappings": ";AAAA,YAAS,YAUT,MAAM,CAAwC,CACpC,YAA6C,CAAC,EAEtD,WAAW,CAAC,EAA6C,CAAC,EAAG,CAC3D,KAAK,YAAc,EAGrB,GAAG,CAAC,EAAuC,CACzC,OAAO,IAAI,EAAoB,CAAC,GAAG,KAAK,YAAa,CAAU,CAAC,EAGlE,KAA0B,CAAC,EAAW,CACpC,OAAO,IAAI,EAA4B,KAAK,YAAa,CAAM,EAGjE,KAAQ,CACN,EAC2C,CAC3C,MAAO,CACL,OAAQ,EAAE,OAAO,CAAC,CAAC,EACnB,QAAS,KACT,KAAM,KACN,MAAO,QACP,UACA,YAAa,KAAK,WACpB,EAGF,QAAW,CACT,EAC8C,CAC9C,MAAO,CACL,OAAQ,EAAE,OAAO,CAAC,CAAC,EACnB,QAAS,KACT,KAAM,KACN,MAAO,WACP,UACA,YAAa,KAAK,WACpB,EAGF,YAAe,CACb,EACkD,CAClD,MAAO,CACL,OAAQ,EAAE,OAAO,CAAC,CAAC,EACnB,QAAS,KACT,KAAM,KACN,MAAO,eACP,UACA,YAAa,KAAK,WACpB,EAQF,YAAe,CACb,EACkD,CAClD,MAAO,CACL,OAAQ,EAAE,OAAO,CAAC,CAAC,EACnB,QAAS,KACT,KAAM,KACN,MAAO,eACP,UACA,YAAa,KAAK,WACpB,EAGF,IAAI,CACF,EAC2D,CAC3D,M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B,EAIF,EAAO,aAAe,EACtB,IAAM,EAAY,EAAO,OAAS,KAAK,OAAO,YAE9C,GAAI,CAAC,EACH,EAAO,QAGT,MAAO,CACL,YACA,UAAW,KAAK,IAAI,EAAG,KAAK,OAAO,YAAc,EAAO,KAAK,EAC7D,UAAW,EAAO,UAAY,KAAK,OAAO,QAC5C,EAGF,SAAS,EAAG,CACV,OAAO,KAAK,OAEhB,CAGO,IAAM,GAAoB,CAAC,IAA4B,CAC5D,IAAM,EAAU,IAAI,EAAY,CAAM,EAEtC,MAAO,OAA8B,IAA0B,CAE7D,GAAI,EAAI,IAAI,QAAQ,IAAI,OAAO,EAC7B,OAGF,IAAM,EACJ,EAAI,IAAI,QAAQ,IAAI,iBAAiB,GAAG,MAAM,GAAG,EAAE,IACnD,EAAI,IAAI,QAAQ,IAAI,WAAW,GAC/B,WAEM,YAAW,YAAW,aAAc,EAAQ,MAAM,CAAE,GAEpD,gBAAiB,EAAQ,UAAU,EAI3C,GAHA,EAAI,UAAU,GAAG,cAA0B,EAAU,SAAS,CAAC,EAC/D,EAAI,UAAU,GAAG,UAAsB,EAAU,SAAS,CAAC,EAEvD,EAEF,MADA,EAAI,UAAU,GAAG,aAAyB,MAAM,EACtC,MAAM,EAAQ,UAAU,EAAE,OAAO",
17
+ "debugId": "6214C7542F1273F264756E2164756E21",
18
18
  "names": []
19
19
  }
@@ -0,0 +1,15 @@
1
+ import type { GetOptimizedImageOptions } from "./types";
2
+ /**
3
+ * Returns an existing optimized image from cache or
4
+ * creates one on the fly
5
+ * @example
6
+ * ```ts
7
+ * const optimizedImage = await getImage({
8
+ * key: "originalFileKey",
9
+ * width: 800
10
+ * })
11
+ * if (!optimizedImage) throw BRPCError.notFound()
12
+ * return optimizedImage
13
+ * ```
14
+ */
15
+ export declare function getOptimizedImage(options: GetOptimizedImageOptions): Promise<Bun.BunFile | null>;
@@ -0,0 +1,3 @@
1
+ export * from "./optimize";
2
+ export * from "./get-optimized";
3
+ export * from "./types";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { ResolvedFileType } from "../types";
2
+ export type OptimizedImageBuffer = {
3
+ buffer: Buffer;
4
+ metadata: {
5
+ name: string;
6
+ type: string;
7
+ extension: string;
8
+ resolvedType: ResolvedFileType;
9
+ };
10
+ };
11
+ export type GetOptimizedImageOptions = {
12
+ key: string;
13
+ width: number;
14
+ quality?: number;
15
+ provider?: "local" | "s3" | "external";
16
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./utils";
2
+ export * from "./images";
3
+ export * from "./s3";
4
+ export * from "./types";
@@ -0,0 +1,4 @@
1
+ // @bun
2
+ var D=(_)=>_.startsWith("image/"),K=(_)=>_.startsWith("video/"),M=(_)=>_.startsWith("audio/"),q=(_)=>_.startsWith("text/")||_.includes("pdf")||_.includes("word")||_.includes("sheet")||_.includes("presentation")||_.includes("opendocument")||_==="application/rtf",z=(_)=>_.includes("zip")||_.includes("rar")||_.includes("tar")||_.includes("7z")||_.includes("gzip"),A=(_)=>_==="application/javascript"||_==="application/json"||_==="text/html"||_==="text/css"||_==="application/xml"||_.includes("javascript"),G=(_)=>_.startsWith("font/")||_.includes("font"),S=(_)=>{if(!_)return"other";let L=_.toLowerCase();if(D(L))return"image";if(K(L))return"video";if(M(L))return"audio";if(q(L))return"document";if(z(L))return"archive";if(A(L))return"code";if(G(L))return"font";return"other"},$={isImage:D,isVideo:K,isAudio:M,isDocument:q,isArchive:z,isCode:A,isFont:G};function h(){let L=Buffer.from("Hello, this is a test file for S3 upload!","utf-8");return new File([L],"test.txt",{type:"text/plain"})}class x extends Error{code;clientCode;httpStatus;data;cause;static STATUS_MAP={BAD_REQUEST:400,UNAUTHORIZED:401,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_SUPPORTED:405,TIMEOUT:408,CONFLICT:409,PRECONDITION_FAILED:412,PAYLOAD_TOO_LARGE:413,UNPROCESSABLE_CONTENT:422,TOO_MANY_REQUESTS:429,CLIENT_CLOSED_REQUEST:499,INTERNAL_SERVER_ERROR:500,NOT_IMPLEMENTED:501,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504};constructor(_){super(_.message);if(this.name="BRPCError",this.code=_.code,this.clientCode=_.clientCode,this.httpStatus=x.STATUS_MAP[_.code],this.data=_.data,this.cause=_.cause,Error.captureStackTrace)Error.captureStackTrace(this,x)}toJSON(){return{name:this.name,code:this.code,clientCode:this.clientCode,message:this.message,data:this.data,httpStatus:this.httpStatus}}static badRequest(_,L,W){return new x({code:"BAD_REQUEST",message:_,clientCode:L,data:W})}static unauthorized(_="Unauthorized",L,W){return new x({code:"UNAUTHORIZED",message:_,clientCode:L,data:W})}static forbidden(_="Forbidden",L,W){return new x({code:"FORBIDDEN",message:_,clientCode:L,data:W})}static notFound(_="Not Found",L,W){return new x({code:"NOT_FOUND",message:_,clientCode:L,data:W})}static preconditionFailed(_="Precondition failed",L,W){return new x({code:"NOT_FOUND",message:_,clientCode:L,data:W})}static conflict(_,L,W){return new x({code:"CONFLICT",message:_,clientCode:L,data:W})}static unprocessableContent(_,L,W){return new x({code:"UNPROCESSABLE_CONTENT",message:_,clientCode:L,data:W})}static tooManyRequests(_="Too many requests",L,W){return new x({code:"TOO_MANY_REQUESTS",message:_,clientCode:L,data:W})}static internalServerError(_="Internal Server Error",L,W){return new x({code:"INTERNAL_SERVER_ERROR",message:_,clientCode:L,data:W})}static timeout(_="Request timeout",L,W){return new x({code:"TIMEOUT",message:_,clientCode:L,data:W})}}import T from"sharp";import{randomUUID as v}from"crypto";import{extname as I}from"path";async function N(_,L,W){if(!L)return{data:null,error:Error("File not found")};let H=_.replace(/^\/+|\/+$/g,""),Q=I(L.name),J=`${v()}${Q}`,X=`${H}/${J}`,Z=`./buckets/${H}/${J}`;try{let Y=await Bun.write(Z,L);return{data:{key:X,file:L,bytesWritten:Y},error:null}}catch(Y){return{data:null,error:Error(`Failed to upload ${L.name} to local filesystem`)}}}async function k(_,L,W){if(!L||L.length===0)return[];let H=L.map((J)=>{let X=W?.(J)??J.name;return N(_,J,X)});return await Promise.all(H)}async function b(_){let L=Bun.file(`./buckets/${_}`);if(!await L.exists())return null;return L}async function F(_){let L=Bun.file(`./buckets/${_}`);if(!await L.exists())return!1;return await L.delete(),!0}var j={uploadOne:N,uploadMany:k,getOne:b,deleteOne:F};async function n(_){let L=`./cache/images/${_.key}_${_.width}_${_.quality}.webp`,W=await j.getOne(L);if(W){if(!$.isImage(W.type))return null;return W}let H=_.key,Q=await j.getOne(H);if(!Q)return null;if(!$.isImage(Q.type))return null;let J=await Q.arrayBuffer(),X=await T(J).resize(_.width,null,{withoutEnlargement:!0}).webp({quality:_.quality??75}).toBuffer();await Bun.write(L,X);let Z=Bun.file(L);if(!await Z.exists())return null;return Z}import E from"path";var V=process.env.AWS_FOLDER,O=V?`${V}/`:"";var{s3:R}=globalThis.Bun;async function t(_,L,W){let H="buffer"in L?{buffer:L.buffer,metadata:L.metadata}:{buffer:L,metadata:{name:L.name,type:L.type,extension:E.extname(L.name),resolvedType:S(L.type)}};if("buffer"in L&&!L.metadata)throw x.badRequest("Metadata required when uploading buffer");let Q=_.length>1?`${_.replace(/^\/+|\/+$/g,"")}/`:"",J=`${crypto.randomUUID()}${H.metadata.extension}`,X=`${O}${Q}${J}`,Z=R.file(X),Y=W?.acl??"public-read",w=Y!=="public-read"&&Y!=="public-read-write",U=await Z.write(H.buffer,{acl:Y});return{uploadedBy:W?.uploadedBy??null,key:X,url:null,name:H.metadata.name,thumbnail:null,resolvedType:H.metadata.resolvedType,provider:"s3",isPrivate:w,metadata:{size:U,type:H.metadata.type,extension:H.metadata.extension,acl:Y},isActive:!0}}var{s3:B}=globalThis.Bun;async function L_(_,L={debug:!0}){try{return{exists:await B.exists(_),error:null}}catch(W){if(L?.debug)console.error("There was an error checking for file existance",W);return{exists:null,error:W}}}export{t as uploadOne,S as mimeTypeToResolvedType,n as getOptimizedImage,h as generateTestFile,$ as fileTypePredicates,L_ as checkFileExistance};
3
+
4
+ //# debugId=DE5A1B3485300E8464756E2164756E21
@@ -0,0 +1,16 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["..\\src\\storage\\utils\\files.ts", "..\\src\\errors\\BRPCError.ts", "..\\src\\storage\\images\\get-optimized.ts", "..\\src\\storage\\local\\index.ts", "..\\src\\storage\\s3\\upload.ts", "..\\src\\storage\\s3\\constants.ts", "..\\src\\storage\\s3\\helpers.ts"],
4
+ "sourcesContent": [
5
+ "import type { ResolvedFileType } from \"../types\";\r\n\r\nconst isImage = (type: string): boolean => type.startsWith(\"image/\");\r\n\r\nconst isVideo = (type: string): boolean => type.startsWith(\"video/\");\r\n\r\nconst isAudio = (type: string): boolean => type.startsWith(\"audio/\");\r\n\r\nconst isDocument = (type: string): boolean =>\r\n type.startsWith(\"text/\") ||\r\n type.includes(\"pdf\") ||\r\n type.includes(\"word\") ||\r\n type.includes(\"sheet\") ||\r\n type.includes(\"presentation\") ||\r\n type.includes(\"opendocument\") ||\r\n type === \"application/rtf\";\r\n\r\nconst isArchive = (type: string): boolean =>\r\n type.includes(\"zip\") ||\r\n type.includes(\"rar\") ||\r\n type.includes(\"tar\") ||\r\n type.includes(\"7z\") ||\r\n type.includes(\"gzip\");\r\n\r\nconst isCode = (type: string): boolean =>\r\n type === \"application/javascript\" ||\r\n type === \"application/json\" ||\r\n type === \"text/html\" ||\r\n type === \"text/css\" ||\r\n type === \"application/xml\" ||\r\n type.includes(\"javascript\");\r\n\r\nconst isFont = (type: string): boolean =>\r\n type.startsWith(\"font/\") || type.includes(\"font\");\r\n\r\n/**\r\n * Maps MIME types to file categories\r\n * @param mimeType - The MIME type string\r\n * @returns FileType category\r\n */\r\nconst mimeTypeToResolvedType = (mimeType: string): ResolvedFileType => {\r\n if (!mimeType) return \"other\";\r\n\r\n const type = mimeType.toLowerCase();\r\n\r\n if (isImage(type)) return \"image\";\r\n if (isVideo(type)) return \"video\";\r\n if (isAudio(type)) return \"audio\";\r\n if (isDocument(type)) return \"document\";\r\n if (isArchive(type)) return \"archive\";\r\n if (isCode(type)) return \"code\";\r\n if (isFont(type)) return \"font\";\r\n\r\n return \"other\";\r\n};\r\n\r\n// Optional: Export the predicate functions if you need them elsewhere\r\nconst fileTypePredicates = {\r\n isImage,\r\n isVideo,\r\n isAudio,\r\n isDocument,\r\n isArchive,\r\n isCode,\r\n isFont,\r\n};\r\n\r\n/**\r\n * Generates a text.txt file so you can test file uploading is working\r\n */\r\nfunction generateTestFile() {\r\n // Create test content\r\n const textContent = \"Hello, this is a test file for S3 upload!\";\r\n const buffer = Buffer.from(textContent, \"utf-8\");\r\n\r\n // Create a File object (assuming you have File constructor available in Bun)\r\n const testFile = new File([buffer], \"test.txt\", { type: \"text/plain\" });\r\n\r\n return testFile;\r\n}\r\n\r\nexport { mimeTypeToResolvedType, fileTypePredicates, generateTestFile };\r\n",
6
+ "export type BRPCErrorCode =\r\n | \"BAD_REQUEST\"\r\n | \"UNAUTHORIZED\"\r\n | \"FORBIDDEN\"\r\n | \"NOT_FOUND\"\r\n | \"METHOD_NOT_SUPPORTED\"\r\n | \"TIMEOUT\"\r\n | \"CONFLICT\"\r\n | \"PRECONDITION_FAILED\"\r\n | \"PAYLOAD_TOO_LARGE\"\r\n | \"UNPROCESSABLE_CONTENT\"\r\n | \"TOO_MANY_REQUESTS\"\r\n | \"CLIENT_CLOSED_REQUEST\"\r\n | \"INTERNAL_SERVER_ERROR\"\r\n | \"NOT_IMPLEMENTED\"\r\n | \"BAD_GATEWAY\"\r\n | \"SERVICE_UNAVAILABLE\"\r\n | \"GATEWAY_TIMEOUT\";\r\n\r\nexport interface BRPCErrorOptions {\r\n code: BRPCErrorCode;\r\n message: string;\r\n clientCode?: string; // Custom code for client-side detection\r\n cause?: Error;\r\n data?: Record<string, any>;\r\n}\r\n\r\nexport class BRPCError extends Error {\r\n public readonly code: BRPCErrorCode;\r\n public readonly clientCode?: string;\r\n public readonly httpStatus: number;\r\n public readonly data?: Record<string, any>;\r\n public readonly cause?: Error;\r\n\r\n private static readonly STATUS_MAP: Record<BRPCErrorCode, number> = {\r\n BAD_REQUEST: 400,\r\n UNAUTHORIZED: 401,\r\n FORBIDDEN: 403,\r\n NOT_FOUND: 404,\r\n METHOD_NOT_SUPPORTED: 405,\r\n TIMEOUT: 408,\r\n CONFLICT: 409,\r\n PRECONDITION_FAILED: 412,\r\n PAYLOAD_TOO_LARGE: 413,\r\n UNPROCESSABLE_CONTENT: 422,\r\n TOO_MANY_REQUESTS: 429,\r\n CLIENT_CLOSED_REQUEST: 499,\r\n INTERNAL_SERVER_ERROR: 500,\r\n NOT_IMPLEMENTED: 501,\r\n BAD_GATEWAY: 502,\r\n SERVICE_UNAVAILABLE: 503,\r\n GATEWAY_TIMEOUT: 504,\r\n };\r\n\r\n constructor(options: BRPCErrorOptions) {\r\n super(options.message);\r\n\r\n this.name = \"BRPCError\";\r\n this.code = options.code;\r\n this.clientCode = options.clientCode;\r\n this.httpStatus = BRPCError.STATUS_MAP[options.code];\r\n this.data = options.data;\r\n this.cause = options.cause;\r\n\r\n // Maintains proper stack trace for where our error was thrown (only available on V8)\r\n if (Error.captureStackTrace) {\r\n Error.captureStackTrace(this, BRPCError);\r\n }\r\n }\r\n\r\n /**\r\n * Create a serializable object for sending to frontend\r\n */\r\n toJSON() {\r\n return {\r\n name: this.name,\r\n code: this.code,\r\n clientCode: this.clientCode,\r\n message: this.message,\r\n data: this.data,\r\n httpStatus: this.httpStatus,\r\n };\r\n }\r\n\r\n /**\r\n * Static factory methods for common errors\r\n */\r\n static badRequest(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"BAD_REQUEST\", message, clientCode, data });\r\n }\r\n\r\n static unauthorized(\r\n message: string = \"Unauthorized\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"UNAUTHORIZED\", message, clientCode, data });\r\n }\r\n\r\n static forbidden(\r\n message: string = \"Forbidden\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"FORBIDDEN\", message, clientCode, data });\r\n }\r\n\r\n static notFound(\r\n message: string = \"Not Found\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static preconditionFailed(\r\n message: string = \"Precondition failed\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static conflict(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"CONFLICT\", message, clientCode, data });\r\n }\r\n\r\n static unprocessableContent(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"UNPROCESSABLE_CONTENT\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static tooManyRequests(\r\n message: string = \"Too many requests\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"TOO_MANY_REQUESTS\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static internalServerError(\r\n message: string = \"Internal Server Error\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"INTERNAL_SERVER_ERROR\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static timeout(\r\n message: string = \"Request timeout\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"TIMEOUT\", message, clientCode, data });\r\n }\r\n}\r\n",
7
+ "import sharp from \"sharp\";\r\nimport { local } from \"../local\";\r\nimport { fileTypePredicates } from \"../utils\";\r\nimport type { GetOptimizedImageOptions } from \"./types\";\r\n\r\n/**\r\n * Returns an existing optimized image from cache or\r\n * creates one on the fly\r\n * @example\r\n * ```ts\r\n * const optimizedImage = await getImage({\r\n * key: \"originalFileKey\",\r\n * width: 800\r\n * })\r\n * if (!optimizedImage) throw BRPCError.notFound()\r\n * return optimizedImage\r\n * ```\r\n */\r\nexport async function getOptimizedImage(options: GetOptimizedImageOptions) {\r\n // const url = new URL(req.url);\r\n // const imagePath = url.searchParams.get(\"src\");\r\n // const width = parseInt(url.searchParams.get(\"w\") || \"800\");\r\n // const quality = parseInt(url.searchParams.get(\"q\") || \"75\");\r\n\r\n const localCache = `./cache/images/${options.key}_${options.width}_${options.quality}.webp`;\r\n\r\n // 1. Check local cache\r\n const localFile = await local.getOne(localCache);\r\n\r\n if (localFile) {\r\n if (!fileTypePredicates.isImage(localFile.type)) {\r\n return null;\r\n }\r\n return localFile;\r\n }\r\n\r\n // 2. Check Spaces cache - using Bun's global s3 instance\r\n // const cachedFile = s3.file(`appS3Folder/cache/${cacheKey}`);\r\n // if (await cachedFile.exists()) {\r\n // const cached = await cachedFile.arrayBuffer();\r\n // await Bun.write(localCache, cached);\r\n // return new Response(cached, {\r\n // headers: { 'Content-Type': 'image/webp' }\r\n // });\r\n // }\r\n\r\n // 3. Fetch from Spaces, optimize, cache\r\n // const originalFile = s3.file(`originals/${imagePath}`);\r\n // const originalBuffer = await originalFile.arrayBuffer();\r\n\r\n // 3.2 Fetch from local\r\n // const originalPath = `./buckets/cache/images/originals/${options.path}`;\r\n const originalKey = options.key;\r\n const originalFile = await local.getOne(originalKey);\r\n\r\n if (!originalFile) return null;\r\n\r\n if (!fileTypePredicates.isImage(originalFile.type)) {\r\n return null;\r\n }\r\n\r\n const buffer = await originalFile.arrayBuffer();\r\n\r\n const optimizedBuffer = await sharp(buffer)\r\n .resize(options.width, null, { withoutEnlargement: true })\r\n .webp({ quality: options.quality ?? 75 })\r\n .toBuffer();\r\n\r\n // Cache everywhere\r\n // await Promise.all([\r\n // Bun.write(localCache, optimizedBuffer),\r\n // cachedFile.write(optimizedBuffer, { type: 'image/webp' })\r\n // ]);\r\n\r\n // Cache to local\r\n await Bun.write(localCache, optimizedBuffer);\r\n\r\n const cachedFile = Bun.file(localCache);\r\n\r\n const exists = await cachedFile.exists();\r\n\r\n if (!exists) return null;\r\n\r\n return cachedFile;\r\n}\r\n",
8
+ "import { randomUUID } from \"crypto\";\r\nimport { extname } from \"path\";\r\nimport type { SafeResult } from \"../../types\";\r\n\r\ntype UploadSuccess = {\r\n key: string;\r\n file: File;\r\n bytesWritten: number;\r\n};\r\n\r\n/**\r\n * Uploads a file to local filesystem\r\n * @param path - The folder path (e.g. \"public/images\")\r\n * @param file - The file to upload\r\n * @param filename - Optional custom filename to use instead of original file.name\r\n * @returns Promise<SafeResult<number>> - Success: {data: bytesWritten, error: null}\r\n * Failure: {data: null, error: Error}\r\n *\r\n * Example:\r\n * ```ts\r\n * const {data, error} = await uploadToLocal(\"public/avatars\", userPhoto);\r\n * if (error) {\r\n * console.error(\"Upload failed:\", error);\r\n * return;\r\n * }\r\n * console.log(`Uploaded ${data} bytes`);\r\n * ```\r\n */\r\nasync function uploadOne(\r\n path: string,\r\n file: File,\r\n _filename?: string\r\n): Promise<SafeResult<UploadSuccess>> {\r\n if (!file)\r\n return {\r\n data: null,\r\n error: new Error(`File not found`),\r\n };\r\n\r\n const cleanPath = path.replace(/^\\/+|\\/+$/g, \"\");\r\n const ext = extname(file.name);\r\n const uniqueName = `${randomUUID()}${ext}`;\r\n const filePath = `${cleanPath}/${uniqueName}`;\r\n const fullPath = `./buckets/${cleanPath}/${uniqueName}`;\r\n\r\n try {\r\n const writtenBytes = await Bun.write(fullPath, file);\r\n return {\r\n data: {\r\n key: filePath,\r\n file: file,\r\n bytesWritten: writtenBytes,\r\n },\r\n error: null,\r\n };\r\n } catch (err) {\r\n return {\r\n data: null,\r\n error: new Error(`Failed to upload ${file.name} to local filesystem`),\r\n };\r\n }\r\n}\r\n\r\nasync function uploadMany(\r\n path: string,\r\n files: File[],\r\n nameGetter?: (file: File) => string\r\n): Promise<SafeResult<UploadSuccess>[]> {\r\n if (!files || files.length === 0) {\r\n return [];\r\n }\r\n\r\n const uploadPromises = files.map((file) => {\r\n const filename = nameGetter?.(file) ?? file.name;\r\n return uploadOne(path, file, filename);\r\n });\r\n\r\n const results = await Promise.all(uploadPromises);\r\n\r\n return results;\r\n}\r\n\r\nasync function getOne(key: string) {\r\n const file = Bun.file(`./buckets/${key}`);\r\n const exists = await file.exists();\r\n\r\n if (!exists) return null;\r\n\r\n return file;\r\n}\r\n\r\nasync function deleteOne(key: string) {\r\n const file = Bun.file(`./buckets/${key}`);\r\n const exists = await file.exists();\r\n\r\n if (!exists) return false;\r\n\r\n await file.delete();\r\n\r\n return true;\r\n}\r\n\r\nexport const local = {\r\n uploadOne,\r\n uploadMany,\r\n getOne,\r\n deleteOne,\r\n};\r\n",
9
+ "import type { Acl, InsertStorageObjet } from \"../types\";\r\nimport bunPath from \"path\";\r\nimport { mimeTypeToResolvedType } from \"../utils\";\r\nimport { BRPCError } from \"../../errors/BRPCError\";\r\nimport { CONSTRUCTED_AWS_FOLDER } from \"./constants\";\r\nimport { s3 as s3Bun } from \"bun\";\r\nimport type { OptimizedImageBuffer } from \"../images\";\r\n\r\n/**\r\n * Uploads a file or optimized buffer to S3 with UUID-based naming\r\n *\r\n * @param path - S3 folder path (e.g. \"images/avatars\")\r\n * @param file - File object or OptimizedBuffer from optimize()\r\n * @returns SafeResult with S3 key, bytes written, isPrivate and metadata for database storage\r\n *\r\n * @throws {Error} When buffer is provided without required metadata\r\n *\r\n * @example\r\n * ```typescript\r\n * // regular files\r\n * const result = await uploadOne('images', userFile);\r\n *\r\n * // optimized images\r\n * const optimized = await optimize(bunFile);\r\n * const result2 = await uploadOne('images/optimized', optimized);\r\n *\r\n * if (result.error) throw BRPCError.conlict(\"Error uploading \")\r\n *\r\n * if (result.data) {\r\n * console.log(`Uploaded: ${result.data.key}`);\r\n * // Save metadata to DB: result.data.metadata\r\n * }\r\n * ```\r\n */\r\nasync function uploadOne(\r\n path: string,\r\n file: File | OptimizedImageBuffer,\r\n opts?: { acl?: Acl; externalReference?: string; uploadedBy?: string }\r\n): Promise<InsertStorageObjet> {\r\n // Extract file info based on type\r\n const fileInfo =\r\n \"buffer\" in file\r\n ? {\r\n buffer: file.buffer,\r\n metadata: file.metadata,\r\n }\r\n : {\r\n buffer: file,\r\n metadata: {\r\n name: file.name,\r\n type: file.type,\r\n extension: bunPath.extname(file.name),\r\n resolvedType: mimeTypeToResolvedType(file.type),\r\n },\r\n };\r\n\r\n // Validate metadata for buffer uploads\r\n if (\"buffer\" in file && !file.metadata) {\r\n throw BRPCError.badRequest(\"Metadata required when uploading buffer\");\r\n }\r\n\r\n // Common upload logic\r\n const cleanPath = path.length > 1 ? `${path.replace(/^\\/+|\\/+$/g, \"\")}/` : \"\";\r\n const fileName = `${crypto.randomUUID()}${fileInfo.metadata.extension}`;\r\n const key = `${CONSTRUCTED_AWS_FOLDER}${cleanPath}${fileName}`;\r\n\r\n const s3file = s3Bun.file(key);\r\n\r\n const acl = opts?.acl ?? \"public-read\";\r\n\r\n const isPrivate = acl !== \"public-read\" && acl !== \"public-read-write\";\r\n\r\n const writtenBytes = await s3file.write(fileInfo.buffer, {\r\n acl: acl,\r\n });\r\n\r\n const digestedObject: InsertStorageObjet = {\r\n uploadedBy: opts?.uploadedBy ?? null,\r\n key: key,\r\n url: null,\r\n name: fileInfo.metadata.name,\r\n thumbnail: null,\r\n resolvedType: fileInfo.metadata.resolvedType,\r\n provider: \"s3\",\r\n isPrivate: isPrivate,\r\n metadata: {\r\n size: writtenBytes,\r\n type: fileInfo.metadata.type,\r\n extension: fileInfo.metadata.extension,\r\n acl: acl,\r\n },\r\n isActive: true,\r\n };\r\n\r\n return digestedObject;\r\n}\r\n\r\nexport { uploadOne };\r\n",
10
+ "const AWS_FOLDER = process.env.AWS_FOLDER;\r\nconst CONSTRUCTED_AWS_FOLDER = AWS_FOLDER ? `${AWS_FOLDER}/` : \"\";\r\n\r\nexport { CONSTRUCTED_AWS_FOLDER };\r\n",
11
+ "import { s3 as s3Bun } from \"bun\";\r\n\r\n/**\r\n * Checks a file existance in S3\r\n */\r\nasync function checkFileExistance(\r\n key: string,\r\n opts: { debug?: boolean } = { debug: process.env.NODE_ENV !== \"production\" }\r\n): Promise<{ exists: boolean; error: null } | { exists: null; error: Error }> {\r\n try {\r\n const exists = await s3Bun.exists(key);\r\n return { exists, error: null };\r\n } catch (e) {\r\n if (opts?.debug) {\r\n console.error(\"There was an error checking for file existance\", e);\r\n }\r\n return { exists: null, error: e as Error };\r\n }\r\n}\r\n\r\nexport { checkFileExistance };\r\n"
12
+ ],
13
+ "mappings": ";AAEA,IAAM,EAAU,CAAC,IAA0B,EAAK,WAAW,QAAQ,EAE7D,EAAU,CAAC,IAA0B,EAAK,WAAW,QAAQ,EAE7D,EAAU,CAAC,IAA0B,EAAK,WAAW,QAAQ,EAE7D,EAAa,CAAC,IAClB,EAAK,WAAW,OAAO,GACvB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,MAAM,GACpB,EAAK,SAAS,OAAO,GACrB,EAAK,SAAS,cAAc,GAC5B,EAAK,SAAS,cAAc,GAC5B,IAAS,kBAEL,EAAY,CAAC,IACjB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,IAAI,GAClB,EAAK,SAAS,MAAM,EAEhB,EAAS,CAAC,IACd,IAAS,0BACT,IAAS,oBACT,IAAS,aACT,IAAS,YACT,IAAS,mBACT,EAAK,SAAS,YAAY,EAEtB,EAAS,CAAC,IACd,EAAK,WAAW,OAAO,GAAK,EAAK,SAAS,MAAM,EAO5C,EAAyB,CAAC,IAAuC,CACrE,GAAI,CAAC,EAAU,MAAO,QAEtB,IAAM,EAAO,EAAS,YAAY,EAElC,GAAI,EAAQ,CAAI,EAAG,MAAO,QAC1B,GAAI,EAAQ,CAAI,EAAG,MAAO,QAC1B,GAAI,EAAQ,CAAI,EAAG,MAAO,QAC1B,GAAI,EAAW,CAAI,EAAG,MAAO,WA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GAAI,CAEF,MAAO,CAAE,OADM,MAAM,EAAM,OAAO,CAAG,EACpB,MAAO,IAAK,EAC7B,MAAO,EAAG,CACV,GAAI,GAAM,MACR,QAAQ,MAAM,iDAAkD,CAAC,EAEnE,MAAO,CAAE,OAAQ,KAAM,MAAO,CAAW",
14
+ "debugId": "DE5A1B3485300E8464756E2164756E21",
15
+ "names": []
16
+ }
@@ -0,0 +1,35 @@
1
+ import type { SafeResult } from "../../types";
2
+ type UploadSuccess = {
3
+ key: string;
4
+ file: File;
5
+ bytesWritten: number;
6
+ };
7
+ /**
8
+ * Uploads a file to local filesystem
9
+ * @param path - The folder path (e.g. "public/images")
10
+ * @param file - The file to upload
11
+ * @param filename - Optional custom filename to use instead of original file.name
12
+ * @returns Promise<SafeResult<number>> - Success: {data: bytesWritten, error: null}
13
+ * Failure: {data: null, error: Error}
14
+ *
15
+ * Example:
16
+ * ```ts
17
+ * const {data, error} = await uploadToLocal("public/avatars", userPhoto);
18
+ * if (error) {
19
+ * console.error("Upload failed:", error);
20
+ * return;
21
+ * }
22
+ * console.log(`Uploaded ${data} bytes`);
23
+ * ```
24
+ */
25
+ declare function uploadOne(path: string, file: File, _filename?: string): Promise<SafeResult<UploadSuccess>>;
26
+ declare function uploadMany(path: string, files: File[], nameGetter?: (file: File) => string): Promise<SafeResult<UploadSuccess>[]>;
27
+ declare function getOne(key: string): Promise<Bun.BunFile | null>;
28
+ declare function deleteOne(key: string): Promise<boolean>;
29
+ export declare const local: {
30
+ uploadOne: typeof uploadOne;
31
+ uploadMany: typeof uploadMany;
32
+ getOne: typeof getOne;
33
+ deleteOne: typeof deleteOne;
34
+ };
35
+ export {};
@@ -0,0 +1,2 @@
1
+ declare const CONSTRUCTED_AWS_FOLDER: string;
2
+ export { CONSTRUCTED_AWS_FOLDER };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Checks a file existance in S3
3
+ */
4
+ declare function checkFileExistance(key: string, opts?: {
5
+ debug?: boolean;
6
+ }): Promise<{
7
+ exists: boolean;
8
+ error: null;
9
+ } | {
10
+ exists: null;
11
+ error: Error;
12
+ }>;
13
+ export { checkFileExistance };
@@ -0,0 +1,2 @@
1
+ export * from "./upload";
2
+ export * from "./helpers";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,34 @@
1
+ import type { Acl, InsertStorageObjet } from "../types";
2
+ import type { OptimizedImageBuffer } from "../images";
3
+ /**
4
+ * Uploads a file or optimized buffer to S3 with UUID-based naming
5
+ *
6
+ * @param path - S3 folder path (e.g. "images/avatars")
7
+ * @param file - File object or OptimizedBuffer from optimize()
8
+ * @returns SafeResult with S3 key, bytes written, isPrivate and metadata for database storage
9
+ *
10
+ * @throws {Error} When buffer is provided without required metadata
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // regular files
15
+ * const result = await uploadOne('images', userFile);
16
+ *
17
+ * // optimized images
18
+ * const optimized = await optimize(bunFile);
19
+ * const result2 = await uploadOne('images/optimized', optimized);
20
+ *
21
+ * if (result.error) throw BRPCError.conlict("Error uploading ")
22
+ *
23
+ * if (result.data) {
24
+ * console.log(`Uploaded: ${result.data.key}`);
25
+ * // Save metadata to DB: result.data.metadata
26
+ * }
27
+ * ```
28
+ */
29
+ declare function uploadOne(path: string, file: File | OptimizedImageBuffer, opts?: {
30
+ acl?: Acl;
31
+ externalReference?: string;
32
+ uploadedBy?: string;
33
+ }): Promise<InsertStorageObjet>;
34
+ export { uploadOne };
@@ -0,0 +1,59 @@
1
+ type Acl = "public-read" | "private" | "public-read-write" | "aws-exec-read" | "authenticated-read" | "bucket-owner-read" | "bucket-owner-full-control" | "log-delivery-write";
2
+ type ResolvedFileType = "image" | "video" | "audio" | "document" | "archive" | "code" | "data" | "font" | "other";
3
+ type StorageObjectMetadata = {
4
+ /**
5
+ * Mime type
6
+ */
7
+ type: string;
8
+ /**
9
+ * File size in bytes
10
+ */
11
+ size: number;
12
+ /**
13
+ * File extension
14
+ */
15
+ extension: string;
16
+ /**
17
+ * S3 acl
18
+ */
19
+ acl: Acl;
20
+ width?: number;
21
+ height?: number;
22
+ duration?: number;
23
+ aspectRatio?: string;
24
+ resolution?: string;
25
+ bitrate?: number;
26
+ codec?: string;
27
+ fps?: number;
28
+ alt?: string;
29
+ colorPalette?: string;
30
+ };
31
+ type StorageProvider = "s3" | "local" | "external";
32
+ type StorageObject = {
33
+ id: string;
34
+ createdAt: string;
35
+ updatedAt: string;
36
+ uploadedBy: string | null;
37
+ /**
38
+ * Key for S3 or Local storage files: "uploads/user123/file.jpg"
39
+ * Will be null for external files
40
+ */
41
+ key: string | null;
42
+ /**
43
+ * Full URL for internal or external files: "https://unsplash.com/photo.jpg"
44
+ * Can be null if using key and file is private
45
+ */
46
+ url: string | null;
47
+ name: string | null;
48
+ thumbnail: string | null;
49
+ resolvedType: ResolvedFileType;
50
+ provider: StorageProvider;
51
+ isPrivate: boolean | null;
52
+ metadata: StorageObjectMetadata | null;
53
+ isActive: boolean;
54
+ referenceCount: number;
55
+ };
56
+ type InsertStorageObjet = Omit<StorageObject, "id" | "createdAt" | "updatedAt" | "referenceCount"> & {
57
+ key: string;
58
+ };
59
+ export type { ResolvedFileType, StorageObject, StorageObjectMetadata, Acl, StorageProvider, InsertStorageObjet, };
@@ -0,0 +1,21 @@
1
+ import type { ResolvedFileType } from "../types";
2
+ /**
3
+ * Maps MIME types to file categories
4
+ * @param mimeType - The MIME type string
5
+ * @returns FileType category
6
+ */
7
+ declare const mimeTypeToResolvedType: (mimeType: string) => ResolvedFileType;
8
+ declare const fileTypePredicates: {
9
+ isImage: (type: string) => boolean;
10
+ isVideo: (type: string) => boolean;
11
+ isAudio: (type: string) => boolean;
12
+ isDocument: (type: string) => boolean;
13
+ isArchive: (type: string) => boolean;
14
+ isCode: (type: string) => boolean;
15
+ isFont: (type: string) => boolean;
16
+ };
17
+ /**
18
+ * Generates a text.txt file so you can test file uploading is working
19
+ */
20
+ declare function generateTestFile(): File;
21
+ export { mimeTypeToResolvedType, fileTypePredicates, generateTestFile };
@@ -0,0 +1 @@
1
+ export * from "./files";
@@ -27,10 +27,10 @@ export declare function streamMediaInternal(file: BunFile, request: Request, all
27
27
  * @example
28
28
  * ```typescript
29
29
  * // Stream any media
30
- * return stream(file, ctx.request);
30
+ * return streamMedia(file, ctx.request);
31
31
  *
32
32
  * // Stream with validation
33
- * return stream(file, ctx.request, {
33
+ * return streamMedia(file, ctx.request, {
34
34
  * acceptedExtensions: ["mp4", "webm", "mov"],
35
35
  * maxChunkSize: 5 * 1024 * 1024,
36
36
  * });
package/dist/types.d.ts CHANGED
@@ -89,3 +89,10 @@ export type InferProcedureType<T> = T extends Procedure<any, any, any, infer P>
89
89
  export type ExtractRouterStructure<T extends Routes> = {
90
90
  [K in keyof T]: T[K] extends Routes ? ExtractRouterStructure<T[K]> : T[K] extends Procedure<any, any, any, any> ? T[K] : never;
91
91
  };
92
+ export type SafeResult<T> = {
93
+ data: null;
94
+ error: Error;
95
+ } | {
96
+ data: T;
97
+ error: null;
98
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mateosuarezdev/brpc",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "A Type-Safe, Flexible Web application framework for Bun",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -11,6 +11,11 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "import": "./dist/index.js",
13
13
  "require": "./dist/index.cjs"
14
+ },
15
+ "./storage": {
16
+ "types": "./dist/storage/index.d.ts",
17
+ "import": "./dist/storage/index.js",
18
+ "require": "./dist/storage/index.cjs"
14
19
  }
15
20
  },
16
21
  "files": [
@@ -58,6 +63,9 @@
58
63
  "zod": "^3.23.8"
59
64
  },
60
65
  "peerDependenciesMeta": {
66
+ "sharp": {
67
+ "optional": true
68
+ },
61
69
  "zod": {
62
70
  "optional": false
63
71
  }
@@ -65,6 +73,7 @@
65
73
  "devDependencies": {
66
74
  "@types/bun": "^1.2.23",
67
75
  "rimraf": "^6.0.1",
76
+ "sharp": "^0.34.4",
68
77
  "typescript": "^5.9.3",
69
78
  "zod": "^3.23.8"
70
79
  },
File without changes