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.45 to 0.1.47

10

dist/index.d.ts

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

import { Ranges, Options } from "range-parser";
import { SerializeOptions } from "@tinyhttp/cookie";
import * as cookie from "@tinyhttp/cookie";
declare const json: (_req: Request, res: Response) => (body: any, ...args: any[]) => Response;
declare const send: (req: Request, res: Response) => (body: any) => Response;
declare const status: (_req: Request, res: Response) => (status: number) => Response;
declare const setCookie: (req: Request, res: Response) => (name: string, value: string | Record<string, unknown>, options?: SerializeOptions & Partial<{
declare const setCookie: (req: Request, res: Response) => (name: string, value: string | Record<string, unknown>, options?: cookie.SerializeOptions & Partial<{
signed: boolean;
}>) => Response;
declare const clearCookie: (req: Request, res: Response) => (name: string, options?: SerializeOptions) => Response;
declare const clearCookie: (req: Request, res: Response) => (name: string, options?: cookie.SerializeOptions) => Response;
declare const setHeader: (_req: Request, res: Response) => (field: string | Record<string, string | number | string[]>, val: string | any[]) => Response;

@@ -30,6 +30,6 @@ declare const setLocationHeader: (req: Request, res: Response) => (url: string) => Response;

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

@@ -36,0 +36,0 @@ links(links: {

@@ -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"es-accepts";import{sign as h}from"@tinyhttp/cookie-signature";import l from"es-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)=>new 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};
import{STATUS_CODES as e,METHODS as t,createServer as r}from"http";import n from"regexparam";import{parse as s}from"url";import o from"range-parser";import a from"proxy-addr";import i from"@foxify/fresh";import d from"es-accepts";import{sign as h}from"@tinyhttp/cookie-signature";import{lookup as l}from"es-mime-types";import{serialize as p}from"@tinyhttp/cookie";import{format as c,parse as u}from"es-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="/")=>s(e,!0).query,g=(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,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,x=e=>(t,r)=>{const n=e.get("Range");if(n)return o(t,n,r)},E=e=>"XMLHttpRequest"===e.headers["X-Requested-With"],b=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},k=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)=>new d(e).types(t),S=(t,r,n)=>{const s=n.statusCode=t.code||t.status||500;"string"==typeof t||Buffer.isBuffer(t)?n.end(t):n.end(t.message||e[s])},j=e=>({path:t,handler:r,method:n,handlers:s,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:n,type:o}),i=s.map(e=>({handler:e}));for(const t of[a,...i])e.push({...t,type:o})};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 s of t)j(this.middleware)({path:e,handler:r,method:s,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=u(e);return r.parameters.charset=t,c(r)}(e,"utf-8"))}let s;return!t.getHeader("etag")&&(s=B(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"):D(0,t)(n,"utf8"):t.end(n,"utf8"),t},P=(e,t)=>e=>(t.statusCode=e,t),q=(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 i="object"==typeof n?"j:"+JSON.stringify(n):String(n);return a&&(i="s:"+h(i,o)),s.maxAge&&(s.expires=new Date(Date.now()+s.maxAge),s.maxAge/=1e3),null==s.path&&(s.path="/"),t.setHeader("Set-Cookie",p(r,String(i),s)),t},M=(e,t)=>(r,n)=>{const s=Object.assign({},{expires:new Date(1),path:"/"},n);return q(e,t)(r,"",s)},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(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=x(e),e.accepts=O(e),e.xhr=E(e),e.hostname=b(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 s={handler:this.noMatchHandler,type:"mw",path:"/"};e.includes(s)||e.push(s);let o=0;const a=e.length-1,i=e=>{e?this.onError(e,t,r):h()},d=e=>async(t,r,s)=>{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);n(o).pattern.test(a)?(t.params=g(t.url,o),t.route=w(this,i),r.statusCode=200,U(i)(t,r,s)):h()}}else t.url.startsWith(o)?U(i)(t,r,s):h()};1===e.length&&d(e[0])(t,r);const h=()=>{r.writableEnded||o<a&&d(e[o++])(t,r,i)};h()}listen(e,t,n="localhost",s){return r((e,t)=>{this.handler(this.middleware,e,t)}).listen(e,n,s,t)}}export{W as App,L as Router,U as applyHandler,E as checkIfXMLHttpRequest,M as clearCookie,I as extendMiddleware,O as getAccepts,A as getFreshOrStale,b as getHostname,k as getIP,H as getProtocol,y as getQueryParams,x 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.45",
"version": "0.1.47",
"description": "tinyhttp core",

@@ -43,5 +43,5 @@ "type": "module",

"@tinyhttp/etag": "0.1.26",
"content-type": "^1.0.4",
"es-accepts": "^0.0.5",
"es-mime-types": "^0.0.3",
"es-accepts": "^0.0.11",
"es-content-type": "^0.0.4",
"es-mime-types": "^0.0.6",
"proxy-addr": "^2.0.6",

@@ -52,3 +52,2 @@ "range-parser": "^1.2.1",

"devDependencies": {
"@types/content-type": "^1.1.3",
"@types/proxy-addr": "^2.0.0",

@@ -55,0 +54,0 @@ "@types/range-parser": "^1.2.3"

Sorry, the diff of this file is not supported yet

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