🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

fast-koa-router

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-koa-router - npm Package Compare versions

Comparing version

to
1.2.0

.nc_history

@@ -41,1 +41,37 @@ exports['Middleware route should be called and state should be changed 1'] = {

}
exports['Middleware it also exports routes 1'] = {
"policy": {
"/foo/bar/3": {
"middleware": [
null
]
},
"/foo": {
"/bar": {
"/3": {
"middleware": [
null
]
}
}
}
},
"get": {
"/foo/:id/3": {
"middleware": [
null
]
},
"/foo": {
"/_VAR_": {
"paramName": "id",
"/3": {
"middleware": [
null
]
}
}
}
}
}

2

.nyc_output/processinfo/index.json

@@ -1,1 +0,1 @@

{"processes":{"b340204d-7060-48d4-aadb-c310221489e6":{"parent":null,"children":[]}},"files":{"/Users/nkostoulas/dev/fast-koa-router/src/middleware.ts":["b340204d-7060-48d4-aadb-c310221489e6"],"/Users/nkostoulas/dev/fast-koa-router/src/router.ts":["b340204d-7060-48d4-aadb-c310221489e6"],"/Users/nkostoulas/dev/fast-koa-router/src/parse.ts":["b340204d-7060-48d4-aadb-c310221489e6"]},"externalIds":{}}
{"processes":{"5f4e8164-ec0d-495e-be63-07196c4d994a":{"parent":null,"children":[]}},"files":{"/Users/nkostoulas/dev/fast-koa-router/src/middleware.ts":["5f4e8164-ec0d-495e-be63-07196c4d994a"],"/Users/nkostoulas/dev/fast-koa-router/src/router.ts":["5f4e8164-ec0d-495e-be63-07196c4d994a"],"/Users/nkostoulas/dev/fast-koa-router/src/parse.ts":["5f4e8164-ec0d-495e-be63-07196c4d994a"]},"externalIds":{}}

@@ -1,1 +0,13 @@

export declare const router: (routes: any) => (ctx: any, next: any) => Promise<void>;
export declare const router: (routes: any) => {
(ctx: any, next: any): Promise<void>;
routes: {
[key: string]: any;
};
matching: (path: any, method?: string) => {
ctx: {
path: any;
method: string;
};
middlewares: any[];
};
};

@@ -5,15 +5,27 @@ "use strict";

const makeArray = args => (Array.isArray(args) ? args : [args]);
const getMatch = router => function matching(path, method = 'GET') {
const ctx = { path, method };
const middlewares = getMiddleware(router, ctx);
return { ctx, middlewares };
};
exports.router = routes => {
const router = new router_1.Router(routes);
return async function fastKoaRouter(ctx, next) {
const policy = router.getPolicy(ctx);
const middleware = router.getMiddlewareAndSetState(ctx);
const middlewares = [];
if (policy)
middlewares.push(...makeArray(policy));
if (middleware)
middlewares.push(...makeArray(middleware));
async function fastKoaRouter(ctx, next) {
const middlewares = getMiddleware(router, ctx);
await middlewares.reduceRight((middleware, r) => () => r(ctx, middleware), next)();
};
}
fastKoaRouter.routes = router.routes;
fastKoaRouter.matching = getMatch(router);
return fastKoaRouter;
};
function getMiddleware(router, ctx) {
const policy = router.getPolicy(ctx);
const middleware = router.getMiddlewareAndSetState(ctx);
const middlewares = [];
if (policy)
middlewares.push(...makeArray(policy));
if (middleware)
middlewares.push(...makeArray(middleware));
return middlewares;
}
//# sourceMappingURL=middleware.js.map
{
"name": "fast-koa-router",
"version": "1.1.0",
"version": "1.2.0",
"description": "",

@@ -37,14 +37,14 @@ "main": "build/middleware.js",

"devDependencies": {
"@koa/router": "^8.0.8",
"@types/mocha": "^5.2.6",
"@types/node": "^11.13.8",
"koa": "^2.6.1",
"koa-router": "^7.4.0",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"snap-shot-it": "^7.8.0",
"source-map-support": "^0.5.12",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
"koa": "^2.11.0",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"snap-shot-it": "^7.9.2",
"source-map-support": "^0.5.16",
"ts-node": "^8.6.2",
"tslint": "^6.0.0",
"typescript": "^3.8.2"
}
}
# Fast Koa Router
It uses a simple routes json object. Routes order does not matter. Performance does not degrade with routes length.
## Installation

@@ -97,2 +99,27 @@

### Debugging in console
```js
node
> const { router } = require('fast-koa-router');
> const route = router(routes);
> route.matching('/nested/path');
{
ctx: {
path: '/nested/path',
method: 'GET',
params: {},
_matchedRoute: '/nested/path'
},
middlewares: [
[AsyncFunction: '/nested/path'], // policy
[AsyncFunction: '/'], // prefix route
[AsyncFunction: '/nested/path'] // get route
]
}
> route.routes
// contains the compiled routes
```
## Star symbol

@@ -118,3 +145,3 @@

Note that star symboly is only supported after version 1.1.0 and only when used in the end of a route.
Note that star symbol is only supported after version 1.1.0 and only when used in the end of a route.

@@ -142,1 +169,22 @@ There is no reason to use it in prefix routes. Prefix routes will always match get, post, delete, patch, put urls if they use the same prefix.

However complex regex matching is not supported.
## Benchmark
Performances tests existing in this codebase and comparing fast-koa-router with @koa/router.
To start fast-koa-router example:
```
node performance-test/router/server.js
```
To start @koa/router example:
```
node performance-test/koa-router/server.js
```
Metrics have been taken using ab:
```
ab -k -n 1000000 -c 100 localhost:8080/api/v1/1/2
```
![image](https://user-images.githubusercontent.com/1398718/75097736-eace0300-55b6-11ea-850e-6c8c62593c07.png)
import { router } from '../src/middleware';
import * as assert from 'assert';

@@ -114,2 +115,73 @@ describe('Middleware', function() {

});
it('it also exports routes', async function() {
const ctx = { path: '/foo/bar/not-found', method: 'GET' };
const r = {
get: {
'/foo/:id/3': async function(ctx) {
ctx.body = await 'body';
}
},
policy: {
'/foo/bar/3': async function(ctx, next) {
(ctx as any).policy = true;
await next();
}
}
};
const middleware = router(r);
snapshot(middleware.routes);
});
it('it also exports matching', async function() {
const ctx = { path: '/foo/bar/not-found', method: 'GET' };
const r = {
get: {
'/foo/:id/3': async function foo(ctx) {
ctx.body = await 'body';
}
},
policy: {
'/foo/bar/3': async function(ctx, next) {
(ctx as any).policy = true;
await next();
}
}
};
const middleware = router(r);
assert.deepEqual(middleware.matching('/foo/1/3'), {
ctx: {
_matchedRoute: '/foo/:id/3',
method: 'GET',
params: {
id: '1'
},
path: '/foo/1/3'
},
middlewares: [r.get['/foo/:id/3']]
});
assert.deepEqual(middleware.matching('/foo/bar/3'), {
ctx: {
_matchedRoute: '/foo/:id/3',
method: 'GET',
params: {
id: 'bar'
},
path: '/foo/bar/3'
},
middlewares: [r.policy['/foo/bar/3'], r.get['/foo/:id/3']]
});
assert.deepEqual(middleware.matching('/foo/test'), {
ctx: {
_matchedRoute: undefined,
method: 'GET',
params: {},
path: '/foo/test'
},
middlewares: []
});
});
});

Sorry, the diff of this file is not supported yet