@tinyhttp/app
Advanced tools
Comparing version 0.1.25 to 0.1.26
@@ -85,3 +85,3 @@ /// <reference types="node" /> | ||
use(handler: Handler): this; | ||
handle(req: Request, res: Response): void; | ||
handle(req: Request, res: Response): Promise<void>; | ||
listen(port?: number, cb?: () => void, host?: string, backlog?: number): import("http").Server; | ||
@@ -88,0 +88,0 @@ } |
@@ -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"encodeurl";import{sign as i}from"@tinyhttp/cookie-signature";import d from"mime";import u from"@tinyhttp/cookie";import{format as l,parse as h}from"content-type";import p 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||[])),c=(e="/")=>n(e,!0).query,m=(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),y=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=>e.headers[t],w=e=>(t,r)=>e.headers[t]=r,C=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},T=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],A=e=>{let t=e.get("X-Forwarded-Host");if(t&&f(e.connection.remoteAddress)||(t=e.get("Host")),t){var r="["===t[0]?t.indexOf("]")+1:0,n=t.indexOf(":",r);return-1!==n?t.substring(0,n):t}},b=e=>s(e,f),x=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return p(r,{weak:!0})};const k=(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),E=(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=h(e);return r.parameters.charset=t,l(r)}(e,"utf-8"))}let o;return!t.getHeader("etag")&&(o=x(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"):k(0,t)(n,"utf8"):t.end(n,"utf8"),t},O=(e,t)=>e=>(t.statusCode=e,t),S=(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 d="object"==typeof n?"j:"+JSON.stringify(n):String(n);return a&&(d="s:"+i(d,s)),o.maxAge&&(o.expires=new Date(Date.now()+o.maxAge),o.maxAge/=1e3),null==o.path&&(o.path="/"),t.setHeader("Set-Cookie",u.serialize(r,String(d),o)),t},j=(e,t)=>(r,n)=>{const o=Object.assign({},{expires:new Date(1),path:"/"},n);return S(e,t)(r,"",o)},P=/;\s*charset\s*=/,v=(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(!P.test(n)){const e=d.getType(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},B=(e,t)=>r=>{let n=r;return"back"===r&&(n=e.get("Referrer")||"/"),t.setHeader("Location",a(n))},D=()=>(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))},q=["GET","POST","PUT","PATCH","HEAD"],L=(t,r,n,o)=>{let s=n.statusCode=t.code||t.status||500;"string"==typeof t||Buffer.isBuffer(t)?n.end(t):n.end(t.message||e[s])},M=({url:e,handler:t,method:r})=>({method:r,handler:t||e,url:"string"==typeof e?e:"*"});class N{constructor(e={noMatchHandler:(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))}}){this.middleware=[],this.noMatchHandler=e.noMatchHandler}get(e,t){return this.middleware.push(M({url:e,handler:t,method:"GET"})),this}post(e,t){return this.middleware.push(M({url:e,handler:t,method:"POST"})),this}put(e,t){return this.middleware.push(M({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.middleware.push(M({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.middleware.push(M({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of q)this.middleware.push(M({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}handle(e,t){var n;e.app=this;const o=y(e),s="https"===o;e.protocol=o,e.secure=s,e.connection=Object.assign(e.socket,{encrypted:s}),e.query=c(e.url),e.get=H(e),e.set=w(e),e.range=C(e),e.xhr=T(e),e.hostname=A(e),t.app=this,t.header=t.set=v(0,t),t.send=E(e,t),t.json=k(0,t),t.status=O(0,t),t.cookie=S(e,t),t.clearCookie=j(e,t),null===(n=this.middleware)||void 0===n||n.forEach(({url:n,method:o,handler:s})=>{t.writableEnded||(o&&e.method===o?n&&e.url&&r(n).pattern.test(e.url)&&(e.params=m(e.url,n),e.route=g(this,s),t.statusCode=200,s(e,t)):s(e,t))}),this.noMatchHandler(e,t)}listen(e,r,n="localhost",o){return t((e,t)=>{this.handle(e,t)}).listen(e,n,o,r)}}export{N as App,q as METHODS,T as checkIfXMLHttpRequest,j as clearCookie,A as getHostname,b as getIP,y as getProtocol,c as getQueryParams,C as getRangeFromHeader,H as getRequestHeader,g as getRouteFromApp,m as getURLParams,k as json,D as notFound,L as onError,E as send,S as setCookie,v as setHeader,B as setLocationHeader,w as setRequestHeader,O as status}; | ||
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"encodeurl";import{sign as i}from"@tinyhttp/cookie-signature";import d from"mime";import u from"@tinyhttp/cookie";import{format as l,parse as h}from"content-type";import p from"@tinyhttp/etag";const c=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||[])),f=(e="/")=>n(e,!0).query,m=(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),y=e=>{const t=e.connection.encrypted?"https":"http";if(!c(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()},w=e=>t=>e.headers[t],H=e=>(t,r)=>e.headers[t]=r,C=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},T=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],A=e=>{let t=e.get("X-Forwarded-Host");if(t&&c(e.connection.remoteAddress)||(t=e.get("Host")),t){var r="["===t[0]?t.indexOf("]")+1:0,n=t.indexOf(":",r);return-1!==n?t.substring(0,n):t}},b=e=>s(e,c),S=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return p(r,{weak:!0})};const x=(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),k=(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=h(e);return r.parameters.charset=t,l(r)}(e,"utf-8"))}let o;return!t.getHeader("etag")&&(o=S(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"):x(0,t)(n,"utf8"):t.end(n,"utf8"),t},E=(e,t)=>e=>(t.statusCode=e,t),O=(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 d="object"==typeof n?"j:"+JSON.stringify(n):String(n);return a&&(d="s:"+i(d,s)),o.maxAge&&(o.expires=new Date(Date.now()+o.maxAge),o.maxAge/=1e3),null==o.path&&(o.path="/"),t.setHeader("Set-Cookie",u.serialize(r,String(d),o)),t},j=(e,t)=>(r,n)=>{const o=Object.assign({},{expires:new Date(1),path:"/"},n);return O(e,t)(r,"",o)},P=/;\s*charset\s*=/,B=(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(!P.test(n)){const e=d.getType(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},D=(e,t)=>r=>{let n=r;return"back"===r&&(n=e.get("Referrer")||"/"),t.setHeader("Location",a(n))},q=()=>(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))},v=["GET","POST","PUT","PATCH","HEAD"],L=(t,r,n,o)=>{let s=n.statusCode=t.code||t.status||500;"string"==typeof t||Buffer.isBuffer(t)?n.end(t):n.end(t.message||e[s])},M=({url:e,handler:t,method:r})=>({method:r,handler:t||e,url:"string"==typeof e?e:"*"});class N{constructor(e={noMatchHandler:(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))}}){this.middleware=[],this.noMatchHandler=e.noMatchHandler}get(e,t){return this.middleware.push(M({url:e,handler:t,method:"GET"})),this}post(e,t){return this.middleware.push(M({url:e,handler:t,method:"POST"})),this}put(e,t){return this.middleware.push(M({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.middleware.push(M({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.middleware.push(M({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of v)this.middleware.push(M({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}async handle(e,t){e.app=this;const n=y(e),o="https"===n;e.protocol=n,e.secure=o,e.connection=Object.assign(e.socket,{encrypted:o}),e.query=f(e.url),e.get=w(e),e.set=H(e),e.range=C(e),e.xhr=T(e),e.hostname=A(e),t.app=this,t.header=t.set=B(0,t),t.send=k(e,t),t.json=x(0,t),t.status=E(0,t),t.cookie=O(e,t),t.clearCookie=j(e,t);const s=[...this.middleware,{handler:this.noMatchHandler}];for(const n of s){const{url:o,method:s,handler:a}=n;t.writableEnded||(s&&e.method===s?o&&e.url&&r(o).pattern.test(e.url)&&(e.params=m(e.url,o),e.route=g(this,a),t.statusCode=200,"AsyncFunction"===a[Symbol.toStringTag]&&await a(e,t),a(e,t)):("AsyncFunction"===a[Symbol.toStringTag]&&await a(e,t),a(e,t)))}}listen(e,r,n="localhost",o){return t((e,t)=>{this.handle(e,t)}).listen(e,n,o,r)}}export{N as App,v as METHODS,T as checkIfXMLHttpRequest,j as clearCookie,A as getHostname,b as getIP,y as getProtocol,f as getQueryParams,C as getRangeFromHeader,w as getRequestHeader,g as getRouteFromApp,m as getURLParams,x as json,q as notFound,L as onError,k as send,O as setCookie,B as setHeader,D as setLocationHeader,H as setRequestHeader,E as status}; |
@@ -1,1 +0,1 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("http"),r=e(require("regexparam")),s=require("url"),n=e(require("range-parser")),o=e(require("proxy-addr")),a=e(require("encodeurl")),i=require("@tinyhttp/cookie-signature"),d=e(require("mime")),u=e(require("@tinyhttp/cookie")),p=require("content-type"),l=e(require("@tinyhttp/etag"));const h=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(/ *, */)),o.compile(e||[])),c=(e="/")=>s.parse(e,!0).query,f=(e="/",t="/")=>((e,t)=>{let r=0,s={},n=t.pattern.exec(e);for(;r<t.keys.length;)s[t.keys[r]]=(null==n?void 0:n[++r])||null;return s})(e,r(t)),g=(e,t)=>e.middleware.find(e=>e.handler.name===t.name),m=e=>{const t=e.connection.encrypted?"https":"http";if(!h(e.connection.remoteAddress))return t;const r=e.headers["X-Forwarded-Proto"]||t,s=r.indexOf(",");return-1!==s?r.substring(0,s).trim():r.trim()},y=e=>t=>e.headers[t],x=e=>(t,r)=>e.headers[t]=r,H=e=>(t,r)=>{const s=e.get("Range");if(s)return n(t,s,r)},w=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],C=e=>{let t=e.get("X-Forwarded-Host");if(t&&h(e.connection.remoteAddress)||(t=e.get("Host")),t){var r="["===t[0]?t.indexOf("]")+1:0,s=t.indexOf(":",r);return-1!==s?t.substring(0,s):t}},T=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return l(r,{weak:!0})};const q=(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),A=(e,t)=>r=>{let s=r;if("object"==typeof r&&"null"!==r)s=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=p.parse(e);return r.parameters.charset=t,p.format(r)}(e,"utf-8"))}let n;return!t.getHeader("etag")&&(n=T(s,"utf8"))&&t.setHeader("etag",n),204!==t.statusCode&&304!==t.statusCode||(t.removeHeader("Content-Type"),t.removeHeader("Content-Length"),t.removeHeader("Transfer-Encoding"),s=""),"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"):q(0,t)(s,"utf8"):t.end(s,"utf8"),t},b=(e,t)=>e=>(t.statusCode=e,t),k=(e,t)=>(r,s,n)=>{const o=e.secret,a=n.signed;if(a&&!o)throw new Error('cookieParser("secret") required for signed cookies');let d="object"==typeof s?"j:"+JSON.stringify(s):String(s);return a&&(d="s:"+i.sign(d,o)),n.maxAge&&(n.expires=new Date(Date.now()+n.maxAge),n.maxAge/=1e3),null==n.path&&(n.path="/"),t.setHeader("Set-Cookie",u.serialize(r,String(d),n)),t},E=(e,t)=>(r,s)=>{const n=Object.assign({},{expires:new Date(1),path:"/"},s);return k(e,t)(r,"",n)},S=/;\s*charset\s*=/,O=(e,t)=>(e,r)=>{if("string"==typeof e){let s=Array.isArray(r)?r.map(String):String(r);if("content-type"===e.toLowerCase()){if(Array.isArray(s))throw new TypeError("Content-Type cannot be set to an Array");if(!S.test(s)){const e=d.getType(s.split(";")[0]);e&&(s+="; charset="+e.toLowerCase())}}t.setHeader(e,s)}else for(const r in e)t.setHeader(r,e[r]);return t},P=()=>(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))},j=["GET","POST","PUT","PATCH","HEAD"],v=({url:e,handler:t,method:r})=>({method:r,handler:t||e,url:"string"==typeof e?e:"*"});exports.App=class{constructor(e={noMatchHandler:(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))}}){this.middleware=[],this.noMatchHandler=e.noMatchHandler}get(e,t){return this.middleware.push(v({url:e,handler:t,method:"GET"})),this}post(e,t){return this.middleware.push(v({url:e,handler:t,method:"POST"})),this}put(e,t){return this.middleware.push(v({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.middleware.push(v({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.middleware.push(v({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of j)this.middleware.push(v({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}handle(e,t){var s;e.app=this;const n=m(e),o="https"===n;e.protocol=n,e.secure=o,e.connection=Object.assign(e.socket,{encrypted:o}),e.query=c(e.url),e.get=y(e),e.set=x(e),e.range=H(e),e.xhr=w(e),e.hostname=C(e),t.app=this,t.header=t.set=O(0,t),t.send=A(e,t),t.json=q(0,t),t.status=b(0,t),t.cookie=k(e,t),t.clearCookie=E(e,t),null===(s=this.middleware)||void 0===s||s.forEach(({url:s,method:n,handler:o})=>{t.writableEnded||(n&&e.method===n?s&&e.url&&r(s).pattern.test(e.url)&&(e.params=f(e.url,s),e.route=g(this,o),t.statusCode=200,o(e,t)):o(e,t))}),this.noMatchHandler(e,t)}listen(e,r,s="localhost",n){return t.createServer((e,t)=>{this.handle(e,t)}).listen(e,s,n,r)}},exports.METHODS=j,exports.checkIfXMLHttpRequest=w,exports.clearCookie=E,exports.getHostname=C,exports.getIP=e=>o(e,h),exports.getProtocol=m,exports.getQueryParams=c,exports.getRangeFromHeader=H,exports.getRequestHeader=y,exports.getRouteFromApp=g,exports.getURLParams=f,exports.json=q,exports.notFound=P,exports.onError=(e,r,s,n)=>{let o=s.statusCode=e.code||e.status||500;"string"==typeof e||Buffer.isBuffer(e)?s.end(e):s.end(e.message||t.STATUS_CODES[o])},exports.send=A,exports.setCookie=k,exports.setHeader=O,exports.setLocationHeader=(e,t)=>r=>{let s=r;return"back"===r&&(s=e.get("Referrer")||"/"),t.setHeader("Location",a(s))},exports.setRequestHeader=x,exports.status=b; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("http"),r=e(require("regexparam")),n=require("url"),s=e(require("range-parser")),o=e(require("proxy-addr")),a=e(require("encodeurl")),i=require("@tinyhttp/cookie-signature"),d=e(require("mime")),u=e(require("@tinyhttp/cookie")),p=require("content-type"),l=e(require("@tinyhttp/etag"));const c=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(/ *, */)),o.compile(e||[])),h=(e="/")=>n.parse(e,!0).query,f=(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,r(t)),g=(e,t)=>e.middleware.find(e=>e.handler.name===t.name),m=e=>{const t=e.connection.encrypted?"https":"http";if(!c(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()},y=e=>t=>e.headers[t],x=e=>(t,r)=>e.headers[t]=r,H=e=>(t,r)=>{const n=e.get("Range");if(n)return s(t,n,r)},w=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],C=e=>{let t=e.get("X-Forwarded-Host");if(t&&c(e.connection.remoteAddress)||(t=e.get("Host")),t){var r="["===t[0]?t.indexOf("]")+1:0,n=t.indexOf(":",r);return-1!==n?t.substring(0,n):t}},T=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return l(r,{weak:!0})};const A=(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),q=(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=p.parse(e);return r.parameters.charset=t,p.format(r)}(e,"utf-8"))}let s;return!t.getHeader("etag")&&(s=T(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"):A(0,t)(n,"utf8"):t.end(n,"utf8"),t},S=(e,t)=>e=>(t.statusCode=e,t),b=(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 d="object"==typeof n?"j:"+JSON.stringify(n):String(n);return a&&(d="s:"+i.sign(d,o)),s.maxAge&&(s.expires=new Date(Date.now()+s.maxAge),s.maxAge/=1e3),null==s.path&&(s.path="/"),t.setHeader("Set-Cookie",u.serialize(r,String(d),s)),t},k=(e,t)=>(r,n)=>{const s=Object.assign({},{expires:new Date(1),path:"/"},n);return b(e,t)(r,"",s)},E=/;\s*charset\s*=/,O=(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(!E.test(n)){const e=d.getType(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},P=()=>(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))},j=["GET","POST","PUT","PATCH","HEAD"],R=({url:e,handler:t,method:r})=>({method:r,handler:t||e,url:"string"==typeof e?e:"*"});exports.App=class{constructor(e={noMatchHandler:(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))}}){this.middleware=[],this.noMatchHandler=e.noMatchHandler}get(e,t){return this.middleware.push(R({url:e,handler:t,method:"GET"})),this}post(e,t){return this.middleware.push(R({url:e,handler:t,method:"POST"})),this}put(e,t){return this.middleware.push(R({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.middleware.push(R({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.middleware.push(R({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of j)this.middleware.push(R({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}async handle(e,t){e.app=this;const n=m(e),s="https"===n;e.protocol=n,e.secure=s,e.connection=Object.assign(e.socket,{encrypted:s}),e.query=h(e.url),e.get=y(e),e.set=x(e),e.range=H(e),e.xhr=w(e),e.hostname=C(e),t.app=this,t.header=t.set=O(0,t),t.send=q(e,t),t.json=A(0,t),t.status=S(0,t),t.cookie=b(e,t),t.clearCookie=k(e,t);const o=[...this.middleware,{handler:this.noMatchHandler}];for(const n of o){const{url:s,method:o,handler:a}=n;t.writableEnded||(o&&e.method===o?s&&e.url&&r(s).pattern.test(e.url)&&(e.params=f(e.url,s),e.route=g(this,a),t.statusCode=200,"AsyncFunction"===a[Symbol.toStringTag]&&await a(e,t),a(e,t)):("AsyncFunction"===a[Symbol.toStringTag]&&await a(e,t),a(e,t)))}}listen(e,r,n="localhost",s){return t.createServer((e,t)=>{this.handle(e,t)}).listen(e,n,s,r)}},exports.METHODS=j,exports.checkIfXMLHttpRequest=w,exports.clearCookie=k,exports.getHostname=C,exports.getIP=e=>o(e,c),exports.getProtocol=m,exports.getQueryParams=h,exports.getRangeFromHeader=H,exports.getRequestHeader=y,exports.getRouteFromApp=g,exports.getURLParams=f,exports.json=A,exports.notFound=P,exports.onError=(e,r,n,s)=>{let o=n.statusCode=e.code||e.status||500;"string"==typeof e||Buffer.isBuffer(e)?n.end(e):n.end(e.message||t.STATUS_CODES[o])},exports.send=q,exports.setCookie=b,exports.setHeader=O,exports.setLocationHeader=(e,t)=>r=>{let n=r;return"back"===r&&(n=e.get("Referrer")||"/"),t.setHeader("Location",a(n))},exports.setRequestHeader=x,exports.status=S; |
{ | ||
"name": "@tinyhttp/app", | ||
"version": "0.1.25", | ||
"version": "0.1.26", | ||
"description": "tinyhttp core", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/talentlessguy/tinyhttp", |
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
19168
133