@cleverbrush/server 4.4.1 → 4.4.2
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/contract.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{d as a,f as b,h as c,i as d,j as e,k as f,l as g,m as h,n as i,o as j}from"./chunk-
|
|
1
|
+
import{d as a,f as b,h as c,i as d,j as e,k as f,l as g,m as h,n as i,o as j}from"./chunk-PLSVKHPB.js";export{c as EndpointBuilder,b as SubscriptionBuilder,d as createEndpoints,g as defineApi,e as endpoint,h as mergeContracts,j as omitGroups,i as pickGroups,f as route,a as tracked};
|
|
2
2
|
//# sourceMappingURL=contract.js.map
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{a as we,b as Pe,c as Te,d as Ce,e as ae,f as xe,g as Ae,h as Be,i as Me,j as ke,k as Ee,l as _e,m as je,n as He,o as qe}from"./chunk-LWRLB46M.js";var C=class{static ok(e,t){return new P(e,200,t)}static created(e,t,r){let n={...r};return t&&(n.location=t),new P(e,201,n)}static accepted(e,t){return new P(e,202,t)}static noContent(){return new z}static badRequest(e,t){return new P(e,400,t)}static unauthorized(e,t){return new P(e,401,t)}static forbidden(e,t){return new P(e,403,t)}static notFound(e,t){return new P(e,404,t)}static conflict(e,t){return new P(e,409,t)}static redirect(e,t=!1){return new I(e,t)}static json(e,t=200,r){return new P(e,t,r)}static file(e,t,r="application/octet-stream"){return new N(e,t,r)}static content(e,t,r=200){return new O(e,t,r)}static stream(e,t,r){return new F(e,t,r)}static raw(e){return new q(e)}static status(e,t){return new D(e,t)}},q=class extends C{handler;constructor(e){super(),this.handler=e}async executeAsync(e,t,r){await this.handler(e,t)}},P=class extends C{body;status;headers;constructor(e,t=200,r){super(),this.body=e,this.status=t,this.headers=r??{}}async executeAsync(e,t,r){for(let[n,i]of Object.entries(this.headers))t.setHeader(n,i);if(this.body===null||this.body===void 0){t.writeHead(this.status),t.end();return}t.writeHead(this.status,{"content-type":"application/json"}),t.end(JSON.stringify(this.body))}},N=class extends C{content;fileName;contentType;constructor(e,t,r="application/octet-stream"){super(),this.content=e,this.fileName=t,this.contentType=r}async executeAsync(e,t,r){let n=this.fileName,i=n.replace(/[^\x20-\x7E]/g,"_"),o=i===n?`attachment; filename="${n}"`:`attachment; filename="${i}"; filename*=UTF-8''${encodeURIComponent(n)}`;t.writeHead(200,{"content-type":this.contentType,"content-disposition":o,"content-length":String(this.content.byteLength)}),t.end(this.content)}},O=class extends C{body;contentType;status;constructor(e,t,r=200){super(),this.body=e,this.contentType=t,this.status=r}async executeAsync(e,t,r){t.writeHead(this.status,{"content-type":this.contentType}),t.end(this.body)}},F=class extends C{readable;contentType;fileName;constructor(e,t,r){super(),this.readable=e,this.contentType=t,this.fileName=r}async executeAsync(e,t,r){let n={"content-type":this.contentType};this.fileName&&(n["content-disposition"]=`attachment; filename="${this.fileName}"`),t.writeHead(200,n),await new Promise((i,o)=>{this.readable.on("error",o),t.on("error",o),this.readable.on("end",i),this.readable.pipe(t,{end:!0})})}},D=class extends C{status;headers;constructor(e,t){super(),this.status=e,this.headers=t??{}}async executeAsync(e,t,r){t.writeHead(this.status,this.headers),t.end()}},I=class extends C{url;permanent;constructor(e,t=!1){super(),this.url=e,this.permanent=t}async executeAsync(e,t,r){t.writeHead(this.permanent?301:302,{location:this.url}),t.end()}},z=class extends C{async executeAsync(e,t,r){t.writeHead(204),t.end()}};function x(s){return JSON.parse(s,(e,t)=>{if(!(e==="__proto__"||e==="constructor"))return t})}function A(s,e=64){K(s,0,e)}function K(s,e,t){if(!(s===null||typeof s!="object")){if(e>=t)throw new Error(`JSON nesting depth exceeds maximum of ${t}`);if(Array.isArray(s))for(let r of s)K(r,e+1,t);else for(let r of Object.values(s))K(r,e+1,t)}}var de={mimeType:"application/json",serialize(s){return JSON.stringify(s)},deserialize(s){let e=x(s);return A(e),e}},ce={mimeType:"application/x-www-form-urlencoded",serialize(s){let e=new URLSearchParams;if(s&&typeof s=="object"){for(let[t,r]of Object.entries(s))if(r!=null)if(Array.isArray(r))for(let n of r)e.append(t,String(n));else e.append(t,String(r))}return e.toString()},deserialize(s){let e={},t=new URLSearchParams(s);for(let[r,n]of t){if(Ne(r))continue;let i=e[r];i===void 0?e[r]=n:Array.isArray(i)?i.push(n):e[r]=[i,n]}return e}};function Ne(s){return s==="__proto__"||s==="constructor"||s==="prototype"}function Oe(s){return s.split(",").map(e=>{let t=e.trim(),[r,...n]=t.split(";").map(o=>o.trim()),i=1;for(let o of n){let[c,d]=o.split("=");c?.trim()==="q"&&d&&(i=parseFloat(d),Number.isNaN(i)&&(i=1))}return{mimeType:r.toLowerCase(),quality:i}}).sort((e,t)=>t.quality-e.quality)}var j=class{#e=new Map;constructor(){this.register(de),this.register(ce)}register(e){this.#e.set(e.mimeType.toLowerCase(),e)}selectResponseHandler(e){if(!e)return this.#e.get("application/json")??null;let t=Oe(e);for(let{mimeType:r}of t){if(r==="*/*")return this.#e.get("application/json")??null;let n=this.#e.get(r);if(n)return n}return null}selectRequestHandler(e){if(!e)return null;let t=e.split(";")[0].trim().toLowerCase();return this.#e.get(t)??null}};var Fe={400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",409:"Conflict",415:"Unsupported Media Type",422:"Unprocessable Content",500:"Internal Server Error",503:"Service Unavailable"};function R(s,e,t,r){return{type:`https://httpstatuses.com/${s}`,status:s,title:e??Fe[s]??"Error",...t!==void 0?{detail:t}:{},...r}}function X(s){return R(400,"Bad Request","One or more validation errors occurred.",{errors:s})}function v(s){return JSON.stringify(s)}var w="application/problem+json";var T=class extends Error{status;title;detail;extensions;constructor(e,t,r,n){super(r??t??`HTTP ${e}`),this.name="HttpError",this.status=e,this.title=t??`HTTP ${e}`,this.detail=r,this.extensions=n}toProblemDetails(){return R(this.status,this.title,this.detail,this.extensions)}},Y=class extends T{constructor(e){super(404,"Not Found",e),this.name="NotFoundError"}},Z=class extends T{constructor(e){super(400,"Bad Request",e),this.name="BadRequestError"}},Q=class extends T{constructor(e){super(401,"Unauthorized",e),this.name="UnauthorizedError"}},ee=class extends T{constructor(e){super(403,"Forbidden",e),this.name="ForbiddenError"}},te=class extends T{constructor(e){super(409,"Conflict",e),this.name="ConflictError"}};function De(s){return["POST","PUT","DELETE","PATCH"].includes(s.toUpperCase())}var Ie=6e4;function ze(s={}){let{ttl:e=864e5,headerName:t="x-idempotency-key",skip:r=o=>!De(o.method)}=s,n=new Map,i=setInterval(()=>{let o=Date.now();for(let[c,d]of n)d.expiresAt<=o&&n.delete(c)},Ie);return i.unref&&i.unref(),async(o,c)=>{if(r(o))return c();let d=o.headers[t]??o.headers[t.toLowerCase()];if(!d||typeof d!="string"||d.length===0)return c();let a=n.get(d);if(a)if(a.expiresAt<=Date.now())n.delete(d);else{let u=o.response;u.writeHead(a.status,a.headers),u.end(a.body),o.responded=!0;return}let f=o.response.writeHead.bind(o.response),m=o.response.end.bind(o.response),p=200,y={},h=[];if(o.response.writeHead=function(u,...l){return p=u,l.length>0&&(y=l[0]),f(u,...l)},o.response.end=function(u,...l){return u&&h.push(Buffer.isBuffer(u)?u:Buffer.from(u)),m(u,...l)},await c(),p>=200&&p<500){let u=Buffer.concat(h);n.set(d,{status:p,headers:y,body:u,expiresAt:Date.now()+e})}}}function Je(s){return["POST","PUT","DELETE","PATCH"].includes(s.toUpperCase())}function le(s,e){return s.map(t=>{let r=[];for(let[n,i]of Object.entries(t.properties).sort(([o],[c])=>o.localeCompare(c))){let o=i.getValue(e);o.success&&o.value!==void 0&&r.push(`${n}=${String(o.value)}`)}return r.length>0?`${t.name}:${r.join(",")}`:t.name})}function Ue(s={}){let{ttlByTag:e={},defaultTtl:t=6e4}=s,r=new Map;return async(n,i)=>{let c=n.items.get("__endpoint_meta")?.cacheTags??[];if(c.length===0)return i();if(Je(n.method)){if(await i(),n.response.statusCode>=200&&n.response.statusCode<300){let d=n.items.get("__raw_body"),a={params:n.pathParams??{},body:d,query:n.queryParams??{},headers:n.headers??{}},f=le(c,a);for(let m of c){for(let[p]of r)(f.includes(p)||f.some(y=>p.startsWith(m.name)))&&r.delete(p);for(let[p]of r)p.startsWith(m.name)&&r.delete(p)}}return}if(n.method==="GET"){let d={params:n.pathParams??{},body:void 0,query:n.queryParams??{},headers:n.headers??{}},a=le(c,d);for(let u of a){let l=r.get(u);if(l&&l.expiresAt>Date.now()){let g=n.response;g.writeHead(l.status,l.headers),g.end(l.body),n.responded=!0;return}l&&r.delete(u)}let f=n.response.writeHead.bind(n.response),m=n.response.end.bind(n.response),p=200,y={},h=[];if(n.response.writeHead=function(u,...l){return p=u,l.length>0&&(y=l[0]),f(u,...l)},n.response.end=function(u,...l){return u&&h.push(Buffer.isBuffer(u)?u:Buffer.from(u)),m(u,...l)},await i(),p>=200&&p<300){let u=Buffer.concat(h),l=c.reduce((g,S)=>{let b=e[S.name]!==void 0?e[S.name]:t;return b>g?b:g},0);if(l>0)for(let g of a)r.set(g,{status:p,headers:y,body:u,expiresAt:Date.now()+l})}return}return i()}}import{URL as We}from"url";import{any as re,boolean as Le,func as ue,object as $e,promise as pe,record as ne,string as B}from"@cleverbrush/schema";var Ve=$e({method:B(),url:B(),pathParams:ne(B(),B()),queryParams:ne(B(),B()),headers:ne(B(),B()),items:re(),body:ue().hasReturnType(pe(re())),json:ue().hasReturnType(pe(re())),responded:Le()}),H=5*1024*1024,k=class{request;response;url;method;headers;items=new Map;maxBodySize;#e={};_queryParams;#t;#r=null;#s=!1;#o=void 0;#d=!1;responded=!1;principal=void 0;constructor(e,t,r){this.request=e,this.response=t,this.method=(e.method??"GET").toUpperCase(),this.maxBodySize=r??H;let n=e.url??"/";this.url=new We(n,`http://${e.headers.host??"localhost"}`);let i={};for(let[o,c]of Object.entries(e.headers))typeof c=="string"?i[o]=c:Array.isArray(c)&&(i[o]=c.join(", "));this.headers=i}get pathParams(){return this.#e}set pathParams(e){this.#e=e}get queryParams(){if(this._queryParams)return this._queryParams;let e={};for(let[t,r]of this.url.searchParams)e[t]=r;return e}get services(){return this.#t}set services(e){this.#t=e}async body(){return this.#s?this.#r:(this.#r=await new Promise((e,t)=>{let r=[],n=0;this.request.on("data",i=>{if(n+=i.length,n>this.maxBodySize){this.request.destroy(),t(new T(413,"Payload Too Large"));return}r.push(i)}),this.request.on("end",()=>e(Buffer.concat(r))),this.request.on("error",t)}),this.#s=!0,this.#r)}async json(){if(this.#d)return this.#o;let t=(await this.body()).toString("utf-8");return t.length>0&&(this.#o=x(t),A(this.#o)),this.#d=!0,this.#o}};import*as G from"http";import*as Se from"https";import{AuthorizationService as Xe,PolicyBuilder as Ye,Principal as L,parseCookies as Ze,requireRole as Qe}from"@cleverbrush/auth";import{ServiceCollection as et}from"@cleverbrush/di";import{Busboy as tt}from"@fastify/busboy";import{WebSocketServer as rt}from"ws";var E=class{#e=[];add(e){this.#e.push(e)}async execute(e,t){let r=0,n=async()=>{if(r<this.#e.length){let i=this.#e[r++];await i(e,n)}else await t()};await n()}};function he(s){return s.bodySchema!=null}async function fe(s,e,t,r){let n=[],i={};if(i.context=t,s.authRoles!==null&&t.principal!==void 0&&(i.principal=t.principal),e&&Object.keys(e).length>0&&(i.params=e),s.bodySchema){let o=await s.bodySchema.validateAsync(r,{doNotStopOnFirstError:!0});if(o.valid)i.body=o.object;else{let c=typeof o.getInvalidProperties=="function"?o.getInvalidProperties:null,d=!1;if(c)for(let a of c()){let f=a.descriptor.toJsonPointer();for(let m of a.errors)n.push({pointer:`/body${f}`,detail:m}),d=!0}if(!d)for(let a of o.errors??[])n.push({pointer:"/body",detail:a.message})}}if(s.querySchema){let o=s.querySchema.introspect();if(o.type!=="object"||!o.properties)throw new Error("Endpoint query schema must be an object schema whose properties map to query parameter names.");let c=o.properties,d={};for(let[a,f]of Object.entries(c)){let m=t.queryParams[a],p=await f.validateAsync(m,{doNotStopOnFirstError:!0});if(p.valid)d[a]=p.object;else for(let y of p.errors??[])n.push({pointer:`/query/${a}`,detail:y.message})}i.query=d}if(s.headerSchema){let o=s.headerSchema.introspect();if(o.type!=="object"||!o.properties)throw new Error("Endpoint headers schema must be an object schema whose properties map to header names.");let c=o.properties,d={};for(let[a,f]of Object.entries(c)){let m=t.headers[a.toLowerCase()],p=await f.validateAsync(m,{doNotStopOnFirstError:!0});if(p.valid)d[a]=p.object;else for(let y of p.errors??[])n.push({pointer:`/headers/${a}`,detail:y.message})}i.headers=d}if(n.length>0)return{valid:!1,problemDetails:X(n)};if(s.serviceSchemas){if(!t.services)throw new Error("Endpoint declares .inject() dependencies but no service provider is available. Register services via createServer().services() before handling this endpoint.");let o={};for(let[c,d]of Object.entries(s.serviceSchemas))o[c]=t.services.get(d);return{valid:!0,args:[i,o]}}return{valid:!0,args:[i]}}function _(s){let e=decodeURI(s);return e.length>1&&e.endsWith("/")?e.slice(0,-1):e}function me(s){return typeof s!="string"&&typeof s.validate=="function"}var J=class{#e=new Map;#t=[];addRoute(e){let{method:t,basePath:r,pathTemplate:n}=e.endpoint,i=t.toUpperCase(),c={basePath:_(r),routePath:n,registration:e};this.#e.has(i)||this.#e.set(i,[]),this.#e.get(i).push(c)}match(e,t){let r;try{r=_(t)}catch{return{match:null,methodNotAllowed:!1,badRequest:!0}}let n=e.toUpperCase(),i=this.#e.get(n);if(i)for(let c of i){let d=this.#r(c,r);if(d)return{match:d,methodNotAllowed:!1}}let o=[];for(let[c,d]of this.#e)if(c!==n){for(let a of d)if(this.#r(a,r)){o.push(c);break}}return o.length>0?{match:null,methodNotAllowed:!0,allowedMethods:o}:{match:null,methodNotAllowed:!1}}#r(e,t){let{basePath:r,routePath:n}=e;if(r&&!t.startsWith(r))return null;let i=r?t.slice(r.length):t;if(me(n)){let d=n.validate(i);return d.valid?{registration:e.registration,parsedPath:d.object}:null}let o=_(n);return(i.length===0?"/":i)===o?{registration:e.registration,parsedPath:null}:null}addSubscriptionRoute(e){let{basePath:t,pathTemplate:r}=e.endpoint,n=_(t);this.#t.push({basePath:n,routePath:r,registration:e})}matchSubscription(e){let t;try{t=_(e)}catch{return null}for(let r of this.#t){let{basePath:n,routePath:i}=r;if(n&&!t.startsWith(n))continue;let o=n?t.slice(n.length):t;if(me(i)){let a=i.validate(o);if(a.valid)return{registration:r.registration,parsedPath:a.object};continue}let c=_(i);if((o.length===0?"/":o)===c)return{registration:r.registration,parsedPath:null}}return null}};import{Readable as Ge,Writable as Ke}from"stream";var U=class extends Ge{method;url;headers;__cleverbrushBatchSubrequest;socket=null;#e;#t=!1;constructor(e){super(),this.method=e.method,this.url=e.url;let t={};for(let[r,n]of Object.entries(e.headers??{}))t[r.toLowerCase()]=n;this.headers={host:"localhost",...t},this.#e=e.body!=null&&e.body.length>0?Buffer.from(e.body,"utf-8"):Buffer.alloc(0)}_read(){this.#t||(this.#t=!0,this.#e.length>0&&this.push(this.#e),this.push(null))}},W=class extends Ke{statusCode=200;headersSent=!1;#e=[];#t={};#r=200;_write(e,t,r){this.#e.push(Buffer.isBuffer(e)?e:Buffer.from(e)),r()}writeHead(e,t){if(this.#r=e,this.statusCode=e,t!=null&&typeof t=="object"&&!Array.isArray(t))for(let[r,n]of Object.entries(t))this.#t[r.toLowerCase()]=Array.isArray(n)?n.join(", "):String(n);return this.headersSent=!0,this}setHeader(e,t){return this.#t[e.toLowerCase()]=Array.isArray(t)?t.join(", "):String(t),this}getHeader(e){return this.#t[e.toLowerCase()]}end(e,...t){if(e!=null&&typeof e!="function"){let r=Buffer.isBuffer(e)||e instanceof Uint8Array?Buffer.from(e):Buffer.from(String(e),"utf-8");this.#e.push(r),super.end(...t)}else typeof e=="function"?super.end(e):super.end(...t);return this}toResult(){return{status:this.#r,headers:{...this.#t},body:Buffer.concat(this.#e).toString("utf-8")}}};function ye(s){return{type:"message",data:s}}function ge(s,e){return{type:"tracked",id:s,data:e}}function be(){return{type:"pong"}}function M(s,e){return{type:"error",code:s,message:e}}function Re(s){let e;try{e=x(s),A(e)}catch{return null}if(typeof e!="object"||e===null)return null;let t=e;return t.type==="ping"?{type:"ping"}:t.type==="message"&&"data"in t?{type:"message",data:t.data}:null}async function nt(s,e){let t=e.maxFileCount??10,r=e.maxFileSize??10*1024*1024,n=e.allowedMimeTypes;return new Promise((i,o)=>{let c={},d={},a=[],f=0,m=tt({headers:s.headers,limits:{fileSize:r,files:t}});m.on("field",(p,y)=>{c[p]=y}),m.on("file",(p,y,h,u,l)=>{if(f>=t){a.push({filename:h,mimeType:l,reason:`Exceeded max file count (${t})`}),y.resume();return}if(n&&!n.some(b=>b.endsWith("/*")?l.startsWith(b.slice(0,-1)):l===b)){a.push({filename:h,mimeType:l,reason:`MIME type "${l}" not allowed (allowed: ${n.join(", ")})`}),y.resume();return}f++;let g=[];y.on("data",S=>{g.push(S)}),y.on("end",()=>{let S=Buffer.concat(g);d[p]={filename:h,mimeType:l,buffer:S,size:S.length}}),y.on("error",o)}),m.on("error",o),m.on("finish",()=>i({fields:c,files:d,rejectedFiles:a})),s.pipe(m)})}var $=class{#e=new et;#t=[];#r=[];#s=[];#o=[];#d=new j;#i;#a=null;#n=null;#c=!1;#l=null;constructor(e={}){this.#i=e}services(e){return e(this.#e),this}use(e){return this.#o.push(e),this}contentType(e){return this.#d.register(e),this}useAuthentication(e){return this.#a=e,this}useAuthorization(e){return this.#n=e??{},this}withHealthcheck(){return this.#c=!0,this}useBatching(e={}){return this.#l=e,this}handle(e,t,r){return this.#t.push({endpoint:e.introspect(),handler:t,middlewares:r?.middlewares}),this}handleAll(e){for(let t of e._entries)this.#t.push({endpoint:t.endpoint.introspect(),handler:t.handler,middlewares:t.middlewares});for(let t of e._subscriptions)this.#r.push({endpoint:t.endpoint.introspect(),handler:t.handler,middlewares:t.middlewares});return this}getRegistrations(){return[...this.#t]}getSubscriptionRegistrations(){return[...this.#r]}webhook(e){return this.#s.push(e),this}getWebhooks(){return[...this.#s]}getAuthenticationConfig(){return this.#a}async listen(e,t){let r=new J;for(let f of this.#t)r.addRoute(f);for(let f of this.#r)r.addSubscriptionRoute(f);let n=this.#e.buildServiceProvider({validateScopes:!1}),i=[];if(this.#a&&i.push(at(this.#a)),this.#n!==null){let f=new Map;if(this.#n.policies)for(let[p,y]of Object.entries(this.#n.policies)){let h=new Ye;y(h),f.set(p,h.build(p))}let m=new Xe(f);i.push(dt(m,this.#a))}let o=[...i,...this.#o],c=new V(r,n,this.#d,o,this.#c,this.#r.length>0,this.#l,this.#i.maxBodySize),d=e??this.#i.port??3e3,a=t??this.#i.host??"0.0.0.0";return await c.start(d,a,this.#i),c}},st=1024,V=class{#e;#t;#r;#s;#o;#d;#i;#a;#n=null;#c=null;#l=new Set;constructor(e,t,r,n,i=!1,o=!1,c=null,d=H){this.#e=e,this.#t=t,this.#r=r,this.#s=n,this.#o=i,this.#d=o,this.#i=c,this.#a=d}async start(e,t,r){let n=(i,o)=>{this.#u(i,o).catch(c=>{o.headersSent||(o.writeHead(500,{"content-type":w}),o.end(v(R(500))))})};r.https?this.#n=Se.createServer({key:r.https.key,cert:r.https.cert},n):this.#n=G.createServer(n),await new Promise(i=>{this.#n.listen(e,t,i)}),this.#d&&(this.#c=new rt({noServer:!0,maxPayload:this.#a}),this.#n.on("upgrade",(i,o,c)=>{let d=new URL(i.url??"/",`http://${i.headers.host??"localhost"}`).pathname,a=this.#e.matchSubscription(d);if(!a){o.write(`HTTP/1.1 404 Not Found\r
|
|
1
|
+
import{a as xe,b as Ae,c as Me,d as Be,e as de,f as Ee,g as ke,h as _e,i as je,j as He,k as qe,l as Ne,m as Oe,n as Fe,o as De}from"./chunk-PLSVKHPB.js";var C=class{static ok(e,t){return new P(e,200,t)}static created(e,t,r){let n={...r};return t&&(n.location=t),new P(e,201,n)}static accepted(e,t){return new P(e,202,t)}static noContent(){return new z}static badRequest(e,t){return new P(e,400,t)}static unauthorized(e,t){return new P(e,401,t)}static forbidden(e,t){return new P(e,403,t)}static notFound(e,t){return new P(e,404,t)}static conflict(e,t){return new P(e,409,t)}static redirect(e,t=!1){return new I(e,t)}static json(e,t=200,r){return new P(e,t,r)}static file(e,t,r="application/octet-stream"){return new N(e,t,r)}static content(e,t,r=200){return new O(e,t,r)}static stream(e,t,r){return new F(e,t,r)}static raw(e){return new q(e)}static status(e,t){return new D(e,t)}},q=class extends C{handler;constructor(e){super(),this.handler=e}async executeAsync(e,t,r){await this.handler(e,t)}},P=class extends C{body;status;headers;constructor(e,t=200,r){super(),this.body=e,this.status=t,this.headers=r??{}}async executeAsync(e,t,r){for(let[n,o]of Object.entries(this.headers))t.setHeader(n,o);if(this.body===null||this.body===void 0){t.writeHead(this.status),t.end();return}t.writeHead(this.status,{"content-type":"application/json"}),t.end(JSON.stringify(this.body))}},N=class extends C{content;fileName;contentType;constructor(e,t,r="application/octet-stream"){super(),this.content=e,this.fileName=t,this.contentType=r}async executeAsync(e,t,r){let n=this.fileName,o=n.replace(/[^\x20-\x7E]/g,"_"),i=o===n?`attachment; filename="${n}"`:`attachment; filename="${o}"; filename*=UTF-8''${encodeURIComponent(n)}`;t.writeHead(200,{"content-type":this.contentType,"content-disposition":i,"content-length":String(this.content.byteLength)}),t.end(this.content)}},O=class extends C{body;contentType;status;constructor(e,t,r=200){super(),this.body=e,this.contentType=t,this.status=r}async executeAsync(e,t,r){t.writeHead(this.status,{"content-type":this.contentType}),t.end(this.body)}},F=class extends C{readable;contentType;fileName;constructor(e,t,r){super(),this.readable=e,this.contentType=t,this.fileName=r}async executeAsync(e,t,r){let n={"content-type":this.contentType};this.fileName&&(n["content-disposition"]=`attachment; filename="${this.fileName}"`),t.writeHead(200,n),await new Promise((o,i)=>{this.readable.on("error",i),t.on("error",i),this.readable.on("end",o),this.readable.pipe(t,{end:!0})})}},D=class extends C{status;headers;constructor(e,t){super(),this.status=e,this.headers=t??{}}async executeAsync(e,t,r){t.writeHead(this.status,this.headers),t.end()}},I=class extends C{url;permanent;constructor(e,t=!1){super(),this.url=e,this.permanent=t}async executeAsync(e,t,r){t.writeHead(this.permanent?301:302,{location:this.url}),t.end()}},z=class extends C{async executeAsync(e,t,r){t.writeHead(204),t.end()}};function x(s){return JSON.parse(s,(e,t)=>{if(!(e==="__proto__"||e==="constructor"))return t})}function A(s,e=64){K(s,0,e)}function K(s,e,t){if(!(s===null||typeof s!="object")){if(e>=t)throw new Error(`JSON nesting depth exceeds maximum of ${t}`);if(Array.isArray(s))for(let r of s)K(r,e+1,t);else for(let r of Object.values(s))K(r,e+1,t)}}var ce={mimeType:"application/json",serialize(s){return JSON.stringify(s)},deserialize(s){let e=x(s);return A(e),e}},le={mimeType:"application/x-www-form-urlencoded",serialize(s){let e=new URLSearchParams;if(s&&typeof s=="object"){for(let[t,r]of Object.entries(s))if(r!=null)if(Array.isArray(r))for(let n of r)e.append(t,String(n));else e.append(t,String(r))}return e.toString()},deserialize(s){let e={},t=new URLSearchParams(s);for(let[r,n]of t){if(Ie(r))continue;let o=e[r];o===void 0?e[r]=n:Array.isArray(o)?o.push(n):e[r]=[o,n]}return e}};function Ie(s){return s==="__proto__"||s==="constructor"||s==="prototype"}function ze(s){return s.split(",").map(e=>{let t=e.trim(),[r,...n]=t.split(";").map(i=>i.trim()),o=1;for(let i of n){let[d,c]=i.split("=");d?.trim()==="q"&&c&&(o=parseFloat(c),Number.isNaN(o)&&(o=1))}return{mimeType:r.toLowerCase(),quality:o}}).sort((e,t)=>t.quality-e.quality)}var j=class{#e=new Map;constructor(){this.register(ce),this.register(le)}register(e){this.#e.set(e.mimeType.toLowerCase(),e)}selectResponseHandler(e){if(!e)return this.#e.get("application/json")??null;let t=ze(e);for(let{mimeType:r}of t){if(r==="*/*")return this.#e.get("application/json")??null;let n=this.#e.get(r);if(n)return n}return null}selectRequestHandler(e){if(!e)return null;let t=e.split(";")[0].trim().toLowerCase();return this.#e.get(t)??null}};var Je={400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",409:"Conflict",415:"Unsupported Media Type",422:"Unprocessable Content",500:"Internal Server Error",503:"Service Unavailable"};function S(s,e,t,r){return{type:`https://httpstatuses.com/${s}`,status:s,title:e??Je[s]??"Error",...t!==void 0?{detail:t}:{},...r}}function X(s){return S(400,"Bad Request","One or more validation errors occurred.",{errors:s})}function v(s){return JSON.stringify(s)}var w="application/problem+json";var T=class extends Error{status;title;detail;extensions;constructor(e,t,r,n){super(r??t??`HTTP ${e}`),this.name="HttpError",this.status=e,this.title=t??`HTTP ${e}`,this.detail=r,this.extensions=n}toProblemDetails(){return S(this.status,this.title,this.detail,this.extensions)}},Y=class extends T{constructor(e){super(404,"Not Found",e),this.name="NotFoundError"}},Z=class extends T{constructor(e){super(400,"Bad Request",e),this.name="BadRequestError"}},Q=class extends T{constructor(e){super(401,"Unauthorized",e),this.name="UnauthorizedError"}},ee=class extends T{constructor(e){super(403,"Forbidden",e),this.name="ForbiddenError"}},te=class extends T{constructor(e){super(409,"Conflict",e),this.name="ConflictError"}};function Ue(s){return["POST","PUT","DELETE","PATCH"].includes(s.toUpperCase())}var Le=6e4;function We(s={}){let{ttl:e=864e5,headerName:t="x-idempotency-key",skip:r=i=>!Ue(i.method)}=s,n=new Map,o=setInterval(()=>{let i=Date.now();for(let[d,c]of n)c.expiresAt<=i&&n.delete(d)},Le);return o.unref&&o.unref(),async(i,d)=>{if(r(i))return d();let c=i.headers[t]??i.headers[t.toLowerCase()];if(!c||typeof c!="string"||c.length===0)return d();let a=n.get(c);if(a)if(a.expiresAt<=Date.now())n.delete(c);else{let u=i.response;u.writeHead(a.status,a.headers),u.end(a.body),i.responded=!0;return}let f=i.response.writeHead.bind(i.response),m=i.response.end.bind(i.response),p=200,g={},h=[];if(i.response.writeHead=function(u,...l){return p=u,l.length>0&&(g=l[0]),f(u,...l)},i.response.end=function(u,...l){return u&&h.push(Buffer.isBuffer(u)?u:Buffer.from(u)),m(u,...l)},await d(),p>=200&&p<500){let u=Buffer.concat(h);n.set(c,{status:p,headers:g,body:u,expiresAt:Date.now()+e})}}}function $e(s){return["POST","PUT","DELETE","PATCH"].includes(s.toUpperCase())}function ue(s,e){return s.map(t=>{let r=[];for(let[n,o]of Object.entries(t.properties).sort(([i],[d])=>i.localeCompare(d))){let i=o.getValue(e);i.success&&i.value!==void 0&&r.push(`${n}=${String(i.value)}`)}return r.length>0?`${t.name}:${r.join(",")}`:t.name})}function Ve(s={}){let{ttlByTag:e={},defaultTtl:t=6e4}=s,r=new Map;return async(n,o)=>{let d=n.items.get("__endpoint_meta")?.cacheTags??[];if(d.length===0)return o();if($e(n.method)){if(await o(),n.response.statusCode>=200&&n.response.statusCode<300){let c=n.items.get("__raw_body"),a={params:n.pathParams??{},body:c,query:n.queryParams??{},headers:n.headers??{}},f=ue(d,a);for(let m of d){for(let[p]of r)(f.includes(p)||f.some(g=>p.startsWith(m.name)))&&r.delete(p);for(let[p]of r)p.startsWith(m.name)&&r.delete(p)}}return}if(n.method==="GET"){let c={params:n.pathParams??{},body:void 0,query:n.queryParams??{},headers:n.headers??{}},a=ue(d,c);for(let u of a){let l=r.get(u);if(l&&l.expiresAt>Date.now()){let y=n.response;y.writeHead(l.status,l.headers),y.end(l.body),n.responded=!0;return}l&&r.delete(u)}let f=n.response.writeHead.bind(n.response),m=n.response.end.bind(n.response),p=200,g={},h=[];if(n.response.writeHead=function(u,...l){return p=u,l.length>0&&(g=l[0]),f(u,...l)},n.response.end=function(u,...l){return u&&h.push(Buffer.isBuffer(u)?u:Buffer.from(u)),m(u,...l)},await o(),p>=200&&p<300){let u=Buffer.concat(h),l=d.reduce((y,b)=>{let R=e[b.name]!==void 0?e[b.name]:t;return R>y?R:y},0);if(l>0)for(let y of a)r.set(y,{status:p,headers:g,body:u,expiresAt:Date.now()+l})}return}return o()}}import{URL as Ge}from"url";import{any as re,boolean as Ke,func as pe,object as Xe,promise as he,record as ne,string as M}from"@cleverbrush/schema";var Ye=Xe({method:M(),url:M(),pathParams:ne(M(),M()),queryParams:ne(M(),M()),headers:ne(M(),M()),items:re(),body:pe().hasReturnType(he(re())),json:pe().hasReturnType(he(re())),responded:Ke()}),H=5*1024*1024,E=class{request;response;url;method;headers;items=new Map;maxBodySize;#e={};_queryParams;#t;#r=null;#n=!1;#s=void 0;#i=!1;responded=!1;principal=void 0;constructor(e,t,r){this.request=e,this.response=t,this.method=(e.method??"GET").toUpperCase(),this.maxBodySize=r??H;let n=e.url??"/";this.url=new Ge(n,`http://${e.headers.host??"localhost"}`);let o={};for(let[i,d]of Object.entries(e.headers))typeof d=="string"?o[i]=d:Array.isArray(d)&&(o[i]=d.join(", "));this.headers=o}get pathParams(){return this.#e}set pathParams(e){this.#e=e}get queryParams(){if(this._queryParams)return this._queryParams;let e={};for(let[t,r]of this.url.searchParams)e[t]=r;return e}get services(){return this.#t}set services(e){this.#t=e}async body(){return this.#n?this.#r:(this.#r=await new Promise((e,t)=>{let r=[],n=0;this.request.on("data",o=>{if(n+=o.length,n>this.maxBodySize){this.request.destroy(),t(new T(413,"Payload Too Large"));return}r.push(o)}),this.request.on("end",()=>e(Buffer.concat(r))),this.request.on("error",t)}),this.#n=!0,this.#r)}async json(){if(this.#i)return this.#s;let t=(await this.body()).toString("utf-8");return t.length>0&&(this.#s=x(t),A(this.#s)),this.#i=!0,this.#s}};import*as G from"http";import*as Te from"https";import{AuthorizationService as nt,PolicyBuilder as st,Principal as W,parseCookies as ot,requireRole as it}from"@cleverbrush/auth";import{ServiceCollection as at}from"@cleverbrush/di";import{Busboy as dt}from"@fastify/busboy";import{WebSocketServer as ct}from"ws";var k=class{#e=[];add(e){this.#e.push(e)}async execute(e,t){let r=0,n=async()=>{if(r<this.#e.length){let o=this.#e[r++];await o(e,n)}else await t()};await n()}};function fe(s){return s.bodySchema!=null}async function me(s,e,t,r){let n=[],o={};if(o.context=t,s.authRoles!==null&&t.principal!==void 0&&(o.principal=t.principal),e&&Object.keys(e).length>0&&(o.params=e),s.bodySchema){let i=await s.bodySchema.validateAsync(r,{doNotStopOnFirstError:!0});if(i.valid)o.body=i.object;else{let d=typeof i.getInvalidProperties=="function"?i.getInvalidProperties:null,c=!1;if(d)for(let a of d()){let f=a.descriptor.toJsonPointer();for(let m of a.errors)n.push({pointer:`/body${f}`,detail:m}),c=!0}if(!c)for(let a of i.errors??[])n.push({pointer:"/body",detail:a.message})}}if(s.querySchema){let i=s.querySchema.introspect();if(i.type!=="object"||!i.properties)throw new Error("Endpoint query schema must be an object schema whose properties map to query parameter names.");let d=i.properties,c={};for(let[a,f]of Object.entries(d)){let m=t.queryParams[a],p=await f.validateAsync(m,{doNotStopOnFirstError:!0});if(p.valid)c[a]=p.object;else for(let g of p.errors??[])n.push({pointer:`/query/${a}`,detail:g.message})}o.query=c}if(s.headerSchema){let i=s.headerSchema.introspect();if(i.type!=="object"||!i.properties)throw new Error("Endpoint headers schema must be an object schema whose properties map to header names.");let d=i.properties,c={};for(let[a,f]of Object.entries(d)){let m=t.headers[a.toLowerCase()],p=await f.validateAsync(m,{doNotStopOnFirstError:!0});if(p.valid)c[a]=p.object;else for(let g of p.errors??[])n.push({pointer:`/headers/${a}`,detail:g.message})}o.headers=c}if(n.length>0)return{valid:!1,problemDetails:X(n)};if(s.serviceSchemas){if(!t.services)throw new Error("Endpoint declares .inject() dependencies but no service provider is available. Register services via createServer().services() before handling this endpoint.");let i={};for(let[d,c]of Object.entries(s.serviceSchemas))i[d]=t.services.get(c);return{valid:!0,args:[o,i]}}return{valid:!0,args:[o]}}function _(s){let e=decodeURI(s);return e.length>1&&e.endsWith("/")?e.slice(0,-1):e}function se(s){return typeof s!="string"&&typeof s.validate=="function"}function ge(s){let e=0,t=!1;for(let r of s)for(let n of r)n==="/"?(t&&e++,t=!1):t=!0;return e+(t?1:0)}function Ze(s,e){if(!se(e)){let o=_(e),i=o==="/"?s||"/":s+o;return{isStatic:!0,literalSegments:ge([i]),dynamicSegments:0}}let{literals:t,segments:r}=e.introspect().templateDefinition,n=[s+(t[0]??""),...t.slice(1)];return{isStatic:r.length===0,literalSegments:ge(n),dynamicSegments:r.length}}function Qe(s,e){return s.isStatic!==e.isStatic?s.isStatic?1:-1:s.literalSegments!==e.literalSegments?s.literalSegments-e.literalSegments:e.dynamicSegments-s.dynamicSegments}function et(s,e){if(!se(e)){let n=_(e);return n==="/"?s||"/":s+n}let{literals:t,segments:r}=e.introspect().templateDefinition;return r.length>0?null:s+(t[0]??"")}function ye(s,e,t){let r=_(s);return{basePath:r,routePath:e,specificity:Ze(r,e),exactPath:et(r,e),registration:t}}function Re(){return{exactRoutes:new Map,dynamicRoutes:[]}}function Se(s,e){if(e.exactPath!==null){s.exactRoutes.has(e.exactPath)||s.exactRoutes.set(e.exactPath,e);return}s.dynamicRoutes.push(e)}var J=class{#e=new Map;#t=Re();#r=!0;addRoute(e){let{method:t,basePath:r,pathTemplate:n}=e.endpoint,o=t.toUpperCase(),i=ye(r,n,e),d=this.#e.get(o);d||(d=Re(),this.#e.set(o,d)),Se(d,i),this.#r=!1}finalize(){if(!this.#r){for(let e of this.#e.values())this.#s(e.dynamicRoutes);this.#s(this.#t.dynamicRoutes),this.#r=!0}}match(e,t){let r;try{r=_(t)}catch{return{match:null,methodNotAllowed:!1,badRequest:!0}}let n=e.toUpperCase();this.finalize();let o=this.#e.get(n);if(o){let d=this.#n(o,r);if(d)return{match:d,methodNotAllowed:!1}}let i=[];for(let[d,c]of this.#e)d!==n&&this.#n(c,r)&&i.push(d);return i.length>0?{match:null,methodNotAllowed:!0,allowedMethods:i}:{match:null,methodNotAllowed:!1}}#n(e,t){let r=e.exactRoutes.get(t);if(r){let n=this.#i(r,t);if(n)return n}for(let n of e.dynamicRoutes){let o=this.#i(n,t);if(o)return o}return null}#s(e){e.sort((t,r)=>Qe(r.specificity,t.specificity))}#i(e,t){let{basePath:r,routePath:n}=e;if(r&&!t.startsWith(r))return null;let o=r?t.slice(r.length):t;if(se(n)){let c=n.validate(o);return c.valid?{registration:e.registration,parsedPath:c.object}:null}let i=_(n);return(o.length===0?"/":o)===i?{registration:e.registration,parsedPath:null}:null}addSubscriptionRoute(e){let{basePath:t,pathTemplate:r}=e.endpoint;Se(this.#t,ye(t,r,e)),this.#r=!1}matchSubscription(e){let t;try{t=_(e)}catch{return null}return this.finalize(),this.#n(this.#t,t)}};import{Readable as tt,Writable as rt}from"stream";var U=class extends tt{method;url;headers;__cleverbrushBatchSubrequest;socket=null;#e;#t=!1;constructor(e){super(),this.method=e.method,this.url=e.url;let t={};for(let[r,n]of Object.entries(e.headers??{}))t[r.toLowerCase()]=n;this.headers={host:"localhost",...t},this.#e=e.body!=null&&e.body.length>0?Buffer.from(e.body,"utf-8"):Buffer.alloc(0)}_read(){this.#t||(this.#t=!0,this.#e.length>0&&this.push(this.#e),this.push(null))}},L=class extends rt{statusCode=200;headersSent=!1;#e=[];#t={};#r=200;_write(e,t,r){this.#e.push(Buffer.isBuffer(e)?e:Buffer.from(e)),r()}writeHead(e,t){if(this.#r=e,this.statusCode=e,t!=null&&typeof t=="object"&&!Array.isArray(t))for(let[r,n]of Object.entries(t))this.#t[r.toLowerCase()]=Array.isArray(n)?n.join(", "):String(n);return this.headersSent=!0,this}setHeader(e,t){return this.#t[e.toLowerCase()]=Array.isArray(t)?t.join(", "):String(t),this}getHeader(e){return this.#t[e.toLowerCase()]}end(e,...t){if(e!=null&&typeof e!="function"){let r=Buffer.isBuffer(e)||e instanceof Uint8Array?Buffer.from(e):Buffer.from(String(e),"utf-8");this.#e.push(r),super.end(...t)}else typeof e=="function"?super.end(e):super.end(...t);return this}toResult(){return{status:this.#r,headers:{...this.#t},body:Buffer.concat(this.#e).toString("utf-8")}}};function be(s){return{type:"message",data:s}}function ve(s,e){return{type:"tracked",id:s,data:e}}function we(){return{type:"pong"}}function B(s,e){return{type:"error",code:s,message:e}}function Pe(s){let e;try{e=x(s),A(e)}catch{return null}if(typeof e!="object"||e===null)return null;let t=e;return t.type==="ping"?{type:"ping"}:t.type==="message"&&"data"in t?{type:"message",data:t.data}:null}async function lt(s,e){let t=e.maxFileCount??10,r=e.maxFileSize??10*1024*1024,n=e.allowedMimeTypes;return new Promise((o,i)=>{let d={},c={},a=[],f=0,m=dt({headers:s.headers,limits:{fileSize:r,files:t}});m.on("field",(p,g)=>{d[p]=g}),m.on("file",(p,g,h,u,l)=>{if(f>=t){a.push({filename:h,mimeType:l,reason:`Exceeded max file count (${t})`}),g.resume();return}if(n&&!n.some(R=>R.endsWith("/*")?l.startsWith(R.slice(0,-1)):l===R)){a.push({filename:h,mimeType:l,reason:`MIME type "${l}" not allowed (allowed: ${n.join(", ")})`}),g.resume();return}f++;let y=[];g.on("data",b=>{y.push(b)}),g.on("end",()=>{let b=Buffer.concat(y);c[p]={filename:h,mimeType:l,buffer:b,size:b.length}}),g.on("error",i)}),m.on("error",i),m.on("finish",()=>o({fields:d,files:c,rejectedFiles:a})),s.pipe(m)})}var $=class{#e=new at;#t=[];#r=[];#n=[];#s=[];#i=new j;#a;#d=null;#o=null;#c=!1;#l=null;constructor(e={}){this.#a=e}services(e){return e(this.#e),this}use(e){return this.#s.push(e),this}contentType(e){return this.#i.register(e),this}useAuthentication(e){return this.#d=e,this}useAuthorization(e){return this.#o=e??{},this}withHealthcheck(){return this.#c=!0,this}useBatching(e={}){return this.#l=e,this}handle(e,t,r){return this.#t.push({endpoint:e.introspect(),handler:t,middlewares:r?.middlewares}),this}handleAll(e){for(let t of e._entries)this.#t.push({endpoint:t.endpoint.introspect(),handler:t.handler,middlewares:t.middlewares});for(let t of e._subscriptions)this.#r.push({endpoint:t.endpoint.introspect(),handler:t.handler,middlewares:t.middlewares});return this}getRegistrations(){return[...this.#t]}getSubscriptionRegistrations(){return[...this.#r]}webhook(e){return this.#n.push(e),this}getWebhooks(){return[...this.#n]}getAuthenticationConfig(){return this.#d}async listen(e,t){let r=new J;for(let f of this.#t)r.addRoute(f);for(let f of this.#r)r.addSubscriptionRoute(f);r.finalize();let n=this.#e.buildServiceProvider({validateScopes:!1}),o=[];if(this.#d&&o.push(ft(this.#d)),this.#o!==null){let f=new Map;if(this.#o.policies)for(let[p,g]of Object.entries(this.#o.policies)){let h=new st;g(h),f.set(p,h.build(p))}let m=new nt(f);o.push(mt(m,this.#d))}let i=[...o,...this.#s],d=new V(r,n,this.#i,i,this.#c,this.#r.length>0,this.#l,this.#a.maxBodySize),c=e??this.#a.port??3e3,a=t??this.#a.host??"0.0.0.0";return await d.start(c,a,this.#a),d}},ut=1024,V=class{#e;#t;#r;#n;#s;#i;#a;#d;#o=null;#c=null;#l=new Set;constructor(e,t,r,n,o=!1,i=!1,d=null,c=H){this.#e=e,this.#t=t,this.#r=r,this.#n=n,this.#s=o,this.#i=i,this.#a=d,this.#d=c}async start(e,t,r){let n=(o,i)=>{this.#u(o,i).catch(d=>{i.headersSent||(i.writeHead(500,{"content-type":w}),i.end(v(S(500))))})};r.https?this.#o=Te.createServer({key:r.https.key,cert:r.https.cert},n):this.#o=G.createServer(n),await new Promise(o=>{this.#o.listen(e,t,o)}),this.#i&&(this.#c=new ct({noServer:!0,maxPayload:this.#d}),this.#o.on("upgrade",(o,i,d)=>{let c=new URL(o.url??"/",`http://${o.headers.host??"localhost"}`).pathname,a=this.#e.matchSubscription(c);if(!a){i.write(`HTTP/1.1 404 Not Found\r
|
|
2
2
|
\r
|
|
3
|
-
`),
|
|
3
|
+
`),i.destroy();return}this.#c.handleUpgrade(o,i,d,f=>{this.#f(f,o,a.registration,a.parsedPath)})}))}async close(){for(let e of this.#l)e.close(1001,"Server shutting down");this.#l.clear(),this.#c&&(await new Promise((e,t)=>{this.#c.close(r=>{r?t(r):e()})}),this.#c=null),this.#o&&(await new Promise((e,t)=>{this.#o.close(r=>{r?t(r):e()})}),this.#o=null)}get address(){let e=this.#o?.address();return!e||typeof e=="string"?null:{port:e.port,host:e.address}}async#u(e,t){let r=this.#t.createScope();try{let n=new E(e,t,this.#d),o=n.url.pathname,i=n.method,d=e.__cleverbrushBatchSubrequest;if(d&&n.items.set("__batch_subrequest",d),this.#s&&i==="GET"&&o==="/health"){t.writeHead(200),t.end();return}if(this.#a!==null&&i==="POST"&&o===(this.#a.path??"/__batch")){n.services=r.serviceProvider;let g=new k;for(let h of this.#n)g.add(h);await g.execute(n,async()=>{n.responded||(await this.#p(e,t),n.responded=!0)});return}let c=this.#e.match(i,o);if(!c.match){if(c.badRequest){let h=S(400);t.writeHead(400,{"content-type":w}),t.end(v(h));return}if(c.methodNotAllowed){let h=S(405,"Method Not Allowed");t.writeHead(405,{"content-type":w,allow:c.allowedMethods.join(", ")}),t.end(v(h));return}let g=S(404);t.writeHead(404,{"content-type":w}),t.end(v(g));return}let{registration:a,parsedPath:f}=c.match,m=a.endpoint;if(f){let g={};oe(f,"",g),n.pathParams=g}n.services=r.serviceProvider,n.items.set("__endpoint_meta",m);let p=new k;for(let g of this.#n)p.add(g);if(a.middlewares)for(let g of a.middlewares)p.add(g);await p.execute(n,async()=>{if(n.responded)return;let g,h,u;if(fe(m)){let b=e.headers["content-type"]??"";if(m.fileUpload&&b.startsWith("multipart/form-data"))try{let R=await lt(e,m.fileUpload);g=R.fields,h=R.files,u=R.rejectedFiles}catch{let R=S(400,"Malformed multipart request");t.writeHead(400,{"content-type":w}),t.end(v(R)),n.responded=!0;return}else if(m.fileUpload){let R=S(400,"File upload endpoint requires multipart/form-data content type");t.writeHead(400,{"content-type":w}),t.end(v(R)),n.responded=!0;return}else{let R=this.#r.selectRequestHandler(b);if(R){let ae=(await n.body()).toString("utf-8");if(ae.length>0)try{g=R.deserialize(ae)}catch{let Ce=S(400,"Malformed request body");t.writeHead(400,{"content-type":w}),t.end(v(Ce)),n.responded=!0;return}}else if(b){let ie=S(415);t.writeHead(415,{"content-type":w}),t.end(v(ie)),n.responded=!0;return}}}let l=await me(m,f,n,g);if(!l.valid){t.writeHead(400,{"content-type":w}),t.end(v(l.problemDetails)),n.responded=!0;return}if(h&&l.args.length>0){let b=l.args[0];b.files=h,u&&u.length>0&&(b.rejectedFiles=u)}let y=a.handler(...l.args);y instanceof Promise&&(y=await y),!n.responded&&(await this.#h(e,t,y),n.responded=!0)})}catch(n){if(t.headersSent)return;if(n instanceof T){let o=n.toProblemDetails();t.writeHead(o.status,{"content-type":w}),t.end(v(o))}else{console.error("[server] Unhandled error:",n);let o=S(500);t.writeHead(500,{"content-type":w}),t.end(v(o))}}finally{try{await r.asyncDispose()}catch{}}}async#p(e,t){let r=this.#a,n=r.maxSize??20,o=r.parallel??!0,i;try{let a=await ht(e,this.#d),f=x(a.toString("utf-8"));if(A(f),i=f,!Array.isArray(i?.requests))throw new Error("requests must be an array")}catch(a){if(a instanceof T&&a.status===413){let m=S(413,"Payload Too Large");t.writeHead(413,{"content-type":w}),t.end(v(m));return}let f=S(400,"Invalid batch request body");t.writeHead(400,{"content-type":w}),t.end(v(f));return}if(i.requests.length>n){let a=S(400,`Batch size ${i.requests.length} exceeds maximum of ${n}`);t.writeHead(400,{"content-type":w}),t.end(v(a));return}let d=async(a,f)=>{let m=new U({method:(a.method??"GET").toUpperCase(),url:a.url,headers:a.headers??{},body:a.body});m.__cleverbrushBatchSubrequest={index:f,size:i.requests.length,path:r.path??"/__batch"};let p=new L;return await this.#u(m,p),p.toResult()},c;if(o)c=await Promise.all(i.requests.map(d));else{c=[];for(let a=0;a<i.requests.length;a++)c.push(await d(i.requests[a],a))}t.writeHead(200,{"content-type":"application/json"}),t.end(JSON.stringify({responses:c}))}async#h(e,t,r){r instanceof C?await r.executeAsync(e,t,this.#r):r==null?(t.writeHead(204),t.end()):await new P(r,200).executeAsync(e,t,this.#r)}#f(e,t,r,n){this.#l.add(e);let o=this.#t.createScope(),i=new AbortController,d=r.endpoint,c=new G.ServerResponse(t),a=new E(t,c);if(n){let m={};oe(n,"",m),a.pathParams=m}a.services=o.serviceProvider,a.items.set("__endpoint_meta",d);let f=new k;for(let m of this.#n)f.add(m);if(r.middlewares)for(let m of r.middlewares)f.add(m);f.execute(a,async()=>{if(a.responded){e.close(1008,"Unauthorized");return}this.#m(e,a,d,r.handler,n,o,i)}).catch(()=>{e.close(1011,"Internal Server Error")}),e.on("close",()=>{this.#l.delete(e),i.abort(),o.asyncDispose().catch(()=>{})}),e.on("error",()=>{this.#l.delete(e),i.abort(),o.asyncDispose().catch(()=>{})})}#m(e,t,r,n,o,i,d){let c=[],a=null,f=!1,m={[Symbol.asyncIterator](){return{next(){return c.length>0?Promise.resolve({value:c.shift(),done:!1}):f?Promise.resolve({value:void 0,done:!0}):new Promise(h=>{a=()=>{a=null,c.length>0?h({value:c.shift(),done:!1}):h({value:void 0,done:!0})}})},return(){return f=!0,Promise.resolve({value:void 0,done:!0})}}}};e.on("message",h=>{let u=typeof h=="string"?h:h.toString("utf-8"),l=Pe(u);if(!l){e.send(JSON.stringify(B(400,"Invalid frame format")));return}if(l.type==="ping"){e.send(JSON.stringify(we()));return}if(c.length>=ut){e.send(JSON.stringify(B(429,"Message queue full \u2014 slow down or reconnect"))),e.close(1008,"Message queue overflow");return}if(r.incomingSchema){let y=r.incomingSchema.validate(l.data);if(!y.valid){let b=(y.errors??[]).map(R=>R.message).join("; ");e.send(JSON.stringify(B(422,`Validation failed: ${b}`)));return}c.push(y.object)}else c.push(l.data);a&&a()}),e.on("close",()=>{f=!0,a&&a()});let p={context:t,signal:d.signal};if(o&&Object.keys(o).length>0&&(p.params=o),r.querySchema){let h={};for(let[l,y]of t.url.searchParams.entries())h[l]=y;let u=r.querySchema.validate(h);if(!u.valid){let l=(u.errors??[]).map(y=>y.message).join("; ");e.close(1002,`Query validation failed: ${l}`);return}p.query=u.object}if(r.headerSchema){let h=r.headerSchema.validate(t.headers);if(!h.valid){let u=(h.errors??[]).map(l=>l.message).join("; ");e.close(1002,`Header validation failed: ${u}`);return}p.headers=h.object}t.principal!==void 0&&(p.principal=t.principal),r.incomingSchema,p.incoming=m;let g=[p];if(r.serviceSchemas){let h={};for(let[u,l]of Object.entries(r.serviceSchemas))h[u]=i.serviceProvider.get(l);g.push(h)}(async()=>{try{let h=n(...g);for await(let u of h){if(e.readyState!==e.OPEN)break;let l;if(de(u)){let y=r.outgoingSchema?r.outgoingSchema.validate(u.data):{valid:!0,object:u.data};if(!y.valid){e.send(JSON.stringify(B(500,"Outgoing validation failed")));continue}l=JSON.stringify(ve(u.id,y.object))}else{let y=r.outgoingSchema?r.outgoingSchema.validate(u):{valid:!0,object:u};if(!y.valid){e.send(JSON.stringify(B(500,"Outgoing validation failed")));continue}l=JSON.stringify(be(y.object))}e.send(l)}}catch(h){e.readyState===e.OPEN&&(h instanceof Error&&console.error("[server] Subscription handler error:",h),e.send(JSON.stringify(B(500,"Internal error"))),e.close(1011,"Handler error"))}})()}};function pt(s){return new $(s)}function ht(s,e=H){return new Promise((t,r)=>{let n=[],o=0;s.on("data",i=>{if(o+=i.length,o>e){s.destroy(),r(new T(413,"Payload Too Large"));return}n.push(i)}),s.on("end",()=>t(Buffer.concat(n))),s.on("error",r)})}function oe(s,e,t){for(let[r,n]of Object.entries(s)){let o=e?`${e}.${r}`:r;n!==null&&typeof n=="object"&&!Array.isArray(n)?oe(n,o,t):t[o]=String(n)}}function ft(s){let e=new Map;for(let r of s.schemes)e.set(r.name,r);let t;return s.trySchemes==="all"?t=s.schemes:t=(s.trySchemes&&s.trySchemes.length>0?s.trySchemes:[s.defaultScheme]).map(n=>e.get(n)).filter(n=>n!==void 0),async(r,n)=>{if(r.items.get("__endpoint_meta")?.authRoles===null){r.principal=W.anonymous(),await n();return}let i={headers:r.headers,cookies:ot(r.headers.cookie??""),items:r.items};r.principal=W.anonymous();for(let d of t){let c=await d.authenticate(i);if(c.succeeded){r.principal=c.principal;break}}await n()}}function mt(s,e){let t={};if(e){for(let r of e.schemes)if(r.challenge){let n=r.challenge();t[n.headerName.toLowerCase()]=n.headerValue}}return async(r,n)=>{let o=r.items.get("__endpoint_meta");if(!o||o.authRoles===null){await n();return}let i=r.principal;if(!i||!(i instanceof W)||!i.isAuthenticated){let d=S(401,"Unauthorized"),c={"content-type":w,...t};r.response.writeHead(401,c),r.response.end(v(d)),r.responded=!0;return}if(o.authRoles.length>0&&!(await s.authorize(i,[it(...o.authRoles)])).allowed){let c=S(403,"Forbidden");r.response.writeHead(403,{"content-type":w}),r.response.end(v(c)),r.responded=!0;return}i instanceof W&&(r.principal=i.value),await n()}}function gt(s,e){return{name:s,...e}}export{C as ActionResult,Z as BadRequestError,te as ConflictError,j as ContentNegotiator,O as ContentResult,H as DEFAULT_MAX_BODY_SIZE,_e as EndpointBuilder,N as FileResult,ee as ForbiddenError,T as HttpError,Ye as IRequestContext,P as JsonResult,z as NoContentResult,Y as NotFoundError,q as RawResult,I as RedirectResult,E as RequestContext,V as Server,$ as ServerBuilder,D as StatusCodeResult,F as StreamResult,Ee as SubscriptionBuilder,Q as UnauthorizedError,Ve as cacheResponse,Me as computeCacheKey,xe as createCacheTagTree,je as createEndpoints,S as createProblemDetails,pt as createServer,X as createValidationProblemDetails,Ne as defineApi,gt as defineWebhook,He as endpoint,le as formUrlEncodedContentTypeHandler,We as idempotency,ce as jsonContentTypeHandler,ke as mapHandlers,Oe as mergeContracts,De as omitGroups,Fe as pickGroups,qe as route,Ae as serializeTag,Be as tracked};
|
|
4
4
|
//# sourceMappingURL=index.js.map
|