Comparing version 0.0.8 to 0.0.9
@@ -23,7 +23,6 @@ "use strict"; | ||
function staticFiles(options = {}) { | ||
const config = setupConfig(options); | ||
const config = getConfig(options); | ||
return function ({ req, res, params }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const wildcards = params.wildcards || []; | ||
const asset = path_1.default.join(config.dir, wildcards[wildcards.length - 1]); | ||
const asset = path_1.default.join(config.dir, params['**'] || '/'); | ||
if (isExcluded(config.exclude, asset)) { | ||
@@ -46,3 +45,3 @@ throw ex_1.default.NotFound(); | ||
} | ||
function setupConfig(options) { | ||
function getConfig(options) { | ||
const config = Object.assign({}, DEFAULT_OPTIONS); | ||
@@ -49,0 +48,0 @@ if (options.dir) { |
@@ -11,2 +11,3 @@ /// <reference types="node" /> | ||
(req: IncomingMessage, res: ServerResponse, override?: ConfigInput): void; | ||
list: () => string[]; | ||
} | ||
@@ -13,0 +14,0 @@ export declare type Bundle = { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -18,3 +37,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const validate_1 = require("./util/validate"); | ||
const processor_1 = __importDefault(require("./processor")); | ||
const processor_1 = __importStar(require("./processor")); | ||
const DEFAULT_OPTIONS = { | ||
@@ -47,2 +66,6 @@ logger: console, | ||
} | ||
function list() { | ||
return (0, processor_1.listRoutes)(_routes); | ||
} | ||
app.list = list; | ||
Object.assign(app, (0, route_scope_1.default)(_routes, { | ||
@@ -49,0 +72,0 @@ pathname: '/', |
@@ -5,1 +5,2 @@ import { Route } from './util/route-scope'; | ||
export default processor; | ||
export declare function listRoutes(routes: Route[]): string[]; |
@@ -15,3 +15,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.listRoutes = void 0; | ||
const find_route_1 = __importDefault(require("./util/find-route")); | ||
const path_params_1 = require("./util/path-params"); | ||
const sanitize_1 = require("./util/sanitize"); | ||
@@ -26,3 +28,3 @@ const render_1 = __importDefault(require("./render")); | ||
const route = (0, find_route_1.default)(routes, req.method, pathname); | ||
Object.assign(bundle.params, extractParams(route.pathname, pathname)); | ||
Object.assign(bundle.params, (0, path_params_1.extractParams)(route.pathname, pathname)); | ||
const payload = yield lifecycle(route, bundle); | ||
@@ -43,23 +45,2 @@ yield (0, render_1.default)(config, payload, bundle); | ||
exports.default = processor; | ||
function extractParams(srcPathname, reqPathname) { | ||
const params = {}; | ||
const srcParts = srcPathname.split('/'); | ||
const reqParts = reqPathname.split('/'); | ||
for (let i = 0; i < srcParts.length; i++) { | ||
if (srcParts[i] === '**') { | ||
params.wildcards = params.wildcards || []; | ||
params.wildcards.push(reqParts.slice(i).join('/')); | ||
return params; | ||
} | ||
if (srcParts[i] === '*') { | ||
params.wildcards = params.wildcards || []; | ||
params.wildcards.push(reqParts[i]); | ||
return params; | ||
} | ||
if (srcParts[i].startsWith(':')) { | ||
params[srcParts[i].substr(1)] = reqParts[i]; | ||
} | ||
} | ||
return params; | ||
} | ||
function lifecycle(route, bundle) { | ||
@@ -75,1 +56,11 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
function listRoutes(routes) { | ||
return [...routes].sort(routeSorter).map(formatRoute); | ||
} | ||
exports.listRoutes = listRoutes; | ||
function routeSorter(a, b) { | ||
return (a.pathname + a.method).localeCompare(b.pathname + b.method); | ||
} | ||
function formatRoute({ method, pathname }) { | ||
return `${method} ${pathname}`; | ||
} |
@@ -37,3 +37,3 @@ "use strict"; | ||
const renderer = renderers[key] || DEFAULT_RENDERERS[key]; | ||
if (typeof renderer !== 'function') { | ||
if (!renderer) { | ||
throw ex_1.default.InternalServerError('Renderer not found', { contentType }); | ||
@@ -40,0 +40,0 @@ } |
@@ -7,2 +7,4 @@ "use strict"; | ||
const ex_1 = __importDefault(require("./ex")); | ||
const path_params_1 = require("./path-params"); | ||
const processor_1 = require("../processor"); | ||
function findRoute(routes, method, pathname) { | ||
@@ -16,3 +18,3 @@ let result = routes.find(routeMatch(method || 'GET', pathname)); | ||
request: { method, pathname }, | ||
routes: [...routes].sort(routeSorter).map(formatRoute) | ||
routes: (0, processor_1.listRoutes)(routes) | ||
}); | ||
@@ -28,26 +30,4 @@ } | ||
} | ||
return comparePathnames(route.pathname, pathname); | ||
return (0, path_params_1.comparePathnames)(route.pathname, pathname); | ||
}; | ||
} | ||
function comparePathnames(srcPathname, reqPathname) { | ||
const srcParts = srcPathname.split('/').filter(part => !!part); | ||
const reqParts = reqPathname.split('/').filter(part => !!part); | ||
for (let i = 0; i < srcParts.length; i++) { | ||
if (srcParts[i] === '**') | ||
return true; | ||
if (srcParts[i] === '*') | ||
continue; | ||
if (srcParts[i].startsWith(':')) | ||
continue; | ||
if (srcParts[i] === reqParts[i]) | ||
continue; | ||
return false; | ||
} | ||
return srcParts.length === reqParts.length; | ||
} | ||
function routeSorter(a, b) { | ||
return (a.pathname + a.method).localeCompare(b.pathname + b.method); | ||
} | ||
function formatRoute({ method, pathname }) { | ||
return `${method} ${pathname}`; | ||
} |
@@ -24,3 +24,3 @@ "use strict"; | ||
if (typeof config.renderers[key] !== 'function') { | ||
throw new Error('Method ' + key + ' missing on renderers'); | ||
throw new Error('Renderer ' + key + ' must be a function'); | ||
} | ||
@@ -27,0 +27,0 @@ } |
{ | ||
"name": "kequapp", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Versatile, non-intrusive, tiny webapp framework", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
@@ -300,3 +300,3 @@ # Kequapp | ||
A rudimentary `staticFiles()` handler can be used to deliver files relative to your project directory. This utility makes use of the `wildcards` parameter as defined by your route to build a valid path. | ||
A rudimentary `staticFiles()` handler can be used to deliver files relative to your project directory. This utility makes use of the `**` parameter as defined by your route to build a valid path. | ||
@@ -303,0 +303,0 @@ By default the `/public` directory is used. |
67508
49
1463