@tinyhttp/app
Advanced tools
Comparing version 0.1.21 to 0.1.22
@@ -74,3 +74,5 @@ /// <reference types="node" /> | ||
noMatchHandler: Handler; | ||
constructor(noMatchHandler?: Handler); | ||
constructor(options?: Partial<{ | ||
noMatchHandler: Handler; | ||
}>); | ||
get(url: string | Handler, handler?: Handler): this; | ||
@@ -77,0 +79,0 @@ post(url: string | Handler, handler?: Handler): this; |
@@ -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 h,parse as l}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.routes.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.toLowerCase()],C=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},w=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],T=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}},A=e=>s(e,f),b=(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=l(e);return r.parameters.charset=t,h(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"):x(0,t)(n,"utf8"):t.end(n,"utf8"),t},E=(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},O=(e,t)=>(r,n)=>{const o=Object.assign({},{expires:new Date(1),path:"/"},n);return S(e,t)(r,"",o)},j=/;\s*charset\s*=/,P=(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(!j.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"))},L=["GET","POST","PUT","PATCH","HEAD"],q=(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])},v=({url:e,handler:t,method:r})=>({method:r,handler:t||e,url:"string"==typeof e?e:"*"});class N{constructor(e=((e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))})){this.routes=[],this.middleware=[],this.noMatchHandler=e}get(e,t){return this.routes.push(v({url:e,handler:t,method:"GET"})),this}post(e,t){return this.routes.push(v({url:e,handler:t,method:"POST"})),this}put(e,t){return this.routes.push(v({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.routes.push(v({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.routes.push(v({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of L)this.routes.push(v({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}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=c(e.url),e.get=H(e),e.range=C(e),e.xhr=w(e),e.hostname=T(e),t.app=this,t.header=t.set=P(0,t),t.send=k(e,t),t.json=x(0,t),t.status=E(0,t),t.cookie=S(e,t),t.clearCookie=O(e,t);for(const n of this.routes){const{url:o,method:s,handler:a}=n;t.writableEnded||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,a(e,t))}let s=this.middleware.filter(e=>"logger"!==e.handler.name);s.push({handler:this.noMatchHandler});const a=this.middleware.find(e=>"logger"===e.handler.name);a&&s.push(a),s.map(({handler:r})=>{r(e,t)})}listen(e,r=(()=>console.log(`Started on http://${n}:${e}`)),n="localhost",o){return t((e,t)=>{this.handle(e,t)}).listen(e,n,o,r)}}export{N as App,L as METHODS,w as checkIfXMLHttpRequest,O as clearCookie,H as getHeader,T as getHostname,A as getIP,y as getProtocol,c as getQueryParams,C as getRangeFromHeader,g as getRouteFromApp,m as getURLParams,x as json,D as notFound,q as onError,k as send,S as setCookie,P as setHeader,B as setLocationHeader,E 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 h,parse as l}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.routes.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.toLowerCase()],C=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},w=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],T=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}},A=e=>s(e,f),b=(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=l(e);return r.parameters.charset=t,h(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"):x(0,t)(n,"utf8"):t.end(n,"utf8"),t},E=(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},O=(e,t)=>(r,n)=>{const o=Object.assign({},{expires:new Date(1),path:"/"},n);return S(e,t)(r,"",o)},j=/;\s*charset\s*=/,P=(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(!j.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"))},L=["GET","POST","PUT","PATCH","HEAD"],q=(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])},v=({url:e,handler:t,method:r})=>({method:r,handler:t||e,url:"string"==typeof e?e:"*"});class M{constructor(e={noMatchHandler:(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))}}){this.routes=[],this.middleware=[],this.noMatchHandler=e.noMatchHandler}get(e,t){return this.routes.push(v({url:e,handler:t,method:"GET"})),this}post(e,t){return this.routes.push(v({url:e,handler:t,method:"POST"})),this}put(e,t){return this.routes.push(v({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.routes.push(v({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.routes.push(v({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of L)this.routes.push(v({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}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=c(e.url),e.get=H(e),e.range=C(e),e.xhr=w(e),e.hostname=T(e),t.app=this,t.header=t.set=P(0,t),t.send=k(e,t),t.json=x(0,t),t.status=E(0,t),t.cookie=S(e,t),t.clearCookie=O(e,t);for(const n of this.routes){const{url:o,method:s,handler:a}=n;t.writableEnded||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,a(e,t))}let s=this.middleware.filter(e=>"logger"!==e.handler.name);s.push({handler:this.noMatchHandler});const a=this.middleware.find(e=>"logger"===e.handler.name);a&&s.push(a),s.map(({handler:r})=>{r(e,t)})}listen(e,r=(()=>console.log(`Started on http://${n}:${e}`)),n="localhost",o){return t((e,t)=>{this.handle(e,t)}).listen(e,n,o,r)}}export{M as App,L as METHODS,w as checkIfXMLHttpRequest,O as clearCookie,H as getHeader,T as getHostname,A as getIP,y as getProtocol,c as getQueryParams,C as getRangeFromHeader,g as getRouteFromApp,m as getURLParams,x as json,D as notFound,q as onError,k as send,S as setCookie,P as setHeader,B as setLocationHeader,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"),o=e(require("range-parser")),n=e(require("proxy-addr")),a=e(require("encodeurl")),i=require("@tinyhttp/cookie-signature"),u=e(require("mime")),d=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(/ *, */)),n.compile(e||[])),c=(e="/")=>s.parse(e,!0).query,f=(e="/",t="/")=>((e,t)=>{let r=0,s={},o=t.pattern.exec(e);for(;r<t.keys.length;)s[t.keys[r]]=(null==o?void 0:o[++r])||null;return s})(e,r(t)),g=(e,t)=>e.routes.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.toLowerCase()],x=e=>(t,r)=>{const s=e.get("Range");if(s)return o(t,s,r)},H=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}},w=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return l(r,{weak:!0})};const T=(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 o;return!t.getHeader("etag")&&(o=w(s,"utf8"))&&t.setHeader("etag",o),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"):T(0,t)(s,"utf8"):t.end(s,"utf8"),t},q=(e,t)=>e=>(t.statusCode=e,t),b=(e,t)=>(r,s,o)=>{const n=e.secret,a=o.signed;if(a&&!n)throw new Error('cookieParser("secret") required for signed cookies');let u="object"==typeof s?"j:"+JSON.stringify(s):String(s);return a&&(u="s:"+i.sign(u,n)),o.maxAge&&(o.expires=new Date(Date.now()+o.maxAge),o.maxAge/=1e3),null==o.path&&(o.path="/"),t.setHeader("Set-Cookie",d.serialize(r,String(u),o)),t},S=(e,t)=>(r,s)=>{const o=Object.assign({},{expires:new Date(1),path:"/"},s);return b(e,t)(r,"",o)},k=/;\s*charset\s*=/,E=(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(!k.test(s)){const e=u.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},O=()=>(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))},P=["GET","POST","PUT","PATCH","HEAD"],j=({url:e,handler:t,method:r})=>({method:r,handler:t||e,url:"string"==typeof e?e:"*"});exports.App=class{constructor(e=((e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))})){this.routes=[],this.middleware=[],this.noMatchHandler=e}get(e,t){return this.routes.push(j({url:e,handler:t,method:"GET"})),this}post(e,t){return this.routes.push(j({url:e,handler:t,method:"POST"})),this}put(e,t){return this.routes.push(j({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.routes.push(j({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.routes.push(j({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of P)this.routes.push(j({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}handle(e,t){e.app=this;const s=m(e),o="https"===s;e.protocol=s,e.secure=o,e.connection=Object.assign(e.socket,{encrypted:o}),e.query=c(e.url),e.get=y(e),e.range=x(e),e.xhr=H(e),e.hostname=C(e),t.app=this,t.header=t.set=E(0,t),t.send=A(e,t),t.json=T(0,t),t.status=q(0,t),t.cookie=b(e,t),t.clearCookie=S(e,t);for(const s of this.routes){const{url:o,method:n,handler:a}=s;t.writableEnded||e.method===n&&o&&e.url&&r(o).pattern.test(e.url)&&(e.params=f(e.url,o),e.route=g(this,a),t.statusCode=200,a(e,t))}let n=this.middleware.filter(e=>"logger"!==e.handler.name);n.push({handler:this.noMatchHandler});const a=this.middleware.find(e=>"logger"===e.handler.name);a&&n.push(a),n.map(({handler:r})=>{r(e,t)})}listen(e,r=(()=>console.log(`Started on http://${s}:${e}`)),s="localhost",o){return t.createServer((e,t)=>{this.handle(e,t)}).listen(e,s,o,r)}},exports.METHODS=P,exports.checkIfXMLHttpRequest=H,exports.clearCookie=S,exports.getHeader=y,exports.getHostname=C,exports.getIP=e=>n(e,h),exports.getProtocol=m,exports.getQueryParams=c,exports.getRangeFromHeader=x,exports.getRouteFromApp=g,exports.getURLParams=f,exports.json=T,exports.notFound=O,exports.onError=(e,r,s,o)=>{let n=s.statusCode=e.code||e.status||500;"string"==typeof e||Buffer.isBuffer(e)?s.end(e):s.end(e.message||t.STATUS_CODES[n])},exports.send=A,exports.setCookie=b,exports.setHeader=E,exports.setLocationHeader=(e,t)=>r=>{let s=r;return"back"===r&&(s=e.get("Referrer")||"/"),t.setHeader("Location",a(s))},exports.status=q; | ||
"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")),o=require("url"),s=e(require("range-parser")),n=e(require("proxy-addr")),a=e(require("encodeurl")),i=require("@tinyhttp/cookie-signature"),u=e(require("mime")),d=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(/ *, */)),n.compile(e||[])),c=(e="/")=>o.parse(e,!0).query,f=(e="/",t="/")=>((e,t)=>{let r=0,o={},s=t.pattern.exec(e);for(;r<t.keys.length;)o[t.keys[r]]=(null==s?void 0:s[++r])||null;return o})(e,r(t)),g=(e,t)=>e.routes.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,o=r.indexOf(",");return-1!==o?r.substring(0,o).trim():r.trim()},y=e=>t=>e.headers[t.toLowerCase()],x=e=>(t,r)=>{const o=e.get("Range");if(o)return s(t,o,r)},H=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,o=t.indexOf(":",r);return-1!==o?t.substring(0,o):t}},w=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return l(r,{weak:!0})};const T=(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 o=r;if("object"==typeof r&&"null"!==r)o=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=w(o,"utf8"))&&t.setHeader("etag",s),204!==t.statusCode&&304!==t.statusCode||(t.removeHeader("Content-Type"),t.removeHeader("Content-Length"),t.removeHeader("Transfer-Encoding"),o=""),"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"):T(0,t)(o,"utf8"):t.end(o,"utf8"),t},q=(e,t)=>e=>(t.statusCode=e,t),b=(e,t)=>(r,o,s)=>{const n=e.secret,a=s.signed;if(a&&!n)throw new Error('cookieParser("secret") required for signed cookies');let u="object"==typeof o?"j:"+JSON.stringify(o):String(o);return a&&(u="s:"+i.sign(u,n)),s.maxAge&&(s.expires=new Date(Date.now()+s.maxAge),s.maxAge/=1e3),null==s.path&&(s.path="/"),t.setHeader("Set-Cookie",d.serialize(r,String(u),s)),t},S=(e,t)=>(r,o)=>{const s=Object.assign({},{expires:new Date(1),path:"/"},o);return b(e,t)(r,"",s)},k=/;\s*charset\s*=/,E=(e,t)=>(e,r)=>{if("string"==typeof e){let o=Array.isArray(r)?r.map(String):String(r);if("content-type"===e.toLowerCase()){if(Array.isArray(o))throw new TypeError("Content-Type cannot be set to an Array");if(!k.test(o)){const e=u.getType(o.split(";")[0]);e&&(o+="; charset="+e.toLowerCase())}}t.setHeader(e,o)}else for(const r in e)t.setHeader(r,e[r]);return t},O=()=>(e,t)=>{t.writableEnded||(t.statusCode=404,t.end("Not found"))},P=["GET","POST","PUT","PATCH","HEAD"],j=({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.routes=[],this.middleware=[],this.noMatchHandler=e.noMatchHandler}get(e,t){return this.routes.push(j({url:e,handler:t,method:"GET"})),this}post(e,t){return this.routes.push(j({url:e,handler:t,method:"POST"})),this}put(e,t){return this.routes.push(j({url:e,handler:t,method:"PUT"})),this}patch(e,t){return this.routes.push(j({url:e,handler:t,method:"PATCH"})),this}head(e,t){return this.routes.push(j({url:e,handler:t,method:"HEAD"})),this}all(e,t){for(const r of P)this.routes.push(j({url:e,handler:t,method:r}));return this}use(e){return this.middleware.push({handler:e}),this}handle(e,t){e.app=this;const o=m(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=y(e),e.range=x(e),e.xhr=H(e),e.hostname=C(e),t.app=this,t.header=t.set=E(0,t),t.send=A(e,t),t.json=T(0,t),t.status=q(0,t),t.cookie=b(e,t),t.clearCookie=S(e,t);for(const o of this.routes){const{url:s,method:n,handler:a}=o;t.writableEnded||e.method===n&&s&&e.url&&r(s).pattern.test(e.url)&&(e.params=f(e.url,s),e.route=g(this,a),t.statusCode=200,a(e,t))}let n=this.middleware.filter(e=>"logger"!==e.handler.name);n.push({handler:this.noMatchHandler});const a=this.middleware.find(e=>"logger"===e.handler.name);a&&n.push(a),n.map(({handler:r})=>{r(e,t)})}listen(e,r=(()=>console.log(`Started on http://${o}:${e}`)),o="localhost",s){return t.createServer((e,t)=>{this.handle(e,t)}).listen(e,o,s,r)}},exports.METHODS=P,exports.checkIfXMLHttpRequest=H,exports.clearCookie=S,exports.getHeader=y,exports.getHostname=C,exports.getIP=e=>n(e,h),exports.getProtocol=m,exports.getQueryParams=c,exports.getRangeFromHeader=x,exports.getRouteFromApp=g,exports.getURLParams=f,exports.json=T,exports.notFound=O,exports.onError=(e,r,o,s)=>{let n=o.statusCode=e.code||e.status||500;"string"==typeof e||Buffer.isBuffer(e)?o.end(e):o.end(e.message||t.STATUS_CODES[n])},exports.send=A,exports.setCookie=b,exports.setHeader=E,exports.setLocationHeader=(e,t)=>r=>{let o=r;return"back"===r&&(o=e.get("Referrer")||"/"),t.setHeader("Location",a(o))},exports.status=q; |
{ | ||
"name": "@tinyhttp/app", | ||
"version": "0.1.21", | ||
"description": "tinyhttp core", | ||
"homepage": "https://github.com/talentlessguy/tinyhttp", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/talentlessguy/tinyhttp.git", | ||
"directory": "packages/app" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.esm.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"keywords": [ | ||
"tinyhttp", | ||
"node.js", | ||
"web framework", | ||
"web", | ||
"backend" | ||
], | ||
"author": "v1rtl", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@tinyhttp/cookie": "^0.0.5", | ||
"@tinyhttp/cookie-signature": "^0.0.4", | ||
"@tinyhttp/etag": "^0.1.15", | ||
"content-type": "^1.0.4", | ||
"encodeurl": "^1.0.2", | ||
"mime": "^2.4.6", | ||
"proxy-addr": "^2.0.6", | ||
"range-parser": "^1.2.1", | ||
"regexparam": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/content-type": "^1.1.3", | ||
"@types/encodeurl": "^1.0.0", | ||
"@types/mime": "^2.0.2", | ||
"@types/proxy-addr": "^2.0.0", | ||
"@types/range-parser": "^1.2.3" | ||
} | ||
"name": "@tinyhttp/app", | ||
"version": "0.1.22", | ||
"description": "tinyhttp core", | ||
"homepage": "https://github.com/talentlessguy/tinyhttp", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/talentlessguy/tinyhttp.git", | ||
"directory": "packages/app" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.esm.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"keywords": [ | ||
"tinyhttp", | ||
"node.js", | ||
"web framework", | ||
"web", | ||
"backend" | ||
], | ||
"author": "v1rtl", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@tinyhttp/cookie": "^0.0.5", | ||
"@tinyhttp/cookie-signature": "workspace:^0.0.4", | ||
"@tinyhttp/etag": "^0.1.15", | ||
"content-type": "^1.0.4", | ||
"encodeurl": "^1.0.2", | ||
"mime": "^2.4.6", | ||
"proxy-addr": "^2.0.6", | ||
"range-parser": "^1.2.1", | ||
"regexparam": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/content-type": "^1.1.3", | ||
"@types/encodeurl": "^1.0.0", | ||
"@types/mime": "^2.0.2", | ||
"@types/proxy-addr": "^2.0.0", | ||
"@types/range-parser": "^1.2.3" | ||
} | ||
} |
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
132
18288
5
- Removed@tinyhttp/cookie-signature@0.0.4(transitive)