Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tinyhttp/app

Package Overview
Dependencies
Maintainers
1
Versions
305
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tinyhttp/app - npm Package Compare versions

Comparing version 0.1.35 to 0.1.36

10

dist/index.d.ts

@@ -10,7 +10,7 @@ /// <reference types="node" />

declare const status: (_req: Request, res: Response) => (status: number) => Response;
declare const setCookie: (req: Request, res: Response) => (name: string, value: string | object, options?: SerializeOptions & Partial<{
declare const setCookie: (req: Request, res: Response) => (name: string, value: string | Record<string, unknown>, options?: SerializeOptions & Partial<{
signed: boolean;
}>) => Response;
declare const clearCookie: (req: Request, res: Response) => (name: string, options?: SerializeOptions) => Response;
declare const setHeader: (_req: Request, res: Response) => (field: string | object, val: string | any[]) => Response;
declare const setHeader: (_req: Request, res: Response) => (field: string | Record<string, string | number | string[]>, val: string | any[]) => Response;
declare const setLocationHeader: (req: Request, res: Response) => (url: string) => Response;

@@ -24,4 +24,4 @@ declare const getResponseHeader: (_req: Request, res: Response) => (field: string) => string | number | string[];

app: App;
header(field: string | object, val: string | any[]): Response;
set(field: string | object, val: string | any[]): Response;
header(field: string | Record<string, unknown>, val: string | any[]): Response;
set(field: string | Record<string, unknown>, val: string | any[]): Response;
get(field: string): string | number | string[];

@@ -32,3 +32,3 @@ send(body: unknown): Response;

sendStatus(statusCode: number): Response;
cookie(name: string, value: string | object, options?: SerializeOptions & Partial<{
cookie(name: string, value: string | Record<string, unknown>, options?: SerializeOptions & Partial<{
signed: boolean;

@@ -35,0 +35,0 @@ }>): Response;

@@ -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]}},T=e=>(t,r)=>e.headers[t]=r,C=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){var 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)=>{let 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},v=(e,t)=>e=>(t.statusCode=e,t),B=(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 B(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=T(t),t.range=C(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=v(0,r),r.sendStatus=G(0,r),r.location=R(t,r),r.links=F(0,r),r.cookie=B(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,a=e.length-1;const 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,C as getRangeFromHeader,H as getRequestHeader,X as getResponseHeader,g as getRouteFromApp,y as getURLParams,P as json,D as send,G as sendStatus,B as setCookie,N as setHeader,F as setLinksHeader,R as setLocationHeader,T as setRequestHeader,v 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"@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]}},T=e=>(t,r)=>e.headers[t]=r,C=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=T(t),t.range=C(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,C 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,T as setRequestHeader,B 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("@foxify/fresh")),i=e(require("accepts")),d=require("@tinyhttp/cookie-signature"),p=e(require("mime-types")),h=e(require("@tinyhttp/cookie")),l=require("content-type"),u=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||[])),f=(e="/")=>s.parse(e,!0).query,g=(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)),y=(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,s=r.indexOf(",");return-1!==s?r.substring(0,s).trim():r.trim()},x=e=>t=>{const r=t.toLowerCase();switch(r){case"referer":case"referrer":return e.headers.referrer||e.headers.referer;default:return e.headers[r]}},H=e=>(t,r)=>e.headers[t]=r,w=e=>(t,r)=>{const s=e.get("Range");if(s)return n(t,s,r)},T=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,s=t.indexOf(":",r);return-1!==s?t.substring(0,s):t}},S=(e,t)=>{const r=e.method,s=t.statusCode;if("GET"!==r&&"HEAD"!==r)return!1;if(s>=200&&s<300||304===s){const r={etag:t.get("ETag"),"last-modified":t.get("Last-Modified")};return a(e.headers,r)}return!1},E=e=>(...t)=>i(e).types(t),A=(e,r,s)=>{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])},k=e=>({path:t,handler:r,method:s,handlers:n,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:s,type:o}),i=n.map(e=>({handler:e}));for(const t of[a,...i])e.push({...t,type:o})};class O{get(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"GET",type:"route"}),this}post(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"POST",type:"route"}),this}put(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"PUT",type:"route"}),this}patch(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"PATCH",type:"route"}),this}head(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"HEAD",type:"route"}),this}delete(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"DELETE",type:"route"}),this}options(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"OPTIONS",type:"route"}),this}all(e,t,...r){for(const s of["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"])k(this.middleware)({path:e,handler:t,method:s,handlers:r,type:"route"});return this}use(e,t,...r){return k(this.middleware)({path:e,handler:"string"==typeof e?t:e,handlers:r,type:"mw"}),this}}const q=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return u(r,{weak:!0})};const b=(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),j=(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=l.parse(e);return r.parameters.charset=t,l.format(r)}(e,"utf-8"))}let n;return!t.getHeader("etag")&&(n=q(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"):b(0,t)(s,"utf8"):t.end(s,"utf8"),t},L=(e,t)=>e=>(t.statusCode=e,t),P=(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 i="object"==typeof s?"j:"+JSON.stringify(s):String(s);return a&&(i="s:"+d.sign(i,o)),n.maxAge&&(n.expires=new Date(Date.now()+n.maxAge),n.maxAge/=1e3),null==n.path&&(n.path="/"),t.setHeader("Set-Cookie",h.serialize(r,String(i),n)),t},R=(e,t)=>(r,s)=>{const n=Object.assign({},{expires:new Date(1),path:"/"},s);return P(e,t)(r,"",n)},D=/;\s*charset\s*=/,v=(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(!D.test(s)){const e=p.lookup(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},M=(e,t)=>r=>{let s=r;return"back"===r&&(s=e.get("Referrer")||"/"),t.setHeader("Location",encodeURIComponent(s)),t},B=(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(", "))},U=(e,r)=>e=>{const s=t.STATUS_CODES[e]||String(e);return r.statusCode=e,r.set("Content-Type","text/plain"),r.send(s)},I=e=>(t,r)=>{r.get=B(0,r),t.app=e;const s=m(t),n="https"===s;t.protocol=s,t.secure=n,t.connection=Object.assign(t.socket,{encrypted:n}),t.query=f(t.url),t.fresh=S(t,r),t.stale=!t.fresh,t.get=x(t),t.set=H(t),t.range=w(t),t.accepts=E(t),t.xhr=T(t),t.hostname=C(t),r.app=e,r.header=r.set=v(0,r),r.send=j(t,r),r.json=b(0,r),r.status=L(0,r),r.sendStatus=U(0,r),r.location=M(t,r),r.links=F(0,r),r.cookie=P(t,r),r.clearCookie=R(t,r)},N=e=>async(t,r,s)=>{"AsyncFunction"===e[Symbol.toStringTag]?await e(t,r,s):e(t,r,s)};exports.App=class extends O{constructor(e={}){super(),this.locals=Object.create(null),this.middleware=[],this.onError=(null==e?void 0:e.onError)||A,this.noMatchHandler=(null==e?void 0:e.noMatchHandler)||this.onError.bind(null,{code:404})}async handler(e,t,s){I(this)(t,s);const n={handler:this.noMatchHandler,type:"mw",path:"/"};e.includes(n)||e.push(n);let o=0,a=e.length-1;const i=e=>{e?this.onError(e,t,s):p()},d=e=>async(t,s,n)=>{const{path:o,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(o).pattern.test(a)?(t.params=g(t.url,o),t.route=y(this,i),s.statusCode=200,N(i)(t,s,n)):p()}}else t.url.startsWith(o)?N(i)(t,s,n):p()};1===e.length&&d(e[0])(t,s);const p=()=>{s.writableEnded||o<a&&d(e[o++])(t,s,i)};p()}listen(e,r,s="localhost",n){return t.createServer((e,t)=>{this.handler(this.middleware,e,t)}).listen(e,s,n,r)}},exports.Router=O,exports.applyHandler=N,exports.checkIfXMLHttpRequest=T,exports.clearCookie=R,exports.extendMiddleware=I,exports.getAccepts=E,exports.getFreshOrStale=S,exports.getHostname=C,exports.getIP=e=>o(e,c),exports.getProtocol=m,exports.getQueryParams=f,exports.getRangeFromHeader=w,exports.getRequestHeader=x,exports.getResponseHeader=B,exports.getRouteFromApp=y,exports.getURLParams=g,exports.json=b,exports.send=j,exports.sendStatus=U,exports.setCookie=P,exports.setHeader=v,exports.setLinksHeader=F,exports.setLocationHeader=M,exports.setRequestHeader=H,exports.status=L;
"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("@foxify/fresh")),i=e(require("accepts")),d=require("@tinyhttp/cookie-signature"),p=e(require("mime-types")),h=e(require("@tinyhttp/cookie")),l=require("content-type"),u=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||[])),f=(e="/")=>s.parse(e,!0).query,g=(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)),y=(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,s=r.indexOf(",");return-1!==s?r.substring(0,s).trim():r.trim()},x=e=>t=>{const r=t.toLowerCase();switch(r){case"referer":case"referrer":return e.headers.referrer||e.headers.referer;default:return e.headers[r]}},H=e=>(t,r)=>e.headers[t]=r,w=e=>(t,r)=>{const s=e.get("Range");if(s)return n(t,s,r)},T=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)return;const r="["===t[0]?t.indexOf("]")+1:0,s=t.indexOf(":",r);return-1!==s?t.substring(0,s):t},S=(e,t)=>{const r=e.method,s=t.statusCode;if("GET"!==r&&"HEAD"!==r)return!1;if(s>=200&&s<300||304===s){const r={etag:t.get("ETag"),"last-modified":t.get("Last-Modified")};return a(e.headers,r)}return!1},E=e=>(...t)=>i(e).types(t),A=(e,r,s)=>{const 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])},k=e=>({path:t,handler:r,method:s,handlers:n,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:s,type:o}),i=n.map(e=>({handler:e}));for(const t of[a,...i])e.push({...t,type:o})};class O{get(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"GET",type:"route"}),this}post(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"POST",type:"route"}),this}put(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"PUT",type:"route"}),this}patch(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"PATCH",type:"route"}),this}head(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"HEAD",type:"route"}),this}delete(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"DELETE",type:"route"}),this}options(e,t,...r){return k(this.middleware)({path:e,handler:t,handlers:r,method:"OPTIONS",type:"route"}),this}all(e,t,...r){for(const s of["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"])k(this.middleware)({path:e,handler:t,method:s,handlers:r,type:"route"});return this}use(e,t,...r){return k(this.middleware)({path:e,handler:"string"==typeof e?t:e,handlers:r,type:"mw"}),this}}const q=(e,t)=>{const r=Buffer.isBuffer(e)?e:Buffer.from(e,t);return u(r,{weak:!0})};const b=(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),j=(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=l.parse(e);return r.parameters.charset=t,l.format(r)}(e,"utf-8"))}let n;return!t.getHeader("etag")&&(n=q(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"):b(0,t)(s,"utf8"):t.end(s,"utf8"),t},L=(e,t)=>e=>(t.statusCode=e,t),P=(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 i="object"==typeof s?"j:"+JSON.stringify(s):String(s);return a&&(i="s:"+d.sign(i,o)),n.maxAge&&(n.expires=new Date(Date.now()+n.maxAge),n.maxAge/=1e3),null==n.path&&(n.path="/"),t.setHeader("Set-Cookie",h.serialize(r,String(i),n)),t},R=(e,t)=>(r,s)=>{const n=Object.assign({},{expires:new Date(1),path:"/"},s);return P(e,t)(r,"",n)},D=/;\s*charset\s*=/,v=(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(!D.test(s)){const e=p.lookup(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},M=(e,t)=>r=>{let s=r;return"back"===r&&(s=e.get("Referrer")||"/"),t.setHeader("Location",encodeURIComponent(s)),t},B=(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(", "))},U=(e,r)=>e=>{const s=t.STATUS_CODES[e]||String(e);return r.statusCode=e,r.set("Content-Type","text/plain"),r.send(s)},I=e=>(t,r)=>{r.get=B(0,r),t.app=e;const s=m(t),n="https"===s;t.protocol=s,t.secure=n,t.connection=Object.assign(t.socket,{encrypted:n}),t.query=f(t.url),t.fresh=S(t,r),t.stale=!t.fresh,t.get=x(t),t.set=H(t),t.range=w(t),t.accepts=E(t),t.xhr=T(t),t.hostname=C(t),r.app=e,r.header=r.set=v(0,r),r.send=j(t,r),r.json=b(0,r),r.status=L(0,r),r.sendStatus=U(0,r),r.location=M(t,r),r.links=F(0,r),r.cookie=P(t,r),r.clearCookie=R(t,r)},N=e=>async(t,r,s)=>{"AsyncFunction"===e[Symbol.toStringTag]?await e(t,r,s):e(t,r,s)};exports.App=class extends O{constructor(e={}){super(),this.locals=Object.create(null),this.middleware=[],this.onError=(null==e?void 0:e.onError)||A,this.noMatchHandler=(null==e?void 0:e.noMatchHandler)||this.onError.bind(null,{code:404})}async handler(e,t,s){I(this)(t,s);const n={handler:this.noMatchHandler,type:"mw",path:"/"};e.includes(n)||e.push(n);let o=0;const a=e.length-1,i=e=>{e?this.onError(e,t,s):p()},d=e=>async(t,s,n)=>{const{path:o,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(o).pattern.test(a)?(t.params=g(t.url,o),t.route=y(this,i),s.statusCode=200,N(i)(t,s,n)):p()}}else t.url.startsWith(o)?N(i)(t,s,n):p()};1===e.length&&d(e[0])(t,s);const p=()=>{s.writableEnded||o<a&&d(e[o++])(t,s,i)};p()}listen(e,r,s="localhost",n){return t.createServer((e,t)=>{this.handler(this.middleware,e,t)}).listen(e,s,n,r)}},exports.Router=O,exports.applyHandler=N,exports.checkIfXMLHttpRequest=T,exports.clearCookie=R,exports.extendMiddleware=I,exports.getAccepts=E,exports.getFreshOrStale=S,exports.getHostname=C,exports.getIP=e=>o(e,c),exports.getProtocol=m,exports.getQueryParams=f,exports.getRangeFromHeader=w,exports.getRequestHeader=x,exports.getResponseHeader=B,exports.getRouteFromApp=y,exports.getURLParams=g,exports.json=b,exports.send=j,exports.sendStatus=U,exports.setCookie=P,exports.setHeader=v,exports.setLinksHeader=F,exports.setLocationHeader=M,exports.setRequestHeader=H,exports.status=L;
{
"name": "@tinyhttp/app",
"version": "0.1.35",
"version": "0.1.36",
"description": "tinyhttp core",

@@ -34,3 +34,3 @@ "homepage": "https://github.com/talentlessguy/tinyhttp",

"@foxify/fresh": "^1.1.0",
"@tinyhttp/cookie": "0.0.10",
"@tinyhttp/cookie": "0.0.11",
"@tinyhttp/cookie-signature": "0.0.6",

@@ -37,0 +37,0 @@ "@tinyhttp/etag": "0.1.23",

@@ -69,1 +69,5 @@ # tinyhttp

Search and explore the full list at [middleware search page](https://tinyhttp.v1rtl.site/mw).
## License
MIT © [v1rtl](https://v1rtl.site)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc