Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

elysia

Package Overview
Dependencies
Maintainers
1
Versions
442
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elysia - npm Package Compare versions

Comparing version 0.2.8 to 0.2.9

2

dist/index.js

@@ -1,1 +0,1 @@

import{Raikiri as e}from"raikiri";import{mapResponse as t,mapEarlyResponse as r}from"./handler";import{mapQuery as s,clone as a,mergeHook as i,mergeDeep as n,createValidationError as h,getSchemaValidator as o,SCHEMA as l,DEFS as d,getResponseSchemaValidator as u,mapPathnameAndQueryRegEx as f}from"./utils";import{registerSchemaPath as c}from"./schema";import{mapErrorCode as p,mapErrorStatus as m}from"./error";export default class v{store={[l]:{},[d]:{}};decorators=null;event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new e;routes=[];lazyLoadModules=[];constructor(e={}){this.config={...e}}_addHandler(e,t,r,s){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:i(a(this.event),s)});let n=this.store[d],h=o(s?.schema?.body??this.$schema?.body,n),f=o(s?.schema?.headers??this.$schema?.headers,n,!0),p=o(s?.schema?.params??this.$schema?.params,n),m=o(s?.schema?.query??this.$schema?.query,n),v=u(s?.schema?.response??this.$schema?.response,n);c({schema:this.store[l],hook:s,method:e,path:t,models:this.store[d]});let g=i(a(this.event),s);g.schema||g.transform.length||g.beforeHandle.length||g.error.length||g.afterHandle.length||(g=void 0);let y={handle:r,hooks:g,validator:h||f||p||m||v?{body:h,header:f,params:p,query:m,response:v}:void 0};this.router.add(e,t,y)}onStart(e){return this.event.start.push(e),this}onRequest(e){return this.event.request.push(e),this}onParse(e){return this.event.parse.splice(this.event.parse.length-1,0,e),this}onTransform(e){return this.event.transform.push(e),this}onBeforeHandle(e){return this.event.beforeHandle.push(e),this}onAfterHandle(e){return this.event.afterHandle.push(e),this}onError(e){return this.event.error.push(e),this}onStop(e){return this.event.stop.push(e),this}on(e,t){switch(e){case"start":this.event.start.push(t);break;case"request":this.event.request.push(t);break;case"parse":this.event.parse.push(t);break;case"transform":this.event.transform.push(t);break;case"beforeHandle":this.event.beforeHandle.push(t);break;case"afterHandle":this.event.afterHandle.push(t);break;case"error":this.event.error.push(t);break;case"stop":this.event.stop.push(t)}return this}group(e,t){let r=new v;r.store=this.store;let s=t(r);return s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),Object.values(r.routes).forEach(({method:t,path:r,handler:a,hooks:n})=>{"/"===r?this._addHandler(t,e,a,i(n,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,i(n,{error:s.event.error}))}),this}guard(e,t){let r=new v;r.store=this.store;let s=t(r);return s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),Object.values(r.routes).forEach(({method:t,path:r,handler:s,hooks:a})=>{this._addHandler(t,r,s,i(e,a))}),this}use(e){if(e instanceof Promise)return this.lazyLoadModules.push(e.then(e=>"function"==typeof e?e(this):e.default(this))),this;let t=e(this);return t instanceof Promise?(this.lazyLoadModules.push(t),this):t}get(e,t,r){return this._addHandler("GET",e,t,r),this}post(e,t,r){return this._addHandler("POST",e,t,r),this}put(e,t,r){return this._addHandler("PUT",e,t,r),this}patch(e,t,r){return this._addHandler("PATCH",e,t,r),this}delete(e,t,r){return this._addHandler("DELETE",e,t,r),this}options(e,t,r){return this._addHandler("OPTIONS",e,t,r),this}all(e,t,r){return this._addHandler("ALL",e,t,r),this}head(e,t,r){return this._addHandler("HEAD",e,t,r),this}trace(e,t,r){return this._addHandler("TRACE",e,t,r),this}connect(e,t,r){return this._addHandler("CONNECT",e,t,r),this}route(e,t,r,s){return this._addHandler(e,t,r,s),this}state(e,t){return e in this.store||(this.store[e]=t),this}decorate(e,t){return this.decorators||(this.decorators={}),e in this.decorators||(this.decorators[e]=t),this}derive(e){return this.store=n(this.store,e(()=>this.store)),this}inject(e){return this.onTransform(t=>{Object.assign(t,e(t))})}schema(e){let t=this.store[d];return this.$schema={body:o(e.body,t),headers:o(e?.headers,t,!0),params:o(e?.params,t),query:o(e?.query,t),response:o(e?.response,t)},this}handle=async e=>{let i;let n={status:200,headers:{}};this.decorators?((i=a(this.decorators)).request=e,i.set=n,i.store=this.store,i.query={}):i={set:n,store:this.store,request:e,query:{}};try{let a;if(this.event.request.length)for(let e=0;e<this.event.request.length;e++){let t=this.event.request[e](i);if(t instanceof Promise&&(t=await t),t=r(t,n))return t}let o=e.url.match(f);if(!o)throw Error("NOT_FOUND");let l=this.router.match(e.method,o[1])??this.router.match("ALL",o[1]),d=l?.store;if(!d)throw Error("NOT_FOUND");if("GET"!==e.method){let t=e.headers.get("content-type");if(t){let r=t.indexOf(";");if(-1!==r&&(t=t.slice(0,r)),this.event.parse.length)for(let e=0;e<this.event.parse.length;e++){let r=this.event.parse[e](i,t);if(r instanceof Promise&&(r=await r),void 0!==r){a=r;break}}if(void 0===a)switch(t){case"application/json":a=await e.json();break;case"text/plain":a=await e.text();break;case"application/x-www-form-urlencoded":a=s(await e.text())}}}i.body=a,i.params=l.params,o[2]&&(i.query=s(o[2]));let u=d.hooks;if(u?.transform.length)for(let e=0;e<u.transform.length;e++){let t=u.transform[e](i);t instanceof Promise&&await t}if(d.validator){let t=d.validator;if(t.headers){let r={};for(let t in e.headers)r[t]=e.headers.get(t);if(!1===t.headers.Check(r))throw h("header",t.headers,r)}if(!1===t.params?.Check(i.params))throw h("params",t.params,i.params);if(!1===t.query?.Check(i.query))throw h("query",t.query,i.query);if(!1===t.body?.Check(a))throw h("body",t.body,a)}if(u?.beforeHandle.length)for(let e=0;e<u.beforeHandle.length;e++){let t=u.beforeHandle[e](i);if(t instanceof Promise&&(t=await t),null!=t){for(let e=0;e<u.afterHandle.length;e++){let r=u.afterHandle[e](i,t);r instanceof Promise&&(r=await r),r&&(t=r)}let e=r(t,i.set);if(e)return e}}let c=d.handle(i);if(c instanceof Promise&&(c=await c),!1===d.validator?.response?.Check(c))throw h("response",d.validator.response,c);if(u?.afterHandle.length)for(let e=0;e<u.afterHandle.length;e++){let t=u.afterHandle[e](i,c);t instanceof Promise&&(t=await t);let s=r(t,i.set);if(s)return s}return t(c,i.set)}catch(t){return(!n.status||n.status<300)&&(n.status=500),this.handleError(e,t,n)}};async handleError(e,r,s={headers:{}}){for(let a=0;a<this.event.error.length;a++){let i=this.event.error[a]({request:e,code:p(r.message),error:r,set:s});if(i instanceof Promise&&(i=await i),null!=i)return t(i,s)}return new Response("string"==typeof r.cause?r.cause:r.message,{headers:s.headers,status:m(p(r.message))})}listen=(e,t)=>{if(!Bun)throw Error("Bun to run");if("string"==typeof e&&Number.isNaN(e=+e))throw Error("Port must be a numeric value");let r=this.handle,s="object"==typeof e?{...this.config.serve,...e,fetch:r}:{...this.config.serve,port:e,fetch:r},a=`$$Elysia:${s.port}`;globalThis[a]?(this.server=globalThis[a],this.server.reload(s)):globalThis[a]=this.server=Bun.serve(s);for(let e=0;e<this.event.start.length;e++)this.event.start[e](this);return t&&t(this.server),Promise.all(this.lazyLoadModules).then(()=>{this.server.pendingRequests||Bun.gc(!0)}),this};stop=async()=>{if(!this.server)throw Error("Elysia isn't running. Call `app.listen` to start the server.");this.server.stop();for(let e=0;e<this.event.stop.length;e++)await this.event.stop[e](this)};get modules(){return Promise.all(this.lazyLoadModules)}setModel(e){return Object.entries(e).forEach(([e,t])=>{e in this.store[d]||(this.store[d][e]=t)}),this}}export{Type as t}from"@sinclair/typebox";export{SCHEMA,DEFS,createValidationError,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,mapPathnameAndQueryRegEx,mapQuery}from"./utils";export{v as Elysia};
import{Raikiri as e}from"raikiri";import{mapResponse as t,mapEarlyResponse as r}from"./handler";import{mapQuery as s,clone as a,mergeHook as i,mergeDeep as n,createValidationError as o,getSchemaValidator as h,SCHEMA as l,DEFS as d,getResponseSchemaValidator as u,mapPathnameAndQueryRegEx as f}from"./utils";import{registerSchemaPath as c}from"./schema";import{mapErrorCode as p,mapErrorStatus as m}from"./error";export default class v{store={[l]:{},[d]:{}};decorators=null;event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new e;routes=[];lazyLoadModules=[];constructor(e={}){this.config={...e}}_addHandler(e,t,r,s){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:i(a(this.event),s)});let n=this.store[d],o=h(s?.schema?.body??this.$schema?.body,n),f=h(s?.schema?.headers??this.$schema?.headers,n,!0),p=h(s?.schema?.params??this.$schema?.params,n),m=h(s?.schema?.query??this.$schema?.query,n),v=u(s?.schema?.response??this.$schema?.response,n);c({schema:this.store[l],hook:s,method:e,path:t,models:this.store[d]});let g=i(a(this.event),s);g.schema||g.transform.length||g.beforeHandle.length||g.error.length||g.afterHandle.length||(g=void 0);let y={handle:r,hooks:g,validator:o||f||p||m||v?{body:o,header:f,params:p,query:m,response:v}:void 0};this.router.add(e,t,y)}onStart(e){return this.event.start.push(e),this}onRequest(e){return this.event.request.push(e),this}onParse(e){return this.event.parse.splice(this.event.parse.length-1,0,e),this}onTransform(e){return this.event.transform.push(e),this}onBeforeHandle(e){return this.event.beforeHandle.push(e),this}onAfterHandle(e){return this.event.afterHandle.push(e),this}onError(e){return this.event.error.push(e),this}onStop(e){return this.event.stop.push(e),this}on(e,t){switch(e){case"start":this.event.start.push(t);break;case"request":this.event.request.push(t);break;case"parse":this.event.parse.push(t);break;case"transform":this.event.transform.push(t);break;case"beforeHandle":this.event.beforeHandle.push(t);break;case"afterHandle":this.event.afterHandle.push(t);break;case"error":this.event.error.push(t);break;case"stop":this.event.stop.push(t)}return this}group(e,t){let r=new v;r.store=this.store;let s=t(r);return s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),Object.values(r.routes).forEach(({method:t,path:r,handler:a,hooks:n})=>{"/"===r?this._addHandler(t,e,a,i(n,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,i(n,{error:s.event.error}))}),this}guard(e,t){let r=new v;r.store=this.store;let s=t(r);return s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),Object.values(r.routes).forEach(({method:t,path:r,handler:s,hooks:a})=>{this._addHandler(t,r,s,i(e,a))}),this}use(e){if(e instanceof Promise)return this.lazyLoadModules.push(e.then(e=>"function"==typeof e?e(this):e.default(this))),this;let t=e(this);return t instanceof Promise?(this.lazyLoadModules.push(t),this):t}get(e,t,r){return this._addHandler("GET",e,t,r),this}post(e,t,r){return this._addHandler("POST",e,t,r),this}put(e,t,r){return this._addHandler("PUT",e,t,r),this}patch(e,t,r){return this._addHandler("PATCH",e,t,r),this}delete(e,t,r){return this._addHandler("DELETE",e,t,r),this}options(e,t,r){return this._addHandler("OPTIONS",e,t,r),this}all(e,t,r){return this._addHandler("ALL",e,t,r),this}head(e,t,r){return this._addHandler("HEAD",e,t,r),this}trace(e,t,r){return this._addHandler("TRACE",e,t,r),this}connect(e,t,r){return this._addHandler("CONNECT",e,t,r),this}route(e,t,r,s){return this._addHandler(e,t,r,s),this}state(e,t){return e in this.store||(this.store[e]=t),this}decorate(e,t){return this.decorators||(this.decorators={}),e in this.decorators||(this.decorators[e]=t),this}derive(e){return this.store=n(this.store,e(()=>this.store)),this}inject(e){return this.onTransform(t=>{Object.assign(t,e(t))})}schema(e){let t=this.store[d];return this.$schema={body:h(e.body,t),headers:h(e?.headers,t,!0),params:h(e?.params,t),query:h(e?.query,t),response:h(e?.response,t)},this}handle=async e=>{let i,n;let h={status:200,headers:{}};this.decorators?((i=a(this.decorators)).request=e,i.set=h,i.store=this.store,i.query={}):i={set:h,store:this.store,request:e,query:{}};try{let a;if(this.event.request.length)for(let e=0;e<this.event.request.length;e++){let t=this.event.request[e](i);if(t instanceof Promise&&(t=await t),t=r(t,h))return t}let l=e.url.match(f);if(!l)throw Error("NOT_FOUND");let d=this.router.match(e.method,l[1])??this.router.match("ALL",l[1]),u=d?.store;if(!u)throw Error("NOT_FOUND");if("GET"!==e.method){let t=e.headers.get("content-type");if(t){let r=t.indexOf(";");if(-1!==r&&(t=t.slice(0,r)),this.event.parse.length)for(let e=0;e<this.event.parse.length;e++){let r=this.event.parse[e](i,t);if(r instanceof Promise&&(r=await r),void 0!==r){a=r;break}}if(void 0===a)switch(t){case"application/json":a=await e.json();break;case"text/plain":a=await e.text();break;case"application/x-www-form-urlencoded":a=s(await e.text())}}}i.body=a,i.params=d.params,l[2]&&(i.query=s(l[2]));let c=u.hooks;if(c?.error&&(n=c?.error),c?.transform.length)for(let e=0;e<c.transform.length;e++){let t=c.transform[e](i);t instanceof Promise&&await t}if(u.validator){let t=u.validator;if(t.headers){let r={};for(let t in e.headers)r[t]=e.headers.get(t);if(!1===t.headers.Check(r))throw o("header",t.headers,r)}if(!1===t.params?.Check(i.params))throw o("params",t.params,i.params);if(!1===t.query?.Check(i.query))throw o("query",t.query,i.query);if(!1===t.body?.Check(a))throw o("body",t.body,a)}if(c?.beforeHandle.length)for(let e=0;e<c.beforeHandle.length;e++){let t=c.beforeHandle[e](i);if(t instanceof Promise&&(t=await t),null!=t){for(let e=0;e<c.afterHandle.length;e++){let r=c.afterHandle[e](i,t);r instanceof Promise&&(r=await r),r&&(t=r)}let e=r(t,i.set);if(e)return e}}let p=u.handle(i);if(p instanceof Promise&&(p=await p),!1===u.validator?.response?.Check(p))throw o("response",u.validator.response,p);if(c?.afterHandle.length)for(let e=0;e<c.afterHandle.length;e++){let t=c.afterHandle[e](i,p);t instanceof Promise&&(t=await t);let s=r(t,i.set);if(s)return s}return t(p,i.set)}catch(t){if((!h.status||h.status<300)&&(h.status=500),n){let s=p(t.message);for(let a=0;a<n.length;a++){let i=n[a]({request:e,error:t,set:h,code:s});i instanceof Promise&&(i=await i);let o=r(i,h);if(o)return o}}return this.handleError(e,t,h)}};async handleError(e,r,s={headers:{}}){for(let a=0;a<this.event.error.length;a++){let i=this.event.error[a]({request:e,code:p(r.message),error:r,set:s});if(i instanceof Promise&&(i=await i),null!=i)return t(i,s)}return new Response("string"==typeof r.cause?r.cause:r.message,{headers:s.headers,status:m(p(r.message))})}listen=(e,t)=>{if(!Bun)throw Error("Bun to run");if("string"==typeof e&&Number.isNaN(e=+e))throw Error("Port must be a numeric value");let r=this.handle,s="object"==typeof e?{...this.config.serve,...e,fetch:r}:{...this.config.serve,port:e,fetch:r},a=`$$Elysia:${s.port}`;globalThis[a]?(this.server=globalThis[a],this.server.reload(s)):globalThis[a]=this.server=Bun.serve(s);for(let e=0;e<this.event.start.length;e++)this.event.start[e](this);return t&&t(this.server),Promise.all(this.lazyLoadModules).then(()=>{this.server.pendingRequests||Bun.gc(!0)}),this};stop=async()=>{if(!this.server)throw Error("Elysia isn't running. Call `app.listen` to start the server.");this.server.stop();for(let e=0;e<this.event.stop.length;e++)await this.event.stop[e](this)};get modules(){return Promise.all(this.lazyLoadModules)}setModel(e){return Object.entries(e).forEach(([e,t])=>{e in this.store[d]||(this.store[d][e]=t)}),this}}export{Type as t}from"@sinclair/typebox";export{SCHEMA,DEFS,createValidationError,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,mapPathnameAndQueryRegEx,mapQuery}from"./utils";export{v as Elysia};
{
"name": "elysia",
"description": "Fast, and friendly Bun web framework",
"version": "0.2.8",
"version": "0.2.9",
"author": {

@@ -6,0 +6,0 @@ "name": "saltyAom",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc