@tinyhttp/app
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -51,6 +51,24 @@ "use strict"; | ||
http_1.createServer((req, res) => { | ||
// Define extensions | ||
res.send = (body) => response_1.send(req, res, body); | ||
res.json = (body) => response_1.json(req, res, body); | ||
/// Define extensions | ||
/* | ||
Request extensions | ||
*/ | ||
const proto = request_1.getProtocol(req); | ||
const secure = proto === 'https'; | ||
req.protocol = proto; | ||
req.secure = secure; | ||
req.connection = Object.assign(req.socket, { | ||
encrypted: secure | ||
}); | ||
req.query = request_1.getQueryParams(req.url); | ||
req.get = request_1.getHeader(req); | ||
req.range = request_1.getRangeFromHeader(req); | ||
req.xhr = request_1.checkIfXMLHttpRequest(req); | ||
req.hostname = request_1.getHostname(req); | ||
/* | ||
Response extensions | ||
*/ | ||
res.send = response_1.send(req, res); | ||
res.json = response_1.json(req, res); | ||
res.status = response_1.status(req, res); | ||
this.routes.map(({ url, method, handler }) => { | ||
@@ -60,2 +78,3 @@ if (req.method === method) { | ||
req.params = request_1.getURLParams(req.url, url); | ||
req.route = request_1.getRouteFromApp(this, handler); | ||
if (!res.writableEnded) { | ||
@@ -62,0 +81,0 @@ res.statusCode = 200; |
/// <reference types="node" /> | ||
import { IncomingMessage } from 'http'; | ||
import { ParsedUrlQuery } from 'querystring'; | ||
import parseRange, { Ranges } from 'range-parser'; | ||
import { App, Handler, Middleware } from '../app'; | ||
export declare const getQueryParams: (url?: string) => ParsedUrlQuery; | ||
@@ -9,5 +11,25 @@ export declare type URLParams = { | ||
export declare const getURLParams: (reqUrl?: string, url?: string) => URLParams; | ||
export declare const getRouteFromApp: (app: App, handler: Handler) => Middleware | undefined; | ||
export declare const compileTrust: (val: any) => any; | ||
export declare const getProtocol: (req: Request) => Protocol; | ||
export declare const getHeader: (req: Request) => (header: string) => string | string[] | undefined; | ||
export declare const getRangeFromHeader: (req: Request) => (size: number, options?: parseRange.Options | undefined) => parseRange.Ranges | -1 | -2 | undefined; | ||
export declare const checkIfXMLHttpRequest: (req: Request) => boolean; | ||
export declare const getHostname: (req: Request) => string | undefined; | ||
export declare const getIP: (req: Request) => string; | ||
export declare type Connection = IncomingMessage['socket'] & { | ||
encrypted: boolean; | ||
}; | ||
export declare type Protocol = 'http' | 'https' | string; | ||
export interface Request extends IncomingMessage { | ||
query: ParsedUrlQuery; | ||
params: URLParams; | ||
connection: Connection; | ||
route?: Middleware | undefined; | ||
protocol: Protocol; | ||
secure: boolean; | ||
xhr: boolean; | ||
hostname: string | undefined; | ||
get: (header: string) => string | string[] | undefined; | ||
range: (size: number, options?: any) => -1 | -2 | Ranges | undefined; | ||
} |
@@ -6,5 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getURLParams = exports.getQueryParams = void 0; | ||
exports.getIP = exports.getHostname = exports.checkIfXMLHttpRequest = exports.getRangeFromHeader = exports.getHeader = exports.getProtocol = exports.compileTrust = exports.getRouteFromApp = exports.getURLParams = exports.getQueryParams = void 0; | ||
const regexparam_1 = __importDefault(require("regexparam")); | ||
const url_1 = require("url"); | ||
const proxy_addr_1 = __importDefault(require("proxy-addr")); | ||
const range_parser_1 = __importDefault(require("range-parser")); | ||
exports.getQueryParams = (url = '/') => { | ||
@@ -24,1 +26,68 @@ return url_1.parse(url, true).query; | ||
}; | ||
exports.getRouteFromApp = (app, handler) => { | ||
return app.routes.find(h => h.handler.name === handler.name); | ||
}; | ||
exports.compileTrust = (val) => { | ||
if (typeof val === 'function') | ||
return val; | ||
if (val === true) { | ||
// Support plain true/false | ||
return function () { | ||
return true; | ||
}; | ||
} | ||
if (typeof val === 'number') { | ||
// Support trusting hop count | ||
return (_, i) => { | ||
if (val) { | ||
return i < val; | ||
} | ||
}; | ||
} | ||
if (typeof val === 'string') { | ||
// Support comma-separated values | ||
val = val.split(/ *, */); | ||
} | ||
return proxy_addr_1.default.compile(val || []); | ||
}; | ||
exports.getProtocol = (req) => { | ||
const proto = req.connection.encrypted ? 'https' : 'http'; | ||
if (!exports.compileTrust(req.connection.remoteAddress)) { | ||
return proto; | ||
} | ||
const header = req.headers['X-Forwarded-Proto'] || proto; | ||
const index = header.indexOf(','); | ||
return index !== -1 ? header.substring(0, index).trim() : header.trim(); | ||
}; | ||
exports.getHeader = (req) => (header) => { | ||
return req.headers[header.toLowerCase()]; | ||
}; | ||
exports.getRangeFromHeader = (req) => (size, options) => { | ||
const range = req.get('Range'); | ||
if (!range) | ||
return; | ||
return range_parser_1.default(size, range, options); | ||
}; | ||
exports.checkIfXMLHttpRequest = (req) => { | ||
if (req.headers['X-Requested-With'] === 'XMLHttpRequest') { | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
}; | ||
exports.getHostname = (req) => { | ||
let host = req.get('X-Forwarded-Host'); | ||
if (!host || !exports.compileTrust(req.connection.remoteAddress)) { | ||
host = req.get('Host'); | ||
} | ||
if (!host) | ||
return; | ||
// IPv6 literal support | ||
var offset = host[0] === '[' ? host.indexOf(']') + 1 : 0; | ||
var index = host.indexOf(':', offset); | ||
return index !== -1 ? host.substring(0, index) : host; | ||
}; | ||
exports.getIP = (req) => { | ||
return proxy_addr_1.default(req, exports.compileTrust); | ||
}; |
/// <reference types="node" /> | ||
import { ServerResponse } from 'http'; | ||
import { Request } from './request'; | ||
export declare const json: (_: Request, res: Response, body: any, ...args: any[]) => void; | ||
export declare const send: (req: Request, res: Response, body: any) => void; | ||
export declare const json: (_: Request, res: Response) => (body: any, ...args: any[]) => Response; | ||
export declare const send: (req: Request, res: Response) => (body: any) => Response; | ||
export declare const status: (_: Request, res: Response) => (status: number) => Response; | ||
export interface Response extends ServerResponse { | ||
send(body: unknown): void; | ||
json(body: unknown): void; | ||
send(body: unknown): Response; | ||
json(body: unknown): Response; | ||
status(status: number): Response; | ||
} |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.send = exports.json = void 0; | ||
exports.status = exports.send = exports.json = void 0; | ||
const content_type_1 = require("content-type"); | ||
@@ -19,3 +19,3 @@ const etag_1 = __importDefault(require("@tinyhttp/etag")); | ||
} | ||
exports.json = (_, res, body, ...args) => { | ||
exports.json = (_, res) => (body, ...args) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
@@ -28,4 +28,5 @@ if (typeof body === 'object' && body != 'null') { | ||
} | ||
return res; | ||
}; | ||
exports.send = (req, res, body) => { | ||
exports.send = (req, res) => (body) => { | ||
let bodyToSend = body; | ||
@@ -72,3 +73,3 @@ // in case of object - turn it to json | ||
else { | ||
encoding ? exports.json(req, res, bodyToSend, encoding) : exports.json(req, res, bodyToSend); | ||
encoding ? exports.json(req, res)(bodyToSend, encoding) : exports.json(req, res)(bodyToSend); | ||
} | ||
@@ -86,2 +87,7 @@ } | ||
} | ||
return res; | ||
}; | ||
exports.status = (_, res) => (status) => { | ||
res.statusCode = status; | ||
return res; | ||
}; |
{ | ||
"name": "@tinyhttp/app", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "tinyhttp core", | ||
@@ -19,9 +19,20 @@ "main": "dist/index.js", | ||
], | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"author": "v1rtl", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@tinyhttp/etag": "^0.1.0", | ||
"@tinyhttp/etag": "^0.1.3", | ||
"content-type": "^1.0.4", | ||
"proxy-addr": "^2.0.6", | ||
"range-parser": "^1.2.1", | ||
"regexparam": "^1.3.0" | ||
} | ||
}, | ||
"devDependencies": { | ||
"@types/proxy-addr": "^2.0.0", | ||
"@types/range-parser": "^1.2.3" | ||
}, | ||
"gitHead": "57c3a4b8fda3989a6bec1285f6fccd713fb991da" | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
24591
700
5
2
16
2
+ Addedproxy-addr@^2.0.6
+ Addedrange-parser@^1.2.1
+ Addedforwarded@0.2.0(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedrange-parser@1.2.1(transitive)
Updated@tinyhttp/etag@^0.1.3