@tinyhttp/app
Advanced tools
Comparing version 0.2.6 to 0.2.7
# @tinyhttp/app | ||
## 0.2.7 | ||
### Patch Changes | ||
- Fix Terser options | ||
- Updated dependencies [undefined] | ||
- @tinyhttp/cookie@0.1.3 | ||
- @tinyhttp/cookie-signature@0.1.3 | ||
- @tinyhttp/etag@0.2.3 | ||
## 0.2.6 | ||
@@ -4,0 +14,0 @@ |
@@ -1,1 +0,1 @@ | ||
import{STATUS_CODES as t,METHODS as e,createServer as r}from"http";import n from"regexparam";import{parse as o}from"url";import s from"range-parser";import i from"proxy-addr";import f from"es-accepts";import h from"es-fresh";import{sign as p}from"@tinyhttp/cookie-signature";import{lookup as a}from"es-mime-types";import{serialize as l}from"@tinyhttp/cookie";import{format as u,parse as c}from"es-content-type";import d from"@tinyhttp/etag";const compileTrust=t=>{if("function"==typeof t)return t;if(!0===t)return function(){return!0};if("number"==typeof t)return(e,r)=>{if(t)return r<t};if("string"==typeof t)t=t.split(/ *, */);return i.compile(t||[])},getQueryParams=(t="/")=>o(t,!0).t,getURLParams=(t="/",e="/")=>((t,e)=>{let r=0,n={},o=e.pattern.exec(t);for(;r<e.keys.length;)n[e.keys[r]]=(null==o?void 0:o[++r])||null;return n})(t,n(e)),getRouteFromApp=(t,e)=>t.o.find(t=>t.s.name===e.name),getProtocol=t=>{const e=t.connection.i?"https":"http";if(!compileTrust(t.connection.h))return e;const r=t.headers["p"]||e,n=r.indexOf(",");return-1!==n?r.substring(0,n).trim():r.trim()},getRequestHeader=t=>e=>{const r=e.toLowerCase();switch(r){case"referer":case"referrer":return t.headers.referrer||t.headers.l;default:return t.headers[r]}},setRequestHeader=t=>(e,r)=>t.headers[e.toLowerCase()]=r,getRangeFromHeader=t=>(e,r)=>{const n=t.get("Range");if(n)return s(e,n,r)},checkIfXMLHttpRequest=t=>{if("XMLHttpRequest"===t.headers["u"])return!0;else return!1},getHostname=t=>{let e=t.get("X-Forwarded-Host");if(!e||!compileTrust(t.connection.h))e=t.get("Host");if(!e)return;const r="["===e[0]?e.indexOf("]")+1:0,n=e.indexOf(":",r);return-1!==n?e.substring(0,n):e},getIP=t=>i(t,compileTrust),getFreshOrStale=(t,e)=>{const r=t.method,n=e.m;if("GET"!==r&&"HEAD"!==r)return!1;if(n>=200&&n<300||304===n){const r={g:e.get("ETag"),T:e.get("Last-Modified")};return h(t.headers,r)}return!1},getAccepts=t=>(...e)=>new f(t).types(e),onErrorHandler=(e,r,n)=>{const o=n.m=e.code||e.status||500;if("string"==typeof e||Buffer.isBuffer(e))n.end(e);else n.end(e.message||t[o])},pushMiddleware=t=>({path:e,s:r,method:n,S:o,type:s})=>{const i=(({path:t,s:e,method:r})=>({method:r,s:e||t,path:"string"==typeof t?t:"/"}))({path:e,s:r,method:n,type:s}),f=o.map(t=>({s:t}));for(const e of[i,...f])t.push({...e,type:s})};class Router{get(t,e,...r){pushMiddleware(this.o)({path:t,s:e,S:r,method:"GET",type:"route"});return this}C(t,e,...r){pushMiddleware(this.o)({path:t,s:e,S:r,method:"POST",type:"route"});return this}put(t,e,...r){pushMiddleware(this.o)({path:t,s:e,S:r,method:"PUT",type:"route"});return this}j(t,e,...r){pushMiddleware(this.o)({path:t,s:e,S:r,method:"PATCH",type:"route"});return this}head(t,e,...r){pushMiddleware(this.o)({path:t,s:e,S:r,method:"HEAD",type:"route"});return this}delete(t,e,...r){pushMiddleware(this.o)({path:t,s:e,S:r,method:"DELETE",type:"route"});return this}options(t,e,...r){pushMiddleware(this.o)({path:t,s:e,S:r,method:"OPTIONS",type:"route"});return this}all(t,r,...n){for(const o of e)pushMiddleware(this.o)({path:t,s:r,method:o,S:n,type:"route"});return this}k(t,e,...r){pushMiddleware(this.o)({path:t,s:"string"==typeof t?e:t,S:r,type:"mw"});return this}}const createETag=(t,e)=>{const r=!Buffer.isBuffer(t)?Buffer.from(t,e):t;return d(r,{A:!0})};const json=(t,e)=>(t,...r)=>{e.O("Content-Type","application/json");if("object"==typeof t&&null!=t)e.end(JSON.stringify(t,null,2),...r);else if("string"==typeof t)e.end(t,...r);return e},send=(t,e)=>r=>{let n=r;if("object"==typeof r&&null!==r)n=JSON.stringify(r,null,2);else if("string"==typeof r){const t=e.D("Content-Type");if("string"==typeof t)e.O("Content-Type",function(t,e){const r=c(t);r.H.charset=e;return u(r)}(t,"utf-8"))}let o;if(!e.D("etag")&&(o=createETag(n,"utf8")))e.O("etag",o);if(204===e.m||304===e.m){e.L("Content-Type");e.L("Content-Length");e.L("Transfer-Encoding");n=""}if("HEAD"===t.method)e.end("");if("object"==typeof r)if(null===r)e.end("");else if(Buffer.isBuffer(r)){if(!e.D("Content-Type"))e.O("content-type","application/octet-stream")}else json(0,e)(n,"utf8");else e.end(n,"utf8");return e},status=(t,e)=>t=>{e.m=t;return e},setCookie=(t,e)=>(r,n,o)=>{const s=t.R,i=o.P;if(i&&!s)throw new Error('cookieParser("secret") required for signed cookies');let f="object"==typeof n?"j:"+JSON.stringify(n):String(n);if(i)f="s:"+p(f,s);if(o.B){o.N=new Date(Date.now()+o.B);o.B/=1e3}if(null==o.path)o.path="/";e.O("Set-Cookie",l(r,String(f),o));return e},clearCookie=(t,e)=>(r,n)=>{const o=Object.assign({},{N:new Date(1),path:"/"},n);return setCookie(t,e)(r,"",o)},y=/;\s*charset\s*=/,setHeader=(t,e)=>(t,r)=>{if("string"==typeof t){let n=Array.isArray(r)?r.map(String):String(r);if("content-type"===t.toLowerCase()){if(Array.isArray(n))throw new TypeError("Content-Type cannot be set to an Array");if(!y.test(n)){const t=a(n.split(";")[0]);if(t)n+="; charset="+t.toLowerCase()}}e.O(t,n)}else for(const r in t)e.O(r,t[r]);return e},setLocationHeader=(t,e)=>r=>{let n=r;if("back"===r)n=t.get("Referrer")||"/";e.O("Location",encodeURIComponent(n));return e},getResponseHeader=(t,e)=>t=>e.D(t),setLinksHeader=(t,e)=>t=>{let r=e.get("Link")||"";if(r)r+=", ";return e.set("Link",r+Object.keys(t).map(e=>"<"+t[e]+'>; rel="'+e+'"').join(", "))},sendStatus=(e,r)=>e=>{const n=t[e]||String(e);r.m=e;r.set("Content-Type","text/plain");return r.send(n)},extendMiddleware=({v:t,J:e})=>(r,n)=>{n.get=getResponseHeader(0,n);if(t){const t=getProtocol(r),e="https"===t;r.protocol=t;r.q=e;r.connection=Object.assign(r.F,{i:e});r.hostname=getHostname(r)}r.t=getQueryParams(r.url);if(e){r.G=getFreshOrStale(r,n);r.I=!r.G}r.get=getRequestHeader(r);r.set=setRequestHeader(r);r.range=getRangeFromHeader(r);r.M=getAccepts(r);r.U=checkIfXMLHttpRequest(r);n.X=n.set=setHeader(0,n);n.send=send(r,n);n.json=json(0,n);n.status=status(0,n);n.K=sendStatus(0,n);n.location=setLocationHeader(r,n);n.links=setLinksHeader(0,n);n.cookie=setCookie(r,n);n.V=clearCookie(r,n)},applyHandler=t=>async(e,r,n)=>{if("AsyncFunction"===t[Symbol.toStringTag])await t(e,r,n);else t(e,r,n)};class App extends Router{constructor(t={}){super();this.W=Object.create(null);this.o=[];this.Y=(null==t?void 0:t.Y)||onErrorHandler;this.Z=(null==t?void 0:t.Z)||this.Y.bind(null,{code:404});this.$=t.$||{}}async s(t,e){const r=this.o;extendMiddleware(this.$)(t,e);const o={s:this.Z,type:"mw",path:"/"};if(!r.includes(o))r.push(o);let s=0;const i=r.length-1,next=r=>{if(r)this.Y(r,t,e);else loop()},handle=t=>async(e,r,o)=>{const{path:s,method:i,s:f,type:h}=t;if("route"===h){if(e.method===i){const t=e.url.lastIndexOf("?"),i=e.url.slice(0,-1===t?e.url.length:t);if(n(s).pattern.test(i)){e._=getURLParams(e.url,s);e.tt=getRouteFromApp(this,f);r.m=200;applyHandler(f)(e,r,o)}else loop()}}else if(e.url.startsWith(s))applyHandler(f)(e,r,o);else loop()};if(1===r.length)handle(r[0])(t,e);const loop=()=>{if(!e.et)if(s<i)handle(r[s++])(t,e,next)};loop()}rt(t,e,n="localhost",o){return r((t,e)=>{this.s(t,e)}).rt(t,n,o,e)}}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.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}; |
{ | ||
"name": "@tinyhttp/app", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "tinyhttp core with App, Request, Response and Router", | ||
@@ -47,5 +47,5 @@ "type": "module", | ||
"dependencies": { | ||
"@tinyhttp/cookie": "0.1.2", | ||
"@tinyhttp/cookie-signature": "0.1.2", | ||
"@tinyhttp/etag": "0.2.2", | ||
"@tinyhttp/cookie": "0.1.3", | ||
"@tinyhttp/cookie-signature": "0.1.3", | ||
"@tinyhttp/etag": "0.2.3", | ||
"es-accepts": "^0.0.12", | ||
@@ -52,0 +52,0 @@ "es-content-type": "^0.0.5", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31783
217
2
+ Added@tinyhttp/cookie@0.1.3(transitive)
+ Added@tinyhttp/cookie-signature@0.1.3(transitive)
+ Added@tinyhttp/etag@0.2.3(transitive)
- Removed@tinyhttp/cookie@0.1.2(transitive)
- Removed@tinyhttp/cookie-signature@0.1.2(transitive)
- Removed@tinyhttp/etag@0.2.2(transitive)
Updated@tinyhttp/cookie@0.1.3
Updated@tinyhttp/etag@0.2.3