Socket
Socket
Sign inDemoInstall

elysia

Package Overview
Dependencies
Maintainers
1
Versions
409
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.4-exp-230201.1659 to 0.2.4-exp-230201.1721

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 n,mergeDeep as h,createValidationError as o,getSchemaValidator as i,SCHEMA as l,DEFS as d,getResponseSchemaValidator as u,mapPathnameAndQueryRegEx as c}from"./utils";import{registerSchemaPath as f}from"./schema";import{mapErrorCode as p,mapErrorStatus as m}from"./error";let v="AsyncFunction";export default class g{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:n(a(this.event),s)});let h=this.store[d],o=i(s?.schema?.body??this.$schema?.body,h),c=i(s?.schema?.headers??this.$schema?.headers,h,!0),p=i(s?.schema?.params??this.$schema?.params,h),m=i(s?.schema?.query??this.$schema?.query,h),v=u(s?.schema?.response??this.$schema?.response,h);f({schema:this.store[l],hook:s,method:e,path:t,models:this.store[d]});let g=n(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||c||p||m||v?{body:o,header:c,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 g;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})=>{"/"===r?this._addHandler(t,e,s,a):this._addHandler(t,`${e}${r}`,s,a)}),this}guard(e,t){let r=new g;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,n(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=h(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:i(e.body,t),headers:i(e?.headers,t,!0),params:i(e?.params,t),query:i(e?.query,t),response:i(e?.response,t)},this}async handle(e){let n;let h={status:200,headers:{}};this.decorators?((n=a(this.decorators)).request=e,n.set=h,n.store=this.store,n.query={}):n={set:h,store:this.store,request:e,query:{}};try{let a;let i=this.event;if(i.request.length)for(let e=0;e<i.request.length;e++){let t=i.request[e],s=t(n);if(t.constructor.name===v&&(s=await s),s=r(s,h))return s}let l=e.url.match(c);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)),i.parse.length)for(let e=0;e<i.parse.length;e++){let r=i.parse[e],s=r(n,t);if(r.constructor.name===v&&(s=await s),s){a=s;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())}}}n.body=a,n.params=d?.params||{},l[2]&&(n.query=s(l[2]));let f=u.hooks;if(f?.transform.length)for(let e=0;e<f.transform.length;e++){let t=f.transform[e],r=t(n);t.constructor.name===v&&await r}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(n.params))throw o("params",t.params,n.params);if(!1===t.query?.Check(n.query))throw o("query",t.query,n.query);if(!1===t.body?.Check(a))throw o("body",t.body,a)}if(f?.beforeHandle.length)for(let e=0;e<f.beforeHandle.length;e++){let t=f.beforeHandle[e],s=t(n);if(t.constructor.name===v&&(s=await s),null!=s){for(let e=0;e<f.afterHandle.length;e++){let t=f.afterHandle[e],r=t(n,s);t.constructor.name===v&&(r=await r),r&&(s=r)}let e=r(s,n.set);if(e)return e}}let p=u.handle(n);if(p instanceof Promise&&(p=await p),!1===u.validator?.response?.Check(p))throw o("response",u.validator.response,p);if(f?.afterHandle.length)for(let e=0;e<f.afterHandle.length;e++){let t=f.afterHandle[e],s=t(n,p);t.constructor.name===v&&(s=await s);let a=r(s,n.set);if(a)return a}return t(p,n.set)}catch(e){return this.handleError(e,h)}}async handleError(e,r={headers:{}}){for(let s=0;s<this.event.error.length;s++){let a=this.event.error[s]({code:p(e.message),error:e,set:r});if(a instanceof Promise&&(a=await a),null!=a)return t(a,r)}return new Response("string"==typeof e.cause?e.cause:e.message,{headers:r.headers,status:m(p(e.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.bind(this),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{g 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 h,createValidationError as n,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 h=this.store[d],n=o(s?.schema?.body??this.$schema?.body,h),f=o(s?.schema?.headers??this.$schema?.headers,h,!0),p=o(s?.schema?.params??this.$schema?.params,h),m=o(s?.schema?.query??this.$schema?.query,h),v=u(s?.schema?.response??this.$schema?.response,h);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:n||f||p||m||v?{body:n,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:s,hooks:a})=>{"/"===r?this._addHandler(t,e,s,a):this._addHandler(t,`${e}${r}`,s,a)}),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=h(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}async handle(e){let i;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;let o=this.event;if(o.request.length)for(let e=0;e<o.request.length;e++){let t=o.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)),o.parse.length)for(let e=0;e<o.parse.length;e++){let r=o.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?.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 n("header",t.headers,r)}if(!1===t.params?.Check(i.params))throw n("params",t.params,i.params);if(!1===t.query?.Check(i.query))throw n("query",t.query,i.query);if(!1===t.body?.Check(a))throw n("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 n("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(e){return this.handleError(e,h)}}async handleError(e,r={headers:{}}){for(let s=0;s<this.event.error.length;s++){let a=this.event.error[s]({code:p(e.message),error:e,set:r});if(a instanceof Promise&&(a=await a),null!=a)return t(a,r)}return new Response("string"==typeof e.cause?e.cause:e.message,{headers:r.headers,status:m(p(e.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.bind(this),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.4-exp-230201.1659",
"version": "0.2.4-exp-230201.1721",
"author": {

@@ -39,4 +39,2 @@ "name": "saltyAom",

"@sinclair/typebox": "^0.25.21",
"@swc/cli": "^0.1.60",
"@swc/core": "^1.3.32",
"openapi-types": "^12.1.0",

@@ -46,2 +44,4 @@ "raikiri": "0.0.0-beta.4"

"devDependencies": {
"@swc/cli": "^0.1.60",
"@swc/core": "^1.3.32",
"@types/node": "^18.11.18",

@@ -48,0 +48,0 @@ "@typescript-eslint/eslint-plugin": "^5.48.2",

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