@mateosuarezdev/brpc 1.0.5
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/BRPCError.d.ts +41 -0
- package/dist/context.d.ts +2 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +19 -0
- package/dist/middlewares/index.d.ts +3 -0
- package/dist/middlewares/pathblocker/index.d.ts +5 -0
- package/dist/middlewares/profanity/index.d.ts +36 -0
- package/dist/middlewares/ratelimiter/index.d.ts +11 -0
- package/dist/procedure.d.ts +30 -0
- package/dist/router/RouteMatcher.d.ts +100 -0
- package/dist/router/index.d.ts +1 -0
- package/dist/router/router.d.ts +123 -0
- package/dist/schemas.d.ts +48 -0
- package/dist/stream/index.d.ts +2 -0
- package/dist/stream/media.d.ts +40 -0
- package/dist/types.d.ts +91 -0
- package/package.json +74 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type BRPCErrorCode = "BAD_REQUEST" | "UNAUTHORIZED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_SUPPORTED" | "TIMEOUT" | "CONFLICT" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "UNPROCESSABLE_CONTENT" | "TOO_MANY_REQUESTS" | "CLIENT_CLOSED_REQUEST" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT";
|
|
2
|
+
export interface BRPCErrorOptions {
|
|
3
|
+
code: BRPCErrorCode;
|
|
4
|
+
message: string;
|
|
5
|
+
clientCode?: string;
|
|
6
|
+
cause?: Error;
|
|
7
|
+
data?: Record<string, any>;
|
|
8
|
+
}
|
|
9
|
+
export declare class BRPCError extends Error {
|
|
10
|
+
readonly code: BRPCErrorCode;
|
|
11
|
+
readonly clientCode?: string;
|
|
12
|
+
readonly httpStatus: number;
|
|
13
|
+
readonly data?: Record<string, any>;
|
|
14
|
+
readonly cause?: Error;
|
|
15
|
+
private static readonly STATUS_MAP;
|
|
16
|
+
constructor(options: BRPCErrorOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Create a serializable object for sending to frontend
|
|
19
|
+
*/
|
|
20
|
+
toJSON(): {
|
|
21
|
+
name: string;
|
|
22
|
+
code: BRPCErrorCode;
|
|
23
|
+
clientCode: string | undefined;
|
|
24
|
+
message: string;
|
|
25
|
+
data: Record<string, any> | undefined;
|
|
26
|
+
httpStatus: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Static factory methods for common errors
|
|
30
|
+
*/
|
|
31
|
+
static badRequest(message: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
32
|
+
static unauthorized(message?: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
33
|
+
static forbidden(message?: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
34
|
+
static notFound(message?: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
35
|
+
static preconditionFailed(message?: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
36
|
+
static conflict(message: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
37
|
+
static unprocessableContent(message: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
38
|
+
static tooManyRequests(message?: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
39
|
+
static internalServerError(message?: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
40
|
+
static timeout(message?: string, clientCode?: string, data?: Record<string, any>): BRPCError;
|
|
41
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./types";
|
|
2
|
+
export { createProcedure } from "./procedure";
|
|
3
|
+
export { createRouter } from "./router";
|
|
4
|
+
export { createContext } from "./context";
|
|
5
|
+
export * from "@mateosuarezdev/headers-builder";
|
|
6
|
+
export * from "./schemas";
|
|
7
|
+
export * from "./middlewares";
|
|
8
|
+
export * from "./BRPCError";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
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};
|
|
3
|
+
|
|
4
|
+
//# debugId=FC699260F69ADD2F64756E2164756E21
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
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"],
|
|
4
|
+
"sourcesContent": [
|
|
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",
|
|
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
|
+
"// 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
|
+
"// @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",
|
|
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
|
+
"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
|
+
],
|
|
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,MAAO,CACL,OAAQ,EAAE,OAAO,CAAC,CAAC,EACnB,QAAS,KACT,KAAM,KACN,MAAO,OACP,UACA,YAAa,KAAK,WACpB,EAIF,UAAU,CACR,EAMA,CACA,MAAO,CACL,OAAQ,EAAE,OAAO,CAAC,CAAC,EACnB,QAAS,KACT,KAAM,KACN,MAAO,aACP,UACA,YAAa,KAAK,WACpB,EAGF,IAAI,CACF,EAC+C,CAC/C,M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AAQ,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,EAC1C,IAAI,MAAM,EAAQ,UAAU,EAAE,OAAO",
|
|
17
|
+
"debugId": "FC699260F69ADD2F64756E2164756E21",
|
|
18
|
+
"names": []
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type BaseContext } from "../..";
|
|
2
|
+
type ProfanityLanguageData = {
|
|
3
|
+
badWords: string[];
|
|
4
|
+
badPhrases: string[];
|
|
5
|
+
};
|
|
6
|
+
type ProfanityFilterConfig = {
|
|
7
|
+
languages?: string[];
|
|
8
|
+
message?: string;
|
|
9
|
+
checkBody?: boolean;
|
|
10
|
+
checkQuery?: boolean;
|
|
11
|
+
checkParams?: boolean;
|
|
12
|
+
customLanguages?: Record<string, ProfanityLanguageData>;
|
|
13
|
+
};
|
|
14
|
+
declare class ProfanityFilter {
|
|
15
|
+
private substitutions;
|
|
16
|
+
private badWords;
|
|
17
|
+
private badPhrases;
|
|
18
|
+
private normalizedBadWords;
|
|
19
|
+
private normalizedBadPhrases;
|
|
20
|
+
private normalizeCache;
|
|
21
|
+
private initializedLanguages;
|
|
22
|
+
constructor(customLanguages?: Record<string, ProfanityLanguageData>);
|
|
23
|
+
private addLanguage;
|
|
24
|
+
private initLanguage;
|
|
25
|
+
private normalize;
|
|
26
|
+
containsProfanity(text: string, languages?: string[]): boolean;
|
|
27
|
+
detectBadContent(text: string, languages?: string[]): Array<{
|
|
28
|
+
content: string;
|
|
29
|
+
index: number;
|
|
30
|
+
length: number;
|
|
31
|
+
}>;
|
|
32
|
+
censorText(text: string, languages?: string[]): string;
|
|
33
|
+
}
|
|
34
|
+
declare const createProfanityMiddleware: (config?: ProfanityFilterConfig) => <C extends BaseContext>(ctx: C) => Promise<void>;
|
|
35
|
+
export { ProfanityFilter, createProfanityMiddleware };
|
|
36
|
+
export type { ProfanityLanguageData, ProfanityFilterConfig };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { BaseContext } from "../../types";
|
|
2
|
+
export interface RateLimitConfig {
|
|
3
|
+
windowMs: number;
|
|
4
|
+
maxRequests: number;
|
|
5
|
+
maxEntries?: number;
|
|
6
|
+
cleanupIntervalMs?: number;
|
|
7
|
+
message?: string;
|
|
8
|
+
statusCode?: number;
|
|
9
|
+
headerPrefix?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const createRateLimiter: (config: RateLimitConfig) => <C extends BaseContext>(ctx: C) => Promise<void>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { BaseContext, Handler, InferInput, Procedure, StreamableResponse } from "./types";
|
|
3
|
+
import type { BunFile } from "bun";
|
|
4
|
+
declare class ProcedureBuilder<C extends BaseContext> {
|
|
5
|
+
private middlewares;
|
|
6
|
+
constructor(middlewares?: ((ctx: C) => Promise<void>)[]);
|
|
7
|
+
use(middleware: (ctx: C) => Promise<void>): ProcedureBuilder<C>;
|
|
8
|
+
input<I extends z.ZodType>(schema: I): InputProcedureBuilder<C, I>;
|
|
9
|
+
query<O>(handler: Handler<C, {}, O>): Procedure<C, z.ZodObject<{}>, O, "query">;
|
|
10
|
+
mutation<O>(handler: Handler<C, {}, O>): Procedure<C, z.ZodObject<{}>, O, "mutation">;
|
|
11
|
+
formMutation<O>(handler: Handler<C, {}, O>): Procedure<C, z.ZodObject<{}>, O, "formMutation">;
|
|
12
|
+
subscription<O>(handler: Handler<C, {}, O>): Procedure<C, z.ZodObject<{}>, O, "subscription">;
|
|
13
|
+
file(handler: Handler<C, {}, BunFile | Response>): Procedure<C, z.ZodObject<{}>, BunFile | Response, "file">;
|
|
14
|
+
fileStream(handler: Handler<C, {}, Blob | string>): Procedure<C, z.ZodObject<{}>, StreamableResponse | Blob | string, "fileStream">;
|
|
15
|
+
html(handler: Handler<C, {}, string>): Procedure<C, z.ZodObject<{}>, string, "html">;
|
|
16
|
+
}
|
|
17
|
+
declare class InputProcedureBuilder<C extends BaseContext, I extends z.ZodType> {
|
|
18
|
+
private middlewares;
|
|
19
|
+
private inputSchema;
|
|
20
|
+
constructor(middlewares: ((ctx: C) => Promise<void>)[], inputSchema: I);
|
|
21
|
+
query<O>(handler: Handler<C, InferInput<I>, O>): Procedure<C, I, O, "query">;
|
|
22
|
+
mutation<O>(handler: Handler<C, InferInput<I>, O>): Procedure<C, I, O, "mutation">;
|
|
23
|
+
formMutation<O>(handler: Handler<C, InferInput<I>, O>): Procedure<C, I, O, "formMutation">;
|
|
24
|
+
subscription<O>(handler: Handler<C, InferInput<I>, O>): Procedure<C, I, O, "subscription">;
|
|
25
|
+
file(handler: Handler<C, InferInput<I>, BunFile | Response>): Procedure<C, I, BunFile | Response, "file">;
|
|
26
|
+
fileStream(handler: Handler<C, InferInput<I>, StreamableResponse | Blob | string>): Procedure<C, I, StreamableResponse | Blob | string, "fileStream">;
|
|
27
|
+
html(handler: Handler<C, {}, string>): Procedure<C, z.ZodObject<{}>, string, "html">;
|
|
28
|
+
}
|
|
29
|
+
export declare function createProcedure<C extends BaseContext>(): ProcedureBuilder<C>;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { Routes, Procedure, ProcedureType } from "../types";
|
|
2
|
+
interface RouteMatch {
|
|
3
|
+
params: Record<string, string>;
|
|
4
|
+
route: Procedure<any, any, any, any>;
|
|
5
|
+
}
|
|
6
|
+
interface RouteMatcherOptions {
|
|
7
|
+
enableCaching?: boolean;
|
|
8
|
+
maxCacheSize?: number;
|
|
9
|
+
debug?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface RouteStats {
|
|
12
|
+
totalRoutes: number;
|
|
13
|
+
staticRoutes: number;
|
|
14
|
+
paramRoutes: number;
|
|
15
|
+
wildcardRoutes: number;
|
|
16
|
+
indexRoutes: number;
|
|
17
|
+
maxDepth: number;
|
|
18
|
+
procedureTypes: Record<ProcedureType, number>;
|
|
19
|
+
}
|
|
20
|
+
export declare class RouteMatcher {
|
|
21
|
+
private routeTree;
|
|
22
|
+
private routeCache;
|
|
23
|
+
private readonly options;
|
|
24
|
+
private routeStats;
|
|
25
|
+
constructor(routes: Routes, options?: RouteMatcherOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Main route matching method
|
|
28
|
+
*/
|
|
29
|
+
match(pathParts: string[]): RouteMatch | null;
|
|
30
|
+
/**
|
|
31
|
+
* Route matching with caching
|
|
32
|
+
*/
|
|
33
|
+
private matchWithCache;
|
|
34
|
+
/**
|
|
35
|
+
* Direct route matching without caching
|
|
36
|
+
*/
|
|
37
|
+
private matchDirect;
|
|
38
|
+
/**
|
|
39
|
+
* Traverse the compiled route tree
|
|
40
|
+
*/
|
|
41
|
+
private traverseTree;
|
|
42
|
+
/**
|
|
43
|
+
* Compile the routes object into an optimized tree structure
|
|
44
|
+
*/
|
|
45
|
+
private compileRoutes;
|
|
46
|
+
/**
|
|
47
|
+
* Type guard to check if value is a Procedure
|
|
48
|
+
*/
|
|
49
|
+
private isProcedure;
|
|
50
|
+
/**
|
|
51
|
+
* Cache management with LRU behavior
|
|
52
|
+
*/
|
|
53
|
+
private cacheResult;
|
|
54
|
+
/**
|
|
55
|
+
* Analyze route structure for optimization insights and debugging
|
|
56
|
+
*/
|
|
57
|
+
private analyzeRoutes;
|
|
58
|
+
/**
|
|
59
|
+
* Utility methods
|
|
60
|
+
*/
|
|
61
|
+
clearCache(): void;
|
|
62
|
+
getCacheStats(): {
|
|
63
|
+
size: number;
|
|
64
|
+
maxSize: number;
|
|
65
|
+
hitRatio?: number;
|
|
66
|
+
routeStats: RouteStats;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Get suggestions for route optimization
|
|
70
|
+
*/
|
|
71
|
+
getOptimizationSuggestions(): string[];
|
|
72
|
+
/**
|
|
73
|
+
* Find all routes matching a pattern (useful for debugging)
|
|
74
|
+
*/
|
|
75
|
+
findRoutesByPattern(_pattern: string): string[];
|
|
76
|
+
/**
|
|
77
|
+
* Get route information for debugging
|
|
78
|
+
*/
|
|
79
|
+
getRouteInfo(path: string): {
|
|
80
|
+
found: boolean;
|
|
81
|
+
route?: Procedure<any, any, any, any>;
|
|
82
|
+
params?: Record<string, string>;
|
|
83
|
+
cached: boolean;
|
|
84
|
+
matchTime?: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Benchmark route matching performance
|
|
88
|
+
*/
|
|
89
|
+
benchmark(testPaths: string[], iterations?: number): {
|
|
90
|
+
averageTime: number;
|
|
91
|
+
totalTime: number;
|
|
92
|
+
routesPerSecond: number;
|
|
93
|
+
cacheHitRatio?: number;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Warm up the cache with common routes
|
|
97
|
+
*/
|
|
98
|
+
warmupCache(commonPaths: string[]): void;
|
|
99
|
+
}
|
|
100
|
+
export type { RouteMatch, RouteMatcherOptions, RouteStats };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./router";
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { BaseContext, InferInput, Procedure, RouterConfig, Routes } from "../types";
|
|
2
|
+
export declare class Router<C extends BaseContext> {
|
|
3
|
+
private routes;
|
|
4
|
+
private routeMatcher;
|
|
5
|
+
private contextCreator;
|
|
6
|
+
private prefix;
|
|
7
|
+
private globalMiddlewares;
|
|
8
|
+
private server;
|
|
9
|
+
private wsConfig;
|
|
10
|
+
private integrations;
|
|
11
|
+
private development;
|
|
12
|
+
private debug;
|
|
13
|
+
private allowedOrigins;
|
|
14
|
+
private activeConnections;
|
|
15
|
+
private readonly maxConnections;
|
|
16
|
+
private readonly connectionTimeout;
|
|
17
|
+
private readonly maxRequestSize;
|
|
18
|
+
private readonly requestTimeout;
|
|
19
|
+
constructor(config: RouterConfig<C>);
|
|
20
|
+
/**
|
|
21
|
+
* Simplified route finding using RouteMatcher
|
|
22
|
+
*/
|
|
23
|
+
private findRoute;
|
|
24
|
+
/**
|
|
25
|
+
* Parses and validates the request input
|
|
26
|
+
*/
|
|
27
|
+
private parseInput;
|
|
28
|
+
/**
|
|
29
|
+
* Helper method to parse form data
|
|
30
|
+
*/
|
|
31
|
+
private parseFormData;
|
|
32
|
+
/**
|
|
33
|
+
* handleError
|
|
34
|
+
* @description global error handling for
|
|
35
|
+
* each incoming request
|
|
36
|
+
*/
|
|
37
|
+
private handleError;
|
|
38
|
+
/**
|
|
39
|
+
* handles the default cache control headers for file routes
|
|
40
|
+
*/
|
|
41
|
+
private handleFileCacheControl;
|
|
42
|
+
/**
|
|
43
|
+
* Handles the behavior of each request
|
|
44
|
+
*/
|
|
45
|
+
private handleRequest;
|
|
46
|
+
private _handleRequest;
|
|
47
|
+
/**
|
|
48
|
+
* Handle responses based on procedure type
|
|
49
|
+
*/
|
|
50
|
+
private handleProcedureResponse;
|
|
51
|
+
private cleanupStaleConnections;
|
|
52
|
+
/**
|
|
53
|
+
* handleWebSocketUpgrade
|
|
54
|
+
* @description upgrades the incoming request to
|
|
55
|
+
* a websocket connection
|
|
56
|
+
*/
|
|
57
|
+
private handleWebSocketUpgrade;
|
|
58
|
+
/**
|
|
59
|
+
* Handles WebSocket authentication
|
|
60
|
+
*/
|
|
61
|
+
private handleAuthenticate;
|
|
62
|
+
/**
|
|
63
|
+
* handles the ws client topic subscription
|
|
64
|
+
*/
|
|
65
|
+
private handleSubscribe;
|
|
66
|
+
/**
|
|
67
|
+
* handles the ws client topic unsubscription
|
|
68
|
+
*/
|
|
69
|
+
private handleUnsubscribe;
|
|
70
|
+
/**
|
|
71
|
+
* handles the message publication of a certain ws client
|
|
72
|
+
*/
|
|
73
|
+
private handlePublish;
|
|
74
|
+
private publishToProcedure;
|
|
75
|
+
publish<P extends Procedure<any, any, any, "subscription">>(procedure: P, input: InferInput<P["_input"]>, params?: Record<string, string>): Promise<any>;
|
|
76
|
+
/**
|
|
77
|
+
* findRoutePath
|
|
78
|
+
* @description finds the full path to a procedure in the routes tree
|
|
79
|
+
*/
|
|
80
|
+
private findRoutePath;
|
|
81
|
+
private getIntegrationRoutes;
|
|
82
|
+
/**
|
|
83
|
+
* New utility methods for route management
|
|
84
|
+
*/
|
|
85
|
+
getRouteStats(): {
|
|
86
|
+
cache: {
|
|
87
|
+
size: number;
|
|
88
|
+
maxSize: number;
|
|
89
|
+
hitRatio?: number;
|
|
90
|
+
routeStats: import("./RouteMatcher").RouteStats;
|
|
91
|
+
};
|
|
92
|
+
suggestions: string[];
|
|
93
|
+
};
|
|
94
|
+
clearRouteCache(): void;
|
|
95
|
+
benchmarkRoutes(testPaths?: string[], iterations?: number): {
|
|
96
|
+
averageTime: number;
|
|
97
|
+
totalTime: number;
|
|
98
|
+
routesPerSecond: number;
|
|
99
|
+
cacheHitRatio?: number;
|
|
100
|
+
} | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Hot reload support for development
|
|
103
|
+
*/
|
|
104
|
+
updateRoutes(newRoutes: Routes): void;
|
|
105
|
+
/**
|
|
106
|
+
* publish
|
|
107
|
+
* @description publish a message to a topic from
|
|
108
|
+
* the server itself
|
|
109
|
+
*/
|
|
110
|
+
freePublish<T>(topic: string, message: T): void;
|
|
111
|
+
/**
|
|
112
|
+
* Listen
|
|
113
|
+
* @description starts up the Bun http server
|
|
114
|
+
*/
|
|
115
|
+
listen(port: number, callback?: () => void): void;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* createRouter
|
|
119
|
+
* @description helper function to create a
|
|
120
|
+
* Router instance
|
|
121
|
+
*/
|
|
122
|
+
export declare function createRouter<C extends BaseContext>(config: RouterConfig<C>): Router<C>;
|
|
123
|
+
export type CreateRouter = ReturnType<typeof createRouter>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
declare const MIME_TYPES: {
|
|
3
|
+
readonly image: readonly ["image/jpeg", "image/png", "image/gif", "image/webp", "image/svg+xml"];
|
|
4
|
+
readonly video: readonly ["video/mp4", "video/webm", "video/ogg"];
|
|
5
|
+
readonly audio: readonly ["audio/mpeg", "audio/ogg", "audio/wav"];
|
|
6
|
+
readonly document: readonly ["application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
|
|
7
|
+
};
|
|
8
|
+
type MimeTypeGroup = keyof typeof MIME_TYPES;
|
|
9
|
+
type MimeTypeValues<T extends MimeTypeGroup> = (typeof MIME_TYPES)[T][number];
|
|
10
|
+
type MimeTypeConfig = {
|
|
11
|
+
[K in MimeTypeGroup]?: "*" | MimeTypeValues<K>[];
|
|
12
|
+
};
|
|
13
|
+
/** Options for creating a file validation schema */
|
|
14
|
+
type FileSchemaOptions = {
|
|
15
|
+
/** Accepted MIME types configuration. Use "*" for all types in a category or specify an array of types
|
|
16
|
+
* @example
|
|
17
|
+
* {
|
|
18
|
+
* image: "*", // All image types
|
|
19
|
+
* audio: ["audio/mpeg"], // Only MP3s
|
|
20
|
+
* document: ["application/pdf", "application/msword"] // PDFs and DOCs
|
|
21
|
+
* }
|
|
22
|
+
*/
|
|
23
|
+
acceptedTypes?: MimeTypeConfig;
|
|
24
|
+
/** Maximum file size in megabytes */
|
|
25
|
+
maxSize?: number;
|
|
26
|
+
/** Minimum file size in megabytes */
|
|
27
|
+
minSize?: number;
|
|
28
|
+
/** Whether the file field is required */
|
|
29
|
+
/** Custom error messages */
|
|
30
|
+
messages?: {
|
|
31
|
+
/** Custom message for type validation failure */
|
|
32
|
+
type?: string;
|
|
33
|
+
/** Custom message for maximum size validation failure */
|
|
34
|
+
maxSize?: string;
|
|
35
|
+
/** Custom message for minimum size validation failure */
|
|
36
|
+
minSize?: string;
|
|
37
|
+
/** Custom message for required field validation failure */
|
|
38
|
+
required?: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Creates a Zod schema for file validation with support for MIME types and size constraints
|
|
43
|
+
* you can extend it as any other zod schema after calling it
|
|
44
|
+
* @param {Object} options - The configuration options for the file schema
|
|
45
|
+
* @returns Zod schema for file validation
|
|
46
|
+
*/
|
|
47
|
+
export declare const createFileSchema: ({ acceptedTypes, maxSize, minSize, messages, }?: FileSchemaOptions) => z.ZodType<File, z.ZodTypeDef, File>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { BunFile } from "bun";
|
|
2
|
+
interface StreamMediaOptions {
|
|
3
|
+
maxChunkSize?: number;
|
|
4
|
+
cacheMaxAge?: number;
|
|
5
|
+
acceptedExtensions?: string[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Metadata for deferred media streaming
|
|
9
|
+
* Used with router's streamFile procedure
|
|
10
|
+
*/
|
|
11
|
+
export interface StreamableMediaResponse {
|
|
12
|
+
type: "stream";
|
|
13
|
+
file: BunFile;
|
|
14
|
+
request: Request;
|
|
15
|
+
options?: StreamMediaOptions;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Handle media streaming with automatic Range request support
|
|
19
|
+
* Supports video, audio, and other streamable media formats
|
|
20
|
+
* @internal - Used by router to process StreamableMediaResponse
|
|
21
|
+
*/
|
|
22
|
+
export declare function streamMediaInternal(file: BunFile, request: Request, allowedOrigins: string[], options?: StreamMediaOptions): Promise<Response>;
|
|
23
|
+
/**
|
|
24
|
+
* Helper function to create a streamable media response
|
|
25
|
+
* Returns metadata that the router will process with streamMediaInternal()
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Stream any media
|
|
30
|
+
* return stream(file, ctx.request);
|
|
31
|
+
*
|
|
32
|
+
* // Stream with validation
|
|
33
|
+
* return stream(file, ctx.request, {
|
|
34
|
+
* acceptedExtensions: ["mp4", "webm", "mov"],
|
|
35
|
+
* maxChunkSize: 5 * 1024 * 1024,
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function streamMedia(file: BunFile, request: Request, options?: StreamMediaOptions): StreamableMediaResponse;
|
|
40
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { BunRequest, ServerWebSocket } from "bun";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
export type ProcedureType = "query" | "mutation" | "subscription" | "file" | "fileStream" | "streamQuery" | "streamMutation" | "formMutation" | "html";
|
|
4
|
+
export type BaseProcedure = {
|
|
5
|
+
_input: z.ZodType<any>;
|
|
6
|
+
_output: any;
|
|
7
|
+
_type: ProcedureType;
|
|
8
|
+
};
|
|
9
|
+
export type InferRouterOutput<T> = {
|
|
10
|
+
[K in keyof T]: T[K] extends BaseProcedure ? InferProcedureOutput<T[K]> : T[K] extends Record<string, any> ? InferRouterOutput<T[K]> : never;
|
|
11
|
+
};
|
|
12
|
+
export type CookieOptions = {
|
|
13
|
+
maxAge?: number;
|
|
14
|
+
expires?: Date;
|
|
15
|
+
httpOnly?: boolean;
|
|
16
|
+
secure?: boolean;
|
|
17
|
+
path?: string;
|
|
18
|
+
domain?: string;
|
|
19
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
20
|
+
};
|
|
21
|
+
export type BaseContext = {
|
|
22
|
+
req: Request;
|
|
23
|
+
res: Response;
|
|
24
|
+
params: Record<string, string>;
|
|
25
|
+
publishToProcedure: <P extends Procedure<any, any, any, "subscription">>(procedure: P, input: InferInput<P["_input"]>, params: Record<string, string>) => Promise<void>;
|
|
26
|
+
setHeader: (name: string, value: string) => void;
|
|
27
|
+
setCookie: (name: string, value: string, options?: CookieOptions) => void;
|
|
28
|
+
};
|
|
29
|
+
export type Handler<C extends BaseContext, I, O> = (params: {
|
|
30
|
+
ctx: C;
|
|
31
|
+
input: I;
|
|
32
|
+
}) => Promise<O>;
|
|
33
|
+
export type InferInput<T> = T extends z.ZodType ? z.infer<T> : never;
|
|
34
|
+
export type InferOutput<T> = T extends (...args: any[]) => Promise<infer R> ? R : never;
|
|
35
|
+
export type Procedure<C extends BaseContext, I extends z.ZodType, O, P> = {
|
|
36
|
+
_input: I;
|
|
37
|
+
_output: O;
|
|
38
|
+
_ctx: C;
|
|
39
|
+
_type: P;
|
|
40
|
+
middlewares: ((ctx: C) => Promise<void>)[];
|
|
41
|
+
handler?: Handler<C, InferInput<I>, O>;
|
|
42
|
+
};
|
|
43
|
+
export type Routes = {
|
|
44
|
+
[key: string]: Procedure<any, any, any, any> | Routes;
|
|
45
|
+
};
|
|
46
|
+
export type RouterConfig<C extends BaseContext> = {
|
|
47
|
+
context: (req: Request) => Promise<C>;
|
|
48
|
+
routes: Routes;
|
|
49
|
+
globalMiddlewares?: ((ctx: C) => Promise<void>)[];
|
|
50
|
+
/**
|
|
51
|
+
* Simple hooks to debug websockets, you don't need to pass anything
|
|
52
|
+
* for brpc subscription procedures to work
|
|
53
|
+
*/
|
|
54
|
+
websocket?: {
|
|
55
|
+
onOpen?: (ws: ServerWebSocket<any>, ctx: C) => void | Promise<void>;
|
|
56
|
+
onClose?: (ws: ServerWebSocket<any>, code: number, reason: string, ctx: C) => void | Promise<void>;
|
|
57
|
+
};
|
|
58
|
+
autoFileCacheControl?: boolean;
|
|
59
|
+
prefix?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Config for third party integrations
|
|
62
|
+
*/
|
|
63
|
+
integrations?: {
|
|
64
|
+
/**
|
|
65
|
+
* better-auth integration
|
|
66
|
+
*/
|
|
67
|
+
betterAuth?: {
|
|
68
|
+
handler: (request: Request) => Promise<Response>;
|
|
69
|
+
};
|
|
70
|
+
rawRoutes?: {
|
|
71
|
+
[key: string]: ((req: BunRequest) => Promise<Response>) | {
|
|
72
|
+
GET?: (req: BunRequest) => Promise<Response>;
|
|
73
|
+
POST?: (req: BunRequest) => Promise<Response>;
|
|
74
|
+
PUT?: (req: BunRequest) => Promise<Response>;
|
|
75
|
+
DELETE?: (req: BunRequest) => Promise<Response>;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
debug?: boolean;
|
|
80
|
+
};
|
|
81
|
+
export type StreamableResponse = {
|
|
82
|
+
type: "stream";
|
|
83
|
+
file: Blob;
|
|
84
|
+
request: Request;
|
|
85
|
+
};
|
|
86
|
+
export type InferProcedureInput<T> = T extends Procedure<any, infer I, any, any> ? InferInput<I> : never;
|
|
87
|
+
export type InferProcedureOutput<T> = T extends Procedure<any, any, infer O, any> ? O : never;
|
|
88
|
+
export type InferProcedureType<T> = T extends Procedure<any, any, any, infer P> ? P : never;
|
|
89
|
+
export type ExtractRouterStructure<T extends Routes> = {
|
|
90
|
+
[K in keyof T]: T[K] extends Routes ? ExtractRouterStructure<T[K]> : T[K] extends Procedure<any, any, any, any> ? T[K] : never;
|
|
91
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mateosuarezdev/brpc",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "A Type-Safe, Flexible Web application framework for Bun",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*.js",
|
|
18
|
+
"dist/**/*.cjs",
|
|
19
|
+
"dist/**/*.d.ts",
|
|
20
|
+
"dist/**/*.map"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "yarn clean && bun run build.ts && tsc --emitDeclarationOnly",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"clean": "rimraf dist",
|
|
27
|
+
"prepublish": "yarn typecheck && yarn build",
|
|
28
|
+
"rel": "yarn npm publish --access public",
|
|
29
|
+
"rel:patch": "yarn version patch && yarn npm publish --access public",
|
|
30
|
+
"rel:minor": "yarn version minor && yarn npm publish --access public",
|
|
31
|
+
"rel:major": "yarn version major && yarn npm publish --access public"
|
|
32
|
+
},
|
|
33
|
+
"packageManager": "yarn@4.10.3",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"bun",
|
|
36
|
+
"router",
|
|
37
|
+
"brpc",
|
|
38
|
+
"rpc",
|
|
39
|
+
"trpc",
|
|
40
|
+
"api",
|
|
41
|
+
"http"
|
|
42
|
+
],
|
|
43
|
+
"author": "Mateo Suarez <msdevuy@gmail.com>",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/mateosuarezdev/brpc-space.git",
|
|
47
|
+
"directory": "packages/brpc"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/mateosuarezdev/brpc-space/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/mateosuarezdev/brpc-space/tree/main/packages/brpc#readme",
|
|
53
|
+
"license": "MIT",
|
|
54
|
+
"engines": {
|
|
55
|
+
"bun": ">=1.2.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"zod": "^3.23.8"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"zod": {
|
|
62
|
+
"optional": false
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@types/bun": "^1.2.23",
|
|
67
|
+
"rimraf": "^6.0.1",
|
|
68
|
+
"typescript": "^5.9.3",
|
|
69
|
+
"zod": "^3.23.8"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@mateosuarezdev/headers-builder": "^1.0.5"
|
|
73
|
+
}
|
|
74
|
+
}
|