@tinyhttp/app
Advanced tools
Comparing version 0.1.40 to 0.1.42
@@ -1,1 +0,1 @@ | ||
import{STATUS_CODES as e,createServer as t}from"http";import r from"regexparam";import{parse as n}from"url";import o from"range-parser";import s from"proxy-addr";import a from"@foxify/fresh";import i from"accepts";import{sign as d}from"@tinyhttp/cookie-signature";import h from"mime-types";import l from"@tinyhttp/cookie";import{format as p,parse as u}from"content-type";import c from"@tinyhttp/etag";const f=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(/ *, */)),s.compile(e||[])),m=(e="/")=>n(e,!0).query,y=(e="/",t="/")=>((e,t)=>{let r=0,n={},o=t.pattern.exec(e);for(;r<t.keys.length;)n[t.keys[r]]=(null==o?void 0:o[++r])||null;return n})(e,r(t)),g=(e,t)=>e.middleware.find(e=>e.handler.name===t.name),w=e=>{const t=e.connection.encrypted?"https":"http";if(!f(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()},H=e=>t=>{const r=t.toLowerCase();switch(r){case"referer":case"referrer":return e.headers.referrer||e.headers.referer;default:return e.headers[r]}},C=e=>(t,r)=>e.headers[t.toLowerCase()]=r,T=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},E=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],k=e=>{let t=e.get("X-Forwarded-Host");if(t&&f(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},x=e=>s(e,f),A=(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 a(e.headers,r)}return!1},O=e=>(...t)=>i(e).types(t),b=(t,r,n)=>{const o=n.statusCode=t.code||t.status||500;"string"==typeof t||Buffer.isBuffer(t)?n.end(t):n.end(t.message||e[o])},S=e=>({path:t,handler:r,method:n,handlers:o,type:s})=>{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:s}),i=o.map(e=>({handler:e}));for(const t of[a,...i])e.push({...t,type:s})};class j{get(e,t,...r){return S(this.middleware)({path:e,handler:t,handlers:r,method:"GET",type:"route"}),this}post(e,t,...r){return S(this.middleware)({path:e,handler:t,handlers:r,method:"POST",type:"route"}),this}put(e,t,...r){return S(this.middleware)({path:e,handler:t,handlers:r,method:"PUT",type:"route"}),this}patch(e,t,...r){return S(this.middleware)({path:e,handler:t,handlers:r,method:"PATCH",type:"route"}),this}head(e,t,...r){return S(this.middleware)({path:e,handler:t,handlers:r,method:"HEAD",type:"route"}),this}delete(e,t,...r){return S(this.middleware)({path:e,handler:t,handlers:r,method:"DELETE",type:"route"}),this}options(e,t,...r){return S(this.middleware)({path:e,handler:t,handlers:r,method:"OPTIONS",type:"route"}),this}all(e,t,...r){for(const n of["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"])S(this.middleware)({path:e,handler:t,method:n,handlers:r,type:"route"});return this}use(e,t,...r){return S(this.middleware)({path:e,handler:"string"==typeof e?t:e,handlers:r,type:"mw"}),this}}const L=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return c(r,{weak:!0})};const P=(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),D=(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(e,t){const r=u(e);return r.parameters.charset=t,p(r)}(e,"utf-8"))}let o;return!t.getHeader("etag")&&(o=L(n,"utf8"))&&t.setHeader("etag",o),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"):P(0,t)(n,"utf8"):t.end(n,"utf8"),t},B=(e,t)=>e=>(t.statusCode=e,t),v=(e,t)=>(r,n,o)=>{const s=e.secret,a=o.signed;if(a&&!s)throw new Error('cookieParser("secret") required for signed cookies');let i="object"==typeof n?"j:"+JSON.stringify(n):String(n);return a&&(i="s:"+d(i,s)),o.maxAge&&(o.expires=new Date(Date.now()+o.maxAge),o.maxAge/=1e3),null==o.path&&(o.path="/"),t.setHeader("Set-Cookie",l.serialize(r,String(i),o)),t},q=(e,t)=>(r,n)=>{const o=Object.assign({},{expires:new Date(1),path:"/"},n);return v(e,t)(r,"",o)},M=/;\s*charset\s*=/,N=(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=h.lookup(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},R=(e,t)=>r=>{let n=r;return"back"===r&&(n=e.get("Referrer")||"/"),t.setHeader("Location",encodeURIComponent(n)),t},X=(e,t)=>e=>t.getHeader(e),F=(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(", "))},G=(t,r)=>t=>{const n=e[t]||String(t);return r.statusCode=t,r.set("Content-Type","text/plain"),r.send(n)},I=e=>(t,r)=>{r.get=X(0,r),t.app=e;const n=w(t),o="https"===n;t.protocol=n,t.secure=o,t.connection=Object.assign(t.socket,{encrypted:o}),t.query=m(t.url),t.fresh=A(t,r),t.stale=!t.fresh,t.get=H(t),t.set=C(t),t.range=T(t),t.accepts=O(t),t.xhr=E(t),t.hostname=k(t),r.app=e,r.header=r.set=N(0,r),r.send=D(t,r),r.json=P(0,r),r.status=B(0,r),r.sendStatus=G(0,r),r.location=R(t,r),r.links=F(0,r),r.cookie=v(t,r),r.clearCookie=q(t,r)},J=e=>async(t,r,n)=>{"AsyncFunction"===e[Symbol.toStringTag]?await e(t,r,n):e(t,r,n)};class U extends j{constructor(e={}){super(),this.locals=Object.create(null),this.middleware=[],this.onError=(null==e?void 0:e.onError)||b,this.noMatchHandler=(null==e?void 0:e.noMatchHandler)||this.onError.bind(null,{code:404})}async handler(e,t,n){I(this)(t,n);const o={handler:this.noMatchHandler,type:"mw",path:"/"};e.includes(o)||e.push(o);let s=0;const a=e.length-1,i=e=>{e?this.onError(e,t,n):h()},d=e=>async(t,n,o)=>{const{path:s,method:a,handler:i,type:d}=e;if("route"===d){if(t.method===a){const e=t.url.indexOf("?"),a=t.url.slice(0,-1===e?t.url.length:e);r(s).pattern.test(a)?(t.params=y(t.url,s),t.route=g(this,i),n.statusCode=200,J(i)(t,n,o)):h()}}else t.url.startsWith(s)?J(i)(t,n,o):h()};1===e.length&&d(e[0])(t,n);const h=()=>{n.writableEnded||s<a&&d(e[s++])(t,n,i)};h()}listen(e,r,n="localhost",o){return t((e,t)=>{this.handler(this.middleware,e,t)}).listen(e,n,o,r)}}export{U as App,j as Router,J as applyHandler,E as checkIfXMLHttpRequest,q as clearCookie,I as extendMiddleware,O as getAccepts,A as getFreshOrStale,k as getHostname,x as getIP,w as getProtocol,m as getQueryParams,T as getRangeFromHeader,H as getRequestHeader,X as getResponseHeader,g as getRouteFromApp,y as getURLParams,P as json,D as send,G as sendStatus,v as setCookie,N as setHeader,F as setLinksHeader,R as setLocationHeader,C as setRequestHeader,B as status}; | ||
import{STATUS_CODES as e,METHODS as t,createServer as r}from"http";import n from"regexparam";import{parse as o}from"url";import s from"range-parser";import a from"proxy-addr";import i from"@foxify/fresh";import d from"accepts";import{sign as h}from"@tinyhttp/cookie-signature";import l from"mime-types";import p from"@tinyhttp/cookie";import{format as u,parse as c}from"content-type";import f from"@tinyhttp/etag";const m=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||[])),y=(e="/")=>o(e,!0).query,g=(e="/",t="/")=>((e,t)=>{let r=0,n={},o=t.pattern.exec(e);for(;r<t.keys.length;)n[t.keys[r]]=(null==o?void 0:o[++r])||null;return n})(e,n(t)),w=(e,t)=>e.middleware.find(e=>e.handler.name===t.name),H=e=>{const t=e.connection.encrypted?"https":"http";if(!m(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()},C=e=>t=>{const r=t.toLowerCase();switch(r){case"referer":case"referrer":return e.headers.referrer||e.headers.referer;default:return e.headers[r]}},T=e=>(t,r)=>e.headers[t.toLowerCase()]=r,k=e=>(t,r)=>{const n=e.get("Range");if(n)return s(t,n,r)},x=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],E=e=>{let t=e.get("X-Forwarded-Host");if(t&&m(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},b=e=>a(e,m),A=(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 i(e.headers,r)}return!1},O=e=>(...t)=>d(e).types(t),S=(t,r,n)=>{const o=n.statusCode=t.code||t.status||500;"string"==typeof t||Buffer.isBuffer(t)?n.end(t):n.end(t.message||e[o])},j=e=>({path:t,handler:r,method:n,handlers:o,type:s})=>{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:s}),i=o.map(e=>({handler:e}));for(const t of[a,...i])e.push({...t,type:s})};class L{get(e,t,...r){return j(this.middleware)({path:e,handler:t,handlers:r,method:"GET",type:"route"}),this}post(e,t,...r){return j(this.middleware)({path:e,handler:t,handlers:r,method:"POST",type:"route"}),this}put(e,t,...r){return j(this.middleware)({path:e,handler:t,handlers:r,method:"PUT",type:"route"}),this}patch(e,t,...r){return j(this.middleware)({path:e,handler:t,handlers:r,method:"PATCH",type:"route"}),this}head(e,t,...r){return j(this.middleware)({path:e,handler:t,handlers:r,method:"HEAD",type:"route"}),this}delete(e,t,...r){return j(this.middleware)({path:e,handler:t,handlers:r,method:"DELETE",type:"route"}),this}options(e,t,...r){return j(this.middleware)({path:e,handler:t,handlers:r,method:"OPTIONS",type:"route"}),this}all(e,r,...n){for(const o of t)j(this.middleware)({path:e,handler:r,method:o,handlers:n,type:"route"});return this}use(e,t,...r){return j(this.middleware)({path:e,handler:"string"==typeof e?t:e,handlers:r,type:"mw"}),this}}const B=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return f(r,{weak:!0})};const D=(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),v=(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(e,t){const r=c(e);return r.parameters.charset=t,u(r)}(e,"utf-8"))}let o;return!t.getHeader("etag")&&(o=B(n,"utf8"))&&t.setHeader("etag",o),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"):D(0,t)(n,"utf8"):t.end(n,"utf8"),t},P=(e,t)=>e=>(t.statusCode=e,t),q=(e,t)=>(r,n,o)=>{const s=e.secret,a=o.signed;if(a&&!s)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,s)),o.maxAge&&(o.expires=new Date(Date.now()+o.maxAge),o.maxAge/=1e3),null==o.path&&(o.path="/"),t.setHeader("Set-Cookie",p.serialize(r,String(i),o)),t},M=(e,t)=>(r,n)=>{const o=Object.assign({},{expires:new Date(1),path:"/"},n);return q(e,t)(r,"",o)},R=/;\s*charset\s*=/,N=(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(!R.test(n)){const e=l.lookup(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},X=(e,t)=>r=>{let n=r;return"back"===r&&(n=e.get("Referrer")||"/"),t.setHeader("Location",encodeURIComponent(n)),t},F=(e,t)=>e=>t.getHeader(e),J=(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(", "))},G=(t,r)=>t=>{const n=e[t]||String(t);return r.statusCode=t,r.set("Content-Type","text/plain"),r.send(n)},I=e=>(t,r)=>{r.get=F(0,r),t.app=e;const n=H(t),o="https"===n;t.protocol=n,t.secure=o,t.connection=Object.assign(t.socket,{encrypted:o}),t.query=y(t.url),t.fresh=A(t,r),t.stale=!t.fresh,t.get=C(t),t.set=T(t),t.range=k(t),t.accepts=O(t),t.xhr=x(t),t.hostname=E(t),r.app=e,r.header=r.set=N(0,r),r.send=v(t,r),r.json=D(0,r),r.status=P(0,r),r.sendStatus=G(0,r),r.location=X(t,r),r.links=J(0,r),r.cookie=q(t,r),r.clearCookie=M(t,r)},U=e=>async(t,r,n)=>{"AsyncFunction"===e[Symbol.toStringTag]?await e(t,r,n):e(t,r,n)};class W extends L{constructor(e={}){super(),this.locals=Object.create(null),this.middleware=[],this.onError=(null==e?void 0:e.onError)||S,this.noMatchHandler=(null==e?void 0:e.noMatchHandler)||this.onError.bind(null,{code:404})}async handler(e,t,r){I(this)(t,r);const o={handler:this.noMatchHandler,type:"mw",path:"/"};e.includes(o)||e.push(o);let s=0;const a=e.length-1,i=e=>{e?this.onError(e,t,r):h()},d=e=>async(t,r,o)=>{const{path:s,method:a,handler:i,type:d}=e;if("route"===d){if(t.method===a){const e=t.url.indexOf("?"),a=t.url.slice(0,-1===e?t.url.length:e);n(s).pattern.test(a)?(t.params=g(t.url,s),t.route=w(this,i),r.statusCode=200,U(i)(t,r,o)):h()}}else t.url.startsWith(s)?U(i)(t,r,o):h()};1===e.length&&d(e[0])(t,r);const h=()=>{r.writableEnded||s<a&&d(e[s++])(t,r,i)};h()}listen(e,t,n="localhost",o){return r((e,t)=>{this.handler(this.middleware,e,t)}).listen(e,n,o,t)}}export{W as App,L as Router,U as applyHandler,x as checkIfXMLHttpRequest,M as clearCookie,I as extendMiddleware,O as getAccepts,A as getFreshOrStale,E as getHostname,b as getIP,H as getProtocol,y as getQueryParams,k as getRangeFromHeader,C as getRequestHeader,F as getResponseHeader,w as getRouteFromApp,g as getURLParams,D as json,v as send,G as sendStatus,q as setCookie,N as setHeader,J as setLinksHeader,X as setLocationHeader,T as setRequestHeader,P as status}; |
{ | ||
"name": "@tinyhttp/app", | ||
"version": "0.1.40", | ||
"version": "0.1.42", | ||
"description": "tinyhttp core", | ||
@@ -43,5 +43,5 @@ "type": "module", | ||
"@foxify/fresh": "^1.1.0", | ||
"@tinyhttp/cookie": "0.0.12", | ||
"@tinyhttp/cookie-signature": "0.0.9", | ||
"@tinyhttp/etag": "0.1.25", | ||
"@tinyhttp/cookie": "0.0.13", | ||
"@tinyhttp/cookie-signature": "0.0.10", | ||
"@tinyhttp/etag": "0.1.26", | ||
"accepts": "^1.3.7", | ||
@@ -48,0 +48,0 @@ "content-type": "^1.0.4", |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
32857
7
327
+ Added@tinyhttp/cookie@0.0.13(transitive)
+ Added@tinyhttp/cookie-signature@0.0.10(transitive)
+ Added@tinyhttp/etag@0.1.26(transitive)
- Removed@tinyhttp/cookie@0.0.12(transitive)
- Removed@tinyhttp/cookie-signature@0.0.9(transitive)
- Removed@tinyhttp/etag@0.1.25(transitive)
Updated@tinyhttp/cookie@0.0.13
Updated@tinyhttp/etag@0.1.26