@tinyhttp/app
Advanced tools
Comparing version 0.1.42 to 0.1.43
@@ -22,3 +22,2 @@ /// <reference types="node" /> | ||
interface Response extends ServerResponse { | ||
app: App; | ||
header(field: string | Record<string, unknown>, val: string | any[]): Response; | ||
@@ -102,3 +101,2 @@ set(field: string | Record<string, unknown>, val: string | any[]): Response; | ||
interface Request extends IncomingMessage { | ||
app: App; | ||
query: ParsedUrlQuery; | ||
@@ -121,2 +119,3 @@ params: URLParams; | ||
stale: boolean; | ||
body?: any; | ||
} | ||
@@ -136,3 +135,3 @@ declare const applyHandler: (h: Handler) => Handler; | ||
} | ||
declare const extendMiddleware: (app: App) => (req: Request, res: Response) => void; | ||
declare const extendMiddleware: () => (req: Request, res: Response) => void; | ||
export { applyHandler, App, getQueryParams, URLParams, getURLParams, getRouteFromApp, getProtocol, getRequestHeader, setRequestHeader, getRangeFromHeader, checkIfXMLHttpRequest, getHostname, getIP, getFreshOrStale, getAccepts, Connection, Protocol, Request, json, send, status, setCookie, clearCookie, setHeader, setLocationHeader, getResponseHeader, setLinksHeader, sendStatus, Response, NextFunction, SyncHandler, AsyncHandler, Handler, ErrorHandler, Middleware, Router, extendMiddleware }; |
@@ -1,1 +0,1 @@ | ||
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}; | ||
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)=>{t.get=F(0,t);const r=H(e),n="https"===r;e.protocol=r,e.secure=n,e.connection=Object.assign(e.socket,{encrypted:n}),e.query=y(e.url),e.fresh=A(e,t),e.stale=!e.fresh,e.get=C(e),e.set=T(e),e.range=k(e),e.accepts=O(e),e.xhr=x(e),e.hostname=E(e),t.header=t.set=N(0,t),t.send=v(e,t),t.json=D(0,t),t.status=P(0,t),t.sendStatus=G(0,t),t.location=X(e,t),t.links=J(0,t),t.cookie=q(e,t),t.clearCookie=M(e,t)},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()(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.42", | ||
"version": "0.1.43", | ||
"description": "tinyhttp core", | ||
@@ -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
25913
6
191