Comparing version 0.0.22 to 0.0.23
/// <reference types="node" /> | ||
import { IncomingMessage } from 'http'; | ||
import { Config } from '../utils/config'; | ||
export interface IGetBody { | ||
@@ -45,2 +46,3 @@ (format: BodyOptions & { | ||
postProcess?(body: BodyJson): BodyJson; | ||
maxPayloadSize?: number; | ||
}; | ||
@@ -50,3 +52,3 @@ export declare type BodyJson = { | ||
}; | ||
declare function createGetBody(req: IncomingMessage, maxPayloadSize?: number): IGetBody; | ||
declare function createGetBody(req: IncomingMessage, config: Config): IGetBody; | ||
export default createGetBody; |
@@ -46,3 +46,3 @@ "use strict"; | ||
}); | ||
function createGetBody(req, maxPayloadSize) { | ||
function createGetBody(req, config) { | ||
let _body; | ||
@@ -53,3 +53,3 @@ return function (options = {}) { | ||
if (_body === undefined) { | ||
const data = yield (0, stream_reader_1.default)(getStream(req), maxPayloadSize); | ||
const data = yield (0, stream_reader_1.default)(getStream(req), getMaxPayloadSize(config, options)); | ||
const headers = (0, sanitize_1.getHeaders)(req, ['Content-Type', 'Content-Disposition']); | ||
@@ -95,4 +95,10 @@ _body = { headers, data }; | ||
} | ||
function getMaxPayloadSize(config, options) { | ||
if (typeof options.maxPayloadSize === 'number' && options.maxPayloadSize > 0) { | ||
return options.maxPayloadSize; | ||
} | ||
return config.maxPayloadSize; | ||
} | ||
function clone(body) { | ||
return Object.assign(Object.assign({}, body), { headers: Object.assign({}, body.headers) }); | ||
} |
@@ -7,3 +7,3 @@ "use strict"; | ||
const ex_1 = __importDefault(require("../utils/ex")); | ||
function streamReader(stream, maxPayloadSize) { | ||
function streamReader(stream, maxPayloadSize = 0) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -26,3 +26,3 @@ const chunks = []; | ||
function verifyPayload() { | ||
if (maxPayloadSize) { | ||
if (maxPayloadSize > 0) { | ||
const payloadSize = chunks.reduce((acc, chunk) => acc + chunk.length, 0); | ||
@@ -29,0 +29,0 @@ if (payloadSize > maxPayloadSize) { |
@@ -33,3 +33,3 @@ "use strict"; | ||
params: {}, | ||
getBody: (0, create_get_body_1.default)(req, config.maxPayloadSize), | ||
getBody: (0, create_get_body_1.default)(req, config), | ||
routes: Object.assign({}, routes), | ||
@@ -36,0 +36,0 @@ logger: config.logger |
@@ -13,4 +13,4 @@ import { Route } from './create-router'; | ||
export default createRoutes; | ||
export declare function findRoute(routes: Route[], method: string | undefined, pathname: string): Route; | ||
export declare function findRoute(routes: Route[], method: string | undefined, pathname: string, autoHead: boolean): Route; | ||
export declare function findRoutes(routes: Route[], pathname: string): Route[]; | ||
export declare function listRoutes(routes: Route[]): Route[]; |
@@ -26,6 +26,6 @@ "use strict"; | ||
exports.default = createRoutes; | ||
function findRoute(routes, method, pathname) { | ||
function findRoute(routes, method, pathname, autoHead) { | ||
const result = findRoutes(routes, pathname); | ||
let match = result.find(route => route.method === (method || 'GET')); | ||
if (!match && method === 'HEAD') { | ||
if (!match && autoHead && method === 'HEAD') { | ||
match = result.find(route => route.method === 'GET'); | ||
@@ -32,0 +32,0 @@ } |
@@ -21,6 +21,7 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { errorHandler, autoHead } = config; | ||
const { req, res, url, logger } = bundle; | ||
const pathname = (0, sanitize_1.sanitizePathname)(url.pathname); | ||
try { | ||
const route = (0, create_routes_1.findRoute)(routes, req.method, pathname); | ||
const route = (0, create_routes_1.findRoute)(routes, req.method, pathname, autoHead); | ||
Object.assign(bundle.params, (0, path_params_1.extractParams)(route.pathname, pathname)); | ||
@@ -31,3 +32,3 @@ const payload = yield lifecycle(route, bundle); | ||
catch (error) { | ||
const payload = yield config.errorHandler(error, bundle); | ||
const payload = yield errorHandler(error, bundle); | ||
if (bundle.res.statusCode === 500) { | ||
@@ -34,0 +35,0 @@ logger.error(error); |
@@ -6,3 +6,4 @@ import { Bundle } from '../main'; | ||
errorHandler: ConfigErrorHandler; | ||
maxPayloadSize?: number; | ||
maxPayloadSize: number; | ||
autoHead: boolean; | ||
}; | ||
@@ -14,2 +15,3 @@ export declare type ConfigInput = { | ||
maxPayloadSize?: number; | ||
autoHead?: boolean; | ||
}; | ||
@@ -16,0 +18,0 @@ export declare type ConfigRenderers = { |
@@ -18,3 +18,4 @@ "use strict"; | ||
errorHandler: error_handler_1.default, | ||
maxPayloadSize: undefined | ||
maxPayloadSize: 1e6, | ||
autoHead: true | ||
}; | ||
@@ -40,2 +41,3 @@ function setupConfig(config) { | ||
validateMaxPayloadSize(config.maxPayloadSize); | ||
validateAutoHead(config.autoHead); | ||
} | ||
@@ -80,2 +82,12 @@ exports.validateConfig = validateConfig; | ||
} | ||
if (maxPayloadSize < 0) { | ||
throw new Error('Max payload size must be a positive number'); | ||
} | ||
} | ||
function validateAutoHead(autoHead) { | ||
if (autoHead === undefined) | ||
return; | ||
if (typeof autoHead !== 'boolean') { | ||
throw new Error('Auto head must be a boolean'); | ||
} | ||
} |
{ | ||
"name": "kequapp", | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"description": "Versatile, non-intrusive, tiny webapp framework", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
@@ -19,3 +19,3 @@ # Kequapp | ||
app.route('/', () => { | ||
app.route(() => { | ||
return 'Hello world!'; | ||
@@ -81,5 +81,9 @@ }); | ||
## HEAD Requests | ||
`HEAD` requests are passed through by default so that it's matching `GET` request lifecycle is triggered. It is the responsibility of the renderer to detect a HEAD request and then pass no body in the response. These can be overridden by defining a HEAD route or disabled entirely by setting the `autoHead` configuration option to `false`. | ||
### Halting Execution | ||
Any handler can return a `payload`. Doing this halts further execution of the request and triggers rendering immediately. This is similar to interrupting the request by throwing an error or by finalizing the response. | ||
Any handler can return a `payload`. Doing this halts further execution of the request and triggers rendering immediately. This is similar to interrupting the request by throwing an error or finalizing the response. | ||
@@ -114,4 +118,4 @@ All processing halts if the response has been finalized. This is useful for example instead of rendering output you want to redirect the user to another page. | ||
| `getBody` | Function to extract params from the request body. | | ||
| `routes` | Helper methods describe your app. | | ||
| `logger` | Logger specified during setup. | | ||
| `routes` | Helper to describe available app routes. | | ||
| `logger` | Logger specified by configuration option. | | ||
@@ -228,3 +232,3 @@ ### Body | ||
| parameter | description | | ||
| ---------- | ---------------------------------------------- | | ||
| --------------- | ---------------------------------------------- | | ||
| `arrays` | Value is returned as an array. | | ||
@@ -238,3 +242,3 @@ | `required` | Value or values are not `null` or `undefined`. | | ||
Querystring values are available from the Javascript [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) object found on the `url` object. | ||
Querystring values are available from the Javascript [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) instance found on the `url` object. | ||
@@ -314,2 +318,14 @@ ```javascript | ||
### Configuration Options | ||
During instantiation of the app there are several configuration options available. | ||
| parameter | description | | ||
| ---------------- | ---------------------------------------------------------- | | ||
| `logger` | Logger to use within the app. (Default: `console`) | | ||
| `renderers` | Renderers to use within the app. | | ||
| `errorHandler` | Error handler to capture and format error responses. | | ||
| `maxPayloadSize` | Maximum payload size for client requests. (Default: `1e6`) | | ||
| `autoHead` | Automatically follow head requests. (Default: `true`) | | ||
### Static Files | ||
@@ -380,3 +396,3 @@ | ||
```javascript | ||
const { getResponse, res } = inject(app, { | ||
const { getResponse } = inject(app, { | ||
method: 'POST', | ||
@@ -394,3 +410,3 @@ url: '/user', | ||
```javascript | ||
const { getResponse, req, res } = inject(app, { | ||
const { getResponse, req } = inject(app, { | ||
method: 'POST', | ||
@@ -397,0 +413,0 @@ url: '/user', |
70467
1512
416