@tinyhttp/app
Advanced tools
Comparing version 0.2.7 to 0.2.8
# @tinyhttp/app | ||
## 0.2.8 | ||
### Patch Changes | ||
- Fix first no matcj | ||
## 0.2.7 | ||
@@ -4,0 +10,0 @@ |
@@ -1,1 +0,1 @@ | ||
import{STATUS_CODES as e,METHODS as t,createServer as r}from"http";import n from"regexparam";import{parse as s}from"url";import o from"range-parser";import a from"proxy-addr";import i from"es-accepts";import d from"es-fresh";import{sign as h}from"@tinyhttp/cookie-signature";import{lookup as l}from"es-mime-types";import{serialize as p}from"@tinyhttp/cookie";import{format as u,parse as c}from"es-content-type";import f from"@tinyhttp/etag";const compileTrust=e=>"function"==typeof e?e:!0===e?function(){return!0}:"number"==typeof e?(t,r)=>{if(e)return r<e}:("string"==typeof e&&(e=e.split(/ *, */)),a.compile(e||[])),getQueryParams=(e="/")=>s(e,!0).query,getURLParams=(e="/",t="/")=>((e,t)=>{let r=0,n={},s=t.pattern.exec(e);for(;r<t.keys.length;)n[t.keys[r]]=(null==s?void 0:s[++r])||null;return n})(e,n(t)),getRouteFromApp=(e,t)=>e.middleware.find(e=>e.handler.name===t.name),getProtocol=e=>{const t=e.connection.encrypted?"https":"http";if(!compileTrust(e.connection.remoteAddress))return t;const r=e.headers["X-Forwarded-Proto"]||t,n=r.indexOf(",");return-1!==n?r.substring(0,n).trim():r.trim()},getRequestHeader=e=>t=>{const r=t.toLowerCase();switch(r){case"referer":case"referrer":return e.headers.referrer||e.headers.referer;default:return e.headers[r]}},setRequestHeader=e=>(t,r)=>e.headers[t.toLowerCase()]=r,getRangeFromHeader=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},checkIfXMLHttpRequest=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],getHostname=e=>{let t=e.get("X-Forwarded-Host");if(t&&compileTrust(e.connection.remoteAddress)||(t=e.get("Host")),!t)return;const r="["===t[0]?t.indexOf("]")+1:0,n=t.indexOf(":",r);return-1!==n?t.substring(0,n):t},getIP=e=>a(e,compileTrust),getFreshOrStale=(e,t)=>{const r=e.method,n=t.statusCode;if("GET"!==r&&"HEAD"!==r)return!1;if(n>=200&&n<300||304===n){const r={etag:t.get("ETag"),"last-modified":t.get("Last-Modified")};return d(e.headers,r)}return!1},getAccepts=e=>(...t)=>new i(e).types(t),onErrorHandler=(t,r,n)=>{const s=n.statusCode=t.code||t.status||500;"string"==typeof t||Buffer.isBuffer(t)?n.end(t):n.end(t.message||e[s])},pushMiddleware=e=>({path:t,handler:r,method:n,handlers:s,type:o})=>{const a=(({path:e,handler:t,method:r})=>({method:r,handler:t||e,path:"string"==typeof e?e:"/"}))({path:t,handler:r,method:n,type:o}),i=s.map(e=>({handler:e}));for(const t of[a,...i])e.push({...t,type:o})};class Router{get(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"GET",type:"route"}),this}post(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"POST",type:"route"}),this}put(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"PUT",type:"route"}),this}patch(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"PATCH",type:"route"}),this}head(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"HEAD",type:"route"}),this}delete(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"DELETE",type:"route"}),this}options(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"OPTIONS",type:"route"}),this}all(e,r,...n){for(const s of t)pushMiddleware(this.middleware)({path:e,handler:r,method:s,handlers:n,type:"route"});return this}use(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:"string"==typeof e?t:e,handlers:r,type:"mw"}),this}}const createETag=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return f(r,{weak:!0})};const json=(e,t)=>(e,...r)=>(t.setHeader("Content-Type","application/json"),"object"==typeof e&&null!=e?t.end(JSON.stringify(e,null,2),...r):"string"==typeof e&&t.end(e,...r),t),send=(e,t)=>r=>{let n=r;if("object"==typeof r&&null!==r)n=JSON.stringify(r,null,2);else if("string"==typeof r){const e=t.getHeader("Content-Type");"string"==typeof e&&t.setHeader("Content-Type",function setCharset(e,t){const r=c(e);return r.parameters.charset=t,u(r)}(e,"utf-8"))}let s;return!t.getHeader("etag")&&(s=createETag(n,"utf8"))&&t.setHeader("etag",s),204!==t.statusCode&&304!==t.statusCode||(t.removeHeader("Content-Type"),t.removeHeader("Content-Length"),t.removeHeader("Transfer-Encoding"),n=""),"HEAD"===e.method&&t.end(""),"object"==typeof r?null===r?t.end(""):Buffer.isBuffer(r)?t.getHeader("Content-Type")||t.setHeader("content-type","application/octet-stream"):json(0,t)(n,"utf8"):t.end(n,"utf8"),t},status=(e,t)=>e=>(t.statusCode=e,t),setCookie=(e,t)=>(r,n,s)=>{const o=e.secret,a=s.signed;if(a&&!o)throw new Error('cookieParser("secret") required for signed cookies');let i="object"==typeof n?"j:"+JSON.stringify(n):String(n);return a&&(i="s:"+h(i,o)),s.maxAge&&(s.expires=new Date(Date.now()+s.maxAge),s.maxAge/=1e3),null==s.path&&(s.path="/"),t.setHeader("Set-Cookie",p(r,String(i),s)),t},clearCookie=(e,t)=>(r,n)=>{const s=Object.assign({},{expires:new Date(1),path:"/"},n);return setCookie(e,t)(r,"",s)},m=/;\s*charset\s*=/,setHeader=(e,t)=>(e,r)=>{if("string"==typeof e){let n=Array.isArray(r)?r.map(String):String(r);if("content-type"===e.toLowerCase()){if(Array.isArray(n))throw new TypeError("Content-Type cannot be set to an Array");if(!m.test(n)){const e=l(n.split(";")[0]);e&&(n+="; charset="+e.toLowerCase())}}t.setHeader(e,n)}else for(const r in e)t.setHeader(r,e[r]);return t},setLocationHeader=(e,t)=>r=>{let n=r;return"back"===r&&(n=e.get("Referrer")||"/"),t.setHeader("Location",encodeURIComponent(n)),t},getResponseHeader=(e,t)=>e=>t.getHeader(e),setLinksHeader=(e,t)=>e=>{let r=t.get("Link")||"";return r&&(r+=", "),t.set("Link",r+Object.keys(e).map(t=>"<"+e[t]+'>; rel="'+t+'"').join(", "))},sendStatus=(t,r)=>t=>{const n=e[t]||String(t);return r.statusCode=t,r.set("Content-Type","text/plain"),r.send(n)},extendMiddleware=({networkExtensions:e,freshnessTesting:t})=>(r,n)=>{if(n.get=getResponseHeader(0,n),e){const e=getProtocol(r),t="https"===e;r.protocol=e,r.secure=t,r.connection=Object.assign(r.socket,{encrypted:t}),r.hostname=getHostname(r)}r.query=getQueryParams(r.url),t&&(r.fresh=getFreshOrStale(r,n),r.stale=!r.fresh),r.get=getRequestHeader(r),r.set=setRequestHeader(r),r.range=getRangeFromHeader(r),r.accepts=getAccepts(r),r.xhr=checkIfXMLHttpRequest(r),n.header=n.set=setHeader(0,n),n.send=send(r,n),n.json=json(0,n),n.status=status(0,n),n.sendStatus=sendStatus(0,n),n.location=setLocationHeader(r,n),n.links=setLinksHeader(0,n),n.cookie=setCookie(r,n),n.clearCookie=clearCookie(r,n)},applyHandler=e=>async(t,r,n)=>{"AsyncFunction"===e[Symbol.toStringTag]?await e(t,r,n):e(t,r,n)};class App extends Router{constructor(e={}){super(),this.locals=Object.create(null),this.middleware=[],this.onError=(null==e?void 0:e.onError)||onErrorHandler,this.noMatchHandler=(null==e?void 0:e.noMatchHandler)||this.onError.bind(null,{code:404}),this.settings=e.settings||{}}async handler(e,t){const r=this.middleware;extendMiddleware(this.settings)(e,t);const s={handler:this.noMatchHandler,type:"mw",path:"/"};r.includes(s)||r.push(s);let o=0;const a=r.length-1,next=r=>{r?this.onError(r,e,t):loop()},handle=e=>async(t,r,s)=>{const{path:o,method:a,handler:i,type:d}=e;if("route"===d){if(t.method===a){const e=t.url.lastIndexOf("?"),a=t.url.slice(0,-1===e?t.url.length:e);n(o).pattern.test(a)?(t.params=getURLParams(t.url,o),t.route=getRouteFromApp(this,i),r.statusCode=200,applyHandler(i)(t,r,s)):loop()}}else t.url.startsWith(o)?applyHandler(i)(t,r,s):loop()};1===r.length&&handle(r[0])(e,t);const loop=()=>{t.writableEnded||o<a&&handle(r[o++])(e,t,next)};loop()}listen(e,t,n="localhost",s){return r((e,t)=>{this.handler(e,t)}).listen(e,n,s,t)}}export{App,Router,applyHandler,checkIfXMLHttpRequest,clearCookie,extendMiddleware,getAccepts,getFreshOrStale,getHostname,getIP,getProtocol,getQueryParams,getRangeFromHeader,getRequestHeader,getResponseHeader,getRouteFromApp,getURLParams,json,send,sendStatus,setCookie,setHeader,setLinksHeader,setLocationHeader,setRequestHeader,status}; | ||
import{STATUS_CODES as e,METHODS as t,createServer as r}from"http";import n from"regexparam";import{parse as s}from"url";import o from"range-parser";import a from"proxy-addr";import i from"es-accepts";import d from"es-fresh";import{sign as h}from"@tinyhttp/cookie-signature";import{lookup as l}from"es-mime-types";import{serialize as p}from"@tinyhttp/cookie";import{format as u,parse as c}from"es-content-type";import f from"@tinyhttp/etag";const compileTrust=e=>"function"==typeof e?e:!0===e?function(){return!0}:"number"==typeof e?(t,r)=>{if(e)return r<e}:("string"==typeof e&&(e=e.split(/ *, */)),a.compile(e||[])),getQueryParams=(e="/")=>s(e,!0).query,getURLParams=(e="/",t="/")=>((e,t)=>{let r=0,n={},s=t.pattern.exec(e);for(;r<t.keys.length;)n[t.keys[r]]=(null==s?void 0:s[++r])||null;return n})(e,n(t)),getRouteFromApp=(e,t)=>e.middleware.find(e=>e.handler.name===t.name),getProtocol=e=>{const t=e.connection.encrypted?"https":"http";if(!compileTrust(e.connection.remoteAddress))return t;const r=e.headers["X-Forwarded-Proto"]||t,n=r.indexOf(",");return-1!==n?r.substring(0,n).trim():r.trim()},getRequestHeader=e=>t=>{const r=t.toLowerCase();switch(r){case"referer":case"referrer":return e.headers.referrer||e.headers.referer;default:return e.headers[r]}},setRequestHeader=e=>(t,r)=>e.headers[t.toLowerCase()]=r,getRangeFromHeader=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},checkIfXMLHttpRequest=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],getHostname=e=>{let t=e.get("X-Forwarded-Host");if(t&&compileTrust(e.connection.remoteAddress)||(t=e.get("Host")),!t)return;const r="["===t[0]?t.indexOf("]")+1:0,n=t.indexOf(":",r);return-1!==n?t.substring(0,n):t},getIP=e=>a(e,compileTrust),getFreshOrStale=(e,t)=>{const r=e.method,n=t.statusCode;if("GET"!==r&&"HEAD"!==r)return!1;if(n>=200&&n<300||304===n){const r={etag:t.get("ETag"),"last-modified":t.get("Last-Modified")};return d(e.headers,r)}return!1},getAccepts=e=>(...t)=>new i(e).types(t),onErrorHandler=t=>(r,n)=>{const s=n.statusCode=t.code||t.status||500;"string"==typeof t||Buffer.isBuffer(t)?n.end(t):n.end(t.message||e[s])},pushMiddleware=e=>({path:t,handler:r,method:n,handlers:s,type:o})=>{const a=(({path:e,handler:t,method:r})=>({method:r,handler:t||e,path:"string"==typeof e?e:"/"}))({path:t,handler:r,method:n,type:o}),i=s.map(e=>({handler:e}));for(const t of[a,...i])e.push({...t,type:o})};class Router{get(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"GET",type:"route"}),this}post(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"POST",type:"route"}),this}put(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"PUT",type:"route"}),this}patch(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"PATCH",type:"route"}),this}head(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"HEAD",type:"route"}),this}delete(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"DELETE",type:"route"}),this}options(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:t,handlers:r,method:"OPTIONS",type:"route"}),this}all(e,r,...n){for(const s of t)pushMiddleware(this.middleware)({path:e,handler:r,method:s,handlers:n,type:"route"});return this}use(e,t,...r){return pushMiddleware(this.middleware)({path:e,handler:"string"==typeof e?t:e,handlers:r,type:"mw"}),this}}const createETag=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return f(r,{weak:!0})};const json=(e,t)=>(e,...r)=>(t.setHeader("Content-Type","application/json"),"object"==typeof e&&null!=e?t.end(JSON.stringify(e,null,2),...r):"string"==typeof e&&t.end(e,...r),t),send=(e,t)=>r=>{let n=r;if("object"==typeof r&&null!==r)n=JSON.stringify(r,null,2);else if("string"==typeof r){const e=t.getHeader("Content-Type");"string"==typeof e&&t.setHeader("Content-Type",function setCharset(e,t){const r=c(e);return r.parameters.charset=t,u(r)}(e,"utf-8"))}let s;return!t.getHeader("etag")&&(s=createETag(n,"utf8"))&&t.setHeader("etag",s),204!==t.statusCode&&304!==t.statusCode||(t.removeHeader("Content-Type"),t.removeHeader("Content-Length"),t.removeHeader("Transfer-Encoding"),n=""),"HEAD"===e.method&&t.end(""),"object"==typeof r?null===r?t.end(""):Buffer.isBuffer(r)?t.getHeader("Content-Type")||t.setHeader("content-type","application/octet-stream"):json(0,t)(n,"utf8"):t.end(n,"utf8"),t},status=(e,t)=>e=>(t.statusCode=e,t),setCookie=(e,t)=>(r,n,s)=>{const o=e.secret,a=s.signed;if(a&&!o)throw new Error('cookieParser("secret") required for signed cookies');let i="object"==typeof n?"j:"+JSON.stringify(n):String(n);return a&&(i="s:"+h(i,o)),s.maxAge&&(s.expires=new Date(Date.now()+s.maxAge),s.maxAge/=1e3),null==s.path&&(s.path="/"),t.setHeader("Set-Cookie",p(r,String(i),s)),t},clearCookie=(e,t)=>(r,n)=>{const s=Object.assign({},{expires:new Date(1),path:"/"},n);return setCookie(e,t)(r,"",s)},m=/;\s*charset\s*=/,setHeader=(e,t)=>(e,r)=>{if("string"==typeof e){let n=Array.isArray(r)?r.map(String):String(r);if("content-type"===e.toLowerCase()){if(Array.isArray(n))throw new TypeError("Content-Type cannot be set to an Array");if(!m.test(n)){const e=l(n.split(";")[0]);e&&(n+="; charset="+e.toLowerCase())}}t.setHeader(e,n)}else for(const r in e)t.setHeader(r,e[r]);return t},setLocationHeader=(e,t)=>r=>{let n=r;return"back"===r&&(n=e.get("Referrer")||"/"),t.setHeader("Location",encodeURIComponent(n)),t},getResponseHeader=(e,t)=>e=>t.getHeader(e),setLinksHeader=(e,t)=>e=>{let r=t.get("Link")||"";return r&&(r+=", "),t.set("Link",r+Object.keys(e).map(t=>"<"+e[t]+'>; rel="'+t+'"').join(", "))},sendStatus=(t,r)=>t=>{const n=e[t]||String(t);return r.statusCode=t,r.set("Content-Type","text/plain"),r.send(n)},extendMiddleware=({networkExtensions:e,freshnessTesting:t})=>(r,n)=>{if(n.get=getResponseHeader(0,n),e){const e=getProtocol(r),t="https"===e;r.protocol=e,r.secure=t,r.connection=Object.assign(r.socket,{encrypted:t}),r.hostname=getHostname(r)}r.query=getQueryParams(r.url),t&&(r.fresh=getFreshOrStale(r,n),r.stale=!r.fresh),r.get=getRequestHeader(r),r.set=setRequestHeader(r),r.range=getRangeFromHeader(r),r.accepts=getAccepts(r),r.xhr=checkIfXMLHttpRequest(r),n.header=n.set=setHeader(0,n),n.send=send(r,n),n.json=json(0,n),n.status=status(0,n),n.sendStatus=sendStatus(0,n),n.location=setLocationHeader(r,n),n.links=setLinksHeader(0,n),n.cookie=setCookie(r,n),n.clearCookie=clearCookie(r,n)},applyHandler=e=>async(t,r,n)=>{"AsyncFunction"===e[Symbol.toStringTag]?await e(t,r,n):e(t,r,n)};class App extends Router{constructor(e={}){super(),this.locals=Object.create(null),this.middleware=[],this.onError=(null==e?void 0:e.onError)||onErrorHandler,this.noMatchHandler=(null==e?void 0:e.noMatchHandler)||this.onError({code:404}),this.settings=e.settings||{}}async handler(e,t){const r=this.middleware;extendMiddleware(this.settings)(e,t);const s={handler:this.noMatchHandler,type:"mw",path:"/"};r.includes(s)||r.push(s);let o=0;const a=r.length-1,next=r=>{r?this.onError(r)(e,t):loop()},handle=e=>async(t,r,s)=>{const{path:o,method:a,handler:i,type:d}=e;if("route"===d){if(t.method===a){const e=t.url.lastIndexOf("?"),a=t.url.slice(0,-1===e?t.url.length:e);n(o).pattern.test(a)?(t.params=getURLParams(t.url,o),t.route=getRouteFromApp(this,i),r.statusCode=200,applyHandler(i)(t,r,s)):loop()}}else t.url.startsWith(o)?applyHandler(i)(t,r,s):loop()};1===r.length&&handle(r[0])(e,t);const loop=()=>{t.writableEnded||o<a&&handle(r[o++])(e,t,next)};loop()}listen(e,t,n="localhost",s){return r((e,t)=>{this.handler(e,t)}).listen(e,n,s,t)}}export{App,Router,applyHandler,checkIfXMLHttpRequest,clearCookie,extendMiddleware,getAccepts,getFreshOrStale,getHostname,getIP,getProtocol,getQueryParams,getRangeFromHeader,getRequestHeader,getResponseHeader,getRouteFromApp,getURLParams,json,send,sendStatus,setCookie,setHeader,setLinksHeader,setLocationHeader,setRequestHeader,status}; |
@@ -1,2 +0,2 @@ | ||
import { ErrorHandler } from './router'; | ||
export declare const onErrorHandler: ErrorHandler; | ||
import { Handler } from './router'; | ||
export declare const onErrorHandler: (err: any) => Handler; |
@@ -7,3 +7,3 @@ import { Request } from './request'; | ||
export declare type Handler = AsyncHandler | SyncHandler; | ||
export declare type ErrorHandler = (err: any, req: Request, res: Response) => void; | ||
export declare type ErrorHandler = (err: any) => (req: Request, res: Response) => void; | ||
declare type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'DELETE' | string; | ||
@@ -10,0 +10,0 @@ declare type MiddlewareType = 'mw' | 'route'; |
{ | ||
"name": "@tinyhttp/app", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"description": "tinyhttp core with App, Request, Response and Router", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31826